Automatically save sim results after completion

This commit is contained in:
Andreas Tsouchlos 2022-11-18 13:33:59 +01:00
parent 1485ade538
commit 2cfbbd529c

View File

@ -300,6 +300,7 @@ class SimulationDeSerializer:
# TODO: Fix typing.Any or Simulator
# TODO: Autosave simulation every so often
class SimulationManager:
"""This class only contains functions relating to stopping and restarting of simulations
(and storing of the simulation state in a file, to be resumed at a later date).
@ -324,8 +325,9 @@ class SimulationManager:
signal.signal(signal.SIGHUP, self._exit_gracefully)
def _sim_configured(self) -> bool:
return (self._simulator is not None)\
and (self._sim_name is not None)\
"""Check whether 'configure_simulation()' has been called."""
return (self._simulator is not None) \
and (self._sim_name is not None) \
and (self._metadata is not None)
def configure_simulation(self, simulator: typing.Any, name: str, column_labels: typing.Sequence[str]) -> None:
@ -338,7 +340,6 @@ class SimulationManager:
"""Check whether the savefile of a previously unfinished simulation is present."""
return self._de_serializer.unfinished_sim_present(sim_name)
# TODO: Where should the files be removed?
def load_unfinished(self, sim_name: str) -> None:
"""Load the state of an unfinished simulation its savefile.
@ -352,10 +353,7 @@ class SimulationManager:
self._de_serializer.remove_unfinished_sim(sim_name)
def _exit_gracefully(self, *args) -> None:
"""Handler called when the program is interrupted.
Pauses and saves the currently running simulation
"""
"""Handler called when the program is interrupted. Pauses and saves the currently running simulation."""
if self._sim_configured():
self._simulator.stop()
self._de_serializer.save_state(self._simulator, self._sim_name, self._metadata)
@ -363,17 +361,9 @@ class SimulationManager:
exit()
def start(self) -> None:
"""Start the simulation.
This is a blocking call. A call to the stop() function
from another thread will stop this function.
"""
def simulate(self) -> None:
"""Start the simulation. This is a blocking call."""
assert self._sim_configured()
self._simulator.start()
def save_results(self) -> None:
assert self._sim_configured()
self._de_serializer.save_results(self._simulator, self._sim_name, self._metadata)