xref: /petsc/src/tao/shell/taoshell.c (revision 3c859ba3a04a72e8efcb87bc7ffc046a6cbab413)
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 
3283a0a5c3SToby Isaac .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 
6083a0a5c3SToby Isaac .seealso: TaoCreateShell(), TaoShellSetContext()
6183a0a5c3SToby Isaac @*/
623ec1f749SStefano Zampini PetscErrorCode  TaoShellGetContext(Tao tao,void *ctx)
6383a0a5c3SToby Isaac {
6483a0a5c3SToby Isaac   PetscErrorCode ierr;
6583a0a5c3SToby Isaac   PetscBool      flg;
6683a0a5c3SToby Isaac 
6783a0a5c3SToby Isaac   PetscFunctionBegin;
6883a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
6983a0a5c3SToby Isaac   PetscValidPointer(ctx,2);
7083a0a5c3SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);CHKERRQ(ierr);
713ec1f749SStefano Zampini   if (!flg) *(void**)ctx = NULL;
723ec1f749SStefano Zampini   else      *(void**)ctx = ((Tao_Shell*)(tao->data))->ctx;
7383a0a5c3SToby Isaac   PetscFunctionReturn(0);
7483a0a5c3SToby Isaac }
7583a0a5c3SToby Isaac 
7683a0a5c3SToby Isaac /*@
7783a0a5c3SToby Isaac     TaoShellSetContext - sets the context for a shell Tao
7883a0a5c3SToby Isaac 
7983a0a5c3SToby Isaac    Logically Collective on Tao
8083a0a5c3SToby Isaac 
8183a0a5c3SToby Isaac     Input Parameters:
8283a0a5c3SToby Isaac +   tao - the shell Tao
8383a0a5c3SToby Isaac -   ctx - the context
8483a0a5c3SToby Isaac 
8583a0a5c3SToby Isaac    Level: advanced
8683a0a5c3SToby Isaac 
8783a0a5c3SToby Isaac    Fortran Notes:
8883a0a5c3SToby Isaac     The context can only be an integer or a PetscObject
8983a0a5c3SToby Isaac       unfortunately it cannot be a Fortran array or derived type.
9083a0a5c3SToby Isaac 
9183a0a5c3SToby Isaac .seealso: TaoCreateShell(), TaoShellGetContext()
9283a0a5c3SToby Isaac @*/
9383a0a5c3SToby Isaac PetscErrorCode  TaoShellSetContext(Tao tao,void *ctx)
9483a0a5c3SToby Isaac {
9583a0a5c3SToby Isaac   Tao_Shell     *shell = (Tao_Shell*)tao->data;
9683a0a5c3SToby Isaac   PetscErrorCode ierr;
9783a0a5c3SToby Isaac   PetscBool      flg;
9883a0a5c3SToby Isaac 
9983a0a5c3SToby Isaac   PetscFunctionBegin;
10083a0a5c3SToby Isaac   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
10183a0a5c3SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);CHKERRQ(ierr);
10283a0a5c3SToby Isaac   if (flg) shell->ctx = ctx;
10383a0a5c3SToby Isaac   PetscFunctionReturn(0);
10483a0a5c3SToby Isaac }
10583a0a5c3SToby Isaac 
10683a0a5c3SToby Isaac static PetscErrorCode TaoSolve_Shell(Tao tao)
10783a0a5c3SToby Isaac {
10883a0a5c3SToby Isaac   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
10983a0a5c3SToby Isaac   PetscErrorCode               ierr;
11083a0a5c3SToby Isaac 
11183a0a5c3SToby Isaac   PetscFunctionBegin;
112*3c859ba3SBarry Smith   PetscCheck(shell->solve,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first");
11383a0a5c3SToby Isaac   tao->reason = TAO_CONVERGED_USER;
11483a0a5c3SToby Isaac   ierr = (*(shell->solve)) (tao);CHKERRQ(ierr);
11583a0a5c3SToby Isaac   PetscFunctionReturn(0);
11683a0a5c3SToby Isaac }
11783a0a5c3SToby Isaac 
11883a0a5c3SToby Isaac PetscErrorCode TaoDestroy_Shell(Tao tao)
11983a0a5c3SToby Isaac {
12083a0a5c3SToby Isaac   PetscErrorCode ierr;
12183a0a5c3SToby Isaac 
12283a0a5c3SToby Isaac   PetscFunctionBegin;
12383a0a5c3SToby Isaac   ierr = PetscFree(tao->data);CHKERRQ(ierr);
12483a0a5c3SToby Isaac   PetscFunctionReturn(0);
12583a0a5c3SToby Isaac }
12683a0a5c3SToby Isaac 
12783a0a5c3SToby Isaac PetscErrorCode TaoSetUp_Shell(Tao tao)
12883a0a5c3SToby Isaac {
12983a0a5c3SToby Isaac   PetscFunctionBegin;
13083a0a5c3SToby Isaac   PetscFunctionReturn(0);
13183a0a5c3SToby Isaac }
13283a0a5c3SToby Isaac 
13383a0a5c3SToby Isaac PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
13483a0a5c3SToby Isaac {
13583a0a5c3SToby Isaac   PetscFunctionBegin;
13683a0a5c3SToby Isaac   PetscFunctionReturn(0);
13783a0a5c3SToby Isaac }
13883a0a5c3SToby Isaac 
13983a0a5c3SToby Isaac PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
14083a0a5c3SToby Isaac {
14183a0a5c3SToby Isaac   PetscFunctionBegin;
14283a0a5c3SToby Isaac   PetscFunctionReturn(0);
14383a0a5c3SToby Isaac }
14483a0a5c3SToby Isaac 
14583a0a5c3SToby Isaac /*MC
14683a0a5c3SToby Isaac   TAOSHELL - a user provided nonlinear solver
14783a0a5c3SToby Isaac 
14883a0a5c3SToby Isaac    Level: advanced
14983a0a5c3SToby Isaac 
15083a0a5c3SToby Isaac .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types)
15183a0a5c3SToby Isaac M*/
15283a0a5c3SToby Isaac PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
15383a0a5c3SToby Isaac {
15483a0a5c3SToby Isaac   Tao_Shell      *shell;
15583a0a5c3SToby Isaac   PetscErrorCode ierr;
15683a0a5c3SToby Isaac 
15783a0a5c3SToby Isaac   PetscFunctionBegin;
15883a0a5c3SToby Isaac   tao->ops->destroy = TaoDestroy_Shell;
15983a0a5c3SToby Isaac   tao->ops->setup = TaoSetUp_Shell;
16083a0a5c3SToby Isaac   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
16183a0a5c3SToby Isaac   tao->ops->view = TaoView_Shell;
16283a0a5c3SToby Isaac   tao->ops->solve = TaoSolve_Shell;
16383a0a5c3SToby Isaac 
16483a0a5c3SToby Isaac   ierr = PetscNewLog(tao,&shell);CHKERRQ(ierr);
16583a0a5c3SToby Isaac   tao->data = (void*)shell;
16683a0a5c3SToby Isaac   PetscFunctionReturn(0);
16783a0a5c3SToby Isaac }
16883a0a5c3SToby Isaac 
169