Boxes with boundary conditions

This page explains how pdemagic solves

\[\sum_i \left(a_i\,\frac{\partial^2 u}{\partial x_i^2} + b_i\,\frac{\partial u}{\partial x_i}\right) + c\,u = f\]

on a box, with a condition \(\alpha\,u + \beta\,\partial u/\partial x_i = g\) on each face. Along some of its axes the box may be periodic in place of having faces. Disks, rings, balls, spherical shells and cylinders are solved in the same way, and have a page of their own.

Further pages build on this one: coefficients that vary, a mixed derivative and nonlinear equations, the plate equation, and the modes of a box or a round domain, which an eigenproblem asks for.

Why not Fourier

The methods for free space and periodic boxes expand in Fourier waves. Waves are periodic, and a function on an interval is not: its periodic continuation jumps at the ends, and the expansion converges slowly. Polynomials do not have this defect. A smooth function on an interval is approximated by polynomials with an error that falls faster than any power of their degree, whatever it does at the ends.

Collocation at Chebyshev points

Along an axis with faces the unknown is represented by its values at the \(n\) Chebyshev points

\[x_j = \frac{x_\text{lower} + x_\text{upper}}{2} - \frac{x_\text{upper} - x_\text{lower}}{2} \cos\frac{\pi j}{n - 1}, \qquad j = 0, \dots, n - 1,\]

which stand for the polynomial of degree \(n - 1\) through them. The points include both ends and crowd towards them, which is what keeps polynomials of high degree well behaved. The derivative of the polynomial at the points is a matrix \(D\) times its values.

The equation is asked to hold at the \(n - 2\) inner points, and the two conditions at the two ends. This is called collocation.

The conditions give the end values

The two conditions are two linear equations that contain the two end values. Solved for them, they give the end values in terms of the inner values and of what is given on the faces, \(g\). Putting that into the operator \(a D^2 + b D\) at the inner points leaves

\[A\,u_\text{inner} + C\,g ,\]

with a matrix \(A\) of \(n - 2\) rows and columns that knows the conditions, and a matrix \(C\) that says what the given values contribute. Every kind of condition is treated this way: values, derivatives, and mixes.

Modes

Let \(A = V \Lambda V^{-1}\) be the eigendecomposition of that matrix. Its eigenvectors are the modes of the axis with its conditions: for a string that is held at both ends they are the sines, to the accuracy of the grid. In the basis of the modes the operator along the axis is a multiplication by the eigenvalue.

In several dimensions the operator is a sum of such operators, one per axis, and each acts along its own axis only. In the basis of the modes of all axes it is therefore diagonal:

\[\Big(\lambda^{(1)}_{k_1} + \lambda^{(2)}_{k_2} + \dots + c\Big)\, \hat u_{k_1 k_2 \dots} = \hat r_{k_1 k_2 \dots} ,\]

where \(r\) is the source at the inner points less what the given values contribute. The solve is a transform to the modes, one division per mode, and a transform back. Along a periodic axis the modes are Fourier waves and the transform is an FFT. Along an axis with faces it is a multiplication of every grid line by \(V^{-1}\) or \(V\).

Last, the values on the faces follow from the conditions.

A mixed derivative couples the axes: with it the operator is no longer a sum of operators along single axes, and the modes do not hold it. In a stationary equation it is iterated on, with the coefficients that vary. In time it is refused.

Accuracy

The method is spectral. For Laplace’s equation on the unit square with \(u = \sin \pi x\) on one face and zero on the others:

Points per axis

Largest error

6

\(1.7 \cdot 10^{-4}\)

8

\(2.2 \cdot 10^{-6}\)

10

\(1.4 \cdot 10^{-8}\)

12

\(6.2 \cdot 10^{-11}\)

16

\(6.9 \cdot 10^{-15}\)

Two more points take off two digits.

Corners

That rate needs a solution that is smooth up to the faces, which is less common than it sounds. \(-\nabla^2 u = 1\) with \(u = 0\) on the faces of a square is as simple as a problem gets, and its solution has a weak singularity in each corner: the conditions on the two faces that meet there do not agree with the equation. The error then falls like a power of the number of points:

Points per axis

Error at the centre

Spectrum of the solution falls to

9

\(1.4 \cdot 10^{-8}\)

\(3.5 \cdot 10^{-3}\)

17

\(3.3 \cdot 10^{-11}\)

\(7.8 \cdot 10^{-5}\)

33

\(1.5 \cdot 10^{-12}\)

\(2.0 \cdot 10^{-6}\)

65

\(1.5 \cdot 10^{-12}\)

\(3.2 \cdot 10^{-8}\)

Away from the corners the solution is accurate early. The spectrum measures the worst place.

How the grid is chosen

The solve is direct and cheap, so the planner solves on one candidate grid after the other, with 16, 24, 32, 48 and more points along the longest side. It takes the first one on which the coefficients of the solution, and of everything that is given, have fallen to \(10^{-10}\) of the largest at high frequencies.

For a solution like the one above that would take very fine grids and gain little. The search therefore also ends at a candidate with at least 64 points that resolves the solution to \(10^{-6}\) and has gained less than a factor of 30 over the one before. The plan says which of the two happened, and the diagnostics spectral_tail (what is given) and solution_tail (the solution) hold the numbers.

A linear problem in time is planned in the same way: the planner solves it on each candidate, with three recorded times, and looks at the solution at the end of the time span. A problem with terms that are stepped is not solved by the planner. Its grid resolves what is given, with at least 32 points along the longest side.

A constant that is left open

If every face gives the derivative alone and the equation has no term with the unknown itself, constants solve the equation with nothing given. One of the modes then has the eigenvalue zero. Its coefficient cannot be divided, and need not be: the solution is determined up to a constant, and pdemagic returns the one whose mean is zero.

A solution exists only if the right-hand side has no part in that mode, which says that the source balances what flows through the faces. The part is dropped, which takes a constant off the source, and that constant is reported as removed_source_mean. It is the same rule as on a periodic box.

An eigenvalue zero in any other case means that the problem has no unique solution: the box resonates. That is an error.

In time

For an equation in time, \(\partial u/\partial t = L u + f\) with the same operator \(L\) in space, the elimination of the end values leaves a system of ordinary differential equations for the inner values:

\[\frac{d u_\text{inner}}{dt} = A\,u_\text{inner} + C\,g(t) + f(t) .\]

What is given on the faces acts as one more source. In the basis of the modes this is one equation per mode, \(\hat u' = \lambda\,\hat u + \hat s(t)\), which is solved exactly, as each Fourier wave is on a periodic box. The same code does both: it needs a basis in which the operator is diagonal, and nothing else. Equations of second order in time, waves, are propagated in the same way, and terms that are nonlinear or have coefficients that vary are stepped, with their derivatives taken on the grid.

So there is no limit on the time step from the grid. That matters more here than on a periodic box: the largest eigenvalue of the second derivative at \(n\) Chebyshev points grows like \(n^4\), and an explicit scheme would need steps that small.

With nothing given that depends on time, a solution is exact in time: heat in a rod is right to \(10^{-12}\) at any time, in one step.

Values on a face that change in time

A source that depends on time is replaced, within a step, by the cubic polynomial through its values at four points in time. On a periodic box the error of that falls with the eighth power of the number of steps, because the points are Gauss points and only the integral of the source counts.

Values on a face are different. They drive the fastest modes hardest, and a fast mode follows what it is given at each instant. What counts is then the polynomial itself, and the error falls with the fourth power: for \(u = e^{-t} \cos x\) on \([0, 1]\), with its value given at one end and its slope at the other, one step over a unit of time errs by \(3 \cdot 10^{-4}\), and two steps by \(1.5 \cdot 10^{-5}\). The planner knows this, and chooses the steps so that the polynomial is right to \(10^{-10}\) at the end of each step, where it is furthest from its points.

Cost

With \(n\) points per axis in \(d\) dimensions, the eigendecompositions cost \(O(n^3)\) each, and the transforms \(O(n^{d+1})\). On one core:

Grid

Time

\(129^2\)

0.09 s

\(32^3\)

0.03 s

\(64^3\)

0.4 s

\(96^3\)

2.1 s

Limits

  • Boundary layers. With a first derivative that dominates the second, the modes of an axis are far from orthogonal, and the transform to them loses digits. pdemagic measures that and refuses a grid beyond a loss of about eight digits. In practice this happens when the solution has a layer thinner than about a thirtieth of the box. The planner then keeps the last grid on which the modes can be used, and its plan says that the solution is not resolved.

  • Coefficients are constant in the conditions. In an equation in time, terms with coefficients that vary are stepped: see nonlinear equations in time for the second order.

  • A shape that is neither a box nor round, and a mixed derivative on a disk, are taken by another method, as a region of the plane: see spectral elements.

  • Conditions hold no derivative in time, which the condition for a wall without reflection would need.

  • Nonlinear terms are not dealiased. On a periodic grid the upper third of the waves of a product is removed. On Chebyshev points it is not: the diagnostic solution_tail tells whether the grid still resolves the solution.

  • Arrays are given at the points of the grid.

Round domains, the iteration for coefficients that vary, the plate equation and the modes have limits of their own: see round domains, iteration between walls, the plate equation and the modes.

Sources

The method of solving in the eigenbasis of each axis is due to Haidvogel and Zang (1979), who used it with Chebyshev polynomials for Poisson’s equation, building on Lynch, Rice and Thomas (1964). It is described in Canuto, Hussaini, Quarteroni and Zang, Spectral Methods: Fundamentals in Single Domains (2006), and Chebyshev differentiation matrices in Trefethen, Spectral Methods in MATLAB (2000). That the eigenvalues of the second derivative at Chebyshev points are real and negative was shown by Gottlieb and Lustman (1983).