Actual source code: cgs.c

petsc-master 2019-08-17
Report Typos and Errors

  2: /*

  4:     Note that for the complex numbers version, the VecDot() arguments
  5:     within the code MUST remain in the order given for correct computation
  6:     of inner products.
  7: */
  8:  #include <petsc/private/kspimpl.h>

 10: static PetscErrorCode KSPSetUp_CGS(KSP ksp)
 11: {

 15:   KSPSetWorkVecs(ksp,7);
 16:   return(0);
 17: }

 19: static PetscErrorCode  KSPSolve_CGS(KSP ksp)
 20: {
 22:   PetscInt       i;
 23:   PetscScalar    rho,rhoold,a,s,b;
 24:   Vec            X,B,V,P,R,RP,T,Q,U,AUQ;
 25:   PetscReal      dp = 0.0;
 26:   PetscBool      diagonalscale;

 29:   /* not sure what residual norm it does use, should use for right preconditioning */

 31:   PCGetDiagonalScale(ksp->pc,&diagonalscale);
 32:   if (diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);

 34:   X   = ksp->vec_sol;
 35:   B   = ksp->vec_rhs;
 36:   R   = ksp->work[0];
 37:   RP  = ksp->work[1];
 38:   V   = ksp->work[2];
 39:   T   = ksp->work[3];
 40:   Q   = ksp->work[4];
 41:   P   = ksp->work[5];
 42:   U   = ksp->work[6];
 43:   AUQ = V;

 45:   /* Compute initial preconditioned residual */
 46:   KSPInitialResidual(ksp,X,V,T,R,B);

 48:   /* Test for nothing to do */
 49:   VecNorm(R,NORM_2,&dp);
 50:   KSPCheckNorm(ksp,dp);
 51:   if (ksp->normtype == KSP_NORM_NATURAL) dp *= dp;
 52:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
 53:   ksp->its   = 0;
 54:   ksp->rnorm = dp;
 55:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
 56:   KSPLogResidualHistory(ksp,dp);
 57:   KSPMonitor(ksp,0,dp);
 58:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
 59:   if (ksp->reason) return(0);

 61:   /* Make the initial Rp == R */
 62:   VecCopy(R,RP);
 63:   /*  added for Fidap */
 64:   /* Penalize Startup - Isaac Hasbani Trick for CGS
 65:      Since most initial conditions result in a mostly 0 residual,
 66:      we change all the 0 values in the vector RP to the maximum.
 67:   */
 68:   if (ksp->normtype == KSP_NORM_NATURAL) {
 69:     PetscReal   vr0max;
 70:     PetscScalar *tmp_RP=0;
 71:     PetscInt    numnp  =0, *max_pos=0;
 72:     VecMax(RP, max_pos, &vr0max);
 73:     VecGetArray(RP, &tmp_RP);
 74:     VecGetLocalSize(RP, &numnp);
 75:     for (i=0; i<numnp; i++) {
 76:       if (tmp_RP[i] == 0.0) tmp_RP[i] = vr0max;
 77:     }
 78:     VecRestoreArray(RP, &tmp_RP);
 79:   }
 80:   /*  end of addition for Fidap */

 82:   /* Set the initial conditions */
 83:   VecDot(R,RP,&rhoold);        /* rhoold = (r,rp)      */
 84:   VecCopy(R,U);
 85:   VecCopy(R,P);
 86:   KSP_PCApplyBAorAB(ksp,P,V,T);

 88:   i = 0;
 89:   do {

 91:     VecDot(V,RP,&s);           /* s <- (v,rp)          */
 92:     KSPCheckDot(ksp,s);
 93:     a    = rhoold / s;                               /* a <- rho / s         */
 94:     VecWAXPY(Q,-a,V,U);      /* q <- u - a v         */
 95:     VecWAXPY(T,1.0,U,Q);      /* t <- u + q           */
 96:     VecAXPY(X,a,T);           /* x <- x + a (u + q)   */
 97:     KSP_PCApplyBAorAB(ksp,T,AUQ,U);
 98:     VecAXPY(R,-a,AUQ);       /* r <- r - a K (u + q) */
 99:     VecDot(R,RP,&rho);         /* rho <- (r,rp)        */
100:     KSPCheckDot(ksp,rho);
101:     if (ksp->normtype == KSP_NORM_NATURAL) {
102:       dp = PetscAbsScalar(rho);
103:     } else {
104:       VecNorm(R,NORM_2,&dp);
105:       KSPCheckNorm(ksp,dp);
106:     }

108:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
109:     ksp->its++;
110:     ksp->rnorm = dp;
111:     PetscObjectSAWsGrantAccess((PetscObject)ksp);
112:     KSPLogResidualHistory(ksp,dp);
113:     KSPMonitor(ksp,i+1,dp);
114:     (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
115:     if (ksp->reason) break;

117:     b      = rho / rhoold;                           /* b <- rho / rhoold    */
118:     VecWAXPY(U,b,Q,R);       /* u <- r + b q         */
119:     VecAXPY(Q,b,P);
120:     VecWAXPY(P,b,Q,U);       /* p <- u + b(q + b p)  */
121:     KSP_PCApplyBAorAB(ksp,P,V,Q);    /* v <- K p    */
122:     rhoold = rho;
123:     i++;
124:   } while (i<ksp->max_it);
125:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;

127:   KSPUnwindPreconditioner(ksp,X,T);
128:   return(0);
129: }

131: /*MC
132:      KSPCGS - This code implements the CGS (Conjugate Gradient Squared) method.

134:    Options Database Keys:
135: .   see KSPSolve()

137:    Level: beginner

139:    References:
140: .     1. - Sonneveld, 1989.

142:    Notes:
143:     Does not require a symmetric matrix. Does not apply transpose of the matrix.
144:         Supports left and right preconditioning, but not symmetric.

146:    Developer Notes:
147:     Has this weird support for doing the convergence test with the natural norm, I assume this works only with
148:       no preconditioning and symmetric positive definite operator.

150: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBCGS, KSPSetPCSide()
151: M*/
152: PETSC_EXTERN PetscErrorCode KSPCreate_CGS(KSP ksp)
153: {

157:   ksp->data = (void*)0;

159:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
160:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,2);
161:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,2);
162:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_RIGHT,2);

164:   ksp->ops->setup          = KSPSetUp_CGS;
165:   ksp->ops->solve          = KSPSolve_CGS;
166:   ksp->ops->destroy        = KSPDestroyDefault;
167:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
168:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
169:   ksp->ops->setfromoptions = 0;
170:   ksp->ops->view           = 0;
171:   return(0);
172: }