To implement the network energy consumption in ns3 has several steps to follows that contain to use the energy framework that offered by ns3 tool then encompasses the energy sources and device energy models.
Below is the complete procedure on implement the energy consumption in ns3:
Step-by-Step Implementation:
Step 1: Set Up the ns3 Environment
- Install ns3: Make certain ns3 is installed on the computer.
sudo apt-get update
sudo apt-get install ns3
Create a New ns3 Project: Create a directory for your new project within the ns3 workspace.
cd ns-3
mkdir scratch/energy-consumption-project
Step 2: Utilize the ns3 Energy Framework
- Include Energy Models: ns3 offers the energy source and device energy model classes. Include these in the simulation script.
Step 3: Develop the Energy Consumption Model
- Create a New Simulation Script: Create a new script in your scratch directory to implement the simulation scenario.
// energy-consumption-project.cc
#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/energy-module.h”
using namespace ns3;
int main(int argc, char *argv[]) {
// Set up the simulation
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(devices);
// Install energy sources
BasicEnergySourceHelper basicSourceHelper;
basicSourceHelper.Set(“BasicEnergySourceInitialEnergyJ”, DoubleValue(100.0));
EnergySourceContainer sources = basicSourceHelper.Install(nodes);
// Install device energy models
WifiRadioEnergyModelHelper radioEnergyHelper;
DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install(devices, sources);
// Install applications
uint16_t port = 9;
UdpEchoServerHelper server(port);
ApplicationContainer serverApps = server.Install(nodes.Get(1));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper client(interfaces.GetAddress(1), port);
client.SetAttribute(“MaxPackets”, UintegerValue(1));
client.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));
client.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApps = client.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
// Enable tracing
pointToPoint.EnablePcapAll(“energy-consumption”);
// Run the simulation
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Compile the Script: Compile your script using the waf build system.
./waf build
Run the Simulation: Run your simulation script and observe the results.
./waf –run scratch/energy-consumption-project
Step 4: Analyze Energy Consumption
- Collect Energy Data: To trace and log the facilities to collect data on energy consumption during the simulation we need to utilize the ns3.
- Analyse Results: Analyse the collected data to understand the energy consumption patterns. We need to use numerous tools to plot and interpret the data.
Additional Tips:
- Modify Energy Parameters: Adjust the parameters of the energy sources and device energy models to reflect different scenarios and energy consumption profiles.
- Custom Energy Models: If the provided models do not meet your requirements, consider extending or creating custom energy models.
- Documentation: Keep your code well-documented to ensure clarity and ease of maintenance.
Example of Logging Energy Consumption
To log the energy consumption at each node, we need to add a few lines to the simulation script to periodically print the remaining energy.
void EnergyConsumptionLogger(Ptr<EnergySource> source) {
std::cout << “Time: ” << Simulator::Now().GetSeconds() << “s, Remaining energy: ”
<< source->GetRemainingEnergy() << “J” << std::endl;
Simulator::Schedule(Seconds(1.0), &EnergyConsumptionLogger, source);
}
int main(int argc, char *argv[]) {
// [Setup code as above]
// Install energy sources
BasicEnergySourceHelper basicSourceHelper;
basicSourceHelper.Set(“BasicEnergySourceInitialEnergyJ”, DoubleValue(100.0));
EnergySourceContainer sources = basicSourceHelper.Install(nodes);
// Install device energy models
WifiRadioEnergyModelHelper radioEnergyHelper;
DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install(devices, sources);
// Log energy consumption
Simulator::Schedule(Seconds(1.0), &EnergyConsumptionLogger, sources.Get(0));
// [Remaining setup and run code as above]
}
Overall, we had done the implementation for energy consumption that includes the energy sources and device energy models classes in simulation scenario and then it Analyse the collected data then it periodically print the remaining energy using the ns3 tool. If you need any other information regarding energy consumption we will provide and support.
We will handle the implementation of Network Energy Consumption in ns3 programming. You can find excellent project ideas and execution details at ns3simulation.com.