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 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::RvBatteryModel")
41  .AddConstructor<RvBatteryModel> ()
42  .AddAttribute ("RvBatteryModelPeriodicEnergyUpdateInterval",
43  "RV battery model sampling interval.",
44  TimeValue (Seconds (1.0)),
45  MakeTimeAccessor (&RvBatteryModel::SetSamplingInterval,
47  MakeTimeChecker ())
48  .AddAttribute ("RvBatteryModelOpenCircuitVoltage",
49  "RV battery model open circuit voltage.",
50  DoubleValue (4.1),
51  MakeDoubleAccessor (&RvBatteryModel::SetOpenCircuitVoltage,
53  MakeDoubleChecker<double> ())
54  .AddAttribute ("RvBatteryModelCutoffVoltage",
55  "RV battery model cutoff voltage.",
56  DoubleValue (3.0),
57  MakeDoubleAccessor (&RvBatteryModel::SetCutoffVoltage,
59  MakeDoubleChecker<double> ())
60  .AddAttribute ("RvBatteryModelAlphaValue",
61  "RV battery model alpha value.",
62  DoubleValue (35220.0),
63  MakeDoubleAccessor (&RvBatteryModel::SetAlpha,
65  MakeDoubleChecker<double> ())
66  .AddAttribute ("RvBatteryModelBetaValue",
67  "RV battery model beta value.",
68  DoubleValue (0.637),
69  MakeDoubleAccessor (&RvBatteryModel::SetBeta,
71  MakeDoubleChecker<double> ())
72  .AddAttribute ("RvBatteryModelNumOfTerms",
73  "The number of terms of the infinite sum for estimating battery level.",
74  IntegerValue (10), // value used in paper
75  MakeIntegerAccessor (&RvBatteryModel::SetNumOfTerms,
77  MakeIntegerChecker<int> ())
78  .AddTraceSource ("RvBatteryModelBatteryLevel",
79  "RV battery model battery level.",
81  .AddTraceSource ("RvBatteryModelBatteryLifetime",
82  "RV battery model battery lifetime.",
84  ;
85  return tid;
86 }
87 
89 {
90  NS_LOG_FUNCTION (this);
91  m_lastSampleTime = Seconds (0.0);
92  m_previousLoad = 0.0;
93  m_batteryLevel = 1; // fully charged
94  m_lifetime = Seconds (0.0);
95 }
96 
98 {
99  NS_LOG_FUNCTION (this);
100 }
101 
102 double
104 {
105  NS_LOG_FUNCTION (this);
106  return m_alpha * GetSupplyVoltage ();
107 }
108 
109 double
111 {
112  NS_LOG_FUNCTION (this);
113  // average of Voc and Vcutoff
115 }
116 
117 double
119 {
120  NS_LOG_FUNCTION (this);
123 }
124 
125 double
127 {
128  NS_LOG_FUNCTION (this);
129  return GetBatteryLevel ();
130 }
131 
132 void
134 {
135  NS_LOG_FUNCTION (this);
136 
137  // do not update if battery is already dead
138  if (m_batteryLevel <= 0)
139  {
140  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
141  return;
142  }
143 
144  // do not update if simulation has finished
145  if (Simulator::IsFinished ())
146  {
147  return;
148  }
149 
150  NS_LOG_DEBUG ("RvBatteryModel:Updating remaining energy!");
151 
153 
154  double currentLoad = CalculateTotalCurrent () * 1000; // must be in mA
155  double calculatedAlpha = Discharge (currentLoad, Simulator::Now ());
156 
157  NS_LOG_DEBUG ("RvBatteryModel:Calculated alpha = " << calculatedAlpha <<
158  " time = " << Simulator::Now ().GetSeconds ());
159 
160  // calculate battery level
161  m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
162  if (m_batteryLevel < 0)
163  {
164  m_batteryLevel = 0;
165  }
166 
167  // check if battery is dead.
168  if (calculatedAlpha >= m_alpha)
169  {
171  NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!");
173  return; // stop periodic sampling
174  }
175 
176  m_previousLoad = currentLoad;
180  this);
181 }
182 
183 void
185 {
186  NS_LOG_FUNCTION (this << interval);
187  m_samplingInterval = interval;
188 }
189 
190 Time
192 {
193  NS_LOG_FUNCTION (this);
194  return m_samplingInterval;
195 }
196 
197 void
199 {
200  NS_LOG_FUNCTION (this << voltage);
201  NS_ASSERT (voltage >= 0);
202  m_openCircuitVoltage = voltage;
203 }
204 
205 double
207 {
208  NS_LOG_FUNCTION (this);
209  return m_openCircuitVoltage;
210 }
211 
212 void
214 {
215  NS_LOG_FUNCTION (this << voltage);
216  NS_ASSERT (voltage <= m_openCircuitVoltage);
217  m_cutoffVoltage = voltage;
218 }
219 
220 double
222 {
223  NS_LOG_FUNCTION (this);
224  return m_cutoffVoltage;
225 }
226 
227 void
229 {
230  NS_LOG_FUNCTION (this << alpha);
231  NS_ASSERT (alpha >= 0);
232  m_alpha = alpha;
233 }
234 
235 double
237 {
238  NS_LOG_FUNCTION (this);
239  return m_alpha;
240 }
241 
242 void
244 {
245  NS_LOG_FUNCTION (this << beta);
246  NS_ASSERT (beta >= 0);
247  m_beta = beta;
248 }
249 
250 double
252 {
253  NS_LOG_FUNCTION (this);
254  return m_beta;
255 }
256 
257 double
259 {
260  NS_LOG_FUNCTION (this);
262  return m_batteryLevel;
263 }
264 
265 Time
267 {
268  NS_LOG_FUNCTION (this);
269  return m_lifetime;
270 }
271 
272 void
274 {
275  NS_LOG_FUNCTION (this << num);
276  m_numOfTerms = num;
277 }
278 
279 int
281 {
282  NS_LOG_FUNCTION (this);
283  return m_numOfTerms;
284 }
285 
286 /*
287  * Private functions start here.
288  */
289 
290 void
292 {
293  NS_LOG_FUNCTION (this);
294  NS_LOG_DEBUG ("RvBatteryModel:Starting battery level update!");
295  UpdateEnergySource (); // start periodic sampling of load (total current)
296 }
297 
298 void
300 {
301  NS_LOG_FUNCTION (this);
302  BreakDeviceEnergyModelRefCycle (); // break reference cycle
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION (this);
309  NS_LOG_DEBUG ("RvBatteryModel:Energy depleted!");
310  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
311 }
312 
313 double
315 {
316  NS_LOG_FUNCTION (this << load << t);
317 
318  // record only when load changes
319  if (load != m_previousLoad)
320  {
321  m_load.push_back (load);
322  m_previousLoad = load;
323  if (t != Seconds (0.0))
324  {
326  }
327  else
328  {
329  m_timeStamps.push_back (Seconds (0.0));
330  }
331  m_timeStamps.push_back (t);
332  }
333  else
334  {
335  m_timeStamps[m_timeStamps.size () - 1] = t;
336  }
337 
338  m_lastSampleTime = t;
339 
340  // calculate alpha for new t
341  NS_ASSERT (m_load.size () == m_timeStamps.size () - 1); // size must be equal
342  double calculatedAlpha = 0.0;
343  if (m_timeStamps.size () == 1)
344  {
345  // constant load
346  calculatedAlpha = m_load[0] * RvModelAFunction (t, t, Seconds (0.0),
347  m_beta);
348  }
349  else
350  {
351  // changing load
352  for (uint64_t i = 1; i < m_timeStamps.size (); i++)
353  {
354  calculatedAlpha += m_load[i - 1] * RvModelAFunction (t, m_timeStamps[i],
355  m_timeStamps[i - 1],
356  m_beta);
357  }
358  }
359 
360  return calculatedAlpha;
361 }
362 
363 double
364 RvBatteryModel::RvModelAFunction (Time t, Time sk, Time sk_1, double beta)
365 {
366  NS_LOG_FUNCTION (this << t << sk << sk_1 << beta);
367 
368  // everything is in minutes
369  double firstDelta = (t.GetSeconds () - sk.GetSeconds ()) / 60;
370  double secondDelta = (t.GetSeconds () - sk_1.GetSeconds ()) / 60;
371  double delta = (sk.GetSeconds () - sk_1.GetSeconds ()) / 60;
372 
373  double sum = 0.0;
374  for (int m = 1; m <= m_numOfTerms; m++)
375  {
376  double square = beta * beta * m * m;
377  sum += (std::exp (-square * (firstDelta)) - std::exp (-square * (secondDelta))) / square;
378  }
379  return delta + 2 * sum;
380 }
381 
382 } // namespace ns3
double Discharge(double load, Time t)
Discharges the battery.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Time m_samplingInterval
(1 / sampling interval) = sampling frequency
Time GetSamplingInterval(void) const
virtual double GetSupplyVoltage(void) const
Energy source base class.
Definition: energy-source.h:71
double GetBeta(void) const
#define NS_ASSERT(condition)
Definition: assert.h:64
Hold a signed integer type.
Definition: integer.h:45
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:824
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.
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
Definition: nstime.h:274
Time GetLifetime(void) const
hold objects of type ns3::Time
Definition: nstime.h:961
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)
Definition: log.h:289
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
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
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:611
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
NS_LOG_COMPONENT_DEFINE("RvBatteryModel")
virtual double GetEnergyFraction(void)
double GetBatteryLevel(void)