Actual source code: cheby.c

  1: #define PETSCKSP_DLL

  3: /*
  4:     This is a first attempt at a Chebychev routine, it is not 
  5:     necessarily well optimized.
  6: */
 7:  #include include/private/kspimpl.h
 8:  #include src/ksp/ksp/impls/cheby/chebctx.h

 12: PetscErrorCode KSPSetUp_Chebychev(KSP ksp)
 13: {

 17:   if (ksp->pc_side == PC_SYMMETRIC) SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPCHEBYCHEV");
 18:   KSPDefaultGetWork(ksp,3);
 19:   return(0);
 20: }

 25: PetscErrorCode  KSPChebychevSetEigenvalues_Chebychev(KSP ksp,PetscReal emax,PetscReal emin)
 26: {
 27:   KSP_Chebychev *chebychevP = (KSP_Chebychev*)ksp->data;

 30:   if (emax <= emin) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Maximum eigenvalue must be larger than minimum: max %g min %G",emax,emin);
 31:   if (emax*emin <= 0.0) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Both eigenvalues must be of the same sign: max %G min %G",emax,emin);
 32:   chebychevP->emax = emax;
 33:   chebychevP->emin = emin;
 34:   return(0);
 35: }

 40: /*@
 41:    KSPChebychevSetEigenvalues - Sets estimates for the extreme eigenvalues
 42:    of the preconditioned problem.

 44:    Collective on KSP

 46:    Input Parameters:
 47: +  ksp - the Krylov space context
 48: -  emax, emin - the eigenvalue estimates

 50:   Options Database:
 51: .  -ksp_chebychev_eigenvalues emin,emax

 53:    Level: intermediate

 55: .keywords: KSP, Chebyshev, set, eigenvalues
 56: @*/
 57: PetscErrorCode  KSPChebychevSetEigenvalues(KSP ksp,PetscReal emax,PetscReal emin)
 58: {
 59:   PetscErrorCode ierr,(*f)(KSP,PetscReal,PetscReal);

 63:   PetscObjectQueryFunction((PetscObject)ksp,"KSPChebychevSetEigenvalues_C",(void (**)(void))&f);
 64:   if (f) {
 65:     (*f)(ksp,emax,emin);
 66:   }
 67:   return(0);
 68: }

 72: PetscErrorCode KSPSetFromOptions_Chebychev(KSP ksp)
 73: {
 74:   KSP_Chebychev *cheb = (KSP_Chebychev*)ksp->data;
 76:   PetscInt       two = 2;

 79:   PetscOptionsHead("KSP Chebychev Options");
 80:     PetscOptionsRealArray("-ksp_chebychev_eigenvalues","extreme eigenvalues","KSPChebychevSetEigenvalues",&cheb->emin,&two,0);
 81:   PetscOptionsTail();
 82:   return(0);
 83: }

 87: PetscErrorCode KSPSolve_Chebychev(KSP ksp)
 88: {
 90:   PetscInt       k,kp1,km1,maxit,ktmp,i;
 91:   PetscScalar    alpha,omegaprod,mu,omega,Gamma,c[3],scale;
 92:   PetscReal      rnorm = 0.0;
 93:   Vec            x,b,p[3],r;
 94:   KSP_Chebychev  *chebychevP = (KSP_Chebychev*)ksp->data;
 95:   Mat            Amat,Pmat;
 96:   MatStructure   pflag;
 97:   PetscTruth     diagonalscale;

100:   PCDiagonalScale(ksp->pc,&diagonalscale);
101:   if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",ksp->type_name);

103:   ksp->its = 0;
104:   PCGetOperators(ksp->pc,&Amat,&Pmat,&pflag);
105:   maxit    = ksp->max_it;

107:   /* These three point to the three active solutions, we
108:      rotate these three at each solution update */
109:   km1    = 0; k = 1; kp1 = 2;
110:   x      = ksp->vec_sol;
111:   b      = ksp->vec_rhs;
112:   p[km1] = x;
113:   p[k]   = ksp->work[0];
114:   p[kp1] = ksp->work[1];
115:   r      = ksp->work[2];

117:   /* use scale*B as our preconditioner */
118:   scale  = 2.0/(chebychevP->emax + chebychevP->emin);

120:   /*   -alpha <=  scale*lambda(B^{-1}A) <= alpha   */
121:   alpha  = 1.0 - scale*(chebychevP->emin); ;
122:   Gamma  = 1.0;
123:   mu     = 1.0/alpha;
124:   omegaprod = 2.0/alpha;

126:   c[km1] = 1.0;
127:   c[k]   = mu;

129:   if (!ksp->guess_zero) {
130:     KSP_MatMult(ksp,Amat,x,r);     /*  r = b - Ax     */
131:     VecAYPX(r,-1.0,b);
132:   } else {
133:     VecCopy(b,r);
134:   }
135: 
136:   KSP_PCApply(ksp,r,p[k]);  /* p[k] = scale B^{-1}r + x */
137:   VecAYPX(p[k],scale,x);

139:   for (i=0; i<maxit; i++) {
140:     PetscObjectTakeAccess(ksp);
141:     ksp->its++;
142:     PetscObjectGrantAccess(ksp);
143:     c[kp1] = 2.0*mu*c[k] - c[km1];
144:     omega = omegaprod*c[k]/c[kp1];

146:     KSP_MatMult(ksp,Amat,p[k],r);                 /*  r = b - Ap[k]    */
147:     VecAYPX(r,-1.0,b);
148:     KSP_PCApply(ksp,r,p[kp1]);             /*  p[kp1] = B^{-1}z  */

150:     /* calculate residual norm if requested */
151:     if (ksp->normtype != KSP_NORM_NO) {
152:       if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {VecNorm(r,NORM_2,&rnorm);}
153:       else {VecNorm(p[kp1],NORM_2,&rnorm);}
154:       PetscObjectTakeAccess(ksp);
155:       ksp->rnorm                              = rnorm;
156:       PetscObjectGrantAccess(ksp);
157:       ksp->vec_sol = p[k];
158:       KSPLogResidualHistory(ksp,rnorm);
159:       KSPMonitor(ksp,i,rnorm);
160:       (*ksp->converged)(ksp,i,rnorm,&ksp->reason,ksp->cnvP);
161:       if (ksp->reason) break;
162:     }

164:     /* y^{k+1} = omega(y^{k} - y^{k-1} + Gamma*r^{k}) + y^{k-1} */
165:     VecScale(p[kp1],omega*Gamma*scale);
166:     VecAXPY(p[kp1],1.0-omega,p[km1]);
167:     VecAXPY(p[kp1],omega,p[k]);

169:     ktmp = km1;
170:     km1  = k;
171:     k    = kp1;
172:     kp1  = ktmp;
173:   }
174:   if (!ksp->reason) {
175:     if (ksp->normtype != KSP_NORM_NO) {
176:       KSP_MatMult(ksp,Amat,p[k],r);       /*  r = b - Ap[k]    */
177:       VecAYPX(r,-1.0,b);
178:       if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
179:         VecNorm(r,NORM_2,&rnorm);
180:       } else {
181:         KSP_PCApply(ksp,r,p[kp1]); /* p[kp1] = B^{-1}z */
182:         VecNorm(p[kp1],NORM_2,&rnorm);
183:       }
184:       PetscObjectTakeAccess(ksp);
185:       ksp->rnorm = rnorm;
186:       PetscObjectGrantAccess(ksp);
187:       ksp->vec_sol = p[k];
188:       KSPLogResidualHistory(ksp,rnorm);
189:       KSPMonitor(ksp,i,rnorm);
190:     }
191:     if (ksp->its >= ksp->max_it) {
192:       if (ksp->normtype != KSP_NORM_NO) {
193:         (*ksp->converged)(ksp,i,rnorm,&ksp->reason,ksp->cnvP);
194:         if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
195:       } else {
196:         ksp->reason = KSP_CONVERGED_ITS;
197:       }
198:     }
199:   }

201:   /* make sure solution is in vector x */
202:   ksp->vec_sol = x;
203:   if (k) {
204:     VecCopy(p[k],x);
205:   }
206:   return(0);
207: }

211: PetscErrorCode KSPView_Chebychev(KSP ksp,PetscViewer viewer)
212: {
213:   KSP_Chebychev  *cheb = (KSP_Chebychev*)ksp->data;
215:   PetscTruth     iascii;

218:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
219:   if (iascii) {
220:     PetscViewerASCIIPrintf(viewer,"  Chebychev: eigenvalue estimates:  min = %G, max = %G\n",cheb->emin,cheb->emax);
221:   } else {
222:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP Chebychev",((PetscObject)viewer)->type_name);
223:   }
224:   return(0);
225: }

229: PetscErrorCode KSPDestroy_Chebychev(KSP ksp)
230: {

234:   KSPDefaultDestroy(ksp);
235:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPChebychevSetEigenvalues_C","",PETSC_NULL);
236:   return(0);
237: }

239: /*MC
240:      KSPCHEBYCHEV - The preconditioned Chebychev iterative method

242:    Options Database Keys:
243: .   -ksp_chebychev_eigenvalues <emin,emax> - set approximations to the smallest and largest eigenvalues
244:                   of the preconditioned operator. If these are accurate you will get much faster convergence.

246:    Level: beginner

248:    Notes: The Chebychev method requires both the matrix and preconditioner to 
249:           be symmetric positive (semi) definite

251: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
252:            KSPChebychevSetEigenvalues()

254: M*/

259: PetscErrorCode  KSPCreate_Chebychev(KSP ksp)
260: {
262:   KSP_Chebychev  *chebychevP;

265:   PetscNew(KSP_Chebychev,&chebychevP);
266:   PetscLogObjectMemory(ksp,sizeof(KSP_Chebychev));

268:   ksp->data                      = (void*)chebychevP;
269:   ksp->pc_side                   = PC_LEFT;

271:   chebychevP->emin               = 1.e-2;
272:   chebychevP->emax               = 1.e+2;

274:   ksp->ops->setup                = KSPSetUp_Chebychev;
275:   ksp->ops->solve                = KSPSolve_Chebychev;
276:   ksp->ops->destroy              = KSPDestroy_Chebychev;
277:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
278:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;
279:   ksp->ops->setfromoptions       = KSPSetFromOptions_Chebychev;
280:   ksp->ops->view                 = KSPView_Chebychev;

282:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPChebychevSetEigenvalues_C",
283:                                     "KSPChebychevSetEigenvalues_Chebychev",
284:                                     KSPChebychevSetEigenvalues_Chebychev);
285:   return(0);
286: }