A Discrete-Event Network Simulator
API
li-ion-energy-source.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Andrea Sacco
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  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
19  */
20 
21 #include "li-ion-energy-source.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 
28 #include <cmath>
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("LiIonEnergySource");
33 
34 NS_OBJECT_ENSURE_REGISTERED (LiIonEnergySource);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::LiIonEnergySource")
41  .AddConstructor<LiIonEnergySource> ()
42  .AddAttribute ("LiIonEnergySourceInitialEnergyJ",
43  "Initial energy stored in basic energy source.",
44  DoubleValue (31752.0), // in Joules
47  MakeDoubleChecker<double> ())
48  .AddAttribute ("LiIonEnergyLowBatteryThreshold",
49  "Low battery threshold for LiIon energy source.",
50  DoubleValue (0.10), // as a fraction of the initial energy
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("InitialCellVoltage",
54  "Initial (maximum) voltage of the cell (fully charged).",
55  DoubleValue (4.05), // in Volts
58  MakeDoubleChecker<double> ())
59  .AddAttribute ("NominalCellVoltage",
60  "Nominal voltage of the cell.",
61  DoubleValue (3.6), // in Volts
63  MakeDoubleChecker<double> ())
64  .AddAttribute ("ExpCellVoltage",
65  "Cell voltage at the end of the exponential zone.",
66  DoubleValue (3.6), // in Volts
68  MakeDoubleChecker<double> ())
69  .AddAttribute ("RatedCapacity",
70  "Rated capacity of the cell.",
71  DoubleValue (2.45), // in Ah
73  MakeDoubleChecker<double> ())
74  .AddAttribute ("NomCapacity",
75  "Cell capacity at the end of the nominal zone.",
76  DoubleValue (1.1), // in Ah
78  MakeDoubleChecker<double> ())
79  .AddAttribute ("ExpCapacity",
80  "Cell Capacity at the end of the exponential zone.",
81  DoubleValue (1.2), // in Ah
83  MakeDoubleChecker<double> ())
84  .AddAttribute ("InternalResistance",
85  "Internal resistance of the cell",
86  DoubleValue (0.083), // in Ohms
88  MakeDoubleChecker<double> ())
89  .AddAttribute ("TypCurrent",
90  "Typical discharge current used to fit the curves",
91  DoubleValue (2.33), // in A
93  MakeDoubleChecker<double> ())
94  .AddAttribute ("ThresholdVoltage",
95  "Minimum threshold voltage to consider the battery depleted.",
96  DoubleValue (3.3), // in Volts
98  MakeDoubleChecker<double> ())
99  .AddAttribute ("PeriodicEnergyUpdateInterval",
100  "Time between two consecutive periodic energy updates.",
101  TimeValue (Seconds (1.0)),
104  MakeTimeChecker ())
105  .AddTraceSource ("RemainingEnergy",
106  "Remaining energy at BasicEnergySource.",
108  "ns3::TracedValue::DoubleCallback")
109  ;
110  return tid;
111 }
112 
114  : m_drainedCapacity (0.0),
115  m_lastUpdateTime (Seconds (0.0))
116 {
117  NS_LOG_FUNCTION (this);
118 }
119 
121 {
122  NS_LOG_FUNCTION (this);
123 }
124 
125 void
126 LiIonEnergySource::SetInitialEnergy (double initialEnergyJ)
127 {
128  NS_LOG_FUNCTION (this << initialEnergyJ);
129  NS_ASSERT (initialEnergyJ >= 0);
130  m_initialEnergyJ = initialEnergyJ;
131  // set remaining energy to be initial energy
133 }
134 
135 double
137 {
138  NS_LOG_FUNCTION (this);
139  return m_initialEnergyJ;
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this << supplyVoltageV);
146  m_eFull = supplyVoltageV;
147  m_supplyVoltageV = supplyVoltageV;
148 }
149 
150 double
152 {
153  NS_LOG_FUNCTION (this);
154  return m_supplyVoltageV;
155 }
156 
157 void
159 {
160  NS_LOG_FUNCTION (this << interval);
161  m_energyUpdateInterval = interval;
162 }
163 
164 Time
166 {
167  NS_LOG_FUNCTION (this);
168  return m_energyUpdateInterval;
169 }
170 
171 double
173 {
174  NS_LOG_FUNCTION (this);
175  // update energy source to get the latest remaining energy.
177  return m_remainingEnergyJ;
178 }
179 
180 double
182 {
183  NS_LOG_FUNCTION (this);
184  // update energy source to get the latest remaining energy.
187 }
188 
189 void
191 {
192  NS_LOG_FUNCTION (this << energyJ);
193  NS_ASSERT (energyJ >= 0);
194  m_remainingEnergyJ -= energyJ;
195 
196  // check if remaining energy is 0
198  {
200  }
201 }
202 
203 void
205 {
206  NS_LOG_FUNCTION (this << energyJ);
207  NS_ASSERT (energyJ >= 0);
208  m_remainingEnergyJ += energyJ;
209 }
210 
211 void
213 {
214  NS_LOG_FUNCTION (this);
215  NS_LOG_DEBUG ("LiIonEnergySource:Updating remaining energy at node #" <<
216  GetNode ()->GetId ());
217 
218  // do not update if simulation has finished
219  if (Simulator::IsFinished ())
220  {
221  return;
222  }
223 
225 
227 
229 
231  {
233  return; // stop periodic update
234  }
235 
238  this);
239 }
240 
241 /*
242  * Private functions start here.
243  */
244 void
246 {
247  NS_LOG_FUNCTION (this);
248  UpdateEnergySource (); // start periodic update
249 }
250 
251 void
253 {
254  NS_LOG_FUNCTION (this);
255  // calculate remaining energy at the end of simulation
257  BreakDeviceEnergyModelRefCycle (); // break reference cycle
258 }
259 
260 
261 void
263 {
264  NS_LOG_FUNCTION (this);
265  NS_LOG_DEBUG ("LiIonEnergySource:Energy depleted at node #" <<
266  GetNode ()->GetId ());
267  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
268  if (m_remainingEnergyJ <= 0)
269  {
270  m_remainingEnergyJ = 0; // energy never goes below 0
271  }
272 }
273 
274 
275 void
277 {
278  NS_LOG_FUNCTION (this);
279  double totalCurrentA = CalculateTotalCurrent ();
280  Time duration = Simulator::Now () - m_lastUpdateTime;
281  NS_ASSERT (duration.GetSeconds () >= 0);
282  // energy = current * voltage * time
283  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
284  m_remainingEnergyJ -= energyToDecreaseJ;
285  m_drainedCapacity += (totalCurrentA * duration.GetSeconds () / 3600);
286  // update the supply voltage
287  m_supplyVoltageV = GetVoltage (totalCurrentA);
288  NS_LOG_DEBUG ("LiIonEnergySource:Remaining energy = " << m_remainingEnergyJ);
289 }
290 
291 double
293 {
294  NS_LOG_FUNCTION (this << i);
295 
296  // integral of i in dt, drained capacity in Ah
297  double it = m_drainedCapacity;
298 
299  // empirical factors
300  double A = m_eFull - m_eExp;
301  double B = 3 / m_qExp;
302 
303  // slope of the polarization curve
304  double K = std::abs ( (m_eFull - m_eNom + A * (std::exp (-B * m_qNom) - 1)) * (m_qRated - m_qNom) / m_qNom);
305 
306  // constant voltage
307  double E0 = m_eFull + K + m_internalResistance * m_typCurrent - A;
308 
309  double E = E0 - K * m_qRated / (m_qRated - it) + A * std::exp (-B * it);
310 
311  // cell voltage
312  double V = E - m_internalResistance * i;
313 
314  NS_LOG_DEBUG ("Voltage: " << V << " with E: " << E);
315 
316  return V;
317 }
318 
319 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
#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:44
void SetEnergyUpdateInterval(Time interval)
virtual double GetEnergyFraction(void)
Introspection did not find any typical Config paths.
Definition: energy-source.h:81
Ptr< Node > GetNode(void) const
void DoDispose(void)
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
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:819
static TypeId GetTypeId(void)
virtual double GetInitialEnergy(void) const
double GetVoltage(double current) const
void CalculateRemainingEnergy(void)
Calculates remaining energy.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:439
virtual double GetRemainingEnergy(void)
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:327
AttributeValue implementation for Time.
Definition: nstime.h:921
TracedValue< double > m_remainingEnergyJ
void SetInitialSupplyVoltage(double supplyVoltageV)
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
double CalculateTotalCurrent(void)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:922
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: double.h:42
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
Time GetEnergyUpdateInterval(void) const
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
virtual double GetSupplyVoltage(void) const
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
virtual void DecreaseRemainingEnergy(double energyJ)
virtual void IncreaseRemainingEnergy(double energyJ)
void SetInitialEnergy(double initialEnergyJ)
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:193
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
void DoInitialize(void)
Initialize() implementation.
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
#define E(a, b, c)