xref: /petsc/src/snes/impls/shell/snesshell.c (revision cac4c232dc4f93991e342196e27ef7b0655dac7b)
1af0996ceSBarry Smith #include <petsc/private/snesimpl.h>             /*I   "petscsnes.h"   I*/
2074cc835SBarry Smith 
3074cc835SBarry Smith typedef struct {PetscErrorCode (*solve)(SNES,Vec);void *ctx;} SNES_Shell;
4074cc835SBarry Smith 
5074cc835SBarry Smith /*@C
6074cc835SBarry Smith    SNESShellSetSolve - Sets routine to apply as solver
7074cc835SBarry Smith 
8074cc835SBarry Smith    Logically Collective on SNES
9074cc835SBarry Smith 
10074cc835SBarry Smith    Input Parameters:
11074cc835SBarry Smith +  snes - the nonlinear solver context
12074cc835SBarry Smith -  apply - the application-provided solver routine
13074cc835SBarry Smith 
14074cc835SBarry Smith    Calling sequence of solve:
15074cc835SBarry Smith .vb
16074cc835SBarry Smith    PetscErrorCode apply (SNES snes,Vec xout)
17074cc835SBarry Smith .ve
18074cc835SBarry Smith 
19074cc835SBarry Smith +  snes - the preconditioner, get the application context with SNESShellGetContext()
20074cc835SBarry Smith -  xout - solution vector
21074cc835SBarry Smith 
2295452b02SPatrick Sanan    Notes:
2395452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
24074cc835SBarry Smith 
25074cc835SBarry Smith    Level: advanced
26074cc835SBarry Smith 
27074cc835SBarry Smith .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext()
28074cc835SBarry Smith @*/
29074cc835SBarry Smith PetscErrorCode  SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
30074cc835SBarry Smith {
31074cc835SBarry Smith   PetscFunctionBegin;
32074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33*cac4c232SBarry Smith   PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));
34074cc835SBarry Smith   PetscFunctionReturn(0);
35074cc835SBarry Smith }
36074cc835SBarry Smith 
37074cc835SBarry Smith PetscErrorCode SNESReset_Shell(SNES snes)
38074cc835SBarry Smith {
39074cc835SBarry Smith   PetscFunctionBegin;
40074cc835SBarry Smith   PetscFunctionReturn(0);
41074cc835SBarry Smith }
42074cc835SBarry Smith 
43074cc835SBarry Smith PetscErrorCode SNESDestroy_Shell(SNES snes)
44074cc835SBarry Smith {
45074cc835SBarry Smith   PetscFunctionBegin;
469566063dSJacob Faibussowitsch   PetscCall(SNESReset_Shell(snes));
479566063dSJacob Faibussowitsch   PetscCall(PetscFree(snes->data));
48074cc835SBarry Smith   PetscFunctionReturn(0);
49074cc835SBarry Smith }
50074cc835SBarry Smith 
51074cc835SBarry Smith PetscErrorCode SNESSetUp_Shell(SNES snes)
52074cc835SBarry Smith {
53074cc835SBarry Smith   PetscFunctionBegin;
54074cc835SBarry Smith   PetscFunctionReturn(0);
55074cc835SBarry Smith }
56074cc835SBarry Smith 
574416b707SBarry Smith PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes)
58074cc835SBarry Smith {
59074cc835SBarry Smith   PetscFunctionBegin;
609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHead(PetscOptionsObject,"SNES Shell options"));
61074cc835SBarry Smith   PetscFunctionReturn(0);
62074cc835SBarry Smith }
63074cc835SBarry Smith 
64074cc835SBarry Smith PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
65074cc835SBarry Smith {
66074cc835SBarry Smith   PetscFunctionBegin;
67074cc835SBarry Smith   PetscFunctionReturn(0);
68074cc835SBarry Smith }
69074cc835SBarry Smith 
7090b77ac2SPeter Brune /*@
71074cc835SBarry Smith     SNESShellGetContext - Returns the user-provided context associated with a shell SNES
72074cc835SBarry Smith 
73074cc835SBarry Smith     Not Collective
74074cc835SBarry Smith 
75074cc835SBarry Smith     Input Parameter:
76074cc835SBarry Smith .   snes - should have been created with SNESSetType(snes,SNESSHELL);
77074cc835SBarry Smith 
78074cc835SBarry Smith     Output Parameter:
79074cc835SBarry Smith .   ctx - the user provided context
80074cc835SBarry Smith 
81074cc835SBarry Smith     Level: advanced
82074cc835SBarry Smith 
83074cc835SBarry Smith     Notes:
84074cc835SBarry Smith     This routine is intended for use within various shell routines
85074cc835SBarry Smith 
86074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellSetContext()
87074cc835SBarry Smith @*/
883ec1f749SStefano Zampini PetscErrorCode  SNESShellGetContext(SNES snes,void *ctx)
89074cc835SBarry Smith {
90074cc835SBarry Smith   PetscBool      flg;
91074cc835SBarry Smith 
92074cc835SBarry Smith   PetscFunctionBegin;
93074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
94074cc835SBarry Smith   PetscValidPointer(ctx,2);
959566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg));
963ec1f749SStefano Zampini   if (!flg) *(void**)ctx = NULL;
973ec1f749SStefano Zampini   else      *(void**)ctx = ((SNES_Shell*)(snes->data))->ctx;
98074cc835SBarry Smith   PetscFunctionReturn(0);
99074cc835SBarry Smith }
100074cc835SBarry Smith 
101074cc835SBarry Smith /*@
102074cc835SBarry Smith     SNESShellSetContext - sets the context for a shell SNES
103074cc835SBarry Smith 
104074cc835SBarry Smith    Logically Collective on SNES
105074cc835SBarry Smith 
106074cc835SBarry Smith     Input Parameters:
107074cc835SBarry Smith +   snes - the shell SNES
108074cc835SBarry Smith -   ctx - the context
109074cc835SBarry Smith 
110074cc835SBarry Smith    Level: advanced
111074cc835SBarry Smith 
11295452b02SPatrick Sanan    Fortran Notes:
11395452b02SPatrick Sanan     The context can only be an integer or a PetscObject
114074cc835SBarry Smith       unfortunately it cannot be a Fortran array or derived type.
115074cc835SBarry Smith 
116074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellGetContext()
117074cc835SBarry Smith @*/
118074cc835SBarry Smith PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
119074cc835SBarry Smith {
120074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell*)snes->data;
121074cc835SBarry Smith   PetscBool      flg;
122074cc835SBarry Smith 
123074cc835SBarry Smith   PetscFunctionBegin;
124074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg));
1261aa26658SKarl Rupp   if (flg) shell->ctx = ctx;
127074cc835SBarry Smith   PetscFunctionReturn(0);
128074cc835SBarry Smith }
129074cc835SBarry Smith 
130074cc835SBarry Smith PetscErrorCode SNESSolve_Shell(SNES snes)
131074cc835SBarry Smith {
132074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell*) snes->data;
133074cc835SBarry Smith 
134074cc835SBarry Smith   PetscFunctionBegin;
13528b400f6SJacob Faibussowitsch   PetscCheck(shell->solve,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first");
1361e633543SBarry Smith   snes->reason = SNES_CONVERGED_ITS;
1379566063dSJacob Faibussowitsch   PetscCall((*shell->solve)(snes,snes->vec_sol));
138074cc835SBarry Smith   PetscFunctionReturn(0);
139074cc835SBarry Smith }
140074cc835SBarry Smith 
141074cc835SBarry Smith PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
142074cc835SBarry Smith {
143074cc835SBarry Smith   SNES_Shell *shell = (SNES_Shell*)snes->data;
144074cc835SBarry Smith 
145074cc835SBarry Smith   PetscFunctionBegin;
146074cc835SBarry Smith   shell->solve = solve;
147074cc835SBarry Smith   PetscFunctionReturn(0);
148074cc835SBarry Smith }
149074cc835SBarry Smith 
150074cc835SBarry Smith /*MC
151074cc835SBarry Smith   SNESSHELL - a user provided nonlinear solver
152074cc835SBarry Smith 
153074cc835SBarry Smith    Level: advanced
154074cc835SBarry Smith 
155074cc835SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
156074cc835SBarry Smith M*/
157074cc835SBarry Smith 
1588cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
159074cc835SBarry Smith {
160074cc835SBarry Smith   SNES_Shell     *shell;
161074cc835SBarry Smith 
162074cc835SBarry Smith   PetscFunctionBegin;
163074cc835SBarry Smith   snes->ops->destroy        = SNESDestroy_Shell;
164074cc835SBarry Smith   snes->ops->setup          = SNESSetUp_Shell;
165074cc835SBarry Smith   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
166074cc835SBarry Smith   snes->ops->view           = SNESView_Shell;
167074cc835SBarry Smith   snes->ops->solve          = SNESSolve_Shell;
168074cc835SBarry Smith   snes->ops->reset          = SNESReset_Shell;
169074cc835SBarry Smith 
170074cc835SBarry Smith   snes->usesksp = PETSC_FALSE;
171efd4aadfSBarry Smith   snes->usesnpc = PETSC_FALSE;
172074cc835SBarry Smith 
1734fc747eaSLawrence Mitchell   snes->alwayscomputesfinalresidual = PETSC_FALSE;
1744fc747eaSLawrence Mitchell 
1759566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(snes,&shell));
176074cc835SBarry Smith   snes->data = (void*) shell;
1779566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell));
178074cc835SBarry Smith   PetscFunctionReturn(0);
179074cc835SBarry Smith }
180