A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
spectrum-ideal-phy-test.cc
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 #include <ns3/math.h>
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 <string>
39 #include <iomanip>
40 #include <ns3/friis-spectrum-propagation-loss.h>
41 #include <ns3/propagation-delay-model.h>
42 #include <ns3/spectrum-helper.h>
43 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
44 #include <ns3/mobility-helper.h>
45 #include <ns3/data-rate.h>
46 #include <ns3/uinteger.h>
47 #include <ns3/packet-socket-helper.h>
48 #include <ns3/packet-socket-address.h>
49 #include <ns3/on-off-helper.h>
50 #include <ns3/config.h>
51 
52 
53 NS_LOG_COMPONENT_DEFINE ("SpectrumIdealPhyTest");
54 
55 namespace ns3 {
56 
57 
58 static uint64_t g_rxBytes;
59 static double g_bandwidth = 20e6; // Hz
60 
61 void
62 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
63 {
64  g_rxBytes += p->GetSize ();
65 }
66 
67 
69 {
70 public:
71  SpectrumIdealPhyTestCase (double snrLinear,
72  uint64_t phyRate,
73  bool rateIsAchievable,
74  std::string channelType);
75  virtual ~SpectrumIdealPhyTestCase ();
76 
77 private:
78  virtual void DoRun (void);
79  static std::string Name (std::string channelType, double snrLinear, uint64_t phyRate);
80 
81  double m_snrLinear;
82  uint64_t m_phyRate;
83  bool m_rateIsAchievable;
84  std::string m_channelType;
85 };
86 
87 std::string
88 SpectrumIdealPhyTestCase::Name (std::string channelType, double snrLinear, uint64_t phyRate)
89 {
90  std::ostringstream oss;
91  oss << channelType
92  << " snr = " << snrLinear << " (linear), "
93  << " phyRate = " << phyRate << " bps";
94  return oss.str();
95 }
96 
97 
98 SpectrumIdealPhyTestCase::SpectrumIdealPhyTestCase (double snrLinear,
99  uint64_t phyRate,
100  bool rateIsAchievable,
101  std::string channelType)
102  : TestCase (Name (channelType, snrLinear, phyRate)),
103  m_snrLinear (snrLinear),
104  m_phyRate (phyRate),
105  m_rateIsAchievable (rateIsAchievable),
106  m_channelType (channelType)
107 {
108 }
109 
110 SpectrumIdealPhyTestCase::~SpectrumIdealPhyTestCase ()
111 {
112 }
113 
114 
115 void
117 {
118  NS_LOG_FUNCTION (m_snrLinear << m_phyRate);
119  double txPowerW = 0.1;
120  // for the noise, we use the Power Spectral Density of thermal noise
121  // at room temperature. The value of the PSD will be constant over the band of interest.
122  const double k = 1.381e-23; //Boltzmann's constant
123  const double T = 290; // temperature in Kelvin
124  double noisePsdValue = k * T; // W/Hz
125  double lossLinear = (txPowerW) / (m_snrLinear * noisePsdValue * g_bandwidth);
126  double lossDb = 10 * std::log10 (lossLinear);
127  uint64_t phyRate = m_phyRate; // bps
128  uint32_t pktSize = 50; // bytes
129 
130  uint32_t numPkts = 200; //desired number of packets in the
131  //test. Directly related with the accuracy
132  //of the measurement.
133 
134  double testDuration = (numPkts * pktSize * 8.0) / phyRate;
135  NS_LOG_INFO ("test duration = " << std::fixed << testDuration);
136 
137  NodeContainer c;
138  c.Create (2);
139 
140  MobilityHelper mobility;
141  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
142  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
143  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
144  mobility.SetPositionAllocator (positionAlloc);
145  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
146 
147 
148  mobility.Install (c);
149 
150 
151  SpectrumChannelHelper channelHelper;
152  channelHelper.SetChannel (m_channelType);
153  channelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
154  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
155  propLoss->SetLoss (c.Get(0)->GetObject<MobilityModel> (), c.Get(1)->GetObject<MobilityModel> (), lossDb, true);
156  channelHelper.AddPropagationLoss (propLoss);
157  Ptr<SpectrumChannel> channel = channelHelper.Create ();
158 
159 
161 
162  uint32_t channelNumber = 1;
163  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
164 
165  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
166 
167  AdhocAlohaNoackIdealPhyHelper deviceHelper;
168  deviceHelper.SetChannel (channel);
169  deviceHelper.SetTxPowerSpectralDensity (txPsd);
170  deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
171  deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate (phyRate)));
172  NetDeviceContainer devices = deviceHelper.Install (c);
173 
174  PacketSocketHelper packetSocket;
175  packetSocket.Install (c);
176 
177  PacketSocketAddress socket;
178  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
179  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
180  socket.SetProtocol (1);
181 
182  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
183  onoff.SetConstantRate (DataRate (static_cast<uint64_t> (1.2*phyRate)));
184  onoff.SetAttribute ("PacketSize", UintegerValue (pktSize));
185 
186  ApplicationContainer apps = onoff.Install (c.Get (0));
187  apps.Start (Seconds (0.0));
188  apps.Stop (Seconds (testDuration));
189 
190  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
191 
192  g_rxBytes = 0;
193  Simulator::Stop (Seconds (testDuration+0.000000001));
194  Simulator::Run ();
195  double throughputBps = (g_rxBytes * 8.0) / testDuration;
196 
197  if (m_rateIsAchievable)
198  {
199  NS_TEST_ASSERT_MSG_EQ_TOL (throughputBps, m_phyRate, m_phyRate*0.01, "throughput does not match PHY rate");
200  }
201  else
202  {
203  NS_TEST_ASSERT_MSG_EQ (throughputBps, 0.0, "PHY rate is not achievable but throughput is non-zero");
204  }
205 
207 }
208 
209 
210 
211 
213 {
214 public:
216 };
217 
218 SpectrumIdealPhyTestSuite::SpectrumIdealPhyTestSuite ()
219  : TestSuite ("spectrum-ideal-phy", SYSTEM)
220 {
221 
222  NS_LOG_INFO ("creating SpectrumIdealPhyTestSuite");
223 
224  for (double snr = 0.01; snr <= 10 ; snr *= 2)
225  {
226  double achievableRate = g_bandwidth*log2(1+snr);
227  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.1), true, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
228  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.5), true, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
229  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.95), true, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
230  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*1.05), false, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
231  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*2), false, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
232  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*4), false, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
233  }
234  for (double snr = 0.01; snr <= 10 ; snr *= 10)
235  {
236  double achievableRate = g_bandwidth*log2(1+snr);
237  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.1), true, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
238  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.5), true, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
239  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.95), true, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
240  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*1.05), false, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
241  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*2), false, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
242  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*4), false, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
243  }
244 }
245 
246 static SpectrumIdealPhyTestSuite g_spectrumIdealPhyTestSuite;
247 
248 } // namespace ns3
holds a vector of ns3::Application pointers.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
A suite of tests to run.
Definition: test.h:962
void SetPhyAttribute(std::string name, const AttributeValue &v)
an address for a packet socket
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
Ptr< SpectrumChannel > Create(void) const
#define NS_LOG_INFO(msg)
Definition: log.h:264
encapsulates test code
Definition: test.h:834
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:41
a 3d vector
Definition: vector.h:31
Give ns3::PacketSocket powers to ns3::Node.
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
Keep track of the current position and velocity of an object.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
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:46
holds a vector of ns3::NetDevice pointers
virtual void DoRun(void)
Implementation to actually run this test case.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
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...
static void Destroy(void)
Definition: simulator.cc:121
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
keep track of a set of node pointers.
void SetMobilityModel(std::string type, 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(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
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 Install(Ptr< Node > node) const
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
hold objects of type ns3::DataRate
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void SetChannel(Ptr< SpectrumChannel > channel)
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
NetDeviceContainer Install(NodeContainer c) const
Ptr< T > GetObject(void) const
Definition: object.h:332
ApplicationContainer Install(NodeContainer c) const
void SetAttribute(std::string name, const AttributeValue &value)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)