Hi,
Consider a very simple Poission equation: -Laplacian(u)=f, which has the exact solution u_D=1+x^2+2y^2. I try to test the adjoint method by setting the source term f as control variable to minimise the difference of exact solution u_D and simulated solution u. And I know the exact source term f=-6. But finally I got the optimised f_opt, which is like a Gaussian function, not a constant value I expected. By updating the f_out and solve the Poission equation again, the simulated solution u is very close to the exact one though, I was still wondering if it is possible to get f_opt as a constant value?
Thank you!
Create mesh and define function space
mesh = UnitSquareMesh(20, 20)
V = FunctionSpace(mesh, βPβ, 1)Define boundary condition
u_D = Expression(β1 + x[0] * x[0] + 2 * x[1] * x[1]β, degree=2)
def boundary(x, on_boundary):
return on_boundarybc = DirichletBC(V, u_D, boundary)
Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = interpolate(Constant(-1.0), V)
a = dot(grad(u), grad(v)) * dx
L = f * v * dxCompute solution
u = Function(V)
solve(a == L, u, bc)The functional of interest
J = assemble((0.5 * inner(u-u_D, u-u_D)) * dx)
Run the optimisation
reduced_functional = ReducedFunctional(J, Control(f))
f_opt = minimize(reduced_functional, method = βL-BFGS-Bβ, tol=2e-08, bounds = (-20, 1), options = {βdispβ: True})Update f and solve again
f.assign(f_opt)
solve(a == L, u, bc)