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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
21
22#include "spectrum-converter.h"
23#include "spectrum-phy.h"
26
27#include <ns3/angles.h>
28#include <ns3/antenna-model.h>
29#include <ns3/double.h>
30#include <ns3/log.h>
31#include <ns3/mobility-model.h>
32#include <ns3/net-device.h>
33#include <ns3/node.h>
34#include <ns3/object.h>
35#include <ns3/packet-burst.h>
36#include <ns3/packet.h>
37#include <ns3/propagation-delay-model.h>
38#include <ns3/propagation-loss-model.h>
39#include <ns3/simulator.h>
40
41#include <algorithm>
42#include <iostream>
43#include <utility>
44
45namespace ns3
46{
47
48NS_LOG_COMPONENT_DEFINE("MultiModelSpectrumChannel");
49
50NS_OBJECT_ENSURE_REGISTERED(MultiModelSpectrumChannel);
51
52/**
53 * \brief Output stream operator
54 * \param lhs output stream
55 * \param rhs the TxSpectrumModelInfoMap to print
56 * \return an output stream
57 */
58std::ostream&
59operator<<(std::ostream& lhs, TxSpectrumModelInfoMap_t& rhs)
60{
61 for (auto it = rhs.begin(); it != rhs.end(); ++it)
62 {
63 for (auto jt = it->second.m_spectrumConverterMap.begin();
64 jt != it->second.m_spectrumConverterMap.end();
65 ++jt)
66 {
67 lhs << "(" << it->first << "," << jt->first << ") ";
68 }
69 }
70 return lhs;
71}
72
74 : m_txSpectrumModel(txSpectrumModel)
75{
76}
77
79 : m_rxSpectrumModel(rxSpectrumModel)
80{
81}
82
84 : m_numDevices{0}
85{
86 NS_LOG_FUNCTION(this);
87}
88
89void
91{
92 NS_LOG_FUNCTION(this);
96}
97
100{
101 static TypeId tid = TypeId("ns3::MultiModelSpectrumChannel")
103 .SetGroupName("Spectrum")
104 .AddConstructor<MultiModelSpectrumChannel>()
105
106 ;
107 return tid;
108}
109
110void
112{
113 NS_LOG_FUNCTION(this << phy);
114
115 // remove a previous entry of this phy if it exists
116 // we need to scan for all rxSpectrumModel values since we don't
117 // know which spectrum model the phy had when it was previously added
118 // (it's probably different than the current one)
119 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
120 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
121 ++rxInfoIterator)
122 {
123 auto phyIt = std::find(rxInfoIterator->second.m_rxPhys.begin(),
124 rxInfoIterator->second.m_rxPhys.end(),
125 phy);
126 if (phyIt != rxInfoIterator->second.m_rxPhys.end())
127 {
128 rxInfoIterator->second.m_rxPhys.erase(phyIt);
129 --m_numDevices;
130 break; // there should be at most one entry
131 }
132 }
133}
134
135void
137{
138 NS_LOG_FUNCTION(this << phy);
139
140 Ptr<const SpectrumModel> rxSpectrumModel = phy->GetRxSpectrumModel();
141
142 NS_ASSERT_MSG(rxSpectrumModel,
143 "phy->GetRxSpectrumModel () returned 0. Please check that the RxSpectrumModel is "
144 "already set for the phy before calling MultiModelSpectrumChannel::AddRx (phy)");
145
146 SpectrumModelUid_t rxSpectrumModelUid = rxSpectrumModel->GetUid();
147
148 RemoveRx(phy);
149
150 ++m_numDevices;
151
152 auto [rxInfoIterator, inserted] =
153 m_rxSpectrumModelInfoMap.emplace(rxSpectrumModelUid, RxSpectrumModelInfo(rxSpectrumModel));
154
155 // rxInfoIterator points either to the newly inserted element or to the element that
156 // prevented insertion. In both cases, add the phy to the element pointed to by rxInfoIterator
157 rxInfoIterator->second.m_rxPhys.push_back(phy);
158
159 if (inserted)
160 {
161 // create the necessary converters for all the TX spectrum models that we know of
162 for (auto txInfoIterator = m_txSpectrumModelInfoMap.begin();
163 txInfoIterator != m_txSpectrumModelInfoMap.end();
164 ++txInfoIterator)
165 {
166 Ptr<const SpectrumModel> txSpectrumModel = txInfoIterator->second.m_txSpectrumModel;
167 SpectrumModelUid_t txSpectrumModelUid = txSpectrumModel->GetUid();
168
169 if (rxSpectrumModelUid != txSpectrumModelUid &&
170 !txSpectrumModel->IsOrthogonal(*rxSpectrumModel))
171 {
172 NS_LOG_LOGIC("Creating converter between SpectrumModelUid "
173 << txSpectrumModel->GetUid() << " and " << rxSpectrumModelUid);
174 SpectrumConverter converter(txSpectrumModel, rxSpectrumModel);
175 auto ret2 = txInfoIterator->second.m_spectrumConverterMap.insert(
176 std::make_pair(rxSpectrumModelUid, converter));
177 NS_ASSERT(ret2.second);
178 }
179 }
180 }
181}
182
183TxSpectrumModelInfoMap_t::const_iterator
185 Ptr<const SpectrumModel> txSpectrumModel)
186{
187 NS_LOG_FUNCTION(this << txSpectrumModel);
188 SpectrumModelUid_t txSpectrumModelUid = txSpectrumModel->GetUid();
189 auto txInfoIterator = m_txSpectrumModelInfoMap.find(txSpectrumModelUid);
190
191 if (txInfoIterator == m_txSpectrumModelInfoMap.end())
192 {
193 // first time we see this TX SpectrumModel
194 // we add it to the list
195 auto ret = m_txSpectrumModelInfoMap.insert(
196 std::make_pair(txSpectrumModelUid, TxSpectrumModelInfo(txSpectrumModel)));
197 NS_ASSERT(ret.second);
198 txInfoIterator = ret.first;
199
200 // and we create the converters for all the RX SpectrumModels that we know of
201 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
202 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
203 ++rxInfoIterator)
204 {
205 Ptr<const SpectrumModel> rxSpectrumModel = rxInfoIterator->second.m_rxSpectrumModel;
206 SpectrumModelUid_t rxSpectrumModelUid = rxSpectrumModel->GetUid();
207
208 if (rxSpectrumModelUid != txSpectrumModelUid &&
209 !txSpectrumModel->IsOrthogonal(*rxSpectrumModel))
210 {
211 NS_LOG_LOGIC("Creating converter between SpectrumModelUid "
212 << txSpectrumModelUid << " and " << rxSpectrumModelUid);
213
214 SpectrumConverter converter(txSpectrumModel, rxSpectrumModel);
215 auto ret2 = txInfoIterator->second.m_spectrumConverterMap.insert(
216 std::make_pair(rxSpectrumModelUid, converter));
217 NS_ASSERT(ret2.second);
218 }
219 }
220 }
221 else
222 {
223 NS_LOG_LOGIC("SpectrumModelUid " << txSpectrumModelUid << " already present");
224 }
225 return txInfoIterator;
226}
227
228void
230{
231 NS_LOG_FUNCTION(this << txParams);
232
233 NS_ASSERT(txParams->txPhy);
234 NS_ASSERT(txParams->psd);
235 Ptr<SpectrumSignalParameters> txParamsTrace =
236 txParams->Copy(); // copy it since traced value cannot be const (because of potential
237 // underlying DynamicCasts)
238 m_txSigParamsTrace(txParamsTrace);
239
240 Ptr<MobilityModel> txMobility = txParams->txPhy->GetMobility();
241 SpectrumModelUid_t txSpectrumModelUid = txParams->psd->GetSpectrumModelUid();
242 NS_LOG_LOGIC("txSpectrumModelUid " << txSpectrumModelUid);
243
244 //
245 auto txInfoIteratorerator =
246 FindAndEventuallyAddTxSpectrumModel(txParams->psd->GetSpectrumModel());
247 NS_ASSERT(txInfoIteratorerator != m_txSpectrumModelInfoMap.end());
248
249 NS_LOG_LOGIC("converter map for TX SpectrumModel with Uid " << txInfoIteratorerator->first);
251 "converter map size: " << txInfoIteratorerator->second.m_spectrumConverterMap.size());
252 NS_LOG_LOGIC("converter map first element: "
253 << txInfoIteratorerator->second.m_spectrumConverterMap.begin()->first);
254
255 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
256 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
257 ++rxInfoIterator)
258 {
259 SpectrumModelUid_t rxSpectrumModelUid = rxInfoIterator->second.m_rxSpectrumModel->GetUid();
260 NS_LOG_LOGIC("rxSpectrumModelUids " << rxSpectrumModelUid);
261
262 Ptr<SpectrumValue> convertedTxPowerSpectrum;
263 if (txSpectrumModelUid == rxSpectrumModelUid)
264 {
265 NS_LOG_LOGIC("no spectrum conversion needed");
266 convertedTxPowerSpectrum = txParams->psd;
267 }
268 else
269 {
270 NS_LOG_LOGIC("converting txPowerSpectrum SpectrumModelUids "
271 << txSpectrumModelUid << " --> " << rxSpectrumModelUid);
272 auto rxConverterIterator =
273 txInfoIteratorerator->second.m_spectrumConverterMap.find(rxSpectrumModelUid);
274 if (rxConverterIterator == txInfoIteratorerator->second.m_spectrumConverterMap.end())
275 {
276 // No converter means TX SpectrumModel is orthogonal to RX SpectrumModel
277 continue;
278 }
279 convertedTxPowerSpectrum = rxConverterIterator->second.Convert(txParams->psd);
280 }
281
282 for (auto rxPhyIterator = rxInfoIterator->second.m_rxPhys.begin();
283 rxPhyIterator != rxInfoIterator->second.m_rxPhys.end();
284 ++rxPhyIterator)
285 {
286 NS_ASSERT_MSG((*rxPhyIterator)->GetRxSpectrumModel()->GetUid() == rxSpectrumModelUid,
287 "SpectrumModel change was not notified to MultiModelSpectrumChannel "
288 "(i.e., AddRx should be called again after model is changed)");
289
290 if ((*rxPhyIterator) != txParams->txPhy)
291 {
292 Ptr<NetDevice> rxNetDevice = (*rxPhyIterator)->GetDevice();
293 Ptr<NetDevice> txNetDevice = txParams->txPhy->GetDevice();
294
295 if (rxNetDevice && txNetDevice)
296 {
297 // we assume that devices are attached to a node
298 if (rxNetDevice->GetNode()->GetId() == txNetDevice->GetNode()->GetId())
299 {
301 "Skipping the pathloss calculation among different antennas of the "
302 "same node, not supported yet by any pathloss model in ns-3.");
303 continue;
304 }
305 }
306
307 if (m_filter && m_filter->Filter(txParams, *rxPhyIterator))
308 {
309 continue;
310 }
311
312 NS_LOG_LOGIC("copying signal parameters " << txParams);
313 Ptr<SpectrumSignalParameters> rxParams = txParams->Copy();
314 rxParams->psd = Copy<SpectrumValue>(convertedTxPowerSpectrum);
315 Time delay = MicroSeconds(0);
316
317 Ptr<MobilityModel> receiverMobility = (*rxPhyIterator)->GetMobility();
318
319 if (txMobility && receiverMobility)
320 {
321 double txAntennaGain = 0;
322 double rxAntennaGain = 0;
323 double propagationGainDb = 0;
324 double pathLossDb = 0;
325 if (rxParams->txAntenna)
326 {
327 Angles txAngles(receiverMobility->GetPosition(), txMobility->GetPosition());
328 txAntennaGain = rxParams->txAntenna->GetGainDb(txAngles);
329 NS_LOG_LOGIC("txAntennaGain = " << txAntennaGain << " dB");
330 pathLossDb -= txAntennaGain;
331 }
332 Ptr<AntennaModel> rxAntenna =
333 DynamicCast<AntennaModel>((*rxPhyIterator)->GetAntenna());
334 if (rxAntenna)
335 {
336 Angles rxAngles(txMobility->GetPosition(), receiverMobility->GetPosition());
337 rxAntennaGain = rxAntenna->GetGainDb(rxAngles);
338 NS_LOG_LOGIC("rxAntennaGain = " << rxAntennaGain << " dB");
339 pathLossDb -= rxAntennaGain;
340 }
342 {
343 propagationGainDb =
344 m_propagationLoss->CalcRxPower(0, txMobility, receiverMobility);
345 NS_LOG_LOGIC("propagationGainDb = " << propagationGainDb << " dB");
346 pathLossDb -= propagationGainDb;
347 }
348 NS_LOG_LOGIC("total pathLoss = " << pathLossDb << " dB");
349 // Gain trace
350 m_gainTrace(txMobility,
351 receiverMobility,
352 txAntennaGain,
353 rxAntennaGain,
354 propagationGainDb,
355 pathLossDb);
356 // Pathloss trace
357 m_pathLossTrace(txParams->txPhy, *rxPhyIterator, pathLossDb);
358 if (pathLossDb > m_maxLossDb)
359 {
360 // beyond range
361 continue;
362 }
363 double pathGainLinear = std::pow(10.0, (-pathLossDb) / 10.0);
364 *(rxParams->psd) *= pathGainLinear;
365
367 {
368 delay = m_propagationDelay->GetDelay(txMobility, receiverMobility);
369 }
370 }
371
372 if (rxNetDevice)
373 {
374 // the receiver has a NetDevice, so we expect that it is attached to a Node
375 uint32_t dstNode = rxNetDevice->GetNode()->GetId();
377 delay,
379 this,
380 rxParams,
381 *rxPhyIterator);
382 }
383 else
384 {
385 // the receiver is not attached to a NetDevice, so we cannot assume that it is
386 // attached to a node
389 this,
390 rxParams,
391 *rxPhyIterator);
392 }
393 }
394 }
395 }
396}
397
398void
400{
401 NS_LOG_FUNCTION(this);
403 {
404 params->psd =
405 m_spectrumPropagationLoss->CalcRxPowerSpectralDensity(params,
406 params->txPhy->GetMobility(),
407 receiver->GetMobility());
408 }
410 {
411 Ptr<const PhasedArrayModel> txPhasedArrayModel =
412 DynamicCast<PhasedArrayModel>(params->txPhy->GetAntenna());
413 Ptr<const PhasedArrayModel> rxPhasedArrayModel =
414 DynamicCast<PhasedArrayModel>(receiver->GetAntenna());
415
416 NS_ASSERT_MSG(txPhasedArrayModel && rxPhasedArrayModel,
417 "PhasedArrayModel instances should be installed at both TX and RX "
418 "SpectrumPhy in order to use PhasedArraySpectrumPropagationLoss.");
419
420 params = m_phasedArraySpectrumPropagationLoss->CalcRxPowerSpectralDensity(
421 params,
422 params->txPhy->GetMobility(),
423 receiver->GetMobility(),
424 txPhasedArrayModel,
425 rxPhasedArrayModel);
426 }
427 receiver->StartRx(params);
428}
429
430std::size_t
432{
433 return m_numDevices;
434}
435
438{
440 // this method implementation is computationally intensive. This
441 // method would be faster if we actually used a std::vector for
442 // storing devices, which we don't due to the need to have fast
443 // SpectrumModel conversions and to allow PHY devices to change a
444 // SpectrumModel at run time. Note that having this method slow is
445 // acceptable as it is not used much at run time (often not at all).
446 // On the other hand, having slow SpectrumModel conversion would be
447 // less acceptable.
448 std::size_t j = 0;
449 for (auto rxInfoIterator = m_rxSpectrumModelInfoMap.begin();
450 rxInfoIterator != m_rxSpectrumModelInfoMap.end();
451 ++rxInfoIterator)
452 {
453 for (const auto& phyIt : rxInfoIterator->second.m_rxPhys)
454 {
455 if (j == i)
456 {
457 return (*phyIt).GetDevice();
458 }
459 j++;
460 }
461 }
462 NS_FATAL_ERROR("m_numDevices > actual number of devices");
463 return nullptr;
464}
465
466} // namespace ns3
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:118
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
std::size_t GetNDevices() 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.
double CalcRxPower(double txPowerDbm, Ptr< MobilityModel > a, Ptr< MobilityModel > b) const
Returns the Rx Power taking into account all the PropagationLossModel(s) chained to the current one.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:571
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
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:105
The Tx spectrum model information.
TxSpectrumModelInfo(Ptr< const SpectrumModel > txSpectrumModel)
Constructor.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:46
std::map< SpectrumModelUid_t, TxSpectrumModelInfo > TxSpectrumModelInfoMap_t
Container: SpectrumModelUid_t, TxSpectrumModelInfo.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
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:159