pybop.pybamm.simulator#

Classes#

Simulator

A class to automatically build/rebuild and solve a pybamm.Simulation for a given model and protocol.

Module Contents#

class pybop.pybamm.simulator.Simulator(model: pybamm.BaseModel, parameter_values: pybamm.ParameterValues | None = None, initial_state: dict | None = None, protocol: pybamm.Experiment | pybop.processing.dataset.Dataset | numpy.ndarray | None = None, solver: pybamm.BaseSolver | None = None, output_variables: list[str] | None = None, geometry: pybamm.Geometry | None = None, submesh_types: dict | None = None, var_pts: dict | None = None, spatial_methods: dict | None = None, discretisation_kwargs: dict | None = None, build_every_time: bool = False)[source]#

Bases: pybop.simulators.base_simulator.BaseSimulator

A class to automatically build/rebuild and solve a pybamm.Simulation for a given model and protocol.

There are two contexts in which this class can be used: 1. A pybamm model can be built once and then run multiple times with different inputs. 2. A pybamm model needs to be built and then run for each set of inputs, for example in the case

where one of the inputs is a geometric parameter which requires a new mesh.

The logic for (1) and (2) happens automatically. To override this logic, the argument build_every_time can be set to True which will force (2) to occur.

Parameters:
  • model (pybamm.BaseModel) – The PyBaMM model to be used.

  • parameter_values (pybamm.ParameterValues, optional) – The parameter values to be used in the model.

  • initial_state (dict, optional) – A valid initial state, e.g. “Initial open-circuit voltage [V]” or ``”Initial SoC”`. Defaults to None, indicating that the existing initial state of charge (for an ECM) or initial concentrations (for an EChem model) will be used.

  • protocol (pybamm.Experiment | Dataset | np.ndarray | None) – The protocol as an experiment, a 1D array of values or dataset containing (time) domain data.

  • solver (pybamm.BaseSolver, optional) – The solver to use to solve the model. If None, uses pybop.pybamm.RecommendedSolver.

  • output_variables (list, optional) – A list of output variables to return.

  • geometry (pybamm.Geometry, optional) – The geometry upon which to solve the model.

  • submesh_types (dict, optional) – A dictionary of the types of submesh to use on each subdomain.

  • var_pts (dict, optional) – A dictionary of the number of points used by each spatial variable.

  • spatial_methods (dict, optional) – A dictionary of the types of spatial method to use on each domain (e.g. pybamm.FiniteVolume).

  • discretisation_kwargs (dict, optional) – Any keyword arguments to pass to the Discretisation class. See pybamm.Discretisation for details.

  • build_every_time (bool, optional) – If True, the model will be rebuilt every evaluation. Otherwise, the need to rebuild will be determined automatically.

__getstate__()[source]#
__setstate__(state)[source]#
_catch_errors(inputs: list[Inputs], options: dict)[source]#
_determine_rebuild_requirement(build_every_time: bool | None) bool[source]#

Determine if model needs rebuilding on each evaluation.

_process_solutions(solutions: list[pybamm.Solution]) list[pybamm.Solution | pybop.simulators.failed_solution.FailedSolution][source]#

Convert failed solutions to FailedSolution objects.

_set_protocol(protocol: pybamm.Experiment | pybop.processing.dataset.Dataset | numpy.ndarray | None)[source]#

Set up the protocol for the simulation.

Parameters:

protocol (pybamm.Experiment | pybop.Dataset | np.ndarray | None) – The protocol as an experiment, a 1D array of values or dataset containing (time) domain data.

t_eval#

The time points to stop the solver at. These points should be used to inform the solver of discontinuities in the solution.

Type:

np.ndarray, optional

t_interp#

The time points at which to interpolate the solution. If None, no interpolation will be done.

Type:

np.ndarray, optional

experiment#

The experimental conditions under which to solve the model.

Type:

pybamm.Experiment | string | list, optional

_set_up_solution_method(output_variables: list[str] | None = None) None[source]#

Configure the mode of operation.

_simulate_with_rebuild(inputs: list[Inputs], options: dict) list[pybamm.Solution][source]#

Simulate, rebuilding the simulation for each set of inputs.

_simulate_without_rebuild(inputs: list[Inputs], options: dict) list[pybamm.Solution][source]#

Simulate without rebuilding the PyBaMM model.

convert_to_pybamm_initial_state(initial_state: dict | None) None[source]#

Convert an initial state of charge into a float and an initial open-circuit voltage into a string ending in “V”.

Parameters:

initial_state (dict) – A valid initial state, e.g. the initial state of charge or open-circuit voltage.

Returns:

If float, this value is used as the initial state of charge (as a decimal between 0 and 1). If str ending in “V”, this value is used as the initial open-circuit voltage.

Return type:

float or str

Raises:

ValueError – If the input is not a dictionary with a single, valid key.

copy()[source]#

Return a copy of the simulation.

create_simulation() pybamm.Simulation[source]#

Create a simulation with current configuration and optional experiment.

set_output_variables(value: list[str] | None)[source]#
solve(inputs: Inputs | list[Inputs] | None = None, calculate_sensitivities: bool = False) pybamm.Solution | pybop.simulators.failed_solution.FailedSolution | list[pybamm.Solution | pybop.simulators.failed_solution.FailedSolution][source]#

Run the simulation using the built model and solver for one or more sets of inputs and return the solution object(s).

Parameters:
  • inputs (Inputs | list[Inputs], optional) – Input parameters (default: None).

  • calculate_sensitivities (bool) – Whether to also return the sensitivities (default: False).

Returns:

The solution object(s).

Return type:

pybamm.Solution | pybop.FailedSolution | list[pybamm.Solution | pybop.FailedSolution]

solve_batch(inputs: list[Inputs], calculate_sensitivities: bool = False) list[pybamm.Solution | pybop.simulators.failed_solution.FailedSolution][source]#

Run the simulation using the built model and solver for each set of inputs and return dict-like simulation results.

Parameters:
  • inputs (list[Inputs]) – A list of input parameters.

  • calculate_sensitivities (bool) – Whether to also return the sensitivities (default: False).

Returns:

A list of len(inputs) containing the solution objects.

Return type:

list[pybamm.Solution | pybop.FailedSolution]

_discretisation_kwargs#
_experiment = None#
_geometry#
_initial_state = None#
_input_parameter_names#
_model#
_output_variables = None#
_parameter_values#
_requires_model_rebuild = False#
_simulation = None#
_solve = None#
_solver = None#
_spatial_methods#
_submesh_types#
_t_eval = None#
_t_interp = None#
_var_pts#
property built_model#
debug_mode = False#
property experiment#
property has_sensitivities#
property initial_state#
property input_parameter_names#
property model#
property output_variables#
property parameter_values#
property requires_model_rebuild#
property solver#
verbose = False#