From 041d20745716e77c4118b3f82bfbac078f31dee1 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 29 Aug 2024 14:08:18 +0200 Subject: [PATCH 01/10] Make t and n optional arguments in store_checkpoint --- fenicsprecice/fenicsprecice.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/fenicsprecice/fenicsprecice.py b/fenicsprecice/fenicsprecice.py index e766f7d7..97da5e5d 100644 --- a/fenicsprecice/fenicsprecice.py +++ b/fenicsprecice/fenicsprecice.py @@ -432,7 +432,7 @@ def initialize(self, coupling_subdomain, read_function_space=None, write_object= self._participant.initialize() - def store_checkpoint(self, payload, t, n): + def store_checkpoint(self, payload, t = None, n = None): """ Defines an object of class SolverState which stores the current state of the variable and the time stamp. @@ -440,9 +440,9 @@ def store_checkpoint(self, payload, t, n): ---------- payload : fenics.function or a list of fenics.functions Current state of the physical variable(s) of interest for this participant. - t : double + t : double (optional) Current simulation time. - n : int + n : int (optional) Current time window (iteration) number. """ if self._first_advance_done: @@ -459,14 +459,25 @@ def retrieve_checkpoint(self): ------- u : FEniCS Function Current state of the physical variable of interest for this participant. - t : double + t : double (optional) Current simulation time. - n : int + n : int (optional) Current time window (iteration) number. """ assert (not self.is_time_window_complete()) logger.debug("Restore solver state") - return self._checkpoint.get_state() + + # since t and n are optional, they should not be returned, if not specified + payload, t, n = self._checkpoint.get_state() + match (t, n): + case (None, None): + return payload + case (_, None): + return payload, t + case (None, _): + return payload, n + case _: + return payload, t, n def advance(self, dt): """ From 4a865cf4684f1796cabbd6c1ac1521a1d1fa6a4f Mon Sep 17 00:00:00 2001 From: Niklas Date: Wed, 6 Nov 2024 16:12:03 +0100 Subject: [PATCH 02/10] Add integration tests --- tests/integration/test_write_read.py | 77 +++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_write_read.py b/tests/integration/test_write_read.py index bc11e848..49b72f56 100644 --- a/tests/integration/test_write_read.py +++ b/tests/integration/test_write_read.py @@ -1,7 +1,7 @@ from unittest.mock import MagicMock, patch from unittest import TestCase from tests import MockedPrecice -from fenics import Expression, UnitSquareMesh, FunctionSpace, VectorFunctionSpace, interpolate, SubDomain, near +from fenics import Expression, UnitSquareMesh, FunctionSpace, VectorFunctionSpace, interpolate, SubDomain, near, UnitIntervalMesh import numpy as np x_left, x_right = 0, 1 @@ -185,3 +185,78 @@ def return_dummy_data(n_points): self.fail(f"Unexpected combination of arg: {arg}, expected_arg: {expected_arg}") np.testing.assert_almost_equal(list(read_data.values()), return_dummy_data(self.n_vertices)) + + def test_optional_parameters(self): + from precice import Participant + import fenicsprecice + + def test_all_parameters(adapter): + #init variables that are tested against + nx = 10 + mesh = UnitIntervalMesh(nx) + V = FunctionSpace(mesh, 'P', 2) + dummy_value = 1 + E = Expression("t", t=dummy_value, degree=2) + u = interpolate(E, V) + t = 0.5 + n = 42 + # test adapter + adapter.store_checkpoint(u, t, n) # is the old implementation still working? + res_u, res_t, res_n = adapter.retrieve_checkpoint() + self.assertEqual(res_t, t) + self.assertEqual(res_n, n) + np.testing.assert_array_equal(res_u.vector(), u.vector()) + + def test_opt_parameters(adapter): + #init variables that are tested against + nx = 10 + mesh = UnitIntervalMesh(nx) + V = FunctionSpace(mesh, 'P', 2) + dummy_value = 1 + E = Expression("t", t=dummy_value, degree=2) + u = interpolate(E, V) + t = 0.5 + n = 42 + # test adapter + adapter.store_checkpoint(u, t) # without n + res = adapter.retrieve_checkpoint() + self.assertEqual(len(res), 2) # correct number of return values + res_u, res_t = res + self.assertEqual(res_t, t) + np.testing.assert_array_equal(res_u.vector(), u.vector()) + + adapter.store_checkpoint(u, n) # without t + res = adapter.retrieve_checkpoint() + self.assertEqual(len(res), 2) # correct number of return values + res_u, res_n = res + self.assertEqual(res_n, n) + np.testing.assert_array_equal(res_u.vector(), u.vector()) + + def test_payload_only(adapter): + nx = 10 + mesh = UnitIntervalMesh(nx) + V = FunctionSpace(mesh, 'P', 2) + dummy_value = 1 + E = Expression("t", t=dummy_value, degree=2) + u = interpolate(E, V) + # test adapter + adapter.store_checkpoint(u) # no optional parameters + res = adapter.retrieve_checkpoint() + np.testing.assert_array_equal(res.vector(), u.vector()) + + + + Participant.is_time_window_complete = MagicMock(return_value=False) + + precice = fenicsprecice.Adapter(self.dummy_config) + + # call the tests + test_all_parameters(precice) + test_opt_parameters(precice) + test_payload_only(precice) + + + + + + From 3c776c62574e594090d55e4ad607a0f0cdf6aad2 Mon Sep 17 00:00:00 2001 From: Benjamin Rodenberg Date: Thu, 5 Sep 2024 10:30:40 +0200 Subject: [PATCH 03/10] Add changelog entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad96d2c..1a63ecff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Use `copy(deepcopy=True)` when checkpointing to make checkpointing more user-friendly and secure. IMPORTANT: might increase runtime, please open an issue if you face serious problems. [#172](https://github.com/precice/fenics-adapter/pull/172) * Add unit tests for checkpointing. [#173](https://github.com/precice/fenics-adapter/pull/173) +* Remove checks for FEniCS installation and python3 from `setup.py` since the approach is deprecated. [#182](https://github.com/precice/fenics-adapter/pull/182) ## 2.1.0 From 803e64f1c05f01e820a08393411f0bd0a7dd357b Mon Sep 17 00:00:00 2001 From: Benjamin Rodenberg Date: Thu, 5 Sep 2024 11:40:44 +0200 Subject: [PATCH 04/10] Remove checks for FEniCS and python3 (#182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Frédéric Simonis --- setup.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/setup.py b/setup.py index 1b277b9e..1f6d7126 100644 --- a/setup.py +++ b/setup.py @@ -1,29 +1,6 @@ import os from setuptools import setup import versioneer -import warnings - -# from https://stackoverflow.com/a/9079062 -import sys -if sys.version_info[0] < 3: - raise Exception("fenicsprecice only supports Python3. Did you run $python setup.py