A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
li-ion-energy-source.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Andrea Sacco
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
7 */
8
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("LiIonEnergySource");
25NS_OBJECT_ENSURE_REGISTERED(LiIonEnergySource);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::energy::LiIonEnergySource")
32 .AddDeprecatedName("ns3::LiIonEnergySource")
34 .SetGroupName("Energy")
35 .AddConstructor<LiIonEnergySource>()
36 .AddAttribute("LiIonEnergySourceInitialEnergyJ",
37 "Initial energy stored in basic energy source.",
38 DoubleValue(31752.0), // in Joules
42 .AddAttribute("LiIonEnergyLowBatteryThreshold",
43 "Low battery threshold for LiIon energy source.",
44 DoubleValue(0.10), // as a fraction of the initial energy
47 .AddAttribute("InitialCellVoltage",
48 "Initial (maximum) voltage of the cell (fully charged).",
49 DoubleValue(4.05), // in Volts
53 .AddAttribute("NominalCellVoltage",
54 "Nominal voltage of the cell.",
55 DoubleValue(3.6), // in Volts
58 .AddAttribute("ExpCellVoltage",
59 "Cell voltage at the end of the exponential zone.",
60 DoubleValue(3.6), // in Volts
63 .AddAttribute("RatedCapacity",
64 "Rated capacity of the cell.",
65 DoubleValue(2.45), // in Ah
68 .AddAttribute("NomCapacity",
69 "Cell capacity at the end of the nominal zone.",
70 DoubleValue(1.1), // in Ah
73 .AddAttribute("ExpCapacity",
74 "Cell Capacity at the end of the exponential zone.",
75 DoubleValue(1.2), // in Ah
78 .AddAttribute("InternalResistance",
79 "Internal resistance of the cell",
80 DoubleValue(0.083), // in Ohms
83 .AddAttribute("TypCurrent",
84 "Typical discharge current used to fit the curves",
85 DoubleValue(2.33), // in A
88 .AddAttribute("ThresholdVoltage",
89 "Minimum threshold voltage to consider the battery depleted.",
90 DoubleValue(3.3), // in Volts
93 .AddAttribute("PeriodicEnergyUpdateInterval",
94 "Time between two consecutive periodic energy updates.",
95 TimeValue(Seconds(1.0)),
99 .AddTraceSource("RemainingEnergy",
100 "Remaining energy at BasicEnergySource.",
102 "ns3::TracedValueCallback::Double");
103 return tid;
104}
105
107 : m_drainedCapacity(0.0),
108 m_lastUpdateTime(Seconds(0.0))
109{
110 NS_LOG_FUNCTION(this);
111}
112
117
118void
120{
121 NS_LOG_FUNCTION(this << initialEnergyJ);
122 NS_ASSERT(initialEnergyJ >= 0);
123 m_initialEnergyJ = initialEnergyJ;
124 // set remaining energy to be initial energy
126}
127
128double
134
135void
137{
138 NS_LOG_FUNCTION(this << supplyVoltageV);
139 m_eFull = supplyVoltageV;
140 m_supplyVoltageV = supplyVoltageV;
141}
142
143double
149
150void
152{
153 NS_LOG_FUNCTION(this << interval);
154 m_energyUpdateInterval = interval;
155}
156
157Time
163
164double
166{
167 NS_LOG_FUNCTION(this);
168 // update energy source to get the latest remaining energy.
170 return m_remainingEnergyJ;
171}
172
173double
175{
176 NS_LOG_FUNCTION(this);
177 // update energy source to get the latest remaining energy.
180}
181
182void
184{
185 NS_LOG_FUNCTION(this << energyJ);
186 NS_ASSERT(energyJ >= 0);
187 m_remainingEnergyJ -= energyJ;
188
189 // check if remaining energy is 0
191 {
193 }
194}
195
196void
198{
199 NS_LOG_FUNCTION(this << energyJ);
200 NS_ASSERT(energyJ >= 0);
201 m_remainingEnergyJ += energyJ;
202}
203
204void
206{
207 NS_LOG_FUNCTION(this);
208 NS_LOG_DEBUG("LiIonEnergySource:Updating remaining energy at node #" << GetNode()->GetId());
209
210 // do not update if simulation has finished
212 {
213 return;
214 }
215
217
219
221
223 {
225 return; // stop periodic update
226 }
227
230}
231
232/*
233 * Private functions start here.
234 */
235void
237{
238 NS_LOG_FUNCTION(this);
239 UpdateEnergySource(); // start periodic update
240}
241
242void
244{
245 NS_LOG_FUNCTION(this);
246 BreakDeviceEnergyModelRefCycle(); // break reference cycle
247}
248
249void
251{
252 NS_LOG_FUNCTION(this);
253 NS_LOG_DEBUG("LiIonEnergySource:Energy depleted at node #" << GetNode()->GetId());
254 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
255}
256
257void
259{
260 NS_LOG_FUNCTION(this);
261 double totalCurrentA = CalculateTotalCurrent();
262 Time duration = Simulator::Now() - m_lastUpdateTime;
263 NS_ASSERT(duration.GetSeconds() >= 0);
264 // energy = current * voltage * time
265 double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds();
266
267 if (m_remainingEnergyJ < energyToDecreaseJ)
268 {
269 m_remainingEnergyJ = 0; // energy never goes below 0
270 }
271 else
272 {
273 m_remainingEnergyJ -= energyToDecreaseJ;
274 }
275
276 m_drainedCapacity += (totalCurrentA * duration).GetHours();
277 // update the supply voltage
278 m_supplyVoltageV = GetVoltage(totalCurrentA);
279 NS_LOG_DEBUG("LiIonEnergySource:Remaining energy = " << m_remainingEnergyJ);
280}
281
282double
284{
285 NS_LOG_FUNCTION(this << i);
286
287 // integral of i in dt, drained capacity in Ah
288 double it = m_drainedCapacity;
289
290 // empirical factors
291 double A = m_eFull - m_eExp;
292 double B = 3 / m_qExp;
293
294 // slope of the polarization curve
295 double K = std::abs((m_eFull - m_eNom + A * (std::exp(-B * m_qNom) - 1)) * (m_qRated - m_qNom) /
296 m_qNom);
297
298 // constant voltage
299 double E0 = m_eFull + K + m_internalResistance * m_typCurrent - A;
300
301 double E = E0 - K * m_qRated / (m_qRated - it) + A * std::exp(-B * it);
302
303 // cell voltage
304 double V = E - m_internalResistance * i;
305
306 NS_LOG_DEBUG("Voltage: " << V << " with E: " << E);
307
308 return V;
309}
310
311} // namespace energy
312} // 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
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
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
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.
Ptr< Node > GetNode() const
Model a generic Lithium Ion Battery basing on [1][2].
void UpdateEnergySource() override
Implements UpdateEnergySource.
void SetInitialEnergy(double initialEnergyJ)
double m_minVoltTh
minimum threshold voltage to consider the battery depleted
double m_qNom
cell capacity at the end of the nominal zone, in Ah
EventId m_energyUpdateEvent
energy update event
double m_eNom
nominal voltage of the cell, in Volts
double m_qExp
capacity value at the end of the exponential zone, in Ah
double m_drainedCapacity
capacity drained from the cell, in Ah
TracedValue< double > m_remainingEnergyJ
remaining energy, in Joules
void DoInitialize() override
Initialize() implementation.
virtual void IncreaseRemainingEnergy(double energyJ)
double GetInitialEnergy() const override
void SetInitialSupplyVoltage(double supplyVoltageV)
virtual void DecreaseRemainingEnergy(double energyJ)
double GetVoltage(double current) const
Get the cell voltage in function of the discharge current.
double m_internalResistance
internal resistance of the cell, in Ohms
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
Time m_energyUpdateInterval
energy update interval
double m_typCurrent
typical discharge current used to fit the curves
void DoDispose() override
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
double m_supplyVoltageV
actual voltage of the cell
double GetSupplyVoltage() const override
static TypeId GetTypeId()
Get the type ID.
void CalculateRemainingEnergy()
Calculates remaining energy.
double m_qRated
rated capacity of the cell, in Ah
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
double m_eFull
initial voltage of the cell, in Volts
double m_eExp
cell voltage at the end of the exponential zone, in Volts
double m_initialEnergyJ
initial energy, in Joules
#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 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
#define E(name, start, end)