A Discrete-Event Network Simulator
API
basic-energy-harvester-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Wireless Communications and Networking Group (WCNG),
3  * University of Rochester, Rochester, NY, USA.
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: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
19  */
20 
21 #include "ns3/basic-energy-harvester.h"
22 #include "ns3/basic-energy-source.h"
23 #include "ns3/config.h"
24 #include "ns3/double.h"
25 #include "ns3/log.h"
26 #include "ns3/node.h"
27 #include "ns3/simulator.h"
28 #include "ns3/string.h"
29 #include "ns3/test.h"
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvesterTestSuite");
34 
41 {
42  public:
44  ~BasicEnergyHarvesterTestCase() override;
45 
46  void DoRun() override;
47 
48  double m_timeS;
49  double m_tolerance;
50 
53 };
54 
56  : TestCase("Basic Energy Harvester test case")
57 {
58  m_timeS = 15; // harvest energy for 15 seconds
59  m_tolerance = 1.0e-13; //
60 }
61 
63 {
64 }
65 
66 void
68 {
69  // set types
70  m_energySource.SetTypeId("ns3::BasicEnergySource");
71  m_energyHarvester.SetTypeId("ns3::BasicEnergyHarvester");
72  // create node
73  Ptr<Node> node = CreateObject<Node>();
74 
75  // create Energy Source
77  // aggregate Energy Source to the node
78  node->AggregateObject(source);
79 
80  // create energy harvester
82  // Set the Energy Harvesting update interval to a value grater than the
83  // simulation duration, so that the power provided by the harvester is constant
84  harvester->SetHarvestedPowerUpdateInterval(Seconds(m_timeS + 1.0));
85  // Connect the Basic Energy Harvester to the Energy Source
86  source->ConnectEnergyHarvester(harvester);
87  harvester->SetNode(node);
88  harvester->SetEnergySource(source);
89 
90  Time now = Simulator::Now();
91 
92  /*
93  * The energy harvester will recharge the energy source for m_timeS seconds.
94  */
95 
96  // Calculate remaining energy at simulation stop time
97  Simulator::Schedule(Seconds(m_timeS), &BasicEnergySource::UpdateEnergySource, source);
98 
99  double timeDelta = 0.000000001; // 1 nanosecond
100  // run simulation; stop just after last scheduled event
101  Simulator::Stop(Seconds(m_timeS + timeDelta));
102  Simulator::Run();
103 
104  // calculate energy harvested
105  double estRemainingEnergy = source->GetInitialEnergy();
106  // energy = power * time
107  estRemainingEnergy += harvester->GetPower() * m_timeS;
108 
109  // obtain remaining energy from source
110  double remainingEnergy = source->GetRemainingEnergy();
111  NS_LOG_DEBUG("Remaining energy is " << remainingEnergy);
112  NS_LOG_DEBUG("Estimated remaining energy is " << estRemainingEnergy);
113  NS_LOG_DEBUG("Difference is " << estRemainingEnergy - remainingEnergy);
114 
115  Simulator::Destroy();
116 
117  // check remaining energy
118  NS_TEST_ASSERT_MSG_EQ_TOL(remainingEnergy,
119  estRemainingEnergy,
120  m_tolerance,
121  "Incorrect Remaining energy!");
122 }
123 
130 {
131  public:
133 };
134 
136  : TestSuite("basic-energy-harvester", UNIT)
137 {
138  AddTestCase(new BasicEnergyHarvesterTestCase, TestCase::QUICK);
139 }
140 
static BasicEnergyHarvesterTestSuite g_basicEnergyHarvesterTestSuite
create an instance of the test suite
void DoRun() override
Implementation to actually run this TestCase.
ObjectFactory m_energySource
Energy source factory.
ObjectFactory m_energyHarvester
Energy harvester factory.
double m_tolerance
Tolerance for energy estimation.
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
double GetRemainingEnergy() override
double GetInitialEnergy() const override
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.