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 
23 #include "ns3/uinteger.h"
24 #include <ns3/double.h>
25 #include <ns3/log.h>
26 #include <ns3/mobility-model.h>
27 #include <ns3/simulator.h>
28 #include <ns3/spectrum-value.h>
29 #include <ns3/string.h>
30 #include <ns3/trace-fading-loss-model.h>
31 
32 #include <fstream>
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE("TraceFadingLossModel");
38 
39 NS_OBJECT_ENSURE_REGISTERED(TraceFadingLossModel);
40 
42  : m_streamsAssigned(false)
43 {
44  NS_LOG_FUNCTION(this);
45  SetNext(nullptr);
46 }
47 
49 {
50  m_fadingTrace.clear();
51  m_windowOffsetsMap.clear();
52  m_startVariableMap.clear();
53 }
54 
55 TypeId
57 {
58  static TypeId tid =
59  TypeId("ns3::TraceFadingLossModel")
61  .SetGroupName("Spectrum")
62  .AddConstructor<TraceFadingLossModel>()
63  .AddAttribute("TraceFilename",
64  "Name of file to load a trace from.",
65  StringValue(""),
68  .AddAttribute("TraceLength",
69  "The total length of the fading trace (default value 10 s.)",
70  TimeValue(Seconds(10.0)),
73  .AddAttribute("SamplesNum",
74  "The number of samples the trace is made of (default 10000)",
75  UintegerValue(10000),
77  MakeUintegerChecker<uint32_t>())
78  .AddAttribute("WindowSize",
79  "The size of the window for the fading trace (default value 0.5 s.)",
80  TimeValue(Seconds(0.5)),
83  .AddAttribute("RbNum",
84  "The number of RB the trace is made of (default 100)",
85  UintegerValue(100),
87  MakeUintegerChecker<uint8_t>())
88  .AddAttribute(
89  "RngStreamSetSize",
90  "The number of RNG streams reserved for the fading model. The maximum number of "
91  "streams that are needed for an LTE FDD scenario is 2 * numUEs * numeNBs.",
92  UintegerValue(200000),
94  MakeUintegerChecker<uint64_t>());
95  return tid;
96 }
97 
98 void
100 {
101  NS_LOG_FUNCTION(this << "Set Fading Trace " << fileName);
102 
103  m_traceFile = fileName;
104 }
105 
106 void
108 {
109  m_traceLength = t;
110 }
111 
112 void
114 {
115  LoadTrace();
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION(this << "Loading Fading Trace " << m_traceFile);
122  std::ifstream ifTraceFile;
123  ifTraceFile.open(m_traceFile, std::ifstream::in);
124  m_fadingTrace.clear();
125  if (!ifTraceFile.good())
126  {
127  NS_LOG_INFO(this << " File: " << m_traceFile);
128  NS_ASSERT_MSG(ifTraceFile.good(), " Fading trace file not found");
129  }
130 
131  // NS_LOG_INFO (this << " length " << m_traceLength.GetSeconds ());
132  // NS_LOG_INFO (this << " RB " << (uint32_t)m_rbNum << " samples " << m_samplesNum);
133  for (uint32_t i = 0; i < m_rbNum; i++)
134  {
135  FadingTraceSample rbTimeFadingTrace;
136  for (uint32_t j = 0; j < m_samplesNum; j++)
137  {
138  double sample;
139  ifTraceFile >> sample;
140  rbTimeFadingTrace.push_back(sample);
141  }
142  m_fadingTrace.push_back(rbTimeFadingTrace);
143  }
146 }
147 
151  Ptr<const MobilityModel> b) const
152 {
153  NS_LOG_FUNCTION(this << *params->psd << a << b);
154 
155  std::map<ChannelRealizationId_t, int>::iterator itOff;
156  ChannelRealizationId_t mobilityPair = std::make_pair(a, b);
157  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  std::map<ChannelRealizationId_t, int>::iterator itOff2;
166  for (itOff2 = m_windowOffsetsMap.begin(); itOff2 != m_windowOffsetsMap.end(); itOff2++)
167  {
168  std::map<ChannelRealizationId_t, Ptr<UniformRandomVariable>>::iterator itVar;
169  itVar = m_startVariableMap.find((*itOff2).first);
170  (*itOff2).second = (*itVar).second->GetValue();
171  }
173  }
174  }
175  else
176  {
177  NS_LOG_LOGIC(this << "insert new channel realization, m_windowOffsetMap.size () = "
178  << m_windowOffsetsMap.size());
179  Ptr<UniformRandomVariable> startV = CreateObject<UniformRandomVariable>();
180  startV->SetAttribute("Min", DoubleValue(1.0));
181  startV->SetAttribute(
182  "Max",
184  if (m_streamsAssigned)
185  {
187  "not enough streams, consider increasing the StreamSetSize attribute");
188  startV->SetStream(m_currentStream);
189  m_currentStream += 1;
190  }
191  ChannelRealizationId_t mobilityPair = std::make_pair(a, b);
192  m_startVariableMap.insert(
193  std::pair<ChannelRealizationId_t, Ptr<UniformRandomVariable>>(mobilityPair, startV));
194  itOff =
196  .insert(std::pair<ChannelRealizationId_t, int>(mobilityPair, startV->GetValue()))
197  .first;
198  }
199 
200  Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue>(params->psd);
201  Values::iterator vit = rxPsd->ValuesBegin();
202 
203  // Vector aSpeedVector = a->GetVelocity ();
204  // Vector bSpeedVector = b->GetVelocity ();
205 
206  // double speed = std::sqrt (std::pow (aSpeedVector.x-bSpeedVector.x,2) + std::pow
207  // (aSpeedVector.y-bSpeedVector.y,2));
208 
209  NS_LOG_LOGIC(this << *rxPsd);
210  NS_ASSERT(!m_fadingTrace.empty());
211  int now_ms = static_cast<int>(Simulator::Now().GetMilliSeconds() * m_timeGranularity);
212  int lastUpdate_ms = static_cast<int>(m_lastWindowUpdate.GetMilliSeconds() * m_timeGranularity);
213  int index = ((*itOff).second + now_ms - lastUpdate_ms) % m_samplesNum;
214  int subChannel = 0;
215  while (vit != rxPsd->ValuesEnd())
216  {
217  NS_ASSERT(subChannel < 100);
218  if (*vit != 0.)
219  {
220  double fading = m_fadingTrace.at(subChannel).at(index);
221  NS_LOG_INFO(this << " FADING now " << now_ms << " offset " << (*itOff).second << " id "
222  << index << " fading " << fading);
223  double power = *vit; // in Watt/Hz
224  power = 10 * std::log10(180000 * power); // in dB
225 
226  NS_LOG_LOGIC(this << subChannel << *vit << power << fading);
227 
228  *vit = std::pow(10., ((power + fading) / 10)) / 180000; // in Watt
229 
230  NS_LOG_LOGIC(this << subChannel << *vit);
231  }
232 
233  ++vit;
234  ++subChannel;
235  }
236 
237  NS_LOG_LOGIC(this << *rxPsd);
238  return rxPsd;
239 }
240 
241 int64_t
243 {
244  NS_LOG_FUNCTION(this << stream);
245  NS_ASSERT(m_streamsAssigned == false);
246  m_streamsAssigned = true;
247  m_currentStream = stream;
248  m_lastStream = stream + m_streamSetSize - 1;
249  std::map<ChannelRealizationId_t, Ptr<UniformRandomVariable>>::iterator itVar;
250  itVar = m_startVariableMap.begin();
251  // the following loop is for eventually pre-existing ChannelRealization instances
252  // note that more instances are expected to be created at run time
253  while (itVar != m_startVariableMap.end())
254  {
256  "not enough streams, consider increasing the StreamSetSize attribute");
257  (*itVar).second->SetStream(m_currentStream);
258  m_currentStream += 1;
259  }
260  return m_streamSetSize;
261 }
262 
263 } // 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:200
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
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:407
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
AttributeValue implementation for Time.
Definition: nstime.h:1423
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
uint8_t m_timeGranularity
time granularity
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.
std::vector< double > FadingTraceSample
Vector with fading samples in time domain (for a fixed RB)
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.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
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
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: string.h:57
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1424
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:46
#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:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.