Gradient computations with intermediate parameter updates and solves

Issue #83 new
Former user created an issue

Hello, I am currently using dolfin adjoint and libadjoint at version 2016.2 and dolfin at version 2017.2.0.dev0. I am solving a PDE where the coefficients are updated once in a while by a black box function with returns only a vector of values. The PDE is re-solved when the coefficients are updated, and I want to compute the gradient of a functional after the update and re-solve. However, I am getting the error that the forward value for the coefficient is required. Adding forget=False gives me other errors instead. The coefficients are updated thousands of times and I don't really want to use forget=False, and it should not be needed since I re-solve the PDE every time. A minimal working example is as follows:

from dolfin import*
from dolfin_adjoint import *

mesh = UnitIntervalMesh(10)
V = FunctionSpace(mesh, 'CG', 1)

u = TrialFunction(V)
v = TestFunction(V)
q = Function(V, name='coefficient')
q.vector()[:] = 1.0
a = -q * inner(grad(u), grad(v)) * dx
f = Constant(1.0)
L = inner(f, v) * dx
u = Function(V)
bc = DirichletBC(V, 0.0, 'on_boundary')
solve(a == L, u, bc)

fun = inner(u, u) * dx
J = Functional(fun)

dJ1 = compute_gradient(J, Control(q))
q.vector()[:] = 2.0
solve(a == L, u, bc)
dJ2 = compute_gradient(J, Control(q))

The error message is as follows:

Solving linear variational problem. Solving linear variational problem. Traceback (most recent call last): File "/home/jens/Code/Main/Research/inverse/test.py", line 24, in <module> dJ2 = compute_gradient(J, Control(q)) File "/home/jens/anaconda3/lib/python3.6/site-packages/dolfin_adjoint/drivers.py", line 151, in compute_gradient (adj_var, output) = adjglobals.adjointer.get_adjoint_solution(i, J) File "/home/jens/anaconda3/lib/python3.6/site-packages/libadjoint/libadjoint.py", line 1177, in get_adjoint_solution clib.adj_get_adjoint_solution(self.adjointer, equation, str(functional), output, adj_var) File "/home/jens/anaconda3/lib/python3.6/site-packages/libadjoint/libadjoint.py", line 47, in handle_error raise exception(errstr) libadjoint.exceptions.LibadjointErrorNeedValue: Need a value for coefficient:0:0:Forward, but don't have one recorded.

Comments (0)

  1. Log in to comment