Actual source code: snesimpl.h

  2: #ifndef __SNESIMPL_H

 5:  #include petscsnes.h

  7: struct _SNESOps {
  8:   PetscErrorCode (*computefunction)(SNES,Vec,Vec,void*);
  9:   PetscErrorCode (*computejacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
 10:   PetscErrorCode (*computescaling)(Vec,Vec,void*);
 11:   PetscErrorCode (*update)(SNES, PetscInt);                     /* General purpose function for update */
 12:   PetscErrorCode (*converged)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*);
 13:   PetscErrorCode (*setup)(SNES);             /* routine to set up the nonlinear solver */
 14:   PetscErrorCode (*solve)(SNES);             /* actual nonlinear solver */
 15:   PetscErrorCode (*view)(SNES,PetscViewer);
 16:   PetscErrorCode (*setfromoptions)(SNES);    /* sets options from database */
 17:   PetscErrorCode (*destroy)(SNES);
 18: };

 20: /*
 21:    Nonlinear solver context
 22:  */
 23: #define MAXSNESMONITORS 5

 25: struct _p_SNES {
 26:   PETSCHEADER(struct _SNESOps);

 28:   /*  ------------------------ User-provided stuff -------------------------------*/
 29:   void  *user;                                        /* user-defined context */

 31:   Vec   vec_sol,vec_sol_always;                 /* pointer to solution */
 32:   Vec   vec_sol_update_always;                  /* pointer to solution update */

 34:   Vec            vec_func,vec_func_always;               /* pointer to function */
 35:   Vec            afine;                                  /* If non-null solve F(x) = afine */
 36:   void           *funP;                                  /* user-defined function context */


 39:   Mat            jacobian;                               /* Jacobian matrix */
 40:   Mat            jacobian_pre;                           /* preconditioner matrix */
 41:   void           *jacP;                                  /* user-defined Jacobian context */
 42:   KSP            ksp;                                   /* linear solver context */

 44:   Vec            scaling;                                /* scaling vector */
 45:   void           *scaP;                                  /* scaling context */

 47:   /* ------------------------Time stepping hooks-----------------------------------*/

 49:   /* ---------------- PETSc-provided (or user-provided) stuff ---------------------*/

 51:   PetscErrorCode (*monitor[MAXSNESMONITORS])(SNES,PetscInt,PetscReal,void*); /* monitor routine */
 52:   PetscErrorCode (*monitordestroy[MAXSNESMONITORS])(void*);          /* monitor context destroy routine */
 53:   void           *monitorcontext[MAXSNESMONITORS];                   /* monitor context */
 54:   PetscInt       numbermonitors;                                     /* number of monitors */
 55:   void           *cnvP;                                                    /* convergence context */
 56:   SNESConvergedReason reason;

 58:   /* --- Routines and data that are unique to each particular solver --- */

 60:   PetscTruth     setupcalled;                /* true if setup has been called */
 61:   void           *data;                      /* implementation-specific data */

 63:   /* --------------------------  Parameters -------------------------------------- */

 65:   PetscInt    max_its;            /* max number of iterations */
 66:   PetscInt    max_funcs;          /* max number of function evals */
 67:   PetscInt    nfuncs;             /* number of function evaluations */
 68:   PetscInt    iter;               /* global iteration number */
 69:   PetscInt    linear_its;         /* total number of linear solver iterations */
 70:   PetscReal   norm;            /* residual norm of current iterate */
 71:   PetscReal   rtol;            /* relative tolerance */
 72:   PetscReal   abstol;            /* absolute tolerance */
 73:   PetscReal   xtol;            /* relative tolerance in solution */
 74:   PetscReal   deltatol;        /* trust region convergence tolerance */
 75:   PetscTruth  printreason;     /* print reason for convergence/divergence after each solve */
 76:   /* ------------------------ Default work-area management ---------------------- */

 78:   PetscInt    nwork;
 79:   Vec         *work;

 81:   /* ------------------------- Miscellaneous Information ------------------------ */

 83:   PetscReal   *conv_hist;         /* If !0, stores function norm (or
 84:                                     gradient norm) at each iteration */
 85:   PetscInt    *conv_hist_its;     /* linear iterations for each Newton step */
 86:   PetscInt    conv_hist_len;      /* size of convergence history array */
 87:   PetscInt    conv_hist_max;      /* actual amount of data in conv_history */
 88:   PetscTruth  conv_hist_reset;    /* reset counter for each new SNES solve */

 90:   /* the next two are used for failures in the line search; they should be put into the LS struct */
 91:   PetscInt    numFailures;        /* number of unsuccessful step attempts */
 92:   PetscInt    maxFailures;        /* maximum number of unsuccessful step attempts */

 94:   PetscInt    numLinearSolveFailures;
 95:   PetscInt    maxLinearSolveFailures;

 97:  /*
 98:    These are REALLY ugly and don't belong here, but since they must 
 99:   be destroyed at the conclusion we have to put them somewhere.
100:  */
101:   PetscTruth  ksp_ewconv;        /* flag indicating use of Eisenstat-Walker KSP convergence criteria */
102:   void        *kspconvctx;       /* KSP convergence context */

104:   PetscReal   ttol;           /* used by default convergence test routine */

106:   Vec             *vwork;            /* more work vectors for Jacobian approx */
107:   PetscInt        nvwork;
108: };

110: /* Context for Eisenstat-Walker convergence criteria for KSP solvers */
111: typedef struct {
112:   PetscInt  version;             /* flag indicating version 1 or 2 of test */
113:   PetscReal rtol_0;              /* initial rtol */
114:   PetscReal rtol_last;           /* last rtol */
115:   PetscReal rtol_max;            /* maximum rtol */
116:   PetscReal gamma;               /* mult. factor for version 2 rtol computation */
117:   PetscReal alpha;               /* power for version 2 rtol computation */
118:   PetscReal alpha2;              /* power for safeguard */
119:   PetscReal threshold;           /* threshold for imposing safeguard */
120:   PetscReal lresid_last;         /* linear residual from last iteration */
121:   PetscReal norm_last;           /* function norm from last iteration */
122: } SNES_KSP_EW_ConvCtx;

124: #define SNESLogConvHistory(snes,res,its) \
125:   { if (snes->conv_hist && snes->conv_hist_max > snes->conv_hist_len) \
126:     { if (snes->conv_hist)     snes->conv_hist[snes->conv_hist_len]     = res; \
127:       if (snes->conv_hist_its) snes->conv_hist_its[snes->conv_hist_len] = its; \
128:       snes->conv_hist_len++;\
129:     }}

131: #define SNESMonitor(snes,it,rnorm) \
132:         { PetscErrorCode _ierr; PetscInt _i,_im = snes->numbermonitors; \
133:           for (_i=0; _i<_im; _i++) {\
134:             _(*snes->monitor[_i])(snes,it,rnorm,snes->monitorcontext[_i]);CHKERRQ(_ierr); \
135:           } \
136:         }

138: PetscErrorCode SNES_KSP_EW_Converged_Private(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*);
139: PetscErrorCode SNES_KSP_EW_ComputeRelativeTolerance_Private(SNES,KSP);
140: PetscErrorCode SNESScaleStep_Private(SNES,Vec,PetscReal*,PetscReal*,PetscReal*,PetscReal*);


145: #endif