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._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.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.

_catch_errors(inputs: list[Inputs])[source]#
_create_experiment_simulation() pybamm.Simulation[source]#

Create a simulation with current configuration and an experiment.

_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._utils.FailedSolution][source]#

Convert failed solutions to FailedSolution objects.

_pybamm_solve(inputs: Inputs | list[Inputs] | None) pybamm.Solution | list[pybamm.Solution][source]#

A function that runs the simulation using the built model.

_set_protocol(protocol: pybamm.Experiment | pybop._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[source]#

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_experiment_with_rebuild(inputs: list[Inputs]) list[pybamm.Solution][source]#

Simulate an experiment, rebuilding the simulation for each set of inputs.

_simulate_experiment_without_rebuild(inputs: list[Inputs]) list[pybamm.Solution][source]#

Simulate an experiment without rebuilding the PyBaMM model.

_solve_in_time_with_rebuild(inputs: list[Inputs]) list[pybamm.Solution][source]#

Solve in time, rebuilding the model for each set of inputs.

_solve_in_time_without_rebuild(inputs: list[Inputs]) list[pybamm.Solution][source]#

Solve in time without rebuilding the PyBaMM model.

batch_solve(inputs: list[Inputs], calculate_sensitivities: bool = False) list[pybamm.Solution | pybop._utils.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]

build_model() None[source]#

Build the model using the given parameter values.

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.

rebuild_model(inputs: pybop.parameters.parameter.Inputs) None[source]#

Update the parameter values and rebuild the model, if required.

set_output_variables(value: list[str] | None)[source]#
solve(inputs: Inputs | list[Inputs] | None = None, calculate_sensitivities: bool = False) pybamm.Solution | pybop._utils.FailedSolution | list[pybamm.Solution | pybop._utils.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]

_built_model = None[source]#
_calculate_sensitivities = False[source]#
_discretisation_kwargs[source]#
_experiment = None[source]#
_geometry[source]#
_initial_state = None[source]#
_input_parameter_names[source]#
_model[source]#
_output_variables = None[source]#
_parameter_values[source]#
_requires_model_rebuild = False[source]#
_sim_experiment = None[source]#
_solve = None[source]#
_solver = None[source]#
_spatial_methods[source]#
_submesh_types[source]#
_t_eval = None[source]#
_t_interp = None[source]#
_var_pts[source]#
property built_model[source]#
debug_mode = False[source]#
exception = ['These parameter values are infeasible.'][source]#
property experiment[source]#
property has_sensitivities[source]#
property initial_state[source]#
property input_parameter_names[source]#
property model[source]#
property output_variables[source]#
property parameter_values[source]#
property requires_model_rebuild[source]#
property solver[source]#
verbose = False[source]#
warning_patterns = ['Ah is greater than', 'Non-physical point encountered'][source]#