A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
generic-battery-discharge-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Tokushima University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
7 */
8
9#include <ns3/core-module.h>
10#include <ns3/energy-module.h>
11#include <ns3/gnuplot.h>
12
13#include <fstream>
14#include <sstream>
15#include <string>
16
17using namespace ns3;
18using namespace ns3::energy;
19
20/**
21 * This example shows the use of batteries in ns-3.
22 * 5 batteries of different chemistries are discharged
23 * using a constant current. Batteries can be configured
24 * manually using the necessary parameters or using
25 * presets.
26 *
27 * In this example, only the first battery uses parameters
28 * to form a NiMh battery. The rest of the batteries in this
29 * example use defined presets with already tested parameters.
30 *
31 * Users can make their own battery presets by setting
32 * the necessary parameters as in the example in the first
33 * battery.
34 *
35 * Plot files are produced as a result of this example.
36 * Graphs can be obtained from the plot using:
37 * \code{.sh}
38 $> gnuplot <plotname>.plt
39 \endcode
40 *
41 */
42
43Gnuplot battDischPlot1 = Gnuplot("BattDisch1.eps");
45std::ofstream battDischFile1("BattDischCurve1.plt");
46
47Gnuplot battDischPlot2 = Gnuplot("BattDisch2.eps");
49std::ofstream battDischFile2("BattDischCurve2.plt");
50
51Gnuplot battDischPlot3 = Gnuplot("BattDisch3.eps");
53std::ofstream battDischFile3("BattDischCurve3.plt");
54
55Gnuplot battDischPlot4 = Gnuplot("BattDisch4.eps");
57std::ofstream battDischFile4("BattDischCurve4.plt");
58
59Gnuplot battDischPlot5 = Gnuplot("BattDisch5.eps");
61std::ofstream battDischFile5("BattDischCurve5.plt");
62
63void
65{
66 // NiMh battery Panasonic HHR650D NiMH
67 double cellVoltage = es->GetSupplyVoltage();
68 Time currentTime = Simulator::Now();
69 battDischDataset1.Add(currentTime.GetMinutes(), cellVoltage);
70 // battDischDataset1.Add(currentTime.GetHours(), cellVoltage);
71
73 {
75 }
76}
77
78void
80{
81 // CSB GP1272 Lead Acid
82 double cellVoltage = es->GetSupplyVoltage();
83 Time currentTime = Simulator::Now();
84 battDischDataset2.Add(currentTime.GetMinutes(), cellVoltage);
85 // battDischDataset2.Add(currentTime.GetHours(), cellVoltage);
86
88 {
90 }
91}
92
93void
95{
96 // Panasonic CGR18650DA Li-on
97 double cellVoltage = es->GetSupplyVoltage();
98 double dischargeCapacityAh = es->GetDrainedCapacity();
99 battDischDataset3.Add(dischargeCapacityAh * 1000, cellVoltage);
100
102 {
104 }
105}
106
107void
109{
110 // Rs Pro LGP12100 Lead Acid
111 double cellVoltage = es->GetSupplyVoltage();
112 Time currentTime = Simulator::Now();
113 battDischDataset4.Add(currentTime.GetMinutes(), cellVoltage);
114 // battDischDataset4.Add(currentTime.GetHours(), cellVoltage);
115
117 {
119 }
120}
121
122void
124{
125 // Panasonic N-700AAC NiCd
126 double cellVoltage = es->GetSupplyVoltage();
127 Time currentTime = Simulator::Now();
128 // battDischDataset5.Add(currentTime.GetMinutes(), cellVoltage);
129 battDischDataset5.Add(currentTime.GetHours(), cellVoltage);
130
132 {
134 }
135}
136
137int
138main(int argc, char** argv)
139{
140 CommandLine cmd(__FILE__);
141 cmd.Parse(argc, argv);
142
143 LogComponentEnable("GenericBatteryModel", LOG_LEVEL_DEBUG);
144
145 Ptr<Node> node;
146 GenericBatteryModelHelper batteryHelper;
147 Ptr<GenericBatteryModel> batteryModel;
148 Ptr<SimpleDeviceEnergyModel> devicesEnergyModel;
149
150 //////////////////////// PANASONIC HHR650D NiMH discharge 1C,2C,5C ////////////////////
151
152 // Discharge 6.5A (1C)
153 battDischDataset1 = Gnuplot2dDataset("Panasonic NiMH HHR650D 6.5 A (1C)");
154
155 node = CreateObject<Node>();
156 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
157 batteryModel = CreateObject<GenericBatteryModel>();
158
159 batteryModel->SetAttribute("FullVoltage", DoubleValue(1.39)); // Vfull
160 batteryModel->SetAttribute("MaxCapacity", DoubleValue(7.0)); // Q
161
162 batteryModel->SetAttribute("NominalVoltage", DoubleValue(1.18)); // Vnom
163 batteryModel->SetAttribute("NominalCapacity", DoubleValue(6.25)); // QNom
164
165 batteryModel->SetAttribute("ExponentialVoltage", DoubleValue(1.28)); // Vexp
166 batteryModel->SetAttribute("ExponentialCapacity", DoubleValue(1.3)); // Qexp
167
168 batteryModel->SetAttribute("InternalResistance", DoubleValue(0.0046)); // R
169 batteryModel->SetAttribute("TypicalDischargeCurrent", DoubleValue(1.3)); // i typical
170 batteryModel->SetAttribute("CutoffVoltage", DoubleValue(1.0)); // End of charge.
171
172 // Capacity Ah(qMax) * (Vfull) voltage * 3600 = (7 * 1.39 * 3.6) = 35028
173 batteryModel->SetAttribute("BatteryType", EnumValue(NIMH_NICD)); // Battery type
174
175 // The Generic battery model allow users to simulate different types of
176 // batteries based on some parameters. However, presets of batteries are
177 // included in ns-3, for example, the previous battery values can be
178 // configured using a helper to set a NiMh battery preset:
179
180 // batteryModel = DynamicCast<GenericBatteryModel>
181 // (batteryHelper.Install(node,PANASONIC_HHR650D_NIMH));
182
183 devicesEnergyModel->SetEnergySource(batteryModel);
184 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
185 devicesEnergyModel->SetNode(node);
186
187 devicesEnergyModel->SetCurrentA(6.5);
188
189 GraphBattery1(batteryModel);
190
192 // 18717 secs around 5.3hrs, 750secs for 32.5 current, or (4200 70 mins)
196
197 // Discharge 13A (2C)
198 battDischDataset1 = Gnuplot2dDataset("Panasonic NiMH HHR650D 13 A (2C)");
199 node = CreateObject<Node>();
200 batteryModel =
202
203 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
204 devicesEnergyModel->SetEnergySource(batteryModel);
205 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
206 devicesEnergyModel->SetNode(node);
207
208 devicesEnergyModel->SetCurrentA(13);
209
210 GraphBattery1(batteryModel);
211
216
217 // Discharge 32.5A (5C)
218 battDischDataset1 = Gnuplot2dDataset("Panasonic NiMH HHR650D 32.5 A (5C)");
219 node = CreateObject<Node>();
220 batteryModel = CreateObject<GenericBatteryModel>();
221 batteryModel =
223
224 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
225 devicesEnergyModel->SetEnergySource(batteryModel);
226 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
227 devicesEnergyModel->SetNode(node);
228
229 devicesEnergyModel->SetCurrentA(32.5);
230
231 GraphBattery1(batteryModel);
233
237
238 battDischPlot1.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
239 battDischPlot1.SetLegend(" Time (minutes)", "Voltage (V)");
240 battDischPlot1.SetExtra("set xrange[0:70]\n\
241 set yrange [0.8:1.8]\n\
242 set xtics 10\n\
243 set ytics 0.1\n\
244 set grid\n\
245 set style line 1 linewidth 5\n\
246 set style line 2 linewidth 5\n\
247 set style line 3 linewidth 5\n\
248 set style line 4 linewidth 5\n\
249 set style line 5 linewidth 5\n\
250 set style line 6 linewidth 5\n\
251 set style line 7 linewidth 5\n\
252 set style line 8 linewidth 5\n\
253 set style increment user\n\
254 set key reverse Left");
255
257 battDischFile1.close();
258 std::cout << "The end, plotting now\n";
259
260 //////////////////////// CSB GP1272 Lead Acid discharge 0.5C, 0.9C ////////////
261
262 // Discharge 0.36A (0.05C)
263 battDischDataset2 = Gnuplot2dDataset("CSB GP1272 0.36 A (0.05C)");
264 node = CreateObject<Node>();
265 batteryModel =
267
268 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
269 devicesEnergyModel->SetEnergySource(batteryModel);
270 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
271 devicesEnergyModel->SetNode(node);
272
273 devicesEnergyModel->SetCurrentA(0.36);
274
275 GraphBattery2(batteryModel);
277
278 Simulator::Stop(Seconds(55000));
281
282 // Discharge 0.648A (0.09C)
283 battDischDataset2 = Gnuplot2dDataset("CSB GP1272 0.648 A (0.09C)");
284 node = CreateObject<Node>();
285 batteryModel =
287
288 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
289 devicesEnergyModel->SetEnergySource(batteryModel);
290 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
291 devicesEnergyModel->SetNode(node);
292
293 devicesEnergyModel->SetCurrentA(0.648);
294
295 GraphBattery2(batteryModel);
297
298 Simulator::Stop(Seconds(30000));
301
302 battDischPlot2.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
303 battDischPlot2.SetLegend(" Time (Minutes)", "Voltage (V)");
304 battDischPlot2.SetExtra("set xrange[1:1800]\n\
305 set yrange [7:14]\n\
306 set logscale x \n\
307 set tics scale 3\n\
308 set xtics (1,2,3,5,10,20,30,60,120,180,300,600,1200,1800)\n\
309 set ytics (0,8,9,10,11,12,13,14)\n\
310 set grid\n\
311 set style line 1 linewidth 5\n\
312 set style line 2 linewidth 5\n\
313 set style line 3 linewidth 5\n\
314 set style line 4 linewidth 5\n\
315 set style line 5 linewidth 5\n\
316 set style line 6 linewidth 5\n\
317 set style line 7 linewidth 5\n\
318 set style line 8 linewidth 5\n\
319 set style increment user\n\
320 set key reverse Left");
322 battDischFile2.close();
323 std::cout << "The end, plotting now\n";
324
325 //////////////////////// Panasonic Li-on CGR18650DA, discharge 0.2C,1C,2C ///////////
326
327 // Discharge 0.466A (0.2C)
328 battDischDataset3 = Gnuplot2dDataset("Panasonic Li-on CGR18650DA 0.466 A (0.2C)");
329 node = CreateObject<Node>();
330 batteryModel =
332
333 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
334 devicesEnergyModel->SetEnergySource(batteryModel);
335 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
336 devicesEnergyModel->SetNode(node);
337
338 devicesEnergyModel->SetCurrentA(0.466);
339
340 GraphBattery3(batteryModel);
342
343 Simulator::Stop(Seconds(17720));
346
347 // Discharge 2.33A (1C)
348 battDischDataset3 = Gnuplot2dDataset("Panasonic Li-on CGR18650DA 2.33 A (1C)");
349 node = CreateObject<Node>();
350 batteryModel =
352
353 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
354 devicesEnergyModel->SetEnergySource(batteryModel);
355 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
356 devicesEnergyModel->SetNode(node);
357
358 devicesEnergyModel->SetCurrentA(2.33);
359
360 GraphBattery3(batteryModel);
362
366
367 // Discharge 4.66A (2C)
368 battDischDataset3 = Gnuplot2dDataset("Panasonic Li-on CGR18650DA 4.66 A (2C)");
369 node = CreateObject<Node>();
370 batteryModel =
372
373 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
374 devicesEnergyModel->SetEnergySource(batteryModel);
375 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
376 devicesEnergyModel->SetNode(node);
377
378 devicesEnergyModel->SetCurrentA(4.66);
379
380 GraphBattery3(batteryModel);
382
386
387 battDischPlot3.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
388 battDischPlot3.SetLegend(" Discharge Capacity (mAh)", "Voltage (V)");
389 battDischPlot3.SetExtra("set xrange[0:2400]\n\
390 set yrange [2.6:4.4]\n\
391 set xtics 400\n\
392 set ytics 0.2\n\
393 set grid\n\
394 set style line 1 linewidth 5\n\
395 set style line 2 linewidth 5\n\
396 set style line 3 linewidth 5\n\
397 set style line 4 linewidth 5\n\
398 set style line 5 linewidth 5\n\
399 set style line 6 linewidth 5\n\
400 set style line 7 linewidth 5\n\
401 set style line 8 linewidth 5\n\
402 set style increment user\n\
403 set key reverse Left");
405 battDischFile3.close();
406 std::cout << "The end, plotting now\n";
407
408 //////////////////////// Rs PRO LGP12100 Lead Acid discharge 0.05C, 1C ///////////////
409
410 // Discharge 0.36A (0.05C)
411 battDischDataset4 = Gnuplot2dDataset("Rs PRO LGP12100 5A (0.05C)");
412 node = CreateObject<Node>();
413 batteryModel =
415
416 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
417 devicesEnergyModel->SetEnergySource(batteryModel);
418 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
419 devicesEnergyModel->SetNode(node);
420
421 devicesEnergyModel->SetCurrentA(5);
422
423 GraphBattery4(batteryModel);
425
426 Simulator::Stop(Seconds(65000));
429
430 // Discharge 100A (1C)
431 battDischDataset4 = Gnuplot2dDataset("Rs PRO LGP12100 100A (1C)");
432 node = CreateObject<Node>();
433 batteryModel =
435
436 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
437 devicesEnergyModel->SetEnergySource(batteryModel);
438 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
439 devicesEnergyModel->SetNode(node);
440
441 devicesEnergyModel->SetCurrentA(100);
442
443 GraphBattery4(batteryModel);
445
449
450 battDischPlot4.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
451 battDischPlot4.SetLegend(" Time (Minutes)", "Voltage (V)");
452 battDischPlot4.SetExtra("set xrange[1:1800]\n\
453 set yrange [7:13]\n\
454 set logscale \n\
455 set tics scale 3\n\
456 set xtics (1,2,4,6,8,10,20,40,60,120,240,360,480,600,1200)\n\
457 set ytics (7,8,9,10,11,12,13)\n\
458 set grid\n\
459 set style line 1 linewidth 5\n\
460 set style line 2 linewidth 5\n\
461 set style line 3 linewidth 5\n\
462 set style line 4 linewidth 5\n\
463 set style line 5 linewidth 5\n\
464 set style line 6 linewidth 5\n\
465 set style line 7 linewidth 5\n\
466 set style line 8 linewidth 5\n\
467 set style increment user\n\
468 set key reverse Left");
470 battDischFile4.close();
471 std::cout << "The end, plotting now\n";
472
473 //////////////////////// Panasonic N-700AAC NiCd discharge ///////////////////////////
474
475 // Discharge 0.7A (0.1C)
476 battDischDataset5 = Gnuplot2dDataset("Panasonic N-700AAC 0.7A (0.01C)");
477 node = CreateObject<Node>();
478 batteryModel =
480
481 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
482 devicesEnergyModel->SetEnergySource(batteryModel);
483 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
484 devicesEnergyModel->SetNode(node);
485
486 devicesEnergyModel->SetCurrentA(0.07);
487
488 GraphBattery5(batteryModel);
490
491 Simulator::Stop(Seconds(38500));
494
495 // Discharge 0.14A (0.2C)
496 battDischDataset5 = Gnuplot2dDataset("Panasonic N-700AAC 0.14A (0.2C)");
497 node = CreateObject<Node>();
498 batteryModel =
500
501 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
502 devicesEnergyModel->SetEnergySource(batteryModel);
503 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
504 devicesEnergyModel->SetNode(node);
505
506 devicesEnergyModel->SetCurrentA(0.14);
507
508 GraphBattery5(batteryModel);
510
511 Simulator::Stop(Seconds(19200));
514
515 // Discharge 0.35A (0.5C)
516 battDischDataset5 = Gnuplot2dDataset("Panasonic N-700AAC 0.35A (0.5C)");
517 node = CreateObject<Node>();
518 batteryModel =
520
521 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
522 devicesEnergyModel->SetEnergySource(batteryModel);
523 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
524 devicesEnergyModel->SetNode(node);
525
526 devicesEnergyModel->SetCurrentA(0.35);
527
528 GraphBattery5(batteryModel);
530
534
535 battDischPlot5.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
536 battDischPlot5.SetLegend(" Time (Hours)", "Voltage (V)");
537 battDischPlot5.SetExtra("set xrange[0:16]\n\
538 set yrange [0.7:1.5]\n\
539 set tics scale 3\n\
540 set xtics 2\n\
541 set ytics 0.1\n\
542 set grid\n\
543 set style line 1 linewidth 5\n\
544 set style line 2 linewidth 5\n\
545 set style line 3 linewidth 5\n\
546 set style line 4 linewidth 5\n\
547 set style line 5 linewidth 5\n\
548 set style line 6 linewidth 5\n\
549 set style line 7 linewidth 5\n\
550 set style line 8 linewidth 5\n\
551 set style increment user\n\
552 set key reverse Left");
554 battDischFile5.close();
555 std::cout << "The end, plotting now\n";
556 return 0;
557}
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Hold variables of type enum.
Definition enum.h:52
Creates and assign an assortment of BatteryModels to Nodes.
Ptr< energy::EnergySourceContainer > Install(NodeContainer c) const
This function installs energy sources in a group of nodes in a node container.
Class to represent a 2D points plot.
Definition gnuplot.h:105
void Add(double x, double y)
Definition gnuplot.cc:366
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition gnuplot.h:359
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition gnuplot.cc:765
void SetTerminal(const std::string &terminal)
Definition gnuplot.cc:753
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
void SetExtra(const std::string &extra)
Definition gnuplot.cc:772
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:160
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetMinutes() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:387
double GetHours() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:382
std::ofstream battDischFile1("BattDischCurve1.plt")
Gnuplot2dDataset battDischDataset2
void GraphBattery1(Ptr< GenericBatteryModel > es)
std::ofstream battDischFile3("BattDischCurve3.plt")
void GraphBattery4(Ptr< GenericBatteryModel > es)
std::ofstream battDischFile5("BattDischCurve5.plt")
void GraphBattery2(Ptr< GenericBatteryModel > es)
std::ofstream battDischFile4("BattDischCurve4.plt")
Gnuplot battDischPlot1
This example shows the use of batteries in ns-3.
Gnuplot2dDataset battDischDataset5
void GraphBattery5(Ptr< GenericBatteryModel > es)
Gnuplot2dDataset battDischDataset1
void GraphBattery3(Ptr< GenericBatteryModel > es)
Gnuplot2dDataset battDischDataset3
Gnuplot2dDataset battDischDataset4
std::ofstream battDischFile2("BattDischCurve2.plt")
@ PANASONIC_HHR650D_NIMH
Panasonic HHR650D NiMh battery.
@ CSB_GP1272_LEADACID
CSB GP1272 Lead acid battery.
@ PANASONIC_CGR18650DA_LION
Panasonic CGR18650DA Li-Ion battery.
@ PANASONIC_N700AAC_NICD
Panasonic N700AAC NiCd battery.
@ RSPRO_LGP12100_LEADACID
RS Pro LGP12100 Lead acid battery.
@ NIMH_NICD
Nickel-metal hydride and Nickel cadmium batteries.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition log.h:102