A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
adhoc-aloha-ideal-phy-matrix-propagation-loss-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
10#include <ns3/applications-module.h>
11#include <ns3/core-module.h>
12#include <ns3/friis-spectrum-propagation-loss.h>
13#include <ns3/ism-spectrum-value-helper.h>
14#include <ns3/log.h>
15#include <ns3/mobility-module.h>
16#include <ns3/network-module.h>
17#include <ns3/propagation-delay-model.h>
18#include <ns3/single-model-spectrum-channel.h>
19#include <ns3/spectrum-analyzer.h>
20#include <ns3/spectrum-helper.h>
21#include <ns3/spectrum-model-300kHz-300GHz-log.h>
22#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
23#include <ns3/waveform-generator.h>
24
25#include <iomanip>
26#include <iostream>
27#include <string>
28
29using namespace ns3;
30
31NS_LOG_COMPONENT_DEFINE("TestAdhocOfdmAloha");
32
33static bool g_verbose = false; //!< True if verbose output.
34static uint64_t g_rxBytes; //!< Rx bytes counter.
35
36/**
37 * Trace for PHY Rx successful end.
38 *
39 * \param context The context.
40 * \param p The packet.
41 */
42void
43PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
44{
45 if (g_verbose)
46 {
47 std::cout << context << " PHY RX END OK p:" << p << std::endl;
48 }
49 g_rxBytes += p->GetSize();
50}
51
52/**
53 * \ingroup spectrum
54 *
55 * Store the last pathloss value for each TX-RX pair. This is an
56 * example of how the PathlossTrace (provided by some SpectrumChannel
57 * implementations) work.
58 *
59 */
61{
62 public:
63 /**
64 * update the pathloss value
65 *
66 * \param context
67 * \param txPhy the transmitting PHY
68 * \param rxPhy the receiving PHY
69 * \param lossDb the loss in dB
70 */
71 void UpdatePathloss(std::string context,
74 double lossDb);
75
76 /**
77 * print the stored pathloss values to standard output
78 *
79 */
80 void Print();
81
82 private:
83 std::map<uint32_t, std::map<uint32_t, double>> m_pathlossMap; //!< Path loss map
84};
85
86void
88 Ptr<const SpectrumPhy> txPhyConst,
89 Ptr<const SpectrumPhy> rxPhyConst,
90 double lossDb)
91{
92 Ptr<SpectrumPhy> txPhy = ConstCast<SpectrumPhy>(txPhyConst);
93 Ptr<SpectrumPhy> rxPhy = ConstCast<SpectrumPhy>(rxPhyConst);
94 uint32_t txNodeId = txPhy->GetMobility()->GetObject<Node>()->GetId();
95 uint32_t rxNodeId = rxPhy->GetMobility()->GetObject<Node>()->GetId();
96 m_pathlossMap[txNodeId][rxNodeId] = lossDb;
97}
98
99void
101{
102 for (auto txit = m_pathlossMap.begin(); txit != m_pathlossMap.end(); ++txit)
103 {
104 for (auto rxit = txit->second.begin(); rxit != txit->second.end(); ++rxit)
105 {
106 std::cout << txit->first << " --> " << rxit->first << " : " << rxit->second << " dB"
107 << std::endl;
108 }
109 }
110}
111
112int
113main(int argc, char** argv)
114{
115 CommandLine cmd(__FILE__);
116 double lossDb = 130;
117 double txPowerW = 0.1;
118 uint64_t phyRate = 500000;
119 uint32_t pktSize = 1000;
120 double simDuration = 0.5;
121 std::string channelType("ns3::SingleModelSpectrumChannel");
122 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
123 cmd.AddValue("lossDb", "link loss in dB", lossDb);
124 cmd.AddValue("txPowerW", "txPower in Watts", txPowerW);
125 cmd.AddValue("phyRate", "PHY rate in bps", phyRate);
126 cmd.AddValue("pktSize", "packet size in bytes", pktSize);
127 cmd.AddValue("simDuration", "duration of the simulation in seconds", simDuration);
128 cmd.AddValue("channelType", "which SpectrumChannel implementation to be used", channelType);
129 cmd.Parse(argc, argv);
130
132 c.Create(2);
133
134 MobilityHelper mobility;
135 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
136 mobility.Install(c);
137 // the actual positions are irrelevant, since we use MatrixPropagationLossModel
138
139 SpectrumChannelHelper channelHelper;
140 channelHelper.SetChannel(channelType);
141 channelHelper.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
143 propLoss->SetLoss(c.Get(0)->GetObject<MobilityModel>(),
144 c.Get(1)->GetObject<MobilityModel>(),
145 lossDb,
146 true);
147 channelHelper.AddPropagationLoss(propLoss);
148 Ptr<SpectrumChannel> channel = channelHelper.Create();
149
151
152 uint32_t channelNumber = 1;
153 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPowerW, channelNumber);
154
155 // for the noise, we use the Power Spectral Density of thermal noise
156 // at room temperature. The value of the PSD will be constant over the band of interest.
157 const double k = 1.381e-23; // Boltzmann's constant
158 const double T = 290; // temperature in Kelvin
159 double noisePsdValue = k * T; // watts per hertz
160 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
161
163 deviceHelper.SetChannel(channel);
164 deviceHelper.SetTxPowerSpectralDensity(txPsd);
165 deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
166 deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate(phyRate)));
167 NetDeviceContainer devices = deviceHelper.Install(c);
168
169 PacketSocketHelper packetSocket;
170 packetSocket.Install(c);
171
172 PacketSocketAddress socket;
173 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
174 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
175 socket.SetProtocol(1);
176
177 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
178 onoff.SetConstantRate(DataRate(2 * phyRate));
179 onoff.SetAttribute("PacketSize", UintegerValue(pktSize));
180
181 ApplicationContainer apps = onoff.Install(c.Get(0));
182 apps.Start(Seconds(0.0));
183 apps.Stop(Seconds(simDuration));
184
185 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
186
187 GlobalPathlossDatabase globalPathlossDatabase;
188 Config::Connect("/ChannelList/*/$ns3::SpectrumChannel/PathLoss",
189 MakeCallback(&GlobalPathlossDatabase::UpdatePathloss, &globalPathlossDatabase));
190
191 g_rxBytes = 0;
192 Simulator::Stop(Seconds(simDuration + 0.000001));
194
195 if (g_verbose)
196 {
197 globalPathlossDatabase.Print();
198
199 double throughputBps = (g_rxBytes * 8.0) / simDuration;
200 std::cout << "throughput: " << throughputBps << std::endl;
201 std::cout << "throughput: " << std::setw(20) << std::fixed << throughputBps << " bps"
202 << std::endl;
203 std::cout << "phy rate : " << std::setw(20) << std::fixed << phyRate * 1.0 << " bps"
204 << std::endl;
205 double rxPowerW = txPowerW / (std::pow(10.0, lossDb / 10.0));
206 double capacity = 20e6 * log2(1.0 + (rxPowerW / 20.0e6) / noisePsdValue);
207 std::cout << "shannon capacity: " << std::setw(20) << std::fixed << capacity << " bps"
208 << std::endl;
209 }
210
212 return 0;
213}
static bool g_verbose
True if verbose output.
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
Trace for PHY Rx successful end.
static uint64_t g_rxBytes
Rx bytes counter.
Store the last pathloss value for each TX-RX pair.
void Print()
print the stored pathloss values to standard output
void UpdatePathloss(std::string context, Ptr< const SpectrumPhy > txPhy, Ptr< const SpectrumPhy > rxPhy, double lossDb)
update the pathloss value
std::map< uint32_t, std::map< uint32_t, double > > m_pathlossMap
Path loss map.
a polymophic address class
Definition address.h:90
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
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.
Class for representing data rates.
Definition data-rate.h:78
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A network Node.
Definition node.h:46
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
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
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
void AddPropagationLoss(std::string name, Ts &&... args)
void SetPropagationDelay(std::string name, Ts &&... args)
void SetChannel(std::string type, Ts &&... args)
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
virtual Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint8_t channel)
Creates a SpectrumValue instance that represents the TX Power Spectral Density of a wifi device corre...
Hold an unsigned integer type.
Definition uinteger.h:34
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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 > ConstCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:573
uint32_t pktSize
packet size used for the simulation (in bytes)