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

How to Calculate Energy Consumption in Ns3

To calculate energy consumption in ns3, we can use the energy model in the ns3 energy framework. This framework simulates the energy consumption in wireless networks, in specific scenarios like battery-powered devices.

The following step-by-step will provide the instructions to calculate energy consumption in ns3.

Steps to Calculate Energy Consumption in ns3

  1. Set Up ns3 Environment:
    • Make sure ns3 is installed on the system.
  2. Create a New ns-3 Script:
    • Create a new script file in the scratch directory of ns3, e.g., energy_consumption.cc.
  3. Include Necessary Headers:
    • Include the necessary ns3 headers in the script.
  4. Define Network Topology:
    • Set up a network topology with multiple nodes.
  5. Add Energy Models to Nodes:
    • Use the energy models to track and calculate the energy consumption of the nodes.
  6. Run the Simulation:
    • Set the simulation time and run the simulation using Simulator::Run() and Simulator::Destroy().

Example Code:

#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 (“EnergyConsumptionExample”);

void ReportRemainingEnergy(Ptr<BasicEnergySource> energySource)

{

double remainingEnergy = energySource->GetRemainingEnergy();

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

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

}

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

{

uint32_t nStas = 10; // Number of stations

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

CommandLine cmd;

cmd.AddValue(“nStas”, “Number of stations”, nStas);

cmd.AddValue(“totalTime”, “Total simulation time”, totalTime);

cmd.Parse(argc, argv);

 

NodeContainer wifiStaNodes;

wifiStaNodes.Create(nStas);

NodeContainer wifiApNode;

wifiApNode.Create(1);

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 staDevices = wifi.Install(phy, mac, wifiStaNodes);

mac.SetType(“ns3::ApWifiMac”, “Ssid”, SsidValue(ssid));

NetDeviceContainer apDevice = wifi.Install(phy, mac, wifiApNode);

MobilityHelper mobility;

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

positionAlloc->Add(Vector(0.0, 0.0, 0.0)); // Position of the AP

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

{

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

}

mobility.SetPositionAllocator(positionAlloc);

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

mobility.Install(wifiApNode);

mobility.SetMobilityModel(“ns3::RandomWalk2dMobilityModel”,

“Bounds”, RectangleValue(Rectangle(-50, 50, -50, 50)));

mobility.Install(wifiStaNodes);

InternetStackHelper stack;

stack.Install(wifiApNode);

stack.Install(wifiStaNodes);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer staInterfaces = address.Assign(staDevices);

Ipv4InterfaceContainer apInterface = address.Assign(apDevice);

 

UdpEchoServerHelper echoServer(9);

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

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(totalTime));

UdpEchoClientHelper echoClient(apInterface.GetAddress(0), 9);

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

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

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

ApplicationContainer clientApps = echoClient.Install(wifiStaNodes);

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(wifiStaNodes);

WifiRadioEnergyModelHelper radioEnergyHelper;

DeviceEnergyModelContainerdeviceModels= radioEnergyHelper.Install(staDevices, sources);

 

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

{

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

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

}

Simulator::Stop(Seconds(totalTime));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Explanation:

  1. Nodes and Links:
    • Created nodes for the access point (AP) and multiple stations (STA).
    • Configured the Wi-Fi channel, physical layer, and MAC layer for the AP and STA nodes.
  2. Mobility:
    • Set the AP node’s position to a constant location.
    • Set the STA nodes’ mobility to random walk within a bounded area.
  3. Applications:
    • Installed a UDP echo server on the AP node.
    • Installed a UDP echo client on the STA nodes to generate traffic.
  4. Energy Models:
    • Installed basic energy sources on the STA nodes with an initial energy value.
    • Installed Wi-Fi radio energy models to track energy consumption of Wi-Fi devices.
    • Used ReportRemainingEnergy function to print the remaining energy of each node periodically.
  5. Running the Simulation:
    • The simulation runs, and the remaining energy of each STA node is reported at regular intervals.

Finally, in the above steps it explained elaborately that in battery powered devices energy consumption will happen. The ns3 energy framework allows to simulate and calculate the energy consumption.

For the given parameters we find the comparative analysis to calculate energy consumption in ns3.