satellite-fading-external-input-trace.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 "satellite-utils.h"
24 
25 #include <ns3/log.h>
26 #include <ns3/simulator.h>
27 
28 #include <algorithm>
29 #include <cmath>
30 #include <fstream>
31 #include <ios>
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 NS_LOG_COMPONENT_DEFINE("SatFadingExternalInputTrace");
37 
38 namespace ns3
39 {
40 
42  : m_traceFileType(),
43  m_startTime(),
44  m_timeInterval()
45 {
46  NS_FATAL_ERROR(
47  "SatFadingExternalInputTrace::SatFadingExternalInputTrace - Constructor not in use");
48 }
49 
51  : m_startTime(-1.0),
52  m_timeInterval(-1.0)
53 {
54  NS_LOG_FUNCTION(this);
55 
56  m_traceFileType = type;
57  ReadTrace(fileName);
58 }
59 
61 {
62  NS_LOG_FUNCTION(this);
63 }
64 
65 void
66 SatFadingExternalInputTrace::ReadTrace(std::string filePathName)
67 {
68  NS_LOG_FUNCTION(this << filePathName);
69 
70  // READ FROM THE SPECIFIED INPUT FILE
71  std::ifstream* ifs = new std::ifstream(filePathName.c_str(), std::ios::in | std::ios::binary);
72 
73  if (!ifs->is_open())
74  {
75  // script might be launched by test.py, try a different base path
76  delete ifs;
77  filePathName = "../../" + filePathName;
78  ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
79 
80  if (!ifs->is_open())
81  {
82  NS_FATAL_ERROR("The file " << filePathName << " is not found.");
83  }
84  }
85 
86  // Currently supports two or three column formats
87  uint32_t columns = (m_traceFileType == FT_TWO_COLUMN) ? 2 : 3;
88 
89  float temp;
90  int32_t count(0);
91  std::vector<float> values;
92  ifs->read((char*)&temp, sizeof(float));
93  m_startTime = temp;
94 
95  // While state is good
96  while (ifs->good())
97  {
98  // Store previous value
99  values.push_back(temp);
100 
101  // Read the new value
102  ++count;
103  ifs->read((char*)&temp, sizeof(float));
104 
105  // Handle previous value
106  if (count % columns == 0)
107  {
108  NS_ASSERT(values.size() == columns);
109 
110  m_traceVector.push_back(values);
111  values.clear();
112 
113  // Calculate the sampling interval
114  if (m_timeInterval < 0.0)
115  {
116  m_timeInterval = temp - m_startTime;
117  }
118  }
119  }
120  ifs->close();
121  delete ifs;
122 }
123 
124 double
126 {
127  NS_LOG_FUNCTION(this);
128  NS_ASSERT(!m_traceVector.empty());
129 
130  float simTime = Simulator::Now().GetSeconds();
131 
132  if (simTime < m_startTime)
133  {
134  NS_LOG_ERROR(this << " requested time is smaller than the minimum time value!");
135  }
136 
137  // Calculate the index to the time sample just before current time
138  uint32_t lowerIndex = (uint32_t)(std::floor(std::abs(simTime - m_startTime) / m_timeInterval));
139 
140  if (lowerIndex >= m_traceVector.size())
141  {
142  NS_LOG_ERROR(this << " calculated index exceeds trace file size!");
143  }
144 
145  float lowerKey = m_traceVector.at(lowerIndex).at(TIME_INDEX);
146  float upperKey = m_traceVector.at(lowerIndex + 1).at(TIME_INDEX);
147 
148  // Interpolation in linear domain
149  float lowerVal = SatUtils::DbToLinear(m_traceVector.at(lowerIndex).at(FADING_INDEX));
150  float upperVal = SatUtils::DbToLinear(m_traceVector.at(lowerIndex + 1).at(FADING_INDEX));
151 
152  // y = y0 + (y1 - y0) * (x - x0) / (x1 - x0)
153  double fading = lowerVal + (upperVal - lowerVal) * (simTime - lowerKey) / (upperKey - lowerKey);
154 
155  /*
156  std::cout << "Now: " << simTime <<
157  ", interpolated fading: " << fading_dB <<
158  ", interpolated fading: " << SatUtils::DbToLinear (fading_dB) <<
159  ", Lower time: " << lowerKey <<
160  ", Upper time: " << upperKey <<
161  ", Lower fading: " << lowerVal <<
162  ", Upper fading: " << upperVal << std::endl;
163  */
164  return fading;
165 }
166 
167 bool
169 {
170  NS_LOG_FUNCTION(this);
171  NS_ASSERT(!m_traceVector.empty());
172 
173  std::vector<std::vector<float>>::const_iterator cit;
174  float prevTime(-1.0);
175  float currTime(-1.0);
176 
177  for (cit = m_traceVector.begin(); cit != m_traceVector.end(); ++cit)
178  {
179  if (prevTime > 0)
180  {
181  currTime = cit->at(TIME_INDEX);
182  double diff = std::abs(std::abs(currTime - prevTime) - m_timeInterval);
183 
184  // Test that the the time samples are from constant interval and
185  // the time samples are always increasing.
186  if (diff > 0.0003 || currTime < prevTime)
187  {
188  return false;
189  }
190  }
191  prevTime = cit->at(TIME_INDEX);
192  }
193 
194  // Succeeded
195  return true;
196 }
197 
198 } // namespace ns3
bool TestFadingTrace() const
A method to test that the fading trace is according to assumptions.
void ReadTrace(std::string filePathName)
Read the fading trace from a binary file.
static const uint32_t TIME_INDEX
Constant indices used in the fading container.
float m_startTime
Fading start time and interval calculated from the actual trace file.
std::vector< std::vector< float > > m_traceVector
Container for the fading trace.
~SatFadingExternalInputTrace()
Destructor for SatFadingExternalInputTrace.
double GetFading() const
Get the current fading value for this specific fading file.
TraceFileType_e m_traceFileType
There may be different fading file types.
static T DbToLinear(T db)
Converts decibels to linear.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.