How to repeatedly compute gradient at different points?
Issue #70
new
Hi there,
Can I repeatedly compute the gradient at different points?
So far, I tried with the Burgers' equation code, by wrapping everything in a function jac(x)
, adding adj_inc_timestep
and slightly changing the target functional, but got this error message:
Traceback (most recent call last):
File "/home/Projects/adjointwrapper.py", line 42, in <module>
print jac(x)
File "/home/Projects/adjointwrapper.py", line 28, in jac
adj_start_timestep()
File "/usr/lib/python2.7/dist-packages/dolfin_adjoint/adjglobals.py", line 25, in adj_start_timestep
adjointer.time.start(time)
File "/usr/lib/python2.7/dist-packages/libadjoint/libadjoint.py", line 830, in start
"time.start() called after simulation started!")
libadjoint.exceptions.LibadjointErrorInvalidInputs: time.start() called after simulation started!
[Finished in 5.0s with exit code -6]
Could you please advice what might be wrong? The full code is attached.
from dolfin import *
from dolfin_adjoint import *
# set_log_level(WARNING) #ERROR #INFO
def jac(x):
nu = Constant(x)
n = 30
mesh = UnitSquareMesh(n, n)
V = VectorFunctionSpace(mesh, "CG", 2)
ic = project(Expression(("sin(2*pi*x[0])", "cos(2*pi*x[1])"), degree=2), V)
u = ic.copy(deepcopy=True)
u_next = Function(V)
v = TestFunction(V)
timestep = Constant(0.01)
F = (inner((u_next - u)/timestep, v)
+ inner(grad(u_next)*u_next, v)
+ nu*inner(grad(u_next), grad(v)))*dx
bc = DirichletBC(V, (0.0, 0.0), "on_boundary")
t = 0.0
end = 0.05
adj_start_timestep()
while (t < end):
solve(F == 0, u_next, bc, annotate=True)
u.assign(u_next)
t += float(timestep)
adj_inc_timestep(t, t > end - .5*float(timestep))
J = Functional(inner(u, u)*dx*dt) # integral over time
dJdnu = compute_gradient(J, Control(nu), forget=False)
return float(dJdnu)
x = 0.0001
print jac(x)
x = 0.0002
print jac(x)
Thanks!
Have you figured out why? Can you advice how to make it work?