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 
42 {
43 public:
46 
47  void DoRun (void);
48 
49  double m_timeS;
50  double m_tolerance;
51 
54 
55 };
56 
58  : TestCase ("Basic Energy Harvester test case")
59 {
60  m_timeS = 15; // harvest energy for 15 seconds
61  m_tolerance = 1.0e-13; //
62 }
63 
65 {
66 }
67 
68 void
70 {
71  // set types
72  m_energySource.SetTypeId ("ns3::BasicEnergySource");
73  m_energyHarvester.SetTypeId ("ns3::BasicEnergyHarvester");
74  // create node
75  Ptr<Node> node = CreateObject<Node> ();
76 
77  // create Energy Source
79  // aggregate Energy Source to the node
80  node->AggregateObject (source);
81 
82  //create energy harvester
84  // Set the Energy Harvesting update interval to a value grater than the
85  // simulation duration, so that the power provided by the harvester is constant
86  harvester->SetHarvestedPowerUpdateInterval (Seconds (m_timeS + 1.0));
87  // Connect the Basic Energy Harvester to the Energy Source
88  source->ConnectEnergyHarvester (harvester);
89  harvester->SetNode (node);
90  harvester->SetEnergySource (source);
91 
92  Time now = Simulator::Now ();
93 
94  /*
95  * The energy harvester will recharge the energy source for m_timeS seconds.
96  */
97 
98  // Calculate remaining energy at simulation stop time
99  Simulator::Schedule (Seconds (m_timeS),
100  &BasicEnergySource::UpdateEnergySource, source);
101 
102  double timeDelta = 0.000000001; // 1 nanosecond
103  // run simulation; stop just after last scheduled event
104  Simulator::Stop (Seconds (m_timeS + timeDelta));
105  Simulator::Run ();
106 
107  // calculate energy harvested
108  double estRemainingEnergy = source->GetInitialEnergy ();
109  // energy = power * time
110  estRemainingEnergy += harvester->GetPower () * m_timeS;
111 
112  // obtain remaining energy from source
113  double remainingEnergy = source->GetRemainingEnergy ();
114  NS_LOG_DEBUG ("Remaining energy is " << remainingEnergy);
115  NS_LOG_DEBUG ("Estimated remaining energy is " << estRemainingEnergy);
116  NS_LOG_DEBUG ("Difference is " << estRemainingEnergy - remainingEnergy);
117 
118  Simulator::Destroy ();
119 
120  // check remaining energy
121  NS_TEST_ASSERT_MSG_EQ_TOL (remainingEnergy, estRemainingEnergy, m_tolerance,
122  "Incorrect Remaining energy!");
123 
124 }
125 
132 {
133 public:
135 };
136 
138  : TestSuite ("basic-energy-harvester", UNIT)
139 {
140  AddTestCase (new BasicEnergyHarvesterTestCase, TestCase::QUICK);
141 }
142 
static BasicEnergyHarvesterTestSuite g_basicEnergyHarvesterTestSuite
create an instance of the test suite
void DoRun(void)
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.
virtual double GetRemainingEnergy(void)
virtual double GetInitialEnergy(void) const
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
Instantiate subclasses of ns3::Object.
Ptr< Object > Create(void) 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:252
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
#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:323
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.