Skip to content

Update BCS for forward recomputation #4408

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
9 changes: 7 additions & 2 deletions firedrake/bcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,15 @@ def apply(self, r, u=None):
r = r.sub(idx)
if u:
u = u.sub(idx)

bc_checkpointed = self.block_variable.checkpoint
bc = bc_checkpointed.checkpoint \
if bc_checkpointed is not None else self.function_arg
Comment on lines +454 to +456
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this logic be put into a new property that gets overloaded in the corresponding adjoint subclass of DirichletBC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it must be. I am still checking to see if this works. It looks like it requires more thought. Sia just wrote that this is leading some Gadopt tests to fail.


if u:
r.assign(u - self.function_arg, subset=self.node_set)
r.assign(u - bc, subset=self.node_set)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still missing a test for this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will get this case by passing pre_apply_bcs=False to the NLVS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

else:
r.assign(self.function_arg, subset=self.node_set)
r.assign(bc, subset=self.node_set)

def integrals(self):
return []
Expand Down
22 changes: 22 additions & 0 deletions tests/firedrake/regression/test_adjoint_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,3 +1050,25 @@ def u_analytical(x, a, b):
# tape._blocks[1] is the DirichletBC block for the right boundary
assert isinstance(tape._blocks[1], DirichletBCBlock) and \
tape._blocks[1]._outputs[0].checkpoint.checkpoint is not bc_right._original_arg


@pytest.mark.skipcomplex # Taping for complex-valued 0-forms not yet done
def test_bdy_update():
# Fix the issue https://github.com/firedrakeproject/firedrake/issues/4387
mesh = UnitSquareMesh(5, 5)
V = FunctionSpace(mesh, "CG", 1)
R = FunctionSpace(mesh, "R", 0)

bc_func = Function(R, val=2.0)
bc = DirichletBC(V, bc_func, 1)
u_ = Function(V)

v = TestFunction(V)
problem = NonlinearVariationalProblem(
inner(grad(u_), grad(v)) * dx - v * dx, u_, bcs=bc)
solver = NonlinearVariationalSolver(problem)
solver.solve()
J = assemble(u_**2 * dx)
c = Control(bc_func)
Jhat = ReducedFunctional(J, c)
assert taylor_test(Jhat, bc_func, Function(R, val=0.1)) > 1.9
Loading