xref: /petsc/src/tao/shell/taoshell.c (revision e056e8ce1cfdaf3750afb193bfd9d5df747ade21)
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);
783a0a5c3SToby Isaac   void *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 $   PetscErrorCode solve(Tao tao)
2120f4b53cSBarry Smith . tao - the optimizer, get the application context with `TaoShellGetContext()`
2283a0a5c3SToby Isaac 
2383a0a5c3SToby Isaac   Level: advanced
2483a0a5c3SToby Isaac 
2520f4b53cSBarry Smith .seealso: `Tao`, `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()`
2683a0a5c3SToby Isaac @*/
27d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve)(Tao))
28d71ae5a4SJacob Faibussowitsch {
2983a0a5c3SToby Isaac   Tao_Shell *shell = (Tao_Shell *)tao->data;
3083a0a5c3SToby Isaac 
3183a0a5c3SToby Isaac   PetscFunctionBegin;
3283a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
3383a0a5c3SToby Isaac   shell->solve = solve;
343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3583a0a5c3SToby Isaac }
3683a0a5c3SToby Isaac 
3783a0a5c3SToby Isaac /*@
3820f4b53cSBarry Smith   TaoShellGetContext - Returns the user-provided context associated with a `TAOSHELL`
3983a0a5c3SToby Isaac 
4083a0a5c3SToby Isaac   Not Collective
4183a0a5c3SToby Isaac 
4283a0a5c3SToby Isaac   Input Parameter:
4320f4b53cSBarry Smith . tao - should have been created with `TaoSetType`(tao,`TAOSHELL`);
4483a0a5c3SToby Isaac 
4583a0a5c3SToby Isaac   Output Parameter:
4683a0a5c3SToby Isaac . ctx - the user provided context
4783a0a5c3SToby Isaac 
4883a0a5c3SToby Isaac   Level: advanced
4983a0a5c3SToby Isaac 
5020f4b53cSBarry Smith   Note:
5183a0a5c3SToby Isaac   This routine is intended for use within various shell routines
5283a0a5c3SToby Isaac 
5320f4b53cSBarry Smith .seealso: `Tao`, `TAOSHELL`, `TaoCreateShell()`, `TaoShellSetContext()`
5483a0a5c3SToby Isaac @*/
55d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoShellGetContext(Tao tao, void *ctx)
56d71ae5a4SJacob Faibussowitsch {
5783a0a5c3SToby Isaac   PetscBool flg;
5883a0a5c3SToby Isaac 
5983a0a5c3SToby Isaac   PetscFunctionBegin;
6083a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
6183a0a5c3SToby Isaac   PetscValidPointer(ctx, 2);
629566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
633ec1f749SStefano Zampini   if (!flg) *(void **)ctx = NULL;
643ec1f749SStefano Zampini   else *(void **)ctx = ((Tao_Shell *)(tao->data))->ctx;
653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6683a0a5c3SToby Isaac }
6783a0a5c3SToby Isaac 
6883a0a5c3SToby Isaac /*@
6920f4b53cSBarry Smith   TaoShellSetContext - sets the context for a `TAOSHELL`
7083a0a5c3SToby Isaac 
71c3339decSBarry Smith   Logically Collective
7283a0a5c3SToby Isaac 
7383a0a5c3SToby Isaac   Input Parameters:
7483a0a5c3SToby Isaac + tao - the shell Tao
7583a0a5c3SToby Isaac - ctx - the context
7683a0a5c3SToby Isaac 
7783a0a5c3SToby Isaac   Level: advanced
7883a0a5c3SToby Isaac 
79*e056e8ceSJacob Faibussowitsch   Fortran Notes:
8020f4b53cSBarry Smith   The context can only be an integer or a `PetscObject`
8183a0a5c3SToby Isaac 
8220f4b53cSBarry Smith .seealso: `Tao`, `TAOSHELL`, `TaoCreateShell()`, `TaoShellGetContext()`
8383a0a5c3SToby Isaac @*/
84d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoShellSetContext(Tao tao, void *ctx)
85d71ae5a4SJacob Faibussowitsch {
8683a0a5c3SToby Isaac   Tao_Shell *shell = (Tao_Shell *)tao->data;
8783a0a5c3SToby Isaac   PetscBool  flg;
8883a0a5c3SToby Isaac 
8983a0a5c3SToby Isaac   PetscFunctionBegin;
9083a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
919566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
9283a0a5c3SToby Isaac   if (flg) shell->ctx = ctx;
933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9483a0a5c3SToby Isaac }
9583a0a5c3SToby Isaac 
96d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSolve_Shell(Tao tao)
97d71ae5a4SJacob Faibussowitsch {
9883a0a5c3SToby Isaac   Tao_Shell *shell = (Tao_Shell *)tao->data;
9983a0a5c3SToby Isaac 
10083a0a5c3SToby Isaac   PetscFunctionBegin;
1013c859ba3SBarry Smith   PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first");
10283a0a5c3SToby Isaac   tao->reason = TAO_CONVERGED_USER;
1039566063dSJacob Faibussowitsch   PetscCall((*(shell->solve))(tao));
1043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10583a0a5c3SToby Isaac }
10683a0a5c3SToby Isaac 
107d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDestroy_Shell(Tao tao)
108d71ae5a4SJacob Faibussowitsch {
10983a0a5c3SToby Isaac   PetscFunctionBegin;
1109566063dSJacob Faibussowitsch   PetscCall(PetscFree(tao->data));
1113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11283a0a5c3SToby Isaac }
11383a0a5c3SToby Isaac 
114d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUp_Shell(Tao tao)
115d71ae5a4SJacob Faibussowitsch {
11683a0a5c3SToby Isaac   PetscFunctionBegin;
1173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11883a0a5c3SToby Isaac }
11983a0a5c3SToby Isaac 
120d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems *PetscOptionsObject)
121d71ae5a4SJacob Faibussowitsch {
12283a0a5c3SToby Isaac   PetscFunctionBegin;
1233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12483a0a5c3SToby Isaac }
12583a0a5c3SToby Isaac 
126d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
127d71ae5a4SJacob Faibussowitsch {
12883a0a5c3SToby Isaac   PetscFunctionBegin;
1293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13083a0a5c3SToby Isaac }
13183a0a5c3SToby Isaac 
13283a0a5c3SToby Isaac /*MC
13320f4b53cSBarry Smith   TAOSHELL - a user provided optimizer
13483a0a5c3SToby Isaac 
13583a0a5c3SToby Isaac    Level: advanced
13683a0a5c3SToby Isaac 
137db781477SPatrick Sanan .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
13883a0a5c3SToby Isaac M*/
139d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
140d71ae5a4SJacob Faibussowitsch {
14183a0a5c3SToby Isaac   Tao_Shell *shell;
14283a0a5c3SToby Isaac 
14383a0a5c3SToby Isaac   PetscFunctionBegin;
14483a0a5c3SToby Isaac   tao->ops->destroy        = TaoDestroy_Shell;
14583a0a5c3SToby Isaac   tao->ops->setup          = TaoSetUp_Shell;
14683a0a5c3SToby Isaac   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
14783a0a5c3SToby Isaac   tao->ops->view           = TaoView_Shell;
14883a0a5c3SToby Isaac   tao->ops->solve          = TaoSolve_Shell;
14983a0a5c3SToby Isaac 
1504dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&shell));
15183a0a5c3SToby Isaac   tao->data = (void *)shell;
1523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15383a0a5c3SToby Isaac }
154