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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
8 */
9
10#include "ns3/basic-energy-harvester.h"
11#include "ns3/basic-energy-source.h"
12#include "ns3/config.h"
13#include "ns3/double.h"
14#include "ns3/log.h"
15#include "ns3/node.h"
16#include "ns3/simulator.h"
17#include "ns3/string.h"
18#include "ns3/test.h"
19
20using namespace ns3;
21using namespace ns3::energy;
22
23NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvesterTestSuite");
24
25/**
26 * \ingroup energy-tests
27 *
28 * \brief Energy Harvester Test
29 */
31{
32 public:
35
36 void DoRun() override;
37
38 double m_timeS; //!< Time, in seconds
39 double m_tolerance; //!< Tolerance for energy estimation
40
41 ObjectFactory m_energySource; //!< Energy source factory
42 ObjectFactory m_energyHarvester; //!< Energy harvester factory
43};
44
46 : TestCase("Basic Energy Harvester test case")
47{
48 m_timeS = 15; // harvest energy for 15 seconds
49 m_tolerance = 1.0e-13; //
50}
51
55
56void
58{
59 // set types
60 m_energySource.SetTypeId("ns3::energy::BasicEnergySource");
61 m_energyHarvester.SetTypeId("ns3::energy::BasicEnergyHarvester");
62 // create node
64
65 // create Energy Source
67 // aggregate Energy Source to the node
68 node->AggregateObject(source);
69
70 // create energy harvester
72 // Set the Energy Harvesting update interval to a value grater than the
73 // simulation duration, so that the power provided by the harvester is constant
74 harvester->SetHarvestedPowerUpdateInterval(Seconds(m_timeS + 1.0));
75 // Connect the Basic Energy Harvester to the Energy Source
76 source->ConnectEnergyHarvester(harvester);
77 harvester->SetNode(node);
78 harvester->SetEnergySource(source);
79
80 Time now = Simulator::Now();
81
82 /*
83 * The energy harvester will recharge the energy source for m_timeS seconds.
84 */
85
86 // Calculate remaining energy at simulation stop time
88
89 double timeDelta = 0.000000001; // 1 nanosecond
90 // run simulation; stop just after last scheduled event
91 Simulator::Stop(Seconds(m_timeS + timeDelta));
93
94 // calculate energy harvested
95 double estRemainingEnergy = source->GetInitialEnergy();
96 // energy = power * time
97 estRemainingEnergy += harvester->GetPower() * m_timeS;
98
99 // obtain remaining energy from source
100 double remainingEnergy = source->GetRemainingEnergy();
101 NS_LOG_DEBUG("Remaining energy is " << remainingEnergy);
102 NS_LOG_DEBUG("Estimated remaining energy is " << estRemainingEnergy);
103 NS_LOG_DEBUG("Difference is " << estRemainingEnergy - remainingEnergy);
104
106
107 // check remaining energy
108 NS_TEST_ASSERT_MSG_EQ_TOL(remainingEnergy,
109 estRemainingEnergy,
111 "Incorrect Remaining energy!");
112}
113
114/**
115 * \ingroup energy-tests
116 *
117 * \brief Energy harvester TestSuite
118 */
120{
121 public:
123};
124
126 : TestSuite("basic-energy-harvester", Type::UNIT)
127{
128 AddTestCase(new BasicEnergyHarvesterTestCase, TestCase::Duration::QUICK);
129}
130
131/// create an instance of the test suite
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.
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.