A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("RvBatteryModel");
30 
31 namespace ns3 {
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)),
44  MakeTimeAccessor (&RvBatteryModel::SetSamplingInterval,
46  MakeTimeChecker ())
47  .AddAttribute ("RvBatteryModelLowBatteryThreshold",
48  "Low battery threshold.",
49  DoubleValue (0.10), // as a fraction of the initial energy
50  MakeDoubleAccessor (&RvBatteryModel::m_lowBatteryTh),
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("RvBatteryModelOpenCircuitVoltage",
53  "RV battery model open circuit voltage.",
54  DoubleValue (4.1),
55  MakeDoubleAccessor (&RvBatteryModel::SetOpenCircuitVoltage,
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("RvBatteryModelCutoffVoltage",
59  "RV battery model cutoff voltage.",
60  DoubleValue (3.0),
61  MakeDoubleAccessor (&RvBatteryModel::SetCutoffVoltage,
63  MakeDoubleChecker<double> ())
64  .AddAttribute ("RvBatteryModelAlphaValue",
65  "RV battery model alpha value.",
66  DoubleValue (35220.0),
67  MakeDoubleAccessor (&RvBatteryModel::SetAlpha,
69  MakeDoubleChecker<double> ())
70  .AddAttribute ("RvBatteryModelBetaValue",
71  "RV battery model beta value.",
72  DoubleValue (0.637),
73  MakeDoubleAccessor (&RvBatteryModel::SetBeta,
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
79  MakeIntegerAccessor (&RvBatteryModel::SetNumOfTerms,
81  MakeIntegerChecker<int> ())
82  .AddTraceSource ("RvBatteryModelBatteryLevel",
83  "RV battery model battery level.",
85  .AddTraceSource ("RvBatteryModelBatteryLifetime",
86  "RV battery model battery lifetime.",
88  ;
89  return tid;
90 }
91 
93 {
94  NS_LOG_FUNCTION (this);
95  m_lastSampleTime = Seconds (0.0);
96  m_previousLoad = 0.0;
97  m_batteryLevel = 1; // fully charged
98  m_lifetime = Seconds (0.0);
99 }
100 
102 {
103  NS_LOG_FUNCTION (this);
104 }
105 
106 double
108 {
109  NS_LOG_FUNCTION (this);
110  return m_alpha * GetSupplyVoltage ();
111 }
112 
113 double
115 {
116  NS_LOG_FUNCTION (this);
117  // average of Voc and Vcutoff
119 }
120 
121 double
123 {
124  NS_LOG_FUNCTION (this);
127 }
128 
129 double
131 {
132  NS_LOG_FUNCTION (this);
133  return GetBatteryLevel ();
134 }
135 
136 void
138 {
139  NS_LOG_FUNCTION (this);
140 
141  // do not update if battery is already dead
142  if (m_batteryLevel <= 0)
143  {
144  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
145  return;
146  }
147 
148  // do not update if simulation has finished
149  if (Simulator::IsFinished ())
150  {
151  return;
152  }
153 
154  NS_LOG_DEBUG ("RvBatteryModel:Updating remaining energy!");
155 
157 
158  double currentLoad = CalculateTotalCurrent () * 1000; // must be in mA
159  double calculatedAlpha = Discharge (currentLoad, Simulator::Now ());
160 
161  NS_LOG_DEBUG ("RvBatteryModel:Calculated alpha = " << calculatedAlpha <<
162  " time = " << Simulator::Now ().GetSeconds ());
163 
164  // calculate battery level
165  m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
166  if (m_batteryLevel < 0)
167  {
168  m_batteryLevel = 0;
169  }
170 
171  // check if battery is dead.
173  {
175  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
177  return; // stop periodic sampling
178  }
179 
180  m_previousLoad = currentLoad;
184  this);
185 }
186 
187 void
189 {
190  NS_LOG_FUNCTION (this << interval);
191  m_samplingInterval = interval;
192 }
193 
194 Time
196 {
197  NS_LOG_FUNCTION (this);
198  return m_samplingInterval;
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION (this << voltage);
205  NS_ASSERT (voltage >= 0);
206  m_openCircuitVoltage = voltage;
207 }
208 
209 double
211 {
212  NS_LOG_FUNCTION (this);
213  return m_openCircuitVoltage;
214 }
215 
216 void
218 {
219  NS_LOG_FUNCTION (this << voltage);
220  NS_ASSERT (voltage <= m_openCircuitVoltage);
221  m_cutoffVoltage = voltage;
222 }
223 
224 double
226 {
227  NS_LOG_FUNCTION (this);
228  return m_cutoffVoltage;
229 }
230 
231 void
233 {
234  NS_LOG_FUNCTION (this << alpha);
235  NS_ASSERT (alpha >= 0);
236  m_alpha = alpha;
237 }
238 
239 double
241 {
242  NS_LOG_FUNCTION (this);
243  return m_alpha;
244 }
245 
246 void
248 {
249  NS_LOG_FUNCTION (this << beta);
250  NS_ASSERT (beta >= 0);
251  m_beta = beta;
252 }
253 
254 double
256 {
257  NS_LOG_FUNCTION (this);
258  return m_beta;
259 }
260 
261 double
263 {
264  NS_LOG_FUNCTION (this);
266  return m_batteryLevel;
267 }
268 
269 Time
271 {
272  NS_LOG_FUNCTION (this);
273  return m_lifetime;
274 }
275 
276 void
278 {
279  NS_LOG_FUNCTION (this << num);
280  m_numOfTerms = num;
281 }
282 
283 int
285 {
286  NS_LOG_FUNCTION (this);
287  return m_numOfTerms;
288 }
289 
290 /*
291  * Private functions start here.
292  */
293 
294 void
296 {
297  NS_LOG_FUNCTION (this);
298  NS_LOG_DEBUG ("RvBatteryModel:Starting battery level update!");
299  UpdateEnergySource (); // start periodic sampling of load (total current)
300 }
301 
302 void
304 {
305  NS_LOG_FUNCTION (this);
306  BreakDeviceEnergyModelRefCycle (); // break reference cycle
307 }
308 
309 void
311 {
312  NS_LOG_FUNCTION (this);
313  NS_LOG_DEBUG ("RvBatteryModel:Energy depleted!");
314  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
315 }
316 
317 double
319 {
320  NS_LOG_FUNCTION (this << load << t);
321 
322  // record only when load changes
323  if (load != m_previousLoad)
324  {
325  m_load.push_back (load);
326  m_previousLoad = load;
327  if (t != Seconds (0.0))
328  {
330  }
331  else
332  {
333  m_timeStamps.push_back (Seconds (0.0));
334  }
335  m_timeStamps.push_back (t);
336  }
337  else
338  {
339  m_timeStamps[m_timeStamps.size () - 1] = t;
340  }
341 
342  m_lastSampleTime = t;
343 
344  // calculate alpha for new t
345  NS_ASSERT (m_load.size () == m_timeStamps.size () - 1); // size must be equal
346  double calculatedAlpha = 0.0;
347  if (m_timeStamps.size () == 1)
348  {
349  // constant load
350  calculatedAlpha = m_load[0] * RvModelAFunction (t, t, Seconds (0.0),
351  m_beta);
352  }
353  else
354  {
355  // changing load
356  for (uint64_t i = 1; i < m_timeStamps.size (); i++)
357  {
358  calculatedAlpha += m_load[i - 1] * RvModelAFunction (t, m_timeStamps[i],
359  m_timeStamps[i - 1],
360  m_beta);
361  }
362  }
363 
364  return calculatedAlpha;
365 }
366 
367 double
368 RvBatteryModel::RvModelAFunction (Time t, Time sk, Time sk_1, double beta)
369 {
370  NS_LOG_FUNCTION (this << t << sk << sk_1 << beta);
371 
372  // everything is in minutes
373  double firstDelta = (t.GetSeconds () - sk.GetSeconds ()) / 60;
374  double secondDelta = (t.GetSeconds () - sk_1.GetSeconds ()) / 60;
375  double delta = (sk.GetSeconds () - sk_1.GetSeconds ()) / 60;
376 
377  double sum = 0.0;
378  for (int m = 1; m <= m_numOfTerms; m++)
379  {
380  double square = beta * beta * m * m;
381  sum += (std::exp (-square * (firstDelta)) - std::exp (-square * (secondDelta))) / square;
382  }
383  return delta + 2 * sum;
384 }
385 
386 } // 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 the class in the ns-3 factory.
Definition: object-base.h:38
Time m_samplingInterval
(1 / sampling interval) = sampling frequency
Time GetSamplingInterval(void) const
virtual double GetSupplyVoltage(void) const
Doxygen 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:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
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:825
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 AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:444
double GetAlpha(void) const
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:322
Time GetLifetime(void) const
Attribute for objects of type ns3::Time.
Definition: nstime.h:912
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.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
TracedValue< Time > m_lifetime
static TypeId GetTypeId(void)
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:213
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
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:47
double GetOpenCircuitVoltage(void) const
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
int GetNumOfTerms(void) const
std::vector< double > m_load
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
double GetCutoffVoltage(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
virtual double GetEnergyFraction(void)
double GetBatteryLevel(void)