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  .AddConstructor<RvBatteryModel> ()
41  .AddAttribute ("RvBatteryModelPeriodicEnergyUpdateInterval",
42  "RV battery model sampling interval.",
43  TimeValue (Seconds (1.0)),
46  MakeTimeChecker ())
47  .AddAttribute ("RvBatteryModelLowBatteryThreshold",
48  "Low battery threshold.",
49  DoubleValue (0.10), // as a fraction of the initial energy
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("RvBatteryModelOpenCircuitVoltage",
53  "RV battery model open circuit voltage.",
54  DoubleValue (4.1),
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("RvBatteryModelCutoffVoltage",
59  "RV battery model cutoff voltage.",
60  DoubleValue (3.0),
63  MakeDoubleChecker<double> ())
64  .AddAttribute ("RvBatteryModelAlphaValue",
65  "RV battery model alpha value.",
66  DoubleValue (35220.0),
69  MakeDoubleChecker<double> ())
70  .AddAttribute ("RvBatteryModelBetaValue",
71  "RV battery model beta value.",
72  DoubleValue (0.637),
75  MakeDoubleChecker<double> ())
76  .AddAttribute ("RvBatteryModelNumOfTerms",
77  "The number of terms of the infinite sum for estimating battery level.",
78  IntegerValue (10), // value used in paper
81  MakeIntegerChecker<int> ())
82  .AddTraceSource ("RvBatteryModelBatteryLevel",
83  "RV battery model battery level.",
85  "ns3::TracedValue::DoubleCallback")
86  .AddTraceSource ("RvBatteryModelBatteryLifetime",
87  "RV battery model battery lifetime.",
89  "ns3::Time::TracedValueCallback")
90  ;
91  return tid;
92 }
93 
95 {
96  NS_LOG_FUNCTION (this);
97  m_lastSampleTime = Seconds (0.0);
98  m_previousLoad = 0.0;
99  m_batteryLevel = 1; // fully charged
100  m_lifetime = Seconds (0.0);
101 }
102 
104 {
105  NS_LOG_FUNCTION (this);
106 }
107 
108 double
110 {
111  NS_LOG_FUNCTION (this);
112  return m_alpha * GetSupplyVoltage ();
113 }
114 
115 double
117 {
118  NS_LOG_FUNCTION (this);
119  // average of Voc and Vcutoff
121 }
122 
123 double
125 {
126  NS_LOG_FUNCTION (this);
129 }
130 
131 double
133 {
134  NS_LOG_FUNCTION (this);
135  return GetBatteryLevel ();
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this);
142 
143  // do not update if battery is already dead
144  if (m_batteryLevel <= 0)
145  {
146  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
147  return;
148  }
149 
150  // do not update if simulation has finished
151  if (Simulator::IsFinished ())
152  {
153  return;
154  }
155 
156  NS_LOG_DEBUG ("RvBatteryModel:Updating remaining energy!");
157 
159 
160  double currentLoad = CalculateTotalCurrent () * 1000; // must be in mA
161  double calculatedAlpha = Discharge (currentLoad, Simulator::Now ());
162 
163  NS_LOG_DEBUG ("RvBatteryModel:Calculated alpha = " << calculatedAlpha <<
164  " time = " << Simulator::Now ().GetSeconds ());
165 
166  // calculate battery level
167  m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
168  if (m_batteryLevel < 0)
169  {
170  m_batteryLevel = 0;
171  }
172 
173  // check if battery is dead.
175  {
177  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
179  return; // stop periodic sampling
180  }
181 
182  m_previousLoad = currentLoad;
186  this);
187 }
188 
189 void
191 {
192  NS_LOG_FUNCTION (this << interval);
193  m_samplingInterval = interval;
194 }
195 
196 Time
198 {
199  NS_LOG_FUNCTION (this);
200  return m_samplingInterval;
201 }
202 
203 void
205 {
206  NS_LOG_FUNCTION (this << voltage);
207  NS_ASSERT (voltage >= 0);
208  m_openCircuitVoltage = voltage;
209 }
210 
211 double
213 {
214  NS_LOG_FUNCTION (this);
215  return m_openCircuitVoltage;
216 }
217 
218 void
220 {
221  NS_LOG_FUNCTION (this << voltage);
222  NS_ASSERT (voltage <= m_openCircuitVoltage);
223  m_cutoffVoltage = voltage;
224 }
225 
226 double
228 {
229  NS_LOG_FUNCTION (this);
230  return m_cutoffVoltage;
231 }
232 
233 void
235 {
236  NS_LOG_FUNCTION (this << alpha);
237  NS_ASSERT (alpha >= 0);
238  m_alpha = alpha;
239 }
240 
241 double
243 {
244  NS_LOG_FUNCTION (this);
245  return m_alpha;
246 }
247 
248 void
250 {
251  NS_LOG_FUNCTION (this << beta);
252  NS_ASSERT (beta >= 0);
253  m_beta = beta;
254 }
255 
256 double
258 {
259  NS_LOG_FUNCTION (this);
260  return m_beta;
261 }
262 
263 double
265 {
266  NS_LOG_FUNCTION (this);
268  return m_batteryLevel;
269 }
270 
271 Time
273 {
274  NS_LOG_FUNCTION (this);
275  return m_lifetime;
276 }
277 
278 void
280 {
281  NS_LOG_FUNCTION (this << num);
282  m_numOfTerms = num;
283 }
284 
285 int
287 {
288  NS_LOG_FUNCTION (this);
289  return m_numOfTerms;
290 }
291 
292 /*
293  * Private functions start here.
294  */
295 
296 void
298 {
299  NS_LOG_FUNCTION (this);
300  NS_LOG_DEBUG ("RvBatteryModel:Starting battery level update!");
301  UpdateEnergySource (); // start periodic sampling of load (total current)
302 }
303 
304 void
306 {
307  NS_LOG_FUNCTION (this);
308  BreakDeviceEnergyModelRefCycle (); // break reference cycle
309 }
310 
311 void
313 {
314  NS_LOG_FUNCTION (this);
315  NS_LOG_DEBUG ("RvBatteryModel:Energy depleted!");
316  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
317 }
318 
319 double
321 {
322  NS_LOG_FUNCTION (this << load << t);
323 
324  // record only when load changes
325  if (load != m_previousLoad)
326  {
327  m_load.push_back (load);
328  m_previousLoad = load;
329  if (t != Seconds (0.0))
330  {
332  }
333  else
334  {
335  m_timeStamps.push_back (Seconds (0.0));
336  }
337  m_timeStamps.push_back (t);
338  }
339  else
340  {
341  m_timeStamps[m_timeStamps.size () - 1] = t;
342  }
343 
344  m_lastSampleTime = t;
345 
346  // calculate alpha for new t
347  NS_ASSERT (m_load.size () == m_timeStamps.size () - 1); // size must be equal
348  double calculatedAlpha = 0.0;
349  if (m_timeStamps.size () == 1)
350  {
351  // constant load
352  calculatedAlpha = m_load[0] * RvModelAFunction (t, t, Seconds (0.0),
353  m_beta);
354  }
355  else
356  {
357  // changing load
358  for (uint64_t i = 1; i < m_timeStamps.size (); i++)
359  {
360  calculatedAlpha += m_load[i - 1] * RvModelAFunction (t, m_timeStamps[i],
361  m_timeStamps[i - 1],
362  m_beta);
363  }
364  }
365 
366  return calculatedAlpha;
367 }
368 
369 double
370 RvBatteryModel::RvModelAFunction (Time t, Time sk, Time sk_1, double beta)
371 {
372  NS_LOG_FUNCTION (this << t << sk << sk_1 << beta);
373 
374  // everything is in minutes
375  double firstDelta = (t.GetSeconds () - sk.GetSeconds ()) / 60;
376  double secondDelta = (t.GetSeconds () - sk_1.GetSeconds ()) / 60;
377  double delta = (sk.GetSeconds () - sk_1.GetSeconds ()) / 60;
378 
379  double sum = 0.0;
380  for (int m = 1; m <= m_numOfTerms; m++)
381  {
382  double square = beta * beta * m * m;
383  sum += (std::exp (-square * (firstDelta)) - std::exp (-square * (secondDelta))) / square;
384  }
385  return delta + 2 * sum;
386 }
387 
388 } // namespace ns3
double Discharge(double load, Time t)
Discharges the battery.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Time m_samplingInterval
(1 / sampling interval) = sampling frequency
Time GetSamplingInterval(void) const
virtual double GetSupplyVoltage(void) const
Introspection did not find any typical Config paths.
Definition: energy-source.h:81
double GetBeta(void) const
#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
Hold a signed integer type.
Definition: integer.h:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void SetBeta(double beta)
Sets the beta value for the battery model.
virtual double GetInitialEnergy(void) const
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:819
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
void SetNumOfTerms(int num)
Sets the number of terms of the infinite sum for estimating battery level.
void SetCutoffVoltage(double voltage)
Sets cutoff voltage of battery.
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:439
double GetAlpha(void) const
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
virtual void DoInitialize(void)
Defined in ns3::Object.
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:327
Time GetLifetime(void) const
AttributeValue implementation for Time.
Definition: nstime.h:921
void SetSamplingInterval(Time interval)
std::vector< Time > m_timeStamps
virtual void DoDispose(void)
Defined in ns3::Object.
double CalculateTotalCurrent(void)
double RvModelAFunction(Time t, Time sk, Time sk_1, double beta)
RV model A function.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:922
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
TracedValue< Time > m_lifetime
static TypeId GetTypeId(void)
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
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
TracedValue< double > m_batteryLevel
Battery level is defined as: output of Discharge function / alpha value.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
virtual double GetRemainingEnergy(void)
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
double GetOpenCircuitVoltage(void) const
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:193
int GetNumOfTerms(void) const
std::vector< double > m_load
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:51
double GetCutoffVoltage(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
virtual double GetEnergyFraction(void)
double GetBatteryLevel(void)