Sample NS-2 TCL Script Files [Free]

1. Create a Wired Loop Network (Kind of Ring Network)

The network link breaks somewhere in between and the link becomes up after some time.

#Create a simulator object
set ns [new Simulator]

#Tell the simulator to use dynamic routing
$ns rtproto DV

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf


#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
#Close the trace file
        close $nf
#Execute nam on the trace file
        exec nam out.nam &
        exit 0
}

#Create seven nodes
for {set i 0} {$i < 7} {incr i} {
        set n($i) [$ns node]
}


#Create links between the nodes
for {set i 0} {$i < 7} {incr i} {
        $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}

#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

#Create a Null agent (a traffic sink) and attach it to node n(3)
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0

#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0 

#Schedule events for the CBR agent and the network dynamics
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run

2. Code to insert Channel Error rate to a wired network


#Set error model on link n2 to n3.

set loss_module [new ErrorModel]
$loss_module set rate_ 0.2
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]
$ns lossmodel $loss_module $n2 $n3


3. Wireless Sensor Network


# ======================================================================
# Define options
# ======================================================================
set val(chan)           Channel/WirelessChannel    ;# Channel Type
set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set val(netif)          Phy/WirelessPhy/802_15_4
set val(mac)            Mac/802_15_4
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)         150                         ;# max packet in ifq
set val(nn)             4                         ;# number of mobilenodes
set val(rp)             AODV                       ;# routing protocol
set val(x)  50
set val(y)  50
#Queue/DropTail/PriQueue set Prefer_Routing_Protocols    1
#LL set mindelay_                50us
#LL set delay_                   25us
#LL set bandwidth_               0       ;# not used
#LL set-limit 1000000




# =================================
# Antenna Settings
# =================================
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0 
Antenna/OmniAntenna set Gr_ 1.0
#======================
#Physica layer setting
#======================
Phy/WirelessPhy set freq_ 2.4e+9 ;# The working band is 2.4GHz
Phy/WirelessPhy set L_ 0.5   ;#Define teh system loss in TwoRayGround
Phy/WirelessPhy set  bandwidth_  28.8*10e3   ;#28.8 kbps
# For model 'TwoRayGround'
# Thereceived signals strenghts  in different distances
set dist(5m)  7.69113e-06
set dist(9m)  2.37381e-06
set dist(10m) 1.92278e-06
set dist(11m) 1.58908e-06
set dist(12m) 1.33527e-06
set dist(13m) 1.13774e-06
set dist(14m) 9.81011e-07
set dist(15m) 8.54570e-07
set dist(16m) 7.51087e-07
set dist(20m) 4.80696e-07
set dist(25m) 3.07645e-07
set dist(30m) 2.13643e-07
set dist(35m) 1.56962e-07
set dist(40m) 1.20174e-07
Phy/WirelessPhy set CSThresh_  ;#$dist(40m)
Phy/WirelessPhy set RXThresh_  ;#$dist(40m)
#Phy/WirelessPhy set CPThresh_ 10
# According to the Two-Ray Ground propagation model
#Pr(d) = Pt*Gt*Gr*ht*ht*hr*hr/(d*d*d*d*L)
# thus the transmitted signal power
#average Pt= d*d*d*d*L*Pr(d)/(Gt*Gr*ht*ht*hr*hr) = 1mW   
Phy/WirelessPhy set  pt_ 0.001
#Specified Parameters for 802.15.4 MAC
Mac/802_15_4 wpanCmd verbose on   ;# to work in verbose mode
Mac/802_15_4 wpanNam namStatus on  ;# default = off (should be turned on before other 'wpanNam' commands can work)

# Initialize Global Variables
set ns_  [new Simulator]
# Tell teh simulator to use teh new type trace data
$ns_ use-newtrace
set scenario1   [open scenario1.tr w]
$ns_ trace-all $scenario1
#Define the NAM output file
set scenario1nam     [open scenario1.nam w]
$ns_ namtrace-all-wireless $scenario1nam $val(x) $val(y)
$ns_ puts-nam-traceall {# nam4wpan #}  ;# inform nam that this is a trace file for wpan (special handling needed)

# set up topography object
set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)

# Create God
set god_ [create-god $val(nn)]
set chan_1_ [new $val(chan)]

# configure node
$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) \
  -topoInstance $topo \
  -agentTrace OFF \
  -routerTrace OFF \
  -macTrace ON \
  #-movementTrace OFF \
                -energyModel "EnergyModel"\
                -initialEnergy 100\
                -rxPower 0.3\
                -txPower 0.3\
  -channel $chan_1_

for {set i 0} {$i < $val(nn) } {incr i} {
 set node_($i) [$ns_ node]
 $node_($i) random-motion 0  ;# disable random motion
 $god_ new_node $node_($i)
}
#Lable the sink node
$ns_ at 0.0 "$node_(3) NodeLabel Sink"
# Define the nodes positions
$node_(0) set X_ 15.0
$node_(0) set Y_ 20.0
$node_(0) set Z_ 0.000000000000
$node_(1) set X_ 25.0
$node_(1) set Y_ 20.0
$node_(1) set Z_ 0.000000000000
$node_(2) set X_ 35.0
$node_(2) set Y_ 20.0
$node_(2) set Z_ 0.000000000000
$node_(3) set X_ 5.0
$node_(3) set Y_ 5.0
$node_(3) set Z_ 0.000000000000
#set null [new Agent/Null]
#$ns_ attach-agent $node_(3) $null
# Define the cbr traffic
proc cbrtraffic { src dst interval starttime stoptime packetsize stage } {
   global ns_ node_
   set udp([expr $src + $stage]) [new Agent/UDP]
   eval $ns_ attach-agent \$node_($src) \$udp([expr $src + $stage])
   set null([expr $src + $stage]) [new Agent/Null]
   eval $ns_ attach-agent \$node_($dst) \$null([expr $src + $stage])
   set cbr([expr $src + $stage]) [new Application/Traffic/CBR]
   eval \$cbr([expr $src + $stage]) set packetSize_ $packetsize
   eval \$cbr([expr $src + $stage]) set interval_ $interval
   eval \$cbr([expr $src + $stage]) set random_ 0
   #eval \$cbr($src) set maxpkts_ 10000
   eval \$cbr([expr $src + $stage]) attach-agent \$udp([expr $src + $stage])
   eval $ns_ connect \$udp([expr $src + $stage]) \$null([expr $src + $stage])
   $ns_ at $starttime "$cbr([expr $src + $stage]) start"
   $ns_ at $stoptime "$cbr([expr $src + $stage]) stop"
# $ns_ at $stoptime "$node_($src) reset"
#$ns_ at $stoptime "$node_($dst) reset"
}

## Establish traffic between nodes
 puts "\nTraffic: cbr"
   Mac/802_15_4 wpanCmd ack4data off
   puts [format "Acknowledgement for data: %s" [Mac/802_15_4 wpanCmd ack4data]]
#src dest interval start stop packetsize stage
#the first sensor activity
cbrtraffic 0 3 0.2 0.1 0.9 5 1;# target is in the range from 15 to 10 meters from the first sensor (sensed signal=5 bytes)
cbrtraffic 0 3 0.2 1.0 1.9 8 2;# target is in the range from 10 to 5 meters from the first sensor (sensed signal=8 bytes)
cbrtraffic 0 3 0.2 2.0 3.9 10 3;#target is in the range from 5 to 0 meters from the first sensor (sensed signal=10 bytes)
cbrtraffic 0 3 0.2 4.0 4.9 8 4;# target is in the range from 5 to 10 meters from the first sensor (sensed signal=8 bytes)
cbrtraffic 0 3 0.2 5.0 5.9 5 5;# target is in the range from 10 to 15 meters from the first sensor (sensed signal=5 bytes)
cbrtraffic 0 3 0.2 6.0 6.9 3 6;# target is in the range from 15 to 20 meters from the first sensor (sensed signal=5 bytes)
cbrtraffic 0 3 0.2 7 7.9 1 7;# target is in the range from 20 to 25 meters from the first sensor (sensed signal=5 bytes)
#the second sensor activity
cbrtraffic 1 3 0.2 0.2 0.9 1 1; # target is the range of 25 to 20 meters far from the sensor(sensed signal=1 bytes)
cbrtraffic 1 3 0.2 1 1.9 3 2; # target is the range of 20 to 15 meters far from the sensor(sensed signal=3 bytes)
cbrtraffic 1 3 0.2 2 2.9 5 3; # target is the range of 15 to 10 meters far from the sensor(sensed signal=5 bytes)
cbrtraffic 1 3 0.2 3 3.9 8 4; # target is the range of 10 to 5 meters far from the sensor(sensed signal=8 bytes)
cbrtraffic 1 3 0.2 4 5.9 10 5; # target is the range of 0 to 5 meters far from the sensor(sensed signal=10 bytes)
cbrtraffic 1 3 0.2 6 6.9 8 6; # target is the range of 5 to 10 meters far from the sensor(sensed signal=8 bytes)
cbrtraffic 1 3 0.2 7 7.9 5 7; # target is the range of 25 to 20 meters far from the sensor(sensed signal=5 bytes)
cbrtraffic 1 3 0.2 8 8.9 3 8; # target is the range of 25 to 20 meters far from the sensor(sensed signal=3 bytes)
cbrtraffic 1 3 0.2 9 9.9 1 9; # target is the range of 25 to 20 meters far from the sensor(sensed signal=1 bytes)
#the third sensor activity
cbrtraffic 2 3 0.2 2 2.9 1 1; # target is the range of 25 to 20 meters far from the sensor(sensed signal=1 bytes)
cbrtraffic 2 3 0.2 3 3.9 3 2; # target is the range of 20 to 15 meters far from the sensor(sensed signal=3 bytes)
cbrtraffic 2 3 0.2 4 4.9 5 3; # target is the range of 15 to 10 meters far from the sensor(sensed signal=5 bytes)
cbrtraffic 2 3 0.2 5 5.9 8 4; # target is the range of 10 to 5 meters far from the sensor(sensed signal=8 bytes)
cbrtraffic 2 3 0.2 6 7.9 10 5; # target is the range of 0 to 5 meters far from the sensor(sensed signal=10 bytes)
cbrtraffic 2 3 0.2 8 8.9 8 6; # target is the range of 5 to 10 meters far from the sensor(sensed signal=8 bytes)
cbrtraffic 2 3 0.2 9 10 5 7; # target is the range of 25 to 20 meters far from the sensor(sensed signal=5 bytes)

# defines the node size in nam
for {set i 0} {$i < $val(nn)} {incr i} {
 $ns_ initial_node_pos $node_($i) 5
}

# Tell nodes simulation ends at 10.0
for {set i 0} {$i < $val(nn) } {incr i} {
   $ns_ at 10 "$node_($i) reset";
}
$ns_ at 10 "stop"
$ns_ at 10 "puts \"\nNS EXITING...\""
$ns_ at 10 "$ns_ halt"

proc stop {} {
    global ns_ scenario1 scenario1nam
    $ns_ flush-trace
    close $scenario1
    #set hasDISPLAY 0
    exec nam scenario1.nam &

}
puts "\nStarting Simulation..."
$ns_ run

4. Sample Code for a Simple Wired Network


#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows
$ns color 1 Blue
$ns color 2 Red

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
#Close the trace file
        close $nf
#Execute nam on the trace file
        exec nam out.nam &
        exit 0
}

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 2Mb 10ms DropTail

$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5

#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

#Create a Null agent (a traffic sink) and attach it to node n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0

#Connect the traffic sources with the traffic sink
$ns connect $udp0 $null0 
$ns connect $udp1 $null0

#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run

5. A sample Code of Wired Network Generated fron Network Script Generator NSG


# This script is created by NSG2 beta1
# <http://wushoupong.googlepages.com/nsg>

#===================================
#     Simulation parameters setup
#===================================
set val(stop)   10.0                         ;# time of simulation end

#===================================
#        Initialization       
#===================================
#Create a ns simulator
set ns [new Simulator]

$ns color 1 Purple
$ns color 2 Yellow

#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile

#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile

#===================================
#        Nodes Definition       
#===================================
#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#===================================
#        Links Definition       
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 10.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 20
$ns duplex-link $n1 $n2 10.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 20
$ns duplex-link $n2 $n3 10.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 20
$ns duplex-link $n3 $n4 10.0Mb 10ms DropTail
$ns queue-limit $n3 $n4 20
$ns duplex-link $n3 $n5 10.0Mb 10ms DropTail
$ns queue-limit $n3 $n5 20

#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down

#===================================
#        Agents Definition       
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0
set null2 [new Agent/Null]
$ns attach-agent $n4 $null2
$ns connect $udp0 $null2
$udp0 set packetSize_ 1000

#Setup a TCP connection
set tcp1 [new Agent/TCP]
$tcp1 set class_ 1
$ns attach-agent $n1 $tcp1
set sink3 [new Agent/TCPSink]
$ns attach-agent $n5 $sink3
$ns connect $tcp1 $sink3
$tcp1 set packetSize_ 1500


#===================================
#        Applications Definition       
#===================================
#Setup a CBR Application over UDP connection
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp0
$cbr1 set packetSize_ 1000
$cbr1 set rate_ 1.0Mb
$cbr1 set random_
$ns at 1.0 "$cbr1 start"
$ns at 2.0 "$cbr1 stop"

#Setup a FTP Application over TCP connection
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp1
$ns at 1.0 "$ftp2 start"
$ns at 5.0 "$ftp2 stop"


#===================================
#        Termination       
#===================================
#Define a 'finish' procedure
proc finish {} {
    global ns tracefile namfile
    $ns flush-trace
    close $tracefile
    close $namfile
    exec nam out.nam &
    exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run


6. A sample Wireless Network script


# 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) 3 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 400 ;# Y dimension of topography
set val(stop) 150 ;# time of simulation end

set ns [new Simulator]
#Creating trace file and nam file
set tracefd       [open dsdv.tr w]
set windowVsTime2 [open win.tr w]
set namtrace      [open dsdv.nam 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)

# configure the nodes
        $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 ON
                 
      for {set i 0} {$i < $val(nn) } { incr i } {
            set node_($i) [$ns node]   
      }

# Provide initial location of mobilenodes
$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 490.0
$node_(1) set Y_ 285.0
$node_(1) set Z_ 0.0

$node_(2) set X_ 150.0
$node_(2) set Y_ 240.0
$node_(2) set Z_ 0.0

# 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 110.0 "$node_(0) setdest 480.0 300.0 5.0"

# Set a TCP connection between node_(0) and node_(1)
set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"

# Printing the window size
proc plotWindow {tcpSource file} {
global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 10.1 "plotWindow $tcp $windowVsTime2"

# 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.01 "puts \"end simulation\" ; $ns halt"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec nam dsdv.nam &
exit 0
}

$ns run


7. A Sample Wireless Script Generated from NSG2


# This script is created by NSG2 beta1
# <http://wushoupong.googlepages.com/nsg>

#===================================
#     Simulation parameters setup
#===================================
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)     3                          ;# number of mobilenodes
set val(rp)     DSDV                       ;# routing protocol
set val(x)      834                      ;# X dimension of topography
set val(y)      600                      ;# Y dimension of topography
set val(stop)   10.0                         ;# time of simulation end

#===================================
#        Initialization       
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object
set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile

#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
#     Mobile node parameter setup
#===================================
$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) \
                -channel       $chan \
                -topoInstance  $topo \
                -agentTrace    ON \
                -routerTrace   ON \
                -macTrace      ON \
                -movementTrace ON

#===================================
#        Nodes Definition       
#===================================
#Create 3 nodes
set n0 [$ns node]
$n0 set X_ 153
$n0 set Y_ 432
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 444
$n1 set Y_ 214
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 734
$n2 set Y_ 474
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20

#===================================
#        Generate movement         
#===================================
$ns at 1 " $n0 setdest 500 500 20 "
$ns at 2 " $n1 setdest 600 450 20 "
$ns at 1 " $n2 setdest 200 300 30 "

#===================================
#        Agents Definition       
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1500

#Setup a UDP connection
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set null3 [new Agent/Null]
$ns attach-agent $n2 $null3
$ns connect $udp1 $null3
$udp1 set packetSize_ 1500


#===================================
#        Applications Definition       
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 5.0 "$ftp0 stop"

#Setup a CBR Application over UDP connection
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 1000
$cbr1 set rate_ 1.0Mb
$cbr1 set random_
$ns at 1.0 "$cbr1 start"
$ns at 5.0 "$cbr1 stop"


#===================================
#        Termination       
#===================================
#Define a 'finish' procedure
proc finish {} {
    global ns tracefile namfile
    $ns flush-trace
    close $tracefile
    close $namfile
    exec nam out.nam &
    exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

8. Sample TCL File for Xgraph Output

# [scenario]
# It consists of 8 mobile nodes: 4 source nodes and 4 destination node.
# Each source is a # CBR source over UDP. The size of a transmitted packet is 512 bytes. # Transmission rate # of a node is 600 Kbps.
# We assumed that the nodes are in transmission range at a # constant distance of 195 m. # The simulation time lasted for 80 sec.

# ====================================================================

# Define Node Configuration paramaters

#====================================================================

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)             8                          ;# number of mobilenodes

set val(rp)             DSDV                       ;# routing protocol

set val(x)              500                        ;# X dimension of the topography

set val(y)              500                           ;# Y dimension of the topography



Mac/802_11 set RTSThreshold_  3000

Mac/802_11 set basicRate_ 1Mb

Mac/802_11 set dataRate_  2Mb



#=====================================================================

# Initialize trace file desctiptors

#=====================================================================

# *** Throughput Trace ***

set f0 [open out02.tr w]

set f1 [open out12.tr w]

set f2 [open out22.tr w]

set f3 [open out32.tr w]



# *** Packet Loss Trace ***

set f4 [open lost02.tr w]

set f5 [open lost12.tr w]

set f6 [open lost22.tr w]

set f7 [open lost32.tr w]



# *** Packet Delay Trace ***

set f8 [open delay02.tr w]

set f9 [open delay12.tr w]

set f10 [open delay22.tr w]

set f11 [open delay32.tr w]



# *** Initialize Simulator ***

set ns_              [new Simulator]



# *** Initialize Trace file ***

set tracefd     [open trace2.tr w]

$ns_ trace-all $tracefd



# *** Initialize Network Animator ***

set namtrace [open sim12.nam w]

$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)



# *** set up topography object ***

set topo       [new Topography]

$topo load_flatgrid 500 500



# Create  General Operations Director (GOD) object. It is used to store global information about the state of the environment, network, or nodes that an

# omniscent observer would have, but that should not be made known to any participant in the simulation.



create-god $val(nn)



# configure nodes

        $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                 



# Create Nodes



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

                set node_($i) [$ns_ node]

                $node_($i) random-motion 0            ;# disable random motion

        }



# Initialize Node Coordinates



$node_(0) set X_ 5.0

$node_(0) set Y_ 5.0

$node_(0) set Z_ 0.0



$node_(1) set X_ 200.0

$node_(1) set Y_ 5.0

$node_(1) set Z_ 0.0



$node_(2) set X_ 5.0

$node_(2) set Y_ 50.0

$node_(2) set Z_ 0.0



$node_(3) set X_ 200.0

$node_(3) set Y_ 50.0

$node_(3) set Z_ 0.0



$node_(4) set X_ 5.0

$node_(4) set Y_ 100.0

$node_(4) set Z_ 0.0



$node_(5) set X_ 200.0

$node_(5) set Y_ 100.0

$node_(5) set Z_ 0.0



$node_(6) set X_ 2.0

$node_(6) set Y_ 150.0

$node_(6) set Z_ 0.0



$node_(7) set X_ 200.0

$node_(7) set Y_ 150.0

$node_(7) set Z_ 0.0



# Setup traffic flow between nodes

# UDP connections between node_(0) and node_(1)



# Create Constant four Bit Rate Traffic sources



set agent1 [new Agent/UDP]             ;# Create UDP Agent

$agent1 set prio_ 0                   ;# Set Its priority to 0

set sink [new Agent/LossMonitor]  ;# Create Loss Monitor Sink in order to be able to trace the number of bytes received

$ns_ attach-agent $node_(0) $agent1     ;# Attach Agent to source node

$ns_ attach-agent $node_(1) $sink ;# Attach Agent to sink node

$ns_ connect $agent1 $sink            ;# Connect the nodes

set app1 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application

$app1 set packetSize_ 512               ;# Set Packet Size to 512 bytes

$app1 set rate_ 600Kb                    ;# Set CBR rate to 200 Kbits/sec

$app1 attach-agent $agent1             ;# Attach Application to agent



set agent2 [new Agent/UDP]             ;# Create UDP Agent

$agent2 set prio_ 1                   ;# Set Its priority to 1

set sink2 [new Agent/LossMonitor]         ;# Create Loss Monitor Sink in order to be able to trace the number obytes received

$ns_ attach-agent $node_(2) $agent2     ;# Attach Agent to source node

$ns_ attach-agent $node_(3) $sink2        ;# Attach Agent to sink node

$ns_ connect $agent2 $sink2                  ;# Connect the nodes

set app2 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application

$app2 set packetSize_ 512               ;# Set Packet Size to 512 bytes

$app2 set rate_ 600Kb                    ;# Set CBR rate to 200 Kbits/sec

$app2 attach-agent $agent2             ;# Attach Application to agent



set agent3 [new Agent/UDP]             ;# Create UDP Agent

$agent3 set prio_ 2                   ;# Set Its priority to 2

set sink3 [new Agent/LossMonitor]         ;# Create Loss Monitor Sink in order to be able to trace the number obytes received

$ns_ attach-agent $node_(4) $agent3     ;# Attach Agent to source node

$ns_ attach-agent $node_(5) $sink3        ;# Attach Agent to sink node

$ns_ connect $agent3 $sink3                  ;# Connect the nodes

set app3 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application

$app3 set packetSize_ 512               ;# Set Packet Size to 512 bytes

$app3 set rate_ 600Kb                    ;# Set CBR rate to 200 Kbits/sec

$app3 attach-agent $agent3             ;# Attach Application to agent



set agent4 [new Agent/UDP]             ;# Create UDP Agent

$agent4 set prio_ 3                   ;# Set Its priority to 3

set sink4 [new Agent/LossMonitor]         ;# Create Loss Monitor Sink in order to be able to trace the number obytes received

$ns_ attach-agent $node_(6) $agent4     ;# Attach Agent to source node

$ns_ attach-agent $node_(7) $sink4        ;# Attach Agent to sink node

$ns_ connect $agent4 $sink4                  ;# Connect the nodes

set app4 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application

$app4 set packetSize_ 512               ;# Set Packet Size to 512 bytes

$app4 set rate_ 600Kb                    ;# Set CBR rate to 200 Kbits/sec

$app4 attach-agent $agent4             ;# Attach Application to agent



# defines the node size in Network Animator



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

    $ns_ initial_node_pos $node_($i) 20

}



# Initialize Flags

set holdtime 0

set holdseq 0



set holdtime1 0

set holdseq1 0



set holdtime2 0

set holdseq2 0



set holdtime3 0

set holdseq3 0



set holdrate1 0

set holdrate2 0

set holdrate3 0

set holdrate4 0



# Function To record Statistcis (Bit Rate, Delay, Drop)



proc record {} {

        global sink sink2 sink3 sink4 f0 f1 f2 f3 f4 f5 f6 f7 holdtime holdseq holdtime1 holdseq1 holdtime2 holdseq2 holdtime3 holdseq3 f8 f9 f10 f11 holdrate1 holdrate2 holdrate3 holdrate4

     

        set ns [Simulator instance]

     

    set time 0.9 ;#Set Sampling Time to 0.9 Sec



        set bw0 [$sink set bytes_]

        set bw1 [$sink2 set bytes_]

        set bw2 [$sink3 set bytes_]

        set bw3 [$sink4 set bytes_]



        set bw4 [$sink set nlost_]

        set bw5 [$sink2 set nlost_]

        set bw6 [$sink3 set nlost_]

        set bw7 [$sink4 set nlost_]



        set bw8 [$sink set lastPktTime_]

        set bw9 [$sink set npkts_]



        set bw10 [$sink2 set lastPktTime_]

        set bw11 [$sink2 set npkts_]



        set bw12 [$sink3 set lastPktTime_]

        set bw13 [$sink3 set npkts_]



        set bw14 [$sink4 set lastPktTime_]

        set bw15 [$sink4 set npkts_]

     

    set now [$ns now]

     

        # Record Bit Rate in Trace Files

        puts $f0 "$now [expr (($bw0+$holdrate1)*8)/(2*$time*1000000)]"

        puts $f1 "$now [expr (($bw1+$holdrate2)*8)/(2*$time*1000000)]"

        puts $f2 "$now [expr (($bw2+$holdrate3)*8)/(2*$time*1000000)]"

        puts $f3 "$now [expr (($bw3+$holdrate4)*8)/(2*$time*1000000)]"



        # Record Packet Loss Rate in File

        puts $f4 "$now [expr $bw4/$time]"

        puts $f5 "$now [expr $bw5/$time]"

        puts $f6 "$now [expr $bw6/$time]"

        puts $f7 "$now [expr $bw7/$time]"



        # Record Packet Delay in File

        if { $bw9 > $holdseq } {

                puts $f8 "$now [expr ($bw8 - $holdtime)/($bw9 - $holdseq)]"

        } else {

                puts $f8 "$now [expr ($bw9 - $holdseq)]"

        }



        if { $bw11 > $holdseq1 } {

                puts $f9 "$now [expr ($bw10 - $holdtime1)/($bw11 - $holdseq1)]"

        } else {

                puts $f9 "$now [expr ($bw11 - $holdseq1)]"

        }



        if { $bw13 > $holdseq2 } {

                puts $f10 "$now [expr ($bw12 - $holdtime2)/($bw13 - $holdseq2)]"

        } else {

                puts $f10 "$now [expr ($bw13 - $holdseq2)]"

        }



        if { $bw15 > $holdseq3 } {

                puts $f11 "$now [expr ($bw14 - $holdtime3)/($bw15 - $holdseq3)]"

        } else {

                puts $f11 "$now [expr ($bw15 - $holdseq3)]"

        }

     

        # Reset Variables

        $sink set bytes_ 0

        $sink2 set bytes_ 0

        $sink3 set bytes_ 0

        $sink4 set bytes_ 0



        $sink set nlost_ 0

        $sink2 set nlost_ 0

        $sink3 set nlost_ 0

        $sink4 set nlost_ 0



        set holdtime $bw8

        set holdseq $bw9



        set  holdrate1 $bw0

        set  holdrate2 $bw1

        set  holdrate3 $bw2

        set  holdrate4 $bw3



    $ns at [expr $now+$time] "record"   ;# Schedule Record after $time interval sec

}





# Start Recording at Time 0

$ns_ at 0.0 "record"



$ns_ at 1.4 "$app1 start"                 ;# Start transmission at time t = 1.4 Sec



$ns_ at 10.0 "$app2 start"               ;# Start transmission at time t = 10 Sec



$ns_ at 20.0 "$app3 start"               ;# Start transmission at time t = 20 Sec



$ns_ at 30.0 "$app4 start"               ;# Start transmission at time t = 30 Sec



# Stop Simulation at Time 80 sec

$ns_ at 80.0 "stop"



# Reset Nodes at time 80 sec



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

    $ns_ at 80.0 "$node_($i) reset";

}



# Exit Simulatoion at Time 80.01 sec

$ns_ at 80.01 "puts \"NS EXITING...\" ; $ns_ halt"



proc stop {} {

        global ns_ tracefd f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11



        # Close Trace Files

        close $f0

        close $f1

        close $f2

        close $f3



        close $f4

        close $f5

        close $f6

        close $f7



        close $f8

        close $f9

        close $f10

        close $f11



        # Plot Recorded Statistics

        exec xgraph out02.tr out12.tr out22.tr out32.tr -geometry 800x400 -P -bg white &

        exec xgraph lost02.tr lost12.tr lost22.tr lost32.tr -geometry 800x400 -P -bg white &

        exec xgraph delay02.tr delay12.tr delay22.tr delay32.tr -geometry 800x400 -P -bg white &

     

        # Reset Trace File

        $ns_ flush-trace

        close $tracefd

     

        exit 0

}



puts "Starting Simulation..."

$ns_ run

No comments:

Post a Comment

A Compendium of Epigrams Vol:1

Buy From Amazon  Kindle Edition Sneak Peak of Second BOOK