Skip to content

Commit

Permalink
Added test to check if simulation has been solved in simulation.py
Browse files Browse the repository at this point in the history
…and `batch_study.py` (#3795)

* Added test to check if simulation has been solved

Signed-off-by: Pradyot Ranjan <[email protected]>

* style: pre-commit fixes

* Commit suggestions

Signed-off-by: Pradyot Ranjan <[email protected]>

---------

Signed-off-by: Pradyot Ranjan <[email protected]>
Co-authored-by: Pradyot Ranjan <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Agriya Khetarpal <[email protected]>
  • Loading branch information
4 people authored Feb 2, 2024
1 parent 705baff commit 45267cd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pybamm/batch_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def create_gif(self, number_of_images=80, duration=0.1, output_filename="plot.gi
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)

Expand Down
2 changes: 2 additions & 0 deletions pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,8 @@ def create_gif(self, number_of_images=80, duration=0.1, output_filename="plot.gi
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)

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_batch_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def test_solve(self):
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
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def test_plot(self):
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
Expand Down

0 comments on commit 45267cd

Please sign in to comment.