A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
spectrum-ideal-phy-test.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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 #include <ns3/object.h>
22 #include <ns3/spectrum-interference.h>
23 #include <ns3/spectrum-error-model.h>
24 #include <ns3/log.h>
25 #include <ns3/test.h>
26 #include <ns3/simulator.h>
27 #include <ns3/packet.h>
28 #include <ns3/ptr.h>
29 #include <ns3/string.h>
30 #include <iostream>
31 
32 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
33 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
34 #include <ns3/wifi-spectrum-value-helper.h>
35 #include <ns3/single-model-spectrum-channel.h>
36 #include <ns3/waveform-generator.h>
37 #include <ns3/spectrum-analyzer.h>
38 #include <ns3/log.h>
39 #include <string>
40 #include <iomanip>
41 #include <ns3/friis-spectrum-propagation-loss.h>
42 #include <ns3/propagation-delay-model.h>
43 #include <ns3/spectrum-helper.h>
44 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
45 #include <ns3/mobility-helper.h>
46 #include <ns3/data-rate.h>
47 #include <ns3/uinteger.h>
48 #include <ns3/packet-socket-helper.h>
49 #include <ns3/packet-socket-address.h>
50 #include <ns3/on-off-helper.h>
51 #include <ns3/config.h>
52 
53 #ifdef __FreeBSD__
54 #define log2(x) (log(x)/M_LN2)
55 #endif
56 
57 NS_LOG_COMPONENT_DEFINE ("SpectrumIdealPhyTest");
58 
59 namespace ns3 {
60 
61 
62 static uint64_t g_rxBytes;
63 static double g_bandwidth = 20e6; // Hz
64 
65 void
66 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
67 {
68  g_rxBytes += p->GetSize ();
69 }
70 
71 
73 {
74 public:
75  SpectrumIdealPhyTestCase (double snrLinear,
76  uint64_t phyRate,
77  bool rateIsAchievable,
78  std::string channelType);
79  virtual ~SpectrumIdealPhyTestCase ();
80 
81 private:
82  virtual void DoRun (void);
83  static std::string Name (std::string channelType, double snrLinear, uint64_t phyRate);
84 
85  double m_snrLinear;
86  uint64_t m_phyRate;
88  std::string m_channelType;
89 };
90 
91 std::string
92 SpectrumIdealPhyTestCase::Name (std::string channelType, double snrLinear, uint64_t phyRate)
93 {
94  std::ostringstream oss;
95  oss << channelType
96  << " snr = " << snrLinear << " (linear), "
97  << " phyRate = " << phyRate << " bps";
98  return oss.str();
99 }
100 
101 
103  uint64_t phyRate,
104  bool rateIsAchievable,
105  std::string channelType)
106  : TestCase (Name (channelType, snrLinear, phyRate)),
107  m_snrLinear (snrLinear),
108  m_phyRate (phyRate),
109  m_rateIsAchievable (rateIsAchievable),
110  m_channelType (channelType)
111 {
112 }
113 
115 {
116 }
117 
118 
119 void
121 {
123  double txPowerW = 0.1;
124  // for the noise, we use the Power Spectral Density of thermal noise
125  // at room temperature. The value of the PSD will be constant over the band of interest.
126  const double k = 1.381e-23; //Boltzmann's constant
127  const double T = 290; // temperature in Kelvin
128  double noisePsdValue = k * T; // W/Hz
129  double lossLinear = (txPowerW) / (m_snrLinear * noisePsdValue * g_bandwidth);
130  double lossDb = 10 * log10 (lossLinear);
131  uint64_t phyRate = m_phyRate; // bps
132  uint32_t pktSize = 50; // bytes
133 
134  uint32_t numPkts = 200; //desired number of packets in the
135  //test. Directly related with the accuracy
136  //of the measurement.
137 
138  double testDuration = (numPkts * pktSize * 8.0) / phyRate;
139  NS_LOG_INFO ("test duration = " << std::fixed << testDuration);
140 
141  NodeContainer c;
142  c.Create (2);
143 
144  MobilityHelper mobility;
145  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
146  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
147  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
148  mobility.SetPositionAllocator (positionAlloc);
149  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
150 
151 
152  mobility.Install (c);
153 
154 
155  SpectrumChannelHelper channelHelper;
156  channelHelper.SetChannel (m_channelType);
157  channelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
158  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
159  propLoss->SetLoss (c.Get(0)->GetObject<MobilityModel> (), c.Get(1)->GetObject<MobilityModel> (), lossDb, true);
160  channelHelper.AddPropagationLoss (propLoss);
161  Ptr<SpectrumChannel> channel = channelHelper.Create ();
162 
163 
165 
166  uint32_t channelNumber = 1;
167  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
168 
169  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
170 
171  AdhocAlohaNoackIdealPhyHelper deviceHelper;
172  deviceHelper.SetChannel (channel);
173  deviceHelper.SetTxPowerSpectralDensity (txPsd);
174  deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
175  deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate (phyRate)));
176  NetDeviceContainer devices = deviceHelper.Install (c);
177 
178  PacketSocketHelper packetSocket;
179  packetSocket.Install (c);
180 
181  PacketSocketAddress socket;
182  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
183  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
184  socket.SetProtocol (1);
185 
186  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
187  onoff.SetConstantRate (DataRate (static_cast<uint64_t> (1.2*phyRate)));
188  onoff.SetAttribute ("PacketSize", UintegerValue (pktSize));
189 
190  ApplicationContainer apps = onoff.Install (c.Get (0));
191  apps.Start (Seconds (0.0));
192  apps.Stop (Seconds (testDuration));
193 
194  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
195 
196  g_rxBytes = 0;
197  Simulator::Stop (Seconds (testDuration+0.000000001));
198  Simulator::Run ();
199  double throughputBps = (g_rxBytes * 8.0) / testDuration;
200 
201  if (m_rateIsAchievable)
202  {
203  NS_TEST_ASSERT_MSG_EQ_TOL (throughputBps, m_phyRate, m_phyRate*0.01, "throughput does not match PHY rate");
204  }
205  else
206  {
207  NS_TEST_ASSERT_MSG_EQ (throughputBps, 0.0, "PHY rate is not achievable but throughput is non-zero");
208  }
209 
211 }
212 
213 
214 
215 
217 {
218 public:
220 };
221 
223  : TestSuite ("spectrum-ideal-phy", SYSTEM)
224 {
225 
226  NS_LOG_INFO ("creating SpectrumIdealPhyTestSuite");
227 
228  for (double snr = 0.01; snr <= 10 ; snr *= 2)
229  {
230  double achievableRate = g_bandwidth*log2(1+snr);
231  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.1), true, "ns3::SingleModelSpectrumChannel"));
232  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.5), true, "ns3::SingleModelSpectrumChannel"));
233  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.95), true, "ns3::SingleModelSpectrumChannel"));
234  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*1.05), false, "ns3::SingleModelSpectrumChannel"));
235  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*2), false, "ns3::SingleModelSpectrumChannel"));
236  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*4), false, "ns3::SingleModelSpectrumChannel"));
237  }
238  for (double snr = 0.01; snr <= 10 ; snr *= 10)
239  {
240  double achievableRate = g_bandwidth*log2(1+snr);
241  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.1), true, "ns3::MultiModelSpectrumChannel"));
242  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.5), true, "ns3::MultiModelSpectrumChannel"));
243  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.95), true, "ns3::MultiModelSpectrumChannel"));
244  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*1.05), false, "ns3::MultiModelSpectrumChannel"));
245  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*2), false, "ns3::MultiModelSpectrumChannel"));
246  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*4), false, "ns3::MultiModelSpectrumChannel"));
247  }
248 }
249 
251 
252 } // namespace ns3