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: .vb
21: PetscErrorCode solve (Tao tao)
22: .ve
24: . tao - the optimizer, get the application context with TaoShellGetContext()
26: Notes:
27: the function MUST return an error code of 0 on success and nonzero on failure.
29: Level: advanced
31: .seealso: `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()`
32: @*/
33: PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve)(Tao))
34: {
35: Tao_Shell *shell = (Tao_Shell *)tao->data;
37: PetscFunctionBegin;
39: shell->solve = solve;
40: PetscFunctionReturn(PETSC_SUCCESS);
41: }
43: /*@
44: TaoShellGetContext - Returns the user-provided context associated with a shell Tao
46: Not Collective
48: Input Parameter:
49: . tao - should have been created with TaoSetType(tao,TAOSHELL);
51: Output Parameter:
52: . ctx - the user provided context
54: Level: advanced
56: Notes:
57: This routine is intended for use within various shell routines
59: .seealso: `TaoCreateShell()`, `TaoShellSetContext()`
60: @*/
61: PetscErrorCode TaoShellGetContext(Tao tao, void *ctx)
62: {
63: PetscBool flg;
65: PetscFunctionBegin;
68: PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
69: if (!flg) *(void **)ctx = NULL;
70: else *(void **)ctx = ((Tao_Shell *)(tao->data))->ctx;
71: PetscFunctionReturn(PETSC_SUCCESS);
72: }
74: /*@
75: TaoShellSetContext - sets the context for a shell Tao
77: Logically Collective
79: Input Parameters:
80: + tao - the shell Tao
81: - ctx - the context
83: Level: advanced
85: Fortran Notes:
86: The context can only be an integer or a PetscObject
87: unfortunately it cannot be a Fortran array or derived type.
89: .seealso: `TaoCreateShell()`, `TaoShellGetContext()`
90: @*/
91: PetscErrorCode TaoShellSetContext(Tao tao, void *ctx)
92: {
93: Tao_Shell *shell = (Tao_Shell *)tao->data;
94: PetscBool flg;
96: PetscFunctionBegin;
98: PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
99: if (flg) shell->ctx = ctx;
100: PetscFunctionReturn(PETSC_SUCCESS);
101: }
103: static PetscErrorCode TaoSolve_Shell(Tao tao)
104: {
105: Tao_Shell *shell = (Tao_Shell *)tao->data;
107: PetscFunctionBegin;
108: PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first");
109: tao->reason = TAO_CONVERGED_USER;
110: PetscCall((*(shell->solve))(tao));
111: PetscFunctionReturn(PETSC_SUCCESS);
112: }
114: PetscErrorCode TaoDestroy_Shell(Tao tao)
115: {
116: PetscFunctionBegin;
117: PetscCall(PetscFree(tao->data));
118: PetscFunctionReturn(PETSC_SUCCESS);
119: }
121: PetscErrorCode TaoSetUp_Shell(Tao tao)
122: {
123: PetscFunctionBegin;
124: PetscFunctionReturn(PETSC_SUCCESS);
125: }
127: PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems *PetscOptionsObject)
128: {
129: PetscFunctionBegin;
130: PetscFunctionReturn(PETSC_SUCCESS);
131: }
133: PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
134: {
135: PetscFunctionBegin;
136: PetscFunctionReturn(PETSC_SUCCESS);
137: }
139: /*MC
140: TAOSHELL - a user provided nonlinear solver
142: Level: advanced
144: .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
145: M*/
146: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
147: {
148: Tao_Shell *shell;
150: PetscFunctionBegin;
151: tao->ops->destroy = TaoDestroy_Shell;
152: tao->ops->setup = TaoSetUp_Shell;
153: tao->ops->setfromoptions = TaoSetFromOptions_Shell;
154: tao->ops->view = TaoView_Shell;
155: tao->ops->solve = TaoSolve_Shell;
157: PetscCall(PetscNew(&shell));
158: tao->data = (void *)shell;
159: PetscFunctionReturn(PETSC_SUCCESS);
160: }