Actual source code: taoshell.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  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: .keywords: Tao, shell, set, user-provided

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

 42:   shell->solve = solve;
 43:   return(0);
 44: }

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

 49:     Not Collective

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

 54:     Output Parameter:
 55: .   ctx - the user provided context

 57:     Level: advanced

 59:     Notes:
 60:     This routine is intended for use within various shell routines

 62: .keywords: Tao, shell, get, context

 64: .seealso: TaoCreateShell(), TaoShellSetContext()
 65: @*/
 66: PetscErrorCode  TaoShellGetContext(Tao tao,void **ctx)
 67: {
 69:   PetscBool      flg;

 74:   PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
 75:   if (!flg) *ctx = 0;
 76:   else      *ctx = ((Tao_Shell*)(tao->data))->ctx;
 77:   return(0);
 78: }

 80: /*@
 81:     TaoShellSetContext - sets the context for a shell Tao

 83:    Logically Collective on Tao

 85:     Input Parameters:
 86: +   tao - the shell Tao
 87: -   ctx - the context

 89:    Level: advanced

 91:    Fortran Notes:
 92:     The context can only be an integer or a PetscObject
 93:       unfortunately it cannot be a Fortran array or derived type.


 96: .seealso: TaoCreateShell(), TaoShellGetContext()
 97: @*/
 98: PetscErrorCode  TaoShellSetContext(Tao tao,void *ctx)
 99: {
100:   Tao_Shell     *shell = (Tao_Shell*)tao->data;
102:   PetscBool      flg;

106:   PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
107:   if (flg) shell->ctx = ctx;
108:   return(0);
109: }

111: static PetscErrorCode TaoSolve_Shell(Tao tao)
112: {
113:   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
114:   PetscErrorCode               ierr;

117:   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first");
118:   tao->reason = TAO_CONVERGED_USER;
119:   (*(shell->solve)) (tao);
120:   return(0);
121: }

123: PetscErrorCode TaoDestroy_Shell(Tao tao)
124: {

128:   PetscFree(tao->data);
129:   return(0);
130: }

132: PetscErrorCode TaoSetUp_Shell(Tao tao)
133: {
135:   return(0);
136: }

138: PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
139: {
141:   return(0);
142: }

144: PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
145: {
147:   return(0);
148: }

150: /*MC
151:   TAOSHELL - a user provided nonlinear solver

153:    Level: advanced

155: .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types)
156: M*/
157: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
158: {
159:   Tao_Shell      *shell;

163:   tao->ops->destroy = TaoDestroy_Shell;
164:   tao->ops->setup = TaoSetUp_Shell;
165:   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
166:   tao->ops->view = TaoView_Shell;
167:   tao->ops->solve = TaoSolve_Shell;

169:   PetscNewLog(tao,&shell);
170:   tao->data = (void*)shell;
171:   return(0);
172: }