Interpolating a solution into a finer solution

Hello everyone,

I am trying to interpolate a solution of a coarse mesh into a fine mesh. This does not seem to hard, however, somehow I keep getting the following error: “PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation”. I run my code on a dolfinx 0.8.0.

My MWE is the Poisson equation example from https://jsdokken.com/dolfinx-tutorial/chapter1/fundamentals_code.html where I try to interpolate the solution to a finer mesh. It looks as follows:

from mpi4py import MPI
from dolfinx import mesh
from dolfinx.fem import functionspace, function, Function, locate_dofs_topological,dirichletbc, Constant
import numpy

domain = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8, mesh.CellType.quadrilateral)
V = functionspace(domain, ("Lagrange", 1))
uD = Function(V)
uD.interpolate(lambda x: 1 + x[0]**2 + 2 * x[1]**2)

# Create facet to cell connectivity required to determine boundary facets
tdim = domain.topology.dim
fdim = tdim - 1
domain.topology.create_connectivity(fdim, tdim)
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = locate_dofs_topological(V, fdim, boundary_facets)
bc = dirichletbc(uD, boundary_dofs)
import ufl
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
from dolfinx import default_scalar_type
f = Constant(domain, default_scalar_type(-6))
a = ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
L = f * v * ufl.dx
from dolfinx.fem.petsc import LinearProblem
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = Function(V)
uh.interpolate(lambda x: 1 + x[0]**2 + 2 * x[1]**2)

fine_domain = mesh.create_unit_square(MPI.COMM_WORLD, 16, 16, mesh.CellType.quadrilateral)
fine_V = functionspace(fine_domain, ("Lagrange",1))
fine_uh = Function(fine_V)
fine_uh.interpolate(uh)

Can someone please show me why this happens? I have been trying to solve it for the past days but I cannot find the reason for the error.

Many thanks in advance!
Cheers, Daan

There are dedicated functionalities for interpolation on different meshes.

A starting point is for instance

if you are on the main branch
or

if you are on 0.8.0.

Once you read that, you’ll notice a few keywords (for instance, create_nonmatching_meshes_interpolation_data in 0.8.0) that may help you find several related posts in this forum which will contain other useful examples.