Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# FEniCS-preCICE adapter changelog

## latest

* Add unit tests for checkpointing. [#173](https://github.com/precice/fenics-adapter/pull/173)

## 2.1.0

* Additionally support checkpoints being provided as a list or tuple (of FEniCS Functions). [#170](https://github.com/precice/fenics-adapter/pull/170)
Expand Down
97 changes: 97 additions & 0 deletions tests/unit/test_checkpointing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from unittest.mock import MagicMock
from unittest import TestCase
from fenics import FunctionSpace, UnitSquareMesh, Expression, interpolate
from fenicsprecice.solverstate import SolverState


class TestCheckpointing(TestCase):
def test_solverstate_basic(self):
"""
Check if correct values are read from the checkpoint, while the state of the object that is copied is not changed
"""
n = 1
size = 5
mesh = UnitSquareMesh(size,size)
V = FunctionSpace(mesh, 'P', 2)
dummy_value = 1
E = Expression("t", t=dummy_value, degree=2)
u = interpolate(E, V)

# "write checkpoint"
sstate = SolverState(u, dummy_value, n)
# "read checkpoint"
u_cp, t_cp, n_cp = sstate.get_state()

#check values
self.assertEqual(t_cp, dummy_value)
self.assertEqual(n, n_cp)
#function should be the same everywhere (-> check vector values of the function)
vec_u = u.vector()
vec_u_cp = u_cp.vector()
for i in range(size*size):
self.assertAlmostEqual(vec_u[i], vec_u_cp[i])

def test_solverstate_modification_vector(self):
"""
Check if correct values are read from the checkpoint, if the dof vector of the dolfin functions are changed directly

Motivation for this test: Related to https://github.com/precice/fenics-adapter/pull/172 and https://github.com/precice/tutorials/pull/554
"""
n = 1
size = 5
mesh = UnitSquareMesh(size,size)
V = FunctionSpace(mesh, 'P', 2)
ref_value = 1
E = Expression("t", t=ref_value, degree=2)
u = interpolate(E, V)

# "write checkpoint"
sstate = SolverState(u, ref_value, n)

# modify state of u
dummy_value = ref_value + 2
u.vector()[:] = dummy_value

# "read checkpoint"
u_cp, _, _ = sstate.get_state()

#check values
#function should be the same everywhere
#(so the vector values should all be ref_value)
vec_u_cp = u_cp.vector()
for i in range(size*size):
self.assertAlmostEqual(ref_value, vec_u_cp[i])


def test_solverstate_modification_assign(self):
"""
Check if correct values are read from the checkpoint, if the dof of the dolfin functions are changed with the assign function
and not directly via the dof vector
"""
n = 1
size = 5
mesh = UnitSquareMesh(size,size)
V = FunctionSpace(mesh, 'P', 2)
ref_value = 1
E = Expression("t", t=ref_value, degree=2)
u = interpolate(E, V)

# "write checkpoint"
sstate = SolverState(u, ref_value, n)

# modify state of u
# "compute" new solution
dummy_value = ref_value + 2
E.t = dummy_value
u2 = interpolate(E,V)
u.assign(u2)

# "read checkpoint"
u_cp, _, _ = sstate.get_state()

#check values
#function should be the same everywhere
#(so the vector values should all be ref_value)
vec_u_cp = u_cp.vector()
for i in range(size*size):
self.assertAlmostEqual(ref_value, vec_u_cp[i])