A Discrete-Event Network Simulator
API
basic-energy-source.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19  */
20 
21 #include "basic-energy-source.h"
22 #include "ns3/log.h"
23 #include "ns3/assert.h"
24 #include "ns3/double.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/simulator.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("BasicEnergySource");
31 
32 NS_OBJECT_ENSURE_REGISTERED (BasicEnergySource);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::BasicEnergySource")
39  .SetGroupName ("Energy")
40  .AddConstructor<BasicEnergySource> ()
41  .AddAttribute ("BasicEnergySourceInitialEnergyJ",
42  "Initial energy stored in basic energy source.",
43  DoubleValue (10), // in Joules
46  MakeDoubleChecker<double> ())
47  .AddAttribute ("BasicEnergySupplyVoltageV",
48  "Initial supply voltage for basic energy source.",
49  DoubleValue (3.0), // in Volts
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("BasicEnergyLowBatteryThreshold",
54  "Low battery threshold for basic energy source.",
55  DoubleValue (0.10), // as a fraction of the initial energy
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("BasicEnergyHighBatteryThreshold",
59  "High battery threshold for basic energy source.",
60  DoubleValue (0.15), // as a fraction of the initial energy
62  MakeDoubleChecker<double> ())
63  .AddAttribute ("PeriodicEnergyUpdateInterval",
64  "Time between two consecutive periodic energy updates.",
65  TimeValue (Seconds (1.0)),
68  MakeTimeChecker ())
69  .AddTraceSource ("RemainingEnergy",
70  "Remaining energy at BasicEnergySource.",
72  "ns3::TracedValueCallback::Double")
73  ;
74  return tid;
75 }
76 
78 {
79  NS_LOG_FUNCTION (this);
80  m_lastUpdateTime = Seconds (0.0);
81  m_depleted = false;
82 }
83 
85 {
86  NS_LOG_FUNCTION (this);
87 }
88 
89 void
90 BasicEnergySource::SetInitialEnergy (double initialEnergyJ)
91 {
92  NS_LOG_FUNCTION (this << initialEnergyJ);
93  NS_ASSERT (initialEnergyJ >= 0);
94  m_initialEnergyJ = initialEnergyJ;
96 }
97 
98 void
99 BasicEnergySource::SetSupplyVoltage (double supplyVoltageV)
100 {
101  NS_LOG_FUNCTION (this << supplyVoltageV);
102  m_supplyVoltageV = supplyVoltageV;
103 }
104 
105 void
107 {
108  NS_LOG_FUNCTION (this << interval);
109  m_energyUpdateInterval = interval;
110 }
111 
112 Time
114 {
115  NS_LOG_FUNCTION (this);
116  return m_energyUpdateInterval;
117 }
118 
119 double
121 {
122  NS_LOG_FUNCTION (this);
123  return m_supplyVoltageV;
124 }
125 
126 double
128 {
129  NS_LOG_FUNCTION (this);
130  return m_initialEnergyJ;
131 }
132 
133 double
135 {
136  NS_LOG_FUNCTION (this);
137  // update energy source to get the latest remaining energy.
139  return m_remainingEnergyJ;
140 }
141 
142 double
144 {
145  NS_LOG_FUNCTION (this);
146  // update energy source to get the latest remaining energy.
149 }
150 
151 void
153 {
154  NS_LOG_FUNCTION (this);
155  NS_LOG_DEBUG ("BasicEnergySource:Updating remaining energy.");
156 
157  double remainingEnergy = m_remainingEnergyJ;
159 
161 
163  {
164  m_depleted = true;
166  }
168  {
169  m_depleted = false;
171  }
172  else if (m_remainingEnergyJ != remainingEnergy)
173  {
175  }
176 
178  {
181  this);
182  }
183 }
184 
185 /*
186  * Private functions start here.
187  */
188 
189 void
191 {
192  NS_LOG_FUNCTION (this);
193  UpdateEnergySource (); // start periodic update
194 }
195 
196 void
198 {
199  NS_LOG_FUNCTION (this);
200  BreakDeviceEnergyModelRefCycle (); // break reference cycle
201 }
202 
203 void
205 {
206  NS_LOG_FUNCTION (this);
207  NS_LOG_DEBUG ("BasicEnergySource:Energy depleted!");
208  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
209 }
210 
211 void
213 {
214  NS_LOG_FUNCTION (this);
215  NS_LOG_DEBUG ("BasicEnergySource:Energy recharged!");
216  NotifyEnergyRecharged (); // notify DeviceEnergyModel objects
217 }
218 
219 void
221 {
222  NS_LOG_FUNCTION (this);
223  double totalCurrentA = CalculateTotalCurrent ();
224  Time duration = Simulator::Now () - m_lastUpdateTime;
225  NS_ASSERT (duration.IsPositive ());
226  // energy = current * voltage * time
227  double energyToDecreaseJ = (totalCurrentA * m_supplyVoltageV * duration).GetSeconds ();
228  NS_ASSERT (m_remainingEnergyJ >= energyToDecreaseJ);
229  m_remainingEnergyJ -= energyToDecreaseJ;
230  NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
231 }
232 
233 } // 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
ns3::BasicEnergySource::m_lowBatteryTh
double m_lowBatteryTh
Definition: basic-energy-source.h:141
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::BasicEnergySource::GetEnergyFraction
virtual double GetEnergyFraction(void)
Definition: basic-energy-source.cc:143
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::BasicEnergySource::GetTypeId
static TypeId GetTypeId(void)
Definition: basic-energy-source.cc:35
ns3::BasicEnergySource::m_depleted
bool m_depleted
Definition: basic-energy-source.h:143
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::BasicEnergySource::m_supplyVoltageV
double m_supplyVoltageV
Definition: basic-energy-source.h:140
ns3::EnergySource::NotifyEnergyChanged
void NotifyEnergyChanged(void)
This function notifies all DeviceEnergyModel of energy changed event.
Definition: energy-source.cc:216
ns3::BasicEnergySource::SetSupplyVoltage
void SetSupplyVoltage(double supplyVoltageV)
Definition: basic-energy-source.cc:99
ns3::BasicEnergySource::UpdateEnergySource
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
Definition: basic-energy-source.cc:152
ns3::BasicEnergySource::HandleEnergyRechargedEvent
void HandleEnergyRechargedEvent(void)
Handles the remaining energy exceeding the high threshold after it went below the low threshold.
Definition: basic-energy-source.cc:212
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::BasicEnergySource::SetInitialEnergy
void SetInitialEnergy(double initialEnergyJ)
Definition: basic-energy-source.cc:90
ns3::BasicEnergySource::m_highBatteryTh
double m_highBatteryTh
Definition: basic-energy-source.h:142
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
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::EnergySource::NotifyEnergyDrained
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
Definition: energy-source.cc:192
ns3::BasicEnergySource::DoDispose
void DoDispose(void)
Defined in ns3::Object.
Definition: basic-energy-source.cc:197
ns3::BasicEnergySource::m_lastUpdateTime
Time m_lastUpdateTime
Definition: basic-energy-source.h:147
ns3::EnergySource::CalculateTotalCurrent
double CalculateTotalCurrent(void)
Definition: energy-source.cc:163
ns3::BasicEnergySource::GetSupplyVoltage
virtual double GetSupplyVoltage(void) const
Definition: basic-energy-source.cc:120
ns3::BasicEnergySource::~BasicEnergySource
virtual ~BasicEnergySource()
Definition: basic-energy-source.cc:84
basic-energy-source.h
ns3::BasicEnergySource
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
Definition: basic-energy-source.h:38
ns3::BasicEnergySource::GetEnergyUpdateInterval
Time GetEnergyUpdateInterval(void) const
Definition: basic-energy-source.cc:113
ns3::EnergySource
Introspection did not find any typical Config paths.
Definition: energy-source.h:82
ns3::BasicEnergySource::m_energyUpdateInterval
Time m_energyUpdateInterval
Definition: basic-energy-source.h:148
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::BasicEnergySource::CalculateRemainingEnergy
void CalculateRemainingEnergy(void)
Calculates remaining energy.
Definition: basic-energy-source.cc:220
ns3::BasicEnergySource::GetInitialEnergy
virtual double GetInitialEnergy(void) const
Definition: basic-energy-source.cc:127
ns3::MakeDoubleAccessor
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:42
ns3::BasicEnergySource::m_remainingEnergyJ
TracedValue< double > m_remainingEnergyJ
Definition: basic-energy-source.h:145
ns3::Time::IsPositive
bool IsPositive(void) const
Exactly equivalent to t >= 0.
Definition: nstime.h:317
ns3::BasicEnergySource::m_energyUpdateEvent
EventId m_energyUpdateEvent
Definition: basic-energy-source.h:146
ns3::EnergySource::BreakDeviceEnergyModelRefCycle
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
Definition: energy-source.cc:228
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::BasicEnergySource::m_initialEnergyJ
double m_initialEnergyJ
Definition: basic-energy-source.h:139
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::BasicEnergySource::SetEnergyUpdateInterval
void SetEnergyUpdateInterval(Time interval)
Definition: basic-energy-source.cc:106
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::EnergySource::NotifyEnergyRecharged
void NotifyEnergyRecharged(void)
This function notifies all DeviceEnergyModel of energy recharged event.
Definition: energy-source.cc:204
ns3::BasicEnergySource::HandleEnergyDrainedEvent
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
Definition: basic-energy-source.cc:204
ns3::BasicEnergySource::GetRemainingEnergy
virtual double GetRemainingEnergy(void)
Definition: basic-energy-source.cc:134
ns3::BasicEnergySource::BasicEnergySource
BasicEnergySource()
Definition: basic-energy-source.cc:77
ns3::BasicEnergySource::DoInitialize
void DoInitialize(void)
Defined in ns3::Object.
Definition: basic-energy-source.cc:190
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
ns3::EventId::IsExpired
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65