How to Calculate Network Size in NS2

To calculate Network size in NS2 has denoted to numerous parameters that demonstrated the features of a network. The common parameter that has contains the number of nodes, the physical area covered by the network, and the communication range of each node. Liable on the requirements, we can compute one or more of these parameters. The following are the diverse ways to describe and compute the network size in ns2:

Step-by-Step Implementation:

  1. Number of Nodes:
  • This is the simplest parameter of network size, denoting to the total number of nodes in the simulation.
  • We can count the number of nodes generated in the simulation that is generally specified in the TCL script.

Example TCL Code to Set Up and Count Nodes:

# Define the number of nodes in the network

set num_nodes 10

# Create nodes

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

set node_($i) [$ns node]

}

# Print the number of nodes (i.e., network size)

puts “Network Size: $num_nodes nodes”

In this sample, the network size in terms of the number of nodes is simply the value of num_nodes.

  1. Physical Area (Geographical Network Size):
  • The physical area is another way to designate the network size, especially in wireless networks.
  • In NS2, the physical area is distinct using the topography object. The area is indicated by providing dimensions like 1000 meters by 1000 meters during the setup of the simulation.

Example TCL Code to Define Physical Area:

# Define Simulator

set ns [new Simulator]

# Create a Topography object for a 1000m x 1000m area

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Print the network area

puts “Network Size (Area): 1000m x 1000m”

In this case, the network size in terms of physical area is 1000m x 1000m.

  1. Communication Range:
  • The communication range of each node impacts the effective network size, as nodes can only interact with other nodes within their interaction range.
  • We can estimate the effective communication area by defining the communication range of each node using the propagation model such as Free-Space or Two-Ray Ground and node metrics like transmission power.

Example to Set and Calculate Communication Range:

# Define wireless channel and propagation model

set chan_ [new Channel/WirelessChannel]

set prop_ [new Propagation/FreeSpace]

# Define transmission power and set the model

set netif_ [new Phy/WirelessPhy]

$netif_ set Pt_ 0.2818  ;# Transmission power in watts

$netif_ set Gt_ 1.0     ;# Transmitter antenna gain

$netif_ set Gr_ 1.0     ;# Receiver antenna gain

$netif_ set freq_ 914e6 ;# Frequency in Hz (e.g., 914 MHz)

$netif_ set RXThresh_ 3.652e-10  ;# Reception threshold (minimum power)

# Calculate communication range using Free-Space Path Loss model

proc calc_communication_range {} {

set lambda [expr 3.0e8 / 914e6]  ;# Wavelength (speed of light / frequency)

set Pt 0.2818  ;# Transmit power in watts

set Gt 1.0     ;# Transmit gain

set Gr 1.0     ;# Receive gain

set RXThresh 3.652e-10  ;# Reception threshold

set L 1.0      ;# System loss

# Calculate the communication range

set range [expr ($lambda / (4 * 3.1415)) * sqrt(($Pt * $Gt * $Gr) / ($RXThresh * $L))]

puts “Communication Range: $range meters”

}

# Call the function to calculate the communication range

calc_communication_range

This will print the communication range of a node that can utilize to regulate the effective network size in terms of communication reach.

  1. Effective Coverage Area:
  • Another parameter for network size is the effective coverage area that relays on the communication range and node density.
  • We can estimate the coverage area as the integrated area sheltered by the communication ranges of all nodes.

Example TCL Code for Coverage Area:

# Assuming circular communication area with radius = communication range

proc calc_coverage_area {num_nodes range} {

set total_area [expr $num_nodes * 3.1415 * $range * $range]

puts “Total Coverage Area: $total_area square meters”

}

# Assuming each node has a communication range of 200 meters

calc_coverage_area 10 200

This will estimate and print the total coverage area of the network according to the number of nodes and their interaction range.

  1. Calculating Network Size Dynamically:

In more complex scenarios in which nodes can be mobile or have dynamic communication ranges, so we can dynamically compute the network size during the simulation by intermittently validating the node positions and their communication status.

Example of Dynamically Calculating Active Nodes:

# Function to calculate active nodes in communication range

proc calc_active_nodes {} {

global ns node_

set active_nodes 0

# Check distances between each pair of nodes

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

for {set j [expr $i + 1]} {$j < $num_nodes} {incr j} {

set x1 [$node_($i) set X_]

set y1 [$node_($i) set Y_]

set x2 [$node_($j) set X_]

set y2 [$node_($j) set Y_]

# Calculate the Euclidean distance between nodes

set distance [expr sqrt(($x1 – $x2) * ($x1 – $x2) + ($y1 – $y2) * ($y1 – $y2))]

# If the distance is less than the communication range, increment the active node count

if {$distance <= 200} {

incr active_nodes

}

}

}

puts “Active Nodes in Communication Range: $active_nodes”

}

# Schedule the function to run periodically

$ns at 5.0 “calc_active_nodes”

This will validate that nodes are within communication range and print the number of active nodes that can interact with each other, providing a dynamic view of the network size based on connectivity.

By using the above techniques we had successfully compute the network size over the network in ns2 tool. If you need more details about how the network size perform in other simulation tools.

Connect  with ns2project.com because figuring out the network size in NS2 can be quite challenging. We’re here to help you achieve the best results!