A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
propagation-loss-model-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 The Boeing Company
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 
19 #include "ns3/log.h"
20 #include "ns3/abort.h"
21 #include "ns3/test.h"
22 #include "ns3/config.h"
23 #include "ns3/double.h"
24 #include "ns3/propagation-loss-model.h"
25 #include "ns3/constant-position-mobility-model.h"
26 #include "ns3/simulator.h"
27 
28 using namespace ns3;
29 
30 NS_LOG_COMPONENT_DEFINE ("PropagationLossModelsTest");
31 
32 // ===========================================================================
33 // This is a simple test to validate propagation loss models of ns-3 wifi.
34 // See the chapter in the ns-3 testing and validation guide for more detail
35 // ===========================================================================
36 //
38 {
39 public:
42 
43 private:
44  virtual void DoRun (void);
45 
46  typedef struct {
48  double m_pt; // dBm
49  double m_pr; // W
50  double m_tolerance;
51  } TestVector;
52 
54 };
55 
57  : TestCase ("Check to see that the ns-3 Friis propagation loss model provides correct received power"), m_testVectors ()
58 {
59 }
60 
62 {
63 }
64 
65 void
67 {
68  // The ns-3 testing manual gives more background on the values selected
69  // for this test. First, set a few defaults.
70 
71  // wavelength at 2.4 GHz is 0.125m
72  Config::SetDefault ("ns3::FriisPropagationLossModel::Lambda", DoubleValue (0.125));
73  Config::SetDefault ("ns3::FriisPropagationLossModel::SystemLoss", DoubleValue (1.0));
74 
75  // Select a reference transmit power
76  // Pt = 10^(17.0206/10)/10^3 = .05035702 W
77  double txPowerW = 0.05035702;
78  double txPowerdBm = 10 * std::log10 (txPowerW) + 30;
79 
80  //
81  // We want to test the propagation loss model calculations at a few chosen
82  // distances and compare the results to those we have manually calculated
83  // according to the model documentation. The model reference specifies,
84  // for instance, that the received power at 100m according to the provided
85  // input power will be 4.98265e-10 W. Since this value specifies the power
86  // to 1e-15 significance, we test the ns-3 calculated value for agreement
87  // within 5e-16.
88  //
89  TestVector testVector;
90 
91  testVector.m_position = Vector (100, 0, 0);
92  testVector.m_pt = txPowerdBm;
93  testVector.m_pr = 4.98265e-10;
94  testVector.m_tolerance = 5e-16;
95  m_testVectors.Add (testVector);
96 
97  testVector.m_position = Vector (500, 0, 0);
98  testVector.m_pt = txPowerdBm;
99  testVector.m_pr = 1.99306e-11;
100  testVector.m_tolerance = 5e-17;
101  m_testVectors.Add (testVector);
102 
103  testVector.m_position = Vector (1000, 0, 0);
104  testVector.m_pt = txPowerdBm;
105  testVector.m_pr = 4.98265e-12;
106  testVector.m_tolerance = 5e-18;
107  m_testVectors.Add (testVector);
108 
109  testVector.m_position = Vector (2000, 0, 0);
110  testVector.m_pt = txPowerdBm;
111  testVector.m_pr = 1.24566e-12;
112  testVector.m_tolerance = 5e-18;
113  m_testVectors.Add (testVector);
114 
115  // Now, check that the received power values are expected
116 
117  Ptr<MobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
118  a->SetPosition (Vector (0,0,0));
119  Ptr<MobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
120 
121  Ptr<FriisPropagationLossModel> lossModel = CreateObject<FriisPropagationLossModel> ();
122  for (uint32_t i = 0; i < m_testVectors.GetN (); ++i)
123  {
124  testVector = m_testVectors.Get (i);
125  b->SetPosition (testVector.m_position);
126  double resultdBm = lossModel->CalcRxPower (testVector.m_pt, a, b);
127  double resultW = std::pow (10.0, resultdBm/10.0)/1000;
128  NS_TEST_EXPECT_MSG_EQ_TOL (resultW, testVector.m_pr, testVector.m_tolerance, "Got unexpected rcv power");
129  }
130 }
131 
132 // Added for Two-Ray Ground Model - tomhewer@mac.com
133 
135 {
136 public:
139 
140 private:
141  virtual void DoRun (void);
142 
143  typedef struct
144  {
146  double m_pt; // dBm
147  double m_pr; // W
148  double m_tolerance;
149  } TestVector;
150 
152 };
153 
155  : TestCase ("Check to see that the ns-3 TwoRayGround propagation loss model provides correct received power"),
156  m_testVectors ()
157 {
158 }
159 
161 {
162 }
163 
164 void
166 {
167  // wavelength at 2.4 GHz is 0.125m
168  Config::SetDefault ("ns3::TwoRayGroundPropagationLossModel::Lambda", DoubleValue (0.125));
169  Config::SetDefault ("ns3::TwoRayGroundPropagationLossModel::SystemLoss", DoubleValue (1.0));
170 
171  // set antenna height to 1.5m above z coordinate
172  Config::SetDefault ("ns3::TwoRayGroundPropagationLossModel::HeightAboveZ", DoubleValue (1.5));
173 
174  // Select a reference transmit power of 17.0206 dBm
175  // Pt = 10^(17.0206/10)/10^3 = .05035702 W
176  double txPowerW = 0.05035702;
177  double txPowerdBm = 10 * std::log10 (txPowerW) + 30;
178 
179  //
180  // As with the Friis tests above, we want to test the propagation loss
181  // model calculations at a few chosen distances and compare the results
182  // to those we can manually calculate. Let us test the ns-3 calculated
183  // value for agreement to be within 5e-16, as above.
184  //
185  TestVector testVector;
186 
187  // Below the Crossover distance use Friis so this test should be the same as that above
188  // Crossover = (4 * PI * TxAntennaHeight * RxAntennaHeight) / Lamdba
189  // Crossover = (4 * PI * 1.5 * 1.5) / 0.125 = 226.1946m
190 
191  testVector.m_position = Vector (100, 0, 0);
192  testVector.m_pt = txPowerdBm;
193  testVector.m_pr = 4.98265e-10;
194  testVector.m_tolerance = 5e-16;
195  m_testVectors.Add (testVector);
196 
197  // These values are above the crossover distance and therefore use the Two Ray calculation
198 
199  testVector.m_position = Vector (500, 0, 0);
200  testVector.m_pt = txPowerdBm;
201  testVector.m_pr = 4.07891862e-12;
202  testVector.m_tolerance = 5e-16;
203  m_testVectors.Add (testVector);
204 
205  testVector.m_position = Vector (1000, 0, 0);
206  testVector.m_pt = txPowerdBm;
207  testVector.m_pr = 2.5493241375e-13;
208  testVector.m_tolerance = 5e-16;
209  m_testVectors.Add (testVector);
210 
211  testVector.m_position = Vector (2000, 0, 0);
212  testVector.m_pt = txPowerdBm;
213  testVector.m_pr = 1.593327585938e-14;
214  testVector.m_tolerance = 5e-16;
215  m_testVectors.Add (testVector);
216 
217  // Repeat the tests for non-zero z coordinates
218 
219  // Pr = (0.05035702 * (1.5*1.5) * (2.5*2.5)) / (500*500*500*500) = 1.13303295e-11
220  // dCross = (4 * pi * 1.5 * 2.5) / 0.125 = 376.99m
221  testVector.m_position = Vector (500, 0, 1);
222  testVector.m_pt = txPowerdBm;
223  testVector.m_pr = 1.13303295e-11;
224  testVector.m_tolerance = 5e-16;
225  m_testVectors.Add (testVector);
226 
227  // Pr = (0.05035702 * (1.5*1.5) * (5.5*5.5)) / (1000*1000*1000*1000) = 3.42742467375e-12
228  // dCross = (4 * pi * 1.5 * 5.5) / 0.125 = 829.38m
229  testVector.m_position = Vector (1000, 0, 4);
230  testVector.m_pt = txPowerdBm;
231  testVector.m_pr = 3.42742467375e-12;
232  testVector.m_tolerance = 5e-16;
233  m_testVectors.Add (testVector);
234 
235  // Pr = (0.05035702 * (1.5*1.5) * (11.5*11.5)) / (2000*2000*2000*2000) = 9.36522547734e-13
236  // dCross = (4 * pi * 1.5 * 11.5) / 0.125 = 1734.15m
237  testVector.m_position = Vector (2000, 0, 10);
238  testVector.m_pt = txPowerdBm;
239  testVector.m_pr = 9.36522547734e-13;
240  testVector.m_tolerance = 5e-16;
241  m_testVectors.Add (testVector);
242 
243 
244  // Now, check that the received power values are expected
245 
246  Ptr<MobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
247  a->SetPosition (Vector (0,0,0));
248  Ptr<MobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
249 
250  Ptr<TwoRayGroundPropagationLossModel> lossModel = CreateObject<TwoRayGroundPropagationLossModel> ();
251  for (uint32_t i = 0; i < m_testVectors.GetN (); ++i)
252  {
253  testVector = m_testVectors.Get (i);
254  b->SetPosition (testVector.m_position);
255  double resultdBm = lossModel->CalcRxPower (testVector.m_pt, a, b);
256  double resultW = std::pow (10.0, resultdBm / 10.0) / 1000;
257  NS_TEST_EXPECT_MSG_EQ_TOL (resultW, testVector.m_pr, testVector.m_tolerance, "Got unexpected rcv power");
258  }
259 }
260 
261 
263 {
264 public:
267 
268 private:
269  virtual void DoRun (void);
270 
271  typedef struct {
273  double m_pt; // dBm
274  double m_pr; // W
275  double m_tolerance;
276  } TestVector;
277 
279 };
280 
282  : TestCase ("Check to see that the ns-3 Log Distance propagation loss model provides correct received power"), m_testVectors ()
283 {
284 }
285 
287 {
288 }
289 
290 void
292 {
293  // reference loss at 2.4 GHz is 40.045997
294  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.045997));
295  Config::SetDefault ("ns3::LogDistancePropagationLossModel::Exponent", DoubleValue (3));
296 
297  // Select a reference transmit power
298  // Pt = 10^(17.0206/10)/10^3 = .05035702 W
299  double txPowerW = 0.05035702;
300  double txPowerdBm = 10 * std::log10 (txPowerW) + 30;
301 
302  //
303  // We want to test the propagation loss model calculations at a few chosen
304  // distances and compare the results to those we have manually calculated
305  // according to the model documentation. The following "TestVector" objects
306  // will drive the test.
307  //
308  TestVector testVector;
309 
310  testVector.m_position = Vector (10, 0, 0);
311  testVector.m_pt = txPowerdBm;
312  testVector.m_pr = 4.98265e-9;
313  testVector.m_tolerance = 5e-15;
314  m_testVectors.Add (testVector);
315 
316  testVector.m_position = Vector (20, 0, 0);
317  testVector.m_pt = txPowerdBm;
318  testVector.m_pr = 6.22831e-10;
319  testVector.m_tolerance = 5e-16;
320  m_testVectors.Add (testVector);
321 
322  testVector.m_position = Vector (40, 0, 0);
323  testVector.m_pt = txPowerdBm;
324  testVector.m_pr = 7.78539e-11;
325  testVector.m_tolerance = 5e-17;
326  m_testVectors.Add (testVector);
327 
328  testVector.m_position = Vector (80, 0, 0);
329  testVector.m_pt = txPowerdBm;
330  testVector.m_pr = 9.73173e-12;
331  testVector.m_tolerance = 5e-17;
332  m_testVectors.Add (testVector);
333 
334  Ptr<MobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
335  a->SetPosition (Vector (0,0,0));
336  Ptr<MobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
337 
338  Ptr<LogDistancePropagationLossModel> lossModel = CreateObject<LogDistancePropagationLossModel> ();
339  for (uint32_t i = 0; i < m_testVectors.GetN (); ++i)
340  {
341  testVector = m_testVectors.Get (i);
342  b->SetPosition (testVector.m_position);
343  double resultdBm = lossModel->CalcRxPower (testVector.m_pt, a, b);
344  double resultW = std::pow (10.0, resultdBm/10.0)/1000;
345  NS_TEST_EXPECT_MSG_EQ_TOL (resultW, testVector.m_pr, testVector.m_tolerance, "Got unexpected rcv power");
346  }
347 }
348 
350 {
351 public:
354 
355 private:
356  virtual void DoRun (void);
357 };
358 
360  : TestCase ("Test MatrixPropagationLossModel")
361 {
362 }
363 
365 {
366 }
367 
368 void
370 {
371  Ptr<MobilityModel> m[3];
372  for (int i = 0; i < 3; ++i)
373  {
374  m[i] = CreateObject<ConstantPositionMobilityModel> ();
375  }
376 
378  // no loss by default
379  loss.SetDefaultLoss (0);
380  // -10 dB for 0 -> 1 and 1 -> 0
381  loss.SetLoss (m[0], m[1], 10);
382  // -30 dB from 0 to 2 and -100 dB from 2 to 0
383  loss.SetLoss (m[0], m[2], 30, /*symmetric = */ false);
384  loss.SetLoss (m[2], m[0], 100, /*symmetric = */ false);
385  // default from 1 to 2
386 
387  NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[0], m[1]), -10, "Loss 0 -> 1 incorrect");
388  NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[1], m[0]), -10, "Loss 1 -> 0 incorrect");
389  NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[0], m[2]), -30, "Loss 0 -> 2 incorrect");
390  NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[2], m[0]), -100, "Loss 2 -> 0 incorrect");
391  NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[1], m[2]), 0, "Loss 1 -> 2 incorrect");
392  NS_TEST_ASSERT_MSG_EQ (loss.CalcRxPower (0, m[2], m[1]), 0, "Loss 2 -> 1 incorrect");
393 
394  Simulator::Destroy ();
395 }
396 
398 {
399 public:
402 
403 private:
404  virtual void DoRun (void);
405 };
406 
408  : TestCase ("Test RangePropagationLossModel")
409 {
410 }
411 
413 {
414 }
415 
416 void
418 {
419  Config::SetDefault ("ns3::RangePropagationLossModel::MaxRange", DoubleValue (127.2));
420  Ptr<MobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
421  a->SetPosition (Vector (0,0,0));
422  Ptr<MobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
423  b->SetPosition (Vector (127.1,0,0)); // within range
424 
425  Ptr<RangePropagationLossModel> lossModel = CreateObject<RangePropagationLossModel> ();
426 
427  double txPwrdBm = -80.0;
428  double tolerance = 1e-6;
429  double resultdBm = lossModel->CalcRxPower (txPwrdBm, a, b);
430  NS_TEST_EXPECT_MSG_EQ_TOL (resultdBm, txPwrdBm, tolerance, "Got unexpected rcv power");
431  b->SetPosition (Vector (127.25,0,0)); // beyond range
432  resultdBm = lossModel->CalcRxPower (txPwrdBm, a, b);
433  NS_TEST_EXPECT_MSG_EQ_TOL (resultdBm, -1000.0, tolerance, "Got unexpected rcv power");
434  Simulator::Destroy ();
435 }
436 
438 {
439 public:
441 };
442 
444  : TestSuite ("propagation-loss-model", UNIT)
445 {
451 }
452