Actual source code: snesimpl.h

  1: /* $Id: snesimpl.h,v 1.56 2001/01/15 21:47:47 bsmith Exp $ */

  3: #ifndef __SNESIMPL_H

  6: #include "petscsnes.h"

  8: /*
  9:    Nonlinear solver context
 10:  */
 11: #define MAXSNESMONITORS 5

 13: struct _p_SNES {
 14:   PETSCHEADER(int)

 16:   /*  ------------------------ User-provided stuff -------------------------------*/
 17:   void  *user;                                        /* user-defined context */

 19:   Vec   vec_sol,vec_sol_always;                /* pointer to solution */
 20:   Vec   vec_sol_update_always;                  /* pointer to solution update */

 22:   int   (*computefunction)(SNES,Vec,Vec,void*); /* function routine */
 23:   Vec   vec_func,vec_func_always;              /* pointer to function (or gradient) */
 24:   void  *funP;                                  /* user-defined function context */

 26:   int   (*computejacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
 27:   Mat   jacobian;                               /* Jacobian (or Hessian) matrix */
 28:   Mat   jacobian_pre;                           /* preconditioner matrix */
 29:   void  *jacP;                                  /* user-defined Jacobian context */
 30:   SLES  sles;                                   /* linear solver context */

 32:   int   (*computescaling)(Vec,Vec,void*);       /* scaling routine */
 33:   Vec   scaling;                                /* scaling vector */
 34:   void  *scaP;                                  /* scaling context */

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

 38:   int   (*monitor[MAXSNESMONITORS])(SNES,int,double,void*); /* monitor routine */
 39:   int   (*monitordestroy[MAXSNESMONITORS])(void*);          /* monitor context destroy routine */
 40:   void  *monitorcontext[MAXSNESMONITORS];                   /* monitor context */
 41:   int   numbermonitors;                                     /* number of monitors */
 42:   int   (*converged)(SNES,double,double,double,SNESConvergedReason*,void*);      /* convergence routine */
 43:   void  *cnvP;                                                    /* convergence context */
 44:   SNESConvergedReason reason;

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

 48:   int   (*setup)(SNES);             /* routine to set up the nonlinear solver */
 49:   int   setupcalled;                /* true if setup has been called */
 50:   int   (*solve)(SNES,int*);        /* actual nonlinear solver */
 51:   int   (*setfromoptions)(SNES);    /* sets options from database */
 52:   void  *data;                      /* implementation-specific data */

 54:   /* --------------------------  Parameters -------------------------------------- */

 56:   int      max_its;            /* max number of iterations */
 57:   int      max_funcs;          /* max number of function evals */
 58:   int      nfuncs;             /* number of function evaluations */
 59:   int      iter;               /* global iteration number */
 60:   int      linear_its;         /* total number of linear solver iterations */
 61:   double   norm;               /* residual norm of current iterate
 62:                                   (or gradient norm of current iterate) */
 63:   double   rtol;               /* relative tolerance */
 64:   double   atol;               /* absolute tolerance */
 65:   double   xtol;               /* relative tolerance in solution */
 66:   double   trunctol;           /* truncation tolerance */

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

 70:   int      nwork;
 71:   Vec      *work;

 73:   /* ------------------------- Miscellaneous Information ------------------------ */

 75:   double     *conv_hist;         /* If !0, stores function norm (or
 76:                                     gradient norm) at each iteration */
 77:   int        *conv_hist_its;     /* linear iterations for each Newton step */
 78:   int        conv_hist_len;      /* size of convergence history array */
 79:   int        conv_hist_max;      /* actual amount of data in conv_history */
 80:   PetscTruth conv_hist_reset;    /* reset counter for each new SNES solve */
 81:   int        nfailures;          /* number of unsuccessful step attempts */

 83:   /* ------------------  Data for unconstrained minimization  ------------------ */
 84:   /* unconstrained minimization info ... For now we share everything else
 85:      with the nonlinear equations code.  We should find a better way to deal 
 86:      with this; the naming conventions are confusing.  Perhaps use unions? */

 88:   int             (*computeumfunction)(SNES,Vec,double*,void*);
 89:   double          fc;                /* function value */
 90:   void            *umfunP;           /* function pointer */
 91:   SNESProblemType method_class;      /* type of solver */
 92:   double          deltatol;          /* trust region convergence tolerance */
 93:   double          fmin;              /* minimum tolerance for function value */
 94:   int             set_method_called; /* flag indicating set_method has been called */

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

103:   Mat         mfshell;           /* MatShell for runtime matrix-free option */

105:   double      ttol;              /* used by default convergence test routine */

107:   Vec         *vwork;            /* more work vectors for Jacobian/Hessian approx */
108:   int         nvwork;
109:   int        (*destroy)(SNES);
110:   int        (*view)(SNES,PetscViewer);
111: };

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

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

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

140: int SNES_KSP_EW_Converged_Private(KSP,int,double,KSPConvergedReason*,void*);
141: int SNES_KSP_EW_ComputeRelativeTolerance_Private(SNES,KSP);
142: int SNESScaleStep_Private(SNES,Vec,double*,double*,double*,double*);

144: #endif