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