A Discrete-Event Network Simulator
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 
37 {
38 public:
41 
42  void DoRun (void);
43 
44  double m_timeS; // in seconds
45  double m_tolerance; // tolerance for energy estimation
46 
49 
50 };
51 
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 
60 {
61 }
62 
63 void
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
74  // aggregate Energy Source to the node
75  node->AggregateObject (source);
76 
77  //create energy harvester
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 
122 {
123 public:
125 };
126 
128  : TestSuite ("basic-energy-harvester", UNIT)
129 {
130  AddTestCase (new BasicEnergyHarvesterTestCase, TestCase::QUICK);
131 }
132 
133 // create an instance of the test suite
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
static BasicEnergyHarvesterTestSuite g_basicEnergyHarvesterTestSuite
void SetHarvestedPowerUpdateInterval(Time updateInterval)
A suite of tests to run.
Definition: test.h:1343
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
encapsulates test code
Definition: test.h:1153
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void SetEnergySource(Ptr< EnergySource > source)
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Instantiate subclasses of ns3::Object.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
double GetPower(void) const
void DoRun(void)
Implementation to actually run this TestCase.
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergyHarvester.