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{
32
33NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvester");
34
35NS_OBJECT_ENSURE_REGISTERED(BasicEnergyHarvester);
36
37TypeId
39{
40 static TypeId tid =
41 TypeId("ns3::BasicEnergyHarvester")
43 .SetGroupName("Energy")
44 .AddConstructor<BasicEnergyHarvester>()
45 .AddAttribute("PeriodicHarvestedPowerUpdateInterval",
46 "Time between two consecutive periodic updates of the harvested power. "
47 "By default, the value is updated every 1 s",
48 TimeValue(Seconds(1.0)),
52 .AddAttribute("HarvestablePower",
53 "The harvestable power [Watts] that the energy harvester is allowed to "
54 "harvest. By default, the model will allow to harvest an amount of power "
55 "defined by a uniformly distributed random variable in 0 and 2.0 Watts",
56 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=2.0]"),
58 MakePointerChecker<RandomVariableStream>())
59 .AddTraceSource("HarvestedPower",
60 "Harvested power by the BasicEnergyHarvester.",
62 "ns3::TracedValueCallback::Double")
63 .AddTraceSource("TotalEnergyHarvested",
64 "Total energy harvested by the harvester.",
66 "ns3::TracedValueCallback::Double");
67 return tid;
68}
69
71{
72 NS_LOG_FUNCTION(this);
73}
74
76{
77 NS_LOG_FUNCTION(this << updateInterval);
78 m_harvestedPowerUpdateInterval = updateInterval;
79}
80
82{
83 NS_LOG_FUNCTION(this);
84}
85
86int64_t
88{
89 NS_LOG_FUNCTION(this << stream);
91 return 1;
92}
93
94void
96{
97 NS_LOG_FUNCTION(this << updateInterval);
98 m_harvestedPowerUpdateInterval = updateInterval;
99}
100
101Time
103{
104 NS_LOG_FUNCTION(this);
106}
107
108/*
109 * Private functions start here.
110 */
111
112void
114{
115 NS_LOG_FUNCTION(this);
116 NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " BasicEnergyHarvester(" << GetNode()->GetId()
117 << "): Updating harvesting power.");
118
120
121 NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
122
123 double energyHarvested = 0.0;
124
125 // do not update if simulation has finished
127 {
128 NS_LOG_DEBUG("BasicEnergyHarvester: Simulation Finished.");
129 return;
130 }
131
133
135
136 energyHarvested = duration.GetSeconds() * m_harvestedPower;
137
138 // update total energy harvested
139 m_totalEnergyHarvestedJ += energyHarvested;
140
141 // notify energy source
142 GetEnergySource()->UpdateEnergySource();
143
144 // update last harvesting time stamp
146
149 this);
150}
151
152void
154{
155 NS_LOG_FUNCTION(this);
156
158
159 UpdateHarvestedPower(); // start periodic harvesting update
160}
161
162void
164{
165 NS_LOG_FUNCTION(this);
166}
167
168void
170{
171 NS_LOG_FUNCTION(this);
172
174
176 << " BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
177}
178
179double
181{
182 NS_LOG_FUNCTION(this);
183 return m_harvestedPower;
184}
185
186} // namespace ns3
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
Time m_lastHarvestingUpdateTime
last harvesting time
void UpdateHarvestedPower()
This function is called every m_energyHarvestingUpdateInterval in order to update the amount of power...
TracedValue< double > m_harvestedPower
current harvested power, in Watt
Ptr< RandomVariableStream > m_harvestablePower
Random variable for the harvestable power.
double DoGetPower() const override
static TypeId GetTypeId()
Get the type ID.
void DoInitialize() override
Defined in ns3::Object.
void SetHarvestedPowerUpdateInterval(Time updateInterval)
TracedValue< double > m_totalEnergyHarvestedJ
total harvested energy, in Joule
void DoDispose() override
Defined in ns3::Object.
int64_t AssignStreams(int64_t stream)
void CalculateHarvestedPower()
Calculates harvested Power.
EventId m_energyHarvestingUpdateEvent
energy harvesting event
Time m_harvestedPowerUpdateInterval
harvestable energy update interval
Energy harvester base class.
Ptr< EnergySource > GetEnergySource() const
Ptr< Node > GetNode() const
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
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#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
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
#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:1326
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.