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.
13: Contributed by Allison Baker
15: */
17: #include <../src/ksp/ksp/impls/gmres/fgmres/fgmresimpl.h>
18: #define FGMRES_DELTA_DIRECTIONS 10
19: #define FGMRES_DEFAULT_MAXK 30
20: static PetscErrorCode KSPFGMRESGetNewVectors(KSP,PetscInt);
21: static PetscErrorCode KSPFGMRESUpdateHessenberg(KSP,PetscInt,PetscBool,PetscReal*);
22: static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);
24: /*
26: KSPSetUp_FGMRES - Sets up the workspace needed by fgmres.
28: This is called once, usually automatically by KSPSolve() or KSPSetUp(),
29: but can be called directly by KSPSetUp().
31: */
32: PetscErrorCode KSPSetUp_FGMRES(KSP ksp)
33: {
35: PetscInt max_k,k;
36: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
39: max_k = fgmres->max_k;
41: KSPSetUp_GMRES(ksp);
43: PetscMalloc1(max_k+2,&fgmres->prevecs);
44: PetscMalloc1(max_k+2,&fgmres->prevecs_user_work);
45: PetscLogObjectMemory((PetscObject)ksp,(max_k+2)*(2*sizeof(void*)));
47: /* fgmres->vv_allocated includes extra work vectors, which are not used in the additional
48: block of vectors used to store the preconditioned directions, hence the -VEC_OFFSET
49: term for this first allocation of vectors holding preconditioned directions */
50: KSPCreateVecs(ksp,fgmres->vv_allocated-VEC_OFFSET,&fgmres->prevecs_user_work[0],0,NULL);
51: PetscLogObjectParents(ksp,fgmres->vv_allocated-VEC_OFFSET,fgmres->prevecs_user_work[0]);
52: for (k=0; k < fgmres->vv_allocated - VEC_OFFSET ; k++) {
53: fgmres->prevecs[k] = fgmres->prevecs_user_work[0][k];
54: }
55: return(0);
56: }
58: /*
59: KSPFGMRESResidual - This routine computes the initial residual (NOT PRECONDITIONED)
60: */
61: static PetscErrorCode KSPFGMRESResidual(KSP ksp)
62: {
63: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
64: Mat Amat,Pmat;
68: PCGetOperators(ksp->pc,&Amat,&Pmat);
70: /* put A*x into VEC_TEMP */
71: KSP_MatMult(ksp,Amat,ksp->vec_sol,VEC_TEMP);
72: /* now put residual (-A*x + f) into vec_vv(0) */
73: VecWAXPY(VEC_VV(0),-1.0,VEC_TEMP,ksp->vec_rhs);
74: return(0);
75: }
77: /*
79: KSPFGMRESCycle - Run fgmres, possibly with restart. Return residual
80: history if requested.
82: input parameters:
83: . fgmres - structure containing parameters and work areas
85: output parameters:
86: . itcount - number of iterations used. If null, ignored.
87: . converged - 0 if not converged
89: Notes:
90: On entry, the value in vector VEC_VV(0) should be
91: the initial residual.
93: */
94: PetscErrorCode KSPFGMRESCycle(PetscInt *itcount,KSP ksp)
95: {
97: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
98: PetscReal res_norm;
99: PetscReal hapbnd,tt;
100: PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
102: PetscInt loc_it; /* local count of # of dir. in Krylov space */
103: PetscInt max_k = fgmres->max_k; /* max # of directions Krylov space */
104: Mat Amat,Pmat;
107: /* Number of pseudo iterations since last restart is the number
108: of prestart directions */
109: loc_it = 0;
111: /* note: (fgmres->it) is always set one less than (loc_it) It is used in
112: KSPBUILDSolution_FGMRES, where it is passed to KSPFGMRESBuildSoln.
113: Note that when KSPFGMRESBuildSoln is called from this function,
114: (loc_it -1) is passed, so the two are equivalent */
115: fgmres->it = (loc_it - 1);
117: /* initial residual is in VEC_VV(0) - compute its norm*/
118: VecNorm(VEC_VV(0),NORM_2,&res_norm);
119: KSPCheckNorm(ksp,res_norm);
121: /* first entry in right-hand-side of hessenberg system is just
122: the initial residual norm */
123: *RS(0) = res_norm;
125: ksp->rnorm = res_norm;
126: KSPLogResidualHistory(ksp,res_norm);
127: KSPMonitor(ksp,ksp->its,res_norm);
129: /* check for the convergence - maybe the current guess is good enough */
130: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
131: if (ksp->reason) {
132: if (itcount) *itcount = 0;
133: return(0);
134: }
136: /* scale VEC_VV (the initial residual) */
137: VecScale(VEC_VV(0),1.0/res_norm);
139: /* MAIN ITERATION LOOP BEGINNING*/
140: /* keep iterating until we have converged OR generated the max number
141: of directions OR reached the max number of iterations for the method */
142: while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
143: if (loc_it) {
144: KSPLogResidualHistory(ksp,res_norm);
145: KSPMonitor(ksp,ksp->its,res_norm);
146: }
147: fgmres->it = (loc_it - 1);
149: /* see if more space is needed for work vectors */
150: if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
151: KSPFGMRESGetNewVectors(ksp,loc_it+1);
152: /* (loc_it+1) is passed in as number of the first vector that should
153: be allocated */
154: }
156: /* CHANGE THE PRECONDITIONER? */
157: /* ModifyPC is the callback function that can be used to
158: change the PC or its attributes before its applied */
159: (*fgmres->modifypc)(ksp,ksp->its,loc_it,res_norm,fgmres->modifyctx);
161: /* apply PRECONDITIONER to direction vector and store with
162: preconditioned vectors in prevec */
163: KSP_PCApply(ksp,VEC_VV(loc_it),PREVEC(loc_it));
165: PCGetOperators(ksp->pc,&Amat,&Pmat);
166: /* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
167: KSP_MatMult(ksp,Amat,PREVEC(loc_it),VEC_VV(1+loc_it));
169: /* update hessenberg matrix and do Gram-Schmidt - new direction is in
170: VEC_VV(1+loc_it)*/
171: (*fgmres->orthog)(ksp,loc_it);
173: /* new entry in hessenburg is the 2-norm of our new direction */
174: VecNorm(VEC_VV(loc_it+1),NORM_2,&tt);
175: KSPCheckNorm(ksp,tt);
177: *HH(loc_it+1,loc_it) = tt;
178: *HES(loc_it+1,loc_it) = tt;
180: /* Happy Breakdown Check */
181: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
182: /* RS(loc_it) contains the res_norm from the last iteration */
183: hapbnd = PetscMin(fgmres->haptol,hapbnd);
184: if (tt > hapbnd) {
185: /* scale new direction by its norm */
186: VecScale(VEC_VV(loc_it+1),1.0/tt);
187: } else {
188: /* This happens when the solution is exactly reached. */
189: /* So there is no new direction... */
190: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP to 0 */
191: hapend = PETSC_TRUE;
192: }
193: /* note that for FGMRES we could get HES(loc_it+1, loc_it) = 0 and the
194: current solution would not be exact if HES was singular. Note that
195: HH non-singular implies that HES is no singular, and HES is guaranteed
196: to be nonsingular when PREVECS are linearly independent and A is
197: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
198: of HES). So we should really add a check to verify that HES is nonsingular.*/
200: /* Now apply rotations to new col of hessenberg (and right side of system),
201: calculate new rotation, and get new residual norm at the same time*/
202: KSPFGMRESUpdateHessenberg(ksp,loc_it,hapend,&res_norm);
203: if (ksp->reason) break;
205: loc_it++;
206: fgmres->it = (loc_it-1); /* Add this here in case it has converged */
208: PetscObjectSAWsTakeAccess((PetscObject)ksp);
209: ksp->its++;
210: ksp->rnorm = res_norm;
211: PetscObjectSAWsGrantAccess((PetscObject)ksp);
213: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
215: /* Catch error in happy breakdown and signal convergence and break from loop */
216: if (hapend) {
217: if (!ksp->reason) {
218: if (ksp->errorifnotconverged) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"You reached the happy break down, but convergence was not indicated. Residual norm = %g",(double)res_norm);
219: else {
220: ksp->reason = KSP_DIVERGED_BREAKDOWN;
221: break;
222: }
223: }
224: }
225: }
226: /* END OF ITERATION LOOP */
227: KSPLogResidualHistory(ksp,res_norm);
229: /*
230: Monitor if we know that we will not return for a restart */
231: if (loc_it && (ksp->reason || ksp->its >= ksp->max_it)) {
232: KSPMonitor(ksp,ksp->its,res_norm);
233: }
235: if (itcount) *itcount = loc_it;
237: /*
238: Down here we have to solve for the "best" coefficients of the Krylov
239: columns, add the solution values together, and possibly unwind the
240: preconditioning from the solution
241: */
243: /* Form the solution (or the solution so far) */
244: /* Note: must pass in (loc_it-1) for iteration count so that KSPFGMRESBuildSoln
245: properly navigates */
247: KSPFGMRESBuildSoln(RS(0),ksp->vec_sol,ksp->vec_sol,ksp,loc_it-1);
248: return(0);
249: }
251: /*
252: KSPSolve_FGMRES - This routine applies the FGMRES method.
254: Input Parameter:
255: . ksp - the Krylov space object that was set to use fgmres
257: Output Parameter:
258: . outits - number of iterations used
260: */
262: PetscErrorCode KSPSolve_FGMRES(KSP ksp)
263: {
265: PetscInt cycle_its = 0; /* iterations done in a call to KSPFGMRESCycle */
266: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
267: PetscBool diagonalscale;
270: PCGetDiagonalScale(ksp->pc,&diagonalscale);
271: if (diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);
273: PetscObjectSAWsTakeAccess((PetscObject)ksp);
274: ksp->its = 0;
275: PetscObjectSAWsGrantAccess((PetscObject)ksp);
277: /* Compute the initial (NOT preconditioned) residual */
278: if (!ksp->guess_zero) {
279: KSPFGMRESResidual(ksp);
280: } else { /* guess is 0 so residual is F (which is in ksp->vec_rhs) */
281: VecCopy(ksp->vec_rhs,VEC_VV(0));
282: }
283: /* now the residual is in VEC_VV(0) - which is what
284: KSPFGMRESCycle expects... */
286: KSPFGMRESCycle(&cycle_its,ksp);
287: while (!ksp->reason) {
288: KSPFGMRESResidual(ksp);
289: if (ksp->its >= ksp->max_it) break;
290: KSPFGMRESCycle(&cycle_its,ksp);
291: }
292: /* mark lack of convergence */
293: if (ksp->its >= ksp->max_it && !ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
294: return(0);
295: }
297: extern PetscErrorCode KSPReset_FGMRES(KSP);
298: /*
300: KSPDestroy_FGMRES - Frees all memory space used by the Krylov method.
302: */
303: PetscErrorCode KSPDestroy_FGMRES(KSP ksp)
304: {
308: KSPReset_FGMRES(ksp);
309: PetscObjectComposeFunction((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",NULL);
310: KSPDestroy_GMRES(ksp);
311: return(0);
312: }
314: /*
315: KSPFGMRESBuildSoln - create the solution from the starting vector and the
316: current iterates.
318: Input parameters:
319: nrs - work area of size it + 1.
320: vguess - index of initial guess
321: vdest - index of result. Note that vguess may == vdest (replace
322: guess with the solution).
323: it - HH upper triangular part is a block of size (it+1) x (it+1)
325: This is an internal routine that knows about the FGMRES internals.
326: */
327: static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar *nrs,Vec vguess,Vec vdest,KSP ksp,PetscInt it)
328: {
329: PetscScalar tt;
331: PetscInt ii,k,j;
332: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
335: /* Solve for solution vector that minimizes the residual */
337: /* If it is < 0, no fgmres steps have been performed */
338: if (it < 0) {
339: VecCopy(vguess,vdest); /* VecCopy() is smart, exists immediately if vguess == vdest */
340: return(0);
341: }
343: /* so fgmres steps HAVE been performed */
345: /* solve the upper triangular system - RS is the right side and HH is
346: the upper triangular matrix - put soln in nrs */
347: if (*HH(it,it) != 0.0) {
348: nrs[it] = *RS(it) / *HH(it,it);
349: } else {
350: nrs[it] = 0.0;
351: }
352: for (ii=1; ii<=it; ii++) {
353: k = it - ii;
354: tt = *RS(k);
355: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
356: nrs[k] = tt / *HH(k,k);
357: }
359: /* Accumulate the correction to the soln of the preconditioned prob. in
360: VEC_TEMP - note that we use the preconditioned vectors */
361: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP components to 0 */
362: VecMAXPY(VEC_TEMP,it+1,nrs,&PREVEC(0));
364: /* put updated solution into vdest.*/
365: if (vdest != vguess) {
366: VecCopy(VEC_TEMP,vdest);
367: VecAXPY(vdest,1.0,vguess);
368: } else { /* replace guess with solution */
369: VecAXPY(vdest,1.0,VEC_TEMP);
370: }
371: return(0);
372: }
374: /*
376: KSPFGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
377: Return new residual.
379: input parameters:
381: . ksp - Krylov space object
382: . it - plane rotations are applied to the (it+1)th column of the
383: modified hessenberg (i.e. HH(:,it))
384: . hapend - PETSC_FALSE not happy breakdown ending.
386: output parameters:
387: . res - the new residual
389: */
390: static PetscErrorCode KSPFGMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscBool hapend,PetscReal *res)
391: {
392: PetscScalar *hh,*cc,*ss,tt;
393: PetscInt j;
394: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
397: hh = HH(0,it); /* pointer to beginning of column to update - so
398: incrementing hh "steps down" the (it+1)th col of HH*/
399: cc = CC(0); /* beginning of cosine rotations */
400: ss = SS(0); /* beginning of sine rotations */
402: /* Apply all the previously computed plane rotations to the new column
403: of the Hessenberg matrix */
404: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
405: and some refs have [c s ; -conj(s) c] (don't be confused!) */
407: for (j=1; j<=it; j++) {
408: tt = *hh;
409: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
410: hh++;
411: *hh = *cc++ * *hh - (*ss++ * tt);
412: /* hh, cc, and ss have all been incremented one by end of loop */
413: }
415: /*
416: compute the new plane rotation, and apply it to:
417: 1) the right-hand-side of the Hessenberg system (RS)
418: note: it affects RS(it) and RS(it+1)
419: 2) the new column of the Hessenberg matrix
420: note: it affects HH(it,it) which is currently pointed to
421: by hh and HH(it+1, it) (*(hh+1))
422: thus obtaining the updated value of the residual...
423: */
425: /* compute new plane rotation */
427: if (!hapend) {
428: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
429: if (tt == 0.0) {
430: ksp->reason = KSP_DIVERGED_NULL;
431: return(0);
432: }
434: *cc = *hh / tt; /* new cosine value */
435: *ss = *(hh+1) / tt; /* new sine value */
437: /* apply to 1) and 2) */
438: *RS(it+1) = -(*ss * *RS(it));
439: *RS(it) = PetscConj(*cc) * *RS(it);
440: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
442: /* residual is the last element (it+1) of right-hand side! */
443: *res = PetscAbsScalar(*RS(it+1));
445: } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
446: another rotation matrix (so RH doesn't change). The new residual is
447: always the new sine term times the residual from last time (RS(it)),
448: but now the new sine rotation would be zero...so the residual should
449: be zero...so we will multiply "zero" by the last residual. This might
450: not be exactly what we want to do here -could just return "zero". */
452: *res = 0.0;
453: }
454: return(0);
455: }
457: /*
459: KSPFGMRESGetNewVectors - This routine allocates more work vectors, starting from
460: VEC_VV(it), and more preconditioned work vectors, starting
461: from PREVEC(i).
463: */
464: static PetscErrorCode KSPFGMRESGetNewVectors(KSP ksp,PetscInt it)
465: {
466: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
467: PetscInt nwork = fgmres->nwork_alloc; /* number of work vector chunks allocated */
468: PetscInt nalloc; /* number to allocate */
470: PetscInt k;
473: nalloc = fgmres->delta_allocate; /* number of vectors to allocate
474: in a single chunk */
476: /* Adjust the number to allocate to make sure that we don't exceed the
477: number of available slots (fgmres->vecs_allocated)*/
478: if (it + VEC_OFFSET + nalloc >= fgmres->vecs_allocated) {
479: nalloc = fgmres->vecs_allocated - it - VEC_OFFSET;
480: }
481: if (!nalloc) return(0);
483: fgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
485: /* work vectors */
486: KSPCreateVecs(ksp,nalloc,&fgmres->user_work[nwork],0,NULL);
487: PetscLogObjectParents(ksp,nalloc,fgmres->user_work[nwork]);
488: for (k=0; k < nalloc; k++) {
489: fgmres->vecs[it+VEC_OFFSET+k] = fgmres->user_work[nwork][k];
490: }
491: /* specify size of chunk allocated */
492: fgmres->mwork_alloc[nwork] = nalloc;
494: /* preconditioned vectors */
495: KSPCreateVecs(ksp,nalloc,&fgmres->prevecs_user_work[nwork],0,NULL);
496: PetscLogObjectParents(ksp,nalloc,fgmres->prevecs_user_work[nwork]);
497: for (k=0; k < nalloc; k++) {
498: fgmres->prevecs[it+k] = fgmres->prevecs_user_work[nwork][k];
499: }
501: /* increment the number of work vector chunks */
502: fgmres->nwork_alloc++;
503: return(0);
504: }
506: /*
508: KSPBuildSolution_FGMRES
510: Input Parameter:
511: . ksp - the Krylov space object
512: . ptr-
514: Output Parameter:
515: . result - the solution
517: Note: this calls KSPFGMRESBuildSoln - the same function that KSPFGMRESCycle
518: calls directly.
520: */
521: PetscErrorCode KSPBuildSolution_FGMRES(KSP ksp,Vec ptr,Vec *result)
522: {
523: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
527: if (!ptr) {
528: if (!fgmres->sol_temp) {
529: VecDuplicate(ksp->vec_sol,&fgmres->sol_temp);
530: PetscLogObjectParent((PetscObject)ksp,(PetscObject)fgmres->sol_temp);
531: }
532: ptr = fgmres->sol_temp;
533: }
534: if (!fgmres->nrs) {
535: /* allocate the work area */
536: PetscMalloc1(fgmres->max_k,&fgmres->nrs);
537: PetscLogObjectMemory((PetscObject)ksp,fgmres->max_k*sizeof(PetscScalar));
538: }
540: KSPFGMRESBuildSoln(fgmres->nrs,ksp->vec_sol,ptr,ksp,fgmres->it);
541: if (result) *result = ptr;
542: return(0);
543: }
545: PetscErrorCode KSPSetFromOptions_FGMRES(PetscOptionItems *PetscOptionsObject,KSP ksp)
546: {
548: PetscBool flg;
551: KSPSetFromOptions_GMRES(PetscOptionsObject,ksp);
552: PetscOptionsHead(PetscOptionsObject,"KSP flexible GMRES Options");
553: PetscOptionsBoolGroupBegin("-ksp_fgmres_modifypcnochange","do not vary the preconditioner","KSPFGMRESSetModifyPC",&flg);
554: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCNoChange,NULL,NULL);}
555: PetscOptionsBoolGroupEnd("-ksp_fgmres_modifypcksp","vary the KSP based preconditioner","KSPFGMRESSetModifyPC",&flg);
556: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCKSP,NULL,NULL);}
557: PetscOptionsTail();
558: return(0);
559: }
561: typedef PetscErrorCode (*FCN1)(KSP,PetscInt,PetscInt,PetscReal,void*); /* force argument to next function to not be extern C*/
562: typedef PetscErrorCode (*FCN2)(void*);
564: static PetscErrorCode KSPFGMRESSetModifyPC_FGMRES(KSP ksp,FCN1 fcn,void *ctx,FCN2 d)
565: {
568: ((KSP_FGMRES*)ksp->data)->modifypc = fcn;
569: ((KSP_FGMRES*)ksp->data)->modifydestroy = d;
570: ((KSP_FGMRES*)ksp->data)->modifyctx = ctx;
571: return(0);
572: }
574: PetscErrorCode KSPReset_FGMRES(KSP ksp)
575: {
576: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
578: PetscInt i;
581: PetscFree (fgmres->prevecs);
582: if (fgmres->nwork_alloc>0) {
583: i=0;
584: /* In the first allocation we allocated VEC_OFFSET fewer vectors in prevecs */
585: VecDestroyVecs(fgmres->mwork_alloc[i]-VEC_OFFSET,&fgmres->prevecs_user_work[i]);
586: for (i=1; i<fgmres->nwork_alloc; i++) {
587: VecDestroyVecs(fgmres->mwork_alloc[i],&fgmres->prevecs_user_work[i]);
588: }
589: }
590: PetscFree(fgmres->prevecs_user_work);
591: if (fgmres->modifydestroy) {
592: (*fgmres->modifydestroy)(fgmres->modifyctx);
593: }
594: KSPReset_GMRES(ksp);
595: return(0);
596: }
598: PetscErrorCode KSPGMRESSetRestart_FGMRES(KSP ksp,PetscInt max_k)
599: {
600: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
604: if (max_k < 1) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
605: if (!ksp->setupstage) {
606: gmres->max_k = max_k;
607: } else if (gmres->max_k != max_k) {
608: gmres->max_k = max_k;
609: ksp->setupstage = KSP_SETUP_NEW;
610: /* free the data structures, then create them again */
611: KSPReset_FGMRES(ksp);
612: }
613: return(0);
614: }
616: PetscErrorCode KSPGMRESGetRestart_FGMRES(KSP ksp,PetscInt *max_k)
617: {
618: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
621: *max_k = gmres->max_k;
622: return(0);
623: }
625: /*MC
626: KSPFGMRES - Implements the Flexible Generalized Minimal Residual method.
627: developed by Saad with restart
629: Options Database Keys:
630: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
631: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
632: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
633: vectors are allocated as needed)
634: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
635: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
636: . -ksp_gmres_cgs_refinement_type <refine_never,refine_ifneeded,refine_always> - determine if iterative refinement is used to increase the
637: stability of the classical Gram-Schmidt orthogonalization.
638: . -ksp_gmres_krylov_monitor - plot the Krylov space generated
639: . -ksp_fgmres_modifypcnochange - do not change the preconditioner between iterations
640: - -ksp_fgmres_modifypcksp - modify the preconditioner using KSPFGMRESModifyPCKSP()
642: Level: beginner
644: Notes:
645: See KSPFGMRESSetModifyPC() for how to vary the preconditioner between iterations
646: Only right preconditioning is supported.
648: Notes:
649: 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)
650: be bi-CG-stab with a preconditioner of Jacobi.
652: Developer Notes:
653: This object is subclassed off of KSPGMRES
655: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPGMRES, KSPLGMRES,
656: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization(), KSPGMRESGetOrthogonalization(),
657: KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
658: KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESGetCGSRefinementType(), KSPGMRESMonitorKrylov(), KSPFGMRESSetModifyPC(),
659: KSPFGMRESModifyPCKSP()
661: M*/
663: PETSC_EXTERN PetscErrorCode KSPCreate_FGMRES(KSP ksp)
664: {
665: KSP_FGMRES *fgmres;
669: PetscNewLog(ksp,&fgmres);
671: ksp->data = (void*)fgmres;
672: ksp->ops->buildsolution = KSPBuildSolution_FGMRES;
673: ksp->ops->setup = KSPSetUp_FGMRES;
674: ksp->ops->solve = KSPSolve_FGMRES;
675: ksp->ops->reset = KSPReset_FGMRES;
676: ksp->ops->destroy = KSPDestroy_FGMRES;
677: ksp->ops->view = KSPView_GMRES;
678: ksp->ops->setfromoptions = KSPSetFromOptions_FGMRES;
679: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
680: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
682: KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,3);
683: KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_RIGHT,1);
685: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",KSPGMRESSetPreAllocateVectors_GMRES);
686: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",KSPGMRESSetOrthogonalization_GMRES);
687: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetOrthogonalization_C",KSPGMRESGetOrthogonalization_GMRES);
688: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetRestart_C",KSPGMRESSetRestart_FGMRES);
689: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetRestart_C",KSPGMRESGetRestart_FGMRES);
690: PetscObjectComposeFunction((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",KSPFGMRESSetModifyPC_FGMRES);
691: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",KSPGMRESSetCGSRefinementType_GMRES);
692: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetCGSRefinementType_C",KSPGMRESGetCGSRefinementType_GMRES);
694: fgmres->haptol = 1.0e-30;
695: fgmres->q_preallocate = 0;
696: fgmres->delta_allocate = FGMRES_DELTA_DIRECTIONS;
697: fgmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
698: fgmres->nrs = NULL;
699: fgmres->sol_temp = NULL;
700: fgmres->max_k = FGMRES_DEFAULT_MAXK;
701: fgmres->Rsvd = NULL;
702: fgmres->orthogwork = NULL;
703: fgmres->modifypc = KSPFGMRESModifyPCNoChange;
704: fgmres->modifyctx = NULL;
705: fgmres->modifydestroy = NULL;
706: fgmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
707: return(0);
708: }