Skip to content

refactor(rust): IDAKLU sizes itself from the evaluator's own table - #6133

Closed
BradyPlanden wants to merge 1 commit into
stack/07-idaklu-rust-modefrom
stack/07a-rust-ffi-layout
Closed

BradyPlanden wants to merge 1 commit into
stack/07-idaklu-rust-modefrom
stack/07a-rust-ffi-layout

Conversation

@BradyPlanden

@BradyPlanden BradyPlanden commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Description

IDAKLUSolver sizes a rust model's solver from the evaluator's own FFI table instead of from counts Python reads off the CompiledModel and passes down.

 IDAKLUSolver.set_up
-  reads 15 CompiledModel members
-  create_rust_solver_group(20 arguments)
-    RustFunctions(15 arguments, 8 of them counts the table already answers)
-    # each count checked in Python, in idaklu.cpp, and in C++ against the table
+  create_rust_solver_group(evaluators, atol, rtol, options, use_algebraic_subblock)
+    RustFunctions reads a RustLayout from its evaluator's table
+      counts, Jacobian CSC pattern, each state's IDA id,
+      algebraic Jacobian COO pattern, which event components read y_dot
+      band widths worked out from the pattern
  • The table gains the Jacobian's CSC pattern, each state's IDA id, the algebraic Jacobian's COO pattern and the event components that read y_dot, appended within ABI version 2. test_ffi_abi_contract.rs pins the layout against pybammsolvers' header.
  • create_rust_solver_group takes the evaluators, atol, rtol, options and whether to use the algebraic sub-block. It refuses an atol of the wrong length, evaluators of different models, and a sub-block that covers only some of the states the mass matrix marks algebraic.
  • The Rust Newton solver reads its block from the table and shifts the pattern's columns into the block itself, beside the gather that puts the block at the trailing states.
  • Vector events: IDA counts one root per event component, as the flags do, so C++ no longer refuses a vector event. The lowering still does.
  • Removed: the C++ pattern checks, Python's _csc_bandwidths, the CompiledModel members that only fed C++ (has_algebraic, algebraic_jacobian_nnz, algebraic_jacobian_sparsity_pattern), and the tests that faked a disagreement to reach those checks.

Evidence

  • Before: six tests reached the triple-checked counts only by monkeypatching a disagreement: counts, output lengths, a Jacobian pattern outside its matrix, two algebraic-block mismatches, and a Newton set-up.
    After: nothing can disagree, so those six go, with test_csc_bandwidths and the vector-event refusal. New tests pin what the table carries and what C++ still refuses:
    • Rust (5): jac_pattern_writes_the_jacobian_in_csc_order, alg_jac_pattern_matches_the_order_alg_jac_assemble_fills, state_ids_mark_the_states_the_mass_matrix_leaves_out_as_algebraic, events_read_y_dot_flags_every_component_of_each_event, every_buffer_of_the_layout_queries_is_null_checked.
    • Python (9): the band and sparsity a rust model takes from its Jacobian; create_rust_solver_group refusing an atol of another length, evaluators of different models, an evaluator count other than num_solvers, and a sub-block over only some algebraic states; the Newton solver taking only atol's size from Python, refusing a model without algebraic states, and solving a DAE's algebraic states at its differential states.

Merge Danger

Door: two-way. create_rust_solver_group's signature changes, but it is pybammsolvers API from #5961, which no pybammsolvers release has shipped (0.13.0 predates it).

Blast Radius: IDAKLU on rust models. A pybamm-core whose table is shorter than the one pybammsolvers reads is refused before any call, as the ABI header already does, so the two extensions must be built from one tree (rust_idaklu_build.md says so).

Type of change

Internal refactor: no changelog fragment.

Key checklist

  • No style issues: pre-commit run
  • Unit tests pass (6676), as do the integration tests (736) and pybammsolvers' tests (82)
  • Rust: cargo test --all-features, clippy over every feature combination with -D warnings, and the 1.89 MSRV check
  • Tests added: the 5 Rust and 9 Python tests above
Rust execution engine stack: proposal #5667, showcase #5732

Each PR builds on the one above it.

# PR Adds
0 #5954 Function.create_copy keeps the function's class
0b #5955 CasADi's RegPower and uniform-grid interpolant lookups match evaluate
1 #5956 pybamm-core: expression graph, typed IR, interpreters, forward-mode AD
2 #5957 pybamm-core: CompiledModel, coloured Jacobians, the C ABI
3 #5958 pybamm.rust bindings, built into the pybamm wheel
4 #5959 Symbol.to_rust
5 #5960 convert_to_format = "rust" for ScipySolver, AlgebraicSolver and EIS
6 #5961 pybammsolvers: IDAKLU over the C ABI
7 #5962 IDAKLUSolver on rust models
7a #6133 IDAKLU sized from the evaluator's own table ← this PR
8 #5963 variables and sensitivities read in Rust
9 #5964 pybamm-core: diffsol DAE solver
10 #5965 pybamm.DiffsolSolver
10a #6134 Interpolant over input parameters and expressions; pybamm.rust exceptions, array-likes, threads and Ctrl-C
10b #6135 pybamm.rust.inspection; every tape, Jacobian and trajectory derived in one place
11 #5966 "rust" as the default convert_to_format

IDAKLU's set-up read 15 CompiledModel members in Python and passed 20
arguments to create_rust_solver_group, whose RustFunctions took 15, eight
of them counts the evaluator's FFI table already answers. Each count was
checked three times: in Python, in idaklu.cpp, and in C++ against the
table. The Rust Newton solver took its block split and Jacobian pattern
from Python the same way, with Python shifting the pattern's columns into
the block.

The table now also carries the Jacobian's CSC pattern, each state's IDA
id, the algebraic Jacobian's COO pattern, and which event components read
y_dot, appended within ABI version 2. RustFunctions reads a RustLayout
from its own evaluator and works out the band widths from the pattern, so
create_rust_solver_group takes only the evaluators, atol, rtol, options
and whether to use the algebraic sub-block. It refuses an atol of the
wrong length, evaluators with different counts, and a sub-block that
covers only some of the states the mass matrix marks algebraic. The
Newton solver reads its block from the table and shifts the columns
itself, beside the gather that puts the block at the trailing states.

IDA counts one root per event component, as the flags do, so C++ no
longer refuses a vector event; the lowering still does. The C++ pattern
checks, Python's _csc_bandwidths, and the CompiledModel members that only
fed C++ (has_algebraic, algebraic_jacobian_nnz,
algebraic_jacobian_sparsity_pattern) go, with the tests that faked a
disagreement to reach those checks.
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.41%. Comparing base (2bd3997) to head (a64ffe8).
⚠️ Report is 1 commits behind head on stack/07-idaklu-rust-mode.

Additional details and impacted files
@@                      Coverage Diff                      @@
##           stack/07-idaklu-rust-mode    #6133      +/-   ##
=============================================================
+ Coverage                      98.39%   98.41%   +0.02%     
=============================================================
  Files                            363      363              
  Lines                          38590    38976     +386     
=============================================================
+ Hits                           37969    38358     +389     
+ Misses                           621      618       -3     

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

@BradyPlanden
BradyPlanden removed this pull request from stack #6136 November 30, 2026 11:52
@BradyPlanden

Copy link
Copy Markdown
Member Author

Folded into #5962: the commit now sits on stack/07-idaklu-rust-mode, so #5962 presents IDAKLU's final set-up interface and is reviewed once.

@BradyPlanden
BradyPlanden deleted the stack/07a-rust-ffi-layout branch November 30, 2026 11:54
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.

1 participant