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