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 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::LiIonEnergySource")
42  .AddConstructor<LiIonEnergySource> ()
43  .AddAttribute ("LiIonEnergySourceInitialEnergyJ",
44  "Initial energy stored in basic energy source.",
45  DoubleValue (31752.0), // in Joules
46  MakeDoubleAccessor (&LiIonEnergySource::SetInitialEnergy,
48  MakeDoubleChecker<double> ())
49  .AddAttribute ("InitialCellVoltage",
50  "Initial (maximum) voltage of the cell (fully charged).",
51  DoubleValue (4.05), // in Volts
52  MakeDoubleAccessor (&LiIonEnergySource::SetInitialSupplyVoltage,
54  MakeDoubleChecker<double> ())
55  .AddAttribute ("NominalCellVoltage",
56  "Nominal voltage of the cell.",
57  DoubleValue (3.6), // in Volts
58  MakeDoubleAccessor (&LiIonEnergySource::m_eNom),
59  MakeDoubleChecker<double> ())
60  .AddAttribute ("ExpCellVoltage",
61  "Cell voltage at the end of the exponential zone.",
62  DoubleValue (3.6), // in Volts
63  MakeDoubleAccessor (&LiIonEnergySource::m_eExp),
64  MakeDoubleChecker<double> ())
65  .AddAttribute ("RatedCapacity",
66  "Rated capacity of the cell.",
67  DoubleValue (2.45), // in Ah
68  MakeDoubleAccessor (&LiIonEnergySource::m_qRated),
69  MakeDoubleChecker<double> ())
70  .AddAttribute ("NomCapacity",
71  "Cell capacity at the end of the nominal zone.",
72  DoubleValue (1.1), // in Ah
73  MakeDoubleAccessor (&LiIonEnergySource::m_qNom),
74  MakeDoubleChecker<double> ())
75  .AddAttribute ("ExpCapacity",
76  "Cell Capacity at the end of the exponential zone.",
77  DoubleValue (1.2), // in Ah
78  MakeDoubleAccessor (&LiIonEnergySource::m_qExp),
79  MakeDoubleChecker<double> ())
80  .AddAttribute ("InternalResistance",
81  "Internal resistance of the cell",
82  DoubleValue (0.083), // in Ohms
83  MakeDoubleAccessor (&LiIonEnergySource::m_internalResistance),
84  MakeDoubleChecker<double> ())
85  .AddAttribute ("TypCurrent",
86  "Typical discharge current used to fit the curves",
87  DoubleValue (2.33), // in A
88  MakeDoubleAccessor (&LiIonEnergySource::m_typCurrent),
89  MakeDoubleChecker<double> ())
90  .AddAttribute ("ThresholdVoltage",
91  "Minimum threshold voltage to consider the battery depleted.",
92  DoubleValue (3.3), // in Volts
93  MakeDoubleAccessor (&LiIonEnergySource::m_minVoltTh),
94  MakeDoubleChecker<double> ())
95  .AddAttribute ("PeriodicEnergyUpdateInterval",
96  "Time between two consecutive periodic energy updates.",
97  TimeValue (Seconds (1.0)),
100  MakeTimeChecker ())
101  .AddTraceSource ("RemainingEnergy",
102  "Remaining energy at BasicEnergySource.",
104  ;
105  return tid;
106 }
107 
109  : m_drainedCapacity (0.0),
110  m_lastUpdateTime (Seconds (0.0))
111 {
112  NS_LOG_FUNCTION (this);
113 }
114 
116 {
117  NS_LOG_FUNCTION (this);
118 }
119 
120 void
121 LiIonEnergySource::SetInitialEnergy (double initialEnergyJ)
122 {
123  NS_LOG_FUNCTION (this << initialEnergyJ);
124  NS_ASSERT (initialEnergyJ >= 0);
125  m_initialEnergyJ = initialEnergyJ;
126  // set remaining energy to be initial energy
128 }
129 
130 double
132 {
133  NS_LOG_FUNCTION (this);
134  return m_initialEnergyJ;
135 }
136 
137 void
139 {
140  NS_LOG_FUNCTION (this << supplyVoltageV);
141  m_eFull = supplyVoltageV;
142  m_supplyVoltageV = supplyVoltageV;
143 }
144 
145 double
147 {
148  NS_LOG_FUNCTION (this);
149  return m_supplyVoltageV;
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION (this << interval);
156  m_energyUpdateInterval = interval;
157 }
158 
159 Time
161 {
162  NS_LOG_FUNCTION (this);
163  return m_energyUpdateInterval;
164 }
165 
166 double
168 {
169  NS_LOG_FUNCTION (this);
170  // update energy source to get the latest remaining energy.
172  return m_remainingEnergyJ;
173 }
174 
175 double
177 {
178  NS_LOG_FUNCTION (this);
179  // update energy source to get the latest remaining energy.
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (this << energyJ);
188  NS_ASSERT (energyJ >= 0);
189  m_remainingEnergyJ -= energyJ;
190 
191  // check if remaining energy is 0
193  {
195  }
196 }
197 
198 void
200 {
201  NS_LOG_FUNCTION (this << energyJ);
202  NS_ASSERT (energyJ >= 0);
203  m_remainingEnergyJ += energyJ;
204 }
205 
206 void
208 {
209  NS_LOG_FUNCTION (this);
210  NS_LOG_DEBUG ("LiIonEnergySource:Updating remaining energy at node #" <<
211  GetNode ()->GetId ());
212 
213  // do not update if simulation has finished
214  if (Simulator::IsFinished ())
215  {
216  return;
217  }
218 
220 
222 
223  if (m_remainingEnergyJ <= 0)
224  {
226  return; // stop periodic update
227  }
228 
230 
233  this);
234 }
235 
236 /*
237  * Private functions start here.
238  */
239 void
241 {
242  NS_LOG_FUNCTION (this);
243  UpdateEnergySource (); // start periodic update
244 }
245 
246 void
248 {
249  NS_LOG_FUNCTION (this);
250  // calculate remaining energy at the end of simulation
252  BreakDeviceEnergyModelRefCycle (); // break reference cycle
253 }
254 
255 
256 void
258 {
259  NS_LOG_FUNCTION (this);
260  NS_LOG_DEBUG ("LiIonEnergySource:Energy depleted at node #" <<
261  GetNode ()->GetId ());
262  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
263  m_remainingEnergyJ = 0; // energy never goes below 0
264 }
265 
266 
267 void
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  m_remainingEnergyJ -= energyToDecreaseJ;
277  m_drainedCapacity += (totalCurrentA * duration.GetSeconds () / 3600);
278  // update the supply voltage
279  m_supplyVoltageV = GetVoltage (totalCurrentA);
280  NS_LOG_DEBUG ("LiIonEnergySource:Remaining energy = " << m_remainingEnergyJ);
281 }
282 
283 double
285 {
286  NS_LOG_FUNCTION (this << i);
287 
288  // integral of i in dt, drained capacity in Ah
289  double it = m_drainedCapacity;
290 
291  // empirical factors
292  double A = m_eFull - m_eExp;
293  double B = 3 / m_qExp;
294 
295  // slope of the polarization curve
296  double K = std::abs ( (m_eFull - m_eNom + A * (std::exp (-B * m_qNom) - 1)) * (m_qRated - m_qNom) / 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 ns3
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
NS_LOG_COMPONENT_DEFINE("LiIonEnergySource")
void SetEnergyUpdateInterval(Time interval)
virtual double GetEnergyFraction(void)
Energy source base class.
Definition: energy-source.h:71
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)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:824
static TypeId GetTypeId(void)
virtual double GetInitialEnergy(void) const
double GetVoltage(double current) const
void CalculateRemainingEnergy(void)
Calculates remaining energy.
virtual double GetRemainingEnergy(void)
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
double GetSeconds(void) const
Definition: nstime.h:274
#define V(v)
hold objects of type ns3::Time
Definition: nstime.h:961
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)
Definition: log.h:289
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
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
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:611
#define E(a, b, c)