How to Implement Network Node Deployment in NS2

To implement the Network Node Deployment using NS2 that has contains placing nodes in the particular places in the simulation area that can be completed in a random or deterministic (grid or predefined) manner rely on the scenario. It is critical stage in simulating several network topologies with the sensor networks, mobile ad-hoc networks (MANETs), or wireless networks. We give stepwise procedure on how to implement it in NS2:

Steps to Implement Node Deployment in NS2

  1. Set up the NS2 Environment

Make certain that NS2 is installed and set up on the computer.

  1. Random Node Deployment

In the random node deployment where nodes are placed randomly in a provided simulation area. This kind of deployment is frequently used in Wireless Sensor Networks (WSN) that sensors are arbitrarily scattered in a field.

Example TCL Script for Random Node Deployment:

# Create a new NS2 simulator

set ns [new Simulator]

# Open trace and NAM output files

set tracefile [open random_deployment.tr w]

$ns trace-all $tracefile

set namfile [open random_deployment.nam w]

$ns namtrace-all $namfile

# Define network parameters

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

set val(prop)   Propagation/TwoRayGround ;# Propagation model

set val(ant)    Antenna/OmniAntenna      ;# Omni-directional antenna

set val(netif)  Phy/WirelessPhy          ;# Physical layer

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

set val(ifq)    Queue/DropTail/PriQueue

set val(ifqlen) 50                       ;# Queue length

set val(ll)     LL

set val(rp)     AODV                     ;# Ad-hoc routing protocol

set val(x)      1000                     ;# X-dimension of network area

set val(y)      1000                     ;# Y-dimension of network area

set val(num_nodes) 50                    ;# Number of nodes

# Create topography

set topo [new Topography]

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

# Configure node parameters

$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 ON

# Create nodes and randomly place them in the network

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

set node($i) [$ns node]

$node($i) set X_ [expr rand() * $val(x)]  ;# Random X position

$node($i) set Y_ [expr rand() * $val(y)]  ;# Random Y position

$node($i) set Z_ 0                        ;# 2D plane (Z-coordinate is 0)

}

# Create UDP traffic between random nodes

set udp0 [new Agent/UDP]

$ns attach-agent $node(0) $udp0

set null0 [new Agent/Null]

$ns attach-agent $node(10) $null0

$ns connect $udp0 $null0

# Configure CBR traffic over UDP

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb

$cbr0 attach-agent $udp0

# Schedule traffic start and stop

$ns at 1.0 “$cbr0 start”

$ns at 9.0 “$cbr0 stop”

# End the simulation

$ns at 10.0 “finish”

# Finish procedure to close trace and NAM files

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam random_deployment.nam &

exit 0

}

# Run the simulation

$ns run

  1. Explanation of Key Components
  • Random Deployment: These nodes are located randomly using the rand() function that produces random X and Y coordinates for each node in the indicated area (1000 x 1000 meters).
  • UDP/CBR Traffic: UDP traffic is made among two randomly selected nodes such as node(0) and node(10), replicating communication among two points within the network.
  • NAM Visualization: The deployment can be envisioned in NAM to observe how the nodes are located in the network.
  1. Run the Simulation

We can save the script as random_deployment.tcl then we run it in NS2:

ns random_deployment.tcl

When the simulation done, we can visualize the network using NAM:

nam random_deployment.nam

  1. Deterministic Node Deployment (Grid-based)

In the deterministic node deployment or grid-based nodes are located in particular positions based on a predefined pattern. It is frequently used in the structured sensor networks, in which nodes are placed at usual intervals.

Example TCL Script for Grid-based Node Deployment:

# Create a new NS2 simulator

set ns [new Simulator]

# Open trace and NAM output files

set tracefile [open grid_deployment.tr w]

$ns trace-all $tracefile

set namfile [open grid_deployment.nam w]

$ns namtrace-all $namfile

# Define network parameters

set val(chan)   Channel/WirelessChannel

set val(prop)   Propagation/TwoRayGround

set val(ant)    Antenna/OmniAntenna

set val(netif)  Phy/WirelessPhy

set val(mac)    Mac/802_11

set val(ifq)    Queue/DropTail/PriQueue

set val(ifqlen) 50

set val(ll)     LL

set val(rp)     AODV

set val(x)      1000

set val(y)      1000

set val(grid_spacing) 100  ;# Distance between nodes in the grid

set val(num_rows) 10       ;# Number of rows

set val(num_cols) 10       ;# Number of columns

# Create topography

set topo [new Topography]

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

# Configure node parameters

$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 ON

# Create nodes in a grid layout

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

for {set j 0} {$j < $val(num_cols)} {incr j} {

set node [expr $i * $val(num_cols) + $j]

set node($node) [$ns node]

$node($node) set X_ [expr $j * $val(grid_spacing)]

$node($node) set Y_ [expr $i * $val(grid_spacing)]

$node($node) set Z_ 0

}

}

# Create UDP traffic between specific nodes

set udp0 [new Agent/UDP]

$ns attach-agent $node(0) $udp0

set null0 [new Agent/Null]

$ns attach-agent $node(99) $null0

$ns connect $udp0 $null0

# Configure CBR traffic

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb

$cbr0 attach-agent $udp0

# Schedule traffic

$ns at 1.0 “$cbr0 start”

$ns at 9.0 “$cbr0 stop”

# End the simulation

$ns at 10.0 “finish”

# Finish procedure to close trace and NAM files

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam grid_deployment.nam &

exit 0

}

# Run the simulation

$ns run

  1. Explanation of Key Components
  • Grid Deployment: These nodes are located in a grid layout, with spacing defined by the variable grid_spacing. The grid is made with a predefined number of rows (num_rows) and columns (num_cols).
  • UDP/CBR Traffic: Communication is configure among the nodes node(0) (top-left corner) and node(99) (bottom-right corner) to mimic data transmission over the grid.
  1. Run the Simulation

We can save the script as grid_deployment.tcl then we run it in NS2:

ns grid_deployment.tcl

After running the simulation, we envision the network using NAM:

nam grid_deployment.nam

  1. Advanced Deployment Options

We can expand the node deployment within NS2 with more furthered features, like:

  1. a) Cluster-based Deployment

These nodes can be grouped into the clusters that replicating a hierarchical network structure.

  1. b) 3D Node Deployment

If the network needs 3D space such as drone networks or underwater sensor networks, we can also place the Z_ coordinate to non-zero values.

  1. c) Dynamic Node Deployment

For situations in which nodes are deployed actively (e.g., drones or mobile nodes), we can update the node positions when the simulation using the setdest command:

# Move node 0 to a new location during the simulation

$ns at 5.0 “$node(0) setdest 500 500 10”

As illustrated above we delivered entire approaches to implement the Network Node Deployment utilizing the NS2 environment. We are prepared to share more informations as required.

Approach ns2project.com for customized implementation assistance with Network Node Deployment in NS2 instruction and project ideas. Stay in touch with us to receive the finest implementation help and outcomes. Our researchers will undertake the finest performance analysis possible, so please share all of your project information with us for the best implementation. Our team works on modeling various network topologies utilizing sensor networks, mobile ad-hoc networks (MANETs), and wireless networks that are relevant to our initiatives.