Actual source code: fas.c
petsc-3.4.0 2013-05-13
1: /* Defines the basic SNES object */
2: #include <../src/snes/impls/fas/fasimpls.h> /*I "petscsnesfas.h" I*/
4: const char *const SNESFASTypes[] = {"MULTIPLICATIVE","ADDITIVE","SNESFASType","SNES_FAS",0};
6: extern PetscErrorCode SNESDestroy_FAS(SNES snes);
7: extern PetscErrorCode SNESSetUp_FAS(SNES snes);
8: extern PetscErrorCode SNESSetFromOptions_FAS(SNES snes);
9: extern PetscErrorCode SNESView_FAS(SNES snes, PetscViewer viewer);
10: extern PetscErrorCode SNESSolve_FAS(SNES snes);
11: extern PetscErrorCode SNESReset_FAS(SNES snes);
12: extern PetscErrorCode SNESFASGalerkinDefaultFunction(SNES, Vec, Vec, void*);
13: extern PetscErrorCode SNESFASCycleCreateSmoother_Private(SNES, SNES*);
15: /*MC
17: SNESFAS - Full Approximation Scheme nonlinear multigrid solver.
19: The nonlinear problem is solved by correction using coarse versions
20: of the nonlinear problem. This problem is perturbed so that a projected
21: solution of the fine problem elicits no correction from the coarse problem.
23: Options Database:
24: + -snes_fas_levels - The number of levels
25: . -snes_fas_cycles<1> - The number of cycles -- 1 for V, 2 for W
26: . -snes_fas_type<additive, multiplicative> - Additive or multiplicative cycle
27: . -snes_fas_galerkin<PETSC_FALSE> - Form coarse problems by projection back upon the fine problem
28: . -snes_fas_smoothup<1> - The number of iterations of the post-smoother
29: . -snes_fas_smoothdown<1> - The number of iterations of the pre-smoother
30: . -snes_fas_monitor - Monitor progress of all of the levels
31: . -fas_levels_snes_ - SNES options for all smoothers
32: . -fas_levels_cycle_snes_ - SNES options for all cycles
33: . -fas_levels_i_snes_ - SNES options for the smoothers on level i
34: . -fas_levels_i_cycle_snes_ - SNES options for the cycle on level i
35: - -fas_coarse_snes_ - SNES options for the coarsest smoother
37: Notes:
38: The organization of the FAS solver is slightly different from the organization of PCMG
39: As each level has smoother SNES instances(down and potentially up) and a cycle SNES instance.
40: The cycle SNES instance may be used for monitoring convergence on a particular level.
42: Level: beginner
44: .seealso: PCMG, SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
45: M*/
49: PETSC_EXTERN PetscErrorCode SNESCreate_FAS(SNES snes)
50: {
51: SNES_FAS *fas;
55: snes->ops->destroy = SNESDestroy_FAS;
56: snes->ops->setup = SNESSetUp_FAS;
57: snes->ops->setfromoptions = SNESSetFromOptions_FAS;
58: snes->ops->view = SNESView_FAS;
59: snes->ops->solve = SNESSolve_FAS;
60: snes->ops->reset = SNESReset_FAS;
62: snes->usesksp = PETSC_FALSE;
63: snes->usespc = PETSC_FALSE;
65: if (!snes->tolerancesset) {
66: snes->max_funcs = 30000;
67: snes->max_its = 10000;
68: }
70: PetscNewLog(snes, SNES_FAS, &fas);
72: snes->data = (void*) fas;
73: fas->level = 0;
74: fas->levels = 1;
75: fas->n_cycles = 1;
76: fas->max_up_it = 1;
77: fas->max_down_it = 1;
78: fas->smoothu = NULL;
79: fas->smoothd = NULL;
80: fas->next = NULL;
81: fas->previous = NULL;
82: fas->fine = snes;
83: fas->interpolate = NULL;
84: fas->restrct = NULL;
85: fas->inject = NULL;
86: fas->monitor = NULL;
87: fas->usedmfornumberoflevels = PETSC_FALSE;
88: fas->fastype = SNES_FAS_MULTIPLICATIVE;
90: fas->eventsmoothsetup = 0;
91: fas->eventsmoothsolve = 0;
92: fas->eventresidual = 0;
93: fas->eventinterprestrict = 0;
94: return(0);
95: }
99: PetscErrorCode SNESReset_FAS(SNES snes)
100: {
101: PetscErrorCode 0;
102: SNES_FAS * fas = (SNES_FAS*)snes->data;
105: SNESDestroy(&fas->smoothu);
106: SNESDestroy(&fas->smoothd);
107: MatDestroy(&fas->inject);
108: MatDestroy(&fas->interpolate);
109: MatDestroy(&fas->restrct);
110: VecDestroy(&fas->rscale);
111: if (fas->next) SNESReset(fas->next);
112: return(0);
113: }
117: PetscErrorCode SNESDestroy_FAS(SNES snes)
118: {
119: SNES_FAS * fas = (SNES_FAS*)snes->data;
120: PetscErrorCode 0;
123: /* recursively resets and then destroys */
124: SNESReset(snes);
125: if (fas->next) {
126: SNESDestroy(&fas->next);
127: }
128: PetscFree(fas);
129: return(0);
130: }
134: PetscErrorCode SNESSetUp_FAS(SNES snes)
135: {
136: SNES_FAS *fas = (SNES_FAS*) snes->data;
138: VecScatter injscatter;
139: PetscInt dm_levels;
140: Vec vec_sol, vec_func, vec_sol_update, vec_rhs; /* preserve these if they're set through the reset */
141: SNES next;
142: PetscBool isFine;
143: SNESLineSearch linesearch;
144: SNESLineSearch slinesearch;
145: void *lsprectx,*lspostctx;
146: PetscErrorCode (*precheck)(SNESLineSearch,Vec,Vec,PetscBool*,void*);
147: PetscErrorCode (*postcheck)(SNESLineSearch,Vec,Vec,Vec,PetscBool*,PetscBool*,void*);
150: SNESFASCycleIsFine(snes, &isFine);
151: if (fas->usedmfornumberoflevels && isFine) {
152: DMGetRefineLevel(snes->dm,&dm_levels);
153: dm_levels++;
154: if (dm_levels > fas->levels) {
155: /* we don't want the solution and func vectors to be destroyed in the SNESReset when it's called in SNESFASSetLevels_FAS*/
156: vec_sol = snes->vec_sol;
157: vec_func = snes->vec_func;
158: vec_sol_update = snes->vec_sol_update;
159: vec_rhs = snes->vec_rhs;
160: snes->vec_sol = NULL;
161: snes->vec_func = NULL;
162: snes->vec_sol_update = NULL;
163: snes->vec_rhs = NULL;
165: /* reset the number of levels */
166: SNESFASSetLevels(snes,dm_levels,NULL);
167: SNESSetFromOptions(snes);
169: snes->vec_sol = vec_sol;
170: snes->vec_func = vec_func;
171: snes->vec_rhs = vec_rhs;
172: snes->vec_sol_update = vec_sol_update;
173: }
174: }
175: SNESFASCycleGetCorrection(snes, &next);
176: if (!isFine) snes->gridsequence = 0; /* no grid sequencing inside the multigrid hierarchy! */
178: SNESSetWorkVecs(snes, 2); /* work vectors used for intergrid transfers */
180: /* set up the smoothers if they haven't already been set up */
181: if (!fas->smoothd) {
182: SNESFASCycleCreateSmoother_Private(snes, &fas->smoothd);
183: }
185: if (snes->dm) {
186: /* set the smoother DMs properly */
187: if (fas->smoothu) SNESSetDM(fas->smoothu, snes->dm);
188: SNESSetDM(fas->smoothd, snes->dm);
189: /* construct EVERYTHING from the DM -- including the progressive set of smoothers */
190: if (next) {
191: /* for now -- assume the DM and the evaluation functions have been set externally */
192: if (!next->dm) {
193: DMCoarsen(snes->dm, PetscObjectComm((PetscObject)next), &next->dm);
194: SNESSetDM(next, next->dm);
195: }
196: /* set the interpolation and restriction from the DM */
197: if (!fas->interpolate) {
198: DMCreateInterpolation(next->dm, snes->dm, &fas->interpolate, &fas->rscale);
199: if (!fas->restrct) {
200: PetscObjectReference((PetscObject)fas->interpolate);
201: fas->restrct = fas->interpolate;
202: }
203: }
204: /* set the injection from the DM */
205: if (!fas->inject) {
206: DMCreateInjection(next->dm, snes->dm, &injscatter);
207: MatCreateScatter(PetscObjectComm((PetscObject)snes), injscatter, &fas->inject);
208: VecScatterDestroy(&injscatter);
209: }
210: }
211: }
212: /*pass the smoother, function, and jacobian up to the next level if it's not user set already */
213: if (fas->galerkin) {
214: if (next) {
215: SNESSetFunction(next, NULL, SNESFASGalerkinDefaultFunction, next);
216: }
217: if (fas->smoothd && fas->level != fas->levels - 1) {
218: SNESSetFunction(fas->smoothd, NULL, SNESFASGalerkinDefaultFunction, snes);
219: }
220: if (fas->smoothu && fas->level != fas->levels - 1) {
221: SNESSetFunction(fas->smoothu, NULL, SNESFASGalerkinDefaultFunction, snes);
222: }
223: }
225: /* sets the down (pre) smoother's default norm and sets it from options */
226: if (fas->smoothd) {
227: if (fas->level == 0 && fas->levels != 1) {
228: SNESSetNormType(fas->smoothd, SNES_NORM_NONE);
229: } else {
230: SNESSetNormType(fas->smoothd, SNES_NORM_FINAL_ONLY);
231: }
232: PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)fas->smoothd);
233: SNESSetFromOptions(fas->smoothd);
234: SNESGetLineSearch(snes,&linesearch);
235: SNESGetLineSearch(fas->smoothd,&slinesearch);
236: SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
237: SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
238: SNESLineSearchSetPreCheck(slinesearch,precheck,lsprectx);
239: SNESLineSearchSetPostCheck(slinesearch,postcheck,lspostctx);
240: PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)slinesearch);
242: fas->smoothd->vec_sol = snes->vec_sol;
243: PetscObjectReference((PetscObject)snes->vec_sol);
244: fas->smoothd->vec_sol_update = snes->vec_sol_update;
245: PetscObjectReference((PetscObject)snes->vec_sol_update);
246: fas->smoothd->vec_func = snes->vec_func;
247: PetscObjectReference((PetscObject)snes->vec_func);
249: if (fas->eventsmoothsetup) {PetscLogEventBegin(fas->eventsmoothsetup,0,0,0,0);}
250: SNESSetUp(fas->smoothd);
251: if (fas->eventsmoothsetup) {PetscLogEventEnd(fas->eventsmoothsetup,0,0,0,0);}
252: }
254: /* sets the up (post) smoother's default norm and sets it from options */
255: if (fas->smoothu) {
256: if (fas->level != fas->levels - 1) {
257: SNESSetNormType(fas->smoothu, SNES_NORM_NONE);
258: } else {
259: SNESSetNormType(fas->smoothu, SNES_NORM_FINAL_ONLY);
260: }
261: PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)fas->smoothu);
262: SNESSetFromOptions(fas->smoothu);
263: SNESGetLineSearch(snes,&linesearch);
264: SNESGetLineSearch(fas->smoothu,&slinesearch);
265: SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
266: SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
267: SNESLineSearchSetPreCheck(slinesearch,precheck,lsprectx);
268: SNESLineSearchSetPostCheck(slinesearch,postcheck,lspostctx);
269: PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)slinesearch);
271: fas->smoothu->vec_sol = snes->vec_sol;
272: PetscObjectReference((PetscObject)snes->vec_sol);
273: fas->smoothu->vec_sol_update = snes->vec_sol_update;
274: PetscObjectReference((PetscObject)snes->vec_sol_update);
275: fas->smoothu->vec_func = snes->vec_func;
276: PetscObjectReference((PetscObject)snes->vec_func);
278: if (fas->eventsmoothsetup) {PetscLogEventBegin(fas->eventsmoothsetup,0,0,0,0);}
279: SNESSetUp(fas->smoothu);
280: if (fas->eventsmoothsetup) {PetscLogEventEnd(fas->eventsmoothsetup,0,0,0,0);}
282: }
284: if (next) {
285: /* gotta set up the solution vector for this to work */
286: if (!next->vec_sol) {SNESFASCreateCoarseVec(snes,&next->vec_sol);}
287: if (!next->vec_rhs) {SNESFASCreateCoarseVec(snes,&next->vec_rhs);}
288: PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)next);
289: SNESGetLineSearch(snes,&linesearch);
290: SNESGetLineSearch(fas->next,&slinesearch);
291: SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
292: SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
293: SNESLineSearchSetPreCheck(slinesearch,precheck,lsprectx);
294: SNESLineSearchSetPostCheck(slinesearch,postcheck,lspostctx);
295: PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)slinesearch);
296: SNESSetUp(next);
297: }
298: /* setup FAS work vectors */
299: if (fas->galerkin) {
300: VecDuplicate(snes->vec_sol, &fas->Xg);
301: VecDuplicate(snes->vec_sol, &fas->Fg);
302: }
303: return(0);
304: }
308: PetscErrorCode SNESSetFromOptions_FAS(SNES snes)
309: {
310: SNES_FAS *fas = (SNES_FAS*) snes->data;
311: PetscInt levels = 1;
312: PetscBool flg = PETSC_FALSE, upflg = PETSC_FALSE, downflg = PETSC_FALSE, monflg = PETSC_FALSE, galerkinflg = PETSC_FALSE;
314: char monfilename[PETSC_MAX_PATH_LEN];
315: SNESFASType fastype;
316: const char *optionsprefix;
317: SNESLineSearch linesearch;
318: PetscInt m, n_up, n_down;
319: SNES next;
320: PetscBool isFine;
323: SNESFASCycleIsFine(snes, &isFine);
324: PetscOptionsHead("SNESFAS Options-----------------------------------");
326: /* number of levels -- only process most options on the finest level */
327: if (isFine) {
328: PetscOptionsInt("-snes_fas_levels", "Number of Levels", "SNESFASSetLevels", levels, &levels, &flg);
329: if (!flg && snes->dm) {
330: DMGetRefineLevel(snes->dm,&levels);
331: levels++;
332: fas->usedmfornumberoflevels = PETSC_TRUE;
333: }
334: SNESFASSetLevels(snes, levels, NULL);
335: fastype = fas->fastype;
336: PetscOptionsEnum("-snes_fas_type","FAS correction type","SNESFASSetType",SNESFASTypes,(PetscEnum)fastype,(PetscEnum*)&fastype,&flg);
337: if (flg) {
338: SNESFASSetType(snes, fastype);
339: }
341: SNESGetOptionsPrefix(snes, &optionsprefix);
342: PetscOptionsInt("-snes_fas_cycles","Number of cycles","SNESFASSetCycles",fas->n_cycles,&m,&flg);
343: if (flg) {
344: SNESFASSetCycles(snes, m);
345: }
347: PetscOptionsBool("-snes_fas_galerkin", "Form coarse problems with Galerkin","SNESFASSetGalerkin",fas->galerkin,&galerkinflg,&flg);
348: if (flg) {
349: SNESFASSetGalerkin(snes, galerkinflg);
350: }
352: PetscOptionsInt("-snes_fas_smoothup","Number of post-smoothing steps","SNESFASSetNumberSmoothUp",fas->max_up_it,&n_up,&upflg);
354: PetscOptionsInt("-snes_fas_smoothdown","Number of pre-smoothing steps","SNESFASSetNumberSmoothDown",fas->max_down_it,&n_down,&downflg);
356: PetscOptionsString("-snes_fas_monitor","Monitor FAS progress","SNESFASSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&monflg);
357: if (monflg) SNESFASSetMonitor(snes, PETSC_TRUE);
359: flg = PETSC_FALSE;
360: monflg = PETSC_TRUE;
361: PetscOptionsBool("-snes_fas_log","Log times for each FAS level","SNESFASSetLog",monflg,&monflg,&flg);
362: if (flg) {SNESFASSetLog(snes,monflg);}
363: }
365: PetscOptionsTail();
366: /* setup from the determined types if there is no pointwise procedure or smoother defined */
367: if (upflg) {
368: SNESFASSetNumberSmoothUp(snes,n_up);
369: }
370: if (downflg) {
371: SNESFASSetNumberSmoothDown(snes,n_down);
372: }
374: /* set up the default line search for coarse grid corrections */
375: if (fas->fastype == SNES_FAS_ADDITIVE) {
376: if (!snes->linesearch) {
377: SNESGetLineSearch(snes, &linesearch);
378: SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);
379: }
380: }
382: SNESFASCycleGetCorrection(snes, &next);
383: /* recursive option setting for the smoothers */
384: if (next) {SNESSetFromOptions(next);}
385: return(0);
386: }
388: #include <petscdraw.h>
391: PetscErrorCode SNESView_FAS(SNES snes, PetscViewer viewer)
392: {
393: SNES_FAS *fas = (SNES_FAS*) snes->data;
394: PetscBool isFine,iascii,isdraw;
395: PetscInt i;
397: SNES smoothu, smoothd, levelsnes;
400: SNESFASCycleIsFine(snes, &isFine);
401: if (isFine) {
402: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
403: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
404: if (iascii) {
405: PetscViewerASCIIPrintf(viewer, "FAS: type is %s, levels=%D, cycles=%D\n", SNESFASTypes[fas->fastype], fas->levels, fas->n_cycles);
406: if (fas->galerkin) {
407: PetscViewerASCIIPrintf(viewer," Using Galerkin computed coarse grid function evaluation\n");
408: } else {
409: PetscViewerASCIIPrintf(viewer," Not using Galerkin computed coarse grid function evaluation\n");
410: }
411: for (i=0; i<fas->levels; i++) {
412: SNESFASGetCycleSNES(snes, i, &levelsnes);
413: SNESFASCycleGetSmootherUp(levelsnes, &smoothu);
414: SNESFASCycleGetSmootherDown(levelsnes, &smoothd);
415: if (!i) {
416: PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level %D -------------------------------\n",i);
417: } else {
418: PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);
419: }
420: PetscViewerASCIIPushTab(viewer);
421: SNESView(smoothd,viewer);
422: PetscViewerASCIIPopTab(viewer);
423: if (i && (smoothd == smoothu)) {
424: PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");
425: } else if (i) {
426: PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);
427: PetscViewerASCIIPushTab(viewer);
428: SNESView(smoothu,viewer);
429: PetscViewerASCIIPopTab(viewer);
430: }
431: }
432: } else if (isdraw) {
433: PetscDraw draw;
434: PetscReal x,w,y,bottom,th,wth;
435: SNES_FAS *curfas = fas;
436: PetscViewerDrawGetDraw(viewer,0,&draw);
437: PetscDrawGetCurrentPoint(draw,&x,&y);
438: PetscDrawStringGetSize(draw,&wth,&th);
439: bottom = y - th;
440: while (curfas) {
441: if (!curfas->smoothu) {
442: PetscDrawPushCurrentPoint(draw,x,bottom);
443: if (curfas->smoothd) SNESView(curfas->smoothd,viewer);
444: PetscDrawPopCurrentPoint(draw);
445: } else {
446: w = 0.5*PetscMin(1.0-x,x);
447: PetscDrawPushCurrentPoint(draw,x-w,bottom);
448: if (curfas->smoothd) SNESView(curfas->smoothd,viewer);
449: PetscDrawPopCurrentPoint(draw);
450: PetscDrawPushCurrentPoint(draw,x+w,bottom);
451: if (curfas->smoothu) SNESView(curfas->smoothu,viewer);
452: PetscDrawPopCurrentPoint(draw);
453: }
454: /* this is totally bogus but we have no way of knowing how low the previous one was draw to */
455: bottom -= 5*th;
456: if (curfas->next) curfas = (SNES_FAS*)curfas->next->data;
457: else curfas = NULL;
458: }
459: }
460: }
461: return(0);
462: }
466: /*
467: Defines the action of the downsmoother
468: */
469: PetscErrorCode SNESFASDownSmooth_Private(SNES snes, Vec B, Vec X, Vec F, PetscReal *fnorm)
470: {
471: PetscErrorCode 0;
472: SNESConvergedReason reason;
473: Vec FPC;
474: SNES smoothd;
475: SNES_FAS *fas = (SNES_FAS*) snes->data;
478: SNESFASCycleGetSmootherDown(snes, &smoothd);
479: SNESSetInitialFunction(smoothd, F);
480: SNESSetInitialFunctionNorm(smoothd, *fnorm);
481: if (fas->eventsmoothsolve) {PetscLogEventBegin(fas->eventsmoothsolve,0,0,0,0);}
482: SNESSolve(smoothd, B, X);
483: if (fas->eventsmoothsolve) {PetscLogEventEnd(fas->eventsmoothsolve,0,0,0,0);}
484: /* check convergence reason for the smoother */
485: SNESGetConvergedReason(smoothd,&reason);
486: if (reason < 0 && !(reason == SNES_DIVERGED_MAX_IT || reason == SNES_DIVERGED_LOCAL_MIN)) {
487: snes->reason = SNES_DIVERGED_INNER;
488: return(0);
489: }
490: SNESGetFunction(smoothd, &FPC, NULL, NULL);
491: VecCopy(FPC, F);
492: SNESGetFunctionNorm(smoothd, fnorm);
493: return(0);
494: }
499: /*
500: Defines the action of the upsmoother
501: */
502: PetscErrorCode SNESFASUpSmooth_Private(SNES snes, Vec B, Vec X, Vec F, PetscReal *fnorm)
503: {
504: PetscErrorCode 0;
505: SNESConvergedReason reason;
506: Vec FPC;
507: SNES smoothu;
508: SNES_FAS *fas = (SNES_FAS*) snes->data;
511: SNESFASCycleGetSmootherUp(snes, &smoothu);
512: if (fas->eventsmoothsolve) {PetscLogEventBegin(fas->eventsmoothsolve,0,0,0,0);}
513: SNESSolve(smoothu, B, X);
514: if (fas->eventsmoothsolve) {PetscLogEventEnd(fas->eventsmoothsolve,0,0,0,0);}
515: /* check convergence reason for the smoother */
516: SNESGetConvergedReason(smoothu,&reason);
517: if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
518: snes->reason = SNES_DIVERGED_INNER;
519: return(0);
520: }
521: SNESGetFunction(smoothu, &FPC, NULL, NULL);
522: VecCopy(FPC, F);
523: SNESGetFunctionNorm(smoothu, fnorm);
524: return(0);
525: }
529: /*@
530: SNESFASCreateCoarseVec - create Vec corresponding to a state vector on one level coarser than current level
532: Collective
534: Input Arguments:
535: . snes - SNESFAS
537: Output Arguments:
538: . Xcoarse - vector on level one coarser than snes
540: Level: developer
542: .seealso: SNESFASSetRestriction(), SNESFASRestrict()
543: @*/
544: PetscErrorCode SNESFASCreateCoarseVec(SNES snes,Vec *Xcoarse)
545: {
547: SNES_FAS *fas = (SNES_FAS*)snes->data;
550: if (fas->rscale) {
551: VecDuplicate(fas->rscale,Xcoarse);
552: } else if (fas->inject) {
553: MatGetVecs(fas->inject,Xcoarse,NULL);
554: } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must set restriction or injection");
555: return(0);
556: }
560: /*@
561: SNESFASRestrict - restrict a Vec to the next coarser level
563: Collective
565: Input Arguments:
566: + fine - SNES from which to restrict
567: - Xfine - vector to restrict
569: Output Arguments:
570: . Xcoarse - result of restriction
572: Level: developer
574: .seealso: SNESFASSetRestriction(), SNESFASSetInjection()
575: @*/
576: PetscErrorCode SNESFASRestrict(SNES fine,Vec Xfine,Vec Xcoarse)
577: {
579: SNES_FAS *fas = (SNES_FAS*)fine->data;
585: if (fas->inject) {
586: MatRestrict(fas->inject,Xfine,Xcoarse);
587: } else {
588: MatRestrict(fas->restrct,Xfine,Xcoarse);
589: VecPointwiseMult(Xcoarse,fas->rscale,Xcoarse);
590: }
591: return(0);
592: }
596: /*
598: Performs the FAS coarse correction as:
600: fine problem: F(x) = 0
601: coarse problem: F^c(x) = b^c
603: b^c = F^c(I^c_fx^f - I^c_fF(x))
605: */
606: PetscErrorCode SNESFASCoarseCorrection(SNES snes, Vec X, Vec F, Vec X_new)
607: {
608: PetscErrorCode ierr;
609: Vec X_c, Xo_c, F_c, B_c;
610: SNESConvergedReason reason;
611: SNES next;
612: Mat restrct, interpolate;
613: SNES_FAS *fasc;
616: SNESFASCycleGetCorrection(snes, &next);
617: if (next) {
618: fasc = (SNES_FAS*)next->data;
620: SNESFASCycleGetRestriction(snes, &restrct);
621: SNESFASCycleGetInterpolation(snes, &interpolate);
623: X_c = next->vec_sol;
624: Xo_c = next->work[0];
625: F_c = next->vec_func;
626: B_c = next->vec_rhs;
628: if (fasc->eventinterprestrict) {PetscLogEventBegin(fasc->eventinterprestrict,0,0,0,0);}
629: SNESFASRestrict(snes,X,Xo_c);
630: /* restrict the defect */
631: MatRestrict(restrct, F, B_c);
632: if (fasc->eventinterprestrict) {PetscLogEventEnd(fasc->eventinterprestrict,0,0,0,0);}
634: if (fasc->eventresidual) {PetscLogEventBegin(fasc->eventresidual,0,0,0,0);}
635: SNESComputeFunction(next, Xo_c, F_c);
636: if (fasc->eventresidual) {PetscLogEventEnd(fasc->eventresidual,0,0,0,0);}
638: /* solve the coarse problem corresponding to F^c(x^c) = b^c = F^c(Rx) - R(F(x) - b) */
639: VecCopy(B_c, X_c);
640: VecCopy(F_c, B_c);
641: VecCopy(X_c, F_c);
642: /* set initial guess of the coarse problem to the projected fine solution */
643: VecCopy(Xo_c, X_c);
645: /* recurse to the next level */
646: SNESSetInitialFunction(next, F_c);
647: SNESSolve(next, B_c, X_c);
648: SNESGetConvergedReason(next,&reason);
649: if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
650: snes->reason = SNES_DIVERGED_INNER;
651: return(0);
652: }
653: /* correct as x <- x + I(x^c - Rx)*/
654: VecAXPY(X_c, -1.0, Xo_c);
656: if (fasc->eventinterprestrict) {PetscLogEventBegin(fasc->eventinterprestrict,0,0,0,0);}
657: MatInterpolateAdd(interpolate, X_c, X, X_new);
658: if (fasc->eventinterprestrict) {PetscLogEventEnd(fasc->eventinterprestrict,0,0,0,0);}
659: }
660: return(0);
661: }
665: /*
667: The additive cycle looks like:
669: xhat = x
670: xhat = dS(x, b)
671: x = coarsecorrection(xhat, b_d)
672: x = x + nu*(xhat - x);
673: (optional) x = uS(x, b)
675: With the coarse RHS (defect correction) as below.
677: */
678: PetscErrorCode SNESFASCycle_Additive(SNES snes, Vec X)
679: {
680: Vec F, B, Xhat;
681: Vec X_c, Xo_c, F_c, B_c;
682: PetscErrorCode ierr;
683: SNESConvergedReason reason;
684: PetscReal xnorm, fnorm, ynorm;
685: PetscBool lssuccess;
686: SNES next;
687: Mat restrct, interpolate;
688: SNES_FAS *fas = (SNES_FAS*)snes->data,*fasc;
691: SNESFASCycleGetCorrection(snes, &next);
692: F = snes->vec_func;
693: B = snes->vec_rhs;
694: Xhat = snes->work[1];
695: VecCopy(X, Xhat);
696: /* recurse first */
697: if (next) {
698: fasc = (SNES_FAS*)next->data;
699: SNESFASCycleGetRestriction(snes, &restrct);
700: SNESFASCycleGetInterpolation(snes, &interpolate);
701: if (fas->eventresidual) {PetscLogEventBegin(fas->eventresidual,0,0,0,0);}
702: SNESComputeFunction(snes, Xhat, F);
703: if (fas->eventresidual) {PetscLogEventEnd(fas->eventresidual,0,0,0,0);}
704: VecNorm(F, NORM_2, &fnorm);
705: X_c = next->vec_sol;
706: Xo_c = next->work[0];
707: F_c = next->vec_func;
708: B_c = next->vec_rhs;
710: SNESFASRestrict(snes,Xhat,Xo_c);
711: /* restrict the defect */
712: MatRestrict(restrct, F, B_c);
714: /* solve the coarse problem corresponding to F^c(x^c) = b^c = Rb + F^c(Rx) - RF(x) */
715: if (fasc->eventresidual) {PetscLogEventBegin(fasc->eventresidual,0,0,0,0);}
716: SNESComputeFunction(next, Xo_c, F_c);
717: if (fasc->eventresidual) {PetscLogEventEnd(fasc->eventresidual,0,0,0,0);}
718: VecCopy(B_c, X_c);
719: VecCopy(F_c, B_c);
720: VecCopy(X_c, F_c);
721: /* set initial guess of the coarse problem to the projected fine solution */
722: VecCopy(Xo_c, X_c);
724: /* recurse */
725: SNESSetInitialFunction(next, F_c);
726: SNESSolve(next, B_c, X_c);
728: /* smooth on this level */
729: SNESFASDownSmooth_Private(snes, B, X, F, &fnorm);
731: SNESGetConvergedReason(next,&reason);
732: if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
733: snes->reason = SNES_DIVERGED_INNER;
734: return(0);
735: }
737: /* correct as x <- x + I(x^c - Rx)*/
738: VecAYPX(X_c, -1.0, Xo_c);
739: MatInterpolate(interpolate, X_c, Xhat);
741: /* additive correction of the coarse direction*/
742: SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Xhat);
743: SNESLineSearchGetSuccess(snes->linesearch, &lssuccess);
744: if (!lssuccess) {
745: if (++snes->numFailures >= snes->maxFailures) {
746: snes->reason = SNES_DIVERGED_LINE_SEARCH;
747: return(0);
748: }
749: }
750: SNESLineSearchGetNorms(snes->linesearch, &xnorm, &snes->norm, &ynorm);
751: } else {
752: SNESFASDownSmooth_Private(snes, B, X, F, &snes->norm);
753: }
754: return(0);
755: }
759: /*
761: Defines the FAS cycle as:
763: fine problem: F(x) = 0
764: coarse problem: F^c(x) = b^c
766: b^c = F^c(I^c_fx^f - I^c_fF(x))
768: correction:
770: x = x + I(x^c - Rx)
772: */
773: PetscErrorCode SNESFASCycle_Multiplicative(SNES snes, Vec X)
774: {
777: Vec F,B;
778: SNES_FAS *fas = (SNES_FAS*)snes->data;
781: F = snes->vec_func;
782: B = snes->vec_rhs;
783: /* pre-smooth -- just update using the pre-smoother */
784: SNESFASDownSmooth_Private(snes, B, X, F, &snes->norm);
785: if (fas->level != 0) {
786: SNESFASCoarseCorrection(snes, X, F, X);
787: SNESFASUpSmooth_Private(snes, B, X, F, &snes->norm);
788: }
789: return(0);
790: }
795: PetscErrorCode SNESSolve_FAS(SNES snes)
796: {
798: PetscInt i, maxits;
799: Vec X, F;
800: PetscReal fnorm;
801: SNES_FAS *fas = (SNES_FAS*)snes->data,*ffas;
802: DM dm;
803: PetscBool isFine;
806: maxits = snes->max_its; /* maximum number of iterations */
807: snes->reason = SNES_CONVERGED_ITERATING;
808: X = snes->vec_sol;
809: F = snes->vec_func;
811: SNESFASCycleIsFine(snes, &isFine);
812: /*norm setup */
813: PetscObjectAMSTakeAccess((PetscObject)snes);
814: snes->iter = 0;
815: snes->norm = 0.;
816: PetscObjectAMSGrantAccess((PetscObject)snes);
817: if (!snes->vec_func_init_set) {
818: if (fas->eventresidual) {PetscLogEventBegin(fas->eventresidual,0,0,0,0);}
819: SNESComputeFunction(snes,X,F);
820: if (fas->eventresidual) {PetscLogEventEnd(fas->eventresidual,0,0,0,0);}
821: if (snes->domainerror) {
822: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
823: return(0);
824: }
825: } else snes->vec_func_init_set = PETSC_FALSE;
827: if (!snes->norm_init_set) {
828: VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F|| */
829: if (PetscIsInfOrNanReal(fnorm)) {
830: snes->reason = SNES_DIVERGED_FNORM_NAN;
831: return(0);
832: }
833: } else {
834: fnorm = snes->norm_init;
835: snes->norm_init_set = PETSC_FALSE;
836: }
838: PetscObjectAMSTakeAccess((PetscObject)snes);
839: snes->norm = fnorm;
840: PetscObjectAMSGrantAccess((PetscObject)snes);
841: SNESLogConvergenceHistory(snes,fnorm,0);
842: SNESMonitor(snes,0,fnorm);
844: /* set parameter for default relative tolerance convergence test */
845: snes->ttol = fnorm*snes->rtol;
846: /* test convergence */
847: (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
848: if (snes->reason) return(0);
851: if (isFine) {
852: /* propagate scale-dependent data up the hierarchy */
853: SNESGetDM(snes,&dm);
854: for (ffas=fas; ffas->next; ffas=(SNES_FAS*)ffas->next->data) {
855: DM dmcoarse;
856: SNESGetDM(ffas->next,&dmcoarse);
857: DMRestrict(dm,ffas->restrct,ffas->rscale,ffas->inject,dmcoarse);
858: dm = dmcoarse;
859: }
860: }
862: for (i = 0; i < maxits; i++) {
863: /* Call general purpose update function */
865: if (snes->ops->update) {
866: (*snes->ops->update)(snes, snes->iter);
867: }
868: if (fas->fastype == SNES_FAS_MULTIPLICATIVE) {
869: SNESFASCycle_Multiplicative(snes, X);
870: } else {
871: SNESFASCycle_Additive(snes, X);
872: }
874: /* check for FAS cycle divergence */
875: if (snes->reason != SNES_CONVERGED_ITERATING) return(0);
877: /* Monitor convergence */
878: PetscObjectAMSTakeAccess((PetscObject)snes);
879: snes->iter = i+1;
880: PetscObjectAMSGrantAccess((PetscObject)snes);
881: SNESLogConvergenceHistory(snes,snes->norm,0);
882: SNESMonitor(snes,snes->iter,snes->norm);
883: /* Test for convergence */
884: if (isFine) {
885: (*snes->ops->converged)(snes,snes->iter,0.0,0.0,snes->norm,&snes->reason,snes->cnvP);
886: if (snes->reason) break;
887: }
888: }
889: if (i == maxits) {
890: PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);
891: if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
892: }
893: return(0);
894: }