xref: /petsc/src/tao/shell/taoshell.c (revision db7814771ca77b190574494e87b584e981451db0)
183a0a5c3SToby Isaac #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/
283a0a5c3SToby Isaac 
383a0a5c3SToby Isaac typedef struct _n_TaoShell Tao_Shell;
483a0a5c3SToby Isaac 
583a0a5c3SToby Isaac struct _n_TaoShell
683a0a5c3SToby Isaac {
783a0a5c3SToby Isaac   PetscErrorCode (*solve)(Tao);
883a0a5c3SToby Isaac   void            *ctx;
983a0a5c3SToby Isaac };
1083a0a5c3SToby Isaac 
1183a0a5c3SToby Isaac /*@C
1283a0a5c3SToby Isaac    TaoShellSetSolve - Sets routine to apply as solver
1383a0a5c3SToby Isaac 
1483a0a5c3SToby Isaac    Logically Collective on Tao
1583a0a5c3SToby Isaac 
1683a0a5c3SToby Isaac    Input Parameters:
1783a0a5c3SToby Isaac +  tao - the nonlinear solver context
1883a0a5c3SToby Isaac -  solve - the application-provided solver routine
1983a0a5c3SToby Isaac 
2083a0a5c3SToby Isaac    Calling sequence of solve:
2183a0a5c3SToby Isaac .vb
2283a0a5c3SToby Isaac    PetscErrorCode solve (Tao tao)
2383a0a5c3SToby Isaac .ve
2483a0a5c3SToby Isaac 
2583a0a5c3SToby Isaac .  tao - the optimizer, get the application context with TaoShellGetContext()
2683a0a5c3SToby Isaac 
2783a0a5c3SToby Isaac    Notes:
2883a0a5c3SToby Isaac     the function MUST return an error code of 0 on success and nonzero on failure.
2983a0a5c3SToby Isaac 
3083a0a5c3SToby Isaac    Level: advanced
3183a0a5c3SToby Isaac 
32*db781477SPatrick Sanan .seealso: `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()`
3383a0a5c3SToby Isaac @*/
3483a0a5c3SToby Isaac PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve) (Tao))
3583a0a5c3SToby Isaac {
3683a0a5c3SToby Isaac   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
3783a0a5c3SToby Isaac 
3883a0a5c3SToby Isaac   PetscFunctionBegin;
3983a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
4083a0a5c3SToby Isaac   shell->solve = solve;
4183a0a5c3SToby Isaac   PetscFunctionReturn(0);
4283a0a5c3SToby Isaac }
4383a0a5c3SToby Isaac 
4483a0a5c3SToby Isaac /*@
4583a0a5c3SToby Isaac     TaoShellGetContext - Returns the user-provided context associated with a shell Tao
4683a0a5c3SToby Isaac 
4783a0a5c3SToby Isaac     Not Collective
4883a0a5c3SToby Isaac 
4983a0a5c3SToby Isaac     Input Parameter:
5083a0a5c3SToby Isaac .   tao - should have been created with TaoSetType(tao,TAOSHELL);
5183a0a5c3SToby Isaac 
5283a0a5c3SToby Isaac     Output Parameter:
5383a0a5c3SToby Isaac .   ctx - the user provided context
5483a0a5c3SToby Isaac 
5583a0a5c3SToby Isaac     Level: advanced
5683a0a5c3SToby Isaac 
5783a0a5c3SToby Isaac     Notes:
5883a0a5c3SToby Isaac     This routine is intended for use within various shell routines
5983a0a5c3SToby Isaac 
60*db781477SPatrick Sanan .seealso: `TaoCreateShell()`, `TaoShellSetContext()`
6183a0a5c3SToby Isaac @*/
623ec1f749SStefano Zampini PetscErrorCode  TaoShellGetContext(Tao tao,void *ctx)
6383a0a5c3SToby Isaac {
6483a0a5c3SToby Isaac   PetscBool      flg;
6583a0a5c3SToby Isaac 
6683a0a5c3SToby Isaac   PetscFunctionBegin;
6783a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
6883a0a5c3SToby Isaac   PetscValidPointer(ctx,2);
699566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg));
703ec1f749SStefano Zampini   if (!flg) *(void**)ctx = NULL;
713ec1f749SStefano Zampini   else      *(void**)ctx = ((Tao_Shell*)(tao->data))->ctx;
7283a0a5c3SToby Isaac   PetscFunctionReturn(0);
7383a0a5c3SToby Isaac }
7483a0a5c3SToby Isaac 
7583a0a5c3SToby Isaac /*@
7683a0a5c3SToby Isaac     TaoShellSetContext - sets the context for a shell Tao
7783a0a5c3SToby Isaac 
7883a0a5c3SToby Isaac    Logically Collective on Tao
7983a0a5c3SToby Isaac 
8083a0a5c3SToby Isaac     Input Parameters:
8183a0a5c3SToby Isaac +   tao - the shell Tao
8283a0a5c3SToby Isaac -   ctx - the context
8383a0a5c3SToby Isaac 
8483a0a5c3SToby Isaac    Level: advanced
8583a0a5c3SToby Isaac 
8683a0a5c3SToby Isaac    Fortran Notes:
8783a0a5c3SToby Isaac     The context can only be an integer or a PetscObject
8883a0a5c3SToby Isaac       unfortunately it cannot be a Fortran array or derived type.
8983a0a5c3SToby Isaac 
90*db781477SPatrick Sanan .seealso: `TaoCreateShell()`, `TaoShellGetContext()`
9183a0a5c3SToby Isaac @*/
9283a0a5c3SToby Isaac PetscErrorCode  TaoShellSetContext(Tao tao,void *ctx)
9383a0a5c3SToby Isaac {
9483a0a5c3SToby Isaac   Tao_Shell     *shell = (Tao_Shell*)tao->data;
9583a0a5c3SToby Isaac   PetscBool      flg;
9683a0a5c3SToby Isaac 
9783a0a5c3SToby Isaac   PetscFunctionBegin;
9883a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
999566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg));
10083a0a5c3SToby Isaac   if (flg) shell->ctx = ctx;
10183a0a5c3SToby Isaac   PetscFunctionReturn(0);
10283a0a5c3SToby Isaac }
10383a0a5c3SToby Isaac 
10483a0a5c3SToby Isaac static PetscErrorCode TaoSolve_Shell(Tao tao)
10583a0a5c3SToby Isaac {
10683a0a5c3SToby Isaac   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
10783a0a5c3SToby Isaac 
10883a0a5c3SToby Isaac   PetscFunctionBegin;
1093c859ba3SBarry Smith   PetscCheck(shell->solve,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first");
11083a0a5c3SToby Isaac   tao->reason = TAO_CONVERGED_USER;
1119566063dSJacob Faibussowitsch   PetscCall((*(shell->solve)) (tao));
11283a0a5c3SToby Isaac   PetscFunctionReturn(0);
11383a0a5c3SToby Isaac }
11483a0a5c3SToby Isaac 
11583a0a5c3SToby Isaac PetscErrorCode TaoDestroy_Shell(Tao tao)
11683a0a5c3SToby Isaac {
11783a0a5c3SToby Isaac   PetscFunctionBegin;
1189566063dSJacob Faibussowitsch   PetscCall(PetscFree(tao->data));
11983a0a5c3SToby Isaac   PetscFunctionReturn(0);
12083a0a5c3SToby Isaac }
12183a0a5c3SToby Isaac 
12283a0a5c3SToby Isaac PetscErrorCode TaoSetUp_Shell(Tao tao)
12383a0a5c3SToby Isaac {
12483a0a5c3SToby Isaac   PetscFunctionBegin;
12583a0a5c3SToby Isaac   PetscFunctionReturn(0);
12683a0a5c3SToby Isaac }
12783a0a5c3SToby Isaac 
12883a0a5c3SToby Isaac PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
12983a0a5c3SToby Isaac {
13083a0a5c3SToby Isaac   PetscFunctionBegin;
13183a0a5c3SToby Isaac   PetscFunctionReturn(0);
13283a0a5c3SToby Isaac }
13383a0a5c3SToby Isaac 
13483a0a5c3SToby Isaac PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
13583a0a5c3SToby Isaac {
13683a0a5c3SToby Isaac   PetscFunctionBegin;
13783a0a5c3SToby Isaac   PetscFunctionReturn(0);
13883a0a5c3SToby Isaac }
13983a0a5c3SToby Isaac 
14083a0a5c3SToby Isaac /*MC
14183a0a5c3SToby Isaac   TAOSHELL - a user provided nonlinear solver
14283a0a5c3SToby Isaac 
14383a0a5c3SToby Isaac    Level: advanced
14483a0a5c3SToby Isaac 
145*db781477SPatrick Sanan .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
14683a0a5c3SToby Isaac M*/
14783a0a5c3SToby Isaac PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
14883a0a5c3SToby Isaac {
14983a0a5c3SToby Isaac   Tao_Shell      *shell;
15083a0a5c3SToby Isaac 
15183a0a5c3SToby Isaac   PetscFunctionBegin;
15283a0a5c3SToby Isaac   tao->ops->destroy = TaoDestroy_Shell;
15383a0a5c3SToby Isaac   tao->ops->setup = TaoSetUp_Shell;
15483a0a5c3SToby Isaac   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
15583a0a5c3SToby Isaac   tao->ops->view = TaoView_Shell;
15683a0a5c3SToby Isaac   tao->ops->solve = TaoSolve_Shell;
15783a0a5c3SToby Isaac 
1589566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(tao,&shell));
15983a0a5c3SToby Isaac   tao->data = (void*)shell;
16083a0a5c3SToby Isaac   PetscFunctionReturn(0);
16183a0a5c3SToby Isaac }
162