A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
three-gpp-v2v-channel-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, University of Padova, Dep. of Information Engineering, SIGNET lab
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8/*
9 * @file
10 * This is an example of how to configure the channel model classes to simulate
11 * a vehicular environment.
12 * The channel condition is determined using the model specified in [1], Table 6.2-1.
13 * The pathloss is determined using the model specified in [1], Table 6.2.1-1.
14 * The model for the fast fading is the one described in 3GPP TR 38.901 v15.0.0,
15 * the model parameters are those specified in [1], Table 6.2.3-1.
16 *
17 * This example generates the output file 'three-gpp-v2v-channel-example-output.txt'. Each row of
18 * the file is organized as follows: Time[s] TxPosX[m] TxPosY[m] RxPosX[m] RxPosY[m] ChannelState
19 * SNR[dB] Pathloss[dB] We also provide a bash script which reads the output file and generates two
20 * figures:
21 * (i) three-gpp-v2v-channel-example-map.gif, a GIF representing the simulation scenario and vehicle
22 * mobility;
23 * (ii) three-gpp-v2v-channel-example-snr.png, which represents the behavior of the SNR.
24 *
25 * [1] 3GPP TR 37.885, v15.3.0
26 */
27
28#include "ns3/buildings-module.h"
29#include "ns3/core-module.h"
30#include "ns3/mobility-module.h"
31#include "ns3/network-module.h"
32#include "ns3/spectrum-signal-parameters.h"
33#include "ns3/three-gpp-channel-model.h"
34#include "ns3/three-gpp-spectrum-propagation-loss-model.h"
35#include "ns3/three-gpp-v2v-propagation-loss-model.h"
36#include "ns3/uniform-planar-array.h"
37
38#include <fstream>
39
40using namespace ns3;
41
42/// the log component
43NS_LOG_COMPONENT_DEFINE("ThreeGppV2vChannelExample");
44
46 m_propagationLossModel; //!< the PropagationLossModel object
48 m_spectrumLossModel; //!< the SpectrumPropagationLossModel object
49static Ptr<ChannelConditionModel> m_condModel; //!< the ChannelConditionModel object
50
51/**
52 * @brief A structure that holds the parameters for the ComputeSnr
53 * function. In this way the problem with the limited
54 * number of parameters of method Schedule is avoided.
55 */
57{
58 Ptr<MobilityModel> txMob; //!< the tx mobility model
59 Ptr<MobilityModel> rxMob; //!< the rx mobility model
60 Ptr<SpectrumSignalParameters> txParams; //!< the params of the tx signal
61 double noiseFigure; //!< the noise figure in dB
62 Ptr<PhasedArrayModel> txAntenna; //!< the tx antenna array
63 Ptr<PhasedArrayModel> rxAntenna; //!< the rx antenna array
64};
65
66/**
67 * Perform the beamforming using the DFT beamforming method
68 * @param txMob the mobility model of the node performing the beamforming
69 * @param thisAntenna the antenna object associated to thisDevice
70 * @param rxMob the mobility model of the node towards which will point the beam
71 */
72static void
74{
75 // compute the azimuth and the elevation angles
76 Angles completeAngle(rxMob->GetPosition(), txMob->GetPosition());
77 PhasedArrayModel::ComplexVector bf = thisAntenna->GetBeamformingVector(completeAngle);
78 thisAntenna->SetBeamformingVector(bf);
79}
80
81/**
82 * Compute the average SNR
83 * @param params A structure that holds a bunch of parameters needed by ComputeSnr function to
84 * calculate the average SNR
85 */
86static void
88{
89 // check the channel condition
90 Ptr<ChannelCondition> cond = m_condModel->GetChannelCondition(params.txMob, params.rxMob);
91 // apply the pathloss
92 double propagationGainDb = m_propagationLossModel->CalcRxPower(0, params.txMob, params.rxMob);
93 NS_LOG_DEBUG("Pathloss " << -propagationGainDb << " dB");
94 double propagationGainLinear = std::pow(10.0, (propagationGainDb) / 10.0);
95 *(params.txParams->psd) *= propagationGainLinear;
96 // apply the fast fading and the beamforming gain
97 auto rxParams =
98 m_spectrumLossModel->CalcRxPowerSpectralDensity(params.txParams,
99 params.txMob,
100 params.rxMob,
101 params.txAntenna,
102 params.rxAntenna,
103 params.txAntenna->GetBeamformingVector(),
104 params.rxAntenna->GetBeamformingVector());
105 Ptr<SpectrumValue> rxPsd = rxParams->psd;
106 NS_LOG_DEBUG("Average rx power " << 10 * log10(Sum(*rxPsd) * 180e3) << " dB");
107
108 // create the noise psd
109 // taken from lte-spectrum-value-helper
110 const double kT_dBm_Hz = -174.0; // dBm/Hz
111 double kT_W_Hz = std::pow(10.0, (kT_dBm_Hz - 30) / 10.0);
112 double noiseFigureLinear = std::pow(10.0, params.noiseFigure / 10.0);
113 double noisePowerSpectralDensity = kT_W_Hz * noiseFigureLinear;
114 Ptr<SpectrumValue> noisePsd = Create<SpectrumValue>(params.txParams->psd->GetSpectrumModel());
115 (*noisePsd) = noisePowerSpectralDensity;
116 // log the average SNR
117 NS_LOG_DEBUG("Average SNR " << 10 * log10(Sum(*rxPsd) / Sum(*noisePsd)) << " dB");
118 // print the SNR and pathloss values in the three-gpp-v2v-channel-example-output.txt file
119 std::ofstream f;
120 f.open("three-gpp-v2v-channel-example-output.txt", std::ios::out | std::ios::app);
121 f << Simulator::Now().GetSeconds() << " " // time [s]
122 << params.txMob->GetPosition().x << " " << params.txMob->GetPosition().y << " "
123 << params.rxMob->GetPosition().x << " " << params.rxMob->GetPosition().y << " "
124 << cond->GetLosCondition() << " " // channel state
125 << 10 * log10(Sum(*rxPsd) / Sum(*noisePsd)) << " " // SNR [dB]
126 << -propagationGainDb << std::endl; // pathloss [dB]
127 f.close();
128}
129
130/**
131 * Generates a GNU-plottable file representing the buildings deployed in the
132 * scenario
133 * @param filename the name of the output file
134 */
135void
137{
138 std::ofstream outFile;
139 outFile.open(filename, std::ios_base::out | std::ios_base::trunc);
140 if (!outFile.is_open())
141 {
142 NS_LOG_ERROR("Can't open file " << filename);
143 return;
144 }
145 for (auto it = BuildingList::Begin(); it != BuildingList::End(); ++it)
146 {
147 Box box = (*it)->GetBoundaries();
148 outFile << box.xMin << " " << box.yMin << " " << box.xMax << " " << box.yMax << std::endl;
149 }
150}
151
152int
153main(int argc, char* argv[])
154{
155 double frequency = 28.0e9; // operating frequency in Hz
156 double txPow_dbm = 30.0; // tx power in dBm
157 double noiseFigure = 9.0; // noise figure in dB
158 Time simTime = Seconds(40); // simulation time
159 Time timeRes = MilliSeconds(10); // time resolution
160 std::string scenario = "V2V-Urban"; // 3GPP propagation scenario, V2V-Urban or V2V-Highway
161 double vScatt = 0; // maximum speed of the vehicles in the scenario [m/s]
162 double subCarrierSpacing = 60e3; // subcarrier spacing in kHz
163 uint32_t numRb = 275; // number of resource blocks
164 uint32_t updatePeriodMs = 0;
165
166 CommandLine cmd(__FILE__);
167 cmd.AddValue("frequency", "operating frequency in Hz", frequency);
168 cmd.AddValue("txPow", "tx power in dBm", txPow_dbm);
169 cmd.AddValue("noiseFigure", "noise figure in dB", noiseFigure);
170 cmd.AddValue("scenario", "3GPP propagation scenario, V2V-Urban or V2V-Highway", scenario);
171 cmd.AddValue("updatePeriodMs", "the channel update period", updatePeriodMs);
172 cmd.Parse(argc, argv);
173
174 // create the nodes
176 nodes.Create(2);
177
178 // create the antenna objects and set their dimensions
179 Ptr<PhasedArrayModel> txAntenna =
181 UintegerValue(2),
182 "NumRows",
183 UintegerValue(2),
184 "BearingAngle",
185 DoubleValue(-M_PI / 2));
186 Ptr<PhasedArrayModel> rxAntenna =
188 UintegerValue(2),
189 "NumRows",
190 UintegerValue(2),
191 "BearingAngle",
192 DoubleValue(M_PI / 2));
193
194 Ptr<MobilityModel> txMob;
195 Ptr<MobilityModel> rxMob;
196 if (scenario == "V2V-Urban")
197 {
198 // 3GPP defines that the maximum speed in urban scenario is 60 km/h
199 vScatt = 60 / 3.6;
200
201 // create a grid of buildings
202 double buildingSizeX = 250 - 3.5 * 2 - 3; // m
203 double buildingSizeY = 433 - 3.5 * 2 - 3; // m
204 double streetWidth = 20; // m
205 double buildingHeight = 10; // m
206 uint32_t numBuildingsX = 2;
207 uint32_t numBuildingsY = 2;
208 double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
209 double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
210
211 std::vector<Ptr<Building>> buildingVector;
212 for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
213 {
214 for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
215 {
216 Ptr<Building> building;
217 building = CreateObject<Building>();
218
219 building->SetBoundaries(
220 Box(buildingIdX * (buildingSizeX + streetWidth),
221 buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
222 buildingIdY * (buildingSizeY + streetWidth),
223 buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
224 0.0,
225 buildingHeight));
226 building->SetNRoomsX(1);
227 building->SetNRoomsY(1);
228 building->SetNFloors(1);
229 buildingVector.push_back(building);
230 }
231 }
232
233 // set the mobility model
234 double vTx = vScatt;
235 double vRx = vScatt / 2;
238 Time nextWaypoint;
239 txMob->GetObject<WaypointMobilityModel>()->AddWaypoint(
240 Waypoint(nextWaypoint, Vector(maxAxisX / 2 - streetWidth / 2, 1.0, 1.5)));
241 nextWaypoint += Seconds((maxAxisY - streetWidth) / 2 / vTx);
242 txMob->GetObject<WaypointMobilityModel>()->AddWaypoint(
243 Waypoint(nextWaypoint,
244 Vector(maxAxisX / 2 - streetWidth / 2, maxAxisY / 2 - streetWidth / 2, 1.5)));
245 nextWaypoint += Seconds((maxAxisX - streetWidth) / 2 / vTx);
246 txMob->GetObject<WaypointMobilityModel>()->AddWaypoint(
247 Waypoint(nextWaypoint, Vector(0.0, maxAxisY / 2 - streetWidth / 2, 1.5)));
248 nextWaypoint = Seconds(0);
249 rxMob->GetObject<WaypointMobilityModel>()->AddWaypoint(
250 Waypoint(nextWaypoint, Vector(maxAxisX / 2 - streetWidth / 2, 0.0, 1.5)));
251 nextWaypoint += Seconds(maxAxisY / vRx);
252 rxMob->GetObject<WaypointMobilityModel>()->AddWaypoint(
253 Waypoint(nextWaypoint, Vector(maxAxisX / 2 - streetWidth / 2, maxAxisY, 1.5)));
254
255 nodes.Get(0)->AggregateObject(txMob);
256 nodes.Get(1)->AggregateObject(rxMob);
257
258 // create the channel condition model
260
261 // create the propagation loss model
263 }
264 else if (scenario == "V2V-Highway")
265 {
266 // Two vehicles are travelling one behid the other with constant velocity
267 // along the y axis. The distance between the two vehicles is 20 meters.
268
269 // 3GPP defines that the maximum speed in urban scenario is 140 km/h
270 vScatt = 140 / 3.6;
271 double vTx = vScatt;
272 double vRx = vScatt / 2;
273
276 txMob->GetObject<ConstantVelocityMobilityModel>()->SetPosition(Vector(300.0, 20.0, 1.5));
277 txMob->GetObject<ConstantVelocityMobilityModel>()->SetVelocity(Vector(0.0, vTx, 0.0));
278 rxMob->GetObject<ConstantVelocityMobilityModel>()->SetPosition(Vector(300.0, 0.0, 1.5));
279 rxMob->GetObject<ConstantVelocityMobilityModel>()->SetVelocity(Vector(0.0, vRx, 0.0));
280
281 nodes.Get(0)->AggregateObject(txMob);
282 nodes.Get(1)->AggregateObject(rxMob);
283
284 // create the channel condition model
286
287 // create the propagation loss model
289 }
290 else
291 {
292 NS_FATAL_ERROR("Unknown scenario");
293 }
294
295 m_condModel->SetAttribute("UpdatePeriod", TimeValue(MilliSeconds(100)));
296
297 m_propagationLossModel->SetAttribute("Frequency", DoubleValue(frequency));
298 m_propagationLossModel->SetAttribute("ShadowingEnabled", BooleanValue(false));
299 m_propagationLossModel->SetAttribute("ChannelConditionModel", PointerValue(m_condModel));
300
301 // create the channel model
303 channelModel->SetAttribute("Scenario", StringValue(scenario));
304 channelModel->SetAttribute("Frequency", DoubleValue(frequency));
305 channelModel->SetAttribute("ChannelConditionModel", PointerValue(m_condModel));
306 channelModel->SetAttribute("vScatt", DoubleValue(vScatt));
307 channelModel->SetAttribute("UpdatePeriod", TimeValue(MilliSeconds(updatePeriodMs)));
308
309 // create the spectrum propagation loss model
311 "ChannelModel",
312 PointerValue(channelModel));
313
315
316 // set the beamforming vectors
317 DoBeamforming(txMob, txAntenna, rxMob);
318 DoBeamforming(rxMob, rxAntenna, txMob);
319
320 // create the tx power spectral density
321 Bands rbs;
322 double freqSubBand = frequency;
323 for (uint32_t n = 0; n < numRb; ++n)
324 {
325 BandInfo rb;
326 rb.fl = freqSubBand;
327 freqSubBand += (subCarrierSpacing * 12) / 2;
328 rb.fc = freqSubBand;
329 freqSubBand += (subCarrierSpacing * 12) / 2;
330 rb.fh = freqSubBand;
331 rbs.push_back(rb);
332 }
333 Ptr<SpectrumModel> spectrumModel = Create<SpectrumModel>(rbs);
334 Ptr<SpectrumValue> txPsd = Create<SpectrumValue>(spectrumModel);
336 double txPow_w = std::pow(10., (txPow_dbm - 30) / 10);
337 double txPowDens = (txPow_w / (numRb * subCarrierSpacing * 12));
338 (*txPsd) = txPowDens;
339 txParams->psd = txPsd->Copy();
340
341 for (int i = 0; i < simTime / timeRes; i++)
342 {
343 ComputeSnrParams params{txMob, rxMob, txParams->Copy(), noiseFigure, txAntenna, rxAntenna};
344 Simulator::Schedule(timeRes * i, &ComputeSnr, params);
345 }
346
347 // initialize the output file
348 std::ofstream f;
349 f.open("three-gpp-v2v-channel-example-output.txt", std::ios::out);
350 f << "Time[s] TxPosX[m] TxPosY[m] RxPosX[m] RxPosY[m] ChannelState SNR[dB] Pathloss[dB]"
351 << std::endl;
352 f.close();
353
354 // print the list of buildings to file
355 PrintGnuplottableBuildingListToFile("three-gpp-v2v-channel-example-buildings.txt");
356
359 return 0;
360}
Class holding the azimuth and inclination angles of spherical coordinates.
Definition angles.h:107
a 3d box
Definition box.h:24
double yMax
The y coordinate of the top bound of the box.
Definition box.h:105
double xMin
The x coordinate of the left bound of the box.
Definition box.h:99
double yMin
The y coordinate of the bottom bound of the box.
Definition box.h:103
double xMax
The x coordinate of the right bound of the box.
Definition box.h:101
static Iterator End()
static Iterator Begin()
static void Install(Ptr< Node > node)
Install the MobilityBuildingInfo to a node.
Parse command-line arguments.
Mobility model for which the current speed does not change once it has been set and until it is set a...
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.
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 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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
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
a (time, location) pair.
Definition waypoint.h:25
Waypoint-based mobility model.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:253
#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 Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
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.
double Sum(const SpectrumValue &x)
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
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
Ptr< SpectrumSignalParameters > txParams
the params of the tx signal
double noiseFigure
the noise figure in dB
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
static Ptr< ChannelConditionModel > m_condModel
the ChannelConditionModel object
static Ptr< ThreeGppPropagationLossModel > m_propagationLossModel
the log component
static void ComputeSnr(const ComputeSnrParams &params)
Compute the average SNR.
static void DoBeamforming(Ptr< MobilityModel > txMob, Ptr< PhasedArrayModel > thisAntenna, Ptr< MobilityModel > rxMob)
Perform the beamforming using the DFT beamforming method.
void PrintGnuplottableBuildingListToFile(std::string filename)
Generates a GNU-plottable file representing the buildings deployed in the scenario.
static Ptr< ThreeGppSpectrumPropagationLossModel > m_spectrumLossModel
the SpectrumPropagationLossModel object