Skip to main content
pybamm.CasadiSolver wraps the CasADi integrator (backed by SUNDIALS CVODES/IDA) and adds battery-specific logic for event detection and step-and-check integration.

Constructor

pybamm.CasadiSolver(
    mode="safe",
    rtol=1e-6,
    atol=1e-6,
    root_method="casadi",
    root_tol=1e-6,
    max_step_decrease_count=5,
    dt_max=None,
    extrap_tol=None,
    on_extrapolation=None,
    extra_options_setup=None,
    extra_options_call=None,
    return_solution_if_failed_early=False,
    perturb_algebraic_initial_conditions=None,
    integrators_maxcount=100,
)
mode
str
default:"\"safe\""
Integration strategy. See Modes below.
rtol
float
default:"1e-6"
Relative tolerance for the integrator.
atol
float
default:"1e-6"
Absolute tolerance for the integrator.
root_method
str
default:"\"casadi\""
Method for computing consistent initial conditions for DAE systems. "casadi" uses CasADi’s Newton rootfinder. Any valid scipy.optimize.root method string (e.g. "lm", "hybr") is also accepted.
root_tol
float
default:"1e-6"
Tolerance for the root-finding step.
max_step_decrease_count
int
default:"5"
Maximum number of times the step size may be halved before raising an error.
dt_max
float | None
default:"600"
Maximum global step size (seconds) in "safe" mode. Defaults to 600 s when None.
extrap_tol
float | None
default:"0"
Tolerance for detecting extrapolation beyond the solved interval.
on_extrapolation
str
default:"\"error\""
Behaviour when extrapolation is detected: "error", "warn", or "ignore".
extra_options_setup
dict | None
default:"{}"
Options forwarded to the CasADi integrator at creation time. Useful keys include "max_num_steps" and "print_stats". See the CasADi docs for the full list.
extra_options_call
dict | None
default:"{}"
Options forwarded to the CasADi integrator at call time.
return_solution_if_failed_early
bool
default:"false"
If True, return a partial Solution when the solver fails mid-simulation instead of raising an error.
perturb_algebraic_initial_conditions
bool | None
default:"None"
Whether to perturb algebraic initial conditions to avoid singularities. Defaults to True in "safe" mode, False otherwise.
integrators_maxcount
int
default:"100"
LRU cache size for compiled CasADi integrators. Set to 0 for unbounded caching.

Modes

ModeDescriptionRecommended for
"safe"Step-and-check in global steps of dt_max, detecting eventsFull charge/discharge cycles
"fast"Direct integration; events are not trackedDrive cycles, no cut-off events
"fast with events"Direct integration then retroactively locates events (experimental)Testing
"safe without grid"Step-by-step without pre-building the time grid (experimental)Memory-constrained runs

Examples

import pybamm

model = pybamm.lithium_ion.DFN()
param = pybamm.ParameterValues("Chen2020")

solver = pybamm.CasadiSolver(mode="safe", rtol=1e-6, atol=1e-6)

sim = pybamm.Simulation(model, parameter_values=param, solver=solver)
sol = sim.solve([0, 3600])
print(sol["Terminal voltage [V]"].entries[-1])
"fast with events" and "safe without grid" modes are experimental and may change without notice.

Build docs developers (and LLMs) love