A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("BasicEnergyHarvester");
32 
33 namespace ns3 {
34 
35 NS_OBJECT_ENSURE_REGISTERED (BasicEnergyHarvester);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::BasicEnergyHarvester")
42  .AddConstructor<BasicEnergyHarvester> ()
43  .AddAttribute ("PeriodicHarvestedPowerUpdateInterval",
44  "Time between two consecutive periodic updates of the harvested power. By default, the value is updated every 1 s",
45  TimeValue (Seconds (1.0)),
48  MakeTimeChecker ())
49  .AddAttribute ("HarvestablePower",
50  "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",
51  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=2.0]"),
52  MakePointerAccessor (&BasicEnergyHarvester::m_harvestablePower),
53  MakePointerChecker<RandomVariableStream> ())
54  .AddTraceSource ("HarvestedPower",
55  "Harvested power by the BasicEnergyHarvester.",
57  .AddTraceSource ("TotalEnergyHarvested",
58  "Total energy harvested by the harvester.",
60  ;
61  return tid;
62 }
63 
65 {
66  NS_LOG_FUNCTION (this);
67 }
68 
70 {
71  NS_LOG_FUNCTION (this << updateInterval);
72  m_harvestedPowerUpdateInterval = updateInterval;
73 }
74 
76 {
77  NS_LOG_FUNCTION (this);
78 }
79 
80 int64_t
82 {
83  NS_LOG_FUNCTION (this << stream);
84  m_harvestablePower->SetStream (stream);
85  return 1;
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION (this << updateInterval);
92  m_harvestedPowerUpdateInterval = updateInterval;
93 }
94 
95 Time
97 {
98  NS_LOG_FUNCTION (this);
100 }
101 
102 /*
103  * Private functions start here.
104  */
105 
106 void
108 {
109  NS_LOG_FUNCTION (this);
110  NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
111  << "s BasicEnergyHarvester(" << GetNode ()->GetId () << "): Updating harvesting power.");
112 
114 
115  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
116 
117  double energyHarvested = 0.0;
118 
119  // do not update if simulation has finished
120  if (Simulator::IsFinished ())
121  {
122  NS_LOG_DEBUG ("BasicEnergyHarvester: Simulation Finished.");
123  return;
124  }
125 
127 
129 
130  energyHarvested = duration.GetSeconds () * m_harvestedPower;
131 
132  // update total energy harvested
133  m_totalEnergyHarvestedJ += energyHarvested;
134 
135  // notify energy source
136  GetEnergySource ()->UpdateEnergySource ();
137 
138  // update last harvesting time stamp
140 
143  this);
144 }
145 
146 void
148 {
149  NS_LOG_FUNCTION (this);
150 
152 
153  UpdateHarvestedPower (); // start periodic harvesting update
154 }
155 
156 void
158 {
159  NS_LOG_FUNCTION (this);
160 }
161 
162 void
164 {
165  NS_LOG_FUNCTION (this);
166 
168 
169  NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
170  << "s BasicEnergyHarvester:Harvested energy = " << m_harvestedPower);
171 }
172 
173 double
175 {
176  NS_LOG_FUNCTION (this);
177  return m_harvestedPower;
178 }
179 
180 } // namespace ns3
Time GetHarvestedPowerUpdateInterval(void) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
#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 this RNG stream.
Ptr< EnergySource > GetEnergySource(void) const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
hold variables of type string
Definition: string.h:18
void SetHarvestedPowerUpdateInterval(Time updateInterval)
TracedValue< double > m_harvestedPower
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
void DoInitialize(void)
Defined in ns3::Object.
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:444
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:322
TracedValue< double > m_totalEnergyHarvestedJ
Attribute for objects of type ns3::Time.
Definition: nstime.h:912
void CalculateHarvestedPower(void)
Calculates harvested Power.
void UpdateHarvestedPower(void)
This function is called every m_energyHarvestingUpdateInterval in order to update the amount of power...
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:334
Energy harvester base class.
virtual double DoGetPower(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:213
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
static bool IsFinished(void)
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
Definition: simulator.cc:150
a unique identifier for an interface.
Definition: type-id.h:49
Ptr< Node > GetNode(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610