Repository navigation
fix: keep the last completed state when an unsaved experiment cycle fails - #5898
Open
ToukoUrsin wants to merge 2 commits into
Open
ToukoUrsin wants to merge 2 commits into
ToukoUrsin wants to merge 2 commits into
Conversation
…ails When a step failed before the first step of a cycle not in save_at_cycles, the returned solution kept a state older than the last completed cycle and on_cycle_end fired for the failed cycle with the previous cycle's summary.
rtimms
approved these changes
Oct 14, 2026
rtimms
left a comment
Contributor
There was a problem hiding this comment.
Thanks @ToukoUrsin, the diagnosis is right, and the new tests fail on main and pass here. A few small things before merge:
- Please drop
last_state.solver_statistics = pybamm.SolverStatistics()and its comment.Solution._build_last_statealready setslast_state's statistics to zero, so the discarded work is not counted twice either way. All 14 tests pass without the line, and it avoids mutating the cachedlast_state, whichSummaryVariablesalso holds. - Please drop
test_unsaved_failure_retention_is_mesh_independent. Mesh size isn't involved in this code path, andtest_failure_keeps_latest_completed_statealready covers the same assertions. CHANGELOG.mdconflicts withmain; please mergemainand keep the entry under# [Unreleased]. Could you also say what changed for the user, e.g. "Simulation.solvewithsave_at_cyclesnow returns the last completed cycle's state when the solver fails on the first step of an unsaved cycle, and no longer callson_cycle_endfor the failed cycle."
#5899 changes the same solve and get_data_dict paths, so it'll need a rebase after this and #5724 land.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
With
save_at_cycles, if the solver fails on the first step of a cycle that is not saved,Simulation.solvereturns a state from before the last completed cycle, andon_cycle_endcan fire for the failed cycle with the previous cycle's summary.Minimal repro (four 1 s rests, solver fails on the 3rd step):
Fix. When no step of the failing cycle completed, keep
current_solution.last_state(if it is newer than what was retained), reset its solver statistics so discarded work is not counted twice, and stop before the cycle-end callback.Tests. New
tests/unit/test_experiments/test_failure_retention.py(14 cases: list/all/integersave_at_cycles, no failure, first/later failure, resume from a failed sparse solve, partial-cycle failure, solver statistics, two meshes). Six fail on currentmain, all pass with the fix;tests/unit/test_experimentsandtests/unit/test_simulation.pypass.Related: #4793 (this does not fix that convergence failure; it fixes what is returned after one).
Type of change
Bug fix, with a
CHANGELOG.mdentry.🤖 Generated with Claude Code (Claude Opus 5.5).