I’m learning dolfin-adjoint but come across the error described in topic.
When I run without optimization, solve(F==0) shows no error.
When optimization or sensitivity analysis codes added, the error comes.
Here’s the mwe (the code is simplified so control actually does not influence object function J). This is a 2D scattering problem and ABC is absorbing boundary condition. I removed obstacle to create for simple mesh, and the result will be a plane wave.

``````import mpi4py.MPI
from fenics import *
import numpy as np
import moola
# pip install git+https://github.com/funsim/moola.git@master

# k = 2*np.pi/0.5 # 2pi/lambda
k0 = 2*np.pi/0.5
E0 = 1.  # incident wave magnitude

mesh = UnitSquareMesh(20,20)

class ABC(SubDomain):
def inside(self,x,on_boundary):
return on_boundary

sub_domains = MeshFunction("size_t",mesh,mesh.topology().dim()-1)
sub_domains.set_all(2)
abc=ABC()
abc.mark(sub_domains,1)
print('1')
ele_type = FiniteElement('Lagrange',triangle,2)
V2 = FunctionSpace(mesh, ele_type*ele_type)
V = FunctionSpace(mesh, ele_type)
uh = Function(V2, name='State')
(u_r, u_i) = split(uh)
(v_r, v_i) = TrialFunction(V2)
ds = Measure('ds', domain=mesh, subdomain_data=sub_domains)
u0 = Constant(0.0)

k = Expression('k0', degree=2, name='Control', k0=k0)
k = interpolate(k, V)
inc_r = Expression('E0*cos(k0*x)', degree=2, k0=k0, E0=E0)
inc_r = interpolate(inc_r, V)
inc_i = Expression('E0*sin(k0*x)', degree=2, k0=k0, E0=E0)
inc_i = interpolate(inc_i, V)

n = FacetNormal(mesh)

solve(F == 0, uh, solver_parameters={"newton_solver": {"absolute_tolerance": 1.0e-7,
"maximum_iterations": 20}})
print('2')

ur, ui = uh.split(True)
File("./results/uh_r.pvd").write(ur)

x = SpatialCoordinate(mesh)
J = assemble(inner(u_r-inc_r, u_r-inc_r)*dx)
control = Control(k)
``````

and full error info

``````Traceback (most recent call last):
File "main_adjoint_mwe.py", line 59, in <module>
File "/usr/local/lib/python3.6/dist-packages/pyadjoint/tape.py", line 46, in wrapper
return function(*args, **kwargs)