A Discrete-Event Network Simulator
API
trace-fading-loss-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Giuseppe Piro <g.piro@poliba.it>
18  * Marco Miozzo <mmiozzo@cttc.es>
19  */
20 
22 
24 #include "spectrum-value.h"
25 
26 #include "ns3/uinteger.h"
27 #include <ns3/double.h>
28 #include <ns3/log.h>
29 #include <ns3/mobility-model.h>
30 #include <ns3/simulator.h>
31 #include <ns3/string.h>
32 
33 #include <fstream>
34 
35 namespace ns3
36 {
37 
38 NS_LOG_COMPONENT_DEFINE("TraceFadingLossModel");
39 
40 NS_OBJECT_ENSURE_REGISTERED(TraceFadingLossModel);
41 
43  : m_streamsAssigned(false)
44 {
45  NS_LOG_FUNCTION(this);
46  SetNext(nullptr);
47 }
48 
50 {
51  m_fadingTrace.clear();
52  m_windowOffsetsMap.clear();
53  m_startVariableMap.clear();
54 }
55 
56 TypeId
58 {
59  static TypeId tid =
60  TypeId("ns3::TraceFadingLossModel")
62  .SetGroupName("Spectrum")
63  .AddConstructor<TraceFadingLossModel>()
64  .AddAttribute("TraceFilename",
65  "Name of file to load a trace from.",
66  StringValue(""),
69  .AddAttribute("TraceLength",
70  "The total length of the fading trace (default value 10 s.)",
71  TimeValue(Seconds(10.0)),
74  .AddAttribute("SamplesNum",
75  "The number of samples the trace is made of (default 10000)",
76  UintegerValue(10000),
78  MakeUintegerChecker<uint32_t>())
79  .AddAttribute("WindowSize",
80  "The size of the window for the fading trace (default value 0.5 s.)",
81  TimeValue(Seconds(0.5)),
84  .AddAttribute("RbNum",
85  "The number of RB the trace is made of (default 100)",
86  UintegerValue(100),
88  MakeUintegerChecker<uint32_t>())
89  .AddAttribute(
90  "RngStreamSetSize",
91  "The number of RNG streams reserved for the fading model. The maximum number of "
92  "streams that are needed for an LTE FDD scenario is 2 * numUEs * numeNBs.",
93  UintegerValue(200000),
95  MakeUintegerChecker<uint64_t>());
96  return tid;
97 }
98 
99 void
101 {
102  NS_LOG_FUNCTION(this << "Set Fading Trace " << fileName);
103 
104  m_traceFile = fileName;
105 }
106 
107 void
109 {
110  m_traceLength = t;
111 }
112 
113 void
115 {
116  LoadTrace();
117 }
118 
119 void
121 {
122  NS_LOG_FUNCTION(this << "Loading Fading Trace " << m_traceFile);
123  std::ifstream ifTraceFile;
124  ifTraceFile.open(m_traceFile, std::ifstream::in);
125  m_fadingTrace.clear();
126  if (!ifTraceFile.good())
127  {
128  NS_LOG_INFO(this << " File: " << m_traceFile);
129  NS_ASSERT_MSG(ifTraceFile.good(), " Fading trace file not found");
130  }
131 
132  // NS_LOG_INFO (this << " length " << m_traceLength.GetSeconds ());
133  // NS_LOG_INFO (this << " RB " << (uint32_t)m_rbNum << " samples " << m_samplesNum);
134  for (uint32_t i = 0; i < m_rbNum; i++)
135  {
136  FadingTraceSample rbTimeFadingTrace;
137  for (uint32_t j = 0; j < m_samplesNum; j++)
138  {
139  double sample;
140  ifTraceFile >> sample;
141  rbTimeFadingTrace.push_back(sample);
142  }
143  m_fadingTrace.push_back(rbTimeFadingTrace);
144  }
147 }
148 
152  Ptr<const MobilityModel> b) const
153 {
154  NS_LOG_FUNCTION(this << *params->psd << a << b);
155 
156  ChannelRealizationId_t mobilityPair = std::make_pair(a, b);
157  auto itOff = m_windowOffsetsMap.find(mobilityPair);
158  if (itOff != m_windowOffsetsMap.end())
159  {
160  if (Simulator::Now().GetSeconds() >=
162  {
163  // update all the offsets
164  NS_LOG_INFO("Fading Windows Updated");
165  for (auto itOff2 = m_windowOffsetsMap.begin(); itOff2 != m_windowOffsetsMap.end();
166  itOff2++)
167  {
168  auto itVar = m_startVariableMap.find((*itOff2).first);
169  (*itOff2).second = (*itVar).second->GetValue();
170  }
172  }
173  }
174  else
175  {
176  NS_LOG_LOGIC(this << "insert new channel realization, m_windowOffsetMap.size () = "
177  << m_windowOffsetsMap.size());
178  Ptr<UniformRandomVariable> startV = CreateObject<UniformRandomVariable>();
179  startV->SetAttribute("Min", DoubleValue(1.0));
180  startV->SetAttribute(
181  "Max",
183  if (m_streamsAssigned)
184  {
186  "not enough streams, consider increasing the StreamSetSize attribute");
187  startV->SetStream(m_currentStream);
188  m_currentStream += 1;
189  }
190  ChannelRealizationId_t mobilityPair = std::make_pair(a, b);
191  m_startVariableMap.insert(
192  std::pair<ChannelRealizationId_t, Ptr<UniformRandomVariable>>(mobilityPair, startV));
193  itOff =
195  .insert(std::pair<ChannelRealizationId_t, int>(mobilityPair, startV->GetValue()))
196  .first;
197  }
198 
199  Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue>(params->psd);
200  auto vit = rxPsd->ValuesBegin();
201 
202  // Vector aSpeedVector = a->GetVelocity ();
203  // Vector bSpeedVector = b->GetVelocity ();
204 
205  // double speed = std::sqrt (std::pow (aSpeedVector.x-bSpeedVector.x,2) + std::pow
206  // (aSpeedVector.y-bSpeedVector.y,2));
207 
208  NS_LOG_LOGIC(this << *rxPsd);
209  NS_ASSERT(!m_fadingTrace.empty());
210  int now_ms = static_cast<int>(Simulator::Now().GetMilliSeconds() * m_timeGranularity);
211  int lastUpdate_ms = static_cast<int>(m_lastWindowUpdate.GetMilliSeconds() * m_timeGranularity);
212  int index = ((*itOff).second + now_ms - lastUpdate_ms) % m_samplesNum;
213  int subChannel = 0;
214  while (vit != rxPsd->ValuesEnd())
215  {
216  NS_ASSERT(subChannel < 100);
217  if (*vit != 0.)
218  {
219  double fading = m_fadingTrace.at(subChannel).at(index);
220  NS_LOG_INFO(this << " FADING now " << now_ms << " offset " << (*itOff).second << " id "
221  << index << " fading " << fading);
222  double power = *vit; // in Watt/Hz
223  power = 10 * std::log10(180000 * power); // in dB
224 
225  NS_LOG_LOGIC(this << subChannel << *vit << power << fading);
226 
227  *vit = std::pow(10., ((power + fading) / 10)) / 180000; // in Watt
228 
229  NS_LOG_LOGIC(this << subChannel << *vit);
230  }
231 
232  ++vit;
233  ++subChannel;
234  }
235 
236  NS_LOG_LOGIC(this << *rxPsd);
237  return rxPsd;
238 }
239 
240 int64_t
242 {
243  NS_LOG_FUNCTION(this << stream);
244  NS_ASSERT(m_streamsAssigned == false);
245  m_streamsAssigned = true;
246  m_currentStream = stream;
247  m_lastStream = stream + m_streamSetSize - 1;
248  auto itVar = m_startVariableMap.begin();
249  // the following loop is for eventually pre-existing ChannelRealization instances
250  // note that more instances are expected to be created at run time
251  while (itVar != m_startVariableMap.end())
252  {
254  "not enough streams, consider increasing the StreamSetSize attribute");
255  (*itVar).second->SetStream(m_currentStream);
256  m_currentStream += 1;
257  }
258  return m_streamSetSize;
259 }
260 
261 } // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:204
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
spectrum-aware propagation loss model
void SetNext(Ptr< SpectrumPropagationLossModel > next)
Used to chain various instances of SpectrumPropagationLossModel.
Values::iterator ValuesBegin()
Values::iterator ValuesEnd()
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetMilliSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:408
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
fading loss model based on precalculated fading traces
uint64_t m_streamSetSize
stream set size
Ptr< SpectrumValue > DoCalcRxPowerSpectralDensity(Ptr< const SpectrumSignalParameters > params, Ptr< const MobilityModel > a, Ptr< const MobilityModel > b) const override
std::string m_traceFile
the trace file name
Time m_lastWindowUpdate
time of last window update
void SetTraceLength(Time t)
Set the trace time.
uint32_t m_samplesNum
number of samples
std::pair< Ptr< const MobilityModel >, Ptr< const MobilityModel > > ChannelRealizationId_t
The couple of mobility node that form a fading channel realization.
int64_t DoAssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
std::vector< double > FadingTraceSample
Vector with fading samples in time domain (for a fixed RB)
uint32_t m_timeGranularity
time granularity
void SetTraceFileName(std::string fileName)
Set the trace file name.
bool m_streamsAssigned
is streams assigned?
static TypeId GetTypeId()
Get the type ID.
std::map< ChannelRealizationId_t, int > m_windowOffsetsMap
windows offsets map
std::map< ChannelRealizationId_t, Ptr< UniformRandomVariable > > m_startVariableMap
start variable map
uint64_t m_currentStream
the current stream
void LoadTrace()
Load trace function.
FadingTrace m_fadingTrace
fading trace
uint64_t m_lastStream
the last stream
void DoInitialize() override
Initialize() implementation.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Definition: string.h:57
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.