View | Details | Raw Unified | Return to bug 2403
Collapse All | Expand All

(-)a/src/energy/model/basic-energy-source.cc (-9 / +58 lines)
 Lines 40-49    Link Here 
40
    .AddConstructor<BasicEnergySource> ()
40
    .AddConstructor<BasicEnergySource> ()
41
    .AddAttribute ("BasicEnergySourceInitialEnergyJ",
41
    .AddAttribute ("BasicEnergySourceInitialEnergyJ",
42
                   "Initial energy stored in basic energy source.",
42
                   "Initial energy stored in basic energy source.",
43
                   DoubleValue (10),  // in Joules
43
                   DoubleValue (0),  // in Joules
44
                   MakeDoubleAccessor (&BasicEnergySource::SetInitialEnergy,
44
                   MakeDoubleAccessor (&BasicEnergySource::SetInitialEnergy,
45
                                       &BasicEnergySource::GetInitialEnergy),
45
                                       &BasicEnergySource::GetInitialEnergy),
46
                   MakeDoubleChecker<double> ())
46
                   MakeDoubleChecker<double> ())
47
    .AddAttribute ("BasicEnergySourceCapacityJ",
48
                   "Energy capacity that can be stored in basic energy source.",
49
                   DoubleValue (0),  // in Joules
50
                   MakeDoubleAccessor (&BasicEnergySource::SetEnergyCapacity,
51
                                       &BasicEnergySource::GetEnergyCapacity),
52
                   MakeDoubleChecker<double> ())
47
    .AddAttribute ("BasicEnergySupplyVoltageV",
53
    .AddAttribute ("BasicEnergySupplyVoltageV",
48
                   "Initial supply voltage for basic energy source.",
54
                   "Initial supply voltage for basic energy source.",
49
                   DoubleValue (3.0), // in Volts
55
                   DoubleValue (3.0), // in Volts
 Lines 77-82    Link Here 
77
BasicEnergySource::BasicEnergySource ()
83
BasicEnergySource::BasicEnergySource ()
78
{
84
{
79
  NS_LOG_FUNCTION (this);
85
  NS_LOG_FUNCTION (this);
86
  m_energyCapacityJ = 0;
80
  m_lastUpdateTime = Seconds (0.0);
87
  m_lastUpdateTime = Seconds (0.0);
81
  m_depleted = false;
88
  m_depleted = false;
82
}
89
}
 Lines 91-98    Link Here 
91
{
98
{
92
  NS_LOG_FUNCTION (this << initialEnergyJ);
99
  NS_LOG_FUNCTION (this << initialEnergyJ);
93
  NS_ASSERT (initialEnergyJ >= 0);
100
  NS_ASSERT (initialEnergyJ >= 0);
101
  NS_ASSERT (initialEnergyJ <= m_energyCapacityJ || m_energyCapacityJ == 0);
94
  m_initialEnergyJ = initialEnergyJ;
102
  m_initialEnergyJ = initialEnergyJ;
95
  m_remainingEnergyJ = m_initialEnergyJ;
103
  m_remainingEnergyJ = m_initialEnergyJ;
104
  if(m_energyCapacityJ == 0)
105
    {
106
      m_energyCapacityJ = m_initialEnergyJ;
107
    }
96
}
108
}
97
109
98
void
110
void
 Lines 145-151    Link Here 
145
  NS_LOG_FUNCTION (this);
157
  NS_LOG_FUNCTION (this);
146
  // update energy source to get the latest remaining energy.
158
  // update energy source to get the latest remaining energy.
147
  UpdateEnergySource ();
159
  UpdateEnergySource ();
148
  return m_remainingEnergyJ / m_initialEnergyJ;
160
  if(m_energyCapacityJ == 0)
161
    {
162
      return 0;
163
    }
164
  return m_remainingEnergyJ / m_energyCapacityJ;
149
}
165
}
150
166
151
void
167
void
 Lines 161-166    Link Here 
161
    }
177
    }
162
178
163
  m_energyUpdateEvent.Cancel ();
179
  m_energyUpdateEvent.Cancel ();
180
  
181
  if(m_energyCapacityJ == 0)
182
    {
183
      m_energyCapacityJ = m_initialEnergyJ;
184
    }
185
  if(m_initialEnergyJ == 0)
186
    {
187
      m_initialEnergyJ = m_energyCapacityJ;
188
    }
164
189
165
  CalculateRemainingEnergy ();
190
  CalculateRemainingEnergy ();
166
191
 Lines 183-188    Link Here 
183
                                             this);
208
                                             this);
184
}
209
}
185
210
211
void
212
BasicEnergySource::SetEnergyCapacity (double energyCapacityJ)
213
{
214
  NS_LOG_FUNCTION (this << energyCapacityJ);
215
  NS_ASSERT (energyCapacityJ >= 0);
216
  NS_ASSERT (energyCapacityJ >= m_initialEnergyJ || energyCapacityJ == 0);
217
  m_energyCapacityJ = energyCapacityJ;
218
  if(m_initialEnergyJ == 0)
219
    {
220
      m_initialEnergyJ = m_energyCapacityJ;
221
      m_remainingEnergyJ = m_initialEnergyJ;
222
    }
223
}
224
225
double
226
BasicEnergySource::GetEnergyCapacity () const
227
{
228
  NS_LOG_FUNCTION (this);
229
  return m_energyCapacityJ;
230
}
231
186
/*
232
/*
187
 * Private functions start here.
233
 * Private functions start here.
188
 */
234
 */
 Lines 226-240    Link Here 
226
  NS_ASSERT (duration.GetSeconds () >= 0);
272
  NS_ASSERT (duration.GetSeconds () >= 0);
227
  // energy = current * voltage * time
273
  // energy = current * voltage * time
228
  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
274
  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
229
275
  
230
  if (m_remainingEnergyJ < energyToDecreaseJ) 
276
  // handles consuming energy below the low battery threshold
277
  if (m_remainingEnergyJ <= m_energyCapacityJ * m_lowBatteryTh && energyToDecreaseJ > 0)
231
    {
278
    {
232
      m_remainingEnergyJ = 0; // energy never goes below 0
279
      energyToDecreaseJ = 0;
233
    } 
280
    }
234
  else 
281
  // handles recharging above the energy capacity
282
  if (m_remainingEnergyJ >= m_energyCapacityJ && energyToDecreaseJ < 0) 
235
    {
283
    {
236
      m_remainingEnergyJ -= energyToDecreaseJ;
284
      energyToDecreaseJ = 0;
237
    }  
285
    }
286
  m_remainingEnergyJ -= energyToDecreaseJ;
238
287
239
  NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
288
  NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
240
}
289
}
(-)a/src/energy/model/basic-energy-source.h (+19 lines)
 Lines 47-52    Link Here 
47
   * Implements GetInitialEnergy.
47
   * Implements GetInitialEnergy.
48
   */
48
   */
49
  virtual double GetInitialEnergy (void) const;
49
  virtual double GetInitialEnergy (void) const;
50
  
51
  /**
52
   * \return The source energy capacity, in Joules.
53
   *
54
   * Implements GetEnergyCapacity.
55
   */
56
  virtual double GetEnergyCapacity (void) const;
50
57
51
  /**
58
  /**
52
   * \returns Supply voltage at the energy source.
59
   * \returns Supply voltage at the energy source.
 Lines 80-87    Link Here 
80
   * Sets initial energy stored in the energy source. Note that initial energy
87
   * Sets initial energy stored in the energy source. Note that initial energy
81
   * is assumed to be set before simulation starts and is set only once per
88
   * is assumed to be set before simulation starts and is set only once per
82
   * simulation.
89
   * simulation.
90
   * Note that if the initial energy is not set, it is assumed to be equal to 
91
   * the source energy capacity.
83
   */
92
   */
84
  void SetInitialEnergy (double initialEnergyJ);
93
  void SetInitialEnergy (double initialEnergyJ);
94
  
95
  /**
96
   * \param The source energy capacity, in Joules.
97
   *
98
   * Sets the energy capacity of the source.
99
   * Note that if the energy capacity is not set, it is assumed to be equal to
100
   * the initial energy.
101
   */
102
  void SetEnergyCapacity (double energyCapacityJ);
85
103
86
  /**
104
  /**
87
   * \param supplyVoltageV Supply voltage at the energy source, in Volts.
105
   * \param supplyVoltageV Supply voltage at the energy source, in Volts.
 Lines 137-142    Link Here 
137
155
138
private:
156
private:
139
  double m_initialEnergyJ;                // initial energy, in Joules
157
  double m_initialEnergyJ;                // initial energy, in Joules
158
  double m_energyCapacityJ;               // energy capacity, in Joules
140
  double m_supplyVoltageV;                // supply voltage, in Volts
159
  double m_supplyVoltageV;                // supply voltage, in Volts
141
  double m_lowBatteryTh;                  // low battery threshold, as a fraction of the initial energy
160
  double m_lowBatteryTh;                  // low battery threshold, as a fraction of the initial energy
142
  double m_highBatteryTh;                 // high battery threshold, as a fraction of the initial energy
161
  double m_highBatteryTh;                 // high battery threshold, as a fraction of the initial energy
(-)a/src/energy/model/energy-source.h (+8 lines)
 Lines 100-105    Link Here 
100
   * with a fixed initial energy (energy capacity), set method is not needed.
100
   * with a fixed initial energy (energy capacity), set method is not needed.
101
   */
101
   */
102
  virtual double GetInitialEnergy (void) const = 0;
102
  virtual double GetInitialEnergy (void) const = 0;
103
  
104
  /**
105
   * \returns Energy capacity of the energy source.
106
   *
107
   * Set method is to be defined in child class only if necessary. For sources
108
   * with a fixed energy capacity, set method is not needed.
109
   */
110
  virtual double GetEnergyCapacity (void) const = 0;
103
111
104
  /**
112
  /**
105
   * \returns Remaining energy at the energy source.
113
   * \returns Remaining energy at the energy source.
(-)a/src/energy/model/li-ion-energy-source.cc (-9 / +58 lines)
 Lines 41-51    Link Here 
41
    .SetGroupName ("Energy")
41
    .SetGroupName ("Energy")
42
    .AddConstructor<LiIonEnergySource> ()
42
    .AddConstructor<LiIonEnergySource> ()
43
    .AddAttribute ("LiIonEnergySourceInitialEnergyJ",
43
    .AddAttribute ("LiIonEnergySourceInitialEnergyJ",
44
                   "Initial energy stored in basic energy source.",
44
                   "Initial energy stored in LiIon energy source.",
45
                   DoubleValue (31752.0),  // in Joules
45
                   DoubleValue (0),  // in Joules
46
                   MakeDoubleAccessor (&LiIonEnergySource::SetInitialEnergy,
46
                   MakeDoubleAccessor (&LiIonEnergySource::SetInitialEnergy,
47
                                       &LiIonEnergySource::GetInitialEnergy),
47
                                       &LiIonEnergySource::GetInitialEnergy),
48
                   MakeDoubleChecker<double> ())
48
                   MakeDoubleChecker<double> ())
49
    .AddAttribute ("LiIonEnergySourceCapacityJ",
50
                   "Energy capacity that can be stored in LiIon energy source.",
51
                   DoubleValue (0),  // in Joules
52
                   MakeDoubleAccessor (&LiIonEnergySource::SetEnergyCapacity,
53
                                       &LiIonEnergySource::GetEnergyCapacity),
54
                   MakeDoubleChecker<double> ())
49
    .AddAttribute ("LiIonEnergyLowBatteryThreshold",
55
    .AddAttribute ("LiIonEnergyLowBatteryThreshold",
50
                   "Low battery threshold for LiIon energy source.",
56
                   "Low battery threshold for LiIon energy source.",
51
                   DoubleValue (0.10), // as a fraction of the initial energy
57
                   DoubleValue (0.10), // as a fraction of the initial energy
 Lines 112-118    Link Here 
112
}
118
}
113
119
114
LiIonEnergySource::LiIonEnergySource ()
120
LiIonEnergySource::LiIonEnergySource ()
115
  : m_drainedCapacity (0.0),
121
  : m_energyCapacityJ (0.0),
122
    m_drainedCapacity (0.0),
116
    m_lastUpdateTime (Seconds (0.0))
123
    m_lastUpdateTime (Seconds (0.0))
117
{
124
{
118
  NS_LOG_FUNCTION (this);
125
  NS_LOG_FUNCTION (this);
 Lines 128-136    Link Here 
128
{
135
{
129
  NS_LOG_FUNCTION (this << initialEnergyJ);
136
  NS_LOG_FUNCTION (this << initialEnergyJ);
130
  NS_ASSERT (initialEnergyJ >= 0);
137
  NS_ASSERT (initialEnergyJ >= 0);
138
  NS_ASSERT (initialEnergyJ <= m_energyCapacityJ || m_energyCapacityJ == 0);
131
  m_initialEnergyJ = initialEnergyJ;
139
  m_initialEnergyJ = initialEnergyJ;
132
  // set remaining energy to be initial energy
140
  // set remaining energy to be initial energy
133
  m_remainingEnergyJ = m_initialEnergyJ;
141
  m_remainingEnergyJ = m_initialEnergyJ;
142
  if(m_energyCapacityJ == 0)
143
    {
144
      m_energyCapacityJ = m_initialEnergyJ;
145
    }
134
}
146
}
135
147
136
double
148
double
 Lines 184-189    Link Here 
184
  NS_LOG_FUNCTION (this);
196
  NS_LOG_FUNCTION (this);
185
  // update energy source to get the latest remaining energy.
197
  // update energy source to get the latest remaining energy.
186
  UpdateEnergySource ();
198
  UpdateEnergySource ();
199
  if(m_energyCapacityJ == 0)
200
    {
201
      return 0;
202
    }
187
  return m_remainingEnergyJ / m_initialEnergyJ;
203
  return m_remainingEnergyJ / m_initialEnergyJ;
188
}
204
}
189
205
 Lines 223-228    Link Here 
223
    }
239
    }
224
240
225
  m_energyUpdateEvent.Cancel ();
241
  m_energyUpdateEvent.Cancel ();
242
  
243
  if(m_energyCapacityJ == 0)
244
    {
245
      m_energyCapacityJ = m_initialEnergyJ;
246
    }
247
  if(m_initialEnergyJ == 0)
248
    {
249
      m_initialEnergyJ = m_energyCapacityJ;
250
    }
226
251
227
  CalculateRemainingEnergy ();
252
  CalculateRemainingEnergy ();
228
253
 Lines 239-244    Link Here 
239
                                             this);
264
                                             this);
240
}
265
}
241
266
267
void
268
LiIonEnergySource::SetEnergyCapacity (double energyCapacityJ)
269
{
270
  NS_LOG_FUNCTION (this << energyCapacityJ);
271
  NS_ASSERT (energyCapacityJ >= 0);
272
  NS_ASSERT (energyCapacityJ >= m_initialEnergyJ || energyCapacityJ == 0);
273
  m_energyCapacityJ = energyCapacityJ;
274
  if(m_initialEnergyJ == 0)
275
    {
276
      m_initialEnergyJ = m_energyCapacityJ;
277
      m_remainingEnergyJ = m_initialEnergyJ;
278
    }
279
}
280
281
double
282
LiIonEnergySource::GetEnergyCapacity () const
283
{
284
  NS_LOG_FUNCTION (this);
285
  return m_energyCapacityJ;
286
}
287
242
/*
288
/*
243
 * Private functions start here.
289
 * Private functions start here.
244
 */
290
 */
 Lines 277-290    Link Here 
277
  // energy = current * voltage * time
323
  // energy = current * voltage * time
278
  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
324
  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
279
325
280
  if (m_remainingEnergyJ < energyToDecreaseJ) 
326
  // handles consuming energy below the low battery threshold
327
  if (m_remainingEnergyJ <= m_energyCapacityJ * m_lowBatteryTh && energyToDecreaseJ > 0)
281
    {
328
    {
282
      m_remainingEnergyJ = 0; // energy never goes below 0
329
      energyToDecreaseJ = 0;
283
    } 
330
    }
284
  else 
331
  // handles recharging above the energy capacity
332
  if (m_remainingEnergyJ >= m_energyCapacityJ && energyToDecreaseJ < 0) 
285
    {
333
    {
286
      m_remainingEnergyJ -= energyToDecreaseJ;
334
      energyToDecreaseJ = 0;
287
    }  
335
    }
336
  m_remainingEnergyJ -= energyToDecreaseJ;
288
337
289
  m_drainedCapacity += (totalCurrentA * duration.GetSeconds () / 3600);
338
  m_drainedCapacity += (totalCurrentA * duration.GetSeconds () / 3600);
290
  // update the supply voltage
339
  // update the supply voltage
(-)a/src/energy/model/li-ion-energy-source.h (+17 lines)
 Lines 90-95    Link Here 
90
   * before simulation starts and is set only once per simulation.
90
   * before simulation starts and is set only once per simulation.
91
   */
91
   */
92
  void SetInitialEnergy (double initialEnergyJ);
92
  void SetInitialEnergy (double initialEnergyJ);
93
  
94
  /**
95
   * \return The source energy capacity, in Joules.
96
   *
97
   * Implements GetEnergyCapacity.
98
   */
99
  virtual double GetEnergyCapacity (void) const;
100
  
101
  /**
102
   * \param The source energy capacity, in Joules.
103
   *
104
   * Sets the energy capacity of the source.
105
   * Note that if the energy capacity is not set, it is assumed to be equal to
106
   * the initial energy.
107
   */
108
  void SetEnergyCapacity (double energyCapacityJ);
93
109
94
  /**
110
  /**
95
   * \returns Supply voltage at the energy source.
111
   * \returns Supply voltage at the energy source.
 Lines 182-187    Link Here 
182
198
183
private:
199
private:
184
  double m_initialEnergyJ;                // initial energy, in Joules
200
  double m_initialEnergyJ;                // initial energy, in Joules
201
  double m_energyCapacityJ;               // energy capacity, in Joules
185
  TracedValue<double> m_remainingEnergyJ; // remaining energy, in Joules
202
  TracedValue<double> m_remainingEnergyJ; // remaining energy, in Joules
186
  double m_drainedCapacity;               // capacity drained from the cell, in Ah
203
  double m_drainedCapacity;               // capacity drained from the cell, in Ah
187
  double m_supplyVoltageV;                // actual voltage of the cell
204
  double m_supplyVoltageV;                // actual voltage of the cell
(-)a/src/energy/model/rv-battery-model.cc (+7 lines)
 Lines 115-120    Link Here 
115
}
115
}
116
116
117
double
117
double
118
RvBatteryModel::GetEnergyCapacity (void) const
119
{
120
  NS_LOG_FUNCTION (this);
121
  return m_alpha * GetSupplyVoltage ();
122
}
123
124
double
118
RvBatteryModel::GetSupplyVoltage (void) const
125
RvBatteryModel::GetSupplyVoltage (void) const
119
{
126
{
120
  NS_LOG_FUNCTION (this);
127
  NS_LOG_FUNCTION (this);
(-)a/src/energy/model/rv-battery-model.h (+7 lines)
 Lines 60-65    Link Here 
60
   * Implements GetInitialEnergy.
60
   * Implements GetInitialEnergy.
61
   */
61
   */
62
  virtual double GetInitialEnergy (void) const;
62
  virtual double GetInitialEnergy (void) const;
63
  
64
  /**
65
   * \return The source energy capacity, in Joules.
66
   *
67
   * Implements GetEnergyCapacity.
68
   */
69
  virtual double GetEnergyCapacity (void) const;
63
70
64
  /**
71
  /**
65
   * \returns Supply voltage at the energy source.
72
   * \returns Supply voltage at the energy source.

Return to bug 2403