D03 Chapter Contents
D03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD03FAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

D03FAF solves the Helmholtz equation in Cartesian coordinates in three dimensions using the standard seven-point finite difference approximation. This routine is designed to be particularly efficient on vector processors.

## 2  Specification

 SUBROUTINE D03FAF ( XS, XF, L, LBDCND, BDXS, BDXF, YS, YF, M, MBDCND, BDYS, BDYF, ZS, ZF, N, NBDCND, BDZS, BDZF, LAMBDA, LDF, LDF2, F, PERTRB, W, LWRK, IFAIL)
 INTEGER L, LBDCND, M, MBDCND, N, NBDCND, LDF, LDF2, LWRK, IFAIL REAL (KIND=nag_wp) XS, XF, BDXS(LDF2,N+1), BDXF(LDF2,N+1), YS, YF, BDYS(LDF,N+1), BDYF(LDF,N+1), ZS, ZF, BDZS(LDF,M+1), BDZF(LDF,M+1), LAMBDA, F(LDF,LDF2,N+1), PERTRB, W(LWRK)

## 3  Description

D03FAF solves the three-dimensional Helmholtz equation in Cartesian coordinates:
 $∂2u ∂x2 + ∂2u ∂y2 + ∂2u ∂z2 +λu=fx,y,z.$
This subroutine forms the system of linear equations resulting from the standard seven-point finite difference equations, and then solves the system using a method based on the fast Fourier transform (FFT) described by Swarztrauber (1984). This subroutine is based on the routine HW3CRT from FISHPACK (see Swarztrauber and Sweet (1979)).
More precisely, the routine replaces all the second derivatives by second-order central difference approximations, resulting in a block tridiagonal system of linear equations. The equations are modified to allow for the prescribed boundary conditions. Either the solution or the derivative of the solution may be specified on any of the boundaries, or the solution may be specified to be periodic in any of the three dimensions. By taking the discrete Fourier transform in the $x$- and $y$-directions, the equations are reduced to sets of tridiagonal systems of equations. The Fourier transforms required are computed using the multiple FFT routines found in Chapter C06.

## 4  References

Swarztrauber P N (1984) Fast Poisson solvers Studies in Numerical Analysis (ed G H Golub) Mathematical Association of America
Swarztrauber P N and Sweet R A (1979) Efficient Fortran subprograms for the solution of separable elliptic partial differential equations ACM Trans. Math. Software 5 352–364

## 5  Parameters

1:     XS – REAL (KIND=nag_wp)Input
On entry: the lower bound of the range of $x$, i.e., ${\mathbf{XS}}\le x\le {\mathbf{XF}}$.
Constraint: ${\mathbf{XS}}<{\mathbf{XF}}$.
2:     XF – REAL (KIND=nag_wp)Input
On entry: the upper bound of the range of $x$, i.e., ${\mathbf{XS}}\le x\le {\mathbf{XF}}$.
Constraint: ${\mathbf{XS}}<{\mathbf{XF}}$.
3:     L – INTEGERInput
On entry: the number of panels into which the interval (XS,XF) is subdivided. Hence, there will be ${\mathbf{L}}+1$ grid points in the $x$-direction given by ${x}_{\mathit{i}}={\mathbf{XS}}+\left(\mathit{i}-1\right)×\delta x$, for $\mathit{i}=1,2,\dots ,{\mathbf{L}}+1$, where $\delta x=\left({\mathbf{XF}}-{\mathbf{XS}}\right)/{\mathbf{L}}$ is the panel width.
Constraint: ${\mathbf{L}}\ge 5$.
4:     LBDCND – INTEGERInput
On entry: indicates the type of boundary conditions at $x={\mathbf{XS}}$ and $x={\mathbf{XF}}$.
${\mathbf{LBDCND}}=0$
If the solution is periodic in $x$, i.e., $u\left({\mathbf{XS}},y,z\right)=u\left({\mathbf{XF}},y,z\right)$.
${\mathbf{LBDCND}}=1$
If the solution is specified at $x={\mathbf{XS}}$ and $x={\mathbf{XF}}$.
${\mathbf{LBDCND}}=2$
If the solution is specified at $x={\mathbf{XS}}$ and the derivative of the solution with respect to $x$ is specified at $x={\mathbf{XF}}$.
${\mathbf{LBDCND}}=3$
If the derivative of the solution with respect to $x$ is specified at $x={\mathbf{XS}}$ and $x={\mathbf{XF}}$.
${\mathbf{LBDCND}}=4$
If the derivative of the solution with respect to $x$ is specified at $x={\mathbf{XS}}$ and the solution is specified at $x={\mathbf{XF}}$.
Constraint: $0\le {\mathbf{LBDCND}}\le 4$.
5:     BDXS(LDF2,${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the values of the derivative of the solution with respect to $x$ at $x={\mathbf{XS}}$. When ${\mathbf{LBDCND}}=3$ or $4$, ${\mathbf{BDXS}}\left(\mathit{j},\mathit{k}\right)={u}_{x}\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$ and $\mathit{k}=1,2,\dots ,{\mathbf{N}}+1$.
When LBDCND has any other value, BDXS is not referenced.
6:     BDXF(LDF2,${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the values of the derivative of the solution with respect to $x$ at $x={\mathbf{XF}}$. When ${\mathbf{LBDCND}}=2$ or $3$, ${\mathbf{BDXF}}\left(\mathit{j},\mathit{k}\right)={u}_{x}\left({\mathbf{XF}},{y}_{\mathit{j}},{z}_{\mathit{k}}\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$ and $\mathit{k}=1,2,\dots ,{\mathbf{N}}+1$.
When LBDCND has any other value, BDXF is not referenced.
7:     YS – REAL (KIND=nag_wp)Input
On entry: the lower bound of the range of $y$, i.e., ${\mathbf{YS}}\le y\le {\mathbf{YF}}$.
Constraint: ${\mathbf{YS}}<{\mathbf{YF}}$.
8:     YF – REAL (KIND=nag_wp)Input
On entry: the upper bound of the range of $y$, i.e., ${\mathbf{YS}}\le y\le {\mathbf{YF}}$.
Constraint: ${\mathbf{YS}}<{\mathbf{YF}}$.
9:     M – INTEGERInput
On entry: the number of panels into which the interval (YS,YF) is subdivided. Hence, there will be ${\mathbf{M}}+1$ grid points in the $y$-direction given by ${y}_{\mathit{j}}={\mathbf{YS}}+\left(\mathit{j}-1\right)×\delta y$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$, where $\delta y=\left({\mathbf{YF}}-{\mathbf{YS}}\right)/{\mathbf{M}}$ is the panel width.
Constraint: ${\mathbf{M}}\ge 5$.
10:   MBDCND – INTEGERInput
On entry: indicates the type of boundary conditions at $y={\mathbf{YS}}$ and $y={\mathbf{YF}}$.
${\mathbf{MBDCND}}=0$
If the solution is periodic in $y$, i.e., $u\left(x,{\mathbf{YF}},z\right)=u\left(x,{\mathbf{YS}},z\right)$.
${\mathbf{MBDCND}}=1$
If the solution is specified at $y={\mathbf{YS}}$ and $y={\mathbf{YF}}$.
${\mathbf{MBDCND}}=2$
If the solution is specified at $y={\mathbf{YS}}$ and the derivative of the solution with respect to $y$ is specified at $y={\mathbf{YF}}$.
${\mathbf{MBDCND}}=3$
If the derivative of the solution with respect to $y$ is specified at $y={\mathbf{YS}}$ and $y={\mathbf{YF}}$.
${\mathbf{MBDCND}}=4$
If the derivative of the solution with respect to $y$ is specified at $y={\mathbf{YS}}$ and the solution is specified at $y={\mathbf{YF}}$.
Constraint: $0\le {\mathbf{MBDCND}}\le 4$.
11:   BDYS(LDF,${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the values of the derivative of the solution with respect to $y$ at $y={\mathbf{YS}}$. When ${\mathbf{MBDCND}}=3$ or $4$, ${\mathbf{BDYS}}\left(\mathit{i},\mathit{k}\right)={u}_{y}\left({x}_{\mathit{i}},{\mathbf{YS}},{z}_{\mathit{k}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{L}}+1$ and $\mathit{k}=1,2,\dots ,{\mathbf{N}}+1$.
When MBDCND has any other value, BDYS is not referenced.
12:   BDYF(LDF,${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the values of the derivative of the solution with respect to $y$ at $y={\mathbf{YF}}$. When ${\mathbf{MBDCND}}=2$ or $3$, ${\mathbf{BDYF}}\left(\mathit{i},\mathit{k}\right)={u}_{y}\left({x}_{\mathit{i}},{\mathbf{YF}},{z}_{\mathit{k}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{L}}+1$ and $\mathit{k}=1,2,\dots ,{\mathbf{N}}+1$.
When MBDCND has any other value, BDYF is not referenced.
13:   ZS – REAL (KIND=nag_wp)Input
On entry: the lower bound of the range of $z$, i.e., ${\mathbf{ZS}}\le z\le {\mathbf{ZF}}$.
Constraint: ${\mathbf{ZS}}<{\mathbf{ZF}}$.
14:   ZF – REAL (KIND=nag_wp)Input
On entry: the upper bound of the range of $z$, i.e., ${\mathbf{ZS}}\le z\le {\mathbf{ZF}}$.
Constraint: ${\mathbf{ZS}}<{\mathbf{ZF}}$.
15:   N – INTEGERInput
On entry: the number of panels into which the interval (ZS,ZF) is subdivided. Hence, there will be ${\mathbf{N}}+1$ grid points in the $z$-direction given by ${z}_{\mathit{k}}={\mathbf{ZS}}+\left(\mathit{k}-1\right)×\delta z$, for $\mathit{k}=1,2,\dots ,{\mathbf{N}}+1$, where $\delta z=\left({\mathbf{ZF}}-{\mathbf{ZS}}\right)/{\mathbf{N}}$ is the panel width.
Constraint: ${\mathbf{N}}\ge 5$.
16:   NBDCND – INTEGERInput
On entry: specifies the type of boundary conditions at $z={\mathbf{ZS}}$ and $z={\mathbf{ZF}}$.
${\mathbf{NBDCND}}=0$
if the solution is periodic in $z$, i.e., $u\left(x,y,{\mathbf{ZF}}\right)=u\left(x,y,{\mathbf{ZS}}\right)$.
${\mathbf{NBDCND}}=1$
if the solution is specified at $z={\mathbf{ZS}}$ and $z={\mathbf{ZF}}$.
${\mathbf{NBDCND}}=2$
if the solution is specified at $z={\mathbf{ZS}}$ and the derivative of the solution with respect to $z$ is specified at $z={\mathbf{ZF}}$.
${\mathbf{NBDCND}}=3$
if the derivative of the solution with respect to $z$ is specified at $z={\mathbf{ZS}}$ and $z={\mathbf{ZF}}$.
${\mathbf{NBDCND}}=4$
if the derivative of the solution with respect to $z$ is specified at $z={\mathbf{ZS}}$ and the solution is specified at $z={\mathbf{ZF}}$.
Constraint: $0\le {\mathbf{NBDCND}}\le 4$.
17:   BDZS(LDF,${\mathbf{M}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the values of the derivative of the solution with respect to $z$ at $z={\mathbf{ZS}}$. When ${\mathbf{NBDCND}}=3$ or $4$, ${\mathbf{BDZS}}\left(\mathit{i},\mathit{j}\right)={u}_{z}\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{L}}+1$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$.
When NBDCND has any other value, BDZS is not referenced.
18:   BDZF(LDF,${\mathbf{M}}+1$) – REAL (KIND=nag_wp) arrayInput
On entry: the values of the derivative of the solution with respect to $z$ at $z={\mathbf{ZF}}$. When ${\mathbf{NBDCND}}=2$ or $3$, ${\mathbf{BDZF}}\left(\mathit{i},\mathit{j}\right)={u}_{z}\left({x}_{i},{y}_{j},{\mathbf{ZF}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{L}}+1$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$.
When NBDCND has any other value, BDZF is not referenced.
19:   LAMBDA – REAL (KIND=nag_wp)Input
On entry: the constant $\lambda$ in the Helmholtz equation. For certain positive values of $\lambda$ a solution to the differential equation may not exist, and close to these values the solution of the discretized problem will be extremely ill-conditioned. If $\lambda >0$, then D03FAF will set ${\mathbf{IFAIL}}={\mathbf{3}}$, but will still attempt to find a solution. However, since in general the values of $\lambda$ for which no solution exists cannot be predicted a priori, you are advised to treat any results computed with $\lambda >0$ with great caution.
20:   LDF – INTEGERInput
On entry: the first dimension of the arrays F, BDYS, BDYF, BDZS and BDZF as declared in the (sub)program from which D03FAF is called.
Constraint: ${\mathbf{LDF}}\ge {\mathbf{L}}+1$.
21:   LDF2 – INTEGERInput
On entry: the second dimension of the array F and the first dimension of the arrays BDXS and BDXF as declared in the (sub)program from which D03FAF is called.
Constraint: ${\mathbf{LDF2}}\ge {\mathbf{M}}+1$.
22:   F(LDF,LDF2,${\mathbf{N}}+1$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the values of the right-side of the Helmholtz equation and boundary values (if any).
 $Fi,j,k = fxi,yj,zk i=2,3,…,L, j=2,3,…,M ​ and ​ k=2,3,…,N.$
On the boundaries F is defined by
 LBDCND ${\mathbf{F}}\left(1,j,k\right)$ ${\mathbf{F}}\left({\mathbf{L}}+1,j,k\right)$ 0 $f\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$ $f\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$ 1 $u\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$ $u\left({\mathbf{XF}},{y}_{j},{z}_{k}\right)$ 2 $u\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$ $f\left({\mathbf{XF}},{y}_{j},{z}_{k}\right)$ $j=1,2,\dots ,{\mathbf{M}}+1$ 3 $f\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$ $f\left({\mathbf{XF}},{y}_{j},{z}_{k}\right)$ $k=1,2,\dots ,{\mathbf{N}}+1$ 4 $f\left({\mathbf{XS}},{y}_{j},{z}_{k}\right)$ $u\left({\mathbf{XF}},{y}_{j},{z}_{k}\right)$ MBDCND ${\mathbf{F}}\left(i,1,k\right)$ ${\mathbf{F}}\left(i,{\mathbf{M}}+1,k\right)$ 0 $f\left({x}_{i},{\mathbf{YS}},{z}_{k}\right)$ $f\left({x}_{i},{\mathbf{YS}},{z}_{k}\right)$ 1 $u\left({\mathbf{YS}},{x}_{i},{z}_{k}\right)$ $u\left({\mathbf{YF}},{x}_{i},{z}_{k}\right)$ 2 $u\left({x}_{i},{\mathbf{YS}},{z}_{k}\right)$ $f\left({x}_{i},{\mathbf{YF}},{z}_{k}\right)$ $i=1,2,\dots ,{\mathbf{L}}+1$ 3 $f\left({x}_{i},{\mathbf{YS}},{z}_{k}\right)$ $f\left({x}_{i},{\mathbf{YF}},{z}_{k}\right)$ $k=1,2,\dots ,{\mathbf{N}}+1$ 4 $f\left({x}_{i},{\mathbf{YS}},{z}_{k}\right)$ $u\left({x}_{i},{\mathbf{YF}},{z}_{k}\right)$ NBDCND ${\mathbf{F}}\left(i,j,1\right)$ ${\mathbf{F}}\left(i,j,{\mathbf{N}}+1\right)$ 0 $f\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$ $f\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$ 1 $u\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$ $u\left({x}_{i},{y}_{j},{\mathbf{ZF}}\right)$ 2 $u\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$ $f\left({x}_{i},{y}_{j},{\mathbf{ZF}}\right)$ $i=1,2,\dots ,{\mathbf{L}}+1$ 3 $f\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$ $f\left({x}_{i},{y}_{j},{\mathbf{ZF}}\right)$ $j=1,2,\dots ,{\mathbf{M}}+1$ 4 $f\left({x}_{i},{y}_{j},{\mathbf{ZS}}\right)$ $u\left({x}_{i},{y}_{j},{\mathbf{ZF}}\right)$
Note: if the table calls for both the solution $u$ and the right-hand side $f$ on a boundary, then the solution must be specified.
On exit: contains the solution $u\left(\mathit{i},\mathit{j},\mathit{k}\right)$ of the finite difference approximation for the grid point $\left({x}_{\mathit{i}},{y}_{\mathit{j}},{z}_{\mathit{k}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{L}}+1$, $\mathit{j}=1,2,\dots ,{\mathbf{M}}+1$ and $\mathit{k}=1,2,\dots ,{\mathbf{N}}+1$.
23:   PERTRB – REAL (KIND=nag_wp)Output
On exit: ${\mathbf{PERTRB}}=0$, unless a solution to Poisson's equation $\left(\lambda =0\right)$ is required with a combination of periodic or derivative boundary conditions (LBDCND, MBDCND and ${\mathbf{NBDCND}}=0$ or $3$). In this case a solution may not exist. PERTRB is a constant, calculated and subtracted from the array F, which ensures that a solution exists. D03FAF then computes this solution, which is a least squares solution to the original approximation. This solution is not unique and is unnormalized. The value of PERTRB should be small compared to the right-hand side F, otherwise a solution has been obtained to an essentially different problem. This comparison should always be made to ensure that a meaningful solution has been obtained.
24:   W(LWRK) – REAL (KIND=nag_wp) arrayWorkspace
25:   LWRK – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which D03FAF is called. $2×\left({\mathbf{N}}+1\right)×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{L}},{\mathbf{M}}\right)+3×{\mathbf{L}}+3×{\mathbf{M}}+4×{\mathbf{N}}+6$ is an upper bound on the required size of W. If LWRK is too small, the routine exits with ${\mathbf{IFAIL}}={\mathbf{2}}$, and if on entry ${\mathbf{IFAIL}}=0$ or $-1$, a message is output giving the exact value of LWRK required to solve the current problem.
26:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{XS}}\ge {\mathbf{XF}}$, or ${\mathbf{L}}<5$, or ${\mathbf{LBDCND}}<0$, or ${\mathbf{LBDCND}}>4$, or ${\mathbf{YS}}\ge {\mathbf{YF}}$, or ${\mathbf{M}}<5$, or ${\mathbf{MBDCND}}<0$, or ${\mathbf{MBDCND}}>4$, or ${\mathbf{ZS}}\ge {\mathbf{ZF}}$, or ${\mathbf{N}}<5$, or ${\mathbf{NBDCND}}<0$, or ${\mathbf{NBDCND}}>4$, or ${\mathbf{LDF}}<{\mathbf{L}}+1$, or ${\mathbf{LDF2}}<{\mathbf{M}}+1$.
${\mathbf{IFAIL}}=2$
 On entry, LWRK is too small.
${\mathbf{IFAIL}}=3$
 On entry, $\lambda >0$.

## 7  Accuracy

Not applicable.

The execution time is roughly proportional to ${\mathbf{L}}×{\mathbf{M}}×{\mathbf{N}}×\left({\mathrm{log}}_{2}{\mathbf{L}}+{\mathrm{log}}_{2}{\mathbf{M}}+5\right)$, but also depends on input parameters LBDCND and MBDCND.

## 9  Example

This example solves the Helmholz equation
 $∂2u ∂x2 + ∂2u ∂y2 + ∂2u ∂z2 +λ u=fx,y,z$
for $\left(x,y,z\right)\in \left[0,1\right]×\left[0,2\pi \right]×\left[0,\frac{\pi }{2}\right]$, where $\lambda =-2$, and $f\left(x,y,z\right)$ is derived from the exact solution
 $ux,y,z=x4sin⁡ycos⁡z.$
The equation is subject to the following boundary conditions, again derived from the exact solution given above.
• $u\left(0,y,z\right)$ and $u\left(1,y,z\right)$ are prescribed (i.e., ${\mathbf{LBDCND}}=1$).
• $u\left(x,0,z\right)=u\left(x,2\pi ,z\right)$ (i.e., ${\mathbf{MBDCND}}=0$).
• $u\left(x,y,0\right)$ and ${u}_{x}\left(x,y,\frac{\pi }{2}\right)$ are prescribed (i.e., ${\mathbf{NBDCND}}=2$).

### 9.1  Program Text

Program Text (d03fafe.f90)

### 9.2  Program Data

Program Data (d03fafe.d)

### 9.3  Program Results

Program Results (d03fafe.r)