Actual source code: bcgs.c

petsc-3.5.1 2014-08-06
Report Typos and Errors
  2: #include <../src/ksp/ksp/impls/bcgs/bcgsimpl.h>       /*I  "petscksp.h"  I*/

  6: PetscErrorCode KSPSetFromOptions_BCGS(KSP ksp)
  7: {

 11:   PetscOptionsHead("KSP BCGS Options");
 12:   PetscOptionsTail();
 13:   return(0);
 14: }

 18: PetscErrorCode KSPSetUp_BCGS(KSP ksp)
 19: {

 23:   KSPSetWorkVecs(ksp,6);
 24:   return(0);
 25: }


 30: PetscErrorCode KSPSolve_BCGS(KSP ksp)
 31: {
 33:   PetscInt       i;
 34:   PetscScalar    rho,rhoold,alpha,beta,omega,omegaold,d1;
 35:   Vec            X,B,V,P,R,RP,T,S;
 36:   PetscReal      dp    = 0.0,d2;
 37:   KSP_BCGS       *bcgs = (KSP_BCGS*)ksp->data;

 40:   X  = ksp->vec_sol;
 41:   B  = ksp->vec_rhs;
 42:   R  = ksp->work[0];
 43:   RP = ksp->work[1];
 44:   V  = ksp->work[2];
 45:   T  = ksp->work[3];
 46:   S  = ksp->work[4];
 47:   P  = ksp->work[5];

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

 52:   /* with right preconditioning need to save initial guess to add to final solution */
 53:   if (ksp->pc_side == PC_RIGHT && !ksp->guess_zero) {
 54:     if (!bcgs->guess) {
 55:       VecDuplicate(X,&bcgs->guess);
 56:     }
 57:     VecCopy(X,bcgs->guess);
 58:     VecSet(X,0.0);
 59:   }

 61:   /* Test for nothing to do */
 62:   if (ksp->normtype != KSP_NORM_NONE) {
 63:     VecNorm(R,NORM_2,&dp);
 64:   }
 65:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
 66:   ksp->its   = 0;
 67:   ksp->rnorm = dp;
 68:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
 69:   KSPLogResidualHistory(ksp,dp);
 70:   KSPMonitor(ksp,0,dp);
 71:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
 72:   if (ksp->reason) return(0);

 74:   /* Make the initial Rp == R */
 75:   VecCopy(R,RP);

 77:   rhoold   = 1.0;
 78:   alpha    = 1.0;
 79:   omegaold = 1.0;
 80:   VecSet(P,0.0);
 81:   VecSet(V,0.0);

 83:   i=0;
 84:   do {
 85:     VecDot(R,RP,&rho);       /*   rho <- (r,rp)      */
 86:     beta = (rho/rhoold) * (alpha/omegaold);
 87:     VecAXPBYPCZ(P,1.0,-omegaold*beta,beta,R,V);  /* p <- r - omega * beta* v + beta * p */
 88:     KSP_PCApplyBAorAB(ksp,P,V,T);  /*   v <- K p           */
 89:     VecDot(V,RP,&d1);
 90:     if (d1 == 0.0) {
 91:       if (ksp->errorifnotconverged) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged due to Nan or Inf inner product");
 92:       else {
 93:         ksp->reason = KSP_DIVERGED_NANORINF;
 94:         break;
 95:       }
 96:     }
 97:     alpha = rho / d1;                 /*   a <- rho / (v,rp)  */
 98:     VecWAXPY(S,-alpha,V,R);     /*   s <- r - a v       */
 99:     KSP_PCApplyBAorAB(ksp,S,T,R); /*   t <- K s    */
100:     VecDotNorm2(S,T,&d1,&d2);
101:     if (d2 == 0.0) {
102:       /* t is 0.  if s is 0, then alpha v == r, and hence alpha p
103:          may be our solution.  Give it a try? */
104:       VecDot(S,S,&d1);
105:       if (d1 != 0.0) {
106:         ksp->reason = KSP_DIVERGED_BREAKDOWN;
107:         break;
108:       }
109:       VecAXPY(X,alpha,P);   /*   x <- x + a p       */
110:       PetscObjectSAWsTakeAccess((PetscObject)ksp);
111:       ksp->its++;
112:       ksp->rnorm  = 0.0;
113:       ksp->reason = KSP_CONVERGED_RTOL;
114:       PetscObjectSAWsGrantAccess((PetscObject)ksp);
115:       KSPLogResidualHistory(ksp,dp);
116:       KSPMonitor(ksp,i+1,0.0);
117:       break;
118:     }
119:     omega = d1 / d2;                               /*   w <- (t's) / (t't) */
120:     VecAXPBYPCZ(X,alpha,omega,1.0,P,S); /* x <- alpha * p + omega * s + x */
121:     VecWAXPY(R,-omega,T,S);     /*   r <- s - w t       */
122:     if (ksp->normtype != KSP_NORM_NONE && ksp->chknorm < i+2) {
123:       VecNorm(R,NORM_2,&dp);
124:     }

126:     rhoold   = rho;
127:     omegaold = omega;

129:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
130:     ksp->its++;
131:     ksp->rnorm = dp;
132:     PetscObjectSAWsGrantAccess((PetscObject)ksp);
133:     KSPLogResidualHistory(ksp,dp);
134:     KSPMonitor(ksp,i+1,dp);
135:     (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
136:     if (ksp->reason) break;
137:     if (rho == 0.0) {
138:       ksp->reason = KSP_DIVERGED_BREAKDOWN;
139:       break;
140:     }
141:     i++;
142:   } while (i<ksp->max_it);

144:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;

146:   KSPUnwindPreconditioner(ksp,X,T);
147:   if (bcgs->guess) {
148:     VecAXPY(X,1.0,bcgs->guess);
149:   }
150:   return(0);
151: }

155: PetscErrorCode KSPBuildSolution_BCGS(KSP ksp,Vec v,Vec *V)
156: {
158:   KSP_BCGS       *bcgs = (KSP_BCGS*)ksp->data;

161:   if (ksp->pc_side == PC_RIGHT) {
162:     if (v) {
163:       KSP_PCApply(ksp,ksp->vec_sol,v);
164:       if (bcgs->guess) {
165:         VecAXPY(v,1.0,bcgs->guess);
166:       }
167:       *V = v;
168:     } else SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Not working with right preconditioner");
169:   } else {
170:     if (v) {
171:       VecCopy(ksp->vec_sol,v); *V = v;
172:     } else *V = ksp->vec_sol;
173:   }
174:   return(0);
175: }

179: PetscErrorCode KSPReset_BCGS(KSP ksp)
180: {
181:   KSP_BCGS       *cg = (KSP_BCGS*)ksp->data;

185:   VecDestroy(&cg->guess);
186:   return(0);
187: }

191: PetscErrorCode KSPDestroy_BCGS(KSP ksp)
192: {

196:   KSPReset_BCGS(ksp);
197:   KSPDestroyDefault(ksp);
198:   return(0);
199: }

201: /*MC
202:      KSPBCGS - Implements the BiCGStab (Stabilized version of BiConjugate Gradient Squared) method.

204:    Options Database Keys:
205: .   see KSPSolve()

207:    Level: beginner

209:    Notes: See KSPBCGSL for additional stabilization
210:           Supports left and right preconditioning but not symmetric

212:    References: van der Vorst, SIAM J. Sci. Stat. Comput., 1992.

214: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBICG, KSPBCGSL, KSPFBICG, KSPSetPCSide()
215: M*/
218: PETSC_EXTERN PetscErrorCode KSPCreate_BCGS(KSP ksp)
219: {
221:   KSP_BCGS       *bcgs;

224:   PetscNewLog(ksp,&bcgs);

226:   ksp->data                = bcgs;
227:   ksp->ops->setup          = KSPSetUp_BCGS;
228:   ksp->ops->solve          = KSPSolve_BCGS;
229:   ksp->ops->destroy        = KSPDestroy_BCGS;
230:   ksp->ops->reset          = KSPReset_BCGS;
231:   ksp->ops->buildsolution  = KSPBuildSolution_BCGS;
232:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
233:   ksp->ops->setfromoptions = KSPSetFromOptions_BCGS;

235:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,2);
236:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,1);
237:   return(0);
238: }