"maximum error at vertices" increased a lot when use irregular mesh instead of regular mesh

Hello everyone. I learned the Fenics tutorial and then tested the ft01_poisson.py provided by the Fenics tutorial. When I used a regular mesh with 81 nodes, the “maximum error at vertices” was “1.3322e-15”. However, when I used an irregular mesh with 90 nodes, the “maximum error at vertices” became “0.005291”. The “maximum error at vertices” increased a lot, which doesn’t make sense because the irregular mesh and regular mesh I used have very close numbers of nodes (one has 81 nodes and the other has 90 nodes).
Any help will be greatly appreciated. Thank you very much in advance.
I presented the code below, which is just the ft01_poisson.py provided by Fenics tutorial.

from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
from mshr import *

# the regular mesh with 81 nodes:
mesh = UnitSquareMesh(8, 8)

# the irregular mesh with 90 nodes:
# domain = Rectangle(Point(0,0), Point(1,1))
# mesh = generate_mesh(domain, 7)

V = FunctionSpace(mesh, 'P', 1)

num_dofs = V.dofmap().global_dimension()
print("num_dofs:",num_dofs)

# Define boundary condition
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

def boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u_D, boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx

# Compute solution
u = Function(V)
solve(a == L, u, bc)

# Plot solution and mesh
plot(u)
plot(mesh)

# Compute error in L2 norm
error_L2 = errornorm(u_D, u, 'L2')

# Compute maximum error at vertices
vertex_values_u_D = u_D.compute_vertex_values(mesh)
vertex_values_u = u.compute_vertex_values(mesh)
import numpy as np
error_max = np.max(np.abs(vertex_values_u_D - vertex_values_u))

# Print errors
print('error_L2  =', error_L2)
print('error_max =', error_max)

# Hold plot
plt.show()

Try changing 1 into 2. Since the exact solution is a polynomial of degree 2, using a P2 finite element space will give you the exact solution up to machine precision.

In general, if you only use a P1 FE space you will get non zero error: in that case, you may want to iteratively decrease the mesh size and verify that the convergence rate is the expected one.

Dear Ballarin,

It works. Using the P2 finite element, the “maximum error at vertices” decreased to “9.3036e-14”. However, the Fenics tutorial said “The machine precision is about 1e-16”. Can we think that “9.3036e-14” is within the machine precision? Thank you very much for your kind help.

Best regards

Yes, you can. Keep in mind that there are operations (solving the linear system, computing the error) that may introduce some additional small errors on top of the machine precision.

Dear Ballarin,

Got it. Thank you very much for your kind help.

Best regards