Previous topic

Algebraic Solvers

Next topic

Scipy Solver

This Page

Base Solvers

class pybamm.BaseSolver(method=None, rtol=1e-06, atol=1e-06, root_method='casadi', root_tol=1e-06, max_steps=1000)[source]

Solve a discretised model.

Parameters:
  • method (str, optional) – The method to use for integration, specific to each solver
  • 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, optional) – The method to use to find initial conditions (default is “casadi”). 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 the initial-condition solver (default is 1e-6).
  • max_steps (int, optional) – The maximum number of steps the solver will take before terminating (default is 1000).
calculate_consistent_state(model, time=0, y0_guess=None, inputs=None)[source]

Calculate consistent state for the algebraic equations through root-finding

Parameters:
  • model (pybamm.BaseModel) – The model for which to calculate initial conditions.
  • time (float) – The time at which to calculate the states
  • y0_guess (np.array) – Guess for the rootfinding
  • inputs (dict, optional) – Any input parameters to pass to the model when solving
Returns:

y0_consistent – Initial conditions that are consistent with the algebraic equations (roots of the algebraic equations)

Return type:

array-like, same shape as y0_guess

get_termination_reason(solution, events)[source]

Identify the cause for termination. In particular, if the solver terminated due to an event, (try to) pinpoint which event was responsible. Note that the current approach (evaluating all the events and then finding which one is smallest at the final timestep) is pretty crude, but is the easiest one that works for all the different solvers.

Parameters:
set_inputs(model, ext_and_inputs)[source]

Set values that are controlled externally, such as external variables and input parameters

Parameters:ext_and_inputs (dict) – Any external variables or input parameters to pass to the model when solving
set_up(model, inputs=None)[source]

Unpack model, perform checks, simplify and calculate jacobian.

Parameters:
  • model (pybamm.BaseModel) – The model whose solution to calculate. Must have attributes rhs and initial_conditions
  • inputs (dict, optional) – Any input parameters to pass to the model when solving
solve(model, t_eval, external_variables=None, inputs=None)[source]

Execute the solver setup and calculate the solution of the model at specified times.

Parameters:
  • model (pybamm.BaseModel) – The model whose solution to calculate. Must have attributes rhs and initial_conditions
  • t_eval (numeric type) – The times at which to compute the solution
  • external_variables (dict) – A dictionary of external variables and their corresponding values at the current time
  • inputs (dict, optional) – Any input parameters to pass to the model when solving
Raises:

pybamm.ModelError – If an empty model is passed (model.rhs = {} and model.algebraic={})

step(old_solution, model, dt, npts=2, external_variables=None, inputs=None, save=True)[source]

Step the solution of the model forward by a given time increment. The first time this method is called it executes the necessary setup by calling self.set_up(model).

Parameters:
  • old_solution (pybamm.Solution or None) – The previous solution to be added to. If None, a new solution is created.
  • model (pybamm.BaseModel) – The model whose solution to calculate. Must have attributes rhs and initial_conditions
  • dt (numeric type) – The timestep over which to step the solution
  • npts (int, optional) – The number of points at which the solution will be returned during the step dt. default is 2 (returns the solution at t0 and t0 + dt).
  • external_variables (dict) – A dictionary of external variables and their corresponding values at the current time
  • inputs (dict, optional) – Any input parameters to pass to the model when solving
  • save (bool) – Turn on to store the solution of all previous timesteps
Raises:

pybamm.ModelError – If an empty model is passed (model.rhs = {} and model.algebraic={})