Interpolation to and from sub-meshes

Dear all, I’m trying to apply an iterative scheme (as discussed here):

  1. Solve on a parent mesh
  2. Move solution to sub mesh
  3. Solve equation on sub mesh
  4. Move solution back and repeat

I think that this test should work (Test interpolation of an expression between a submesh and its parent). I repeat the code here:


from mpi4py import MPI
import numpy as np
import basix
import ufl
from dolfinx import default_real_type, default_scalar_type
from dolfinx.fem import Expression,Function,assemble_scalar,form,functionspace
from dolfinx.mesh import (
    CellType,
    create_mesh,
    create_rectangle,
    create_submesh,
    create_unit_cube,
    create_unit_square,
    locate_entities,
    locate_entities_boundary,
    meshtags,
)

mesh = create_unit_square(MPI.COMM_WORLD, 6, 7)
def left_locator(x):
    return x[0] <= 0.5 + 1e-14
def ref_func(x):
    return x[0] + x[1] ** 2
tdim = mesh.topology.dim
cells = locate_entities(mesh, tdim, left_locator)
submesh, sub_to_parent, _, _ = create_submesh(mesh, tdim, cells)
V = functionspace(mesh, ("Lagrange", 2))
u = Function(V)
u.interpolate(ref_func)
V_sub = functionspace(submesh, ("DG", 3))
u_sub = Function(V_sub)
# Map from parent to sub mesh
u_sub.interpolate(u, cell_map=sub_to_parent)
u_sub_exact = Function(V_sub)
u_sub_exact.interpolate(ref_func)
atol = 5 * np.finfo(default_scalar_type).resolution
np.testing.assert_allclose(u_sub_exact.x.array, u_sub.x.array, atol=atol)

W = functionspace(mesh, ("DG", 4))
w = Function(W)
cell_imap = mesh.topology.index_map(tdim)
num_cells = cell_imap.size_local + cell_imap.num_ghosts
parent_to_sub = np.full(num_cells, -1, dtype=np.int32)
parent_to_sub[sub_to_parent] = np.arange(len(sub_to_parent))
w.interpolate(u_sub_exact, cells=sub_to_parent, cell_map=parent_to_sub)

import dolfinx
with dolfinx.io.XDMFFile(mesh.comm, "f_mesh.xdmf", "w") as file:
    file.write_mesh(mesh)
    file.write_function(w)

with dolfinx.io.XDMFFile(submesh.comm, "f_submesh.xdmf", "w") as file:
    file.write_mesh(submesh)
    file.write_function(u_sub)

but there is an error: TypeError: Function.interpolate() got an unexpected keyword argument ‘cell_map’. I suspect this might be due to my DOLFINx version, which is 0.9.0. Does anyone know a solution?"

Thank you for your time!

If you consider interpolation between parent and submesh for v0.9.0, I would suggest looking at:

and: dolfinx/python/test/unit/fem/test_interpolation.py at v0.9.0.post1 · FEniCS/dolfinx · GitHub
which shows parent to sub, and sub to parent respectively.

2 Likes

I appreciate your quick response, it resolved my issue. Thank you for your time!