Skip to content

Add pybammsolvers nonlinear solver#5459

Open
MarcBerliner wants to merge 32 commits intomainfrom
nonlinear-solver
Open

Add pybammsolvers nonlinear solver#5459
MarcBerliner wants to merge 32 commits intomainfrom
nonlinear-solver

Conversation

@MarcBerliner
Copy link
Copy Markdown
Member

Description

Replaces CasadiAlgebraicSolver as the default nonlinear solver. This fixes an issue where the algebraic solver could terminate early when hitting step change tolerances but not the desired absolute tolerance. The new NonlinearSolver is tightly integrated with sundials to satisfy the same error tolerance and avoid expensive calls to IDACalcIC where only analgebraic sub-solver is necessary

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

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

@MarcBerliner MarcBerliner marked this pull request as ready for review April 23, 2026 18:37
@MarcBerliner MarcBerliner requested a review from a team as a code owner April 23, 2026 18:37
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

❌ Patch coverage is 92.22798% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.15%. Comparing base (66acdf4) to head (c00dca6).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/pybamm/solvers/idaklu_solver.py 86.36% 6 Missing ⚠️
src/pybamm/solvers/nonlinear_solver.py 95.79% 5 Missing ⚠️
src/pybamm/solvers/base_solver.py 85.71% 3 Missing ⚠️
src/pybamm/solvers/algebraic_solver.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5459      +/-   ##
==========================================
- Coverage   98.29%   98.15%   -0.15%     
==========================================
  Files         337      338       +1     
  Lines       30950    31119     +169     
==========================================
+ Hits        30423    30544     +121     
- Misses        527      575      +48     

☔ View full report in Codecov by Sentry.
📢 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
Copy link
Copy Markdown
Member Author

@martinjrobins on my machine, this fixes the lithium-plating-composite.ipynb initialization issue with unified experiments that you mentioned in #5422 (comment)

Copy link
Copy Markdown
Member

@BradyPlanden BradyPlanden left a comment

Choose a reason for hiding this comment

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

This looks great - thanks @MarcBerliner! Just a few comments/questions

Comment thread pyproject.toml
"Topic :: Scientific/Engineering",
]
dependencies = [
"pybammsolvers>=0.6.0,<0.7.0",
Copy link
Copy Markdown
Member

@BradyPlanden BradyPlanden Apr 29, 2026

Choose a reason for hiding this comment

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

issue: I think it's worth removing the upper pin on pybammsolvers. I just realised we haven't been using 0.7.0 since it wasn't updated here. It probably better to fail loudly, then to silently forget about it.

Comment on lines +466 to +472
alg_res_fn = None
jac_alg_fn = None

if alg_res_fn is None or not callable(getattr(alg_res_fn, "serialize", None)):
alg_res_fn = casadi.Function("empty_alg_res", [], [])
if jac_alg_fn is None or not callable(getattr(jac_alg_fn, "serialize", None)):
jac_alg_fn = casadi.Function("empty_alg_jac", [], [])
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.

suggestion (non-blocking): I believe this can be compressed into a single if-else statement + a helper.

"Charge at 1 A until 4.1 V",
"Hold at 4.1 V until C/2",
"Discharge at 2 W for 1 hour",
"Discharge at C/10 for 1 hour",
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.

question: was there an issue with performance or coverage on the previous experiment?

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.

2 participants