Serialise#

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

Converts a discretised model to and from a JSON file.

load_model(filename: str, 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) – Path to the JSON file containing the serialised model file

  • 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

save_model(model: BaseModel, mesh: Mesh | None = None, variables: FuzzyDict | 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 (pybamm.FuzzyDict (optional)) – The discretised model varaibles. Not necessary to solve a model, but required to use pybamm’s plotting tools.

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