Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. | 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 m_lifetime = Seconds (0.0); | 92 m_lifetime = Seconds (0.0); |
93 } | 93 } |
94 | 94 |
95 RvBatteryModel::~RvBatteryModel () | 95 RvBatteryModel::~RvBatteryModel () |
96 { | 96 { |
97 } | 97 } |
98 | 98 |
99 double | 99 double |
100 RvBatteryModel::GetInitialEnergy (void) const | 100 RvBatteryModel::GetInitialEnergy (void) const |
101 { | 101 { |
102 NS_LOG_FUNCTION (this); | |
Mathieu Lacage
2010/10/19 11:56:08
in general, adding logging code to Get methods mer
(Tony) He Wu
2010/10/19 18:44:17
Will remove them in next patch.
| |
103 return m_alpha * GetSupplyVoltage (); | 102 return m_alpha * GetSupplyVoltage (); |
104 } | 103 } |
105 | 104 |
106 double | 105 double |
107 RvBatteryModel::GetSupplyVoltage (void) const | 106 RvBatteryModel::GetSupplyVoltage (void) const |
108 { | 107 { |
109 NS_LOG_FUNCTION (this); | |
110 // average of Voc and Vcutoff | 108 // average of Voc and Vcutoff |
111 return (m_openCircuitVoltage - m_cutoffVoltage) / 2 + m_cutoffVoltage; | 109 return (m_openCircuitVoltage - m_cutoffVoltage) / 2 + m_cutoffVoltage; |
112 } | 110 } |
113 | 111 |
114 double | 112 double |
115 RvBatteryModel::GetRemainingEnergy (void) | 113 RvBatteryModel::GetRemainingEnergy (void) |
116 { | 114 { |
117 NS_LOG_FUNCTION (this); | 115 NS_LOG_FUNCTION (this); |
118 UpdateEnergySource (); | 116 UpdateEnergySource (); |
119 return m_alpha * GetSupplyVoltage () * m_batteryLevel; | 117 return m_alpha * GetSupplyVoltage () * m_batteryLevel; |
120 } | 118 } |
121 | 119 |
122 double | 120 double |
123 RvBatteryModel::GetEnergyFraction (void) | 121 RvBatteryModel::GetEnergyFraction (void) |
124 { | 122 { |
125 NS_LOG_FUNCTION (this); | |
126 return GetBatteryLevel (); | 123 return GetBatteryLevel (); |
127 } | |
128 | |
129 void | |
130 RvBatteryModel::DecreaseRemainingEnergy (double energyJ) | |
131 { | |
132 NS_LOG_FUNCTION (this); | |
133 NS_FATAL_ERROR ("RvBatteryModel: Can not decrease energy directly!"); | |
134 } | |
135 | |
136 void | |
137 RvBatteryModel::IncreaseRemainingEnergy (double energyJ) | |
138 { | |
139 NS_LOG_FUNCTION (this); | |
140 NS_FATAL_ERROR ("RvBatteryModel: Can not increase energy directly!"); | |
141 } | 124 } |
142 | 125 |
143 void | 126 void |
144 RvBatteryModel::UpdateEnergySource (void) | 127 RvBatteryModel::UpdateEnergySource (void) |
145 { | 128 { |
146 NS_LOG_FUNCTION (this); | 129 NS_LOG_FUNCTION (this); |
147 | 130 |
148 // do not update if battery is already dead | 131 // do not update if battery is already dead |
149 if (m_batteryLevel <= 0) | 132 if (m_batteryLevel <= 0) |
150 { | 133 { |
151 NS_LOG_DEBUG ("RvBatteryModel:Battery is dead at node #" << | 134 NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!"); |
152 GetNode ()->GetId ()); | |
153 return; | 135 return; |
154 } | 136 } |
155 | 137 |
156 // do not update if simulation has finished | 138 // do not update if simulation has finished |
157 if (Simulator::IsFinished ()) | 139 if (Simulator::IsFinished ()) |
158 { | 140 { |
159 return; | 141 return; |
160 } | 142 } |
161 | 143 |
162 NS_LOG_DEBUG ("RvBatteryModel:Updating remaining energy at node #" << | 144 NS_LOG_DEBUG ("RvBatteryModel:Updating remaining energy!"); |
163 GetNode ()->GetId ()); | |
164 | 145 |
165 m_currentSampleEvent.Cancel (); | 146 m_currentSampleEvent.Cancel (); |
166 | 147 |
167 double currentLoad = CalculateTotalCurrent () * 1000; // must be in mA | 148 double currentLoad = CalculateTotalCurrent () * 1000; // must be in mA |
168 double calculatedAlpha = Discharge (currentLoad, Simulator::Now ()); | 149 double calculatedAlpha = Discharge (currentLoad, Simulator::Now ()); |
169 | 150 |
170 NS_LOG_DEBUG ("RvBatteryModel:Calculated alpha = " << calculatedAlpha << | 151 NS_LOG_DEBUG ("RvBatteryModel:Calculated alpha = " << calculatedAlpha << |
171 " time = " << Simulator::Now ().GetSeconds ()); | 152 " time = " << Simulator::Now ().GetSeconds ()); |
172 | 153 |
173 // calculate battery level | 154 // calculate battery level |
174 m_batteryLevel = 1 - (calculatedAlpha / m_alpha); | 155 m_batteryLevel = 1 - (calculatedAlpha / m_alpha); |
175 if (m_batteryLevel < 0) | 156 if (m_batteryLevel < 0) |
176 { | 157 { |
177 m_batteryLevel = 0; | 158 m_batteryLevel = 0; |
178 } | 159 } |
179 | 160 |
180 NS_LOG_DEBUG ("RvBatteryModel:Battery level is " << m_batteryLevel << | |
181 " at node #" << GetNode ()->GetId () << " at time = " << | |
182 Simulator::Now ().GetSeconds () << "s"); | |
183 | |
184 // check if battery is dead. | 161 // check if battery is dead. |
185 if (calculatedAlpha >= m_alpha) | 162 if (calculatedAlpha >= m_alpha) |
186 { | 163 { |
187 m_lifetime = Simulator::Now (); | 164 m_lifetime = Simulator::Now (); |
188 NS_LOG_DEBUG ("RvBatteryModel:Battery is dead at node #" << | 165 NS_LOG_DEBUG ("RvBatteryModel:Battery is dead!"); |
189 GetNode ()->GetId () << " at time = " << | |
190 m_lifetime.Get ().GetSeconds () << "s"); | |
191 HandleEnergyDrainedEvent (); | 166 HandleEnergyDrainedEvent (); |
192 return; // stop periodic sampling | 167 return; // stop periodic sampling |
193 } | 168 } |
194 | 169 |
195 m_previousLoad = currentLoad; | 170 m_previousLoad = currentLoad; |
196 m_lastSampleTime = Simulator::Now (); | 171 m_lastSampleTime = Simulator::Now (); |
197 m_currentSampleEvent = Simulator::Schedule (m_samplingInterval, | 172 m_currentSampleEvent = Simulator::Schedule (m_samplingInterval, |
198 &RvBatteryModel::UpdateEnergySourc e, | 173 &RvBatteryModel::UpdateEnergySourc e, |
199 this); | 174 this); |
200 } | 175 } |
201 | 176 |
202 void | 177 void |
203 RvBatteryModel::SetSamplingInterval (Time interval) | 178 RvBatteryModel::SetSamplingInterval (Time interval) |
204 { | 179 { |
205 NS_LOG_FUNCTION (this << interval); | 180 NS_LOG_FUNCTION (this << interval); |
206 m_samplingInterval = interval; | 181 m_samplingInterval = interval; |
207 } | 182 } |
208 | 183 |
209 Time | 184 Time |
210 RvBatteryModel::GetSamplingInterval (void) const | 185 RvBatteryModel::GetSamplingInterval (void) const |
211 { | 186 { |
212 NS_LOG_FUNCTION (this); | |
213 return m_samplingInterval; | 187 return m_samplingInterval; |
214 } | 188 } |
215 | 189 |
216 void | 190 void |
217 RvBatteryModel::SetOpenCircuitVoltage (double voltage) | 191 RvBatteryModel::SetOpenCircuitVoltage (double voltage) |
218 { | 192 { |
219 NS_LOG_FUNCTION (this << voltage); | 193 NS_LOG_FUNCTION (this << voltage); |
220 NS_ASSERT (voltage >= 0); | 194 NS_ASSERT (voltage >= 0); |
221 m_openCircuitVoltage = voltage; | 195 m_openCircuitVoltage = voltage; |
222 } | 196 } |
223 | 197 |
224 double | 198 double |
225 RvBatteryModel::GetOpenCircuitVoltage (void) const | 199 RvBatteryModel::GetOpenCircuitVoltage (void) const |
226 { | 200 { |
227 NS_LOG_FUNCTION (this); | |
228 return m_openCircuitVoltage; | 201 return m_openCircuitVoltage; |
229 } | 202 } |
230 | 203 |
231 void | 204 void |
232 RvBatteryModel::SetCutoffVoltage (double voltage) | 205 RvBatteryModel::SetCutoffVoltage (double voltage) |
233 { | 206 { |
234 NS_LOG_FUNCTION (this << voltage); | 207 NS_LOG_FUNCTION (this << voltage); |
235 NS_ASSERT (voltage <= m_openCircuitVoltage); | 208 NS_ASSERT (voltage <= m_openCircuitVoltage); |
236 m_cutoffVoltage = voltage; | 209 m_cutoffVoltage = voltage; |
237 } | 210 } |
238 | 211 |
239 double | 212 double |
240 RvBatteryModel::GetCutoffVoltage (void) const | 213 RvBatteryModel::GetCutoffVoltage (void) const |
241 { | 214 { |
242 NS_LOG_FUNCTION (this); | |
243 return m_cutoffVoltage; | 215 return m_cutoffVoltage; |
244 } | 216 } |
245 | 217 |
246 void | 218 void |
247 RvBatteryModel::SetAlpha (double alpha) | 219 RvBatteryModel::SetAlpha (double alpha) |
248 { | 220 { |
249 NS_LOG_FUNCTION (this << alpha); | 221 NS_LOG_FUNCTION (this << alpha); |
250 NS_ASSERT (alpha >= 0); | 222 NS_ASSERT (alpha >= 0); |
251 m_alpha = alpha; | 223 m_alpha = alpha; |
252 } | 224 } |
253 | 225 |
254 double | 226 double |
255 RvBatteryModel::GetAlpha (void) const | 227 RvBatteryModel::GetAlpha (void) const |
256 { | 228 { |
257 NS_LOG_FUNCTION (this); | |
258 return m_alpha; | 229 return m_alpha; |
259 } | 230 } |
260 | 231 |
261 void | 232 void |
262 RvBatteryModel::SetBeta (double beta) | 233 RvBatteryModel::SetBeta (double beta) |
263 { | 234 { |
264 NS_LOG_FUNCTION (this << beta); | 235 NS_LOG_FUNCTION (this << beta); |
265 NS_ASSERT (beta >= 0); | 236 NS_ASSERT (beta >= 0); |
266 m_beta = beta; | 237 m_beta = beta; |
267 } | 238 } |
268 | 239 |
269 double | 240 double |
270 RvBatteryModel::GetBeta (void) const | 241 RvBatteryModel::GetBeta (void) const |
271 { | 242 { |
272 NS_LOG_FUNCTION (this); | |
273 return m_beta; | 243 return m_beta; |
274 } | 244 } |
275 | 245 |
276 double | 246 double |
277 RvBatteryModel::GetBatteryLevel (void) | 247 RvBatteryModel::GetBatteryLevel (void) |
278 { | 248 { |
279 NS_LOG_FUNCTION (this); | 249 NS_LOG_FUNCTION (this); |
280 UpdateEnergySource (); | 250 UpdateEnergySource (); |
281 return m_batteryLevel; | 251 return m_batteryLevel; |
282 } | 252 } |
283 | 253 |
284 Time | 254 Time |
285 RvBatteryModel::GetLifetime (void) const | 255 RvBatteryModel::GetLifetime (void) const |
286 { | 256 { |
287 NS_LOG_FUNCTION (this); | |
288 return m_lifetime; | 257 return m_lifetime; |
289 } | 258 } |
290 | 259 |
291 void | 260 void |
292 RvBatteryModel::SetNumOfTerms (int num) | 261 RvBatteryModel::SetNumOfTerms (int num) |
293 { | 262 { |
294 NS_LOG_FUNCTION (this); | 263 NS_LOG_FUNCTION (this << num); |
295 m_numOfTerms = num; | 264 m_numOfTerms = num; |
296 } | 265 } |
297 | 266 |
298 int | 267 int |
299 RvBatteryModel::GetNumOfTerms (void) const | 268 RvBatteryModel::GetNumOfTerms (void) const |
300 { | 269 { |
301 NS_LOG_FUNCTION (this); | |
302 return m_numOfTerms; | 270 return m_numOfTerms; |
303 } | 271 } |
304 | 272 |
305 /* | 273 /* |
306 * Private functions start here. | 274 * Private functions start here. |
307 */ | 275 */ |
308 | 276 |
309 void | 277 void |
310 RvBatteryModel::DoStart (void) | 278 RvBatteryModel::DoStart (void) |
311 { | 279 { |
280 NS_LOG_DEBUG ("RvBatteryModel:Starting battery level update!"); | |
281 UpdateEnergySource (); // start periodic sampling of load (total current) | |
282 } | |
283 | |
284 void | |
285 RvBatteryModel::DoDispose (void) | |
286 { | |
287 BreakDeviceEnergyModelRefCycle (); // break reference cycle | |
288 } | |
289 | |
290 void | |
291 RvBatteryModel::HandleEnergyDrainedEvent (void) | |
292 { | |
312 NS_LOG_FUNCTION (this); | 293 NS_LOG_FUNCTION (this); |
313 NS_LOG_DEBUG ("RvBatteryModel:Starting battery level update at node #" << | 294 NS_LOG_DEBUG ("RvBatteryModel:Energy depleted!"); |
314 GetNode ()->GetId ()); | |
315 UpdateEnergySource (); // start periodic sampling of load (total current) | |
316 } | |
317 | |
318 void | |
319 RvBatteryModel::DoDispose (void) | |
320 { | |
321 NS_LOG_FUNCTION (this); | |
322 BreakDeviceEnergyModelRefCycle (); // break reference cycle | |
323 } | |
324 | |
325 void | |
326 RvBatteryModel::HandleEnergyDrainedEvent (void) | |
327 { | |
328 NS_LOG_FUNCTION (this); | |
329 NS_LOG_DEBUG ("RvBatteryModel:Energy depleted at node #" << | |
330 GetNode ()->GetId ()); | |
331 NotifyEnergyDrained (); // notify DeviceEnergyModel objects | 295 NotifyEnergyDrained (); // notify DeviceEnergyModel objects |
332 } | 296 } |
333 | 297 |
334 double | 298 double |
335 RvBatteryModel::Discharge (double load, Time t) | 299 RvBatteryModel::Discharge (double load, Time t) |
336 { | 300 { |
337 NS_LOG_FUNCTION (this << load << t); | 301 NS_LOG_FUNCTION (this << load << t); |
338 | 302 |
339 // record only when load changes | 303 // record only when load changes |
340 if (load != m_previousLoad) | 304 if (load != m_previousLoad) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 | 344 |
381 return calculatedAlpha; | 345 return calculatedAlpha; |
382 } | 346 } |
383 | 347 |
384 double | 348 double |
385 RvBatteryModel::RvModelAFunction (Time t, Time sk, Time sk_1, double beta) | 349 RvBatteryModel::RvModelAFunction (Time t, Time sk, Time sk_1, double beta) |
386 { | 350 { |
387 NS_LOG_FUNCTION (this << t << sk << sk_1 << beta); | 351 NS_LOG_FUNCTION (this << t << sk << sk_1 << beta); |
388 | 352 |
389 // everything is in minutes | 353 // everything is in minutes |
390 double firstDelta = (t.GetSeconds () - sk.GetSeconds ()) / 60; | 354 double firstDelta = (t.GetSeconds () - sk.GetSeconds ()) / 60; |
Mathieu Lacage
2010/10/19 11:56:08
how about adding a Time::GetMinutes method instead
(Tony) He Wu
2010/10/19 18:44:17
Adding a GetMinutes method will be better.
To add
| |
391 double secondDelta = (t.GetSeconds () - sk_1.GetSeconds ()) / 60; | 355 double secondDelta = (t.GetSeconds () - sk_1.GetSeconds ()) / 60; |
392 double delta = (sk.GetSeconds () - sk_1.GetSeconds ()) / 60; | 356 double delta = (sk.GetSeconds () - sk_1.GetSeconds ()) / 60; |
393 | 357 |
394 double sum = 0.0; | 358 double sum = 0.0; |
395 for (int m = 1; m <= m_numOfTerms; m++) | 359 for (int m = 1; m <= m_numOfTerms; m++) |
396 { | 360 { |
397 double square = beta * beta * m * m; | 361 double square = beta * beta * m * m; |
398 sum += (exp (- square * (firstDelta)) - exp (- square * (secondDelta))) / square; | 362 sum += (exp (- square * (firstDelta)) - exp (- square * (secondDelta))) / square; |
399 } | 363 } |
400 return delta + 2 * sum; | 364 return delta + 2 * sum; |
401 } | 365 } |
402 | 366 |
403 } // namespace ns3 | 367 } // namespace ns3 |
LEFT | RIGHT |