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 "ns3/assert.h"
13#include "ns3/log.h"
14#include "ns3/pointer.h"
15#include "ns3/simulator.h"
16#include "ns3/string.h"
17#include "ns3/trace-source-accessor.h"
18
19namespace ns3
20{
21namespace energy
22{
23
24NS_LOG_COMPONENT_DEFINE("BasicEnergyHarvester");
25NS_OBJECT_ENSURE_REGISTERED(BasicEnergyHarvester);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::energy::BasicEnergyHarvester")
32 .AddDeprecatedName("ns3::BasicEnergyHarvester")
34 .SetGroupName("Energy")
35 .AddConstructor<BasicEnergyHarvester>()
36 .AddAttribute("PeriodicHarvestedPowerUpdateInterval",
37 "Time between two consecutive periodic updates of the harvested power. "
38 "By default, the value is updated every 1 s",
39 TimeValue(Seconds(1.0)),
43 .AddAttribute("HarvestablePower",
44 "The harvestable power [Watts] that the energy harvester is allowed to "
45 "harvest. By default, the model will allow to harvest an amount of power "
46 "defined by a uniformly distributed random variable in 0 and 2.0 Watts",
47 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=2.0]"),
50 .AddTraceSource("HarvestedPower",
51 "Harvested power by the BasicEnergyHarvester.",
53 "ns3::TracedValueCallback::Double")
54 .AddTraceSource("TotalEnergyHarvested",
55 "Total energy harvested by the harvester.",
57 "ns3::TracedValueCallback::Double");
58 return tid;
59}
60
65
67{
68 NS_LOG_FUNCTION(this << updateInterval);
69 m_harvestedPowerUpdateInterval = updateInterval;
70}
71
76
77int64_t
79{
80 NS_LOG_FUNCTION(this << stream);
82 return 1;
83}
84
85void
87{
88 NS_LOG_FUNCTION(this << updateInterval);
89 m_harvestedPowerUpdateInterval = updateInterval;
90}
91
92Time
98
99/*
100 * Private functions start here.
101 */
102
103void
105{
106 NS_LOG_FUNCTION(this);
107 NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " BasicEnergyHarvester(" << GetNode()->GetId()
108 << "): Updating harvesting power.");
109
111
112 NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
113
114 double energyHarvested = 0.0;
115
116 // do not update if simulation has finished
118 {
119 NS_LOG_DEBUG("BasicEnergyHarvester: Simulation Finished.");
120 return;
121 }
122
124
126
127 energyHarvested = duration.GetSeconds() * m_harvestedPower;
128
129 // update total energy harvested
130 m_totalEnergyHarvestedJ += energyHarvested;
131
132 // notify energy source
133 GetEnergySource()->UpdateEnergySource();
134
135 // update last harvesting time stamp
137
140 this);
141}
142
143void
145{
146 NS_LOG_FUNCTION(this);
147
149
150 UpdateHarvestedPower(); // start periodic harvesting update
151}
152
153void
158
159void
161{
162 NS_LOG_FUNCTION(this);
163
165
167 << " BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
168}
169
170double
176
177} // namespace energy
178} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
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:560
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:160
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
@ S
second
Definition nstime.h:105
a unique identifier for an interface.
Definition type-id.h:48
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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: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:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#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
#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:1308
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:1396
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416