D03 Chapter Contents
D03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD03EDF

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

D03EDF solves seven-diagonal systems of linear equations which arise from the discretization of an elliptic partial differential equation on a rectangular region. This routine uses a multigrid technique.

## 2  Specification

 SUBROUTINE D03EDF ( NGX, NGY, LDA, A, RHS, UB, MAXIT, ACC, US, U, IOUT, NUMIT, IFAIL)
 INTEGER NGX, NGY, LDA, MAXIT, IOUT, NUMIT, IFAIL REAL (KIND=nag_wp) A(LDA,7), RHS(LDA), UB(NGX*NGY), ACC, US(LDA), U(LDA)

## 3  Description

D03EDF solves, by multigrid iteration, the seven-point scheme
 $Ai,j6ui-1,j+1 + Ai,j7ui,j+1 + Ai,j3ui-1,j + Ai,j4uij + Ai,j5ui+1,j + Ai,j1ui,j-1 + Ai,j2ui+1,j-1=fij, i=1,2,…,nx​ and ​j=1,2,…,ny,$
which arises from the discretization of an elliptic partial differential equation of the form
 $α x,yUxx+β x,yUxy+γ x,yUyy+δ x,yUx+ε x,yUy+ϕ x,yU=ψ x,y$
and its boundary conditions, defined on a rectangular region. This we write in matrix form as
 $Au=f.$
The algorithm is described in separate reports by Wesseling (1982a), Wesseling (1982b) and McCarthy (1983).
Systems of linear equations, matching the seven-point stencil defined above, are solved by a multigrid iteration. An initial estimate of the solution must be provided by you. A zero guess may be supplied if no better approximation is available.
A ‘smoother’ based on incomplete Crout decomposition is used to eliminate the high frequency components of the error. A restriction operator is then used to map the system on to a sequence of coarser grids. The errors are then smoothed and prolongated (mapped onto successively finer grids). When the finest cycle is reached, the approximation to the solution is corrected. The cycle is repeated for MAXIT iterations or until the required accuracy, ACC, is reached.
D03EDF will automatically determine the number $l$ of possible coarse grids, ‘levels’ of the multigrid scheme, for a particular problem. In other words, D03EDF determines the maximum integer $l$ so that ${n}_{x}$ and ${n}_{y}$ can be expressed in the form
 $nx = m2l-1 + 1 , ny = n2l-1 + 1 , with m≥2 ​ and ​ n≥2 .$
It should be noted that the rate of convergence improves significantly with the number of levels used (see McCarthy (1983)), so that ${n}_{x}$ and ${n}_{y}$ should be carefully chosen so that ${n}_{x}-1$ and ${n}_{y}-1$ have factors of the form ${2}^{l}$, with $l$ as large as possible. For good convergence the integer $l$ should be at least $2$.
D03EDF has been found to be robust in application, but being an iterative method the problem of divergence can arise. For a strictly diagonally dominant matrix $A$
 $Aij4 > ∑k≠4 Aijk , i=1,2,…,nx ​ and ​ j=1,2,…,ny$
no such problem is foreseen. The diagonal dominance of $A$ is not a necessary condition, but should this condition be strongly violated then divergence may occur. The quickest test is to try the routine.

## 4  References

McCarthy G J (1983) Investigation into the multigrid code MGD1 Report AERE-R 10889 Harwell
Wesseling P (1982a) MGD1 – a robust and efficient multigrid method Multigrid Methods. Lecture Notes in Mathematics 960 614–630 Springer–Verlag
Wesseling P (1982b) Theoretical aspects of a multigrid method SIAM J. Sci. Statist. Comput. 3 387–407

## 5  Parameters

1:     NGX – INTEGERInput
On entry: the number of interior grid points in the $x$-direction, ${n}_{x}$. ${\mathbf{NGX}}-1$ should preferably be divisible by as high a power of $2$ as possible.
Constraint: ${\mathbf{NGX}}\ge 3$.
2:     NGY – INTEGERInput
On entry: the number of interior grid points in the $y$-direction, ${n}_{y}$. ${\mathbf{NGY}}-1$ should preferably be divisible by as high a power of $2$ as possible.
Constraint: ${\mathbf{NGY}}\ge 3$.
3:     LDA – INTEGERInput
On entry: the first dimension of the array A, which must also be a lower bound for the dimension of the arrays RHS, US and U as declared in the (sub)program from which D03EDF is called. It is always sufficient to set ${\mathbf{LDA}}\ge \left(4×\left({\mathbf{NGX}}+1\right)×\left({\mathbf{NGY}}+1\right)\right)/3$, but slightly smaller values may be permitted, depending on the values of NGX and NGY. If on entry, LDA is too small, an error message gives the minimum permitted value. (LDA must be large enough to allow space for the coarse-grid approximations.)
4:     A(LDA,$7$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${\mathbf{A}}\left(\mathit{i}+\left(\mathit{j}-1\right)×{\mathbf{NGX}},\mathit{k}\right)$ must be set to ${{\mathbf{A}}}_{\mathit{i}\mathit{j}}^{\mathit{k}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{NGX}}$, $\mathit{j}=1,2,\dots ,{\mathbf{NGY}}$ and $\mathit{k}=1,2,\dots ,7$.
On exit: is overwritten.
5:     RHS(LDA) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${\mathbf{RHS}}\left(\mathit{i}+\left(\mathit{j}-1\right)×{\mathbf{NGX}}\right)$ must be set to ${f}_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{NGX}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{NGY}}$.
On exit: the first ${\mathbf{NGX}}×{\mathbf{NGY}}$ elements are unchanged and the rest of the array is used as workspace.
6:     UB(${\mathbf{NGX}}×{\mathbf{NGY}}$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${\mathbf{UB}}\left(i+\left(j-1\right)×{\mathbf{NGX}}\right)$ must be set to the initial estimate for the solution ${u}_{ij}$.
On exit: the corresponding component of the residual $r=f-{\mathbf{A}}u$.
7:     MAXIT – INTEGERInput
On entry: the maximum permitted number of multigrid iterations. If ${\mathbf{MAXIT}}=0$, no multigrid iterations are performed, but the coarse-grid approximations and incomplete Crout decompositions are computed, and may be output if IOUT is set accordingly.
Constraint: ${\mathbf{MAXIT}}\ge 0$.
8:     ACC – REAL (KIND=nag_wp)Input
On entry: the required tolerance for convergence of the residual $2$-norm:
 $r2=∑k=1 NGX×NGY rk 2$
where $r=f-Au$ and $u$ is the computed solution. Note that the norm is not scaled by the number of equations. The routine will stop after fewer than MAXIT iterations if the residual $2$-norm is less than the specified tolerance. (If ${\mathbf{MAXIT}}>0$, at least one iteration is always performed.)
If on entry ${\mathbf{ACC}}=0.0$, then the machine precision is used as a default value for the tolerance; if ${\mathbf{ACC}}>0.0$, but ACC is less than the machine precision, then the routine will stop when the residual $2$-norm is less than the machine precision and IFAIL will be set to $4$.
Constraint: ${\mathbf{ACC}}\ge 0.0$.
9:     US(LDA) – REAL (KIND=nag_wp) arrayOutput
On exit: the residual $2$-norm, stored in element ${\mathbf{US}}\left(1\right)$.
10:   U(LDA) – REAL (KIND=nag_wp) arrayOutput
On exit: the computed solution ${u}_{\mathit{i}\mathit{j}}$ is returned in ${\mathbf{U}}\left(\mathit{i}+\left(\mathit{j}-1\right)×{\mathbf{NGX}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NGX}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{NGY}}$.
11:   IOUT – INTEGERInput
On entry: controls the output of printed information to the advisory message unit as returned by X04ABF:
${\mathbf{IOUT}}=0$
No output.
${\mathbf{IOUT}}=1$
The solution ${u}_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{NGX}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{NGY}}$.
${\mathbf{IOUT}}=2$
The residual $2$-norm after each iteration, with the reduction factor over the previous iteration.
${\mathbf{IOUT}}=3$
As for ${\mathbf{IOUT}}=1$ and ${\mathbf{IOUT}}=2$.
${\mathbf{IOUT}}=4$
As for ${\mathbf{IOUT}}=3$, plus the final residual (as returned in UB).
${\mathbf{IOUT}}=5$
As for ${\mathbf{IOUT}}=4$, plus the initial elements of A and RHS.
${\mathbf{IOUT}}=6$
As for ${\mathbf{IOUT}}=5$, plus the Galerkin coarse grid approximations.
${\mathbf{IOUT}}=7$
As for ${\mathbf{IOUT}}=6$, plus the incomplete Crout decompositions.
${\mathbf{IOUT}}=8$
As for ${\mathbf{IOUT}}=7$, plus the residual after each iteration.
The elements ${\mathbf{A}}\left(p,k\right)$, the Galerkin coarse grid approximations and the incomplete Crout decompositions are output in the format:
• Y-index $\text{}=j$
• X-index $\text{}=i{\mathbf{A}}\left(p,1\right){\mathbf{A}}\left(p,2\right){\mathbf{A}}\left(p,3\right){\mathbf{A}}\left(p,4\right){\mathbf{A}}\left(p,5\right){\mathbf{A}}\left(p,6\right){\mathbf{A}}\left(p,7\right)$
• where $p=\mathit{i}+\left(\mathit{j}-1\right)×{\mathbf{NGX}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{NGX}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{NGY}}$.
The vectors ${\mathbf{U}}\left(p\right)$, ${\mathbf{UB}}\left(p\right)$, ${\mathbf{RHS}}\left(p\right)$ are output in matrix form with NGY rows and NGX columns. Where ${\mathbf{NGX}}>10$, the NGX values for a given $j$ value are produced in rows of $10$. Values of ${\mathbf{IOUT}}>4$ may yield considerable amounts of output.
Constraint: $0\le {\mathbf{IOUT}}\le 8$.
12:   NUMIT – INTEGEROutput
On exit: the number of iterations performed.
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, 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{NGX}}<3$, or ${\mathbf{NGY}}<3$, or LDA is too small, or ${\mathbf{ACC}}<0.0$, or ${\mathbf{MAXIT}}<0$, or ${\mathbf{IOUT}}<0$, or ${\mathbf{IOUT}}>8$.
${\mathbf{IFAIL}}=2$
MAXIT iterations have been performed with the residual $2$-norm decreasing at each iteration but the residual $2$-norm has not been reduced to less than the specified tolerance (see ACC). Examine the progress of the iteration by setting ${\mathbf{IOUT}}\ge 2$.
${\mathbf{IFAIL}}=3$
As for ${\mathbf{IFAIL}}={\mathbf{2}}$, except that at one or more iterations the residual $2$-norm did not decrease. It is likely that the method fails to converge for the given matrix $A$.
${\mathbf{IFAIL}}=4$
On entry, ACC is less than the machine precision. The routine terminated because the residual norm is less than the machine precision.

## 7  Accuracy

See ACC (Section 5).

The rate of convergence of this routine is strongly dependent upon the number of levels, $l$, in the multigrid scheme, and thus the choice of NGX and NGY is very important. You are advised to experiment with different values of NGX and NGY to see the effect they have on the rate of convergence; for example, using a value such as ${\mathbf{NGX}}=65$ ($\text{}={2}^{6}+1$) followed by ${\mathbf{NGX}}=64$ (for which $l=1$).

## 9  Example

The program solves the elliptic partial differential equation
 $Uxx-αUxy+Uyy=-4, α=1.7$
on the unit square $0\le x,y\le 1$, with boundary conditions
 $U=0 on ​ x=0, 0≤y≤1 y=0, 0≤x≤1 y=1, 0≤x≤1 U=1on ​x=1, 0≤y≤1.$
For the equation to be elliptic, $\alpha$ must be less than $2$.
The equation is discretized on a square grid with mesh spacing $h$ in both directions using the following approximations:
Figure 1
 $Uxx ≃ 1h2UE-2UO+UW Uyy ≃ 1h2UN-2UO+US Uxy ≃ 12⁢h2 UN-UNW +UE-2UO+UW-USE +US.$
Thus the following equations are solved:
 $12α ui- 1,j+ 1 + 1-12αui,j+ 1 + 1-12αui+ 1,j + - 4+αuij + 1-12αui+ 1,j + 1-12α ui,j- 1 + 12α ui+ 1,j- 1=-4h2$

### 9.1  Program Text

Program Text (d03edfe.f90)

### 9.2  Program Data

Program Data (d03edfe.d)

### 9.3  Program Results

Program Results (d03edfe.r)