Thursday 12 May 2016

Ns2 scipts

set val(chan) Channel/WirelessChannel ;
set val(prop) Propagation/TwoRayGround ;
set val(netif) Phy/WirelessPhy ;
set val(mac) Mac/802_11 ;
set val(ifq) Queue/DropTail/PriQueue ;
set val(ll) LL ;
set val(ant) Antenna/OmniAntenna ;
set val(ifqlen) 30 ;
set val(nn) 20 ;
set val(rp) AODV ;
set val(x) 500 ;
set val(y) 500 ;
set val(stop) 50 ;

set ns [new Simulator]

set tracefd [open AODV_50.tr w]

set winFile [open CwMaodv_50 w]

set namtracefd [open namwrls.nam w]

$ns trace-all $tracefd
$ns use-newtrace

$ns namtrace-all-wireless $namtracefd $val(x) $val(y)

set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)


$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) \
                -channelType $val(chan) \
                -topoInstance $topo \
                -agentTrace ON \
                -routerTrace ON \
                -macTrace OFF \
                -movementTrace OFF \


for {
set i 0
} {
$i < $val(nn)
} {
incr i
} {

  set node_($i) [$ns node]
 
}


#===================================
#        Nodes Definition      
#===================================
#Create 16 nodes
set n0 [$ns node]
$n0 set X_ 437
$n0 set Y_ 301
$n0 set Z_ 0.0
$n0 color orange
$ns at 0.0 "$n0 color orange"
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 324
$n1 set Y_ 337
$n1 set Z_ 0.0
$n1 color orange
$ns at 0.0 "$n1 color orange"
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 274
$n2 set Y_ 257
$n2 set Z_ 0.0
$n2 color orange
$ns at 0.0 "$n2 color orange"
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 291
$n3 set Y_ 456
$n3 set Z_ 0.0
$n3 color orange
$ns at 0.0 "$n3 color orange"
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 478
$n4 set Y_ 390
$n4 set Z_ 0.0
$n4 color orange
$ns at 0.0 "$n4 color orange"
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 150
$n5 set Y_ 357
$n5 set Z_ 0.0
$n5 color orange
$ns at 0.0 "$n5 color orange"
$ns initial_node_pos $n5 20
set n6 [$ns node]
$n6 set X_ 436
$n6 set Y_ 153
$n6 set Z_ 0.0
$n6 color orange
$ns at 0.0 "$n6 color orange"
$ns initial_node_pos $n6 20
set n7 [$ns node]
$n7 set X_ 451
$n7 set Y_ 509
$n7 set Z_ 0.0
$n7 color orange
$ns at 0.0 "$n7 color orange"
$ns initial_node_pos $n7 20
set n8 [$ns node]
$n8 set X_ 150
$n8 set Y_ 166
$n8 set Z_ 0.0
$n8 color orange
$ns at 0.0 "$n8 color orange"
$ns initial_node_pos $n8 20
set n9 [$ns node]
$n9 set X_ 566
$n9 set Y_ 305
$n9 set Z_ 0.0
$n9 color orange
$ns at 0.0 "$n9 color orange"
$ns initial_node_pos $n9 20
set n10 [$ns node]
$n10 set X_ 611
$n10 set Y_ 462
$n10 set Z_ 0.0
$n10 color orange
$ns at 0.0 "$n10 color orange"
$ns initial_node_pos $n10 20
set n11 [$ns node]
$n11 set X_ 244
$n11 set Y_ 552
$n11 set Z_ 0.0
$n11 color orange
$ns at 0.0 "$n11 color orange"
$ns initial_node_pos $n11 20
set n12 [$ns node]
$n12 set X_ 298
$n12 set Y_ 76
$n12 set Z_ 0.0
$n12 color orange
$ns at 0.0 "$n12 color orange"
$ns initial_node_pos $n12 20
set n13 [$ns node]
$n13 set X_ 544
$n13 set Y_ 213
$n13 set Z_ 0.0
$n13 color orange
$ns at 0.0 "$n13 color orange"
$ns initial_node_pos $n13 20
set n14 [$ns node]
$n14 set X_ 663
$n14 set Y_ 355
$n14 set Z_ 0.0
$n14 color orange
$ns at 0.0 "$n14 color orange"
$ns initial_node_pos $n14 20
set n15 [$ns node]
$n15 set X_ 503
$n15 set Y_ 119
$n15 set Z_ 0.0
$n15 color red
$ns at 0.0 "$n15 color red"
$ns initial_node_pos $n15 20

$ns at 10.0 "$node_(0) setdest 190.0 210.0 8.0"

$ns at 10.0 "$node_(1) setdest 145.0 40.0 8.0"

$ns at 10.0 "$node_(3) setdest 257.0 110.0 8.0"

$ns at 10.0 "$node_(5) setdest 340.0 278.0 8.0"

$ns at 10.0 "$node_(7) setdest 160.0 210.0 8.0"

$ns at 10.0 "$node_(2) setdest 490.0 310.0 8.0"

$ns at 10.0 "$node_(6) setdest 400.0 110.0 8.0"

$ns at 10.0 "$node_(12) setdest 290.0 210.0 8.0"

$ns at 10.0 "$node_(11) setdest 315.0 200.0 8.0"

$ns at 10.0 "$node_(2) setdest 250.0 180.0 8.0"

$ns at 10.0 "$node_(13) setdest 45.0 20.0 8.0"

$ns at 10.0 "$node_(15) setdest 85.0 70.0 8.0"

$ns at 10.0 "$node_(9) setdest 255.0 140.0 8.0"

$ns at 10.0 "$node_(4) setdest 140.0 80.0 8.0"



set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(2) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(4) $tcp
$ns attach-agent $node_(8) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(8) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(1) $tcp
$ns attach-agent $node_(5) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(3) $tcp
$ns attach-agent $node_(7) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(5) $tcp
$ns attach-agent $node_(9) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(6) $tcp
$ns attach-agent $node_(10) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(11) $tcp
$ns attach-agent $node_(15) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(13) $tcp
$ns attach-agent $node_(5) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(14) $tcp
$ns attach-agent $node_(7) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(15) $tcp
$ns attach-agent $node_(6) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(15) $tcp
$ns attach-agent $node_(5) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(12) $tcp
$ns attach-agent $node_(4) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(8) $tcp
$ns attach-agent $node_(3) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(9) $tcp
$ns attach-agent $node_(2) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(2) $tcp
$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(2) $tcp
$ns attach-agent $node_(0) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

$ns at 0.1 "$ftp start"

for {
set i 0
} {
$i < $val(nn)
} {
incr i
} {

 $ns initial_node_pos $node_($i) 10
 
}

for {
set i 0
} {
$i < $val(nn)
} {
incr i
} {

 $ns at $val(stop) "$node_($i) reset"

}

$ns at $val(stop) "stop"


proc plotWindow {
tcpSource file
} {

global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file"
}
$ns at 0.1 "plotWindow $tcp $winFile"

proc stop {

} {

global ns tracefd namtracefd
$ns flush-trace
close $tracefd
close $namtracefd
exec nam namwrls.nam &
exit 0

}


$ns run

No comments:

Post a Comment