A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
trace-fading-loss-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Giuseppe Piro <g.piro@poliba.it>
7 * Marco Miozzo <mmiozzo@cttc.es>
8 */
9
11
13#include "spectrum-value.h"
14
15#include "ns3/uinteger.h"
16#include <ns3/double.h>
17#include <ns3/log.h>
18#include <ns3/mobility-model.h>
19#include <ns3/simulator.h>
20#include <ns3/string.h>
21
22#include <fstream>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("TraceFadingLossModel");
28
29NS_OBJECT_ENSURE_REGISTERED(TraceFadingLossModel);
30
32 : m_streamsAssigned(false)
33{
34 NS_LOG_FUNCTION(this);
35 SetNext(nullptr);
36}
37
44
47{
48 static TypeId tid =
49 TypeId("ns3::TraceFadingLossModel")
51 .SetGroupName("Spectrum")
52 .AddConstructor<TraceFadingLossModel>()
53 .AddAttribute("TraceFilename",
54 "Name of file to load a trace from.",
55 StringValue(""),
58 .AddAttribute("TraceLength",
59 "The total length of the fading trace (default value 10 s.)",
60 TimeValue(Seconds(10.0)),
63 .AddAttribute("SamplesNum",
64 "The number of samples the trace is made of (default 10000)",
65 UintegerValue(10000),
68 .AddAttribute("WindowSize",
69 "The size of the window for the fading trace (default value 0.5 s.)",
70 TimeValue(Seconds(0.5)),
73 .AddAttribute("RbNum",
74 "The number of RB the trace is made of (default 100)",
75 UintegerValue(100),
78 .AddAttribute(
79 "RngStreamSetSize",
80 "The number of RNG streams reserved for the fading model. The maximum number of "
81 "streams that are needed for an LTE FDD scenario is 2 * numUEs * numeNBs.",
82 UintegerValue(200000),
85 return tid;
86}
87
88void
90{
91 NS_LOG_FUNCTION(this << "Set Fading Trace " << fileName);
92
93 m_traceFile = fileName;
94}
95
96void
101
102void
107
108void
110{
111 NS_LOG_FUNCTION(this << "Loading Fading Trace " << m_traceFile);
112 std::ifstream ifTraceFile;
113 ifTraceFile.open(m_traceFile, std::ifstream::in);
114 m_fadingTrace.clear();
115 if (!ifTraceFile.good())
116 {
117 NS_LOG_INFO(this << " File: " << m_traceFile);
118 NS_ASSERT_MSG(ifTraceFile.good(), " Fading trace file not found");
119 }
120
121 // NS_LOG_INFO (this << " length " << m_traceLength.GetSeconds ());
122 // NS_LOG_INFO (this << " RB " << (uint32_t)m_rbNum << " samples " << m_samplesNum);
123 for (uint32_t i = 0; i < m_rbNum; i++)
124 {
125 FadingTraceSample rbTimeFadingTrace;
126 for (uint32_t j = 0; j < m_samplesNum; j++)
127 {
128 double sample;
129 ifTraceFile >> sample;
130 rbTimeFadingTrace.push_back(sample);
131 }
132 m_fadingTrace.push_back(rbTimeFadingTrace);
133 }
136}
137
142{
143 NS_LOG_FUNCTION(this << *params->psd << a << b);
144
145 ChannelRealizationId_t mobilityPair = std::make_pair(a, b);
146 auto itOff = m_windowOffsetsMap.find(mobilityPair);
147 if (itOff != m_windowOffsetsMap.end())
148 {
149 if (Simulator::Now().GetSeconds() >=
151 {
152 // update all the offsets
153 NS_LOG_INFO("Fading Windows Updated");
154 for (auto itOff2 = m_windowOffsetsMap.begin(); itOff2 != m_windowOffsetsMap.end();
155 itOff2++)
156 {
157 auto itVar = m_startVariableMap.find((*itOff2).first);
158 (*itOff2).second = (*itVar).second->GetValue();
159 }
161 }
162 }
163 else
164 {
165 NS_LOG_LOGIC(this << "insert new channel realization, m_windowOffsetMap.size () = "
166 << m_windowOffsetsMap.size());
168 startV->SetAttribute("Min", DoubleValue(1.0));
169 startV->SetAttribute(
170 "Max",
173 {
175 "not enough streams, consider increasing the StreamSetSize attribute");
176 startV->SetStream(m_currentStream);
177 m_currentStream += 1;
178 }
179 ChannelRealizationId_t mobilityPair = std::make_pair(a, b);
180 m_startVariableMap.insert(
181 std::pair<ChannelRealizationId_t, Ptr<UniformRandomVariable>>(mobilityPair, startV));
182 itOff =
184 .insert(std::pair<ChannelRealizationId_t, int>(mobilityPair, startV->GetValue()))
185 .first;
186 }
187
188 Ptr<SpectrumValue> rxPsd = Copy<SpectrumValue>(params->psd);
189 auto vit = rxPsd->ValuesBegin();
190
191 // Vector aSpeedVector = a->GetVelocity ();
192 // Vector bSpeedVector = b->GetVelocity ();
193
194 // double speed = std::sqrt (std::pow (aSpeedVector.x-bSpeedVector.x,2) + std::pow
195 // (aSpeedVector.y-bSpeedVector.y,2));
196
197 NS_LOG_LOGIC(this << *rxPsd);
198 NS_ASSERT(!m_fadingTrace.empty());
199 int now_ms = static_cast<int>(Simulator::Now().GetMilliSeconds() * m_timeGranularity);
200 int lastUpdate_ms = static_cast<int>(m_lastWindowUpdate.GetMilliSeconds() * m_timeGranularity);
201 int index = ((*itOff).second + now_ms - lastUpdate_ms) % m_samplesNum;
202 int subChannel = 0;
203 while (vit != rxPsd->ValuesEnd())
204 {
205 NS_ASSERT(subChannel < 100);
206 if (*vit != 0.)
207 {
208 double fading = m_fadingTrace.at(subChannel).at(index);
209 NS_LOG_INFO(this << " FADING now " << now_ms << " offset " << (*itOff).second << " id "
210 << index << " fading " << fading);
211 double power = *vit; // in Watt/Hz
212 power = 10 * std::log10(180000 * power); // in dB
213
214 NS_LOG_LOGIC(this << subChannel << *vit << power << fading);
215
216 *vit = std::pow(10., ((power + fading) / 10)) / 180000; // in Watt
217
218 NS_LOG_LOGIC(this << subChannel << *vit);
219 }
220
221 ++vit;
222 ++subChannel;
223 }
224
225 NS_LOG_LOGIC(this << *rxPsd);
226 return rxPsd;
227}
228
229int64_t
231{
232 NS_LOG_FUNCTION(this << stream);
234 m_streamsAssigned = true;
235 m_currentStream = stream;
236 m_lastStream = stream + m_streamSetSize - 1;
237 auto itVar = m_startVariableMap.begin();
238 // the following loop is for eventually pre-existing ChannelRealization instances
239 // note that more instances are expected to be created at run time
240 while (itVar != m_startVariableMap.end())
241 {
243 "not enough streams, consider increasing the StreamSetSize attribute");
244 (*itVar).second->SetStream(m_currentStream);
245 m_currentStream += 1;
246 }
247 return m_streamSetSize;
248}
249
250} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
spectrum-aware propagation loss model
void SetNext(Ptr< SpectrumPropagationLossModel > next)
Used to chain various instances of SpectrumPropagationLossModel.
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetMilliSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:397
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
fading loss model based on precalculated fading traces
uint64_t m_streamSetSize
stream set size
Ptr< SpectrumValue > DoCalcRxPowerSpectralDensity(Ptr< const SpectrumSignalParameters > params, Ptr< const MobilityModel > a, Ptr< const MobilityModel > b) const override
std::string m_traceFile
the trace file name
Time m_lastWindowUpdate
time of last window update
void SetTraceLength(Time t)
Set the trace time.
uint32_t m_samplesNum
number of samples
std::pair< Ptr< const MobilityModel >, Ptr< const MobilityModel > > ChannelRealizationId_t
The couple of mobility node that form a fading channel realization.
int64_t DoAssignStreams(int64_t stream) override
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)
uint32_t m_timeGranularity
time granularity
void SetTraceFileName(std::string fileName)
Set the trace file name.
bool m_streamsAssigned
is streams assigned?
static TypeId GetTypeId()
Get the type ID.
std::map< ChannelRealizationId_t, int > m_windowOffsetsMap
windows offsets map
std::map< ChannelRealizationId_t, Ptr< UniformRandomVariable > > m_startVariableMap
start variable map
uint64_t m_currentStream
the current stream
void LoadTrace()
Load trace function.
FadingTrace m_fadingTrace
fading trace
uint64_t m_lastStream
the last stream
void DoInitialize() override
Initialize() implementation.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition ptr.h:604
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Definition string.h:46
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416