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 * 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 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
18 */
19
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
30namespace ns3
31{
32namespace energy
33{
34
35NS_LOG_COMPONENT_DEFINE("LiIonEnergySource");
36NS_OBJECT_ENSURE_REGISTERED(LiIonEnergySource);
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::LiIonEnergySource")
44 .SetGroupName("Energy")
45 .AddConstructor<LiIonEnergySource>()
46 .AddAttribute("LiIonEnergySourceInitialEnergyJ",
47 "Initial energy stored in basic energy source.",
48 DoubleValue(31752.0), // in Joules
51 MakeDoubleChecker<double>())
52 .AddAttribute("LiIonEnergyLowBatteryThreshold",
53 "Low battery threshold for LiIon energy source.",
54 DoubleValue(0.10), // as a fraction of the initial energy
56 MakeDoubleChecker<double>())
57 .AddAttribute("InitialCellVoltage",
58 "Initial (maximum) voltage of the cell (fully charged).",
59 DoubleValue(4.05), // in Volts
62 MakeDoubleChecker<double>())
63 .AddAttribute("NominalCellVoltage",
64 "Nominal voltage of the cell.",
65 DoubleValue(3.6), // in Volts
67 MakeDoubleChecker<double>())
68 .AddAttribute("ExpCellVoltage",
69 "Cell voltage at the end of the exponential zone.",
70 DoubleValue(3.6), // in Volts
72 MakeDoubleChecker<double>())
73 .AddAttribute("RatedCapacity",
74 "Rated capacity of the cell.",
75 DoubleValue(2.45), // in Ah
77 MakeDoubleChecker<double>())
78 .AddAttribute("NomCapacity",
79 "Cell capacity at the end of the nominal zone.",
80 DoubleValue(1.1), // in Ah
82 MakeDoubleChecker<double>())
83 .AddAttribute("ExpCapacity",
84 "Cell Capacity at the end of the exponential zone.",
85 DoubleValue(1.2), // in Ah
87 MakeDoubleChecker<double>())
88 .AddAttribute("InternalResistance",
89 "Internal resistance of the cell",
90 DoubleValue(0.083), // in Ohms
92 MakeDoubleChecker<double>())
93 .AddAttribute("TypCurrent",
94 "Typical discharge current used to fit the curves",
95 DoubleValue(2.33), // in A
97 MakeDoubleChecker<double>())
98 .AddAttribute("ThresholdVoltage",
99 "Minimum threshold voltage to consider the battery depleted.",
100 DoubleValue(3.3), // in Volts
102 MakeDoubleChecker<double>())
103 .AddAttribute("PeriodicEnergyUpdateInterval",
104 "Time between two consecutive periodic energy updates.",
105 TimeValue(Seconds(1.0)),
109 .AddTraceSource("RemainingEnergy",
110 "Remaining energy at BasicEnergySource.",
112 "ns3::TracedValueCallback::Double");
113 return tid;
114}
115
117 : m_drainedCapacity(0.0),
118 m_lastUpdateTime(Seconds(0.0))
119{
120 NS_LOG_FUNCTION(this);
121}
122
124{
125 NS_LOG_FUNCTION(this);
126}
127
128void
130{
131 NS_LOG_FUNCTION(this << initialEnergyJ);
132 NS_ASSERT(initialEnergyJ >= 0);
133 m_initialEnergyJ = initialEnergyJ;
134 // set remaining energy to be initial energy
136}
137
138double
140{
141 NS_LOG_FUNCTION(this);
142 return m_initialEnergyJ;
143}
144
145void
147{
148 NS_LOG_FUNCTION(this << supplyVoltageV);
149 m_eFull = supplyVoltageV;
150 m_supplyVoltageV = supplyVoltageV;
151}
152
153double
155{
156 NS_LOG_FUNCTION(this);
157 return m_supplyVoltageV;
158}
159
160void
162{
163 NS_LOG_FUNCTION(this << interval);
164 m_energyUpdateInterval = interval;
165}
166
167Time
169{
170 NS_LOG_FUNCTION(this);
172}
173
174double
176{
177 NS_LOG_FUNCTION(this);
178 // update energy source to get the latest remaining energy.
180 return m_remainingEnergyJ;
181}
182
183double
185{
186 NS_LOG_FUNCTION(this);
187 // update energy source to get the latest remaining energy.
190}
191
192void
194{
195 NS_LOG_FUNCTION(this << energyJ);
196 NS_ASSERT(energyJ >= 0);
197 m_remainingEnergyJ -= energyJ;
198
199 // check if remaining energy is 0
201 {
203 }
204}
205
206void
208{
209 NS_LOG_FUNCTION(this << energyJ);
210 NS_ASSERT(energyJ >= 0);
211 m_remainingEnergyJ += energyJ;
212}
213
214void
216{
217 NS_LOG_FUNCTION(this);
218 NS_LOG_DEBUG("LiIonEnergySource:Updating remaining energy at node #" << GetNode()->GetId());
219
220 // do not update if simulation has finished
222 {
223 return;
224 }
225
227
229
231
233 {
235 return; // stop periodic update
236 }
237
240}
241
242/*
243 * Private functions start here.
244 */
245void
247{
248 NS_LOG_FUNCTION(this);
249 UpdateEnergySource(); // start periodic update
250}
251
252void
254{
255 NS_LOG_FUNCTION(this);
256 BreakDeviceEnergyModelRefCycle(); // break reference cycle
257}
258
259void
261{
262 NS_LOG_FUNCTION(this);
263 NS_LOG_DEBUG("LiIonEnergySource:Energy depleted at node #" << GetNode()->GetId());
264 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
265}
266
267void
269{
270 NS_LOG_FUNCTION(this);
271 double totalCurrentA = CalculateTotalCurrent();
272 Time duration = Simulator::Now() - m_lastUpdateTime;
273 NS_ASSERT(duration.GetSeconds() >= 0);
274 // energy = current * voltage * time
275 double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds();
276
277 if (m_remainingEnergyJ < energyToDecreaseJ)
278 {
279 m_remainingEnergyJ = 0; // energy never goes below 0
280 }
281 else
282 {
283 m_remainingEnergyJ -= energyToDecreaseJ;
284 }
285
286 m_drainedCapacity += (totalCurrentA * duration).GetHours();
287 // update the supply voltage
288 m_supplyVoltageV = GetVoltage(totalCurrentA);
289 NS_LOG_DEBUG("LiIonEnergySource:Remaining energy = " << m_remainingEnergyJ);
290}
291
292double
294{
295 NS_LOG_FUNCTION(this << i);
296
297 // integral of i in dt, drained capacity in Ah
298 double it = m_drainedCapacity;
299
300 // empirical factors
301 double A = m_eFull - m_eExp;
302 double B = 3 / m_qExp;
303
304 // slope of the polarization curve
305 double K = std::abs((m_eFull - m_eNom + A * (std::exp(-B * m_qNom) - 1)) * (m_qRated - m_qNom) /
306 m_qNom);
307
308 // constant voltage
309 double E0 = m_eFull + K + m_internalResistance * m_typCurrent - A;
310
311 double E = E0 - K * m_qRated / (m_qRated - it) + A * std::exp(-B * it);
312
313 // cell voltage
314 double V = E - m_internalResistance * i;
315
316 NS_LOG_DEBUG("Voltage: " << V << " with E: " << E);
317
318 return V;
319}
320
321} // namespace energy
322} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
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
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Energy source base class.
Definition: energy-source.h:88
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)
void SetEnergyUpdateInterval(Time interval)
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: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:1319
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:1407
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427
#define E(name, start, end)