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 Number of alive nodes in ns3

To calculate the number of nodes alive in a network, we have to track the energy levels of nodes and determine if they have enough energy to remain operational.

Here is a complete guide for achieving this in ns3.

Steps for calculating number of nodes alive in a network

1. Set up the simulation :
• Make sure that ns3 is installed in the computer. If not, install it and include necessary modules.
1. Create Network Topology:
• create nodes and configure the network topology.
• Set up mobility models for the nodes.
1. Configure Energy Models :
• On the nodes, install energy sources and energy models to track energy consumption.
1. Enable tracing and Metrics Collection:
• To capture relevant metrics such as energy consumption and node status, use ns3 tracing capabilities.
1. Run the simulation :
• Execute the simulation and collect the trace data.
1. Analyze the results :
• Post-process the trace data to calculate the number of alive nodes over time.

Example to Calculate the Number of Alive Nodes

Create a basic set up of network with energy models, generate traffic, and capture metrics to calculate number of alive nodes Rate in ns3.

#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/log.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“AliveNodesExample”);

void CheckAliveNodes (NodeContainer nodes, double threshold, Time interval)

{

int aliveNodes = 0;

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

{

Ptr<Node> node = *i;

Ptr<EnergySourceContainer> sources = node->GetObject<EnergySourceContainer> ();

if (sources)

{

Ptr<EnergySource> source = DynamicCast<EnergySource> (sources->Get (0));

if (source && source->GetRemainingEnergy () > threshold)

{

aliveNodes++;

}

}

}

NS_LOG_UNCOND (“Time: ” << Simulator::Now ().GetSeconds () << “s, Alive Nodes: ” << aliveNodes);

Simulator::Schedule (interval, &CheckAliveNodes, nodes, threshold, interval);

}

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 channel = YansWifiChannelHelper::Default ();

YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();

phy.SetChannel (channel.Create ());

WifiMacHelper mac;

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

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

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

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

NetDeviceContainer apDevices = wifi.Install (phy, mac, nodes.Get (0));

// Install Internet stack

InternetStackHelper stack;

stack.Install (nodes);

// Assign IP addresses

ipv4.SetBase (“10.1.3.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = ipv4.Assign (staDevices);

ipv4.Assign (apDevices);

// Install Energy Source and Device Energy Models

BasicEnergySourceHelper basicSourceHelper;

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

EnergySourceContainer sources = basicSourceHelper.Install (nodes);

// Install and start applications on nodes

UdpServerHelper myServer (9);

ApplicationContainer serverApp = myServer.Install (nodes.Get (1));

serverApp.Start (Seconds (1.0));

serverApp.Stop (Seconds (10.0));

UdpClientHelper myClient (interfaces.GetAddress (1), 9);

myClient.SetAttribute (“MaxPackets”, UintegerValue (320));

myClient.SetAttribute (“Interval”, TimeValue (MilliSeconds (50)));

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

ApplicationContainer clientApp = myClient.Install (nodes.Get (0));

clientApp.Start (Seconds (2.0));

clientApp.Stop (Seconds (10.0));

// Schedule periodic check for alive nodes

Simulator::Schedule (Seconds (1.0), &CheckAliveNodes, nodes, 0.0, Seconds (1.0));

// Run the simulation

Simulator::Stop (Seconds (11.0));

Simulator::Run ();

// Clean up

Simulator::Destroy ();

return 0;

}

Explanation:

1. Setup Logging:
• To track activities, enable logging for the UDP applications.
2. Create Nodes:
• Create a set of nodes representing devices in the network.
3. Install Mobility Model:
• On the nodes, install a mobility model (e.g., ConstantVelocityMobilityModel).
4. Install Wi-Fi Devices:
• On the nodes, configure and install Wi-Fi devices.
5. Install Internet Stack:
• On the nodes, install the Internet stack.
6. Assign IP Addresses:
• Assign IP addresses to the network interfaces.
7. Install Energy Source and Device Energy Models:
• To track energy consumption, install energy sources and device energy models on the nodes.
8. Install Applications:
• On the nodes, install UDP server and client applications.
9. Schedule Periodic Check for Alive Nodes:
• To periodically check and log the number of alive nodes based on their remaining energy, define a function.
• Schedule this function to run at regular intervals.
10. Run Simulation:
• Run the simulation for the specified duration.

Analyzing the Results:

• Number of Alive Nodes:
• Based on the remaining energy, the number of alive nodes is periodically logged.
• Nodes are considered alive if their remaining energy is above a specified threshold.

So here we had simulation results on calculating number of alive nodes in a network by tracking the energy levels of nodes and determining if they have enough energy to remain operational. Also, we provide related contents on Number of alive nodes in a network.

Our team of researchers will guide in presenting the findings on the performance analysis of the number of active nodes in ns3 simulation for your project. If you are interested in conducting a comparative analysis of networks, keep in contact with us.