Previous topic

Discretisation and spatial methods

Next topic

Spatial Method

This Page

Discretisation

class pybamm.Discretisation(mesh=None, spatial_methods=None)[source]

The discretisation class, with methods to process a model and replace Spatial Operators with Matrices and Variables with StateVectors

Parameters:
  • mesh (pybamm.Mesh) – contains all submeshes to be used on each domain
  • spatial_methods (dict) – a dictionary of the spatial methods to be used on each domain. The keys correspond to the model domains and the values to the spatial method.
check_model(model)[source]

Perform some basic checks to make sure the discretised model makes sense.

check_tab_conditions(symbol, bcs)[source]

Check any boundary conditions applied on “negative tab”, “positive tab” and “no tab”. For 1D current collector meshes, these conditions are converted into boundary conditions on “left” (tab at z=0) or “right” (tab at z=l_z) depending on the tab location stored in the mesh. For 2D current collector meshes, the boundary conditions can be applied on the tabs directly.

Parameters:
  • symbol (pybamm.expression_tree.symbol.Symbol) – The symbol on which the boundary conditions are applied.
  • bcs (dict) – The dictionary of boundary conditions (a dict of {side: equation}).
Returns:

The dictionary of boundary conditions, with the keys changed to “left” and “right” where necessary.

Return type:

dict

check_variables(model)[source]

Check variables in variable list against rhs Be lenient with size check if the variable in model.variables is broadcasted, or a concatenation (if broadcasted, variable is a multiplication with a vector of ones)

create_jacobian(model)[source]

Creates Jacobian of the discretised model. Note that the model is assumed to be of the form M*y_dot = f(t,y), where M is the (possibly singular) mass matrix. The Jacobian is df/dy.

Note: At present, calculation of the Jacobian is deferred until after simplification, since it is much faster to compute the Jacobian of the simplified model. However, in some use cases (e.g. running the same model multiple times but with different parameters) it may be more efficient to compute the Jacobian once, before simplification, so that parameters in the Jacobian can be updated (see PR #670).

Parameters:model (pybamm.BaseModel) – Discretised model. Must have attributes rhs, initial_conditions and boundary_conditions (all dicts of {variable: equation})
Returns:The expression trees corresponding to the Jacobian of the model
Return type:pybamm.Concatenation
create_mass_matrix(model)[source]

Creates mass matrix of the discretised model. Note that the model is assumed to be of the form M*y_dot = f(t,y), where M is the (possibly singular) mass matrix.

Parameters:model (pybamm.BaseModel) – Discretised model. Must have attributes rhs, initial_conditions and boundary_conditions (all dicts of {variable: equation})
Returns:
  • pybamm.Matrix – The mass matrix
  • pybamm.Matrix – The inverse of the ode part of the mass matrix (required by solvers which only accept the ODEs in explicit form)
process_boundary_conditions(model)[source]

Discretise model boundary_conditions, also converting keys to ids

Parameters:model (pybamm.BaseModel) – Model to dicretise. Must have attributes rhs, initial_conditions and boundary_conditions (all dicts of {variable: equation})
Returns:Dictionary of processed boundary conditions
Return type:dict
process_dict(var_eqn_dict)[source]

Discretise a dictionary of {variable: equation}, broadcasting if necessary (can be model.rhs, model.algebraic, model.initial_conditions or model.variables).

Parameters:var_eqn_dict (dict) – Equations ({variable: equation} dict) to dicretise (can be model.rhs, model.algebraic, model.initial_conditions or model.variables)
Returns:new_var_eqn_dict – Discretised equations
Return type:dict
process_initial_conditions(model)[source]

Discretise model initial_conditions.

Parameters:model (pybamm.BaseModel) – Model to dicretise. Must have attributes rhs, initial_conditions and boundary_conditions (all dicts of {variable: equation})
Returns:Tuple of processed_initial_conditions (dict of initial conditions) and concatenated_initial_conditions (numpy array of concatenated initial conditions)
Return type:tuple
process_model(model, inplace=True, check_model=True)[source]

Discretise a model. Currently inplace, could be changed to return a new model.

Parameters:
  • model (pybamm.BaseModel) – Model to dicretise. Must have attributes rhs, initial_conditions and boundary_conditions (all dicts of {variable: equation})
  • inplace (bool, optional) – If True, discretise the model in place. Otherwise, return a new discretised model. Default is True.
  • check_model (bool, optional) – If True, model checks are performed after discretisation. For large systems these checks can be slow, so can be skipped by setting this option to False. When developing, testing or debugging it is recommened to leave this option as True as it may help to identify any errors. Default is True.
Returns:

model_disc – The discretised model. Note that if inplace is True, model will have also been discretised in place so model == model_disc. If inplace is False, model != model_disc

Return type:

pybamm.BaseModel

Raises:

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

process_rhs_and_algebraic(model)[source]

Discretise model equations - differential (‘rhs’) and algebraic.

Parameters:model (pybamm.BaseModel) – Model to dicretise. Must have attributes rhs, initial_conditions and boundary_conditions (all dicts of {variable: equation})
Returns:Tuple of processed_rhs (dict of processed differential equations), processed_concatenated_rhs, processed_algebraic (dict of processed algebraic equations) and processed_concatenated_algebraic
Return type:tuple
process_symbol(symbol)[source]

Discretise operators in model equations. If a symbol has already been discretised, the stored value is returned.

Parameters:symbol (pybamm.expression_tree.symbol.Symbol) – Symbol to discretise
Returns:Discretised symbol
Return type:pybamm.expression_tree.symbol.Symbol
set_external_variables(model)[source]

Add external variables to the list of variables to account for, being careful about concatenations

set_internal_boundary_conditions(model)[source]

A method to set the internal boundary conditions for the submodel. These are required to properly calculate the gradient. Note: this method modifies the state of self.boundary_conditions.

set_variable_slices(variables)[source]

Sets the slicing for variables.

Parameters:variables (iterable of pybamm.Variables) – The variables for which to set slices