How to Calculate Energy Consumption in NS2

To calculate the energy consumption in Network Simulator 2 (NS2), this is very useful for the simulation of wireless networks particularly in scenarios in Wireless Sensor Networks (WSNs) where energy efficiency is an important performance. NS2 offers energy models that permit you to replicate energy consumption in terms of transmission, reception, idle and sleep states of the nodes. In the given below, we have offered the computational process in ns2:

Steps to Calculate Energy Consumption in NS2

  1. Understand the Energy Model in NS2:

NS2 uses a simple energy model that tracks the energy consumption of each node during different behaviors:

  • Transmit Power (TxPower_): Power utilized during transmission.
  • Receive Power (RxPower_): Power used during reception.
  • Idle Power (IdlePower_): Power consumed when the node is idle but ready to get data.
  • Sleep Power (SleepPower_): Power consumed when the node is in sleep mode (usually minimal or zero).
  1. Configure the Energy Model in NS2:

You can define these parameters in your TCL script by setting up the energy model for each node.

Here’s how to set up the energy model in your NS2 simulation:

# Create the simulator

set ns [new Simulator]

# Create topography for the network (e.g., 1000m x 1000m area)

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Create nodes

set node_(0) [$ns node]

set node_(1) [$ns node]

# Define energy model parameters for each node

set energyModel [new EnergyModel]

$node_(0) set energyModel_ $energyModel

$node_(1) set energyModel_ $energyModel

# Set initial energy (in Joules)

$node_(0) energyModel set energy_ 100.0  ;# 100 Joules for node 0

$node_(1) energyModel set energy_ 100.0  ;# 100 Joules for node 1

# Define power consumption for different states (in Watts)

$energyModel set TxPower_ 0.5    ;# Power consumed during transmission (0.5W)

$energyModel set RxPower_ 0.3    ;# Power consumed during reception (0.3W)

$energyModel set IdlePower_ 0.1  ;# Power consumed during idle (0.1W)

$energyModel set SleepPower_ 0.001  ;# Power consumed during sleep (0.001W)

# Create wireless channel and attach nodes to it

set chan_ [new Channel/WirelessChannel]

$node_(0) set channel_ $chan_

$node_(1) set channel_ $chan_

# Configure mobility (optional)

$ns at 10.0 “$node_(0) setdest 500 500 10”  ;# Node 0 moves at time 10s

# Attach traffic agents (optional)

set udp0 [new Agent/UDP]

set sink [new Agent/Null]

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

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

$ns connect $udp0 $sink

# Create traffic source (e.g., CBR)

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$ns at 1.0 “$cbr0 start”

$ns at 5.0 “$cbr0 stop”

  1. Calculating Energy Consumption:

Energy consumption is computed by the energy model as the simulation progresses. The total energy consumption for a node is estimate based on the given below:

  • Transmit Energy: Energy consumed during transmission of packets is calculated as:

ETx=PTx×TTxE_{\text{Tx}} = P_{\text{Tx}} \times T_{\text{Tx}}ETx​=PTx​×TTx​

where:

    • PTxP_{\text{Tx}}PTx​ is the transmit power.
    • TTxT_{\text{Tx}}TTx​ is the total time spent transmitting.
  • Receive Energy: Energy utilized during reception is calculated correspondingly:

ERx=PRx×TRxE_{\text{Rx}} = P_{\text{Rx}} \times T_{\text{Rx}}ERx​=PRx​×TRx​

where:

    • PRxP_{\text{Rx}}PRx​ is the receive power.
    • TRxT_{\text{Rx}}TRx​ is the total time spent receiving.
  • Idle Energy: Energy consumed when the node is idle but not transferrring or receiving:

EIdle=PIdle×TIdleE_{\text{Idle}} = P_{\text{Idle}} \times T_{\text{Idle}}EIdle​=PIdle​×TIdle​

  • Sleep Energy: If the node assists sleep mode, the energy consumed in sleep mode is:

ESleep=PSleep×TSleepE_{\text{Sleep}} = P_{\text{Sleep}} \times T_{\text{Sleep}}ESleep​=PSleep​×TSleep​

  1. Accessing the Energy Consumption of Nodes:

To observe the remaining energy of each node during or after the simulation, you can use the below code snippet in TCL to print the remaining energy of a node:

# Print the energy of a node at a specific time

proc check_energy {node_id time} {

global ns node_

set energy [$node_($node_id) energy]

puts “Node $node_id energy at time $time: $energy Joules”

}

# Schedule energy checks during the simulation

$ns at 2.0 “check_energy 0 2.0”

$ns at 6.0 “check_energy 0 6.0”

$ns at 10.0 “check_energy 1 10.0”

# End the simulation

proc finish {} {

global ns

$ns flush-trace

exit 0

}

$ns at 20.0 “finish”

  1. Trace Energy Consumption in the Trace File:

If you want to attach energy data in your trace file for post-simulation analysis, you can fine-tune the trace settings to log energy utilization:

# Enable tracing and create trace file

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Flush trace and close file at the end

$ns at 20.0 “$ns flush-trace; close $tracefile; exit 0”

After executing the simulation, the trace file (out.tr) will contain events related to the nodes as well as their energy consumption.

  1. Post-Simulation Energy Analysis:

After the simulation, you can estimate the total energy consumption by validating the remaining energy of each node and relating it with the initial energy. The energy consumed by each node is:

Energy Consumed=Initial Energy−Remaining Energy\text{Energy Consumed} = \text{Initial Energy} – \text{Remaining Energy}Energy Consumed=Initial Energy−Remaining Energy

You can whether print the energy natively during the simulation or assess the trace file for more detailed energy data.

  1. Energy Consumption Example:

If node 0 begins with 100 Joules of energy and terminates the simulation with 75 Joules, the energy consumption for that node is:

Energy Consumed=100 Joules−75 Joules=25 Joules\text{Energy Consumed} = 100 \, \text{Joules} – 75 \, \text{Joules} = 25 \, \text{Joules}Energy Consumed=100Joules−75Joules=25Joules

  1. Extending the Model for Complex Networks:

In a more advanced network scenario with numerous nodes, changing mobility, and traffic patterns, you can observe the energy consumption of each node over time. You can also enhance energy consumption by altering transmission power, using energy-efficient routing protocols, or executing sleep modes for nodes that are not actively take part in communication.

We have aggregated the formulas and present them as a structured format for you to estimate the energy consumption in the ns2 tool by establishing the energy models into the simulation to track the utilization of energy in the network. Kindly share the specifics of your energy consumption parameters, and we will support you in optimizing project performance accordingly.