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