Parameter Substitutor#

The ParameterSubstitutor class handles symbol and model parameterization, walking through expression trees and replacing Parameter nodes with their corresponding values from a ParameterStore.

class pybamm.parameters.ParameterSubstitutor(store: ParameterStore)[source]#

Handles symbol and model parameterization.

This class is responsible for walking through expression trees and replacing Parameter nodes with their corresponding values from a ParameterStore.

Parameters:

store (ParameterStore) – The parameter store to read values from.

Examples

>>> from pybamm.parameters import ParameterStore, ParameterSubstitutor
>>> store = ParameterStore({"Temperature [K]": 298.15})
>>> processor = ParameterSubstitutor(store)
>>> param = pybamm.Parameter("Temperature [K]")
>>> processed = processor.process_symbol(param)
>>> float(processed.evaluate())
298.15
property cache: dict[Symbol, Symbol]#

Return the processed symbols cache (read-only access).

clear_cache() None[source]#

Invalidate the processed symbol cache.

Example

>>> from pybamm.parameters import ParameterStore, ParameterSubstitutor
>>> store = ParameterStore({"Temperature [K]": 298.15})
>>> processor = ParameterSubstitutor(store)
>>> processor.clear_cache()  # Force re-processing of symbols
evaluate(symbol: Symbol, inputs: dict | None = None) Any[source]#

Process and evaluate a symbol.

Parameters:
  • symbol (pybamm.Symbol) – Symbol or Expression tree to evaluate.

  • inputs (dict, optional) – Input parameter values for evaluation.

Returns:

The evaluated symbol.

Return type:

number or array

Raises:

ValueError – If symbol does not evaluate to a constant.

Example

>>> from pybamm.parameters import ParameterStore, ParameterSubstitutor
>>> store = ParameterStore({"Current [A]": 5.0})
>>> processor = ParameterSubstitutor(store)
>>> param = pybamm.Parameter("Current [A]")
>>> result = processor.evaluate(param)  # Returns evaluated value
process_boundary_conditions(model: BaseModel) dict[Symbol, dict[str, tuple[Symbol, str]]][source]#

Process boundary conditions for a model.

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.

process_geometry(geometry: dict) None[source]#

Assign parameter values to a geometry (inplace).

Parameters:

geometry (dict) – Geometry specs to assign parameter values to.

Example

>>> from pybamm.parameters import ParameterStore, ParameterSubstitutor
>>> store = pybamm.ParameterValues("Marquis2019").store
>>> processor = ParameterSubstitutor(store)
>>> geometry = pybamm.battery_geometry()
>>> processor.process_geometry(geometry)
process_model(unprocessed_model: BaseModel, *, inplace: bool = True, delayed_variable_processing: bool | None = None) BaseModel[source]#

Assign parameter values to a model.

Example

>>> from pybamm.parameters import ParameterStore, ParameterSubstitutor
>>> store = pybamm.ParameterValues("Chen2020").store
>>> processor = ParameterSubstitutor(store)
>>> model = pybamm.lithium_ion.SPM()
>>> processed_model = processor.process_model(model)
Parameters:
  • unprocessed_model (pybamm.BaseModel) – Model to assign parameter values for.

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

  • delayed_variable_processing (bool, optional) – If True, make variable processing a post-processing step. Default is False.

Returns:

The parameterized model.

Return type:

pybamm.BaseModel

Raises:

pybamm.ModelError – If an empty model is passed.

process_symbol(symbol: Symbol) Symbol[source]#

Walk through the symbol and replace any Parameter with a Value.

If a symbol has already been processed, the cached value is returned.

Parameters:

symbol (pybamm.Symbol) – Symbol or Expression tree to set parameters for.

Returns:

Symbol with Parameter instances replaced by values.

Return type:

pybamm.Symbol

Example

>>> from pybamm.parameters import ParameterStore, ParameterSubstitutor
>>> store = ParameterStore({"Current [A]": 5.0})
>>> processor = ParameterSubstitutor(store)
>>> param = pybamm.Parameter("Current [A]")
>>> processed = processor.process_symbol(param)
>>> result = processed.evaluate()  # Returns evaluated value