A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
three-gpp-ntn-channel-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 SIGNET Lab, Department of Information Engineering,
3 * University of Padova
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 */
7
8/**
9 * @file
10 * This example is a modified version of "three-gpp-channel-example", to include
11 * the 3GPP NTN channel model.
12 * Specifically, most changes (which are also highlighted throughout the code)
13 * impact the main method, and comprise:
14 * - the configuration of ad-hoc propagation and channel condition models;
15 * - the use of GeocentricConstantPositionMobilityModel for the nodes' mobility.
16 * The pre-configured parameters are the one provided by 3GPP in TR 38.821,
17 * more specifically scenario 10 in down-link mode.
18 * Two static nodes, one on the ground and one in orbit, communicate with each other.
19 * The carrier frequency is set at 20GHz with 400MHz bandwidth.
20 * The result is the SNR of the signal and the path loss, saved in the ntn-snr-trace.txt file.
21 */
22
23#include "ns3/antenna-model.h"
24#include "ns3/core-module.h"
25#include "ns3/geocentric-constant-position-mobility-model.h"
26#include "ns3/isotropic-antenna-model.h"
27#include "ns3/mobility-model.h"
28#include "ns3/node-container.h"
29#include "ns3/node.h"
30#include "ns3/spectrum-signal-parameters.h"
31#include "ns3/three-gpp-channel-model.h"
32#include "ns3/three-gpp-propagation-loss-model.h"
33#include "ns3/three-gpp-spectrum-propagation-loss-model.h"
34#include "ns3/uniform-planar-array.h"
35
36#include <fstream>
37
38NS_LOG_COMPONENT_DEFINE("NTNChannelExample");
39
40using namespace ns3;
41
43 m_propagationLossModel; //!< the PropagationLossModel object
45 m_spectrumLossModel; //!< the SpectrumPropagationLossModel object
46static std::ofstream resultsFile; //!< The results file
47
48/**
49 * @brief Create the PSD for the TX
50 *
51 * @param fcHz the carrier frequency in Hz
52 * @param pwrDbm the transmission power in dBm
53 * @param bwHz the bandwidth in Hz
54 * @param rbWidthHz the Resource Block (RB) width in Hz
55 *
56 * @return the pointer to the PSD
57 */
59CreateTxPowerSpectralDensity(double fcHz, double pwrDbm, double bwHz, double rbWidthHz)
60{
61 unsigned int numRbs = std::floor(bwHz / rbWidthHz);
62 double f = fcHz - (numRbs * rbWidthHz / 2.0);
63 double powerTx = pwrDbm; // dBm power
64
65 Bands rbs; // A vector representing each resource block
66 for (uint32_t numrb = 0; numrb < numRbs; ++numrb)
67 {
68 BandInfo rb;
69 rb.fl = f;
70 f += rbWidthHz / 2;
71 rb.fc = f;
72 f += rbWidthHz / 2;
73 rb.fh = f;
74
75 rbs.push_back(rb);
76 }
79
80 double powerTxW = std::pow(10., (powerTx - 30) / 10); // Get Tx power in Watts
81 double txPowerDensity = (powerTxW / bwHz);
82
83 for (auto psd = txPsd->ValuesBegin(); psd != txPsd->ValuesEnd(); ++psd)
84 {
85 *psd = txPowerDensity;
86 }
87
88 return txPsd; // [W/Hz]
89}
90
91/**
92 * @brief Create the noise PSD for the
93 *
94 * @param fcHz the carrier frequency in Hz
95 * @param noiseFigureDb the noise figure in dB
96 * @param bwHz the bandwidth in Hz
97 * @param rbWidthHz the Resource Block (RB) width in Hz
98 *
99 * @return the pointer to the noise PSD
100 */
102CreateNoisePowerSpectralDensity(double fcHz, double noiseFigureDb, double bwHz, double rbWidthHz)
103{
104 unsigned int numRbs = std::floor(bwHz / rbWidthHz);
105 double f = fcHz - (numRbs * rbWidthHz / 2.0);
106
107 Bands rbs; // A vector representing each resource block
108 std::vector<int> rbsId; // A vector representing the resource block IDs
109 for (uint32_t numrb = 0; numrb < numRbs; ++numrb)
110 {
111 BandInfo rb;
112 rb.fl = f;
113 f += rbWidthHz / 2;
114 rb.fc = f;
115 f += rbWidthHz / 2;
116 rb.fh = f;
117
118 rbs.push_back(rb);
119 rbsId.push_back(numrb);
120 }
123
124 // see "LTE - From theory to practice"
125 // Section 22.4.4.2 Thermal Noise and Receiver Noise Figure
126 const double ktDbmHz = -174.0; // dBm/Hz
127 double ktWHz = std::pow(10.0, (ktDbmHz - 30) / 10.0); // W/Hz
128 double noiseFigureLinear = std::pow(10.0, noiseFigureDb / 10.0);
129
130 double noisePowerSpectralDensity = ktWHz * noiseFigureLinear;
131
132 for (auto rbId : rbsId)
133 {
134 (*txPsd)[rbId] = noisePowerSpectralDensity;
135 }
136
137 return txPsd; // W/Hz
138}
139
140/**
141 * @brief A structure that holds the parameters for the
142 * ComputeSnr function. In this way the problem with the limited
143 * number of parameters of method Schedule is avoided.
144 */
145struct ComputeSnrParams
146{
147 Ptr<MobilityModel> txMob; //!< the tx mobility model
148 Ptr<MobilityModel> rxMob; //!< the rx mobility model
149 double txPow; //!< the tx power in dBm
150 double noiseFigure; //!< the noise figure in dB
151 Ptr<PhasedArrayModel> txAntenna; //!< the tx antenna array
152 Ptr<PhasedArrayModel> rxAntenna; //!< the rx antenna array
153 double frequency; //!< the carrier frequency in Hz
154 double bandwidth; //!< the total bandwidth in Hz
155 double resourceBlockBandwidth; //!< the Resource Block bandwidth in Hz
156
157 /**
158 * @brief Constructor
159 * @param pTxMob the tx mobility model
160 * @param pRxMob the rx mobility model
161 * @param pTxPow the tx power in dBm
162 * @param pNoiseFigure the noise figure in dB
163 * @param pTxAntenna the tx antenna array
164 * @param pRxAntenna the rx antenna array
165 * @param pFrequency the carrier frequency in Hz
166 * @param pBandwidth the total bandwidth in Hz
167 * @param pResourceBlockBandwidth the Resource Block bandwidth in Hz
168 */
170 Ptr<MobilityModel> pRxMob,
171 double pTxPow,
172 double pNoiseFigure,
173 Ptr<PhasedArrayModel> pTxAntenna,
174 Ptr<PhasedArrayModel> pRxAntenna,
175 double pFrequency,
176 double pBandwidth,
177 double pResourceBlockBandwidth)
178 {
179 txMob = pTxMob;
180 rxMob = pRxMob;
181 txPow = pTxPow;
182 noiseFigure = pNoiseFigure;
183 txAntenna = pTxAntenna;
184 rxAntenna = pRxAntenna;
185 frequency = pFrequency;
186 bandwidth = pBandwidth;
187 resourceBlockBandwidth = pResourceBlockBandwidth;
188 }
189};
190
191/**
192 * Perform the beamforming using the DFT beamforming method
193 * @param txMob the mobility model of the node performing the beamforming
194 * @param thisAntenna the antenna object associated to txMob
195 * @param rxMob the mobility model of the node towards which will point the beam
196 */
197static void
199{
200 // compute the azimuth and the elevation angles
201 Angles completeAngle(txMob->GetPosition(), rxMob->GetPosition());
202 double hAngleRadian = completeAngle.GetAzimuth();
203 double vAngleRadian = completeAngle.GetInclination(); // the elevation angle
204
205 // retrieve the number of antenna elements and resize the vector
206 uint64_t totNoArrayElements = thisAntenna->GetNumElems();
207 PhasedArrayModel::ComplexVector antennaWeights(totNoArrayElements);
208
209 // the total power is divided equally among the antenna elements
210 double power = 1.0 / sqrt(totNoArrayElements);
211
212 // compute the antenna weights
213 const double sinVAngleRadian = sin(vAngleRadian);
214 const double cosVAngleRadian = cos(vAngleRadian);
215 const double sinHAngleRadian = sin(hAngleRadian);
216 const double cosHAngleRadian = cos(hAngleRadian);
217
218 for (uint64_t ind = 0; ind < totNoArrayElements; ind++)
219 {
220 Vector loc = thisAntenna->GetElementLocation(ind);
221 double phase = -2 * M_PI *
222 (sinVAngleRadian * cosHAngleRadian * loc.x +
223 sinVAngleRadian * sinHAngleRadian * loc.y + cosVAngleRadian * loc.z);
224 antennaWeights[ind] = exp(std::complex<double>(0, phase)) * power;
225 }
226
227 // store the antenna weights
228 thisAntenna->SetBeamformingVector(antennaWeights);
229}
230
231/**
232 * Compute the average SNR
233 * @param params A structure that holds the parameters that are needed to perform calculations in
234 * ComputeSnr
235 */
236static void
238{
239 Ptr<SpectrumValue> txPsd = CreateTxPowerSpectralDensity(params.frequency,
240 params.txPow,
241 params.bandwidth,
242 params.resourceBlockBandwidth);
243 Ptr<SpectrumValue> rxPsd = txPsd->Copy();
244 NS_LOG_DEBUG("Average tx power " << 10 * log10(Sum(*txPsd) * params.resourceBlockBandwidth)
245 << " dB");
246
247 // create the noise PSD
248 Ptr<SpectrumValue> noisePsd = CreateNoisePowerSpectralDensity(params.frequency,
249 params.noiseFigure,
250 params.bandwidth,
251 params.resourceBlockBandwidth);
252 NS_LOG_DEBUG("Average noise power "
253 << 10 * log10(Sum(*noisePsd) * params.resourceBlockBandwidth) << " dB");
254
255 // apply the pathloss
256 double propagationGainDb = m_propagationLossModel->CalcRxPower(0, params.txMob, params.rxMob);
257 NS_LOG_DEBUG("Pathloss " << -propagationGainDb << " dB");
258 double propagationGainLinear = std::pow(10.0, (propagationGainDb) / 10.0);
259 *(rxPsd) *= propagationGainLinear;
260
261 NS_ASSERT_MSG(params.txAntenna, "params.txAntenna is nullptr!");
262 NS_ASSERT_MSG(params.rxAntenna, "params.rxAntenna is nullptr!");
263
265 rxSsp->psd = rxPsd;
266 rxSsp->txAntenna =
267 ConstCast<AntennaModel, const AntennaModel>(params.txAntenna->GetAntennaElement());
268
269 // apply the fast fading and the beamforming gain
270 rxSsp =
271 m_spectrumLossModel->CalcRxPowerSpectralDensity(rxSsp,
272 params.txMob,
273 params.rxMob,
274 params.txAntenna,
275 params.rxAntenna,
276 params.txAntenna->GetBeamformingVector(),
277 params.rxAntenna->GetBeamformingVector());
278 NS_LOG_DEBUG("Average rx power " << 10 * log10(Sum(*rxSsp->psd) * params.bandwidth) << " dB");
279
280 // compute the SNR
281 NS_LOG_DEBUG("Average SNR " << 10 * log10(Sum(*rxSsp->psd) / Sum(*noisePsd)) << " dB");
282
283 // print the SNR and pathloss values in the output file
285 << 10 * log10(Sum(*rxSsp->psd) / Sum(*noisePsd)) << " " << propagationGainDb
286 << std::endl;
287}
288
289int
290main(int argc, char* argv[])
291{
292 uint32_t simTimeMs = 1000; // simulation time in milliseconds
293 uint32_t timeResMs = 10; // time resolution in milliseconds
294
295 // Start changes with respect to three-gpp-channel-example
296 // SCENARIO 10 DL of TR 38.321
297 // This parameters can be set accordingly to 3GPP TR 38.821 or arbitrarily modified
298 std::string scenario = "NTN-Suburban"; // 3GPP propagation scenario
299 // All available NTN scenarios: DenseUrban, Urban, Suburban, Rural.
300
301 double frequencyHz = 20e9; // operating frequency in Hz
302 double bandwidthHz = 400e6; // Hz
303 double RbBandwidthHz = 120e3; // Hz
304
305 // Satellite parameters
306 double satEIRPDensity = 40; // dBW/MHz
307 double satAntennaGainDb = 58.5; // dB
308
309 // UE Parameters
310 double vsatAntennaGainDb = 39.7; // dB
311 double vsatAntennaNoiseFigureDb = 1.2; // dB
312 // End changes with respect to three-gpp-channel-example
313
314 /* Command line argument parser setup. */
315 CommandLine cmd(__FILE__);
316 cmd.AddValue("scenario",
317 "The 3GPP NTN scenario to use. Valid options are: "
318 "NTN-DenseUrban, NTN-Urban, NTN-Suburban, and NTN-Rural",
319 scenario);
320 cmd.AddValue("frequencyHz", "The carrier frequency in Hz", frequencyHz);
321 cmd.AddValue("bandwidthHz", "The bandwidth in Hz", bandwidthHz);
322 cmd.AddValue("satEIRPDensity", "The satellite EIRP density in dBW/MHz", satEIRPDensity);
323 cmd.AddValue("satAntennaGainDb", "The satellite antenna gain in dB", satAntennaGainDb);
324 cmd.AddValue("vsatAntennaGainDb", "The UE VSAT antenna gain in dB", vsatAntennaGainDb);
325 cmd.AddValue("vsatAntennaNoiseFigureDb",
326 "The UE VSAT antenna noise figure in dB",
327 vsatAntennaNoiseFigureDb);
328 cmd.Parse(argc, argv);
329
330 // Calculate transmission power in dBm using EIRPDensity + 10*log10(Bandwidth) - AntennaGain +
331 // 30
332 double txPowDbm = (satEIRPDensity + 10 * log10(bandwidthHz / 1e6) - satAntennaGainDb) + 30;
333
334 NS_LOG_DEBUG("Transmitting power: " << txPowDbm << "dBm, (" << pow(10., (txPowDbm - 30) / 10)
335 << "W)");
336
337 Config::SetDefault("ns3::ThreeGppChannelModel::UpdatePeriod",
338 TimeValue(MilliSeconds(10))); // update the channel at every 10 ms
339 Config::SetDefault("ns3::ThreeGppChannelConditionModel::UpdatePeriod",
340 TimeValue(MilliSeconds(0))); // do not update the channel condition
341
344
345 // create and configure the factories for the channel condition and propagation loss models
346 ObjectFactory propagationLossModelFactory;
347 ObjectFactory channelConditionModelFactory;
348
349 // Start changes with respect to three-gpp-channel-example
350 if (scenario == "NTN-DenseUrban")
351 {
352 propagationLossModelFactory.SetTypeId(
354 channelConditionModelFactory.SetTypeId(
356 }
357 else if (scenario == "NTN-Urban")
358 {
359 propagationLossModelFactory.SetTypeId(ThreeGppNTNUrbanPropagationLossModel::GetTypeId());
360 channelConditionModelFactory.SetTypeId(ThreeGppNTNUrbanChannelConditionModel::GetTypeId());
361 }
362 else if (scenario == "NTN-Suburban")
363 {
365 channelConditionModelFactory.SetTypeId(
367 }
368 else if (scenario == "NTN-Rural")
369 {
370 propagationLossModelFactory.SetTypeId(ThreeGppNTNRuralPropagationLossModel::GetTypeId());
371 channelConditionModelFactory.SetTypeId(ThreeGppNTNRuralChannelConditionModel::GetTypeId());
372 }
373 else
374 {
375 NS_FATAL_ERROR("Unknown NTN scenario");
376 }
377 // End changes with respect to three-gpp-channel-example
378
379 // create the propagation loss model
380 m_propagationLossModel = propagationLossModelFactory.Create<ThreeGppPropagationLossModel>();
381 m_propagationLossModel->SetAttribute("Frequency", DoubleValue(frequencyHz));
382 m_propagationLossModel->SetAttribute("ShadowingEnabled", BooleanValue(true));
383
384 // create the spectrum propagation loss model
386 m_spectrumLossModel->SetChannelModelAttribute("Frequency", DoubleValue(frequencyHz));
387 m_spectrumLossModel->SetChannelModelAttribute("Scenario", StringValue(scenario));
388
389 // create the channel condition model and associate it with the spectrum and
390 // propagation loss model
392 channelConditionModelFactory.Create<ThreeGppChannelConditionModel>();
393 m_spectrumLossModel->SetChannelModelAttribute("ChannelConditionModel", PointerValue(condModel));
394 m_propagationLossModel->SetChannelConditionModel(condModel);
395
396 // create the tx and rx nodes
398 nodes.Create(2);
399
400 // Start changes with respect to three-gpp-channel-example, here a mobility model
401 // tailored to NTN scenarios is used (GeocentricConstantPositionMobilityModel)
402 // create the tx and rx mobility models, set the positions
407
408 txMob->SetGeographicPosition(Vector(45.40869, 11.89448, 35786000)); // GEO over Padova
409 rxMob->SetGeographicPosition(Vector(45.40869, 11.89448, 14.0)); // Padova Coordinates
410
411 // This is not strictly necessary, but is useful to have "sensible" values when using
412 // GetPosition()
413 txMob->SetCoordinateTranslationReferencePoint(
414 Vector(45.40869, 11.89448, 0.0)); // Padova Coordinates without altitude
415 rxMob->SetCoordinateTranslationReferencePoint(
416 Vector(45.40869, 11.89448, 0.0)); // Padova Coordinates without altitude
417 // End changes with respect to three-gpp-channel-example,
418
419 NS_LOG_DEBUG("TX Position: " << txMob->GetPosition());
420 NS_LOG_DEBUG("RX Position: " << rxMob->GetPosition());
421
422 // assign the mobility models to the nodes
423 nodes.Get(0)->AggregateObject(txMob);
424 nodes.Get(1)->AggregateObject(rxMob);
425
426 // Start changes with respect to three-gpp-channel-example,
427 // Here antenna models mimic the gain achieved by the CircularApertureAntennaModel,
428 // which is not used to avoid inheriting the latter's dependence on either libstdc++
429 // or Boost.
431 "NumColumns",
432 UintegerValue(1),
433 "NumRows",
434 UintegerValue(1),
435 "AntennaElement",
438 DoubleValue(satAntennaGainDb))));
439
441 "NumColumns",
442 UintegerValue(1),
443 "NumRows",
444 UintegerValue(1),
445 "AntennaElement",
448 DoubleValue(vsatAntennaGainDb))));
449 // End changes with respect to three-gpp-channel-example
450
451 // set the beamforming vectors
452 DoBeamforming(rxMob, rxAntenna, txMob);
453 DoBeamforming(txMob, txAntenna, rxMob);
454
455 // Open the output results file
456 resultsFile.open("ntn-snr-trace.txt", std::ios::out);
457 NS_ASSERT_MSG(resultsFile.is_open(), "Results file could not be created");
458
459 for (int i = 0; i < floor(simTimeMs / timeResMs); i++)
460 {
461 Simulator::Schedule(MilliSeconds(timeResMs * i),
462 &ComputeSnr,
463 ComputeSnrParams(txMob,
464 rxMob,
465 txPowDbm,
466 vsatAntennaNoiseFigureDb,
467 txAntenna,
468 rxAntenna,
469 frequencyHz,
470 bandwidthHz,
471 RbBandwidthHz));
472 }
473
476
477 resultsFile.close();
478
479 return 0;
480}
Class holding the azimuth and inclination angles of spherical coordinates.
Definition angles.h:107
double GetInclination() const
Getter for inclination angle.
Definition angles.cc:236
double GetAzimuth() const
Getter for azimuth angle.
Definition angles.cc:230
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
keep track of a set of node pointers.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
ComplexMatrixArray ComplexVector
the underlying Valarray
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
Hold variables of type string.
Definition string.h:45
Base class for the 3GPP channel condition models.
Base class for the 3GPP propagation models.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:398
Hold an unsigned integer type.
Definition uinteger.h:34
#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:76
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:886
#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:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1330
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::vector< BandInfo > Bands
Container of BandInfo.
Ptr< T1 > ConstCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:631
double Sum(const SpectrumValue &x)
A structure that holds the parameters for the ComputeSnr function.
ComputeSnrParams(Ptr< MobilityModel > pTxMob, Ptr< MobilityModel > pRxMob, double pTxPow, double pNoiseFigure, Ptr< PhasedArrayModel > pTxAntenna, Ptr< PhasedArrayModel > pRxAntenna, double pFrequency, double pBandwidth, double pResourceBlockBandwidth)
Constructor.
Ptr< PhasedArrayModel > txAntenna
the tx antenna array
Ptr< MobilityModel > rxMob
the rx mobility model
double bandwidth
the total bandwidth in Hz
double noiseFigure
the noise figure in dB
double resourceBlockBandwidth
the Resource Block bandwidth in Hz
double txPow
the tx power in dBm
double frequency
the carrier frequency in Hz
Ptr< PhasedArrayModel > rxAntenna
the rx antenna array
Ptr< MobilityModel > txMob
the tx mobility model
The building block of a SpectrumModel.
double fc
center frequency
double fl
lower limit of subband
double fh
upper limit of subband
Ptr< SpectrumValue > CreateNoisePowerSpectralDensity(double fcHz, double noiseFigureDb, double bwHz, double rbWidthHz)
Create the noise PSD for the.
static std::ofstream resultsFile
The results file.
static void ComputeSnr(ComputeSnrParams &params)
Compute the average SNR.
static void DoBeamforming(Ptr< MobilityModel > rxMob, Ptr< PhasedArrayModel > thisAntenna, Ptr< MobilityModel > txMob)
Perform the beamforming using the DFT beamforming method.
Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double fcHz, double pwrDbm, double bwHz, double rbWidthHz)
Create the PSD for the TX.
static Ptr< ThreeGppPropagationLossModel > m_propagationLossModel
the log component
static void ComputeSnr(const ComputeSnrParams &params)
Compute the average SNR.
static Ptr< ThreeGppSpectrumPropagationLossModel > m_spectrumLossModel
the SpectrumPropagationLossModel object