Actual source code: taoshell.c

  1: #include <petsc/private/taoimpl.h>

  3: typedef struct _n_TaoShell Tao_Shell;

  5: struct _n_TaoShell
  6: {
  7:   PetscErrorCode (*solve)(Tao);
  8:   void            *ctx;
  9: };

 11: /*@C
 12:    TaoShellSetSolve - Sets routine to apply as solver

 14:    Logically Collective on Tao

 16:    Input Parameters:
 17: +  tao - the nonlinear solver context
 18: -  solve - the application-provided solver routine

 20:    Calling sequence of solve:
 21: .vb
 22:    PetscErrorCode solve (Tao tao)
 23: .ve

 25: .  tao - the optimizer, get the application context with TaoShellGetContext()

 27:    Notes:
 28:     the function MUST return an error code of 0 on success and nonzero on failure.

 30:    Level: advanced

 32: .seealso: TAOSHELL, TaoShellSetContext(), TaoShellGetContext()
 33: @*/
 34: PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve) (Tao))
 35: {
 36:   Tao_Shell                    *shell = (Tao_Shell*)tao->data;

 39:   shell->solve = solve;
 40:   return 0;
 41: }

 43: /*@
 44:     TaoShellGetContext - Returns the user-provided context associated with a shell Tao

 46:     Not Collective

 48:     Input Parameter:
 49: .   tao - should have been created with TaoSetType(tao,TAOSHELL);

 51:     Output Parameter:
 52: .   ctx - the user provided context

 54:     Level: advanced

 56:     Notes:
 57:     This routine is intended for use within various shell routines

 59: .seealso: TaoCreateShell(), TaoShellSetContext()
 60: @*/
 61: PetscErrorCode  TaoShellGetContext(Tao tao,void *ctx)
 62: {
 63:   PetscBool      flg;

 67:   PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
 68:   if (!flg) *(void**)ctx = NULL;
 69:   else      *(void**)ctx = ((Tao_Shell*)(tao->data))->ctx;
 70:   return 0;
 71: }

 73: /*@
 74:     TaoShellSetContext - sets the context for a shell Tao

 76:    Logically Collective on Tao

 78:     Input Parameters:
 79: +   tao - the shell Tao
 80: -   ctx - the context

 82:    Level: advanced

 84:    Fortran Notes:
 85:     The context can only be an integer or a PetscObject
 86:       unfortunately it cannot be a Fortran array or derived type.

 88: .seealso: TaoCreateShell(), TaoShellGetContext()
 89: @*/
 90: PetscErrorCode  TaoShellSetContext(Tao tao,void *ctx)
 91: {
 92:   Tao_Shell     *shell = (Tao_Shell*)tao->data;
 93:   PetscBool      flg;

 96:   PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
 97:   if (flg) shell->ctx = ctx;
 98:   return 0;
 99: }

101: static PetscErrorCode TaoSolve_Shell(Tao tao)
102: {
103:   Tao_Shell                    *shell = (Tao_Shell*)tao->data;

106:   tao->reason = TAO_CONVERGED_USER;
107:   (*(shell->solve)) (tao);
108:   return 0;
109: }

111: PetscErrorCode TaoDestroy_Shell(Tao tao)
112: {
113:   PetscFree(tao->data);
114:   return 0;
115: }

117: PetscErrorCode TaoSetUp_Shell(Tao tao)
118: {
119:   return 0;
120: }

122: PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
123: {
124:   return 0;
125: }

127: PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
128: {
129:   return 0;
130: }

132: /*MC
133:   TAOSHELL - a user provided nonlinear solver

135:    Level: advanced

137: .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types)
138: M*/
139: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
140: {
141:   Tao_Shell      *shell;

143:   tao->ops->destroy = TaoDestroy_Shell;
144:   tao->ops->setup = TaoSetUp_Shell;
145:   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
146:   tao->ops->view = TaoView_Shell;
147:   tao->ops->solve = TaoSolve_Shell;

149:   PetscNewLog(tao,&shell);
150:   tao->data = (void*)shell;
151:   return 0;
152: }