Skip to content

Conversation

MarcBerliner
Copy link
Member

@MarcBerliner MarcBerliner commented Apr 18, 2025

Description

This PR updates the CasadiAlgebraicSolver with three key improvements:

Caches the root solver:

  • This makes a cached and reusable version of the root solver generated by casadi, which works for different values of time, initial values of the differential states, and input parameters. This reduces the runtime of the DFN by ~6.5% in the example below.

Added a step_tol parameter:

  • This sets the tolerance for the Newton iteration step norm and defaults to 1e-4 (equal to IDA's rtol). Previously, the casadi rootfinder used a much tighter default of 1e-12, which caused issues. That strict value made it harder for the line search algorithm to pick a good step size, often leading to solver failure. By relaxing step_tol, the solver is now more robust, typically requires fewer iterations to converge, and adds minimal error to the solve

Improved convergence criterion:

  • The solver now uses casadi’s success flag and adds a safeguard to ensure the solution doesn’t contain nan or inf values

Fixes #4952

Example:

import pybamm
from tqdm import tqdm

experiment = pybamm.Experiment(
    [
        (
            "Discharge at C/10 for 10 hours or until 3.3 V",
            "Rest for 1 hour",
            "Charge at 1 A until 4.1 V",
            "Hold at 4.1 V until 50 mA",
            "Rest for 1 hour",
        )
    ]
    * 3,
)
model = pybamm.lithium_ion.DFN()
sim = pybamm.Simulation(model, experiment=experiment, solver=pybamm.IDAKLUSolver())
sim.solve()

time = 0
repeats = 100
timer = pybamm.Timer()
for i in tqdm(range(repeats)):
    timer.reset()
    sol = sim.solve()
    time += timer.time()
print(time / repeats)

# Time (new): 327.475 ms
# Time (old): 349.887 ms

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

Copy link

codecov bot commented Apr 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.60%. Comparing base (eb9f5fc) to head (07e8708).
Report is 106 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4985      +/-   ##
===========================================
- Coverage    98.62%   98.60%   -0.02%     
===========================================
  Files          304      304              
  Lines        23755    23809      +54     
===========================================
+ Hits         23428    23478      +50     
- Misses         327      331       +4     

☔ 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 MarcBerliner marked this pull request as ready for review April 18, 2025 20:06
@MarcBerliner MarcBerliner requested a review from a team as a code owner April 18, 2025 20:06
@kratman kratman enabled auto-merge (squash) April 23, 2025 15:44
@kratman kratman merged commit b4c9df8 into develop Apr 23, 2025
25 of 26 checks passed
@kratman kratman deleted the mdb/improve-casadi-algebraic branch April 23, 2025 16:05
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.

[Bug]: Getting Different Results for Coupled Degradation Example (Solver?)
2 participants