Skip to content

Commit

Permalink
Add model.clear and remove setters (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaCourtier authored Aug 7, 2024
1 parent 78dcca5 commit a8dbbe6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
44 changes: 11 additions & 33 deletions pybop/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ def set_params(self, rebuild=False):
self._parameter_set.process_geometry(self.geometry)
self.pybamm_model = self._model_with_set_params

def clear(self):
"""
Clear any built PyBaMM model.
"""
self._model_with_set_params = None
self._built_model = None
self._built_initial_soc = None
self._mesh = None
self._disc = None

def rebuild(
self,
dataset: Dataset = None,
Expand Down Expand Up @@ -698,38 +708,22 @@ def approximate_capacity(self, inputs: Inputs):
def built_model(self):
return self._built_model

@built_model.setter
def built_model(self, built_model):
self._built_model = built_model if built_model is not None else None

@property
def built_initial_soc(self):
return self._built_initial_soc

@built_initial_soc.setter
def built_initial_soc(self, built_initial_soc):
self._built_initial_soc = (
built_initial_soc if built_initial_soc is not None else None
)

@property
def parameter_set(self):
return self._parameter_set

@parameter_set.setter
@parameter_set.setter # remove in #425
def parameter_set(self, parameter_set):
self._parameter_set = parameter_set.copy() if parameter_set is not None else {}

@property
def model_with_set_params(self):
return self._model_with_set_params

@model_with_set_params.setter
def model_with_set_params(self, model_with_set_params):
self._model_with_set_params = (
model_with_set_params.copy() if model_with_set_params is not None else None
)

@property
def geometry(self):
return self._geometry
Expand All @@ -742,34 +736,18 @@ def submesh_types(self):
def mesh(self):
return self._mesh

@mesh.setter
def mesh(self, mesh):
self._mesh = mesh if mesh is not None else None

@property
def disc(self):
return self._disc

@disc.setter
def disc(self, disc):
self._disc = disc if disc is not None else None

@property
def var_pts(self):
return self._var_pts

@var_pts.setter
def var_pts(self, var_pts: Optional[dict[str, int]]):
self._var_pts = var_pts.copy() if var_pts is not None else None

@property
def spatial_methods(self):
return self._spatial_methods

@property
def solver(self):
return self._solver

@solver.setter
def solver(self, solver):
self._solver = solver.copy() if solver is not None else None
2 changes: 1 addition & 1 deletion pybop/models/empirical/base_ecm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(
self._spatial_methods = (
spatial_methods or self.pybamm_model.default_spatial_methods
)
self.solver = solver or self.pybamm_model.default_solver
self._solver = solver or self.pybamm_model.default_solver

# Internal attributes for the built model are initialized but not set
self._model_with_set_params = None
Expand Down
6 changes: 1 addition & 5 deletions pybop/problems/fitting_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ def __init__(

# Build the model from scratch
if self._model.built_model is not None:
self._model.model_with_set_params = None
self._model.built_model = None
self._model.built_initial_soc = None
self._model.mesh = None
self._model.disc = None
self._model.clear()
self._model.build(
dataset=self._dataset,
parameters=self.parameters,
Expand Down

0 comments on commit a8dbbe6

Please sign in to comment.