Repository navigation
feat(rust): an in-process diffsol DAE solver in pybamm-core - #5964
Open
BradyPlanden wants to merge 1 commit into
Open
BradyPlanden wants to merge 1 commit into
BradyPlanden wants to merge 1 commit into
Conversation
This was referenced Oct 22, 2026
This was referenced Oct 22, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## stack/08-native-observation #5964 +/- ##
==============================================================
Coverage ? 98.20%
==============================================================
Files ? 353
Lines ? 37029
Branches ? 0
==============================================================
Hits ? 36364
Misses ? 665
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Oct 22, 2026
BradyPlanden
force-pushed
the
stack/09-rust-core-diffsol
branch
2 times, most recently
from
October 28, 2026 11:06
080bc62 to
acc59db
Compare
BradyPlanden
force-pushed
the
stack/09-rust-core-diffsol
branch
3 times, most recently
from
October 29, 2026 08:01
44a3505 to
8a7b5ac
Compare
BradyPlanden
marked this pull request as ready for review
October 29, 2026 08:56
Contributor
|
| Project | PyBaMM |
| Branch | stack/09-rust-core-diffsol |
| Testbed | bare-metal |
🐰 View full continuous benchmarking report in Bencher
⚠️ WARNING: Truncated view!The full continuous benchmarking report exceeds the maximum length allowed on this platform.
The diffsol-backed solver module in pybamm-core: operators, forward sensitivities batched one lane per parameter, and the batch layer.
BradyPlanden
force-pushed
the
stack/09-rust-core-diffsol
branch
from
October 29, 2026 17:26
8a7b5ac to
553a149
Compare
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
Adds a DAE solver to
pybamm-core: asolvermodule behind adiffsolfeature, on by default. It integrates aCompiledModelin-process with diffsol's BDF method and faer's sparse LU, with forward sensitivities, termination events, output-variable trajectories and a rayon batch layer for input sets. It depends ondiffsol 0.18.0from crates.io,faer 0.24andrayon. Python reaches it in #5965.PreparedSolver:Send + Syncand one instance serves concurrent solves.Equations(diffsol'sOdeEquations).Equationsowns aModelEvaluatorplus the solve's inputs, initial state and initial sensitivities, and the operator views borrow the evaluator'sRefCell<Workspace>per call.SolveRequest(stop times, outputs, sensitivities and LU threads) and anInputSet(inputs,y0, and an optionaldy0/dpseed for a continued step).SolveOutcomewith the times, states,ypand sensitivities (whose last column is the end state), the start and event states and their sensitivities, the index of the event that ended the solve, andSolverStatistics.sens_rtol = rtoland parameter scales|p|, or 1 wherepis 0 or not finite. On differential statessens_atol = atol * sens_atol_factor; algebraic states keep the stateatol. The default factor of 1.0 matches IDAS'sIDASensEEtolerances.sens_error_control_relaxedis set. An initial-condition failure is not retried.bdf_state.t_evalwith two times one ulp apart, the solver steps across to the far side of the bracket, re-solvesyandy'with a line-searched Newton iteration and then the sensitivity system, and resets the step size, as IDAKLU'sHandleBreakpointdoes. Other stop times on the grid restart where the previous segment ended, and stop times off the grid are integrated through.t,derivative_wrt_timecorrects the storedypat each segment start.dg/dp + dg/dy·S.ypon that column and no repeat of the previous column's time.solve_batchruns input sets on a rayon pool, with every sparse LU of each set, the algebraic block's included, on one thread to avoid oversubscription. A single solve's LUs take the request's thread count, never faer's global setting, so the solver also runs where a host disables faer's global parallelism. A failing set returns its own error, and results are bitwise equal to a serial loop.SolverOptionsexposes 15 of diffsol's options with diffsol's defaults, exceptmax_nonlinear_solver_failures = 100_000.Each solve builds its
OdeSolverProblemafresh.Equationsholds aRefCell, so it is notSync, whilesolve_batchshares one&PreparedSolveracross workers. The time this takes is reported per solve asproblem_setup_time_secs.Limits:
df/dpis assembled from a dense pattern.yp: only for state trajectories of up to 4096 points, never for output variables.pybamm-pythonbuildspybamm-corewith its default features, so the wheel compiles diffsol, faer and rayon from this PR on, before #5965 uses them.Review on #5732
EquationsduplicatesModelEvaluator:Equationsowns aModelEvaluator, the type the C ABI uses, and adds only the solve's own state.Rcwhere aRefCellwould do:ModelEvaluatorholds its workspace in aRefCell, which the operator views borrow through a shared reference. The C ABI holds the evaluator exclusively and usesget_mut, with no borrow-flag check.UnitCallable:ResetandOutareUnitCallable.dy0/dpfrom the previous step (InputSet::with_sens_seed), which feat(rust): pybamm.DiffsolSolver, integrating rust models with diffsol #5965 tests against CasADi.RhsOpdocstring is one line.OdeSolverProblemper solve: this keepsPreparedSolverSend + Syncforsolve_batch, as above. The cost is reported asproblem_setup_time_secs; per solve it is 0.7 µs for the SPM, 1.2 µs for the SPMe and 4.5 µs for the DFN.Type of change
New feature, under
## FeaturesinCHANGELOG.md.Key checklist
pre-commit run, includingcargo fmtandcargo clippy--all-features, 1 ignored), and the MSRV check passes (cargo +1.89 check --locked --all-targets --all-features)yp, including time-dependent constraints;t_eval, lengths, and tolerances IDAS would reject.dy0/dpseed and sensitivity subsets;sens_atoland its factor;solve(), which from the shared initial state is bitwise equal to a relaxed solve from scratch, and a retry that fails too (SensRetryFailed).PreparedSolverisSend + Sync;solver_setup: preparing, reusing, and setting up afresh;sens_params: 1, 4 and 16 of 16 sensitivity columns.Rust execution engine stack: proposal #5667, showcase #5732
Each PR is one commit on the one above it.
Function.create_copykeeps the function's classRegPowerand uniform-grid interpolant lookups matchevaluatepybamm-core: expression graph, typed IR, interpreters, forward-mode ADpybamm-core:CompiledModel, coloured Jacobians, the C ABIpybamm.rustbindings, built into thepybammwheelSymbol.to_rustconvert_to_format = "rust"forScipySolver,AlgebraicSolverand EISpybammsolvers: IDAKLU over the C ABIIDAKLUSolveron rust modelspybamm-core: diffsol DAE solver ← this PRpybamm.DiffsolSolver"rust"as the defaultconvert_to_format