Skip to content

perf: precalculate state mapper for mapping between experimental steps#5380

Merged
MarcBerliner merged 15 commits intomainfrom
simulation-state-mapper
Feb 25, 2026
Merged

perf: precalculate state mapper for mapping between experimental steps#5380
MarcBerliner merged 15 commits intomainfrom
simulation-state-mapper

Conversation

@martinjrobins
Copy link
Contributor

@martinjrobins martinjrobins commented Feb 15, 2026

Description

Summary

Pre-calculate state mapper during simulation setup instead of on-demand at each step. performance gains mainly for smaller models (SPM)

Changes

File Changes
base_model.py New build_initial_state_mapper() method
simulation.py Cache mapper during experiment setup
base_solver.py Use cached mapper when available
test_simulation_with_experiment.py Mapper validation tests

Benchmark Results (10 repeats)

Case Setup Solve 1 Solve 2 Total
CCCV · Chen2020 · DFN · CasadiSolver -8.09% -0.67% -1.09% -0.95%
CCCV · Chen2020 · DFN · IDAKLUSolver -12.04% +2.00% +2.37% +1.37%
CCCV · Chen2020 · SPM · CasadiSolver +10.27% +1.04% +0.48% +2.08%
CCCV · Chen2020 · SPM · IDAKLUSolver +13.81% -0.49% +12.36% +1.73%
CCCV · Marquis2019 · DFN · IDAKLUSolver +0.82% +0.48% +1.70% +0.58%
CCCV · Marquis2019 · SPM · CasadiSolver -16.35% +1.80% +4.55% +0.59%
CCCV · Marquis2019 · SPM · IDAKLUSolver -5.81% +2.43% +10.43% +1.89%
GITT · Chen2020 · DFN · CasadiSolver -6.91% +6.50% +8.58% +7.22%
GITT · Chen2020 · DFN · IDAKLUSolver +2.27% -1.08% -5.93% -1.84%
GITT · Chen2020 · SPM · CasadiSolver +22.71% +3.08% +18.76% +9.79%
GITT · Chen2020 · SPM · IDAKLUSolver +1.90% +6.95% +19.84% +7.63%
GITT · Marquis2019 · DFN · IDAKLUSolver +3.06% -0.95% -5.91% -1.61%
GITT · Marquis2019 · SPM · CasadiSolver -14.97% +9.40% +19.61% +9.30%
GITT · Marquis2019 · SPM · IDAKLUSolver +12.25% +0.15% +20.21% +4.51%
Average +0.21% +2.19% +7.57% +3.02%

Note: Positive values indicate speedup (mapper is faster). 10 repeats per case.

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

@martinjrobins martinjrobins requested a review from a team as a code owner February 15, 2026 14:15
@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

❌ Patch coverage is 90.38462% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.24%. Comparing base (ed711b6) to head (a50fb69).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/pybamm/models/base_model.py 84.21% 6 Missing ⚠️
src/pybamm/simulation.py 93.10% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5380      +/-   ##
==========================================
- Coverage   98.30%   98.24%   -0.06%     
==========================================
  Files         328      328              
  Lines       28952    29052     +100     
==========================================
+ Hits        28460    28542      +82     
- Misses        492      510      +18     

☔ 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.

Copy link
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 Martin, this is a nice performance boost! Just some minor comments

@MarcBerliner MarcBerliner self-requested a review February 24, 2026 18:51
Copy link
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 @martinjrobins, looks good to me other than the doctest failure

@MarcBerliner MarcBerliner merged commit 4d0c6cd into main Feb 25, 2026
23 of 24 checks passed
@MarcBerliner MarcBerliner deleted the simulation-state-mapper branch February 25, 2026 14:35
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