PySDM_examples.Szumowski_et_al_1998.gui_controller

  1import shutil
  2from tempfile import TemporaryDirectory
  3from threading import Thread
  4from time import sleep
  5
  6from PySDM_examples.utils.widgets import Button, Checkbox, FloatProgress, HBox
  7
  8from PySDM.exporters import VTKExporter
  9
 10
 11class GUIController:
 12    def __init__(self, simulator, viewer, ncdf_exporter, ncdf_file, vtk_file):
 13        self.progress = FloatProgress(value=0.0, min=0.0, max=1.0)
 14        self.button = Button()
 15        self.link = HBox()
 16        self.checkbox = Checkbox(
 17            description="VTK output", indent=False, layout={"width": "125px"}
 18        )
 19        self.panic = False
 20        self.thread = None
 21        self.simulator = simulator
 22        self.ncdf_exporter = ncdf_exporter
 23        self.ncdf_file = ncdf_file
 24        self.vtk_file = vtk_file
 25        self.tempdir = None
 26        self.vtk_exporter = None
 27        self.viewer = viewer
 28        self._setup_play()
 29
 30    def __enter__(self):
 31        self.panic = False
 32        self.set_percent(0)
 33
 34    def __exit__(self, *_):
 35        if self.panic:
 36            self._setup_play()
 37        else:
 38            self._setup_save()
 39        self.panic = False
 40        self.progress.description = " "
 41
 42    def reinit(self, _=None):
 43        self.panic = True
 44        self._setup_play()
 45        self.progress.value = 0
 46        self.progress.description = " "
 47        self.viewer.clear()
 48        self.link.children = ()
 49        self.vtk_exporter = None
 50
 51    def box(self):
 52        netcdf_box = Checkbox(
 53            description="netCDF output",
 54            disabled=True,
 55            value=True,
 56            indent=False,
 57            layout={"width": "125px"},
 58        )
 59        return HBox([self.progress, self.button, netcdf_box, self.checkbox, self.link])
 60
 61    def set_percent(self, value):
 62        self.progress.value = value
 63
 64    def _setup_play(self):
 65        self.button.on_click(self._handle_stop, remove=True)
 66        self.button.on_click(self._handle_save, remove=True)
 67        self.button.on_click(self._handle_play)
 68        self.button.icon = "play"
 69        self.button.description = "start simulation"
 70        self.checkbox.disabled = False
 71
 72    def _setup_stop(self):
 73        self.button.on_click(self._handle_play, remove=True)
 74        self.button.on_click(self._handle_save, remove=True)
 75        self.button.on_click(self._handle_stop)
 76        self.button.icon = "stop"
 77        self.button.description = "interrupt"
 78
 79    def _setup_save(self):
 80        self.button.icon = "download"
 81        self.button.description = "save output"
 82        self.button.on_click(self._handle_stop, remove=True)
 83        self.button.on_click(self._handle_save)
 84
 85    def _handle_stop(self, _):
 86        self.panic = True
 87        while self.panic:
 88            sleep(0.1)
 89        self._setup_play()
 90
 91    def _handle_play(self, _):
 92        self._setup_stop()
 93
 94        self.link.children = ()
 95        self.progress.value = 0
 96        self.progress.description = "initialisation"
 97        self.checkbox.disabled = True
 98
 99        if self.checkbox.value:
100            self.tempdir = TemporaryDirectory()
101            self.vtk_exporter = VTKExporter(verbose=False, path=self.tempdir.name)
102
103        self.thread = Thread(target=self.simulator.run, args=(self, self.vtk_exporter))
104
105        self.simulator.reinit()
106        self.thread.start()
107        self.progress.description = "running"
108        self.viewer.reinit(self.simulator.products)
109
110    def _handle_save(self, _):
111        def task(controller):
112            controller.progress.value = 0
113            if self.checkbox.value:
114                self.vtk_exporter.write_pvd()
115                controller.progress.description = "VTK..."
116                shutil.make_archive(
117                    self.vtk_file.absolute_path[:-4], "zip", self.vtk_exporter.path
118                )
119            controller.progress.description = "netCDF..."
120            self.ncdf_exporter.run(controller)
121            controller.link.children = (
122                (self.ncdf_file.make_link_widget(),)
123                if not self.checkbox.value
124                else (
125                    self.ncdf_file.make_link_widget(),
126                    self.vtk_file.make_link_widget(),
127                )
128            )
129
130        self.thread = Thread(target=task, args=(self,))
131        self.thread.start()
132        self._setup_stop()
class GUIController:
 12class GUIController:
 13    def __init__(self, simulator, viewer, ncdf_exporter, ncdf_file, vtk_file):
 14        self.progress = FloatProgress(value=0.0, min=0.0, max=1.0)
 15        self.button = Button()
 16        self.link = HBox()
 17        self.checkbox = Checkbox(
 18            description="VTK output", indent=False, layout={"width": "125px"}
 19        )
 20        self.panic = False
 21        self.thread = None
 22        self.simulator = simulator
 23        self.ncdf_exporter = ncdf_exporter
 24        self.ncdf_file = ncdf_file
 25        self.vtk_file = vtk_file
 26        self.tempdir = None
 27        self.vtk_exporter = None
 28        self.viewer = viewer
 29        self._setup_play()
 30
 31    def __enter__(self):
 32        self.panic = False
 33        self.set_percent(0)
 34
 35    def __exit__(self, *_):
 36        if self.panic:
 37            self._setup_play()
 38        else:
 39            self._setup_save()
 40        self.panic = False
 41        self.progress.description = " "
 42
 43    def reinit(self, _=None):
 44        self.panic = True
 45        self._setup_play()
 46        self.progress.value = 0
 47        self.progress.description = " "
 48        self.viewer.clear()
 49        self.link.children = ()
 50        self.vtk_exporter = None
 51
 52    def box(self):
 53        netcdf_box = Checkbox(
 54            description="netCDF output",
 55            disabled=True,
 56            value=True,
 57            indent=False,
 58            layout={"width": "125px"},
 59        )
 60        return HBox([self.progress, self.button, netcdf_box, self.checkbox, self.link])
 61
 62    def set_percent(self, value):
 63        self.progress.value = value
 64
 65    def _setup_play(self):
 66        self.button.on_click(self._handle_stop, remove=True)
 67        self.button.on_click(self._handle_save, remove=True)
 68        self.button.on_click(self._handle_play)
 69        self.button.icon = "play"
 70        self.button.description = "start simulation"
 71        self.checkbox.disabled = False
 72
 73    def _setup_stop(self):
 74        self.button.on_click(self._handle_play, remove=True)
 75        self.button.on_click(self._handle_save, remove=True)
 76        self.button.on_click(self._handle_stop)
 77        self.button.icon = "stop"
 78        self.button.description = "interrupt"
 79
 80    def _setup_save(self):
 81        self.button.icon = "download"
 82        self.button.description = "save output"
 83        self.button.on_click(self._handle_stop, remove=True)
 84        self.button.on_click(self._handle_save)
 85
 86    def _handle_stop(self, _):
 87        self.panic = True
 88        while self.panic:
 89            sleep(0.1)
 90        self._setup_play()
 91
 92    def _handle_play(self, _):
 93        self._setup_stop()
 94
 95        self.link.children = ()
 96        self.progress.value = 0
 97        self.progress.description = "initialisation"
 98        self.checkbox.disabled = True
 99
100        if self.checkbox.value:
101            self.tempdir = TemporaryDirectory()
102            self.vtk_exporter = VTKExporter(verbose=False, path=self.tempdir.name)
103
104        self.thread = Thread(target=self.simulator.run, args=(self, self.vtk_exporter))
105
106        self.simulator.reinit()
107        self.thread.start()
108        self.progress.description = "running"
109        self.viewer.reinit(self.simulator.products)
110
111    def _handle_save(self, _):
112        def task(controller):
113            controller.progress.value = 0
114            if self.checkbox.value:
115                self.vtk_exporter.write_pvd()
116                controller.progress.description = "VTK..."
117                shutil.make_archive(
118                    self.vtk_file.absolute_path[:-4], "zip", self.vtk_exporter.path
119                )
120            controller.progress.description = "netCDF..."
121            self.ncdf_exporter.run(controller)
122            controller.link.children = (
123                (self.ncdf_file.make_link_widget(),)
124                if not self.checkbox.value
125                else (
126                    self.ncdf_file.make_link_widget(),
127                    self.vtk_file.make_link_widget(),
128                )
129            )
130
131        self.thread = Thread(target=task, args=(self,))
132        self.thread.start()
133        self._setup_stop()
GUIController(simulator, viewer, ncdf_exporter, ncdf_file, vtk_file)
13    def __init__(self, simulator, viewer, ncdf_exporter, ncdf_file, vtk_file):
14        self.progress = FloatProgress(value=0.0, min=0.0, max=1.0)
15        self.button = Button()
16        self.link = HBox()
17        self.checkbox = Checkbox(
18            description="VTK output", indent=False, layout={"width": "125px"}
19        )
20        self.panic = False
21        self.thread = None
22        self.simulator = simulator
23        self.ncdf_exporter = ncdf_exporter
24        self.ncdf_file = ncdf_file
25        self.vtk_file = vtk_file
26        self.tempdir = None
27        self.vtk_exporter = None
28        self.viewer = viewer
29        self._setup_play()
progress
button
checkbox
panic
thread
simulator
ncdf_exporter
ncdf_file
vtk_file
tempdir
vtk_exporter
viewer
def reinit(self, _=None):
43    def reinit(self, _=None):
44        self.panic = True
45        self._setup_play()
46        self.progress.value = 0
47        self.progress.description = " "
48        self.viewer.clear()
49        self.link.children = ()
50        self.vtk_exporter = None
def box(self):
52    def box(self):
53        netcdf_box = Checkbox(
54            description="netCDF output",
55            disabled=True,
56            value=True,
57            indent=False,
58            layout={"width": "125px"},
59        )
60        return HBox([self.progress, self.button, netcdf_box, self.checkbox, self.link])
def set_percent(self, value):
62    def set_percent(self, value):
63        self.progress.value = value