How to Simulate Flat Topology Projects Using NS2

To simulate a Flat Topology in NS2 has includes to generate a network in which all nodes are correspondingly linked without any hierarchical structure (nothing like hierarchical or multi-tier topologies). Basically in flat topologies, every node has the similar role in the network, and data communication happens among them using either direct connections or routing protocols for multi-hop communication. This type of topology is usually utilized in ad-hoc networks and wireless sensor networks (WSNs).

Here’s how to simulate a Flat Topology project using NS2:

Steps to Simulate Flat Topology Projects in NS2

  1. Set up NS2 Environment:

Make sure that NS2 is installed and set up appropriately. In a flat topology, all nodes will have a same role and interact directly or via multi-hop routing rely on the network’s design.

  1. Understanding Flat Topology:
  • In a Flat Topology, all nodes are the same, and there is no difference among the servers, routers, or clients. This is usual in wireless ad-hoc networks, peer-to-peer networks, and sensor networks.
  • Nodes interact directly if within range, or over multi-hop communication if the destination is beyond away.
  1. Create a TCL Script for Flat Topology:

Below is a TCL script to replicate a Flat Topology with 6 nodes in NS2. These nodes will utilize AODV (Ad hoc On-Demand Distance Vector) in place of the routing protocol to interconnect in a flat, multi-hop structure.

Flat Topology Simulation

# Create a new simulator instance

set ns [new Simulator]

# Define the type of channel used in the wireless network

set opt(chan)           Channel/WirelessChannel

set opt(prop)           Propagation/TwoRayGround

set opt(netif)          Phy/WirelessPhy

set opt(mac)            Mac/802_11

set opt(ifq)            Queue/DropTail/PriQueue

set opt(ll)             LL

set opt(ant)            Antenna/OmniAntenna

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

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

set opt(ifqlen)         50      ;# Max packets in the interface queue

set opt(seed)           0.0     ;# Seed for random number generation

set opt(adhocRouting)   AODV    ;# Routing protocol for the flat topology

set opt(nn)             6       ;# Number of nodes

set opt(stop)           20.0    ;# Simulation stop time

# Define the node movement model

set opt(cp) “cbr”       ;# Traffic type (constant bit rate)

# Create the topography object

create-god $opt(nn)

# Set up the initial network topology

$ns node-config -adhocRouting $opt(adhocRouting) \

-llType $opt(ll) \

-macType $opt(mac) \

-ifqType $opt(ifq) \

-ifqLen $opt(ifqlen) \

-antType $opt(ant) \

-propType $opt(prop) \

-phyType $opt(netif) \

-channelType $opt(chan) \

-topoInstance [new Topography] \

-wiredRouting OFF \

-agentTrace ON \

-routerTrace ON \

-macTrace ON \

-movementTrace OFF

# Configure the wireless nodes

set topo [new Topography]

$topo load_flatgrid $opt(x) $opt(y)

# Create the nodes

for {set i 0} {$i < $opt(nn)} {incr i} {

set node_($i) [$ns node]

}

# Set node positions in a grid (you can randomize positions too)

$node_(0) set X_ 50.0

$node_(0) set Y_ 50.0

$node_(0) set Z_ 0.0

$node_(1) set X_ 150.0

$node_(1) set Y_ 50.0

$node_(1) set Z_ 0.0

$node_(2) set X_ 250.0

$node_(2) set Y_ 50.0

$node_(2) set Z_ 0.0

$node_(3) set X_ 150.0

$node_(3) set Y_ 150.0

$node_(3) set Z_ 0.0

$node_(4) set X_ 50.0

$node_(4) set Y_ 150.0

$node_(4) set Z_ 0.0

$node_(5) set X_ 250.0

$node_(5) set Y_ 150.0

$node_(5) set Z_ 0.0

# ======= Traffic Generation =======

# Attach UDP agents to some nodes

set udp0 [new Agent/UDP]

$ns attach-agent $node_(0) $udp0

set udp1 [new Agent/UDP]

$ns attach-agent $node_(1) $udp1

set udp2 [new Agent/UDP]

$ns attach-agent $node_(2) $udp2

# Attach Null agents (traffic sinks) to other nodes

set null0 [new Agent/Null]

$ns attach-agent $node_(3) $null0

set null1 [new Agent/Null]

$ns attach-agent $node_(4) $null1

set null2 [new Agent/Null]

$ns attach-agent $node_(5) $null2

# Connect the UDP agents to Null agents

$ns connect $udp0 $null0

$ns connect $udp1 $null1

$ns connect $udp2 $null2

# Create CBR traffic generators and attach them to the UDP agents

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 512

$cbr0 set rate_ 100Kb

set cbr1 [new Application/Traffic/CBR]

$cbr1 attach-agent $udp1

$cbr1 set packetSize_ 512

$cbr1 set rate_ 100Kb

set cbr2 [new Application/Traffic/CBR]

$cbr2 attach-agent $udp2

$cbr2 set packetSize_ 512

$cbr2 set rate_ 100Kb

# Schedule the CBR traffic to start and stop

$ns at 1.0 “$cbr0 start”

$ns at 1.5 “$cbr1 start”

$ns at 2.0 “$cbr2 start”

$ns at 15.0 “$cbr0 stop”

$ns at 15.5 “$cbr1 stop”

$ns at 16.0 “$cbr2 stop”

# Stop the simulation at 20 seconds

$ns at $opt(stop) “finish”

# Procedure to finish the simulation

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

}

# Run the simulation

$ns run

  1. Explanation of the Code:
  • Wireless Nodes: Six wireless nodes (node_(0) to node_(5)) are constructed and configured with the AODV routing protocol. These nodes are employed in a grid structure.
  • UDP Agents: UDP agents are attached to some of the nodes (node_(0), node_(1), node_(2)) for sending data, and Null agents (traffic sinks) are attached to the receiving nodes (node_(3), node_(4), node_(5)).
  • CBR Traffic: Constant Bit Rate (CBR) traffic generators are attached to the UDP agents to replicate data transmission. The traffic initiate at 1.0, 1.5, and 2.0 seconds for diverse nodes.
  • Flat Topology: While all nodes are similarly placed and have the same role, this setup denoted a flat topology, in which each node can interact directly or through multi-hop routing (if the nodes are far apart).
  • Simulation End: The simulation ends at 20 seconds, and the outcomes are saved to a .nam file for visualization.
  1. Run the Simulation:
  1. Save the script as flat_topology.tcl.
  2. Open a terminal and navigate to the folder in which the script is saved.
  3. Execute the simulation using:

ns flat_topology.tcl

  1. The simulation will create an out.nam file that can be opened using Network Animator (NAM) for visualization.
  1. Visualization in NAM:
  • Open the out.nam file in NAM to envision the flat topology. We will see the six nodes interacts in the terms of the AODV routing protocol, with traffic flowing from the UDP sources to the Null agents (traffic sinks).
  1. Customization and Enhancements:
  • Increase Node Count: we can upsurge the amount of nodes by incorporating more nodes and modifying their positions.
  • TCP Traffic: we can replace UDP agents with TCP agents for reliable communication:

set tcp0 [new Agent/TCP]

$ns attach-agent $node_(0) $tcp0

set sink0 [new Agent/TCPSink]

$ns attach-agent $node_(3) $sink0

$ns connect $tcp0 $sink0

  • Node Mobility: We can replicate node mobility by incorporates movement to the nodes:

$node_(0) setdest 100.0 100.0 5.0   ;# Move node 0 to position (100, 100) at speed 5.0

  • Performance Metrics: Incorporate tracing to evaluate parameters such as throughput, delay, and packet loss:

set tracefile [open trace.tr w]

$ns trace-all $tracefile

  1. Performance Analysis:

To evaluate the performance of the flat topology, we can:

  • Evaluate throughput to see how much data is successfully routed among nodes.
  • Assess latency (delay) to assess on how long data takes to travel via multiple hops.
  • Measure packet loss to learn the reliability of the network when traffic is high or nodes are far at a distance.

We were showed you through the implementation process using step-by-step approach regarding the Flat Topology which will be executed, analysed, validated and customized in ns2 environment settings. For you future requirements, we can deliver any extra details on this topic for you.

Our team provides comparison analysis results for your projects. Our team specializes in direct connections and routing protocols for multi-hop communication, so keep in touch for the best outcomes. Check out phdprime.com for guidance on simulating Flat Topology Projects with the NS2 tool. If you need solid research support and simulation help, our experts are here for you.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2