A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
adhoc-aloha-ideal-phy-matrix-propagation-loss-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Nicola Baldo <nbaldo@cttc.es>
18  */
19 
20 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
21 #include <ns3/applications-module.h>
22 #include <ns3/core-module.h>
23 #include <ns3/friis-spectrum-propagation-loss.h>
24 #include <ns3/ism-spectrum-value-helper.h>
25 #include <ns3/log.h>
26 #include <ns3/mobility-module.h>
27 #include <ns3/network-module.h>
28 #include <ns3/propagation-delay-model.h>
29 #include <ns3/single-model-spectrum-channel.h>
30 #include <ns3/spectrum-analyzer.h>
31 #include <ns3/spectrum-helper.h>
32 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
33 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
34 #include <ns3/waveform-generator.h>
35 
36 #include <iomanip>
37 #include <iostream>
38 #include <string>
39 
40 using namespace ns3;
41 
42 NS_LOG_COMPONENT_DEFINE("TestAdhocOfdmAloha");
43 
44 static bool g_verbose = false;
45 static uint64_t g_rxBytes;
46 
53 void
54 PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
55 {
56  if (g_verbose)
57  {
58  std::cout << context << " PHY RX END OK p:" << p << std::endl;
59  }
60  g_rxBytes += p->GetSize();
61 }
62 
72 {
73  public:
82  void UpdatePathloss(std::string context,
85  double lossDb);
86 
91  void Print();
92 
93  private:
94  std::map<uint32_t, std::map<uint32_t, double>> m_pathlossMap;
95 };
96 
97 void
99  Ptr<const SpectrumPhy> txPhyConst,
100  Ptr<const SpectrumPhy> rxPhyConst,
101  double lossDb)
102 {
103  Ptr<SpectrumPhy> txPhy = ConstCast<SpectrumPhy>(txPhyConst);
104  Ptr<SpectrumPhy> rxPhy = ConstCast<SpectrumPhy>(rxPhyConst);
105  uint32_t txNodeId = txPhy->GetMobility()->GetObject<Node>()->GetId();
106  uint32_t rxNodeId = rxPhy->GetMobility()->GetObject<Node>()->GetId();
107  m_pathlossMap[txNodeId][rxNodeId] = lossDb;
108 }
109 
110 void
112 {
113  for (auto txit = m_pathlossMap.begin(); txit != m_pathlossMap.end(); ++txit)
114  {
115  for (auto rxit = txit->second.begin(); rxit != txit->second.end(); ++rxit)
116  {
117  std::cout << txit->first << " --> " << rxit->first << " : " << rxit->second << " dB"
118  << std::endl;
119  }
120  }
121 }
122 
123 int
124 main(int argc, char** argv)
125 {
126  CommandLine cmd(__FILE__);
127  double lossDb = 130;
128  double txPowerW = 0.1;
129  uint64_t phyRate = 500000;
130  uint32_t pktSize = 1000;
131  double simDuration = 0.5;
132  std::string channelType("ns3::SingleModelSpectrumChannel");
133  cmd.AddValue("verbose", "Print trace information if true", g_verbose);
134  cmd.AddValue("lossDb", "link loss in dB", lossDb);
135  cmd.AddValue("txPowerW", "txPower in Watts", txPowerW);
136  cmd.AddValue("phyRate", "PHY rate in bps", phyRate);
137  cmd.AddValue("pktSize", "packet size in bytes", pktSize);
138  cmd.AddValue("simDuration", "duration of the simulation in seconds", simDuration);
139  cmd.AddValue("channelType", "which SpectrumChannel implementation to be used", channelType);
140  cmd.Parse(argc, argv);
141 
142  NodeContainer c;
143  c.Create(2);
144 
146  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
147  mobility.Install(c);
148  // the actual positions are irrelevant, since we use MatrixPropagationLossModel
149 
150  SpectrumChannelHelper channelHelper;
151  channelHelper.SetChannel(channelType);
152  channelHelper.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
153  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel>();
154  propLoss->SetLoss(c.Get(0)->GetObject<MobilityModel>(),
155  c.Get(1)->GetObject<MobilityModel>(),
156  lossDb,
157  true);
158  channelHelper.AddPropagationLoss(propLoss);
159  Ptr<SpectrumChannel> channel = channelHelper.Create();
160 
162 
163  uint32_t channelNumber = 1;
164  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPowerW, channelNumber);
165 
166  // for the noise, we use the Power Spectral Density of thermal noise
167  // at room temperature. The value of the PSD will be constant over the band of interest.
168  const double k = 1.381e-23; // Boltzmann's constant
169  const double T = 290; // temperature in Kelvin
170  double noisePsdValue = k * T; // watts per hertz
171  Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
172 
173  AdhocAlohaNoackIdealPhyHelper deviceHelper;
174  deviceHelper.SetChannel(channel);
175  deviceHelper.SetTxPowerSpectralDensity(txPsd);
176  deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
177  deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate(phyRate)));
178  NetDeviceContainer devices = deviceHelper.Install(c);
179 
180  PacketSocketHelper packetSocket;
181  packetSocket.Install(c);
182 
183  PacketSocketAddress socket;
184  socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
185  socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
186  socket.SetProtocol(1);
187 
188  OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
189  onoff.SetConstantRate(DataRate(2 * phyRate));
190  onoff.SetAttribute("PacketSize", UintegerValue(pktSize));
191 
192  ApplicationContainer apps = onoff.Install(c.Get(0));
193  apps.Start(Seconds(0.0));
194  apps.Stop(Seconds(simDuration));
195 
196  Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
197 
198  GlobalPathlossDatabase globalPathlossDatabase;
199  Config::Connect("/ChannelList/*/$ns3::SpectrumChannel/PathLoss",
200  MakeCallback(&GlobalPathlossDatabase::UpdatePathloss, &globalPathlossDatabase));
201 
202  g_rxBytes = 0;
203  Simulator::Stop(Seconds(simDuration + 0.000001));
204  Simulator::Run();
205 
206  if (g_verbose)
207  {
208  globalPathlossDatabase.Print();
209 
210  double throughputBps = (g_rxBytes * 8.0) / simDuration;
211  std::cout << "throughput: " << throughputBps << std::endl;
212  std::cout << "throughput: " << std::setw(20) << std::fixed << throughputBps << " bps"
213  << std::endl;
214  std::cout << "phy rate : " << std::setw(20) << std::fixed << phyRate * 1.0 << " bps"
215  << std::endl;
216  double rxPowerW = txPowerW / (std::pow(10.0, lossDb / 10.0));
217  double capacity = 20e6 * log2(1.0 + (rxPowerW / 20.0e6) / noisePsdValue);
218  std::cout << "shannon capacity: " << std::setw(20) << std::fixed << capacity << " bps"
219  << std::endl;
220  }
221 
223  return 0;
224 }
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:101
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.
Definition: command-line.h:232
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:57
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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)
virtual Ptr< MobilityModel > GetMobility() const =0
Get the associated MobilityModel instance.
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:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void Print(ComponentCarrier cc)
devices
Definition: first.py:42
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:704
cmd
Definition: second.py:40
channel
Definition: third.py:88
mobility
Definition: third.py:105
uint32_t pktSize
packet size used for the simulation (in bytes)