L2 projection between meshes

I attempted to replicate the code by Jørgen S. Dokken from this discussion in my current version of FEniCSx (0.7.3), but I encountered the following error:


# Illustration of projection from one mesh to another
# Author: Jørgen S. Dokken
# SPDX License: MIT
import ufl
import dolfinx
from mpi4py import MPI
import basix.ufl
import dolfinx.fem.petsc
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

# Compute function on fine mesh
V = dolfinx.fem.functionspace(mesh, (("Lagrange", 1)))
uh = dolfinx.fem.Function(V)
uh.interpolate(lambda x: x[0]+2*x[1])

# Create coarse mesh
coarser_mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 5, 5)
V_coarse = dolfinx.fem.functionspace(coarser_mesh, ("Lagrange", 1))


# Compute function for fine grid at quadrature points of the coarse grid
degree = 4
Qe = basix.ufl.quadrature_element(
    coarser_mesh.topology.cell_name(), value_shape=(),degree=degree)
V_quadrature = dolfinx.fem.functionspace(coarser_mesh, Qe)
q_e = dolfinx.fem.Function(V_quadrature)
nmmid = dolfinx.fem.create_nonmatching_meshes_interpolation_data(V_quadrature.mesh._cpp_object,
                                                                 V_quadrature.element,
                                                                 V.mesh._cpp_object, padding=1e-5)

q_func = dolfinx.fem.Function(V_quadrature)
q_func.interpolate(uh, nmm_interpolation_data=nmmid)

# Project fine function at quadrature points to coarse grid
u = ufl.TrialFunction(V_coarse)
v = ufl.TestFunction(V_coarse)
a_coarse = ufl.inner(u, v) * ufl.dx
L_coarse = ufl.inner(q_func, v)*ufl.dx
problem = dolfinx.fem.petsc.LinearProblem(a_coarse, L_coarse)
u_coarse = problem.solve()

with dolfinx.io.VTXWriter(coarser_mesh.comm, "u_coarse.bp", [u_coarse], engine="BP4") as bp:
    bp.write(0.0)

and the error is

    nmmid = dolfinx.fem.create_nonmatching_meshes_interpolation_data(V_quadrature.mesh._cpp_object,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Cannot get interpolation points - no Basix element available. Maybe this is a mixed element?

Could someone help me debug this error?

As 0.7 was released prior to: Implement interpolation for quadrature elements by mscroggs · Pull Request #2878 · FEniCS/dolfinx · GitHub
you cannot use this functionality, please upgrade to v0.8

Hi Dokken,

I have upgraded my version to 0.8 but now it now pops up a different error.

dolfinx-version 0.8.0 installed via repository ppa:fenics-packages/fenics on Ubuntu 20.04
Traceback (most recent call last):
  File "/home/peri_pr/Schreibtisch/G-Laufwerk/Oeffentlich/peri_pr/Interpolation/Cylinder.py", line 30, in <module>
    nmmid = dolfinx.fem.create_nonmatching_meshes_interpolation_data(V_quadrature.mesh._cpp_object, V_quadrature.element,V.mesh._cpp_object, padding=1e-5)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peri_pr/miniforge3/envs/fenicsx-env/lib/python3.12/site-packages/dolfinx/fem/__init__.py", line 94, in create_nonmatching_meshes_interpolation_data
    mesh_to._cpp_object, element, mesh_from._cpp_object, padding
    ^^^^^^^^^^^^^^^^^^^
AttributeError: 'dolfinx.cpp.mesh.Mesh_float64' object has no attribute '_cpp_object'

``

See: Interpolation Data Has Wrong Shape/Size - #6 by dokken