Serialise#

class pybamm.expression_tree.operations.serialise.Serialise[source]#

Converts a discretised model to and from a JSON file.

static load_custom_geometry(filename: str | dict) Geometry[source]#

Loads a custom PyBaMM geometry from a JSON file or dictionary.

Parameters:

filename (str or dict) – Path to the JSON file containing the saved geometry, or a dictionary containing the serialised geometry data.

Returns:

The reconstructed geometry object.

Return type:

pybamm.Geometry

static load_custom_model(filename: str | dict) BaseModel[source]#

Loads a custom (symbolic) PyBaMM model from a JSON file or dictionary.

Reconstructs a model saved using save_custom_model, including its rhs, algebraic equations, initial and boundary conditions, events, and variables. Returns a fully symbolic model ready for further processing or discretisation.

Automatically detects and decompresses data that was serialised with compression enabled (compress=True in serialise_custom_model).

Parameters:

filename (str or dict) – Path to the JSON file containing the saved model, or a dictionary containing the serialised model data (optionally compressed).

Returns:

The reconstructed symbolic PyBaMM model.

Return type:

pybamm.BaseModel or subclass

Example

>>> import pybamm
>>> model = pybamm.lithium_ion.BasicDFN()
>>> from pybamm.expression_tree.operations.serialise import Serialise
>>> Serialise.save_custom_model(model, "basicdfn_model.json")
>>> loaded_model = Serialise.load_custom_model("basicdfn_model.json")
load_model(filename: str | dict, battery_model: BaseModel | None = None) BaseModel[source]#

Loads a discretised, ready to solve model into PyBaMM.

A new pybamm battery model instance will be created, which can be solved and the results plotted as usual.

Currently only available for pybamm models which have previously been written out using the save_model() option.

Warning: This only loads in discretised models. If you wish to make edits to the model or initial conditions, a new model will need to be constructed seperately.

Parameters:
  • filename (str or dict) – Path to the JSON file containing the serialised model file, or a dictionary containing the serialised model data

  • battery_model (pybamm.BaseModel (optional)) – PyBaMM model to be created (e.g. pybamm.lithium_ion.SPM), which will override any model names within the file. If None, the function will look for the saved object path, present if the original model came from PyBaMM.

Returns:

A PyBaMM model object, of type specified either in the JSON or in battery_model.

Return type:

pybamm.BaseModel

static load_parameters(filename)[source]#

Load a JSON file of parameters (either from Serialise.save_parameters or from a standard pybamm.ParameterValues.save), and return a pybamm.ParameterValues object.

  • If a value is a dict with a “type” key, deserialize it as a PyBaMM symbol.

  • Otherwise (float, int, bool, str, list, dict-without-type), leave it as-is.

static load_spatial_methods(filename: str | dict) dict[source]#

Loads spatial methods from a JSON file or dictionary.

Parameters:

filename (str or dict) – Path to the JSON file containing the saved spatial methods, or a dictionary containing the serialised spatial methods data.

Returns:

Dictionary mapping domain names to spatial method instances.

Return type:

dict

static load_submesh_types(filename: str | dict) dict[source]#

Loads submesh types from a JSON file or dictionary.

Parameters:

filename (str or dict) – Path to the JSON file containing the saved submesh types, or a dictionary containing the serialised submesh types data.

Returns:

Dictionary mapping domain names to MeshGenerator objects.

Return type:

dict

static load_var_pts(filename: str | dict) dict[source]#

Loads var_pts from a JSON file or dictionary.

Parameters:

filename (str or dict) – Path to the JSON file containing the saved var_pts, or a dictionary containing the serialised var_pts data.

Returns:

Dictionary mapping spatial variable names (strings) to number of points.

Return type:

dict

static save_custom_geometry(geometry: Geometry, filename: str | Path | None = None) None[source]#

Saves a custom PyBaMM geometry to a JSON file.

Parameters:
  • geometry (pybamm.Geometry) – The geometry object to be saved.

  • filename (str or Path, optional) – The desired name of the JSON file. If not provided, a name will be generated using current datetime.

static save_custom_model(model: BaseModel, filename: str | Path | None = None, compress: bool = False) None[source]#

Saves a custom (non-discretised) PyBaMM model to a JSON file. Works for user defined models that are subclasses of BaseModel.

This includes symbolic expressions for rhs, algebraic, initial and boundary conditions, events, and variables. Useful for storing or sharing models before discretisation.

Parameters:
  • model (pybamm.BaseModel) – The custom symbolic model to be saved.

  • filename (str, optional) – The desired name of the JSON file. If not provided, a name will be generated from the model name and current datetime.

  • compress (bool, optional) – If True, the model data will be compressed using zlib before saving. This can significantly reduce file size. Default is False.

Example

>>> import pybamm
>>> model = pybamm.lithium_ion.BasicDFN()
>>> from pybamm.expression_tree.operations.serialise import Serialise
>>> Serialise.save_custom_model(model, "basicdfn_model.json")
>>> # Or with compression:
>>> Serialise.save_custom_model(model, "basicdfn_model.json", compress=True)
save_model(model: BaseModel, mesh: Mesh | None = None, variables: None = None, filename: str | None = None)[source]#

Saves a discretised model to a JSON file.

As the model is discretised and ready to solve, only the right hand side, algebraic and initial condition variables are saved.

Parameters:
  • model (pybamm.BaseModel) – The discretised model to be saved

  • mesh (pybamm.Mesh (optional)) – The mesh the model has been discretised over. Not neccesary to solve the model when read in, but required to use pybamm’s plotting tools.

  • variables (None (optional)) – This parameter is deprecated and enabled by default.

  • filename (str (optional)) – The desired name of the JSON file. If no name is provided, one will be created based on the model name, and the current datetime.

static save_parameters(parameters: dict, filename=None)[source]#

Serializes a dictionary of parameters to a JSON file. The values can be numbers, PyBaMM symbols, or callables.

Parameters:
  • parameters (dict) – A dictionary of parameter names and values. Values can be numeric, PyBaMM symbols, or callables.

  • filename (str, optional) – If given, saves the serialized parameters to this file.

static save_spatial_methods(spatial_methods: dict, filename: str | Path | None = None) None[source]#

Saves spatial methods to a JSON file.

Parameters:
  • spatial_methods (dict) – Dictionary mapping domain names to spatial method instances.

  • filename (str or Path, optional) – The desired name of the JSON file. If not provided, a name will be generated using current datetime.

static save_submesh_types(submesh_types: dict, filename: str | Path | None = None) None[source]#

Saves submesh types to a JSON file.

Parameters:
  • submesh_types (dict) – Dictionary mapping domain names to submesh classes.

  • filename (str or Path, optional) – The desired name of the JSON file. If not provided, a name will be generated using current datetime.

static save_var_pts(var_pts: dict, filename: str | Path | None = None) None[source]#

Saves var_pts to a JSON file.

Parameters:
  • var_pts (dict) – Dictionary mapping spatial variable names to number of points.

  • filename (str or Path, optional) – The desired name of the JSON file. If not provided, a name will be generated using current datetime.

static serialise_custom_geometry(geometry: Geometry) dict[source]#

Converts a custom PyBaMM geometry to a JSON-serialisable dictionary.

Parameters:

geometry (pybamm.Geometry) – The geometry object to be serialised.

Returns:

A JSON-serialisable dictionary representation of the geometry

Return type:

dict

static serialise_custom_model(model: BaseModel, compress: bool = False) dict[source]#

Converts a custom (non-discretised) PyBaMM model to a JSON-serialisable dictionary.

This includes symbolic expressions for rhs, algebraic, initial and boundary conditions, events, and variables. Works for user defined models that are subclasses of BaseModel.

Parameters:
  • model (pybamm.BaseModel) – The custom symbolic model to be serialised.

  • compress (bool, optional) – If True, the resulting dictionary will be compressed using zlib and encoded as base64. The output will contain a “compressed” flag set to True and a “data” field with the compressed payload. Default is False.

Returns:

A JSON-serialisable dictionary representation of the model. If compress is True, returns {“compressed”: True, “data”: <base64-encoded-zlib-data>}.

Return type:

dict

Raises:

AttributeError – If the model is missing required sections

serialise_model(model: BaseModel, mesh: Mesh | None = None, variables: None = None) dict[source]#

Converts a discretised model to a JSON-serialisable dictionary.

As the model is discretised and ready to solve, only the right hand side, algebraic and initial condition variables are serialised.

Parameters:
  • model (pybamm.BaseModel) – The discretised model to be serialised

  • mesh (pybamm.Mesh (optional)) – The mesh the model has been discretised over. Not necessary to solve the model when read in, but required to use pybamm’s plotting tools.

  • variables (None (optional)) – This parameter is deprecated and enabled by default.

Returns:

A JSON-serialisable dictionary representation of the model

Return type:

dict

static serialise_spatial_methods(spatial_methods: dict) dict[source]#

Converts a dictionary of spatial methods to a JSON-serialisable dictionary.

Parameters:

spatial_methods (dict) – Dictionary mapping domain names to spatial method instances.

Returns:

A JSON-serialisable dictionary representation of the spatial methods

Return type:

dict

static serialise_submesh_types(submesh_types: dict) dict[source]#

Converts a dictionary of submesh types to a JSON-serialisable dictionary.

Parameters:

submesh_types (dict) – Dictionary mapping domain names to submesh classes or MeshGenerator objects.

Returns:

A JSON-serialisable dictionary representation of the submesh types

Return type:

dict

static serialise_var_pts(var_pts: dict) dict[source]#

Converts a var_pts dictionary to a JSON-serialisable dictionary.

Parameters:

var_pts (dict) – Dictionary mapping spatial variable names (str or SpatialVariable) to number of points (int).

Returns:

A JSON-serialisable dictionary representation of var_pts

Return type:

dict