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 {
43  NS_LOG_FUNCTION (this);
44  SetNext (NULL);
45 }
46 
47 
49 {
50  m_fadingTrace.clear ();
51  m_windowOffsetsMap.clear ();
52  m_startVariableMap.clear ();
53 }
54 
55 
56 TypeId
58 {
59  static TypeId tid = TypeId ("ns3::TraceFadingLossModel")
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
93 {
94  NS_LOG_FUNCTION (this << "Set Fading Trace " << fileName);
95 
96  m_traceFile = fileName;
97 }
98 
99 void
101 {
102  m_traceLength = t;
103 }
104 
105 void
107 {
108  LoadTrace ();
109 }
110 
111 
112 void
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  }
140 }
141 
142 
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  {
157  {
158  NS_LOG_INFO ("Fading Window Updated");
159  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
160 
161  itVar = m_startVariableMap.find (mobilityPair);
162  (*itOff).second = (*itVar).second->GetValue ();
163 
165  }
166  }
167  else
168  {
169  NS_LOG_LOGIC (this << "insert new channel realization, m_windowOffsetMap.size () = " << m_windowOffsetsMap.size ());
170  Ptr<UniformRandomVariable> startV = CreateObject<UniformRandomVariable> ();
171  startV->SetAttribute ("Min", DoubleValue (1.0));
172  startV->SetAttribute ("Max", DoubleValue ((m_traceLength.GetSeconds () - m_windowSize.GetSeconds ()) * 1000.0));
173  ChannelRealizationId_t mobilityPair = std::make_pair (a,b);
174  m_startVariableMap.insert (std::pair<ChannelRealizationId_t,Ptr<UniformRandomVariable> > (mobilityPair, startV));
175  m_windowOffsetsMap.insert (std::pair<ChannelRealizationId_t,int> (mobilityPair, startV->GetValue ()));
176  }
177 
178 
179  Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue> (txPsd);
180  Values::iterator vit = rxPsd->ValuesBegin ();
181 
182  //Vector aSpeedVector = a->GetVelocity ();
183  //Vector bSpeedVector = b->GetVelocity ();
184 
185  //double speed = sqrt (pow (aSpeedVector.x-bSpeedVector.x,2) + pow (aSpeedVector.y-bSpeedVector.y,2));
186 
187  NS_LOG_LOGIC (this << *rxPsd);
188  NS_ASSERT (!m_fadingTrace.empty ());
189  int now_ms = static_cast<int> (Simulator::Now ().GetMilliSeconds () * m_timeGranularity);
190  int lastUpdate_ms = static_cast<int> (m_lastWindowUpdate.GetMilliSeconds () * m_timeGranularity);
191  int index = (*itOff).second + now_ms - lastUpdate_ms;
192  int subChannel = 0;
193  while (vit != rxPsd->ValuesEnd ())
194  {
195  NS_ASSERT (subChannel < 100);
196  if (*vit != 0.)
197  {
198  double fading = m_fadingTrace.at (subChannel).at (index);
199  //NS_LOG_INFO (this << " offset " << (*itOff).second << " fading " << fading);
200  double power = *vit; // in Watt/Hz
201  power = 10 * log10 (180000 * power); // in dB
202 
203  NS_LOG_LOGIC (this << subChannel << *vit << power << fading);
204 
205  *vit = pow (10., ((power + fading) / 10)) / 180000; // in Watt
206 
207  NS_LOG_LOGIC (this << subChannel << *vit);
208 
209  }
210 
211  ++vit;
212  ++subChannel;
213 
214  }
215 
216  NS_LOG_LOGIC (this << *rxPsd);
217  return rxPsd;
218 }
219 
220 int64_t
222 {
223  NS_LOG_FUNCTION (this << stream);
224  int64_t currentStream = stream;
225  std::map <ChannelRealizationId_t, Ptr<UniformRandomVariable> >::iterator itVar;
226  itVar = m_startVariableMap.begin ();
227  while (itVar!=m_startVariableMap.end ())
228  {
229  (*itVar).second->SetStream (currentStream);
230  currentStream += 1;
231  }
232  return (currentStream - stream);
233 }
234 
235 
236 
237 } // namespace ns3