xref: /petsc/src/snes/impls/shell/snesshell.c (revision 074cc835faa3d6c800494dd2ea2bd8f95d70c354)
1*074cc835SBarry Smith #include <private/snesimpl.h>
2*074cc835SBarry Smith 
3*074cc835SBarry Smith typedef struct {PetscErrorCode (*solve)(SNES,Vec);void *ctx;} SNES_Shell;
4*074cc835SBarry Smith 
5*074cc835SBarry Smith #undef __FUNCT__
6*074cc835SBarry Smith #define __FUNCT__ "SNESShellSetSolve"
7*074cc835SBarry Smith /*@C
8*074cc835SBarry Smith    SNESShellSetSolve - Sets routine to apply as solver
9*074cc835SBarry Smith 
10*074cc835SBarry Smith    Logically Collective on SNES
11*074cc835SBarry Smith 
12*074cc835SBarry Smith    Input Parameters:
13*074cc835SBarry Smith +  snes - the nonlinear solver context
14*074cc835SBarry Smith -  apply - the application-provided solver routine
15*074cc835SBarry Smith 
16*074cc835SBarry Smith    Calling sequence of solve:
17*074cc835SBarry Smith .vb
18*074cc835SBarry Smith    PetscErrorCode apply (SNES snes,Vec xout)
19*074cc835SBarry Smith .ve
20*074cc835SBarry Smith 
21*074cc835SBarry Smith +  snes - the preconditioner, get the application context with SNESShellGetContext()
22*074cc835SBarry Smith -  xout - solution vector
23*074cc835SBarry Smith 
24*074cc835SBarry Smith    Notes: the function MUST return an error code of 0 on success and nonzero on failure.
25*074cc835SBarry Smith 
26*074cc835SBarry Smith    Level: advanced
27*074cc835SBarry Smith 
28*074cc835SBarry Smith .keywords: SNES, shell, set, apply, user-provided
29*074cc835SBarry Smith 
30*074cc835SBarry Smith .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext()
31*074cc835SBarry Smith @*/
32*074cc835SBarry Smith PetscErrorCode  SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
33*074cc835SBarry Smith {
34*074cc835SBarry Smith   PetscErrorCode ierr;
35*074cc835SBarry Smith 
36*074cc835SBarry Smith   PetscFunctionBegin;
37*074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
38*074cc835SBarry Smith   ierr = PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));CHKERRQ(ierr);
39*074cc835SBarry Smith   PetscFunctionReturn(0);
40*074cc835SBarry Smith }
41*074cc835SBarry Smith 
42*074cc835SBarry Smith #undef __FUNCT__
43*074cc835SBarry Smith #define __FUNCT__ "SNESReset_Shell"
44*074cc835SBarry Smith PetscErrorCode SNESReset_Shell(SNES snes)
45*074cc835SBarry Smith {
46*074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell*) snes->data;
47*074cc835SBarry Smith 
48*074cc835SBarry Smith   PetscFunctionBegin;
49*074cc835SBarry Smith   PetscFunctionReturn(0);
50*074cc835SBarry Smith }
51*074cc835SBarry Smith 
52*074cc835SBarry Smith #undef __FUNCT__
53*074cc835SBarry Smith #define __FUNCT__ "SNESDestroy_Shell"
54*074cc835SBarry Smith PetscErrorCode SNESDestroy_Shell(SNES snes)
55*074cc835SBarry Smith {
56*074cc835SBarry Smith   PetscErrorCode ierr;
57*074cc835SBarry Smith 
58*074cc835SBarry Smith   PetscFunctionBegin;
59*074cc835SBarry Smith   ierr = SNESReset_Shell(snes);CHKERRQ(ierr);
60*074cc835SBarry Smith   ierr = PetscFree(snes->data);
61*074cc835SBarry Smith   PetscFunctionReturn(0);
62*074cc835SBarry Smith }
63*074cc835SBarry Smith 
64*074cc835SBarry Smith #undef __FUNCT__
65*074cc835SBarry Smith #define __FUNCT__ "SNESSetUp_Shell"
66*074cc835SBarry Smith PetscErrorCode SNESSetUp_Shell(SNES snes)
67*074cc835SBarry Smith {
68*074cc835SBarry Smith   SNES_Shell    *shell = (SNES_Shell *) snes->data;
69*074cc835SBarry Smith 
70*074cc835SBarry Smith   PetscFunctionBegin;
71*074cc835SBarry Smith   PetscFunctionReturn(0);
72*074cc835SBarry Smith }
73*074cc835SBarry Smith 
74*074cc835SBarry Smith #undef __FUNCT__
75*074cc835SBarry Smith #define __FUNCT__ "SNESSetFromOptions_Shell"
76*074cc835SBarry Smith PetscErrorCode SNESSetFromOptions_Shell(SNES snes)
77*074cc835SBarry Smith {
78*074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell *) snes->data;
79*074cc835SBarry Smith   PetscErrorCode ierr;
80*074cc835SBarry Smith 
81*074cc835SBarry Smith   PetscFunctionBegin;
82*074cc835SBarry Smith   ierr = PetscOptionsHead("SNES Shell options");CHKERRQ(ierr);
83*074cc835SBarry Smith   PetscFunctionReturn(0);
84*074cc835SBarry Smith }
85*074cc835SBarry Smith 
86*074cc835SBarry Smith #undef __FUNCT__
87*074cc835SBarry Smith #define __FUNCT__ "SNESView_Shell"
88*074cc835SBarry Smith PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
89*074cc835SBarry Smith {
90*074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell *) snes->data;
91*074cc835SBarry Smith 
92*074cc835SBarry Smith   PetscFunctionBegin;
93*074cc835SBarry Smith   PetscFunctionReturn(0);
94*074cc835SBarry Smith }
95*074cc835SBarry Smith 
96*074cc835SBarry Smith #undef __FUNCT__
97*074cc835SBarry Smith #define __FUNCT__ "SNESShellGetContext"
98*074cc835SBarry Smith /*@C
99*074cc835SBarry Smith     SNESShellGetContext - Returns the user-provided context associated with a shell SNES
100*074cc835SBarry Smith 
101*074cc835SBarry Smith     Not Collective
102*074cc835SBarry Smith 
103*074cc835SBarry Smith     Input Parameter:
104*074cc835SBarry Smith .   snes - should have been created with SNESSetType(snes,SNESSHELL);
105*074cc835SBarry Smith 
106*074cc835SBarry Smith     Output Parameter:
107*074cc835SBarry Smith .   ctx - the user provided context
108*074cc835SBarry Smith 
109*074cc835SBarry Smith     Level: advanced
110*074cc835SBarry Smith 
111*074cc835SBarry Smith     Notes:
112*074cc835SBarry Smith     This routine is intended for use within various shell routines
113*074cc835SBarry Smith 
114*074cc835SBarry Smith .keywords: SNES, shell, get, context
115*074cc835SBarry Smith 
116*074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellSetContext()
117*074cc835SBarry Smith @*/
118*074cc835SBarry Smith PetscErrorCode  SNESShellGetContext(SNES snes,void **ctx)
119*074cc835SBarry Smith {
120*074cc835SBarry Smith   PetscErrorCode ierr;
121*074cc835SBarry Smith   PetscBool      flg;
122*074cc835SBarry Smith 
123*074cc835SBarry Smith   PetscFunctionBegin;
124*074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
125*074cc835SBarry Smith   PetscValidPointer(ctx,2);
126*074cc835SBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr);
127*074cc835SBarry Smith   if (!flg) *ctx = 0;
128*074cc835SBarry Smith   else      *ctx = ((SNES_Shell*)(snes->data))->ctx;
129*074cc835SBarry Smith   PetscFunctionReturn(0);
130*074cc835SBarry Smith }
131*074cc835SBarry Smith 
132*074cc835SBarry Smith #undef __FUNCT__
133*074cc835SBarry Smith #define __FUNCT__ "SNESShellSetContext"
134*074cc835SBarry Smith /*@
135*074cc835SBarry Smith     SNESShellSetContext - sets the context for a shell SNES
136*074cc835SBarry Smith 
137*074cc835SBarry Smith    Logically Collective on SNES
138*074cc835SBarry Smith 
139*074cc835SBarry Smith     Input Parameters:
140*074cc835SBarry Smith +   snes - the shell SNES
141*074cc835SBarry Smith -   ctx - the context
142*074cc835SBarry Smith 
143*074cc835SBarry Smith    Level: advanced
144*074cc835SBarry Smith 
145*074cc835SBarry Smith    Fortran Notes: The context can only be an integer or a PetscObject
146*074cc835SBarry Smith       unfortunately it cannot be a Fortran array or derived type.
147*074cc835SBarry Smith 
148*074cc835SBarry Smith 
149*074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellGetContext()
150*074cc835SBarry Smith @*/
151*074cc835SBarry Smith PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
152*074cc835SBarry Smith {
153*074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell*)snes->data;
154*074cc835SBarry Smith   PetscErrorCode ierr;
155*074cc835SBarry Smith   PetscBool      flg;
156*074cc835SBarry Smith 
157*074cc835SBarry Smith   PetscFunctionBegin;
158*074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
159*074cc835SBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr);
160*074cc835SBarry Smith   if (flg) {
161*074cc835SBarry Smith     shell->ctx = ctx;
162*074cc835SBarry Smith   }
163*074cc835SBarry Smith   PetscFunctionReturn(0);
164*074cc835SBarry Smith }
165*074cc835SBarry Smith 
166*074cc835SBarry Smith #undef __FUNCT__
167*074cc835SBarry Smith #define __FUNCT__ "SNESSolve_Shell"
168*074cc835SBarry Smith PetscErrorCode SNESSolve_Shell(SNES snes)
169*074cc835SBarry Smith {
170*074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell *) snes->data;
171*074cc835SBarry Smith   PetscErrorCode ierr;
172*074cc835SBarry Smith 
173*074cc835SBarry Smith   PetscFunctionBegin;
174*074cc835SBarry Smith   if (!shell->solve) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first");
175*074cc835SBarry Smith   ierr = (*shell->solve)(snes,snes->vec_sol);CHKERRQ(ierr);
176*074cc835SBarry Smith   PetscFunctionReturn(0);
177*074cc835SBarry Smith }
178*074cc835SBarry Smith 
179*074cc835SBarry Smith EXTERN_C_BEGIN
180*074cc835SBarry Smith #undef __FUNCT__
181*074cc835SBarry Smith #define __FUNCT__ "SNESShellSetSolve_Shell"
182*074cc835SBarry Smith PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
183*074cc835SBarry Smith {
184*074cc835SBarry Smith   SNES_Shell *shell = (SNES_Shell*)snes->data;
185*074cc835SBarry Smith 
186*074cc835SBarry Smith   PetscFunctionBegin;
187*074cc835SBarry Smith   shell->solve = solve;
188*074cc835SBarry Smith   PetscFunctionReturn(0);
189*074cc835SBarry Smith }
190*074cc835SBarry Smith EXTERN_C_END
191*074cc835SBarry Smith 
192*074cc835SBarry Smith /*MC
193*074cc835SBarry Smith   SNESSHELL - a user provided nonlinear solver
194*074cc835SBarry Smith 
195*074cc835SBarry Smith    Level: advanced
196*074cc835SBarry Smith 
197*074cc835SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
198*074cc835SBarry Smith M*/
199*074cc835SBarry Smith 
200*074cc835SBarry Smith EXTERN_C_BEGIN
201*074cc835SBarry Smith #undef __FUNCT__
202*074cc835SBarry Smith #define __FUNCT__ "SNESCreate_Shell"
203*074cc835SBarry Smith PetscErrorCode SNESCreate_Shell(SNES snes)
204*074cc835SBarry Smith {
205*074cc835SBarry Smith   SNES_Shell     *shell;
206*074cc835SBarry Smith   PetscErrorCode ierr;
207*074cc835SBarry Smith 
208*074cc835SBarry Smith   PetscFunctionBegin;
209*074cc835SBarry Smith   snes->ops->destroy        = SNESDestroy_Shell;
210*074cc835SBarry Smith   snes->ops->setup          = SNESSetUp_Shell;
211*074cc835SBarry Smith   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
212*074cc835SBarry Smith   snes->ops->view           = SNESView_Shell;
213*074cc835SBarry Smith   snes->ops->solve          = SNESSolve_Shell;
214*074cc835SBarry Smith   snes->ops->reset          = SNESReset_Shell;
215*074cc835SBarry Smith 
216*074cc835SBarry Smith   snes->usesksp             = PETSC_FALSE;
217*074cc835SBarry Smith 
218*074cc835SBarry Smith   ierr = PetscNewLog(snes, SNES_Shell, &shell);CHKERRQ(ierr);
219*074cc835SBarry Smith   snes->data = (void*) shell;
220*074cc835SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESShellSetSolve_C","SNESShellSetSolve_Shell",SNESShellSetSolve_Shell);CHKERRQ(ierr);
221*074cc835SBarry Smith   PetscFunctionReturn(0);
222*074cc835SBarry Smith }
223*074cc835SBarry Smith EXTERN_C_END
224