Actual source code: cgs.c

petsc-master 2017-01-23
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:   if (ksp->normtype == KSP_NORM_NATURAL) dp *= dp;
 51:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
 52:   ksp->its   = 0;
 53:   ksp->rnorm = dp;
 54:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
 55:   KSPLogResidualHistory(ksp,dp);
 56:   KSPMonitor(ksp,0,dp);
 57:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
 58:   if (ksp->reason) return(0);

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

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

 87:   i = 0;
 88:   do {

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

104:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
105:     ksp->its++;
106:     ksp->rnorm = dp;
107:     PetscObjectSAWsGrantAccess((PetscObject)ksp);
108:     KSPLogResidualHistory(ksp,dp);
109:     KSPMonitor(ksp,i+1,dp);
110:     (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
111:     if (ksp->reason) break;

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

123:   KSPUnwindPreconditioner(ksp,X,T);
124:   return(0);
125: }

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

130:    Options Database Keys:
131: .   see KSPSolve()

133:    Level: beginner

135:    References:
136: .     1. - Sonneveld, 1989.

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

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

144: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBCGS, KSPSetPCSide()
145: M*/
146: PETSC_EXTERN PetscErrorCode KSPCreate_CGS(KSP ksp)
147: {

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

153:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
154:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,2);
155:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,2);
156:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_RIGHT,2);

158:   ksp->ops->setup          = KSPSetUp_CGS;
159:   ksp->ops->solve          = KSPSolve_CGS;
160:   ksp->ops->destroy        = KSPDestroyDefault;
161:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
162:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
163:   ksp->ops->setfromoptions = 0;
164:   ksp->ops->view           = 0;
165:   return(0);
166: }