Actual source code: snesngmres.c
petsc-3.6.0 2015-06-09
1: #include <../src/snes/impls/ngmres/snesngmres.h> /*I "petscsnes.h" I*/
2: #include <petscblaslapack.h>
4: const char *const SNESNGMRESRestartTypes[] = {"NONE","PERIODIC","DIFFERENCE","SNESNGMRESRestartType","SNES_NGMRES_RESTART_",0};
5: const char *const SNESNGMRESSelectTypes[] = {"NONE","DIFFERENCE","LINESEARCH","SNESNGMRESSelectType","SNES_NGMRES_SELECT_",0};
9: PetscErrorCode SNESReset_NGMRES(SNES snes)
10: {
11: SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data;
15: VecDestroyVecs(ngmres->msize,&ngmres->Fdot);
16: VecDestroyVecs(ngmres->msize,&ngmres->Xdot);
17: SNESLineSearchDestroy(&ngmres->additive_linesearch);
18: return(0);
19: }
23: PetscErrorCode SNESDestroy_NGMRES(SNES snes)
24: {
26: SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;
29: SNESReset_NGMRES(snes);
30: PetscFree5(ngmres->h,ngmres->beta,ngmres->xi,ngmres->fnorms,ngmres->q);
31: PetscFree(ngmres->s);
32: PetscFree(ngmres->xnorms);
33: #if PETSC_USE_COMPLEX
34: PetscFree(ngmres->rwork);
35: #endif
36: PetscFree(ngmres->work);
37: PetscFree(snes->data);
38: return(0);
39: }
43: PetscErrorCode SNESSetUp_NGMRES(SNES snes)
44: {
45: SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data;
46: const char *optionsprefix;
47: PetscInt msize,hsize;
51: if (snes->pc && snes->pcside == PC_LEFT && snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
52: SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"SNESNGMRES does not support left preconditioning with unpreconditioned function");
53: }
54: if (snes->pcside == PC_LEFT && snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_PRECONDITIONED;
55: SNESSetWorkVecs(snes,5);
56: if (!ngmres->Xdot) {VecDuplicateVecs(snes->vec_sol,ngmres->msize,&ngmres->Xdot);}
57: if (!ngmres->Fdot) {VecDuplicateVecs(snes->vec_sol,ngmres->msize,&ngmres->Fdot);}
58: if (!ngmres->setup_called) {
59: msize = ngmres->msize; /* restart size */
60: hsize = msize * msize;
62: /* explicit least squares minimization solve */
63: PetscMalloc5(hsize,&ngmres->h, msize,&ngmres->beta, msize,&ngmres->xi, msize,&ngmres->fnorms, hsize,&ngmres->q);
64: PetscMalloc1(msize,&ngmres->xnorms);
65: ngmres->nrhs = 1;
66: ngmres->lda = msize;
67: ngmres->ldb = msize;
68: PetscMalloc1(msize,&ngmres->s);
69: PetscMemzero(ngmres->h, hsize*sizeof(PetscScalar));
70: PetscMemzero(ngmres->q, hsize*sizeof(PetscScalar));
71: PetscMemzero(ngmres->xi, msize*sizeof(PetscScalar));
72: PetscMemzero(ngmres->beta,msize*sizeof(PetscScalar));
73: ngmres->lwork = 12*msize;
74: #if PETSC_USE_COMPLEX
75: PetscMalloc1(ngmres->lwork,&ngmres->rwork);
76: #endif
77: PetscMalloc1(ngmres->lwork,&ngmres->work);
78: }
80: /* linesearch setup */
81: SNESGetOptionsPrefix(snes,&optionsprefix);
83: if (ngmres->select_type == SNES_NGMRES_SELECT_LINESEARCH) {
84: SNESLineSearchCreate(PetscObjectComm((PetscObject)snes),&ngmres->additive_linesearch);
85: SNESLineSearchSetSNES(ngmres->additive_linesearch,snes);
86: SNESLineSearchSetType(ngmres->additive_linesearch,SNESLINESEARCHL2);
87: SNESLineSearchAppendOptionsPrefix(ngmres->additive_linesearch,"additive_");
88: SNESLineSearchAppendOptionsPrefix(ngmres->additive_linesearch,optionsprefix);
89: SNESLineSearchSetFromOptions(ngmres->additive_linesearch);
90: }
92: ngmres->setup_called = PETSC_TRUE;
93: return(0);
94: }
98: PetscErrorCode SNESSetFromOptions_NGMRES(PetscOptions *PetscOptionsObject,SNES snes)
99: {
100: SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data;
102: PetscBool debug = PETSC_FALSE;
103: SNESLineSearch linesearch;
106: PetscOptionsHead(PetscOptionsObject,"SNES NGMRES options");
107: PetscOptionsEnum("-snes_ngmres_select_type","Select type","SNESNGMRESSetSelectType",SNESNGMRESSelectTypes,
108: (PetscEnum)ngmres->select_type,(PetscEnum*)&ngmres->select_type,NULL);
109: PetscOptionsEnum("-snes_ngmres_restart_type","Restart type","SNESNGMRESSetRestartType",SNESNGMRESRestartTypes,
110: (PetscEnum)ngmres->restart_type,(PetscEnum*)&ngmres->restart_type,NULL);
111: PetscOptionsBool("-snes_ngmres_candidate", "Use candidate storage", "SNES",ngmres->candidate,&ngmres->candidate,NULL);
112: PetscOptionsBool("-snes_ngmres_approxfunc","Linearly approximate the function", "SNES",ngmres->approxfunc,&ngmres->approxfunc,NULL);
113: PetscOptionsInt("-snes_ngmres_m", "Number of directions", "SNES",ngmres->msize,&ngmres->msize,NULL);
114: PetscOptionsInt("-snes_ngmres_restart", "Iterations before forced restart", "SNES",ngmres->restart_periodic,&ngmres->restart_periodic,NULL);
115: PetscOptionsInt("-snes_ngmres_restart_it", "Tolerance iterations before restart","SNES",ngmres->restart_it,&ngmres->restart_it,NULL);
116: PetscOptionsBool("-snes_ngmres_monitor", "Monitor actions of NGMRES", "SNES",ngmres->monitor ? PETSC_TRUE : PETSC_FALSE,&debug,NULL);
117: if (debug) {
118: ngmres->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
119: }
120: PetscOptionsReal("-snes_ngmres_gammaA", "Residual selection constant", "SNES",ngmres->gammaA,&ngmres->gammaA,NULL);
121: PetscOptionsReal("-snes_ngmres_gammaC", "Residual restart constant", "SNES",ngmres->gammaC,&ngmres->gammaC,NULL);
122: PetscOptionsReal("-snes_ngmres_epsilonB", "Difference selection constant", "SNES",ngmres->epsilonB,&ngmres->epsilonB,NULL);
123: PetscOptionsReal("-snes_ngmres_deltaB", "Difference residual selection constant", "SNES",ngmres->deltaB,&ngmres->deltaB,NULL);
124: PetscOptionsBool("-snes_ngmres_single_reduction", "Aggregate reductions", "SNES",ngmres->singlereduction,&ngmres->singlereduction,NULL);
125: PetscOptionsTail();
126: if ((ngmres->gammaA > ngmres->gammaC) && (ngmres->gammaC > 2.)) ngmres->gammaC = ngmres->gammaA;
128: /* set the default type of the line search if the user hasn't already. */
129: if (!snes->linesearch) {
130: SNESGetLineSearch(snes,&linesearch);
131: SNESLineSearchSetType(linesearch,SNESLINESEARCHBASIC);
132: }
133: return(0);
134: }
138: PetscErrorCode SNESView_NGMRES(SNES snes,PetscViewer viewer)
139: {
140: SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data;
141: PetscBool iascii;
145: PetscObjectTypeCompare((PetscObject) viewer,PETSCVIEWERASCII,&iascii);
146: if (iascii) {
147: PetscViewerASCIIPrintf(viewer," Number of stored past updates: %d\n", ngmres->msize);
148: PetscViewerASCIIPrintf(viewer," Residual selection: gammaA=%1.0e, gammaC=%1.0e\n",ngmres->gammaA,ngmres->gammaC);
149: PetscViewerASCIIPrintf(viewer," Difference restart: epsilonB=%1.0e, deltaB=%1.0e\n",ngmres->epsilonB,ngmres->deltaB);
150: }
151: return(0);
152: }
156: PetscErrorCode SNESSolve_NGMRES(SNES snes)
157: {
158: SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data;
159: /* present solution, residual, and preconditioned residual */
160: Vec X,F,B,D,Y;
162: /* candidate linear combination answers */
163: Vec XA,FA,XM,FM;
165: /* coefficients and RHS to the minimization problem */
166: PetscReal fnorm,fMnorm,fAnorm;
167: PetscReal xnorm,xMnorm,xAnorm;
168: PetscReal ynorm,yMnorm,yAnorm;
169: PetscInt k,k_restart,l,ivec,restart_count = 0;
171: /* solution selection data */
172: PetscBool selectRestart;
173: PetscReal dnorm,dminnorm = 0.0;
174: PetscReal fminnorm;
176: SNESConvergedReason reason;
177: SNESLineSearchReason lssucceed;
178: PetscErrorCode ierr;
182: if (snes->xl || snes->xu || snes->ops->computevariablebounds) {
183: SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);
184: }
186: PetscCitationsRegister(SNESCitation,&SNEScite);
187: /* variable initialization */
188: snes->reason = SNES_CONVERGED_ITERATING;
189: X = snes->vec_sol;
190: F = snes->vec_func;
191: B = snes->vec_rhs;
192: XA = snes->vec_sol_update;
193: FA = snes->work[0];
194: D = snes->work[1];
196: /* work for the line search */
197: Y = snes->work[2];
198: XM = snes->work[3];
199: FM = snes->work[4];
201: PetscObjectSAWsTakeAccess((PetscObject)snes);
202: snes->iter = 0;
203: snes->norm = 0.;
204: PetscObjectSAWsGrantAccess((PetscObject)snes);
206: /* initialization */
208: if (snes->pc && snes->pcside == PC_LEFT) {
209: SNESApplyNPC(snes,X,NULL,F);
210: SNESGetConvergedReason(snes->pc,&reason);
211: if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
212: snes->reason = SNES_DIVERGED_INNER;
213: return(0);
214: }
215: VecNorm(F,NORM_2,&fnorm);
216: } else {
217: if (!snes->vec_func_init_set) {
218: SNESComputeFunction(snes,X,F);
219: } else snes->vec_func_init_set = PETSC_FALSE;
221: VecNorm(F,NORM_2,&fnorm);
222: SNESCheckFunctionNorm(snes,fnorm);
223: }
224: fminnorm = fnorm;
226: PetscObjectSAWsTakeAccess((PetscObject)snes);
227: snes->norm = fnorm;
228: PetscObjectSAWsGrantAccess((PetscObject)snes);
229: SNESLogConvergenceHistory(snes,fnorm,0);
230: SNESMonitor(snes,0,fnorm);
231: (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
232: if (snes->reason) return(0);
233: SNESNGMRESUpdateSubspace_Private(snes,0,0,F,fnorm,X);
235: k_restart = 1;
236: l = 1;
237: ivec = 0;
238: for (k=1; k < snes->max_its+1; k++) {
239: /* Computation of x^M */
240: if (snes->pc && snes->pcside == PC_RIGHT) {
241: VecCopy(X,XM);
242: SNESSetInitialFunction(snes->pc,F);
244: PetscLogEventBegin(SNES_NPCSolve,snes->pc,XM,B,0);
245: SNESSolve(snes->pc,B,XM);
246: PetscLogEventEnd(SNES_NPCSolve,snes->pc,XM,B,0);
248: SNESGetConvergedReason(snes->pc,&reason);
249: if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
250: snes->reason = SNES_DIVERGED_INNER;
251: return(0);
252: }
253: SNESGetNPCFunction(snes,FM,&fMnorm);
254: } else {
255: /* no preconditioner -- just take gradient descent with line search */
256: VecCopy(F,Y);
257: VecCopy(F,FM);
258: VecCopy(X,XM);
260: fMnorm = fnorm;
262: SNESLineSearchApply(snes->linesearch,XM,FM,&fMnorm,Y);
263: SNESLineSearchGetReason(snes->linesearch,&lssucceed);
264: if (lssucceed) {
265: if (++snes->numFailures >= snes->maxFailures) {
266: snes->reason = SNES_DIVERGED_LINE_SEARCH;
267: return(0);
268: }
269: }
270: }
271: SNESNGMRESFormCombinedSolution_Private(snes,ivec,l,XM,FM,fMnorm,X,XA,FA);
272: /* r = F(x) */
273: if (fminnorm > fMnorm) fminnorm = fMnorm; /* the minimum norm is now of F^M */
275: /* differences for selection and restart */
276: if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE || ngmres->select_type == SNES_NGMRES_SELECT_DIFFERENCE) {
277: SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,&dnorm,&dminnorm,&xMnorm,NULL,&yMnorm,&xAnorm,&fAnorm,&yAnorm);
278: } else {
279: SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,&xMnorm,NULL,&yMnorm,&xAnorm,&fAnorm,&yAnorm);
280: }
281: SNESCheckFunctionNorm(snes,fnorm);
283: /* combination (additive) or selection (multiplicative) of the N-GMRES solution */
284: SNESNGMRESSelect_Private(snes,k_restart,XM,FM,xMnorm,fMnorm,yMnorm,XA,FA,xAnorm,fAnorm,yAnorm,dnorm,fminnorm,dminnorm,X,F,Y,&xnorm,&fnorm,&ynorm);
285: selectRestart = PETSC_FALSE;
286: if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE) {
287: SNESNGMRESSelectRestart_Private(snes,l,fAnorm,dnorm,fminnorm,dminnorm,&selectRestart);
288: /* if the restart conditions persist for more than restart_it iterations, restart. */
289: if (selectRestart) restart_count++;
290: else restart_count = 0;
291: } else if (ngmres->restart_type == SNES_NGMRES_RESTART_PERIODIC) {
292: if (k_restart > ngmres->restart_periodic) {
293: if (ngmres->monitor) PetscViewerASCIIPrintf(ngmres->monitor,"periodic restart after %D iterations\n",k_restart);
294: restart_count = ngmres->restart_it;
295: }
296: }
297: ivec = k_restart % ngmres->msize; /* replace the last used part of the subspace */
298: /* restart after restart conditions have persisted for a fixed number of iterations */
299: if (restart_count >= ngmres->restart_it) {
300: if (ngmres->monitor) {
301: PetscViewerASCIIPrintf(ngmres->monitor,"Restarted at iteration %d\n",k_restart);
302: }
303: restart_count = 0;
304: k_restart = 1;
305: l = 1;
306: ivec = 0;
307: /* q_{00} = nu */
308: SNESNGMRESUpdateSubspace_Private(snes,0,0,FM,fMnorm,XM);
309: } else {
310: /* select the current size of the subspace */
311: if (l < ngmres->msize) l++;
312: k_restart++;
313: /* place the current entry in the list of previous entries */
314: if (ngmres->candidate) {
315: if (fminnorm > fMnorm) fminnorm = fMnorm;
316: SNESNGMRESUpdateSubspace_Private(snes,ivec,l,FM,fMnorm,XM);
317: } else {
318: if (fminnorm > fnorm) fminnorm = fnorm;
319: SNESNGMRESUpdateSubspace_Private(snes,ivec,l,F,fnorm,X);
320: }
321: }
323: PetscObjectSAWsTakeAccess((PetscObject)snes);
324: snes->iter = k;
325: snes->norm = fnorm;
326: PetscObjectSAWsGrantAccess((PetscObject)snes);
327: SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
328: SNESMonitor(snes,snes->iter,snes->norm);
329: (*snes->ops->converged)(snes,snes->iter,0,0,fnorm,&snes->reason,snes->cnvP);
330: if (snes->reason) return(0);
331: }
332: snes->reason = SNES_DIVERGED_MAX_IT;
333: return(0);
334: }
338: /*@
339: SNESNGMRESSetRestartType - Sets the restart type for SNESNGMRES.
341: Logically Collective on SNES
343: Input Parameters:
344: + snes - the iterative context
345: - rtype - restart type
347: Options Database:
348: + -snes_ngmres_restart_type<difference,periodic,none> - set the restart type
349: - -snes_ngmres_restart[30] - sets the number of iterations before restart for periodic
351: Level: intermediate
353: SNESNGMRESRestartTypes:
354: + SNES_NGMRES_RESTART_NONE - never restart
355: . SNES_NGMRES_RESTART_DIFFERENCE - restart based upon difference criteria
356: - SNES_NGMRES_RESTART_PERIODIC - restart after a fixed number of iterations
358: Notes:
359: The default line search used is the L2 line search and it requires two additional function evaluations.
361: .keywords: SNES, SNESNGMRES, restart, type, set SNESLineSearch
362: @*/
363: PetscErrorCode SNESNGMRESSetRestartType(SNES snes,SNESNGMRESRestartType rtype)
364: {
369: PetscTryMethod(snes,"SNESNGMRESSetRestartType_C",(SNES,SNESNGMRESRestartType),(snes,rtype));
370: return(0);
371: }
375: /*@
376: SNESNGMRESSetSelectType - Sets the selection type for SNESNGMRES. This determines how the candidate solution and
377: combined solution are used to create the next iterate.
379: Logically Collective on SNES
381: Input Parameters:
382: + snes - the iterative context
383: - stype - selection type
385: Options Database:
386: . -snes_ngmres_select_type<difference,none,linesearch>
388: Level: intermediate
390: SNESNGMRESSelectTypes:
391: + SNES_NGMRES_SELECT_NONE - choose the combined solution all the time
392: . SNES_NGMRES_SELECT_DIFFERENCE - choose based upon the selection criteria
393: - SNES_NGMRES_SELECT_LINESEARCH - choose based upon line search combination
395: Notes:
396: The default line search used is the L2 line search and it requires two additional function evaluations.
398: .keywords: SNES, SNESNGMRES, selection, type, set SNESLineSearch
399: @*/
400: PetscErrorCode SNESNGMRESSetSelectType(SNES snes,SNESNGMRESSelectType stype)
401: {
406: PetscTryMethod(snes,"SNESNGMRESSetSelectType_C",(SNES,SNESNGMRESSelectType),(snes,stype));
407: return(0);
408: }
412: PetscErrorCode SNESNGMRESSetSelectType_NGMRES(SNES snes,SNESNGMRESSelectType stype)
413: {
414: SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;
417: ngmres->select_type = stype;
418: return(0);
419: }
423: PetscErrorCode SNESNGMRESSetRestartType_NGMRES(SNES snes,SNESNGMRESRestartType rtype)
424: {
425: SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;
428: ngmres->restart_type = rtype;
429: return(0);
430: }
432: /*MC
433: SNESNGMRES - The Nonlinear Generalized Minimum Residual method.
435: Level: beginner
437: Options Database:
438: + -snes_ngmres_select_type<difference,none,linesearch> - choose the select between candidate and combined solution
439: . -snes_ngmres_restart_type<difference,none,periodic> - choose the restart conditions
440: . -snes_ngmres_candidate - Use NGMRES variant which combines candidate solutions instead of actual solutions
441: . -snes_ngmres_m - Number of stored previous solutions and residuals
442: . -snes_ngmres_restart_it - Number of iterations the restart conditions hold before restart
443: . -snes_ngmres_gammaA - Residual tolerance for solution select between the candidate and combination
444: . -snes_ngmres_gammaC - Residual tolerance for restart
445: . -snes_ngmres_epsilonB - Difference tolerance between subsequent solutions triggering restart
446: . -snes_ngmres_deltaB - Difference tolerance between residuals triggering restart
447: . -snes_ngmres_monitor - Prints relevant information about the ngmres iteration
448: . -snes_linesearch_type <basic,l2,cp> - Line search type used for the default smoother
449: - -additive_snes_linesearch_type - linesearch type used to select between the candidate and combined solution with additive select type
451: Notes:
453: The N-GMRES method combines m previous solutions into a minimum-residual solution by solving a small linearized
454: optimization problem at each iteration.
456: References:
458: "Krylov Subspace Acceleration of Nonlinear Multigrid with Application to Recirculating Flows", C. W. Oosterlee and T. Washio,
459: SIAM Journal on Scientific Computing, 21(5), 2000.
461: .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
462: M*/
466: PETSC_EXTERN PetscErrorCode SNESCreate_NGMRES(SNES snes)
467: {
468: SNES_NGMRES *ngmres;
472: snes->ops->destroy = SNESDestroy_NGMRES;
473: snes->ops->setup = SNESSetUp_NGMRES;
474: snes->ops->setfromoptions = SNESSetFromOptions_NGMRES;
475: snes->ops->view = SNESView_NGMRES;
476: snes->ops->solve = SNESSolve_NGMRES;
477: snes->ops->reset = SNESReset_NGMRES;
479: snes->usespc = PETSC_TRUE;
480: snes->usesksp = PETSC_FALSE;
481: snes->pcside = PC_RIGHT;
483: PetscNewLog(snes,&ngmres);
484: snes->data = (void*) ngmres;
485: ngmres->msize = 30;
487: if (!snes->tolerancesset) {
488: snes->max_funcs = 30000;
489: snes->max_its = 10000;
490: }
492: ngmres->candidate = PETSC_FALSE;
494: ngmres->additive_linesearch = NULL;
495: ngmres->approxfunc = PETSC_FALSE;
496: ngmres->restart_it = 2;
497: ngmres->restart_periodic = 30;
498: ngmres->gammaA = 2.0;
499: ngmres->gammaC = 2.0;
500: ngmres->deltaB = 0.9;
501: ngmres->epsilonB = 0.1;
503: ngmres->restart_type = SNES_NGMRES_RESTART_DIFFERENCE;
504: ngmres->select_type = SNES_NGMRES_SELECT_DIFFERENCE;
506: PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetSelectType_C",SNESNGMRESSetSelectType_NGMRES);
507: PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetRestartType_C",SNESNGMRESSetRestartType_NGMRES);
508: return(0);
509: }