Actual source code: taoshell.c

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

  3: typedef struct _n_TaoShell Tao_Shell;

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

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

 13:    Logically Collective

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

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

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

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

 29:    Level: advanced

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

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

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

 45:     Not Collective

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

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

 53:     Level: advanced

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

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

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

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

 75:    Logically Collective

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

 81:    Level: advanced

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

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

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

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

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

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

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

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

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

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

134:    Level: advanced

136: .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType`
137: M*/
138: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
139: {
140:   Tao_Shell *shell;

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

148:   PetscNew(&shell);
149:   tao->data = (void *)shell;
150:   return 0;
151: }