xref: /petsc/src/snes/impls/fas/fasgalerkin.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
1 #include <../src/snes/impls/fas/fasimpls.h> /*I  "petscsnes.h"  I*/
2 
3 /*@
4    SNESFASGetGalerkin - Gets if the coarse problems are formed by projection to the fine problem
5 
6    Input Parameter:
7 .  snes - the nonlinear solver context
8 
9    Output parameter:
10 .  flg - the status of the galerkin problem
11 
12    Level: advanced
13 
14 .seealso: SNESFASSetLevels(), SNESFASSetGalerkin()
15 @*/
16 PetscErrorCode SNESFASGetGalerkin(SNES snes, PetscBool *flg)
17 {
18   SNES_FAS *fas;
19 
20   PetscFunctionBegin;
21   PetscValidHeaderSpecificType(snes,SNES_CLASSID,1,SNESFAS);
22   fas = (SNES_FAS*)snes->data;
23   *flg = fas->galerkin;
24   PetscFunctionReturn(0);
25 }
26 
27 /*@
28    SNESFASSetGalerkin - Sets coarse problems as formed by projection to the fine problem
29 
30    Input Parameters:
31 +  snes - the nonlinear solver context
32 -  flg - the status of the galerkin problem
33 
34    Level: advanced
35 
36 .seealso: SNESFASSetLevels(), SNESFASGetGalerkin()
37 @*/
38 PetscErrorCode SNESFASSetGalerkin(SNES snes, PetscBool flg)
39 {
40   SNES_FAS       *fas;
41 
42   PetscFunctionBegin;
43   PetscValidHeaderSpecificType(snes,SNES_CLASSID,1,SNESFAS);
44   fas = (SNES_FAS*)snes->data;
45   fas->galerkin = flg;
46   if (fas->next) PetscCall(SNESFASSetGalerkin(fas->next, flg));
47   PetscFunctionReturn(0);
48 }
49 
50 /*@C
51    SNESFASGalerkinFunctionDefault - Computes the Galerkin FAS function
52 
53    Input Parameters:
54 +  snes - the nonlinear solver context
55 .  X - input vector
56 -  ctx - the FAS context
57 
58    Output Parameter:
59 .  F - output vector
60 
61    Notes:
62    The Galerkin FAS function evalutation is defined as
63 $  F^l(x^l) = I^l_0 F^0(P^0_l x^l)
64 
65    Level: developer
66 
67 .seealso: SNESFASGetGalerkin(), SNESFASSetGalerkin()
68 @*/
69 PetscErrorCode SNESFASGalerkinFunctionDefault(SNES snes, Vec X, Vec F, void *ctx)
70 {
71   SNES           fassnes;
72   SNES_FAS       *fas;
73   SNES_FAS       *prevfas;
74   SNES           prevsnes;
75   Vec            b_temp;
76 
77   PetscFunctionBegin;
78   /* prolong to the fine level and evaluate there. */
79   fassnes  = (SNES)ctx;
80   fas      = (SNES_FAS*)fassnes->data;
81   prevsnes = fas->previous;
82   prevfas  = (SNES_FAS*)prevsnes->data;
83   /* interpolate down the solution */
84   PetscCall(MatInterpolate(prevfas->interpolate, X, prevfas->Xg));
85   /* the RHS we care about is at the coarsest level */
86   b_temp            = prevsnes->vec_rhs;
87   prevsnes->vec_rhs = NULL;
88   PetscCall(SNESComputeFunction(prevsnes, prevfas->Xg, prevfas->Fg));
89   prevsnes->vec_rhs = b_temp;
90   /* restrict up the function */
91   PetscCall(MatRestrict(prevfas->restrct, prevfas->Fg, F));
92   PetscFunctionReturn(0);
93 }
94