Actual source code: taoshell.c
1: #include <petsc/private/taoimpl.h>
3: typedef struct _n_TaoShell Tao_Shell;
5: struct _n_TaoShell
6: {
7: PetscErrorCode (*solve)(Tao);
8: void *ctx;
9: };
11: /*@C
12: TaoShellSetSolve - Sets routine to apply as solver
14: Logically Collective on Tao
16: Input Parameters:
17: + tao - the nonlinear solver context
18: - solve - the application-provided solver routine
20: Calling sequence of solve:
21: .vb
22: PetscErrorCode solve (Tao tao)
23: .ve
25: . tao - the optimizer, get the application context with TaoShellGetContext()
27: Notes:
28: the function MUST return an error code of 0 on success and nonzero on failure.
30: Level: advanced
32: .seealso: TAOSHELL, TaoShellSetContext(), TaoShellGetContext()
33: @*/
34: PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve) (Tao))
35: {
36: Tao_Shell *shell = (Tao_Shell*)tao->data;
40: shell->solve = solve;
41: return(0);
42: }
44: /*@
45: TaoShellGetContext - Returns the user-provided context associated with a shell Tao
47: Not Collective
49: Input Parameter:
50: . tao - should have been created with TaoSetType(tao,TAOSHELL);
52: Output Parameter:
53: . ctx - the user provided context
55: Level: advanced
57: Notes:
58: This routine is intended for use within various shell routines
60: .seealso: TaoCreateShell(), TaoShellSetContext()
61: @*/
62: PetscErrorCode TaoShellGetContext(Tao tao,void *ctx)
63: {
65: PetscBool flg;
70: PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
71: if (!flg) *(void**)ctx = NULL;
72: else *(void**)ctx = ((Tao_Shell*)(tao->data))->ctx;
73: return(0);
74: }
76: /*@
77: TaoShellSetContext - sets the context for a shell Tao
79: Logically Collective on Tao
81: Input Parameters:
82: + tao - the shell Tao
83: - ctx - the context
85: Level: advanced
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.
91: .seealso: TaoCreateShell(), TaoShellGetContext()
92: @*/
93: PetscErrorCode TaoShellSetContext(Tao tao,void *ctx)
94: {
95: Tao_Shell *shell = (Tao_Shell*)tao->data;
97: PetscBool flg;
101: PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
102: if (flg) shell->ctx = ctx;
103: return(0);
104: }
106: static PetscErrorCode TaoSolve_Shell(Tao tao)
107: {
108: Tao_Shell *shell = (Tao_Shell*)tao->data;
109: PetscErrorCode ierr;
112: if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first");
113: tao->reason = TAO_CONVERGED_USER;
114: (*(shell->solve)) (tao);
115: return(0);
116: }
118: PetscErrorCode TaoDestroy_Shell(Tao tao)
119: {
123: PetscFree(tao->data);
124: return(0);
125: }
127: PetscErrorCode TaoSetUp_Shell(Tao tao)
128: {
130: return(0);
131: }
133: PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
134: {
136: return(0);
137: }
139: PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
140: {
142: return(0);
143: }
145: /*MC
146: TAOSHELL - a user provided nonlinear solver
148: Level: advanced
150: .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types)
151: M*/
152: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
153: {
154: Tao_Shell *shell;
158: tao->ops->destroy = TaoDestroy_Shell;
159: tao->ops->setup = TaoSetUp_Shell;
160: tao->ops->setfromoptions = TaoSetFromOptions_Shell;
161: tao->ops->view = TaoView_Shell;
162: tao->ops->solve = TaoSolve_Shell;
164: PetscNewLog(tao,&shell);
165: tao->data = (void*)shell;
166: return(0);
167: }