183a0a5c3SToby Isaac #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/
283a0a5c3SToby Isaac
383a0a5c3SToby Isaac typedef struct _n_TaoShell Tao_Shell;
483a0a5c3SToby Isaac
59371c9d4SSatish Balay struct _n_TaoShell {
683a0a5c3SToby Isaac PetscErrorCode (*solve)(Tao);
7*2a8381b2SBarry Smith PetscCtx ctx;
883a0a5c3SToby Isaac };
983a0a5c3SToby Isaac
1083a0a5c3SToby Isaac /*@C
1183a0a5c3SToby Isaac TaoShellSetSolve - Sets routine to apply as solver
1283a0a5c3SToby Isaac
13c3339decSBarry Smith Logically Collective
1483a0a5c3SToby Isaac
1583a0a5c3SToby Isaac Input Parameters:
1683a0a5c3SToby Isaac + tao - the nonlinear solver context
1783a0a5c3SToby Isaac - solve - the application-provided solver routine
1883a0a5c3SToby Isaac
1920f4b53cSBarry Smith Calling sequence of `solve`:
2020f4b53cSBarry Smith . tao - the optimizer, get the application context with `TaoShellGetContext()`
2183a0a5c3SToby Isaac
2283a0a5c3SToby Isaac Level: advanced
2383a0a5c3SToby Isaac
2420f4b53cSBarry Smith .seealso: `Tao`, `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()`
2583a0a5c3SToby Isaac @*/
TaoShellSetSolve(Tao tao,PetscErrorCode (* solve)(Tao))26d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve)(Tao))
27d71ae5a4SJacob Faibussowitsch {
2883a0a5c3SToby Isaac Tao_Shell *shell = (Tao_Shell *)tao->data;
2983a0a5c3SToby Isaac
3083a0a5c3SToby Isaac PetscFunctionBegin;
3183a0a5c3SToby Isaac PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
3283a0a5c3SToby Isaac shell->solve = solve;
333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3483a0a5c3SToby Isaac }
3583a0a5c3SToby Isaac
3683a0a5c3SToby Isaac /*@
3720f4b53cSBarry Smith TaoShellGetContext - Returns the user-provided context associated with a `TAOSHELL`
3883a0a5c3SToby Isaac
3983a0a5c3SToby Isaac Not Collective
4083a0a5c3SToby Isaac
4183a0a5c3SToby Isaac Input Parameter:
4220f4b53cSBarry Smith . tao - should have been created with `TaoSetType`(tao,`TAOSHELL`);
4383a0a5c3SToby Isaac
4483a0a5c3SToby Isaac Output Parameter:
4583a0a5c3SToby Isaac . ctx - the user provided context
4683a0a5c3SToby Isaac
4783a0a5c3SToby Isaac Level: advanced
4883a0a5c3SToby Isaac
4920f4b53cSBarry Smith Note:
5083a0a5c3SToby Isaac This routine is intended for use within various shell routines
5183a0a5c3SToby Isaac
52*2a8381b2SBarry Smith Fortran Note:
53*2a8381b2SBarry Smith This only works when the context is a Fortran derived type or a `PetscObject`. Define `ctx` with
54*2a8381b2SBarry Smith .vb
55*2a8381b2SBarry Smith type(tUsertype), pointer :: ctx
56*2a8381b2SBarry Smith .ve
57*2a8381b2SBarry Smith
580241b274SPierre Jolivet .seealso: `Tao`, `TAOSHELL`, `TaoShellSetContext()`
5983a0a5c3SToby Isaac @*/
TaoShellGetContext(Tao tao,PetscCtxRt ctx)60*2a8381b2SBarry Smith PetscErrorCode TaoShellGetContext(Tao tao, PetscCtxRt ctx)
61d71ae5a4SJacob Faibussowitsch {
6283a0a5c3SToby Isaac PetscBool flg;
6383a0a5c3SToby Isaac
6483a0a5c3SToby Isaac PetscFunctionBegin;
6583a0a5c3SToby Isaac PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
664f572ea9SToby Isaac PetscAssertPointer(ctx, 2);
679566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
683ec1f749SStefano Zampini if (!flg) *(void **)ctx = NULL;
69f4f49eeaSPierre Jolivet else *(void **)ctx = ((Tao_Shell *)tao->data)->ctx;
703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
7183a0a5c3SToby Isaac }
7283a0a5c3SToby Isaac
7383a0a5c3SToby Isaac /*@
7420f4b53cSBarry Smith TaoShellSetContext - sets the context for a `TAOSHELL`
7583a0a5c3SToby Isaac
76c3339decSBarry Smith Logically Collective
7783a0a5c3SToby Isaac
7883a0a5c3SToby Isaac Input Parameters:
7983a0a5c3SToby Isaac + tao - the shell Tao
8083a0a5c3SToby Isaac - ctx - the context
8183a0a5c3SToby Isaac
8283a0a5c3SToby Isaac Level: advanced
8383a0a5c3SToby Isaac
840241b274SPierre Jolivet .seealso: `Tao`, `TAOSHELL`, `TaoShellGetContext()`
8583a0a5c3SToby Isaac @*/
TaoShellSetContext(Tao tao,PetscCtx ctx)86*2a8381b2SBarry Smith PetscErrorCode TaoShellSetContext(Tao tao, PetscCtx ctx)
87d71ae5a4SJacob Faibussowitsch {
8883a0a5c3SToby Isaac Tao_Shell *shell = (Tao_Shell *)tao->data;
8983a0a5c3SToby Isaac PetscBool flg;
9083a0a5c3SToby Isaac
9183a0a5c3SToby Isaac PetscFunctionBegin;
9283a0a5c3SToby Isaac PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
939566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
9483a0a5c3SToby Isaac if (flg) shell->ctx = ctx;
953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
9683a0a5c3SToby Isaac }
9783a0a5c3SToby Isaac
TaoSolve_Shell(Tao tao)98d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSolve_Shell(Tao tao)
99d71ae5a4SJacob Faibussowitsch {
10083a0a5c3SToby Isaac Tao_Shell *shell = (Tao_Shell *)tao->data;
10183a0a5c3SToby Isaac
10283a0a5c3SToby Isaac PetscFunctionBegin;
1033c859ba3SBarry Smith PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first");
10483a0a5c3SToby Isaac tao->reason = TAO_CONVERGED_USER;
105f4f49eeaSPierre Jolivet PetscCall((*shell->solve)(tao));
1063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
10783a0a5c3SToby Isaac }
10883a0a5c3SToby Isaac
TaoDestroy_Shell(Tao tao)10966976f2fSJacob Faibussowitsch static PetscErrorCode TaoDestroy_Shell(Tao tao)
110d71ae5a4SJacob Faibussowitsch {
11183a0a5c3SToby Isaac PetscFunctionBegin;
1129566063dSJacob Faibussowitsch PetscCall(PetscFree(tao->data));
1133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
11483a0a5c3SToby Isaac }
11583a0a5c3SToby Isaac
TaoSetUp_Shell(Tao tao)11666976f2fSJacob Faibussowitsch static PetscErrorCode TaoSetUp_Shell(Tao tao)
117d71ae5a4SJacob Faibussowitsch {
11883a0a5c3SToby Isaac PetscFunctionBegin;
1193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
12083a0a5c3SToby Isaac }
12183a0a5c3SToby Isaac
TaoSetFromOptions_Shell(Tao tao,PetscOptionItems PetscOptionsObject)122ce78bad3SBarry Smith static PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems PetscOptionsObject)
123d71ae5a4SJacob Faibussowitsch {
12483a0a5c3SToby Isaac PetscFunctionBegin;
1253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
12683a0a5c3SToby Isaac }
12783a0a5c3SToby Isaac
TaoView_Shell(Tao tao,PetscViewer viewer)12866976f2fSJacob Faibussowitsch static PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
129d71ae5a4SJacob Faibussowitsch {
13083a0a5c3SToby Isaac PetscFunctionBegin;
1313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
13283a0a5c3SToby Isaac }
13383a0a5c3SToby Isaac
13483a0a5c3SToby Isaac /*MC
13520f4b53cSBarry Smith TAOSHELL - a user provided optimizer
13683a0a5c3SToby Isaac
13783a0a5c3SToby Isaac Level: advanced
13883a0a5c3SToby Isaac
139db781477SPatrick Sanan .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
14083a0a5c3SToby Isaac M*/
TaoCreate_Shell(Tao tao)141d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
142d71ae5a4SJacob Faibussowitsch {
14383a0a5c3SToby Isaac Tao_Shell *shell;
14483a0a5c3SToby Isaac
14583a0a5c3SToby Isaac PetscFunctionBegin;
14683a0a5c3SToby Isaac tao->ops->destroy = TaoDestroy_Shell;
14783a0a5c3SToby Isaac tao->ops->setup = TaoSetUp_Shell;
14883a0a5c3SToby Isaac tao->ops->setfromoptions = TaoSetFromOptions_Shell;
14983a0a5c3SToby Isaac tao->ops->view = TaoView_Shell;
15083a0a5c3SToby Isaac tao->ops->solve = TaoSolve_Shell;
15183a0a5c3SToby Isaac
152606f75f6SBarry Smith PetscCall(TaoParametersInitialize(tao));
153606f75f6SBarry Smith
1544dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&shell));
15583a0a5c3SToby Isaac tao->data = (void *)shell;
1563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
15783a0a5c3SToby Isaac }
158