Skip to content

Commit ab421a7

Browse files
Merge pull request #2894 from abillscmu/progress
add (optional) progress bar for cycling experiments
2 parents aa80775 + 8a97d2a commit ab421a7

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

pybamm/simulation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99
import sys
1010
from functools import lru_cache
11+
import tqdm
1112

1213

1314
def is_notebook():
@@ -510,6 +511,7 @@ def solve(
510511
starting_solution=None,
511512
initial_soc=None,
512513
callbacks=None,
514+
showprogress=False,
513515
**kwargs,
514516
):
515517
"""
@@ -557,6 +559,10 @@ def solve(
557559
callbacks : list of callbacks, optional
558560
A list of callbacks to be called at each time step. Each callback must
559561
implement all the methods defined in :class:`pybamm.callbacks.BaseCallback`.
562+
showprogress : bool, optional
563+
Whether to show a progress bar for cycling. If true, shows a progress bar
564+
for cycles. Has no effect when not used with an experiment.
565+
Default is False.
560566
**kwargs
561567
Additional key-word arguments passed to `solver.solve`.
562568
See :meth:`pybamm.BaseSolver.solve`.
@@ -698,7 +704,13 @@ def solve(
698704
num_cycles = len(self.experiment.cycle_lengths)
699705
feasible = True # simulation will stop if experiment is infeasible
700706
for cycle_num, cycle_length in enumerate(
701-
self.experiment.cycle_lengths, start=1
707+
# tqdm is the progress bar.
708+
tqdm.tqdm(
709+
self.experiment.cycle_lengths,
710+
disable=(not showprogress),
711+
desc="Cycling",
712+
),
713+
start=1,
702714
):
703715
logs["cycle number"] = (
704716
cycle_num + cycle_offset,

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ imageio>=2.9.0
99
pybtex>=0.24.0
1010
sympy >= 1.8
1111
bpx
12+
tqdm
1213
# Note: Matplotlib is loaded for debug plots but to ensure pybamm runs
1314
# on systems without an attached display it should never be imported
1415
# outside of plot() methods.

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def compile_KLU():
215215
"pybtex>=0.24.0",
216216
"sympy>=1.8",
217217
"bpx",
218+
"tqdm",
218219
# Note: Matplotlib is loaded for debug plots, but to ensure pybamm runs
219220
# on systems without an attached display, it should never be imported
220221
# outside of plot() methods.

tests/unit/test_experiments/test_simulation_with_experiment.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ def test_run_experiment_termination_capacity(self):
342342
# all but the last value should be above the termination condition
343343
np.testing.assert_array_less(5.04, C[:-1])
344344

345+
def test_run_experiment_with_pbar(self):
346+
# The only thing to test here is for errors.
347+
experiment = pybamm.Experiment(
348+
[
349+
(
350+
"Discharge at 1C for 1 sec",
351+
"Charge at 1C for 1 sec",
352+
),
353+
]
354+
* 10,
355+
)
356+
model = pybamm.lithium_ion.SPM()
357+
sim = pybamm.Simulation(model, experiment=experiment)
358+
sim.solve(showprogress=True)
359+
345360
def test_run_experiment_termination_voltage(self):
346361
# with percent
347362
experiment = pybamm.Experiment(

0 commit comments

Comments
 (0)