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: . tao - the optimizer, get the application context with `TaoShellGetContext()`
22: Level: advanced
24: .seealso: `Tao`, `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()`
25: @*/
26: PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve)(Tao))
27: {
28: Tao_Shell *shell = (Tao_Shell *)tao->data;
30: PetscFunctionBegin;
32: shell->solve = solve;
33: PetscFunctionReturn(PETSC_SUCCESS);
34: }
36: /*@
37: TaoShellGetContext - Returns the user-provided context associated with a `TAOSHELL`
39: Not Collective
41: Input Parameter:
42: . tao - should have been created with `TaoSetType`(tao,`TAOSHELL`);
44: Output Parameter:
45: . ctx - the user provided context
47: Level: advanced
49: Note:
50: This routine is intended for use within various shell routines
52: .seealso: `Tao`, `TAOSHELL`, `TaoShellSetContext()`
53: @*/
54: PetscErrorCode TaoShellGetContext(Tao tao, void *ctx)
55: {
56: PetscBool flg;
58: PetscFunctionBegin;
60: PetscAssertPointer(ctx, 2);
61: PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
62: if (!flg) *(void **)ctx = NULL;
63: else *(void **)ctx = ((Tao_Shell *)tao->data)->ctx;
64: PetscFunctionReturn(PETSC_SUCCESS);
65: }
67: /*@
68: TaoShellSetContext - sets the context for a `TAOSHELL`
70: Logically Collective
72: Input Parameters:
73: + tao - the shell Tao
74: - ctx - the context
76: Level: advanced
78: .seealso: `Tao`, `TAOSHELL`, `TaoShellGetContext()`
79: @*/
80: PetscErrorCode TaoShellSetContext(Tao tao, void *ctx)
81: {
82: Tao_Shell *shell = (Tao_Shell *)tao->data;
83: PetscBool flg;
85: PetscFunctionBegin;
87: PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
88: if (flg) shell->ctx = ctx;
89: PetscFunctionReturn(PETSC_SUCCESS);
90: }
92: static PetscErrorCode TaoSolve_Shell(Tao tao)
93: {
94: Tao_Shell *shell = (Tao_Shell *)tao->data;
96: PetscFunctionBegin;
97: PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first");
98: tao->reason = TAO_CONVERGED_USER;
99: PetscCall((*shell->solve)(tao));
100: PetscFunctionReturn(PETSC_SUCCESS);
101: }
103: static PetscErrorCode TaoDestroy_Shell(Tao tao)
104: {
105: PetscFunctionBegin;
106: PetscCall(PetscFree(tao->data));
107: PetscFunctionReturn(PETSC_SUCCESS);
108: }
110: static PetscErrorCode TaoSetUp_Shell(Tao tao)
111: {
112: PetscFunctionBegin;
113: PetscFunctionReturn(PETSC_SUCCESS);
114: }
116: static PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems PetscOptionsObject)
117: {
118: PetscFunctionBegin;
119: PetscFunctionReturn(PETSC_SUCCESS);
120: }
122: static PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
123: {
124: PetscFunctionBegin;
125: PetscFunctionReturn(PETSC_SUCCESS);
126: }
128: /*MC
129: TAOSHELL - a user provided optimizer
131: Level: advanced
133: .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
134: M*/
135: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
136: {
137: Tao_Shell *shell;
139: PetscFunctionBegin;
140: tao->ops->destroy = TaoDestroy_Shell;
141: tao->ops->setup = TaoSetUp_Shell;
142: tao->ops->setfromoptions = TaoSetFromOptions_Shell;
143: tao->ops->view = TaoView_Shell;
144: tao->ops->solve = TaoSolve_Shell;
146: PetscCall(TaoParametersInitialize(tao));
148: PetscCall(PetscNew(&shell));
149: tao->data = (void *)shell;
150: PetscFunctionReturn(PETSC_SUCCESS);
151: }