Finite difference approximation of functionals seems to have some unusual behavior. Usually the rule of thumb is that you can take functional finite differences down to around the square root of machine precision ($\approx 10^{-7}$) or often lower without a problem, but in my tests I observe that in Fenics, functional finite differences break down way earlier, around a stepsize of $10^{-2}$(!)
I've seen this in several situations, but heres a simple "minimal example" demonstrating the issue. One defines
$$f(u):=\int_{\partial \Omega} \nabla u \cdot n ds,$$
and computes the finite difference,
$$f'(u) \delta u \approx (f(u+s\delta u) - f(u))/s.$$
The dolfin code is as follows:
from dolfin import *
mesh = UnitSquareMesh(30,30)
V = FunctionSpace(mesh,'Lagrange',1)
u = Function(V)
n = FacetNormal(mesh)
f = dot(nabla_grad(u), n)*ds
u0 = project(Expression('sin(2*pi*x[0]+1)+cos(2*pi*x[1]-1)+x[0]*x[1]+x[1]'),V) #Some random function I made up
f0 = replace(f,{u:u0})
delta_u = project(Expression('1.3-tan(x[0])+sin(2*pi*x[0])*exp(x[1])'),V)
set_log_level(50)
print 's', 'form','diff'
for k in range(9):
s = pow(10,-k)
u1 = project(u0 + s*delta_u)
f1 = replace(f,{u:u1})
df_diff = (assemble(f1) - assemble(f0))/s
df = derivative(f,u,delta_u)
df_form = assemble(df)
print s, df_form, df_diff
When run it outputs the following:
Solving linear system of size 961 x 961 (PETSc Krylov solver).
Solving linear system of size 961 x 961 (PETSc Krylov solver).
s form diff
1 -2.61447044141 -2.61445236177
0.1 -2.61447044141 -2.61439465518
0.01 -2.61447044141 -2.61379277866
0.001 -2.61447044141 -2.60777870884
0.0001 -2.61447044141 -2.54763865842
1e-05 -2.61447044141 -1.94623822025
1e-06 -2.61447044141 4.06776613726
1e-07 -2.61447044141 64.2078098556
1e-08 -2.61447044141 665.608249162
In the right column you can see the accuracy of the finite difference deteriorating as the stepsize is made smaller, far before one would expect it to.
What is going on here?
Thanks for any help.