How to Calculate Network Traveling path length in NS2
To calculate the traveling path length in ns2 has needs to define the total distance a packet routed from the source node to the destination node, that is the sum of the distances among each pair of following nodes on the packet’s path. To estimating the traveling path length needs to understanding the positions of the nodes and the routing path that the packet takes. The following are the approaches on how to calculate the network travelling path length in ns2:
Steps to Calculate Network Traveling Path Length in NS2:
- Set up the Simulation: Generate a network with nodes, links, and traffic flows. The packet’s route will be determined according to the routing protocol such as AODV, DSR, DSDV.
- Track the Routing Path: Utilize NS2’s trace file to log the route taken by each packet that has the intermediate nodes it passes through.
- Extract Node Positions: Acquire the coordinates of each node to estimate the distance among the nodes.
- Calculate the Distance between Nodes: Utilize the Euclidean distance formula to estimate the distance among each consecutive node on the packet’s route.
- Sum the Distances: Summation the distances among all following nodes on the packet’s route to acquire the total traveling path length.
Example Tcl Script to Simulate a Simple Network:
Here’s a sample NS2 Tcl script that mimics a network with a few nodes. The trace file will log the packets’ paths, and we can extract the node positions to estimate the traveling path length.
# Create a new simulator instance
set ns [new Simulator]
# Define node positions (x, y coordinates)
set val(x) 500 ;# X dimension of the simulation area
set val(y) 500 ;# Y dimension of the simulation area
# Create nodes
set node0 [$ns node]
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
# Set node positions
$node0 set X_ 50.0
$node0 set Y_ 100.0
$node0 set Z_ 0.0
$node1 set X_ 150.0
$node1 set Y_ 200.0
$node1 set Z_ 0.0
$node2 set X_ 250.0
$node2 set Y_ 300.0
$node2 set Z_ 0.0
$node3 set X_ 350.0
$node3 set Y_ 400.0
$node3 set Z_ 0.0
$node4 set X_ 450.0
$node4 set Y_ 500.0
$node4 set Z_ 0.0
# Define network links
$ns duplex-link $node0 $node1 1Mb 10ms DropTail
$ns duplex-link $node1 $node2 1Mb 10ms DropTail
$ns duplex-link $node2 $node3 1Mb 10ms DropTail
$ns duplex-link $node3 $node4 1Mb 10ms DropTail
# Attach UDP agents and connect traffic
set udp0 [new Agent/UDP]
set null0 [new Agent/Null]
$ns attach-agent $node0 $udp0
$ns attach-agent $node4 $null0
$ns connect $udp0 $null0
# Create traffic source (CBR)
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 512
$cbr0 set rate_ 1Mb
# Start traffic at 1 second and stop at 5 seconds
$ns at 1.0 “$cbr0 start”
$ns at 5.0 “$cbr0 stop”
# Open trace file to log events
set tracefile [open trace.tr w]
$ns trace-all $tracefile
# Define finish procedure to close trace file and stop the simulation
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# End simulation at 6 seconds
$ns at 6.0 “finish”
$ns run
- Trace File Analysis for Path Length:
The trace file will log all the packet transmissions and receptions among the nodes. By evaluating the trace file, we can classify the nodes that the packet traverses on its path from the source to the destination.
Example Trace File Output:
s 1.0 _0_ AGT — 512 [0 0 0 0] ——- [0:0 4:0 32 0]
r 1.2 _1_ AGT — 512 [0 0 0 0] ——- [0:0 4:0 32 0]
r 1.4 _2_ AGT — 512 [0 0 0 0] ——- [0:0 4:0 32 0]
r 1.6 _3_ AGT — 512 [0 0 0 0] ——- [0:0 4:0 32 0]
r 1.8 _4_ AGT — 512 [0 0 0 0] ——- [0:0 4:0 32 0]
In this example:
- s represents a packet sent.
- r represents a packet received at each intermediate node (_1_, _2_, _3_) before reaching the destination (_4_).
- Calculate the Distance Between Nodes:
To compute the distance among two nodes, use the Euclidean distance formula:
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
Where:
- X1,Y1X_1, Y_1X1,Y1 are the coordinates of the first node.
- X2,Y2X_2, Y_2X2,Y2 are the coordinates of the second node.
For example, the distance among node0 and node1 would be:
Distance0→1=(150.0−50.0)2+(200.0−100.0)2=1002+1002=20000=141.42 meters\text{Distance}_{0 \to 1} = \sqrt{(150.0 – 50.0)^2 + (200.0 – 100.0)^2} = \sqrt{100^2 + 100^2} = \sqrt{20000} = 141.42 \text{ meters}Distance0→1=(150.0−50.0)2+(200.0−100.0)2=1002+1002=20000=141.42 meters
- Sum the Distances:
After estimating the distance among each consecutive pair of nodes on the route, summation these distances to get the total traveling path length.
Bash Script for Automating the Calculation:
# Extract the sequence of nodes from the trace file
grep “^r” trace.tr | awk ‘{print $3}’ > path_nodes.txt
# Define node positions (X and Y coordinates)
declare -A x_coords
declare -A y_coords
x_coords[0]=50.0
y_coords[0]=100.0
x_coords[1]=150.0
y_coords[1]=200.0
x_coords[2]=250.0
y_coords[2]=300.0
x_coords[3]=350.0
y_coords[3]=400.0
x_coords[4]=450.0
y_coords[4]=500.0
# Initialize total path length
total_path_length=0
# Read the sequence of nodes and calculate distances
prev_node=-1
while read -r node; do
if [ $prev_node -ne -1 ]; then
dx=$(echo “${x_coords[$node]} – ${x_coords[$prev_node]}” | bc)
dy=$(echo “${y_coords[$node]} – ${y_coords[$prev_node]}” | bc)
distance=$(echo “scale=2; sqrt($dx^2 + $dy^2)” | bc)
total_path_length=$(echo “$total_path_length + $distance” | bc)
fi
prev_node=$node
done < path_nodes.txt
echo “Total Traveling Path Length: $total_path_length meters”
This script:
- Extracts the sequence of nodes from the trace file in which the packets are received.
- Usages the Euclidean distance formula to analyse the distance among the following nodes.
- Calculations the distances to acquire the total traveling path length.
- Summary:
- Run the Simulation: Generate a network in NS2, describe node positions, and create traffic among nodes.
- Trace the Path: Use the trace file to log the route taken by the packet and find the sequence of nodes intricate.
- Calculate the Path Length: To estimate the distance among each pair of nodes on the transmission using the Euclidean distance formula.
- Sum the Distances: Summation the distances among the successive nodes to acquire the total traveling path length.
Here, we collects the essential information and delivers on how the network traveling path length will perform in ns2 simulation and we help to provide further information about how the network traveling path length will adapt in different environments.
Send us all the information about your project and project manager, and we’ll assist you right away. With customized project ideas and topics, ns2project.com will assist you in determining the Network Traveling Path Length for your project using the NS2 tool.