xref: /petsc/src/snes/impls/fas/fas.c (revision 8427d9d206bbb1bdbabec00e401929ba5773360d)
1 /* Defines the basic SNES object */
2 #include <../src/snes/impls/fas/fasimpls.h>    /*I  "petscsnesfas.h"  I*/
3 
4 const char *SNESFASTypes[] = {"MULTIPLICATIVE","ADDITIVE","SNESFASType","SNES_FAS",0};
5 
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 *);
14 
15 /*MC
16 
17 SNESFAS - Full Approximation Scheme nonlinear multigrid solver.
18 
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.
22 
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
36 
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.
41 
42 Level: advanced
43 
44 .seealso: PCMG, SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
45 M*/
46 
47 #undef __FUNCT__
48 #define __FUNCT__ "SNESCreate_FAS"
49 PETSC_EXTERN_C PetscErrorCode SNESCreate_FAS(SNES snes)
50 {
51   SNES_FAS * fas;
52   PetscErrorCode ierr;
53 
54   PetscFunctionBegin;
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;
61 
62   snes->usesksp             = PETSC_FALSE;
63   snes->usespc              = PETSC_FALSE;
64 
65   if (!snes->tolerancesset) {
66     snes->max_funcs = 30000;
67     snes->max_its   = 10000;
68   }
69 
70   ierr = PetscNewLog(snes, SNES_FAS, &fas);CHKERRQ(ierr);
71   snes->data                  = (void*) fas;
72   fas->level                  = 0;
73   fas->levels                 = 1;
74   fas->n_cycles               = 1;
75   fas->max_up_it              = 1;
76   fas->max_down_it            = 1;
77   fas->smoothu                = PETSC_NULL;
78   fas->smoothd                = PETSC_NULL;
79   fas->next                   = PETSC_NULL;
80   fas->previous               = PETSC_NULL;
81   fas->fine                   = snes;
82   fas->interpolate            = PETSC_NULL;
83   fas->restrct                = PETSC_NULL;
84   fas->inject                 = PETSC_NULL;
85   fas->monitor                = PETSC_NULL;
86   fas->usedmfornumberoflevels = PETSC_FALSE;
87   fas->fastype                = SNES_FAS_MULTIPLICATIVE;
88   PetscFunctionReturn(0);
89 }
90 
91 #undef __FUNCT__
92 #define __FUNCT__ "SNESReset_FAS"
93 PetscErrorCode SNESReset_FAS(SNES snes)
94 {
95   PetscErrorCode ierr = 0;
96   SNES_FAS * fas = (SNES_FAS *)snes->data;
97 
98   PetscFunctionBegin;
99   if (fas->smoothu != fas->smoothd) {
100     ierr = SNESDestroy(&fas->smoothu);CHKERRQ(ierr);
101     ierr = SNESDestroy(&fas->smoothd);CHKERRQ(ierr);
102   } else {
103     ierr = SNESDestroy(&fas->smoothd);CHKERRQ(ierr);
104     fas->smoothu = PETSC_NULL;
105   }
106   ierr = MatDestroy(&fas->inject);CHKERRQ(ierr);
107   ierr = MatDestroy(&fas->interpolate);CHKERRQ(ierr);
108   ierr = MatDestroy(&fas->restrct);CHKERRQ(ierr);
109   ierr = VecDestroy(&fas->rscale);CHKERRQ(ierr);
110   if (fas->next)      ierr = SNESReset(fas->next);CHKERRQ(ierr);
111 
112   PetscFunctionReturn(0);
113 }
114 
115 #undef __FUNCT__
116 #define __FUNCT__ "SNESDestroy_FAS"
117 PetscErrorCode SNESDestroy_FAS(SNES snes)
118 {
119   SNES_FAS * fas = (SNES_FAS *)snes->data;
120   PetscErrorCode ierr = 0;
121 
122   PetscFunctionBegin;
123   /* recursively resets and then destroys */
124   ierr = SNESReset(snes);CHKERRQ(ierr);
125   if (fas->next)         ierr = SNESDestroy(&fas->next);CHKERRQ(ierr);
126   ierr = PetscFree(fas);CHKERRQ(ierr);
127 
128   PetscFunctionReturn(0);
129 }
130 
131 #undef __FUNCT__
132 #define __FUNCT__ "SNESSetUp_FAS"
133 PetscErrorCode SNESSetUp_FAS(SNES snes)
134 {
135   SNES_FAS                *fas = (SNES_FAS *) snes->data;
136   PetscErrorCode          ierr;
137   VecScatter              injscatter;
138   PetscInt                dm_levels;
139   Vec                     vec_sol, vec_func, vec_sol_update, vec_rhs; /* preserve these if they're set through the reset */
140   SNES                    next;
141   PetscBool               isFine;
142   PetscFunctionBegin;
143 
144   ierr = SNESFASCycleIsFine(snes, &isFine);CHKERRQ(ierr);
145   if (fas->usedmfornumberoflevels && isFine) {
146     ierr = DMGetRefineLevel(snes->dm,&dm_levels);CHKERRQ(ierr);
147     dm_levels++;
148     if (dm_levels > fas->levels) {
149       /* we don't want the solution and func vectors to be destroyed in the SNESReset when it's called in SNESFASSetLevels_FAS*/
150       vec_sol = snes->vec_sol;
151       vec_func = snes->vec_func;
152       vec_sol_update = snes->vec_sol_update;
153       vec_rhs = snes->vec_rhs;
154       snes->vec_sol = PETSC_NULL;
155       snes->vec_func = PETSC_NULL;
156       snes->vec_sol_update = PETSC_NULL;
157       snes->vec_rhs = PETSC_NULL;
158 
159       /* reset the number of levels */
160       ierr = SNESFASSetLevels(snes,dm_levels,PETSC_NULL);CHKERRQ(ierr);
161       ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
162 
163       snes->vec_sol = vec_sol;
164       snes->vec_func = vec_func;
165       snes->vec_rhs = vec_rhs;
166       snes->vec_sol_update = vec_sol_update;
167     }
168   }
169   ierr = SNESFASCycleGetCorrection(snes, &next);CHKERRQ(ierr);
170   if (!isFine) snes->gridsequence = 0; /* no grid sequencing inside the multigrid hierarchy! */
171 
172   if (fas->fastype == SNES_FAS_MULTIPLICATIVE) {
173     ierr = SNESDefaultGetWork(snes, 1);CHKERRQ(ierr); /* work vectors used for intergrid transfers */
174   } else {
175     ierr = SNESDefaultGetWork(snes, 2);CHKERRQ(ierr); /* work vectors used for intergrid transfers */
176   }
177 
178   /* set up the smoothers if they haven't already been set up */
179   if (!fas->smoothd) {
180     ierr = SNESFASCycleCreateSmoother_Private(snes, &fas->smoothd);CHKERRQ(ierr);
181   }
182 
183   if (snes->dm) {
184     /* set the smoother DMs properly */
185     if (fas->smoothu) ierr = SNESSetDM(fas->smoothu, snes->dm);CHKERRQ(ierr);
186     ierr = SNESSetDM(fas->smoothd, snes->dm);CHKERRQ(ierr);
187     /* construct EVERYTHING from the DM -- including the progressive set of smoothers */
188     if (next) {
189       /* for now -- assume the DM and the evaluation functions have been set externally */
190       if (!next->dm) {
191         ierr = DMCoarsen(snes->dm, ((PetscObject)next)->comm, &next->dm);CHKERRQ(ierr);
192         ierr = SNESSetDM(next, next->dm);CHKERRQ(ierr);
193       }
194       /* set the interpolation and restriction from the DM */
195       if (!fas->interpolate) {
196         ierr = DMCreateInterpolation(next->dm, snes->dm, &fas->interpolate, &fas->rscale);CHKERRQ(ierr);
197         if (!fas->restrct) {
198           ierr = PetscObjectReference((PetscObject)fas->interpolate);CHKERRQ(ierr);
199           fas->restrct = fas->interpolate;
200         }
201       }
202       /* set the injection from the DM */
203       if (!fas->inject) {
204         ierr = DMCreateInjection(next->dm, snes->dm, &injscatter);CHKERRQ(ierr);
205         ierr = MatCreateScatter(((PetscObject)snes)->comm, injscatter, &fas->inject);CHKERRQ(ierr);
206         ierr = VecScatterDestroy(&injscatter);CHKERRQ(ierr);
207       }
208     }
209   }
210   /*pass the smoother, function, and jacobian up to the next level if it's not user set already */
211 
212   if (fas->galerkin) {
213     if (next)
214       ierr = SNESSetFunction(next, PETSC_NULL, SNESFASGalerkinDefaultFunction, next);CHKERRQ(ierr);
215     if (fas->smoothd && fas->level != fas->levels - 1) ierr = SNESSetFunction(fas->smoothd, PETSC_NULL, SNESFASGalerkinDefaultFunction, snes);CHKERRQ(ierr);
216     if (fas->smoothu && fas->level != fas->levels - 1) ierr = SNESSetFunction(fas->smoothu, PETSC_NULL, SNESFASGalerkinDefaultFunction, snes);CHKERRQ(ierr);
217   }
218 
219   /* set the smoothers up here so that precedence is taken for instance-specific options over the whole-solver options */
220   if(fas->smoothu) ierr = SNESSetFromOptions(fas->smoothu);CHKERRQ(ierr);
221   if(fas->smoothd) ierr = SNESSetFromOptions(fas->smoothd);CHKERRQ(ierr);
222 
223   if (next) {
224     /* gotta set up the solution vector for this to work */
225     if (!next->vec_sol) {ierr = SNESFASCreateCoarseVec(snes,&next->vec_sol);CHKERRQ(ierr);}
226     if (!next->vec_rhs) {ierr = SNESFASCreateCoarseVec(snes,&next->vec_rhs);CHKERRQ(ierr);}
227     ierr = SNESSetUp(next);CHKERRQ(ierr);
228   }
229   /* setup FAS work vectors */
230   if (fas->galerkin) {
231     ierr = VecDuplicate(snes->vec_sol, &fas->Xg);CHKERRQ(ierr);
232     ierr = VecDuplicate(snes->vec_sol, &fas->Fg);CHKERRQ(ierr);
233   }
234   PetscFunctionReturn(0);
235 }
236 
237 #undef __FUNCT__
238 #define __FUNCT__ "SNESSetFromOptions_FAS"
239 PetscErrorCode SNESSetFromOptions_FAS(SNES snes)
240 {
241   SNES_FAS       *fas = (SNES_FAS *) snes->data;
242   PetscInt       levels = 1;
243   PetscBool      flg = PETSC_FALSE, upflg = PETSC_FALSE, downflg = PETSC_FALSE, monflg = PETSC_FALSE, galerkinflg = PETSC_FALSE;
244   PetscErrorCode ierr;
245   char           monfilename[PETSC_MAX_PATH_LEN];
246   SNESFASType    fastype;
247   const char     *optionsprefix;
248   SNESLineSearch linesearch;
249   PetscInt       m, n_up, n_down;
250   SNES           next;
251   PetscBool      isFine;
252 
253   PetscFunctionBegin;
254   ierr = SNESFASCycleIsFine(snes, &isFine);CHKERRQ(ierr);
255   ierr = PetscOptionsHead("SNESFAS Options-----------------------------------");CHKERRQ(ierr);
256 
257   /* number of levels -- only process most options on the finest level */
258   if (isFine) {
259     ierr = PetscOptionsInt("-snes_fas_levels", "Number of Levels", "SNESFASSetLevels", levels, &levels, &flg);CHKERRQ(ierr);
260     if (!flg && snes->dm) {
261       ierr = DMGetRefineLevel(snes->dm,&levels);CHKERRQ(ierr);
262       levels++;
263       fas->usedmfornumberoflevels = PETSC_TRUE;
264     }
265     ierr = SNESFASSetLevels(snes, levels, PETSC_NULL);CHKERRQ(ierr);
266     fastype = fas->fastype;
267     ierr = PetscOptionsEnum("-snes_fas_type","FAS correction type","SNESFASSetType",SNESFASTypes,(PetscEnum)fastype,(PetscEnum*)&fastype,&flg);CHKERRQ(ierr);
268     if (flg) {
269       ierr = SNESFASSetType(snes, fastype);CHKERRQ(ierr);
270     }
271 
272     ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr);
273     ierr = PetscOptionsInt("-snes_fas_cycles","Number of cycles","SNESFASSetCycles",fas->n_cycles,&m,&flg);CHKERRQ(ierr);
274     if (flg) {
275       ierr = SNESFASSetCycles(snes, m);CHKERRQ(ierr);
276     }
277 
278     ierr = PetscOptionsBool("-snes_fas_galerkin", "Form coarse problems with Galerkin","SNESFASSetGalerkin",fas->galerkin,&galerkinflg,&flg);CHKERRQ(ierr);
279     if (flg) {
280       ierr = SNESFASSetGalerkin(snes, galerkinflg);CHKERRQ(ierr);
281     }
282 
283     ierr = PetscOptionsInt("-snes_fas_smoothup","Number of post-smoothing steps","SNESFASSetNumberSmoothUp",fas->max_up_it,&n_up,&upflg);CHKERRQ(ierr);
284 
285     ierr = PetscOptionsInt("-snes_fas_smoothdown","Number of pre-smoothing steps","SNESFASSetNumberSmoothDown",fas->max_down_it,&n_down,&downflg);CHKERRQ(ierr);
286 
287     ierr = PetscOptionsString("-snes_fas_monitor","Monitor FAS progress","SNESFASSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&monflg);CHKERRQ(ierr);
288     if (monflg) ierr = SNESFASSetMonitor(snes, PETSC_TRUE);CHKERRQ(ierr);
289   }
290 
291   ierr = PetscOptionsTail();CHKERRQ(ierr);
292   /* setup from the determined types if there is no pointwise procedure or smoother defined */
293   if (upflg) {
294     ierr = SNESFASSetNumberSmoothUp(snes,n_up);CHKERRQ(ierr);
295   }
296   if (downflg) {
297     ierr = SNESFASSetNumberSmoothDown(snes,n_down);CHKERRQ(ierr);
298   }
299 
300   /* set up the default line search for coarse grid corrections */
301   if (fas->fastype == SNES_FAS_ADDITIVE) {
302     if (!snes->linesearch) {
303       ierr = SNESGetSNESLineSearch(snes, &linesearch);CHKERRQ(ierr);
304       ierr = SNESLineSearchSetType(linesearch, SNES_LINESEARCH_L2);CHKERRQ(ierr);
305     }
306   }
307 
308   ierr = SNESFASCycleGetCorrection(snes, &next);CHKERRQ(ierr);
309   /* recursive option setting for the smoothers */
310   if (next) {ierr = SNESSetFromOptions(next);CHKERRQ(ierr);}
311   PetscFunctionReturn(0);
312 }
313 
314 #undef __FUNCT__
315 #define __FUNCT__ "SNESView_FAS"
316 PetscErrorCode SNESView_FAS(SNES snes, PetscViewer viewer)
317 {
318   SNES_FAS       *fas = (SNES_FAS *) snes->data;
319   PetscBool      isFine, iascii;
320   PetscInt       i;
321   PetscErrorCode ierr;
322   SNES           smoothu, smoothd, levelsnes;
323 
324   PetscFunctionBegin;
325   ierr = SNESFASCycleIsFine(snes, &isFine);CHKERRQ(ierr);
326   if (isFine) {
327     ierr = PetscTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
328     if (iascii) {
329       ierr = PetscViewerASCIIPrintf(viewer, "FAS: type is %s, levels=%D, cycles=%D\n",  SNESFASTypes[fas->fastype], fas->levels, fas->n_cycles);CHKERRQ(ierr);
330       if (fas->galerkin) {
331         ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid function evaluation\n");CHKERRQ(ierr);
332       } else {
333         ierr = PetscViewerASCIIPrintf(viewer,"    Not using Galerkin computed coarse grid function evaluation\n");CHKERRQ(ierr);
334       }
335       for (i=0; i<fas->levels; i++) {
336         ierr = SNESFASGetCycleSNES(snes, i, &levelsnes);CHKERRQ(ierr);
337         ierr = SNESFASCycleGetSmootherUp(levelsnes, &smoothu);CHKERRQ(ierr);
338         ierr = SNESFASCycleGetSmootherDown(levelsnes, &smoothd);CHKERRQ(ierr);
339         if (!i) {
340           ierr = PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level %D -------------------------------\n",i);CHKERRQ(ierr);
341         } else {
342           ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr);
343         }
344         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
345         ierr = SNESView(smoothd,viewer);CHKERRQ(ierr);
346         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
347         if (i && (smoothd == smoothu)) {
348           ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr);
349         } else if (i) {
350           ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr);
351           ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
352           ierr = SNESView(smoothu,viewer);CHKERRQ(ierr);
353           ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
354         }
355       }
356     } else {
357       SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for SNESFAS",((PetscObject)viewer)->type_name);
358     }
359   }
360   PetscFunctionReturn(0);
361 }
362 
363 #undef __FUNCT__
364 #define __FUNCT__ "SNESFASDownSmooth_Private"
365 /*
366 Defines the action of the downsmoother
367  */
368 PetscErrorCode SNESFASDownSmooth_Private(SNES snes, Vec B, Vec X, Vec F, PetscReal *fnorm)
369 {
370   PetscErrorCode      ierr = 0;
371   SNESConvergedReason reason;
372   Vec                 FPC;
373   SNES                smoothd;
374   PetscFunctionBegin;
375   ierr = SNESFASCycleGetSmootherDown(snes, &smoothd);CHKERRQ(ierr);
376   ierr = SNESSolve(smoothd, B, X);CHKERRQ(ierr);
377   /* check convergence reason for the smoother */
378   ierr = SNESGetConvergedReason(smoothd,&reason);CHKERRQ(ierr);
379   if (reason < 0 && !(reason == SNES_DIVERGED_MAX_IT || reason == SNES_DIVERGED_LOCAL_MIN)) {
380     snes->reason = SNES_DIVERGED_INNER;
381     PetscFunctionReturn(0);
382   }
383   ierr = SNESGetFunction(smoothd, &FPC, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
384   ierr = VecCopy(FPC, F);CHKERRQ(ierr);
385   ierr = SNESGetFunctionNorm(smoothd, fnorm);CHKERRQ(ierr);
386   PetscFunctionReturn(0);
387 }
388 
389 
390 #undef __FUNCT__
391 #define __FUNCT__ "SNESFASUpSmooth_Private"
392 /*
393 Defines the action of the upsmoother
394  */
395 PetscErrorCode SNESFASUpSmooth_Private (SNES snes, Vec B, Vec X, Vec F, PetscReal *fnorm) {
396   PetscErrorCode      ierr = 0;
397   SNESConvergedReason reason;
398   Vec                 FPC;
399   SNES                smoothu;
400   PetscFunctionBegin;
401 
402   ierr = SNESFASCycleGetSmootherUp(snes, &smoothu);CHKERRQ(ierr);
403   ierr = SNESSolve(smoothu, B, X);CHKERRQ(ierr);
404   /* check convergence reason for the smoother */
405   ierr = SNESGetConvergedReason(smoothu,&reason);CHKERRQ(ierr);
406   if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
407     snes->reason = SNES_DIVERGED_INNER;
408     PetscFunctionReturn(0);
409   }
410   ierr = SNESGetFunction(smoothu, &FPC, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
411   ierr = VecCopy(FPC, F);CHKERRQ(ierr);
412   ierr = SNESGetFunctionNorm(smoothu, fnorm);CHKERRQ(ierr);
413   PetscFunctionReturn(0);
414 }
415 
416 #undef __FUNCT__
417 #define __FUNCT__ "SNESFASCreateCoarseVec"
418 /*@
419    SNESFASCreateCoarseVec - create Vec corresponding to a state vector on one level coarser than current level
420 
421    Collective
422 
423    Input Arguments:
424 .  snes - SNESFAS
425 
426    Output Arguments:
427 .  Xcoarse - vector on level one coarser than snes
428 
429    Level: developer
430 
431 .seealso: SNESFASSetRestriction(), SNESFASRestrict()
432 @*/
433 PetscErrorCode SNESFASCreateCoarseVec(SNES snes,Vec *Xcoarse)
434 {
435   PetscErrorCode ierr;
436   SNES_FAS       *fas = (SNES_FAS*)snes->data;
437 
438   PetscFunctionBegin;
439   if (fas->rscale) {ierr = VecDuplicate(fas->rscale,Xcoarse);CHKERRQ(ierr);}
440   else if (fas->inject) {ierr = MatGetVecs(fas->inject,Xcoarse,PETSC_NULL);CHKERRQ(ierr);}
441   else SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set restriction or injection");CHKERRQ(ierr);
442   PetscFunctionReturn(0);
443 }
444 
445 #undef __FUNCT__
446 #define __FUNCT__ "SNESFASRestrict"
447 /*@
448    SNESFASRestrict - restrict a Vec to the next coarser level
449 
450    Collective
451 
452    Input Arguments:
453 +  fine - SNES from which to restrict
454 -  Xfine - vector to restrict
455 
456    Output Arguments:
457 .  Xcoarse - result of restriction
458 
459    Level: developer
460 
461 .seealso: SNESFASSetRestriction(), SNESFASSetInjection()
462 @*/
463 PetscErrorCode SNESFASRestrict(SNES fine,Vec Xfine,Vec Xcoarse)
464 {
465   PetscErrorCode ierr;
466   SNES_FAS       *fas = (SNES_FAS*)fine->data;
467 
468   PetscFunctionBegin;
469   PetscValidHeaderSpecific(fine,SNES_CLASSID,1);
470   PetscValidHeaderSpecific(Xfine,VEC_CLASSID,2);
471   PetscValidHeaderSpecific(Xcoarse,VEC_CLASSID,3);
472   if (fas->inject) {
473     ierr = MatRestrict(fas->inject,Xfine,Xcoarse);CHKERRQ(ierr);
474   } else {
475     ierr = MatRestrict(fas->restrct,Xfine,Xcoarse);CHKERRQ(ierr);
476     ierr = VecPointwiseMult(Xcoarse,fas->rscale,Xcoarse);CHKERRQ(ierr);
477   }
478   PetscFunctionReturn(0);
479 }
480 
481 #undef __FUNCT__
482 #define __FUNCT__ "FASCoarseCorrection"
483 /*
484 
485 Performs the FAS coarse correction as:
486 
487 fine problem: F(x) = 0
488 coarse problem: F^c(x) = b^c
489 
490 b^c = F^c(I^c_fx^f - I^c_fF(x))
491 
492  */
493 PetscErrorCode FASCoarseCorrection(SNES snes, Vec X, Vec F, Vec X_new) {
494   PetscErrorCode      ierr;
495   Vec                 X_c, Xo_c, F_c, B_c;
496   SNESConvergedReason reason;
497   SNES                next;
498   Mat                 restrct, interpolate;
499   PetscFunctionBegin;
500   ierr = SNESFASCycleGetCorrection(snes, &next);CHKERRQ(ierr);
501   if (next) {
502     ierr = SNESFASCycleGetRestriction(snes, &restrct);CHKERRQ(ierr);
503     ierr = SNESFASCycleGetInterpolation(snes, &interpolate);CHKERRQ(ierr);
504 
505     X_c  = next->vec_sol;
506     Xo_c = next->work[0];
507     F_c  = next->vec_func;
508     B_c  = next->vec_rhs;
509 
510     ierr = SNESFASRestrict(snes,X,Xo_c);CHKERRQ(ierr);
511     /* restrict the defect */
512     ierr = MatRestrict(restrct, F, B_c);CHKERRQ(ierr);
513 
514     /* solve the coarse problem corresponding to F^c(x^c) = b^c = F^c(Rx) - R(F(x) - b) */
515     ierr = SNESComputeFunction(next, Xo_c, F_c);CHKERRQ(ierr);
516     ierr = VecCopy(F_c, B_c);CHKERRQ(ierr);
517 
518     /* set initial guess of the coarse problem to the projected fine solution */
519     ierr = VecCopy(Xo_c, X_c);CHKERRQ(ierr);
520 
521     /* recurse to the next level */
522     ierr = SNESSolve(next, B_c, X_c);CHKERRQ(ierr);
523     ierr = SNESGetConvergedReason(next,&reason);CHKERRQ(ierr);
524     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
525       snes->reason = SNES_DIVERGED_INNER;
526       PetscFunctionReturn(0);
527     }
528     /* correct as x <- x + I(x^c - Rx)*/
529     ierr = VecAXPY(X_c, -1.0, Xo_c);CHKERRQ(ierr);
530     ierr = MatInterpolateAdd(interpolate, X_c, X, X_new);CHKERRQ(ierr);
531   }
532   PetscFunctionReturn(0);
533 }
534 
535 #undef __FUNCT__
536 #define __FUNCT__ "SNESFASCycle_Additive"
537 /*
538 
539 The additive cycle looks like:
540 
541 xhat = x
542 xhat = dS(x, b)
543 x = coarsecorrection(xhat, b_d)
544 x = x + nu*(xhat - x);
545 (optional) x = uS(x, b)
546 
547 With the coarse RHS (defect correction) as below.
548 
549  */
550 PetscErrorCode SNESFASCycle_Additive(SNES snes, Vec X) {
551   Vec                 F, B, Xhat;
552   Vec                 X_c, Xo_c, F_c, B_c;
553   PetscErrorCode      ierr;
554   SNESConvergedReason reason;
555   PetscReal           xnorm, fnorm, ynorm;
556   PetscBool           lssuccess;
557   SNES                next;
558   Mat                 restrct, interpolate;
559   PetscFunctionBegin;
560   ierr = SNESFASCycleGetCorrection(snes, &next);CHKERRQ(ierr);
561   F = snes->vec_func;
562   B = snes->vec_rhs;
563   Xhat = snes->work[1];
564   ierr = VecCopy(X, Xhat);CHKERRQ(ierr);
565   /* recurse first */
566   if (next) {
567     ierr = SNESFASCycleGetRestriction(snes, &restrct);CHKERRQ(ierr);
568     ierr = SNESFASCycleGetInterpolation(snes, &interpolate);CHKERRQ(ierr);
569     ierr = SNESComputeFunction(snes, Xhat, F);CHKERRQ(ierr);
570 
571     X_c  = next->vec_sol;
572     Xo_c = next->work[0];
573     F_c  = next->vec_func;
574     B_c  = next->vec_rhs;
575 
576     ierr = SNESFASRestrict(snes,Xhat,Xo_c);CHKERRQ(ierr);
577     /* restrict the defect */
578     ierr = MatRestrict(restrct, F, B_c);CHKERRQ(ierr);
579 
580     /* solve the coarse problem corresponding to F^c(x^c) = b^c = Rb + F^c(Rx) - RF(x) */
581     ierr = SNESComputeFunction(next, Xo_c, F_c);CHKERRQ(ierr);
582     ierr = VecCopy(F_c, B_c);CHKERRQ(ierr);
583     /* set initial guess of the coarse problem to the projected fine solution */
584     ierr = VecCopy(Xo_c, X_c);CHKERRQ(ierr);
585 
586     /* recurse */
587     ierr = SNESSolve(next, B_c, X_c);CHKERRQ(ierr);
588 
589     /* smooth on this level */
590     ierr = SNESFASDownSmooth_Private(snes, B, X, F, &fnorm);CHKERRQ(ierr);
591 
592     ierr = SNESGetConvergedReason(next,&reason);CHKERRQ(ierr);
593     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
594       snes->reason = SNES_DIVERGED_INNER;
595       PetscFunctionReturn(0);
596     }
597 
598     /* correct as x <- x + I(x^c - Rx)*/
599     ierr = VecAYPX(X_c, -1.0, Xo_c);CHKERRQ(ierr);
600     ierr = MatInterpolate(interpolate, X_c, Xhat);CHKERRQ(ierr);
601 
602     /* additive correction of the coarse direction*/
603     ierr = SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Xhat);CHKERRQ(ierr);
604     ierr = SNESLineSearchGetSuccess(snes->linesearch, &lssuccess);CHKERRQ(ierr);
605     if (!lssuccess) {
606       if (++snes->numFailures >= snes->maxFailures) {
607         snes->reason = SNES_DIVERGED_LINE_SEARCH;
608         PetscFunctionReturn(0);
609       }
610     }
611     ierr = SNESLineSearchGetNorms(snes->linesearch, &xnorm, &snes->norm, &ynorm);CHKERRQ(ierr);
612   } else {
613     ierr = SNESFASDownSmooth_Private(snes, B, X, F, &snes->norm);CHKERRQ(ierr);
614   }
615   PetscFunctionReturn(0);
616 }
617 
618 #undef __FUNCT__
619 #define __FUNCT__ "SNESFASCycle_Multiplicative"
620 /*
621 
622 Defines the FAS cycle as:
623 
624 fine problem: F(x) = 0
625 coarse problem: F^c(x) = b^c
626 
627 b^c = F^c(I^c_fx^f - I^c_fF(x))
628 
629 correction:
630 
631 x = x + I(x^c - Rx)
632 
633  */
634 PetscErrorCode SNESFASCycle_Multiplicative(SNES snes, Vec X) {
635 
636   PetscErrorCode      ierr;
637   Vec                 F,B;
638   SNES_FAS            *fas = (SNES_FAS *)snes->data;
639 
640   PetscFunctionBegin;
641   F = snes->vec_func;
642   B = snes->vec_rhs;
643   /* pre-smooth -- just update using the pre-smoother */
644   ierr = SNESFASDownSmooth_Private(snes, B, X, F, &snes->norm);CHKERRQ(ierr);
645 
646   if (fas->level != 0) {
647     ierr = FASCoarseCorrection(snes, X, F, X);CHKERRQ(ierr);
648     ierr = SNESFASUpSmooth_Private(snes, B, X, F, &snes->norm);CHKERRQ(ierr);
649   }
650   PetscFunctionReturn(0);
651 }
652 
653 #undef __FUNCT__
654 #define __FUNCT__ "SNESSolve_FAS"
655 
656 PetscErrorCode SNESSolve_FAS(SNES snes)
657 {
658   PetscErrorCode ierr;
659   PetscInt       i, maxits;
660   Vec            X, F;
661   PetscReal      fnorm;
662   SNES_FAS       *fas = (SNES_FAS *)snes->data,*ffas;
663   DM             dm;
664   PetscBool      isFine;
665 
666   PetscFunctionBegin;
667   maxits = snes->max_its;            /* maximum number of iterations */
668   snes->reason = SNES_CONVERGED_ITERATING;
669   X = snes->vec_sol;
670   F = snes->vec_func;
671 
672   ierr = SNESFASCycleIsFine(snes, &isFine);CHKERRQ(ierr);
673   /*norm setup */
674   ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
675   snes->iter = 0;
676   snes->norm = 0.;
677   ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
678   if (isFine || fas->monitor) {
679     ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
680     if (snes->domainerror) {
681       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
682       PetscFunctionReturn(0);
683     }
684     ierr = VecNorm(F, NORM_2, &fnorm);CHKERRQ(ierr); /* fnorm <- ||F||  */
685     if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
686     ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
687     snes->norm = fnorm;
688     ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
689     SNESLogConvHistory(snes,fnorm,0);
690     ierr = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr);
691 
692     /* set parameter for default relative tolerance convergence test */
693     snes->ttol = fnorm*snes->rtol;
694     /* test convergence */
695     ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
696     if (snes->reason) PetscFunctionReturn(0);
697   }
698 
699   if (isFine) {
700     /* propagate scale-dependent data up the hierarchy */
701     ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
702     for (ffas=fas; ffas->next; ffas=(SNES_FAS*)ffas->next->data) {
703       DM dmcoarse;
704       ierr = SNESGetDM(ffas->next,&dmcoarse);CHKERRQ(ierr);
705       ierr = DMRestrict(dm,ffas->restrct,ffas->rscale,ffas->inject,dmcoarse);CHKERRQ(ierr);
706       dm = dmcoarse;
707     }
708   }
709 
710   for (i = 0; i < maxits; i++) {
711     /* Call general purpose update function */
712 
713     if (snes->ops->update) {
714       ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
715     }
716     if (fas->fastype == SNES_FAS_MULTIPLICATIVE) {
717       ierr = SNESFASCycle_Multiplicative(snes, X);CHKERRQ(ierr);
718     } else {
719       ierr = SNESFASCycle_Additive(snes, X);CHKERRQ(ierr);
720     }
721 
722     /* check for FAS cycle divergence */
723     if (snes->reason != SNES_CONVERGED_ITERATING) {
724       PetscFunctionReturn(0);
725     }
726 
727     /* Monitor convergence */
728     ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
729     snes->iter = i+1;
730     ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
731     SNESLogConvHistory(snes,snes->norm,0);
732     ierr = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr);
733     /* Test for convergence */
734     if (isFine) {
735       ierr = (*snes->ops->converged)(snes,snes->iter,0.0,0.0,snes->norm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
736       if (snes->reason) break;
737     }
738   }
739   if (i == maxits) {
740     ierr = PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);CHKERRQ(ierr);
741     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
742   }
743   PetscFunctionReturn(0);
744 }
745