-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring state and checkpointing (#34)
* Add a Checkpoint class * Add documentation to functions controlling checkpointing * Add SolverState object to wrap FEniCS solver's state update * Use is_timestep_complete API function * Use module logger * Update tests
- Loading branch information
1 parent
1e675e0
commit 550ca38
Showing
6 changed files
with
155 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from .solverstate import SolverState | ||
|
||
class Checkpoint: | ||
|
||
def __init__(self): | ||
""" | ||
A checkpoint for the solver state | ||
""" | ||
self._state = None | ||
|
||
def get_state(self): | ||
return self._state | ||
|
||
def write(self, new_state): | ||
""" | ||
write checkpoint from solver state. | ||
:param u: function value | ||
:param t: time | ||
:param n: timestep | ||
""" | ||
if self.is_empty(): | ||
self._state = SolverState(None, None, None) | ||
|
||
self._state.copy(new_state) | ||
|
||
def is_empty(self): | ||
""" | ||
Returns whether checkpoint is empty. An empty checkpoint has no state saved. | ||
:return: | ||
""" | ||
return not self._state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class SolverState: | ||
def __init__(self, u, t, n): | ||
""" | ||
Solver state consists of a value u, associated time t and the timestep n | ||
:param u: value | ||
:param t: time | ||
:param n: timestep | ||
""" | ||
self.u = u | ||
self.t = t | ||
self.n = n | ||
|
||
def get_state(self): | ||
""" | ||
returns the state variables value u, associated time t and timestep n | ||
:return: | ||
""" | ||
return self.u, self.t, self.n | ||
|
||
def update(self, other_state): | ||
""" | ||
updates the state using FEniCS assing function. self.u is updated. | ||
This may also have an effect outside of this object! Compare to SolverState.copy(other_state). | ||
:param other_state: | ||
""" | ||
self.u.assign(other_state.u) | ||
self.t = other_state.t | ||
self.n = other_state.n | ||
|
||
def copy(self, other_state): | ||
""" | ||
copies a state using FEniCS copy function. self.u is overwritten. | ||
This does not have an effect outside of this object! Compare to SolverState.update(other_state). | ||
:param other_state: | ||
""" | ||
self.u = other_state.u.copy() | ||
self.t = other_state.t | ||
self.n = other_state.n | ||
|
||
def print_state(self): | ||
u, t, n = self.get_state() | ||
return "u={u}, t={t}, n={n}".format(u=u, t=t, n=n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters