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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
8 */
9
11
12#include "energy-source.h"
13
14#include "ns3/assert.h"
15#include "ns3/log.h"
16#include "ns3/pointer.h"
17#include "ns3/simulator.h"
18#include "ns3/string.h"
19#include "ns3/trace-source-accessor.h"
20
21namespace ns3
22{
23namespace energy
24{
25
26NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvester");
28
29TypeId
31{
32 static TypeId tid =
33 TypeId("ns3::energy::BasicEnergyHarvester")
34 .AddDeprecatedName("ns3::BasicEnergyHarvester")
36 .SetGroupName("Energy")
37 .AddConstructor<BasicEnergyHarvester>()
38 .AddAttribute("PeriodicHarvestedPowerUpdateInterval",
39 "Time between two consecutive periodic updates of the harvested power. "
40 "By default, the value is updated every 1 s",
45 .AddAttribute("HarvestablePower",
46 "The harvestable power [Watts] that the energy harvester is allowed to "
47 "harvest. By default, the model will allow to harvest an amount of power "
48 "defined by a uniformly distributed random variable in 0 and 2.0 Watts",
49 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=2.0]"),
52 .AddTraceSource("HarvestedPower",
53 "Harvested power by the BasicEnergyHarvester.",
55 "ns3::TracedValueCallback::Double")
56 .AddTraceSource("TotalEnergyHarvested",
57 "Total energy harvested by the harvester.",
59 "ns3::TracedValueCallback::Double");
60 return tid;
61}
62
67
69{
70 NS_LOG_FUNCTION(this << updateInterval);
71 m_harvestedPowerUpdateInterval = updateInterval;
72}
73
78
79int64_t
81{
82 NS_LOG_FUNCTION(this << stream);
83 m_harvestablePower->SetStream(stream);
84 return 1;
85}
86
87void
89{
90 NS_LOG_FUNCTION(this << updateInterval);
91 m_harvestedPowerUpdateInterval = updateInterval;
92}
93
94Time
100
101/*
102 * Private functions start here.
103 */
104
105void
107{
108 NS_LOG_FUNCTION(this);
109 NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " BasicEnergyHarvester(" << GetNode()->GetId()
110 << "): Updating harvesting power.");
111
113
114 NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
115
116 double energyHarvested = 0.0;
117
118 // do not update if simulation has finished
120 {
121 NS_LOG_DEBUG("BasicEnergyHarvester: Simulation Finished.");
122 return;
123 }
124
126
128
129 energyHarvested = duration.GetSeconds() * m_harvestedPower;
130
131 // update total energy harvested
132 m_totalEnergyHarvestedJ += energyHarvested;
133
134 // notify energy source
135 GetEnergySource()->UpdateEnergySource();
136
137 // update last harvesting time stamp
139
142 this);
143}
144
145void
147{
148 NS_LOG_FUNCTION(this);
149
151
152 UpdateHarvestedPower(); // start periodic harvesting update
153}
154
155void
160
161void
163{
164 NS_LOG_FUNCTION(this);
165
167
169 << " BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
170}
171
172double
178
179} // namespace energy
180} // namespace ns3
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:154
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:414
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:399
@ S
second
Definition nstime.h:107
AttributeValue implementation for Time.
Definition nstime.h:1483
a unique identifier for an interface.
Definition type-id.h:49
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:860
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
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
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:55
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:250
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:273
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition nstime.h:1484
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1504
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#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:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
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.