How to optimise a constant?

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_boundary

bc = 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 * dx

Compute 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)

1 Like

You can add a regularization term to your objective functional that regularizes the spatial derivative of f:

alpha = 1e-5 # Regularisation parameter
J += alpha * assemble(inner(grad(f), grad(f))*dx)
1 Like

Thanks a lot! It works!