A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/test.h"
29 #include "ns3/node.h"
30 #include "ns3/simulator.h"
31 #include "ns3/double.h"
32 #include "ns3/config.h"
33 #include "ns3/string.h"
34 #include "ns3/yans-wifi-helper.h"
35 #include "ns3/nqos-wifi-mac-helper.h"
36 #include <cmath>
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("BasicEnergyModelTestSuite");
41 
47 {
48 public:
50  virtual ~BasicEnergyUpdateTest ();
51 
52 private:
53  void DoRun (void);
54 
62  bool StateSwitchTest (WifiPhy::State state);
63 
64 private:
65  double m_timeS; // in seconds
66  double m_tolerance; // tolerance for power estimation
67 
70 };
71 
73  : TestCase ("Basic energy model update remaining energy test case")
74 {
75  m_timeS = 15.5; // idle for 15 seconds before changing state
76  m_tolerance = 1.0e-13; //
77 }
78 
80 {
81 }
82 
83 void
85 {
86  // set types
87  m_energySource.SetTypeId ("ns3::BasicEnergySource");
88  m_deviceEnergyModel.SetTypeId ("ns3::WifiRadioEnergyModel");
89 
90  // run state switch tests
91  NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::IDLE), false, "Problem with state switch test (WifiPhy idle).");
92  NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::CCA_BUSY), false, "Problem with state switch test (WifiPhy cca busy).");
93  NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::TX), false, "Problem with state switch test (WifiPhy tx).");
94  NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::RX), false, "Problem with state switch test (WifiPhy rx).");
95  NS_TEST_ASSERT_MSG_EQ (StateSwitchTest (WifiPhy::SWITCHING), false, "Problem with state switch test (WifiPhy switching).");
96 }
97 
98 bool
100 {
101  // create node
102  Ptr<Node> node = CreateObject<Node> ();
103 
104  // create energy source
106  // aggregate energy source to node
107  node->AggregateObject (source);
108 
109  // create device energy model
112  // set energy source pointer
113  model->SetEnergySource (source);
114  // add device energy model to model list in energy source
115  source->AppendDeviceEnergyModel (model);
116 
117  // retrieve device energy model from energy source
119  source->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel");
120  // check list
121  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (false, (models.GetN () == 0), "Model list is empty!");
122  // get pointer
123  Ptr<WifiRadioEnergyModel> devModel =
124  DynamicCast<WifiRadioEnergyModel> (models.Get (0));
125  // check pointer
126  NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL (0, devModel, "NULL pointer to device model!");
127 
128  /*
129  * The radio will stay IDLE for m_timeS seconds. Then it will switch into a
130  * different state.
131  */
132 
133  // schedule change of state
134  Simulator::Schedule (Seconds (m_timeS),
135  &WifiRadioEnergyModel::ChangeState, devModel, state);
136 
137  // Calculate remaining energy at simulation stop time
138  Simulator::Schedule (Seconds (m_timeS * 2),
139  &BasicEnergySource::UpdateEnergySource, source);
140 
141  double timeDelta = 0.000000001; // 1 nanosecond
142  // run simulation; stop just after last scheduled event
143  Simulator::Stop (Seconds (m_timeS * 2 + timeDelta));
144  Simulator::Run ();
145 
146  // energy = current * voltage * time
147 
148  // calculate idle power consumption
149  double estRemainingEnergy = source->GetInitialEnergy ();
150  double voltage = source->GetSupplyVoltage ();
151  estRemainingEnergy -= devModel->GetIdleCurrentA () * voltage * m_timeS;
152 
153  /*
154  * Manually calculate the number of periodic updates performed by the source.
155  * This is to check if the periodic updates are performed correctly.
156  */
157  double actualTime = m_timeS;
158  actualTime /= source->GetEnergyUpdateInterval ().GetSeconds ();
159  actualTime = floor (actualTime); // rounding for update interval
160  actualTime *= source->GetEnergyUpdateInterval ().GetSeconds ();
161 
162  // calculate new state power consumption
163  double current = 0.0;
164  switch (state)
165  {
166  case WifiPhy::IDLE:
167  current = devModel->GetIdleCurrentA ();
168  break;
169  case WifiPhy::CCA_BUSY:
170  current = devModel->GetCcaBusyCurrentA ();
171  break;
172  case WifiPhy::TX:
173  current = devModel->GetTxCurrentA ();
174  break;
175  case WifiPhy::RX:
176  current = devModel->GetRxCurrentA ();
177  break;
178  case WifiPhy::SWITCHING:
179  current = devModel->GetSwitchingCurrentA ();
180  break;
181  default:
182  NS_FATAL_ERROR ("Undefined radio state: " << state);
183  break;
184  }
185  estRemainingEnergy -= current * voltage * m_timeS;
186 
187  // obtain remaining energy from source
188  double remainingEnergy = source->GetRemainingEnergy ();
189  NS_LOG_DEBUG ("Remaining energy is " << remainingEnergy);
190  NS_LOG_DEBUG ("Estimated remaining energy is " << estRemainingEnergy);
191  NS_LOG_DEBUG ("Difference is " << estRemainingEnergy - remainingEnergy);
192  // check remaining energy
193  NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL (remainingEnergy, estRemainingEnergy, m_tolerance,
194  "Incorrect remaining energy!");
195 
196  // obtain radio state
197  WifiPhy::State endState = devModel->GetCurrentState ();
198  NS_LOG_DEBUG ("Radio state is " << endState);
199  // check end state
200  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (endState, state, "Incorrect end state!");
201  Simulator::Destroy ();
202 
203  return false; // no error
204 }
205 
206 // -------------------------------------------------------------------------- //
207 
213 {
214 public:
216  virtual ~BasicEnergyDepletionTest ();
217 
218 private:
219  void DoRun (void);
220 
224  void DepletionHandler (void);
225 
233  bool DepletionTestCase (double simTimeS, double updateIntervalS);
234 
235 private:
236  int m_numOfNodes; // number of nodes in simulation
237  int m_callbackCount; // counter for # of callbacks invoked
238  double m_simTimeS; // maximum simulation time, in seconds
239  double m_timeStepS; // simulation time step size, in seconds
240  double m_updateIntervalS; // update interval of each device model
241 
242 };
243 
245  : TestCase ("Basic energy model energy depletion test case")
246 {
247  m_numOfNodes = 10;
248  m_callbackCount = 0;
249  m_simTimeS = 4.5;
250  m_timeStepS = 0.5;
251  m_updateIntervalS = 1.5;
252 }
253 
255 {
256 }
257 
258 void
260 {
261  /*
262  * Run simulation with different simulation time and update interval.
263  */
264  for (double simTimeS = 0.0; simTimeS <= m_simTimeS; simTimeS += m_timeStepS)
265  {
266  for (double updateIntervalS = 0.5; updateIntervalS <= m_updateIntervalS;
267  updateIntervalS += m_timeStepS)
268  {
269  NS_TEST_ASSERT_MSG_EQ (DepletionTestCase (simTimeS, updateIntervalS), false, "Depletion test case problem.");
270  // reset callback count
271  m_callbackCount = 0;
272  }
273  }
274 }
275 
276 void
278 {
279  m_callbackCount++;
280 }
281 
282 bool
284  double updateIntervalS)
285 {
286  // create node
287  NodeContainer c;
288  c.Create (m_numOfNodes);
289 
290  std::string phyMode ("DsssRate1Mbps");
291 
292  // disable fragmentation for frames below 2200 bytes
293  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold",
294  StringValue ("2200"));
295  // turn off RTS/CTS for frames below 2200 bytes
296  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold",
297  StringValue ("2200"));
298  // Fix non-unicast data rate to be the same as that of unicast
299  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
300  StringValue (phyMode));
301 
302  // install YansWifiPhy
303  WifiHelper wifi;
305 
306  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
307  /*
308  * This is one parameter that matters when using FixedRssLossModel, set it to
309  * zero; otherwise, gain will be added.
310  */
311  wifiPhy.Set ("RxGain", DoubleValue (0));
312  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
313  wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
314 
315  YansWifiChannelHelper wifiChannel;
316  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
317  wifiPhy.SetChannel (wifiChannel.Create ());
318 
319  // Add a non-QoS upper MAC, and disable rate control
320  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
321  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
322  "DataMode", StringValue (phyMode),
323  "ControlMode", StringValue (phyMode));
324  // Set it to ad-hoc mode
325  wifiMac.SetType ("ns3::AdhocWifiMac");
326  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
327 
328  /*
329  * Create and install energy source and a single basic radio energy model on
330  * the node using helpers.
331  */
332  // source helper
333  BasicEnergySourceHelper basicSourceHelper;
334  // set energy to 0 so that we deplete energy at the beginning of simulation
335  basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
336  // set update interval
337  basicSourceHelper.Set ("PeriodicEnergyUpdateInterval",
338  TimeValue (Seconds (updateIntervalS)));
339  // install source
340  EnergySourceContainer sources = basicSourceHelper.Install (c);
341 
342  // device energy model helper
343  WifiRadioEnergyModelHelper radioEnergyHelper;
344  // set energy depletion callback
347  radioEnergyHelper.SetDepletionCallback (callback);
348  // install on node
349  DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install (devices, sources);
350 
351  // run simulation
352  Simulator::Stop (Seconds (simTimeS));
353  Simulator::Run ();
354  Simulator::Destroy ();
355 
356  NS_LOG_DEBUG ("Simulation time = " << simTimeS << "s");
357  NS_LOG_DEBUG ("Update interval = " << updateIntervalS << "s");
358  NS_LOG_DEBUG ("Expected callback count is " << m_numOfNodes);
359  NS_LOG_DEBUG ("Actual callback count is " << m_callbackCount);
360 
361  // check result, call back should only be invoked once
362  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (m_numOfNodes, m_callbackCount, "Not all callbacks are invoked!");
363 
364  return false;
365 }
366 
367 // -------------------------------------------------------------------------- //
368 
375 {
376 public:
378 };
379 
381  : TestSuite ("basic-energy-model", UNIT)
382 {
383  AddTestCase (new BasicEnergyUpdateTest, TestCase::QUICK);
384  AddTestCase (new BasicEnergyDepletionTest, TestCase::QUICK);
385 }
386 
387 // create an instance of the test suite
389 
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:73
hold variables of type string
Definition: string.h:18
Make it easy to create and manage PHY objects for the yans model.
A suite of tests to run.
Definition: test.h:1105
State
The state of the PHY layer.
Definition: wifi-phy.h:123
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())
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:62
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
void SetTypeId(TypeId tid)
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
Test case of update remaining energy for BasicEnergySource and WifiRadioEnergyModel.
void Set(std::string name, const AttributeValue &v)
void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:929
Unit test suite for energy model.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:88
void DoRun(void)
Implementation to actually run this TestCase.
Holds a vector of ns3::DeviceEnergyModel pointers.
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:102
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:207
void SetChannel(Ptr< YansWifiChannel > channel)
double GetCcaBusyCurrentA(void) const
#define NS_TEST_ASSERT_MSG_EQ_TOL_RETURNS_BOOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:443
static BasicEnergyModelTestSuite g_energyModelTestSuite
hold objects of type ns3::Time
Definition: nstime.h:1008
Ptr< Object > Create(void) const
#define NS_TEST_ASSERT_MSG_NE_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not...
Definition: test.h:647
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:148
Creates a BasicEnergySource object.
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:96
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
WifiPhy::State GetCurrentState(void) const
bool StateSwitchTest(WifiPhy::State state)
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
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.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
keep track of a set of node pointers.
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
EnergySourceContainer Install(Ptr< Node > node) const
Test case of energy depletion handling for BasicEnergySource and WifiRadioEnergyModel.
manage and create wifi channel objects for the yans model.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
A WiFi radio energy model.
instantiate subclasses of ns3::Object.
Time current
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
void Set(std::string name, const AttributeValue &v)
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
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())
Hold a floating point type.
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