xref: /petsc/src/tao/shell/taoshell.c (revision 83a0a5c3df6d3e0f7da064b660be189e8d26b53c)
1*83a0a5c3SToby Isaac #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/
2*83a0a5c3SToby Isaac 
3*83a0a5c3SToby Isaac typedef struct _n_TaoShell Tao_Shell;
4*83a0a5c3SToby Isaac 
5*83a0a5c3SToby Isaac struct _n_TaoShell
6*83a0a5c3SToby Isaac {
7*83a0a5c3SToby Isaac   PetscErrorCode (*solve)(Tao);
8*83a0a5c3SToby Isaac   void *ctx;
9*83a0a5c3SToby Isaac };
10*83a0a5c3SToby Isaac 
11*83a0a5c3SToby Isaac /*@C
12*83a0a5c3SToby Isaac    TaoShellSetSolve - Sets routine to apply as solver
13*83a0a5c3SToby Isaac 
14*83a0a5c3SToby Isaac    Logically Collective on Tao
15*83a0a5c3SToby Isaac 
16*83a0a5c3SToby Isaac    Input Parameters:
17*83a0a5c3SToby Isaac +  tao - the nonlinear solver context
18*83a0a5c3SToby Isaac -  solve - the application-provided solver routine
19*83a0a5c3SToby Isaac 
20*83a0a5c3SToby Isaac    Calling sequence of solve:
21*83a0a5c3SToby Isaac .vb
22*83a0a5c3SToby Isaac    PetscErrorCode solve (Tao tao)
23*83a0a5c3SToby Isaac .ve
24*83a0a5c3SToby Isaac 
25*83a0a5c3SToby Isaac .  tao - the optimizer, get the application context with TaoShellGetContext()
26*83a0a5c3SToby Isaac 
27*83a0a5c3SToby Isaac    Notes:
28*83a0a5c3SToby Isaac     the function MUST return an error code of 0 on success and nonzero on failure.
29*83a0a5c3SToby Isaac 
30*83a0a5c3SToby Isaac    Level: advanced
31*83a0a5c3SToby Isaac 
32*83a0a5c3SToby Isaac .keywords: Tao, shell, set, user-provided
33*83a0a5c3SToby Isaac 
34*83a0a5c3SToby Isaac .seealso: TAOSHELL, TaoShellSetContext(), TaoShellGetContext()
35*83a0a5c3SToby Isaac @*/
36*83a0a5c3SToby Isaac PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve) (Tao))
37*83a0a5c3SToby Isaac {
38*83a0a5c3SToby Isaac   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
39*83a0a5c3SToby Isaac 
40*83a0a5c3SToby Isaac   PetscFunctionBegin;
41*83a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
42*83a0a5c3SToby Isaac   shell->solve = solve;
43*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
44*83a0a5c3SToby Isaac }
45*83a0a5c3SToby Isaac 
46*83a0a5c3SToby Isaac /*@
47*83a0a5c3SToby Isaac     TaoShellGetContext - Returns the user-provided context associated with a shell Tao
48*83a0a5c3SToby Isaac 
49*83a0a5c3SToby Isaac     Not Collective
50*83a0a5c3SToby Isaac 
51*83a0a5c3SToby Isaac     Input Parameter:
52*83a0a5c3SToby Isaac .   tao - should have been created with TaoSetType(tao,TAOSHELL);
53*83a0a5c3SToby Isaac 
54*83a0a5c3SToby Isaac     Output Parameter:
55*83a0a5c3SToby Isaac .   ctx - the user provided context
56*83a0a5c3SToby Isaac 
57*83a0a5c3SToby Isaac     Level: advanced
58*83a0a5c3SToby Isaac 
59*83a0a5c3SToby Isaac     Notes:
60*83a0a5c3SToby Isaac     This routine is intended for use within various shell routines
61*83a0a5c3SToby Isaac 
62*83a0a5c3SToby Isaac .keywords: Tao, shell, get, context
63*83a0a5c3SToby Isaac 
64*83a0a5c3SToby Isaac .seealso: TaoCreateShell(), TaoShellSetContext()
65*83a0a5c3SToby Isaac @*/
66*83a0a5c3SToby Isaac PetscErrorCode  TaoShellGetContext(Tao tao,void **ctx)
67*83a0a5c3SToby Isaac {
68*83a0a5c3SToby Isaac   PetscErrorCode ierr;
69*83a0a5c3SToby Isaac   PetscBool      flg;
70*83a0a5c3SToby Isaac 
71*83a0a5c3SToby Isaac   PetscFunctionBegin;
72*83a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
73*83a0a5c3SToby Isaac   PetscValidPointer(ctx,2);
74*83a0a5c3SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);CHKERRQ(ierr);
75*83a0a5c3SToby Isaac   if (!flg) *ctx = 0;
76*83a0a5c3SToby Isaac   else      *ctx = ((Tao_Shell*)(tao->data))->ctx;
77*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
78*83a0a5c3SToby Isaac }
79*83a0a5c3SToby Isaac 
80*83a0a5c3SToby Isaac /*@
81*83a0a5c3SToby Isaac     TaoShellSetContext - sets the context for a shell Tao
82*83a0a5c3SToby Isaac 
83*83a0a5c3SToby Isaac    Logically Collective on Tao
84*83a0a5c3SToby Isaac 
85*83a0a5c3SToby Isaac     Input Parameters:
86*83a0a5c3SToby Isaac +   tao - the shell Tao
87*83a0a5c3SToby Isaac -   ctx - the context
88*83a0a5c3SToby Isaac 
89*83a0a5c3SToby Isaac    Level: advanced
90*83a0a5c3SToby Isaac 
91*83a0a5c3SToby Isaac    Fortran Notes:
92*83a0a5c3SToby Isaac     The context can only be an integer or a PetscObject
93*83a0a5c3SToby Isaac       unfortunately it cannot be a Fortran array or derived type.
94*83a0a5c3SToby Isaac 
95*83a0a5c3SToby Isaac 
96*83a0a5c3SToby Isaac .seealso: TaoCreateShell(), TaoShellGetContext()
97*83a0a5c3SToby Isaac @*/
98*83a0a5c3SToby Isaac PetscErrorCode  TaoShellSetContext(Tao tao,void *ctx)
99*83a0a5c3SToby Isaac {
100*83a0a5c3SToby Isaac   Tao_Shell     *shell = (Tao_Shell*)tao->data;
101*83a0a5c3SToby Isaac   PetscErrorCode ierr;
102*83a0a5c3SToby Isaac   PetscBool      flg;
103*83a0a5c3SToby Isaac 
104*83a0a5c3SToby Isaac   PetscFunctionBegin;
105*83a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
106*83a0a5c3SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);CHKERRQ(ierr);
107*83a0a5c3SToby Isaac   if (flg) shell->ctx = ctx;
108*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
109*83a0a5c3SToby Isaac }
110*83a0a5c3SToby Isaac 
111*83a0a5c3SToby Isaac static PetscErrorCode TaoSolve_Shell(Tao tao)
112*83a0a5c3SToby Isaac {
113*83a0a5c3SToby Isaac   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
114*83a0a5c3SToby Isaac   PetscErrorCode               ierr;
115*83a0a5c3SToby Isaac 
116*83a0a5c3SToby Isaac   PetscFunctionBegin;
117*83a0a5c3SToby Isaac   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first");
118*83a0a5c3SToby Isaac   tao->reason = TAO_CONVERGED_USER;
119*83a0a5c3SToby Isaac   ierr = (*(shell->solve)) (tao);CHKERRQ(ierr);
120*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
121*83a0a5c3SToby Isaac }
122*83a0a5c3SToby Isaac 
123*83a0a5c3SToby Isaac PetscErrorCode TaoDestroy_Shell(Tao tao)
124*83a0a5c3SToby Isaac {
125*83a0a5c3SToby Isaac   PetscErrorCode ierr;
126*83a0a5c3SToby Isaac 
127*83a0a5c3SToby Isaac   PetscFunctionBegin;
128*83a0a5c3SToby Isaac   ierr = PetscFree(tao->data);CHKERRQ(ierr);
129*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
130*83a0a5c3SToby Isaac }
131*83a0a5c3SToby Isaac 
132*83a0a5c3SToby Isaac PetscErrorCode TaoSetUp_Shell(Tao tao)
133*83a0a5c3SToby Isaac {
134*83a0a5c3SToby Isaac   PetscFunctionBegin;
135*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
136*83a0a5c3SToby Isaac }
137*83a0a5c3SToby Isaac 
138*83a0a5c3SToby Isaac PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
139*83a0a5c3SToby Isaac {
140*83a0a5c3SToby Isaac   PetscFunctionBegin;
141*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
142*83a0a5c3SToby Isaac }
143*83a0a5c3SToby Isaac 
144*83a0a5c3SToby Isaac PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
145*83a0a5c3SToby Isaac {
146*83a0a5c3SToby Isaac   PetscFunctionBegin;
147*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
148*83a0a5c3SToby Isaac }
149*83a0a5c3SToby Isaac 
150*83a0a5c3SToby Isaac /*MC
151*83a0a5c3SToby Isaac   TAOSHELL - a user provided nonlinear solver
152*83a0a5c3SToby Isaac 
153*83a0a5c3SToby Isaac    Level: advanced
154*83a0a5c3SToby Isaac 
155*83a0a5c3SToby Isaac .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types)
156*83a0a5c3SToby Isaac M*/
157*83a0a5c3SToby Isaac PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
158*83a0a5c3SToby Isaac {
159*83a0a5c3SToby Isaac   Tao_Shell      *shell;
160*83a0a5c3SToby Isaac   PetscErrorCode ierr;
161*83a0a5c3SToby Isaac 
162*83a0a5c3SToby Isaac   PetscFunctionBegin;
163*83a0a5c3SToby Isaac   tao->ops->destroy = TaoDestroy_Shell;
164*83a0a5c3SToby Isaac   tao->ops->setup = TaoSetUp_Shell;
165*83a0a5c3SToby Isaac   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
166*83a0a5c3SToby Isaac   tao->ops->view = TaoView_Shell;
167*83a0a5c3SToby Isaac   tao->ops->solve = TaoSolve_Shell;
168*83a0a5c3SToby Isaac 
169*83a0a5c3SToby Isaac   ierr = PetscNewLog(tao,&shell);CHKERRQ(ierr);
170*83a0a5c3SToby Isaac   tao->data = (void*)shell;
171*83a0a5c3SToby Isaac   PetscFunctionReturn(0);
172*83a0a5c3SToby Isaac }
173*83a0a5c3SToby Isaac 
174