Actual source code: cr.c

  1: /*$Id: cr.c,v 1.64 2001/08/07 03:03:49 balay Exp $*/

  3: /*                       
  4:            This implements Preconditioned Conjugate Residuals.       
  5: */
 6:  #include src/sles/ksp/kspimpl.h

  8: static int KSPSetUp_CR(KSP ksp)
  9: {

 13:   if (ksp->pc_side == PC_RIGHT) {SETERRQ(2,"no right preconditioning for KSPCR");}
 14:   else if (ksp->pc_side == PC_SYMMETRIC) {SETERRQ(2,"no symmetric preconditioning for KSPCR");}
 15:   KSPDefaultGetWork(ksp,6);
 16:   return(0);
 17: }

 19: static int  KSPSolve_CR(KSP ksp,int *its)
 20: {
 21:   int          i = 0, maxit, ierr;
 22:   MatStructure pflag;
 23:   PetscReal    dp;
 24:   PetscScalar  ai, bi;
 25:   PetscScalar  apq,btop, bbot, mone = -1.0;
 26:   Vec          X,B,R,RT,P,AP,ART,Q;
 27:   Mat          Amat, Pmat;


 31:   maxit   = ksp->max_it;
 32:   X       = ksp->vec_sol;
 33:   B       = ksp->vec_rhs;
 34:   R       = ksp->work[0];
 35:   RT      = ksp->work[1];
 36:   P       = ksp->work[2];
 37:   AP      = ksp->work[3];
 38:   ART     = ksp->work[4];
 39:   Q       = ksp->work[5];

 41:   /* R is the true residual norm, RT is the preconditioned residual norm */
 42:   PCGetOperators(ksp->B,&Amat,&Pmat,&pflag);
 43:   if (!ksp->guess_zero) {
 44:     KSP_MatMult(ksp,Amat,X,R);     /*   R <- A*X           */
 45:     VecAYPX(&mone,B,R);            /*   R <- B-R == B-A*X  */
 46:   } else {
 47:     VecCopy(B,R);                  /*   R <- B (X is 0)    */
 48:   }
 49:   KSP_PCApply(ksp,ksp->B,R,P);     /*   P   <- B*R         */
 50:   KSP_MatMult(ksp,Amat,P,AP);      /*   AP  <- A*P         */
 51:   VecCopy(P,RT);                   /*   RT  <- P           */
 52:   VecCopy(AP,ART);                 /*   ART <- AP          */
 53:   ierr   = VecDot(RT,ART,&btop);          /*   (RT,ART)           */
 54:   if (ksp->normtype == KSP_PRECONDITIONED_NORM) {
 55:     VecNorm(RT,NORM_2,&dp);        /*   dp <- RT'*RT       */
 56:   } else if (ksp->normtype == KSP_UNPRECONDITIONED_NORM) {
 57:     VecNorm(R,NORM_2,&dp);         /*   dp <- R'*R         */
 58:   } else if (ksp->normtype == KSP_NATURAL_NORM) {
 59:     dp = sqrt(PetscAbsScalar(btop));                    /* dp = sqrt(R,AR)      */
 60:   }
 61:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
 62:   if (ksp->reason) {*its = 0; return(0);}
 63:   KSPMonitor(ksp,0,dp);
 64:   PetscObjectTakeAccess(ksp);
 65:   ksp->its = 0;
 66:   ksp->rnorm              = dp;
 67:   PetscObjectGrantAccess(ksp);
 68:   KSPLogResidualHistory(ksp,dp);

 70:   for ( i=0; i<maxit; i++) {
 71:     ierr   = KSP_PCApply(ksp,ksp->B,AP,Q);/*   Q <- B* AP          */
 72:                                                         /* Step 3                */

 74:     ierr   = VecDot(AP,Q,&apq);
 75:     ai = btop/apq;                                      /* ai = (RT,ART)/(AP,Q)  */

 77:     ierr   = VecAXPY(&ai,P,X);            /*   X   <- X + ai*P     */
 78:     ai     = -ai;
 79:     ierr   = VecAXPY(&ai,Q,RT);           /*   RT  <- RT - ai*Q    */
 80:     ierr   = KSP_MatMult(ksp,Amat,RT,ART);/*   ART <-   A*RT       */
 81:     bbot = btop;
 82:     ierr   = VecDot(RT,ART,&btop);

 84:     if (ksp->normtype == KSP_PRECONDITIONED_NORM) {
 85:       VecNorm(RT,NORM_2,&dp);      /*   dp <- || RT ||      */
 86:     } else if (ksp->normtype == KSP_NATURAL_NORM) {
 87:       dp = sqrt(PetscAbsScalar(btop));                  /* dp = sqrt(R,AR)       */
 88:     } else if (ksp->normtype == KSP_NO_NORM) {
 89:       dp = 0.0;
 90:     } else if (ksp->normtype == KSP_UNPRECONDITIONED_NORM) {
 91:       VecAXPY(&ai,AP,R);           /*   R   <- R - ai*AP    */
 92:       VecNorm(R,NORM_2,&dp);       /*   dp <- R'*R          */
 93:     } else {
 94:       SETERRQ1(1,"KSPNormType of %d not supported",(int)ksp->normtype);
 95:     }

 97:     PetscObjectTakeAccess(ksp);
 98:     ksp->its++;
 99:     ksp->rnorm = dp;
100:     PetscObjectGrantAccess(ksp);

102:     KSPLogResidualHistory(ksp,dp);
103:     KSPMonitor(ksp,i+1,dp);
104:     (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
105:     if (ksp->reason) break;

107:     bi = btop/bbot;
108:     VecAYPX(&bi,RT,P);              /*   P <- RT + Bi P     */
109:     VecAYPX(&bi,ART,AP);            /*   AP <- ART + Bi AP  */
110:   }
111:   if (i == maxit) {
112:     ksp->reason =  KSP_DIVERGED_ITS;
113:     i--;
114:   }
115:   *its = i + 1;
116:   return(0);
117: }


120: EXTERN_C_BEGIN
121: int KSPCreate_CR(KSP ksp)
122: {
124:   ksp->pc_side                   = PC_LEFT;
125:   ksp->ops->setup                = KSPSetUp_CR;
126:   ksp->ops->solve                = KSPSolve_CR;
127:   ksp->ops->destroy              = KSPDefaultDestroy;
128:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
129:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;
130:   ksp->ops->setfromoptions       = 0;
131:   ksp->ops->view                 = 0;
132:   return(0);
133: }
134: EXTERN_C_END