Actual source code: cheby.c

petsc-master 2019-06-15
Report Typos and Errors

  2:  #include <../src/ksp/ksp/impls/cheby/chebyshevimpl.h>

  4: static PetscErrorCode KSPReset_Chebyshev(KSP ksp)
  5: {
  6:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;

 10:   KSPReset(cheb->kspest);
 11:   return(0);
 12: }

 14: static PetscErrorCode KSPSetUp_Chebyshev(KSP ksp)
 15: {
 16:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;

 20:   KSPSetWorkVecs(ksp,3);
 21:   if ((cheb->emin == 0. || cheb->emax == 0.) && !cheb->kspest) { /* We need to estimate eigenvalues */
 22:     KSPChebyshevEstEigSet(ksp,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);
 23:   }
 24:   return(0);
 25: }

 27: static PetscErrorCode KSPChebyshevSetEigenvalues_Chebyshev(KSP ksp,PetscReal emax,PetscReal emin)
 28: {
 29:   KSP_Chebyshev  *chebyshevP = (KSP_Chebyshev*)ksp->data;

 33:   if (emax <= emin) SETERRQ2(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_INCOMP,"Maximum eigenvalue must be larger than minimum: max %g min %g",(double)emax,(double)emin);
 34:   if (emax*emin <= 0.0) SETERRQ2(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_INCOMP,"Both eigenvalues must be of the same sign: max %g min %g",(double)emax,(double)emin);
 35:   chebyshevP->emax = emax;
 36:   chebyshevP->emin = emin;

 38:   KSPChebyshevEstEigSet(ksp,0.,0.,0.,0.); /* Destroy any estimation setup */
 39:   return(0);
 40: }

 42: static PetscErrorCode KSPChebyshevEstEigSet_Chebyshev(KSP ksp,PetscReal a,PetscReal b,PetscReal c,PetscReal d)
 43: {
 44:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;

 48:   if (a != 0.0 || b != 0.0 || c != 0.0 || d != 0.0) {
 49:     if (!cheb->kspest) { /* should this block of code be moved to KSPSetUp_Chebyshev()? */
 50:       KSPCreate(PetscObjectComm((PetscObject)ksp),&cheb->kspest);
 51:       KSPSetErrorIfNotConverged(cheb->kspest,ksp->errorifnotconverged);
 52:       PetscObjectIncrementTabLevel((PetscObject)cheb->kspest,(PetscObject)ksp,1);
 53:       KSPSetPC(cheb->kspest,ksp->pc);
 54:       /* use PetscObjectSet/AppendOptionsPrefix() instead of KSPSet/AppendOptionsPrefix() so that the PC prefix is not changed */
 55:       PetscObjectSetOptionsPrefix((PetscObject)cheb->kspest,((PetscObject)ksp)->prefix);
 56:       PetscObjectAppendOptionsPrefix((PetscObject)cheb->kspest,"esteig_");
 57:       KSPSetSkipPCSetFromOptions(cheb->kspest,PETSC_TRUE);

 59:       KSPSetComputeEigenvalues(cheb->kspest,PETSC_TRUE);

 61:       /* We cannot turn off convergence testing because GMRES will break down if you attempt to keep iterating after a zero norm is obtained */
 62:       KSPSetTolerances(cheb->kspest,1.e-12,PETSC_DEFAULT,PETSC_DEFAULT,cheb->eststeps);
 63:     }
 64:     if (a >= 0) cheb->tform[0] = a;
 65:     if (b >= 0) cheb->tform[1] = b;
 66:     if (c >= 0) cheb->tform[2] = c;
 67:     if (d >= 0) cheb->tform[3] = d;
 68:     cheb->amatid    = 0;
 69:     cheb->pmatid    = 0;
 70:     cheb->amatstate = -1;
 71:     cheb->pmatstate = -1;
 72:   } else {
 73:     KSPDestroy(&cheb->kspest);
 74:   }
 75:   return(0);
 76: }

 78: static PetscErrorCode KSPChebyshevEstEigSetUseNoisy_Chebyshev(KSP ksp,PetscBool use)
 79: {
 80:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;

 83:   cheb->usenoisy = use;
 84:   return(0);
 85: }

 87: /*@
 88:    KSPChebyshevSetEigenvalues - Sets estimates for the extreme eigenvalues
 89:    of the preconditioned problem.

 91:    Logically Collective on ksp

 93:    Input Parameters:
 94: +  ksp - the Krylov space context
 95: -  emax, emin - the eigenvalue estimates

 97:   Options Database:
 98: .  -ksp_chebyshev_eigenvalues emin,emax

100:    Note: Call KSPChebyshevEstEigSet() or use the option -ksp_chebyshev_esteig a,b,c,d to have the KSP
101:          estimate the eigenvalues and use these estimated values automatically

103:    Level: intermediate

105: @*/
106: PetscErrorCode  KSPChebyshevSetEigenvalues(KSP ksp,PetscReal emax,PetscReal emin)
107: {

114:   PetscTryMethod(ksp,"KSPChebyshevSetEigenvalues_C",(KSP,PetscReal,PetscReal),(ksp,emax,emin));
115:   return(0);
116: }

118: /*@
119:    KSPChebyshevEstEigSet - Automatically estimate the eigenvalues to use for Chebyshev

121:    Logically Collective on ksp

123:    Input Parameters:
124: +  ksp - the Krylov space context
125: .  a - multiple of min eigenvalue estimate to use for min Chebyshev bound (or PETSC_DECIDE)
126: .  b - multiple of max eigenvalue estimate to use for min Chebyshev bound (or PETSC_DECIDE)
127: .  c - multiple of min eigenvalue estimate to use for max Chebyshev bound (or PETSC_DECIDE)
128: -  d - multiple of max eigenvalue estimate to use for max Chebyshev bound (or PETSC_DECIDE)

130:   Options Database:
131: .  -ksp_chebyshev_esteig a,b,c,d

133:    Notes:
134:    The Chebyshev bounds are set using
135: .vb
136:    minbound = a*minest + b*maxest
137:    maxbound = c*minest + d*maxest
138: .ve
139:    The default configuration targets the upper part of the spectrum for use as a multigrid smoother, so only the maximum eigenvalue estimate is used.
140:    The minimum eigenvalue estimate obtained by Krylov iteration is typically not accurate until the method has converged.

142:    If 0.0 is passed for all transform arguments (a,b,c,d), eigenvalue estimation is disabled.

144:    The default transform is (0,0.1; 0,1.1) which targets the "upper" part of the spectrum, as desirable for use with multigrid.

146:    The eigenvalues are estimated using the Lanczo (KSPCG) or Arnoldi (KSPGMRES) process using a noisy right hand side vector.

148:    Level: intermediate

150: @*/
151: PetscErrorCode KSPChebyshevEstEigSet(KSP ksp,PetscReal a,PetscReal b,PetscReal c,PetscReal d)
152: {

161:   PetscTryMethod(ksp,"KSPChebyshevEstEigSet_C",(KSP,PetscReal,PetscReal,PetscReal,PetscReal),(ksp,a,b,c,d));
162:   return(0);
163: }

165: /*@
166:    KSPChebyshevEstEigSetUseNoisy - use a noisy right hand side in order to do the estimate instead of the given right hand side

168:    Logically Collective

170:    Input Arguments:
171: +  ksp - linear solver context
172: -  use - PETSC_TRUE to use noisy

174:    Options Database:
175: +  -ksp_chebyshev_esteig_noisy <true,false>

177:   Notes:
178:     This alledgely works better for multigrid smoothers

180:   Level: intermediate

182: .seealso: KSPChebyshevEstEigSet()
183: @*/
184: PetscErrorCode KSPChebyshevEstEigSetUseNoisy(KSP ksp,PetscBool use)
185: {

190:   PetscTryMethod(ksp,"KSPChebyshevEstEigSetUseNoisy_C",(KSP,PetscBool),(ksp,use));
191:   return(0);
192: }

194: /*@
195:   KSPChebyshevEstEigGetKSP - Get the Krylov method context used to estimate eigenvalues for the Chebyshev method.  If
196:   a Krylov method is not being used for this purpose, NULL is returned.  The reference count of the returned KSP is
197:   not incremented: it should not be destroyed by the user.

199:   Input Parameters:
200: . ksp - the Krylov space context

202:   Output Parameters:
203: . kspest the eigenvalue estimation Krylov space context

205:   Level: intermediate

207: .seealso: KSPChebyshevEstEigSet()
208: @*/
209: PetscErrorCode KSPChebyshevEstEigGetKSP(KSP ksp, KSP *kspest)
210: {

215:   *kspest = NULL;
216:   PetscTryMethod(ksp,"KSPChebyshevEstEigGetKSP_C",(KSP,KSP*),(ksp,kspest));
217:   return(0);
218: }

220: static PetscErrorCode KSPChebyshevEstEigGetKSP_Chebyshev(KSP ksp, KSP *kspest)
221: {
222:   KSP_Chebyshev *cheb = (KSP_Chebyshev*)ksp->data;

225:   *kspest = cheb->kspest;
226:   return(0);
227: }

229: static PetscErrorCode KSPSetFromOptions_Chebyshev(PetscOptionItems *PetscOptionsObject,KSP ksp)
230: {
231:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;
233:   PetscInt       neigarg = 2, nestarg = 4;
234:   PetscReal      eminmax[2] = {0., 0.};
235:   PetscReal      tform[4] = {PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE};
236:   PetscBool      flgeig, flgest;

239:   PetscOptionsHead(PetscOptionsObject,"KSP Chebyshev Options");
240:   PetscOptionsInt("-ksp_chebyshev_esteig_steps","Number of est steps in Chebyshev","",cheb->eststeps,&cheb->eststeps,NULL);
241:   PetscOptionsRealArray("-ksp_chebyshev_eigenvalues","extreme eigenvalues","KSPChebyshevSetEigenvalues",eminmax,&neigarg,&flgeig);
242:   if (flgeig) {
243:     if (neigarg != 2) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_INCOMP,"-ksp_chebyshev_eigenvalues: must specify 2 parameters, min and max eigenvalues");
244:     KSPChebyshevSetEigenvalues(ksp, eminmax[1], eminmax[0]);
245:   }
246:   PetscOptionsRealArray("-ksp_chebyshev_esteig","estimate eigenvalues using a Krylov method, then use this transform for Chebyshev eigenvalue bounds","KSPChebyshevEstEigSet",tform,&nestarg,&flgest);
247:   if (flgest) {
248:     switch (nestarg) {
249:     case 0:
250:       KSPChebyshevEstEigSet(ksp,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);
251:       break;
252:     case 2:                     /* Base everything on the max eigenvalues */
253:       KSPChebyshevEstEigSet(ksp,PETSC_DECIDE,tform[0],PETSC_DECIDE,tform[1]);
254:       break;
255:     case 4:                     /* Use the full 2x2 linear transformation */
256:       KSPChebyshevEstEigSet(ksp,tform[0],tform[1],tform[2],tform[3]);
257:       break;
258:     default: SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_INCOMP,"Must specify either 0, 2, or 4 parameters for eigenvalue estimation");
259:     }
260:   }

262:   /* We need to estimate eigenvalues; need to set this here so that KSPSetFromOptions() is called on the estimator */
263:   if ((cheb->emin == 0. || cheb->emax == 0.) && !cheb->kspest) {
264:    KSPChebyshevEstEigSet(ksp,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);
265:   }

267:   if (cheb->kspest) {
268:     PetscOptionsBool("-ksp_chebyshev_esteig_noisy","Use noisy right hand side for estimate","KSPChebyshevEstEigSetUseNoisy",cheb->usenoisy,&cheb->usenoisy,NULL);
269:     KSPSetFromOptions(cheb->kspest);
270:   }
271:   PetscOptionsTail();
272:   return(0);
273: }

275: /*
276:  * Must be passed a KSP solver that has "converged", with KSPSetComputeEigenvalues() called before the solve
277:  */
278: static PetscErrorCode KSPChebyshevComputeExtremeEigenvalues_Private(KSP kspest,PetscReal *emin,PetscReal *emax)
279: {
281:   PetscInt       n,neig;
282:   PetscReal      *re,*im,min,max;

285:   KSPGetIterationNumber(kspest,&n);
286:   PetscMalloc2(n,&re,n,&im);
287:   KSPComputeEigenvalues(kspest,n,re,im,&neig);
288:   min  = PETSC_MAX_REAL;
289:   max  = PETSC_MIN_REAL;
290:   for (n=0; n<neig; n++) {
291:     min = PetscMin(min,re[n]);
292:     max = PetscMax(max,re[n]);
293:   }
294:   PetscFree2(re,im);
295:   *emax = max;
296:   *emin = min;
297:   return(0);
298: }

300: PETSC_STATIC_INLINE PetscScalar chebyhash(PetscInt xx)
301: {
302:   unsigned int x = xx;
303:   x = ((x >> 16) ^ x) * 0x45d9f3b;
304:   x = ((x >> 16) ^ x) * 0x45d9f3b;
305:   x = ((x >> 16) ^ x);
306:   return (PetscScalar)((PetscInt64)x-2147483648)*5.e-10; /* center around zero, scaled about -1. to 1.*/
307: }

309: static PetscErrorCode KSPSolve_Chebyshev(KSP ksp)
310: {
311:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;
313:   PetscInt       k,kp1,km1,ktmp,i;
314:   PetscScalar    alpha,omegaprod,mu,omega,Gamma,c[3],scale;
315:   PetscReal      rnorm = 0.0;
316:   Vec            sol_orig,b,p[3],r;
317:   Mat            Amat,Pmat;
318:   PetscBool      diagonalscale;

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

324:   PCGetOperators(ksp->pc,&Amat,&Pmat);
325:   if (cheb->kspest) {
326:     PetscObjectId    amatid,    pmatid;
327:     PetscObjectState amatstate, pmatstate;
328:     PCFailedReason   pcreason;
329:     PC               pc;

331:     PetscObjectGetId((PetscObject)Amat,&amatid);
332:     PetscObjectGetId((PetscObject)Pmat,&pmatid);
333:     PetscObjectStateGet((PetscObject)Amat,&amatstate);
334:     PetscObjectStateGet((PetscObject)Pmat,&pmatstate);
335:     if (amatid != cheb->amatid || pmatid != cheb->pmatid || amatstate != cheb->amatstate || pmatstate != cheb->pmatstate) {
336:       PetscReal          max=0.0,min=0.0;
337:       Vec                B;
338:       KSPConvergedReason reason;

340:       if (cheb->usenoisy) {
341:         B  = ksp->work[1];
342:         {
344:           PetscInt       n,i,istart;
345:           PetscScalar    *xx;
346:           VecGetOwnershipRange(B,&istart,NULL);
347:           VecGetLocalSize(B,&n);
348:           VecGetArray(B,&xx);
349:           for (i=0; i<n; i++) {
350:             PetscScalar v = chebyhash(i+istart);
351:             xx[i] = v;
352:           }
353:           VecRestoreArray(B,&xx);
354:         }
355:       } else {
356:         PC        pc;
357:         PetscBool change;

359:         KSPGetPC(cheb->kspest,&pc);
360:         PCPreSolveChangeRHS(pc,&change);
361:         if (change) {
362:           B = ksp->work[1];
363:           VecCopy(ksp->vec_rhs,B);
364:         } else {
365:           B = ksp->vec_rhs;
366:         }
367:       }
368:       KSPSolve(cheb->kspest,B,ksp->work[0]);
369:       KSPGetConvergedReason(cheb->kspest,&reason);
370:       KSPGetPC(cheb->kspest,&pc);
371:       PCGetFailedReason(pc,&pcreason);
372:       if (reason < 0 || pcreason) {
373:         if (reason == KSP_DIVERGED_ITS) {
374:           PetscInfo(ksp,"Eigen estimator ran for prescribed number of iterations\n");
375:         } else {
376:           PetscInt its;
377:           KSPGetIterationNumber(cheb->kspest,&its);
378:           ksp->reason = KSP_DIVERGED_PC_FAILED;
379:           VecSetInf(ksp->vec_sol);
380:           PetscInfo3(ksp,"Eigen estimator failed: %s %s at iteration %D",KSPConvergedReasons[reason],PCFailedReasons[pcreason],its);
381:           return(0);
382:         }
383:       } else if (reason==KSP_CONVERGED_RTOL ||reason==KSP_CONVERGED_ATOL) {
384:         PetscInfo(ksp,"Eigen estimator converged prematurely. Should not happen except for small or low rank problem\n");
385:       } else {
386:         PetscInfo1(ksp,"Eigen estimator did not converge by iteration: %s\n",KSPConvergedReasons[reason]);
387:       }

389:       KSPChebyshevComputeExtremeEigenvalues_Private(cheb->kspest,&min,&max);

391:       cheb->emin_computed = min;
392:       cheb->emax_computed = max;
393:       cheb->emin = cheb->tform[0]*min + cheb->tform[1]*max;
394:       cheb->emax = cheb->tform[2]*min + cheb->tform[3]*max;

396:       cheb->amatid    = amatid;
397:       cheb->pmatid    = pmatid;
398:       cheb->amatstate = amatstate;
399:       cheb->pmatstate = pmatstate;
400:     }
401:   }
402:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
403:   ksp->its = 0;
404:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
405:   /* These three point to the three active solutions, we
406:      rotate these three at each solution update */
407:   km1      = 0; k = 1; kp1 = 2;
408:   sol_orig = ksp->vec_sol; /* ksp->vec_sol will be asigned to rotating vector p[k], thus save its address */
409:   b        = ksp->vec_rhs;
410:   p[km1]   = sol_orig;
411:   p[k]     = ksp->work[0];
412:   p[kp1]   = ksp->work[1];
413:   r        = ksp->work[2];

415:   /* use scale*B as our preconditioner */
416:   scale = 2.0/(cheb->emax + cheb->emin);

418:   /*   -alpha <=  scale*lambda(B^{-1}A) <= alpha   */
419:   alpha     = 1.0 - scale*(cheb->emin);
420:   Gamma     = 1.0;
421:   mu        = 1.0/alpha;
422:   omegaprod = 2.0/alpha;

424:   c[km1] = 1.0;
425:   c[k]   = mu;

427:   if (!ksp->guess_zero) {
428:     KSP_MatMult(ksp,Amat,sol_orig,r);     /*  r = b - A*p[km1] */
429:     VecAYPX(r,-1.0,b);
430:   } else {
431:     VecCopy(b,r);
432:   }

434:   /* calculate residual norm if requested, we have done one iteration */
435:   if (ksp->normtype) {
436:     switch (ksp->normtype) {
437:     case KSP_NORM_PRECONDITIONED:
438:       KSP_PCApply(ksp,r,p[k]);  /* p[k] = B^{-1}r */
439:       VecNorm(p[k],NORM_2,&rnorm);
440:       break;
441:     case KSP_NORM_UNPRECONDITIONED:
442:     case KSP_NORM_NATURAL:
443:       VecNorm(r,NORM_2,&rnorm);
444:       break;
445:     case KSP_NORM_NONE:
446:       rnorm = 0.0;
447:       break;
448:     default: SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"%s",KSPNormTypes[ksp->normtype]);
449:     }
450:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
451:     ksp->rnorm   = rnorm;
452:     PetscObjectSAWsGrantAccess((PetscObject)ksp);
453:     KSPLogResidualHistory(ksp,rnorm);
454:     KSPMonitor(ksp,0,rnorm);
455:     (*ksp->converged)(ksp,0,rnorm,&ksp->reason,ksp->cnvP);
456:   }
457:   else ksp->reason = KSP_CONVERGED_ITERATING;
458:   if (ksp->reason || ksp->max_it==0) {
459:     if (ksp->max_it==0) ksp->reason = KSP_DIVERGED_ITS; /* This for a V(0,x) cycle */
460:     return(0);
461:   }
462:   if (ksp->normtype != KSP_NORM_PRECONDITIONED) {
463:     KSP_PCApply(ksp,r,p[k]);  /* p[k] = B^{-1}r */
464:   }
465:   VecAYPX(p[k],scale,p[km1]);  /* p[k] = scale B^{-1}r + p[km1] */
466:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
467:   ksp->its = 1;
468:   PetscObjectSAWsGrantAccess((PetscObject)ksp);

470:   for (i=1; i<ksp->max_it; i++) {
471:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
472:     ksp->its++;
473:     PetscObjectSAWsGrantAccess((PetscObject)ksp);

475:     KSP_MatMult(ksp,Amat,p[k],r);          /*  r = b - Ap[k]    */
476:     VecAYPX(r,-1.0,b);
477:     /* calculate residual norm if requested */
478:     if (ksp->normtype) {
479:       switch (ksp->normtype) {
480:       case KSP_NORM_PRECONDITIONED:
481:         KSP_PCApply(ksp,r,p[kp1]);             /*  p[kp1] = B^{-1}r  */
482:         VecNorm(p[kp1],NORM_2,&rnorm);
483:         break;
484:       case KSP_NORM_UNPRECONDITIONED:
485:       case KSP_NORM_NATURAL:
486:         VecNorm(r,NORM_2,&rnorm);
487:         break;
488:       default:
489:         rnorm = 0.0;
490:         break;
491:       }
492:       KSPCheckNorm(ksp,rnorm);
493:       PetscObjectSAWsTakeAccess((PetscObject)ksp);
494:       ksp->rnorm   = rnorm;
495:       PetscObjectSAWsGrantAccess((PetscObject)ksp);
496:       KSPLogResidualHistory(ksp,rnorm);
497:       KSPMonitor(ksp,i,rnorm);
498:       (*ksp->converged)(ksp,i,rnorm,&ksp->reason,ksp->cnvP);
499:       if (ksp->reason) break;
500:       if (ksp->normtype != KSP_NORM_PRECONDITIONED) {
501:         KSP_PCApply(ksp,r,p[kp1]);             /*  p[kp1] = B^{-1}r  */
502:       }
503:     } else {
504:       KSP_PCApply(ksp,r,p[kp1]);             /*  p[kp1] = B^{-1}r  */
505:     }
506:     ksp->vec_sol = p[k];

508:     c[kp1] = 2.0*mu*c[k] - c[km1];
509:     omega  = omegaprod*c[k]/c[kp1];

511:     /* y^{k+1} = omega(y^{k} - y^{k-1} + Gamma*r^{k}) + y^{k-1} */
512:     VecAXPBYPCZ(p[kp1],1.0-omega,omega,omega*Gamma*scale,p[km1],p[k]);

514:     ktmp = km1;
515:     km1  = k;
516:     k    = kp1;
517:     kp1  = ktmp;
518:   }
519:   if (!ksp->reason) {
520:     if (ksp->normtype) {
521:       KSP_MatMult(ksp,Amat,p[k],r);       /*  r = b - Ap[k]    */
522:       VecAYPX(r,-1.0,b);
523:       switch (ksp->normtype) {
524:       case KSP_NORM_PRECONDITIONED:
525:         KSP_PCApply(ksp,r,p[kp1]); /* p[kp1] = B^{-1}r */
526:         VecNorm(p[kp1],NORM_2,&rnorm);
527:         break;
528:       case KSP_NORM_UNPRECONDITIONED:
529:       case KSP_NORM_NATURAL:
530:         VecNorm(r,NORM_2,&rnorm);
531:         break;
532:       default:
533:         rnorm = 0.0;
534:         break;
535:       }
536:       KSPCheckNorm(ksp,rnorm);
537:       PetscObjectSAWsTakeAccess((PetscObject)ksp);
538:       ksp->rnorm   = rnorm;
539:       PetscObjectSAWsGrantAccess((PetscObject)ksp);
540:       KSPLogResidualHistory(ksp,rnorm);
541:       KSPMonitor(ksp,i,rnorm);
542:     }
543:     if (ksp->its >= ksp->max_it) {
544:       if (ksp->normtype != KSP_NORM_NONE) {
545:         (*ksp->converged)(ksp,i,rnorm,&ksp->reason,ksp->cnvP);
546:         if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
547:       } else ksp->reason = KSP_CONVERGED_ITS;
548:     }
549:   }

551:   /* make sure solution is in vector x */
552:   ksp->vec_sol = sol_orig;
553:   if (k) {
554:     VecCopy(p[k],sol_orig);
555:   }
556:   return(0);
557: }

559: static  PetscErrorCode KSPView_Chebyshev(KSP ksp,PetscViewer viewer)
560: {
561:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;
563:   PetscBool      iascii;

566:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
567:   if (iascii) {
568:     PetscViewerASCIIPrintf(viewer,"  eigenvalue estimates used:  min = %g, max = %g\n",(double)cheb->emin,(double)cheb->emax);
569:     if (cheb->kspest) {
570:       PetscViewerASCIIPrintf(viewer,"  eigenvalues estimate via %s min %g, max %g\n",((PetscObject)(cheb->kspest))->type_name,(double)cheb->emin_computed,(double)cheb->emax_computed);
571:       PetscViewerASCIIPrintf(viewer,"  eigenvalues estimated using %s with translations  [%g %g; %g %g]\n",((PetscObject) cheb->kspest)->type_name,(double)cheb->tform[0],(double)cheb->tform[1],(double)cheb->tform[2],(double)cheb->tform[3]);
572:       PetscViewerASCIIPushTab(viewer);
573:       KSPView(cheb->kspest,viewer);
574:       PetscViewerASCIIPopTab(viewer);
575:       if (cheb->usenoisy) {
576:         PetscViewerASCIIPrintf(viewer,"  estimating eigenvalues using noisy right hand side\n");
577:       }
578:     }
579:   }
580:   return(0);
581: }

583: static PetscErrorCode KSPDestroy_Chebyshev(KSP ksp)
584: {
585:   KSP_Chebyshev  *cheb = (KSP_Chebyshev*)ksp->data;

589:   KSPDestroy(&cheb->kspest);
590:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevSetEigenvalues_C",NULL);
591:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevEstEigSet_C",NULL);
592:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevEstEigSetUseNoisy_C",NULL);
593:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevEstEigGetKSP_C",NULL);
594:   KSPDestroyDefault(ksp);
595:   return(0);
596: }

598: /*MC
599:      KSPCHEBYSHEV - The preconditioned Chebyshev iterative method

601:    Options Database Keys:
602: +   -ksp_chebyshev_eigenvalues <emin,emax> - set approximations to the smallest and largest eigenvalues
603:                   of the preconditioned operator. If these are accurate you will get much faster convergence.
604: .   -ksp_chebyshev_esteig <a,b,c,d> - estimate eigenvalues using a Krylov method, then use this
605:                          transform for Chebyshev eigenvalue bounds (KSPChebyshevEstEigSet())
606: .   -ksp_chebyshev_esteig_steps - number of estimation steps
607: -   -ksp_chebyshev_esteig_noisy - use noisy number generator to create right hand side for eigenvalue estimator

609:    Level: beginner

611:    Notes:
612:     The Chebyshev method requires both the matrix and preconditioner to
613:           be symmetric positive (semi) definite.
614:           Only support for left preconditioning.

616:           Chebyshev is configured as a smoother by default, targetting the "upper" part of the spectrum.
617:           The user should call KSPChebyshevSetEigenvalues() if they have eigenvalue estimates.

619: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
620:            KSPChebyshevSetEigenvalues(), KSPChebyshevEstEigSet(), KSPChebyshevEstEigSetUseNoisy()
621:            KSPRICHARDSON, KSPCG, PCMG

623: M*/

625: PETSC_EXTERN PetscErrorCode KSPCreate_Chebyshev(KSP ksp)
626: {
628:   KSP_Chebyshev  *chebyshevP;

631:   PetscNewLog(ksp,&chebyshevP);

633:   ksp->data = (void*)chebyshevP;
634:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
635:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_LEFT,2);
636:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);
637:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_RIGHT,1);

639:   chebyshevP->emin = 0.;
640:   chebyshevP->emax = 0.;

642:   chebyshevP->tform[0] = 0.0;
643:   chebyshevP->tform[1] = 0.1;
644:   chebyshevP->tform[2] = 0;
645:   chebyshevP->tform[3] = 1.1;
646:   chebyshevP->eststeps = 10;
647:   chebyshevP->usenoisy = PETSC_TRUE;

649:   ksp->ops->setup          = KSPSetUp_Chebyshev;
650:   ksp->ops->solve          = KSPSolve_Chebyshev;
651:   ksp->ops->destroy        = KSPDestroy_Chebyshev;
652:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
653:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
654:   ksp->ops->setfromoptions = KSPSetFromOptions_Chebyshev;
655:   ksp->ops->view           = KSPView_Chebyshev;
656:   ksp->ops->reset          = KSPReset_Chebyshev;

658:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevSetEigenvalues_C",KSPChebyshevSetEigenvalues_Chebyshev);
659:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevEstEigSet_C",KSPChebyshevEstEigSet_Chebyshev);
660:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevEstEigSetUseNoisy_C",KSPChebyshevEstEigSetUseNoisy_Chebyshev);
661:   PetscObjectComposeFunction((PetscObject)ksp,"KSPChebyshevEstEigGetKSP_C",KSPChebyshevEstEigGetKSP_Chebyshev);
662:   return(0);
663: }