To calculate the energy utilization in ns3 were specifically useful for computing the efficiency and sustainability of wireless networks like as IoT or mobile devices. We need to monitor the energy consumption of network nodes over the simulation period.
Here are the thorough steps on how to accomplish this in ns3:
Step-by-Step Guide to Calculate Energy Utilization in ns3
- Set Up the Simulation Environment:
- Download ns3 and make sure it is configured.
- Concludes all the required modules in the simulation (e.g., Internet, Mobility, and Energy).
- Create Network Topology:
- Define nodes and configure the network topology.
- Configure the mobility models for the nodes if essential.
- Configure Energy Models:
- To track energy consumption by installing energy source and energy model on the nodes.
- Configure Applications:
- Install traffic-generating applications (e.g., UDP, TCP) on the nodes.
- Enable Tracing and Metrics Collection:
- Allow tracing to capture relevant metrics such as energy consumption.
- Run the Simulation:
- Perform the simulation and gather the trace data.
- Analyze the Results:
- Post-process the trace data to compute the energy utilization.
Example Code Snippet for Energy Utilization in ns3
Here is a sample on how to configure the network topology with energy models, generate traffic, and capture metrics to compute energy utilization in ns-3:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/energy-module.h”
#include “ns3/wifi-module.h”
#include “ns3/config-store.h”
#include “ns3/log.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“EnergyUtilizationExample”);
void EnergyConsumptionCallback (Ptr<BasicEnergySource> energySource)
{
double remainingEnergy = energySource->GetRemainingEnergy ();
NS_LOG_UNCOND (“Remaining energy: ” << remainingEnergy << ” J”);
}
int main (int argc, char *argv[])
{
// Set up logging
LogComponentEnable (“UdpClient”, LOG_LEVEL_INFO);
LogComponentEnable (“UdpServer”, LOG_LEVEL_INFO);
// Create nodes
NodeContainer nodes;
nodes.Create (10);
// Install Mobility model
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// Install WiFi devices
WifiHelper wifi;
wifi.SetStandard (WIFI_STANDARD_80211b);
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Install Internet stack
InternetStackHelper internet;
internet.Install (nodes);
// Assign IP addresses
Ipv4AddressHelper ipv4;
ipv4.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);
// Install Energy Source and Device Energy Models
BasicEnergySourceHelper basicSourceHelper;
basicSourceHelper.Set (“BasicEnergySourceInitialEnergyJ”, DoubleValue (10000.0));
EnergySourceContainer sources = basicSourceHelper.Install (nodes);
WifiRadioEnergyModelHelper radioEnergyHelper;
DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices, sources);
// Trace energy consumption
for (EnergySourceContainer::Iterator iter = sources.Begin (); iter != sources.End (); ++iter)
{
Ptr<BasicEnergySource> basicSource = DynamicCast<BasicEnergySource> (*iter);
basicSource->TraceConnectWithoutContext (“RemainingEnergy”, MakeCallback (&EnergyConsumptionCallback));
}
// Install and start applications on nodes
uint16_t port = 9; // Discard port (RFC 863)
UdpServerHelper server (port);
ApplicationContainer serverApp = server.Install (nodes.Get (nodes.GetN () – 1));
serverApp.Start (Seconds (1.0));
serverApp.Stop (Seconds (10.0));
UdpClientHelper client (interfaces.GetAddress (nodes.GetN () – 1), port);
client.SetAttribute (“MaxPackets”, UintegerValue (320));
client.SetAttribute (“Interval”, TimeValue (MilliSeconds (50)));
client.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApp = client.Install (nodes.Get (0));
clientApp.Start (Seconds (2.0));
clientApp.Stop (Seconds (10.0));
// Run the simulation
Simulator::Stop (Seconds (11.0));
Simulator::Run ();
// Calculate total energy consumption
double totalEnergyConsumed = 0.0;
for (EnergySourceContainer::Iterator iter = sources.Begin (); iter != sources.End (); ++iter)
{
Ptr<BasicEnergySource> basicSource = DynamicCast<BasicEnergySource> (*iter);
totalEnergyConsumed += (10000.0 – basicSource->GetRemainingEnergy ());
}
NS_LOG_UNCOND (“Total energy consumed: ” << totalEnergyConsumed << ” J”);
// Clean up
Simulator::Destroy ();
return 0;
}
Explanation:
Here, we detailed explained the process for Energy Utilization in ns3:
- Setup Logging:
- Enable logging for the UDP applications to track their activities.
- Create Nodes:
- Create a set of nodes representing devices in the network.
- Install Mobility Model:
- Install a mobility model on the nodes (e.g., ConstantPositionMobilityModel).
- Install WiFi Devices:
- Install WiFi devices on the nodes to enable wireless communication.
- Install Internet Stack:
- Install the Internet stack on the nodes.
- Assign IP Addresses:
- Assign IP addresses to the network interfaces.
- Install Energy Source and Device Energy Models:
- Install energy sources and device energy models on the nodes to track energy consumption.
- Trace energy consumption by connecting a callback to the RemainingEnergy trace source.
- Install Applications:
- Install UDP server and client applications on the nodes.
- Run Simulation:
- Run the simulation for the specified duration.
- Calculate Total Energy Consumption:
- After the simulation, calculate the total energy consumed by summing the difference between the initial energy and the remaining energy for each node.
Analysing the Results:
- Energy Utilization:
- Energy utilization is calculated as the total energy consumed by all nodes in the network.
- This metric provides an indication of the energy efficiency of the network.
Here, we had learned about how the energy consumption analyse the results in ns3 simulator and also deliver and elaborate the further insights how the energy consumption will perform in other simulation tools.
To Calculate Energy Utilization in ns3tool for your project you can approach ns3simulation.com.