From 4b9870ce78ea31e58bbeb9f32a8cac76c50a0c97 Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Tue, 26 May 2020 18:05:57 -0400 Subject: [PATCH] #1023 add case for already discretised model --- pybamm/simulation.py | 17 ++++++++++------- tests/unit/test_simulation.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/pybamm/simulation.py b/pybamm/simulation.py index e32421b186..003b03895a 100644 --- a/pybamm/simulation.py +++ b/pybamm/simulation.py @@ -298,13 +298,16 @@ def build(self, check_model=True): if self.built_model: return None - - self.set_parameters() - self._mesh = pybamm.Mesh(self._geometry, self._submesh_types, self._var_pts) - self._disc = pybamm.Discretisation(self._mesh, self._spatial_methods) - self._built_model = self._disc.process_model( - self._model_with_set_params, inplace=False, check_model=check_model - ) + elif self.model.is_discretised: + self._model_with_set_params = self.model + self._built_model = self.model + else: + self.set_parameters() + self._mesh = pybamm.Mesh(self._geometry, self._submesh_types, self._var_pts) + self._disc = pybamm.Discretisation(self._mesh, self._spatial_methods) + self._built_model = self._disc.process_model( + self._model_with_set_params, inplace=False, check_model=check_model + ) def solve( self, diff --git a/tests/unit/test_simulation.py b/tests/unit/test_simulation.py index 8a59951fd2..7cb8fc40ad 100644 --- a/tests/unit/test_simulation.py +++ b/tests/unit/test_simulation.py @@ -103,6 +103,27 @@ def test_solve_non_battery_model(self): sim.solution["v"].entries, np.exp(-np.linspace(0, 1, 100)) ) + def test_solve_already_partially_processed_model(self): + + model = pybamm.lithium_ion.SPM() + + # Process model manually + geometry = model.default_geometry + param = model.default_parameter_values + param.process_model(model) + param.process_geometry(geometry) + # Let simulation take over + sim = pybamm.Simulation(model) + sim.solve() + + # Discretised manually + mesh = pybamm.Mesh(geometry, model.default_submesh_types, model.default_var_pts) + disc = pybamm.Discretisation(mesh, model.default_spatial_methods) + disc.process_model(model) + # Let simulation take over + sim = pybamm.Simulation(model) + sim.solve() + def test_reuse_commands(self): sim = pybamm.Simulation(pybamm.lithium_ion.SPM())