Discretizations for general shapes¶
The comparison that package A0 of the backlog asked for. It prepares one decision of the owner: how pdemagic solves equations on a domain of any shape (packages A4 and A5), and which libraries that brings.
The page ends in a recommendation, which the owner has taken (decision
D32.6, 2026-11-19). Everything it claims about accuracy and cost was
measured with prototypes that are kept in the repository, in
prototypes/shapes. They are not part of the library.
In short¶
Recommended: spectral elements on quadrilaterals that a program makes from a triangulation.
They are the method of boxes (decision D64), repeated on many pieces that are bent to the shape. A smooth problem on a smooth shape is solved to ten digits with a few thousand unknowns, as pdemagic does elsewhere.
The mesh is no obstacle. Any triangulation becomes a mesh of quadrilaterals by cutting each triangle in three, and the accuracy survives that: this was the open question of the candidate, and it was measured.
Sparse factorizations come from faer, which pdemagic already depends on. No new crate is needed for them. The triangulation needs one new crate, spade, which is small and permissively licensed.
The modes of a shape in the plane can be computed from them, so that the exact propagation in time of boxes carries over.
Integral equations on the boundary need far fewer unknowns still, and treat the outside of a body exactly. They are recommended as the way to couple a shape to free space, and not as what carries shapes: they solve only equations whose Green’s function is known.
Finite elements of low degree and finite differences on a grid that the boundary cuts are not recommended. Both work, and neither reaches the accuracy of the rest of pdemagic at a reasonable size.
The candidates¶
The roadmap named three. The fourth was added because pdemagic already uses it, for bodies (decision D40).
Candidate |
Where the unknowns are |
What it needs |
|---|---|---|
Finite elements on triangles, of degree 1, 2, 4 and 6 |
At the points of a triangle mesh that follows the boundary |
A triangulation, and sparse factorizations |
Spectral elements on quadrilaterals, of degree 4 to 20 |
At Gauss–Lobatto points in a few large quadrilaterals with curved sides |
A cut of the domain into quadrilaterals, and sparse factorizations |
Embedded boundary: finite differences of second order |
On a Cartesian grid that does not follow the boundary. Beside the boundary the formula uses the value where the grid line crosses it |
Nothing but the grid, and sparse factorizations |
Integral equation on the boundary |
On the boundary alone. The source is treated on a grid by the Fourier method that exists (decision D16) |
The Green’s function of the equation, and dense matrices |
Finite elements of high degree on triangles and spectral elements on quadrilaterals are close relatives. They differ in the shape of the pieces, and that decides how a mesh is made and how much each unknown is worth.
The test problems¶
The three that the backlog named, each with a solution that is known exactly. The unknown is given on the whole boundary.
Problem |
Equation |
Exact solution |
|---|---|---|
L-shape: the square \((-1,1)^2\) without one quadrant |
\(-\nabla^2 u = f\) |
Smooth: \(\cos(2x + 0.5)\,e^y\). With the corner: the same plus \(r^{2/3}\sin(2\theta/3)\), whose gradient is infinite at the corner that points inwards |
Disk with a hole: the unit disk without a disk of radius 0.25 around \((0.4, 0.2)\) |
\(-\nabla^2 u = f\) |
\(\sin(3x + 0.7)\cos(2y - 0.3)\) |
Outside an ellipse with half axes 1 and 0.5 |
\(\nabla^2 u + k^2 u = 0\) with \(k = 8\), and only outgoing waves |
The wave of two point sources inside the ellipse |
The L-shape is solved twice because its two solutions ask different things. The smooth one asks what a method does with a corner in the boundary. The other asks what it does with the singularity that such a corner causes in almost every real problem.
The measure of error is the same for all: the largest error at the points of the method that are at least 0.1 away from the boundary, relative to the largest value of the solution. For the wave the points also lie within a radius of 2. The cost is the number of unknowns of the linear system, the number of its entries, and the time of its solution.
What was measured¶
Every line is one method, refined from left to right.
The tables say how many unknowns each method took to reach an error of
\(10^{-3}\), \(10^{-6}\) and \(10^{-9}\), and the best it reached. A dash means
that it did not reach that error at any size that was tried. All numbers
are in prototypes/shapes/results.json.
L-shape, smooth solution
Method |
1e-3 |
1e-6 |
1e-9 |
Best error |
with unknowns |
|---|---|---|---|---|---|
Embedded boundary |
705 |
- |
- |
2e-06 |
195 585 |
Embedded boundary, domain turned |
765 |
- |
- |
1e-06 |
196 605 |
Finite elements, degree 1 |
161 |
- |
- |
1e-05 |
12 033 |
Finite elements, degree 2 |
705 |
2 945 |
- |
2e-09 |
48 641 |
Finite elements, degree 4 |
161 |
705 |
12 033 |
9e-10 |
12 033 |
Spectral elements |
33 |
85 |
161 |
4e-15 |
385 |
Integral equation |
1 408 |
3 328 |
6 784 |
7e-11 |
6 784 |
L-shape, with the singularity of the corner
Method |
1e-3 |
1e-6 |
1e-9 |
Best error |
with unknowns |
|---|---|---|---|---|---|
Embedded boundary |
2 945 |
- |
- |
7e-05 |
195 585 |
Embedded boundary, domain turned |
3 069 |
- |
- |
7e-05 |
196 605 |
Finite elements, degree 1 |
2 945 |
- |
- |
4e-04 |
12 033 |
Finite elements, degree 2 |
2 945 |
- |
- |
8e-05 |
48 641 |
Finite elements, degree 4 |
705 |
- |
- |
1e-04 |
12 033 |
Spectral elements |
161 |
- |
- |
3e-05 |
1 633 |
Spectral elements, mesh graded to the corner |
385 |
9 185 |
- |
7e-08 |
23 941 |
Integral equation |
1 408 |
3 328 |
6 784 |
9e-11 |
6 784 |
Disk with a hole
Method |
1e-3 |
1e-6 |
1e-9 |
Best error |
with unknowns |
|---|---|---|---|---|---|
Embedded boundary |
3 003 |
- |
- |
6e-06 |
192 906 |
Finite elements, degree 1 |
1 235 |
- |
- |
1e-05 |
21 235 |
Finite elements, degree 2 |
1 171 |
20 982 |
- |
5e-09 |
85 569 |
Finite elements, degree 4 |
1 018 |
4 842 |
84 556 |
6e-11 |
84 556 |
Finite elements, degree 6 |
540 |
11 013 |
- |
3e-08 |
11 013 |
Spectral elements |
528 |
960 |
2 208 |
3e-14 |
3 968 |
Spectral elements, mesh from a triangulation |
1 116 |
2 574 |
4 632 |
1e-13 |
7 290 |
Integral equation |
25 |
41 |
61 |
8e-15 |
121 |
Helmholtz outside an ellipse
Method |
1e-3 |
1e-6 |
1e-9 |
Best error |
with unknowns |
|---|---|---|---|---|---|
Embedded boundary |
643 388 |
- |
- |
9e-04 |
643 388 |
Finite elements, degree 1 |
- |
- |
- |
1e-02 |
20 205 |
Finite elements, degree 2 |
19 954 |
- |
- |
1e-04 |
80 511 |
Finite elements, degree 4 |
19 564 |
- |
- |
2e-06 |
79 508 |
Spectral elements |
1 536 |
3 456 |
6 144 |
8e-14 |
9 600 |
Integral equation |
32 |
64 |
96 |
1e-14 |
128 |
The dashed lines of the figure are spectral elements on meshes that were not made by hand:
From a triangulation, on the disk with a hole. A coarse triangle mesh of 25 triangles is turned into 75 quadrilaterals, three from each triangle. On the other lines of spectral elements the domain was cut by hand, into 16 pieces between the two circles.
Graded to the corner, on the L-shape. Each of its three squares is cut again and again towards the corner, each cut at 0.15 of the one before, and the degree is raised with the number of cuts.
What the numbers say¶
Second order is not enough. The embedded boundary reaches \(6 \cdot 10^{-6}\) on the disk with 193 000 unknowns, and \(10^{-3}\) for the wave with 643 000. It converges exactly as theory says, at order 2, also where the boundary cuts through the cells. It never comes near the digits that pdemagic gives elsewhere.
The degree buys more than the mesh. For an error of \(10^{-9}\) on the disk, finite elements of degree 2 do not get there with 86 000 unknowns, degree 4 takes 85 000, and spectral elements of degree 12 take 2 200.
Quadrilaterals are worth more than triangles. At the same degree 6, on meshes made by a program in both cases, quadrilaterals reach \(7 \cdot 10^{-8}\) with 2 600 unknowns and triangles \(3 \cdot 10^{-8}\) with 11 000. A quadrilateral holds all products of powers up to the degree in each direction, which a triangle does not.
The mesh of quadrilaterals can be made by a program. Cutting each triangle of a triangulation in three keeps the exponential convergence: \(10^{-10}\) with 4 600 unknowns and \(10^{-13}\) with 7 300. It costs about twice the unknowns of the cut by hand, because the pieces are smaller and less regular.
A corner that points inwards stops every method on a uniform mesh. With the singularity, finite elements of every degree, spectral elements and the embedded boundary all converge at the order \(4/3\), and none gets below \(10^{-5}\). A mesh that is graded to the corner restores fast convergence: \(7 \cdot 10^{-8}\) with 24 000 unknowns. This was measured for spectral elements. It holds for finite elements as well.
Integral equations need the fewest unknowns by far. 81 unknowns on the two circles give \(10^{-12}\) on the disk, and 128 on the ellipse give \(10^{-14}\) for the wave, with the radiation condition exact and no artificial boundary. On the L-shape they are the only method that reaches \(10^{-10}\) with the singularity, since they only have to resolve the boundary near the corner. There they need 6 800 unknowns and a dense matrix, because the charge on the boundary is singular at every corner, also for a smooth solution.
The outside costs the mesh methods a second boundary. Finite and spectral elements were given the exact condition for outgoing waves on a circle around the ellipse, which couples all unknowns on it. The embedded boundary was given a layer that absorbs waves. The layer more than doubles its unknowns.
What the numbers do not show¶
What was measured is the accuracy for one kind of equation. What decides is also what else each candidate can carry. The entries below that were not measured are what the literature of each method says, and are marked.
Finite elements |
Spectral elements |
Embedded boundary |
Integral equation |
|
|---|---|---|---|---|
Accuracy on smooth problems |
Order of the degree plus one |
Exponential |
Order 2 |
Exponential on smooth curves |
Mesh |
A triangulation |
Quadrilaterals, made from a triangulation |
None |
Points on the boundary |
Corners with singular solutions |
A mesh graded to the corner (not measured) |
A mesh graded to the corner |
Order 4/3 |
Points graded to the corner |
Coefficients that vary, nonlinear terms |
Yes (not measured) |
Yes: values at the points of the elements (not measured) |
Yes (not measured) |
Only by an iteration around it, as decision D71 does in free space |
Equations without a known Green’s function |
Yes |
Yes |
Yes |
No |
Conditions on the derivative |
Natural: they are part of the weak form (not measured) |
Natural (not measured) |
Awkward: the normal is not along the grid (not measured) |
Another layer on that part (not measured) |
The outside of a body, radiation |
A second boundary with an exact or absorbing condition |
The same |
An absorbing layer |
Exact |
Modes, and exact propagation in time |
Only for small problems |
In the plane: yes, measured |
No |
No |
Stepping in time |
Yes (not measured) |
Yes. The mass matrix is diagonal (not measured) |
Yes (not measured) |
No |
Three dimensions |
Tetrahedra |
Hexahedra, four from each tetrahedron (not measured) |
The same grid |
Surfaces, as bodies have today, at second order |
How the planner would judge the resolution |
An estimate of the error from two meshes, which decision D33 put off |
The decay of the coefficients in each element: the spectrum that it reads today |
As finite elements |
The spectrum of the charge along the boundary |
How each fits the pipeline¶
The problem description needs the same of all candidates: a domain that is bounded by curves, and a condition on a named part of a curve. That is the decision that decisions D63 and D77 left open for curves, and it does not depend on the discretization. It was taken with milestone 33 (decisions D33.2 and D33.3).
The plan holds the method, and for element methods the size of the mesh and the degree. Both are numbers that a user can read and change, like the shape of a grid today.
The methods that exist. A spectral element is a tensor product of points
along two axes, with a rule for the points of each axis. That is what a
Grid is (decision D30), and a box with boundary conditions (decision D64)
is the case of one element that is not bent. The derivative along an axis,
the elimination of values on faces, and the planner that solves on trial
grids all have their counterpart. Finite elements on triangles share none
of this.
Modes. On boxes and round domains, problems in time are propagated exactly in the modes of the domain (decision D65). The modes of the disk with a hole were computed from spectral elements as one dense symmetric eigenproblem:
Degree |
Unknowns |
Seconds |
Error of the lowest 40 eigenvalues |
|---|---|---|---|
8 |
960 |
0.05 |
\(4 \cdot 10^{-6}\) |
10 |
1 520 |
0.22 |
\(1 \cdot 10^{-8}\) |
12 |
2 208 |
0.48 |
\(3 \cdot 10^{-10}\) |
16 |
3 968 |
1.87 |
\(1 \cdot 10^{-11}\) |
So in the plane a shape can offer what a box offers, behind the same
interface (collocation::Modes), and heat, waves and the Schrödinger
equation on a shape would be exact in time. This holds while the unknowns
number a few thousand, which is what spectral elements need there. It does
not carry to three dimensions, where time has to be stepped.
Milestone 34 built this. It found that smooth data need no more points than that, that a narrow pulse needs ten times as many (package C17), and that in the library 3 500 points take 7 s on one core.
What each would bring into the repository¶
Sparse factorizations: faer, which is there. Decision D4 asks for a
standard library. faer (decision D39) has sparse Cholesky, LU and QR
factorizations with fill-reducing orderings behind its feature
sparse-linalg, which adds no crate. A small Rust program,
prototypes/shapes/sparse, factorized three systems of the finite element
prototype with it, and scipy solved the same systems with SuperLU:
System |
Unknowns |
Entries |
faer: factorize and solve |
SuperLU through scipy |
|---|---|---|---|---|
Poisson on the disk with a hole, degree 2 |
85 569 |
975 815 |
0.30 s |
1.62 s |
The same, degree 4 |
84 556 |
1 962 248 |
0.33 s |
1.85 s |
Helmholtz outside the ellipse, degree 2: complex, with the dense block of the outer circle |
80 511 |
1 927 529 |
1.89 s |
7.91 s |
The times are for four cores of an Intel Xeon at 2.1 GHz. On one thread faer takes 0.22, 0.18 and 2.54 seconds: the first two are no slower, since systems of this size are too small for threads to pay.
The two programs do not do the same work: faer was asked for a Cholesky factorization where the matrix allows it, and scipy always computes an LU factorization. The numbers say that faer is fast enough, and not by how much it is faster. pdemagic builds faer without its threads (decision D69) and clears the vector registers after its calls (decision D68). Whether the sparse routines need the same care is for the milestone that uses them to find out. Milestone 33 clears the registers after them too, and did not measure whether they need it (decision D33.6).
A triangulation: spade. For finite elements, and for spectral elements
by way of triangles. The crate spade (MIT or Apache-2.0) offers
triangulations that keep given edges and refines them to bounds on angles
and areas. It brings six further small crates, all permissively licensed
(hashbrown, smallvec, robust, allocator-api2, equivalent,
foldhash). It was not used in the prototypes, which triangulate with
scipy. Whether its meshes are good
enough is for the first milestone of A4 to confirm. Milestone 33
confirmed that they serve.
Nothing for the embedded boundary and for integral equations in the plane. A fast method for integral equations with many unknowns is package C5.
The recommendation¶
Spectral elements on quadrilaterals, with the mesh made from a triangulation, and the degree an entry of the plan.
They keep the promise of the project. Ten digits on a modest number of unknowns, with an error that the planner can read off a spectrum. The two candidates of low order cannot.
They are general. Any linear or nonlinear equation, coefficients that vary, systems, conditions on values and on derivatives, stationary and in time. Integral equations are not: they stop at equations with a known Green’s function.
They continue what is there. The method of boxes becomes the method of one element, and the modes of a shape in the plane open the propagation in time that exists.
Their weak point was the mesh, and it held. No program for quadrilateral meshes is needed, only a triangulation.
They include finite elements. At degree 1 or 2 the same code is the classic method on quadrilaterals. It is there for the problem whose data are too rough for a high degree to pay.
They cost one new dependency, for the triangulation.
Beside them, integral equations for the outside. Where a shape is surrounded by free space, the condition there should be exact, as the owner decided for bodies (decision D39). The prototypes show how well that works in the plane. This is not part of A4, which is about bounded domains. It belongs to A5 and to scattering (J4, H11).
What speaks against the recommendation:
A mesh of large curved elements can fold. A piece beside a strongly curved boundary must be small enough for its map to stay one-to-one. The prototype stops when an element folds. An implementation has to refine the triangulation there.
Sharp corners need a graded mesh, and the mesher has to be told where: at every corner of the boundary that points inwards.
Three dimensions are harder. Four hexahedra from each tetrahedron are of poorer shape than three quadrilaterals from a triangle, and direct factorizations grow too fast there. A5 will need an iterative solver with a preconditioner. That is true of finite elements as well.
Many small pieces, a domain with fine detail everywhere, favour a low degree, where spectral elements have no advantage left over finite elements. They are then no worse either.
What the owner decided¶
The owner took all four proposals (decision D32.6).
Question |
Decision |
|---|---|
How general shapes are discretized |
Spectral elements on quadrilaterals, made from a triangulation. The degree is an entry of the plan |
Which library supplies sparse factorizations |
faer, with its feature |
Which library triangulates |
spade, as a new dependency, to be confirmed by the first milestone that uses it. Milestone 33 confirmed it |
What treats the outside of a shape |
An integral equation on the boundary, later, with A5. No truncated outside with an approximate condition, as in decision D39 |
A4 in milestones¶
A proposal for the cut that the backlog asks for, now that the discretization is chosen. The session that takes A4 refines it.
The cut was made differently: a first solve for \(a\,\nabla^2 u + c\,u = f\) (milestone 33), propagation in time (34), any linear elliptic equation of second order (35), nonlinear stationary equations (36), stepping in time (38), and a mesh graded to corners (40). The backlog says what is left.
Part |
Content |
|---|---|
A4.1 Shapes and a first solve |
A domain bounded by curves in the problem description, and conditions on named parts of them. The mesh: a triangulation, quadrilaterals from it, and their bending to the boundary. Linear stationary equations of second order with constant coefficients, with values or derivatives given. The planner chooses the mesh and the degree by solving on trial meshes |
A4.2 Time |
The modes of a shape, and with them the propagation and the stepping that boxes have |
A4.3 Corners and media |
A mesh graded to the corners that need it. Coefficients that vary and nonlinear stationary equations, which are packages A8 and A9 on boxes |
Limits of this comparison¶
One kind of condition. The unknown was given on the whole boundary. Conditions on the derivative were not measured.
Two dimensions. Nothing was measured in space.
Prototypes in Python. The times are those of the solution of the linear system. Building the matrices was not timed: in Python it says nothing about Rust.
A simple mesher. Points on the boundary, a hexagonal lattice inside, and a Delaunay triangulation. The meshes are fair and not good, which is to the disadvantage of the element methods.
Finite elements up to degree 6. The prototype uses equally spaced points and powers as its basis, which loses digits above that.
The error is measured away from the boundary. For integral equations the values close to the boundary need a quadrature of their own, which the prototype does not have. For the other methods the error at the boundary is no larger than inside.
Sources as formulas. The integral equation treats the source on a grid that reaches beyond the domain, where the formula of the source is continued and then faded out. A source that is known only inside the domain would have to be continued smoothly first.
Running it again¶
uv run prototypes/shapes/run.py
runs every prototype on every problem and takes a few minutes. It asserts
each order of convergence that this page claims, and fails if one no
longer holds. prototypes/shapes/README.md says how the figures and the
timings of faer are made.