pybop._utils#

Classes#

FailedSolution

return isinstance(x, int | float | np.number)

FailedVariable

Check if a variable is numeric.

RecommendedSolver

A shortcut for creating the PyBaMM solver recommended for optimisation.

SymbolReplacer

Helper class to replace all instances of one or more symbols in an expression tree

Functions#

add_spaces(string)

Return the class name as a string with spaces before each new capitalised word.

is_numeric(x)

Check if a variable is numeric.

Module Contents#

class pybop._utils.FailedSolution(variable_names: list[str], parameter_names: list[str])[source]#

return isinstance(x, int | float | np.number) Container for a failed PyBaMM solution that returns [np.inf] for all processed variables.

This class mimics the interface of a successful PyBaMM solution but returns infinity values to indicate failure while maintaining API compatibility.

Parameters:
  • variable_names – List of variable names in the solution

  • parameter_names – List of parameter names for sensitivity analysis

Example

>>> solution = FailedSolution(["Voltage [V]"], ["Negative particle radius [m]"])
>>> voltage = solution["Voltage [V]"]
>>> print(voltage.data)  # np.ndarray([inf])
__getattr__(name)[source]#
__getitem__(key)[source]#
_initialise_variables() None[source]#

Initialise all variables with failed state.

_validate_inputs(variable_names: list[str], parameter_names: list[str] | None) None[source]#

Validate constructor inputs.

copy()[source]#
items() list[tuple[str, FailedVariable]][source]#

Get all variable name-value pairs.

keys() list[str][source]#

Get all variable names.

plot(*args, **kwargs)[source]#
save(*args, **kwargs)[source]#
values() list[FailedVariable][source]#

Get all variables.

_parameter_names[source]#
_t_eval: numpy.ndarray[source]#
_variable_names[source]#
_variables: {str, FailedVariable}[source]#
cycles: int | None = None[source]#
integration_time: float = 0.0[source]#
property parameter_names: list[str][source]#

Get list of parameter names (read-only).

solve_time: float = 0.0[source]#
property t_eval: numpy.ndarray[source]#

Time evaluation points (returns [inf] for failed solutions).

termination: str = 'failure'[source]#
property variable_names: list[str][source]#

Get list of variable names (read-only).

class pybop._utils.FailedVariable[source]#

Check if a variable is numeric. Container for a failed PyBaMM variable that returns np.inf.

Parameters:
  • name – Variable name

  • data – Array data, defaults to [np.inf]

  • sensitivities – Sensitivity data mapping parameter names to arrays

__post_init__() None[source]#

Validate inputs after initialisation.

data: numpy.ndarray[source]#
name: str[source]#
sensitivities: {str, np.ndarray}[source]#
class pybop._utils.RecommendedSolver(output_variables: list[str] | None = None)[source]#

Bases: pybamm.IDAKLUSolver

A shortcut for creating the PyBaMM solver recommended for optimisation.

class pybop._utils.SymbolReplacer(symbol_replacement_map: dict[pybamm.Symbol, pybamm.Symbol], processed_symbols: dict[pybamm.Symbol, pybamm.Symbol] | None = None, process_initial_conditions: bool = True)[source]#

Helper class to replace all instances of one or more symbols in an expression tree with another symbol, as defined by the dictionary symbol_replacement_map Originally developed by pybamm: pybamm-team/pybamm

Parameters:
  • symbol_replacement_map (dict {pybamm.Symbol -> pybamm.Symbol}) – Map of which symbols should be replaced by which.

  • processed_symbols (dict {pybamm.Symbol -> pybamm.Symbol}, optional) – cached replaced symbols

  • process_initial_conditions (bool, optional) – Whether to process initial conditions, default is True

_process_events(events: list) list[source]#
_process_symbol(symbol: pybamm.Symbol) pybamm.Symbol[source]#
process_boundary_conditions(model)[source]#

Process boundary conditions for a PybaMM model class Boundary conditions are dictionaries {“left”: left bc, “right”: right bc} in general, but may be imposed on the tabs (or not on the tab) for a small number of variables, e.g. {“negative tab”: neg. tab bc, “positive tab”: pos. tab bc “no tab”: no tab bc}.

process_model(unprocessed_model, inplace=True)[source]#

Replace all instances of a symbol in a PyBaMM model class.

Parameters:
  • unprocessed_model (pybamm.BaseModel) – Model class to assign parameter values to

  • inplace (bool, optional) – If True, replace the parameters in the model in place. Otherwise, return a new model with parameter values set (default: True).

process_symbol(symbol)[source]#

This function recurses down the tree, replacing any symbols in self._symbol_replacement_map.keys() with their corresponding value

Parameters:

symbol (pybamm.Symbol) – The symbol to replace

Returns:

Symbol with all replacements performed

Return type:

pybamm.Symbol

_process_initial_conditions = True[source]#
_processed_symbols[source]#
_symbol_replacement_map[source]#
pybop._utils.add_spaces(string)[source]#

Return the class name as a string with spaces before each new capitalised word.

pybop._utils.is_numeric(x)[source]#

Check if a variable is numeric.