Casadi Solver#

class pybamm.CasadiSolver(mode='safe', rtol=1e-06, atol=1e-06, root_method='casadi', root_tol=1e-06, max_step_decrease_count=5, dt_max=None, extrap_tol=None, extra_options_setup=None, extra_options_call=None, return_solution_if_failed_early=False, perturb_algebraic_initial_conditions=None, integrators_maxcount=100)[source]#

Solve a discretised model, using CasADi.

Parameters:
  • mode (str) –

    How to solve the model (default is “safe”):

    • ”fast”: perform direct integration, without accounting for events. Recommended when simulating a drive cycle or other simulation where no events should be triggered.

    • ”fast with events”: perform direct integration of the whole timespan, then go back and check where events were crossed. Experimental only.

    • ”safe”: perform step-and-check integration in global steps of size dt_max, checking whether events have been triggered. Recommended for simulations of a full charge or discharge.

    • ”safe without grid”: perform step-and-check integration step-by-step. Takes more steps than “safe” mode, but doesn’t require creating the grid each time, so may be faster. Experimental only.

  • rtol (float, optional) – The relative tolerance for the solver (default is 1e-6).

  • atol (float, optional) – The absolute tolerance for the solver (default is 1e-6).

  • root_method (str or pybamm algebraic solver class, optional) – The method to use to find initial conditions (for DAE solvers). If a solver class, must be an algebraic solver class. If “casadi”, the solver uses casadi’s Newton rootfinding algorithm to find initial conditions. Otherwise, the solver uses ‘scipy.optimize.root’ with method specified by ‘root_method’ (e.g. “lm”, “hybr”, …)

  • root_tol (float, optional) – The tolerance for root-finding. Default is 1e-6.

  • max_step_decrease_count (float, optional) – The maximum number of times step size can be decreased before an error is raised. Default is 5.

  • dt_max (float, optional) – The maximum global step size (in seconds) used in “safe” mode. If None the default value is 600 seconds.

  • extrap_tol (float, optional) – The tolerance to assert whether extrapolation occurs or not. Default is 0.

  • extra_options_setup (dict, optional) –

    Any options to pass to the CasADi integrator when creating the integrator. Please consult CasADi documentation for details. Some useful options:

    • ”max_num_steps”: Maximum number of integrator steps

    • ”print_stats”: Print out statistics after integration

  • extra_options_call (dict, optional) –

    Any options to pass to the CasADi integrator when calling the integrator. Please consult CasADi documentation for details.

  • return_solution_if_failed_early (bool, optional) – Whether to return a Solution object if the solver fails to reach the end of the simulation, but managed to take some successful steps. Default is False.

  • perturb_algebraic_initial_conditions (bool, optional) – Whether to perturb algebraic initial conditions to avoid a singularity. This can sometimes slow down the solver, but is kept True as default for “safe” mode as it seems to be more robust (False by default for other modes).

  • integrators_maxcount (int, optional) – The maximum number of integrators that the solver will retain before ejecting past integrators using an LRU methodology. A value of 0 or None leaves the number of integrators unbound. Default is 100.

Extends: pybamm.solvers.base_solver.BaseSolver

create_integrator(model, inputs, t_eval=None, use_event_switch=False)[source]#

Method to create a casadi integrator object. If t_eval is provided, the integrator uses t_eval to make the grid. Otherwise, the integrator has grid [0,1].