Skip to content

refactor: split Simulation into BaseSimulation and Simulation #5430

Merged
BradyPlanden merged 8 commits intomainfrom
feat/simulation-refactor
Apr 1, 2026
Merged

refactor: split Simulation into BaseSimulation and Simulation #5430
BradyPlanden merged 8 commits intomainfrom
feat/simulation-refactor

Conversation

@BradyPlanden
Copy link
Copy Markdown
Member

Extract a BaseSimulation class for non-experiment simulations and refactor Simulation to inherit from it, adding experiment support.

  • BaseSimulation: core build/solve/step, eSOH, plotting, serialisation
  • Simulation(BaseSimulation): experiment orchestration, state mapping,
    unified/legacy model modes, cycle/step loop
  • Extract _prepare_solve() and _discretise_experiment_models() helpers
    to eliminate code duplication between base and experiment paths
  • Add _recompute_initial_conditions override in Simulation to clear
    per-step solver caches (correctness fix)
  • Replace magic strings with class-level constants
    (MODE_WITHOUT_EXPERIMENT, MODE_DRIVE_CYCLE, MODE_WITH_EXPERIMENT,
    _PADDING_REST_KEY)
  • Consolidate setstate backward-compat guards into _SETSTATE_DEFAULTS
  • Update isinstance checks in plotting modules to use BaseSimulation
  • Add test_base_simulation.py with 23 tests covering class hierarchy,
    solve/build, pickle, properties, and plotting acceptance# Description

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Extract a BaseSimulation class for non-experiment simulations and
refactor Simulation to inherit from it, adding experiment support.
This improves separation of concerns and maintainability.

- BaseSimulation: core build/solve/step, eSOH, plotting, serialisation
- Simulation(BaseSimulation): experiment orchestration, state mapping,
  unified/legacy model modes, cycle/step loop
- Extract _prepare_solve() and _discretise_experiment_models() helpers
  to eliminate code duplication between base and experiment paths
- Add _recompute_initial_conditions override in Simulation to clear
  per-step solver caches (correctness fix)
- Replace magic strings with class-level constants
  (MODE_WITHOUT_EXPERIMENT, MODE_DRIVE_CYCLE, MODE_WITH_EXPERIMENT,
  _PADDING_REST_KEY)
- Consolidate __setstate__ backward-compat guards into _SETSTATE_DEFAULTS
- Update isinstance checks in plotting modules to use BaseSimulation
- Add test_base_simulation.py with 23 tests covering class hierarchy,
  solve/build, pickle, properties, and plotting acceptance
Performance:
- Replace termination staticmethods with class-level string constants
  (_STEP_INDEX_INPUT, _TERMINATION_TIME/VOLTAGE/CAPACITY,
  _TERMINATION_FINAL_TIME, _TERMINATION_EXPERIMENT_TAG,
  _COMBINED_TERMINATION_EVENT) to avoid function call overhead
- Hoist loop-invariant attribute lookups (experiment_steps,
  uses_unified, step_indices) as locals before the experiment
  solve loop
- Move Serialise import to local scope in save_model to avoid
  loading serialisation machinery at module import time
- Add early return in _build_experiment_state_mappers for unified
  model path

Idiom improvements:
- Extract _should_save_cycle() and _check_infeasible_steps() helpers
  to reduce solve() complexity
- Simplify dt computation, initial_start_time ternary, and
  voltage_stop conditional
- Deprecate _experiment_step_index_input_name() in favour of
  _STEP_INDEX_INPUT constant
- Update base_solver.py to use class constant directly
- Remove WHAT-not-WHY comments throughout
@BradyPlanden BradyPlanden requested a review from a team as a code owner March 31, 2026 14:23
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 99.16268% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.34%. Comparing base (72bd9c4) to head (611d2cb).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/pybamm/simulation/simulation.py 98.88% 6 Missing ⚠️
src/pybamm/simulation/base_simulation.py 99.65% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5430      +/-   ##
==========================================
+ Coverage   98.31%   98.34%   +0.03%     
==========================================
  Files         331      333       +2     
  Lines       30471    30475       +4     
==========================================
+ Hits        29957    29971      +14     
+ Misses        514      504      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

BradyPlanden and others added 3 commits March 31, 2026 17:02
Cover previously untested methods: _pv_fingerprint, _normalize_inputs,
_compute_esoh_fingerprint, set_initial_state (cache_esoh=False path),
_prepare_solve (calc_esoh warning), _get_built_models, plot/create_gif/
plot_voltage_components error paths, deprecated method warnings, save_model
edge cases, _should_save_cycle, _check_infeasible_steps, experiment model
mode validation, unified vs legacy input builders, state mapper edge cases,
and experiment type coercion.
MarcBerliner
MarcBerliner previously approved these changes Mar 31, 2026
Copy link
Copy Markdown
Member

@MarcBerliner MarcBerliner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @BradyPlanden, this is a nice cleanup

@BradyPlanden BradyPlanden merged commit beedfff into main Apr 1, 2026
23 of 26 checks passed
@BradyPlanden BradyPlanden deleted the feat/simulation-refactor branch April 1, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants