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