A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
7 */
8
9#include "rv-battery-model.h"
10
11#include "ns3/assert.h"
12#include "ns3/double.h"
13#include "ns3/log.h"
14#include "ns3/simulator.h"
15#include "ns3/trace-source-accessor.h"
16
17#include <cmath>
18
19namespace ns3
20{
21namespace energy
22{
23
24NS_LOG_COMPONENT_DEFINE("RvBatteryModel");
25NS_OBJECT_ENSURE_REGISTERED(RvBatteryModel);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::energy::RvBatteryModel")
32 .AddDeprecatedName("ns3::RvBatteryModel")
34 .SetGroupName("Energy")
35 .AddConstructor<RvBatteryModel>()
36 .AddAttribute("RvBatteryModelPeriodicEnergyUpdateInterval",
37 "RV battery model sampling interval.",
38 TimeValue(Seconds(1.0)),
42 .AddAttribute("RvBatteryModelLowBatteryThreshold",
43 "Low battery threshold.",
44 DoubleValue(0.10), // as a fraction of the initial energy
47 .AddAttribute("RvBatteryModelOpenCircuitVoltage",
48 "RV battery model open circuit voltage.",
49 DoubleValue(4.1),
53 .AddAttribute("RvBatteryModelCutoffVoltage",
54 "RV battery model cutoff voltage.",
55 DoubleValue(3.0),
59 .AddAttribute("RvBatteryModelAlphaValue",
60 "RV battery model alpha value.",
61 DoubleValue(35220.0),
64 .AddAttribute("RvBatteryModelBetaValue",
65 "RV battery model beta value.",
66 DoubleValue(0.637),
69 .AddAttribute(
70 "RvBatteryModelNumOfTerms",
71 "The number of terms of the infinite sum for estimating battery level.",
72 IntegerValue(10), // value used in paper
75 .AddTraceSource("RvBatteryModelBatteryLevel",
76 "RV battery model battery level.",
78 "ns3::TracedValueCallback::Double")
79 .AddTraceSource("RvBatteryModelBatteryLifetime",
80 "RV battery model battery lifetime.",
82 "ns3::TracedValueCallback::Time");
83 return tid;
84}
85
95
100
101double
107
108double
110{
111 NS_LOG_FUNCTION(this);
112 // average of Voc and Vcutoff
114}
115
116double
123
124double
130
131void
133{
134 NS_LOG_FUNCTION(this);
135
136 // do not update if battery is already dead
137 if (m_batteryLevel <= 0)
138 {
139 NS_LOG_DEBUG("RvBatteryModel:Battery is dead!");
140 return;
141 }
142
143 // do not update if simulation has finished
145 {
146 return;
147 }
148
149 NS_LOG_DEBUG("RvBatteryModel:Updating remaining energy!");
150
152
153 double currentLoad = CalculateTotalCurrent() * 1000; // must be in mA
154 double calculatedAlpha = Discharge(currentLoad, Simulator::Now());
155
156 NS_LOG_DEBUG("RvBatteryModel:Calculated alpha = " << calculatedAlpha << " time = "
157 << Simulator::Now().As(Time::S));
158
159 // calculate battery level
160 m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
161 if (m_batteryLevel < 0)
162 {
163 m_batteryLevel = 0;
164 }
165
166 // check if battery level is below the low battery threshold.
168 {
170 NS_LOG_DEBUG("RvBatteryModel:Battery level below threshold!");
172 }
173
174 m_previousLoad = currentLoad;
178}
179
180void
182{
183 NS_LOG_FUNCTION(this << interval);
184 m_samplingInterval = interval;
185}
186
187Time
193
194void
196{
197 NS_LOG_FUNCTION(this << voltage);
198 NS_ASSERT(voltage >= 0);
199 m_openCircuitVoltage = voltage;
200}
201
202double
208
209void
211{
212 NS_LOG_FUNCTION(this << voltage);
214 m_cutoffVoltage = voltage;
215}
216
217double
219{
220 NS_LOG_FUNCTION(this);
221 return m_cutoffVoltage;
222}
223
224void
226{
227 NS_LOG_FUNCTION(this << alpha);
228 NS_ASSERT(alpha >= 0);
229 m_alpha = alpha;
230}
231
232double
234{
235 NS_LOG_FUNCTION(this);
236 return m_alpha;
237}
238
239void
241{
242 NS_LOG_FUNCTION(this << beta);
243 NS_ASSERT(beta >= 0);
244 m_beta = beta;
245}
246
247double
249{
250 NS_LOG_FUNCTION(this);
251 return m_beta;
252}
253
254double
261
262Time
264{
265 NS_LOG_FUNCTION(this);
266 return m_lifetime;
267}
268
269void
271{
272 NS_LOG_FUNCTION(this << num);
273 m_numOfTerms = num;
274}
275
276int
278{
279 NS_LOG_FUNCTION(this);
280 return m_numOfTerms;
281}
282
283/*
284 * Private functions start here.
285 */
286
287void
289{
290 NS_LOG_FUNCTION(this);
291 NS_LOG_DEBUG("RvBatteryModel:Starting battery level update!");
292 UpdateEnergySource(); // start periodic sampling of load (total current)
293}
294
295void
297{
298 NS_LOG_FUNCTION(this);
299 BreakDeviceEnergyModelRefCycle(); // break reference cycle
300}
301
302void
304{
305 NS_LOG_FUNCTION(this);
306 NS_LOG_DEBUG("RvBatteryModel:Energy depleted!");
307 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
308}
309
310double
312{
313 NS_LOG_FUNCTION(this << load << t);
314
315 // record only when load changes
316 if (load != m_previousLoad)
317 {
318 m_load.push_back(load);
319 m_previousLoad = load;
321 m_timeStamps.push_back(t);
322 }
323 else
324 {
325 if (!m_timeStamps.empty())
326 {
327 m_timeStamps[m_timeStamps.size() - 1] = t;
328 }
329 }
330
332
333 // calculate alpha for new t
334 NS_ASSERT(m_load.size() == m_timeStamps.size() - 1); // size must be equal
335 double calculatedAlpha = 0.0;
336 if (m_timeStamps.size() == 1)
337 {
338 // constant load
339 calculatedAlpha = m_load[0] * RvModelAFunction(t, t, Seconds(0.0), m_beta);
340 }
341 else
342 {
343 // changing load
344 for (uint64_t i = 1; i < m_timeStamps.size(); i++)
345 {
346 calculatedAlpha +=
347 m_load[i - 1] * RvModelAFunction(t, m_timeStamps[i], m_timeStamps[i - 1], m_beta);
348 }
349 }
350
351 return calculatedAlpha;
352}
353
354double
356{
357 NS_LOG_FUNCTION(this << t << sk << sk_1 << beta);
358
359 // everything is in minutes
360 double firstDelta = (t - sk).GetMinutes();
361 double secondDelta = (t - sk_1).GetMinutes();
362 double delta = (sk - sk_1).GetMinutes();
363
364 double sum = 0.0;
365 for (int m = 1; m <= m_numOfTerms; m++)
366 {
367 double square = beta * beta * m * m;
368 sum += (std::exp(-square * (firstDelta)) - std::exp(-square * (secondDelta))) / square;
369 }
370 return delta + 2 * sum;
371}
372
373} // namespace energy
374} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
Hold a signed integer type.
Definition integer.h:34
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:160
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
@ S
second
Definition nstime.h:105
a unique identifier for an interface.
Definition type-id.h:48
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Energy source base class.
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.
Rakhmatov Vrudhula non-linear battery model.
double m_beta
beta value of RV model, in second^-1
double RvModelAFunction(Time t, Time sk, Time sk_1, double beta)
RV model A function.
static TypeId GetTypeId()
Get the type ID.
TracedValue< Time > m_lifetime
time of death of the battery
TracedValue< double > m_batteryLevel
Battery level is defined as: output of Discharge function / alpha value.
Time m_lastSampleTime
Last sample time.
double GetRemainingEnergy() override
EventId m_currentSampleEvent
Current sample event.
void DoDispose() override
Defined in ns3::Object.
double GetInitialEnergy() const override
double GetSupplyVoltage() const override
std::vector< double > m_load
load profile
void UpdateEnergySource() override
Implements UpdateEnergySource.
double Discharge(double load, Time t)
Discharges the battery.
void SetNumOfTerms(int num)
Sets the number of terms of the infinite sum for estimating battery level.
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
double m_alpha
alpha value of RV model, in Coulomb
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
int m_numOfTerms
Number# of terms for infinite sum in battery level estimation.
void SetBeta(double beta)
Sets the beta value for the battery model.
double m_openCircuitVoltage
Open circuit voltage (in Volts)
std::vector< Time > m_timeStamps
time stamps of load profile
void DoInitialize() override
Defined in ns3::Object.
void SetCutoffVoltage(double voltage)
Sets cutoff voltage of battery.
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
double m_previousLoad
load value (total current) of previous sampling
double GetEnergyFraction() override
void SetSamplingInterval(Time interval)
double m_cutoffVoltage
Cutoff voltage (in Volts)
Time m_samplingInterval
Sampling interval.
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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 AttributeChecker > MakeIntegerChecker()
Definition integer.h:99
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition integer.h:35
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416