A Discrete-Event Network Simulator
API
basic-energy-harvester.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Wireless Communications and Networking Group (WCNG),
4  * University of Rochester, Rochester, NY, USA.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
20  */
21 
22 #include "basic-energy-harvester.h"
23 
24 #include "ns3/log.h"
25 #include "ns3/assert.h"
26 #include "ns3/pointer.h"
27 #include "ns3/string.h"
28 #include "ns3/trace-source-accessor.h"
29 #include "ns3/simulator.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("BasicEnergyHarvester");
34 
35 NS_OBJECT_ENSURE_REGISTERED (BasicEnergyHarvester);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::BasicEnergyHarvester")
42  .SetGroupName ("Energy")
43  .AddConstructor<BasicEnergyHarvester> ()
44  .AddAttribute ("PeriodicHarvestedPowerUpdateInterval",
45  "Time between two consecutive periodic updates of the harvested power. By default, the value is updated every 1 s",
46  TimeValue (Seconds (1.0)),
49  MakeTimeChecker ())
50  .AddAttribute ("HarvestablePower",
51  "The harvestable power [Watts] that the energy harvester is allowed to harvest. By default, the model will allow to harvest an amount of power defined by a uniformly distributed random variable in 0 and 2.0 Watts",
52  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=2.0]"),
54  MakePointerChecker<RandomVariableStream> ())
55  .AddTraceSource ("HarvestedPower",
56  "Harvested power by the BasicEnergyHarvester.",
58  "ns3::TracedValueCallback::Double")
59  .AddTraceSource ("TotalEnergyHarvested",
60  "Total energy harvested by the harvester.",
62  "ns3::TracedValueCallback::Double")
63  ;
64  return tid;
65 }
66 
68 {
69  NS_LOG_FUNCTION (this);
70 }
71 
73 {
74  NS_LOG_FUNCTION (this << updateInterval);
75  m_harvestedPowerUpdateInterval = updateInterval;
76 }
77 
79 {
80  NS_LOG_FUNCTION (this);
81 }
82 
83 int64_t
85 {
86  NS_LOG_FUNCTION (this << stream);
87  m_harvestablePower->SetStream (stream);
88  return 1;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION (this << updateInterval);
95  m_harvestedPowerUpdateInterval = updateInterval;
96 }
97 
98 Time
100 {
101  NS_LOG_FUNCTION (this);
103 }
104 
105 /*
106  * Private functions start here.
107  */
108 
109 void
111 {
112  NS_LOG_FUNCTION (this);
114  << " BasicEnergyHarvester(" << GetNode ()->GetId () << "): Updating harvesting power.");
115 
117 
118  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
119 
120  double energyHarvested = 0.0;
121 
122  // do not update if simulation has finished
123  if (Simulator::IsFinished ())
124  {
125  NS_LOG_DEBUG ("BasicEnergyHarvester: Simulation Finished.");
126  return;
127  }
128 
130 
132 
133  energyHarvested = duration.GetSeconds () * m_harvestedPower;
134 
135  // update total energy harvested
136  m_totalEnergyHarvestedJ += energyHarvested;
137 
138  // notify energy source
139  GetEnergySource ()->UpdateEnergySource ();
140 
141  // update last harvesting time stamp
143 
146  this);
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION (this);
153 
155 
156  UpdateHarvestedPower (); // start periodic harvesting update
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this);
163 }
164 
165 void
167 {
168  NS_LOG_FUNCTION (this);
169 
171 
173  << " BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
174 }
175 
176 double
178 {
179  NS_LOG_FUNCTION (this);
180  return m_harvestedPower;
181 }
182 
183 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::BasicEnergyHarvester::SetHarvestedPowerUpdateInterval
void SetHarvestedPowerUpdateInterval(Time updateInterval)
Definition: basic-energy-harvester.cc:92
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::EnergyHarvester::GetNode
Ptr< Node > GetNode(void) const
Definition: energy-harvester.cc:60
ns3::BasicEnergyHarvester::m_harvestedPowerUpdateInterval
Time m_harvestedPowerUpdateInterval
Definition: basic-energy-harvester.h:125
ns3::BasicEnergyHarvester::m_harvestedPower
TracedValue< double > m_harvestedPower
Definition: basic-energy-harvester.h:120
ns3::BasicEnergyHarvester::m_harvestablePower
Ptr< RandomVariableStream > m_harvestablePower
Definition: basic-energy-harvester.h:118
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::BasicEnergyHarvester::AssignStreams
int64_t AssignStreams(int64_t stream)
Definition: basic-energy-harvester.cc:84
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::BasicEnergyHarvester::m_energyHarvestingUpdateEvent
EventId m_energyHarvestingUpdateEvent
Definition: basic-energy-harvester.h:123
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
ns3::BasicEnergyHarvester::GetTypeId
static TypeId GetTypeId(void)
Definition: basic-energy-harvester.cc:38
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::BasicEnergyHarvester::CalculateHarvestedPower
void CalculateHarvestedPower(void)
Calculates harvested Power.
Definition: basic-energy-harvester.cc:166
ns3::EnergyHarvester::GetEnergySource
Ptr< EnergySource > GetEnergySource(void) const
Definition: energy-harvester.cc:75
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::Time::GetNanoSeconds
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:392
ns3::MakePointerAccessor
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:227
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::BasicEnergyHarvester::DoGetPower
virtual double DoGetPower(void) const
Definition: basic-energy-harvester.cc:177
ns3::BasicEnergyHarvester::UpdateHarvestedPower
void UpdateHarvestedPower(void)
This function is called every m_energyHarvestingUpdateInterval in order to update the amount of power...
Definition: basic-energy-harvester.cc:110
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::BasicEnergyHarvester::m_totalEnergyHarvestedJ
TracedValue< double > m_totalEnergyHarvestedJ
Definition: basic-energy-harvester.h:121
ns3::BasicEnergyHarvester::DoInitialize
void DoInitialize(void)
Defined in ns3::Object.
Definition: basic-energy-harvester.cc:150
ns3::BasicEnergyHarvester::m_lastHarvestingUpdateTime
Time m_lastHarvestingUpdateTime
Definition: basic-energy-harvester.h:124
ns3::RandomVariableStream::GetValue
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::BasicEnergyHarvester::GetHarvestedPowerUpdateInterval
Time GetHarvestedPowerUpdateInterval(void) const
Definition: basic-energy-harvester.cc:99
ns3::Simulator::IsFinished
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:165
basic-energy-harvester.h
ns3::BasicEnergyHarvester::BasicEnergyHarvester
BasicEnergyHarvester()
Definition: basic-energy-harvester.cc:67
ns3::RandomVariableStream::SetStream
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Definition: random-variable-stream.cc:100
ns3::BasicEnergyHarvester::DoDispose
void DoDispose(void)
Defined in ns3::Object.
Definition: basic-energy-harvester.cc:160
ns3::BasicEnergyHarvester
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
Definition: basic-energy-harvester.h:49
ns3::BasicEnergyHarvester::~BasicEnergyHarvester
virtual ~BasicEnergyHarvester()
Definition: basic-energy-harvester.cc:78
ns3::EnergyHarvester
Energy harvester base class.
Definition: energy-harvester.h:52
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
ns3::MakeTimeAccessor
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:1354