Skip to content

Commit

Permalink
Dualspace (#70)
Browse files Browse the repository at this point in the history
* Assembled 1-forms are now Cofunction in Firedrake
* Expunge Vector from firedrake_adjoint
  • Loading branch information
nbouziani authored Sep 20, 2023
1 parent 8e524e4 commit 78b3bd6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions tests/firedrake_adjoint/test_assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/firedrake_adjoint/test_solving.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion tests/firedrake_adjoint/test_tlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 78b3bd6

Please sign in to comment.