Interpolating Test Function between Mismatched Meshes

A working but propably very slow way to do this is setting up a function to act as the test function and assembling multiple vectors:

from dolfin import *

mesh_coarse = UnitSquareMesh(2, 2)
mesh_fine = refine(mesh_coarse)

coarse_space = FunctionSpace(mesh_coarse,"CG",1)
fine_space = FunctionSpace(mesh_fine,"CG",1)

fine_trial = TestFunction(fine_space)

A = []
for dof in range(coarse_space.dim()):
    coarse_test = Function(coarse_space)
    coarse_test.vector()[dof] = 1
    coarse_test_interpolated = interpolate(coarse_test, fine_space)
    assert( near( assemble( coarse_test**2 * dx ), assemble( coarse_test_interpolated**2 * dx ) ) )
    A.append( assemble(coarse_test_interpolated*fine_trial*dx ) )

print(A[0][:])

This post

describes how you can get a matrix from these vectors. You may have to transpose the matrix.

Note also that this works for this example, as the function spaces on the fine and coarse mesh are hierarchical, and thus the interpolated function acting as test function has the same shape as the original function (see the heuristic assert() test in the loop).