How to Implement Network Multi Hop Energy in NS2

To implement the Multi-hop Energy-aware Routing within NS2, which needs to encompass making a wireless network in which the nodes are forward data via the intermediate nodes (multi-hop), whereas deliberating the energy consumption at every node. The energy efficiency is vital in the networks such as Wireless Sensor Networks (WSN) or Ad-hoc networks that nodes have limited battery life. The gaol is to balance energy consumption over the network and to prevent draining nodes very rapidly. Here is a simple technique to execute it in NS2:

Steps to Implement Multi-hop Energy-aware routing in NS2:

  1. Set up the NS2 Environment

Make sure that NS2 is installed and set up on the machine with the support for wireless simulations, also containing energy models. The simulation environment NS2 has simple support for energy models already that can use to replicate the energy consumption.

  1. Modify the TCL Script for Energy-aware Simulation

The simulation NS2 has an Energy Model made into the simulator, which we can be used to replicate the energy consumption at every node. The energy model permits to describe the primary energy, that energy consumed while the reception, idle times, and transmission.

Example: Multi-hop Energy-aware Simulation

The following is an instance TCL script, which makes a multi-hop wireless network and uses an energy model to mimic the energy consumption at every node.

# Create a new NS2 simulator

set ns [new Simulator]

# Open trace file and nam output file

set tracefile [open energy_multihop.tr w]

$ns trace-all $tracefile

set namfile [open energy_multihop.nam w]

$ns namtrace-all $namfile

# Define topology parameters for wireless nodes

set val(chan)   Channel/WirelessChannel

set val(prop)   Propagation/TwoRayGround

set val(netif)  Phy/WirelessPhy

set val(mac)    Mac/802_11

set val(ifq)    Queue/DropTail/PriQueue

set val(ll)     LL

set val(ant)    Antenna/OmniAntenna

set val(rp)     DSR               ;# Using Dynamic Source Routing (DSR)

set val(x)      500

set val(y)      500

# Create topography

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

# Energy Model Parameters

set val(initialEnergy) 100.0      ;# Initial energy at each node (in Joules)

set val(txPower) 0.5             ;# Transmission power (W)

set val(rxPower) 0.3             ;# Reception power (W)

set val(idlePower) 0.01          ;# Idle power (W)

set val(sleepPower) 0.001        ;# Sleep power (W)

# Configure node properties (Energy model included)

$ns node-config -adhocRouting $val(rp) \

-llType $val(ll) \

-macType $val(mac) \

-ifqType $val(ifq) \

-ifqLen 50 \

-antType $val(ant) \

-propType $val(prop) \

-phyType $val(netif) \

-channelType $val(chan) \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON \

-energyModel EnergyModel \

-initialEnergy $val(initialEnergy) \

-txPower $val(txPower) \

-rxPower $val(rxPower) \

-idlePower $val(idlePower) \

-sleepPower $val(sleepPower)

# Create nodes and set their positions

set node0 [$ns node]

set node1 [$ns node]

set node2 [$ns node]

set node3 [$ns node]

# Define node positions (static for simplicity)

$node0 set X_ 50

$node0 set Y_ 50

$node0 set Z_ 0

$node1 set X_ 150

$node1 set Y_ 150

$node1 set Z_ 0

$node2 set X_ 250

$node2 set Y_ 150

$node2 set Z_ 0

$node3 set X_ 350

$node3 set Y_ 50

$node3 set Z_ 0

# Create traffic flow: Source (node0) to Destination (node3) via multi-hop

set udp0 [new Agent/UDP]

$ns attach-agent $node0 $udp0

set null0 [new Agent/Null]

$ns attach-agent $node3 $null0

$ns connect $udp0 $null0

# Create CBR traffic (Constant Bit Rate) over UDP

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set rate_ 500Kb

$cbr0 attach-agent $udp0

# Schedule the traffic

$ns at 1.0 “$cbr0 start”

$ns at 9.0 “$cbr0 stop”

# Schedule the end of the simulation

$ns at 10.0 “finish”

# Define the finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam energy_multihop.nam &

exit 0

}

# Run the simulation

$ns run

  1. Explanation of Key Components
  • Energy Model:
    • initialEnergy: The initial energy for each node in  Joules.
    • txPower, rxPower, idlePower, and sleepPower: State the power consumption for the transmission, reception, idle, and sleep modes, correspondingly.
  • Nodes: These four wireless nodes (node0 to node3) are made, and their positions are placed in a grid-like manner. The source node (node0) transfers the data to the end node (node3) via the intermediate nodes (node1 and node2).
  • Routing Protocol: For multi-hop communication, DSR (Dynamic Source Routing) is used as the routing protocol. We can be also used AODV or other protocols.
  • Traffic: CBR (Constant Bit Rate) traffic is transferred from the source node to the end node over UDP, replicating a multimedia or data flow.
  • Energy Consumption: When the nodes are transmit, receive, and idle, their energy will be reduced according to the power settings in the energy model.
  1. Run the Simulation

We can save the above script as energy_multihop.tcl then we run the simulation using NS2:

ns energy_multihop.tcl

It will be made a trace file (energy_multihop.tr) and a NAM file (energy_multihop.nam) for visualization.

To envision the network using NAM:

nam energy_multihop.nam

  1. Analyze the Results

You can analyze the energy consumption and performance metrics from the trace file. In the trace file (energy_multihop.tr), you can monitor events such as packet transmission, reception, and node energy depletion.

To verify the balancing enerfy at every node, we can be used an AWK script or manually analyse the the trace file. The energy consumption events are logged in the trace file.

Below is an example AWK command to extract energy levels from the trace file:

awk ‘/Energy/ {print “Node ” $1 ” Energy: ” $2}’ energy_multihop.tr

It will output the energy level of every node at various time intervals while the simulation.

  1. Advanced Energy-aware Features
  2. a) Energy-aware Routing Protocol

We can change the routing protocol like AODV, DSR to encompass the energy-awareness in the route selection process. For specimen, nodes with low energy can be permitted while selecting a route.

  1. b) Node Mobility

To replicate a dynamic, energy-aware environment, we can be launched node mobility using setdest commands to move nodes around when the simulation.

Example: Adding Mobility to Nodes

# Adding mobility to node 1

$ns at 2.0 “$node1 setdest 200 300 5.0”   ;# Move node1 to (200, 300) at 5 m/s

$ns at 4.0 “$node1 setdest 100 100 5.0”   ;# Move node1 back to (100, 100) at 5 m/s

  1. c) Energy Harvesting or Recharging

We can execute the mechanisms for nodes to harvest or recharge energy while the simulation, like using solar power.

# Simulate node0 recharging its energy

$ns at 5.0 “$node0 energyModel energy_ [expr [$node0 energyModel energy_] + 20]”

  1. d) Energy-aware MAC Protocols

We can alter or execute the energy-aware MAC protocols, which set the nodes into sleep mode while they are idle to conserve energy.

Hence, this setup you get more details and knowledge on how to implement and evaluate the Network Multi Hop Energy with the help of these implementation approaches using the simulation tool ns2. More informations and procedure will be shared based on your needs.

Ns2project.com provide customized implementation support on Network Multi Hop Energy in NS2 guidelines and project ideas, get in touch with ns2project.com. Keep in contact with us to receive the finest advice for implementation and the greatest outcomes. Get the greatest performance analysis possible from our researchers by sharing all the details of your project with us, and we’ll take care of the rest. In relation to our projects, our team focuses on Wireless Sensor Networks (WSN) and Ad-hoc networks.