To implement network mobility control in ns3, we need to create a network. In that network simulation has been done based on predefined or dynamic mobility patterns to simulate the movement of nodes in a network. This type of simulation mainly used in mobile ad hoc networks (MANETs), vehicular ad hoc networks (VANETs), or any other network with mobile nodes. The following steps will guide on how to create a basic simulation of network mobility control in ns3.
Step-by-step guide to implement network mobility control in ns3:
Step 1: Setup ns3 Environment
Ensure that ns3 is installed and properly configured.
git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
./waf configure
./waf build
Step 2: Create the Network Mobility Control Simulation Script
We will create a script that sets up a network with mobile nodes and controls their movement using a mobility model.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “ns3/wifi-module.h”
#include “ns3/flow-monitor-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE(“NetworkMobilityControlExample”);
void CourseChange(std::string context, Ptr<const MobilityModel> model)
{
Vector position = model->GetPosition();
Vector velocity = model->GetVelocity();
NS_LOG_UNCOND(Simulator::Now().GetSeconds() << “s: Node at (” << position.x << “, ” << position.y << “, ” << position.z << “) with velocity (” << velocity.x << “, ” << velocity.y << “, ” << velocity.z << “)”);
}
int main(int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse(argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create(10); // Ten nodes in total
// Set up mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”
“X”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),
“Y”,StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”)); mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue(“ns3::UniformRandomVariable[Min=1.0|Max=5.0]”),
“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=2.0]”),
“PositionAllocator”, PointerValue(mobility.GetPositionAllocator()));
mobility.Install(nodes);
// Log position changes
Config::Connect(“/NodeList/*/$ns3::MobilityModel/CourseChange”, MakeCallback(&CourseChange));
// Set up WiFi
WifiHelper wifi;
wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);
WifiMacHelper mac;
Ssid ssid = Ssid(“ns-3-ssid”);
mac.SetType(“ns3::StaWifiMac”,
“Ssid”, SsidValue(ssid),
“ActiveProbing”, BooleanValue(false));
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
phy.SetChannel(channel.Create());
NetDeviceContainer devices = wifi.Install(phy, mac, nodes);
mac.SetType(“ns3::ApWifiMac”,
“Ssid”, SsidValue(ssid));
NetDeviceContainer apDevices = wifi.Install(phy, mac, nodes.Get(0));
// Install the internet stack
InternetStackHelper stack;
stack.Install(nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(devices);
address.Assign(apDevices);
// Install applications to generate traffic
uint16_t port = 9;
OnOffHelperonoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(interfaces.GetAddress(1), port)));
onoff.SetConstantRate(DataRate(“1Mbps”));
ApplicationContainer apps = onoff.Install(nodes.Get(0));
apps.Start(Seconds(1.0));
apps.Stop(Seconds(10.0));
PacketSinkHelpersink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
apps = sink.Install(nodes.Get(1));
apps.Start(Seconds(0.0));
apps.Stop(Seconds(10.0));
// Enable FlowMonitor to measure performance metrics
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
// Run the simulation
Simulator::Stop(Seconds(15.0));
Simulator::Run();
// Print per-flow statistics
monitor->CheckForLostPackets();
Ptr<Ipv4FlowClassifier>classifier = DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin(); i != stats.end(); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(i->first);
NS_LOG_UNCOND(“Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)”);
NS_LOG_UNCOND(” Tx Packets: ” << i->second.txPackets);
NS_LOG_UNCOND(” Tx Bytes: ” << i->second.txBytes);
NS_LOG_UNCOND(” Rx Packets: ” << i->second.rxPackets);
NS_LOG_UNCOND(” Rx Bytes: ” << i->second.rxBytes);
NS_LOG_UNCOND(” Lost Packets: ” << i->second.lostPackets); NS_LOG_UNCOND(” Throughput: ” << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() – i->second.timeFirstTxPacket.GetSeconds()) / 1024 / 1024 << ” Mbps”);
}
// Clean up
Simulator::Destroy();
return 0;
}
Step 3: Compile and Run the Simulation
- Compile the Simulation:
./waf configure –enable-examples
./waf build
Run the Simulation:
./waf –run scratch/network-mobility-control-example
Step 4: Analyze Results
The simulation script sets up a network with mobile nodes using a RandomWaypointMobilityModel, which moves the nodes randomly within a defined area. The positions and velocities of the nodes are logged as they move. FlowMonitor is used to collect and print out statistics about the traffic flows, such as packet loss, throughput, and delay.
Additional Considerations
To extend the functionality of your network mobility control simulation, consider the following:
1. Advanced Mobility Models
Implement more advanced mobility models such as Gauss-Markov, Random Direction, or Levy walk models to simulate more realistic movement patterns.
2. Dynamic Mobility Control
Incorporate dynamic mobility control algorithms that adjust the movement of nodes based on network conditions, environmental factors, or application requirements.
3. Communication Protocols
Integrate communication protocols to optimize data routing and resource allocation in the presence of node mobility.
4. Performance Metrics
Collect and analyze additional metrics such as energy consumption, network latency, and packet delivery ratio to evaluate the performance of the mobility control algorithms.
5. Real-World Scenarios
To test the effectiveness of the mobility control approach in practical applications we need to simulate real-world scenarios such as vehicular networks, drone networks, or mobile IoT deployments.
In this example we had concluded the implementation process of Network mobility control in ns3 by simulating the movement of nodes on the basis of predefined or dynamic mobility pattern and we had used the Flow monitor to analyse the results in ns3.
Approach ns3simulation.com to get best results for comparative analysis and Implementation of Network Mobility Control in ns3tool which are carried out by us. We work on mobile ad hoc networks (MANETs), vehicular ad hoc networks (VANETs), or any other network with mobile nodes for your projects and give optimal results.