Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

# How to Calculate Power Consumption in ns3

Calculating power consumption in ns3 involves using the energy model which is provided by the ns3 energy framework. This framework permits us to simulate and track the power consumption of nodes in a wireless network. Here’s a complete guide on setting up and calculating power consumption in ns3.

Steps for calculation

1. Set up your ns3 :
• Make sure that ns3 is installed in the computer. If not, install it.

1. Create a new ns3 script :
• In the scratch directory of ns3, create a new script.

1. Include necessary libraries :
• In your script, include the necessary libraries.

1. Define network topology :
• For your network topology, create multiple nodes.
1. Add energy modules to the nodes :
• Keep track of the number of nodes and to calculate the power consumption, use the energy models.

1. Running the Simulation :
• Define the simulation time and run the simulation.

Example for calculating power consumption in ns3

Here is the example for the calculation of power consumption :

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/energy-module.h”

#include “ns3/applications-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“PowerConsumptionExample”);

void ReportPowerConsumption(Ptr<BasicEnergySource> energySource)

{

double remainingEnergy = energySource->GetRemainingEnergy();

std::cout << Simulator::Now().GetSeconds() << “s: Remaining energy = ” << remainingEnergy << ” J” << std::endl;

Simulator::Schedule(Seconds(1.0), &ReportPowerConsumption, energySource); // Schedule next report

}

int main(int argc, char *argv[])

{

uint32_t nNodes = 10; // Number of nodes

double totalTime = 100.0; // Total simulation time in seconds

CommandLine cmd;

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(nNodes);

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

phy.SetChannel(channel.Create());

WifiHelper wifi = WifiHelper::Default();

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper mac;

Ssid ssid = Ssid(“ns-3-ssid”);

mac.SetType(“ns3::StaWifiMac”, “Ssid”, SsidValue(ssid), “ActiveProbing”, BooleanValue(false));

NetDeviceContainer devices = wifi.Install(phy, mac, nodes);

MobilityHelper mobility;

Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();

for (uint32_t i = 0; i < nNodes; ++i)

{

positionAlloc->Add(Vector(rand() % 100, rand() % 100, 0.0)); // Random positions for nodes

}

mobility.SetPositionAllocator(positionAlloc);

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApps = echoServer.Install(nodes.Get(0));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(totalTime));

echoClient.SetAttribute(“MaxPackets”, UintegerValue(100));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApps = echoClient.Install(nodes.Get(nNodes – 1));

clientApps.Start(Seconds(2.0));

clientApps.Stop(Seconds(totalTime));

// Install energy sources and models

BasicEnergySourceHelper basicSourceHelper;

basicSourceHelper.Set(“BasicEnergySourceInitialEnergyJ”, DoubleValue(10000.0));

EnergySourceContainer sources = basicSourceHelper.Install(nodes);

for (uint32_t i = 0; i < nodes.GetN(); ++i)

{

Ptr<BasicEnergySource> basicSource = DynamicCast<BasicEnergySource>(sources.Get(i));

Simulator::Schedule(Seconds(1.0), &ReportPowerConsumption, basicSource);

}

Simulator::Stop(Seconds(totalTime));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Explanation

Nodes are created. Wi-Fi network is configured for communication.

1. Mobility :

Random position for the nodes are configured. To keep the nodes stationary, ConstantPositionMobilityModel is used.

1. Applications :

On one node, a UDP echo server is installed. and On another node, a UDP echo server is installed to generate traffic.

1. Energy models :

On the nodes, basic energy sources is installed with an initial energy value. To track energy consumption of Wi-Fi devices, ReportPowerConsumption is used.

1. Running the Simulation :

The simulation runs. and the remaining energy of each node is reported at regular intervals.

Overall, we had learned on calculating power consumption in ns3 by using the energy model provided by the ns3 energy framework.

If you require assistance with conducting a comparison analysis for calculating power consumption in ns3 for your project, please provide us with your parameters. We guarantee detailed results.