satellite-fading-external-input-trace-container.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd.
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: Jani Puttonen <jani.puttonen@magister.fi>
19  */
20 
22 
23 #include "../utils/satellite-env-variables.h"
24 
25 #include <ns3/double.h>
26 #include <ns3/enum.h>
27 #include <ns3/log.h>
28 #include <ns3/singleton.h>
29 #include <ns3/string.h>
30 
31 #include <fstream>
32 
33 NS_LOG_COMPONENT_DEFINE("SatFadingExternalInputTraceContainer");
34 
35 namespace ns3
36 {
37 
38 NS_OBJECT_ENSURE_REGISTERED(SatFadingExternalInputTraceContainer);
39 
40 TypeId
42 {
43  static TypeId tid =
44  TypeId("ns3::SatFadingExternalInputTraceContainer")
45  .SetParent<Object>()
46  .AddConstructor<SatFadingExternalInputTraceContainer>()
47  .AddAttribute("UtInputMode",
48  "Input mode to read trace source files from given index table.",
52  "ListMode",
54  "PositionMode",
56  "RandomMode"))
57  .AddAttribute(
58  "UtRtnUpIndexFileName",
59  "Index file defining trace source files for return up link/UTs.",
60  StringValue("BeamId-1_256_UT_fading_rtnup_trace_index.txt"),
62  MakeStringChecker())
63  .AddAttribute(
64  "UtFwdDownIndexFileName",
65  "Index file defining trace source files for forward down link/UTs.",
66  StringValue("BeamId-1_256_UT_fading_fwddwn_trace_index.txt"),
68  MakeStringChecker())
69  .AddAttribute(
70  "GwFwdUpIndexFileName",
71  "Index file defining trace source files for forward up link/GWs.",
72  StringValue("GW_fading_fwdup_traces.txt"),
74  MakeStringChecker())
75  .AddAttribute(
76  "GwRtnDownIndexFileName",
77  "Index file defining trace source files for return down link/GWs.",
78  StringValue("GW_fading_rtndwn_traces.txt"),
80  MakeStringChecker())
81  .AddAttribute(
82  "MaxDistance",
83  "Maximum distance allowed to fading source in position based mode [m].",
84  DoubleValue(5000),
86  MakeDoubleChecker<double>());
87  return tid;
88 }
89 
90 TypeId
92 {
93  return GetTypeId();
94 }
95 
97  : m_utInputMode(LIST_MODE),
98  m_indexFilesLoaded(false),
99  m_maxDistanceToFading(0)
100 {
101  NS_LOG_FUNCTION(this);
102 
103  ObjectBase::ConstructSelf(AttributeConstructionList());
104  m_dataPath =
105  Singleton<SatEnvVariables>::Get()->LocateDataDirectory() + "/ext-fadingtraces/input/";
106 }
107 
109 {
110  NS_LOG_FUNCTION(this);
111 
112  m_utFadingMap.clear();
113  m_gwFadingMap.clear();
114 }
115 
116 void
118 {
119  NS_LOG_FUNCTION(this);
120 
121  if (!m_indexFilesLoaded)
122  {
127 
128  m_indexFilesLoaded = true;
129  }
130 }
131 
132 void
134  Ptr<MobilityModel> mobility)
135 {
136  NS_LOG_FUNCTION(this << utId);
137 
138  if (!m_indexFilesLoaded)
139  {
140  LoadIndexFiles();
141  }
142 
143  Ptr<SatFadingExternalInputTrace> ftRet =
147  utId - 1,
148  mobility);
149  Ptr<SatFadingExternalInputTrace> ftFwd =
153  utId - 1,
154  mobility);
155 
156  // First = RETURN_USER
157  // Second = FORWARD_USER
158  m_utFadingMap.insert(std::make_pair(utId, std::make_pair(ftRet, ftFwd)));
159 }
160 
161 void
163  Ptr<MobilityModel> mobility)
164 {
165  NS_LOG_FUNCTION(this << gwId);
166 
167  if (!m_indexFilesLoaded)
168  {
169  LoadIndexFiles();
170  }
171 
172  Ptr<SatFadingExternalInputTrace> ftRet =
174  LIST_MODE,
176  gwId - 1,
177  mobility);
178  Ptr<SatFadingExternalInputTrace> ftFwd =
180  LIST_MODE,
182  gwId - 1,
183  mobility);
184 
185  // First = RETURN_FEEDER
186  // Second = FORWARD_FEEDR
187  m_gwFadingMap.insert(std::make_pair(gwId, std::make_pair(ftRet, ftFwd)));
188 }
189 
190 Ptr<SatFadingExternalInputTrace>
192  SatEnums::ChannelType_t channelType,
193  Ptr<MobilityModel> mobility)
194 {
195  NS_LOG_FUNCTION(this << nodeId);
196 
197  Ptr<SatFadingExternalInputTrace> ft;
198  switch (channelType)
199  {
201  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(nodeId);
202 
203  if (iter == m_utFadingMap.end())
204  {
205  CreateUtFadingTrace(nodeId, mobility);
206  }
207 
208  ft = m_utFadingMap.at(nodeId).second;
209  break;
210  }
212  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(nodeId);
213 
214  if (iter == m_utFadingMap.end())
215  {
216  CreateUtFadingTrace(nodeId, mobility);
217  }
218 
219  ft = m_utFadingMap.at(nodeId).first;
220  break;
221  }
223  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(nodeId);
224 
225  if (iter == m_gwFadingMap.end())
226  {
227  CreateGwFadingTrace(nodeId, mobility);
228  }
229 
230  ft = m_gwFadingMap.at(nodeId).second;
231  break;
232  }
234  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(nodeId);
235 
236  if (iter == m_gwFadingMap.end())
237  {
238  CreateGwFadingTrace(nodeId, mobility);
239  }
240 
241  ft = m_gwFadingMap.at(nodeId).first;
242  break;
243  }
244  default: {
245  NS_LOG_ERROR(this << " not valid channel type!");
246  break;
247  }
248  }
249  return ft;
250 }
251 
252 bool
253 SatFadingExternalInputTraceContainer::TestFadingTraces(uint32_t numOfUts, uint32_t numOfGws)
254 {
255  NS_LOG_FUNCTION(this);
256  NS_ASSERT(numOfUts > 0);
257  NS_ASSERT(numOfGws > 0);
258 
259  uint32_t ueCount = m_utFadingMap.size();
260  uint32_t gwCount = m_gwFadingMap.size();
261 
262  if (!m_indexFilesLoaded)
263  {
268  m_indexFilesLoaded = true;
269  }
270 
271  for (uint32_t i = 1; i <= numOfUts; i++)
272  {
273  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(i);
274 
275  if (iter == m_utFadingMap.end())
276  {
277  ueCount++;
278  CreateUtFadingTrace(ueCount, NULL);
279  }
280  }
281 
282  for (uint32_t i = 1; i <= numOfGws; i++)
283  {
284  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(i);
285 
286  if (iter == m_gwFadingMap.end())
287  {
288  gwCount++;
289  CreateGwFadingTrace(gwCount, NULL);
290  }
291  }
292 
293  // Go through all the created fading trace class as
294  // test each one of those. If even one test fails,
295  // return false.
296 
297  // Test UT fading traces
298  std::map<uint32_t, ChannelTracePair_t>::const_iterator cit;
299  for (cit = m_utFadingMap.begin(); cit != m_utFadingMap.end(); ++cit)
300  {
301  if (!cit->second.first->TestFadingTrace())
302  {
303  return false;
304  }
305  if (!cit->second.second->TestFadingTrace())
306  {
307  return false;
308  }
309  }
310 
311  // Test GW fading traces
312  for (cit = m_gwFadingMap.begin(); cit != m_gwFadingMap.end(); ++cit)
313  {
314  if (!cit->second.first->TestFadingTrace())
315  {
316  return false;
317  }
318  if (!cit->second.second->TestFadingTrace())
319  {
320  return false;
321  }
322  }
323 
324  // All tests succeeded
325  return true;
326 }
327 
328 void
330  TraceFileContainer_t& container)
331 {
332  NS_LOG_FUNCTION(this << indexFile);
333 
334  // READ FROM THE GIVEN INDEX FILE
335  std::ifstream* ifs = new std::ifstream((m_dataPath + indexFile).c_str(), std::ios::in);
336 
337  if (ifs->is_open())
338  {
339  double lat, lon, alt;
340  uint32_t id;
341  std::string fileName;
342 
343  *ifs >> id >> fileName >> lat >> lon >> alt;
344 
345  while (ifs->good())
346  {
347  NS_LOG_DEBUG(this << " id = " << id << " file = " << fileName
348  << " latitude [deg] = " << lat << " longitude [deg] = " << lon);
349 
350  // Store the values
351  TraceFileContainerItem_t item = std::make_pair(fileName, GeoCoordinate(lat, lon, alt));
352  container.push_back(item);
353 
354  // get next row
355  *ifs >> id >> fileName >> lat >> lon >> alt;
356  }
357 
358  ifs->close();
359  }
360  else
361  {
362  NS_FATAL_ERROR(m_dataPath << indexFile << " index file not found.");
363  }
364 
365  delete ifs;
366 }
367 
368 Ptr<SatFadingExternalInputTrace>
371  InputMode_t inputMode,
372  TraceFileContainer_t& container,
373  uint32_t id,
374  Ptr<MobilityModel> mobility)
375 {
376  NS_LOG_FUNCTION(this << mobility);
377 
378  Ptr<SatFadingExternalInputTrace> trace;
379  std::string fileName;
380 
381  switch (inputMode)
382  {
383  case LIST_MODE:
384  if (container.empty() || (id > container.size()))
385  {
386  NS_FATAL_ERROR("No input available!");
387  }
388  else
389  {
390  fileName = container.at(id).first;
391  }
392  break;
393 
394  case RANDOM_MODE:
395  if (container.empty())
396  {
397  NS_FATAL_ERROR("No input available!");
398  }
399  else
400  {
401  fileName = container.at(std::rand() % container.size()).first;
402  }
403  break;
404 
405  case POSITION_MODE:
406  fileName = FindSourceBasedOnPosition(container, id, mobility);
407  break;
408 
409  default:
410  NS_FATAL_ERROR("Not supported mode.");
411  break;
412  }
413 
414  NS_LOG_INFO("Creation info: Mode=" << m_utInputMode << ", ID (GW/UT)=" << id
415  << ", FileName=" << fileName);
416 
417  // find from loaded list
418 
419  TraceInputContainer_t::iterator it = m_loadedTraces.find(fileName);
420 
421  if (it == m_loadedTraces.end())
422  {
423  // create if not found
424  trace = Create<SatFadingExternalInputTrace>(fileType, m_dataPath + fileName);
425  }
426  else
427  {
428  trace = it->second;
429  }
430 
431  return trace;
432 }
433 
434 std::string
436  uint32_t id,
437  Ptr<MobilityModel> mobility)
438 {
439  NS_LOG_FUNCTION(this << mobility);
440 
441  std::string fileName;
442  double currentDistanceToFading = std::numeric_limits<double>::max();
443  Vector position = mobility->GetPosition();
444 
445  for (TraceFileContainer_t::iterator it = container.begin(); it != container.end(); it++)
446  {
447  Vector fadingPosition = it->second.ToVector();
448 
449  double distanceToFading = CalculateDistance(position, fadingPosition);
450 
451  if (distanceToFading < currentDistanceToFading)
452  {
453  currentDistanceToFading = distanceToFading;
454  fileName = it->first;
455  }
456  }
457 
458  if (currentDistanceToFading > m_maxDistanceToFading)
459  {
460  NS_FATAL_ERROR(
461  "No valid fading based on position (min found distance, max allowed distance): "
462  << currentDistanceToFading << ", " << m_maxDistanceToFading);
463  }
464  else
465  {
466  NS_LOG_INFO("Minimum distance to fading trace source: " << currentDistanceToFading);
467  }
468 
469  return fileName;
470 }
471 
472 } // namespace ns3
GeoCoordinate class is used to store and operate with geodetic coordinates.
ChannelType_t
Types of channel.
std::string m_gwRtnDownIndexFileName
The name of file which defines the index table to be used for trace input of forward up link for GWs.
void CreateGwFadingTrace(uint32_t gwId, Ptr< MobilityModel > mobility)
Create new GW fading trace.
void ReadIndexFile(std::string indexFile, TraceFileContainer_t &container)
Read trace file information from given index file.
std::string m_utFwdDownIndexFileName
The name of file which defines the index table to be used for trace input of forward down link for UT...
void CreateUtFadingTrace(uint32_t utId, Ptr< MobilityModel > mobility)
Create new UT fading trace.
std::string FindSourceBasedOnPosition(TraceFileContainer_t &container, uint32_t id, Ptr< MobilityModel > mobility)
Find the nearest fading trace source file for the requested UT/GW based on given mobility.
TraceFileContainer_t m_utRtnUpFileNames
UT return up link trace file names.
virtual TypeId GetInstanceTypeId(void) const
Get the type ID of instance.
InputMode_t
Definitions for different modes of using trace input files.
TraceFileContainer_t m_gwRtnDownFileNames
GW return down link trace file names.
std::map< uint32_t, ChannelTracePair_t > m_gwFadingMap
Container of the GW fading traces.
double m_maxDistanceToFading
Maximum distance allowed to the external fading trace source.
std::string m_utRtnUpIndexFileName
The name of file which defines the index table to be used for trace input of return up link for UTs.
Ptr< SatFadingExternalInputTrace > CreateFadingTrace(SatFadingExternalInputTrace::TraceFileType_e fileType, InputMode_t inputMode, TraceFileContainer_t &container, uint32_t id, Ptr< MobilityModel > mobility)
Create (or load) fading trace source for the requested UT/GW.
std::string m_gwFwdUpIndexFileName
The name of file which defines the index table to be used for trace input of forward up link for GWs.
InputMode_t m_utInputMode
Input mode to read trace files form given index table (file) for UTs.
Ptr< SatFadingExternalInputTrace > GetFadingTrace(uint32_t nodeId, SatEnums::ChannelType_t channelType, Ptr< MobilityModel > mobility)
Get method for getting a proper fading trace.
TraceFileContainer_t m_gwFwdUpFileNames
GW forward up link trace file names.
bool m_indexFilesLoaded
flag telling if index trace files are already loaded
bool TestFadingTraces(uint32_t numOfUts, uint32_t numOfGws)
A method to test that the fading traces are according to assumptions.
std::map< uint32_t, ChannelTracePair_t > m_utFadingMap
Container of the UT fading traces.
TraceFileContainer_t m_utFwdDownFileNames
UT forward down link trace file names.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.