A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
basic-energy-harvester-test.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2014 Wireless Communications and Networking Group (WCNG),
4
* University of Rochester, Rochester, NY, USA.
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation;
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
*
19
* Author: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
20
*/
21
22
#include "ns3/log.h"
23
#include "ns3/test.h"
24
#include "ns3/node.h"
25
#include "ns3/simulator.h"
26
#include "ns3/double.h"
27
#include "ns3/config.h"
28
#include "ns3/string.h"
29
#include "ns3/basic-energy-harvester.h"
30
#include "ns3/basic-energy-source.h"
31
32
using namespace
ns3
;
33
34
NS_LOG_COMPONENT_DEFINE
(
"BasicEnergyHarvesterTestSuite"
);
35
36
class
BasicEnergyHarvesterTestCase
:
public
TestCase
37
{
38
public
:
39
BasicEnergyHarvesterTestCase
();
40
~
BasicEnergyHarvesterTestCase
();
41
42
void
DoRun (
void
);
43
44
double
m_timeS
;
// in seconds
45
double
m_tolerance
;
// tolerance for energy estimation
46
47
ObjectFactory
m_energySource
;
48
ObjectFactory
m_energyHarvester
;
49
50
};
51
52
BasicEnergyHarvesterTestCase::BasicEnergyHarvesterTestCase
()
53
:
TestCase
(
"Basic Energy Harvester test case"
)
54
{
55
m_timeS
= 15;
// harvest energy for 15 seconds
56
m_tolerance
= 1.0e-13;
//
57
}
58
59
BasicEnergyHarvesterTestCase::~BasicEnergyHarvesterTestCase
()
60
{
61
}
62
63
void
64
BasicEnergyHarvesterTestCase::DoRun
()
65
{
66
// set types
67
m_energySource
.
SetTypeId
(
"ns3::BasicEnergySource"
);
68
m_energyHarvester
.
SetTypeId
(
"ns3::BasicEnergyHarvester"
);
69
// create node
70
Ptr<Node>
node = CreateObject<Node> ();
71
72
// create Energy Source
73
Ptr<BasicEnergySource>
source =
m_energySource
.
Create
<
BasicEnergySource
> ();
74
// aggregate Energy Source to the node
75
node->
AggregateObject
(source);
76
77
//create energy harvester
78
Ptr<BasicEnergyHarvester>
harvester =
m_energyHarvester
.
Create
<
BasicEnergyHarvester
> ();
79
// Set the Energy Harvesting update interval to a value grater than the
80
// simulation duration, so that the power provided by the harvester is constant
81
harvester->
SetHarvestedPowerUpdateInterval
(
Seconds
(
m_timeS
+ 1.0));
82
// Connect the Basic Energy Harvester to the Energy Source
83
source->
ConnectEnergyHarvester
(harvester);
84
harvester->
SetNode
(node);
85
harvester->
SetEnergySource
(source);
86
87
Time
now =
Simulator::Now
();
88
89
/*
90
* The energy harvester will recharge the energy source for m_timeS seconds.
91
*/
92
93
// Calculate remaining energy at simulation stop time
94
Simulator::Schedule (
Seconds
(
m_timeS
),
95
&BasicEnergySource::UpdateEnergySource, source);
96
97
double
timeDelta = 0.000000001;
// 1 nanosecond
98
// run simulation; stop just after last scheduled event
99
Simulator::Stop (
Seconds
(
m_timeS
+ timeDelta));
100
Simulator::Run ();
101
102
// calculate energy harvested
103
double
estRemainingEnergy = source->
GetInitialEnergy
();
104
// energy = power * time
105
estRemainingEnergy += harvester->
GetPower
() *
m_timeS
;
106
107
// obtain remaining energy from source
108
double
remainingEnergy = source->
GetRemainingEnergy
();
109
NS_LOG_DEBUG
(
"Remaining energy is "
<< remainingEnergy);
110
NS_LOG_DEBUG
(
"Estimated remaining energy is "
<< estRemainingEnergy);
111
NS_LOG_DEBUG
(
"Difference is "
<< estRemainingEnergy - remainingEnergy);
112
113
Simulator::Destroy ();
114
115
// check remaining energy
116
NS_TEST_ASSERT_MSG_EQ_TOL
(remainingEnergy, estRemainingEnergy,
m_tolerance
,
117
"Incorrect Remaining energy!"
);
118
119
}
120
121
class
BasicEnergyHarvesterTestSuite
:
public
TestSuite
122
{
123
public
:
124
BasicEnergyHarvesterTestSuite
();
125
};
126
127
BasicEnergyHarvesterTestSuite::BasicEnergyHarvesterTestSuite
()
128
:
TestSuite
(
"basic-energy-harvester"
, UNIT)
129
{
130
AddTestCase
(
new
BasicEnergyHarvesterTestCase
, TestCase::QUICK);
131
}
132
133
// create an instance of the test suite
134
static
BasicEnergyHarvesterTestSuite
g_basicEnergyHarvesterTestSuite
;
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
ns3::EnergyHarvester::GetPower
double GetPower(void) const
Definition:
energy-harvester.cc:83
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::BasicEnergyHarvester::SetHarvestedPowerUpdateInterval
void SetHarvestedPowerUpdateInterval(Time updateInterval)
Definition:
basic-energy-harvester.cc:92
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::EnergyHarvester::SetEnergySource
void SetEnergySource(Ptr< EnergySource > source)
Definition:
energy-harvester.cc:67
BasicEnergyHarvesterTestCase::DoRun
void DoRun(void)
Implementation to actually run this TestCase.
Definition:
basic-energy-harvester-test.cc:64
BasicEnergyHarvesterTestSuite::BasicEnergyHarvesterTestSuite
BasicEnergyHarvesterTestSuite()
Definition:
basic-energy-harvester-test.cc:127
BasicEnergyHarvesterTestCase::m_timeS
double m_timeS
Definition:
basic-energy-harvester-test.cc:44
ns3::TestCase
encapsulates test code
Definition:
test.h:1154
ns3::Ptr< Node >
BasicEnergyHarvesterTestCase::m_energySource
ObjectFactory m_energySource
Definition:
basic-energy-harvester-test.cc:47
ns3::Now
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition:
simulator.cc:287
ns3::BasicEnergySource
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
Definition:
basic-energy-source.h:38
NS_TEST_ASSERT_MSG_EQ_TOL
#define NS_TEST_ASSERT_MSG_EQ_TOL(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:378
ns3::EnergyHarvester::SetNode
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergyHarvester.
Definition:
energy-harvester.cc:52
ns3::ObjectFactory
Instantiate subclasses of ns3::Object.
Definition:
object-factory.h:48
ns3::Object::AggregateObject
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition:
object.cc:252
BasicEnergyHarvesterTestSuite
Definition:
basic-energy-harvester-test.cc:122
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition:
nstime.h:104
ns3::BasicEnergySource::GetInitialEnergy
virtual double GetInitialEnergy(void) const
Definition:
basic-energy-source.cc:127
BasicEnergyHarvesterTestCase::~BasicEnergyHarvesterTestCase
~BasicEnergyHarvesterTestCase()
Definition:
basic-energy-harvester-test.cc:59
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1344
g_basicEnergyHarvesterTestSuite
static BasicEnergyHarvesterTestSuite g_basicEnergyHarvesterTestSuite
Definition:
basic-energy-harvester-test.cc:134
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition:
log.h:273
BasicEnergyHarvesterTestCase::BasicEnergyHarvesterTestCase
BasicEnergyHarvesterTestCase()
Definition:
basic-energy-harvester-test.cc:52
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1289
BasicEnergyHarvesterTestCase::m_tolerance
double m_tolerance
Definition:
basic-energy-harvester-test.cc:45
ns3::ObjectFactory::SetTypeId
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Definition:
object-factory.cc:40
ns3::BasicEnergySource::GetRemainingEnergy
virtual double GetRemainingEnergy(void)
Definition:
basic-energy-source.cc:134
ns3::ObjectFactory::Create
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Definition:
object-factory.cc:98
ns3::BasicEnergyHarvester
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
Definition:
basic-energy-harvester.h:49
ns3::EnergySource::ConnectEnergyHarvester
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
Definition:
energy-source.cc:140
BasicEnergyHarvesterTestCase
Definition:
basic-energy-harvester-test.cc:37
BasicEnergyHarvesterTestCase::m_energyHarvester
ObjectFactory m_energyHarvester
Definition:
basic-energy-harvester-test.cc:48
src
energy
test
basic-energy-harvester-test.cc
Generated on Fri Oct 1 2021 17:03:03 for ns-3 by
1.8.20