A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #ifdef __FreeBSD__
44 #define log2(x) (log (x)/M_LN2)
45 #endif
46 
47 NS_LOG_COMPONENT_DEFINE ("TestAdhocOfdmAloha");
48 
49 using namespace ns3;
50 
51 static bool g_verbose = false;
52 static uint64_t g_rxBytes;
53 
54 void
55 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
56 {
57  if (g_verbose)
58  {
59  std::cout << context << " PHY RX END OK p:" << p << std::endl;
60  }
61  g_rxBytes += p->GetSize ();
62 }
63 
64 
65 
73 {
74 public:
75 
84  void UpdatePathloss (std::string context, Ptr<SpectrumPhy> txPhy, Ptr<SpectrumPhy> rxPhy, double lossDb);
85 
90  void Print ();
91 
92 private:
93  std::map<uint32_t, std::map<uint32_t, double> > m_pathlossMap;
94 };
95 
96 void
98  Ptr<SpectrumPhy> txPhy,
99  Ptr<SpectrumPhy> rxPhy,
100  double lossDb)
101 {
102  uint32_t txNodeId = txPhy->GetMobility ()->GetObject<Node> ()->GetId ();
103  uint32_t rxNodeId = rxPhy->GetMobility ()->GetObject<Node> ()->GetId ();
104  m_pathlossMap[txNodeId][rxNodeId] = lossDb;
105 }
106 
107 void
109 {
110  for (std::map<uint32_t, std::map<uint32_t, double> >::const_iterator txit = m_pathlossMap.begin ();
111  txit != m_pathlossMap.end ();
112  ++txit)
113  {
114  for (std::map<uint32_t, double>::const_iterator rxit = txit->second.begin ();
115  rxit != txit->second.end ();
116  ++rxit)
117  {
118  std::cout << txit->first << " --> " << rxit->first << " : " << rxit->second << " dB" << std::endl;
119  }
120  }
121 }
122 
123 
124 
125 int main (int argc, char** argv)
126 {
127  CommandLine cmd;
128  double lossDb = 150;
129  double txPowerW = 0.1;
130  uint64_t phyRate = 500000;
131  uint32_t pktSize = 1000;
132  double simDuration = 0.5;
133  std::string channelType ("ns3::SingleModelSpectrumChannel");
134  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
135  cmd.AddValue ("lossDb", "link loss in dB", lossDb);
136  cmd.AddValue ("txPowerW", "txPower in Watts", txPowerW);
137  cmd.AddValue ("phyRate", "PHY rate in bps", phyRate);
138  cmd.AddValue ("pktSize", "packet size in bytes", pktSize);
139  cmd.AddValue ("simDuration", "duration of the simulation in seconds", simDuration);
140  cmd.AddValue ("channelType", "which SpectrumChannel implementation to be used", channelType);
141  cmd.Parse (argc, argv);
142 
143  NodeContainer c;
144  c.Create (2);
145 
146  MobilityHelper mobility;
147  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
148  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
149  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
150  mobility.SetPositionAllocator (positionAlloc);
151  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
152 
153 
154  mobility.Install (c);
155 
156 
157  SpectrumChannelHelper channelHelper;
158  channelHelper.SetChannel (channelType);
159  channelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
160  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
161  propLoss->SetLoss (c.Get (0)->GetObject<MobilityModel> (), c.Get (1)->GetObject<MobilityModel> (), lossDb, true);
162  channelHelper.AddPropagationLoss (propLoss);
163  Ptr<SpectrumChannel> channel = channelHelper.Create ();
164 
165 
167 
168  uint32_t channelNumber = 1;
169  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
170 
171  // for the noise, we use the Power Spectral Density of thermal noise
172  // at room temperature. The value of the PSD will be constant over the band of interest.
173  const double k = 1.381e-23; //Boltzmann's constant
174  const double T = 290; // temperature in Kelvin
175  double noisePsdValue = k * T; // watts per hertz
176  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
177 
178  AdhocAlohaNoackIdealPhyHelper deviceHelper;
179  deviceHelper.SetChannel (channel);
180  deviceHelper.SetTxPowerSpectralDensity (txPsd);
181  deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
182  deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate (phyRate)));
183  NetDeviceContainer devices = deviceHelper.Install (c);
184 
185  PacketSocketHelper packetSocket;
186  packetSocket.Install (c);
187 
188  PacketSocketAddress socket;
189  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
190  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
191  socket.SetProtocol (1);
192 
193  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
194  onoff.SetConstantRate (DataRate (2*phyRate));
195  onoff.SetAttribute ("PacketSize", UintegerValue (pktSize));
196 
197  ApplicationContainer apps = onoff.Install (c.Get (0));
198  apps.Start (Seconds (0.0));
199  apps.Stop (Seconds (simDuration));
200 
201  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
202 
203  GlobalPathlossDatabase globalPathlossDatabase;
204  Config::Connect ("/ChannelList/*/PropagationLoss",
205  MakeCallback (&GlobalPathlossDatabase::UpdatePathloss, &globalPathlossDatabase));
206 
207  g_rxBytes = 0;
208  Simulator::Stop (Seconds (simDuration + 0.000001));
209  Simulator::Run ();
210 
211  if (g_verbose)
212  {
213  globalPathlossDatabase.Print ();
214 
215  double throughputBps = (g_rxBytes * 8.0) / simDuration;
216  std::cout << "throughput: " << throughputBps << std::endl;
217  std::cout << "throughput: " << std::setw (20) << std::fixed << throughputBps << " bps" << std::endl;
218  std::cout << "phy rate : " << std::setw (20) << std::fixed << phyRate*1.0 << " bps" << std::endl;
219  double rxPowerW = txPowerW / (pow (10.0, lossDb/10.0));
220  double capacity = 20e6*log2 (1.0 + (rxPowerW/20.0e6)/noisePsdValue);
221  std::cout << "shannon capacity: " << std::setw (20) << std::fixed << capacity << " bps" << std::endl;
222 
223  }
224 
225 
226 
228  return 0;
229 }