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

How to Calculate Network Cluster head lifetime in ns3

To calculate the network cluster head lifetime in ns3, we need to implement a wireless sensor network (WSN) where nodes are organized into clusters, and each cluster has a cluster head liable for communication inside the cluster and with the base station. The duration of cluster head is when node becomes cluster head while waiting for it to runs out of energy. Here are the procedures on how to achieve this in ns3:

  1. Set Up the ns3 Environment:
    • Ensure you have ns3 installed.
  2. Define the Network Topology:
    • Create a WSN with multiple nodes.
  3. Implement the Energy Model:
    • Add an energy model to the nodes to simulate energy consumption.
  4. Cluster Formation:
    • Implement a clustering algorithm to select cluster heads.
  5. Simulate Cluster Head Lifetime:
    • Monitor the energy levels of the cluster heads and measure the time until they run out of energy.

Example Code

The below is the instance of how to setup a basic ns3 simulation to calculate the cluster head lifetime. This is the sample to assumes a simple clustering algorithm .

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/energy-module.h”

#include “ns3/wifi-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“ClusterHeadLifetimeExample”);

void ReportEnergyRemaining (Ptr<BasicEnergySource> energySource, Ptr<ConstantPositionMobilityModel> mob)

{

Time now = Simulator::Now ();

double energyRemaining = energySource->GetRemainingEnergy ();

NS_LOG_UNCOND (“Time: ” << now.GetSeconds () << “s, Node: ” << mob->GetObject<Node>()->GetId()  << “, Energy Remaining: ” << energyRemaining << “J”);

if (energyRemaining > 0)

{

Simulator::Schedule (Seconds (1.0), &ReportEnergyRemaining, energySource, mob);

}

else

{

NS_LOG_UNCOND (“Node ” << mob->GetObject<Node>()->GetId() << ” has run out of energy.”);

}

}

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

Time::SetResolution (Time::NS);

NodeContainer nodes;

nodes.Create (10);

MobilityHelper mobility;

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

“MinX”, DoubleValue (0.0),

“MinY”, DoubleValue (0.0),

“DeltaX”, DoubleValue (5.0),

“DeltaY”, DoubleValue (5.0),

“GridWidth”, UintegerValue (5),

“LayoutType”, StringValue (“RowFirst”));

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

mobility.Install (nodes);

WifiHelper wifi;

wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

wifiPhy.SetChannel (wifiChannel.Create ());

WifiMacHelper wifiMac;

wifiMac.SetType (“ns3::AdhocWifiMac”);

NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

InternetStackHelper stack;

stack.Install (nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Energy Model Setup

BasicEnergySourceHelper basicSourceHelper;

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

EnergySourceContainer sources = basicSourceHelper.Install (nodes);

WifiRadioEnergyModelHelper radioEnergyHelper;

DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices, sources);

// Monitor Energy Levels

for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i)

{

Ptr<Node> node = *i;

Ptr<BasicEnergySource> basicSource = DynamicCast<BasicEnergySource> (node->GetObject<EnergySourceContainer>()->Get (0));

Ptr<ConstantPositionMobilityModel> mob = node->GetObject<ConstantPositionMobilityModel> ();

Simulator::Schedule (Seconds (1.0), &ReportEnergyRemaining, basicSource, mob);

}

Simulator::Stop (Seconds (100.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

} 

Explanation

  1. Setup: The code sets up a simple WSN with 10 nodes, each with an initial energy of 100 Joules.
  2. Mobility: Nodes are placed in a grid layout.
  3. Wi-Fi: Nodes communicate using Wi-Fi in ad-hoc mode.
  4. Energy Model: Each node is equipped with a basic energy source and a Wi-Fi radio energy model to simulate energy consumption.
  5. Energy Monitoring: The ReportEnergyRemaining function logs the energy remaining for each node at 1-second intervals. When a node runs out of energy, it is noted.

Running the Simulation

Compile and run the simulation using the following commands in your ns3 environment:

./waf configure

./waf build

./waf –run your-script-name

Replace your-script-name with the actual name of your script file.

Here, we had understood basic implementation and estimation for how the network lifetime communicates inside the cluster and with the base station with the use of ns3 tool. We will give how the network lifetime will perform in other simulation tools. . In ns3tool, we calculate the lifetime of network cluster heads and conduct a comparative analysis. Share your parameters with us to achieve the best results from our team.