Skip to content

perf: faster model build and IDAKLU set-up for large models - #5994

Merged
MarcBerliner merged 9 commits into
mainfrom
perf/large-model-build-v2
Oct 30, 2026
Merged

MarcBerliner merged 9 commits into
mainfrom
perf/large-model-build-v2

Conversation

@MarcBerliner

Copy link
Copy Markdown
Member

Description

Speeds up model build and IDAKLUSolver set-up for large models (hundreds of thousands to millions of states), where several steps scaled worse than linearly or copied large objects needlessly:

  • StateVector stores its slices as index ranges instead of a dense boolean evaluation array over every state. Construction, size, Jacobians and CasADi conversion no longer touch an array the length of the state vector for each node.
  • CasADi functions and matrices are shared with pybammsolvers by pointer when it links the same CasADi library as the Python package (idaklu.shares_python_casadi), instead of being serialised and rebuilt. Sparse and dense casadi.DMs are filled from NumPy/SciPy buffers in one copy (to_casadi_dm, CSC and CSR), rather than element by element through SWIG. Without a shared library, the serialised path is used as before.
  • IDAKLUSolver no longer builds the rhs-only Jacobian, which only ScipySolver uses (BaseSolver.uses_rhs_jacobian).
  • Unstructured meshes compute cell geometry per local vertex and group faces with a lexsort (_group_rows) instead of np.unique(axis=0); hex geometry is about 2.5x faster at 400k cells, with identical volumes.
  • stack_inputs concatenates input values with one buffer copy.

Shared functions keep CasADi's reference counting, so the solver owns them for its lifetime and releases them on deletion: 40 repeated set-up and solve cycles of a 38k-state model show no memory growth, in either the shared or the serialised mode.

Benchmarks

Best of 3, IDAKLUSolver() defaults, 1 h discharge; end voltages agree with main to 10 digits.

Model States Build (ms) main → PR Set-up (ms) main → PR
SPM 42 18 → 17 15 → 11
DFN 962 52 → 42 53 → 25
DFN, lumped thermal 963 81 → 64 265 → 116
MPM 1204 45 → 39 72 → 36
DFN, 2D current collectors 48202 458 → 72 1350 → 455

Type of change

  • Optimisation, with one breaking change and one deprecation (see CHANGELOG.md): StateVector and StateVectorDot now raise ShapeError for stepped, overlapping or out-of-order slices, and their evaluation_array argument is deprecated and ignored.

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

- StateVector stores its slices as index ranges instead of a boolean mask.
- CasADi functions and matrices are shared with pybammsolvers instead of copied.
- IDAKLU no longer builds the rhs-only jacobian, which only ScipySolver uses.
- Unstructured meshes gather faces per vertex.
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.75325% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.22%. Comparing base (8c6816d) to head (9f72b8a).

Files with missing lines Patch % Lines
.../pybamm/src/pybamm/expression_tree/state_vector.py 92.85% 4 Missing ⚠️
...ackages/pybamm/src/pybamm/solvers/idaklu_solver.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5994      +/-   ##
==========================================
- Coverage   98.24%   98.22%   -0.02%     
==========================================
  Files         350      350              
  Lines       36112    36173      +61     
==========================================
+ Hits        35478    35532      +54     
- Misses        634      641       +7     

☔ View full report in Codecov by Harness.
📢 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.

@MarcBerliner
MarcBerliner marked this pull request as ready for review October 28, 2026 18:09
@MarcBerliner
MarcBerliner requested a review from a team as a code owner October 28, 2026 18:09
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

ProjectPyBaMM
Branchperf/large-model-build-v2
Testbedbare-metal

⚠️ WARNING: Truncated view!

The full continuous benchmarking report exceeds the maximum length allowed on this platform.

🐰 View full continuous benchmarking report in Bencher

@BradyPlanden BradyPlanden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@MarcBerliner
MarcBerliner enabled auto-merge (squash) October 30, 2026 13:30
@MarcBerliner
MarcBerliner merged commit 63476bc into main Oct 30, 2026
48 of 50 checks passed
@MarcBerliner
MarcBerliner deleted the perf/large-model-build-v2 branch October 30, 2026 13:45
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.

3 participants