Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_pde_2d_ellip_mgrid (d03ed)

## Purpose

nag_pde_2d_ellip_mgrid (d03ed) solves seven-diagonal systems of linear equations which arise from the discretization of an elliptic partial differential equation on a rectangular region. This function uses a multigrid technique.

## Syntax

[a, rhs, ub, us, u, numit, ifail] = d03ed(ngx, ngy, a, rhs, ub, maxit, acc, iout)
[a, rhs, ub, us, u, numit, ifail] = nag_pde_2d_ellip_mgrid(ngx, ngy, a, rhs, ub, maxit, acc, iout)

## Description

nag_pde_2d_ellip_mgrid (d03ed) 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.
nag_pde_2d_ellip_mgrid (d03ed) will automatically determine the number $l$ of possible coarse grids, ‘levels’ of the multigrid scheme, for a particular problem. In other words, nag_pde_2d_ellip_mgrid (d03ed) 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$.
nag_pde_2d_ellip_mgrid (d03ed) 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 function.

## 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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{ngx}$int64int32nag_int scalar
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:     $\mathrm{ngy}$int64int32nag_int scalar
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:     $\mathrm{a}\left(\mathit{lda},7\right)$ – double array
${\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$.
4:     $\mathrm{rhs}\left(\mathit{lda}\right)$ – double array
${\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}}$.
5:     $\mathrm{ub}\left({\mathbf{ngx}}×{\mathbf{ngy}}\right)$ – double array
${\mathbf{ub}}\left(i+\left(j-1\right)×{\mathbf{ngx}}\right)$ must be set to the initial estimate for the solution ${u}_{ij}$.
6:     $\mathrm{maxit}$int64int32nag_int scalar
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$.
7:     $\mathrm{acc}$ – double scalar
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 function 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 function 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$.
8:     $\mathrm{iout}$int64int32nag_int scalar
Controls the output of printed information to the advisory message unit as returned by nag_file_set_unit_advisory (x04ab):
${\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$.

None.

### Output Parameters

1:     $\mathrm{a}\left(\mathit{lda},7\right)$ – double array
Is overwritten.
2:     $\mathrm{rhs}\left(\mathit{lda}\right)$ – double array
The first ${\mathbf{ngx}}×{\mathbf{ngy}}$ elements are unchanged and the rest of the array is used as workspace.
3:     $\mathrm{ub}\left({\mathbf{ngx}}×{\mathbf{ngy}}\right)$ – double array
The corresponding component of the residual $r=f-{\mathbf{a}}u$.
4:     $\mathrm{us}\left(\mathit{lda}\right)$ – double array
The residual $2$-norm, stored in element ${\mathbf{us}}\left(1\right)$.
5:     $\mathrm{u}\left(\mathit{lda}\right)$ – double array
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}}$.
6:     $\mathrm{numit}$int64int32nag_int scalar
The number of iterations performed.
7:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\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$.
W  ${\mathbf{ifail}}=4$
On entry, acc is less than the machine precision. The function terminated because the residual norm is less than the machine precision.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

See acc (Arguments).

The rate of convergence of this function 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$).

## 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:
 $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$
function d03ed_example

fprintf('d03ed example results\n\n');

ngx = int64(65);
ngy = ngx;
nn  = ngx*ngy;
lda = 4*(ngx+1)*(ngy+1)/3;
a   = zeros(lda,7);
rhs = zeros(lda, 1);
ub  = zeros(nn, 1);

maxit = int64(15); acc = 0.0001; iout = int64(0);
hx = 1/double(ngx+1);
hy = 1/double(ngy+1);
alpha = 1.7;

% Set up operator, right-hand side and initial guess for
% step-lengths hx and hy
a(1:nn,[1 3 5 7]) = 1 - 0.5*alpha;
a(1:nn,[2 6])     = 0.5*alpha;
a(1:nn,4)         = -4 + alpha;
rhs(1:nn)         = -4*hx*hy;

% Boundary conditions u(x,y=0,1) = 0
a(2:ngx-1,1:2)       = 0.0;
a(nn-ngx+2:nn-1,6:7) = 0.0;
for j = 2:ngy-1
% Boundary condition u(x=0,y) = 0
iy = (j-1)*ngx + 1;
a(iy,[3 6]) = 0.0;
% Boundary condition u(x=1,y) = 1
iy = j*ngx;
rhs(iy) = rhs(iy) - a(iy,5) - a(iy,2);
a(iy,[2 5]) = 0.0;
end

% Bottom left corner
a(1,[1:3 6]) = 0.0;
% Top left corner
k = nn - ngx + 1;
a(k,[3 6:7]) = 0.0;
% Bottom right corner
% Use average value at discontinuity ( = 0.5 )
k = ngx;
rhs(k) = rhs(k) - a(k,2)*0.5 - a(k,5);
a(k,[1:2 5]) = 0.0;
% Top right corner
k = ngx*ngy;
rhs(k) = rhs(k) - a(k,2) - a(k,5);
a(k,[2 5:7]) = 0.0;
u = zeros(ngx*ngy,1);

[a, rhs, ub, resid, u, numit, ifail] = ...
d03ed(...
ngx, ngy, a, rhs, ub, maxit, acc, iout);

% Output solution statistics
fprintf('Number of iterations  %4d\n',numit);
fprintf('Maximum residual       %8.3e\n',resid(numit));

% Plot the solution u
fig1 = figure;
u2 = u(1:nn);
umat = reshape(u2,ngx,ngy);
x(1:ngx) = hx:hx:1-hx; y(1:ngy) = hy:hy:1-hy;
[xs,ys] = meshgrid(y,x);
mesh(xs,ys,umat);
xlabel('x'); ylabel('y'); zlabel ('u(x,y)');
title('d03ed example:  U_{xx}-\alpha U_{xy}+U_{yy}=-4, \alpha=1.7');


d03ed example results

Number of iterations     4
Maximum residual       -1.060e-08 