A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("TraceFadingLossModel");
34 
35 namespace ns3 {
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  .AddConstructor<TraceFadingLossModel> ()
63  .AddAttribute ("TraceFilename",
64  "Name of file to load a trace from.",
65  StringValue (""),
66  MakeStringAccessor (&TraceFadingLossModel::SetTraceFileName),
67  MakeStringChecker ())
68  .AddAttribute ("TraceLength",
69  "The total length of the fading trace (default value 10 s.)",
70  TimeValue (Seconds (10.0)),
71  MakeTimeAccessor (&TraceFadingLossModel::SetTraceLength),
72  MakeTimeChecker ())
73  .AddAttribute ("SamplesNum",
74  "The number of samples the trace is made of (default 10000)",
75  UintegerValue (10000),
76  MakeUintegerAccessor (&TraceFadingLossModel::m_samplesNum),
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)),
81  MakeTimeAccessor (&TraceFadingLossModel::m_windowSize),
82  MakeTimeChecker ())
83  .AddAttribute ("RbNum",
84  "The number of RB the trace is made of (default 100)",
85  UintegerValue (100),
86  MakeUintegerAccessor (&TraceFadingLossModel::m_rbNum),
87  MakeUintegerChecker<uint8_t> ())
88  .AddAttribute ("RngStreamSetSize",
89  "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.",
90  UintegerValue (200000),
91  MakeUintegerAccessor (&TraceFadingLossModel::m_streamSetSize),
92  MakeUintegerChecker<uint64_t> ())
93  ;
94  return tid;
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION (this << "Set Fading Trace " << fileName);
101 
102  m_traceFile = fileName;
103 }
104 
105 void
107 {
108  m_traceLength = t;
109 }
110 
111 void
113 {
114  LoadTrace ();
115 }
116 
117 
118 void
120 {
121  NS_LOG_FUNCTION (this << "Loading Fading Trace " << m_traceFile);
122  std::ifstream ifTraceFile;
123  ifTraceFile.open (m_traceFile.c_str (), 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 
148 
153  Ptr<const MobilityModel> b) const
154 {
155  NS_LOG_FUNCTION (this << *txPsd << a << b);
156 
157  std::map <ChannelRealizationId_t, int >::iterator itOff;
158  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
159  itOff = m_windowOffsetsMap.find (mobilityPair);
160  if (itOff!=m_windowOffsetsMap.end ())
161  {
163  {
164  // update all the offsets
165  NS_LOG_INFO ("Fading Windows Updated");
166  std::map <ChannelRealizationId_t, int >::iterator itOff2;
167  for (itOff2 = m_windowOffsetsMap.begin (); itOff2 != m_windowOffsetsMap.end (); itOff2++)
168  {
169  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
170  itVar = m_startVariableMap.find ((*itOff2).first);
171  (*itOff2).second = (*itVar).second->GetValue ();
172  }
174  }
175  }
176  else
177  {
178  NS_LOG_LOGIC (this << "insert new channel realization, m_windowOffsetMap.size () = " << m_windowOffsetsMap.size ());
179  Ptr<UniformRandomVariable> startV = CreateObject<UniformRandomVariable> ();
180  startV->SetAttribute ("Min", DoubleValue (1.0));
181  startV->SetAttribute ("Max", DoubleValue ((m_traceLength.GetSeconds () - m_windowSize.GetSeconds ()) * 1000.0));
182  if (m_streamsAssigned)
183  {
184  NS_ASSERT_MSG (m_currentStream <= m_lastStream, "not enough streams, consider increasing the StreamSetSize attribute");
185  startV->SetStream (m_currentStream);
186  m_currentStream += 1;
187  }
188  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
189  m_startVariableMap.insert (std::pair<ChannelRealizationId_t,Ptr<UniformRandomVariable> > (mobilityPair, startV));
190  m_windowOffsetsMap.insert (std::pair<ChannelRealizationId_t,int> (mobilityPair, startV->GetValue ()));
191  }
192 
193 
194  Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue> (txPsd);
195  Values::iterator vit = rxPsd->ValuesBegin ();
196 
197  //Vector aSpeedVector = a->GetVelocity ();
198  //Vector bSpeedVector = b->GetVelocity ();
199 
200  //double speed = std::sqrt (std::pow (aSpeedVector.x-bSpeedVector.x,2) + std::pow (aSpeedVector.y-bSpeedVector.y,2));
201 
202  NS_LOG_LOGIC (this << *rxPsd);
203  NS_ASSERT (!m_fadingTrace.empty ());
204  int now_ms = static_cast<int> (Simulator::Now ().GetMilliSeconds () * m_timeGranularity);
205  int lastUpdate_ms = static_cast<int> (m_lastWindowUpdate.GetMilliSeconds () * m_timeGranularity);
206  int index = ((*itOff).second + now_ms - lastUpdate_ms) % m_samplesNum;
207  int subChannel = 0;
208  while (vit != rxPsd->ValuesEnd ())
209  {
210  NS_ASSERT (subChannel < 100);
211  if (*vit != 0.)
212  {
213  double fading = m_fadingTrace.at (subChannel).at (index);
214  NS_LOG_INFO (this << " FADING now " << now_ms << " offset " << (*itOff).second << " id " << index << " fading " << fading);
215  double power = *vit; // in Watt/Hz
216  power = 10 * std::log10 (180000 * power); // in dB
217 
218  NS_LOG_LOGIC (this << subChannel << *vit << power << fading);
219 
220  *vit = std::pow (10., ((power + fading) / 10)) / 180000; // in Watt
221 
222  NS_LOG_LOGIC (this << subChannel << *vit);
223 
224  }
225 
226  ++vit;
227  ++subChannel;
228 
229  }
230 
231  NS_LOG_LOGIC (this << *rxPsd);
232  return rxPsd;
233 }
234 
235 int64_t
237 {
238  NS_LOG_FUNCTION (this << stream);
239  NS_ASSERT (m_streamsAssigned == false);
240  m_streamsAssigned = true;
241  m_currentStream = stream;
242  m_lastStream = stream + m_streamSetSize - 1;
243  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
244  itVar = m_startVariableMap.begin ();
245  // the following loop is for eventually pre-existing ChannelRealization instances
246  // note that more instances are expected to be created at run time
247  while (itVar!=m_startVariableMap.end ())
248  {
249  NS_ASSERT_MSG (m_currentStream <= m_lastStream, "not enough streams, consider increasing the StreamSetSize attribute");
250  (*itVar).second->SetStream (m_currentStream);
251  m_currentStream += 1;
252  }
253  return m_streamSetSize;
254 }
255 
256 
257 
258 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
int64_t AssignStreams(int64_t stream)
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)
hold variables of type string
Definition: string.h:18
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
Values::iterator ValuesEnd()
double GetSeconds(void) const
Definition: nstime.h:272
std::pair< Ptr< const MobilityModel >, Ptr< const MobilityModel > > ChannelRealizationId_t
The couple of mobility mnode that form a fading channel realization.
hold objects of type ns3::Time
Definition: nstime.h:1008
Hold an unsigned integer type.
Definition: uinteger.h:46
std::map< ChannelRealizationId_t, Ptr< UniformRandomVariable > > m_startVariableMap
void SetTraceFileName(std::string fileName)
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
Ptr< SpectrumValue > DoCalcRxPowerSpectralDensity(Ptr< const SpectrumValue > txPsd, Ptr< const MobilityModel > a, Ptr< const MobilityModel > b) const
void SetNext(Ptr< SpectrumPropagationLossModel > next)
used to chain various instances of SpectrumPropagationLossModel
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
#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:84
Values::iterator ValuesBegin()
std::map< ChannelRealizationId_t, int > m_windowOffsetsMap
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:441
spectrum-aware propagation loss model
Hold a floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
a unique identifier for an interface.
Definition: type-id.h:49
int64_t GetMilliSeconds(void) const
Definition: nstime.h:281
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610