A Discrete-Event Network Simulator
API
rv-battery-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
18  */
19 
20 #include "rv-battery-model.h"
21 
22 #include "ns3/assert.h"
23 #include "ns3/double.h"
24 #include "ns3/log.h"
25 #include "ns3/simulator.h"
26 #include "ns3/trace-source-accessor.h"
27 
28 #include <cmath>
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("RvBatteryModel");
34 
35 NS_OBJECT_ENSURE_REGISTERED(RvBatteryModel);
36 
37 TypeId
39 {
40  static TypeId tid =
41  TypeId("ns3::RvBatteryModel")
43  .SetGroupName("Energy")
44  .AddConstructor<RvBatteryModel>()
45  .AddAttribute("RvBatteryModelPeriodicEnergyUpdateInterval",
46  "RV battery model sampling interval.",
47  TimeValue(Seconds(1.0)),
51  .AddAttribute("RvBatteryModelLowBatteryThreshold",
52  "Low battery threshold.",
53  DoubleValue(0.10), // as a fraction of the initial energy
55  MakeDoubleChecker<double>())
56  .AddAttribute("RvBatteryModelOpenCircuitVoltage",
57  "RV battery model open circuit voltage.",
58  DoubleValue(4.1),
61  MakeDoubleChecker<double>())
62  .AddAttribute("RvBatteryModelCutoffVoltage",
63  "RV battery model cutoff voltage.",
64  DoubleValue(3.0),
67  MakeDoubleChecker<double>())
68  .AddAttribute("RvBatteryModelAlphaValue",
69  "RV battery model alpha value.",
70  DoubleValue(35220.0),
72  MakeDoubleChecker<double>())
73  .AddAttribute("RvBatteryModelBetaValue",
74  "RV battery model beta value.",
75  DoubleValue(0.637),
77  MakeDoubleChecker<double>())
78  .AddAttribute(
79  "RvBatteryModelNumOfTerms",
80  "The number of terms of the infinite sum for estimating battery level.",
81  IntegerValue(10), // value used in paper
83  MakeIntegerChecker<int>())
84  .AddTraceSource("RvBatteryModelBatteryLevel",
85  "RV battery model battery level.",
87  "ns3::TracedValueCallback::Double")
88  .AddTraceSource("RvBatteryModelBatteryLifetime",
89  "RV battery model battery lifetime.",
91  "ns3::TracedValueCallback::Time");
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 << " time = "
166  << 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 }
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;
330  m_timeStamps.push_back(t);
331  }
332  else
333  {
334  if (!m_timeStamps.empty())
335  {
336  m_timeStamps[m_timeStamps.size() - 1] = t;
337  }
338  }
339 
340  m_lastSampleTime = t;
341 
342  // calculate alpha for new t
343  NS_ASSERT(m_load.size() == m_timeStamps.size() - 1); // size must be equal
344  double calculatedAlpha = 0.0;
345  if (m_timeStamps.size() == 1)
346  {
347  // constant load
348  calculatedAlpha = m_load[0] * RvModelAFunction(t, t, Seconds(0.0), m_beta);
349  }
350  else
351  {
352  // changing load
353  for (uint64_t i = 1; i < m_timeStamps.size(); i++)
354  {
355  calculatedAlpha +=
356  m_load[i - 1] * RvModelAFunction(t, m_timeStamps[i], m_timeStamps[i - 1], m_beta);
357  }
358  }
359 
360  return calculatedAlpha;
361 }
362 
363 double
365 {
366  NS_LOG_FUNCTION(this << t << sk << sk_1 << beta);
367 
368  // everything is in minutes
369  double firstDelta = (t - sk).GetMinutes();
370  double secondDelta = (t - sk_1).GetMinutes();
371  double delta = (sk - sk_1).GetMinutes();
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
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
double CalculateTotalCurrent()
void BreakDeviceEnergyModelRefCycle()
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
void NotifyEnergyDrained()
This function notifies all DeviceEnergyModel of energy depletion event.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
Hold a signed integer type.
Definition: integer.h:45
Rakhmatov Vrudhula non-linear battery model.
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
void DoDispose() override
Defined in ns3::Object.
double m_openCircuitVoltage
Open circuit voltage (in Volts)
double GetBeta() const
void SetNumOfTerms(int num)
Sets the number of terms of the infinite sum for estimating battery level.
double m_cutoffVoltage
Cutoff voltage (in Volts)
EventId m_currentSampleEvent
Current sample event.
void SetSamplingInterval(Time interval)
Time m_lastSampleTime
Last sample time.
double GetCutoffVoltage() const
static TypeId GetTypeId()
Get the type ID.
double m_previousLoad
load value (total current) of previous sampling
std::vector< Time > m_timeStamps
time stamps of load profile
int m_numOfTerms
Number# of terms for infinite sum in battery level estimation.
double Discharge(double load, Time t)
Discharges the battery.
Time GetSamplingInterval() const
void SetCutoffVoltage(double voltage)
Sets cutoff voltage of battery.
void SetBeta(double beta)
Sets the beta value for the battery model.
double GetEnergyFraction() override
double GetInitialEnergy() const override
double GetOpenCircuitVoltage() const
std::vector< double > m_load
load profile
TracedValue< Time > m_lifetime
time of death of the battery
Time m_samplingInterval
Sampling interval.
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
double RvModelAFunction(Time t, Time sk, Time sk_1, double beta)
RV model A function.
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
double GetAlpha() const
void DoInitialize() override
Defined in ns3::Object.
double m_alpha
alpha value of RV model, in Coulomb
double GetSupplyVoltage() const override
TracedValue< double > m_batteryLevel
Battery level is defined as: output of Discharge function / alpha value.
void UpdateEnergySource() override
Implements UpdateEnergySource.
double GetRemainingEnergy() override
double m_beta
beta value of RV model, in second^-1
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:171
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition: integer.h:46
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43