How to Calculate Network Residual Energy Level in NS2
To calculate the network residual energy using the simulation tool NS2 that refers to the remaining energy obtainable at the node after particular network operations like idle periods, reception, or packet transmission. It is especially significant in the wireless networks, like mobile ad hoc networks (MANETs) and wireless sensor networks (WSNs) that nodes are energy-constrained. The virtual environment NS2 distributes the energy models, which permits to replicate an energy consumption and track residual energy at every single node in the course of the simulation. The following is a simple approach on how to compute and analyse the network residual energy level within NS2:
Steps to Calculate Network Residual Energy in NS2
- Enable the Energy Model in NS2
To track residual energy in NS2, we require to allow the energy model for the nodes. The energy model permits to stipulate the initial energy, transmission power, reception power, and idle power consumption.
Example of Setting Up the Energy Model:
# Create a new simulator object
set ns [new Simulator]
# Define energy model parameters for the nodes
set initial_energy 100.0 ;# Initial energy in Joules
set tx_power 0.5 ;# Transmission power in Watts
set rx_power 0.3 ;# Reception power in Watts
set idle_power 0.01 ;# Idle power in Watts
set sleep_power 0.001 ;# Sleep power in Watts (if supported)
# Create nodes and assign energy models
for {set i 0} {$i < 10} {incr i} {
set node($i) [$ns node]
$node($i) add-energy-model $initial_energy $tx_power $rx_power $idle_power
}
In this instance:
- initial_energy is set to 100 Joules that specifies the total amount of energy available at the beginning of the simulation.
- tx_power, rx_power, and idle_power stipulate the energy consumed in the course of transmission, reception, and idle states, correspondingly.
- Monitor Residual Energy
NS2 permits to observe the residual energy of nodes during the simulation. We can query the energy level of a node at any point in time using the energy method.
TCL Script to Monitor Residual Energy:
# Function to monitor and print the residual energy of a node
proc monitor_residual_energy {node_id} {
global ns node
set energy_level [$node($node_id) energy]
puts “Time: [$ns now], Node: $node_id, Residual Energy: $energy_level Joules”
# Re-schedule this procedure to check the residual energy every 10 seconds
$ns at [expr [$ns now] + 10.0] “monitor_residual_energy $node_id”
}
# Schedule the first call to monitor residual energy for each node
for {set i 0} {$i < 10} {incr i} {
$ns at 0.0 “monitor_residual_energy $i”
}
This script:
- Monitor the residual energy of each node at the regular intervals (every 10 seconds in this case).
- Prints the residual energy level of every node to the console.
- Analyse Residual Energy Over Time
To evaluate how the residual energy depletes over time for all nodes, we can be logged the residual energy at periodic intervals and then plot the outcomes.
TCL Script to Log Residual Energy to a File:
# Open a file to log the residual energy data
set energy_log [open energy_log.txt w]
# Function to log the residual energy of all nodes
proc log_residual_energy {} {
global ns node energy_log
for {set i 0} {$i < 10} {incr i} {
set energy_level [$node($i) energy]
puts $energy_log “Time: [$ns now], Node: $i, Residual Energy: $energy_level Joules”
}
# Schedule the next logging event in 10 seconds
$ns at [expr [$ns now] + 10.0] “log_residual_energy”
}
# Schedule the first call to log the residual energy
$ns at 0.0 “log_residual_energy”
This script records the residual energy of each node to a file is known as energy_log.txt that we can be evaluated later. The file will be included the energy level of each node at regular intervals during the simulation.
- Calculate the Network’s Average Residual Energy
We can be estimated the average residual energy of all nodes at any given point during the simulation to monitor the overall energy status of the network.
TCL Script to Calculate and Log Average Residual Energy:
# Function to calculate and log the average residual energy of all nodes
proc log_average_residual_energy {} {
global ns node
set total_energy 0
set num_nodes 10
for {set i 0} {$i < $num_nodes} {incr i} {
set energy_level [$node($i) energy]
set total_energy [expr $total_energy + $energy_level]
}
set avg_energy [expr $total_energy / $num_nodes]
puts “Time: [$ns now], Average Residual Energy: $avg_energy Joules”
# Schedule the next calculation in 10 seconds
$ns at [expr [$ns now] + 10.0] “log_average_residual_energy”
}
# Schedule the first call to calculate the average residual energy
$ns at 0.0 “log_average_residual_energy”
This script:
- Adds the residual energy of all nodes.
- Calculates the average residual energy at regular intervals (every 10 seconds).
- Prints the outcomes to the console or logs it to a file.
- Analyse Residual Energy from the Trace File
NS2 does not automatically contain the energy information in the trace file, thus we require to record it physically as shown above. We can be evaluated the energy consumption trends of the whole network or individual nodes, after running the simulation and logging the residual energy to a file. If we need to envision the outcomes then we can be used an external tools such as MATLAB, Python, or any graphing software to plot the residual energy over time.
For instance, using Python and matplotlib then we can be plotted the energy levels of all nodes:
Python Script to Plot Residual Energy:
import matplotlib.pyplot as plt
# Load the data from the log file
time = []
energy_levels = {i: [] for i in range(10)} # Assume 10 nodes
with open(‘energy_log.txt’, ‘r’) as f:
for line in f:
parts = line.strip().split(‘, ‘)
time_value = float(parts[0].split(“: “)[1])
node_id = int(parts[1].split(“: “)[1])
energy_value = float(parts[2].split(“: “)[1])
time.append(time_value)
energy_levels[node_id].append(energy_value)
# Plot the residual energy of each node
for node_id, energy in energy_levels.items():
plt.plot(time, energy, label=f’Node {node_id}’)
plt.xlabel(‘Time (s)’)
plt.ylabel(‘Residual Energy (Joules)’)
plt.title(‘Residual Energy of Nodes Over Time’)
plt.legend()
plt.show()
This script interprets the residual energy log and plots the energy depletion for every single node over time.
- Consider Energy Consumption in Different States
The energy consumed by a node differs according to its state:
- Transmission: Nodes are consume more energy when transmitting the packets.
- Reception: Also these nodes are consume an energy when receiving packets.
- Idle: Nodes are consume less energy when they are idle (i.e., not transmitting or receiving).
- Sleep: Some nodes are may support a low-power sleep state, where they consume minimal energy.
Make sure to modify the power consumption values (transmission, reception, idle, and sleep) within the energy model to match the features of the devices are replicating.
Here, we had shown the efficient procedure to compute and analyse the Network Residual Energy Level within the simulation tool NS2. As well, we will be distributed further details regarding this topic in another manual.
Our team specializes in mobile ad hoc networks (MANETs) and wireless sensor networks (WSNs). If you have a project in mind, feel free to share the details with us, and we’ll be glad to assist you. We can help you assess the Network Residual Energy Level using the NS2 tool, and we’re here to provide expert insights on your project ideas and topics.