Repository: pybamm-team/PyBaMM

Issue:
Running a simulation with a discretized model
**Describe the bug**
If a user discretizes a model manually then passes it to a simulation, the simulation fails when trying to process parameters

**Expected behaviour**
The simulation should recognize that the model has been discretized already

Discussion:

Patch:
diff --git a/pybamm/simulation.py b/pybamm/simulation.py
index e32421b18..003b03895 100644
--- a/pybamm/simulation.py
+++ b/pybamm/simulation.py
@@ -298,13 +298,16 @@ class Simulation:
 
         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,


Test patch:
diff --git a/tests/unit/test_simulation.py b/tests/unit/test_simulation.py
index 8a59951fd..7cb8fc40a 100644
--- a/tests/unit/test_simulation.py
+++ b/tests/unit/test_simulation.py
@@ -103,6 +103,27 @@ class TestSimulation(unittest.TestCase):
             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())