Actual source code: taoshell.c
petsc-3.13.6 2020-09-29
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 Section 1.5 Writing Application Codes with PETSc-provided solver routine

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

     25: .  tao - the optimizer, get the Section 1.5 Writing Application Codes with PETSc 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;

     40:   shell->solve = solve;
     41:   return(0);
     42: }

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

     47:     Not Collective

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

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

     55:     Level: advanced

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

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

     70:   PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
     71:   if (!flg) *ctx = 0;
     72:   else      *ctx = ((Tao_Shell*)(tao->data))->ctx;
     73:   return(0);
     74: }

     76: /*@
     77:     TaoShellSetContext - sets the context for a shell Tao

     79:    Logically Collective on Tao

     81:     Input Parameters:
     82: +   tao - the shell Tao
     83: -   ctx - the context

     85:    Level: advanced

     87:    Fortran Notes:
     88:     The context can only be an integer or a PetscObject
     89:       unfortunately it cannot be a Fortran array or derived type.

     92: .seealso: TaoCreateShell(), TaoShellGetContext()
     93: @*/
     94: PetscErrorCode  TaoShellSetContext(Tao tao,void *ctx)
     95: {
     96:   Tao_Shell     *shell = (Tao_Shell*)tao->data;
     98:   PetscBool      flg;

    102:   PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);
    103:   if (flg) shell->ctx = ctx;
    104:   return(0);
    105: }

    107: static PetscErrorCode TaoSolve_Shell(Tao tao)
    108: {
    109:   Tao_Shell                    *shell = (Tao_Shell*)tao->data;
    110:   PetscErrorCode               ierr;

    113:   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first");
    114:   tao->reason = TAO_CONVERGED_USER;
    115:   (*(shell->solve)) (tao);
    116:   return(0);
    117: }

    119: PetscErrorCode TaoDestroy_Shell(Tao tao)
    120: {

    124:   PetscFree(tao->data);
    125:   return(0);
    126: }

    128: PetscErrorCode TaoSetUp_Shell(Tao tao)
    129: {
    131:   return(0);
    132: }

    134: PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao)
    135: {
    137:   return(0);
    138: }

    140: PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer)
    141: {
    143:   return(0);
    144: }

    146: /*MC
    147:   TAOSHELL - a user provided nonlinear solver

    149:    Level: advanced

    151: .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types)
    152: M*/
    153: PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao)
    154: {
    155:   Tao_Shell      *shell;

    159:   tao->ops->destroy = TaoDestroy_Shell;
    160:   tao->ops->setup = TaoSetUp_Shell;
    161:   tao->ops->setfromoptions = TaoSetFromOptions_Shell;
    162:   tao->ops->view = TaoView_Shell;
    163:   tao->ops->solve = TaoSolve_Shell;

    165:   PetscNewLog(tao,&shell);
    166:   tao->data = (void*)shell;
    167:   return(0);
    168: }