Hello folks, hope you all are doing well. I would appreciate if you can offer any help regarding this issue. I was trying to do a H1 projection from fine mesh to coarse mesh but after computing fine mesh sol in quadrature space, fenicsx doesn’t allow to calculate gradient of such function, I would appreciate if you can provide any insight egarding how to achieve this. Here is the minimal code example and error.
import ufl
import dolfinx
from mpi4py import MPI
import basix.ufl
import dolfinx.fem.petsc
from ufl import inner, grad, dx
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(), 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,
V_quadrature.element,
V.mesh, 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 = inner(u, v)*dx + inner(grad(u),grad(v))*dx
L_coarse = inner(q_func, v)*dx + inner(grad(q_func),grad(v))*dx
problem = dolfinx.fem.petsc.LinearProblem(a_coarse, L_coarse)
u_coarse = problem.solve()
error
ValueError Traceback (most recent call last)
<ipython-input-10-f496e4efcf70> in <cell line: 38>()
36 a_coarse = inner(u, v)*dx + inner(grad(u),grad(v))*dx
37 L_coarse = inner(q_func, v)*dx + inner(grad(q_func),grad(v))*dx
---> 38 problem = dolfinx.fem.petsc.LinearProblem(a_coarse, L_coarse)
39 u_coarse = problem.solve()
16 frames
/usr/local/lib/python3.10/dist-packages/basix/ufl.py in tabulate(self, nderivs, points)
1532 """
1533 if nderivs > 0:
-> 1534 raise ValueError("Cannot take derivatives of Quadrature element.")
1535
1536 if points.shape != self._points.shape:
ValueError: Cannot take derivatives of Quadrature element