How to Implement Wireless Topology in NS2

To implement a wireless topology in Network Simulator 2 (NS2) has need to describe wireless nodes and setting up their communication performance metrics like transmission range, mobility, and routing protocols. NS2 has built-in support for wireless simulations, that include wireless channels, mobility models, and wireless communication protocols. The given below is the detailed procedure to implement a basic wireless topology in NS2:

Steps to Implement Wireless Topology in NS2

  1. Set up NS2

Makesure NS2 is installed on the system.

sudo apt-get update

sudo apt-get install ns2

  1. Create a TCL Script for Wireless Topology

In wireless topologies:

  • Nodes interact through a wireless channel.
  • We need to mimic mobility using movement patterns.
  • Routing protocols such as AODV, DSDV, or DSR can be implemented to manage the multi-hop communication among the nodes.

Example TCL Script for Wireless Topology:

The below is a sample of a TCL script that mimic a basic wireless topology with 6 nodes that interact over a wireless channel.

# Define the simulator

set ns [new Simulator]

# Open trace files for output

set tracefile [open wireless_out.tr w]

set namfile [open wireless_out.nam w]

$ns trace-all $tracefile

$ns namtrace-all-wireless $namfile 500 500

# Define the topography object

set topo [new Topography]

$topo load_flatgrid 500 500

# Create a wireless channel

set chan_1_ [new Channel/WirelessChannel]

# Define radio propagation model, physical and MAC layers

Phy/WirelessPhy set CPThresh_ 10.0

Phy/WirelessPhy set CSThresh_ 1.0e-10

Phy/WirelessPhy set RXThresh_ 1.0e-10

Phy/WirelessPhy set Rb_ 2Mb

Phy/WirelessPhy set Pt_ 0.2818

Phy/WirelessPhy set freq_ 2.472e9

# Create a global node configuration for wireless

$ns node-config -adhocRouting AODV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channelType $chan_1_ \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON

# Create six wireless nodes

set node_0 [$ns node]

set node_1 [$ns node]

set node_2 [$ns node]

set node_3 [$ns node]

set node_4 [$ns node]

set node_5 [$ns node]

# Define the initial positions of the nodes

$node_0 set X_ 50.0

$node_0 set Y_ 100.0

$node_0 set Z_ 0.0

$node_1 set X_ 150.0

$node_1 set Y_ 100.0

$node_1 set Z_ 0.0

$node_2 set X_ 100.0

$node_2 set Y_ 200.0

$node_2 set Z_ 0.0

$node_3 set X_ 250.0

$node_3 set Y_ 200.0

$node_3 set Z_ 0.0

$node_4 set X_ 350.0

$node_4 set Y_ 150.0

$node_4 set Z_ 0.0

$node_5 set X_ 300.0

$node_5 set Y_ 50.0

$node_5 set Z_ 0.0

# Define a mobility model (for example, no movement initially)

$ns at 1.0 “$node_0 setdest 100.0 200.0 5.0”

$ns at 1.5 “$node_1 setdest 150.0 250.0 5.0”

$ns at 2.0 “$node_2 setdest 200.0 150.0 5.0”

$ns at 2.5 “$node_3 setdest 300.0 300.0 5.0”

$ns at 3.0 “$node_4 setdest 350.0 100.0 5.0”

$ns at 3.5 “$node_5 setdest 400.0 200.0 5.0”

# Set up traffic flow (CBR over UDP) from node_0 to node_5

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $node_0 $udp0

$ns attach-agent $node_5 $null0

$ns connect $udp0 $null0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set interval_ 0.1

$cbr0 attach-agent $udp0

$ns at 1.0 “$cbr0 start”

# Schedule simulation end

$ns at 10.0 “finish”

# Finish procedure to close the simulation and generate output

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam wireless_out.nam &

exit 0

}

# Run the simulation

$ns run

Explanation of the Script:

  1. Wireless Channel and Node Configuration:
    • The script configure a wireless channel using $chan_1_, and set up the physical layer, MAC layer (802.11), and routing protocol (AODV) for all the nodes.
  2. Creating Wireless Nodes:
    • Six nodes (node_0 to node_5) are generaed using the wireless channel, and their initial positions are set using the set X_ and set Y_ commands.
  3. Mobility Model:
    • The nodes are starting stationary; however their positions are updated after 1.0, 1.5, 2.0 seconds, etc., using the setdest command, that describe their destination and speed. This generates mobility in the network.
  4. Traffic Flow:
    • UDP traffic is created from node_0 to node_5 using CBR (Constant Bit Rate), in which the packets are delivering at regular intervals.
  5. Trace and NAM Visualization:
    • The script creates a trace file (wireless_out.tr) and a NAM file (wireless_out.nam). These files can be used to visualize the network topology and traffic in NAM (Network Animator).
  1. Run the Simulation

Save the script as wireless_topology.tcl and execute it in NS2:

ns wireless_topology.tcl

To view the network using NAM, run:

nam wireless_out.nam

Customization:

  1. Node Mobility:
    • Modify the movement speed and direction using the setdest command to mimic diverse mobility scenarios. For instance, we can mimic random waypoint mobility or constant velocity by varying the setdest values.
  2. Routing Protocols:
    • We can switch among diverse wireless routing protocols such as DSDV, AODV, and DSR by adjusting the -adhocRouting option in the node-config section.
  3. Traffic Patterns:
    • We can add more traffic flows among diverse nodes by generating extra UDP agents and CBR traffic. Instead, utilize TCP agents for reliable interaction.
  4. Wireless Parameters:
    • Adapt the physical layer and MAC layer metrics to mimic various wireless communication conditions. For example, we can adjust the transmission power (Pt_) or data rate (Rb_) to see the effects on network performance.

Within this module, we presented the entire demonstration about how to replicate the scenario and analyse the performance regarding the wireless topology in the tool of ns2. Additional in depth details about the wireless topology will offer too.

Get ns2project.com expert help for using Wireless Topology in the NS2 tool. Share your project details with us, and we will help you with a detailed comparison analysis. We provide full implementation support, including wireless channels, mobility models, and wireless communication protocols.