Computing integrals over an element in Fenicsx

I want to compute the functionals over each element and then use them later as refinement indicators. A simple example will be to compute \integral_k (u_exact - uh)^2 dx where k represents an element in the mesh. Currently, I compute the L2 norm of the error for a given exact solution using the following snippet of the code:

M = (u_ex  - uh)**2 * ufl.dx
M = fem.form(M)
error = msh.comm.allreduce(fem.assemble_scalar(M),op = MPI.SUM)

But I would like to have an array wich contains element contribution.

regards
Ankit

I would suggest creating a DG-0 space, and assemble

W = dolfinx.fem.FunctionSpace(mesh, ("DG", 0))
v = ufl.TestFunction(W)
a = ufl.inner(u_ex - u_h, u_ex - u_h)*v*ufl.dx
M = dolfinx.fem.form(a)
error = dolfinx.fem.petsc.assemble_vector(M)
1 Like