How to creat the wireless network in ns

 

Home 
IEEE 802.11 
      Network Simulator 
Our Simulation 
Presentation 
Link 
Contact 

 

[How to creat the wireless network in ns][Other Examples]

 

How to creat the wireless network in ns

(Original Ns documentation are here: Ns-2 Documentation)

 

       When we simulate the wireless network in NS, we need to define the type of each network components. In addtion, we need to define other some parameters like the type of antenna, the radio-propagation, the routing protocol which are used by mobilenodes. In our simulation, we define our wireless network like the code below.

 

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

# Define options

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

 set val(chan)         Channel/WirelessChannel  ;# channel type

 set val(prop)         Propagation/TwoRayGround ;# radio-propagation model

 set val(ant)          Antenna/OmniAntenna      ;# Antenna type

 set val(ll)           LL                       ;# Link layer type

 set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type

 set val(ifqlen)       50                       ;# max packet in ifq

 set val(netif)        Phy/WirelessPhy          ;# network interface type

 set val(mac)          Mac/802_11               ;# MAC type

 set val(nn)           2                        ;# number of mobilenodes

 set val(rp)           DSR                    ;# routing protocol

 set val(x)            600

 set val(y)            600

 

Next we start by creating an instance of  the simulator, ns.

 

set ns [new Simulator]

 

We open a file, simplenetwork.nam  for writing that is going to be used for the nam trace data.

 

$ns trace-all $f

set namtrace [open simplenetwrok.nam w]

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

 

Then, to record ome parameters, we open some file like when we open a nam file.

 

set f [open 1_out.tr w]

set f0 [open proj_out0.tr w]

set f1 [open proj_out1.tr w]

 

In this simulation, we define that the mobile nodes movoes in the topology of 600*600 like below.

 

set topo [new Topography]

$topo load_flatgrid 600 600

 

We create the object God, as follows. God object stores the total number of mobilenodes and a table of shortest number of hops required to reach from one node to another.

 

create-god $val(nn)

 

We define the wireless channel as follow:

 

set chan_1 [new $val(chan)]

set chan_2 [new $val(chan)]

 

We use the default value of flat addressing; Also lets turn on only AgentTrace and RouterTrace; You can experiment with the traces by turning all of them on. AgentTraces are marked with AGT, RouterTrace with RTR and MacTrace with MAC in their 5th fields. MovementTrace, when turned on, shows the movement of the mobilenodes and the trace is marked with M in their 2nd field. The configuration API for creating mobilenodes looks as follows:

# CONFIGURE AND CREATE 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 OFF \

                 -routerTrace ON \

                 -macTrace ON \

                 -movementTrace OFF \

                 -channel $chan_1   \

                 -channel $chan_2   

 

We need to close the files which we opened  at some point. We use a modified 'finish' procedure to do that. In this procedure, we add some 'exec' to see the graph or execute nam file.

 

proc finish {} {

        global ns f f0 f1 namtrace

        $ns flush-trace

        close $namtrace   

        close $f0

        close $f1

        #exec xgraph proj_out0.tr proj_out1.tr proj_out2.tr proj_out3.tr

        exec nam -r 5m 1_out.nam &

        exit 0

      }

 

The record procedure is for recording data which we want to analyze. In this case, we record the number of packets received and the number of packets lost.

 

proc record {} {

  global sink0 sink1 f0 f1

   #Get An Instance Of The Simulator

   set ns [Simulator instance]

   

   #Set The Time After Which The Procedure Should Be Called Again

   set time 0.05

   #How Many Bytes Have Been Received By The Traffic Sinks?

   set bw0 [$sink0 set npkts_]

   set bw1 [$sink0 set nlost_]

 

   #Get The Current Time

   set now [$ns now]

   

   #Save Data To The Files

   puts $f0 "$now [expr $bw0]"

   puts $f1 "$now [expr $bw1]"

 

   #Re-Schedule The Procedure

   $ns at [expr $now+$time] "record"

  }

 

To get more beautiful  nam file, we can change the color of nodes or packets.

 

# define color index

$ns color 0 blue

$ns color 1 red

$ns color 2 chocolate

$ns color 3 red

$ns color 4 brown

$ns color 5 tan

$ns color 6 gold

$ns color 7 black

                        

set n(0) [$ns node]

#$ns at 0.0 "$n(0) color red"

$n(0) color "0"

$n(0) shape "circle"

set n(1) [$ns node]

$n(1) color "blue"

$n(1) shape "circle"

 

The start-position and future destination for mobilenodes may be set by using following the APIs.

 

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

        $ns initial_node_pos $n($i) 30+i*100

}

 

$n(0) set X_ 0.0

$n(0) set Y_ 0.0

$n(0) set Z_ 0.0

 

$n(1) set X_ 0.0

$n(1) set Y_ 0.0

$n(1) set Z_ 0.0

 

 

$ns at 0.0 "$n(0) setdest 200.0 200.0 3000.0"

$ns at 0.0 "$n(1) setdest 300.0 300.0 3000.0"

$ns at 1.0 "$n(0) setdest 200.0 200.0 500.0"

$ns at 1.0 "$n(1) setdest 400.0 400.0 500.0"

$ns at 1.5 "$n(0) setdest 200.0 200.0 500.0"

$ns at 1.5 "$n(1) setdest 300.0 300.0 500.0"

 

The following is to config and set up the packet flow.

 

# CONFIGURE AND SET UP A FLOW

set sink0 [new Agent/LossMonitor]

set sink1 [new Agent/LossMonitor]

$ns attach-agent $n(0) $sink0

$ns attach-agent $n(1) $sink1

 

#$ns attach-agent $sink2 $sink3

set tcp0 [new Agent/TCP]

$ns attach-agent $n(0) $tcp0

set tcp1 [new Agent/TCP]

$ns attach-agent $n(1) $tcp1

 

proc attach-CBR-traffic { node sink size interval } {

   #Get an instance of the simulator

   set ns [Simulator instance]

   #Create a CBR  agent and attach it to the node

   set cbr [new Agent/CBR]

   $ns attach-agent $node $cbr

   $cbr set packetSize_ $size

   $cbr set interval_ $interval

 

   #Attach CBR source to sink;

   $ns connect $cbr $sink

   return $cbr

  }

 

set cbr0 [attach-CBR-traffic $n(0) $sink1 1000 .015]

set cbr1 [attach-CBR-traffic $n(1) $sink0 1000 .015]

 

We have to tell the CBR agent when to send data and when to stop sending. It's probably best to put the following lines just before the line '$ns at 10.0 "finish"'.

 

$ns at 0.0 "record"

$ns at 0.3 "$cbr0 start"

$ns at 0.3 "$cbr1 start"

$ns at 10.0 "finish"

 

puts "Start of simulation.."

$ns run

 

Copyright(c) 2002. All rights reserved.
faithink@bu.edu, kimsooil@bu.edu