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