3次元FDM用CG法NO.1(C)プログラム

    2003/08/24 日立製作所 & 早稲田大学 後 保範 ( Ushiro Yasunori )
--------------------------------------------------------------

1. 概要

 3次元差分法で離散化した疎行列を係数とする連立一次方程式Ax=bに対してCG法で
反復解xを求める。
α=(p,r)/(p,Ap),β=-(r,Ap)/(p,Ap)を使用した、直交性に比較的強いバージョン

2. プログラム

#include <stdio.h>
#include <math.h>
// Global Define
#define NDX  51
#define NDY  51
#define NDZ  51
#define A(i,j,k,l)  A[l-1][k][j][i]
#define B(i,j,k)    B[k][j][i]
#define X(i,j,k)    X[k][j][i]
#define R(i,j,k)    R[k][j][i]
#define P(i,j,k)    P[k][j][i]
#define Q(i,j,k)    Q[k][j][i]
extern double A[4][NDZ][NDY][NDX], B[NDZ][NDY][NDX], X[NDZ][NDY][NDX] ;
extern double R[NDZ][NDY][NDX], P[NDZ][NDY][NDX], Q[NDZ][NDY][NDX] ;
extern FILE *FT1 ;
//=================================================================C
int CG3D1(int NX, int NY, int NZ, double EPS, int *ITER, double *ERR)
//=================================================================C
//  Solve Ax=b by CG NO.1 with 3 dimensional FDM                   C
//    Alpha=(P,R)/(P,AP),   Beta=-(R,AP)/(P,AP)                    C
//-----------------------------------------------------------------C
//    NX           I*4, In,  Grid Numbers on X-axis                C
//    NY           I*4, In,  Grid Numbers on Y-axis                C
//    NZ           I*4, In,  Grid Numbers on Y-axis                C
//    EPS          R*8, In,  if ||r||/||b|| <= EPS --> return      C
//    ITER         I*4, I/O, Number of Iteration                   C
//    ERR          R*8, Out, ERR=||r||/||b||                       C
//    return       I*4, Out, IERR=0,  Normal Return                C
//                               =1,  No Convergent                C
//-----------------------------------------------------------------C
//    Written by Yasunori Ushiro,   2003/08/21                     C
//        ( Hitachi Ltd. and Waseda University )                   C
//=================================================================C
{ int     i, j, k, kk, IERR ;
  double  BN, RN, C, Alpha, Beta ;
//  P=R=B-A*X
  IERR = 0 ;
  BN   = 0.0 ;
  for (k=1; k<=NZ-1; k++) {
   for (j=1; j<=NY-1; j++) {
    for (i=1; i<=NX-1; i++) {
      BN     = BN + B(i,j,k)*B(i,j,k) ;
      R(i,j,k) = B(i,j,k) - A(i,j,k,1)*X(i,j,k-1) 
               - A(i,j,k,2)*X(i,j-1,k) - A(i,j,k,3)*X(i-1,j,k)
               - A(i,j,k,4)*X(i,j,k)   - A(i+1,j,k,3)*X(i+1,j,k)
               - A(i,j+1,k,2)*X(i,j+1,k) - A(i,j,k+1,1)*X(i,j,k+1) ;
      P(i,j,k) = R(i,j,k) ;  }
    } }
//  Main Loop
  for (kk=1; kk<=*ITER; kk++) {
//   Q=A*P, C=(P,Q), Alpha=(P,R)/C
    C     = 0.0 ;
    Alpha = 0.0 ;
    for (k=1; k<=NZ-1; k++) {
     for (j=1; j<=NY-1; j++) {
      for (i=1; i<=NX-1; i++) {
        Q(i,j,k) = A(i,j,k,1)*P(i,j,k-1) + A(i,j,k,2)*P(i,j-1,k)
                 + A(i,j,k,3)*P(i-1,j,k) + A(i,j,k,4)*P(i,j,k)
                 + A(i+1,j,k,3)*P(i+1,j,k) + A(i,j+1,k,2)*P(i,j+1,k)
                 + A(i,j,k+1,1)*P(i,j,k+1) ;
        C        = C + P(i,j,k)*Q(i,j,k) ;
        Alpha    = Alpha + P(i,j,k)*R(i,j,k) ;  }
     } }
    Alpha = Alpha/C ;
//   X=X+Alpha*P,  R=R-Alpha*Q
    RN   = 0.0 ;
    Beta = 0.0 ;
    for (k=1; k<=NZ-1; k++) {
     for (j=1; j<=NY-1; j++) {
      for (i=1; i<=NX-1; i++) {
        X(i,j,k) = X(i,j,k) + Alpha*P(i,j,k) ;
        R(i,j,k) = R(i,j,k) - Alpha*Q(i,j,k) ;
        RN       = RN + R(i,j,k)*R(i,j,k) ;
        Beta     = Beta + R(i,j,k)*Q(i,j,k) ;  }
     } }
//   if(ERR <= EPS) Convergent,  Beta=-(R,Q)/(P,Q)
    *ERR = sqrt(RN/BN) ;
    if(*ERR <= EPS) goto M100 ;
    Beta = -Beta/C ;
//   P=R+Beta*P
    for (k=1; k<=NZ-1; k++) {
     for (j=1; j<=NY-1; j++) {
      for (i=1; i<=NX-1; i++) {
        P(i,j,k) = R(i,j,k) + Beta*P(i,j,k) ;  }
     } }
   }
    IERR = 1 ;
//
 M100: *ITER = kk ;
 return (IERR) ; }