A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("LiIonEnergySource");
31 
32 namespace ns3 {
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
45  MakeDoubleAccessor (&LiIonEnergySource::SetInitialEnergy,
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
51  MakeDoubleAccessor (&LiIonEnergySource::m_lowBatteryTh),
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("InitialCellVoltage",
54  "Initial (maximum) voltage of the cell (fully charged).",
55  DoubleValue (4.05), // in Volts
56  MakeDoubleAccessor (&LiIonEnergySource::SetInitialSupplyVoltage,
58  MakeDoubleChecker<double> ())
59  .AddAttribute ("NominalCellVoltage",
60  "Nominal voltage of the cell.",
61  DoubleValue (3.6), // in Volts
62  MakeDoubleAccessor (&LiIonEnergySource::m_eNom),
63  MakeDoubleChecker<double> ())
64  .AddAttribute ("ExpCellVoltage",
65  "Cell voltage at the end of the exponential zone.",
66  DoubleValue (3.6), // in Volts
67  MakeDoubleAccessor (&LiIonEnergySource::m_eExp),
68  MakeDoubleChecker<double> ())
69  .AddAttribute ("RatedCapacity",
70  "Rated capacity of the cell.",
71  DoubleValue (2.45), // in Ah
72  MakeDoubleAccessor (&LiIonEnergySource::m_qRated),
73  MakeDoubleChecker<double> ())
74  .AddAttribute ("NomCapacity",
75  "Cell capacity at the end of the nominal zone.",
76  DoubleValue (1.1), // in Ah
77  MakeDoubleAccessor (&LiIonEnergySource::m_qNom),
78  MakeDoubleChecker<double> ())
79  .AddAttribute ("ExpCapacity",
80  "Cell Capacity at the end of the exponential zone.",
81  DoubleValue (1.2), // in Ah
82  MakeDoubleAccessor (&LiIonEnergySource::m_qExp),
83  MakeDoubleChecker<double> ())
84  .AddAttribute ("InternalResistance",
85  "Internal resistance of the cell",
86  DoubleValue (0.083), // in Ohms
87  MakeDoubleAccessor (&LiIonEnergySource::m_internalResistance),
88  MakeDoubleChecker<double> ())
89  .AddAttribute ("TypCurrent",
90  "Typical discharge current used to fit the curves",
91  DoubleValue (2.33), // in A
92  MakeDoubleAccessor (&LiIonEnergySource::m_typCurrent),
93  MakeDoubleChecker<double> ())
94  .AddAttribute ("ThresholdVoltage",
95  "Minimum threshold voltage to consider the battery depleted.",
96  DoubleValue (3.3), // in Volts
97  MakeDoubleAccessor (&LiIonEnergySource::m_minVoltTh),
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  ;
109  return tid;
110 }
111 
113  : m_drainedCapacity (0.0),
114  m_lastUpdateTime (Seconds (0.0))
115 {
116  NS_LOG_FUNCTION (this);
117 }
118 
120 {
121  NS_LOG_FUNCTION (this);
122 }
123 
124 void
125 LiIonEnergySource::SetInitialEnergy (double initialEnergyJ)
126 {
127  NS_LOG_FUNCTION (this << initialEnergyJ);
128  NS_ASSERT (initialEnergyJ >= 0);
129  m_initialEnergyJ = initialEnergyJ;
130  // set remaining energy to be initial energy
132 }
133 
134 double
136 {
137  NS_LOG_FUNCTION (this);
138  return m_initialEnergyJ;
139 }
140 
141 void
143 {
144  NS_LOG_FUNCTION (this << supplyVoltageV);
145  m_eFull = supplyVoltageV;
146  m_supplyVoltageV = supplyVoltageV;
147 }
148 
149 double
151 {
152  NS_LOG_FUNCTION (this);
153  return m_supplyVoltageV;
154 }
155 
156 void
158 {
159  NS_LOG_FUNCTION (this << interval);
160  m_energyUpdateInterval = interval;
161 }
162 
163 Time
165 {
166  NS_LOG_FUNCTION (this);
167  return m_energyUpdateInterval;
168 }
169 
170 double
172 {
173  NS_LOG_FUNCTION (this);
174  // update energy source to get the latest remaining energy.
176  return m_remainingEnergyJ;
177 }
178 
179 double
181 {
182  NS_LOG_FUNCTION (this);
183  // update energy source to get the latest remaining energy.
186 }
187 
188 void
190 {
191  NS_LOG_FUNCTION (this << energyJ);
192  NS_ASSERT (energyJ >= 0);
193  m_remainingEnergyJ -= energyJ;
194 
195  // check if remaining energy is 0
197  {
199  }
200 }
201 
202 void
204 {
205  NS_LOG_FUNCTION (this << energyJ);
206  NS_ASSERT (energyJ >= 0);
207  m_remainingEnergyJ += energyJ;
208 }
209 
210 void
212 {
213  NS_LOG_FUNCTION (this);
214  NS_LOG_DEBUG ("LiIonEnergySource:Updating remaining energy at node #" <<
215  GetNode ()->GetId ());
216 
217  // do not update if simulation has finished
218  if (Simulator::IsFinished ())
219  {
220  return;
221  }
222 
224 
226 
228 
230  {
232  return; // stop periodic update
233  }
234 
237  this);
238 }
239 
240 /*
241  * Private functions start here.
242  */
243 void
245 {
246  NS_LOG_FUNCTION (this);
247  UpdateEnergySource (); // start periodic update
248 }
249 
250 void
252 {
253  NS_LOG_FUNCTION (this);
254  // calculate remaining energy at the end of simulation
256  BreakDeviceEnergyModelRefCycle (); // break reference cycle
257 }
258 
259 
260 void
262 {
263  NS_LOG_FUNCTION (this);
264  NS_LOG_DEBUG ("LiIonEnergySource:Energy depleted at node #" <<
265  GetNode ()->GetId ());
266  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
267  if (m_remainingEnergyJ <= 0)
268  {
269  m_remainingEnergyJ = 0; // energy never goes below 0
270  }
271 }
272 
273 
274 void
276 {
277  NS_LOG_FUNCTION (this);
278  double totalCurrentA = CalculateTotalCurrent ();
279  Time duration = Simulator::Now () - m_lastUpdateTime;
280  NS_ASSERT (duration.GetSeconds () >= 0);
281  // energy = current * voltage * time
282  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
283  m_remainingEnergyJ -= energyToDecreaseJ;
284  m_drainedCapacity += (totalCurrentA * duration.GetSeconds () / 3600);
285  // update the supply voltage
286  m_supplyVoltageV = GetVoltage (totalCurrentA);
287  NS_LOG_DEBUG ("LiIonEnergySource:Remaining energy = " << m_remainingEnergyJ);
288 }
289 
290 double
292 {
293  NS_LOG_FUNCTION (this << i);
294 
295  // integral of i in dt, drained capacity in Ah
296  double it = m_drainedCapacity;
297 
298  // empirical factors
299  double A = m_eFull - m_eExp;
300  double B = 3 / m_qExp;
301 
302  // slope of the polarization curve
303  double K = std::abs ( (m_eFull - m_eNom + A * (std::exp (-B * m_qNom) - 1)) * (m_qRated - m_qNom) / m_qNom);
304 
305  // constant voltage
306  double E0 = m_eFull + K + m_internalResistance * m_typCurrent - A;
307 
308  double E = E0 - K * m_qRated / (m_qRated - it) + A * std::exp (-B * it);
309 
310  // cell voltage
311  double V = E - m_internalResistance * i;
312 
313  NS_LOG_DEBUG ("Voltage: " << V << " with E: " << E);
314 
315  return V;
316 }
317 
318 } // 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 the class in the ns-3 factory.
Definition: object-base.h:38
void SetEnergyUpdateInterval(Time interval)
virtual double GetEnergyFraction(void)
Doxygen 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:170
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:825
static TypeId GetTypeId(void)
virtual double GetInitialEnergy(void) const
double GetVoltage(double current) const
void CalculateRemainingEnergy(void)
Calculates remaining energy.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:444
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:322
Attribute for objects of type ns3::Time.
Definition: nstime.h:912
TracedValue< double > m_remainingEnergyJ
void SetInitialSupplyVoltage(double supplyVoltageV)
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
double CalculateTotalCurrent(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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:213
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
virtual double GetSupplyVoltage(void) const
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
virtual void DecreaseRemainingEnergy(double energyJ)
virtual void IncreaseRemainingEnergy(double energyJ)
void SetInitialEnergy(double initialEnergyJ)
static bool IsFinished(void)
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
Definition: simulator.cc:150
Hold a floating point type.
Definition: double.h:41
void DoInitialize(void)
This method is called only once by Object::Initialize.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
#define E(a, b, c)