MATLAB demo of FFT-based
Authors: Alex Barnett and Dan Fortunato (CCM, Flatiron Institute), March 2023.
Letting
The solver spectralfft2d
in this repo answers in the affirmative the question of Fortunato-Townsend as to the existence of an optimal-complexity spectral method for the above BVP,
and in a simpler alternative fashion to their proposal [1].
However, in cases where the solution
has singularities on the domain boundary---rendering both solvers
merely algebraically convergent---the proposal of [1]
converges at least twice as fast, due to its node clustering at edges.
And if the solution is analytic in a larger domain
(something unusual in physical problems, although it may occur if the box is a subdomain in a solver for a larger domain) the proposal of [1]
may be spectrally convergent while the FFT solver merely algebraic.
We make the accuracy and speed comparisons below.
In MATLAB/Octave, run spectralfft2d
without arguments, which performs a set of demo and convergence tests and produces the below figure outputs. This function is documented at the bottom of this document.
To perform the comparison against the solver of [1], install a recent version
of chebfun, add chebfun
to your
MATLAB/Octave path, then run fft_vs_cheb
. See below for discussion of output.
The method is simply solution by the 2D fast Fourier sine tranform
given the samples
The boundary values of
- First we test an analytic single-wavenumber forcing
$f(x,y) = (\sin \pi x)(\sin 3\pi y)$ for which the solution is$u = [\pi^2 + (3\pi)^2]^{-1}f$ . We see instant convergence to relative error around$\epsilon_{mach}$ whenever$n \ge 3$ . Here is the unfolded$\tilde f$ , 2D Fourier coefficients, unfolded$\tilde u$ , then$u$ on$\Omega$ , for the somewhat arbitrary$n=17$ :
- Now we test
$f$ a Gaussian bump sufficiently narrow that its$\epsilon$ -support lies within$\Omega$ for$\epsilon \sim 10^{-14}$ . This means the periodization of the function is smooth apart from jumps at the$\epsilon$ level. The observed convergence with$n$ (the maximum frequency) is spectral, possibly even super-exponential:
This is as expected since (at least down to a level
- Finally we test
$f\equiv 1$ , which unfolds under reflection to the checkerboard$-1$ in$[1,2]\times[0,1]$ and$[0,1]\times[1,2]$ , and$+1$ again in$[1,2]\times[1,2]$ . Thus each edge has a discontinuity in$f$ , to the PDE gives that$u$ is$C^1$ but not$C^2$ there. Naively (thinking in 1D) one might expect that$f_{ml} = {\cal O}(1/k_{ml})$ so that$u_{ml} = {\cal O}(1/k_{ml}^3)$ , and summing the truncated (1D) tail of such coefficients from$n$ to$\infty$ one might get an error${\cal O}(1/n^2)$ . Indeed this algebraic rate is observed:
The solution for this last case is that of Poiseuille flow in a pipe
of square cross-section, or the torsion problem in elasticity for a
bar of square cross-section (eg see [2]). The usual series solution
[3, eqn (2.47)] uses a Fourier series in, say,
This indicates
so that by the PDE,
which has
Convergence rate.
We start with the question:
How much better is the algebraic rate of the Chebyshev grid method of Fortunato-Townsend than the
To test this idea we run fft_vs_cheb
, and look at the first plot (labeled "One"):
Indeed, in the
We now turn to the other three RHS tests in the above.
In all cases note that for the FFT solver the two norms appear
very similar (equivalent).
For the 2nd ("Random") plot, the RHS
The 3rd ("Compactly supported") plot shows the same numerically
localized Gaussian
The 4th ("Smooth solution") tests
Speed. A crude timing comparison at spectralfft2d
vs 0.019 sec for chebfun2.poisson
.
The FFT solver is thus about 13 times faster. Since the Chebyshev
solver is iterative, this factor varies with the form of the solution.
At the much larger grid size
-
Understand if the improved
$L^2$ vs$L^\infty$ error norm in the Chebyshev case for$f\equiv1$ is due to corners or edges. The hint that it is due to a corner singularity comes from the observation that in the analogous 1D BVP the solution$u(x) = x(1-x)/2$ should have spectral accuracy with Chebyshev but only$1/n^2$ accuracy with sine-reflection FFT. -
Understand the case for inhomogeneous boundary data
$u = g$ on$\partial\Omega$ , which occurs in the HPS and other settings.
% SPECTRALFFT2D Poisson solver for Dirichlet on square via spectral FFT
%
% u = spectralfft2d(rhsfun,n) returns u values on the (n+1)^2 grid defined
% below, given handle to rhsfun for the right-hand side of form
% f = rhsfun(x,y), on unit square domain D = [0,1]^2, with homogeneous
% Dirichlet BCs. u solves the Poisson equation -Delta u = f with these BCs.
%
% The returned 2D grid definition is (i/n,j/n) for i,j=0,..,n. The solution
% is zero at 1st or last point in either dimension, as per BCs.
%
% [u info] = spectralfft2d(f,n) also returns a debug struct info giving:
% info.f - rhsfun on 2n*2n expanded grid. Note: x fast, y slow.
% info.u - u on 2n*2n expanded grid.
% info.fhat - 2D DFT of f
% info.kg - 1D k-grid
%
% Notes:
% 1) this answers the question in Fortunato-Townsend 2020 IMAJNA about
% existence of a fast spectrally-accurate
% solver for the Dirichlet square, in a simpler way (see 1st, 2nd tests).
% 2) Convergence should deteriorate to merely algebraic
% when f does not reflect to [0,2]^2 as a smooth function (see 3rd test).
[1]. D. Fortunato and A. Townsend, "Fast Poisson solvers for spectral methods", IMA J. Numer. Anal. 40, 1994--2018 (2020).
[2]. M. Bazant, "Exact solutions and physical analogies for unidirectional flows", Phys. Rev. Fluids 1, 024001 (2016).
[3]. H. Bruus, Theoretical Microfluidics (Oxford University Press, Oxford, 2008).
We thank Manas Rachh for a useful discussion.