How to Calculate Network the Velocity of the Node in NS2

To calculate the velocity of a node in NS2, we essential to monitor the positions of the node at diverse timestamps in the course of the simulation and then estimate the distance travelled over time. Velocity is usually expressed as distance per unit of time like meters per second. Below is the procedure to calculate the velocity of the node in ns2:

Formula for Node Velocity:

The velocity of a node can be estimated using the formula:

Velocity (m/s)=Distance Traveled (meters)Time Interval (seconds)\text{Velocity (m/s)} = \frac{\text{Distance Traveled (meters)}}{\text{Time Interval (seconds)}}Velocity (m/s)=Time Interval (seconds)Distance Traveled (meters)​

Where:

  • Distance Traveled is the distance the node transfers among two time points, that can be intended using the Euclidean distance formula.
  • Time Interval is the difference in time among two consecutive recorded positions.

Steps to Calculate the Node Velocity:

  1. Define a Mobility Model: The node’s movement in NS2 is overseen by a mobility model, like Random Waypoint, Random Walk, or other predefined designs. These mobility designs control the speed and movement of nodes in the simulation.
  2. Track Node Position: The trace file created by NS2 that contain the data about the node’s position at diverse times. We can extract this information and estimate the velocity by defining how far the node transfers over a time interval.
  3. Calculate the Velocity: Using the positions recorded at two diverse timestamps, estimates the velocity by dividing the distance travelled by the time elapsed.

Example Tcl Script for NS2 Simulation with Mobility:

Here’s an instance of NS2 Tcl script in which nodes transfer based on the Random Waypoint mobility model. The mobility patterns are set for nodes to transfer randomly within a specified area.

# Create a new simulator instance

set ns [new Simulator]

# Define the simulation area

set val(x) 500  ;# X dimension of the simulation area (meters)

set val(y) 500  ;# Y dimension of the simulation area (meters)

# Create nodes

set num_nodes 5

for {set i 0} {$i < $num_nodes} {incr i} {

set node($i) [$ns node]

$node($i) set X_ [expr rand()*$val(x)]

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

$node($i) set Z_ 0.0

}

# Define mobility model (Random Waypoint)

for {set i 0} {$i < $num_nodes} {incr i} {

$ns at 0.0 “$node($i) setdest [expr rand()*$val(x)] [expr rand()*$val(y)] 10.0”

$ns at 5.0 “$node($i) setdest [expr rand()*$val(x)] [expr rand()*$val(y)] 15.0”

}

# Open trace file to record events

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Simulation end

$ns at 10.0 “finish”

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Run the simulation

$ns run

In this script:

  • Nodes are allocated random destinations within a 500×500 meter area.
  • The speed is begins to set to 10 meters/second and then variations to 15 meters/second after 5 seconds.
  1. Extract Node Position from the Trace File:

In the NS2 trace file, node position variations are recorded in lines that start with M. These lines log the node’s position at a particular time.

Example Trace File Output (for node positions):

Copy code

M 0.1 _1_ 100.0 200.0 0.0

M 0.2 _1_ 150.0 250.0 0.0

M 0.3 _2_ 200.0 300.0 0.0

M 0.4 _1_ 200.0 350.0 0.0

Here:

  • M indicates a mobility event.
  • 0.1 and 0.2 are the timestamps (in seconds).
  • _1_ refers to node 1.
  • 100.0, 200.0 are the X and Y coordinates of the node at the given timestamp.
  1. Calculate Distance Traveled:

To estimate the velocity, we first required to estimate the distance the node has travelled among two consecutive timestamps.

The Euclidean distance between two points (X1,Y1)(X_1, Y_1)(X1​,Y1​) and (X2,Y2)(X_2, Y_2)(X2​,Y2​) can be calculated as:

Distance=(X2−X1)2+(Y2−Y1)2\text{Distance} = \sqrt{(X_2 – X_1)^2 + (Y_2 – Y_1)^2}Distance=(X2​−X1​)2+(Y2​−Y1​)2​

  1. Calculate the Velocity:

Once we have the distance and time interval, we can estimate the velocity using:

Velocity=Distance TraveledTime Interval\text{Velocity} = \frac{\text{Distance Traveled}}{\text{Time Interval}}Velocity=Time IntervalDistance Traveled​

Example Bash Script to Calculate Node Velocity:

# Extract node mobility events for node 1

grep “^M” trace.tr | grep “_1_” > node1_mobility.txt

# Calculate velocity for node 1

awk ‘{

if (prev_time != “”) {

dx = $3 – prev_x;

dy = $4 – prev_y;

dist = sqrt(dx*dx + dy*dy);

time_diff = $1 – prev_time;

velocity = dist / time_diff;

print “Time: ” $1 “, Distance: ” dist ” meters, Velocity: ” velocity ” m/s”;

}

prev_time = $1;

prev_x = $3;

prev_y = $4;

}’ node1_mobility.txt

In this script:

  • $1 is the timestamp.
  • $3 and $4 are the X and Y coordinates, respectively.
  • The script estimate the distance among consecutive positions and divides it by the time difference to estimate the velocity.

Example Calculation:

Let’s say:

  • At time 0.1 seconds, the node’s position is (100.0,200.0)(100.0, 200.0)(100.0,200.0).
  • At time 0.2 seconds, the node’s position is (150.0,250.0)(150.0, 250.0)(150.0,250.0).

The distance traveled is:

Distance=(150.0−100.0)2+(250.0−200.0)2=502+502=5000=70.71 meters\text{Distance} = \sqrt{(150.0 – 100.0)^2 + (250.0 – 200.0)^2} = \sqrt{50^2 + 50^2} = \sqrt{5000} = 70.71 \text{ meters}Distance=(150.0−100.0)2+(250.0−200.0)2​=502+502​=5000​=70.71 meters

If the time interval is 0.1 seconds, the velocity is:

Velocity=70.71 meters0.1 seconds=707.1 meters/second\text{Velocity} = \frac{70.71 \text{ meters}}{0.1 \text{ seconds}} = 707.1 \text{ meters/second}Velocity=0.1 seconds70.71 meters​=707.1 meters/second

  1. Calculate Average Node Velocity:

We can also estimate the average velocity of the node across the entire simulation by sum all distances travelled and dividing by the total simulation time.

# Calculate total distance and average velocity

awk ‘{

if (prev_time != “”) {

dx = $3 – prev_x;

dy = $4 – prev_y;

dist = sqrt(dx*dx + dy*dy);

time_diff = $1 – prev_time;

total_distance += dist;

total_time += time_diff;

}

prev_time = $1;

prev_x = $3;

prev_y = $4;

} END {

avg_velocity = total_distance / total_time;

print “Total Distance: ” total_distance ” meters, Average Velocity: ” avg_velocity ” m/s”;

}’ node1_mobility.txt

Summary:

  1. Run the NS2 simulation with a mobility model, make sure that node positions are followed in the trace file.
  2. Extract node positions from the trace file at diverse timestamps.
  3. Calculate the distance travelled using the Euclidean distance formula.
  4. Compute the velocity by dividing the distance travelled by the time interval.
  5. Optional: Estimate the average velocity over the simulation period by sum all distances and dividing by the total time.

From the entire page, we had collected the most essential information that will very helpful to compute the velocity of a node in ns2 tool. If you want more details regarding this process we will provide it. To calculate the velocity of a node in NS2 tool for your project ns2project.com will serve you right with tailored project ideas and topics. Drop us all your project details we will provide you with immediate help.