Skip to content

Commit 394664d

Browse files
authored
Make t and n optional arguments in store_checkpoint (#178)
* Make t and n optional arguments in store_checkpoint * Add integration tests
1 parent a636fea commit 394664d

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## develop
44

55
* Set required version of `scipy` to `<1.15.0` to prevent failing tests because of a required module that is not found for later versions of scipy. [#195](https://github.com/precice/fenics-adapter/pull/195)
6+
* `store_checkpoint` no longer requires the time `t` and the current time window number `n` as parameters as they are now optional. If they are not specified in `store_checkpoint`, `retrieve_checkpoint` still returns a tuple of three elements but for unspecified parameters, it returns `None`.
67

78
## 2.2.0
89

fenicsprecice/fenicsprecice.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,17 @@ def initialize(self, coupling_subdomain, read_function_space=None, write_object=
432432

433433
self._participant.initialize()
434434

435-
def store_checkpoint(self, payload, t, n):
435+
def store_checkpoint(self, payload, t=None, n=None):
436436
"""
437437
Defines an object of class SolverState which stores the current state of the variable and the time stamp.
438438
439439
Parameters
440440
----------
441441
payload : fenics.function or a list of fenics.functions
442442
Current state of the physical variable(s) of interest for this participant.
443-
t : double
443+
t : double (optional)
444444
Current simulation time.
445-
n : int
445+
n : int (optional)
446446
Current time window (iteration) number.
447447
"""
448448
if self._first_advance_done:
@@ -460,9 +460,9 @@ def retrieve_checkpoint(self):
460460
u : FEniCS Function
461461
Current state of the physical variable of interest for this participant.
462462
t : double
463-
Current simulation time.
463+
Current simulation time or None if not specified in store_checkpoint
464464
n : int
465-
Current time window (iteration) number.
465+
Current time window (iteration) number or None if not specified in store_checkpoint
466466
"""
467467
assert (not self.is_time_window_complete())
468468
logger.debug("Restore solver state")

tests/integration/test_write_read.py

+72-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import MagicMock, patch
22
from unittest import TestCase
33
from tests import MockedPrecice
4-
from fenics import Expression, UnitSquareMesh, FunctionSpace, VectorFunctionSpace, interpolate, SubDomain, near
4+
from fenics import Expression, UnitSquareMesh, FunctionSpace, VectorFunctionSpace, interpolate, SubDomain, near, UnitIntervalMesh
55
import numpy as np
66

77
x_left, x_right = 0, 1
@@ -185,3 +185,74 @@ def return_dummy_data(n_points):
185185
self.fail(f"Unexpected combination of arg: {arg}, expected_arg: {expected_arg}")
186186

187187
np.testing.assert_almost_equal(list(read_data.values()), return_dummy_data(self.n_vertices))
188+
189+
def test_optional_parameters(self):
190+
from precice import Participant
191+
import fenicsprecice
192+
193+
def test_all_parameters(adapter):
194+
# init variables that are tested against
195+
nx = 10
196+
mesh = UnitIntervalMesh(nx)
197+
V = FunctionSpace(mesh, 'P', 2)
198+
dummy_value = 1
199+
E = Expression("t", t=dummy_value, degree=2)
200+
u = interpolate(E, V)
201+
t = 0.5
202+
n = 42
203+
# test adapter
204+
adapter.store_checkpoint(u, t, n) # is the old implementation still working?
205+
res_u, res_t, res_n = adapter.retrieve_checkpoint()
206+
self.assertEqual(res_t, t)
207+
self.assertEqual(res_n, n)
208+
np.testing.assert_array_equal(res_u.vector(), u.vector())
209+
210+
def test_opt_parameters(adapter):
211+
# init variables that are tested against
212+
nx = 10
213+
mesh = UnitIntervalMesh(nx)
214+
V = FunctionSpace(mesh, 'P', 2)
215+
dummy_value = 1
216+
E = Expression("t", t=dummy_value, degree=2)
217+
u = interpolate(E, V)
218+
t = 0.5
219+
n = 42
220+
# test adapter
221+
adapter.store_checkpoint(u, t=t) # without n
222+
res = adapter.retrieve_checkpoint()
223+
self.assertEqual(len(res), 3) # correct number of return values
224+
res_u, res_t, res_n = res
225+
self.assertEqual(res_t, t)
226+
self.assertEqual(res_n, None)
227+
np.testing.assert_array_equal(res_u.vector(), u.vector())
228+
229+
adapter.store_checkpoint(u, n=n) # without t
230+
res = adapter.retrieve_checkpoint()
231+
self.assertEqual(len(res), 3) # correct number of return values
232+
res_u, res_t, res_n = res
233+
self.assertEqual(res_n, n)
234+
self.assertEqual(res_t, None)
235+
np.testing.assert_array_equal(res_u.vector(), u.vector())
236+
237+
def test_payload_only(adapter):
238+
nx = 10
239+
mesh = UnitIntervalMesh(nx)
240+
V = FunctionSpace(mesh, 'P', 2)
241+
dummy_value = 1
242+
E = Expression("t", t=dummy_value, degree=2)
243+
u = interpolate(E, V)
244+
# test adapter
245+
adapter.store_checkpoint(u) # no optional parameters
246+
res_u, res_t, res_n = adapter.retrieve_checkpoint()
247+
self.assertEqual(res_t, None)
248+
self.assertEqual(res_n, None)
249+
np.testing.assert_array_equal(res_u.vector(), u.vector())
250+
251+
Participant.is_time_window_complete = MagicMock(return_value=False)
252+
253+
precice = fenicsprecice.Adapter(self.dummy_config)
254+
255+
# call the tests
256+
test_all_parameters(precice)
257+
test_opt_parameters(precice)
258+
test_payload_only(precice)

0 commit comments

Comments
 (0)