Repository: pybamm-team/PyBaMM (python)

Issue:
Check if the solution exists before creating a GIF (`Simulation.create_gif` and `BatchStudy.create_gif`)
I liked how #3723 checks if the simulations have been solved before proceeding ahead to the plotting functionality. It would be nice to have similar checks in -

https://github.com/pybamm-team/PyBaMM/blob/c3def31a04df163769851733657d9b49b3700bca/pybamm/batch_study.py#L184-L187

and 

https://github.com/pybamm-team/PyBaMM/blob/c3def31a04df163769851733657d9b49b3700bca/pybamm/simulation.py#L1047-L1050

Pull request description:
Added test to check if simulation has been solved in `simulation.py` and `batch_study.py`
# Description

Simple check for if simulations have been solved or not.

Fixes #3725 

## Type of change

Please add a line in the relevant section of [CHANGELOG.md](https://github.com/pybamm-team/PyBaMM/blob/develop/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)
- [ ] Bug fix (non-breaking change which fixes an issue)

# Key checklist:

- [ ] No style issues: `$ pre-commit run` (or `$ nox -s pre-commit`) (see [CONTRIBUTING.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CONTRIBUTING.md#installing-and-using-pre-commit) for how to set this up to run automatically when committing locally, in just two lines of code)
- [ ] All tests pass: `$ python run-tests.py --all` (or `$ nox -s tests`)
- [ ] The documentation builds: `$ python run-tests.py --doctest` (or `$ nox -s doctests`)

You can run integration tests, unit tests, and doctests together at once, using `$ python run-tests.py --quick` (or `$ nox -s quick`).

## Further checks:

- [ ] Code is commented, particularly in hard-to-understand areas
- [ ] Tests added that prove fix is effective or that feature works


Patch:
diff --git a/pybamm/batch_study.py b/pybamm/batch_study.py
index 4f1c37e71..1503c3048 100644
--- a/pybamm/batch_study.py
+++ b/pybamm/batch_study.py
@@ -196,7 +196,8 @@ class BatchStudy:
             Name of the generated GIF file.
 
         """
-
+        if not hasattr(self, "sims"):
+            raise ValueError("The simulations have not been solved yet.")
         if self.quick_plot is None:
             self.quick_plot = pybamm.QuickPlot(self.sims)
 
diff --git a/pybamm/simulation.py b/pybamm/simulation.py
index 73c8188be..a4c0d4bb3 100644
--- a/pybamm/simulation.py
+++ b/pybamm/simulation.py
@@ -1072,6 +1072,8 @@ class Simulation:
             Name of the generated GIF file.
 
         """
+        if self.solution is None:
+            raise ValueError("The simulation has not been solved yet.")
         if self.quick_plot is None:
             self.quick_plot = pybamm.QuickPlot(self._solution)
 


Test patch:
diff --git a/tests/unit/test_batch_study.py b/tests/unit/test_batch_study.py
index f8762133a..1d48bd08b 100644
--- a/tests/unit/test_batch_study.py
+++ b/tests/unit/test_batch_study.py
@@ -93,6 +93,12 @@ class TestBatchStudy(TestCase):
     def test_create_gif(self):
         with TemporaryDirectory() as dir_name:
             bs = pybamm.BatchStudy({"spm": pybamm.lithium_ion.SPM()})
+            with self.assertRaisesRegex(
+                ValueError, "The simulations have not been solved yet."
+            ):
+                pybamm.BatchStudy(
+                    models={"SPM": spm, "SPM uniform": spm_uniform}
+                ).create_gif()
             bs.solve([0, 10])
 
             # Create a temporary file name
diff --git a/tests/unit/test_simulation.py b/tests/unit/test_simulation.py
index ef055fdc9..6e7896bbb 100644
--- a/tests/unit/test_simulation.py
+++ b/tests/unit/test_simulation.py
@@ -396,6 +396,10 @@ class TestSimulation(TestCase):
     def test_create_gif(self):
         with TemporaryDirectory() as dir_name:
             sim = pybamm.Simulation(pybamm.lithium_ion.SPM())
+            with self.assertRaisesRegex(
+                ValueError, "The simulation has not been solved yet."
+            ):
+                sim.create_gif()
             sim.solve(t_eval=[0, 10])
 
             # Create a temporary file name