Simulate Ad Hoc Network in NS-2

Simulate Ad Hoc Network in NS-2 Code

set ns_ [new Simulator]
set val(x) 800 ;# X dimension of topology
set val(y) 800 ;# Y dimension of topology
set val(nn) 50 ;# Number of node
set val(stop) 150.0 ;# Simulation time
set val(routing) AODV
set qtype Queue/DropTail/PriQueue
#set qtype CMUPriQueue # This queue will be used for DSR
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
set tracefd [open out.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtrace [open out.nam w]
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
set windowsVsTime2 [open w.tr w]
#=====================================================================
# Main Program
# ======================================================================
set god_ [create-god $val(nn)]
#

# Initialize Global Variables

#global node setting

$ns_ node-config -adhocRouting $val(routing)
-llType LL
-macType Mac/802_11
-ifqType $qtype
-ifqLen 50
-antType Antenna/OmniAntenna
-propType Propagation/TwoRayGround
-phyType Phy/WirelessPhy
-channelType Channel/WirelessChannel
-topoInstance $topo
-agentTrace ON
-routerTrace ON
-macTrace ON
-energyModel EnergyModel
-rxPower 0.3
-txPower 0.6
-initialEnergy 100
#-idlePower 712e-6
#-sleepPower 144e-9
# -sensePower 0.0175/
Phy/WirelessPhy set CPThresh_ 10.0
Phy/WirelessPhy set CSThresh_ 3.65262e-10 ;#250m
Phy/WirelessPhy set RXThresh_ 3.65262e-10 ;#250m
Phy/WirelessPhy set Rb_ 2*1e6
Phy/WirelessPhy set Pt_ 3.00
Phy/WirelessPhy set freq_ 2.4e+7
Phy/WirelessPhy set L_ 1.0

#Create the specified number of nodes
for {set i 0} { $i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
}
source scen-50 # Number of Nodes Download this file and put in the same directory that configures nodes position.

for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ initial_node_pos $node_($i) 30
}

set tcp1 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(1) $tcp1

set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1

set sink1 [new Agent/TCPSink]
$ns_ attach-agent $node_(2) $sink1

$ns_ connect $tcp1 $sink1

$ns_ at 0.5 “$ftp1 start”
$ns_ at 140.0 “$ftp1 stop”

set tcp2 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(3) $tcp2

set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2

set sink2 [new Agent/TCPSink]
$ns_ attach-agent $node_(4) $sink2

$ns_ connect $tcp2 $sink2

$ns_ at 0.5 “$ftp2 start”
$ns_ at 140.0 “$ftp2 stop”

set tcp3 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(5) $tcp3

set ftp3 [new Application/FTP]
$ftp3 attach-agent $tcp3

set sink3 [new Agent/TCPSink]
$ns_ attach-agent $node_(6) $sink3

$ns_ connect $tcp3 $sink3

$ns_ at 0.5 “$ftp3 start”
$ns_ at 140.0 “$ftp3 stop”

set tcp4 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(7) $tcp4

set ftp4 [new Application/FTP]
$ftp4 attach-agent $tcp4

set sink4 [new Agent/TCPSink]
$ns_ attach-agent $node_(8) $sink4

$ns_ connect $tcp4 $sink4

$ns_ at 0.5 “$ftp4 start”
$ns_ at 140.0 “$ftp4 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 1.1 “plotWindow $tcp1 $windowsVsTime2”
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop) “$node_($i) reset “;
}
$ns_ at $val(stop) “stop”
$ns_ at [expr $val(stop)+ 0.1 ] “puts “end simulation” ; $ns_ halt”
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
# Tell nodes when the simulation ends

for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 “$node_($i) reset”;
}
$ns_ at $val(stop).000 “$ns_ halt”
$ns_ run

 

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.