-
Notifications
You must be signed in to change notification settings - Fork 157
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
base: master
Are you sure you want to change the base?
Conversation
@@ -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()) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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"] |
There was a problem hiding this comment.
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))] |
There was a problem hiding this comment.
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.
Description
This PR updates the adjoint solver implementation when the Jacobian is not constant, changing it from
firedrake.solve
toLinearVariationalSolver
. 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.