Actual source code: taoshell.c
1: #include <petsc/private/taoimpl.h>
3: typedef struct _n_TaoShell Tao_Shell;
5: struct _n_TaoShell {
6: PetscErrorCode (*solve)(Tao);
7: void *ctx;
8: };
10: /*@C
11: TaoShellSetSolve - Sets routine to apply as solver
13: Logically Collective
15: Input Parameters:
16: + tao - the nonlinear solver context
17: - solve - the application-provided solver routine
19: Calling sequence of `solve`:
20: $ PetscErrorCode solve(Tao tao)
21: . tao - the optimizer, get the application context with `TaoShellGetContext()`
23: Level: advanced
25: .seealso: `Tao`, `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()`
26: @*/
27: PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve)(Tao))
28: {
29: Tao_Shell *shell = (Tao_Shell *)tao->data;
31: PetscFunctionBegin;
33: shell->solve = solve;
34: PetscFunctionReturn(PETSC_SUCCESS);
35: }
37: /*@
38: TaoShellGetContext - Returns the user-provided context associated with a `TAOSHELL`
40: Not Collective
42: Input Parameter:
43: . tao - should have been created with `TaoSetType`(tao,`TAOSHELL`);
45: Output Parameter:
46: . ctx - the user provided context
48: Level: advanced
50: Note:
51: This routine is intended for use within various shell routines
53: .seealso: `Tao`, `TAOSHELL`, `TaoCreateShell()`, `TaoShellSetContext()`
54: @*/
55: PetscErrorCode TaoShellGetContext(Tao tao, void *ctx)
56: {
57: PetscBool flg;
59: PetscFunctionBegin;
62: PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
63: if (!flg) *(void **)ctx = NULL;
64: else *(void **)ctx = ((Tao_Shell *)(tao->data))->ctx;
65: PetscFunctionReturn(PETSC_SUCCESS);
66: }
68: /*@
69: TaoShellSetContext - sets the context for a `TAOSHELL`
71: Logically Collective
73: Input Parameters:
74: + tao - the shell Tao
75: - ctx - the context
77: Level: advanced
79: Fortran Note:
80: The context can only be an integer or a `PetscObject`
82: .seealso: `Tao`, `TAOSHELL`, `TaoCreateShell()`, `TaoShellGetContext()`
83: @*/
84: PetscErrorCode TaoShellSetContext(Tao tao, void *ctx)
85: {
86: Tao_Shell *shell = (Tao_Shell *)tao->data;
87: PetscBool flg;
89: PetscFunctionBegin;
91: PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
92: if (flg) shell->ctx = ctx;
93: PetscFunctionReturn(PETSC_SUCCESS);
94: }
96: static PetscErrorCode TaoSolve_Shell(Tao tao)
97: {
98: Tao_Shell *shell = (Tao_Shell *)tao->data;
100: PetscFunctionBegin;
101: PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first");
102: tao->reason = TAO_CONVERGED_USER;
103: PetscCall((*(shell->solve))(tao));
104: PetscFunctionReturn(PETSC_SUCCESS);
105: }
107: PetscErrorCode TaoDestroy_Shell(Tao tao)
108: {
109: PetscFunctionBegin;
110: PetscCall(PetscFree(tao->data));
111: PetscFunctionReturn(PETSC_SUCCESS);
112: }
114: PetscErrorCode TaoSetUp_Shell(Tao tao)
115: {
116: PetscFunctionBegin;
117: PetscFunctionReturn(PETSC_SUCCESS);
118: }
120: PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems *PetscOptionsObject)
121: {
122: PetscFunctionBegin;
123: PetscFunctionReturn(PETSC_SUCCESS);
124: }
126: PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
127: {
128: PetscFunctionBegin;
129: PetscFunctionReturn(PETSC_SUCCESS);
130: }
132: /*MC
133: TAOSHELL - a user provided optimizer
135: Level: advanced
137: .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
138: M*/
139: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
140: {
141: Tao_Shell *shell;
143: PetscFunctionBegin;
144: tao->ops->destroy = TaoDestroy_Shell;
145: tao->ops->setup = TaoSetUp_Shell;
146: tao->ops->setfromoptions = TaoSetFromOptions_Shell;
147: tao->ops->view = TaoView_Shell;
148: tao->ops->solve = TaoSolve_Shell;
150: PetscCall(PetscNew(&shell));
151: tao->data = (void *)shell;
152: PetscFunctionReturn(PETSC_SUCCESS);
153: }