How to Implement Energy Consumption in NS2

To implement the energy consumption in Network simulator 2 (ns2), we can use or expand the in-built energymodel of ns2 which notes energy utilization for various behaviors performed by the nodes including transmitting, receiving and being idle. It is certainly vital for simulating situations like Wireless Sensor Networks (WSNs), Internet of Things (IoT) or mobile ad hoc networks (MANETs) in which energy efficiency is important for the network durability.

Here is a step-by-step guide on how to implement and extend energy consumption in NS2:

Steps for Implementing Energy Consumption in NS2

  1. Basic Energy Model in NS2

NS2 has a built-in EnergyModel class that replicates energy consumption for nodes. The model decrease the node’s energy in terms of actions like transmission, reception, or being in idle state.

  • EnergyModel Parameters:
    • initialenergy_: The initial energy (in Joules) allocated to the node.
    • txPower_: Energy consumption per unit time during transmission (in watts).
    • rxPower_: Energy consumption per unit time during reception (in watts).
    • idlePower_: Energy consumption per unit time during idle state (in watts).
  1. Modify EnergyModel for Custom Scenarios

Tailor the energy consumption by modifying or extending the EnergyModel class, which is executed in energy_model.{cc,h} files.

Example Initialization of the Energy Model in C++

// Initializing the energy model

EnergyModel::EnergyModel() {

initialenergy_ = 100.0;  // Initial energy in Joules

txPower_ = 1.0;          // Transmission power consumption in watts

rxPower_ = 0.5;          // Reception power consumption in watts

idlePower_ = 0.1;        // Idle state power consumption in watts

energy_ = initialenergy_; // Set current energy to initial energy

}

  1. Energy Consumption During Transmission, Reception, and Idle

NS2 minimizes the node’s energy according to the current action:

  • Transmission Energy: The energy utilized during packet transmission is computed depends on the duration of the transmission and the power usage of the node during transmission.

void EnergyModel::DecrTxEnergy(double time) {

double energyConsumed = txPower_ * time;

if (energy_ > energyConsumed) {

energy_ -= energyConsumed;

} else {

energy_ = 0;

nodeDie();  // Call function to handle node death

}

}

  • Reception Energy: Likewise, energy is consumed when receiving packets.

void EnergyModel::DecrRxEnergy(double time) {

double energyConsumed = rxPower_ * time;

if (energy_ > energyConsumed) {

energy_ -= energyConsumed;

} else {

energy_ = 0;

nodeDie();

}

}

  • Idle Energy: Nodes in idle state also utilize energy, although at a lower rate than during transmission or reception.

void EnergyModel::DecrIdleEnergy(double time) {

double energyConsumed = idlePower_ * time;

if (energy_ > energyConsumed) {

energy_ -= energyConsumed;

} else {

energy_ = 0;

nodeDie();

}

}

  1. Integrate Energy Model with the Node

In your NS2 simulation, you can allocate the energy model to each node. You do this in the Tcl script by including an instance of the EnergyModel to each node.

Example Tcl Script to Set Up Energy Consumption for Nodes:

# Create a simulator instance

set ns [new Simulator]

# Create nodes

set node1 [$ns node]

set node2 [$ns node]

# Set up energy model for nodes

$node1 set energyModel_ [new EnergyModel]

$node2 set energyModel_ [new EnergyModel]

# Set initial energy for each node (in Joules)

$node1 set initialenergy_ 100.0

$node2 set initialenergy_ 120.0

# Set transmission power, reception power, and idle power (in watts)

$node1 set txPower_ 0.5

$node1 set rxPower_ 0.3

$node1 set idlePower_ 0.05

$node2 set txPower_ 0.6

$node2 set rxPower_ 0.4

$node2 set idlePower_ 0.07

# Define traffic and agents for nodes

set udp1 [new Agent/UDP]

set udp2 [new Agent/UDP]

$ns attach-agent $node1 $udp1

$ns attach-agent $node2 $udp2

# Define a CBR (Constant Bit Rate) traffic generator

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp1

$cbr set packetSize_ 512

$cbr set interval_ 0.1  ;# Send a packet every 0.1 seconds

# Schedule the traffic

$ns at 1.0 “$cbr start”

$ns at 10.0 “$cbr stop”

# Run the simulation

$ns run

  1. Monitoring Energy Consumption in NS2

To observe energy consumption during the simulation, you can either output the energy level at particular intervals or track when nodes run out of energy and die.

Example C++ Code to Output Energy Level:

void EnergyModel::printEnergy() {

printf(“Node %d energy level: %f Joules\n”, node_->nodeid(), energy_);

}

In your Tcl script, you can periodically check the energy level:

# Check energy levels every 5 seconds

proc check_energy {} {

global node1

puts “Node1 energy: [$node1 set energyModel_ getEnergy] Joules”

$ns at [expr [$ns now] + 5] “check_energy”

}

# Start energy checks

$ns at 5.0 “check_energy”

  1. Node Death and Handling Low Energy

When a node’s energy reaches zero, you may want to deactivate its potential to send or receive packets. This is already managed in the default energy model with the nodeDie() function, yet you can tailor this activities.

Example of Node Death Handling:

void EnergyModel::nodeDie() {

printf(“Node %d has run out of energy and is dead\n”, node_->nodeid());

node_->disable();  // Disable the node when energy is depleted

}

  1. Advanced Energy Models

For more realistic simulations, you can execute advanced energy models like:

  • Energy Harvesting: Nodes that harvest energy from the environment (solar, wind, RF) and replenish their energy reserves over time.
  • Sleep and Wake Mechanisms: Nodes switch to a low-power mode (sleep) when they are not actively transmitting or receiving, to save energy.
  • Energy-Aware Routing: Fine-tune routing protocols to consider energy levels when picking routes, making sure that nodes with low energy are not overburdened.
  1. Energy-Aware Routing (Optional)

You can also alter routing protocols (like AODV or DSR) to consider energy levels when picking routes. For instance, the routing protocol might evade routes that involve nodes with low energy, to expand the lifetime of the network.

Example: Energy-Aware AODV Routing Modification:

// Check energy level before choosing the route

if (neighbor->energyModel()->getEnergy() > energy_threshold) {

// Select route if energy is sufficient

selectRoute(neighbor);

}

With the help of presented manual, you can gain the knowledge about the implementation and execution of the energy consumption and be able to monitor the consumption of energy in the simulated environment. If required, we will provide any other details which are critical for you.

Our team specializes in simulating scenarios such as Wireless Sensor Networks (WSNs), the Internet of Things (IoT), and mobile ad hoc networks (MANETs) tailored to your project specifications. For efficient Energy Consumption setup in NS2, visit ns2project.com, where our skilled developers provide outstanding assistance. Rely on our experts for guidance on enhancing your project’s network performance.