Dear community, I found the inconsistent derivatives at the quadrature points (in Dolfinx v0.8.0) and would like to know how to fix it. Here is the MWE:
from mpi4py import MPI
from dolfinx.mesh import create_rectangle, CellType
import scipy
import ufl
from dolfinx.fem import functionspace, Function, Expression, form
from dolfinx.fem.petsc import create_vector, assemble_vector, create_matrix, assemble_matrix
from basix.ufl import quadrature_element
mesh = create_rectangle(MPI.COMM_WORLD, [[0.0, 0.0], [2, 5]],
[1, 1], CellType.quadrilateral)
mesh.geometry.x[0, 1] += 0.5
S0 = functionspace(mesh, ("DG", 0))
S = functionspace(mesh, ("CG", 1))
quad_element = quadrature_element(mesh.topology.cell_name(), degree=2)
SQ = functionspace(mesh, quad_element)
u = Function(S)
uq = Function(SQ)
v = ufl.TestFunction(SQ)
# Compute element volumes
v0 = ufl.TestFunction(S0)
volume_form = form(v0*ufl.dx)
volume_vec = create_vector(volume_form)
assemble_vector(volume_vec, volume_form)
vol = Function(S0)
vol.vector.array = volume_vec.array
# FEniCSx solution
num_pts = 4
dudu_form = form(ufl.derivative(num_pts*u*v/vol*ufl.dx, u))
dudu_mat = create_matrix(dudu_form)
assemble_matrix(dudu_mat, dudu_form)
dudu_mat.assemble()
ai, aj, av = dudu_mat.getValuesCSR()
mat = scipy.sparse.csr_matrix((av, aj, ai)).todense()
# Finite difference
pts = uq.function_space.element.interpolation_points()
u_expr = Expression(u, pts)
uq.interpolate(u_expr)
value1 = uq.vector.array.copy()
idx, eps = 0, 1e-6
u.vector.array[idx] += eps
uq.interpolate(u_expr)
value2 = uq.vector.array.copy()
fenicsx_sol = mat[:, idx].ravel()
finite_difference = (value2-value1) / eps
print(fenicsx_sol)
print(finite_difference)
The results are
[[0.60310759 0.17173114 0.16160219 0.04601522]]
[0.62200847 0.16666667 0.16666667 0.0446582 ]
and we can see the inconsistencies.
If we use the degree-1 quadrature element or use a rectangular element by commenting
mesh.geometry.x[0, 1] += 0.5
the derivatives are consistent again:
[[0.62200847 0.16666667 0.16666667 0.0446582 ]]
[0.62200847 0.16666667 0.16666667 0.0446582 ]