A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
46
class
BasicEnergyUpdateTest
:
public
TestCase
47
{
48
public
:
49
BasicEnergyUpdateTest
();
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
68
ObjectFactory
m_energySource
;
69
ObjectFactory
m_deviceEnergyModel
;
70
};
71
72
BasicEnergyUpdateTest::BasicEnergyUpdateTest
()
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
79
BasicEnergyUpdateTest::~BasicEnergyUpdateTest
()
80
{
81
}
82
83
void
84
BasicEnergyUpdateTest::DoRun
(
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
99
BasicEnergyUpdateTest::StateSwitchTest
(
WifiPhy::State
state)
100
{
101
// create node
102
Ptr<Node>
node = CreateObject<Node> ();
103
104
// create energy source
105
Ptr<BasicEnergySource>
source =
m_energySource
.
Create
<
BasicEnergySource
> ();
106
// aggregate energy source to node
107
node->
AggregateObject
(source);
108
109
// create device energy model
110
Ptr<WifiRadioEnergyModel>
model =
111
m_deviceEnergyModel
.
Create
<
WifiRadioEnergyModel
> ();
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
118
DeviceEnergyModelContainer
models =
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
212
class
BasicEnergyDepletionTest
:
public
TestCase
213
{
214
public
:
215
BasicEnergyDepletionTest
();
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
244
BasicEnergyDepletionTest::BasicEnergyDepletionTest
()
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
254
BasicEnergyDepletionTest::~BasicEnergyDepletionTest
()
255
{
256
}
257
258
void
259
BasicEnergyDepletionTest::DoRun
(
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
277
BasicEnergyDepletionTest::DepletionHandler
(
void
)
278
{
279
m_callbackCount
++;
280
}
281
282
bool
283
BasicEnergyDepletionTest::DepletionTestCase
(
double
simTimeS,
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;
304
wifi.
SetStandard
(
WIFI_PHY_STANDARD_80211b
);
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
345
WifiRadioEnergyModel::WifiRadioEnergyDepletionCallback
callback =
346
MakeCallback
(&
BasicEnergyDepletionTest::DepletionHandler
,
this
);
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
374
class
BasicEnergyModelTestSuite
:
public
TestSuite
375
{
376
public
:
377
BasicEnergyModelTestSuite
();
378
};
379
380
BasicEnergyModelTestSuite::BasicEnergyModelTestSuite
()
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
388
static
BasicEnergyModelTestSuite
g_energyModelTestSuite
;
389
src
energy
test
basic-energy-model-test.cc
Generated on Tue May 14 2013 11:08:20 for ns-3 by
1.8.1.2