From 48c74869b355a0202d6ecee5fd23e4886dc78236 Mon Sep 17 00:00:00 2001 From: Andrew Moodie Date: Mon, 25 May 2020 13:57:40 -0500 Subject: [PATCH] add a multifinalization flag and tests --- pyDeltaRCM/deltaRCM_driver.py | 2 ++ pyDeltaRCM/deltaRCM_tools.py | 3 +++ pyDeltaRCM/init_tools.py | 2 ++ tests/test_deltaRCM_driver.py | 20 ++++++++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/pyDeltaRCM/deltaRCM_driver.py b/pyDeltaRCM/deltaRCM_driver.py index d6b3951c..6e03b536 100644 --- a/pyDeltaRCM/deltaRCM_driver.py +++ b/pyDeltaRCM/deltaRCM_driver.py @@ -71,6 +71,8 @@ def finalize(self): except Exception: pass + self._is_finalized = True + # define properties @property diff --git a/pyDeltaRCM/deltaRCM_tools.py b/pyDeltaRCM/deltaRCM_tools.py index e8358bd0..625cf669 100644 --- a/pyDeltaRCM/deltaRCM_tools.py +++ b/pyDeltaRCM/deltaRCM_tools.py @@ -37,6 +37,9 @@ def run_one_timestep(self): print('-' * 20) print('Timestep: ' + str(self._time)) + if self._is_finalized: + raise RuntimeError('Cannot update model, model already finalized!') + for iteration in range(self.itermax): self.init_water_iteration() diff --git a/pyDeltaRCM/init_tools.py b/pyDeltaRCM/init_tools.py index 70008773..4aa23403 100644 --- a/pyDeltaRCM/init_tools.py +++ b/pyDeltaRCM/init_tools.py @@ -228,6 +228,8 @@ def create_other_variables(self): if self.case_prefix: self.prefix += self.case_prefix + '_' + self._is_finalized = False + def create_domain(self): """ Creates the model domain diff --git a/tests/test_deltaRCM_driver.py b/tests/test_deltaRCM_driver.py index 624bbf26..51c53585 100644 --- a/tests/test_deltaRCM_driver.py +++ b/tests/test_deltaRCM_driver.py @@ -17,8 +17,28 @@ def test_init(): test the deltaRCM_driver init (happened when delta.initialize was run) """ assert delta._time == 0. + assert delta._is_finalized == False def test_update(): delta.update() assert delta._time == 1.0 + assert delta._is_finalized == False + + +def test_finalize(): + delta.finalize() + assert delta._is_finalized == True + + +@pytest.mark.xfail(raises=RuntimeError, strict=True) +def test_multifinalization_error(): + err_delta = pyDeltaRCM(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml')) + err_delta.update() + # test will Fail if any assertion is wrong + assert err_delta._time == 1.0 + assert err_delta._is_finalized == False + err_delta.finalize() + assert err_delta._is_finalized == True + # next line should throw RuntimeError and test will xFail + err_delta.update()