Actual source code: petsc-snesimpl.h

petsc-3.3-p3 2012-08-29
  2: #ifndef __SNESIMPL_H

  5: #include <petscsnes.h>

  7: typedef struct _SNESOps *SNESOps;

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

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

 33: struct _p_SNES {
 34:   PETSCHEADER(struct _SNESOps);
 35:   DM   dm;
 36:   SNES pc;
 37:   PetscBool usespc;

 39:   /*  ------------------------ User-provided stuff -------------------------------*/
 40:   void  *user;                   /* user-defined context */

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

 45:   Vec  vec_func;                 /* pointer to function */

 47:   Mat  jacobian;                 /* Jacobian matrix */
 48:   Mat  jacobian_pre;             /* preconditioner matrix */
 49:   void *initialguessP;           /* user-defined initial guess context */
 50:   KSP  ksp;                      /* linear solver context */
 51:   SNESLineSearch linesearch;     /* line search context */
 52:   PetscBool usesksp;
 53:   MatStructure matstruct;        /* Used by Picard solver */

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

 57:   Vec  scaling;                  /* scaling vector */
 58:   void *scaP;                    /* scaling context */

 60:   PetscReal precheck_picard_angle; /* For use with SNESLineSearchPreCheckPicard */

 62:   /* ------------------------Time stepping hooks-----------------------------------*/

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

 66:   PetscErrorCode      (*monitor[MAXSNESMONITORS])(SNES,PetscInt,PetscReal,void*); /* monitor routine */
 67:   PetscErrorCode      (*monitordestroy[MAXSNESMONITORS])(void**);                 /* monitor context destroy routine */
 68:   void                *monitorcontext[MAXSNESMONITORS];                           /* monitor context */
 69:   PetscInt            numbermonitors;                                             /* number of monitors */
 70:   void                *cnvP;                                                      /* convergence context */
 71:   SNESConvergedReason reason;
 72:   PetscBool           errorifnotconverged;

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

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

 79:   /* --------------------------  Parameters -------------------------------------- */

 81:   PetscInt    max_its;            /* max number of iterations */
 82:   PetscInt    max_funcs;          /* max number of function evals */
 83:   PetscInt    nfuncs;             /* number of function evaluations */
 84:   PetscInt    iter;               /* global iteration number */
 85:   PetscInt    linear_its;         /* total number of linear solver iterations */
 86:   PetscReal   norm;               /* residual norm of current iterate */
 87:   PetscReal   rtol;               /* relative tolerance */
 88:   PetscReal   abstol;             /* absolute tolerance */
 89:   PetscReal   stol;               /* step length tolerance*/
 90:   PetscReal   deltatol;           /* trust region convergence tolerance */
 91:   PetscBool   printreason;        /* print reason for convergence/divergence after each solve */
 92:   PetscInt    lagpreconditioner;  /* SNESSetLagPreconditioner() */
 93:   PetscInt    lagjacobian;        /* SNESSetLagJacobian() */
 94:   PetscInt    gridsequence;       /* number of grid sequence steps to take; defaults to zero */
 95:   PetscInt    gssweeps;           /* number of GS sweeps */

 97:   PetscBool   tolerancesset;      /* SNESSetTolerances() called and tolerances should persist through SNESCreate_XXX()*/

 99:   PetscReal   norm_init;          /* the initial norm value */
100:   PetscBool   norm_init_set;      /* the initial norm has been set */
101:   PetscBool   vec_func_init_set;  /* the initial function has been set */

103:   SNESNormType normtype;          /* Norm computation type for SNES instance */

105:   /* ------------------------ Default work-area management ---------------------- */

107:   PetscInt    nwork;
108:   Vec         *work;

110:   /* ------------------------- Miscellaneous Information ------------------------ */

112:   PetscReal   *conv_hist;         /* If !0, stores function norm (or
113:                                     gradient norm) at each iteration */
114:   PetscInt    *conv_hist_its;     /* linear iterations for each Newton step */
115:   PetscInt    conv_hist_len;      /* size of convergence history array */
116:   PetscInt    conv_hist_max;      /* actual amount of data in conv_history */
117:   PetscBool   conv_hist_reset;    /* reset counter for each new SNES solve */
118:   PetscBool   conv_malloc;

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

124:   PetscInt    numLinearSolveFailures;
125:   PetscInt    maxLinearSolveFailures;

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

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

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

134:   Vec         *vwork;            /* more work vectors for Jacobian approx */
135:   PetscInt    nvwork;

137:   PetscBool   mf_operator;      /* -snes_mf_operator was used on this snes */

139:   Vec         xl,xu;             /* upper and lower bounds for box constrained VI problems */
140:   PetscInt    ntruebounds;       /* number of non-infinite bounds set for VI box constraints */
141:   PetscBool   usersetbounds;     /* bounds have been set via SNESVISetVariableBounds(), rather than via computevariablebounds() callback. */
142: };

144: /* Context for resolution-dependent SNES callback information */
145: typedef struct _n_SNESDM *SNESDM;
146: struct _n_SNESDM {
147:   PetscErrorCode (*computefunction)(SNES,Vec,Vec,void*);
148:   PetscErrorCode (*computegs)(SNES,Vec,Vec,void*);
149:   PetscErrorCode (*computejacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);

151:   /* Picard iteration functions */
152:   PetscErrorCode (*computepfunction)(SNES,Vec,Vec,void*);
153:   PetscErrorCode (*computepjacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
154:   void *functionctx;
155:   void *gsctx;
156:   void *pctx;
157:   void *jacobianctx;

159:   /* This context/destroy pair allows implementation-specific routines such as DMDA local functions. */
160:   PetscErrorCode (*destroy)(SNESDM);
161:   void *data;

163:   /* This is NOT reference counted. The SNES that originally created this context is cached here to implement copy-on-write.
164:    * Fields in the SNESDM should only be written if the SNES matches originalsnes.
165:    */
166:   DM originaldm;
167: };
168: PETSC_EXTERN PetscErrorCode DMSNESGetContext(DM,SNESDM*);
169: PETSC_EXTERN PetscErrorCode DMSNESGetContextWrite(DM,SNESDM*);
170: PETSC_EXTERN PetscErrorCode DMSNESCopyContext(DM,DM);
171: PETSC_EXTERN PetscErrorCode DMSNESDuplicateContext(DM,DM);
172: PETSC_EXTERN PetscErrorCode DMSNESSetUpLegacy(DM);

174: /* Context for Eisenstat-Walker convergence criteria for KSP solvers */
175: typedef struct {
176:   PetscInt  version;             /* flag indicating version 1 or 2 of test */
177:   PetscReal rtol_0;              /* initial rtol */
178:   PetscReal rtol_last;           /* last rtol */
179:   PetscReal rtol_max;            /* maximum rtol */
180:   PetscReal gamma;               /* mult. factor for version 2 rtol computation */
181:   PetscReal alpha;               /* power for version 2 rtol computation */
182:   PetscReal alpha2;              /* power for safeguard */
183:   PetscReal threshold;           /* threshold for imposing safeguard */
184:   PetscReal lresid_last;         /* linear residual from last iteration */
185:   PetscReal norm_last;           /* function norm from last iteration */
186: } SNESKSPEW;

188: #define SNESLogConvHistory(snes,res,its) \
189:   { if (snes->conv_hist && snes->conv_hist_max > snes->conv_hist_len) \
190:     { if (snes->conv_hist)     snes->conv_hist[snes->conv_hist_len]     = res; \
191:       if (snes->conv_hist_its) snes->conv_hist_its[snes->conv_hist_len] = its; \
192:       snes->conv_hist_len++;\
193:     }}

195: PETSC_EXTERN PetscErrorCode SNESDefaultGetWork(SNES,PetscInt);

197: PETSC_EXTERN PetscErrorCode SNESVIProjectOntoBounds(SNES,Vec);
198: PETSC_EXTERN PetscErrorCode SNESVICheckLocalMin_Private(SNES,Mat,Vec,Vec,PetscReal,PetscBool*);
199: PETSC_EXTERN PetscErrorCode SNESReset_VI(SNES);
200: PETSC_EXTERN PetscErrorCode SNESDestroy_VI(SNES);
201: PETSC_EXTERN PetscErrorCode SNESView_VI(SNES,PetscViewer);
202: PETSC_EXTERN PetscErrorCode SNESSetFromOptions_VI(SNES);
203: PETSC_EXTERN PetscErrorCode SNESSetUp_VI(SNES);
204: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*SNESVIComputeVariableBoundsFunction)(SNES,Vec,Vec);
205: EXTERN_C_BEGIN
206: PETSC_EXTERN PetscErrorCode SNESVISetComputeVariableBounds_VI(SNES,SNESVIComputeVariableBoundsFunction);
207: PETSC_EXTERN PetscErrorCode SNESVISetVariableBounds_VI(SNES,Vec,Vec);
208: EXTERN_C_END
209: PETSC_EXTERN PetscErrorCode SNESDefaultConverged_VI(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*);

211: PetscErrorCode SNES_KSPSolve(SNES,KSP,Vec,Vec);
212: PetscErrorCode SNESScaleStep_Private(SNES,Vec,PetscReal*,PetscReal*,PetscReal*,PetscReal*);

214: PETSC_EXTERN PetscBool SNESRegisterAllCalled;
215: PETSC_EXTERN PetscFList SNESList;

217: PETSC_EXTERN PetscLogEvent SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval, SNES_GSEval;

219: #endif