Shapes in the plane¶
This page explains how pdemagic solves a linear equation of second order,
on a region of the plane that closed curves bound. The coefficients may vary from place to place. The simplest case is \(a\,\nabla^2 u + c\,u = f\) with constants: Laplace, Poisson, Helmholtz. The equation comes with a condition
on every piece of the curves, where \(n\) is the normal that points out of the region. The method is called spectral elements. The same elements carry linear equations in time: heat, waves, the Schrödinger equation.
Further pages build on this one: shapes with corners, grids and arrays, systems, nonlinear equations and the modes of a shape, which an eigenproblem asks for.
Why elements¶
On a box, pdemagic expands the unknown in polynomials of high degree along each axis, and reaches ten digits with a few dozen points (collocation). A region of any shape is no product of axes. It can be cut into pieces that are, though: quadrilaterals with curved sides, each the image of a square. On every piece the unknown is again a polynomial of high degree in two directions, and the pieces are joined by sharing the values on their common sides.
That keeps what makes the method of boxes good. The error still falls exponentially with the degree, and a box is the case of one piece that is not bent.
The mesh¶
A program can cut any region into triangles, and far less easily into quadrilaterals. pdemagic takes the detour:
Points on the boundary. Every piece of a curve is cut into parts of about the length
spacing, and into enough of them that none turns through more than 45 degrees.Triangles. A Delaunay triangulation that keeps those parts as edges fills the region, and is refined until no triangle is larger than three quarters of
spacingsquared or has an angle below 25 degrees. This is done by the crate spade.Three quadrilaterals from each triangle. The centre of a triangle is joined to the middle of each of its sides.
A side that lies on an arc follows it exactly, and the bend is carried into the quadrilateral by blending: its opposite sides stay straight. The mesh therefore covers the region and nothing else, whatever the degree.
Polynomials at Gauss–Lobatto points¶
On the square \([-1, 1]^2\) that a quadrilateral is the image of, the unknown is a polynomial of degree \(N\) in each direction, given by its values at the \((N+1)^2\) Gauss–Lobatto points: the two ends of the interval and the zeros of the derivative of the Legendre polynomial \(P_N\) between them, in both directions. The points on a side belong to both elements at that side.
The map from the square to the quadrilateral is the polynomial of the same degree through the images of the points.
The weak form¶
The equation is multiplied by a test function \(v\) and integrated over the region. Integrating the second derivatives by parts moves one derivative onto \(v\) and leaves an integral over the boundary. For \(a\,\nabla^2 u + c\,u = f\):
This is asked to hold for every \(v\) that is a polynomial on each element. All integrals are taken by the quadrature of the Gauss–Lobatto points, which makes the matrix of \(\int u\,v\) diagonal.
The conditions enter in two ways:
A value, \(\beta = 0\): the unknown at the points of that piece is \(g/\alpha\), and the equation is not asked for there.
A slope or a mix, \(\beta \neq 0\): the condition gives \(\partial u/\partial n = (g - \alpha u)/\beta\), which is put into the integral over the boundary. Nothing has to be differentiated on the boundary: the condition holds in the mean over each part of it, to the accuracy of the method.
Media, drift and directions¶
The general equation is treated in the same way. With the matrix \(A = \varepsilon\,\mathbf{1} + (a_{ij})\) in front of the second derivatives, the weak form is
Each kind of term has its place in it:
A medium inside a divergence, \(\nabla\cdot(\varepsilon\nabla u)\), is what the first integral holds: the coefficient is needed at the quadrature points, and never differentiated.
A coefficient in front of second derivatives, \(a_{ij}\,\partial_i\partial_j u\), is taken under the first derivative: \(a_{ij}\,\partial_i\partial_j u = \partial_i(a_{ij}\,\partial_j u) - (\partial_i a_{ij})\,\partial_j u\). The second term is a drift, which is why \(\tilde{\mathbf b}\) above is \(\mathbf b\) less the divergence of \((a_{ij})\). The derivative of the coefficient is that of its polynomial on the element.
A drift and a term with the unknown are integrals of the unknown itself against \(v\). The matrix is then no longer symmetric, which the factorization does not need.
On the boundary the weak form leaves the flux \(\mathbf{n}\cdot A\,\nabla u\), while a condition speaks of the slope along the normal. For a matrix \(A\) that is not a multiple of the unit matrix the two differ by a part along the boundary: \(\mathbf{n}\cdot A\,\nabla u = (\mathbf{n}\cdot A\,\mathbf{n})\, \partial u/\partial n + (\mathbf{n}\cdot A\,\mathbf{t})\, \partial u/\partial t\). The first slope comes from the condition, and the second from the values on that side of the element.
A condition always means the derivative along the normal, \(\partial u/\partial n\), also in a medium. The flux through the boundary is \(\varepsilon\,\partial u/\partial n\): divide what is given by \(\varepsilon\).
The equation must be elliptic: \(A\) must be definite at every point, so that the second derivatives act in every direction with the same sign. This is checked at the points of the elements.
Solving¶
The points inside an element belong to it alone. They are eliminated first, element by element, on all cores, by a dense factorization of the size \((N-1)^2\). What is left couples the points on the sides of the elements, which are far fewer. That system is sparse, and is factorized directly, by faer. The points inside then follow from those on the sides.
The assembly is written once, in complex numbers. What it assembles is then solved as what it is. Where the equation and all it is given are real, the blocks of the elements and the sparse matrix are real, and are held and factorized in real numbers. Where the matrix is also symmetric, and of one sign, it is factorized by Cholesky’s method, which keeps one triangle and does no pivoting. A diffusion with a loss, \(\nabla\cdot(\varepsilon\nabla u) - c\,u = f\) with \(c \ge 0\), is of that kind, and so is Poisson’s equation. For the matrix to be symmetric the values that a condition gives are taken to the right-hand side, so that they are no unknowns.
That a matrix is real and symmetric is read off its entries. That it is of one sign shows when Cholesky’s method is tried: a pivot that is not positive ends the attempt, and the general factorization is taken. The attempt is skipped where the equation says that it would fail, as for the Helmholtz equation, whose term without derivatives works against the second derivatives.
Accuracy¶
The method is spectral. For \(-\nabla^2 u = f\) on the unit disk with a hole of radius 0.25 around \((0.4, 0.2)\), with \(u = \sin(3x + 0.7)\cos(2y - 0.3)\) given on both circles, on a mesh of 111 elements:
Degree |
Points |
Largest error |
|---|---|---|
4 |
1 860 |
\(3.3 \cdot 10^{-5}\) |
6 |
4 122 |
\(6.6 \cdot 10^{-8}\) |
8 |
7 272 |
\(6.9 \cdot 10^{-11}\) |
10 |
11 310 |
\(8.0 \cdot 10^{-14}\) |
Two more degrees take off three digits. A finer mesh at a fixed degree gains far less for the same work: at degree 6, halving the spacing of the mesh twice brings the error from \(6.6 \cdot 10^{-8}\) to \(6.1 \cdot 10^{-11}\) with sixteen times the elements.
The slope along the normal, given on the edge of the hole in place of the value, is met to the same accuracy.
So is an equation with coefficients that vary in every term. On the same region and mesh, for \((1+x^2)\,u_{xx} + xy\,u_{xy} + (2+y^2)\,u_{yy} + y\,u_x - (1+x^2)\,u = f\) with the same solution, its value on the rim and its slope on the edge of the hole:
Degree |
Largest error |
|---|---|
4 |
\(2.3 \cdot 10^{-5}\) |
6 |
\(5.6 \cdot 10^{-8}\) |
8 |
\(9.5 \cdot 10^{-11}\) |
10 |
\(9.6 \cdot 10^{-14}\) |
A solution that is not smooth at a corner of the region does not reach that rate by the degree alone. The mesh then shrinks towards the corner: see shapes with corners.
How the mesh and the degree are chosen¶
The solve is direct, so the planner solves on one candidate after the other. It starts with a mesh whose spacing is a quarter of the size of the region, and raises the degree from 4 in steps of 2 up to 16. It takes the first degree at which the Legendre coefficients of the solution, and of the source, have fallen to \(10^{-10}\) of the largest in every element. If no degree does, it halves the spacing, at most twice.
Before that, the mesh is given its layers towards the
corners at which the solution has a singularity.
They decide how small the
coefficients can get: what the innermost layer leaves of the singularity
does not fall with the degree. The search therefore also ends when a
degree of 8 or more has gained less than a factor of 3 over the one
before. The plan says which of the two happened, and the diagnostics
spectral_tail (the source) and solution_tail (the solution) hold the
numbers.
The planner solves with up to 30 000 points on the sides of elements, and with up to 60 000 where the problem is real.
Too coarse a mesh can fail where two parts of the boundary come close: an element that follows one of them folds over. The planner then goes on to the next finer mesh.
A constant that is left open¶
If every piece of the boundary gives the slope alone and the equation has
no term with the unknown itself, with or without a drift, the solution is
determined up to a constant. pdemagic returns the one whose mean is zero. A solution exists
only if the source balances what flows through the boundary: the constant
that it lacks for that is taken off the source and reported as
removed_source_mean, as on a box.
In any other case in which the equation has no unique solution, the region resonates: \(c/a\) is an eigenvalue of the Laplacian with these conditions. That is an error.
Cost¶
For the disk with a hole above, at the degree 10 that the planner chooses (111 elements, 11 310 points), on four cores:
Step |
Time |
|---|---|
The planner, which solves at the degrees 4 to 10 |
0.09 s |
The solve |
0.1 s |
For the L-shape with its 18 layers (828 elements of the degree 12, 119 857 points) the planner takes 0.9 s and the solve 0.4 s.
Time and memory grow a little faster than the number of points on the sides of elements, which the sparse factorization works on. How fast depends on what the matrix is. On the unit disk at the degree 8, for \(-\nabla^2 u = 1\), whose matrix is real and of one sign, for \(\nabla^2 u + 30u = 1\), whose matrix is real, and for \(\nabla^2 u + (30 + i)u = 1\):
Points on the sides of elements |
Real, of one sign |
Real |
Complex |
|---|---|---|---|
16 400 |
0.2 GB, 0.2 s |
0.3 GB, 0.4 s |
0.4 GB, 0.6 s |
24 900 |
0.3 GB, 0.3 s |
0.4 GB, 0.7 s |
0.7 GB, 1.3 s |
49 900 |
0.4 GB, 0.7 s |
0.9 GB, 1.9 s |
1.6 GB, 4.0 s |
99 500 |
0.8 GB, 1.6 s |
2.3 GB, 6.1 s |
|
145 200 |
1.1 GB, 2.4 s |
Before the matrix was factorized as what it is, every problem took what a complex one takes. A plan with more than 80 000 such points is refused, and a real problem may have 160 000. A smooth problem stays far below that: the disk above has 2 319.
Limits¶
Of second order, elliptic. No derivatives above the second order.
Conditions with constant coefficients, on the value and on the slope along the normal.
Lines and arcs. A piece of the boundary is a straight line or an arc of an ellipse whose axes lie along the coordinates.
Parts of the boundary that come very close to each other need a plan with a finer mesh than the planner tries.
Large meshes. The factorization runs on one core. A matrix that is symmetric but of both signs, as that of a Helmholtz equation, is factorized as a general one: one that used its symmetry would take a fraction of the memory.
The plane. Shapes in space are not solved yet.
The pages that build on this one have limits of their own: see corners, grids and arrays, systems, nonlinear equations and problems in time.
Sources¶
Spectral elements are due to Patera (1984). The method is described in Karniadakis and Sherwin, Spectral/hp Element Methods for Computational Fluid Dynamics (2005), and in Deville, Fischer and Mund, High-Order Methods for Incompressible Fluid Flow (2002), which also treats the elimination of the points inside each element. Blending a curved side into an element goes back to Gordon and Hall (1973). The refinement of a Delaunay triangulation to bounds on angles and areas is due to Ruppert (1995). Why this method was chosen for shapes, and what it was compared with, is in the comparison of discretizations.