I am a beginner with ns-3, and I am currently involved in a project for my university that requires simulating a VANET environment using 5G networks. I am utilizing the 5G-LENA framework along with the New Radio module. I have modified the example "cttc-3gpp-channel-example.cc" from the New Radio module to meet my needs and to learn more about it. Code:
#include "ns3/core-module.h"
#include "ns3/mobility-module.h"
#include "ns3/netanim-module.h"
#include "ns3/internet-module.h"
#include "ns3/nr-module.h"
#include <ns3/antenna-module.h>
#include "ns3/udp-client-server-helper.h"
#include "ns3/nr-point-to-point-epc-helper.h"
#include "ns3/point-to-point-helper.h"
#include <ns3/buildings-helper.h>
#include <iostream>
using namespace ns3;
int main(int argc, char *argv[])
{
// Enable logging for the animation interface
LogComponentEnable("AnimationInterface", LOG_LEVEL_INFO);
// Number of vehicles (nodes representing the vehicles)
// Central frequency and bandwidth for NR (New Radio)
double frequency = 28e9; // 28 GHz (mmWave)
double bandwidth = 100e6; // 100 MHz bandwidth
double hBS = 25; // Base station height
double hUT = 1.5; // User terminal (UE) antenna height
double speed = 1; // Speed of UEs in m/s (walking)
double txPower = 40; // Transmission power in dBm
bool mobility = true; // Enable mobility
BandwidthPartInfo::Scenario scenario = BandwidthPartInfo::UMa; // Urban Macro scenario
// Create nodes representing the vehicles and base stations
NodeContainer vehicles;
NodeContainer base;
vehicles.Create(2); // Create 2 vehicle nodes
base.Create(2); // Create 2 base station nodes
// Define positions for base stations
Ptr<ListPositionAllocator> enbPositionAlloc = CreateObject<ListPositionAllocator>();
enbPositionAlloc->Add(Vector(0.0, 0.0, hBS)); // Position of the first base station
enbPositionAlloc->Add(Vector(0.0, 80.0, hBS)); // Position of the second base station
MobilityHelper enbmobility;
enbmobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); // Constant position model
enbmobility.SetPositionAllocator(enbPositionAlloc); // Assign positions
enbmobility.Install(base); // Install mobility to the base stations
// Position the mobile terminals and enable mobility
MobilityHelper uemobility;
uemobility.SetMobilityModel("ns3::ConstantVelocityMobilityModel"); // Constant velocity model for UEs
uemobility.Install(vehicles); // Install mobility to the vehicle nodes
// Define the mobility behavior for the UEs based on the mobility flag
if (mobility)
{
// Set initial position and velocity for the first UE
vehicles.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(90, 15, hUT)); // Position (x, y, z)
vehicles.Get(0)->GetObject<ConstantVelocityMobilityModel>()->SetVelocity(Vector(0, speed, 0)); // Velocity (x, y, z)
// Set initial position and velocity for the second UE
vehicles.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(30, 50.0, hUT)); // Position (x, y, z)
vehicles.Get(1)->GetObject<ConstantVelocityMobilityModel>()->SetVelocity(Vector(-speed, 0, 0)); // Velocity (x, y, z)
}
else
{
// Set positions for UEs if mobility is disabled (no movement)
vehicles.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(90, 15, hUT));
vehicles.Get(0)->GetObject<ConstantVelocityMobilityModel>()->SetVelocity(Vector(0, 0, 0));
vehicles.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(30, 50.0, hUT));
vehicles.Get(1)->GetObject<ConstantVelocityMobilityModel>()->SetVelocity(Vector(0, 0, 0));
}
/*
* Create NR (New Radio) helpers
*/
Ptr<NrPointToPointEpcHelper> epcHelper = CreateObject<NrPointToPointEpcHelper>(); // EPC helper
Ptr<IdealBeamformingHelper> idealBeamformingHelper = CreateObject<IdealBeamformingHelper>(); // Beamforming helper
Ptr<NrHelper> nrHelper = CreateObject<NrHelper>(); // Main NR helper
nrHelper->SetBeamformingHelper(idealBeamformingHelper); // Set the beamforming helper
nrHelper->SetEpcHelper(epcHelper); // Set the EPC helper
// NR configuration for communication
BandwidthPartInfoPtrVector allBwps;
CcBwpCreator ccBwpCreator; // Bandwidth part creator
const uint8_t numCcPerBand = 1;
// Define the configuration of the operation band (frequency, bandwidth, and scenario)
CcBwpCreator::SimpleOperationBandConf bandConf(frequency, bandwidth, numCcPerBand, scenario);
// Create the operation band based on the configuration
OperationBandInfo band = ccBwpCreator.CreateOperationBandContiguousCc(bandConf);
nrHelper->InitializeOperationBand(&band); // Initialize the operation band with the helper
allBwps = CcBwpCreator::GetAllBwps({band}); // Get all bandwidth parts
// Set ideal beamforming method
idealBeamformingHelper->SetAttribute("BeamformingMethod",
TypeIdValue(DirectPathBeamforming::GetTypeId()));
// Set scheduler type for NR
nrHelper->SetSchedulerTypeId(NrMacSchedulerTdmaRR::GetTypeId());
// Configure UEs antennas
nrHelper->SetUeAntennaAttribute("NumRows", UintegerValue(2));
nrHelper->SetUeAntennaAttribute("NumColumns", UintegerValue(4));
nrHelper->SetUeAntennaAttribute("AntennaElement",
PointerValue(CreateObject<IsotropicAntennaModel>()));
// Configure gNBs antennas
nrHelper->SetGnbAntennaAttribute("NumRows", UintegerValue(8));
nrHelper->SetGnbAntennaAttribute("NumColumns", UintegerValue(8));
nrHelper->SetGnbAntennaAttribute("AntennaElement",
PointerValue(CreateObject<IsotropicAntennaModel>()));
// Install NR devices (gNB and UE)
NetDeviceContainer Uedevices = nrHelper->InstallUeDevice(vehicles, allBwps); // Install on vehicles (UEs)
NetDeviceContainer Enbdevices = nrHelper->InstallGnbDevice(base, allBwps); // Install on base stations (gNBs)
// Assign random streams to devices for randomization purposes
int64_t randomStream = 1;
randomStream += nrHelper->AssignStreams(Enbdevices, randomStream);
randomStream += nrHelper->AssignStreams(Uedevices, randomStream);
// Set transmission power for gNBs and UEs
nrHelper->GetGnbPhy(Enbdevices.Get(0), 0)->SetTxPower(txPower);
nrHelper->GetGnbPhy(Uedevices.Get(1), 0)->SetTxPower(txPower);
// Update the configuration for gNB and UE devices
for (auto it = Enbdevices.Begin(); it != Enbdevices.End(); ++it)
{
DynamicCast<NrGnbNetDevice>(*it)->UpdateConfig();
}
for (auto it = Uedevices.Begin(); it != Uedevices.End(); ++it)
{
DynamicCast<NrUeNetDevice>(*it)->UpdateConfig();
}
// Create the internet stack and install on the UEs
Ptr<Node> pgw = epcHelper->GetPgwNode(); // Get the PGW node (packet gateway)
NodeContainer remoteHostContainer;
remoteHostContainer.Create(1); // Create the remote host
Ptr<Node> remoteHost = remoteHostContainer.Get(0);
InternetStackHelper internet;
internet.Install(remoteHostContainer); // Install internet stack on the remote host
// Connect the remote host to the PGW and setup routing
PointToPointHelper p2ph;
p2ph.SetDeviceAttribute("DataRate", DataRateValue(DataRate("100Gb/s"))); // Data rate of the point-to-point link
p2ph.SetDeviceAttribute("Mtu", UintegerValue(2500)); // MTU size
p2ph.SetChannelAttribute("Delay", TimeValue(Seconds(0.010))); // Link delay
NetDeviceContainer internetDevices = p2ph.Install(pgw, remoteHost); // Install devices on PGW and remote host
// Assign IP addresses to the remote host
Ipv4AddressHelper ipv4h;
ipv4h.SetBase("1.0.0.0", "255.0.0.0"); // Base IP address
Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign(internetDevices);
Ipv4StaticRoutingHelper ipv4RoutingHelper;
// Setup routing for the remote host
Ptr<Ipv4StaticRouting> remoteHostStaticRouting =
ipv4RoutingHelper.GetStaticRouting(remoteHost->GetObject<Ipv4>());
remoteHostStaticRouting->AddNetworkRouteTo(Ipv4Address("7.0.0.0"), Ipv4Mask("255.0.0.0"), 1);
// Configure internet protocol on the UEs and gNBs
internet.Install(vehicles);
// Setup IP addresses for the UEs
Ipv4InterfaceContainer ueIpIface;
ueIpIface = epcHelper->AssignUeIpv4Address(NetDeviceContainer(Uedevices));
// Attach UEs to the network (associate with gNB)
nrHelper->AttachToClosestEnb(Uedevices, Enbdevices);
// Setup routes on the UEs
for (uint32_t u = 0; u < vehicles.GetN(); ++u)
{
Ptr<Node> ueNode = vehicles.Get(u);
Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting(ueNode->GetObject<Ipv4>());
ueStaticRouting->SetDefaultRoute(epcHelper->GetUeDefaultGatewayAddress(), 1);
}
// UDP server to receive packets
UdpServerHelper udpServer;
ApplicationContainer serverApp = udpServer.Install(vehicles.Get(0));
serverApp.Start(Seconds(0.0));
serverApp.Stop(Seconds(30.0));
// UDP client to send packets
uint16_t dlPort = 10000; // Downlink port number
UdpClientHelper udpClient(ueIpIface.GetAddress(0), dlPort); // Create UDP client
udpClient.SetAttribute("MaxPackets", UintegerValue(320)); // Maximum number of packets
udpClient.SetAttribute("Interval", TimeValue(MilliSeconds(100))); // Interval between packets
udpClient.SetAttribute("PacketSize", UintegerValue(1024)); // Packet size
ApplicationContainer clientApp = udpClient.Install(remoteHost);
clientApp.Start(Seconds(1.0));
clientApp.Stop(Seconds(30.0));
// Install animation interface for visualizing the simulation
AnimationInterface anim("scenario-vanet-nr.xml");
anim.EnablePacketMetadata(true); // Enable packet metadata in the animation
anim.EnableIpv4RouteTracking("scenario-nr.xml", Seconds(0.0), Seconds(30), Seconds(0.25)); // Enable route tracking
// Run the simulation
Simulator::Stop(Seconds(30.0)); // Simulation duration
Simulator::Run();
Simulator::Destroy();
return 0;
}
Command 'build/scratch/ns3.42-sim-vanet-optimized' died with <Signals.SIGSEGV: 11>.
I would appreciate any help or insights from the community. Thank you in advance for your collaboration!