A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
31using namespace ns3;
32
33NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvesterTestSuite");
34
41{
42 public:
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
66void
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
98
99 double timeDelta = 0.000000001; // 1 nanosecond
100 // run simulation; stop just after last scheduled event
101 Simulator::Stop(Seconds(m_timeS + timeDelta));
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
116
117 // check remaining energy
118 NS_TEST_ASSERT_MSG_EQ_TOL(remainingEnergy,
119 estRemainingEnergy,
121 "Incorrect Remaining energy!");
122}
123
130{
131 public:
133};
134
136 : TestSuite("basic-energy-harvester", Type::UNIT)
137{
138 AddTestCase(new BasicEnergyHarvesterTestCase, TestCase::Duration::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.
void UpdateEnergySource() override
Implements UpdateEnergySource.
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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
#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:338
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.