A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
TraceFadingLossModel::TraceFadingLossModel
()
42
: m_streamsAssigned (false)
43
{
44
NS_LOG_FUNCTION
(
this
);
45
SetNext
(NULL);
46
}
47
48
49
TraceFadingLossModel::~TraceFadingLossModel
()
50
{
51
m_fadingTrace
.clear ();
52
m_windowOffsetsMap
.clear ();
53
m_startVariableMap
.clear ();
54
}
55
56
57
TypeId
58
TraceFadingLossModel::GetTypeId
(
void
)
59
{
60
static
TypeId
tid =
TypeId
(
"ns3::TraceFadingLossModel"
)
61
.
SetParent
<
SpectrumPropagationLossModel
> ()
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
98
TraceFadingLossModel::SetTraceFileName
(std::string fileName)
99
{
100
NS_LOG_FUNCTION
(
this
<<
"Set Fading Trace "
<< fileName);
101
102
m_traceFile
= fileName;
103
}
104
105
void
106
TraceFadingLossModel::SetTraceLength
(
Time
t)
107
{
108
m_traceLength
= t;
109
}
110
111
void
112
TraceFadingLossModel::DoInitialize
()
113
{
114
LoadTrace
();
115
}
116
117
118
void
119
TraceFadingLossModel::LoadTrace
()
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
}
144
m_timeGranularity
=
m_traceLength
.
GetMilliSeconds
() /
m_samplesNum
;
145
m_lastWindowUpdate
=
Simulator::Now
();
146
}
147
148
149
Ptr<SpectrumValue>
150
TraceFadingLossModel::DoCalcRxPowerSpectralDensity
(
151
Ptr<const SpectrumValue>
txPsd,
152
Ptr<const MobilityModel>
a,
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
{
162
if
(
Simulator::Now
().
GetSeconds
() >=
m_lastWindowUpdate
.
GetSeconds
() +
m_windowSize
.
GetSeconds
())
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
}
173
m_lastWindowUpdate
=
Simulator::Now
();
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
236
TraceFadingLossModel::AssignStreams
(int64_t stream)
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
src
lte
model
trace-fading-loss-model.cc
Generated on Fri Aug 30 2013 01:42:56 for ns-3 by
1.8.1.2