Actual source code: fgmres.c


  2: /*
  3:     This file implements FGMRES (a Generalized Minimal Residual) method.
  4:     Reference:  Saad, 1993.

  6:     Preconditioning:  If the preconditioner is constant then this fgmres
  7:     code is equivalent to RIGHT-PRECONDITIONED GMRES.
  8:     FGMRES is a modification of gmres that allows the preconditioner to change
  9:     at each iteration.

 11:     Restarts:  Restarts are basically solves with x0 not equal to zero.
 12: */

 14: #include <../src/ksp/ksp/impls/gmres/fgmres/fgmresimpl.h>
 15: #define FGMRES_DELTA_DIRECTIONS 10
 16: #define FGMRES_DEFAULT_MAXK     30
 17: static PetscErrorCode KSPFGMRESGetNewVectors(KSP, PetscInt);
 18: static PetscErrorCode KSPFGMRESUpdateHessenberg(KSP, PetscInt, PetscBool, PetscReal *);
 19: static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar *, Vec, Vec, KSP, PetscInt);

 21: /*

 23:     KSPSetUp_FGMRES - Sets up the workspace needed by fgmres.

 25:     This is called once, usually automatically by KSPSolve() or KSPSetUp(),
 26:     but can be called directly by KSPSetUp().

 28: */
 29: PetscErrorCode KSPSetUp_FGMRES(KSP ksp)
 30: {
 31:   PetscInt    max_k, k;
 32:   KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;

 34:   max_k = fgmres->max_k;

 36:   KSPSetUp_GMRES(ksp);

 38:   PetscMalloc1(max_k + 2, &fgmres->prevecs);
 39:   PetscMalloc1(max_k + 2, &fgmres->prevecs_user_work);

 41:   /* fgmres->vv_allocated includes extra work vectors, which are not used in the additional
 42:      block of vectors used to store the preconditioned directions, hence  the -VEC_OFFSET
 43:      term for this first allocation of vectors holding preconditioned directions */
 44:   KSPCreateVecs(ksp, fgmres->vv_allocated - VEC_OFFSET, &fgmres->prevecs_user_work[0], 0, NULL);
 45:   for (k = 0; k < fgmres->vv_allocated - VEC_OFFSET; k++) fgmres->prevecs[k] = fgmres->prevecs_user_work[0][k];
 46:   return 0;
 47: }

 49: /*
 50:     KSPFGMRESResidual - This routine computes the initial residual (NOT PRECONDITIONED)
 51: */
 52: static PetscErrorCode KSPFGMRESResidual(KSP ksp)
 53: {
 54:   KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
 55:   Mat         Amat, Pmat;

 57:   PCGetOperators(ksp->pc, &Amat, &Pmat);

 59:   /* put A*x into VEC_TEMP */
 60:   KSP_MatMult(ksp, Amat, ksp->vec_sol, VEC_TEMP);
 61:   /* now put residual (-A*x + f) into vec_vv(0) */
 62:   VecWAXPY(VEC_VV(0), -1.0, VEC_TEMP, ksp->vec_rhs);
 63:   return 0;
 64: }

 66: /*

 68:     KSPFGMRESCycle - Run fgmres, possibly with restart.  Return residual
 69:                   history if requested.

 71:     input parameters:
 72: .        fgmres  - structure containing parameters and work areas

 74:     output parameters:
 75: .        itcount - number of iterations used.  If null, ignored.
 76: .        converged - 0 if not converged

 78:     Notes:
 79:     On entry, the value in vector VEC_VV(0) should be
 80:     the initial residual.

 82:  */
 83: PetscErrorCode KSPFGMRESCycle(PetscInt *itcount, KSP ksp)
 84: {
 85:   KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
 86:   PetscReal   res_norm;
 87:   PetscReal   hapbnd, tt;
 88:   PetscBool   hapend = PETSC_FALSE;  /* indicates happy breakdown ending */
 89:   PetscInt    loc_it;                /* local count of # of dir. in Krylov space */
 90:   PetscInt    max_k = fgmres->max_k; /* max # of directions Krylov space */
 91:   Mat         Amat, Pmat;

 93:   /* Number of pseudo iterations since last restart is the number
 94:      of prestart directions */
 95:   loc_it = 0;

 97:   /* note: (fgmres->it) is always set one less than (loc_it) It is used in
 98:      KSPBUILDSolution_FGMRES, where it is passed to KSPFGMRESBuildSoln.
 99:      Note that when KSPFGMRESBuildSoln is called from this function,
100:      (loc_it -1) is passed, so the two are equivalent */
101:   fgmres->it = (loc_it - 1);

103:   /* initial residual is in VEC_VV(0)  - compute its norm*/
104:   VecNorm(VEC_VV(0), NORM_2, &res_norm);
105:   KSPCheckNorm(ksp, res_norm);

107:   /* first entry in right-hand-side of hessenberg system is just
108:      the initial residual norm */
109:   *RS(0) = res_norm;

111:   ksp->rnorm = res_norm;
112:   KSPLogResidualHistory(ksp, res_norm);
113:   KSPMonitor(ksp, ksp->its, res_norm);

115:   /* check for the convergence - maybe the current guess is good enough */
116:   (*ksp->converged)(ksp, ksp->its, res_norm, &ksp->reason, ksp->cnvP);
117:   if (ksp->reason) {
118:     if (itcount) *itcount = 0;
119:     return 0;
120:   }

122:   /* scale VEC_VV (the initial residual) */
123:   VecScale(VEC_VV(0), 1.0 / res_norm);

125:   /* MAIN ITERATION LOOP BEGINNING*/
126:   /* keep iterating until we have converged OR generated the max number
127:      of directions OR reached the max number of iterations for the method */
128:   while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
129:     if (loc_it) {
130:       KSPLogResidualHistory(ksp, res_norm);
131:       KSPMonitor(ksp, ksp->its, res_norm);
132:     }
133:     fgmres->it = (loc_it - 1);

135:     /* see if more space is needed for work vectors */
136:     if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
137:       KSPFGMRESGetNewVectors(ksp, loc_it + 1);
138:       /* (loc_it+1) is passed in as number of the first vector that should
139:          be allocated */
140:     }

142:     /* CHANGE THE PRECONDITIONER? */
143:     /* ModifyPC is the callback function that can be used to
144:        change the PC or its attributes before its applied */
145:     (*fgmres->modifypc)(ksp, ksp->its, loc_it, res_norm, fgmres->modifyctx);

147:     /* apply PRECONDITIONER to direction vector and store with
148:        preconditioned vectors in prevec */
149:     KSP_PCApply(ksp, VEC_VV(loc_it), PREVEC(loc_it));

151:     PCGetOperators(ksp->pc, &Amat, &Pmat);
152:     /* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
153:     KSP_MatMult(ksp, Amat, PREVEC(loc_it), VEC_VV(1 + loc_it));

155:     /* update hessenberg matrix and do Gram-Schmidt - new direction is in
156:        VEC_VV(1+loc_it)*/
157:     (*fgmres->orthog)(ksp, loc_it);

159:     /* new entry in hessenburg is the 2-norm of our new direction */
160:     VecNorm(VEC_VV(loc_it + 1), NORM_2, &tt);
161:     KSPCheckNorm(ksp, tt);

163:     *HH(loc_it + 1, loc_it)  = tt;
164:     *HES(loc_it + 1, loc_it) = tt;

166:     /* Happy Breakdown Check */
167:     hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
168:     /* RS(loc_it) contains the res_norm from the last iteration  */
169:     hapbnd = PetscMin(fgmres->haptol, hapbnd);
170:     if (tt > hapbnd) {
171:       /* scale new direction by its norm */
172:       VecScale(VEC_VV(loc_it + 1), 1.0 / tt);
173:     } else {
174:       /* This happens when the solution is exactly reached. */
175:       /* So there is no new direction... */
176:       VecSet(VEC_TEMP, 0.0); /* set VEC_TEMP to 0 */
177:       hapend = PETSC_TRUE;
178:     }
179:     /* note that for FGMRES we could get HES(loc_it+1, loc_it)  = 0 and the
180:        current solution would not be exact if HES was singular.  Note that
181:        HH non-singular implies that HES is no singular, and HES is guaranteed
182:        to be nonsingular when PREVECS are linearly independent and A is
183:        nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
184:        of HES). So we should really add a check to verify that HES is nonsingular.*/

186:     /* Now apply rotations to new col of hessenberg (and right side of system),
187:        calculate new rotation, and get new residual norm at the same time*/
188:     KSPFGMRESUpdateHessenberg(ksp, loc_it, hapend, &res_norm);
189:     if (ksp->reason) break;

191:     loc_it++;
192:     fgmres->it = (loc_it - 1); /* Add this here in case it has converged */

194:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
195:     ksp->its++;
196:     ksp->rnorm = res_norm;
197:     PetscObjectSAWsGrantAccess((PetscObject)ksp);

199:     (*ksp->converged)(ksp, ksp->its, res_norm, &ksp->reason, ksp->cnvP);

201:     /* Catch error in happy breakdown and signal convergence and break from loop */
202:     if (hapend) {
203:       if (!ksp->reason) {
205:         ksp->reason = KSP_DIVERGED_BREAKDOWN;
206:         break;
207:       }
208:     }
209:   }
210:   /* END OF ITERATION LOOP */
211:   KSPLogResidualHistory(ksp, res_norm);

213:   /*
214:      Monitor if we know that we will not return for a restart */
215:   if (loc_it && (ksp->reason || ksp->its >= ksp->max_it)) KSPMonitor(ksp, ksp->its, res_norm);

217:   if (itcount) *itcount = loc_it;

219:   /*
220:     Down here we have to solve for the "best" coefficients of the Krylov
221:     columns, add the solution values together, and possibly unwind the
222:     preconditioning from the solution
223:    */

225:   /* Form the solution (or the solution so far) */
226:   /* Note: must pass in (loc_it-1) for iteration count so that KSPFGMRESBuildSoln
227:      properly navigates */

229:   KSPFGMRESBuildSoln(RS(0), ksp->vec_sol, ksp->vec_sol, ksp, loc_it - 1);
230:   return 0;
231: }

233: /*
234:     KSPSolve_FGMRES - This routine applies the FGMRES method.

236:    Input Parameter:
237: .     ksp - the Krylov space object that was set to use fgmres

239:    Output Parameter:
240: .     outits - number of iterations used

242: */

244: PetscErrorCode KSPSolve_FGMRES(KSP ksp)
245: {
246:   PetscInt    cycle_its = 0; /* iterations done in a call to KSPFGMRESCycle */
247:   KSP_FGMRES *fgmres    = (KSP_FGMRES *)ksp->data;
248:   PetscBool   diagonalscale;

250:   PCGetDiagonalScale(ksp->pc, &diagonalscale);

253:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
254:   ksp->its = 0;
255:   PetscObjectSAWsGrantAccess((PetscObject)ksp);

257:   /* Compute the initial (NOT preconditioned) residual */
258:   if (!ksp->guess_zero) {
259:     KSPFGMRESResidual(ksp);
260:   } else { /* guess is 0 so residual is F (which is in ksp->vec_rhs) */
261:     VecCopy(ksp->vec_rhs, VEC_VV(0));
262:   }
263:   /* This may be true only on a subset of MPI ranks; setting it here so it will be detected by the first norm computation in the Krylov method */
264:   if (ksp->reason == KSP_DIVERGED_PC_FAILED) VecSetInf(VEC_VV(0));

266:   /* now the residual is in VEC_VV(0) - which is what
267:      KSPFGMRESCycle expects... */

269:   KSPFGMRESCycle(&cycle_its, ksp);
270:   while (!ksp->reason) {
271:     KSPFGMRESResidual(ksp);
272:     if (ksp->its >= ksp->max_it) break;
273:     KSPFGMRESCycle(&cycle_its, ksp);
274:   }
275:   /* mark lack of convergence */
276:   if (ksp->its >= ksp->max_it && !ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
277:   return 0;
278: }

280: extern PetscErrorCode KSPReset_FGMRES(KSP);
281: /*

283:    KSPDestroy_FGMRES - Frees all memory space used by the Krylov method.

285: */
286: PetscErrorCode KSPDestroy_FGMRES(KSP ksp)
287: {
288:   KSPReset_FGMRES(ksp);
289:   PetscObjectComposeFunction((PetscObject)ksp, "KSPFGMRESSetModifyPC_C", NULL);
290:   KSPDestroy_GMRES(ksp);
291:   return 0;
292: }

294: /*
295:     KSPFGMRESBuildSoln - create the solution from the starting vector and the
296:                       current iterates.

298:     Input parameters:
299:         nrs - work area of size it + 1.
300:         vguess  - index of initial guess
301:         vdest - index of result.  Note that vguess may == vdest (replace
302:                 guess with the solution).
303:         it - HH upper triangular part is a block of size (it+1) x (it+1)

305:      This is an internal routine that knows about the FGMRES internals.
306:  */
307: static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar *nrs, Vec vguess, Vec vdest, KSP ksp, PetscInt it)
308: {
309:   PetscScalar tt;
310:   PetscInt    ii, k, j;
311:   KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);

313:   /* Solve for solution vector that minimizes the residual */

315:   /* If it is < 0, no fgmres steps have been performed */
316:   if (it < 0) {
317:     VecCopy(vguess, vdest); /* VecCopy() is smart, exists immediately if vguess == vdest */
318:     return 0;
319:   }

321:   /* so fgmres steps HAVE been performed */

323:   /* solve the upper triangular system - RS is the right side and HH is
324:      the upper triangular matrix  - put soln in nrs */
325:   if (*HH(it, it) != 0.0) {
326:     nrs[it] = *RS(it) / *HH(it, it);
327:   } else {
328:     nrs[it] = 0.0;
329:   }
330:   for (ii = 1; ii <= it; ii++) {
331:     k  = it - ii;
332:     tt = *RS(k);
333:     for (j = k + 1; j <= it; j++) tt = tt - *HH(k, j) * nrs[j];
334:     nrs[k] = tt / *HH(k, k);
335:   }

337:   /* Accumulate the correction to the soln of the preconditioned prob. in
338:      VEC_TEMP - note that we use the preconditioned vectors  */
339:   VecSet(VEC_TEMP, 0.0); /* set VEC_TEMP components to 0 */
340:   VecMAXPY(VEC_TEMP, it + 1, nrs, &PREVEC(0));

342:   /* put updated solution into vdest.*/
343:   if (vdest != vguess) {
344:     VecCopy(VEC_TEMP, vdest);
345:     VecAXPY(vdest, 1.0, vguess);
346:   } else { /* replace guess with solution */
347:     VecAXPY(vdest, 1.0, VEC_TEMP);
348:   }
349:   return 0;
350: }

352: /*

354:     KSPFGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
355:                             Return new residual.

357:     input parameters:

359: .        ksp -    Krylov space object
360: .        it  -    plane rotations are applied to the (it+1)th column of the
361:                   modified hessenberg (i.e. HH(:,it))
362: .        hapend - PETSC_FALSE not happy breakdown ending.

364:     output parameters:
365: .        res - the new residual

367:  */
368: static PetscErrorCode KSPFGMRESUpdateHessenberg(KSP ksp, PetscInt it, PetscBool hapend, PetscReal *res)
369: {
370:   PetscScalar *hh, *cc, *ss, tt;
371:   PetscInt     j;
372:   KSP_FGMRES  *fgmres = (KSP_FGMRES *)(ksp->data);

374:   hh = HH(0, it); /* pointer to beginning of column to update - so
375:                       incrementing hh "steps down" the (it+1)th col of HH*/
376:   cc = CC(0);     /* beginning of cosine rotations */
377:   ss = SS(0);     /* beginning of sine rotations */

379:   /* Apply all the previously computed plane rotations to the new column
380:      of the Hessenberg matrix */
381:   /* Note: this uses the rotation [conj(c)  s ; -s   c], c= cos(theta), s= sin(theta),
382:      and some refs have [c   s ; -conj(s)  c] (don't be confused!) */

384:   for (j = 1; j <= it; j++) {
385:     tt  = *hh;
386:     *hh = PetscConj(*cc) * tt + *ss * *(hh + 1);
387:     hh++;
388:     *hh = *cc++ * *hh - (*ss++ * tt);
389:     /* hh, cc, and ss have all been incremented one by end of loop */
390:   }

392:   /*
393:     compute the new plane rotation, and apply it to:
394:      1) the right-hand-side of the Hessenberg system (RS)
395:         note: it affects RS(it) and RS(it+1)
396:      2) the new column of the Hessenberg matrix
397:         note: it affects HH(it,it) which is currently pointed to
398:         by hh and HH(it+1, it) (*(hh+1))
399:     thus obtaining the updated value of the residual...
400:   */

402:   /* compute new plane rotation */

404:   if (!hapend) {
405:     tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh + 1)) * *(hh + 1));
406:     if (tt == 0.0) {
407:       ksp->reason = KSP_DIVERGED_NULL;
408:       return 0;
409:     }

411:     *cc = *hh / tt;       /* new cosine value */
412:     *ss = *(hh + 1) / tt; /* new sine value */

414:     /* apply to 1) and 2) */
415:     *RS(it + 1) = -(*ss * *RS(it));
416:     *RS(it)     = PetscConj(*cc) * *RS(it);
417:     *hh         = PetscConj(*cc) * *hh + *ss * *(hh + 1);

419:     /* residual is the last element (it+1) of right-hand side! */
420:     *res = PetscAbsScalar(*RS(it + 1));

422:   } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
423:             another rotation matrix (so RH doesn't change).  The new residual is
424:             always the new sine term times the residual from last time (RS(it)),
425:             but now the new sine rotation would be zero...so the residual should
426:             be zero...so we will multiply "zero" by the last residual.  This might
427:             not be exactly what we want to do here -could just return "zero". */

429:     *res = 0.0;
430:   }
431:   return 0;
432: }

434: /*

436:    KSPFGMRESGetNewVectors - This routine allocates more work vectors, starting from
437:                          VEC_VV(it), and more preconditioned work vectors, starting
438:                          from PREVEC(i).

440: */
441: static PetscErrorCode KSPFGMRESGetNewVectors(KSP ksp, PetscInt it)
442: {
443:   KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
444:   PetscInt    nwork  = fgmres->nwork_alloc; /* number of work vector chunks allocated */
445:   PetscInt    nalloc;                       /* number to allocate */
446:   PetscInt    k;

448:   nalloc = fgmres->delta_allocate; /* number of vectors to allocate
449:                                       in a single chunk */

451:   /* Adjust the number to allocate to make sure that we don't exceed the
452:      number of available slots (fgmres->vecs_allocated)*/
453:   if (it + VEC_OFFSET + nalloc >= fgmres->vecs_allocated) nalloc = fgmres->vecs_allocated - it - VEC_OFFSET;
454:   if (!nalloc) return 0;

456:   fgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */

458:   /* work vectors */
459:   KSPCreateVecs(ksp, nalloc, &fgmres->user_work[nwork], 0, NULL);
460:   for (k = 0; k < nalloc; k++) fgmres->vecs[it + VEC_OFFSET + k] = fgmres->user_work[nwork][k];
461:   /* specify size of chunk allocated */
462:   fgmres->mwork_alloc[nwork] = nalloc;

464:   /* preconditioned vectors */
465:   KSPCreateVecs(ksp, nalloc, &fgmres->prevecs_user_work[nwork], 0, NULL);
466:   for (k = 0; k < nalloc; k++) fgmres->prevecs[it + k] = fgmres->prevecs_user_work[nwork][k];

468:   /* increment the number of work vector chunks */
469:   fgmres->nwork_alloc++;
470:   return 0;
471: }

473: /*

475:    KSPBuildSolution_FGMRES

477:      Input Parameter:
478: .     ksp - the Krylov space object
479: .     ptr-

481:    Output Parameter:
482: .     result - the solution

484:    Note: this calls KSPFGMRESBuildSoln - the same function that KSPFGMRESCycle
485:    calls directly.

487: */
488: PetscErrorCode KSPBuildSolution_FGMRES(KSP ksp, Vec ptr, Vec *result)
489: {
490:   KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;

492:   if (!ptr) {
493:     if (!fgmres->sol_temp) { VecDuplicate(ksp->vec_sol, &fgmres->sol_temp); }
494:     ptr = fgmres->sol_temp;
495:   }
496:   if (!fgmres->nrs) {
497:     /* allocate the work area */
498:     PetscMalloc1(fgmres->max_k, &fgmres->nrs);
499:   }

501:   KSPFGMRESBuildSoln(fgmres->nrs, ksp->vec_sol, ptr, ksp, fgmres->it);
502:   if (result) *result = ptr;
503:   return 0;
504: }

506: PetscErrorCode KSPSetFromOptions_FGMRES(KSP ksp, PetscOptionItems *PetscOptionsObject)
507: {
508:   PetscBool flg;

510:   KSPSetFromOptions_GMRES(ksp, PetscOptionsObject);
511:   PetscOptionsHeadBegin(PetscOptionsObject, "KSP flexible GMRES Options");
512:   PetscOptionsBoolGroupBegin("-ksp_fgmres_modifypcnochange", "do not vary the preconditioner", "KSPFGMRESSetModifyPC", &flg);
513:   if (flg) KSPFGMRESSetModifyPC(ksp, KSPFGMRESModifyPCNoChange, NULL, NULL);
514:   PetscOptionsBoolGroupEnd("-ksp_fgmres_modifypcksp", "vary the KSP based preconditioner", "KSPFGMRESSetModifyPC", &flg);
515:   if (flg) KSPFGMRESSetModifyPC(ksp, KSPFGMRESModifyPCKSP, NULL, NULL);
516:   PetscOptionsHeadEnd();
517:   return 0;
518: }

520: typedef PetscErrorCode (*FCN1)(KSP, PetscInt, PetscInt, PetscReal, void *); /* force argument to next function to not be extern C*/
521: typedef PetscErrorCode (*FCN2)(void *);

523: static PetscErrorCode KSPFGMRESSetModifyPC_FGMRES(KSP ksp, FCN1 fcn, void *ctx, FCN2 d)
524: {
526:   ((KSP_FGMRES *)ksp->data)->modifypc      = fcn;
527:   ((KSP_FGMRES *)ksp->data)->modifydestroy = d;
528:   ((KSP_FGMRES *)ksp->data)->modifyctx     = ctx;
529:   return 0;
530: }

532: PetscErrorCode KSPReset_FGMRES(KSP ksp)
533: {
534:   KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
535:   PetscInt    i;

537:   PetscFree(fgmres->prevecs);
538:   if (fgmres->nwork_alloc > 0) {
539:     i = 0;
540:     /* In the first allocation we allocated VEC_OFFSET fewer vectors in prevecs */
541:     VecDestroyVecs(fgmres->mwork_alloc[i] - VEC_OFFSET, &fgmres->prevecs_user_work[i]);
542:     for (i = 1; i < fgmres->nwork_alloc; i++) VecDestroyVecs(fgmres->mwork_alloc[i], &fgmres->prevecs_user_work[i]);
543:   }
544:   PetscFree(fgmres->prevecs_user_work);
545:   if (fgmres->modifydestroy) (*fgmres->modifydestroy)(fgmres->modifyctx);
546:   KSPReset_GMRES(ksp);
547:   return 0;
548: }

550: PetscErrorCode KSPGMRESSetRestart_FGMRES(KSP ksp, PetscInt max_k)
551: {
552:   KSP_FGMRES *gmres = (KSP_FGMRES *)ksp->data;

555:   if (!ksp->setupstage) {
556:     gmres->max_k = max_k;
557:   } else if (gmres->max_k != max_k) {
558:     gmres->max_k    = max_k;
559:     ksp->setupstage = KSP_SETUP_NEW;
560:     /* free the data structures, then create them again */
561:     KSPReset_FGMRES(ksp);
562:   }
563:   return 0;
564: }

566: PetscErrorCode KSPGMRESGetRestart_FGMRES(KSP ksp, PetscInt *max_k)
567: {
568:   KSP_FGMRES *gmres = (KSP_FGMRES *)ksp->data;

570:   *max_k = gmres->max_k;
571:   return 0;
572: }

574: /*MC
575:      KSPFGMRES - Implements the Flexible Generalized Minimal Residual method. [](sec_flexibleksp)

577:    Options Database Keys:
578: +   -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
579: .   -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
580: .   -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
581:                              vectors are allocated as needed)
582: .   -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
583: .   -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
584: .   -ksp_gmres_cgs_refinement_type <refine_never,refine_ifneeded,refine_always> - determine if iterative refinement is used to increase the
585:                                    stability of the classical Gram-Schmidt  orthogonalization.
586: .   -ksp_gmres_krylov_monitor - plot the Krylov space generated
587: .   -ksp_fgmres_modifypcnochange - do not change the preconditioner between iterations
588: -   -ksp_fgmres_modifypcksp - modify the preconditioner using `KSPFGMRESModifyPCKSP()`

590:    Level: beginner

592:     Notes:
593:     See `KSPFGMRESSetModifyPC()` for how to vary the preconditioner between iterations

595:     Only right preconditioning is supported.

597:     The following options -ksp_type fgmres -pc_type ksp -ksp_ksp_type bcgs -ksp_view -ksp_pc_type jacobi make the preconditioner (or inner solver)
598:     be bi-CG-stab with a preconditioner of Jacobi.

600:     Developer Note:
601:     This object is subclassed off of `KSPGMRES`

603:     Contributed by:
604:     Allison Baker

606: .seealso: [](chapter_ksp), [](sec_flexibleksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPGMRES`, `KSPLGMRES`,
607:           `KSPGMRESSetRestart()`, `KSPGMRESSetHapTol()`, `KSPGMRESSetPreAllocateVectors()`, `KSPGMRESSetOrthogonalization()`, `KSPGMRESGetOrthogonalization()`,
608:           `KSPGMRESClassicalGramSchmidtOrthogonalization()`, `KSPGMRESModifiedGramSchmidtOrthogonalization()`,
609:           `KSPGMRESCGSRefinementType`, `KSPGMRESSetCGSRefinementType()`, `KSPGMRESGetCGSRefinementType()`, `KSPGMRESMonitorKrylov()`, `KSPFGMRESSetModifyPC()`,
610:           `KSPFGMRESModifyPCKSP()`
611: M*/

613: PETSC_EXTERN PetscErrorCode KSPCreate_FGMRES(KSP ksp)
614: {
615:   KSP_FGMRES *fgmres;

617:   PetscNew(&fgmres);

619:   ksp->data                              = (void *)fgmres;
620:   ksp->ops->buildsolution                = KSPBuildSolution_FGMRES;
621:   ksp->ops->setup                        = KSPSetUp_FGMRES;
622:   ksp->ops->solve                        = KSPSolve_FGMRES;
623:   ksp->ops->reset                        = KSPReset_FGMRES;
624:   ksp->ops->destroy                      = KSPDestroy_FGMRES;
625:   ksp->ops->view                         = KSPView_GMRES;
626:   ksp->ops->setfromoptions               = KSPSetFromOptions_FGMRES;
627:   ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
628:   ksp->ops->computeeigenvalues           = KSPComputeEigenvalues_GMRES;

630:   KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 3);
631:   KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 1);

633:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetPreAllocateVectors_C", KSPGMRESSetPreAllocateVectors_GMRES);
634:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetOrthogonalization_C", KSPGMRESSetOrthogonalization_GMRES);
635:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetOrthogonalization_C", KSPGMRESGetOrthogonalization_GMRES);
636:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetRestart_C", KSPGMRESSetRestart_FGMRES);
637:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetRestart_C", KSPGMRESGetRestart_FGMRES);
638:   PetscObjectComposeFunction((PetscObject)ksp, "KSPFGMRESSetModifyPC_C", KSPFGMRESSetModifyPC_FGMRES);
639:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetCGSRefinementType_C", KSPGMRESSetCGSRefinementType_GMRES);
640:   PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetCGSRefinementType_C", KSPGMRESGetCGSRefinementType_GMRES);

642:   fgmres->haptol         = 1.0e-30;
643:   fgmres->q_preallocate  = 0;
644:   fgmres->delta_allocate = FGMRES_DELTA_DIRECTIONS;
645:   fgmres->orthog         = KSPGMRESClassicalGramSchmidtOrthogonalization;
646:   fgmres->nrs            = NULL;
647:   fgmres->sol_temp       = NULL;
648:   fgmres->max_k          = FGMRES_DEFAULT_MAXK;
649:   fgmres->Rsvd           = NULL;
650:   fgmres->orthogwork     = NULL;
651:   fgmres->modifypc       = KSPFGMRESModifyPCNoChange;
652:   fgmres->modifyctx      = NULL;
653:   fgmres->modifydestroy  = NULL;
654:   fgmres->cgstype        = KSP_GMRES_CGS_REFINE_NEVER;
655:   return 0;
656: }