xref: /petsc/src/snes/impls/shell/snesshell.c (revision efd4aadf157bf1ba2d80c2be092fcf4247860003)
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 
22074cc835SBarry Smith    Notes: the function MUST return an error code of 0 on success and nonzero on failure.
23074cc835SBarry Smith 
24074cc835SBarry Smith    Level: advanced
25074cc835SBarry Smith 
26074cc835SBarry Smith .keywords: SNES, shell, set, apply, user-provided
27074cc835SBarry Smith 
28074cc835SBarry Smith .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext()
29074cc835SBarry Smith @*/
30074cc835SBarry Smith PetscErrorCode  SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
31074cc835SBarry Smith {
32074cc835SBarry Smith   PetscErrorCode ierr;
33074cc835SBarry Smith 
34074cc835SBarry Smith   PetscFunctionBegin;
35074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
36074cc835SBarry Smith   ierr = PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));CHKERRQ(ierr);
37074cc835SBarry Smith   PetscFunctionReturn(0);
38074cc835SBarry Smith }
39074cc835SBarry Smith 
40074cc835SBarry Smith PetscErrorCode SNESReset_Shell(SNES snes)
41074cc835SBarry Smith {
42074cc835SBarry Smith   PetscFunctionBegin;
43074cc835SBarry Smith   PetscFunctionReturn(0);
44074cc835SBarry Smith }
45074cc835SBarry Smith 
46074cc835SBarry Smith PetscErrorCode SNESDestroy_Shell(SNES snes)
47074cc835SBarry Smith {
48074cc835SBarry Smith   PetscErrorCode ierr;
49074cc835SBarry Smith 
50074cc835SBarry Smith   PetscFunctionBegin;
51074cc835SBarry Smith   ierr = SNESReset_Shell(snes);CHKERRQ(ierr);
5222d28d08SBarry Smith   ierr = PetscFree(snes->data);CHKERRQ(ierr);
53074cc835SBarry Smith   PetscFunctionReturn(0);
54074cc835SBarry Smith }
55074cc835SBarry Smith 
56074cc835SBarry Smith PetscErrorCode SNESSetUp_Shell(SNES snes)
57074cc835SBarry Smith {
58074cc835SBarry Smith   PetscFunctionBegin;
59074cc835SBarry Smith   PetscFunctionReturn(0);
60074cc835SBarry Smith }
61074cc835SBarry Smith 
624416b707SBarry Smith PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes)
63074cc835SBarry Smith {
64074cc835SBarry Smith   PetscErrorCode ierr;
65074cc835SBarry Smith 
66074cc835SBarry Smith   PetscFunctionBegin;
67e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"SNES Shell options");CHKERRQ(ierr);
68074cc835SBarry Smith   PetscFunctionReturn(0);
69074cc835SBarry Smith }
70074cc835SBarry Smith 
71074cc835SBarry Smith PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
72074cc835SBarry Smith {
73074cc835SBarry Smith   PetscFunctionBegin;
74074cc835SBarry Smith   PetscFunctionReturn(0);
75074cc835SBarry Smith }
76074cc835SBarry Smith 
7790b77ac2SPeter Brune /*@
78074cc835SBarry Smith     SNESShellGetContext - Returns the user-provided context associated with a shell SNES
79074cc835SBarry Smith 
80074cc835SBarry Smith     Not Collective
81074cc835SBarry Smith 
82074cc835SBarry Smith     Input Parameter:
83074cc835SBarry Smith .   snes - should have been created with SNESSetType(snes,SNESSHELL);
84074cc835SBarry Smith 
85074cc835SBarry Smith     Output Parameter:
86074cc835SBarry Smith .   ctx - the user provided context
87074cc835SBarry Smith 
88074cc835SBarry Smith     Level: advanced
89074cc835SBarry Smith 
90074cc835SBarry Smith     Notes:
91074cc835SBarry Smith     This routine is intended for use within various shell routines
92074cc835SBarry Smith 
93074cc835SBarry Smith .keywords: SNES, shell, get, context
94074cc835SBarry Smith 
95074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellSetContext()
96074cc835SBarry Smith @*/
97074cc835SBarry Smith PetscErrorCode  SNESShellGetContext(SNES snes,void **ctx)
98074cc835SBarry Smith {
99074cc835SBarry Smith   PetscErrorCode ierr;
100074cc835SBarry Smith   PetscBool      flg;
101074cc835SBarry Smith 
102074cc835SBarry Smith   PetscFunctionBegin;
103074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
104074cc835SBarry Smith   PetscValidPointer(ctx,2);
105251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr);
106074cc835SBarry Smith   if (!flg) *ctx = 0;
107074cc835SBarry Smith   else      *ctx = ((SNES_Shell*)(snes->data))->ctx;
108074cc835SBarry Smith   PetscFunctionReturn(0);
109074cc835SBarry Smith }
110074cc835SBarry Smith 
111074cc835SBarry Smith /*@
112074cc835SBarry Smith     SNESShellSetContext - sets the context for a shell SNES
113074cc835SBarry Smith 
114074cc835SBarry Smith    Logically Collective on SNES
115074cc835SBarry Smith 
116074cc835SBarry Smith     Input Parameters:
117074cc835SBarry Smith +   snes - the shell SNES
118074cc835SBarry Smith -   ctx - the context
119074cc835SBarry Smith 
120074cc835SBarry Smith    Level: advanced
121074cc835SBarry Smith 
122074cc835SBarry Smith    Fortran Notes: The context can only be an integer or a PetscObject
123074cc835SBarry Smith       unfortunately it cannot be a Fortran array or derived type.
124074cc835SBarry Smith 
125074cc835SBarry Smith 
126074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellGetContext()
127074cc835SBarry Smith @*/
128074cc835SBarry Smith PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
129074cc835SBarry Smith {
130074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell*)snes->data;
131074cc835SBarry Smith   PetscErrorCode ierr;
132074cc835SBarry Smith   PetscBool      flg;
133074cc835SBarry Smith 
134074cc835SBarry Smith   PetscFunctionBegin;
135074cc835SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
136251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr);
1371aa26658SKarl Rupp   if (flg) shell->ctx = ctx;
138074cc835SBarry Smith   PetscFunctionReturn(0);
139074cc835SBarry Smith }
140074cc835SBarry Smith 
141074cc835SBarry Smith PetscErrorCode SNESSolve_Shell(SNES snes)
142074cc835SBarry Smith {
143074cc835SBarry Smith   SNES_Shell     *shell = (SNES_Shell*) snes->data;
144074cc835SBarry Smith   PetscErrorCode ierr;
145074cc835SBarry Smith 
146074cc835SBarry Smith   PetscFunctionBegin;
147ce94432eSBarry Smith   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first");
1481e633543SBarry Smith   snes->reason = SNES_CONVERGED_ITS;
149074cc835SBarry Smith   ierr         = (*shell->solve)(snes,snes->vec_sol);CHKERRQ(ierr);
150074cc835SBarry Smith   PetscFunctionReturn(0);
151074cc835SBarry Smith }
152074cc835SBarry Smith 
153074cc835SBarry Smith PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
154074cc835SBarry Smith {
155074cc835SBarry Smith   SNES_Shell *shell = (SNES_Shell*)snes->data;
156074cc835SBarry Smith 
157074cc835SBarry Smith   PetscFunctionBegin;
158074cc835SBarry Smith   shell->solve = solve;
159074cc835SBarry Smith   PetscFunctionReturn(0);
160074cc835SBarry Smith }
161074cc835SBarry Smith 
162074cc835SBarry Smith /*MC
163074cc835SBarry Smith   SNESSHELL - a user provided nonlinear solver
164074cc835SBarry Smith 
165074cc835SBarry Smith    Level: advanced
166074cc835SBarry Smith 
167074cc835SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
168074cc835SBarry Smith M*/
169074cc835SBarry Smith 
1708cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
171074cc835SBarry Smith {
172074cc835SBarry Smith   SNES_Shell     *shell;
173074cc835SBarry Smith   PetscErrorCode ierr;
174074cc835SBarry Smith 
175074cc835SBarry Smith   PetscFunctionBegin;
176074cc835SBarry Smith   snes->ops->destroy        = SNESDestroy_Shell;
177074cc835SBarry Smith   snes->ops->setup          = SNESSetUp_Shell;
178074cc835SBarry Smith   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
179074cc835SBarry Smith   snes->ops->view           = SNESView_Shell;
180074cc835SBarry Smith   snes->ops->solve          = SNESSolve_Shell;
181074cc835SBarry Smith   snes->ops->reset          = SNESReset_Shell;
182074cc835SBarry Smith 
183074cc835SBarry Smith   snes->usesksp = PETSC_FALSE;
184*efd4aadfSBarry Smith   snes->usesnpc = PETSC_FALSE;
185074cc835SBarry Smith 
1864fc747eaSLawrence Mitchell   snes->alwayscomputesfinalresidual = PETSC_FALSE;
1874fc747eaSLawrence Mitchell 
188b00a9115SJed Brown   ierr       = PetscNewLog(snes,&shell);CHKERRQ(ierr);
189074cc835SBarry Smith   snes->data = (void*) shell;
190bdf89e91SBarry Smith   ierr       = PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);CHKERRQ(ierr);
191074cc835SBarry Smith   PetscFunctionReturn(0);
192074cc835SBarry Smith }
193