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 
36 using namespace ns3;
37 
40 static double dround (double number, double precision)
41 {
42  number /= precision;
43  if (number >= 0)
44  {
45  number = floor (number + 0.5);
46  }
47  else
48  {
49  number = ceil (number - 0.5);
50  }
51  number *= precision;
52  return number;
53 }
54 
55 static Gnuplot
57 {
58  Ptr<ConstantPositionMobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
59  Ptr<ConstantPositionMobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
60 
61  Gnuplot plot;
62 
63  plot.AppendExtra ("set xlabel 'Distance'");
64  plot.AppendExtra ("set ylabel 'rxPower (dBm)'");
65  plot.AppendExtra ("set key top right");
66 
67  double txPowerDbm = +20; // dBm
68 
69  Gnuplot2dDataset dataset;
70 
72 
73  {
74  a->SetPosition (Vector (0.0, 0.0, 0.0));
75 
76  for (double distance = 0.0; distance < 2500.0; distance += 10.0)
77  {
78  b->SetPosition (Vector (distance, 0.0, 0.0));
79 
80  // CalcRxPower() returns dBm.
81  double rxPowerDbm = model->CalcRxPower (txPowerDbm, a, b);
82 
83  dataset.Add (distance, rxPowerDbm);
84 
85  Simulator::Stop (Seconds (1.0));
86  Simulator::Run ();
87  }
88  }
89 
90  std::ostringstream os;
91  os << "txPower " << txPowerDbm << "dBm";
92  dataset.SetTitle (os.str ());
93 
94  plot.AddDataset (dataset);
95 
96  plot.AddDataset ( Gnuplot2dFunction ("-94 dBm CSThreshold", "-94.0") );
97 
98  return plot;
99 }
100 
101 static Gnuplot
102 TestProbabilistic (Ptr<PropagationLossModel> model, unsigned int samples = 100000)
103 {
104  Ptr<ConstantPositionMobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
105  Ptr<ConstantPositionMobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
106 
107  Gnuplot plot;
108 
109  plot.AppendExtra ("set xlabel 'Distance'");
110  plot.AppendExtra ("set ylabel 'rxPower (dBm)'");
111  plot.AppendExtra ("set zlabel 'Probability' offset 0,+10");
112  plot.AppendExtra ("set view 50, 120, 1.0, 1.0");
113  plot.AppendExtra ("set key top right");
114 
115  plot.AppendExtra ("set ticslevel 0");
116  plot.AppendExtra ("set xtics offset -0.5,0");
117  plot.AppendExtra ("set ytics offset 0,-0.5");
118  plot.AppendExtra ("set xrange [100:]");
119 
120  double txPowerDbm = +20; // dBm
121 
122  Gnuplot3dDataset dataset;
123 
124  dataset.SetStyle ("with linespoints");
125  dataset.SetExtra ("pointtype 3 pointsize 0.5");
126 
127  typedef std::map<double, unsigned int> rxPowerMapType;
128 
129  // Take given number of samples from CalcRxPower() and show probability
130  // density for discrete distances.
131  {
132  a->SetPosition (Vector (0.0, 0.0, 0.0));
133 
134  for (double distance = 100.0; distance < 2500.0; distance += 100.0)
135  {
136  b->SetPosition (Vector (distance, 0.0, 0.0));
137 
138  rxPowerMapType rxPowerMap;
139 
140  for (unsigned int samp = 0; samp < samples; ++samp)
141  {
142  // CalcRxPower() returns dBm.
143  double rxPowerDbm = model->CalcRxPower (txPowerDbm, a, b);
144  rxPowerDbm = dround (rxPowerDbm, 1.0);
145 
146  rxPowerMap[ rxPowerDbm ]++;
147 
148  Simulator::Stop (Seconds (0.01));
149  Simulator::Run ();
150  }
151 
152  for (rxPowerMapType::const_iterator i = rxPowerMap.begin ();
153  i != rxPowerMap.end (); ++i)
154  {
155  dataset.Add (distance, i->first, (double)i->second / (double)samples);
156  }
157  dataset.AddEmptyLine ();
158  }
159  }
160 
161  std::ostringstream os;
162  os << "txPower " << txPowerDbm << "dBm";
163  dataset.SetTitle (os.str ());
164 
165  plot.AddDataset (dataset);
166 
167  return plot;
168 }
169 
170 static Gnuplot
172  Time timeStep = Seconds (0.001),
173  Time timeTotal = Seconds (1.0),
174  double distance = 100.0)
175 {
176  Ptr<ConstantPositionMobilityModel> a = CreateObject<ConstantPositionMobilityModel> ();
177  Ptr<ConstantPositionMobilityModel> b = CreateObject<ConstantPositionMobilityModel> ();
178 
179  Gnuplot plot;
180 
181  plot.AppendExtra ("set xlabel 'Time (s)'");
182  plot.AppendExtra ("set ylabel 'rxPower (dBm)'");
183  plot.AppendExtra ("set key center right");
184 
185  double txPowerDbm = +20; // dBm
186 
187  Gnuplot2dDataset dataset;
188 
190 
191  {
192  a->SetPosition (Vector (0.0, 0.0, 0.0));
193  b->SetPosition (Vector (distance, 0.0, 0.0));
194 
196  while( Simulator::Now () < start + timeTotal )
197  {
198  // CalcRxPower() returns dBm.
199  double rxPowerDbm = model->CalcRxPower (txPowerDbm, a, b);
200 
201  Time elapsed = Simulator::Now () - start;
202  dataset.Add (elapsed.GetSeconds (), rxPowerDbm);
203 
204  Simulator::Stop (timeStep);
205  Simulator::Run ();
206  }
207  }
208 
209  std::ostringstream os;
210  os << "txPower " << txPowerDbm << "dBm";
211  dataset.SetTitle (os.str ());
212 
213  plot.AddDataset (dataset);
214 
215  plot.AddDataset ( Gnuplot2dFunction ("-94 dBm CSThreshold", "-94.0") );
216 
217  return plot;
218 }
219 
220 int main (int argc, char *argv[])
221 {
222  CommandLine cmd (__FILE__);
223  cmd.Parse (argc, argv);
224 
225  GnuplotCollection gnuplots ("main-propagation-loss.pdf");
226 
227  {
228  Ptr<FriisPropagationLossModel> friis = CreateObject<FriisPropagationLossModel> ();
229 
230  Gnuplot plot = TestDeterministic (friis);
231  plot.SetTitle ("ns3::FriisPropagationLossModel (Default Parameters)");
232  gnuplots.AddPlot (plot);
233  }
234 
235  {
236  Ptr<LogDistancePropagationLossModel> log = CreateObject<LogDistancePropagationLossModel> ();
237  log->SetAttribute ("Exponent", DoubleValue (2.5));
238 
239  Gnuplot plot = TestDeterministic (log);
240  plot.SetTitle ("ns3::LogDistancePropagationLossModel (Exponent = 2.5)");
241  gnuplots.AddPlot (plot);
242  }
243 
244  {
245  Ptr<RandomPropagationLossModel> random = CreateObject<RandomPropagationLossModel> ();
246  Ptr<ExponentialRandomVariable> expVar = CreateObjectWithAttributes<ExponentialRandomVariable> ("Mean", DoubleValue (50.0));
247  random->SetAttribute ("Variable", PointerValue (expVar));
248 
249  Gnuplot plot = TestDeterministic (random);
250  plot.SetTitle ("ns3::RandomPropagationLossModel with Exponential Distribution");
251  gnuplots.AddPlot (plot);
252  }
253 
254  {
255  Ptr<JakesPropagationLossModel> jakes = CreateObject<JakesPropagationLossModel> ();
256 
257  // doppler frequency shift for 5.15 GHz at 100 km/h
258  Config::SetDefault ("ns3::JakesProcess::DopplerFrequencyHz", DoubleValue (477.9));
259 
260  Gnuplot plot = TestDeterministicByTime (jakes, Seconds (0.001), Seconds (1.0));
261  plot.SetTitle ("ns3::JakesPropagationLossModel (with 477.9 Hz shift and 1 millisec resolution)");
262  gnuplots.AddPlot (plot);
263  }
264 
265  {
266  Ptr<JakesPropagationLossModel> jakes = CreateObject<JakesPropagationLossModel> ();
267 
268  // doppler frequency shift for 5.15 GHz at 100 km/h
269  Config::SetDefault ("ns3::JakesProcess::DopplerFrequencyHz", DoubleValue (477.9));
270 
271  Gnuplot plot = TestDeterministicByTime (jakes, Seconds (0.0001), Seconds (0.1));
272  plot.SetTitle ("ns3::JakesPropagationLossModel (with 477.9 Hz shift and 0.1 millisec resolution)");
273  gnuplots.AddPlot (plot);
274  }
275 
276  {
277  Ptr<ThreeLogDistancePropagationLossModel> log3 = CreateObject<ThreeLogDistancePropagationLossModel> ();
278 
279  Gnuplot plot = TestDeterministic (log3);
280  plot.SetTitle ("ns3::ThreeLogDistancePropagationLossModel (Defaults)");
281  gnuplots.AddPlot (plot);
282  }
283 
284  {
285  Ptr<ThreeLogDistancePropagationLossModel> log3 = CreateObject<ThreeLogDistancePropagationLossModel> ();
286  // more prominent example values:
287  log3->SetAttribute ("Exponent0", DoubleValue (1.0));
288  log3->SetAttribute ("Exponent1", DoubleValue (3.0));
289  log3->SetAttribute ("Exponent2", DoubleValue (10.0));
290 
291  Gnuplot plot = TestDeterministic (log3);
292  plot.SetTitle ("ns3::ThreeLogDistancePropagationLossModel (Exponents 1.0, 3.0 and 10.0)");
293  gnuplots.AddPlot (plot);
294  }
295 
296  {
297  Ptr<NakagamiPropagationLossModel> nak = CreateObject<NakagamiPropagationLossModel> ();
298 
299  Gnuplot plot = TestProbabilistic (nak);
300  plot.SetTitle ("ns3::NakagamiPropagationLossModel (Default Parameters)");
301  gnuplots.AddPlot (plot);
302  }
303 
304  {
305  Ptr<ThreeLogDistancePropagationLossModel> log3 = CreateObject<ThreeLogDistancePropagationLossModel> ();
306 
307  Ptr<NakagamiPropagationLossModel> nak = CreateObject<NakagamiPropagationLossModel> ();
308  log3->SetNext (nak);
309 
310  Gnuplot plot = TestProbabilistic (log3);
311  plot.SetTitle ("ns3::ThreeLogDistancePropagationLossModel and ns3::NakagamiPropagationLossModel (Default Parameters)");
312  gnuplots.AddPlot (plot);
313  }
314 
315  gnuplots.GenerateOutput (std::cout);
316 
317  // produce clean valgrind
319  return 0;
320 }
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:749
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
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...
Class to represent a 3D points plot.
Definition: gnuplot.h:272
Class to represent a 2D points plot.
Definition: gnuplot.h:117
void SetNext(Ptr< PropagationLossModel > next)
Enables a chain of loss models to act on the signal.
void SetTitle(const std::string &title)
Change line title.
Definition: gnuplot.cc:141
def start()
Definition: core.py:1855
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
void Add(double x, double y, double z)
Definition: gnuplot.cc:593
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
cmd
Definition: second.py:35
static double dround(double number, double precision)
Round a double number to the given precision.
void SetExtra(const std::string &extra)
Add extra formatting parameters to this dataset.
Definition: gnuplot.cc:152
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:371
a simple class to group together multiple gnuplots into one file, e.g.
Definition: gnuplot.h:487
void SetTitle(const std::string &title)
Definition: gnuplot.cc:730
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition: gnuplot.cc:604
static Gnuplot TestDeterministicByTime(Ptr< PropagationLossModel > model, Time timeStep=Seconds(0.001), Time timeTotal=Seconds(1.0), double distance=100.0)
void Add(double x, double y)
Definition: gnuplot.cc:359
Parse command-line arguments.
Definition: command-line.h:227
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static Gnuplot TestProbabilistic(Ptr< PropagationLossModel > model, unsigned int samples=100000)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
void SetStyle(enum Style style)
Definition: gnuplot.cc:342
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
void SetPosition(const Vector &position)
void SetStyle(const std::string &style)
Definition: gnuplot.cc:587
static Gnuplot TestDeterministic(Ptr< PropagationLossModel > model)
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Class to represent a 2D function expression plot.
Definition: gnuplot.h:243
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185