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);
113  NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
114  << "s 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 
172  NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
173  << "s BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
174 }
175 
176 double
178 {
179  NS_LOG_FUNCTION (this);
180  return m_harvestedPower;
181 }
182 
183 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Hold variables of type string.
Definition: string.h:41
void SetHarvestedPowerUpdateInterval(Time updateInterval)
TracedValue< double > m_harvestedPower
virtual double DoGetPower(void) const
Ptr< Node > GetNode(void) const
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void DoInitialize(void)
Defined in ns3::Object.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
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:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
TracedValue< double > m_totalEnergyHarvestedJ
AttributeValue implementation for Time.
Definition: nstime.h:1076
void CalculateHarvestedPower(void)
Calculates harvested Power.
void UpdateHarvestedPower(void)
This function is called every m_energyHarvestingUpdateInterval in order to update the amount of power...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:367
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:1077
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
Time GetHarvestedPowerUpdateInterval(void) const
Energy harvester base class.
Ptr< EnergySource > GetEnergySource(void) const
Ptr< RandomVariableStream > m_harvestablePower
void DoDispose(void)
Defined in ns3::Object.
int64_t AssignStreams(int64_t stream)
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:219
BasicEnergyHarvester increases remaining energy stored in an associated Energy Source.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915