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 <cstdlib>
32 #include <fstream>
33 #include <ios>
34 #include <limits>
35 #include <map>
36 #include <string>
37 #include <utility>
38 
39 NS_LOG_COMPONENT_DEFINE("SatFadingExternalInputTraceContainer");
40 
41 namespace ns3
42 {
43 
44 NS_OBJECT_ENSURE_REGISTERED(SatFadingExternalInputTraceContainer);
45 
46 TypeId
48 {
49  static TypeId tid =
50  TypeId("ns3::SatFadingExternalInputTraceContainer")
51  .SetParent<Object>()
52  .AddConstructor<SatFadingExternalInputTraceContainer>()
53  .AddAttribute("UtInputMode",
54  "Input mode to read trace source files from given index table.",
56  MakeEnumAccessor<SatFadingExternalInputTraceContainer::InputMode_t>(
59  "ListMode",
61  "PositionMode",
63  "RandomMode"))
64  .AddAttribute(
65  "UtRtnUpIndexFileName",
66  "Index file defining trace source files for return up link/UTs.",
67  StringValue("BeamId-1_256_UT_fading_rtnup_trace_index.txt"),
69  MakeStringChecker())
70  .AddAttribute(
71  "UtFwdDownIndexFileName",
72  "Index file defining trace source files for forward down link/UTs.",
73  StringValue("BeamId-1_256_UT_fading_fwddwn_trace_index.txt"),
75  MakeStringChecker())
76  .AddAttribute(
77  "GwFwdUpIndexFileName",
78  "Index file defining trace source files for forward up link/GWs.",
79  StringValue("GW_fading_fwdup_traces.txt"),
81  MakeStringChecker())
82  .AddAttribute(
83  "GwRtnDownIndexFileName",
84  "Index file defining trace source files for return down link/GWs.",
85  StringValue("GW_fading_rtndwn_traces.txt"),
87  MakeStringChecker())
88  .AddAttribute(
89  "MaxDistance",
90  "Maximum distance allowed to fading source in position based mode [m].",
91  DoubleValue(5000),
93  MakeDoubleChecker<double>());
94  return tid;
95 }
96 
97 TypeId
99 {
100  return GetTypeId();
101 }
102 
104  : m_utInputMode(LIST_MODE),
105  m_indexFilesLoaded(false),
106  m_maxDistanceToFading(0)
107 {
108  NS_LOG_FUNCTION(this);
109 
110  ObjectBase::ConstructSelf(AttributeConstructionList());
111  m_dataPath = Singleton<SatEnvVariables>::Get()->LocateDataDirectory() +
112  "/additional-input/ext-fadingtraces/input/";
113 }
114 
116 {
117  NS_LOG_FUNCTION(this);
118 
119  m_utFadingMap.clear();
120  m_gwFadingMap.clear();
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION(this);
127 
128  if (!m_indexFilesLoaded)
129  {
134 
135  m_indexFilesLoaded = true;
136  }
137 }
138 
139 void
141  Ptr<MobilityModel> mobility)
142 {
143  NS_LOG_FUNCTION(this << utId);
144 
145  if (!m_indexFilesLoaded)
146  {
147  LoadIndexFiles();
148  }
149 
150  Ptr<SatFadingExternalInputTrace> ftRet =
154  utId - 1,
155  mobility);
156  Ptr<SatFadingExternalInputTrace> ftFwd =
160  utId - 1,
161  mobility);
162 
163  // First = RETURN_USER
164  // Second = FORWARD_USER
165  m_utFadingMap.insert(std::make_pair(utId, std::make_pair(ftRet, ftFwd)));
166 }
167 
168 void
170  Ptr<MobilityModel> mobility)
171 {
172  NS_LOG_FUNCTION(this << gwId);
173 
174  if (!m_indexFilesLoaded)
175  {
176  LoadIndexFiles();
177  }
178 
179  Ptr<SatFadingExternalInputTrace> ftRet =
181  LIST_MODE,
183  gwId - 1,
184  mobility);
185  Ptr<SatFadingExternalInputTrace> ftFwd =
187  LIST_MODE,
189  gwId - 1,
190  mobility);
191 
192  // First = RETURN_FEEDER
193  // Second = FORWARD_FEEDR
194  m_gwFadingMap.insert(std::make_pair(gwId, std::make_pair(ftRet, ftFwd)));
195 }
196 
197 Ptr<SatFadingExternalInputTrace>
199  SatEnums::ChannelType_t channelType,
200  Ptr<MobilityModel> mobility)
201 {
202  NS_LOG_FUNCTION(this << nodeId);
203 
204  Ptr<SatFadingExternalInputTrace> ft;
205  switch (channelType)
206  {
208  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(nodeId);
209 
210  if (iter == m_utFadingMap.end())
211  {
212  CreateUtFadingTrace(nodeId, mobility);
213  }
214 
215  ft = m_utFadingMap.at(nodeId).second;
216  break;
217  }
219  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(nodeId);
220 
221  if (iter == m_utFadingMap.end())
222  {
223  CreateUtFadingTrace(nodeId, mobility);
224  }
225 
226  ft = m_utFadingMap.at(nodeId).first;
227  break;
228  }
230  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(nodeId);
231 
232  if (iter == m_gwFadingMap.end())
233  {
234  CreateGwFadingTrace(nodeId, mobility);
235  }
236 
237  ft = m_gwFadingMap.at(nodeId).second;
238  break;
239  }
241  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(nodeId);
242 
243  if (iter == m_gwFadingMap.end())
244  {
245  CreateGwFadingTrace(nodeId, mobility);
246  }
247 
248  ft = m_gwFadingMap.at(nodeId).first;
249  break;
250  }
251  default: {
252  NS_LOG_ERROR(this << " not valid channel type!");
253  break;
254  }
255  }
256  return ft;
257 }
258 
259 bool
260 SatFadingExternalInputTraceContainer::TestFadingTraces(uint32_t numOfUts, uint32_t numOfGws)
261 {
262  NS_LOG_FUNCTION(this);
263  NS_ASSERT(numOfUts > 0);
264  NS_ASSERT(numOfGws > 0);
265 
266  uint32_t ueCount = m_utFadingMap.size();
267  uint32_t gwCount = m_gwFadingMap.size();
268 
269  if (!m_indexFilesLoaded)
270  {
275  m_indexFilesLoaded = true;
276  }
277 
278  for (uint32_t i = 1; i <= numOfUts; i++)
279  {
280  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(i);
281 
282  if (iter == m_utFadingMap.end())
283  {
284  ueCount++;
285  CreateUtFadingTrace(ueCount, nullptr);
286  }
287  }
288 
289  for (uint32_t i = 1; i <= numOfGws; i++)
290  {
291  std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(i);
292 
293  if (iter == m_gwFadingMap.end())
294  {
295  gwCount++;
296  CreateGwFadingTrace(gwCount, nullptr);
297  }
298  }
299 
300  // Go through all the created fading trace class as
301  // test each one of those. If even one test fails,
302  // return false.
303 
304  // Test UT fading traces
305  std::map<uint32_t, ChannelTracePair_t>::const_iterator cit;
306  for (cit = m_utFadingMap.begin(); cit != m_utFadingMap.end(); ++cit)
307  {
308  if (!cit->second.first->TestFadingTrace())
309  {
310  return false;
311  }
312  if (!cit->second.second->TestFadingTrace())
313  {
314  return false;
315  }
316  }
317 
318  // Test GW fading traces
319  for (cit = m_gwFadingMap.begin(); cit != m_gwFadingMap.end(); ++cit)
320  {
321  if (!cit->second.first->TestFadingTrace())
322  {
323  return false;
324  }
325  if (!cit->second.second->TestFadingTrace())
326  {
327  return false;
328  }
329  }
330 
331  // All tests succeeded
332  return true;
333 }
334 
335 void
337  TraceFileContainer_t& container)
338 {
339  NS_LOG_FUNCTION(this << indexFile);
340 
341  // READ FROM THE GIVEN INDEX FILE
342  std::ifstream* ifs = new std::ifstream((m_dataPath + indexFile).c_str(), std::ios::in);
343 
344  if (ifs->is_open())
345  {
346  double lat, lon, alt;
347  uint32_t id;
348  std::string fileName;
349 
350  *ifs >> id >> fileName >> lat >> lon >> alt;
351 
352  while (ifs->good())
353  {
354  NS_LOG_DEBUG(this << " id = " << id << " file = " << fileName
355  << " latitude [deg] = " << lat << " longitude [deg] = " << lon);
356 
357  // Store the values
358  TraceFileContainerItem_t item = std::make_pair(fileName, GeoCoordinate(lat, lon, alt));
359  container.push_back(item);
360 
361  // get next row
362  *ifs >> id >> fileName >> lat >> lon >> alt;
363  }
364 
365  ifs->close();
366  }
367  else
368  {
369  NS_FATAL_ERROR(m_dataPath << indexFile << " index file not found.");
370  }
371 
372  delete ifs;
373 }
374 
375 Ptr<SatFadingExternalInputTrace>
378  InputMode_t inputMode,
379  TraceFileContainer_t& container,
380  uint32_t id,
381  Ptr<MobilityModel> mobility)
382 {
383  NS_LOG_FUNCTION(this << mobility);
384 
385  Ptr<SatFadingExternalInputTrace> trace;
386  std::string fileName;
387 
388  switch (inputMode)
389  {
390  case LIST_MODE:
391  if (container.empty() || (id > container.size()))
392  {
393  NS_FATAL_ERROR("No input available!");
394  }
395  else
396  {
397  fileName = container.at(id).first;
398  }
399  break;
400 
401  case RANDOM_MODE:
402  if (container.empty())
403  {
404  NS_FATAL_ERROR("No input available!");
405  }
406  else
407  {
408  fileName = container.at(std::rand() % container.size()).first;
409  }
410  break;
411 
412  case POSITION_MODE:
413  fileName = FindSourceBasedOnPosition(container, id, mobility);
414  break;
415 
416  default:
417  NS_FATAL_ERROR("Not supported mode.");
418  break;
419  }
420 
421  NS_LOG_INFO("Creation info: Mode=" << m_utInputMode << ", ID (GW/UT)=" << id
422  << ", FileName=" << fileName);
423 
424  // find from loaded list
425 
426  TraceInputContainer_t::iterator it = m_loadedTraces.find(fileName);
427 
428  if (it == m_loadedTraces.end())
429  {
430  // create if not found
431  trace = Create<SatFadingExternalInputTrace>(fileType, m_dataPath + fileName);
432  }
433  else
434  {
435  trace = it->second;
436  }
437 
438  return trace;
439 }
440 
441 std::string
443  uint32_t id,
444  Ptr<MobilityModel> mobility)
445 {
446  NS_LOG_FUNCTION(this << mobility);
447 
448  std::string fileName;
449  double currentDistanceToFading = std::numeric_limits<double>::max();
450  Vector position = mobility->GetPosition();
451 
452  for (TraceFileContainer_t::iterator it = container.begin(); it != container.end(); it++)
453  {
454  Vector fadingPosition = it->second.ToVector();
455 
456  double distanceToFading = CalculateDistance(position, fadingPosition);
457 
458  if (distanceToFading < currentDistanceToFading)
459  {
460  currentDistanceToFading = distanceToFading;
461  fileName = it->first;
462  }
463  }
464 
465  if (currentDistanceToFading > m_maxDistanceToFading)
466  {
467  NS_FATAL_ERROR(
468  "No valid fading based on position (min found distance, max allowed distance): "
469  << currentDistanceToFading << ", " << m_maxDistanceToFading);
470  }
471  else
472  {
473  NS_LOG_INFO("Minimum distance to fading trace source: " << currentDistanceToFading);
474  }
475 
476  return fileName;
477 }
478 
479 } // 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.