A Discrete-Event Network Simulator
API
main-propagation-loss.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2008 Timo Bingmann
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Timo Bingmann <timo.bingmann@student.kit.edu>
19 */
20
21#include "ns3/propagation-loss-model.h"
22#include "ns3/jakes-propagation-loss-model.h"
23#include "ns3/constant-position-mobility-model.h"
24
25#include "ns3/config.h"
26#include "ns3/command-line.h"
27#include "ns3/string.h"
28#include "ns3/boolean.h"
29#include "ns3/double.h"
30#include "ns3/pointer.h"
31#include "ns3/gnuplot.h"
32#include "ns3/simulator.h"
33
34#include <map>
35
36using namespace ns3;
37
46static double dround (double number, double precision)
47{
48 number /= precision;
49 if (number >= 0)
50 {
51 number = floor (number + 0.5);
52 }
53 else
54 {
55 number = ceil (number - 0.5);
56 }
57 number *= precision;
58 return number;
59}
60
69static Gnuplot
70TestDeterministic (Ptr<PropagationLossModel> model, double targetDistance, double step)
71{
72 Ptr<ConstantPositionMobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
73 Ptr<ConstantPositionMobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
74
75 Gnuplot plot;
76
77 plot.AppendExtra ("set xlabel 'Distance'");
78 plot.AppendExtra ("set ylabel 'rxPower (dBm)'");
79 plot.AppendExtra ("set key top right");
80
81 double txPowerDbm = +20; // dBm
82
83 Gnuplot2dDataset dataset;
84
85 dataset.SetStyle (Gnuplot2dDataset::LINES);
86
87 {
88 a->SetPosition (Vector (0.0, 0.0, 0.0));
89
90 for (double distance = 0.0; distance < targetDistance; distance += step)
91 {
92 b->SetPosition (Vector (distance, 0.0, 0.0));
93
94 // CalcRxPower() returns dBm.
95 double rxPowerDbm = model->CalcRxPower (txPowerDbm, a, b);
96
97 dataset.Add (distance, rxPowerDbm);
98
99 Simulator::Stop (Seconds (1.0));
101 }
102 }
103
104 std::ostringstream os;
105 os << "txPower " << txPowerDbm << "dBm";
106 dataset.SetTitle (os.str ());
107
108 plot.AddDataset (dataset);
109
110 plot.AddDataset ( Gnuplot2dFunction ("-94 dBm CSThreshold", "-94.0") );
111
112 return plot;
113}
114
124static Gnuplot
125TestProbabilistic (Ptr<PropagationLossModel> model, double targetDistance, double step, unsigned int samples)
126{
127 Ptr<ConstantPositionMobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
128 Ptr<ConstantPositionMobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
129
130 Gnuplot plot;
131
132 plot.AppendExtra ("set xlabel 'Distance'");
133 plot.AppendExtra ("set ylabel 'rxPower (dBm)'");
134 plot.AppendExtra ("set zlabel 'Probability' offset 0,+10");
135 plot.AppendExtra ("set view 50, 120, 1.0, 1.0");
136 plot.AppendExtra ("set key top right");
137
138 plot.AppendExtra ("set ticslevel 0");
139 plot.AppendExtra ("set xtics offset -0.5,0");
140 plot.AppendExtra ("set ytics offset 0,-0.5");
141 plot.AppendExtra ("set xrange [100:]");
142
143 double txPowerDbm = +20; // dBm
144
145 Gnuplot3dDataset dataset;
146
147 dataset.SetStyle ("with linespoints");
148 dataset.SetExtra ("pointtype 3 pointsize 0.5");
149
150 typedef std::map<double, unsigned int> rxPowerMapType;
151
152 // Take given number of samples from CalcRxPower() and show probability
153 // density for discrete distances.
154 {
155 a->SetPosition (Vector (0.0, 0.0, 0.0));
156
157 for (double distance = 100.0; distance < targetDistance; distance += step)
158 {
159 b->SetPosition (Vector (distance, 0.0, 0.0));
160
161 rxPowerMapType rxPowerMap;
162
163 for (unsigned int samp = 0; samp < samples; ++samp)
164 {
165 // CalcRxPower() returns dBm.
166 double rxPowerDbm = model->CalcRxPower (txPowerDbm, a, b);
167 rxPowerDbm = dround (rxPowerDbm, 1.0);
168
169 rxPowerMap[ rxPowerDbm ]++;
170
171 Simulator::Stop (Seconds (0.01));
173 }
174
175 for (rxPowerMapType::const_iterator i = rxPowerMap.begin ();
176 i != rxPowerMap.end (); ++i)
177 {
178 dataset.Add (distance, i->first, (double)i->second / (double)samples);
179 }
180 dataset.AddEmptyLine ();
181 }
182 }
183
184 std::ostringstream os;
185 os << "txPower " << txPowerDbm << "dBm";
186 dataset.SetTitle (os.str ());
187
188 plot.AddDataset (dataset);
189
190 return plot;
191}
192
202static Gnuplot
204 Time timeStep,
205 Time timeTotal,
206 double distance)
207{
208 Ptr<ConstantPositionMobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
209 Ptr<ConstantPositionMobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
210
211 Gnuplot plot;
212
213 plot.AppendExtra ("set xlabel 'Time (s)'");
214 plot.AppendExtra ("set ylabel 'rxPower (dBm)'");
215 plot.AppendExtra ("set key center right");
216
217 double txPowerDbm = +20; // dBm
218
219 Gnuplot2dDataset dataset;
220
221 dataset.SetStyle (Gnuplot2dDataset::LINES);
222
223 {
224 a->SetPosition (Vector (0.0, 0.0, 0.0));
225 b->SetPosition (Vector (distance, 0.0, 0.0));
226
228 while( Simulator::Now () < start + timeTotal )
229 {
230 // CalcRxPower() returns dBm.
231 double rxPowerDbm = model->CalcRxPower (txPowerDbm, a, b);
232
233 Time elapsed = Simulator::Now () - start;
234 dataset.Add (elapsed.GetSeconds (), rxPowerDbm);
235
236 Simulator::Stop (timeStep);
238 }
239 }
240
241 std::ostringstream os;
242 os << "txPower " << txPowerDbm << "dBm";
243 dataset.SetTitle (os.str ());
244
245 plot.AddDataset (dataset);
246
247 plot.AddDataset ( Gnuplot2dFunction ("-94 dBm CSThreshold", "-94.0") );
248
249 return plot;
250}
251
252int main (int argc, char *argv[])
253{
254 bool test = false;
255 CommandLine cmd (__FILE__);
256 cmd.AddValue ("test", "Run as a test, sample the models only once", test);
257 cmd.Parse (argc, argv);
258
259 double testDeterministicDistance = 2500.0;
260 double testProbabilisticDistance = 2500.0;
261 unsigned int testProbabilisticSamples = 100000;
262 Time testJakesTimeOneMsRes = Seconds (1.0);
263 Time testJakesTimeZeroDotOneMsRes = Seconds (0.1);
264
265 if (test)
266 {
267 testDeterministicDistance = 10;
268 testProbabilisticDistance = 200;
269 testProbabilisticSamples = 1;
270 testJakesTimeOneMsRes = Seconds (0.001);
271 testJakesTimeZeroDotOneMsRes = Seconds (0.0001);
272 }
273
274 GnuplotCollection gnuplots ("main-propagation-loss.pdf");
275
276 {
277 Ptr<FriisPropagationLossModel> friis = CreateObject<FriisPropagationLossModel> ();
278
279 Gnuplot plot = TestDeterministic (friis, testDeterministicDistance, 10.0);
280 plot.SetTitle ("ns3::FriisPropagationLossModel (Default Parameters)");
281 gnuplots.AddPlot (plot);
282 }
283
284 {
285 Ptr<LogDistancePropagationLossModel> log = CreateObject<LogDistancePropagationLossModel> ();
286 log->SetAttribute ("Exponent", DoubleValue (2.5));
287
288 Gnuplot plot = TestDeterministic (log, testDeterministicDistance, 10.0);
289 plot.SetTitle ("ns3::LogDistancePropagationLossModel (Exponent = 2.5)");
290 gnuplots.AddPlot (plot);
291 }
292
293 {
294 Ptr<RandomPropagationLossModel> random = CreateObject<RandomPropagationLossModel> ();
295 Ptr<ExponentialRandomVariable> expVar = CreateObjectWithAttributes<ExponentialRandomVariable> ("Mean", DoubleValue (50.0));
296 random->SetAttribute ("Variable", PointerValue (expVar));
297
298 Gnuplot plot = TestDeterministic (random, testDeterministicDistance, 10.0);
299 plot.SetTitle ("ns3::RandomPropagationLossModel with Exponential Distribution");
300 gnuplots.AddPlot (plot);
301 }
302
303 {
304 Ptr<JakesPropagationLossModel> jakes = CreateObject<JakesPropagationLossModel> ();
305
306 // doppler frequency shift for 5.15 GHz at 100 km/h
307 Config::SetDefault ("ns3::JakesProcess::DopplerFrequencyHz", DoubleValue (477.9));
308
309 Gnuplot plot = TestDeterministicByTime (jakes, Seconds (0.001), testJakesTimeOneMsRes, 100.0);
310 plot.SetTitle ("ns3::JakesPropagationLossModel (with 477.9 Hz shift and 1 millisec resolution)");
311 gnuplots.AddPlot (plot);
312 // Usually objects are aggregated either to a Node or a Channel, and this aggregation ensures
313 // a proper call to Dispose.
314 // Here we must call it manually, since the PropagationLossModel is not aggregated to anything.
315 jakes->Dispose ();
316 }
317
318 {
319 Ptr<JakesPropagationLossModel> jakes = CreateObject<JakesPropagationLossModel> ();
320
321 // doppler frequency shift for 5.15 GHz at 100 km/h
322 Config::SetDefault ("ns3::JakesProcess::DopplerFrequencyHz", DoubleValue (477.9));
323
324 Gnuplot plot = TestDeterministicByTime (jakes, Seconds (0.0001), testJakesTimeZeroDotOneMsRes, 100.0);
325 plot.SetTitle ("ns3::JakesPropagationLossModel (with 477.9 Hz shift and 0.1 millisec resolution)");
326 gnuplots.AddPlot (plot);
327 // Usually objects are aggregated either to a Node or a Channel, and this aggregation ensures
328 // a proper call to Dispose.
329 // Here we must call it manually, since the PropagationLossModel is not aggregated to anything.
330 jakes->Dispose ();
331 }
332
333 {
334 Ptr<ThreeLogDistancePropagationLossModel> log3 = CreateObject<ThreeLogDistancePropagationLossModel> ();
335
336 Gnuplot plot = TestDeterministic (log3, testDeterministicDistance, 10.0);
337 plot.SetTitle ("ns3::ThreeLogDistancePropagationLossModel (Defaults)");
338 gnuplots.AddPlot (plot);
339 }
340
341 {
342 Ptr<ThreeLogDistancePropagationLossModel> log3 = CreateObject<ThreeLogDistancePropagationLossModel> ();
343 // more prominent example values:
344 log3->SetAttribute ("Exponent0", DoubleValue (1.0));
345 log3->SetAttribute ("Exponent1", DoubleValue (3.0));
346 log3->SetAttribute ("Exponent2", DoubleValue (10.0));
347
348 Gnuplot plot = TestDeterministic (log3, testDeterministicDistance, 10.0);
349 plot.SetTitle ("ns3::ThreeLogDistancePropagationLossModel (Exponents 1.0, 3.0 and 10.0)");
350 gnuplots.AddPlot (plot);
351 }
352
353 {
354 Ptr<NakagamiPropagationLossModel> nak = CreateObject<NakagamiPropagationLossModel> ();
355
356 Gnuplot plot = TestProbabilistic (nak, testProbabilisticDistance, 100.0, testProbabilisticSamples);
357 plot.SetTitle ("ns3::NakagamiPropagationLossModel (Default Parameters)");
358 gnuplots.AddPlot (plot);
359 }
360
361 {
362 Ptr<ThreeLogDistancePropagationLossModel> log3 = CreateObject<ThreeLogDistancePropagationLossModel> ();
363
364 Ptr<NakagamiPropagationLossModel> nak = CreateObject<NakagamiPropagationLossModel> ();
365 log3->SetNext (nak);
366
367 Gnuplot plot = TestProbabilistic (log3, testProbabilisticDistance, 100.0, testProbabilisticSamples);
368 plot.SetTitle ("ns3::ThreeLogDistancePropagationLossModel and ns3::NakagamiPropagationLossModel (Default Parameters)");
369 gnuplots.AddPlot (plot);
370 }
371
372 gnuplots.GenerateOutput (std::cout);
373
374 // produce clean valgrind
375 Simulator::Destroy ();
376 return 0;
377}
void Run(ObjectFactory &factory, uint32_t pop, uint32_t total, uint32_t runs, Ptr< RandomVariableStream > eventStream, bool calRev)
Perform the runs for a single scheduler type.
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Class to represent a 2D points plot.
Definition: gnuplot.h:118
void SetStyle(enum Style style)
Definition: gnuplot.cc:344
void Add(double x, double y)
Definition: gnuplot.cc:361
Class to represent a 2D function expression plot.
Definition: gnuplot.h:244
Class to represent a 3D points plot.
Definition: gnuplot.h:273
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition: gnuplot.cc:606
void Add(double x, double y, double z)
Definition: gnuplot.cc:595
void SetStyle(const std::string &style)
Definition: gnuplot.cc:589
a simple class to group together multiple gnuplots into one file, e.g.
Definition: gnuplot.h:488
void SetExtra(const std::string &extra)
Add extra formatting parameters to this dataset.
Definition: gnuplot.cc:152
void SetTitle(const std::string &title)
Change line title.
Definition: gnuplot.cc:141
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:372
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:758
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:751
void SetTitle(const std::string &title)
Definition: gnuplot.cc:732
Hold objects of type Ptr<T>.
Definition: pointer.h:37
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.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1245
static double dround(double number, double precision)
Round a double number to the given precision.
static Gnuplot TestDeterministicByTime(Ptr< PropagationLossModel > model, Time timeStep, Time timeTotal, double distance)
Test the model by sampling over time.
static Gnuplot TestDeterministic(Ptr< PropagationLossModel > model, double targetDistance, double step)
Test the model by sampling over a distance.
static Gnuplot TestProbabilistic(Ptr< PropagationLossModel > model, double targetDistance, double step, unsigned int samples)
Test the model by sampling over a distance.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:43
def start()
Definition: core.py:1852