A Discrete-Event Network Simulator
API
basic-energy-model-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
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: He Wu <mdzz@u.washington.edu>
19  */
20 
21 #include "ns3/basic-energy-source.h"
22 #include "ns3/wifi-radio-energy-model.h"
23 #include "ns3/basic-energy-source-helper.h"
24 #include "ns3/wifi-radio-energy-model-helper.h"
25 #include "ns3/energy-source-container.h"
26 #include "ns3/device-energy-model-container.h"
27 #include "ns3/log.h"
28 #include "ns3/node.h"
29 #include "ns3/simulator.h"
30 #include "ns3/double.h"
31 #include "ns3/config.h"
32 #include "ns3/string.h"
33 #include "ns3/yans-wifi-helper.h"
34 #include "ns3/nqos-wifi-mac-helper.h"
35 #include <cmath>
36 
37 using namespace ns3;
38 
39 NS_LOG_COMPONENT_DEFINE ("BasicEnergyModelTestSuite");
40 
46 {
47 public:
49  virtual ~BasicEnergyUpdateTest ();
50 
55  bool DoRun (void);
56 
57 private:
65  bool StateSwitchTest (WifiPhy::State state);
66 
67 private:
68  double m_timeS; // in seconds
69  double m_tolerance; // tolerance for power estimation
70 
73 };
74 
76 {
77  m_timeS = 15.5; // idle for 15 seconds before changing state
78  m_tolerance = 1.0e-13; //
79 }
80 
82 {
83 }
84 
85 bool
87 {
88  // set types
89  m_energySource.SetTypeId ("ns3::BasicEnergySource");
90  m_deviceEnergyModel.SetTypeId ("ns3::WifiRadioEnergyModel");
91 
92  uint8_t ret = 0;
93 
94  // run state switch tests
95  if (StateSwitchTest (WifiPhy::IDLE))
96  {
97  ret = 1;
98  std::cerr << "Problem with state switch test (WifiPhy idle)." << std::endl;
99  }
100  if (StateSwitchTest (WifiPhy::CCA_BUSY))
101  {
102  ret = 1;
103  std::cerr << "Problem with state switch test (WifiPhy cca busy)." << std::endl;
104  }
105  if (StateSwitchTest (WifiPhy::TX))
106  {
107  ret = 1;
108  std::cerr << "Problem with state switch test (WifiPhy tx)." << std::endl;
109  }
110  if (StateSwitchTest (WifiPhy::RX))
111  {
112  ret = 1;
113  std::cerr << "Problem with state switch test (WifiPhy rx)." << std::endl;
114  }
115  if (StateSwitchTest (WifiPhy::SWITCHING))
116  {
117  ret = 1;
118  std::cerr << "Problem with state switch test (WifiPhy switching)." << std::endl;
119  }
120  if (StateSwitchTest (WifiPhy::SLEEP))
121  {
122  ret = 1;
123  std::cerr << "Problem with state switch test (WifiPhy sleep)." << std::endl;
124  }
125  return ret;
126 }
127 
128 bool
130 {
131  // create node
132  Ptr<Node> node = CreateObject<Node> ();
133 
134  // create energy source
135  Ptr<BasicEnergySource> source = m_energySource.Create<BasicEnergySource> ();
136  // aggregate energy source to node
137  node->AggregateObject (source);
138 
139  // create device energy model
141  m_deviceEnergyModel.Create<WifiRadioEnergyModel> ();
142  // set energy source pointer
143  model->SetEnergySource (source);
144  // add device energy model to model list in energy source
145  source->AppendDeviceEnergyModel (model);
146 
147  // retrieve device energy model from energy source
149  source->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel");
150  // check list
151  if ((models.GetN () == 0))
152  {
153  std::cerr << "Model list is empty!." << std::endl;
154  return true;
155  }
156  // get pointer
157  Ptr<WifiRadioEnergyModel> devModel =
158  DynamicCast<WifiRadioEnergyModel> (models.Get (0));
159  // check pointer
160  if ((devModel == 0))
161  {
162  std::cerr << "NULL pointer to device model!." << std::endl;
163  return true;
164  }
165 
166  /*
167  * The radio will stay IDLE for m_timeS seconds. Then it will switch into a
168  * different state.
169  */
170 
171  // schedule change of state
172  Simulator::Schedule (Seconds (m_timeS),
173  &WifiRadioEnergyModel::ChangeState, devModel, state);
174 
175  // Calculate remaining energy at simulation stop time
176  Simulator::Schedule (Seconds (m_timeS * 2),
178 
179  double timeDelta = 0.000000001; // 1 nanosecond
180  // run simulation; stop just after last scheduled event
181  Simulator::Stop (Seconds (m_timeS * 2 + timeDelta));
182  Simulator::Run ();
183 
184  // energy = current * voltage * time
185 
186  // calculate idle power consumption
187  double estRemainingEnergy = source->GetInitialEnergy ();
188  double voltage = source->GetSupplyVoltage ();
189  estRemainingEnergy -= devModel->GetIdleCurrentA () * voltage * m_timeS;
190 
191  /*
192  * Manually calculate the number of periodic updates performed by the source.
193  * This is to check if the periodic updates are performed correctly.
194  */
195  double actualTime = m_timeS;
196  actualTime /= source->GetEnergyUpdateInterval ().GetSeconds ();
197  actualTime = floor (actualTime); // rounding for update interval
198  actualTime *= source->GetEnergyUpdateInterval ().GetSeconds ();
199 
200  // calculate new state power consumption
201  double current = 0.0;
202  switch (state)
203  {
204  case WifiPhy::IDLE:
205  current = devModel->GetIdleCurrentA ();
206  break;
207  case WifiPhy::CCA_BUSY:
208  current = devModel->GetCcaBusyCurrentA ();
209  break;
210  case WifiPhy::TX:
211  current = devModel->GetTxCurrentA ();
212  break;
213  case WifiPhy::RX:
214  current = devModel->GetRxCurrentA ();
215  break;
216  case WifiPhy::SWITCHING:
217  current = devModel->GetSwitchingCurrentA ();
218  break;
219  case WifiPhy::SLEEP:
220  current = devModel->GetSleepCurrentA ();
221  break;
222  default:
223  NS_FATAL_ERROR ("Undefined radio state: " << state);
224  break;
225  }
226  estRemainingEnergy -= current * voltage * m_timeS;
227 
228  // obtain remaining energy from source
229  double remainingEnergy = source->GetRemainingEnergy ();
230  NS_LOG_DEBUG ("Remaining energy is " << remainingEnergy);
231  NS_LOG_DEBUG ("Estimated remaining energy is " << estRemainingEnergy);
232  NS_LOG_DEBUG ("Difference is " << estRemainingEnergy - remainingEnergy);
233 
234  // check remaining energy
235  if ((remainingEnergy > (estRemainingEnergy + m_tolerance)) ||
236  (remainingEnergy < (estRemainingEnergy - m_tolerance)))
237  {
238  std::cerr << "Incorrect remaining energy!" << std::endl;
239  return true;
240  }
241 
242  // obtain radio state
243  WifiPhy::State endState = devModel->GetCurrentState ();
244  NS_LOG_DEBUG ("Radio state is " << endState);
245  // check end state
246  if (endState != state)
247  {
248  std::cerr << "Incorrect end state!" << std::endl;
249  return true;
250  }
252 
253  return false; // no error
254 }
255 
256 // -------------------------------------------------------------------------- //
257 
263 {
264 public:
266  virtual ~BasicEnergyDepletionTest ();
267 
272  bool DoRun (void);
273 
274 private:
278  void DepletionHandler (void);
279 
287  bool DepletionTestCase (double simTimeS, double updateIntervalS);
288 
289 private:
290  int m_numOfNodes; // number of nodes in simulation
291  int m_callbackCount; // counter for # of callbacks invoked
292  double m_simTimeS; // maximum simulation time, in seconds
293  double m_timeStepS; // simulation time step size, in seconds
294  double m_updateIntervalS; // update interval of each device model
295 
296 };
297 
299 {
300  m_numOfNodes = 10;
301  m_callbackCount = 0;
302  m_simTimeS = 4.5;
303  m_timeStepS = 0.5;
304  m_updateIntervalS = 1.5;
305 }
306 
308 {
309 }
310 
311 bool
313 {
314  /*
315  * Run simulation with different simulation time and update interval.
316  */
317  uint8_t ret = 0;
318 
319  for (double simTimeS = 0.0; simTimeS <= m_simTimeS; simTimeS += m_timeStepS)
320  {
321  for (double updateIntervalS = 0.5; updateIntervalS <= m_updateIntervalS;
322  updateIntervalS += m_timeStepS)
323  {
324  if (DepletionTestCase (simTimeS, updateIntervalS))
325  {
326  ret = 1;
327  std::cerr << "Depletion test case problem." << std::endl;
328  }
329  // reset callback count
330  m_callbackCount = 0;
331  }
332  }
333  return ret;
334 }
335 
336 void
338 {
339  m_callbackCount++;
340 }
341 
342 bool
344  double updateIntervalS)
345 {
346  // create node
347  NodeContainer c;
348  c.Create (m_numOfNodes);
349 
350  std::string phyMode ("DsssRate1Mbps");
351 
352  // disable fragmentation for frames below 2200 bytes
353  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold",
354  StringValue ("2200"));
355  // turn off RTS/CTS for frames below 2200 bytes
356  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold",
357  StringValue ("2200"));
358  // Fix non-unicast data rate to be the same as that of unicast
359  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
360  StringValue (phyMode));
361 
362  // install YansWifiPhy
365 
367  /*
368  * This is one parameter that matters when using FixedRssLossModel, set it to
369  * zero; otherwise, gain will be added.
370  */
371  wifiPhy.Set ("RxGain", DoubleValue (0));
372  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
374 
375  YansWifiChannelHelper wifiChannel;
376  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
377  wifiPhy.SetChannel (wifiChannel.Create ());
378 
379  // Add a non-QoS upper MAC, and disable rate control
381  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
382  "DataMode", StringValue (phyMode),
383  "ControlMode", StringValue (phyMode));
384  // Set it to ad-hoc mode
385  wifiMac.SetType ("ns3::AdhocWifiMac");
386  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
387 
388  /*
389  * Create and install energy source and a single basic radio energy model on
390  * the node using helpers.
391  */
392  // source helper
393  BasicEnergySourceHelper basicSourceHelper;
394  // set energy to 0 so that we deplete energy at the beginning of simulation
395  basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
396  // set update interval
397  basicSourceHelper.Set ("PeriodicEnergyUpdateInterval",
398  TimeValue (Seconds (updateIntervalS)));
399  // install source
400  EnergySourceContainer sources = basicSourceHelper.Install (c);
401 
402  // device energy model helper
403  WifiRadioEnergyModelHelper radioEnergyHelper;
404  // set energy depletion callback
407  radioEnergyHelper.SetDepletionCallback (callback);
408  // install on node
409  DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices, sources);
410 
411  // run simulation
412  Simulator::Stop (Seconds (simTimeS));
413  Simulator::Run ();
415 
416  NS_LOG_DEBUG ("Simulation time = " << simTimeS << "s");
417  NS_LOG_DEBUG ("Update interval = " << updateIntervalS << "s");
418  NS_LOG_DEBUG ("Expected callback count is " << m_numOfNodes);
419  NS_LOG_DEBUG ("Actual callback count is " << m_callbackCount);
420 
421  // check result, call back should only be invoked once
422  if (m_numOfNodes != m_callbackCount)
423  {
424  std::cerr << "Not all callbacks are invoked!" << std::endl;
425  return true;
426  }
427 
428  return false;
429 }
430 
431 // -------------------------------------------------------------------------- //
432 
433 int
434 main (int argc, char **argv)
435 {
436  BasicEnergyUpdateTest testEnergyUpdate;
437  if (testEnergyUpdate.DoRun ())
438  {
439  return 1;
440  }
441 
442  BasicEnergyDepletionTest testEnergyDepletion;
443  if (testEnergyDepletion.DoRun ())
444  {
445  return 1;
446  }
447 
448  return 0;
449 }
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
Assign WifiRadioEnergyModel to wifi devices.
Holds a vector of ns3::EnergySource pointers.
tuple devices
Definition: first.py:32
void SetDepletionCallback(WifiRadioEnergyModel::WifiRadioEnergyDepletionCallback callback)
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:74
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
The PHY layer has sense the medium busy through the CCA mechanism.
Definition: wifi-phy.h:153
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:246
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
virtual void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
The PHY layer is sleeping.
Definition: wifi-phy.h:169
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:145
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
Test case of update remaining energy for BasicEnergySource and WifiRadioEnergyModel.
void Set(std::string name, const AttributeValue &v)
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:96
Include Radiotap link layer information.
Holds a vector of ns3::DeviceEnergyModel pointers.
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:103
void SetChannel(Ptr< YansWifiChannel > channel)
double GetCcaBusyCurrentA(void) const
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1216
AttributeValue implementation for Time.
Definition: nstime.h:957
The PHY layer is IDLE.
Definition: wifi-phy.h:149
Creates a BasicEnergySource object.
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:97
The PHY layer is receiving a packet.
Definition: wifi-phy.h:161
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
static NqosWifiMacHelper Default(void)
Create a mac helper in a default working state.
WifiPhy::State GetCurrentState(void) const
The PHY layer is sending a packet.
Definition: wifi-phy.h:157
bool StateSwitchTest(WifiPhy::State state)
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
The PHY layer is switching to other channel.
Definition: wifi-phy.h:165
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
Ptr< DeviceEnergyModel > Get(uint32_t i) const
Get the i-th Ptr stored in this container.
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
bool DoRun(void)
Performs some tests involving state updates and the relative energy consumption.
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
EnergySourceContainer Install(Ptr< Node > node) const
bool DoRun(void)
Performs some tests involving energy depletion.
Test case of energy depletion handling for BasicEnergySource and WifiRadioEnergyModel.
manage and create wifi channel objects for the yans model.
A WiFi radio energy model.
Instantiate subclasses of ns3::Object.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void Set(std::string name, const AttributeValue &v)
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPropagationDelay(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
State
The state of the PHY layer.
Definition: wifi-phy.h:144
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
bool DepletionTestCase(double simTimeS, double updateIntervalS)
void DepletionHandler(void)
Callback invoked when energy is drained from source.
double GetSwitchingCurrentA(void) const