NAG Library Routine Document
D03EEF
1 Purpose
D03EEF discretizes a secondorder elliptic partial differential equation (PDE) on a rectangular region.
2 Specification
SUBROUTINE D03EEF ( 
XMIN, XMAX, YMIN, YMAX, PDEF, BNDY, NGX, NGY, LDA, A, RHS, SCHEME, IFAIL) 
INTEGER 
NGX, NGY, LDA, IFAIL 
REAL (KIND=nag_wp) 
XMIN, XMAX, YMIN, YMAX, A(LDA,7), RHS(LDA) 
CHARACTER(1) 
SCHEME 
EXTERNAL 
PDEF, BNDY 

3 Description
D03EEF discretizes a secondorder linear elliptic partial differential equation of the form
on a rectangular region
subject to boundary conditions of the form
where
$\frac{\partial U}{\partial n}$ denotes the outward pointing normal derivative on the boundary. Equation
(1) is said to be elliptic if
for all points in the rectangular region. The linear equations produced are in a form suitable for passing directly to the multigrid routine
D03EDF.
The equation is discretized on a rectangular grid, with
${n}_{x}$ grid points in the
$x$direction and
${n}_{y}$ grid points in the
$y$direction. The grid spacing used is therefore
and the coordinates of the grid points
$\left({x}_{i},{y}_{j}\right)$ are
At each grid point
$\left({x}_{i},{y}_{j}\right)$ six neighbouring grid points are used to approximate the partial differential equation, so that the equation is discretized on the sevenpoint stencil shown in
Figure 1.
For convenience the approximation ${u}_{ij}$ to the exact solution $U\left({x}_{i},{y}_{j}\right)$ is denoted by ${u}_{\mathrm{O}}$, and the neighbouring approximations are labelled according to points of the compass as shown. Where numerical labels for the seven points are required, these are also shown.
The following approximations are used for the second derivatives:
Two possible schemes may be used to approximate the first derivatives:
Central differences are more accurate than upwind differences, but upwind differences may lead to a more diagonally dominant matrix for those problems where the coefficients of the first derivatives are significantly larger than the coefficients of the second derivatives.
The approximations used for the first derivatives may be written in a more compact form as follows:
where
${k}_{x}=\mathrm{sign}\delta $ and
${k}_{y}=\mathrm{sign}\epsilon $ for upwind differences, and
${k}_{x}={k}_{y}=0$ for central differences.
At all points in the rectangular domain, including the boundary, the coefficients in the partial differential equation are evaluated by calling
PDEF, and applying the approximations. This leads to a sevendiagonal system of linear equations of the form:
where the coefficients are given by
These equations then have to be modified to take account of the boundary conditions. These may be Dirichlet (where the solution is given), Neumann (where the derivative of the solution is given), or mixed (where a linear combination of solution and derivative is given).
If the boundary conditions are Dirichlet, there are an infinity of possible equations which may be applied:
If
D03EDF is used to solve the discretized equations, it turns out that the choice of
$\mu $ can have a dramatic effect on the rate of convergence, and the obvious choice
$\mu =1$ is not the best. Some choices may even cause the multigrid method to fail altogether. In practice it has been found that a value of the same order as the other diagonal elements of the matrix is best, and the following value has been found to work well in practice:
If the boundary conditions are either mixed or Neumann (i.e.,
$B\ne 0$ on return from
BNDY), then one of the points in the sevenpoint stencil lies outside the domain. In this case the normal derivative in the boundary conditions is used to eliminate the ‘fictitious’ point,
${u}_{\text{outside}}$:
It should be noted that if the boundary conditions are Neumann and $\varphi \left(x,y\right)\equiv 0$, then there is no unique solution. The routine returns with ${\mathbf{IFAIL}}={\mathbf{5}}$ in this case, and the sevendiagonal matrix is singular.
The four corners are treated separately.
BNDY is called twice, once along each of the edges meeting at the corner. If both boundary conditions at this point are Dirichlet and the prescribed solution values agree, then this value is used in an equation of the form
(2). If the prescribed solution is discontinuous at the corner, then the average of the two values is used. If one boundary condition is Dirichlet and the other is mixed, then the value prescribed by the Dirichlet condition is used in an equation of the form given above. Finally, if both conditions are mixed or Neumann, then two ‘fictitious’ points are eliminated using two equations of the form
(3).
It is possible that equations for which the solution is known at all points on the boundary, have coefficients which are not defined on the boundary. Since this routine calls
PDEF at
all points in the domain, including boundary points, arithmetic errors may occur in
PDEF which this routine cannot trap. If you have an equation with Dirichlet boundary conditions (i.e.,
$B=0$ at all points on the boundary), but with PDE coefficients which are singular on the boundary, then
D03EDF could be called directly only using interior grid points at your discretization.
After the equations have been set up as described above, they are checked for diagonal dominance. That is to say,
If this condition is not satisfied then the routine returns with
${\mathbf{IFAIL}}={\mathbf{6}}$. The multigrid routine
D03EDF may still converge in this case, but if the coefficients of the first derivatives in the partial differential equation are large compared with the coefficients of the second derivative, you should consider using upwind differences (
${\mathbf{SCHEME}}=\text{'U'}$).
Since this routine is designed primarily for use with
D03EDF, this document should be read in conjunction with the document for that routine.
4 References
Wesseling P (1982) MGD1 – a robust and efficient multigrid method Multigrid Methods. Lecture Notes in Mathematics 960 614–630 Springer–Verlag
5 Parameters
 1: XMIN – REAL (KIND=nag_wp)Input
 2: XMAX – REAL (KIND=nag_wp)Input
On entry: the lower and upper $x$ coordinates of the rectangular region respectively, ${x}_{A}$ and ${x}_{B}$.
Constraint:
${\mathbf{XMIN}}<{\mathbf{XMAX}}$.
 3: YMIN – REAL (KIND=nag_wp)Input
 4: YMAX – REAL (KIND=nag_wp)Input
On entry: the lower and upper $y$ coordinates of the rectangular region respectively, ${y}_{A}$ and ${y}_{B}$.
Constraint:
${\mathbf{YMIN}}<{\mathbf{YMAX}}$.
 5: PDEF – SUBROUTINE, supplied by the user.External Procedure
PDEF must evaluate the functions
$\alpha \left(x,y\right)$,
$\beta \left(x,y\right)$,
$\gamma \left(x,y\right)$,
$\delta \left(x,y\right)$,
$\epsilon \left(x,y\right)$,
$\varphi \left(x,y\right)$ and
$\psi \left(x,y\right)$ which define the equation at a general point
$\left(x,y\right)$.
The specification of
PDEF is:
REAL (KIND=nag_wp) 
X, Y, ALPHA, BETA, GAMMA, DELTA, EPSLON, PHI, PSI 

 1: X – REAL (KIND=nag_wp)Input
 2: Y – REAL (KIND=nag_wp)Input
On entry: the $x$ and $y$ coordinates of the point at which the coefficients of the partial differential equation are to be evaluated.
 3: ALPHA – REAL (KIND=nag_wp)Output
 4: BETA – REAL (KIND=nag_wp)Output
 5: GAMMA – REAL (KIND=nag_wp)Output
 6: DELTA – REAL (KIND=nag_wp)Output
 7: EPSLON – REAL (KIND=nag_wp)Output
 8: PHI – REAL (KIND=nag_wp)Output
 9: PSI – REAL (KIND=nag_wp)Output
On exit:
ALPHA,
BETA,
GAMMA,
DELTA,
EPSLON,
PHI and
PSI must be set to the values of
$\alpha \left(x,y\right)$,
$\beta \left(x,y\right)$,
$\gamma \left(x,y\right)$,
$\delta \left(x,y\right)$,
$\epsilon \left(x,y\right)$,
$\varphi \left(x,y\right)$ and
$\psi \left(x,y\right)$ respectively at the point specified by
X and
Y.
PDEF must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D03EEF is called. Parameters denoted as
Input must
not be changed by this procedure.
 6: BNDY – SUBROUTINE, supplied by the user.External Procedure
BNDY must evaluate the functions
$a\left(x,y\right)$,
$b\left(x,y\right)$, and
$c\left(x,y\right)$ involved in the boundary conditions.
The specification of
BNDY is:
INTEGER 
IBND 
REAL (KIND=nag_wp) 
X, Y, A, B, C 

 1: X – REAL (KIND=nag_wp)Input
 2: Y – REAL (KIND=nag_wp)Input
On entry: the $x$ and $y$ coordinates of the point at which the boundary conditions are to be evaluated.
 3: A – REAL (KIND=nag_wp)Output
 4: B – REAL (KIND=nag_wp)Output
 5: C – REAL (KIND=nag_wp)Output
On exit:
A,
B and
C must be set to the values of the functions appearing in the boundary conditions.
 6: IBND – INTEGERInput
On entry: specifies on which boundary the point (
X,
Y) lies.
${\mathbf{IBND}}=0$,
$1$,
$2$ or
$3$ according as the point lies on the bottom, right, top or left boundary.
BNDY must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D03EEF is called. Parameters denoted as
Input must
not be changed by this procedure.
 7: NGX – INTEGERInput
 8: NGY – INTEGERInput
On entry: the number of interior grid points in the
$x$ and
$y$directions respectively,
${n}_{x}$ and
${n}_{y}$. If the sevendiagonal equations are to be solved by
D03EDF, then
${\mathbf{NGX}}1$ and
${\mathbf{NGY}}1$ should preferably be divisible by as high a power of
$2$ as possible.
Constraints:
 ${\mathbf{NGX}}\ge 3$;
 ${\mathbf{NGY}}\ge 3$.
 9: LDA – INTEGERInput
On entry: the first dimension of the array
A and the dimension of the array
RHS as declared in the (sub)program from which D03EEF is called.
Constraint:
if only the sevendiagonal equations are required, then
${\mathbf{LDA}}\ge {\mathbf{NGX}}\times {\mathbf{NGY}}$. If a call to this routine is to be followed by a call to
D03EDF to solve the sevendiagonal linear equations,
${\mathbf{LDA}}\ge \left(4\times \left({\mathbf{NGX}}+1\right)\times \left({\mathbf{NGY}}+1\right)\right)/3$.
Note: this routine only checks the former condition.
D03EDF, if called, will check the latter condition.
 10: A(LDA,$7$) – REAL (KIND=nag_wp) arrayOutput
On exit:
${\mathbf{A}}\left(\mathit{i},\mathit{j}\right)$, for
$\mathit{i}=1,2,\dots ,{\mathbf{NGX}}\times {\mathbf{NGY}}$ and
$\mathit{j}=1,2,\dots ,7$, contains the sevendiagonal linear equations produced by the discretization described above. If
${\mathbf{LDA}}>{\mathbf{NGX}}\times {\mathbf{NGY}}$, the remaining elements are not referenced by the routine, but if
${\mathbf{LDA}}\ge \left(4\times \left({\mathbf{NGX}}+1\right)\times \left({\mathbf{NGY}}+1\right)\right)/3$ then the array
A can be passed directly to
D03EDF, where these elements are used as workspace.
 11: RHS(LDA) – REAL (KIND=nag_wp) arrayOutput
On exit: the first
${\mathbf{NGX}}\times {\mathbf{NGY}}$ elements contain the righthand sides of the sevendiagonal linear equations produced by the discretization described above. If
${\mathbf{LDA}}>{\mathbf{NGX}}\times {\mathbf{NGY}}$, the remaining elements are not referenced by the routine, but if
${\mathbf{LDA}}\ge \left(4\times \left({\mathbf{NGY}}+1\right)\times \left({\mathbf{NGY}}+1\right)\right)/3$ then the array
RHS can be passed directly to
D03EDF, where these elements are used as workspace.
 12: SCHEME – CHARACTER(1)Input
On entry: the type of approximation to be used for the first derivatives which occur in the partial differential equation.
 ${\mathbf{SCHEME}}=\text{'C'}$
 Central differences are used.
 ${\mathbf{SCHEME}}=\text{'U'}$
 Upwind differences are used.
Constraint:
${\mathbf{SCHEME}}=\text{'C'}$ or
$\text{'U'}$.
Note: generally speaking, if at least one of the coefficients multiplying the first derivatives (
DELTA or
EPSLON as returned by
PDEF) are large compared with the coefficients multiplying the second derivatives, then upwind differences may be more appropriate. Upwind differences are less accurate than central differences, but may result in more rapid convergence for strongly convective equations. The easiest test is to try both schemes.
 13: 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, because for this routine the values of the output parameters may be useful even if
${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is
$1$.
When the value $\mathbf{1}\text{ or}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).
Note: D03EEF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
 ${\mathbf{IFAIL}}=1$
On entry,  ${\mathbf{XMIN}}\ge {\mathbf{XMAX}}$, 
or  ${\mathbf{YMIN}}\ge {\mathbf{YMAX}}$, 
or  ${\mathbf{NGX}}<3$, 
or  ${\mathbf{NGY}}<3$, 
or  ${\mathbf{LDA}}<{\mathbf{NGX}}\times {\mathbf{NGY}}$, 
or  SCHEME is not one of 'C' or 'U'. 
 ${\mathbf{IFAIL}}=2$
At some point on the boundary there is a derivative in the boundary conditions (
${\mathbf{B}}\ne 0$ on return from
BNDY) and there is a nonzero coefficient of the mixed derivative
$\frac{{\partial}^{2}U}{\partial x\partial y}$ (
${\mathbf{BETA}}\ne 0$ on return from
PDEF).
 ${\mathbf{IFAIL}}=3$
A null boundary has been specified, i.e., at some point both
A and
B are zero on return from a call to
BNDY.
 ${\mathbf{IFAIL}}=4$
The equation is not elliptic, i.e.,
$4\times {\mathbf{ALPHA}}\times {\mathbf{GAMMA}}<{{\mathbf{BETA}}}^{2}$ after a call to
PDEF. The discretization has been completed, but the convergence of
D03EDF cannot be guaranteed.
 ${\mathbf{IFAIL}}=5$
The boundary conditions are purely Neumann (only the derivative is specified) and there is, in general, no unique solution.
 ${\mathbf{IFAIL}}=6$
The equations were not diagonally dominant. (See
Section 3.)
7 Accuracy
Not applicable.
If this routine is used as a preprocessor to the multigrid routine
D03EDF it should be noted that the rate of convergence of that routine is strongly dependent upon the number of levels in the multigrid scheme, and thus the choice of
NGX and
NGY is very important.
9 Example
The program solves the elliptic partial differential equation
on the unit square
$0\le x$,
$y\le 1$, with boundary conditions
 $\frac{\partial U}{\partial n}$ given on $x=0$ and $y=0$,
 $U$ given on $x=1$ and $y=1$.
The function $f\left(x,y\right)$ and the exact form of the boundary conditions are derived from the exact solution $U\left(x,y\right)=\mathrm{sin}x\mathrm{sin}y$.
The equation is first solved using central differences. Since the coefficients of the first derivatives are large, the linear equations are not diagonally dominated, and convergence is slow. The equation is solved a second time with upwind differences, showing that convergence is more rapid, but the solution is less accurate.
9.1 Program Text
Program Text (d03eefe.f90)
9.2 Program Data
Program Data (d03eefe.d)
9.3 Program Results
Program Results (d03eefe.r)