A Discrete-Event Network Simulator
API
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 #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/packet-socket-client.h>
50 #include <ns3/config.h>
51 
52 
53 using namespace ns3;
54 
55 NS_LOG_COMPONENT_DEFINE ("SpectrumIdealPhyTest");
56 
57 static uint64_t g_rxBytes;
58 static double g_bandwidth = 20e6; // Hz
59 
60 void
61 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
62 {
63  g_rxBytes += p->GetSize ();
64 }
65 
66 
68 {
69 public:
70  SpectrumIdealPhyTestCase (double snrLinear,
71  uint64_t phyRate,
72  bool rateIsAchievable,
73  std::string channelType);
74  virtual ~SpectrumIdealPhyTestCase ();
75 
76 private:
77  virtual void DoRun (void);
78  static std::string Name (std::string channelType, double snrLinear, uint64_t phyRate);
79 
80  double m_snrLinear;
81  uint64_t m_phyRate;
83  std::string m_channelType;
84 };
85 
86 std::string
87 SpectrumIdealPhyTestCase::Name (std::string channelType, double snrLinear, uint64_t phyRate)
88 {
89  std::ostringstream oss;
90  oss << channelType
91  << " snr = " << snrLinear << " (linear), "
92  << " phyRate = " << phyRate << " bps";
93  return oss.str();
94 }
95 
96 
98  uint64_t phyRate,
99  bool rateIsAchievable,
100  std::string channelType)
101  : TestCase (Name (channelType, snrLinear, phyRate)),
102  m_snrLinear (snrLinear),
103  m_phyRate (phyRate),
104  m_rateIsAchievable (rateIsAchievable),
105  m_channelType (channelType)
106 {
107 }
108 
110 {
111 }
112 
113 
114 void
116 {
118  double txPowerW = 0.1;
119  // for the noise, we use the Power Spectral Density of thermal noise
120  // at room temperature. The value of the PSD will be constant over the band of interest.
121  const double k = 1.381e-23; //Boltzmann's constant
122  const double T = 290; // temperature in Kelvin
123  double noisePsdValue = k * T; // W/Hz
124  double lossLinear = (txPowerW) / (m_snrLinear * noisePsdValue * g_bandwidth);
125  double lossDb = 10 * std::log10 (lossLinear);
126  uint64_t phyRate = m_phyRate; // bps
127  uint32_t pktSize = 50; // bytes
128 
129  uint32_t numPkts = 200; //desired number of packets in the
130  //test. Directly related with the accuracy
131  //of the measurement.
132 
133  double testDuration = (numPkts * pktSize * 8.0) / phyRate;
134  NS_LOG_INFO ("test duration = " << std::fixed << testDuration);
135 
136  NodeContainer c;
137  c.Create (2);
138 
140  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
141  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
142  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
143  mobility.SetPositionAllocator (positionAlloc);
144  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
145 
146 
147  mobility.Install (c);
148 
149 
150  SpectrumChannelHelper channelHelper;
151  channelHelper.SetChannel (m_channelType);
152  channelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
153  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
154  propLoss->SetLoss (c.Get(0)->GetObject<MobilityModel> (), c.Get(1)->GetObject<MobilityModel> (), lossDb, true);
155  channelHelper.AddPropagationLoss (propLoss);
156  Ptr<SpectrumChannel> channel = channelHelper.Create ();
157 
158 
160 
161  uint32_t channelNumber = 1;
162  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
163 
164  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
165 
166  AdhocAlohaNoackIdealPhyHelper deviceHelper;
167  deviceHelper.SetChannel (channel);
168  deviceHelper.SetTxPowerSpectralDensity (txPsd);
169  deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
170  deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate (phyRate)));
171  NetDeviceContainer devices = deviceHelper.Install (c);
172 
173  PacketSocketHelper packetSocket;
174  packetSocket.Install (c);
175 
176  PacketSocketAddress socket;
177  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
178  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
179  socket.SetProtocol (1);
180 
181  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient> ();
182  client->SetRemote (socket);
183  client->SetAttribute ("Interval", TimeValue (Seconds (double (pktSize*8) / (1.2*double (phyRate)))));
184  client->SetAttribute ("PacketSize", UintegerValue (pktSize));
185  client->SetAttribute ("MaxPackets", UintegerValue (0));
186  client->SetStartTime(Seconds (0.0));
187  client->SetStopTime(Seconds (testDuration));
188  c.Get (0)->AddApplication (client);
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  std::clog.unsetf(std::ios_base::floatfield);
198 
199  if (m_rateIsAchievable)
200  {
201  NS_TEST_ASSERT_MSG_EQ_TOL (throughputBps, m_phyRate, m_phyRate*0.01, "throughput does not match PHY rate");
202  }
203  else
204  {
205  NS_TEST_ASSERT_MSG_EQ (throughputBps, 0.0, "PHY rate is not achievable but throughput is non-zero");
206  }
207 
208  Simulator::Destroy ();
209 }
210 
211 
212 
213 
215 {
216 public:
218 };
219 
221  : TestSuite ("spectrum-ideal-phy", SYSTEM)
222 {
223 
224  NS_LOG_INFO ("creating SpectrumIdealPhyTestSuite");
225 
226  for (double snr = 0.01; snr <= 10 ; snr *= 2)
227  {
228  double achievableRate = g_bandwidth*log2(1+snr);
229  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.1), true, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
230  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.5), true, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
231  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.95), true, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
232  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*1.05), false, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
233  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*2), false, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
234  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*4), false, "ns3::SingleModelSpectrumChannel"), TestCase::QUICK);
235  }
236  for (double snr = 0.01; snr <= 10 ; snr *= 10)
237  {
238  double achievableRate = g_bandwidth*log2(1+snr);
239  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.1), true, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
240  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.5), true, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
241  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*0.95), true, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
242  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*1.05), false, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
243  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*2), false, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
244  AddTestCase (new SpectrumIdealPhyTestCase (snr, static_cast<uint64_t> (achievableRate*4), false, "ns3::MultiModelSpectrumChannel"), TestCase::QUICK);
245  }
246 }
247 
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:75
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
SpectrumIdealPhyTestCase(double snrLinear, uint64_t phyRate, bool rateIsAchievable, std::string channelType)
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 PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
A suite of tests to run.
Definition: test.h:1342
void SetPhyAttribute(std::string name, const AttributeValue &v)
an address for a packet socket
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:280
encapsulates test code
Definition: test.h:1155
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
static double g_bandwidth
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.
static std::string Name(std::string str, uint32_t totalStreamSize, uint32_t sourceWriteSize, uint32_t serverReadSize, uint32_t serverWriteSize, uint32_t sourceReadSize, bool useIpv6)
Definition: tcp-test.cc:166
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())
AttributeValue implementation for Time.
Definition: nstime.h:1124
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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())
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:380
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:871
static SpectrumIdealPhyTestSuite g_spectrumIdealPhyTestSuite
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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.
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())
Helper class used to assign positions and mobility models to nodes.
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
static std::string Name(std::string channelType, double snrLinear, uint64_t phyRate)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
void SetProtocol(uint16_t protocol)
Set the protocol.
void Add(Vector v)
Add a position to the list of positions.
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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Setup a SpectrumChannel.
static uint64_t g_rxBytes
virtual void DoRun(void)
Implementation to actually run this TestCase.
devices
Definition: first.py:32
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution...
NetDeviceContainer Install(NodeContainer c) const
void SetStartTime(Time start)
Specify application start time.
Definition: application.cc:69
Ptr< SpectrumChannel > Create(void) const
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)