How to interpolate experimental data with finite elements using FEniCs?

I want to use FEniCs to compare experimental data and calculated data (by finite element method). I have a very large number of experimental data which are not exactly at the same point of my finite element mesh. So I need to interpolate these experimental data using finite elements. But I don’t know to do this in FEniCs.

I am thinking that the code below can do the job but not very sure. If it can do the Job how to change v = Expression("sin(pi*x[0])", degree=2) with my experimental data.

from dolfin import *
import matplotlib.pyplot as plt

mesh = UnitSquareMesh(24, 24)

v = Expression("sin(pi*x[0])", degree=2)
V = FunctionSpace(mesh, "Lagrange", 1)
Iv = interpolate(v, V)

plt.figure()
plot(Iv, title = 'Iv')
plot(mesh)

plt.show()

Also it is possible explain me how the interpolation is done.

Please need your help. Thank you

This will slightly depend on the layout of your experimental data, and what you want to do with it.

If you only need to compare them pointwise with your FEM solution, then it is easier to evaluate your FEM solution at experimental data coordinates.
How to do so will also depend on your data:

  • If your dataset is a bunch of values scattered at multiple coordinates, with no spatial structure, you will need to use the eval method from your fem solution.
  • If your dataset has a regular spatial structure (image, voxel data, etc.) and is very large, I highly recommend using pyvista for evaluating your FEM solution. The reason is that, compared to the eval method, pyvista will take advantage of the structured layout of your data to use a different evaluation algorithm which is orders of magnitude faster. Doing so involves converting your dolfin mesh to a pyvista.UnstructuredGrid, copy the values of your FEM solution inside the pyvista grid point_data or cell_data, wrap your experimental data inside a pyvista.UniformGrid, and then call the probe method from the unstructured grid.

If for some reason, you really need to interpolate your data onto your FEM mesh, then again, they are two cases:

  • If your dataset has a defined connectivity between data points or a spatial structure, then you need to construct a FEM mesh from your dataset, and then interpolate from that mesh onto you initial FEM mesh. To do so, see, e.g. here.
  • If your dataset has no connectivity nor any spatial structure, then there is no obvious way to define such an interpolation. You could try to define a UserExpression that stores your experimental data, and at each location, define your own custom operation to extract the corresponding experimental value.

Thanks for the quick reply. I am going to test your propositions and if I still have problems, I will come back.

I would just like to add that this depends on the function space your functions are in. If the function space is CG-1 you can use the dof_to_vertex map to map the data directly from the function pyvista grid.

However, if you are using any other or discontinuous Lagrange space, mapping (interpolating) it to a CG-1 mesh will cause a loss of information/accuracy.