Skip to content

Commit 0fcec04

Browse files
MarcBerlinerpipliggins
authored andcommitted
Add options to terminate IDAKLUSolver early when there's little progress (#5201)
* fail fast IDA options * tests * Update CHANGELOG.md * fix defaults
1 parent b40d127 commit 0fcec04

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Features
44

5+
- Added the `num_steps_no_progress` and `t_no_progress` options in the `IDAKLUSolver` to early terminate the simulation if little progress is detected. ([#5201](https://github.com/pybamm-team/PyBaMM/pull/5201))
56
- Added helper functions to import external 3D meshes in PyBaMM ([#5162](https://github.com/pybamm-team/PyBaMM/pull/5162))
67
- Added support for algebraic and differential surface form in composite models. ([#5165](https://github.com/pybamm-team/PyBaMM/pull/5165))
78
- Adds a composite electrode electrode soh model ([#5160](https://github.com/pybamm-team/PyBaMM/pull/5129))

src/pybamm/solvers/idaklu_solver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ class IDAKLUSolver(pybamm.BaseSolver):
133133
"init_all_y_ic": False,
134134
# Calculate consistent initial conditions
135135
"calc_ic": True,
136+
## Early termination
137+
# Maximum number of consecutive steps allowed without advancing
138+
# the solution time by at least `t_no_progress` seconds.
139+
# If set to 0, this feature is disabled.
140+
"num_steps_no_progress": 0,
141+
# Minimum required time advancement (in seconds) after
142+
# `num_steps_no_progress` consecutive steps.
143+
# If set to 0.0, this feature is disabled.
144+
"t_no_progress": 0.0,
136145
}
137146
138147
Note: These options only have an effect if model.convert_to_format == 'casadi'
@@ -186,6 +195,8 @@ def __init__(
186195
"linesearch_off_ic": False,
187196
"init_all_y_ic": False,
188197
"calc_ic": True,
198+
"num_steps_no_progress": 0,
199+
"t_no_progress": 0.0,
189200
}
190201
if options is None:
191202
options = default_options

tests/unit/test_solvers/test_idaklu_solver.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,3 +1486,47 @@ def test_on_failure_option(self):
14861486
)
14871487
assert len(w) > 0
14881488
assert "FAILURE" in str(w[0].message)
1489+
1490+
def test_no_progress_early_termination(self):
1491+
# SPM at rest
1492+
model = pybamm.lithium_ion.SPM()
1493+
parameter_values = pybamm.ParameterValues("Chen2020")
1494+
parameter_values.update({"Current function [A]": 0})
1495+
1496+
t_eval = [0, 10000]
1497+
1498+
options_successes = [
1499+
# Case 1: feature disabled because num_steps_no_progress is default (0)
1500+
# even if t_no_progress is huge
1501+
{
1502+
"t_no_progress": 1e10,
1503+
"num_steps_no_progress": 0,
1504+
},
1505+
# Case 2: feature disabled because t_no_progress is default (0.0)
1506+
# even if num_steps_no_progress is positive
1507+
{
1508+
"num_steps_no_progress": 5,
1509+
"t_no_progress": 0.0,
1510+
},
1511+
]
1512+
1513+
for options in options_successes:
1514+
solver = pybamm.IDAKLUSolver(on_failure="ignore", options=options)
1515+
sim = pybamm.Simulation(
1516+
model, parameter_values=parameter_values, solver=solver
1517+
)
1518+
sol = sim.solve(t_eval)
1519+
assert sol.termination == "final time"
1520+
1521+
## Check failure
1522+
options_failures = {
1523+
"num_steps_no_progress": 5,
1524+
"t_no_progress": 1e10,
1525+
}
1526+
solver = pybamm.IDAKLUSolver(on_failure="ignore", options=options_failures)
1527+
sim = pybamm.Simulation(model, parameter_values=parameter_values, solver=solver)
1528+
sol = sim.solve(t_eval)
1529+
assert sol.termination == "failure"
1530+
1531+
assert len(sol.t) == options_failures["num_steps_no_progress"]
1532+
assert sol.t[-1] < options_failures["t_no_progress"]

0 commit comments

Comments
 (0)