Actual source code: snesimpl.h

  1: /* $Id: snesimpl.h,v 1.59 2001/08/21 21:03: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:   /* Identifies this as a grid SNES structure */
 17:   PetscTruth  isGSNES;                          /* This problem arises from an underlying grid */

 19:   /*  ------------------------ User-provided stuff -------------------------------*/
 20:   void  *user;                                        /* user-defined context */

 22:   Vec   vec_sol,vec_sol_always;                /* pointer to solution */
 23:   Vec   vec_sol_update_always;                  /* pointer to solution update */

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

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

 35:   int   (*computescaling)(Vec,Vec,void*);       /* scaling routine */
 36:   Vec   scaling;                                /* scaling vector */
 37:   void  *scaP;                                  /* scaling context */

 39:   /* ------------------------Boundary conditions-----------------------------------*/
 40:   int (*applyrhsbc)(SNES, Vec, void *);         /* Applies boundary conditions to the rhs */
 41:   int (*applysolbc)(SNES, Vec, void *);         /* Applies boundary conditions to the solution */

 43:   /* ------------------------Time stepping hooks-----------------------------------*/
 44:   int (*update)(SNES, int);                     /* General purpose function for update */

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

 48:   int   (*monitor[MAXSNESMONITORS])(SNES,int,PetscReal,void*); /* monitor routine */
 49:   int   (*monitordestroy[MAXSNESMONITORS])(void*);          /* monitor context destroy routine */
 50:   void  *monitorcontext[MAXSNESMONITORS];                   /* monitor context */
 51:   int   numbermonitors;                                     /* number of monitors */
 52:   int   (*converged)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*);      /* convergence routine */
 53:   void  *cnvP;                                                    /* convergence context */
 54:   SNESConvergedReason reason;

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

 58:   int   (*setup)(SNES);             /* routine to set up the nonlinear solver */
 59:   int   setupcalled;                /* true if setup has been called */
 60:   int   (*solve)(SNES,int*);        /* actual nonlinear solver */
 61:   int   (*setfromoptions)(SNES);    /* sets options from database */
 62:   int   (*printhelp)(SNES,char*);   /* prints help info */
 63:   void  *data;                      /* implementation-specific data */

 65:   /* --------------------------  Parameters -------------------------------------- */

 67:   int      max_its;            /* max number of iterations */
 68:   int      max_funcs;          /* max number of function evals */
 69:   int      nfuncs;             /* number of function evaluations */
 70:   int      iter;               /* global iteration number */
 71:   int      linear_its;         /* total number of linear solver iterations */
 72:   PetscReal   norm;               /* residual norm of current iterate
 73:                                   (or gradient norm of current iterate) */
 74:   PetscReal   rtol;               /* relative tolerance */
 75:   PetscReal   atol;               /* absolute tolerance */
 76:   PetscReal   xtol;               /* relative tolerance in solution */

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

 80:   int      nwork;
 81:   Vec      *work;

 83:   /* ------------------------- Miscellaneous Information ------------------------ */

 85:   PetscReal     *conv_hist;         /* If !0, stores function norm (or
 86:                                     gradient norm) at each iteration */
 87:   int        *conv_hist_its;     /* linear iterations for each Newton step */
 88:   int        conv_hist_len;      /* size of convergence history array */
 89:   int        conv_hist_max;      /* actual amount of data in conv_history */
 90:   PetscTruth conv_hist_reset;    /* reset counter for each new SNES solve */
 91:   int        numFailures;        /* number of unsuccessful step attempts */
 92:   int        maxFailures;        /* maximum number of unsuccessful step attempts */

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

 99:   int             (*computeumfunction)(SNES,Vec,PetscReal*,void*);
100:   PetscReal          fc;                /* function value */
101:   void            *umfunP;           /* function pointer */
102:   SNESProblemType method_class;      /* type of solver */
103:   PetscReal          deltatol;          /* trust region convergence tolerance */
104:   PetscReal          fmin;              /* minimum tolerance for function value */
105:   int             set_method_called; /* flag indicating set_method has been called */

107:  /*
108:    These are REALLY ugly and don't belong here, but since they must 
109:   be destroyed at the conclusion we have to put them somewhere.
110:  */
111:   PetscTruth  ksp_ewconv;        /* flag indicating use of Eisenstat-Walker KSP convergence criteria */
112:   void        *kspconvctx;       /* KSP convergence context */

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

116:   Vec         *vwork;            /* more work vectors for Jacobian/Hessian approx */
117:   int         nvwork;
118:   int        (*destroy)(SNES);
119:   int        (*view)(SNES,PetscViewer);
120: };

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

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

143: #define SNESMonitor(snes,it,rnorm) 
144:         { int _ierr,_i,_im = snes->numbermonitors; 
145:           for (_i=0; _i<_im; _i++) {
146:             _(*snes->monitor[_i])(snes,it,rnorm,snes->monitorcontext[_i]);CHKERRQ(_ierr); 
147:           } 
148:         }

150: int SNES_KSP_EW_Converged_Private(KSP,int,PetscReal,KSPConvergedReason*,void*);
151: int SNES_KSP_EW_ComputeRelativeTolerance_Private(SNES,KSP);
152: int SNESScaleStep_Private(SNES,Vec,PetscReal*,PetscReal*,PetscReal*,PetscReal*);

154: #endif