A Discrete-Event Network Simulator
API
trace-fading-loss-model.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
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: Giuseppe Piro <g.piro@poliba.it>
19  * Marco Miozzo <mmiozzo@cttc.es>
20  */
21 
22 
23 #include <ns3/trace-fading-loss-model.h>
24 #include <ns3/mobility-model.h>
25 #include <ns3/spectrum-value.h>
26 #include <ns3/log.h>
27 #include <ns3/string.h>
28 #include <ns3/double.h>
29 #include "ns3/uinteger.h"
30 #include <fstream>
31 #include <ns3/simulator.h>
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("TraceFadingLossModel");
36 
37 NS_OBJECT_ENSURE_REGISTERED (TraceFadingLossModel);
38 
39 
40 
42  : m_streamsAssigned (false)
43 {
44  NS_LOG_FUNCTION (this);
45  SetNext (NULL);
46 }
47 
48 
50 {
51  m_fadingTrace.clear ();
52  m_windowOffsetsMap.clear ();
53  m_startVariableMap.clear ();
54 }
55 
56 
57 TypeId
59 {
60  static TypeId tid = 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)),
73  MakeTimeChecker ())
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)),
83  MakeTimeChecker ())
84  .AddAttribute ("RbNum",
85  "The number of RB the trace is made of (default 100)",
86  UintegerValue (100),
88  MakeUintegerChecker<uint8_t> ())
89  .AddAttribute ("RngStreamSetSize",
90  "The number of RNG streams reserved for the fading model. The maximum number of streams that are needed for an LTE FDD scenario is 2 * numUEs * numeNBs.",
91  UintegerValue (200000),
93  MakeUintegerChecker<uint64_t> ())
94  ;
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 
119 void
121 {
122  NS_LOG_FUNCTION (this << "Loading Fading Trace " << m_traceFile);
123  std::ifstream ifTraceFile;
124  ifTraceFile.open (m_traceFile.c_str (), 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 
149 
154  Ptr<const MobilityModel> b) const
155 {
156  NS_LOG_FUNCTION (this << *txPsd << a << b);
157 
158  std::map <ChannelRealizationId_t, int >::iterator itOff;
159  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
160  itOff = m_windowOffsetsMap.find (mobilityPair);
161  if (itOff!=m_windowOffsetsMap.end ())
162  {
163  if (Simulator::Now ().GetSeconds () >= m_lastWindowUpdate.GetSeconds () + m_windowSize.GetSeconds ())
164  {
165  // update all the offsets
166  NS_LOG_INFO ("Fading Windows Updated");
167  std::map <ChannelRealizationId_t, int >::iterator itOff2;
168  for (itOff2 = m_windowOffsetsMap.begin (); itOff2 != m_windowOffsetsMap.end (); itOff2++)
169  {
170  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
171  itVar = m_startVariableMap.find ((*itOff2).first);
172  (*itOff2).second = (*itVar).second->GetValue ();
173  }
175  }
176  }
177  else
178  {
179  NS_LOG_LOGIC (this << "insert new channel realization, m_windowOffsetMap.size () = " << m_windowOffsetsMap.size ());
180  Ptr<UniformRandomVariable> startV = CreateObject<UniformRandomVariable> ();
181  startV->SetAttribute ("Min", DoubleValue (1.0));
182  startV->SetAttribute ("Max", DoubleValue ((m_traceLength.GetSeconds () - m_windowSize.GetSeconds ()) * 1000.0));
183  if (m_streamsAssigned)
184  {
185  NS_ASSERT_MSG (m_currentStream <= m_lastStream, "not enough streams, consider increasing the StreamSetSize attribute");
186  startV->SetStream (m_currentStream);
187  m_currentStream += 1;
188  }
189  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
190  m_startVariableMap.insert (std::pair<ChannelRealizationId_t,Ptr<UniformRandomVariable> > (mobilityPair, startV));
191  itOff = m_windowOffsetsMap.insert (std::pair<ChannelRealizationId_t,int> (mobilityPair, startV->GetValue ())).first;
192  }
193 
194 
195  Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue> (txPsd);
196  Values::iterator vit = rxPsd->ValuesBegin ();
197 
198  //Vector aSpeedVector = a->GetVelocity ();
199  //Vector bSpeedVector = b->GetVelocity ();
200 
201  //double speed = std::sqrt (std::pow (aSpeedVector.x-bSpeedVector.x,2) + std::pow (aSpeedVector.y-bSpeedVector.y,2));
202 
203  NS_LOG_LOGIC (this << *rxPsd);
204  NS_ASSERT (!m_fadingTrace.empty ());
205  int now_ms = static_cast<int> (Simulator::Now ().GetMilliSeconds () * m_timeGranularity);
206  int lastUpdate_ms = static_cast<int> (m_lastWindowUpdate.GetMilliSeconds () * m_timeGranularity);
207  int index = ((*itOff).second + now_ms - lastUpdate_ms) % m_samplesNum;
208  int subChannel = 0;
209  while (vit != rxPsd->ValuesEnd ())
210  {
211  NS_ASSERT (subChannel < 100);
212  if (*vit != 0.)
213  {
214  double fading = m_fadingTrace.at (subChannel).at (index);
215  NS_LOG_INFO (this << " FADING now " << now_ms << " offset " << (*itOff).second << " id " << index << " fading " << fading);
216  double power = *vit; // in Watt/Hz
217  power = 10 * std::log10 (180000 * power); // in dB
218 
219  NS_LOG_LOGIC (this << subChannel << *vit << power << fading);
220 
221  *vit = std::pow (10., ((power + fading) / 10)) / 180000; // in Watt
222 
223  NS_LOG_LOGIC (this << subChannel << *vit);
224 
225  }
226 
227  ++vit;
228  ++subChannel;
229 
230  }
231 
232  NS_LOG_LOGIC (this << *rxPsd);
233  return rxPsd;
234 }
235 
236 int64_t
238 {
239  NS_LOG_FUNCTION (this << stream);
240  NS_ASSERT (m_streamsAssigned == false);
241  m_streamsAssigned = true;
242  m_currentStream = stream;
243  m_lastStream = stream + m_streamSetSize - 1;
244  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
245  itVar = m_startVariableMap.begin ();
246  // the following loop is for eventually pre-existing ChannelRealization instances
247  // note that more instances are expected to be created at run time
248  while (itVar!=m_startVariableMap.end ())
249  {
250  NS_ASSERT_MSG (m_currentStream <= m_lastStream, "not enough streams, consider increasing the StreamSetSize attribute");
251  (*itVar).second->SetStream (m_currentStream);
252  m_currentStream += 1;
253  }
254  return m_streamSetSize;
255 }
256 
257 
258 
259 } // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
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:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:383
AttributeValue implementation for Time.
Definition: nstime.h:1308
fading loss model based on precalculated fading traces
uint64_t m_streamSetSize
stream set size
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)
std::map< ChannelRealizationId_t, int > m_windowOffsetsMap
windows offsets map
Ptr< SpectrumValue > DoCalcRxPowerSpectralDensity(Ptr< const SpectrumValue > txPsd, Ptr< const MobilityModel > a, Ptr< const MobilityModel > b) const
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, 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
virtual void DoInitialize(void)
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:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#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:88
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:42
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
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:1309
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:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
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:522