A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
multi-model-spectrum-channel.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
10
11#include "spectrum-converter.h"
12#include "spectrum-phy.h"
15
16#include <ns3/angles.h>
17#include <ns3/antenna-model.h>
18#include <ns3/double.h>
19#include <ns3/log.h>
20#include <ns3/mobility-model.h>
21#include <ns3/net-device.h>
22#include <ns3/node.h>
23#include <ns3/object.h>
24#include <ns3/packet-burst.h>
25#include <ns3/packet.h>
26#include <ns3/propagation-delay-model.h>
27#include <ns3/propagation-loss-model.h>
28#include <ns3/simulator.h>
29
30#include <algorithm>
31#include <iostream>
32#include <utility>
33
34namespace ns3
35{
36
37NS_LOG_COMPONENT_DEFINE("MultiModelSpectrumChannel");
38
39NS_OBJECT_ENSURE_REGISTERED(MultiModelSpectrumChannel);
40
41/**
42 * \brief Output stream operator
43 * \param lhs output stream
44 * \param rhs the TxSpectrumModelInfoMap to print
45 * \return an output stream
46 */
47std::ostream&
48operator<<(std::ostream& lhs, TxSpectrumModelInfoMap_t& rhs)
49{
50 for (auto it = rhs.begin(); it != rhs.end(); ++it)
51 {
52 for (auto jt = it->second.m_spectrumConverterMap.begin();
53 jt != it->second.m_spectrumConverterMap.end();
54 ++jt)
55 {
56 lhs << "(" << it->first << "," << jt->first << ") ";
57 }
58 }
59 return lhs;
60}
61
63 : m_txSpectrumModel(txSpectrumModel)
64{
65}
66
68 : m_rxSpectrumModel(rxSpectrumModel)
69{
70}
71
77
78void
86
89{
90 static TypeId tid = TypeId("ns3::MultiModelSpectrumChannel")
92 .SetGroupName("Spectrum")
93 .AddConstructor<MultiModelSpectrumChannel>()
94
95 ;
96 return tid;
97}
98
99void
101{
102 NS_LOG_FUNCTION(this << phy);
103
104 // remove a previous entry of this phy if it exists
105 // we need to scan for all rxSpectrumModel values since we don't
106 // know which spectrum model the phy had when it was previously added
107 // (it's probably different than the current one)
108 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
109 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
110 ++rxInfoIterator)
111 {
112 auto phyIt = std::find(rxInfoIterator->second.m_rxPhys.begin(),
113 rxInfoIterator->second.m_rxPhys.end(),
114 phy);
115 if (phyIt != rxInfoIterator->second.m_rxPhys.end())
116 {
117 rxInfoIterator->second.m_rxPhys.erase(phyIt);
118 --m_numDevices;
119 break; // there should be at most one entry
120 }
121 }
122}
123
124void
126{
127 NS_LOG_FUNCTION(this << phy);
128
129 Ptr<const SpectrumModel> rxSpectrumModel = phy->GetRxSpectrumModel();
130
131 NS_ASSERT_MSG(rxSpectrumModel,
132 "phy->GetRxSpectrumModel () returned 0. Please check that the RxSpectrumModel is "
133 "already set for the phy before calling MultiModelSpectrumChannel::AddRx (phy)");
134
135 SpectrumModelUid_t rxSpectrumModelUid = rxSpectrumModel->GetUid();
136
137 RemoveRx(phy);
138
139 ++m_numDevices;
140
141 auto [rxInfoIterator, inserted] =
142 m_rxSpectrumModelInfoMap.emplace(rxSpectrumModelUid, RxSpectrumModelInfo(rxSpectrumModel));
143
144 // rxInfoIterator points either to the newly inserted element or to the element that
145 // prevented insertion. In both cases, add the phy to the element pointed to by rxInfoIterator
146 rxInfoIterator->second.m_rxPhys.push_back(phy);
147
148 if (inserted)
149 {
150 // create the necessary converters for all the TX spectrum models that we know of
151 for (auto txInfoIterator = m_txSpectrumModelInfoMap.begin();
152 txInfoIterator != m_txSpectrumModelInfoMap.end();
153 ++txInfoIterator)
154 {
155 Ptr<const SpectrumModel> txSpectrumModel = txInfoIterator->second.m_txSpectrumModel;
156 SpectrumModelUid_t txSpectrumModelUid = txSpectrumModel->GetUid();
157
158 if (rxSpectrumModelUid != txSpectrumModelUid &&
159 !txSpectrumModel->IsOrthogonal(*rxSpectrumModel))
160 {
161 NS_LOG_LOGIC("Creating converter between SpectrumModelUid "
162 << txSpectrumModel->GetUid() << " and " << rxSpectrumModelUid);
163 SpectrumConverter converter(txSpectrumModel, rxSpectrumModel);
164 auto ret2 = txInfoIterator->second.m_spectrumConverterMap.insert(
165 std::make_pair(rxSpectrumModelUid, converter));
166 NS_ASSERT(ret2.second);
167 }
168 }
169 }
170}
171
172TxSpectrumModelInfoMap_t::const_iterator
174 Ptr<const SpectrumModel> txSpectrumModel)
175{
176 NS_LOG_FUNCTION(this << txSpectrumModel);
177 SpectrumModelUid_t txSpectrumModelUid = txSpectrumModel->GetUid();
178 auto txInfoIterator = m_txSpectrumModelInfoMap.find(txSpectrumModelUid);
179
180 if (txInfoIterator == m_txSpectrumModelInfoMap.end())
181 {
182 // first time we see this TX SpectrumModel
183 // we add it to the list
184 auto ret = m_txSpectrumModelInfoMap.insert(
185 std::make_pair(txSpectrumModelUid, TxSpectrumModelInfo(txSpectrumModel)));
186 NS_ASSERT(ret.second);
187 txInfoIterator = ret.first;
188
189 // and we create the converters for all the RX SpectrumModels that we know of
190 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
191 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
192 ++rxInfoIterator)
193 {
194 Ptr<const SpectrumModel> rxSpectrumModel = rxInfoIterator->second.m_rxSpectrumModel;
195 SpectrumModelUid_t rxSpectrumModelUid = rxSpectrumModel->GetUid();
196
197 if (rxSpectrumModelUid != txSpectrumModelUid &&
198 !txSpectrumModel->IsOrthogonal(*rxSpectrumModel))
199 {
200 NS_LOG_LOGIC("Creating converter between SpectrumModelUid "
201 << txSpectrumModelUid << " and " << rxSpectrumModelUid);
202 SpectrumConverter converter(txSpectrumModel, rxSpectrumModel);
203 auto ret2 = txInfoIterator->second.m_spectrumConverterMap.insert(
204 std::make_pair(rxSpectrumModelUid, converter));
205 NS_ASSERT(ret2.second);
206 }
207 }
208 }
209 else
210 {
211 NS_LOG_LOGIC("SpectrumModelUid " << txSpectrumModelUid << " already present");
212 }
213 return txInfoIterator;
214}
215
216void
218{
219 NS_LOG_FUNCTION(this << txParams);
220
221 NS_ASSERT(txParams->txPhy);
222 NS_ASSERT(txParams->psd);
223 Ptr<SpectrumSignalParameters> txParamsTrace =
224 txParams->Copy(); // copy it since traced value cannot be const (because of potential
225 // underlying DynamicCasts)
226 m_txSigParamsTrace(txParamsTrace);
227
228 auto txMobility = txParams->txPhy->GetMobility();
229 auto txSpectrumModelUid = txParams->psd->GetSpectrumModelUid();
230 NS_LOG_LOGIC("txSpectrumModelUid " << txSpectrumModelUid);
231
232 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
233 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
234 ++rxInfoIterator)
235 {
236 SpectrumModelUid_t rxSpectrumModelUid = rxInfoIterator->second.m_rxSpectrumModel->GetUid();
237 NS_LOG_LOGIC("rxSpectrumModelUids " << rxSpectrumModelUid);
238
239 for (auto rxPhyIterator = rxInfoIterator->second.m_rxPhys.begin();
240 rxPhyIterator != rxInfoIterator->second.m_rxPhys.end();
241 ++rxPhyIterator)
242 {
243 NS_ASSERT_MSG((*rxPhyIterator)->GetRxSpectrumModel()->GetUid() == rxSpectrumModelUid,
244 "SpectrumModel change was not notified to MultiModelSpectrumChannel "
245 "(i.e., AddRx should be called again after model is changed)");
246
247 if ((*rxPhyIterator) != txParams->txPhy)
248 {
249 auto rxNetDevice = (*rxPhyIterator)->GetDevice();
250 auto txNetDevice = txParams->txPhy->GetDevice();
251
252 if (rxNetDevice && txNetDevice)
253 {
254 // we assume that devices are attached to a node
255 if (rxNetDevice->GetNode()->GetId() == txNetDevice->GetNode()->GetId())
256 {
258 "Skipping the pathloss calculation among different antennas of the "
259 "same node, not supported yet by any pathloss model in ns-3.");
260 continue;
261 }
262 }
263
264 if (m_filter && m_filter->Filter(txParams, *rxPhyIterator))
265 {
266 continue;
267 }
268
269 NS_LOG_LOGIC("copying signal parameters " << txParams);
270 auto rxParams = txParams->Copy();
271 rxParams->psd = Copy<SpectrumValue>(txParams->psd);
272 Time delay{0};
273
274 auto receiverMobility = (*rxPhyIterator)->GetMobility();
275
276 if (txMobility && receiverMobility)
277 {
278 auto txAntennaGain{0.0};
279 auto rxAntennaGain{0.0};
280 auto propagationGainDb{0.0};
281 auto pathLossDb{0.0};
282 if (rxParams->txAntenna)
283 {
284 Angles txAngles(receiverMobility->GetPosition(), txMobility->GetPosition());
285 txAntennaGain = rxParams->txAntenna->GetGainDb(txAngles);
286 NS_LOG_LOGIC("txAntennaGain = " << txAntennaGain << " dB");
287 pathLossDb -= txAntennaGain;
288 }
289 auto rxAntenna = DynamicCast<AntennaModel>((*rxPhyIterator)->GetAntenna());
290 if (rxAntenna)
291 {
292 Angles rxAngles(txMobility->GetPosition(), receiverMobility->GetPosition());
293 rxAntennaGain = rxAntenna->GetGainDb(rxAngles);
294 NS_LOG_LOGIC("rxAntennaGain = " << rxAntennaGain << " dB");
295 pathLossDb -= rxAntennaGain;
296 }
298 {
299 if (txMobility->GetPosition() == receiverMobility->GetPosition())
300 {
301 propagationGainDb = 0; // Assume no propagation loss when co-located
302 }
303 else
304 {
305 propagationGainDb =
306 m_propagationLoss->CalcRxPower(0, txMobility, receiverMobility);
307 }
308 NS_LOG_LOGIC("propagationGainDb = " << propagationGainDb << " dB");
309 pathLossDb -= propagationGainDb;
310 }
311 NS_LOG_LOGIC("total pathLoss = " << pathLossDb << " dB");
312 // Gain trace
313 m_gainTrace(txMobility,
314 receiverMobility,
315 txAntennaGain,
316 rxAntennaGain,
317 propagationGainDb,
318 pathLossDb);
319 // Pathloss trace
320 m_pathLossTrace(txParams->txPhy, *rxPhyIterator, pathLossDb);
321 if (pathLossDb > m_maxLossDb)
322 {
323 // beyond range
324 continue;
325 }
326 auto pathGainLinear = std::pow(10.0, (-pathLossDb) / 10.0);
327 *(rxParams->psd) *= pathGainLinear;
328
330 {
331 delay = m_propagationDelay->GetDelay(txMobility, receiverMobility);
332 }
333 }
334
335 if (rxNetDevice)
336 {
337 // the receiver has a NetDevice, so we expect that it is attached to a Node
338 auto dstNode = rxNetDevice->GetNode()->GetId();
340 delay,
342 this,
343 rxParams,
344 *rxPhyIterator);
345 }
346 else
347 {
348 // the receiver is not attached to a NetDevice, so we cannot assume that it is
349 // attached to a node
352 this,
353 rxParams,
354 *rxPhyIterator);
355 }
356 }
357 }
358 }
359}
360
361void
363{
364 NS_LOG_FUNCTION(this);
365 const auto txSpectrumModelUid = params->psd->GetSpectrumModelUid();
366 const auto rxSpectrumModelUid = receiver->GetRxSpectrumModel()->GetUid();
367
368 auto txInfoIteratorerator =
369 FindAndEventuallyAddTxSpectrumModel(params->psd->GetSpectrumModel());
370 NS_ASSERT(txInfoIteratorerator != m_txSpectrumModelInfoMap.end());
371
372 NS_LOG_LOGIC("converter map for TX SpectrumModel with Uid " << txInfoIteratorerator->first);
374 "converter map size: " << txInfoIteratorerator->second.m_spectrumConverterMap.size());
375 NS_LOG_LOGIC("converter map first element: "
376 << txInfoIteratorerator->second.m_spectrumConverterMap.begin()->first);
377
378 Ptr<SpectrumValue> convertedPsd;
379 if (txSpectrumModelUid == rxSpectrumModelUid)
380 {
381 NS_LOG_LOGIC("no spectrum conversion needed");
382 convertedPsd = params->psd;
383 }
384 else
385 {
386 NS_LOG_LOGIC("converting txPowerSpectrum SpectrumModelUids "
387 << txSpectrumModelUid << " --> " << rxSpectrumModelUid);
388 auto rxConverterIterator =
389 txInfoIteratorerator->second.m_spectrumConverterMap.find(rxSpectrumModelUid);
390 if (rxConverterIterator == txInfoIteratorerator->second.m_spectrumConverterMap.end())
391 {
392 // No converter means TX SpectrumModel is orthogonal to RX SpectrumModel
393 return;
394 }
395 convertedPsd = rxConverterIterator->second.Convert(params->psd);
396 NS_LOG_LOGIC("convertedPsd " << convertedPsd->GetValuesN());
397 }
398 params->psd = convertedPsd;
400 {
401 params->psd =
402 m_spectrumPropagationLoss->CalcRxPowerSpectralDensity(params,
403 params->txPhy->GetMobility(),
404 receiver->GetMobility());
405 }
407 {
408 auto txPhasedArrayModel = DynamicCast<PhasedArrayModel>(params->txPhy->GetAntenna());
409 auto rxPhasedArrayModel = DynamicCast<PhasedArrayModel>(receiver->GetAntenna());
410
411 NS_ASSERT_MSG(txPhasedArrayModel && rxPhasedArrayModel,
412 "PhasedArrayModel instances should be installed at both TX and RX "
413 "SpectrumPhy in order to use PhasedArraySpectrumPropagationLoss.");
414
415 params = m_phasedArraySpectrumPropagationLoss->CalcRxPowerSpectralDensity(
416 params,
417 params->txPhy->GetMobility(),
418 receiver->GetMobility(),
419 txPhasedArrayModel,
420 rxPhasedArrayModel);
421 }
422 receiver->StartRx(params);
423}
424
425std::size_t
430
433{
435 // this method implementation is computationally intensive. This
436 // method would be faster if we actually used a std::vector for
437 // storing devices, which we don't due to the need to have fast
438 // SpectrumModel conversions and to allow PHY devices to change a
439 // SpectrumModel at run time. Note that having this method slow is
440 // acceptable as it is not used much at run time (often not at all).
441 // On the other hand, having slow SpectrumModel conversion would be
442 // less acceptable.
443 std::size_t j = 0;
444 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
445 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
446 ++rxInfoIterator)
447 {
448 for (const auto& phyIt : rxInfoIterator->second.m_rxPhys)
449 {
450 if (j == i)
451 {
452 return (*phyIt).GetDevice();
453 }
454 j++;
455 }
456 }
457 NS_FATAL_ERROR("m_numDevices > actual number of devices");
458 return nullptr;
459}
460
461} // namespace ns3
Class holding the azimuth and inclination angles of spherical coordinates.
Definition angles.h:107
This SpectrumChannel implementation can handle the presence of SpectrumPhy instances which can use di...
void RemoveRx(Ptr< SpectrumPhy > phy) override
Remove a SpectrumPhy from a channel.
Ptr< NetDevice > GetDevice(std::size_t i) const override
void AddRx(Ptr< SpectrumPhy > phy) override
Add a SpectrumPhy to a channel, so it can receive packets.
TxSpectrumModelInfoMap_t m_txSpectrumModelInfoMap
Data structure holding, for each TX SpectrumModel, all the converters to any RX SpectrumModel,...
std::size_t m_numDevices
Number of devices connected to the channel.
void DoDispose() override
Destructor implementation.
void StartTx(Ptr< SpectrumSignalParameters > params) override
Used by attached PHY instances to transmit signals on the channel.
TxSpectrumModelInfoMap_t::const_iterator FindAndEventuallyAddTxSpectrumModel(Ptr< const SpectrumModel > txSpectrumModel)
This method checks if m_rxSpectrumModelInfoMap contains an entry for the given TX SpectrumModel.
static TypeId GetTypeId()
Get the type ID.
virtual void StartRx(Ptr< SpectrumSignalParameters > params, Ptr< SpectrumPhy > receiver)
Used internally to reschedule transmission after the propagation delay.
RxSpectrumModelInfoMap_t m_rxSpectrumModelInfoMap
Data structure holding, for each RX spectrum model, all the corresponding SpectrumPhy instances.
Smart pointer class similar to boost::intrusive_ptr.
The Rx spectrum model information.
RxSpectrumModelInfo(Ptr< const SpectrumModel > rxSpectrumModel)
Constructor.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
Defines the interface for spectrum-aware channel implementations.
TracedCallback< Ptr< SpectrumSignalParameters > > m_txSigParamsTrace
Traced callback for SpectrumSignalParameters in StartTx requests.
void DoDispose() override
Destructor implementation.
Ptr< SpectrumTransmitFilter > m_filter
Transmit filter to be used with this channel.
Ptr< PropagationDelayModel > m_propagationDelay
Propagation delay model to be used with this channel.
Ptr< SpectrumPropagationLossModel > m_spectrumPropagationLoss
Frequency-dependent propagation loss model to be used with this channel.
Ptr< PhasedArraySpectrumPropagationLossModel > m_phasedArraySpectrumPropagationLoss
Frequency-dependent propagation loss model to be used with this channel.
TracedCallback< Ptr< const SpectrumPhy >, Ptr< const SpectrumPhy >, double > m_pathLossTrace
The PathLoss trace source.
TracedCallback< Ptr< const MobilityModel >, Ptr< const MobilityModel >, double, double, double, double > m_gainTrace
The Gain trace source.
Ptr< PropagationLossModel > m_propagationLoss
Single-frequency propagation loss model to be used with this channel.
double m_maxLossDb
Maximum loss [dB].
Class which implements a converter between SpectrumValue which are defined over different SpectrumMod...
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
The Tx spectrum model information.
TxSpectrumModelInfo(Ptr< const SpectrumModel > txSpectrumModel)
Constructor.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
std::map< SpectrumModelUid_t, TxSpectrumModelInfo > TxSpectrumModelInfoMap_t
Container: SpectrumModelUid_t, TxSpectrumModelInfo.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition ptr.h:604