I found that in dolfinx, the following two equivalent expressions
\begin{align} \int_\Omega div(\nabla v) v, \qquad \text{and} \qquad - \int_{ \Omega} \nabla v \cdot \nabla v + \int_{\partial \Omega} v\nabla v \cdot n \end{align}
are numerically not the same, i.e. their difference is quite “large”. The following assumes that v is a second order polynomial and computes the two integrals:
from mpi4py import MPI
import ufl
from basix.ufl import element
from dolfinx.fem import (Function, form, functionspace,assemble_scalar)
from dolfinx.mesh import *
from ufl import div, dx, ds, grad, inner, FacetNormal
# mesh, spaces
Nx=50
msh = create_unit_square(MPI.COMM_WORLD, Nx, Nx)
P1 = element("Lagrange", "triangle", 3)
XV = functionspace(msh, P1)
x = ufl.SpatialCoordinate(msh)
n = FacetNormal(msh)
# function 1+x^2*y^2
def func(x):
return 1. + x[0]**2 * x[1]**2
pex = Function(XV)
pex.interpolate(func)
# INTEGRAL BEFORE AND AFTER INT. BY PARTS
I1 = form(inner(div(grad(pex)),pex)*dx)
I2 = form((-1)* inner(grad(pex),grad(pex))*dx + inner(grad(pex),pex*n)*ds )
print(assemble_scalar(I1)-assemble_scalar(I2))
On my installation (dolfinx version 0.7, conda-forge, virtual environment in linux), using third-order FEM polynomials, the difference is around 10^{-4}. Is this to be expected?
Bonus question: since when is the double dollar sign syntax $$
for mathmode not working anymore? For some reason, in this post it was necessary to use single dollar signs for the mathjax compiler.