Skip to content
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

Adjoint variational solver #3723

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Adjoint variational solver #3723

wants to merge 28 commits into from

Conversation

Ig-dolci
Copy link
Contributor

@Ig-dolci Ig-dolci commented Aug 4, 2024

Description

This PR updates the adjoint solver implementation when the Jacobian is not constant, changing it from firedrake.solve to LinearVariationalSolver. The existing test suites in pyadjoint and firedrake cover various adjoint-solving scenarios. Additionally, I have included a new test to check the adjoint-based gradient when adjoint boundary conditions need to be computed.

Note: The adjoint solver for cases where the Jacobian is constant may have improvements, but this requires more thought.

@Ig-dolci Ig-dolci changed the title Adjoint linear variational solver Adjoint variational solver Aug 4, 2024
@Ig-dolci Ig-dolci marked this pull request as ready for review August 6, 2024 13:54
@@ -60,7 +60,8 @@ def evaluate_adj_component(self, inputs, adj_inputs, block_variable, idx,
adj_value = firedrake.Function(self.collapsed_space, vec.dat)

if adj_value.ufl_shape == () or adj_value.ufl_shape[0] <= 1:
r = adj_value.dat.data_ro.sum()
R = firedrake.FunctionSpace(self.parent_space.mesh(), "R", 0)
r = firedrake.Function(R.dual(), val=adj_value.dat.data_ro.sum())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix: We need a Cofunction instead of a float.

@@ -48,7 +49,7 @@ def __init__(self, lhs, rhs, func, bcs, *args, **kwargs):
# Equation RHS
self.rhs = rhs
# Solution function
self.func = func
self._func = weakref.ref(func)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove

@@ -72,6 +73,10 @@ def __init__(self, lhs, rhs, func, bcs, *args, **kwargs):
self.add_dependency(mesh)
self._init_solver_parameters(args, kwargs)

@property
def func(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove

dFdu_form, dJdu, compute_bdy
)

if self._ad_nlvs._problem._constant_jacobian:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Work on this part.

@@ -667,31 +721,33 @@ def _assemble_dFdu_adj(self, dFdu_adj_form, **kwargs):

def prepare_evaluate_adj(self, inputs, adj_inputs, relevant_dependencies):
dJdu = adj_inputs[0]

F_form = self._create_F_form()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keep here!

@@ -701,7 +757,7 @@ def evaluate_adj_component(self, inputs, adj_inputs, block_variable, idx,
if not self.linear and self.func == block_variable.output:
# We are not able to calculate derivatives wrt initial guess.
return None
F_form = prepared["form"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make this in prepare

J_hat = ReducedFunctional(J, [Control(a), Control(b)])

assert taylor_test(
J_hat, [a, b], [Function(R, val=rand(1)), Function(R, val=rand(1))]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See this random form. Keep the same random values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant