Loading...
Searching...
No Matches
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
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
39NS_LOG_COMPONENT_DEFINE("SatFadingExternalInputTraceContainer");
40
41namespace ns3
42{
43
44NS_OBJECT_ENSURE_REGISTERED(SatFadingExternalInputTraceContainer);
45
46TypeId
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
104
105void
107{
108 NS_LOG_FUNCTION(this);
109
110 Object::NotifyConstructionCompleted();
111
112 m_dataPath = SatEnvVariables::GetInstance()->LocateDataDirectory() +
113 "/additional-input/ext-fadingtraces/input/";
114}
115
117{
118 NS_LOG_FUNCTION(this);
119
120 m_utFadingMap.clear();
121 m_gwFadingMap.clear();
122}
123
124void
139
140void
142 Ptr<MobilityModel> mobility)
143{
144 NS_LOG_FUNCTION(this << utId);
145
147 {
149 }
150
151 Ptr<SatFadingExternalInputTrace> ftRet =
155 utId - 1,
156 mobility);
157 Ptr<SatFadingExternalInputTrace> ftFwd =
161 utId - 1,
162 mobility);
163
164 // First = RETURN_USER
165 // Second = FORWARD_USER
166 m_utFadingMap.insert(std::make_pair(utId, std::make_pair(ftRet, ftFwd)));
167}
168
169void
171 Ptr<MobilityModel> mobility)
172{
173 NS_LOG_FUNCTION(this << gwId);
174
176 {
178 }
179
180 Ptr<SatFadingExternalInputTrace> ftRet =
182 LIST_MODE,
184 gwId - 1,
185 mobility);
186 Ptr<SatFadingExternalInputTrace> ftFwd =
188 LIST_MODE,
190 gwId - 1,
191 mobility);
192
193 // First = RETURN_FEEDER
194 // Second = FORWARD_FEEDR
195 m_gwFadingMap.insert(std::make_pair(gwId, std::make_pair(ftRet, ftFwd)));
196}
197
198Ptr<SatFadingExternalInputTrace>
200 SatEnums::ChannelType_t channelType,
201 Ptr<MobilityModel> mobility)
202{
203 NS_LOG_FUNCTION(this << nodeId);
204
205 Ptr<SatFadingExternalInputTrace> ft;
206 switch (channelType)
207 {
209 std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(nodeId);
210
211 if (iter == m_utFadingMap.end())
212 {
213 CreateUtFadingTrace(nodeId, mobility);
214 }
215
216 ft = m_utFadingMap.at(nodeId).second;
217 break;
218 }
220 std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(nodeId);
221
222 if (iter == m_utFadingMap.end())
223 {
224 CreateUtFadingTrace(nodeId, mobility);
225 }
226
227 ft = m_utFadingMap.at(nodeId).first;
228 break;
229 }
231 std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(nodeId);
232
233 if (iter == m_gwFadingMap.end())
234 {
235 CreateGwFadingTrace(nodeId, mobility);
236 }
237
238 ft = m_gwFadingMap.at(nodeId).second;
239 break;
240 }
242 std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(nodeId);
243
244 if (iter == m_gwFadingMap.end())
245 {
246 CreateGwFadingTrace(nodeId, mobility);
247 }
248
249 ft = m_gwFadingMap.at(nodeId).first;
250 break;
251 }
252 default: {
253 NS_LOG_ERROR(this << " not valid channel type!");
254 break;
255 }
256 }
257 return ft;
258}
259
260bool
262{
263 NS_LOG_FUNCTION(this << numOfUts << numOfGws);
264 NS_ASSERT(numOfUts > 0);
265 NS_ASSERT(numOfGws > 0);
266
267 uint32_t ueCount = m_utFadingMap.size();
268 uint32_t gwCount = m_gwFadingMap.size();
269
271 {
276 m_indexFilesLoaded = true;
277 }
278
279 for (uint32_t i = 1; i <= numOfUts; i++)
280 {
281 std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_utFadingMap.find(i);
282
283 if (iter == m_utFadingMap.end())
284 {
285 ueCount++;
286 CreateUtFadingTrace(ueCount, nullptr);
287 }
288 }
289
290 for (uint32_t i = 1; i <= numOfGws; i++)
291 {
292 std::map<uint32_t, ChannelTracePair_t>::iterator iter = m_gwFadingMap.find(i);
293
294 if (iter == m_gwFadingMap.end())
295 {
296 gwCount++;
297 CreateGwFadingTrace(gwCount, nullptr);
298 }
299 }
300
301 // Go through all the created fading trace class as
302 // test each one of those. If even one test fails,
303 // return false.
304
305 // Test UT fading traces
306 std::map<uint32_t, ChannelTracePair_t>::const_iterator cit;
307 for (cit = m_utFadingMap.begin(); cit != m_utFadingMap.end(); ++cit)
308 {
309 if (!cit->second.first->TestFadingTrace())
310 {
311 return false;
312 }
313 if (!cit->second.second->TestFadingTrace())
314 {
315 return false;
316 }
317 }
318
319 // Test GW fading traces
320 for (cit = m_gwFadingMap.begin(); cit != m_gwFadingMap.end(); ++cit)
321 {
322 if (!cit->second.first->TestFadingTrace())
323 {
324 return false;
325 }
326 if (!cit->second.second->TestFadingTrace())
327 {
328 return false;
329 }
330 }
331
332 // All tests succeeded
333 return true;
334}
335
336void
338 TraceFileContainer_t& container)
339{
340 NS_LOG_FUNCTION(this << indexFile);
341
342 // READ FROM THE GIVEN INDEX FILE
343 std::ifstream* ifs = new std::ifstream((m_dataPath + indexFile).c_str(), std::ios::in);
344
345 if (ifs->is_open())
346 {
347 double lat, lon, alt;
348 uint32_t id;
349 std::string fileName;
350
351 *ifs >> id >> fileName >> lat >> lon >> alt;
352
353 while (ifs->good())
354 {
355 NS_LOG_DEBUG(this << " id = " << id << " file = " << fileName
356 << " latitude [deg] = " << lat << " longitude [deg] = " << lon);
357
358 // Store the values
359 TraceFileContainerItem_t item = std::make_pair(fileName, GeoCoordinate(lat, lon, alt));
360 container.push_back(item);
361
362 // get next row
363 *ifs >> id >> fileName >> lat >> lon >> alt;
364 }
365
366 ifs->close();
367 }
368 else
369 {
370 NS_FATAL_ERROR(m_dataPath << indexFile << " index file not found.");
371 }
372
373 delete ifs;
374}
375
376Ptr<SatFadingExternalInputTrace>
379 InputMode_t inputMode,
380 TraceFileContainer_t& container,
381 uint32_t id,
382 Ptr<MobilityModel> mobility)
383{
384 NS_LOG_FUNCTION(this << mobility);
385
386 Ptr<SatFadingExternalInputTrace> trace;
387 std::string fileName;
388
389 switch (inputMode)
390 {
391 case LIST_MODE:
392 if (container.empty() || (id > container.size()))
393 {
394 NS_FATAL_ERROR("No input available!");
395 }
396 else
397 {
398 fileName = container.at(id).first;
399 }
400 break;
401
402 case RANDOM_MODE:
403 if (container.empty())
404 {
405 NS_FATAL_ERROR("No input available!");
406 }
407 else
408 {
409 fileName = container.at(std::rand() % container.size()).first;
410 }
411 break;
412
413 case POSITION_MODE:
414 fileName = FindSourceBasedOnPosition(container, id, mobility);
415 break;
416
417 default:
418 NS_FATAL_ERROR("Not supported mode.");
419 break;
420 }
421
422 NS_LOG_INFO("Creation info: Mode=" << m_utInputMode << ", ID (GW/UT)=" << id
423 << ", FileName=" << fileName);
424
425 // find from loaded list
426
427 TraceInputContainer_t::iterator it = m_loadedTraces.find(fileName);
428
429 if (it == m_loadedTraces.end())
430 {
431 // create if not found
432 trace = Create<SatFadingExternalInputTrace>(fileType, m_dataPath + fileName);
433 }
434 else
435 {
436 trace = it->second;
437 }
438
439 return trace;
440}
441
442std::string
444 uint32_t id,
445 Ptr<MobilityModel> mobility)
446{
447 NS_LOG_FUNCTION(this << mobility);
448
449 std::string fileName;
450 double currentDistanceToFading = std::numeric_limits<double>::max();
451 Vector position = mobility->GetPosition();
452
453 for (TraceFileContainer_t::iterator it = container.begin(); it != container.end(); it++)
454 {
455 Vector fadingPosition = it->second.ToVector();
456
457 double distanceToFading = CalculateDistance(position, fadingPosition);
458
459 if (distanceToFading < currentDistanceToFading)
460 {
461 currentDistanceToFading = distanceToFading;
462 fileName = it->first;
463 }
464 }
465
466 if (currentDistanceToFading > m_maxDistanceToFading)
467 {
468 NS_FATAL_ERROR(
469 "No valid fading based on position (min found distance, max allowed distance): "
470 << currentDistanceToFading << ", " << m_maxDistanceToFading);
471 }
472 else
473 {
474 NS_LOG_INFO("Minimum distance to fading trace source: " << currentDistanceToFading);
475 }
476
477 return fileName;
478}
479
480} // namespace ns3
GeoCoordinate class is used to store and operate with geodetic coordinates.
ChannelType_t
Types of channel.
static Ptr< SatEnvVariables > GetInstance()
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.
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.
virtual void NotifyConstructionCompleted() override
Notifier called once the ObjectBase is fully constructed.
TraceFileContainer_t m_utFwdDownFileNames
UT forward down link trace file names.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.