From 78b3bd6bb12e7426abf0af8e981c91f7b56b34c8 Mon Sep 17 00:00:00 2001 From: Nacime Bouziani <48448063+nbouziani@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:27:29 +0100 Subject: [PATCH] Dualspace (#70) * Assembled 1-forms are now Cofunction in Firedrake * Expunge Vector from firedrake_adjoint --- tests/firedrake_adjoint/test_assemble.py | 8 +++++--- tests/firedrake_adjoint/test_solving.py | 4 ++-- tests/firedrake_adjoint/test_tlm.py | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/firedrake_adjoint/test_assemble.py b/tests/firedrake_adjoint/test_assemble.py index 62cbb7bd..6ef8c8b2 100644 --- a/tests/firedrake_adjoint/test_assemble.py +++ b/tests/firedrake_adjoint/test_assemble.py @@ -55,7 +55,8 @@ def J(f): w1 = assemble(inner(f, v) * dx) w2 = assemble(inner(f**2, v) * dx) w3 = assemble(inner(f**3, v) * dx) - return assemble((w1 + w2 + w3)**2 * dx) + inner_dual = lambda x: assemble(action(x, x.riesz_representation())) + return sum(inner_dual(c) for c in (w1, w2, w3)) _test_adjoint(J, f) @@ -72,7 +73,8 @@ def test_assemble_1_forms_tlm(): w1 = assemble(inner(f, v) * dx) w2 = assemble(inner(f**2, v) * dx) w3 = assemble(inner(f**3, v) * dx) - J = assemble((w1 + w2 + w3)**2 * dx) + inner_dual = lambda x: assemble(action(x, x.riesz_representation())) + J = sum(inner_dual(c) for c in (w1, w2, w3)) Jhat = ReducedFunctional(J, Control(f)) h = Function(V) @@ -102,7 +104,7 @@ def _test_adjoint(J, f): Jm.block_variable.adj_value = 1.0 tape.evaluate_adj() - dJdf = f.block_variable.adj_value + dJdf = f.block_variable.adj_value.vector() residual = abs(Jp - Jm - eps * dJdf.inner(h.vector())) residuals.append(residual) diff --git a/tests/firedrake_adjoint/test_solving.py b/tests/firedrake_adjoint/test_solving.py index fd216bef..45edb07e 100644 --- a/tests/firedrake_adjoint/test_solving.py +++ b/tests/firedrake_adjoint/test_solving.py @@ -387,7 +387,7 @@ def _test_adjoint_constant(J, c): Jm.block_variable.adj_value = 1.0 tape.evaluate_adj() - dJdc = c.block_variable.adj_value[0] + dJdc = c.block_variable.adj_value.vector()[0] print(dJdc) residual = abs(Jp - Jm - eps*dJdc) @@ -419,7 +419,7 @@ def _test_adjoint(J, f): Jm.block_variable.adj_value = 1.0 tape.evaluate_adj() - dJdf = f.block_variable.adj_value + dJdf = f.block_variable.adj_value.vector() residual = abs(Jp - Jm - eps*dJdf.inner(h.vector())) residuals.append(residual) diff --git a/tests/firedrake_adjoint/test_tlm.py b/tests/firedrake_adjoint/test_tlm.py index fa11b8db..03321398 100644 --- a/tests/firedrake_adjoint/test_tlm.py +++ b/tests/firedrake_adjoint/test_tlm.py @@ -61,7 +61,9 @@ def test_tlm_bc(): J = assemble(c ** 2 * u * dx) Jhat = ReducedFunctional(J, Control(c)) - c.block_variable.tlm_value = Constant(1) + # Need to specify the domain for the constant as `ufl.action`, which requires `ufl.Constant` + # to have a function space, will be applied on the tlm value. + c.block_variable.tlm_value = Constant(1, domain=mesh) tape.evaluate_tlm() assert (taylor_test(Jhat, Constant(c), Constant(1), dJdm=J.block_variable.tlm_value) > 1.9)