How to Simulate Network Mobility Control in NS2

To implement the Network Mobility Control in NS2 encompasses to simulate an environment with nodes (like mobile devices or vehicles) travel throughout the simulation. NS2 offers assistance for mobility models that control how nodes travel inside the defined area. It requires configuring mobile nodes, stating a mobility pattern and controlling how nodes interact with one another while dispatching.

Here’s the guide on how to implement Network Mobility Control in NS2:

Step-by-Step Guide to Implement Network Mobility Control in NS2

  1. Set Up the Basic Network Topology with Mobile Nodes:
  • In NS2, you have to add the mobility patterns to build nodes as mobile. Start by defining the simplified network topology where nodes are mobile, granting them to travel during the simulation.

Example OTcl script to set up mobile nodes:

set ns [new Simulator]

set nf [open out.tr w]

$ns trace-all $nf

set namfile [open out.nam w]

$ns namtrace-all $namfile

# Define a network with wireless mobile nodes

set topo [new Topography]

$topo load_flatgrid 500 500   ;# Define a 500×500 grid for node movement

# Create two mobile nodes

set node0 [$ns node]

set node1 [$ns node]

# Attach a wireless network interface to the mobile nodes

$node0 set X_ 50  ;# Initial X-coordinate

$node0 set Y_ 50  ;# Initial Y-coordinate

$node0 set Z_ 0   ;# Initial Z-coordinate (2D simulation)

$node1 set X_ 100

$node1 set Y_ 100

$node1 set Z_ 0

# Define mobility for the nodes

$node0 setdest 200 200 10   ;# Move to (200,200) at speed 10 m/s

$node1 setdest 300 300 15   ;# Move to (300,300) at speed 15 m/s

# Run the simulation

$ns at 6.0 “finish”

proc finish {} {

global ns nf namfile

$ns flush-trace

close $nf

close $namfile

exec nam out.nam &

exit 0

}

$ns run

  • This basic setup states a 500×500 grid for two mobile nodes, node0 and node1, and allocate starting positions and destinations. Each node will travel to its destination with the predefined speed.
  1. Define Mobility Models:
  • In NS2, mobility models dictate how mobile nodes move. Regular mobility models include:
    • Random Waypoint: Nodes randomly pick a destination and move toward it, resting for a random amount of time.
    • Random Walk: Nodes travel in random directions for a fixed time.
    • Constant Speed: Nodes move to specified destinations at constant speeds.

Example OTcl script for Random Waypoint Mobility:

# Set up Random Waypoint mobility model for a node

proc random_waypoint {node} {

set x [expr rand() * 500]  ;# Random X within the grid

set y [expr rand() * 500]  ;# Random Y within the grid

set speed [expr 5 + rand() * 10]  ;# Random speed between 5 and 15 m/s

set pause_time [expr rand() * 5]  ;# Random pause time between 0 and 5 seconds

# Move the node to the new random destination

$node setdest $x $y $speed

$ns at [expr $pause_time + 1] “random_waypoint $node”

}

# Start the Random Waypoint mobility for the nodes

$ns at 1.0 “random_waypoint $node0”

$ns at 1.0 “random_waypoint $node1”

  • In this sample, the nodes will randomly move to new destinations inside the defined grid, at random speeds, and pause for a random time.
  1. Implement Network Communication for Mobile Nodes:
  • After configuring the mobility model, you can execute network communication amongst mobile nodes. This frequently involves wireless communication, and in NS2, mobile nodes typically interact using the Ad-hoc On-Demand Distance Vector (AODV) protocol or other routing protocols.

Example OTcl script to enable communication amongst mobile nodes using AODV:

# Set up AODV as the routing protocol for mobile nodes

set val(rp) AODV

# Define traffic source and destination for node communication

set udp [new Agent/UDP]

set null [new Agent/Null]

$ns attach-agent $node0 $udp

$ns attach-agent $node1 $null

$ns connect $udp $null

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ 512

$cbr set rate_ 1Mb

# Start traffic between the mobile nodes

$ns at 2.0 “$cbr start”

$ns at 5.0 “$cbr stop”

  • This example sets up UDP communication amidst node0 and node1 using AODV as the routing protocol. The traffic source is CBR (Constant Bit Rate) traffic.
  1. Implement Control Over Mobility (Changing Speeds, Pausing, etc.):
  • To further control node mobility, you can dynamically change their speeds, pause their movement, or redirect them during the simulation.

Example OTcl script to control mobility (pausing and changing speed):

# Pause the movement of node0 for a specific duration

$ns at 3.0 “$node0 pause 2”   ;# Pause node0 for 2 seconds

# Change the speed of node1 dynamically

proc change_speed {node new_speed} {

set dest_x [$node X_]   ;# Get current destination X

set dest_y [$node Y_]   ;# Get current destination Y

$node setdest $dest_x $dest_y $new_speed

puts “Node speed changed to $new_speed m/s”

}

# Change node1 speed to 20 m/s at 4.0 seconds

$ns at 4.0 “change_speed $node1 20”

  • In this script, node0 pauses for 2 seconds at 3.0 seconds into the simulation, and node1 changes its speed to 20 m/s at 4.0 seconds.
  1. Monitor Mobility Metrics (Position, Speed, Distance):
  • You can observe the mobility metrics like the location, speed, and distance covered by the mobile nodes during the simulation. This helps to analyze the effectiveness of the mobility model and its influence on network performance.

Example OTcl script to log node positions:

# Log the position of the mobile node at intervals

proc log_position {node} {

set x [$node set X_]

set y [$node set Y_]

puts “Node position: X=$x, Y=$y”

}

# Periodically log node positions

$ns at 2.0 “log_position $node0”

$ns at 3.0 “log_position $node1”

  • This logs the locations of the nodes at specified times to track their movement.
  1. Run the Simulation and Collect Results:
  • After setting up mobility control, execute the simulation to see how nodes travel and interact in the network.

To run the simulation:

ns your_script.tcl

  • The trace file (out.tr) will capture the places, movement, packet transmissions, and other metrics during the simulation.
  1. Analyze Mobility Control Metrics:
  • Node Position: Find the positions of nodes over time to make sure they are moving as predicted.
  • Communication Performance: Compute packet delivery, delay, and throughput as the nodes move.
  • Routing Efficiency: Analyze how effectively routing protocols (for instance: AODV) manage node mobility as well as how frequently routes varies.

Example AWK script to observe packet delivery:

awk ‘{if ($1 == “r” && $4 == “UDP”) received_packets++;}

END { print “Total received packets:”, received_packets }’ out.tr

  • This script counts the number of obtained UDP packets to assess network performance during mobility.
  1. Visualize Mobility in NAM:
  • NAM (Network Animator) is used to visualize node movement and communication in real-time.

nam out.nam

  • In NAM, you can see how the nodes travel inside the defined area, how they interact, and how mobility influences network performance.

With this step-by-step approach, we have clearly presented the necessary information about the Network Mobility Control that explains how to set up the network topology and how to define mobility nodes and how to establish the communication into the network to establish it into the simulation with the help of ns2 tool.

For a successfully implementation of  Network Mobility Control in the NS2 tool with great efficiency you can rely on us . If you need expert guidance, feel free to contact us! We have all the resources needed to provide you with timely and effective support.