Previous topic

Algebraic Solvers

Scipy Solver

Base Solvers¶

class pybamm.BaseSolver(method=None, tol=1e-08)[source]

Solve a discretised model.

Parameters: tolerance (float, optional) – The tolerance for the solver (default is 1e-8).
compute_solution(model, t_eval)[source]

Calculate the solution of the model at specified times. Note: this does not execute the solver setup.

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
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: solution (pybamm.Solution) – The solution object events (dict) – Dictionary of events
set_up(model)[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 pybamm.SolverError – If the model contains any algebraic equations (in which case a DAE solver should be used instead)
solve(model, t_eval)[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 pybamm.ModelError – If an empty model is passed (model.rhs = {} and model.algebraic={})
step(model, dt, npts=2)[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: 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). pybamm.ModelError – If an empty model is passed (model.rhs = {} and model.algebraic={})
class pybamm.OdeSolver(method=None, tol=1e-08)[source]

Solve a discretised model.

Parameters: tolerance (float, optional) – The tolerance for the solver (default is 1e-8).
compute_solution(model, t_eval)[source]

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
integrate(derivs, y0, t_eval, events=None, mass_matrix=None, jacobian=None)[source]

Solve a model defined by dydt with initial conditions y0.

Parameters: derivs (method) – A function that takes in t and y and returns the time-derivative dydt y0 (numeric type) – The initial conditions t_eval (numeric type) – The times at which to compute the solution events (method, optional) – A function that takes in t and y and returns conditions for the solver to stop mass_matrix (array_like, optional) – The (sparse) mass matrix for the chosen spatial method. jacobian (method, optional) – A function that takes in t and y and returns the Jacobian
set_up(model)[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 pybamm.SolverError – If the model contains any algebraic equations (in which case a DAE solver should be used instead)
class pybamm.DaeSolver(method=None, tol=1e-08, root_method='lm', root_tol=1e-06, max_steps=1000)[source]

Solve a discretised model.

Parameters: tolerance (float, optional) – The tolerance for the solver (default is 1e-8). root_method (str, optional) – The method to use to find initial conditions (default is “lm”) tolerance – The tolerance for the initial-condition solver (default is 1e-8). max_steps (int, optional) – The maximum number of steps the solver will take before terminating (default is 1000).
calculate_consistent_initial_conditions(rhs, algebraic, y0_guess, jac=None)[source]

Calculate consistent initial conditions for the algebraic equations through root-finding

Parameters: rhs (method) – Function that takes in t and y and returns the value of the differential equations algebraic (method) – Function that takes in t and y and returns the value of the algebraic equations y0_guess (array-like) – Array of the user’s guess for the initial conditions, used to initialise the root finding algorithm jac (method) – Function that takes in t and y and returns the value of the jacobian for the algebraic equations y0_consistent – Initial conditions that are consistent with the algebraic equations (roots of the algebraic equations) array-like, same shape as y0_guess
compute_solution(model, t_eval)[source]

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
integrate(residuals, y0, t_eval, events=None, mass_matrix=None, jacobian=None)[source]

Solve a DAE model defined by residuals with initial conditions y0.

Parameters: residuals (method) – A function that takes in t, y and ydot and returns the residuals of the equations y0 (numeric type) – The initial conditions t_eval (numeric type) – The times at which to compute the solution events (method, optional) – A function that takes in t and y and returns conditions for the solver to stop mass_matrix (array_like, optional) – The (sparse) mass matrix for the chosen spatial method. jacobian (method, optional) – A function that takes in t, y and ydot and returns the Jacobian
set_up(model)[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 pybamm.SolverError – If the model contains any algebraic equations (in which case a DAE solver should be used instead)