A Discrete-Event Network Simulator
API
rv-battery-model.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 "rv-battery-model.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 #include <cmath>
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("RvBatteryModel");
32 
33 NS_OBJECT_ENSURE_REGISTERED (RvBatteryModel);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::RvBatteryModel")
40  .SetGroupName ("Energy")
41  .AddConstructor<RvBatteryModel> ()
42  .AddAttribute ("RvBatteryModelPeriodicEnergyUpdateInterval",
43  "RV battery model sampling interval.",
44  TimeValue (Seconds (1.0)),
47  MakeTimeChecker ())
48  .AddAttribute ("RvBatteryModelLowBatteryThreshold",
49  "Low battery threshold.",
50  DoubleValue (0.10), // as a fraction of the initial energy
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("RvBatteryModelOpenCircuitVoltage",
54  "RV battery model open circuit voltage.",
55  DoubleValue (4.1),
58  MakeDoubleChecker<double> ())
59  .AddAttribute ("RvBatteryModelCutoffVoltage",
60  "RV battery model cutoff voltage.",
61  DoubleValue (3.0),
64  MakeDoubleChecker<double> ())
65  .AddAttribute ("RvBatteryModelAlphaValue",
66  "RV battery model alpha value.",
67  DoubleValue (35220.0),
70  MakeDoubleChecker<double> ())
71  .AddAttribute ("RvBatteryModelBetaValue",
72  "RV battery model beta value.",
73  DoubleValue (0.637),
76  MakeDoubleChecker<double> ())
77  .AddAttribute ("RvBatteryModelNumOfTerms",
78  "The number of terms of the infinite sum for estimating battery level.",
79  IntegerValue (10), // value used in paper
82  MakeIntegerChecker<int> ())
83  .AddTraceSource ("RvBatteryModelBatteryLevel",
84  "RV battery model battery level.",
86  "ns3::TracedValueCallback::Double")
87  .AddTraceSource ("RvBatteryModelBatteryLifetime",
88  "RV battery model battery lifetime.",
90  "ns3::TracedValueCallback::Time")
91  ;
92  return tid;
93 }
94 
96 {
97  NS_LOG_FUNCTION (this);
99  m_timeStamps.push_back (m_lastSampleTime);
100  m_previousLoad = -1.0;
101  m_batteryLevel = 1; // fully charged
102  m_lifetime = Seconds (0.0);
103 }
104 
106 {
107  NS_LOG_FUNCTION (this);
108 }
109 
110 double
112 {
113  NS_LOG_FUNCTION (this);
114  return m_alpha * GetSupplyVoltage ();
115 }
116 
117 double
119 {
120  NS_LOG_FUNCTION (this);
121  // average of Voc and Vcutoff
123 }
124 
125 double
127 {
128  NS_LOG_FUNCTION (this);
131 }
132 
133 double
135 {
136  NS_LOG_FUNCTION (this);
137  return GetBatteryLevel ();
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this);
144 
145  // do not update if battery is already dead
146  if (m_batteryLevel <= 0)
147  {
148  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
149  return;
150  }
151 
152  // do not update if simulation has finished
153  if (Simulator::IsFinished ())
154  {
155  return;
156  }
157 
158  NS_LOG_DEBUG ("RvBatteryModel:Updating remaining energy!");
159 
161 
162  double currentLoad = CalculateTotalCurrent () * 1000; // must be in mA
163  double calculatedAlpha = Discharge (currentLoad, Simulator::Now ());
164 
165  NS_LOG_DEBUG ("RvBatteryModel:Calculated alpha = " << calculatedAlpha <<
166  " time = " << Simulator::Now ().As (Time::S));
167 
168  // calculate battery level
169  m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
170  if (m_batteryLevel < 0)
171  {
172  m_batteryLevel = 0;
173  }
174 
175  // check if battery level is below the low battery threshold.
177  {
179  NS_LOG_DEBUG ("RvBatteryModel:Battery level below threshold!");
181  }
182 
183  m_previousLoad = currentLoad;
187  this);
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION (this << interval);
194  m_samplingInterval = interval;
195 }
196 
197 Time
199 {
200  NS_LOG_FUNCTION (this);
201  return m_samplingInterval;
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION (this << voltage);
208  NS_ASSERT (voltage >= 0);
209  m_openCircuitVoltage = voltage;
210 }
211 
212 double
214 {
215  NS_LOG_FUNCTION (this);
216  return m_openCircuitVoltage;
217 }
218 
219 void
221 {
222  NS_LOG_FUNCTION (this << voltage);
223  NS_ASSERT (voltage <= m_openCircuitVoltage);
224  m_cutoffVoltage = voltage;
225 }
226 
227 double
229 {
230  NS_LOG_FUNCTION (this);
231  return m_cutoffVoltage;
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION (this << alpha);
238  NS_ASSERT (alpha >= 0);
239  m_alpha = alpha;
240 }
241 
242 double
244 {
245  NS_LOG_FUNCTION (this);
246  return m_alpha;
247 }
248 
249 void
251 {
252  NS_LOG_FUNCTION (this << beta);
253  NS_ASSERT (beta >= 0);
254  m_beta = beta;
255 }
256 
257 double
259 {
260  NS_LOG_FUNCTION (this);
261  return m_beta;
262 }
263 
264 double
266 {
267  NS_LOG_FUNCTION (this);
269  return m_batteryLevel;
270 }
271 
272 Time
274 {
275  NS_LOG_FUNCTION (this);
276  return m_lifetime;
277 }
278 
279 void
281 {
282  NS_LOG_FUNCTION (this << num);
283  m_numOfTerms = num;
284 }
285 
286 int
288 {
289  NS_LOG_FUNCTION (this);
290  return m_numOfTerms;
291 }
292 
293 /*
294  * Private functions start here.
295  */
296 
297 void
299 {
300  NS_LOG_FUNCTION (this);
301  NS_LOG_DEBUG ("RvBatteryModel:Starting battery level update!");
302  UpdateEnergySource (); // start periodic sampling of load (total current)
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION (this);
309  BreakDeviceEnergyModelRefCycle (); // break reference cycle
310 }
311 
312 void
314 {
315  NS_LOG_FUNCTION (this);
316  NS_LOG_DEBUG ("RvBatteryModel:Energy depleted!");
317  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
318 }
319 
320 double
322 {
323  NS_LOG_FUNCTION (this << load << t);
324 
325  // record only when load changes
326  if (load != m_previousLoad)
327  {
328  m_load.push_back (load);
329  m_previousLoad = load;
331  m_timeStamps.push_back (t);
332  }
333  else
334  {
335  if (!m_timeStamps.empty())
336  {
337  m_timeStamps[m_timeStamps.size () - 1] = t;
338  }
339  }
340 
341  m_lastSampleTime = t;
342 
343  // calculate alpha for new t
344  NS_ASSERT (m_load.size () == m_timeStamps.size () - 1); // size must be equal
345  double calculatedAlpha = 0.0;
346  if (m_timeStamps.size () == 1)
347  {
348  // constant load
349  calculatedAlpha = m_load[0] * RvModelAFunction (t, t, Seconds (0.0),
350  m_beta);
351  }
352  else
353  {
354  // changing load
355  for (uint64_t i = 1; i < m_timeStamps.size (); i++)
356  {
357  calculatedAlpha += m_load[i - 1] * RvModelAFunction (t, m_timeStamps[i],
358  m_timeStamps[i - 1],
359  m_beta);
360  }
361  }
362 
363  return calculatedAlpha;
364 }
365 
366 double
367 RvBatteryModel::RvModelAFunction (Time t, Time sk, Time sk_1, double beta)
368 {
369  NS_LOG_FUNCTION (this << t << sk << sk_1 << beta);
370 
371  // everything is in minutes
372  double firstDelta = (t - sk).GetMinutes ();
373  double secondDelta = (t - sk_1).GetMinutes ();
374  double delta = (sk - sk_1).GetMinutes ();
375 
376  double sum = 0.0;
377  for (int m = 1; m <= m_numOfTerms; m++)
378  {
379  double square = beta * beta * m * m;
380  sum += (std::exp (-square * (firstDelta)) - std::exp (-square * (secondDelta))) / square;
381  }
382  return delta + 2 * sum;
383 }
384 
385 } // namespace ns3
ns3::RvBatteryModel::SetCutoffVoltage
void SetCutoffVoltage(double voltage)
Sets cutoff voltage of battery.
Definition: rv-battery-model.cc:220
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::RvBatteryModel::Discharge
double Discharge(double load, Time t)
Discharges the battery.
Definition: rv-battery-model.cc:321
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::MakeIntegerAccessor
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: integer.h:45
ns3::RvBatteryModel::GetSupplyVoltage
virtual double GetSupplyVoltage(void) const
Definition: rv-battery-model.cc:118
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::RvBatteryModel
Rakhmatov Vrudhula non-linear battery model.
Definition: rv-battery-model.h:51
ns3::RvBatteryModel::DoInitialize
virtual void DoInitialize(void)
Defined in ns3::Object.
Definition: rv-battery-model.cc:298
ns3::RvBatteryModel::GetRemainingEnergy
virtual double GetRemainingEnergy(void)
Definition: rv-battery-model.cc:126
ns3::RvBatteryModel::GetBatteryLevel
double GetBatteryLevel(void)
Definition: rv-battery-model.cc:265
ns3::IntegerValue
Hold a signed integer type.
Definition: integer.h:44
ns3::RvBatteryModel::GetTypeId
static TypeId GetTypeId(void)
Definition: rv-battery-model.cc:36
ns3::RvBatteryModel::SetSamplingInterval
void SetSamplingInterval(Time interval)
Definition: rv-battery-model.cc:191
ns3::RvBatteryModel::SetBeta
void SetBeta(double beta)
Sets the beta value for the battery model.
Definition: rv-battery-model.cc:250
ns3::RvBatteryModel::m_previousLoad
double m_previousLoad
Definition: rv-battery-model.h:224
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::RvBatteryModel::m_cutoffVoltage
double m_cutoffVoltage
Definition: rv-battery-model.h:220
ns3::RvBatteryModel::GetEnergyFraction
virtual double GetEnergyFraction(void)
Definition: rv-battery-model.cc:134
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::RvBatteryModel::SetOpenCircuitVoltage
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
Definition: rv-battery-model.cc:205
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::RvBatteryModel::RvBatteryModel
RvBatteryModel()
Definition: rv-battery-model.cc:95
ns3::RvBatteryModel::SetNumOfTerms
void SetNumOfTerms(int num)
Sets the number of terms of the infinite sum for estimating battery level.
Definition: rv-battery-model.cc:280
ns3::RvBatteryModel::m_currentSampleEvent
EventId m_currentSampleEvent
Definition: rv-battery-model.h:255
ns3::EnergySource::NotifyEnergyDrained
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
Definition: energy-source.cc:192
ns3::RvBatteryModel::GetLifetime
Time GetLifetime(void) const
Definition: rv-battery-model.cc:273
ns3::RvBatteryModel::m_lifetime
TracedValue< Time > m_lifetime
Definition: rv-battery-model.h:257
ns3::RvBatteryModel::HandleEnergyDrainedEvent
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
Definition: rv-battery-model.cc:313
ns3::RvBatteryModel::~RvBatteryModel
virtual ~RvBatteryModel()
Definition: rv-battery-model.cc:105
ns3::EnergySource::CalculateTotalCurrent
double CalculateTotalCurrent(void)
Definition: energy-source.cc:163
sample-rng-plot.alpha
alpha
Definition: sample-rng-plot.py:37
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::EnergySource
Introspection did not find any typical Config paths.
Definition: energy-source.h:82
ns3::RvBatteryModel::m_openCircuitVoltage
double m_openCircuitVoltage
Definition: rv-battery-model.h:219
ns3::RvBatteryModel::m_load
std::vector< double > m_load
Definition: rv-battery-model.h:225
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::RvBatteryModel::m_alpha
double m_alpha
Definition: rv-battery-model.h:221
ns3::RvBatteryModel::DoDispose
virtual void DoDispose(void)
Defined in ns3::Object.
Definition: rv-battery-model.cc:306
ns3::RvBatteryModel::GetInitialEnergy
virtual double GetInitialEnergy(void) const
Definition: rv-battery-model.cc:111
ns3::RvBatteryModel::m_numOfTerms
int m_numOfTerms
Definition: rv-battery-model.h:229
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::RvBatteryModel::GetOpenCircuitVoltage
double GetOpenCircuitVoltage(void) const
Definition: rv-battery-model.cc:213
ns3::RvBatteryModel::RvModelAFunction
double RvModelAFunction(Time t, Time sk, Time sk_1, double beta)
RV model A function.
Definition: rv-battery-model.cc:367
ns3::RvBatteryModel::GetSamplingInterval
Time GetSamplingInterval(void) const
Definition: rv-battery-model.cc:198
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::Time::S
@ S
second
Definition: nstime.h:115
ns3::RvBatteryModel::m_beta
double m_beta
Definition: rv-battery-model.h:222
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::Simulator::IsFinished
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:165
ns3::RvBatteryModel::GetCutoffVoltage
double GetCutoffVoltage(void) const
Definition: rv-battery-model.cc:228
rv-battery-model.h
ns3::RvBatteryModel::GetBeta
double GetBeta(void) const
Definition: rv-battery-model.cc:258
ns3::RvBatteryModel::SetAlpha
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
Definition: rv-battery-model.cc:235
ns3::RvBatteryModel::m_batteryLevel
TracedValue< double > m_batteryLevel
Battery level is defined as: output of Discharge function / alpha value.
Definition: rv-battery-model.h:247
ns3::RvBatteryModel::UpdateEnergySource
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
Definition: rv-battery-model.cc:141
ns3::RvBatteryModel::m_lastSampleTime
Time m_lastSampleTime
Definition: rv-battery-model.h:227
ns3::RvBatteryModel::GetAlpha
double GetAlpha(void) const
Definition: rv-battery-model.cc:243
ns3::RvBatteryModel::m_samplingInterval
Time m_samplingInterval
(1 / sampling interval) = sampling frequency
Definition: rv-battery-model.h:254
ns3::RvBatteryModel::GetNumOfTerms
int GetNumOfTerms(void) const
Definition: rv-battery-model.cc:287
ns3::RvBatteryModel::m_lowBatteryTh
double m_lowBatteryTh
Definition: rv-battery-model.h:249
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::RvBatteryModel::m_timeStamps
std::vector< Time > m_timeStamps
Definition: rv-battery-model.h:226