1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (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: Marco Miozzo <marco.miozzo@cttc.es>
19  */
20 
21 #include "ns3/simulator.h"
22 #include "ns3/log.h"
23 #include "ns3/spectrum-test.h"
24 #include "ns3/lte-phy-tag.h"
25 #include "ns3/lte-chunk-processor.h"
26 #include <ns3/hybrid-buildings-propagation-loss-model.h>
27 #include <ns3/node-container.h>
28 #include <ns3/mobility-helper.h>
29 #include <ns3/buildings-helper.h>
30 #include <ns3/lte-helper.h>
31 #include <ns3/single-model-spectrum-channel.h>
32 #include "ns3/string.h"
33 #include "ns3/double.h"
34 #include <ns3/boolean.h>
35 #include <ns3/building.h>
36 #include <ns3/enum.h>
37 #include <ns3/net-device-container.h>
38 #include <ns3/lte-ue-net-device.h>
39 #include <ns3/lte-enb-net-device.h>
40 #include <ns3/lte-ue-rrc.h>
41 #include <ns3/lte-enb-phy.h>
42 #include <ns3/lte-ue-phy.h>
43 #include "lte-test-ue-phy.h"
44 #include "lte-test-pathloss-model.h"
45 
46 using namespace ns3;
47 
48 NS_LOG_COMPONENT_DEFINE ("LtePathlossModelTest");
49 
50 /**
51  * Test 1.1 Pathloss compound test
52  */
53 
54 /**
55  * This TestSuite tests the BuildingPathlossModel by reproducing
56  * several communication scenarios
57  */
58 
59 
60 void
LteTestPathlossDlSchedCallback(LtePathlossModelSystemTestCase * testcase,std::string path,DlSchedulingCallbackInfo dlInfo)61 LteTestPathlossDlSchedCallback (LtePathlossModelSystemTestCase *testcase, std::string path,
62 		                        DlSchedulingCallbackInfo dlInfo)
63 {
64   testcase->DlScheduling (dlInfo);
65 }
66 
67 
68 
LtePathlossModelTestSuite()69 LtePathlossModelTestSuite::LtePathlossModelTestSuite ()
70   : TestSuite ("lte-pathloss-model", SYSTEM)
71 {
72   // LogLevel logLevel = (LogLevel)(LOG_PREFIX_FUNC | LOG_PREFIX_TIME | LOG_LEVEL_ALL);
73   // LogComponentEnable ("LteHelper", logLevel);
74   // LogComponentEnable ("LtePathlossModelTest", logLevel);
75   // LogComponentEnable ("BuildingsPropagationLossModel", logLevel);
76   // LogComponentEnable ("LteInterference", logLevel);
77   // LogComponentEnable ("LteSpectrumValueHelper", logLevel);
78   // LogComponentEnable ("LteEnbNetDevice", logLevel);
79 
80   struct SnrEfficiencyMcs
81   {
82     double  snrDb;
83     double  efficiency;
84     int  mcsIndex;
85   };
86 
87   /**
88   * Test vectors: SNRDB, Spectral Efficiency, MCS index
89   * From XXX
90   */
91   SnrEfficiencyMcs snrEfficiencyMcs[] = {
92     { -5.00000,  0.08024,        -1},
93     { -4.00000,  0.10030,        -1},
94     { -3.00000,  0.12518,        -1},
95     { -2.00000,  0.15589,        0},
96     { -1.00000,  0.19365,        0},
97     { 0.00000,   0.23983,        2},
98     { 1.00000,   0.29593,        2},
99     { 2.00000,   0.36360,        2},
100     { 3.00000,   0.44451,        4},
101     { 4.00000,   0.54031,        4},
102     { 5.00000,   0.65251,        6},
103     { 6.00000,   0.78240,        6},
104     { 7.00000,   0.93086,        8},
105     { 8.00000,   1.09835,        8},
106     { 9.00000,   1.28485,        10},
107     { 10.00000,  1.48981,        12},
108     { 11.00000,  1.71229,        12},
109     { 12.00000,  1.95096,        14},
110     { 13.00000,  2.20429,        14},
111     { 14.00000,  2.47062,        16},
112     { 15.00000,  2.74826,        18},
113     { 16.00000,  3.03560,        18},
114     { 17.00000,  3.33115,        20},
115     { 18.00000,  3.63355,        20},
116     { 19.00000,  3.94163,        22},
117     { 20.00000,  4.25439,        22},
118     { 21.00000,  4.57095,        24},
119     { 22.00000,  4.89060,        24},
120     { 23.00000,  5.21276,        26},
121     { 24.00000,  5.53693,        26},
122     { 25.00000,  5.86271,        28},
123     { 26.00000,  6.18980,        28},
124     { 27.00000,  6.51792,        28},
125     { 28.00000,  6.84687,        28},
126     { 29.00000,  7.17649,        28},
127     { 30.00000,  7.50663,        28},
128   };
129 
130 
131   double txPowerDbm = 30; // default eNB TX power over whole bandwidth
132   double txPowerLin = std::pow (10, (txPowerDbm - 30)/10);
133   double ktDbm = -174;    // reference LTE noise PSD
134   double noisePowerDbm = ktDbm + 10 * std::log10 (25 * 180000); // corresponds to kT*bandwidth in linear units
135   double receiverNoiseFigureDb = 9.0; // default UE noise figure
136   double noiseLin = std::pow (10, (noisePowerDbm-30+receiverNoiseFigureDb)/10);
137 
138   // reference values obtained with the octave script src/lte/test/reference/lte_pathloss.m
139 
140   double loss[] = {81.062444, 134.078605, 144.259958};
141   double dist[] = {100.0, 500.0, 1500};
142 
143   int numOfTests = sizeof (loss) / sizeof (double);
144   for ( int i = 0 ; i < numOfTests; i++ )
145   {
146     //     double lossDb = txPowerDbm - snrEfficiencyMcs[i].snrDb - noisePowerDbm - receiverNoiseFigureDb;
147     double sinrLin = (txPowerLin/(pow(10, loss[i]/10))) / noiseLin;
148     //     double sinrDb = txPowerDbm- noisePowerDbm - receiverNoiseFigureDb - loss[i];
149     double sinrDb = 10 * std::log10 (sinrLin);
150     NS_LOG_INFO (" Ptx " << txPowerDbm << " Pn " << noisePowerDbm << " Fn " << receiverNoiseFigureDb << " Pl " << loss[i] << " dist " << dist[i]);
151 
152     int mcs = -1;
153     int numSnrEfficiencyMcsEntries = sizeof (snrEfficiencyMcs) / sizeof (SnrEfficiencyMcs);
154     for (int j = 0; j < numSnrEfficiencyMcsEntries && snrEfficiencyMcs[j].snrDb < sinrDb; ++j)
155       {
156         mcs = snrEfficiencyMcs[j].mcsIndex;
157       }
158 
159     std::ostringstream name;
160     name << " snr= " << sinrDb << " dB, "
161          << " mcs= " << snrEfficiencyMcs[i].mcsIndex;
162     AddTestCase (new LtePathlossModelSystemTestCase (name.str (),  sinrDb, dist[i], mcs), TestCase::QUICK);
163   }
164 
165 
166 
167 
168 
169 }
170 
171 static LtePathlossModelTestSuite ltePathlossModelTestSuite;
172 
173 
174 
175 
LtePathlossModelSystemTestCase(std::string name,double snrDb,double dist,uint16_t mcsIndex)176 LtePathlossModelSystemTestCase::LtePathlossModelSystemTestCase (std::string name, double snrDb, double dist, uint16_t mcsIndex)
177 : TestCase (name),
178 m_snrDb (snrDb),
179 m_distance (dist),
180 m_mcsIndex (mcsIndex)
181 {
182   std::ostringstream sstream1, sstream2;
183   sstream1 << " snr=" << snrDb
184   << " mcs=" << mcsIndex << " distance=" << dist;
185 
186   NS_LOG_INFO ("Creating LtePathlossModelSystemTestCase: " + sstream1.str ());
187 }
188 
~LtePathlossModelSystemTestCase()189 LtePathlossModelSystemTestCase::~LtePathlossModelSystemTestCase ()
190 {
191 }
192 
193 void
DoRun(void)194 LtePathlossModelSystemTestCase::DoRun (void)
195 {
196   Config::SetDefault ("ns3::MacStatsCalculator::DlOutputFilename", StringValue (CreateTempDirFilename ("DlMacStats.txt")));
197   Config::SetDefault ("ns3::MacStatsCalculator::UlOutputFilename", StringValue (CreateTempDirFilename ("UlMacStats.txt")));
198   Config::SetDefault ("ns3::RadioBearerStatsCalculator::DlRlcOutputFilename", StringValue (CreateTempDirFilename ("DlRlcStats.txt")));
199   Config::SetDefault ("ns3::RadioBearerStatsCalculator::UlRlcOutputFilename", StringValue (CreateTempDirFilename ("UlRlcStats.txt")));
200   /**
201   * Simulation Topology
202   */
203   //Disable Uplink Power Control
204   Config::SetDefault ("ns3::LteUePhy::EnableUplinkPowerControl", BooleanValue (false));
205 
206   Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
207   //   lteHelper->EnableLogComponents ();
208   lteHelper->SetAttribute ("PathlossModel", StringValue ("ns3::HybridBuildingsPropagationLossModel"));
209 
210   // set frequency. This is important because it changes the behavior of the path loss model
211   lteHelper->SetEnbDeviceAttribute ("DlEarfcn", UintegerValue (200));
212   lteHelper->SetEnbDeviceAttribute ("UlEarfcn", UintegerValue (18200));
213   lteHelper->SetUeDeviceAttribute ("DlEarfcn", UintegerValue (200));
214 
215   // remove shadowing component
216   lteHelper->SetPathlossModelAttribute ("ShadowSigmaOutdoor", DoubleValue (0.0));
217   lteHelper->SetPathlossModelAttribute ("ShadowSigmaIndoor", DoubleValue (0.0));
218   lteHelper->SetPathlossModelAttribute ("ShadowSigmaExtWalls", DoubleValue (0.0));
219 
220   // Create Nodes: eNodeB and UE
221   NodeContainer enbNodes;
222   NodeContainer ueNodes;
223   enbNodes.Create (1);
224   ueNodes.Create (1);
225   NodeContainer allNodes = NodeContainer ( enbNodes, ueNodes );
226 
227   // Install Mobility Model
228   MobilityHelper mobility;
229   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
230   mobility.Install (allNodes);
231   BuildingsHelper::Install (allNodes);
232 
233 
234   // Create Devices and install them in the Nodes (eNB and UE)
235   NetDeviceContainer enbDevs;
236   NetDeviceContainer ueDevs;
237   lteHelper->SetSchedulerType ("ns3::RrFfMacScheduler");
238   enbDevs = lteHelper->InstallEnbDevice (enbNodes);
239   ueDevs = lteHelper->InstallUeDevice (ueNodes);
240 
241   Ptr<MobilityModel> mm_enb = enbNodes.Get (0)->GetObject<MobilityModel> ();
242   mm_enb->SetPosition (Vector (0.0, 0.0, 30.0));
243   Ptr<MobilityModel> mm_ue = ueNodes.Get (0)->GetObject<MobilityModel> ();
244   mm_ue->SetPosition (Vector (m_distance, 0.0, 1.0));
245 
246   Ptr<LteEnbNetDevice> lteEnbDev = enbDevs.Get (0)->GetObject<LteEnbNetDevice> ();
247   Ptr<LteEnbPhy> enbPhy = lteEnbDev->GetPhy ();
248   enbPhy->SetAttribute ("TxPower", DoubleValue (30.0));
249   enbPhy->SetAttribute ("NoiseFigure", DoubleValue (5.0));
250 
251   Ptr<LteUeNetDevice> lteUeDev = ueDevs.Get (0)->GetObject<LteUeNetDevice> ();
252   Ptr<LteUePhy> uePhy = lteUeDev->GetPhy ();
253   uePhy->SetAttribute ("TxPower", DoubleValue (23.0));
254   uePhy->SetAttribute ("NoiseFigure", DoubleValue (9.0));
255 
256 
257   // Attach a UE to a eNB
258   lteHelper->Attach (ueDevs, enbDevs.Get (0));
259 
260   // Activate an EPS bearer
261   enum EpsBearer::Qci q = EpsBearer::GBR_CONV_VOICE;
262   EpsBearer bearer (q);
263   lteHelper->ActivateDataRadioBearer (ueDevs, bearer);
264 
265   // Use testing chunk processor in the PHY layer
266   // It will be used to test that the SNR is as intended
267   //Ptr<LtePhy> uePhy = ueDevs.Get (0)->GetObject<LteUeNetDevice> ()->GetPhy ()->GetObject<LtePhy> ();
268   Ptr<LteChunkProcessor> testSinr = Create<LteChunkProcessor> ();
269   LteSpectrumValueCatcher sinrCatcher;
270   testSinr->AddCallback (MakeCallback (&LteSpectrumValueCatcher::ReportValue, &sinrCatcher));
271   uePhy->GetDownlinkSpectrumPhy ()->AddCtrlSinrChunkProcessor (testSinr);
272 
273 //   Config::Connect ("/NodeList/0/DeviceList/0/LteEnbMac/DlScheduling",
274 //                    MakeBoundCallback (&LteTestPathlossDlSchedCallback, this));
275 
276   lteHelper->EnableMacTraces ();
277   lteHelper->EnableRlcTraces ();
278 
279   Simulator::Stop (Seconds (0.035));
280   Simulator::Run ();
281 
282   double calculatedSinrDb = 10.0 * std::log10 (sinrCatcher.GetValue ()->operator[] (0));
283   NS_LOG_INFO ("Distance " << m_distance << " Calculated SINR " << calculatedSinrDb << " ref " << m_snrDb);
284   Simulator::Destroy ();
285   NS_TEST_ASSERT_MSG_EQ_TOL (calculatedSinrDb, m_snrDb, 0.001, "Wrong SINR !");
286 }
287 
288 
289 void
DlScheduling(DlSchedulingCallbackInfo dlInfo)290 LtePathlossModelSystemTestCase::DlScheduling (DlSchedulingCallbackInfo dlInfo)
291 {
292   static bool firstTime = true;
293 
294   if ( firstTime )
295   {
296     firstTime = false;
297     NS_LOG_INFO ("SNR\tRef_MCS\tCalc_MCS");
298   }
299 
300 
301   // need to allow for RRC connection establishment + SRS transmission
302   if (Simulator::Now () > MilliSeconds (21))
303   {
304     NS_LOG_INFO (m_snrDb << "\t" << m_mcsIndex << "\t" << (uint16_t)dlInfo.mcsTb1);
305 
306     NS_TEST_ASSERT_MSG_EQ ((uint16_t)dlInfo.mcsTb1, m_mcsIndex, "Wrong MCS index");
307   }
308 }
309