Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model.clear and remove setters #448

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
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
Loading