Consider the minimal working example:
from mpi4py import MPI
import dolfinx
import numpy as np
L = 1
N = 5
def interface(x):
return np.isclose(x[0], L)
mesh_1 = dolfinx.mesh.create_box(
comm=MPI.COMM_WORLD, points=[(0, 0, 0), (L, L, L)], n=[N, N, N]
)
V = dolfinx.fem.functionspace(mesh_1, ("Lagrange", 2))
u = dolfinx.fem.Function(V)
u.interpolate(lambda x: x[0] + np.cos(x[1]) + np.sin(np.pi * x[2]))
Nx, Ny, Nz = 12, 13, 15
mesh_2 = dolfinx.mesh.create_box(
comm=MPI.COMM_WORLD, points=[(L, -0.1, -0.1), (2 * L, 3 * L, 3 * L)], n=[Nx, Ny, Nz]
)
V_2 = dolfinx.fem.functionspace(mesh_2, ("Lagrange", 2))
boundary_2 = dolfinx.mesh.locate_entities_boundary(
mesh_2, mesh_2.topology.dim - 1, interface
)
adjacent_cells = dolfinx.mesh.compute_incident_entities(
mesh_2.topology, boundary_2, mesh_2.topology.dim - 1, mesh_2.topology.dim
).astype(np.int32)
interpolation_data = dolfinx.fem.create_nonmatching_meshes_interpolation_data(
mesh_2.geometry, V_2.element, mesh_1, adjacent_cells, padding=1.0e-6
)
u2 = dolfinx.fem.Function(V_2)
u2.interpolate(u, cells=adjacent_cells, nmm_interpolation_data=interpolation_data)
with dolfinx.io.VTXWriter(mesh_1.comm, "u_1.bp", [u]) as bp:
bp.write(0.0)
with dolfinx.io.VTXWriter(mesh_2.comm, "u_2.bp", [u2]) as bp:
bp.write(0.0)