A Discrete-Event Network Simulator
API
adhoc-aloha-ideal-phy-matrix-propagation-loss-model.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 
22 
23 #include <iostream>
24 
25 #include <ns3/core-module.h>
26 #include <ns3/network-module.h>
27 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
28 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
29 #include <ns3/wifi-spectrum-value-helper.h>
30 #include <ns3/single-model-spectrum-channel.h>
31 #include <ns3/waveform-generator.h>
32 #include <ns3/spectrum-analyzer.h>
33 #include <ns3/log.h>
34 #include <string>
35 #include <iomanip>
36 #include <ns3/friis-spectrum-propagation-loss.h>
37 #include <ns3/propagation-delay-model.h>
38 #include <ns3/mobility-module.h>
39 #include <ns3/spectrum-helper.h>
40 #include <ns3/applications-module.h>
41 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
42 
43 
44 using namespace ns3;
45 
46 NS_LOG_COMPONENT_DEFINE ("TestAdhocOfdmAloha");
47 
48 static bool g_verbose = false;
49 static uint64_t g_rxBytes;
50 
51 void
52 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
53 {
54  if (g_verbose)
55  {
56  std::cout << context << " PHY RX END OK p:" << p << std::endl;
57  }
58  g_rxBytes += p->GetSize ();
59 }
60 
61 
62 
72 {
73 public:
74 
83  void UpdatePathloss (std::string context, Ptr<const SpectrumPhy> txPhy, Ptr<const SpectrumPhy> rxPhy, double lossDb);
84 
89  void Print ();
90 
91 private:
92  std::map<uint32_t, std::map<uint32_t, double> > m_pathlossMap;
93 };
94 
95 void
97  Ptr<const SpectrumPhy> txPhyConst,
98  Ptr<const SpectrumPhy> rxPhyConst,
99  double lossDb)
100 {
101  Ptr<SpectrumPhy> txPhy = ConstCast<SpectrumPhy> (txPhyConst);
102  Ptr<SpectrumPhy> rxPhy = ConstCast<SpectrumPhy> (rxPhyConst);
103  uint32_t txNodeId = txPhy->GetMobility ()->GetObject<Node> ()->GetId ();
104  uint32_t rxNodeId = rxPhy->GetMobility ()->GetObject<Node> ()->GetId ();
105  m_pathlossMap[txNodeId][rxNodeId] = lossDb;
106 }
107 
108 void
110 {
111  for (std::map<uint32_t, std::map<uint32_t, double> >::const_iterator txit = m_pathlossMap.begin ();
112  txit != m_pathlossMap.end ();
113  ++txit)
114  {
115  for (std::map<uint32_t, double>::const_iterator rxit = txit->second.begin ();
116  rxit != txit->second.end ();
117  ++rxit)
118  {
119  std::cout << txit->first << " --> " << rxit->first << " : " << rxit->second << " dB" << std::endl;
120  }
121  }
122 }
123 
124 
125 
126 int main (int argc, char** argv)
127 {
128  CommandLine cmd (__FILE__);
129  double lossDb = 130;
130  double txPowerW = 0.1;
131  uint64_t phyRate = 500000;
132  uint32_t pktSize = 1000;
133  double simDuration = 0.5;
134  std::string channelType ("ns3::SingleModelSpectrumChannel");
135  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
136  cmd.AddValue ("lossDb", "link loss in dB", lossDb);
137  cmd.AddValue ("txPowerW", "txPower in Watts", txPowerW);
138  cmd.AddValue ("phyRate", "PHY rate in bps", phyRate);
139  cmd.AddValue ("pktSize", "packet size in bytes", pktSize);
140  cmd.AddValue ("simDuration", "duration of the simulation in seconds", simDuration);
141  cmd.AddValue ("channelType", "which SpectrumChannel implementation to be used", channelType);
142  cmd.Parse (argc, argv);
143 
144  NodeContainer c;
145  c.Create (2);
146 
148  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
149  mobility.Install (c);
150  // the actual positions are irrelevant, since we use MatrixPropagationLossModel
151 
152 
153  SpectrumChannelHelper channelHelper;
154  channelHelper.SetChannel (channelType);
155  channelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
156  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
157  propLoss->SetLoss (c.Get (0)->GetObject<MobilityModel> (), c.Get (1)->GetObject<MobilityModel> (), lossDb, true);
158  channelHelper.AddPropagationLoss (propLoss);
159  Ptr<SpectrumChannel> channel = channelHelper.Create ();
160 
161 
163 
164  uint32_t channelNumber = 1;
165  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
166 
167  // for the noise, we use the Power Spectral Density of thermal noise
168  // at room temperature. The value of the PSD will be constant over the band of interest.
169  const double k = 1.381e-23; //Boltzmann's constant
170  const double T = 290; // temperature in Kelvin
171  double noisePsdValue = k * T; // watts per hertz
172  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
173 
174  AdhocAlohaNoackIdealPhyHelper deviceHelper;
175  deviceHelper.SetChannel (channel);
176  deviceHelper.SetTxPowerSpectralDensity (txPsd);
177  deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
178  deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate (phyRate)));
179  NetDeviceContainer devices = deviceHelper.Install (c);
180 
181  PacketSocketHelper packetSocket;
182  packetSocket.Install (c);
183 
184  PacketSocketAddress socket;
185  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
186  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
187  socket.SetProtocol (1);
188 
189  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
190  onoff.SetConstantRate (DataRate (2*phyRate));
191  onoff.SetAttribute ("PacketSize", UintegerValue (pktSize));
192 
193  ApplicationContainer apps = onoff.Install (c.Get (0));
194  apps.Start (Seconds (0.0));
195  apps.Stop (Seconds (simDuration));
196 
197  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
198 
199  GlobalPathlossDatabase globalPathlossDatabase;
200  Config::Connect ("/ChannelList/*/$ns3::SpectrumChannel/PathLoss",
201  MakeCallback (&GlobalPathlossDatabase::UpdatePathloss, &globalPathlossDatabase));
202 
203  g_rxBytes = 0;
204  Simulator::Stop (Seconds (simDuration + 0.000001));
205  Simulator::Run ();
206 
207  if (g_verbose)
208  {
209  globalPathlossDatabase.Print ();
210 
211  double throughputBps = (g_rxBytes * 8.0) / simDuration;
212  std::cout << "throughput: " << throughputBps << std::endl;
213  std::cout << "throughput: " << std::setw (20) << std::fixed << throughputBps << " bps" << std::endl;
214  std::cout << "phy rate : " << std::setw (20) << std::fixed << phyRate*1.0 << " bps" << std::endl;
215  double rxPowerW = txPowerW / (std::pow (10.0, lossDb/10.0));
216  double capacity = 20e6*log2 (1.0 + (rxPowerW/20.0e6)/noisePsdValue);
217  std::cout << "shannon capacity: " << std::setw (20) << std::fixed << capacity << " bps" << std::endl;
218 
219  }
220 
221 
222 
224  return 0;
225 }
holds a vector of ns3::Application pointers.
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
void UpdatePathloss(std::string context, Ptr< const SpectrumPhy > txPhy, Ptr< const SpectrumPhy > rxPhy, double lossDb)
update the pathloss value
void SetPhyAttribute(std::string name, const AttributeValue &v)
an address for a packet socket
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
cmd
Definition: second.py:35
virtual Ptr< MobilityModel > GetMobility()=0
Get the associated MobilityModel instance.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:92
mobility
Definition: third.py:108
Class for representing data rates.
Definition: data-rate.h:88
Keep track of the current position and velocity of an object.
void Print()
print the stored pathloss values to standard output
void AddPropagationLoss(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void SetPropagationDelay(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:226
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:918
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
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...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetPhysicalAddress(const Address address)
Set the destination address.
Store the last pathloss value for each TX-RX pair.
keep track of a set of node pointers.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
void SetChannel(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
AttributeValue implementation for DataRate.
Definition: data-rate.h:229
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
A network Node.
Definition: node.h:56
void Print(ComponentCarrier cc)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetLoss(Ptr< MobilityModel > a, Ptr< MobilityModel > b, double loss, bool symmetric=true)
Set loss (in dB, positive) between pair of ns-3 objects (typically, nodes).
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper ...
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
std::map< uint32_t, std::map< uint32_t, double > > m_pathlossMap
Path loss map.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Setup a SpectrumChannel.
devices
Definition: first.py:39
uint32_t pktSize
packet size used for the simulation (in bytes)
Definition: wifi-bianchi.cc:83
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution...
NetDeviceContainer Install(NodeContainer c) const
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
Ptr< SpectrumChannel > Create(void) const
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)