Interpolating Test Function between Mismatched Meshes

Hello,

I have a test function defined on a mesh and a function defined on a refined version of that mesh. I would like to be able to integrate my test function and refined function using quadrature defined on the refined mesh. So something like this:

varform = coarse_test*refined_function*dx(refined_mesh)

It is my understanding that I need to introduce an interpolation operator to map the test function onto the same space as the the refined function, i.e.

varform = Interpolate(coarse_test,V_refined)*refined_function*dx(refined_mesh)

However, fenics’ Interpolate function does not support test function inputs so this line throws an error. Is there any other way to handle interpolation of a test function in this context?

My minimal working example:

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)

coarse_test = TestFunction(coarse_space)
fine_trial = TrialFunction(fine_space)

varf = interpolate(coarse_test,fine_space)*fine_trial*dx(mesh_fine)
A = assemble(varf)

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).