A Discrete-Event Network Simulator
API
spectrum-ideal-phy-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Nicola Baldo <nbaldo@cttc.es>
18  */
19 
20 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
21 #include <ns3/config.h>
22 #include <ns3/data-rate.h>
23 #include <ns3/friis-spectrum-propagation-loss.h>
24 #include <ns3/ism-spectrum-value-helper.h>
25 #include <ns3/log.h>
26 #include <ns3/math.h>
27 #include <ns3/mobility-helper.h>
28 #include <ns3/object.h>
29 #include <ns3/packet-socket-address.h>
30 #include <ns3/packet-socket-client.h>
31 #include <ns3/packet-socket-helper.h>
32 #include <ns3/packet.h>
33 #include <ns3/propagation-delay-model.h>
34 #include <ns3/ptr.h>
35 #include <ns3/simulator.h>
36 #include <ns3/single-model-spectrum-channel.h>
37 #include <ns3/spectrum-analyzer.h>
38 #include <ns3/spectrum-error-model.h>
39 #include <ns3/spectrum-helper.h>
40 #include <ns3/spectrum-interference.h>
41 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
42 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
43 #include <ns3/string.h>
44 #include <ns3/test.h>
45 #include <ns3/uinteger.h>
46 #include <ns3/waveform-generator.h>
47 
48 #include <iomanip>
49 #include <iostream>
50 #include <string>
51 
52 using namespace ns3;
53 
54 NS_LOG_COMPONENT_DEFINE("SpectrumIdealPhyTest");
55 
56 static uint64_t g_rxBytes;
57 static double g_bandwidth = 20e6; // Hz
58 
59 void
60 PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
61 {
62  g_rxBytes += p->GetSize();
63 }
64 
71 {
72  public:
80  SpectrumIdealPhyTestCase(double snrLinear,
81  uint64_t phyRate,
82  bool rateIsAchievable,
83  std::string channelType);
84  ~SpectrumIdealPhyTestCase() override;
85 
86  private:
87  void DoRun() override;
95  static std::string Name(std::string channelType, double snrLinear, uint64_t phyRate);
96 
97  double m_snrLinear;
98  uint64_t m_phyRate;
100  std::string m_channelType;
101 };
102 
103 std::string
104 SpectrumIdealPhyTestCase::Name(std::string channelType, double snrLinear, uint64_t phyRate)
105 {
106  std::ostringstream oss;
107  oss << channelType << " snr = " << snrLinear << " (linear), "
108  << " phyRate = " << phyRate << " bps";
109  return oss.str();
110 }
111 
113  uint64_t phyRate,
114  bool rateIsAchievable,
115  std::string channelType)
116  : TestCase(Name(channelType, snrLinear, phyRate)),
117  m_snrLinear(snrLinear),
118  m_phyRate(phyRate),
119  m_rateIsAchievable(rateIsAchievable),
120  m_channelType(channelType)
121 {
122 }
123 
125 {
126 }
127 
128 void
130 {
132  double txPowerW = 0.1;
133  // for the noise, we use the Power Spectral Density of thermal noise
134  // at room temperature. The value of the PSD will be constant over the band of interest.
135  const double k = 1.381e-23; // Boltzmann's constant
136  const double T = 290; // temperature in Kelvin
137  double noisePsdValue = k * T; // W/Hz
138  double lossLinear = (txPowerW) / (m_snrLinear * noisePsdValue * g_bandwidth);
139  double lossDb = 10 * std::log10(lossLinear);
140  uint64_t phyRate = m_phyRate; // bps
141  uint32_t pktSize = 50; // bytes
142 
143  uint32_t numPkts = 200; // desired number of packets in the
144  // test. Directly related with the accuracy
145  // of the measurement.
146 
147  double testDuration = (numPkts * pktSize * 8.0) / phyRate;
148  NS_LOG_INFO("test duration = " << std::fixed << testDuration);
149 
150  NodeContainer c;
151  c.Create(2);
152 
154  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
155  positionAlloc->Add(Vector(0.0, 0.0, 0.0));
156  positionAlloc->Add(Vector(5.0, 0.0, 0.0));
157  mobility.SetPositionAllocator(positionAlloc);
158  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
159 
160  mobility.Install(c);
161 
162  SpectrumChannelHelper channelHelper;
163  channelHelper.SetChannel(m_channelType);
164  channelHelper.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
165  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel>();
166  propLoss->SetLoss(c.Get(0)->GetObject<MobilityModel>(),
167  c.Get(1)->GetObject<MobilityModel>(),
168  lossDb,
169  true);
170  channelHelper.AddPropagationLoss(propLoss);
171  Ptr<SpectrumChannel> channel = channelHelper.Create();
172 
174 
175  uint32_t channelNumber = 1;
176  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPowerW, channelNumber);
177 
178  Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
179 
180  AdhocAlohaNoackIdealPhyHelper deviceHelper;
181  deviceHelper.SetChannel(channel);
182  deviceHelper.SetTxPowerSpectralDensity(txPsd);
183  deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
184  deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate(phyRate)));
185  NetDeviceContainer devices = deviceHelper.Install(c);
186 
187  PacketSocketHelper packetSocket;
188  packetSocket.Install(c);
189 
190  PacketSocketAddress socket;
191  socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
192  socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
193  socket.SetProtocol(1);
194 
195  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient>();
196  client->SetRemote(socket);
197  client->SetAttribute("Interval",
198  TimeValue(Seconds(double(pktSize * 8) / (1.2 * double(phyRate)))));
199  client->SetAttribute("PacketSize", UintegerValue(pktSize));
200  client->SetAttribute("MaxPackets", UintegerValue(0));
201  client->SetStartTime(Seconds(0.0));
202  client->SetStopTime(Seconds(testDuration));
203  c.Get(0)->AddApplication(client);
204 
205  Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
206 
207  g_rxBytes = 0;
208  Simulator::Stop(Seconds(testDuration + 0.000000001));
209  Simulator::Run();
210  double throughputBps = (g_rxBytes * 8.0) / testDuration;
211 
212  std::clog.unsetf(std::ios_base::floatfield);
213 
214  if (m_rateIsAchievable)
215  {
216  NS_TEST_ASSERT_MSG_EQ_TOL(throughputBps,
217  m_phyRate,
218  m_phyRate * 0.01,
219  "throughput does not match PHY rate");
220  }
221  else
222  {
223  NS_TEST_ASSERT_MSG_EQ(throughputBps,
224  0.0,
225  "PHY rate is not achievable but throughput is non-zero");
226  }
227 
228  Simulator::Destroy();
229 }
230 
237 {
238  public:
240 };
241 
243  : TestSuite("spectrum-ideal-phy", SYSTEM)
244 {
245  NS_LOG_INFO("creating SpectrumIdealPhyTestSuite");
246 
247  for (double snr = 0.01; snr <= 10; snr *= 2)
248  {
249  double achievableRate = g_bandwidth * log2(1 + snr);
251  static_cast<uint64_t>(achievableRate * 0.1),
252  true,
253  "ns3::SingleModelSpectrumChannel"),
254  TestCase::QUICK);
256  static_cast<uint64_t>(achievableRate * 0.5),
257  true,
258  "ns3::SingleModelSpectrumChannel"),
259  TestCase::QUICK);
261  static_cast<uint64_t>(achievableRate * 0.95),
262  true,
263  "ns3::SingleModelSpectrumChannel"),
264  TestCase::QUICK);
266  static_cast<uint64_t>(achievableRate * 1.05),
267  false,
268  "ns3::SingleModelSpectrumChannel"),
269  TestCase::QUICK);
271  static_cast<uint64_t>(achievableRate * 2),
272  false,
273  "ns3::SingleModelSpectrumChannel"),
274  TestCase::QUICK);
276  static_cast<uint64_t>(achievableRate * 4),
277  false,
278  "ns3::SingleModelSpectrumChannel"),
279  TestCase::QUICK);
280  }
281  for (double snr = 0.01; snr <= 10; snr *= 10)
282  {
283  double achievableRate = g_bandwidth * log2(1 + snr);
285  static_cast<uint64_t>(achievableRate * 0.1),
286  true,
287  "ns3::MultiModelSpectrumChannel"),
288  TestCase::QUICK);
290  static_cast<uint64_t>(achievableRate * 0.5),
291  true,
292  "ns3::MultiModelSpectrumChannel"),
293  TestCase::QUICK);
295  static_cast<uint64_t>(achievableRate * 0.95),
296  true,
297  "ns3::MultiModelSpectrumChannel"),
298  TestCase::QUICK);
300  static_cast<uint64_t>(achievableRate * 1.05),
301  false,
302  "ns3::MultiModelSpectrumChannel"),
303  TestCase::QUICK);
305  static_cast<uint64_t>(achievableRate * 2),
306  false,
307  "ns3::MultiModelSpectrumChannel"),
308  TestCase::QUICK);
310  static_cast<uint64_t>(achievableRate * 4),
311  false,
312  "ns3::MultiModelSpectrumChannel"),
313  TestCase::QUICK);
314  }
315 }
316 
Ideal Spectrum PHY Test.
static std::string Name(std::string channelType, double snrLinear, uint64_t phyRate)
Get the test name.
bool m_rateIsAchievable
Check if the rate is achievable.
uint64_t m_phyRate
PHY rate (bps)
void DoRun() override
Implementation to actually run this TestCase.
std::string m_channelType
Channel type.
SpectrumIdealPhyTestCase(double snrLinear, uint64_t phyRate, bool rateIsAchievable, std::string channelType)
Constructor.
Ideal Spectrum PHY TestSuite.
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
void AddPropagationLoss(std::string name, Ts &&... args)
void SetPropagationDelay(std::string name, Ts &&... args)
void SetChannel(std::string type, Ts &&... args)
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
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...
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Hold an unsigned integer type.
Definition: uinteger.h:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
#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:144
#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:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
channel
Definition: third.py:88
mobility
Definition: third.py:105
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
static SpectrumIdealPhyTestSuite g_spectrumIdealPhyTestSuite
Static variable for test initialization.
static uint64_t g_rxBytes
static double g_bandwidth
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
uint32_t pktSize
packet size used for the simulation (in bytes)