Repository: pybamm-team/PyBaMM (python)

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

Pull request:
#1023 add case for already discretised model
# Description

Add a case to the simulation to catch simulating an already discretised model

Fixes #1023 

## Type of change

Please add a line in the relevant section of [CHANGELOG.md](https://github.com/pybamm-team/PyBaMM/blob/master/CHANGELOG.md) to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Optimization (back-end change that speeds up the code)
- [x] Bug fix (non-breaking change which fixes an issue)


# Key checklist:

- [x] No style issues: `$ flake8`
- [x] All tests pass: `$ python run-tests.py --unit`
- [x] The documentation builds: `$ cd docs` and then `$ make clean; make html`

You can run all three at once, using `$ python run-tests.py --quick`.

## Further checks:

- [x] Code is commented, particularly in hard-to-understand areas
- [x] Tests added that prove fix is effective or that feature works


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())