Actual source code: petsc-tsimpl.h

petsc-3.4.5 2014-06-29
  2: #ifndef __TSIMPL_H

  5: #include <petscts.h>
  6: #include <petsc-private/petscimpl.h>

  8: /*
  9:     Timesteping context.
 10:       General DAE: F(t,U,U_t) = 0, required Jacobian is G'(U) where G(U) = F(t,U,U0+a*U)
 11:       General ODE: U_t = F(t,U) <-- the right-hand-side function
 12:       Linear  ODE: U_t = A(t) U <-- the right-hand-side matrix
 13:       Linear (no time) ODE: U_t = A U <-- the right-hand-side matrix
 14: */

 16: /*
 17:      Maximum number of monitors you can run with a single TS
 18: */
 19: #define MAXTSMONITORS 5

 21: typedef struct _TSOps *TSOps;

 23: struct _TSOps {
 24:   PetscErrorCode (*snesfunction)(SNES,Vec,Vec,TS);
 25:   PetscErrorCode (*snesjacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,TS);
 26:   PetscErrorCode (*setup)(TS);
 27:   PetscErrorCode (*step)(TS);
 28:   PetscErrorCode (*solve)(TS);
 29:   PetscErrorCode (*interpolate)(TS,PetscReal,Vec);
 30:   PetscErrorCode (*evaluatestep)(TS,PetscInt,Vec,PetscBool*);
 31:   PetscErrorCode (*setfromoptions)(TS);
 32:   PetscErrorCode (*destroy)(TS);
 33:   PetscErrorCode (*view)(TS,PetscViewer);
 34:   PetscErrorCode (*reset)(TS);
 35:   PetscErrorCode (*linearstability)(TS,PetscReal,PetscReal,PetscReal*,PetscReal*);
 36:   PetscErrorCode (*load)(TS,PetscViewer);
 37: };

 39: struct _p_TS {
 40:   PETSCHEADER(struct _TSOps);
 41:   DM            dm;
 42:   TSProblemType problem_type;
 43:   Vec           vec_sol;
 44:   TSAdapt       adapt;

 46:   /* ---------------- User (or PETSc) Provided stuff ---------------------*/
 47:   PetscErrorCode (*monitor[MAXTSMONITORS])(TS,PetscInt,PetscReal,Vec,void*); /* returns control to user after */
 48:   PetscErrorCode (*monitordestroy[MAXTSMONITORS])(void**);
 49:   void *monitorcontext[MAXTSMONITORS];                 /* residual calculation, allows user */
 50:   PetscInt  numbermonitors;                                 /* to, for instance, print residual norm, etc. */

 52:   PetscErrorCode (*prestep)(TS);
 53:   PetscErrorCode (*prestage)(TS,PetscReal);
 54:   PetscErrorCode (*poststep)(TS);

 56:   /* ---------------------- IMEX support ---------------------------------*/
 57:   /* These extra slots are only used when the user provides both Implicit and RHS */
 58:   Mat Arhs;     /* Right hand side matrix */
 59:   Mat Brhs;     /* Right hand side preconditioning matrix */
 60:   Vec Frhs;     /* Right hand side function value */

 62:   /* This is a general caching scheme to avoid recomputing the Jacobian at a place that has been previously been evaluated.
 63:    * The present use case is that TSComputeRHSFunctionLinear() evaluates the Jacobian once and we don't want it to be immeditely re-evaluated.
 64:    */
 65:   struct {
 66:     PetscReal time;             /* The time at which the matrices were last evaluated */
 67:     Vec X;                      /* Solution vector at which the Jacobian was last evaluated */
 68:     PetscInt Xstate;            /* State of the solution vector */
 69:     MatStructure mstructure;    /* The structure returned */
 70:     /* Flag to unshift Jacobian before calling the IJacobian or RHSJacobian functions.  This is useful
 71:      * if the user would like to reuse (part of) the Jacobian from the last evaluation. */
 72:     PetscBool reuse;
 73:     PetscReal scale,shift;
 74:   } rhsjacobian;

 76:   struct {
 77:     PetscReal shift;            /* The derivative of the lhs wrt to Xdot */
 78:   } ijacobian;

 80:   /* ---------------------Nonlinear Iteration------------------------------*/
 81:   SNES  snes;

 83:   /* --- Data that is unique to each particular solver --- */
 84:   PetscInt setupcalled;             /* true if setup has been called */
 85:   void     *data;                   /* implementationspecific data */
 86:   void     *user;                   /* user context */

 88:   /* ------------------  Parameters -------------------------------------- */
 89:   PetscInt  max_steps;              /* max number of steps */
 90:   PetscReal max_time;               /* max time allowed */
 91:   PetscReal time_step;              /* current/completed time increment */
 92:   PetscReal time_step_prev;         /* previous time step  */

 94:   /*
 95:      these are temporary to support increasing the time step if nonlinear solver convergence remains good
 96:      and time_step was previously cut due to failed nonlinear solver
 97:   */
 98:   PetscReal time_step_orig;            /* original time step requested by user */
 99:   PetscInt  time_steps_since_decrease; /* number of timesteps since timestep was decreased due to lack of convergence */
100:   /* ----------------------------------------------------------------------------------------------------------------*/

102:   PetscInt  steps;                  /* steps taken so far */
103:   PetscReal ptime;                  /* time at the start of the current step (stage time is internal if it exists) */
104:   PetscReal solvetime;              /* time at the conclusion of TSSolve() */
105:   PetscInt  ksp_its;                /* total number of linear solver iterations */
106:   PetscInt  snes_its;               /* total number of nonlinear solver iterations */

108:   PetscInt num_snes_failures;
109:   PetscInt max_snes_failures;
110:   TSConvergedReason reason;
111:   TSEquationType equation_type;
112:   PetscBool errorifstepfailed;
113:   TSExactFinalTimeOption  exact_final_time;
114:   PetscBool retain_stages;
115:   PetscInt reject,max_reject;

117:   PetscReal atol,rtol;          /* Relative and absolute tolerance for local truncation error */
118:   Vec       vatol,vrtol;        /* Relative and absolute tolerance in vector form */
119:   PetscReal cfltime,cfltime_local;

121:   /* ------------------- Default work-area management ------------------ */
122:   PetscInt nwork;
123:   Vec      *work;
124: };

126: struct _TSAdaptOps {
127:   PetscErrorCode (*choose)(TSAdapt,TS,PetscReal,PetscInt*,PetscReal*,PetscBool*,PetscReal*);
128:   PetscErrorCode (*checkstage)(TSAdapt,TS,PetscBool*);
129:   PetscErrorCode (*destroy)(TSAdapt);
130:   PetscErrorCode (*view)(TSAdapt,PetscViewer);
131:   PetscErrorCode (*setfromoptions)(TSAdapt);
132:   PetscErrorCode (*load)(TSAdapt,PetscViewer);
133: };

135: struct _p_TSAdapt {
136:   PETSCHEADER(struct _TSAdaptOps);
137:   void *data;
138:   struct {
139:     PetscInt   n;                /* number of candidate schemes, including the one currently in use */
140:     PetscBool  inuse_set;        /* the current scheme has been set */
141:     const char *name[16];        /* name of the scheme */
142:     PetscInt   order[16];        /* classical order of each scheme */
143:     PetscInt   stageorder[16];   /* stage order of each scheme */
144:     PetscReal  ccfl[16];         /* stability limit relative to explicit Euler */
145:     PetscReal  cost[16];         /* relative measure of the amount of work required for each scheme */
146:   } candidates;
147:   PetscReal   dt_min,dt_max;
148:   PetscReal   scale_solve_failed; /* Scale step by this factor if solver (linear or nonlinear) fails. */
149:   PetscViewer monitor;
150: };

152: typedef struct _p_DMTS *DMTS;
153: typedef struct _DMTSOps *DMTSOps;
154: struct _DMTSOps {
155:   TSRHSFunction rhsfunction;
156:   TSRHSJacobian rhsjacobian;

158:   TSIFunction ifunction;
159:   PetscErrorCode (*ifunctionview)(void*,PetscViewer);
160:   PetscErrorCode (*ifunctionload)(void**,PetscViewer);

162:   TSIJacobian ijacobian;
163:   PetscErrorCode (*ijacobianview)(void*,PetscViewer);
164:   PetscErrorCode (*ijacobianload)(void**,PetscViewer);

166:   TSSolutionFunction solution;
167:   PetscErrorCode (*forcing)(TS,PetscReal,Vec,void*);

169:   PetscErrorCode (*destroy)(DMTS);
170:   PetscErrorCode (*duplicate)(DMTS,DMTS);
171: };

173: struct _p_DMTS {
174:   PETSCHEADER(struct _DMTSOps);
175:   void *rhsfunctionctx;
176:   void *rhsjacobianctx;

178:   void *ifunctionctx;
179:   void *ijacobianctx;

181:   void *solutionctx;
182:   void *forcingctx;

184:   void *data;

186:   /* This is NOT reference counted. The DM on which this context was first created is cached here to implement one-way
187:    * copy-on-write. When DMGetDMTSWrite() sees a request using a different DM, it makes a copy. Thus, if a user
188:    * only interacts directly with one level, e.g., using TSSetIFunction(), then coarse levels of a multilevel item
189:    * integrator are built, then the user changes the routine with another call to TSSetIFunction(), it automatically
190:    * propagates to all the levels. If instead, they get out a specific level and set the function on that level,
191:    * subsequent changes to the original level will no longer propagate to that level.
192:    */
193:   DM originaldm;
194: };

196: PETSC_EXTERN PetscErrorCode DMGetDMTS(DM,DMTS*);
197: PETSC_EXTERN PetscErrorCode DMGetDMTSWrite(DM,DMTS*);
198: PETSC_EXTERN PetscErrorCode DMCopyDMTS(DM,DM);
199: PETSC_EXTERN PetscErrorCode DMTSView(DMTS,PetscViewer);
200: PETSC_EXTERN PetscErrorCode DMTSLoad(DMTS,PetscViewer);


203: PETSC_EXTERN PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;

205: typedef enum {TS_STEP_INCOMPLETE, /* vec_sol, ptime, etc point to beginning of step */
206:               TS_STEP_PENDING,    /* vec_sol advanced, but step has not been accepted yet */
207:               TS_STEP_COMPLETE    /* step accepted and ptime, steps, etc have been advanced */
208: } TSStepStatus;

210: #endif