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 
41 
43  : m_streamsAssigned (false)
44 {
45  NS_LOG_FUNCTION (this);
46  SetNext (NULL);
47 }
48 
49 
51 {
52  m_fadingTrace.clear ();
53  m_windowOffsetsMap.clear ();
54  m_startVariableMap.clear ();
55 }
56 
57 
58 TypeId
60 {
61  static TypeId tid = TypeId ("ns3::TraceFadingLossModel")
63  .AddConstructor<TraceFadingLossModel> ()
64  .AddAttribute ("TraceFilename",
65  "Name of file to load a trace from.",
66  StringValue (""),
67  MakeStringAccessor (&TraceFadingLossModel::SetTraceFileName),
68  MakeStringChecker ())
69  .AddAttribute ("TraceLength",
70  "The total length of the fading trace (default value 10 s.)",
71  TimeValue (Seconds (10.0)),
72  MakeTimeAccessor (&TraceFadingLossModel::SetTraceLength),
73  MakeTimeChecker ())
74  .AddAttribute ("SamplesNum",
75  "The number of samples the trace is made of (default 10000)",
76  UintegerValue (10000),
77  MakeUintegerAccessor (&TraceFadingLossModel::m_samplesNum),
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)),
82  MakeTimeAccessor (&TraceFadingLossModel::m_windowSize),
83  MakeTimeChecker ())
84  .AddAttribute ("RbNum",
85  "The number of RB the trace is made of (default 100)",
86  UintegerValue (100),
87  MakeUintegerAccessor (&TraceFadingLossModel::m_rbNum),
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),
92  MakeUintegerAccessor (&TraceFadingLossModel::m_streamSetSize),
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  {
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  m_windowOffsetsMap.insert (std::pair<ChannelRealizationId_t,int> (mobilityPair, startV->GetValue ()));
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
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
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:19
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
#define NS_LOG_INFO(msg)
Definition: log.h:298
double GetSeconds(void) const
Definition: nstime.h:274
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:961
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)
Definition: log.h:368
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)
Definition: assert.h:86
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:452
spectrum-aware propagation loss model
NS_LOG_COMPONENT_DEFINE("TraceFadingLossModel")
Hold a floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
a unique identifier for an interface.
Definition: type-id.h:49
int64_t GetMilliSeconds(void) const
Definition: nstime.h:283
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611