A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
radio-environment-map-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 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 
23 
24 #include <ns3/abort.h>
25 #include <ns3/log.h>
26 #include <ns3/double.h>
27 #include <ns3/uinteger.h>
28 #include <ns3/string.h>
29 #include <ns3/boolean.h>
30 #include <ns3/spectrum-channel.h>
31 #include <ns3/config.h>
32 #include <ns3/rem-spectrum-phy.h>
33 #include <ns3/mobility-building-info.h>
34 #include <ns3/constant-position-mobility-model.h>
35 #include <ns3/simulator.h>
36 #include <ns3/node.h>
37 #include <ns3/buildings-helper.h>
38 #include <ns3/lte-spectrum-value-helper.h>
39 
40 #include <fstream>
41 #include <limits>
42 
43 NS_LOG_COMPONENT_DEFINE ("RadioEnvironmentMapHelper");
44 
45 namespace ns3 {
46 
47 
48 
49 NS_OBJECT_ENSURE_REGISTERED (RadioEnvironmentMapHelper)
50  ;
51 
53 {
54 }
55 
56 
58 {
59 }
60 
61 
62 
63 void
65 {
66  NS_LOG_FUNCTION (this);
67 }
68 
69 TypeId
71 {
72  NS_LOG_FUNCTION ("RadioEnvironmentMapHelper::GetTypeId");
73  static TypeId tid = TypeId ("ns3::RadioEnvironmentMapHelper")
74  .SetParent<Object> ()
75  .AddConstructor<RadioEnvironmentMapHelper> ()
76  .AddAttribute ("ChannelPath", "The path to the channel for which the Radio Environment Map is to be generated",
77  StringValue ("/ChannelList/0"),
78  MakeStringAccessor (&RadioEnvironmentMapHelper::m_channelPath),
79  MakeStringChecker ())
80  .AddAttribute ("OutputFile", "the filename to which the Radio Environment Map is saved",
81  StringValue ("rem.out"),
82  MakeStringAccessor (&RadioEnvironmentMapHelper::m_outputFile),
83  MakeStringChecker ())
84  .AddAttribute ("XMin", "The min x coordinate of the map.",
85  DoubleValue (0.0),
86  MakeDoubleAccessor (&RadioEnvironmentMapHelper::m_xMin),
87  MakeDoubleChecker<double> ())
88  .AddAttribute ("YMin", "The min y coordinate of the map.",
89  DoubleValue (0.0),
90  MakeDoubleAccessor (&RadioEnvironmentMapHelper::m_yMin),
91  MakeDoubleChecker<double> ())
92  .AddAttribute ("XMax", "The max x coordinate of the map.",
93  DoubleValue (1.0),
94  MakeDoubleAccessor (&RadioEnvironmentMapHelper::m_xMax),
95  MakeDoubleChecker<double> ())
96  .AddAttribute ("YMax", "The max y coordinate of the map.",
97  DoubleValue (1.0),
98  MakeDoubleAccessor (&RadioEnvironmentMapHelper::m_yMax),
99  MakeDoubleChecker<double> ())
100  .AddAttribute ("XRes", "The resolution (number of points) of the map along the x axis.",
101  UintegerValue (100),
102  MakeUintegerAccessor (&RadioEnvironmentMapHelper::m_xRes),
103  MakeUintegerChecker<uint32_t> (2,std::numeric_limits<uint16_t>::max ()))
104  .AddAttribute ("YRes", "The resolution (number of points) of the map along the y axis.",
105  UintegerValue (100),
106  MakeUintegerAccessor (&RadioEnvironmentMapHelper::m_yRes),
107  MakeUintegerChecker<uint16_t> (2,std::numeric_limits<uint16_t>::max ()))
108  .AddAttribute ("Z", "The value of the z coordinate for which the map is to be generated",
109  DoubleValue (0.0),
110  MakeDoubleAccessor (&RadioEnvironmentMapHelper::m_z),
111  MakeDoubleChecker<double> ())
112  .AddAttribute ("StopWhenDone", "If true, Simulator::Stop () will be called as soon as the REM has been generated",
113  BooleanValue (true),
114  MakeBooleanAccessor (&RadioEnvironmentMapHelper::m_stopWhenDone),
115  MakeBooleanChecker ())
116  .AddAttribute ("NoisePower",
117  "the power of the measuring instrument noise, in Watts. Default to a kT of -174 dBm with a noise figure of 9 dB and a bandwidth of 25 LTE Resource Blocks",
118  DoubleValue (1.4230e-10),
119  MakeDoubleAccessor (&RadioEnvironmentMapHelper::m_noisePower),
120  MakeDoubleChecker<double> ())
121  .AddAttribute ("MaxPointsPerIteration", "Maximum number of REM points to be calculated per iteration. Every point consumes approximately 5KB of memory.",
122  UintegerValue (20000),
124  MakeUintegerChecker<uint32_t> (1,std::numeric_limits<uint32_t>::max ()))
125  .AddAttribute ("Earfcn",
126  "E-UTRA Absolute Radio Frequency Channel Number (EARFCN) "
127  "as per 3GPP 36.101 Section 5.7.3. ",
128  UintegerValue (100),
129  MakeUintegerAccessor (&RadioEnvironmentMapHelper::m_earfcn),
130  MakeUintegerChecker<uint16_t> ())
131  .AddAttribute ("Bandwidth",
132  "Transmission Bandwidth Configuration (in number of RBs) over which the SINR will be calculated",
133  UintegerValue (25),
134  MakeUintegerAccessor (&RadioEnvironmentMapHelper::SetBandwidth,
136  MakeUintegerChecker<uint16_t> ())
137  ;
138  return tid;
139 }
140 
141 
142 uint8_t
144 {
145  return m_bandwidth;
146 }
147 
148 void
150 {
151  switch (bw)
152  {
153  case 6:
154  case 15:
155  case 25:
156  case 50:
157  case 75:
158  case 100:
159  m_bandwidth = bw;
160  break;
161 
162  default:
163  NS_FATAL_ERROR ("invalid bandwidth value " << (uint16_t) bw);
164  break;
165  }
166 }
167 
168 
169 
170 void
172 {
173  NS_LOG_FUNCTION (this);
174  if (!m_rem.empty ())
175  {
176  NS_FATAL_ERROR ("only one REM supported per instance of RadioEnvironmentMapHelper");
177  }
179  if (match.GetN () != 1)
180  {
181  NS_FATAL_ERROR ("Lookup " << m_channelPath << " should have exactly one match");
182  }
183  m_channel = match.Get (0)->GetObject<SpectrumChannel> ();
184  NS_ABORT_MSG_IF (m_channel == 0, "object at " << m_channelPath << "is not of type SpectrumChannel");
185 
186  m_outFile.open (m_outputFile.c_str ());
187  if (!m_outFile.is_open ())
188  {
189  NS_FATAL_ERROR ("Can't open file " << (m_outputFile));
190  return;
191  }
192 
193  Simulator::Schedule (Seconds (0.0026),
195  this);
196 }
197 
198 
199 void
201 {
202  NS_LOG_FUNCTION (this);
203  m_xStep = (m_xMax - m_xMin)/(m_xRes-1);
204  m_yStep = (m_yMax - m_yMin)/(m_yRes-1);
205 
206  if ((double)m_xRes * (double) m_yRes < (double) m_maxPointsPerIteration)
207  {
209  }
210 
211  for (uint32_t i = 0; i < m_maxPointsPerIteration; ++i)
212  {
213  RemPoint p;
214  p.phy = CreateObject<RemSpectrumPhy> ();
215  p.bmm = CreateObject<ConstantPositionMobilityModel> ();
216  Ptr<MobilityBuildingInfo> buildingInfo = CreateObject<MobilityBuildingInfo> ();
217  p.bmm->AggregateObject (buildingInfo); // operation usually done by BuildingsHelper::Install
219  p.phy->SetMobility (p.bmm);
220  m_channel->AddRx (p.phy);
221  m_rem.push_back (p);
222  }
223 
224  double remIterationStartTime = 0.0001;
225  double xMinNext = m_xMin;
226  double yMinNext = m_yMin;
227  uint32_t numPointsCurrentIteration = 0;
228  bool justScheduled = false;
229  for (double x = m_xMin; x < m_xMax + 0.5*m_xStep; x += m_xStep)
230  {
231  for (double y = m_yMin; y < m_yMax + 0.5*m_yStep ; y += m_yStep)
232  {
233  if (justScheduled)
234  {
235  xMinNext = x;
236  yMinNext = y;
237  justScheduled = false;
238  }
239 
240  ++numPointsCurrentIteration;
241  if ((numPointsCurrentIteration == m_maxPointsPerIteration)
242  || ((x > m_xMax - 0.5*m_xStep) && (y > m_yMax - 0.5*m_yStep)) )
243  {
244  Simulator::Schedule (Seconds (remIterationStartTime),
246  this, xMinNext, x, yMinNext, y);
247  remIterationStartTime += 0.001;
248  justScheduled = true;
249  numPointsCurrentIteration = 0;
250  }
251  }
252  }
253  Simulator::Schedule (Seconds (remIterationStartTime),
255  this);
256 }
257 
258 
259 void
260 RadioEnvironmentMapHelper::RunOneIteration (double xMin, double xMax, double yMin, double yMax)
261 {
262  NS_LOG_FUNCTION (this << xMin << xMax << yMin << yMax);
263  std::list<RemPoint>::iterator remIt = m_rem.begin ();
264  double x;
265  double y;
266  for (x = xMin; x < xMax + 0.5*m_xStep; x += m_xStep)
267  {
268  for (y = (x == xMin) ? yMin : m_yMin;
269  y < ((x == xMax) ? yMax : m_yMax) + 0.5*m_yStep;
270  y += m_yStep)
271  {
272  NS_ASSERT (remIt != m_rem.end ());
273  remIt->bmm->SetPosition (Vector (x, y, m_z));
274  BuildingsHelper::MakeConsistent (remIt->bmm);
275  ++remIt;
276  }
277  }
278 
279  if (remIt != m_rem.end ())
280  {
281  NS_ASSERT ((x > m_xMax - 0.5*m_xStep) && (y > m_yMax - 0.5*m_yStep));
282  NS_LOG_LOGIC ("deactivating RemSpectrumPhys that are unneeded in the last iteration");
283  while (remIt != m_rem.end ())
284  {
285  remIt->phy->Deactivate ();
286  ++remIt;
287  }
288  }
289 
291 }
292 
293 void
295 {
296  NS_LOG_FUNCTION (this);
297 
298  for (std::list<RemPoint>::iterator it = m_rem.begin ();
299  it != m_rem.end ();
300  ++it)
301  {
302  if (!(it->phy->IsActive ()))
303  {
304  // should occur only upon last iteration when some RemPoint
305  // at the end of the list can be unused
306  break;
307  }
308  Vector pos = it->bmm->GetPosition ();
309  NS_LOG_LOGIC ("output: " << pos.x << "\t"
310  << pos.y << "\t"
311  << pos.z << "\t"
312  << it->phy->GetSinr (m_noisePower));
313  m_outFile << pos.x << "\t"
314  << pos.y << "\t"
315  << pos.z << "\t"
316  << it->phy->GetSinr (m_noisePower)
317  << std::endl;
318  it->phy->Reset ();
319  }
320 }
321 
322 void
324 {
325  NS_LOG_FUNCTION (this);
326  m_outFile.close ();
327  if (m_stopWhenDone)
328  {
329  Simulator::Stop ();
330  }
331 }
332 
333 
334 } // namespace ns3
double x
x coordinate of vector
Definition: vector.h:49
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Hold a bool native type.
Definition: boolean.h:38
hold variables of type string
Definition: string.h:19
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
static Ptr< SpectrumModel > GetSpectrumModel(uint16_t earfcn, uint8_t bandwidth)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
a 3d vector
Definition: vector.h:31
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
static void MakeConsistent(Ptr< MobilityModel > bmm)
Make the given mobility model consistent, by determining whether its position falls inside any of the...
Config::MatchContainer LookupMatches(std::string path)
Definition: config.cc:739
void RunOneIteration(double xMin, double xMax, double yMin, double yMax)
Hold an unsigned integer type.
Definition: uinteger.h:46
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
uint32_t GetN(void) const
Definition: config.cc:63
void Install()
Deploy the RemSpectrumPhy objects that generate the map according to the specified settings...
Ptr< Object > Get(uint32_t i) const
Definition: config.cc:69
double y
y coordinate of vector
Definition: vector.h:53
hold a set of objects which match a specific search string.
Definition: config.h:127
NS_LOG_COMPONENT_DEFINE("RadioEnvironmentMapHelper")
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Defines the interface for spectrum-aware channel implementations.
a base class which provides memory management and object aggregation
Definition: object.h:63
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:98
Hold a floating point type.
Definition: double.h:41
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Ptr< T > GetObject(void) const
Definition: object.h:361
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
double z
z coordinate of vector
Definition: vector.h:57