Repository navigation
feat(rust): lower expression trees with Symbol.to_rust - #5959
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❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## stack/03-pybamm-rust-packaging #5959 +/- ##
=================================================================
Coverage ? 98.18%
=================================================================
Files ? 351
Lines ? 36399
Branches ? 0
=================================================================
Hits ? 35738
Misses ? 661
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
3 of 5 tasks
BradyPlanden
force-pushed
the
stack/04-to-rust-lowering
branch
from
October 28, 2026 09:55
0c46274 to
64cb90a
Compare
BradyPlanden
force-pushed
the
stack/04-to-rust-lowering
branch
3 times, most recently
from
October 29, 2026 08:01
d0980ff to
bfa1ad0
Compare
BradyPlanden
marked this pull request as ready for review
October 29, 2026 08:55
Contributor
|
| Project | PyBaMM |
| Branch | stack/04-to-rust-lowering |
| 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.
to_rust on Symbol and every expression-tree class that lowers.
BradyPlanden
force-pushed
the
stack/04-to-rust-lowering
branch
from
October 29, 2026 17:26
bfa1ad0 to
595db2e
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
Symbol.to_rust(graph, rust_symbols=None), which lowers a discretised expression tree into apybamm.rust.ExprGraph. Each class implements_to_rust, and a subtree shared within the tree is lowered once, cached on its id. Nothing calls it yet; #5960 uses it to compile a model's functions.Scalar,Time,InputParameter(at its expected width), andStateVector/StateVectorDot, with several slices concatenated.Array: lowers as dense values, or as a dense matrix when it has more than one column. A sparse array stays a CSR matrix as the left operand of a matrix product, copied into canonical order so that each row sums in ascending column order, as CasADi does. scipy's stored order, which can be unsorted, left residual noise near roundoff that stalled IDA on the DFN.Power,Inner,Minimum/Maximum,Modulo,Hypot, the Heavisides,EqualityandMatrixMultiplication.Modulotruncates like CasADi'sfmod, so for a negative operand it matches CasADi rather thanevaluate's floored modulo. At a tie,Minimum/Maximumand the max/min reductions take the left operand's or first entry's derivative, asSymbol.jacdoes, where CasADi splits it 0.5/0.5;Negate,AbsoluteValue,Sign,Floor,CeilingandIndex;NumpyConcatenation,DomainConcatenation,SparseStack,ConditionalandVectorField;ExplicitTimeIntegral,DiscreteTimeSumandNotConstantlower their child.SpecificFunctions, andArcsinh2in the same sign-and-hypotform as CasADi;RegPowerthrough the_decompositionthatevaluateand CasADi share (fix: make CasADi's RegPower and uniform-grid interpolants match evaluate #5955), so for exponents 0.5 and 2 withdelta > 0Rust agrees with both bitwise; withdelta = 0all three takepower(base, exponent).Functioncannot be lowered, so this relies on fix: keep a function's class when copying it without simplification #5954 keeping a copied function's class.pybamm.ModelErrornamingconvert_to_format = 'casadi';PPolycoefficientsevaluateuses;extrapolate=Falseis ignored, as on CasADi.Unsupported nodes raise when they are lowered:
TypeError, naming the class andconvert_to_format = 'casadi', forKroneckerProduct,TensorProduct,Transpose,Magnitude,Component,Norm, spatial operators and a genericFunction;NotImplementedErrorfor a stridedIndex, 1D interpolant data with more than two dimensions, and interpolants in four or more dimensions.Type of change
New feature, under
## FeaturesinCHANGELOG.md.Key checklist
pre-commit runevaluate;RegPowerbitwise equal across numpy, CasADi MX and SX, and Rust, for exponents 0.5 and 2 at scales 1 and 33133;RegPowerwithdelta = 0, andModulowith negative operands, against CasADi;abs,sign,Minimum,Maximumand the reductions at ties.DomainConcatenation, and dense-row colouring of the SPMe "voltage as a state" Jacobian.dual_backend_compareis intests/conftest_rust.py; feat(rust): evaluate rust models through compiled evaluators #5960 addsrust_backendthere, once"rust"is a format.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_rust← this PRconvert_to_format = "rust"forScipySolver,AlgebraicSolverand EISpybammsolvers: IDAKLU over the C ABIIDAKLUSolveron rust modelspybamm-core: diffsol DAE solverpybamm.DiffsolSolver"rust"as the defaultconvert_to_format