How to interpolate a function defined on a smaller mesh to select nodal points on a larger mesh

Hi,

I am trying to interpolate a function defined on mesh1 to mesh2. Following the example from: here, I am able to create a transfer matrix and interpolate functions between meshes.

The case that I am looking to implement is a case where mesh2 has points that is not inside mesh1. The create_transfer_matrix() function seems to do some kind of interpolation for points not in mesh1. I am wondering if I can create a transfer matrix such that it only interpolates to nodal points in mesh2 which is inside mesh1 and set the rest to 0.

Here is a 1D example:

import numpy as np
import matplotlib.pyplot as plt
import sys
import fenics as fe

mesh1 = fe.IntervalMesh(40,4,6) # Small finer mesh
mesh2 = fe.IntervalMesh(20,3,7) # Large coarse mesh
V1    = fe.FunctionSpace( mesh1, "Lagrange", 1 )
V2    = fe.FunctionSpace( mesh2, "Lagrange", 1 )
f1 = fe.Function(V1)
# Create transfer matrix from V1 to V2
A = fe.PETScDMCollection.create_transfer_matrix(V1,V2)
f1.interpolate(fe.Expression("exp(-x[0])",degree=2))
f2 = fe.Function(V2)
# Interpolate f1 to f2
f2.vector()[:] = A*f1.vector()
# Plot result
plt.figure()
fe.plot(f2,label='f2')
fe.plot(f1,label='f1')
plt.legend()
plt.show()

which gives a result as:
transfer_matrix_intrp

I am looking for a result which looks like:
Actual_intrp

Thank you very much for any insights you may have.

1 Like