Skip to content

Fix adjoint interpolation #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 13 additions & 17 deletions animate/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,31 +232,27 @@ def _transfer_adjoint(target_b, source_b, transfer_method, **kwargs):
bounded = is_project and kwargs.pop("bounded", False)

# Map to Functions to apply the adjoint transfer
if not isinstance(target_b, firedrake.Function):
target_b = cofunction2function(target_b)
if not isinstance(source_b, firedrake.Function):
source_b = cofunction2function(source_b)
target_b_func = cofunction2function(target_b)
source_b_func = cofunction2function(source_b)

Vt = target_b.function_space()
Vs = source_b.function_space()
Vt = target_b_func.function_space()
Vs = source_b_func.function_space()
if Vs == Vt:
source_b.assign(target_b)
return function2cofunction(source_b)
source_b_func.assign(target_b_func)
return function2cofunction(source_b_func, source_b)

_validate_matching_spaces(Vs, Vt)
if hasattr(Vs, "num_sub_spaces"):
target_b_split = target_b.subfunctions
source_b_split = source_b.subfunctions
target_b_func_split = target_b_func.subfunctions
source_b_func_split = source_b_func.subfunctions
else:
target_b_split = [target_b]
source_b_split = [source_b]
target_b_func_split = [target_b_func]
source_b_func_split = [source_b_func]

# Apply adjoint transfer operator to each component
for i, (t_b, s_b) in enumerate(zip(target_b_split, source_b_split)):
for i, (t_b, s_b) in enumerate(zip(target_b_func_split, source_b_func_split)):
if transfer_method == "interpolate":
raise NotImplementedError(
"Adjoint of interpolation operator not implemented."
) # TODO (#113)
s_b.interpolate(t_b, adjoint=True, **kwargs)
elif transfer_method == "project":
ksp = petsc4py.KSP().create()
ksp.setOperators(assemble_mass_matrix(t_b.function_space(), lumped=bounded))
Expand All @@ -272,7 +268,7 @@ def _transfer_adjoint(target_b, source_b, transfer_method, **kwargs):
)

# Map back to a Cofunction
return function2cofunction(source_b)
return function2cofunction(source_b_func, source_b)


def _validate_matching_spaces(Vs, Vt):
Expand Down
14 changes: 10 additions & 4 deletions animate/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,17 @@ def assemble_mass_matrix(space, norm_type="L2", lumped=False):
return mass_matrix.createDiagonal(x)


def cofunction2function(cofunc):
def cofunction2function(cofunc, func=None):
"""
:arg cofunc: a cofunction
:type cofunc: :class:`firedrake.cofunction.Cofunction`
:kwarg func: a function for the return value
:type func: :class:`firedrake.function.Function`
:returns: a function with the same underyling data
:rtype: :class:`firedrake.function.Function`
"""
func = ffunc.Function(cofunc.function_space().dual())
if func is None:
func = ffunc.Function(cofunc.function_space().dual())
if isinstance(func.dat.data_with_halos, tuple):
for i, arr in enumerate(func.dat.data_with_halos):
arr[:] = cofunc.dat.data_with_halos[i]
Expand All @@ -295,14 +298,17 @@ def cofunction2function(cofunc):
return func


def function2cofunction(func):
def function2cofunction(func, cofunc=None):
"""
:arg func: a function
:type func: :class:`firedrake.function.Function`
:kwarg cofunc: a cofunction for the return value
:type cofunc: :class:`firedrake.cofunction.Cofunction`
:returns: a cofunction with the same underlying data
:rtype: :class:`firedrake.cofunction.Cofunction`
"""
cofunc = firedrake.Cofunction(func.function_space().dual())
if cofunc is None:
cofunc = firedrake.Cofunction(func.function_space().dual())
if isinstance(cofunc.dat.data_with_halos, tuple):
for i, arr in enumerate(cofunc.dat.data_with_halos):
arr[:] = func.dat.data_with_halos[i]
Expand Down
13 changes: 5 additions & 8 deletions test/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
TensorFunctionSpace,
VectorFunctionSpace,
)
from firedrake.norms import errornorm
from firedrake.utility_meshes import UnitSquareMesh
from parameterized import parameterized

Expand All @@ -28,7 +27,7 @@
project,
transfer,
)
from animate.utility import function2cofunction
from animate.utility import errornorm, function2cofunction


class TestClement(unittest.TestCase):
Expand Down Expand Up @@ -255,9 +254,8 @@ def test_transfer_same_space(self, transfer_method):
expected = source
self.assertAlmostEqual(errornorm(expected, target), 0)

@parameterized.expand(["project"]) # TODO: interpolate (#113)
@parameterized.expand(["interpolate", "project"])
def test_transfer_same_space_adjoint(self, transfer_method):
pytest.skip() # TODO: (#114)
Vs = FunctionSpace(self.source_mesh, "CG", 1)
source = Function(Vs).interpolate(self.sinusoid())
source = function2cofunction(source)
Expand All @@ -279,9 +277,8 @@ def test_transfer_same_space_mixed(self, transfer_method):
expected = source
self.assertAlmostEqual(errornorm(expected, target), 0)

@parameterized.expand(["project"]) # TODO: interpolate (#113)
@parameterized.expand(["interpolate", "project"])
def test_transfer_same_space_mixed_adjoint(self, transfer_method):
pytest.skip() # TODO: (#114)
P1 = FunctionSpace(self.source_mesh, "CG", 1)
Vs = P1 * P1
source = Function(Vs)
Expand All @@ -307,7 +304,7 @@ def test_transfer_same_mesh(self, transfer_method):
expected = Function(Vt).project(source)
self.assertAlmostEqual(errornorm(expected, target), 0)

@parameterized.expand(["project"]) # TODO: interpolate (#113)
@parameterized.expand(["interpolate", "project"])
def test_transfer_same_mesh_adjoint(self, transfer_method):
pytest.skip() # TODO: (#114)
Vs = FunctionSpace(self.source_mesh, "CG", 1)
Expand Down Expand Up @@ -343,7 +340,7 @@ def test_transfer_same_mesh_mixed(self, transfer_method):
e2.project(s2)
self.assertAlmostEqual(errornorm(expected, target), 0)

@parameterized.expand(["project"]) # TODO: interpolate (#113)
@parameterized.expand(["interpolate", "project"])
def test_transfer_same_mesh_mixed_adjoint(self, transfer_method):
pytest.skip() # TODO: (#114)
P1 = FunctionSpace(self.source_mesh, "CG", 1)
Expand Down