A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
aloha-throughput.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
9#include "ns3/building-allocator.h"
10#include "ns3/building-penetration-loss.h"
11#include "ns3/buildings-helper.h"
12#include "ns3/callback.h"
13#include "ns3/command-line.h"
14#include "ns3/constant-position-mobility-model.h"
15#include "ns3/correlated-shadowing-propagation-loss-model.h"
16#include "ns3/double.h"
17#include "ns3/end-device-lora-phy.h"
18#include "ns3/end-device-lorawan-mac.h"
19#include "ns3/forwarder-helper.h"
20#include "ns3/gateway-lora-phy.h"
21#include "ns3/gateway-lorawan-mac.h"
22#include "ns3/log.h"
23#include "ns3/lora-device-address.h"
24#include "ns3/lora-frame-header.h"
25#include "ns3/lora-helper.h"
26#include "ns3/lora-net-device.h"
27#include "ns3/lora-phy.h"
28#include "ns3/lorawan-mac-header.h"
29#include "ns3/mobility-helper.h"
30#include "ns3/network-server-helper.h"
31#include "ns3/node-container.h"
32#include "ns3/periodic-sender-helper.h"
33#include "ns3/pointer.h"
34#include "ns3/position-allocator.h"
35#include "ns3/random-variable-stream.h"
36#include "ns3/simulator.h"
37
38#include <algorithm>
39#include <ctime>
40
41using namespace ns3;
42using namespace lorawan;
43
44NS_LOG_COMPONENT_DEFINE("AlohaThroughput");
45
46// Network settings
47int nDevices = 200; //!< Number of end device nodes to create
48int nGateways = 1; //!< Number of gateway nodes to create
49double radiusMeters = 1000; //!< Radius (m) of the deployment
50double simulationTimeSeconds = 100; //!< Scenario duration (s) in simulated time
51
52// Channel model
53bool realisticChannelModel = false; //!< Whether to use a more realistic channel model with
54 //!< buildings and correlated shadowing
55
56/** Record received pkts by Data Rate (DR) [index 0 -> DR5, index 5 -> DR0]. */
57auto packetsSent = std::vector<int>(6, 0);
58/** Record received pkts by Data Rate (DR) [index 0 -> DR5, index 5 -> DR0]. */
59auto packetsReceived = std::vector<int>(6, 0);
60
61/**
62 * Record the beginning of a transmission by an end device.
63 *
64 * \param packet A pointer to the packet sent.
65 * \param senderNodeId Node id of the sender end device.
66 */
67void
69{
70 NS_LOG_FUNCTION(packet << senderNodeId);
71 LoraTag tag;
72 packet->PeekPacketTag(tag);
73 packetsSent.at(tag.GetSpreadingFactor() - 7)++;
74}
75
76/**
77 * Record the correct reception of a packet by a gateway.
78 *
79 * \param packet A pointer to the packet received.
80 * \param receiverNodeId Node id of the receiver gateway.
81 */
82void
84{
85 NS_LOG_FUNCTION(packet << receiverNodeId);
86 LoraTag tag;
87 packet->PeekPacketTag(tag);
88 packetsReceived.at(tag.GetSpreadingFactor() - 7)++;
89}
90
91int
92main(int argc, char* argv[])
93{
94 std::string interferenceMatrix = "aloha";
95
96 CommandLine cmd(__FILE__);
97 cmd.AddValue("nDevices", "Number of end devices to include in the simulation", nDevices);
98 cmd.AddValue("simulationTime", "Simulation Time (s)", simulationTimeSeconds);
99 cmd.AddValue("interferenceMatrix",
100 "Interference matrix to use [aloha, goursaud]",
101 interferenceMatrix);
102 cmd.AddValue("radius", "Radius (m) of the deployment", radiusMeters);
103 cmd.Parse(argc, argv);
104
106
107 // Set up logging
108 LogComponentEnable("AlohaThroughput", LOG_LEVEL_ALL);
109
110 // Make all devices use SF7 (i.e., DR5)
111 // Config::SetDefault ("ns3::EndDeviceLorawanMac::DataRate", UintegerValue (5));
112
113 if (interferenceMatrix == "aloha")
114 {
116 }
117 else if (interferenceMatrix == "goursaud")
118 {
120 }
121
122 /***********
123 * Setup *
124 ***********/
125
126 // Mobility
128 mobility.SetPositionAllocator("ns3::UniformDiscPositionAllocator",
129 "rho",
131 "X",
132 DoubleValue(0.0),
133 "Y",
134 DoubleValue(0.0));
135 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
136
137 /************************
138 * Create the channel *
139 ************************/
140
141 // Create the lora channel object
143 loss->SetPathLossExponent(3.76);
144 loss->SetReference(1, 7.7);
145
147 {
148 // Create the correlated shadowing component
151
152 // Aggregate shadowing to the logdistance loss
153 loss->SetNext(shadowing);
154
155 // Add the effect to the channel propagation loss
157
158 shadowing->SetNext(buildingLoss);
159 }
160
162
164
165 /************************
166 * Create the helpers *
167 ************************/
168
169 // Create the LoraPhyHelper
170 LoraPhyHelper phyHelper = LoraPhyHelper();
171 phyHelper.SetChannel(channel);
172
173 // Create the LorawanMacHelper
176
177 // Create the LoraHelper
178 LoraHelper helper = LoraHelper();
179 helper.EnablePacketTracking(); // Output filename
180
181 // Create the NetworkServerHelper
183
184 // Create the ForwarderHelper
185 ForwarderHelper forHelper = ForwarderHelper();
186
187 /************************
188 * Create End Devices *
189 ************************/
190
191 // Create a set of nodes
192 NodeContainer endDevices;
193 endDevices.Create(nDevices);
194
195 // Assign a mobility model to each node
196 mobility.Install(endDevices);
197
198 // Make it so that nodes are at a certain height > 0
199 for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
200 {
201 Ptr<MobilityModel> mobility = (*j)->GetObject<MobilityModel>();
202 Vector position = mobility->GetPosition();
203 position.z = 1.2;
204 mobility->SetPosition(position);
205 }
206
207 // Create the LoraNetDevices of the end devices
208 uint8_t nwkId = 54;
209 uint32_t nwkAddr = 1864;
212
213 // Create the LoraNetDevices of the end devices
214 macHelper.SetAddressGenerator(addrGen);
217 helper.Install(phyHelper, macHelper, endDevices);
218
219 // Now end devices are connected to the channel
220
221 // Connect trace sources
222 for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
223 {
224 Ptr<Node> node = *j;
225 Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(node->GetDevice(0));
226 Ptr<LoraPhy> phy = loraNetDevice->GetPhy();
227 }
228
229 /*********************
230 * Create Gateways *
231 *********************/
232
233 // Create the gateway nodes (allocate them uniformly on the disc)
234 NodeContainer gateways;
235 gateways.Create(nGateways);
236
238 // Make it so that nodes are at a certain height > 0
239 allocator->Add(Vector(0.0, 0.0, 15.0));
240 mobility.SetPositionAllocator(allocator);
241 mobility.Install(gateways);
242
243 // Create a netdevice for each gateway
246 helper.Install(phyHelper, macHelper, gateways);
247
248 NS_LOG_DEBUG("Completed configuration");
249
250 /*********************************************
251 * Install applications on the end devices *
252 *********************************************/
253
254 Time appStopTime = Seconds(simulationTimeSeconds);
255 int packetSize = 50;
258 appHelper.SetPacketSize(packetSize);
259 ApplicationContainer appContainer = appHelper.Install(endDevices);
260
261 appContainer.Start(Seconds(0));
262 appContainer.Stop(appStopTime);
263
264 std::ofstream outputFile;
265 // Delete contents of the file as it is opened
266 outputFile.open("durations.txt", std::ofstream::out | std::ofstream::trunc);
267 for (uint8_t sf = 7; sf <= 12; sf++)
268 {
269 LoraTxParameters txParams;
270 txParams.sf = sf;
271 txParams.headerDisabled = false;
272 txParams.codingRate = 1;
273 txParams.bandwidthHz = 125000;
274 txParams.nPreamble = 8;
275 txParams.crcEnabled = true;
278
279 LoraFrameHeader frameHdr = LoraFrameHeader();
280 frameHdr.SetAsUplink();
281 frameHdr.SetFPort(1);
282 frameHdr.SetAddress(LoraDeviceAddress());
283 frameHdr.SetAdr(false);
284 frameHdr.SetAdrAckReq(false);
285 frameHdr.SetFCnt(0);
286 pkt->AddHeader(frameHdr);
287
290 macHdr.SetMajor(1);
291 pkt->AddHeader(macHdr);
292
293 outputFile << LoraPhy::GetOnAirTime(pkt, txParams).GetMicroSeconds() << " ";
294 }
295 outputFile.close();
296
297 /**************************
298 * Create network server *
299 ***************************/
300
301 // Create the network server node
302 Ptr<Node> networkServer = CreateObject<Node>();
303
304 // PointToPoint links between gateways and server
306 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
307 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
308 // Store network server app registration details for later
309 P2PGwRegistration_t gwRegistration;
310 for (auto gw = gateways.Begin(); gw != gateways.End(); ++gw)
311 {
312 auto container = p2p.Install(networkServer, *gw);
313 auto serverP2PNetDev = DynamicCast<PointToPointNetDevice>(container.Get(0));
314 gwRegistration.emplace_back(serverP2PNetDev, *gw);
315 }
316
317 // Create a network server for the network
318 nsHelper.SetGatewaysP2P(gwRegistration);
319 nsHelper.SetEndDevices(endDevices);
320 nsHelper.Install(networkServer);
321
322 // Create a forwarder for each gateway
323 forHelper.Install(gateways);
324
325 // Install trace sources
326 for (auto node = gateways.Begin(); node != gateways.End(); node++)
327 {
328 DynamicCast<LoraNetDevice>((*node)->GetDevice(0))
329 ->GetPhy()
330 ->TraceConnectWithoutContext("ReceivedPacket", MakeCallback(OnPacketReceptionCallback));
331 }
332
333 // Install trace sources
334 for (auto node = endDevices.Begin(); node != endDevices.End(); node++)
335 {
336 DynamicCast<LoraNetDevice>((*node)->GetDevice(0))
337 ->GetPhy()
338 ->TraceConnectWithoutContext("StartSending", MakeCallback(OnTransmissionCallback));
339 }
340
341 LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
342
343 ////////////////
344 // Simulation //
345 ////////////////
346
347 Simulator::Stop(appStopTime + Hours(1));
348
349 NS_LOG_INFO("Running simulation...");
351
353
354 /////////////////////////////
355 // Print results to stdout //
356 /////////////////////////////
357 NS_LOG_INFO("Computing performance metrics...");
358
359 for (int i = 0; i < 6; i++)
360 {
361 std::cout << packetsSent.at(i) << " " << packetsReceived.at(i) << std::endl;
362 }
363
364 return 0;
365}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:402
This class can be used to install Forwarder applications on a set of gateways.
ApplicationContainer Install(NodeContainer c) const
Install a Forwarder application on each node of the input container configured with all the attribute...
This class represents the device address of a LoraWAN end device.
This class represents the Frame header (FHDR) used in a LoraWAN network.
void SetFCnt(uint16_t fCnt)
Set the FCnt value.
void SetAdr(bool adr)
Set the value of the ADR bit field.
void SetAddress(LoraDeviceAddress address)
Set the address.
void SetAsUplink()
State that this is an uplink message.
void SetAdrAckReq(bool adrAckReq)
Set the value of the ADRACKReq bit field.
void SetFPort(uint8_t fPort)
Set the FPort value.
Helps to create LoraNetDevice objects.
Definition lora-helper.h:37
void EnablePacketTracking()
Enable tracking of packets via trace sources.
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
static CollisionMatrix collisionMatrix
Collision matrix type set by the constructor.
Helper to install LoraPhy instances on multiple Nodes.
void SetDeviceType(enum DeviceType dt)
Set the kind of PHY this helper will create.
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel to connect the PHYs to.
static Time GetOnAirTime(Ptr< Packet > packet, LoraTxParameters txParams)
Compute the time that a packet with certain characteristics will take to be transmitted.
Definition lora-phy.cc:156
static Time GetTSym(LoraTxParameters txParams)
Compute the symbol time from spreading factor and bandwidth.
Definition lora-phy.cc:150
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition lora-tag.h:26
uint8_t GetSpreadingFactor() const
Read which Spreading Factor this packet was transmitted with.
Definition lora-tag.cc:83
This class represents the Mac header of a LoRaWAN packet.
void SetMajor(uint8_t major)
Set the major version of this header.
void SetMType(enum MType mtype)
Set the message type.
Helper class for configuring and installing the LorawanMac class on devices and gateways.
void SetDeviceType(enum DeviceType dt)
Set the kind of MAC this helper will create.
void SetAddressGenerator(Ptr< LoraDeviceAddressGenerator > addrGen)
Set the address generator to use for creation of these nodes.
static std::vector< int > SetSpreadingFactorsUp(NodeContainer endDevices, NodeContainer gateways, Ptr< LoraChannel > channel)
Initialize the end devices' data rate parameter.
void SetRegion(enum Regions region)
Set the region in which the device is to operate.
This class can install a NetworkServer application on a node.
void SetGatewaysP2P(const P2PGwRegistration_t &registration)
Register gateways connected with point-to-point to this network server.
void SetEndDevices(NodeContainer endDevices)
Set which end devices will be managed by this network server.
ApplicationContainer Install(Ptr< Node > node)
Create one lorawan network server application on the Node.
This class can be used to install PeriodicSender applications on a wide range of nodes.
void SetPeriod(Time period)
Set the period to be used by the applications created by this helper.
void SetPacketSize(uint8_t size)
Set the base value for applications packet size in bytes.
ApplicationContainer Install(NodeContainer c) const
Install a PeriodicSender application on each node of the input container configured with all the attr...
int appPeriodSeconds
Duration (s) of the inter-transmission time of end devices.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time Hours(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1284
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
std::list< std::pair< Ptr< PointToPointNetDevice >, Ptr< Node > > > P2PGwRegistration_t
Store network server app registration details for gateway nodes having a P2P link with the network se...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
channel
Definition third.py:77
mobility
Definition third.py:92
phy
Definition third.py:78
auto packetsReceived
Record received pkts by Data Rate (DR) [index 0 -> DR5, index 5 -> DR0].
void OnPacketReceptionCallback(Ptr< const Packet > packet, uint32_t receiverNodeId)
Record the correct reception of a packet by a gateway.
double simulationTimeSeconds
Scenario duration (s) in simulated time.
auto packetsSent
Record received pkts by Data Rate (DR) [index 0 -> DR5, index 5 -> DR0].
int nDevices
Number of end device nodes to create.
bool realisticChannelModel
Whether to use a more realistic channel model with buildings and correlated shadowing.
int nGateways
Number of gateway nodes to create.
double radiusMeters
Radius (m) of the deployment.
void OnTransmissionCallback(Ptr< const Packet > packet, uint32_t senderNodeId)
Record the beginning of a transmission by an end device.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:38
uint8_t codingRate
Code rate (obtained as 4/(codingRate+4))
Definition lora-phy.h:41
uint32_t nPreamble
Number of preamble symbols.
Definition lora-phy.h:43
bool headerDisabled
Whether to use implicit header mode.
Definition lora-phy.h:40
double bandwidthHz
Bandwidth in Hz.
Definition lora-phy.h:42
bool lowDataRateOptimizationEnabled
Whether low data rate optimization is enabled.
Definition lora-phy.h:45
bool crcEnabled
Whether Cyclic Redundancy Check (CRC) is enabled.
Definition lora-phy.h:44
uint8_t sf
Spreading Factor.
Definition lora-phy.h:39
static const uint32_t packetSize
Packet size generated at the AP.