src/devices/wifi/propagation-loss-model-test-suite.cc
changeset 6068 a2127017ecb4
parent 6067 ccbdc2b19ea5
parent 6062 f62b76f5c92a
child 6069 c21754b56036
equal deleted inserted replaced
6067:ccbdc2b19ea5 6068:a2127017ecb4
     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/pcap-file.h"
       
    23 #include "ns3/config.h"
       
    24 #include "ns3/string.h"
       
    25 #include "ns3/uinteger.h"
       
    26 #include "ns3/double.h"
       
    27 #include "ns3/data-rate.h"
       
    28 #include "ns3/inet-socket-address.h"
       
    29 #include "ns3/internet-stack-helper.h"
       
    30 #include "ns3/ipv4-address-helper.h"
       
    31 #include "ns3/tcp-socket-factory.h"
       
    32 #include "ns3/yans-wifi-helper.h"
       
    33 #include "ns3/propagation-loss-model.h"
       
    34 #include "ns3/propagation-delay-model.h"
       
    35 #include "ns3/yans-wifi-channel.h"
       
    36 #include "ns3/yans-wifi-phy.h"
       
    37 #include "ns3/wifi-net-device.h"
       
    38 #include "ns3/mobility-helper.h"
       
    39 #include "ns3/constant-position-mobility-model.h"
       
    40 #include "ns3/nqos-wifi-mac-helper.h"
       
    41 #include "ns3/simulator.h"
       
    42 
       
    43 using namespace ns3;
       
    44 
       
    45 NS_LOG_COMPONENT_DEFINE ("PropagationLossModelsTest");
       
    46 
       
    47 // ===========================================================================
       
    48 // This is a simple test to validate propagation loss models of ns-3 wifi.
       
    49 // See the chapter in the ns-3 testing and validation guide for more detail
       
    50 // ===========================================================================
       
    51 //
       
    52 class FriisPropagationLossModelTestCase : public TestCase
       
    53 {
       
    54 public:
       
    55   FriisPropagationLossModelTestCase ();
       
    56   virtual ~FriisPropagationLossModelTestCase ();
       
    57 
       
    58 private:
       
    59   virtual bool DoRun (void);
       
    60 
       
    61   typedef struct {
       
    62     Vector m_position;
       
    63     double m_pt;  // dBm
       
    64     double m_pr;  // W
       
    65     double m_tolerance;
       
    66   } TestVector;
       
    67 
       
    68   TestVectors<TestVector> m_testVectors;
       
    69 };
       
    70 
       
    71 FriisPropagationLossModelTestCase::FriisPropagationLossModelTestCase ()
       
    72   : TestCase ("Check to see that the ns-3 Friis propagation loss model provides correct received power"), m_testVectors ()
       
    73 {
       
    74 }
       
    75 
       
    76 FriisPropagationLossModelTestCase::~FriisPropagationLossModelTestCase ()
       
    77 {
       
    78 }
       
    79 
       
    80 bool
       
    81 FriisPropagationLossModelTestCase::DoRun (void)
       
    82 {
       
    83   // The ns-3 testing manual gives more background on the values selected
       
    84   // for this test.  First, set a few defaults. 
       
    85 
       
    86   // wavelength at 2.4 GHz is 0.125m
       
    87   Config::SetDefault ("ns3::FriisPropagationLossModel::Lambda", DoubleValue (0.125));
       
    88   Config::SetDefault ("ns3::FriisPropagationLossModel::SystemLoss", DoubleValue (1.0));
       
    89 
       
    90   // Select a reference transmit power
       
    91   // Pt = 10^(17.0206/10)/10^3 = .05035702 W
       
    92   double txPowerW = 0.05035702;
       
    93   double txPowerdBm = 10 * log10 (txPowerW) + 30;
       
    94 
       
    95   //
       
    96   // We want to test the propagation loss model calculations at a few chosen 
       
    97   // distances and compare the results to those we have manually calculated
       
    98   // according to the model documentation.  The model reference specifies, 
       
    99   // for instance, that the received power at 100m according to the provided
       
   100   // input power will be 4.98265e-10 W.  Since this value specifies the power
       
   101   // to 1e-15 significance, we test the ns-3 calculated value for agreement 
       
   102   // within 5e-16.
       
   103   //
       
   104   TestVector testVector;
       
   105 
       
   106   testVector.m_position = Vector (100, 0, 0);
       
   107   testVector.m_pt = txPowerdBm;
       
   108   testVector.m_pr = 4.98265e-10;
       
   109   testVector.m_tolerance = 5e-16;
       
   110   m_testVectors.Add (testVector);
       
   111 
       
   112   testVector.m_position = Vector (500, 0, 0);
       
   113   testVector.m_pt = txPowerdBm;
       
   114   testVector.m_pr = 1.99306e-11;
       
   115   testVector.m_tolerance = 5e-17;
       
   116   m_testVectors.Add (testVector);
       
   117 
       
   118   testVector.m_position = Vector (1000, 0, 0);
       
   119   testVector.m_pt = txPowerdBm;
       
   120   testVector.m_pr = 4.98265e-12;
       
   121   testVector.m_tolerance = 5e-18;
       
   122   m_testVectors.Add (testVector);
       
   123 
       
   124   testVector.m_position = Vector (2000, 0, 0);
       
   125   testVector.m_pt = txPowerdBm;
       
   126   testVector.m_pr = 1.24566e-12;
       
   127   testVector.m_tolerance = 5e-18;
       
   128   m_testVectors.Add (testVector);
       
   129 
       
   130   // Now, check that the received power values are expected
       
   131 
       
   132   Ptr<MobilityModel> a = CreateObject<ConstantPositionMobilityModel> (); 
       
   133   a->SetPosition (Vector (0,0,0));
       
   134   Ptr<MobilityModel> b = CreateObject<ConstantPositionMobilityModel> (); 
       
   135 
       
   136   Ptr<FriisPropagationLossModel> lossModel = CreateObject<FriisPropagationLossModel> (); 
       
   137   for (uint32_t i = 0; i < m_testVectors.GetN (); ++i)
       
   138     {
       
   139       testVector = m_testVectors.Get (i);
       
   140       b->SetPosition (testVector.m_position);
       
   141       double resultdBm = lossModel->CalcRxPower (testVector.m_pt, a, b);
       
   142       double resultW =   pow (10.0, resultdBm/10.0)/1000;
       
   143       NS_TEST_EXPECT_MSG_EQ_TOL (resultW, testVector.m_pr, testVector.m_tolerance, "Got unexpected rcv power");
       
   144     }
       
   145 	
       
   146   return GetErrorStatus ();
       
   147 }
       
   148 
       
   149 class LogDistancePropagationLossModelTestCase : public TestCase
       
   150 {
       
   151 public:
       
   152   LogDistancePropagationLossModelTestCase ();
       
   153   virtual ~LogDistancePropagationLossModelTestCase ();
       
   154 
       
   155 private:
       
   156   virtual bool DoRun (void);
       
   157 
       
   158   typedef struct {
       
   159     Vector m_position;
       
   160     double m_pt;  // dBm
       
   161     double m_pr;  // W
       
   162     double m_tolerance;
       
   163   } TestVector;
       
   164 
       
   165   TestVectors<TestVector> m_testVectors;
       
   166 };
       
   167 
       
   168 LogDistancePropagationLossModelTestCase::LogDistancePropagationLossModelTestCase ()
       
   169   : TestCase ("Check to see that the ns-3 Log Distance propagation loss model provides correct received power"), m_testVectors ()
       
   170 {
       
   171 }
       
   172 
       
   173 LogDistancePropagationLossModelTestCase::~LogDistancePropagationLossModelTestCase ()
       
   174 {
       
   175 }
       
   176 
       
   177 bool
       
   178 LogDistancePropagationLossModelTestCase::DoRun (void)
       
   179 {
       
   180   // reference loss at 2.4 GHz is 40.045997
       
   181   Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.045997));
       
   182   Config::SetDefault ("ns3::LogDistancePropagationLossModel::Exponent", DoubleValue (3));
       
   183 
       
   184   // Select a reference transmit power
       
   185   // Pt = 10^(17.0206/10)/10^3 = .05035702 W
       
   186   double txPowerW = 0.05035702;
       
   187   double txPowerdBm = 10 * log10 (txPowerW) + 30;
       
   188 
       
   189   //
       
   190   // We want to test the propagation loss model calculations at a few chosen 
       
   191   // distances and compare the results to those we have manually calculated
       
   192   // according to the model documentation.  The following "TestVector" objects
       
   193   // will drive the test.
       
   194   //
       
   195   TestVector testVector;
       
   196 
       
   197   testVector.m_position = Vector (10, 0, 0);
       
   198   testVector.m_pt = txPowerdBm;
       
   199   testVector.m_pr = 4.98265e-9;
       
   200   testVector.m_tolerance = 5e-15; 
       
   201   m_testVectors.Add (testVector);
       
   202 
       
   203   testVector.m_position = Vector (20, 0, 0);
       
   204   testVector.m_pt = txPowerdBm;
       
   205   testVector.m_pr = 6.22831e-10;
       
   206   testVector.m_tolerance = 5e-16;
       
   207   m_testVectors.Add (testVector);
       
   208 
       
   209   testVector.m_position = Vector (40, 0, 0);
       
   210   testVector.m_pt = txPowerdBm;
       
   211   testVector.m_pr = 7.78539e-11;
       
   212   testVector.m_tolerance = 5e-17;
       
   213   m_testVectors.Add (testVector);
       
   214 
       
   215   testVector.m_position = Vector (80, 0, 0);
       
   216   testVector.m_pt = txPowerdBm;
       
   217   testVector.m_pr = 9.73173e-12;
       
   218   testVector.m_tolerance = 5e-17;
       
   219   m_testVectors.Add (testVector);
       
   220 
       
   221   Ptr<MobilityModel> a = CreateObject<ConstantPositionMobilityModel> (); 
       
   222   a->SetPosition (Vector (0,0,0));
       
   223   Ptr<MobilityModel> b = CreateObject<ConstantPositionMobilityModel> (); 
       
   224 
       
   225   Ptr<LogDistancePropagationLossModel> lossModel = CreateObject<LogDistancePropagationLossModel> (); 
       
   226   for (uint32_t i = 0; i < m_testVectors.GetN (); ++i)
       
   227     {
       
   228       testVector = m_testVectors.Get (i);
       
   229       b->SetPosition (testVector.m_position);
       
   230       double resultdBm = lossModel->CalcRxPower (testVector.m_pt, a, b);
       
   231       double resultW =   pow (10.0, resultdBm/10.0)/1000;
       
   232       NS_TEST_EXPECT_MSG_EQ_TOL (resultW, testVector.m_pr, testVector.m_tolerance, "Got unexpected rcv power");
       
   233     }
       
   234 	
       
   235   return GetErrorStatus ();
       
   236 }
       
   237 
       
   238 class PropagationLossModelsTestSuite : public TestSuite
       
   239 {
       
   240 public:
       
   241   PropagationLossModelsTestSuite ();
       
   242 };
       
   243 
       
   244 PropagationLossModelsTestSuite::PropagationLossModelsTestSuite ()
       
   245   : TestSuite ("propagation-loss-model", UNIT)
       
   246 {
       
   247   AddTestCase (new FriisPropagationLossModelTestCase);
       
   248   AddTestCase (new LogDistancePropagationLossModelTestCase);
       
   249 }
       
   250 
       
   251 PropagationLossModelsTestSuite WifiPropagationLossModelsTestSuite;