A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
basic-energy-harvester.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
22
23#include "ns3/assert.h"
24#include "ns3/log.h"
25#include "ns3/pointer.h"
26#include "ns3/simulator.h"
27#include "ns3/string.h"
28#include "ns3/trace-source-accessor.h"
29
30namespace ns3
31{
32namespace energy
33{
34
35NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvester");
36NS_OBJECT_ENSURE_REGISTERED(BasicEnergyHarvester);
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::BasicEnergyHarvester")
44 .SetGroupName("Energy")
45 .AddConstructor<BasicEnergyHarvester>()
46 .AddAttribute("PeriodicHarvestedPowerUpdateInterval",
47 "Time between two consecutive periodic updates of the harvested power. "
48 "By default, the value is updated every 1 s",
49 TimeValue(Seconds(1.0)),
53 .AddAttribute("HarvestablePower",
54 "The harvestable power [Watts] that the energy harvester is allowed to "
55 "harvest. By default, the model will allow to harvest an amount of power "
56 "defined by a uniformly distributed random variable in 0 and 2.0 Watts",
57 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=2.0]"),
59 MakePointerChecker<RandomVariableStream>())
60 .AddTraceSource("HarvestedPower",
61 "Harvested power by the BasicEnergyHarvester.",
63 "ns3::TracedValueCallback::Double")
64 .AddTraceSource("TotalEnergyHarvested",
65 "Total energy harvested by the harvester.",
67 "ns3::TracedValueCallback::Double");
68 return tid;
69}
70
72{
73 NS_LOG_FUNCTION(this);
74}
75
77{
78 NS_LOG_FUNCTION(this << updateInterval);
79 m_harvestedPowerUpdateInterval = updateInterval;
80}
81
83{
84 NS_LOG_FUNCTION(this);
85}
86
87int64_t
89{
90 NS_LOG_FUNCTION(this << stream);
92 return 1;
93}
94
95void
97{
98 NS_LOG_FUNCTION(this << updateInterval);
99 m_harvestedPowerUpdateInterval = updateInterval;
100}
101
102Time
104{
105 NS_LOG_FUNCTION(this);
107}
108
109/*
110 * Private functions start here.
111 */
112
113void
115{
116 NS_LOG_FUNCTION(this);
117 NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " BasicEnergyHarvester(" << GetNode()->GetId()
118 << "): Updating harvesting power.");
119
121
122 NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
123
124 double energyHarvested = 0.0;
125
126 // do not update if simulation has finished
128 {
129 NS_LOG_DEBUG("BasicEnergyHarvester: Simulation Finished.");
130 return;
131 }
132
134
136
137 energyHarvested = duration.GetSeconds() * m_harvestedPower;
138
139 // update total energy harvested
140 m_totalEnergyHarvestedJ += energyHarvested;
141
142 // notify energy source
143 GetEnergySource()->UpdateEnergySource();
144
145 // update last harvesting time stamp
147
150 this);
151}
152
153void
155{
156 NS_LOG_FUNCTION(this);
157
159
160 UpdateHarvestedPower(); // start periodic harvesting update
161}
162
163void
165{
166 NS_LOG_FUNCTION(this);
167}
168
169void
171{
172 NS_LOG_FUNCTION(this);
173
175
177 << " BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
178}
179
180double
182{
183 NS_LOG_FUNCTION(this);
184 return m_harvestedPower;
185}
186
187} // namespace energy
188} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:171
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
Time m_harvestedPowerUpdateInterval
harvestable energy update interval
static TypeId GetTypeId()
Get the type ID.
TracedValue< double > m_totalEnergyHarvestedJ
total harvested energy, in Joule
Ptr< RandomVariableStream > m_harvestablePower
Random variable for the harvestable power.
void CalculateHarvestedPower()
Calculates harvested Power.
void DoInitialize() override
Defined in ns3::Object.
void UpdateHarvestedPower()
This function is called every m_energyHarvestingUpdateInterval in order to update the amount of power...
Time m_lastHarvestingUpdateTime
last harvesting time
void DoDispose() override
Defined in ns3::Object.
TracedValue< double > m_harvestedPower
current harvested power, in Watt
void SetHarvestedPowerUpdateInterval(Time updateInterval)
EventId m_energyHarvestingUpdateEvent
energy harvesting event
Energy harvester base class.
Ptr< EnergySource > GetEnergySource() const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
#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_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1407
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427