From cf3e264e81a23acb5efbdfd035b484b8b739532c Mon Sep 17 00:00:00 2001 From: Benjamin Rodenberg Date: Sun, 10 Mar 2024 00:37:56 +0100 Subject: [PATCH 1/4] Support listlike checkpoints. --- fenicsprecice/fenicsprecice.py | 6 +++++- fenicsprecice/solverstate.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/fenicsprecice/fenicsprecice.py b/fenicsprecice/fenicsprecice.py index 09e77d77..fa2e97a5 100644 --- a/fenicsprecice/fenicsprecice.py +++ b/fenicsprecice/fenicsprecice.py @@ -449,7 +449,11 @@ def store_checkpoint(self, user_u, t, n): assert (self.is_time_window_complete()) logger.debug("Store checkpoint") - my_u = user_u.copy() + try: + my_u = user_u.copy() + except AttributeError: # if .copy() does not exist, it probably is a list + my_u = [item.copy() for item in user_u] + # making sure that the FEniCS function provided by user is not directly accessed by the Adapter assert (my_u != user_u) self._checkpoint = SolverState(my_u, t, n) diff --git a/fenicsprecice/solverstate.py b/fenicsprecice/solverstate.py index c7c05dde..a0a53832 100644 --- a/fenicsprecice/solverstate.py +++ b/fenicsprecice/solverstate.py @@ -5,6 +5,7 @@ def __init__(self, u, t, n): Parameters ---------- + #TODO rename u to payload or similar u : Object of class dolfin.functions.function.Function FEniCS function related to the field during each coupling iteration. t : double @@ -29,7 +30,10 @@ def get_state(self): n : int Iteration number. """ - return self.u.copy(), self.t, self.n + try: + return self.u.copy(), self.t, self.n + except AttributeError: # if .copy() does not exist, it probably is a list + return [item.copy() for item in self.u], self.t, self.n def print_state(self): u, t, n = self.get_state() From 74e46a41715240dbda84fc0ce4241aa504c16daa Mon Sep 17 00:00:00 2001 From: Benjamin Rodenberg Date: Wed, 13 Mar 2024 13:56:12 +0100 Subject: [PATCH 2/4] Add changelog. --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 538cc7c2..a35003f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # FEniCS-preCICE adapter changelog +## latest + +* Additionally support checkpoints being provided as a list or tuple (of FEniCS Functions). https://github.com/precice/fenics-adapter/pull/170 + ## 2.0.0 * Drop support for preCICE 2.x version, as this is a breaking release. From f24fb23b215a902dbb51a43194c30d929edd679e Mon Sep 17 00:00:00 2001 From: Benjamin Rodenberg Date: Wed, 13 Mar 2024 14:03:18 +0100 Subject: [PATCH 3/4] Some cleaning up and simplification. --- fenicsprecice/fenicsprecice.py | 15 ++++----------- fenicsprecice/solverstate.py | 29 ++++++++++++++++------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/fenicsprecice/fenicsprecice.py b/fenicsprecice/fenicsprecice.py index fa2e97a5..e766f7d7 100644 --- a/fenicsprecice/fenicsprecice.py +++ b/fenicsprecice/fenicsprecice.py @@ -432,14 +432,14 @@ def initialize(self, coupling_subdomain, read_function_space=None, write_object= self._participant.initialize() - def store_checkpoint(self, user_u, t, n): + def store_checkpoint(self, payload, t, n): """ Defines an object of class SolverState which stores the current state of the variable and the time stamp. Parameters ---------- - user_u : FEniCS Function - Current state of the physical variable of interest for this participant. + payload : fenics.function or a list of fenics.functions + Current state of the physical variable(s) of interest for this participant. t : double Current simulation time. n : int @@ -449,14 +449,7 @@ def store_checkpoint(self, user_u, t, n): assert (self.is_time_window_complete()) logger.debug("Store checkpoint") - try: - my_u = user_u.copy() - except AttributeError: # if .copy() does not exist, it probably is a list - my_u = [item.copy() for item in user_u] - - # making sure that the FEniCS function provided by user is not directly accessed by the Adapter - assert (my_u != user_u) - self._checkpoint = SolverState(my_u, t, n) + self._checkpoint = SolverState(payload, t, n) def retrieve_checkpoint(self): """ diff --git a/fenicsprecice/solverstate.py b/fenicsprecice/solverstate.py index a0a53832..ca90a96f 100644 --- a/fenicsprecice/solverstate.py +++ b/fenicsprecice/solverstate.py @@ -1,40 +1,43 @@ class SolverState: - def __init__(self, u, t, n): + def __init__(self, payload, t, n): """ - Solver state consists of a value u, associated time t and the timestep n + Solver state consists of a payload (either a single fenics.Function or a list of several fenics.Functions), associated time t and the timestep n Parameters ---------- - #TODO rename u to payload or similar - u : Object of class dolfin.functions.function.Function - FEniCS function related to the field during each coupling iteration. + payload : A fenics.Function or a list of fenics.Functions + Describes the state of the solver. t : double Time stamp. n : int Iteration number. """ - self.u = u + try: + self.payload = payload.copy() + except AttributeError: # if .copy() does not exist, it probably is a list + self.payload = [item.copy() for item in payload] + self.t = t self.n = n def get_state(self): """ - Returns the state variables value u, associated time t and timestep n + Returns the state variables payload, associated time t and timestep n Returns ------- - u : Object of class dolfin.functions.function.Function - A copy of FEniCS function related to the field during each coupling iteration. + payload : A fenics.Function or a list of fenics.Functions + Describes the state of the solver. t : double Time stamp. n : int Iteration number. """ try: - return self.u.copy(), self.t, self.n + return self.payload.copy(), self.t, self.n except AttributeError: # if .copy() does not exist, it probably is a list - return [item.copy() for item in self.u], self.t, self.n + return [item.copy() for item in self.payload], self.t, self.n def print_state(self): - u, t, n = self.get_state() - return print("u={u}, t={t}, n={n}".format(u=u, t=t, n=n)) + payload, t, n = self.get_state() + return print(f"payload={payload}, t={t}, n={n}") From ffe17738e5e25542a0a7460c5e5214914f4c83f7 Mon Sep 17 00:00:00 2001 From: Benjamin Rodenberg Date: Wed, 13 Mar 2024 14:09:42 +0100 Subject: [PATCH 4/4] Fix format. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a35003f6..6fc10283 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## latest -* Additionally support checkpoints being provided as a list or tuple (of FEniCS Functions). https://github.com/precice/fenics-adapter/pull/170 +* Additionally support checkpoints being provided as a list or tuple (of FEniCS Functions). [#170](https://github.com/precice/fenics-adapter/pull/170) ## 2.0.0