xref: /petsc/src/tao/shell/taoshell.c (revision f4f49eeac7efa77fffa46b7ff95a3ed169f659ed)
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 . 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 @*/
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 
5220f4b53cSBarry Smith .seealso: `Tao`, `TAOSHELL`, `TaoCreateShell()`, `TaoShellSetContext()`
5383a0a5c3SToby Isaac @*/
54d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoShellGetContext(Tao tao, void *ctx)
55d71ae5a4SJacob Faibussowitsch {
5683a0a5c3SToby Isaac   PetscBool flg;
5783a0a5c3SToby Isaac 
5883a0a5c3SToby Isaac   PetscFunctionBegin;
5983a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
604f572ea9SToby Isaac   PetscAssertPointer(ctx, 2);
619566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
623ec1f749SStefano Zampini   if (!flg) *(void **)ctx = NULL;
63*f4f49eeaSPierre Jolivet   else *(void **)ctx = ((Tao_Shell *)tao->data)->ctx;
643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6583a0a5c3SToby Isaac }
6683a0a5c3SToby Isaac 
6783a0a5c3SToby Isaac /*@
6820f4b53cSBarry Smith   TaoShellSetContext - sets the context for a `TAOSHELL`
6983a0a5c3SToby Isaac 
70c3339decSBarry Smith   Logically Collective
7183a0a5c3SToby Isaac 
7283a0a5c3SToby Isaac   Input Parameters:
7383a0a5c3SToby Isaac + tao - the shell Tao
7483a0a5c3SToby Isaac - ctx - the context
7583a0a5c3SToby Isaac 
7683a0a5c3SToby Isaac   Level: advanced
7783a0a5c3SToby Isaac 
78e056e8ceSJacob Faibussowitsch   Fortran Notes:
7920f4b53cSBarry Smith   The context can only be an integer or a `PetscObject`
8083a0a5c3SToby Isaac 
8120f4b53cSBarry Smith .seealso: `Tao`, `TAOSHELL`, `TaoCreateShell()`, `TaoShellGetContext()`
8283a0a5c3SToby Isaac @*/
83d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoShellSetContext(Tao tao, void *ctx)
84d71ae5a4SJacob Faibussowitsch {
8583a0a5c3SToby Isaac   Tao_Shell *shell = (Tao_Shell *)tao->data;
8683a0a5c3SToby Isaac   PetscBool  flg;
8783a0a5c3SToby Isaac 
8883a0a5c3SToby Isaac   PetscFunctionBegin;
8983a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
909566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg));
9183a0a5c3SToby Isaac   if (flg) shell->ctx = ctx;
923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9383a0a5c3SToby Isaac }
9483a0a5c3SToby Isaac 
95d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSolve_Shell(Tao tao)
96d71ae5a4SJacob Faibussowitsch {
9783a0a5c3SToby Isaac   Tao_Shell *shell = (Tao_Shell *)tao->data;
9883a0a5c3SToby Isaac 
9983a0a5c3SToby Isaac   PetscFunctionBegin;
1003c859ba3SBarry Smith   PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first");
10183a0a5c3SToby Isaac   tao->reason = TAO_CONVERGED_USER;
102*f4f49eeaSPierre Jolivet   PetscCall((*shell->solve)(tao));
1033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10483a0a5c3SToby Isaac }
10583a0a5c3SToby Isaac 
10666976f2fSJacob Faibussowitsch static PetscErrorCode TaoDestroy_Shell(Tao tao)
107d71ae5a4SJacob Faibussowitsch {
10883a0a5c3SToby Isaac   PetscFunctionBegin;
1099566063dSJacob Faibussowitsch   PetscCall(PetscFree(tao->data));
1103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11183a0a5c3SToby Isaac }
11283a0a5c3SToby Isaac 
11366976f2fSJacob Faibussowitsch static PetscErrorCode TaoSetUp_Shell(Tao tao)
114d71ae5a4SJacob Faibussowitsch {
11583a0a5c3SToby Isaac   PetscFunctionBegin;
1163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11783a0a5c3SToby Isaac }
11883a0a5c3SToby Isaac 
11966976f2fSJacob Faibussowitsch static PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems *PetscOptionsObject)
120d71ae5a4SJacob Faibussowitsch {
12183a0a5c3SToby Isaac   PetscFunctionBegin;
1223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12383a0a5c3SToby Isaac }
12483a0a5c3SToby Isaac 
12566976f2fSJacob Faibussowitsch static PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
126d71ae5a4SJacob Faibussowitsch {
12783a0a5c3SToby Isaac   PetscFunctionBegin;
1283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12983a0a5c3SToby Isaac }
13083a0a5c3SToby Isaac 
13183a0a5c3SToby Isaac /*MC
13220f4b53cSBarry Smith   TAOSHELL - a user provided optimizer
13383a0a5c3SToby Isaac 
13483a0a5c3SToby Isaac    Level: advanced
13583a0a5c3SToby Isaac 
136db781477SPatrick Sanan .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
13783a0a5c3SToby Isaac M*/
138d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
139d71ae5a4SJacob Faibussowitsch {
14083a0a5c3SToby Isaac   Tao_Shell *shell;
14183a0a5c3SToby Isaac 
14283a0a5c3SToby Isaac   PetscFunctionBegin;
14383a0a5c3SToby Isaac   tao->ops->destroy        = TaoDestroy_Shell;
14483a0a5c3SToby Isaac   tao->ops->setup          = TaoSetUp_Shell;
14583a0a5c3SToby Isaac   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
14683a0a5c3SToby Isaac   tao->ops->view           = TaoView_Shell;
14783a0a5c3SToby Isaac   tao->ops->solve          = TaoSolve_Shell;
14883a0a5c3SToby Isaac 
1494dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&shell));
15083a0a5c3SToby Isaac   tao->data = (void *)shell;
1513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15283a0a5c3SToby Isaac }
153