Skip to content

Commit

Permalink
#3690 fix issue with skipped steps (#3708)
Browse files Browse the repository at this point in the history
* #3690 fix issue with skipped steps

* #3690 changelog

* #3690 add test
  • Loading branch information
rtimms authored Jan 15, 2024
1 parent c3def31 commit a2481fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# [Unreleased](https://github.com/pybamm-team/PyBaMM/)

## Bug Fixes

- Fixed a bug where if the first step(s) in a cycle are skipped then the cycle solution started from the model's initial conditions instead of from the last state of the previous cycle ([#3708](https://github.com/pybamm-team/PyBaMM/pull/3708))
# [v24.1rc0](https://github.com/pybamm-team/PyBaMM/tree/v24.1rc0) - 2024-01-31

## Features
Expand Down
15 changes: 14 additions & 1 deletion pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,20 @@ def solve(

steps.append(step_solution)

cycle_solution = cycle_solution + step_solution
# If there haven't been any successful steps yet in this cycle, then
# carry the solution over from the previous cycle (but
# `step_solution` should still be an EmptySolution so that in the
# list of returned step solutions we can see which steps were
# skipped)
if (
cycle_solution is None
and isinstance(step_solution, pybamm.EmptySolution)
and not isinstance(current_solution, pybamm.EmptySolution)
):
cycle_solution = current_solution.last_state
else:
cycle_solution = cycle_solution + step_solution

current_solution = cycle_solution

callbacks.on_step_end(logs)
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_experiments/test_simulation_with_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,25 @@ def test_run_experiment_skip_steps(self):
decimal=5,
)

def test_skipped_step_continuous(self):
model = pybamm.lithium_ion.SPM({"SEI": "solvent-diffusion limited"})
experiment = pybamm.Experiment(
[
("Rest for 24 hours (1 hour period)",),
(
"Charge at C/3 until 4.1 V",
"Hold at 4.1V until C/20",
"Discharge at C/3 until 2.5 V",
),
]
)
sim = pybamm.Simulation(model, experiment=experiment)
sim.solve(initial_soc=1)
np.testing.assert_array_almost_equal(
sim.solution.cycles[0].last_state.y.full(),
sim.solution.cycles[1].steps[-1].first_state.y.full(),
)

def test_all_empty_solution_errors(self):
model = pybamm.lithium_ion.SPM()
parameter_values = pybamm.ParameterValues("Chen2020")
Expand Down

0 comments on commit a2481fa

Please sign in to comment.