hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_pde_2d_ellip_mgrid (d03ed)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 nx and ny can be expressed in the form
nx = m2l-1 + 1 ,   ny = n2l-1 + 1 ,   with   m2 ​ and ​ n2 .  
It should be noted that the rate of convergence improves significantly with the number of levels used (see McCarthy (1983)), so that nx and ny should be carefully chosen so that nx-1 and ny-1 have factors of the form 2l, 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 > k4 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:     ngx int64int32nag_int scalar
The number of interior grid points in the x-direction, nx. ngx-1 should preferably be divisible by as high a power of 2 as possible.
Constraint: ngx3.
2:     ngy int64int32nag_int scalar
The number of interior grid points in the y-direction, ny. ngy-1 should preferably be divisible by as high a power of 2 as possible.
Constraint: ngy3.
3:     alda7 – double array
ai+j-1×ngxk must be set to aijk, for i=1,2,,ngx, j=1,2,,ngy and k=1,2,,7.
4:     rhslda – double array
rhsi+j-1×ngx must be set to fij, for i=1,2,,ngx and j=1,2,,ngy.
5:     ubngx×ngy – double array
ubi+j-1×ngx must be set to the initial estimate for the solution uij.
6:     maxit int64int32nag_int scalar
The maximum permitted number of multigrid iterations. If 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: maxit0.
7:     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 maxit>0, at least one iteration is always performed.)
If on entry acc=0.0, then the machine precision is used as a default value for the tolerance; if 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: acc0.0.
8:     iout int64int32nag_int scalar
Controls the output of printed information to the advisory message unit as returned by nag_file_set_unit_advisory (x04ab):
iout=0
No output.
iout=1
The solution uij, for i=1,2,,ngx and j=1,2,,ngy.
iout=2
The residual 2-norm after each iteration, with the reduction factor over the previous iteration.
iout=3
As for iout=1 and iout=2.
iout=4
As for iout=3, plus the final residual (as returned in ub).
iout=5
As for iout=4, plus the initial elements of a and rhs.
iout=6
As for iout=5, plus the Galerkin coarse grid approximations.
iout=7
As for iout=6, plus the incomplete Crout decompositions.
iout=8
As for iout=7, plus the residual after each iteration.
The elements apk, the Galerkin coarse grid approximations and the incomplete Crout decompositions are output in the format:
  • Y-index =j
  • X-index =iap1ap2ap3ap4ap5ap6ap7
  • where p=i+j-1×ngx, for i=1,2,,ngx and j=1,2,,ngy.
The vectors up, ubp, rhsp are output in matrix form with ngy rows and ngx columns. Where ngx>10, the ngx values for a given j value are produced in rows of 10. Values of iout>4 may yield considerable amounts of output.
Constraint: 0iout8.

Optional Input Parameters

None.

Output Parameters

1:     alda7 – double array
Is overwritten.
2:     rhslda – double array
The first ngx×ngy elements are unchanged and the rest of the array is used as workspace.
3:     ubngx×ngy – double array
The corresponding component of the residual r=f-au.
4:     uslda – double array
The residual 2-norm, stored in element us1.
5:     ulda – double array
The computed solution uij is returned in ui+j-1×ngx, for i=1,2,,ngx and j=1,2,,ngy.
6:     numit int64int32nag_int scalar
The number of iterations performed.
7:     ifail int64int32nag_int scalar
ifail=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.

   ifail=1
On entry,ngx<3,
orngy<3,
orlda is too small,
oracc<0.0,
ormaxit<0,
oriout<0,
oriout>8.
   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 iout2.
   ifail=3
As for ifail=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  ifail=4
On entry, acc is less than the machine precision. The function terminated because the residual norm is less than the machine precision.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

See acc (Arguments).

Further Comments

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 ngx=65 (=26+1) followed by 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 0x,y1, with boundary conditions
U=0 on ​ x=0, 0y1 y=0, 0x1 y=1, 0x1 U=1on ​x=1,  0y1.  
For the equation to be elliptic, α 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 12h2 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
d03ed_fig1.png

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015