The Poisson equation in 3D#
Author: Jørgen S. Dokken
SPDX-License-Identifier: MIT
The previous demos solve an eigenvalue problem. Here, we use the same library for the Poisson equation
with a manufactured solution, such that we can measure the error. We choose a Gaussian bump
and set \(f=-\Delta u_{ex}\) and \(g=u_{ex}\). The solution is smooth, but varies rapidly close to \(x_0\) and is almost constant away from it, so a high degree is only needed close to the bump.
The problem#
The discretisation is the symmetric interior penalty method [ABCM02], where the Dirichlet condition is imposed weakly with Nitsche’s method [Nit71]. The residual of the degree \(i\) cells is
and the interface terms couple the degrees as in the submesh demo.
The bilinear and linear forms are extracted from the residual with ufl.lhs() and
ufl.rhs().
The problem is described by a dolfinx_padapt.models.PoissonProblem, which takes the
manufactured solution as a function of the spatial coordinate:
from mpi4py import MPI
from petsc4py import PETSc
import dolfinx
import numpy as np
import ufl
from dolfinx_padapt import PoissonProblem
from dolfinx_padapt.adaptivity import solve_adaptive
from dolfinx_padapt.approaches import SubmeshApproach
from dolfinx_padapt.forms import Parameters
from dolfinx_padapt.plotting import Animation, plot_convergence, plot_history
from dolfinx_padapt.postprocessing import print_header, print_iteration
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 6, 6, 6)
def u_exact(x):
return ufl.exp(-50 * ((x[0] - 0.3) ** 2 + (x[1] - 0.4) ** 2 + (x[2] - 0.6) ** 2))
problem = PoissonProblem(mesh, u_exact)
num_cells = mesh.topology.index_map(mesh.topology.dim).size_global
PETSc.Sys.Print(f"Number of cells: {num_cells}")
Number of cells: 1296
The error is measured in the \(L^2\)-norm and the broken \(H^1\)-seminorm, \(\Vert\nabla(u_h-u_{ex})\Vert_{L^2(\Omega)}\), which is the error measure of the adaptive loop.
Error indicator#
The error indicator is the one of the eigenvalue demos, with the cell residual \(f + \Delta u_h\) and the boundary term \(u_h - g\) [HSchotzauW07]:
The adaptive loop#
We use one submesh per degree, with degrees from 1 to 4, and the approach takes the problem as an argument. We track the progress in a GIF: the solution on three slices through the centre of the bump (left), and the number of cells of each degree (right).
params = Parameters(pmin=1, pmax=4)
approach = SubmeshApproach(problem, params)
degrees = approach.degrees
with Animation(mesh.comm, "poisson.gif", origin=(0.3, 0.4, 0.6)) as animation:
history = solve_adaptive(approach, max_iterations=12, tol=1e-3, callback=animation)
it dofs error eta update solve estim. values, degrees
0 5184 5.256e-01 4.347e+00 0.015 0.036 0.003 L2=0.01567445462, H1=0.5256280964, {1: 1296}
1 5226 4.749e-01 2.961e+00 0.018 0.037 0.003 L2=0.01471671868, H1=0.4748635189, {1: 1289, 2: 7}
2 5340 4.228e-01 2.166e+00 0.015 0.036 0.003 L2=0.01271110053, H1=0.4227996025, {1: 1280, 2: 10, 3: 6}
3 5599 3.554e-01 1.724e+00 0.015 0.045 0.004 L2=0.01085245524, H1=0.3553901465, {1: 1261, 2: 19, 3: 13, 4: 3}
4 6216 2.504e-01 1.287e+00 0.015 0.059 0.004 L2=0.007748709045, H1=0.2504086943, {1: 1214, 2: 49, 3: 19, 4: 14}
5 7023 1.394e-01 8.145e-01 0.015 0.087 0.004 L2=0.003493996636, H1=0.1394400757, {1: 1162, 2: 74, 3: 31, 4: 29}
6 7752 1.083e-01 6.090e-01 0.015 0.111 0.004 L2=0.00248735922, H1=0.1083316601, {1: 1128, 2: 81, 3: 41, 4: 46}
7 9223 6.077e-02 3.630e-01 0.015 0.169 0.005 L2=0.00132285697, H1=0.06077127015, {1: 1067, 2: 81, 3: 69, 4: 79}
8 10680 3.801e-02 2.409e-01 0.015 0.264 0.005 L2=0.0007126648654, H1=0.03801412741, {1: 1015, 2: 80, 3: 81, 4: 120}
9 12272 2.850e-02 1.804e-01 0.016 0.340 0.006 L2=0.0004582117317, H1=0.02849962568, {1: 968, 2: 68, 3: 92, 4: 168}
10 13847 2.364e-02 1.523e-01 0.016 0.444 0.006 L2=0.0003264833572, H1=0.02364385222, {1: 918, 2: 73, 3: 82, 4: 223}
11 15383 2.206e-02 1.423e-01 0.015 0.546 0.007 L2=0.000287299206, H1=0.0220635885, {1: 882, 2: 58, 3: 79, 4: 277}
Comparison with a uniform degree#
We compare the adaptive solution with the solution of the same degree on every cell.
uniform = []
for k in range(params.pmin, params.pmax + 1):
degrees.set_degrees(np.full(degrees.num_owned_cells, k, dtype=np.int32))
result = solve_adaptive(approach, max_iterations=1)[0]
PETSc.Sys.Print(f"P{k}: {result.num_dofs} dofs, H1 error {result.error:.3e}")
uniform.append(result)
P1: 5184 dofs, H1 error 5.256e-01
P2: 12960 dofs, H1 error 2.056e-01
P3: 25920 dofs, H1 error 7.580e-02
P4: 45360 dofs, H1 error 2.155e-02
Coarsening#
The degree can also be lowered where the error is small. To see the effect of coarsening, we start two runs from degree 3 on every cell: one that only increases the degree, and one that at every iteration also lowers the degree (never below \(p_{min}\)) of the cells with \(\eta_K < \theta_c \max_{K'}\eta_{K'}\), with \(\theta_c = 0.05\). The cells marked for enrichment are not coarsened, and \(\theta_c\) is much smaller than the marking parameter for enrichment, such that cells do not alternate between the two.
degrees.set_degrees(np.full(degrees.num_owned_cells, 3, dtype=np.int32))
print_header()
enriched = solve_adaptive(approach, max_iterations=8, tol=1e-3, callback=print_iteration)
degrees.set_degrees(np.full(degrees.num_owned_cells, 3, dtype=np.int32))
print_header()
coarsened = solve_adaptive(
approach, max_iterations=8, tol=1e-3, coarsen_theta=0.05, callback=print_iteration
)
it dofs error eta update solve estim. values, degrees
0 25920 7.580e-02 4.979e-01 0.016 0.881 0.011 L2=0.001308481596, H1=0.07580353867, {3: 1296}
1 26085 6.145e-02 3.809e-01 0.015 0.910 0.011 L2=0.000980607581, H1=0.06144651551, {3: 1285, 4: 11}
2 26715 3.996e-02 2.486e-01 0.016 0.963 0.011 L2=0.0005860452937, H1=0.03995837725, {3: 1243, 4: 53}
3 27435 2.961e-02 1.840e-01 0.015 1.081 0.012 L2=0.0004104437104, H1=0.02960808764, {3: 1195, 4: 101}
4 28155 2.441e-02 1.550e-01 0.015 1.136 0.012 L2=0.0003171538495, H1=0.02441047747, {3: 1147, 4: 149}
5 29280 2.233e-02 1.426e-01 0.017 1.339 0.012 L2=0.0002870237819, H1=0.02233332026, {3: 1072, 4: 224}
6 30210 2.174e-02 1.391e-01 0.015 1.486 0.012 L2=0.0002776524506, H1=0.02174250904, {3: 1010, 4: 286}
7 31065 2.160e-02 1.381e-01 0.015 1.677 0.012 L2=0.0002756761934, H1=0.02159695232, {3: 953, 4: 343}
it dofs error eta update solve estim. values, degrees
0 25920 7.580e-02 4.979e-01 0.016 0.881 0.011 L2=0.001308481596, H1=0.07580353867, {3: 1296}
1 14955 6.587e-02 4.086e-01 0.015 0.273 0.005 L2=0.001071794054, H1=0.06586818274, {2: 1113, 3: 172, 4: 11}
2 10095 4.653e-02 2.923e-01 0.015 0.173 0.005 L2=0.0007276657753, H1=0.04652723953, {1: 915, 2: 198, 3: 130, 4: 53}
3 11205 3.528e-02 2.188e-01 0.016 0.273 0.005 L2=0.0005458549591, H1=0.0352839479, {1: 915, 2: 180, 3: 86, 4: 115}
4 12605 2.674e-02 1.704e-01 0.016 0.351 0.006 L2=0.0003865440273, H1=0.02674456607, {1: 915, 2: 124, 3: 86, 4: 171}
5 14297 2.304e-02 1.479e-01 0.016 0.468 0.007 L2=0.0003086278318, H1=0.02303549091, {1: 898, 2: 75, 3: 90, 4: 233}
6 15809 2.209e-02 1.419e-01 0.017 0.552 0.007 L2=0.0002859362986, H1=0.02208533039, {1: 861, 2: 61, 3: 89, 4: 285}
7 14695 2.255e-02 1.457e-01 0.016 0.498 0.007 L2=0.0002941493298, H1=0.02254809876, {1: 885, 2: 71, 3: 97, 4: 243}
We plot the error and the number of degrees of freedom at each iteration of the two runs.
if mesh.comm.rank == 0:
plot_history(
{"from P3": enriched, "from P3, with coarsening": coarsened},
"poisson_history.png",
ylabel=r"$\Vert\nabla(u_h - u_{ex})\Vert_{L^2(\Omega)}$",
)

Both runs enrich the cells close to the bump, and reach the same error once these cells have the maximal degree, after which the error cannot be reduced further on this mesh. With coarsening, the degree is also lowered away from the bump: the error decreases slightly slower in the first iterations, but with less than half of the degrees of freedom.
Against the number of degrees of freedom (with the iteration number next to each point of the run with coarsening), all adaptive runs need far fewer degrees of freedom than a uniform degree to reach the same error, and coarsening removes the degrees of freedom that the initial degree put away from the bump.
if mesh.comm.rank == 0:
plot_convergence(
{
"from P1": history,
"from P3": enriched,
"from P3, with coarsening": coarsened,
"uniform degree": uniform,
},
"poisson_convergence.png",
ylabel=r"$\Vert\nabla(u_h - u_{ex})\Vert_{L^2(\Omega)}$",
annotate=["from P3, with coarsening"],
)

Douglas N. Arnold, Franco Brezzi, Bernardo Cockburn, and L. Donatella Marini. Unified analysis of discontinuous Galerkin methods for elliptic problems. SIAM Journal on Numerical Analysis, 39(5):1749–1779, 2002. doi:10.1137/S0036142901384162.
Paul Houston, Dominik Schötzau, and Thomas P. Wihler. Energy norm a posteriori error estimation of hp-adaptive discontinuous Galerkin methods for elliptic problems. Mathematical Models and Methods in Applied Sciences, 17(1):33–62, 2007. doi:10.1142/S0218202507001826.
Joachim Nitsche. Über ein Variationsprinzip zur Lösung von Dirichlet-Problemen bei Verwendung von Teilräumen, die keinen Randbedingungen unterworfen sind. Abhandlungen aus dem Mathematischen Seminar der Universität Hamburg, 36(1):9–15, 1971. doi:10.1007/BF02995904.