To calculate the coverage percentage in ns3, we need to simulate the network that contains to determine the area or number of nodes that are efficiently protect by a signal or service inside the network.
Here, we provide the sample that concentrated on a wireless network, while the coverage is a percentage of nodes within the particular area that obtain a signal from an access point (AP) or a base station.
Steps to Calculate Coverage Percentage in ns3
- Set Up ns3 Environment:
- Make certain ns3 is installed in the computer.
- Create a New ns3 Script:
- Create a new script file in the scratch directory of ns-3, e.g., coverage_percentage.cc.
- Include Necessary Headers:
- In the script, conclude all essential ns3 headers.
- Define Network Topology:
- Use of multiple nodes and an AP to configure the network topology.
- Implement Coverage Calculation Logic:
- Compute the number of nodes within the coverage range of the AP.
- Run the Simulation:
- Set the simulation time and run the simulation using Simulator::Run() and Simulator::Destroy().
Example Code:
At this point, we had provided the sample demonstration for calculating the Coverage Percentage in ns3:
#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”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“CoveragePercentage”);
double CalculateCoveragePercentage(NodeContainer &apNodes, NodeContainer &staNodes, double range)
{
Ptr<Node> apNode = apNodes.Get(0); // Assuming single AP for simplicity
Ptr<MobilityModel> apMobility = apNode->GetObject<MobilityModel>();
Vector apPosition = apMobility->GetPosition();
uint32_t coveredNodes = 0;
for (NodeContainer::Iterator i = staNodes.Begin(); i != staNodes.End(); ++i)
{
Ptr<Node> staNode = *i;
Ptr<MobilityModel> staMobility = staNode->GetObject<MobilityModel>();
Vector staPosition = staMobility->GetPosition();
double distance = CalculateDistance(apPosition, staPosition);
if (distance <= range)
{
coveredNodes++;
}
}
double coveragePercentage = (double)coveredNodes / staNodes.GetN() * 100.0;
return coveragePercentage;
}
int main(int argc, char *argv[])
{
uint32_t nStas = 10; // Number of stations
double range = 50.0; // Coverage range of the AP
CommandLine cmd;
cmd.AddValue(“nStas”, “Number of stations”, nStas);
cmd.AddValue(“range”, “Coverage range of the AP”, range);
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;
staDevices = wifi.Install(phy, mac, wifiStaNodes);
mac.SetType(“ns3::ApWifiMac”, “Ssid”, SsidValue(ssid));
NetDeviceContainer apDevice;
apDevice = wifi.Install(phy, mac, wifiApNode);
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(0.0),
“MinY”, DoubleValue(0.0),
“DeltaX”, DoubleValue(10.0),
“DeltaY”, DoubleValue(10.0),
“GridWidth”, UintegerValue(5),
“LayoutType”, StringValue(“RowFirst”));
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(10.0));
UdpEchoClientHelper echoClient(apInterface.GetAddress(0), 9);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(1));
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(10.0));
Simulator::Stop(Seconds(10.0));
Simulator::Run();
double coveragePercentage = CalculateCoveragePercentage(wifiApNode, wifiStaNodes, range);
std::cout << “Coverage Percentage: ” << coveragePercentage << “%” << std::endl;
Simulator::Destroy();
return 0;
}
Explanation:
Here are the complete process descriptions that are given below;
- 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.
- Mobility:
- Set the AP node’s position to a constant location.
- Set the STA nodes’ mobility to random walk within a bounded area.
- Applications:
- Installed a UDP echo server on the AP node.
- Installed a UDP echo client on the STA nodes to generate traffic.
- Coverage Calculation Logic:
- Implemented a CalculateCoveragePercentage function that calculates the number of STA nodes within the specified range of the AP node.
- Calculated the coverage percentage by dividing the number of covered nodes by the total number of STA nodes and multiplying by 100.
- Running the Simulation:
- The simulation runs with the STA nodes moving within the bounded area, and the coverage percentage is calculated and printed at the end.
Overall, we had learned about what coverage is and how it implemented and calculated with the help of ns3 simulation. We also deliver the additional networking comparison analysis for your project.