Actual source code: snesimpl.h

  2: #ifndef __SNESIMPL_H

  5: #include <petscsnes.h>

  7: typedef struct _SNESOps *SNESOps;

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

 27: /*
 28:    Nonlinear solver context
 29:  */
 30: #define MAXSNESMONITORS 5

 32: struct _p_SNES {
 33:   PETSCHEADER(struct _SNESOps);
 34:   DM   dm;
 35:   SNES pc;

 37:   /*  ------------------------ User-provided stuff -------------------------------*/
 38:   void  *user;                          /* user-defined context */

 40:   Vec  vec_rhs;                  /* If non-null, solve F(x) = rhs */
 41:   Vec  vec_sol;                  /* pointer to solution */

 43:   Vec  vec_func;                 /* pointer to function */
 44:   void *funP;                    /* user-defined function context */

 46:   Mat  jacobian;                 /* Jacobian matrix */
 47:   Mat  jacobian_pre;             /* preconditioner matrix */
 48:   void *jacP;                    /* user-defined Jacobian context */
 49:   void *initialguessP;           /* user-defined initial guess context */
 50:   KSP  ksp;                      /* linear solver context */

 52:   Vec  vec_sol_update;           /* pointer to solution update */

 54:   Vec  scaling;                  /* scaling vector */
 55:   void *scaP;                    /* scaling context */

 57:   /* ------------------------Time stepping hooks-----------------------------------*/

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

 61:   PetscErrorCode      (*monitor[MAXSNESMONITORS])(SNES,PetscInt,PetscReal,void*); /* monitor routine */
 62:   PetscErrorCode      (*monitordestroy[MAXSNESMONITORS])(void**);          /* monitor context destroy routine */
 63:   void                *monitorcontext[MAXSNESMONITORS];                   /* monitor context */
 64:   PetscInt            numbermonitors;                                     /* number of monitors */
 65:   void                *cnvP;                                                    /* convergence context */
 66:   SNESConvergedReason reason;
 67:   PetscBool           errorifnotconverged;

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

 71:   PetscBool      setupcalled;                /* true if setup has been called */
 72:   void           *data;                      /* implementation-specific data */

 74:   /* --------------------------  Parameters -------------------------------------- */

 76:   PetscInt    max_its;            /* max number of iterations */
 77:   PetscInt    max_funcs;          /* max number of function evals */
 78:   PetscInt    nfuncs;             /* number of function evaluations */
 79:   PetscInt    iter;               /* global iteration number */
 80:   PetscInt    linear_its;         /* total number of linear solver iterations */
 81:   PetscReal   norm;            /* residual norm of current iterate */
 82:   PetscReal   rtol;            /* relative tolerance */
 83:   PetscReal   abstol;            /* absolute tolerance */
 84:   PetscReal   xtol;            /* relative tolerance in solution */
 85:   PetscReal   deltatol;        /* trust region convergence tolerance */
 86:   PetscBool   printreason;     /* print reason for convergence/divergence after each solve */
 87:   PetscInt    lagpreconditioner; /* SNESSetLagPreconditioner() */
 88:   PetscInt    lagjacobian;       /* SNESSetLagJacobian() */
 89:   PetscInt    gridsequence;      /* number of grid sequence steps to take; defaults to zero */
 90:   /* ------------------------ Default work-area management ---------------------- */

 92:   PetscInt    nwork;
 93:   Vec         *work;

 95:   /* ------------------------- Miscellaneous Information ------------------------ */

 97:   PetscReal   *conv_hist;         /* If !0, stores function norm (or
 98:                                     gradient norm) at each iteration */
 99:   PetscInt    *conv_hist_its;     /* linear iterations for each Newton step */
100:   PetscInt    conv_hist_len;      /* size of convergence history array */
101:   PetscInt    conv_hist_max;      /* actual amount of data in conv_history */
102:   PetscBool   conv_hist_reset;    /* reset counter for each new SNES solve */
103:   PetscBool   conv_malloc;

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

109:   PetscInt    numLinearSolveFailures;
110:   PetscInt    maxLinearSolveFailures;

112:   PetscBool   domainerror;       /* set with SNESSetFunctionDomainError() */

114:   PetscBool   ksp_ewconv;        /* flag indicating use of Eisenstat-Walker KSP convergence criteria */
115:   void        *kspconvctx;       /* Eisenstat-Walker KSP convergence context */

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

119:   Vec         *vwork;            /* more work vectors for Jacobian approx */
120:   PetscInt    nvwork;

122:   PetscBool   mf_operator;      /* -snes_mf_operator was used on this snes */
123: };

125: /* Context for Eisenstat-Walker convergence criteria for KSP solvers */
126: typedef struct {
127:   PetscInt  version;             /* flag indicating version 1 or 2 of test */
128:   PetscReal rtol_0;              /* initial rtol */
129:   PetscReal rtol_last;           /* last rtol */
130:   PetscReal rtol_max;            /* maximum rtol */
131:   PetscReal gamma;               /* mult. factor for version 2 rtol computation */
132:   PetscReal alpha;               /* power for version 2 rtol computation */
133:   PetscReal alpha2;              /* power for safeguard */
134:   PetscReal threshold;           /* threshold for imposing safeguard */
135:   PetscReal lresid_last;         /* linear residual from last iteration */
136:   PetscReal norm_last;           /* function norm from last iteration */
137: } SNESKSPEW;

139: #define SNESLogConvHistory(snes,res,its) \
140:   { if (snes->conv_hist && snes->conv_hist_max > snes->conv_hist_len) \
141:     { if (snes->conv_hist)     snes->conv_hist[snes->conv_hist_len]     = res; \
142:       if (snes->conv_hist_its) snes->conv_hist_its[snes->conv_hist_len] = its; \
143:       snes->conv_hist_len++;\
144:     }}


148: PetscErrorCode SNES_KSPSolve(SNES,KSP,Vec,Vec);
149: PetscErrorCode SNESScaleStep_Private(SNES,Vec,PetscReal*,PetscReal*,PetscReal*,PetscReal*);



156: #endif