A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trace-fading-loss-model.cc
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 TraceFadingLossModel::TraceFadingLossModel ()
42 {
43  NS_LOG_FUNCTION (this);
44  SetNext (NULL);
45 }
46 
47 
48 TraceFadingLossModel::~TraceFadingLossModel ()
49 {
50  m_fadingTrace.clear ();
51  m_windowOffsetsMap.clear ();
52  m_startVariableMap.clear ();
53 }
54 
55 
56 TypeId
57 TraceFadingLossModel::GetTypeId (void)
58 {
59  static TypeId tid = TypeId ("ns3::TraceFadingLossModel")
60  .SetParent<SpectrumPropagationLossModel> ()
61  .AddConstructor<TraceFadingLossModel> ()
62  .AddAttribute ("TraceFilename",
63  "Name of file to load a trace from.",
64  StringValue (""),
65  MakeStringAccessor (&TraceFadingLossModel::SetTraceFileName),
66  MakeStringChecker ())
67  .AddAttribute ("TraceLength",
68  "The total length of the fading trace (default value 10 s.)",
69  TimeValue (Seconds (10.0)),
70  MakeTimeAccessor (&TraceFadingLossModel::SetTraceLength),
71  MakeTimeChecker ())
72  .AddAttribute ("SamplesNum",
73  "The number of samples the trace is made of (default 10000)",
74  UintegerValue (10000),
75  MakeUintegerAccessor (&TraceFadingLossModel::m_samplesNum),
76  MakeUintegerChecker<uint32_t> ())
77  .AddAttribute ("WindowSize",
78  "The size of the window for the fading trace (default value 0.5 s.)",
79  TimeValue (Seconds (0.5)),
80  MakeTimeAccessor (&TraceFadingLossModel::m_windowSize),
81  MakeTimeChecker ())
82  .AddAttribute ("RbNum",
83  "The number of RB the trace is made of (default 100)",
84  UintegerValue (100),
85  MakeUintegerAccessor (&TraceFadingLossModel::m_rbNum),
86  MakeUintegerChecker<uint8_t> ())
87  ;
88  return tid;
89 }
90 
91 void
92 TraceFadingLossModel::SetTraceFileName (std::string fileName)
93 {
94  NS_LOG_FUNCTION (this << "Set Fading Trace " << fileName);
95 
96  m_traceFile = fileName;
97 }
98 
99 void
100 TraceFadingLossModel::SetTraceLength (Time t)
101 {
102  m_traceLength = t;
103 }
104 
105 void
107 {
108  LoadTrace ();
109 }
110 
111 
112 void
113 TraceFadingLossModel::LoadTrace ()
114 {
115  NS_LOG_FUNCTION (this << "Loading Fading Trace " << m_traceFile);
116  std::ifstream ifTraceFile;
117  ifTraceFile.open (m_traceFile.c_str (), std::ifstream::in);
118  m_fadingTrace.clear ();
119  if (!ifTraceFile.good ())
120  {
121  NS_LOG_INFO (this << " File: " << m_traceFile);
122  NS_ASSERT_MSG(ifTraceFile.good (), " Fading trace file not found");
123  }
124 
125 // NS_LOG_INFO (this << " length " << m_traceLength.GetSeconds ());
126 // NS_LOG_INFO (this << " RB " << (uint32_t)m_rbNum << " samples " << m_samplesNum);
127  for (uint32_t i = 0; i < m_rbNum; i++)
128  {
129  FadingTraceSample rbTimeFadingTrace;
130  for (uint32_t j = 0; j < m_samplesNum; j++)
131  {
132  double sample;
133  ifTraceFile >> sample;
134  rbTimeFadingTrace.push_back (sample);
135  }
136  m_fadingTrace.push_back (rbTimeFadingTrace);
137  }
138  m_timeGranularity = m_traceLength.GetMilliSeconds () / m_samplesNum;
139  m_lastWindowUpdate = Simulator::Now ();
140 }
141 
142 
143 Ptr<SpectrumValue>
147  Ptr<const MobilityModel> b) const
148 {
149  NS_LOG_FUNCTION (this << *txPsd << a << b);
150 
151  std::map <ChannelRealizationId_t, int >::iterator itOff;
152  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
153  itOff = m_windowOffsetsMap.find (mobilityPair);
154  if (itOff!=m_windowOffsetsMap.end ())
155  {
156  if (Simulator::Now ().GetSeconds () >= m_lastWindowUpdate.GetSeconds () + m_windowSize.GetSeconds ())
157  {
158  // update all the offsets
159  NS_LOG_INFO ("Fading Windows Updated");
160  std::map <ChannelRealizationId_t, int >::iterator itOff2;
161  for (itOff2 = m_windowOffsetsMap.begin (); itOff2 != m_windowOffsetsMap.end (); itOff2++)
162  {
163  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
164  itVar = m_startVariableMap.find ((*itOff2).first);
165  (*itOff2).second = (*itVar).second->GetValue ();
166  }
167  m_lastWindowUpdate = Simulator::Now ();
168  }
169  }
170  else
171  {
172  NS_LOG_LOGIC (this << "insert new channel realization, m_windowOffsetMap.size () = " << m_windowOffsetsMap.size ());
173  Ptr<UniformRandomVariable> startV = CreateObject<UniformRandomVariable> ();
174  startV->SetAttribute ("Min", DoubleValue (1.0));
175  startV->SetAttribute ("Max", DoubleValue ((m_traceLength.GetSeconds () - m_windowSize.GetSeconds ()) * 1000.0));
176  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
177  m_startVariableMap.insert (std::pair<ChannelRealizationId_t,Ptr<UniformRandomVariable> > (mobilityPair, startV));
178  m_windowOffsetsMap.insert (std::pair<ChannelRealizationId_t,int> (mobilityPair, startV->GetValue ()));
179  }
180 
181 
182  Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue> (txPsd);
183  Values::iterator vit = rxPsd->ValuesBegin ();
184 
185  //Vector aSpeedVector = a->GetVelocity ();
186  //Vector bSpeedVector = b->GetVelocity ();
187 
188  //double speed = std::sqrt (std::pow (aSpeedVector.x-bSpeedVector.x,2) + std::pow (aSpeedVector.y-bSpeedVector.y,2));
189 
190  NS_LOG_LOGIC (this << *rxPsd);
191  NS_ASSERT (!m_fadingTrace.empty ());
192  int now_ms = static_cast<int> (Simulator::Now ().GetMilliSeconds () * m_timeGranularity);
193  int lastUpdate_ms = static_cast<int> (m_lastWindowUpdate.GetMilliSeconds () * m_timeGranularity);
194  int index = ((*itOff).second + now_ms - lastUpdate_ms) % m_samplesNum;
195  int subChannel = 0;
196  while (vit != rxPsd->ValuesEnd ())
197  {
198  NS_ASSERT (subChannel < 100);
199  if (*vit != 0.)
200  {
201  double fading = m_fadingTrace.at (subChannel).at (index);
202  NS_LOG_INFO (this << " FADING now " << now_ms << " offset " << (*itOff).second << " id " << index << " fading " << fading);
203  double power = *vit; // in Watt/Hz
204  power = 10 * std::log10 (180000 * power); // in dB
205 
206  NS_LOG_LOGIC (this << subChannel << *vit << power << fading);
207 
208  *vit = std::pow (10., ((power + fading) / 10)) / 180000; // in Watt
209 
210  NS_LOG_LOGIC (this << subChannel << *vit);
211 
212  }
213 
214  ++vit;
215  ++subChannel;
216 
217  }
218 
219  NS_LOG_LOGIC (this << *rxPsd);
220  return rxPsd;
221 }
222 
223 int64_t
225 {
226  NS_LOG_FUNCTION (this << stream);
227  int64_t currentStream = stream;
228  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
229  itVar = m_startVariableMap.begin ();
230  while (itVar!=m_startVariableMap.end ())
231  {
232  (*itVar).second->SetStream (currentStream);
233  currentStream += 1;
234  }
235  return (currentStream - stream);
236 }
237 
238 
239 
240 } // namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
int64_t AssignStreams(int64_t stream)
std::vector< double > FadingTraceSample
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_INFO(msg)
Definition: log.h:264
double GetSeconds(void) const
Definition: nstime.h:262
std::pair< Ptr< const MobilityModel >, Ptr< const MobilityModel > > ChannelRealizationId_t
The couple of mobility mnode that form a fading channel realization.
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
Ptr< SpectrumValue > DoCalcRxPowerSpectralDensity(Ptr< const SpectrumValue > txPsd, Ptr< const MobilityModel > a, Ptr< const MobilityModel > b) const
void SetNext(Ptr< SpectrumPropagationLossModel > next)
static Time Now(void)
Definition: simulator.cc:179
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Hold an floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
int64_t GetMilliSeconds(void) const
Definition: nstime.h:271