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 <cmath>
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("BasicEnergyModelTestSuite");
39 
45 {
46 public:
48  virtual ~BasicEnergyUpdateTest ();
49 
54  bool DoRun (void);
55 
56 private:
64  bool StateSwitchTest (WifiPhy::State state);
65 
66 private:
67  double m_timeS; // in seconds
68  double m_tolerance; // tolerance for power estimation
69 
72 };
73 
75 {
76  m_timeS = 15.5; // idle for 15 seconds before changing state
77  m_tolerance = 1.0e-13; //
78 }
79 
81 {
82 }
83 
84 bool
86 {
87  // set types
88  m_energySource.SetTypeId ("ns3::BasicEnergySource");
89  m_deviceEnergyModel.SetTypeId ("ns3::WifiRadioEnergyModel");
90 
91  uint8_t ret = 0;
92 
93  // run state switch tests
94  if (StateSwitchTest (WifiPhy::IDLE))
95  {
96  ret = 1;
97  std::cerr << "Problem with state switch test (WifiPhy idle)." << std::endl;
98  }
99  if (StateSwitchTest (WifiPhy::CCA_BUSY))
100  {
101  ret = 1;
102  std::cerr << "Problem with state switch test (WifiPhy cca busy)." << std::endl;
103  }
104  if (StateSwitchTest (WifiPhy::TX))
105  {
106  ret = 1;
107  std::cerr << "Problem with state switch test (WifiPhy tx)." << std::endl;
108  }
109  if (StateSwitchTest (WifiPhy::RX))
110  {
111  ret = 1;
112  std::cerr << "Problem with state switch test (WifiPhy rx)." << std::endl;
113  }
114  if (StateSwitchTest (WifiPhy::SWITCHING))
115  {
116  ret = 1;
117  std::cerr << "Problem with state switch test (WifiPhy switching)." << std::endl;
118  }
119  if (StateSwitchTest (WifiPhy::SLEEP))
120  {
121  ret = 1;
122  std::cerr << "Problem with state switch test (WifiPhy sleep)." << std::endl;
123  }
124  return ret;
125 }
126 
127 bool
129 {
130  // create node
131  Ptr<Node> node = CreateObject<Node> ();
132 
133  // create energy source
134  Ptr<BasicEnergySource> source = m_energySource.Create<BasicEnergySource> ();
135  // aggregate energy source to node
136  node->AggregateObject (source);
137 
138  // create device energy model
140  m_deviceEnergyModel.Create<WifiRadioEnergyModel> ();
141  // set energy source pointer
142  model->SetEnergySource (source);
143  // add device energy model to model list in energy source
144  source->AppendDeviceEnergyModel (model);
145 
146  // retrieve device energy model from energy source
148  source->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel");
149  // check list
150  if ((models.GetN () == 0))
151  {
152  std::cerr << "Model list is empty!." << std::endl;
153  return true;
154  }
155  // get pointer
156  Ptr<WifiRadioEnergyModel> devModel =
157  DynamicCast<WifiRadioEnergyModel> (models.Get (0));
158  // check pointer
159  if ((devModel == 0))
160  {
161  std::cerr << "NULL pointer to device model!." << std::endl;
162  return true;
163  }
164 
165  /*
166  * The radio will stay IDLE for m_timeS seconds. Then it will switch into a
167  * different state.
168  */
169 
170  // schedule change of state
171  Simulator::Schedule (Seconds (m_timeS),
172  &WifiRadioEnergyModel::ChangeState, devModel, state);
173 
174  // Calculate remaining energy at simulation stop time
175  Simulator::Schedule (Seconds (m_timeS * 2),
177 
178  double timeDelta = 0.000000001; // 1 nanosecond
179  // run simulation; stop just after last scheduled event
180  Simulator::Stop (Seconds (m_timeS * 2 + timeDelta));
181  Simulator::Run ();
182 
183  // energy = current * voltage * time
184 
185  // calculate idle power consumption
186  double estRemainingEnergy = source->GetInitialEnergy ();
187  double voltage = source->GetSupplyVoltage ();
188  estRemainingEnergy -= devModel->GetIdleCurrentA () * voltage * m_timeS;
189 
190 
191  // calculate new state power consumption
192  double current = 0.0;
193  switch (state)
194  {
195  case WifiPhy::IDLE:
196  current = devModel->GetIdleCurrentA ();
197  break;
198  case WifiPhy::CCA_BUSY:
199  current = devModel->GetCcaBusyCurrentA ();
200  break;
201  case WifiPhy::TX:
202  current = devModel->GetTxCurrentA ();
203  break;
204  case WifiPhy::RX:
205  current = devModel->GetRxCurrentA ();
206  break;
207  case WifiPhy::SWITCHING:
208  current = devModel->GetSwitchingCurrentA ();
209  break;
210  case WifiPhy::SLEEP:
211  current = devModel->GetSleepCurrentA ();
212  break;
213  default:
214  NS_FATAL_ERROR ("Undefined radio state: " << state);
215  break;
216  }
217  estRemainingEnergy -= current * voltage * m_timeS;
218 
219  // obtain remaining energy from source
220  double remainingEnergy = source->GetRemainingEnergy ();
221  NS_LOG_DEBUG ("Remaining energy is " << remainingEnergy);
222  NS_LOG_DEBUG ("Estimated remaining energy is " << estRemainingEnergy);
223  NS_LOG_DEBUG ("Difference is " << estRemainingEnergy - remainingEnergy);
224 
225  // check remaining energy
226  if ((remainingEnergy > (estRemainingEnergy + m_tolerance)) ||
227  (remainingEnergy < (estRemainingEnergy - m_tolerance)))
228  {
229  std::cerr << "Incorrect remaining energy!" << std::endl;
230  return true;
231  }
232 
233  // obtain radio state
234  WifiPhy::State endState = devModel->GetCurrentState ();
235  NS_LOG_DEBUG ("Radio state is " << endState);
236  // check end state
237  if (endState != state)
238  {
239  std::cerr << "Incorrect end state!" << std::endl;
240  return true;
241  }
243 
244  return false; // no error
245 }
246 
247 // -------------------------------------------------------------------------- //
248 
254 {
255 public:
257  virtual ~BasicEnergyDepletionTest ();
258 
263  bool DoRun (void);
264 
265 private:
269  void DepletionHandler (void);
270 
278  bool DepletionTestCase (double simTimeS, double updateIntervalS);
279 
280 private:
281  int m_numOfNodes; // number of nodes in simulation
282  int m_callbackCount; // counter for # of callbacks invoked
283  double m_simTimeS; // maximum simulation time, in seconds
284  double m_timeStepS; // simulation time step size, in seconds
285  double m_updateIntervalS; // update interval of each device model
286 
287 };
288 
290 {
291  m_numOfNodes = 10;
292  m_callbackCount = 0;
293  m_simTimeS = 4.5;
294  m_timeStepS = 0.5;
295  m_updateIntervalS = 1.5;
296 }
297 
299 {
300 }
301 
302 bool
304 {
305  /*
306  * Run simulation with different simulation time and update interval.
307  */
308  uint8_t ret = 0;
309 
310  for (double simTimeS = 0.0; simTimeS <= m_simTimeS; simTimeS += m_timeStepS)
311  {
312  for (double updateIntervalS = 0.5; updateIntervalS <= m_updateIntervalS;
313  updateIntervalS += m_timeStepS)
314  {
315  if (DepletionTestCase (simTimeS, updateIntervalS))
316  {
317  ret = 1;
318  std::cerr << "Depletion test case problem." << std::endl;
319  }
320  // reset callback count
321  m_callbackCount = 0;
322  }
323  }
324  return ret;
325 }
326 
327 void
329 {
330  m_callbackCount++;
331 }
332 
333 bool
335  double updateIntervalS)
336 {
337  // create node
338  NodeContainer c;
339  c.Create (m_numOfNodes);
340 
341  std::string phyMode ("DsssRate1Mbps");
342 
343  // disable fragmentation for frames below 2200 bytes
344  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold",
345  StringValue ("2200"));
346  // turn off RTS/CTS for frames below 2200 bytes
347  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold",
348  StringValue ("2200"));
349  // Fix non-unicast data rate to be the same as that of unicast
350  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
351  StringValue (phyMode));
352 
353  // install YansWifiPhy
356 
358  /*
359  * This is one parameter that matters when using FixedRssLossModel, set it to
360  * zero; otherwise, gain will be added.
361  */
362  wifiPhy.Set ("RxGain", DoubleValue (0));
363  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
365 
366  YansWifiChannelHelper wifiChannel;
367  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
368  wifiPhy.SetChannel (wifiChannel.Create ());
369 
370  // Add a MAC and disable rate control
371  WifiMacHelper wifiMac;
372  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
373  "DataMode", StringValue (phyMode),
374  "ControlMode", StringValue (phyMode));
375  // Set it to ad-hoc mode
376  wifiMac.SetType ("ns3::AdhocWifiMac");
377  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
378 
379  /*
380  * Create and install energy source and a single basic radio energy model on
381  * the node using helpers.
382  */
383  // source helper
384  BasicEnergySourceHelper basicSourceHelper;
385  // set energy to 0 so that we deplete energy at the beginning of simulation
386  basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
387  // set update interval
388  basicSourceHelper.Set ("PeriodicEnergyUpdateInterval",
389  TimeValue (Seconds (updateIntervalS)));
390  // install source
391  EnergySourceContainer sources = basicSourceHelper.Install (c);
392 
393  // device energy model helper
394  WifiRadioEnergyModelHelper radioEnergyHelper;
395  // set energy depletion callback
398  radioEnergyHelper.SetDepletionCallback (callback);
399  // install on node
400  DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices, sources);
401 
402  // run simulation
403  Simulator::Stop (Seconds (simTimeS));
404  Simulator::Run ();
406 
407  NS_LOG_DEBUG ("Simulation time = " << simTimeS << "s");
408  NS_LOG_DEBUG ("Update interval = " << updateIntervalS << "s");
409  NS_LOG_DEBUG ("Expected callback count is " << m_numOfNodes);
410  NS_LOG_DEBUG ("Actual callback count is " << m_callbackCount);
411 
412  // check result, call back should only be invoked once
413  if (m_numOfNodes != m_callbackCount)
414  {
415  std::cerr << "Not all callbacks are invoked!" << std::endl;
416  return true;
417  }
418 
419  return false;
420 }
421 
422 // -------------------------------------------------------------------------- //
423 
424 int
425 main (int argc, char **argv)
426 {
427  BasicEnergyUpdateTest testEnergyUpdate;
428  if (testEnergyUpdate.DoRun ())
429  {
430  return 1;
431  }
432 
433  BasicEnergyDepletionTest testEnergyDepletion;
434  if (testEnergyDepletion.DoRun ())
435  {
436  return 1;
437  }
438 
439  return 0;
440 }
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:132
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:719
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:180
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#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.
void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
The PHY layer is sleeping.
Definition: wifi-phy.h:196
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
Test case of update remaining energy for BasicEnergySource and WifiRadioEnergyModel.
double GetSleepCurrentA(void) const
Gets sleep current.
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:213
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:566
Holds a vector of ns3::DeviceEnergyModel pointers.
void SetChannel(Ptr< YansWifiChannel > channel)
double GetCcaBusyCurrentA(void) const
Gets CCA busy current.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
AttributeValue implementation for Time.
Definition: nstime.h:1055
The PHY layer is IDLE.
Definition: wifi-phy.h:176
Creates a BasicEnergySource object.
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:742
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:748
The PHY layer is receiving a packet.
Definition: wifi-phy.h:188
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
WifiPhy::State GetCurrentState(void) const
The PHY layer is sending a packet.
Definition: wifi-phy.h:184
bool StateSwitchTest(WifiPhy::State state)
The PHY layer is switching to other channel.
Definition: wifi-phy.h:192
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Ptr< DeviceEnergyModel > Get(uint32_t i) const
Get the i-th Ptr stored in this container.
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
double GetTxCurrentA(void) const
Gets transmit current.
bool DoRun(void)
Performs some tests involving energy depletion.
Test case of energy depletion handling for BasicEnergySource and WifiRadioEnergyModel.
double GetRxCurrentA(void) const
Gets receive current.
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
A WiFi radio energy model.
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(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
Instantiate subclasses of ns3::Object.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void Set(std::string name, const AttributeValue &v)
double GetIdleCurrentA(void) const
Gets idle current.
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 SetEnergySource(const Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
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:171
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
Include Radiotap link layer information.
Definition: wifi-helper.h:111
bool DepletionTestCase(double simTimeS, double updateIntervalS)
void DepletionHandler(void)
Callback invoked when energy is drained from source.
double GetSwitchingCurrentA(void) const
Gets switching current.