Skip to content

fix: keep a function's class when copying it without simplification - #5954

Merged
BradyPlanden merged 1 commit into
mainfrom
stack/00-function-copy-fix
Oct 28, 2026
Merged

BradyPlanden merged 1 commit into
mainfrom
stack/00-function-copy-fix

Conversation

@BradyPlanden

Copy link
Copy Markdown
Member

Description

Function.create_copy(perform_simplifications=False) built a generic pybamm.Function instead of a copy of the original class. ParameterValues takes this path for every function in a parameter set loaded with Serialise.load_parameters, since it rebuilds the tree with simplify=False (#5779). After processing, pybamm.exp, pybamm.tanh and the other specific functions were generic Function nodes, and a copied Arcsinh2 could not be evaluated.

create_copy always goes through _function_new_copy(children, perform_simplifications), which gains the flag on Function, SpecificFunction and RegPower. With the flag off, a copy keeps its class, RegPower.delta and Arcsinh2.eps. Arcsinh2 is never simplified, so it ignores the flag.

_function_new_copy is private, but a subclass outside PyBaMM that overrides it with the old one-argument signature raises TypeError on every copy. A Function subclass with no override still copies as a generic pybamm.Function, as on main.

This is the base of the Rust execution engine stack (#5732). The lowering in #5959 dispatches on a function's class and rejects a generic Function, so without this fix a reloaded parameter set cannot be solved once #5966 makes "rust" the default. It uses nothing else from the stack and can merge on its own.

Type of change

Bug fix, under ## Bug fixes in CHANGELOG.md.

Key checklist

  • No style issues: pre-commit run
  • Unit tests pass (N)
  • Tests added: a copy of Exp, Tanh, Arcsinh2 and RegPower keeps its class, its replaced child and delta, with and without simplification, and a copied Arcsinh2 evaluates as the original; a saved and reloaded Marquis2019 parameter set keeps Exp in a processed SPM, with no generic Function left
Rust execution engine stack: proposal #5667, showcase #5732

Each PR is one commit on the one above it.

# PR Adds
0 #5954 Function.create_copy keeps the function's class ← this PR
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
8 #5963 variables and sensitivities read in Rust
9 #5964 pybamm-core: diffsol DAE solver
10 #5965 pybamm.DiffsolSolver
11 #5966 "rust" as the default convert_to_format

@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.25%. Comparing base (e50fd5c) to head (743663a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5954   +/-   ##
=======================================
  Coverage   98.25%   98.25%           
=======================================
  Files         350      350           
  Lines       36102    36109    +7     
=======================================
+ Hits        35471    35478    +7     
  Misses        631      631           

☔ 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 marked this pull request as ready for review October 22, 2026 15:35
@BradyPlanden
BradyPlanden requested a review from a team as a code owner October 22, 2026 15:35
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

ProjectPyBaMM
Branchstack/00-function-copy-fix
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

MarcBerliner
MarcBerliner previously approved these changes Oct 23, 2026
Function.create_copy(perform_simplifications=False) built a generic
pybamm.Function, so pybamm.exp and the other specific functions lost their
class and an Arcsinh2 copy could not be evaluated. Since #5779,
ParameterValues takes that path for a parameter set loaded with
Serialise.load_parameters. _function_new_copy now takes
perform_simplifications, as _unary_new_copy does.
@BradyPlanden
BradyPlanden merged commit 609b54e into main Oct 28, 2026
47 checks passed
@BradyPlanden
BradyPlanden deleted the stack/00-function-copy-fix branch October 28, 2026 15:28
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