How to set energy of mobile node?

41 views
Skip to first unread message

Sadik Manik

unread,
May 4, 2024, 11:47:30 PM5/4/24
to Network Simulator 2 (NS2)
Hello, I tried to create a simple energy model, but there was an error that prevented me from reading the energy.
My code is as follows:
# A 100 node example for ad-hoc simulation with AODV

# Define options
set val(chan)       Channel/WirelessChannel  ;# channel type
set val(prop) Propagation/TwoRayGround    ;# radio-propagation model
set val(netif) Phy/WirelessPhy     ;# network interface type

set val(mac) Mac/802_11     ;# MAC Type
set val(ifq) Queue/DropTail/PriQueue     ;# interface queue type
set val(ll) LL     ;# link layer type
set val(ant) Antenna/OmniAntenna     ;# antenna model
set val(ifqlen) 50     ;# max packet in ifq
set val(nn) 150     ;# number of mobilnode
set val(rp) CBRP     ;# routing protocol
set val(x)         1225     ;# X dimensions of topography
set val(y) 1225     ;# Y dimensions of topography
set val(stop) 200     ;# time of simulation end

set ns [new Simulator]
set tracefd   [open CBRP1.tr w]
set windowVsTime2 [open win.tr w]
set namtrace   [open testAODV.nam w]
set node_pos_file [open "150node.txt" w]

$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)

#set up topography object
set topo      [new Topography]

$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)

#
# Create nn mobilenodes [$val(nn)] and attach them to the channel.
#

# configure the nodes
set channel [new Channel/WirelessChannel]
$channel set errorProbability_ 0.0
$ns node-config -adhocRouting $val(rp) \
                -llType $val(ll) \
                -macType $val(mac) \
                -ifqType $val(ifq) \
                -ifqLen $val(ifqlen) \
                -antType $val(ant) \
                -propType $val(prop) \
                -phyType $val(netif) \
-energyModel "EnergyModel" \
-initialEnergy  200 \
-txPower 0.660 \
                -rxPower 0.395 \
                -idlePower 0.035 \
                -sleepPower 0.05\
                -channel $channel \
                -topoInstance $topo \
                -agentTrace ON \
                -routerTrace ON \
                -macTrace OFF \
                -movementTrace ON

          for {set i 0} {$i < $val(nn) } { incr i } {
         set node_($i) [$ns node]

 set x [ expr 10+round(rand()*1205) ]
 set y [ expr 10+round(rand()*1205) ]
         $node_($i) set initialEnergy_ [expr 100 + rand()*100]
         $node_($i) set X_ $x
         $node_($i) set Y_ $y
         $node_($i) set Z_ 0.0
 # puts $node_pos_file "Node $i: X=$x Y=$y"
     }
     # 设置节点目的地和能量更新
for {set i 0} {$i < $val(nn)} {incr i} {
    set t [expr 15 + round(rand() * 60)]
    set X [expr 10 + round(rand() * 1205)]
    set Y [expr 10 + round(rand() * 1205)]
    set speed [expr 2 + round(rand() * 15)]
    set en [$node_($i) energyModel getEnergy]
    set zlxs [expr 0.5]
    set change [expr $speed * $zlxs]
    set new_energy [expr $en - $change]
    $ns at $t "$node_($i) setdest $X $Y $speed"
    for {set j 0} {$j < $val(stop) * 10} {incr j} {
        set t1 [expr $j * 0.1 + $t]
        $ns at $t1 "$node_($i) set energy_ $new_energy"
    }
}

     
# Generation of movements
# $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
# $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
# $ns at 70.0 "$node_(2) setdest 480.0 300.0 5.0"
# $ns at 20.0 "$node_(3) setdest 200.0 200.0 5.0"
# $ns at 25.0 "$node_(4) setdest 50.0 50.0 10.0"
# $ns at 60.0 "$node_(5) setdest 150.0 70.0 2.0"
# $ns at 90.0 "$node_(6) setdest 380.0 150.0 8.0"
# $ns at 42.0 "$node_(7) setdest 200.0 100.0 15.0"
# $ns at 55.0 "$node_(8) setdest 50.0 275.0 5.0"
# $ns at 19.0 "$node_(9) setdest 250.0 250.0 7.0"
# $ns at 90.0 "$node_(10) setdest 150.0 150.0 20.0"

# Set a UDP connection between node_(2) and node_(11)
set udp [new Agent/UDP]
$udp set class_ 2
set null [new Agent/Null]
$ns attach-agent $node_(2) $udp
$ns attach-agent $node_(11) $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 500
$cbr set interval_ 0.05
$ns at 10.0 "$cbr start"
# Define node initial position in nam
for {set i 0} {$i < $val(nn) } { incr i } {
# 30 defines the node size for nam
$ns initial_node_pos $node_($i) 30
}

# Telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "$node_($i) reset";
}

# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150 "puts \"end simulation\" ; $ns halt"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
}

$ns run

Sadik Manik

unread,
May 5, 2024, 12:54:28 AM5/5/24
to Network Simulator 2 (NS2)
Is there any wrong with"  set en [$node_($i) energyModel getEnergy]  " ?

knudfl

unread,
May 5, 2024, 3:34:18 AM5/5/24
to Network Simulator 2 (NS2)
There are no examples of "getEnergy" or "new_energy" in the 3000 ns2 examples
cd 000-All-examples-5/
$ grep -n "new_energy" *
$ grep -n "getEnergy" *
My comment : Why look for getEnergy / new_energy: when you already have set fixed values for Energy ??

An edited example quentin-energy-2.tcl is here ...

All  3000 ns2 simulations


-
Reply all
Reply to author
Forward
0 new messages