How to Implement Grid Topology in NS2

 

To implement the Grid Topology is a network arrangement in which the nodes are located in a grid (or matrix-like) structure, and every node is connected to its neighbours in the horizontal and also vertical directions. This kind of topology is general in wireless sensor networks and mesh networks. In NS2 that is Network Simulator 2, we can execute a grid topology by placing a nodes in a grid structure and also connecting every node to its neighbours.

We provide stepwise detailed procedure that helps to know on how to implement the grid topology in the NS2:

Steps to Implement Grid Topology in NS2:

  1. Set Up the Grid Network Topology:
    • Describe the nodes and arrange them in a grid.
    • Make a duplex links among every node and its neighbours in the grid.
  2. Simulate Traffic Flow:
    • Create the traffic among nodes over the grid and permitting the data to traverse via the neighbouring nodes.

Example of Grid Topology Implementation in NS2:

Given below is an example script that explained how to configure a 3×3 grid topology (a grid with 9 nodes) and mimic traffic among them:

# Create a new simulator

set ns [new Simulator]

# Open trace file for output

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Define nodes for a 3×3 grid topology (9 nodes)

set n00 [$ns node]  ;# Node at position (0,0)

set n01 [$ns node]  ;# Node at position (0,1)

set n02 [$ns node]  ;# Node at position (0,2)

set n10 [$ns node]  ;# Node at position (1,0)

set n11 [$ns node]  ;# Node at position (1,1)

set n12 [$ns node]  ;# Node at position (1,2)

set n20 [$ns node]  ;# Node at position (2,0)

set n21 [$ns node]  ;# Node at position (2,1)

set n22 [$ns node]  ;# Node at position (2,2)

# ======= CREATE DUPLEX LINKS TO FORM A 3×3 GRID =======

# Connect rows (horizontal links)

$ns duplex-link $n00 $n01 1Mb 10ms DropTail  ;# Link between (0,0) and (0,1)

$ns duplex-link $n01 $n02 1Mb 10ms DropTail  ;# Link between (0,1) and (0,2)

$ns duplex-link $n10 $n11 1Mb 10ms DropTail  ;# Link between (1,0) and (1,1)

$ns duplex-link $n11 $n12 1Mb 10ms DropTail  ;# Link between (1,1) and (1,2)

$ns duplex-link $n20 $n21 1Mb 10ms DropTail  ;# Link between (2,0) and (2,1)

$ns duplex-link $n21 $n22 1Mb 10ms DropTail  ;# Link between (2,1) and (2,2)

# Connect columns (vertical links)

$ns duplex-link $n00 $n10 1Mb 10ms DropTail  ;# Link between (0,0) and (1,0)

$ns duplex-link $n10 $n20 1Mb 10ms DropTail  ;# Link between (1,0) and (2,0)

$ns duplex-link $n01 $n11 1Mb 10ms DropTail  ;# Link between (0,1) and (1,1)

$ns duplex-link $n11 $n21 1Mb 10ms DropTail  ;# Link between (1,1) and (2,1)

$ns duplex-link $n02 $n12 1Mb 10ms DropTail  ;# Link between (0,2) and (1,2)

$ns duplex-link $n12 $n22 1Mb 10ms DropTail  ;# Link between (1,2) and (2,2)

# ======= TRAFFIC SIMULATION =======

# Define TCP agents for communication between nodes

# Traffic from node (0,0) to node (2,2) (across the grid)

set tcp0 [new Agent/TCP]

set sink0 [new Agent/TCPSink]

$ns attach-agent $n00 $tcp0

$ns attach-agent $n22 $sink0

$ns connect $tcp0 $sink0

# Simulate FTP traffic from node (0,0) to node (2,2)

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

$ns at 1.0 “$ftp0 start”   ;# Start traffic at 1 second

# Traffic from node (1,1) to node (0,2)

set tcp1 [new Agent/TCP]

set sink1 [new Agent/TCPSink]

$ns attach-agent $n11 $tcp1

$ns attach-agent $n02 $sink1

$ns connect $tcp1 $sink1

# Simulate FTP traffic from node (1,1) to node (0,2)

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

$ns at 2.0 “$ftp1 start”   ;# Start traffic at 2 seconds

# End the simulation after 10 seconds

$ns at 10.0 “finish”

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Run the simulation

$ns run

Explanation of the Script:

  1. Nodes:
    • These nodes n00, n01, n02, n10, n11, n12, n20, n21, and n22 are signify the 9 nodes arranged in a 3×3 grid.
  2. Links:
    • Horizontal links: Duplex links connect adjacent nodes in the similar row. For instance, the node n00 is connected to n01, n01 is connected to n02, etc.
    • Vertical links: Duplex links connect the adjacent nodes in the similar column. For sample, the node n00 is connected to n10, n10 is connected to n20, etc.
  3. Traffic Simulation:
    • TCP agents mimic communication among the nodes.
    • FTP (File Transfer Protocol) produces traffic, mimicking file transfers among the nodes.
    • In this specimen, the traffic flows from n00 (node at position 0, 0) to n22 (node at position 2, 2) and from n11 to n02.
  4. Simulation Duration:
    • The simulation runs for 10 seconds that permitting traffic to flow over this topology.

Post-Simulation Analysis:

  1. Trace File Analysis:
    • Now, open the trace file (out.tr) to monitor how packets are forwarded among the nodes along the grid. Evaluate the parameters such as packet delivery, delay, and throughput for communication among the nodes.
  2. NAM Visualization:
    • We can use the NAM (Network Animator) to envision the grid topology. We will observe the nodes arranged in a grid structure with horizontal and vertical links connecting them.
  3. Performance Metrics:
    • Estimate the network performance metrics like delay, throughput, and packet loss to measure how effectively the grid topology manages the traffic.

Enhancing the Simulation:

  1. Adding More Nodes:
    • Expand the grid to a larger size such as a 4×4 or 5×5 grid and monitor how performance alters with more nodes.
  2. Changing Traffic Type:
    • Substitute the TCP with UDP or use CBR (Constant Bit Rate) instead of FTP to mimic various kinds of traffic and test how the network behaves.
  3. Simulating Traffic in Both Directions:
    • Configure extra traffic in both directions such as from n22 to n00 or from n02 to n11 to mimic bidirectional communication over the grid.
  4. Varying Link Parameters:
    • Analyse with various link parameters like bandwidth, delay, and queue type such as DropTail or RED to learn how the network performance modifies under several conditions.
  5. Simulating Link Failures:
    • To replicate the link failures by disconnecting links among the nodes and monitor on how the grid topology manages disruptions and reroutes traffic via other obtainable paths.

The step-by-step approach was applied to the Grid topology, with the implementation and analysis performed over the simulation tool NS2. Further details will be provided elaborately.

For obtaining your Grid Topology in NS2, feel free to visit ns2project.com. We offer excellent simulation support and are dedicated to helping you with wireless sensor networks and mesh networks  related aspects of your project.