I though that I had understood the difference between interpolate
and project
. The first one looks for a function that whose values coincide at the points of the mesh whereas the second one looks for a function that minimizes some norm. I tried a very simple case:
Lets say that I have a function defined on the interval \left[0,1\right]
I understand that if I create a mesh with one single cell with vertices \left\lbrace 0, 1\right\rbrace I can construct two different piecewise linear functions. The interpolating one, g would have the values:
x | g(x) |
---|---|
0 | \frac{1}{2} |
1 | 1 |
On the other hand, if I project f onto that piecewise linear space, I would expect to obtain a function h whose values at the vertices are
x | h(x) |
---|---|
0 | \frac{3}{8}=0.375 |
1 | \frac{7}{8}=0.875 |
as I have computed them (by “hand”) by minimizing the L^2 norm of f-h. However, when I try this at dolfin
from dolfin import IntervalMesh, FunctionSpace, FiniteElement, Function, Expression, project
mesh_1 = IntervalMesh(2,0.,1.)
mesh_2 = IntervalMesh(1,0.,1)
H_1 = FunctionSpace( mesh_1, FiniteElement( family="Lagrange", cell= mesh_1.ufl_cell(), degree=1))
H_2 = FunctionSpace( mesh_2, FiniteElement( family="Lagrange", cell= mesh_2.ufl_cell(), degree=1))
f = Function(H_1)
g = Function(H_2)
h = Function(H_2)
f.interpolate(Expression("x[0]<=0.5 ? 0.5 : x[0]",degree=1))
g.interpolate(f)
h = project(f,V=H_2, mesh=mesh_1)
print(' x : f(x)')
for (x,y) in zip(mesh_1.coordinates()[:,0],f.compute_vertex_values()):
print( f'{x:.2f} : {y:.2f}')
print('')
print(' x : g(x)')
for (x,y) in zip(mesh_2.coordinates()[:,0],g.compute_vertex_values()):
print( f'{x:.2f} : {y:.2f}')
print('')
print(' x : h(x)')
for (x,y) in zip(mesh_2.coordinates()[:,0],h.compute_vertex_values()):
print( f'{x:.2f} : {y:.2f}')
I get:
x : f(x)
0.00 : 0.50
0.50 : 0.50
1.00 : 1.00
x : g(x)
0.00 : 0.50
1.00 : 1.00
x : h(x)
0.00 : 0.40
1.00 : 0.70
What am I missing? ( and what is the meaning of the mesh variable in project?)
Thank you very much!