Actual source code: fas.c

petsc-dev 2014-02-02
Report Typos and Errors
  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","FULL","KASKADE","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,full,kaskade>  -  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: .   -snes_fas_full_downsweepsmooth<PETSC_FALSE> - call the downsmooth on the initial downsweep of full FAS
 32: .   -fas_levels_snes_ -  SNES options for all smoothers
 33: .   -fas_levels_cycle_snes_ -  SNES options for all cycles
 34: .   -fas_levels_i_snes_ -  SNES options for the smoothers on level i
 35: .   -fas_levels_i_cycle_snes_ - SNES options for the cycle on level i
 36: -   -fas_coarse_snes_ -  SNES options for the coarsest smoother

 38: Notes:
 39:    The organization of the FAS solver is slightly different from the organization of PCMG
 40:    As each level has smoother SNES instances(down and potentially up) and a cycle SNES instance.
 41:    The cycle SNES instance may be used for monitoring convergence on a particular level.

 43: Level: beginner

 45: .seealso: PCMG, SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
 46: M*/

 50: PETSC_EXTERN PetscErrorCode SNESCreate_FAS(SNES snes)
 51: {
 52:   SNES_FAS       *fas;

 56:   snes->ops->destroy        = SNESDestroy_FAS;
 57:   snes->ops->setup          = SNESSetUp_FAS;
 58:   snes->ops->setfromoptions = SNESSetFromOptions_FAS;
 59:   snes->ops->view           = SNESView_FAS;
 60:   snes->ops->solve          = SNESSolve_FAS;
 61:   snes->ops->reset          = SNESReset_FAS;

 63:   snes->usesksp = PETSC_FALSE;
 64:   snes->usespc  = PETSC_FALSE;

 66:   if (!snes->tolerancesset) {
 67:     snes->max_funcs = 30000;
 68:     snes->max_its   = 10000;
 69:   }

 71:   PetscNewLog(snes,&fas);

 73:   snes->data                  = (void*) fas;
 74:   fas->level                  = 0;
 75:   fas->levels                 = 1;
 76:   fas->n_cycles               = 1;
 77:   fas->max_up_it              = 1;
 78:   fas->max_down_it            = 1;
 79:   fas->smoothu                = NULL;
 80:   fas->smoothd                = NULL;
 81:   fas->next                   = NULL;
 82:   fas->previous               = NULL;
 83:   fas->fine                   = snes;
 84:   fas->interpolate            = NULL;
 85:   fas->restrct                = NULL;
 86:   fas->inject                 = NULL;
 87:   fas->monitor                = NULL;
 88:   fas->usedmfornumberoflevels = PETSC_FALSE;
 89:   fas->fastype                = SNES_FAS_MULTIPLICATIVE;
 90:   fas->full_downsweep         = PETSC_FALSE;

 92:   fas->eventsmoothsetup    = 0;
 93:   fas->eventsmoothsolve    = 0;
 94:   fas->eventresidual       = 0;
 95:   fas->eventinterprestrict = 0;
 96:   return(0);
 97: }

101: PetscErrorCode SNESReset_FAS(SNES snes)
102: {
103:   PetscErrorCode 0;
104:   SNES_FAS       * fas = (SNES_FAS*)snes->data;

107:   SNESDestroy(&fas->smoothu);
108:   SNESDestroy(&fas->smoothd);
109:   MatDestroy(&fas->inject);
110:   MatDestroy(&fas->interpolate);
111:   MatDestroy(&fas->restrct);
112:   VecDestroy(&fas->rscale);
113:   if (fas->galerkin) {
114:     VecDestroy(&fas->Xg);
115:     VecDestroy(&fas->Fg);
116:   }
117:   if (fas->next) {SNESReset(fas->next);}
118:   return(0);
119: }

123: PetscErrorCode SNESDestroy_FAS(SNES snes)
124: {
125:   SNES_FAS       * fas = (SNES_FAS*)snes->data;
126:   PetscErrorCode 0;

129:   /* recursively resets and then destroys */
130:   SNESReset(snes);
131:   if (fas->next) {
132:     SNESDestroy(&fas->next);
133:   }
134:   PetscFree(fas);
135:   return(0);
136: }

140: PetscErrorCode SNESSetUp_FAS(SNES snes)
141: {
142:   SNES_FAS       *fas = (SNES_FAS*) snes->data;
144:   VecScatter     injscatter;
145:   PetscInt       dm_levels;
146:   Vec            vec_sol, vec_func, vec_sol_update, vec_rhs; /* preserve these if they're set through the reset */
147:   SNES           next;
148:   PetscBool      isFine;
149:   SNESLineSearch linesearch;
150:   SNESLineSearch slinesearch;
151:   void           *lsprectx,*lspostctx;
152:   PetscErrorCode (*precheck)(SNESLineSearch,Vec,Vec,PetscBool*,void*);
153:   PetscErrorCode (*postcheck)(SNESLineSearch,Vec,Vec,Vec,PetscBool*,PetscBool*,void*);

156:   SNESFASCycleIsFine(snes, &isFine);
157:   if (fas->usedmfornumberoflevels && isFine) {
158:     DMGetRefineLevel(snes->dm,&dm_levels);
159:     dm_levels++;
160:     if (dm_levels > fas->levels) {
161:       /* we don't want the solution and func vectors to be destroyed in the SNESReset when it's called in SNESFASSetLevels_FAS*/
162:       vec_sol              = snes->vec_sol;
163:       vec_func             = snes->vec_func;
164:       vec_sol_update       = snes->vec_sol_update;
165:       vec_rhs              = snes->vec_rhs;
166:       snes->vec_sol        = NULL;
167:       snes->vec_func       = NULL;
168:       snes->vec_sol_update = NULL;
169:       snes->vec_rhs        = NULL;

171:       /* reset the number of levels */
172:       SNESFASSetLevels(snes,dm_levels,NULL);
173:       SNESSetFromOptions(snes);

175:       snes->vec_sol        = vec_sol;
176:       snes->vec_func       = vec_func;
177:       snes->vec_rhs        = vec_rhs;
178:       snes->vec_sol_update = vec_sol_update;
179:     }
180:   }
181:   SNESFASCycleGetCorrection(snes, &next);
182:   if (!isFine) snes->gridsequence = 0; /* no grid sequencing inside the multigrid hierarchy! */

184:   SNESSetWorkVecs(snes, 2); /* work vectors used for intergrid transfers */

186:   /* set up the smoothers if they haven't already been set up */
187:   if (!fas->smoothd) {
188:     SNESFASCycleCreateSmoother_Private(snes, &fas->smoothd);
189:   }

191:   if (snes->dm) {
192:     /* set the smoother DMs properly */
193:     if (fas->smoothu) SNESSetDM(fas->smoothu, snes->dm);
194:     SNESSetDM(fas->smoothd, snes->dm);
195:     /* construct EVERYTHING from the DM -- including the progressive set of smoothers */
196:     if (next) {
197:       /* for now -- assume the DM and the evaluation functions have been set externally */
198:       if (!next->dm) {
199:         DMCoarsen(snes->dm, PetscObjectComm((PetscObject)next), &next->dm);
200:         SNESSetDM(next, next->dm);
201:       }
202:       /* set the interpolation and restriction from the DM */
203:       if (!fas->interpolate) {
204:         DMCreateInterpolation(next->dm, snes->dm, &fas->interpolate, &fas->rscale);
205:         if (!fas->restrct) {
206:           PetscObjectReference((PetscObject)fas->interpolate);
207:           fas->restrct = fas->interpolate;
208:         }
209:       }
210:       /* set the injection from the DM */
211:       if (!fas->inject) {
212:         DMCreateInjection(next->dm, snes->dm, &injscatter);
213:         MatCreateScatter(PetscObjectComm((PetscObject)snes), injscatter, &fas->inject);
214:         VecScatterDestroy(&injscatter);
215:       }
216:     }
217:   }
218:   /*pass the smoother, function, and jacobian up to the next level if it's not user set already */
219:   if (fas->galerkin) {
220:     if (next) {
221:       SNESSetFunction(next, NULL, SNESFASGalerkinDefaultFunction, next);
222:     }
223:     if (fas->smoothd && fas->level != fas->levels - 1) {
224:       SNESSetFunction(fas->smoothd, NULL, SNESFASGalerkinDefaultFunction, snes);
225:     }
226:     if (fas->smoothu && fas->level != fas->levels - 1) {
227:       SNESSetFunction(fas->smoothu, NULL, SNESFASGalerkinDefaultFunction, snes);
228:     }
229:   }

231:   /* sets the down (pre) smoother's default norm and sets it from options */
232:   if (fas->smoothd) {
233:     if (fas->level == 0 && fas->levels != 1) {
234:       SNESSetNormSchedule(fas->smoothd, SNES_NORM_NONE);
235:     } else {
236:       SNESSetNormSchedule(fas->smoothd, SNES_NORM_FINAL_ONLY);
237:     }
238:     PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)fas->smoothd);
239:     SNESSetFromOptions(fas->smoothd);
240:     SNESGetLineSearch(snes,&linesearch);
241:     SNESGetLineSearch(fas->smoothd,&slinesearch);
242:     SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
243:     SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
244:     SNESLineSearchSetPreCheck(slinesearch,precheck,lsprectx);
245:     SNESLineSearchSetPostCheck(slinesearch,postcheck,lspostctx);
246:     PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)slinesearch);

248:     fas->smoothd->vec_sol        = snes->vec_sol;
249:     PetscObjectReference((PetscObject)snes->vec_sol);
250:     fas->smoothd->vec_sol_update = snes->vec_sol_update;
251:     PetscObjectReference((PetscObject)snes->vec_sol_update);
252:     fas->smoothd->vec_func       = snes->vec_func;
253:     PetscObjectReference((PetscObject)snes->vec_func);

255:     if (fas->eventsmoothsetup) {PetscLogEventBegin(fas->eventsmoothsetup,0,0,0,0);}
256:     SNESSetUp(fas->smoothd);
257:     if (fas->eventsmoothsetup) {PetscLogEventEnd(fas->eventsmoothsetup,0,0,0,0);}
258:   }

260:   /* sets the up (post) smoother's default norm and sets it from options */
261:   if (fas->smoothu) {
262:     if (fas->level != fas->levels - 1) {
263:       SNESSetNormSchedule(fas->smoothu, SNES_NORM_NONE);
264:     } else {
265:       SNESSetNormSchedule(fas->smoothu, SNES_NORM_FINAL_ONLY);
266:     }
267:     PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)fas->smoothu);
268:     SNESSetFromOptions(fas->smoothu);
269:     SNESGetLineSearch(snes,&linesearch);
270:     SNESGetLineSearch(fas->smoothu,&slinesearch);
271:     SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
272:     SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
273:     SNESLineSearchSetPreCheck(slinesearch,precheck,lsprectx);
274:     SNESLineSearchSetPostCheck(slinesearch,postcheck,lspostctx);
275:     PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)slinesearch);

277:     fas->smoothu->vec_sol        = snes->vec_sol;
278:     PetscObjectReference((PetscObject)snes->vec_sol);
279:     fas->smoothu->vec_sol_update = snes->vec_sol_update;
280:     PetscObjectReference((PetscObject)snes->vec_sol_update);
281:     fas->smoothu->vec_func       = snes->vec_func;
282:     PetscObjectReference((PetscObject)snes->vec_func);

284:     if (fas->eventsmoothsetup) {PetscLogEventBegin(fas->eventsmoothsetup,0,0,0,0);}
285:     SNESSetUp(fas->smoothu);
286:     if (fas->eventsmoothsetup) {PetscLogEventEnd(fas->eventsmoothsetup,0,0,0,0);}

288:   }

290:   if (next) {
291:     /* gotta set up the solution vector for this to work */
292:     if (!next->vec_sol) {SNESFASCreateCoarseVec(snes,&next->vec_sol);}
293:     if (!next->vec_rhs) {SNESFASCreateCoarseVec(snes,&next->vec_rhs);}
294:     PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)next);
295:     SNESGetLineSearch(snes,&linesearch);
296:     SNESGetLineSearch(fas->next,&slinesearch);
297:     SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
298:     SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
299:     SNESLineSearchSetPreCheck(slinesearch,precheck,lsprectx);
300:     SNESLineSearchSetPostCheck(slinesearch,postcheck,lspostctx);
301:     PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)slinesearch);
302:     SNESSetUp(next);
303:   }
304:   /* setup FAS work vectors */
305:   if (fas->galerkin) {
306:     VecDuplicate(snes->vec_sol, &fas->Xg);
307:     VecDuplicate(snes->vec_sol, &fas->Fg);
308:   }
309:   return(0);
310: }

314: PetscErrorCode SNESSetFromOptions_FAS(SNES snes)
315: {
316:   SNES_FAS       *fas   = (SNES_FAS*) snes->data;
317:   PetscInt       levels = 1;
318:   PetscBool      flg    = PETSC_FALSE, upflg = PETSC_FALSE, downflg = PETSC_FALSE, monflg = PETSC_FALSE, galerkinflg = PETSC_FALSE;
320:   char           monfilename[PETSC_MAX_PATH_LEN];
321:   SNESFASType    fastype;
322:   const char     *optionsprefix;
323:   SNESLineSearch linesearch;
324:   PetscInt       m, n_up, n_down;
325:   SNES           next;
326:   PetscBool      isFine;

329:   SNESFASCycleIsFine(snes, &isFine);
330:   PetscOptionsHead("SNESFAS Options-----------------------------------");

332:   /* number of levels -- only process most options on the finest level */
333:   if (isFine) {
334:     PetscOptionsInt("-snes_fas_levels", "Number of Levels", "SNESFASSetLevels", levels, &levels, &flg);
335:     if (!flg && snes->dm) {
336:       DMGetRefineLevel(snes->dm,&levels);
337:       levels++;
338:       fas->usedmfornumberoflevels = PETSC_TRUE;
339:     }
340:     SNESFASSetLevels(snes, levels, NULL);
341:     fastype = fas->fastype;
342:     PetscOptionsEnum("-snes_fas_type","FAS correction type","SNESFASSetType",SNESFASTypes,(PetscEnum)fastype,(PetscEnum*)&fastype,&flg);
343:     if (flg) {
344:       SNESFASSetType(snes, fastype);
345:     }

347:     SNESGetOptionsPrefix(snes, &optionsprefix);
348:     PetscOptionsInt("-snes_fas_cycles","Number of cycles","SNESFASSetCycles",fas->n_cycles,&m,&flg);
349:     if (flg) {
350:       SNESFASSetCycles(snes, m);
351:     }

353:     PetscOptionsBool("-snes_fas_galerkin", "Form coarse problems with Galerkin","SNESFASSetGalerkin",fas->galerkin,&galerkinflg,&flg);
354:     if (flg) {
355:       SNESFASSetGalerkin(snes, galerkinflg);
356:     }

358:     if (fas->fastype == SNES_FAS_FULL) {
359:       PetscOptionsBool("-snes_fas_full_downsweep","Smooth on the initial upsweep for full FAS cycles","SNESFASFullSetDownSweep",fas->full_downsweep,&fas->full_downsweep,&flg);
360:       if (flg) {SNESFASFullSetDownSweep(snes,fas->full_downsweep);}
361:     }

363:     PetscOptionsInt("-snes_fas_smoothup","Number of post-smoothing steps","SNESFASSetNumberSmoothUp",fas->max_up_it,&n_up,&upflg);

365:     PetscOptionsInt("-snes_fas_smoothdown","Number of pre-smoothing steps","SNESFASSetNumberSmoothDown",fas->max_down_it,&n_down,&downflg);

367:     PetscOptionsString("-snes_fas_monitor","Monitor FAS progress","SNESFASSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&monflg);
368:     if (monflg) SNESFASSetMonitor(snes, PETSC_TRUE);

370:     flg    = PETSC_FALSE;
371:     monflg = PETSC_TRUE;
372:     PetscOptionsBool("-snes_fas_log","Log times for each FAS level","SNESFASSetLog",monflg,&monflg,&flg);
373:     if (flg) {SNESFASSetLog(snes,monflg);}
374:   }

376:   PetscOptionsTail();
377:   /* setup from the determined types if there is no pointwise procedure or smoother defined */
378:   if (upflg) {
379:     SNESFASSetNumberSmoothUp(snes,n_up);
380:   }
381:   if (downflg) {
382:     SNESFASSetNumberSmoothDown(snes,n_down);
383:   }

385:   /* set up the default line search for coarse grid corrections */
386:   if (fas->fastype == SNES_FAS_ADDITIVE) {
387:     if (!snes->linesearch) {
388:       SNESGetLineSearch(snes, &linesearch);
389:       SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);
390:     }
391:   }

393:   SNESFASCycleGetCorrection(snes, &next);
394:   /* recursive option setting for the smoothers */
395:   if (next) {SNESSetFromOptions(next);}
396:   return(0);
397: }

399: #include <petscdraw.h>
402: PetscErrorCode SNESView_FAS(SNES snes, PetscViewer viewer)
403: {
404:   SNES_FAS       *fas = (SNES_FAS*) snes->data;
405:   PetscBool      isFine,iascii,isdraw;
406:   PetscInt       i;
408:   SNES           smoothu, smoothd, levelsnes;

411:   SNESFASCycleIsFine(snes, &isFine);
412:   if (isFine) {
413:     PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
414:     PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
415:     if (iascii) {
416:       PetscViewerASCIIPrintf(viewer, "FAS: type is %s, levels=%D, cycles=%D\n",  SNESFASTypes[fas->fastype], fas->levels, fas->n_cycles);
417:       if (fas->galerkin) {
418:         PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid function evaluation\n");
419:       } else {
420:         PetscViewerASCIIPrintf(viewer,"    Not using Galerkin computed coarse grid function evaluation\n");
421:       }
422:       for (i=0; i<fas->levels; i++) {
423:         SNESFASGetCycleSNES(snes, i, &levelsnes);
424:         SNESFASCycleGetSmootherUp(levelsnes, &smoothu);
425:         SNESFASCycleGetSmootherDown(levelsnes, &smoothd);
426:         if (!i) {
427:           PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level %D -------------------------------\n",i);
428:         } else {
429:           PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);
430:         }
431:         PetscViewerASCIIPushTab(viewer);
432:         if (smoothd) {
433:           SNESView(smoothd,viewer);
434:         } else {
435:           PetscViewerASCIIPrintf(viewer,"Not yet available\n");
436:         }
437:         PetscViewerASCIIPopTab(viewer);
438:         if (i && (smoothd == smoothu)) {
439:           PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");
440:         } else if (i) {
441:           PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);
442:           PetscViewerASCIIPushTab(viewer);
443:           if (smoothu) {
444:             SNESView(smoothu,viewer);
445:           } else {
446:             PetscViewerASCIIPrintf(viewer,"Not yet available\n");
447:           }
448:           PetscViewerASCIIPopTab(viewer);
449:         }
450:       }
451:     } else if (isdraw) {
452:       PetscDraw draw;
453:       PetscReal x,w,y,bottom,th,wth;
454:       SNES_FAS  *curfas = fas;
455:       PetscViewerDrawGetDraw(viewer,0,&draw);
456:       PetscDrawGetCurrentPoint(draw,&x,&y);
457:       PetscDrawStringGetSize(draw,&wth,&th);
458:       bottom = y - th;
459:       while (curfas) {
460:         if (!curfas->smoothu) {
461:           PetscDrawPushCurrentPoint(draw,x,bottom);
462:           if (curfas->smoothd) SNESView(curfas->smoothd,viewer);
463:           PetscDrawPopCurrentPoint(draw);
464:         } else {
465:           w    = 0.5*PetscMin(1.0-x,x);
466:           PetscDrawPushCurrentPoint(draw,x-w,bottom);
467:           if (curfas->smoothd) SNESView(curfas->smoothd,viewer);
468:           PetscDrawPopCurrentPoint(draw);
469:           PetscDrawPushCurrentPoint(draw,x+w,bottom);
470:           if (curfas->smoothu) SNESView(curfas->smoothu,viewer);
471:           PetscDrawPopCurrentPoint(draw);
472:         }
473:         /* this is totally bogus but we have no way of knowing how low the previous one was draw to */
474:         bottom -= 5*th;
475:         if (curfas->next) curfas = (SNES_FAS*)curfas->next->data;
476:         else curfas = NULL;
477:       }
478:     }
479:   }
480:   return(0);
481: }

485: /*
486: Defines the action of the downsmoother
487:  */
488: PetscErrorCode SNESFASDownSmooth_Private(SNES snes, Vec B, Vec X, Vec F, PetscReal *fnorm)
489: {
490:   PetscErrorCode      0;
491:   SNESConvergedReason reason;
492:   Vec                 FPC;
493:   SNES                smoothd;
494:   SNES_FAS            *fas = (SNES_FAS*) snes->data;

497:   SNESFASCycleGetSmootherDown(snes, &smoothd);
498:   SNESSetInitialFunction(smoothd, F);
499:   if (fas->eventsmoothsolve) {PetscLogEventBegin(fas->eventsmoothsolve,0,0,0,0);}
500:   SNESSolve(smoothd, B, X);
501:   if (fas->eventsmoothsolve) {PetscLogEventEnd(fas->eventsmoothsolve,0,0,0,0);}
502:   /* check convergence reason for the smoother */
503:   SNESGetConvergedReason(smoothd,&reason);
504:   if (reason < 0 && !(reason == SNES_DIVERGED_MAX_IT || reason == SNES_DIVERGED_LOCAL_MIN || reason == SNES_DIVERGED_LINE_SEARCH)) {
505:     snes->reason = SNES_DIVERGED_INNER;
506:     return(0);
507:   }
508:   SNESGetFunction(smoothd, &FPC, NULL, NULL);
509:   VecCopy(FPC, F);
510:   SNESGetFunctionNorm(smoothd, fnorm);
511:   return(0);
512: }


517: /*
518: Defines the action of the upsmoother
519:  */
520: PetscErrorCode SNESFASUpSmooth_Private(SNES snes, Vec B, Vec X, Vec F, PetscReal *fnorm)
521: {
522:   PetscErrorCode      0;
523:   SNESConvergedReason reason;
524:   Vec                 FPC;
525:   SNES                smoothu;
526:   SNES_FAS            *fas = (SNES_FAS*) snes->data;

529:   SNESFASCycleGetSmootherUp(snes, &smoothu);
530:   if (fas->eventsmoothsolve) {PetscLogEventBegin(fas->eventsmoothsolve,0,0,0,0);}
531:   SNESSolve(smoothu, B, X);
532:   if (fas->eventsmoothsolve) {PetscLogEventEnd(fas->eventsmoothsolve,0,0,0,0);}
533:   /* check convergence reason for the smoother */
534:   SNESGetConvergedReason(smoothu,&reason);
535:   if (reason < 0 && !(reason == SNES_DIVERGED_MAX_IT || reason == SNES_DIVERGED_LOCAL_MIN || reason == SNES_DIVERGED_LINE_SEARCH)) {
536:     snes->reason = SNES_DIVERGED_INNER;
537:     return(0);
538:   }
539:   SNESGetFunction(smoothu, &FPC, NULL, NULL);
540:   VecCopy(FPC, F);
541:   SNESGetFunctionNorm(smoothu, fnorm);
542:   return(0);
543: }

547: /*@
548:    SNESFASCreateCoarseVec - create Vec corresponding to a state vector on one level coarser than current level

550:    Collective

552:    Input Arguments:
553: .  snes - SNESFAS

555:    Output Arguments:
556: .  Xcoarse - vector on level one coarser than snes

558:    Level: developer

560: .seealso: SNESFASSetRestriction(), SNESFASRestrict()
561: @*/
562: PetscErrorCode SNESFASCreateCoarseVec(SNES snes,Vec *Xcoarse)
563: {
565:   SNES_FAS       *fas = (SNES_FAS*)snes->data;

568:   if (fas->rscale) {
569:     VecDuplicate(fas->rscale,Xcoarse);
570:   } else if (fas->inject) {
571:     MatGetVecs(fas->inject,Xcoarse,NULL);
572:   } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must set restriction or injection");
573:   return(0);
574: }

578: /*@
579:    SNESFASRestrict - restrict a Vec to the next coarser level

581:    Collective

583:    Input Arguments:
584: +  fine - SNES from which to restrict
585: -  Xfine - vector to restrict

587:    Output Arguments:
588: .  Xcoarse - result of restriction

590:    Level: developer

592: .seealso: SNESFASSetRestriction(), SNESFASSetInjection()
593: @*/
594: PetscErrorCode SNESFASRestrict(SNES fine,Vec Xfine,Vec Xcoarse)
595: {
597:   SNES_FAS       *fas = (SNES_FAS*)fine->data;

603:   if (fas->inject) {
604:     MatRestrict(fas->inject,Xfine,Xcoarse);
605:   } else {
606:     MatRestrict(fas->restrct,Xfine,Xcoarse);
607:     VecPointwiseMult(Xcoarse,fas->rscale,Xcoarse);
608:   }
609:   return(0);
610: }

614: /*

616: Performs the FAS coarse correction as:

618: fine problem: F(x) = 0
619: coarse problem: F^c(x) = b^c

621: b^c = F^c(I^c_fx^f - I^c_fF(x))

623:  */
624: PetscErrorCode SNESFASCoarseCorrection(SNES snes, Vec X, Vec F, Vec X_new)
625: {
626:   PetscErrorCode      ierr;
627:   Vec                 X_c, Xo_c, F_c, B_c;
628:   SNESConvergedReason reason;
629:   SNES                next;
630:   Mat                 restrct, interpolate;
631:   SNES_FAS            *fasc;

634:   SNESFASCycleGetCorrection(snes, &next);
635:   if (next) {
636:     fasc = (SNES_FAS*)next->data;

638:     SNESFASCycleGetRestriction(snes, &restrct);
639:     SNESFASCycleGetInterpolation(snes, &interpolate);

641:     X_c  = next->vec_sol;
642:     Xo_c = next->work[0];
643:     F_c  = next->vec_func;
644:     B_c  = next->vec_rhs;

646:     if (fasc->eventinterprestrict) {PetscLogEventBegin(fasc->eventinterprestrict,0,0,0,0);}
647:     SNESFASRestrict(snes,X,Xo_c);
648:     /* restrict the defect */
649:     MatRestrict(restrct, F, B_c);
650:     if (fasc->eventinterprestrict) {PetscLogEventEnd(fasc->eventinterprestrict,0,0,0,0);}

652:     if (fasc->eventresidual) {PetscLogEventBegin(fasc->eventresidual,0,0,0,0);}
653:     SNESComputeFunction(next, Xo_c, F_c);
654:     if (fasc->eventresidual) {PetscLogEventEnd(fasc->eventresidual,0,0,0,0);}

656:     /* solve the coarse problem corresponding to F^c(x^c) = b^c = F^c(Rx) - R(F(x) - b) */
657:     VecCopy(B_c, X_c);
658:     VecCopy(F_c, B_c);
659:     VecCopy(X_c, F_c);
660:     /* set initial guess of the coarse problem to the projected fine solution */
661:     VecCopy(Xo_c, X_c);

663:     /* recurse to the next level */
664:     SNESSetInitialFunction(next, F_c);
665:     SNESSolve(next, B_c, X_c);
666:     SNESGetConvergedReason(next,&reason);
667:     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
668:       snes->reason = SNES_DIVERGED_INNER;
669:       return(0);
670:     }
671:     /* correct as x <- x + I(x^c - Rx)*/
672:     VecAXPY(X_c, -1.0, Xo_c);

674:     if (fasc->eventinterprestrict) {PetscLogEventBegin(fasc->eventinterprestrict,0,0,0,0);}
675:     MatInterpolateAdd(interpolate, X_c, X, X_new);
676:     if (fasc->eventinterprestrict) {PetscLogEventEnd(fasc->eventinterprestrict,0,0,0,0);}
677:   }
678:   return(0);
679: }

683: /*

685: The additive cycle looks like:

687: xhat = x
688: xhat = dS(x, b)
689: x = coarsecorrection(xhat, b_d)
690: x = x + nu*(xhat - x);
691: (optional) x = uS(x, b)

693: With the coarse RHS (defect correction) as below.

695:  */
696: PetscErrorCode SNESFASCycle_Additive(SNES snes, Vec X)
697: {
698:   Vec                 F, B, Xhat;
699:   Vec                 X_c, Xo_c, F_c, B_c;
700:   PetscErrorCode      ierr;
701:   SNESConvergedReason reason;
702:   PetscReal           xnorm, fnorm, ynorm;
703:   PetscBool           lssuccess;
704:   SNES                next;
705:   Mat                 restrct, interpolate;
706:   SNES_FAS            *fas = (SNES_FAS*)snes->data,*fasc;

709:   SNESFASCycleGetCorrection(snes, &next);
710:   F    = snes->vec_func;
711:   B    = snes->vec_rhs;
712:   Xhat = snes->work[1];
713:   VecCopy(X, Xhat);
714:   /* recurse first */
715:   if (next) {
716:     fasc = (SNES_FAS*)next->data;
717:     SNESFASCycleGetRestriction(snes, &restrct);
718:     SNESFASCycleGetInterpolation(snes, &interpolate);
719:     if (fas->eventresidual) {PetscLogEventBegin(fas->eventresidual,0,0,0,0);}
720:     SNESComputeFunction(snes, Xhat, F);
721:     if (fas->eventresidual) {PetscLogEventEnd(fas->eventresidual,0,0,0,0);}
722:     VecNorm(F, NORM_2, &fnorm);
723:     X_c  = next->vec_sol;
724:     Xo_c = next->work[0];
725:     F_c  = next->vec_func;
726:     B_c  = next->vec_rhs;

728:     SNESFASRestrict(snes,Xhat,Xo_c);
729:     /* restrict the defect */
730:     MatRestrict(restrct, F, B_c);

732:     /* solve the coarse problem corresponding to F^c(x^c) = b^c = Rb + F^c(Rx) - RF(x) */
733:     if (fasc->eventresidual) {PetscLogEventBegin(fasc->eventresidual,0,0,0,0);}
734:     SNESComputeFunction(next, Xo_c, F_c);
735:     if (fasc->eventresidual) {PetscLogEventEnd(fasc->eventresidual,0,0,0,0);}
736:     VecCopy(B_c, X_c);
737:     VecCopy(F_c, B_c);
738:     VecCopy(X_c, F_c);
739:     /* set initial guess of the coarse problem to the projected fine solution */
740:     VecCopy(Xo_c, X_c);

742:     /* recurse */
743:     SNESSetInitialFunction(next, F_c);
744:     SNESSolve(next, B_c, X_c);

746:     /* smooth on this level */
747:     SNESFASDownSmooth_Private(snes, B, X, F, &fnorm);

749:     SNESGetConvergedReason(next,&reason);
750:     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
751:       snes->reason = SNES_DIVERGED_INNER;
752:       return(0);
753:     }

755:     /* correct as x <- x + I(x^c - Rx)*/
756:     VecAYPX(X_c, -1.0, Xo_c);
757:     MatInterpolate(interpolate, X_c, Xhat);

759:     /* additive correction of the coarse direction*/
760:     SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Xhat);
761:     SNESLineSearchGetSuccess(snes->linesearch, &lssuccess);
762:     if (!lssuccess) {
763:       if (++snes->numFailures >= snes->maxFailures) {
764:         snes->reason = SNES_DIVERGED_LINE_SEARCH;
765:         return(0);
766:       }
767:     }
768:     SNESLineSearchGetNorms(snes->linesearch, &xnorm, &snes->norm, &ynorm);
769:   } else {
770:     SNESFASDownSmooth_Private(snes, B, X, F, &snes->norm);
771:   }
772:   return(0);
773: }

777: /*

779: Defines the FAS cycle as:

781: fine problem: F(x) = 0
782: coarse problem: F^c(x) = b^c

784: b^c = F^c(I^c_fx^f - I^c_fF(x))

786: correction:

788: x = x + I(x^c - Rx)

790:  */
791: PetscErrorCode SNESFASCycle_Multiplicative(SNES snes, Vec X)
792: {

795:   Vec            F,B;
796:   SNES           next;

799:   F = snes->vec_func;
800:   B = snes->vec_rhs;
801:   /* pre-smooth -- just update using the pre-smoother */
802:   SNESFASCycleGetCorrection(snes,&next);
803:   SNESFASDownSmooth_Private(snes, B, X, F, &snes->norm);
804:   if (next) {
805:     SNESFASCoarseCorrection(snes, X, F, X);
806:     SNESFASUpSmooth_Private(snes, B, X, F, &snes->norm);
807:   }
808:   return(0);
809: }

813: PetscErrorCode SNESFASCycleSetupPhase_Full(SNES snes)
814: {
815:   SNES           next;
816:   SNES_FAS       *fas = (SNES_FAS*)snes->data;
817:   PetscBool      isFine;

821:   /* pre-smooth -- just update using the pre-smoother */
822:   SNESFASCycleIsFine(snes,&isFine);
823:   SNESFASCycleGetCorrection(snes,&next);
824:   fas->full_stage = 0;
825:   if (next) {SNESFASCycleSetupPhase_Full(next);}
826:   return(0);
827: }

831: PetscErrorCode SNESFASCycle_Full(SNES snes, Vec X)
832: {
834:   Vec            F,B;
835:   SNES_FAS       *fas = (SNES_FAS*)snes->data;
836:   PetscBool      isFine;
837:   SNES           next;

840:   F = snes->vec_func;
841:   B = snes->vec_rhs;
842:   SNESFASCycleIsFine(snes,&isFine);
843:   SNESFASCycleGetCorrection(snes,&next);

845:   if (isFine) {
846:     SNESFASCycleSetupPhase_Full(snes);
847:   }

849:   if (fas->full_stage == 0) {
850:     /* downsweep */
851:     if (next) {
852:       if (fas->level != 1) next->max_its += 1;
853:       if (fas->full_downsweep||isFine) {SNESFASDownSmooth_Private(snes,B,X,F,&snes->norm);}
854:       SNESFASCoarseCorrection(snes,X,F,X);
855:       SNESFASUpSmooth_Private(snes,B,X,F,&snes->norm);
856:       if (fas->level != 1) next->max_its -= 1;
857:     } else {
858:       SNESFASDownSmooth_Private(snes,B,X,F,&snes->norm);
859:     }
860:     fas->full_stage = 1;
861:   } else if (fas->full_stage == 1) {
862:     if (snes->iter == 0) {SNESFASDownSmooth_Private(snes,B,X,F,&snes->norm);}
863:     if (next) {
864:       SNESFASCoarseCorrection(snes,X,F,X);
865:       SNESFASUpSmooth_Private(snes,B,X,F,&snes->norm);
866:     }
867:   }
868:   /* final v-cycle */
869:   if (isFine) {
870:     if (next) {
871:       SNESFASCoarseCorrection(snes,X,F,X);
872:       SNESFASUpSmooth_Private(snes,B,X,F,&snes->norm);
873:     }
874:   }
875:   return(0);
876: }

880: PetscErrorCode SNESFASCycle_Kaskade(SNES snes, Vec X)
881: {

884:   Vec            F,B;
885:   SNES           next;

888:   F = snes->vec_func;
889:   B = snes->vec_rhs;
890:   SNESFASCycleGetCorrection(snes,&next);
891:   if (next) {
892:     SNESFASCoarseCorrection(snes,X,F,X);
893:     SNESFASUpSmooth_Private(snes,B,X,F,&snes->norm);
894:   } else {
895:     SNESFASDownSmooth_Private(snes,B,X,F,&snes->norm);
896:   }
897:   return(0);
898: }


903: PetscErrorCode SNESSolve_FAS(SNES snes)
904: {
906:   PetscInt       i, maxits;
907:   Vec            X, F;
908:   PetscReal      fnorm;
909:   SNES_FAS       *fas = (SNES_FAS*)snes->data,*ffas;
910:   DM             dm;
911:   PetscBool      isFine;

914:   maxits       = snes->max_its;      /* maximum number of iterations */
915:   snes->reason = SNES_CONVERGED_ITERATING;
916:   X            = snes->vec_sol;
917:   F            = snes->vec_func;

919:   SNESFASCycleIsFine(snes, &isFine);
920:   /*norm setup */
921:   PetscObjectSAWsTakeAccess((PetscObject)snes);
922:   snes->iter = 0;
923:   snes->norm = 0.;
924:   PetscObjectSAWsGrantAccess((PetscObject)snes);
925:   if (!snes->vec_func_init_set) {
926:     if (fas->eventresidual) {PetscLogEventBegin(fas->eventresidual,0,0,0,0);}
927:     SNESComputeFunction(snes,X,F);
928:     if (fas->eventresidual) {PetscLogEventEnd(fas->eventresidual,0,0,0,0);}
929:     if (snes->domainerror) {
930:       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
931:       return(0);
932:     }
933:   } else snes->vec_func_init_set = PETSC_FALSE;

935:   VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F||  */
936:   if (PetscIsInfOrNanReal(fnorm)) {
937:     snes->reason = SNES_DIVERGED_FNORM_NAN;
938:     return(0);
939:   }

941:   PetscObjectSAWsTakeAccess((PetscObject)snes);
942:   snes->norm = fnorm;
943:   PetscObjectSAWsGrantAccess((PetscObject)snes);
944:   SNESLogConvergenceHistory(snes,fnorm,0);
945:   SNESMonitor(snes,0,fnorm);

947:   /* test convergence */
948:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
949:   if (snes->reason) return(0);


952:   if (isFine) {
953:     /* propagate scale-dependent data up the hierarchy */
954:     SNESGetDM(snes,&dm);
955:     for (ffas=fas; ffas->next; ffas=(SNES_FAS*)ffas->next->data) {
956:       DM dmcoarse;
957:       SNESGetDM(ffas->next,&dmcoarse);
958:       DMRestrict(dm,ffas->restrct,ffas->rscale,ffas->inject,dmcoarse);
959:       dm   = dmcoarse;
960:     }
961:   }

963:   for (i = 0; i < maxits; i++) {
964:     /* Call general purpose update function */

966:     if (snes->ops->update) {
967:       (*snes->ops->update)(snes, snes->iter);
968:     }
969:     if (fas->fastype == SNES_FAS_MULTIPLICATIVE) {
970:       SNESFASCycle_Multiplicative(snes, X);
971:     } else if (fas->fastype == SNES_FAS_ADDITIVE) {
972:       SNESFASCycle_Additive(snes, X);
973:     } else if (fas->fastype == SNES_FAS_FULL) {
974:       SNESFASCycle_Full(snes, X);
975:     } else if (fas->fastype ==SNES_FAS_KASKADE) {
976:       SNESFASCycle_Kaskade(snes, X);
977:     } else {
978:       SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Unsupported FAS type");
979:     }

981:     /* check for FAS cycle divergence */
982:     if (snes->reason != SNES_CONVERGED_ITERATING) return(0);

984:     /* Monitor convergence */
985:     PetscObjectSAWsTakeAccess((PetscObject)snes);
986:     snes->iter = i+1;
987:     PetscObjectSAWsGrantAccess((PetscObject)snes);
988:     SNESLogConvergenceHistory(snes,snes->norm,0);
989:     SNESMonitor(snes,snes->iter,snes->norm);
990:     /* Test for convergence */
991:     if (isFine) {
992:       (*snes->ops->converged)(snes,snes->iter,0.0,0.0,snes->norm,&snes->reason,snes->cnvP);
993:       if (snes->reason) break;
994:     }
995:   }
996:   if (i == maxits) {
997:     PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);
998:     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
999:   }
1000:   return(0);
1001: }