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