Actual source code: tsimpl.h
1: #pragma once
3: #include <petscts.h>
4: #include <petsc/private/petscimpl.h>
6: /* SUBMANSEC = TS */
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 10
21: PETSC_EXTERN PetscBool TSRegisterAllCalled;
22: PETSC_EXTERN PetscErrorCode TSRegisterAll(void);
23: PETSC_EXTERN PetscErrorCode TSAdaptRegisterAll(void);
25: PETSC_EXTERN PetscErrorCode TSRKRegisterAll(void);
26: PETSC_EXTERN PetscErrorCode TSMPRKRegisterAll(void);
27: PETSC_EXTERN PetscErrorCode TSARKIMEXRegisterAll(void);
28: PETSC_EXTERN PetscErrorCode TSRosWRegisterAll(void);
29: PETSC_EXTERN PetscErrorCode TSGLLERegisterAll(void);
30: PETSC_EXTERN PetscErrorCode TSGLLEAdaptRegisterAll(void);
31: PETSC_EXTERN PetscErrorCode TSIRKRegisterAll(void);
33: typedef struct _TSOps *TSOps;
35: struct _TSOps {
36: PetscErrorCode (*snesfunction)(SNES, Vec, Vec, TS);
37: PetscErrorCode (*snesjacobian)(SNES, Vec, Mat, Mat, TS);
38: PetscErrorCode (*setup)(TS);
39: PetscErrorCode (*step)(TS);
40: PetscErrorCode (*solve)(TS);
41: PetscErrorCode (*interpolate)(TS, PetscReal, Vec);
42: PetscErrorCode (*evaluatewlte)(TS, NormType, PetscInt *, PetscReal *);
43: PetscErrorCode (*evaluatestep)(TS, PetscInt, Vec, PetscBool *);
44: PetscErrorCode (*setfromoptions)(TS, PetscOptionItems);
45: PetscErrorCode (*destroy)(TS);
46: PetscErrorCode (*view)(TS, PetscViewer);
47: PetscErrorCode (*reset)(TS);
48: PetscErrorCode (*linearstability)(TS, PetscReal, PetscReal, PetscReal *, PetscReal *);
49: PetscErrorCode (*load)(TS, PetscViewer);
50: PetscErrorCode (*rollback)(TS);
51: PetscErrorCode (*getstages)(TS, PetscInt *, Vec *[]);
52: PetscErrorCode (*adjointstep)(TS);
53: PetscErrorCode (*adjointsetup)(TS);
54: PetscErrorCode (*adjointreset)(TS);
55: PetscErrorCode (*adjointintegral)(TS);
56: PetscErrorCode (*forwardsetup)(TS);
57: PetscErrorCode (*forwardreset)(TS);
58: PetscErrorCode (*forwardstep)(TS);
59: PetscErrorCode (*forwardintegral)(TS);
60: PetscErrorCode (*forwardgetstages)(TS, PetscInt *, Mat *[]);
61: PetscErrorCode (*getsolutioncomponents)(TS, PetscInt *, Vec *);
62: PetscErrorCode (*getauxsolution)(TS, Vec *);
63: PetscErrorCode (*gettimeerror)(TS, PetscInt, Vec *);
64: PetscErrorCode (*settimeerror)(TS, Vec);
65: PetscErrorCode (*startingmethod)(TS);
66: PetscErrorCode (*initcondition)(TS, Vec);
67: PetscErrorCode (*exacterror)(TS, Vec, Vec);
68: PetscErrorCode (*resizeregister)(TS, PetscBool);
69: };
71: /*
72: TSEvent - Abstract object to handle event monitoring
73: */
74: typedef struct _n_TSEvent *TSEvent;
76: typedef struct _TSTrajectoryOps *TSTrajectoryOps;
78: struct _TSTrajectoryOps {
79: PetscErrorCode (*view)(TSTrajectory, PetscViewer);
80: PetscErrorCode (*reset)(TSTrajectory);
81: PetscErrorCode (*destroy)(TSTrajectory);
82: PetscErrorCode (*set)(TSTrajectory, TS, PetscInt, PetscReal, Vec);
83: PetscErrorCode (*get)(TSTrajectory, TS, PetscInt, PetscReal *);
84: PetscErrorCode (*setfromoptions)(TSTrajectory, PetscOptionItems);
85: PetscErrorCode (*setup)(TSTrajectory, TS);
86: };
88: /* TSHistory is an helper object that allows inquiring
89: the TSTrajectory by time and not by the step number only */
90: typedef struct _n_TSHistory *TSHistory;
92: struct _p_TSTrajectory {
93: PETSCHEADER(struct _TSTrajectoryOps);
94: TSHistory tsh; /* associates times to unique step ids */
95: /* stores necessary data to reconstruct states and derivatives via Lagrangian interpolation */
96: struct {
97: PetscInt order; /* interpolation order */
98: Vec *W; /* work vectors */
99: PetscScalar *L; /* workspace for Lagrange basis */
100: PetscReal *T; /* Lagrange times (stored) */
101: Vec *WW; /* just an array of pointers */
102: PetscBool *TT; /* workspace for Lagrange */
103: PetscReal *TW; /* Lagrange times (workspace) */
105: /* caching */
106: PetscBool caching;
107: struct {
108: PetscObjectId id;
109: PetscObjectState state;
110: PetscReal time;
111: PetscInt step;
112: } Ucached;
113: struct {
114: PetscObjectId id;
115: PetscObjectState state;
116: PetscReal time;
117: PetscInt step;
118: } Udotcached;
119: } lag;
120: Vec U, Udot; /* used by TSTrajectory{Get|Restore}UpdatedHistoryVecs */
121: PetscBool usehistory; /* whether to use TSHistory */
122: PetscBool solution_only; /* whether we dump just the solution or also the stages */
123: PetscBool adjoint_solve_mode; /* whether we will use the Trajectory inside a TSAdjointSolve() or not */
124: PetscViewer monitor;
125: PetscBool setupcalled; /* true if setup has been called */
126: PetscInt recomps; /* counter for recomputations in the adjoint run */
127: PetscInt diskreads, diskwrites; /* counters for disk checkpoint reads and writes */
128: char **names; /* the name of each variable; each process has only the local names */
129: PetscBool keepfiles; /* keep the files generated during the run after the run is complete */
130: char *dirname, *filetemplate; /* directory name and file name template for disk checkpoints */
131: char *dirfiletemplate; /* complete directory and file name template for disk checkpoints */
132: PetscErrorCode (*transform)(void *, Vec, Vec *);
133: PetscErrorCode (*transformdestroy)(void *);
134: void *transformctx;
135: void *data;
136: };
138: typedef struct _TS_RHSSplitLink *TS_RHSSplitLink;
139: struct _TS_RHSSplitLink {
140: TS ts;
141: char *splitname;
142: IS is;
143: TS_RHSSplitLink next;
144: PetscLogEvent event;
145: };
147: typedef struct _TS_EvaluationTimes *TSEvaluationTimes;
148: struct _TS_EvaluationTimes {
149: PetscInt num_time_points; /* number of time points */
150: PetscReal *time_points; /* array of the time span */
151: PetscReal reltol; /* relative tolerance for span point detection */
152: PetscReal abstol; /* absolute tolerance for span point detection */
153: PetscReal worktol; /* the ultimate tolerance (variable), maintained within a single TS time step for consistency */
154: PetscInt time_point_idx; /* index of the time_point to be reached next */
155: PetscInt sol_idx; /* index into sol_vecs and sol_times */
156: Vec *sol_vecs; /* array of the solutions at the specified time points */
157: PetscReal *sol_times; /* array of times that sol_vecs was taken at */
158: };
160: struct _p_TS {
161: PETSCHEADER(struct _TSOps);
162: TSProblemType problem_type;
163: TSEquationType equation_type;
165: DM dm;
166: Vec vec_sol; /* solution vector in first and second order equations */
167: Vec vec_sol0; /* solution vector at the beginning of the step */
168: Vec vec_dot; /* time derivative vector in second order equations */
169: TSAdapt adapt;
170: TSAdaptType default_adapt_type;
171: TSEvent event;
173: /* ---------------- Resize ---------------------*/
174: PetscBool resizerollback;
175: PetscObjectList resizetransferobjs;
177: /* ---------------- User (or PETSc) Provided stuff ---------------------*/
178: PetscErrorCode (*monitor[MAXTSMONITORS])(TS, PetscInt, PetscReal, Vec, void *);
179: PetscCtxDestroyFn *monitordestroy[MAXTSMONITORS];
180: void *monitorcontext[MAXTSMONITORS];
181: PetscInt numbermonitors;
182: PetscErrorCode (*adjointmonitor[MAXTSMONITORS])(TS, PetscInt, PetscReal, Vec, PetscInt, Vec *, Vec *, void *);
183: PetscCtxDestroyFn *adjointmonitordestroy[MAXTSMONITORS];
184: void *adjointmonitorcontext[MAXTSMONITORS];
185: PetscInt numberadjointmonitors;
187: PetscErrorCode (*prestep)(TS);
188: PetscErrorCode (*prestage)(TS, PetscReal);
189: PetscErrorCode (*poststage)(TS, PetscReal, PetscInt, Vec *);
190: PetscErrorCode (*postevaluate)(TS);
191: PetscErrorCode (*poststep)(TS);
192: PetscErrorCode (*functiondomainerror)(TS, PetscReal, Vec, PetscBool *);
193: PetscErrorCode (*resizesetup)(TS, PetscInt, PetscReal, Vec, PetscBool *, void *);
194: PetscErrorCode (*resizetransfer)(TS, PetscInt, Vec[], Vec[], void *);
195: void *resizectx;
197: /* ---------------------- Sensitivity Analysis support -----------------*/
198: TSTrajectory trajectory; /* All solutions are kept here for the entire time integration process */
199: Vec *vecs_sensi; /* one vector for each cost function */
200: Vec *vecs_sensip;
201: PetscInt numcost; /* number of cost functions */
202: Vec vec_costintegral;
203: PetscBool adjointsetupcalled;
204: PetscInt adjoint_steps;
205: PetscInt adjoint_max_steps;
206: PetscBool adjoint_solve; /* immediately call TSAdjointSolve() after TSSolve() is complete */
207: PetscBool costintegralfwd; /* cost integral is evaluated in the forward run if true */
208: Vec vec_costintegrand; /* workspace for Adjoint computations */
209: Mat Jacp, Jacprhs;
210: void *ijacobianpctx, *rhsjacobianpctx;
211: void *costintegrandctx;
212: Vec *vecs_drdu;
213: Vec *vecs_drdp;
214: Vec vec_drdu_col, vec_drdp_col;
216: /* first-order adjoint */
217: PetscErrorCode (*rhsjacobianp)(TS, PetscReal, Vec, Mat, void *);
218: PetscErrorCode (*ijacobianp)(TS, PetscReal, Vec, Vec, PetscReal, Mat, void *);
219: PetscErrorCode (*costintegrand)(TS, PetscReal, Vec, Vec, void *);
220: PetscErrorCode (*drdufunction)(TS, PetscReal, Vec, Vec *, void *);
221: PetscErrorCode (*drdpfunction)(TS, PetscReal, Vec, Vec *, void *);
223: /* second-order adjoint */
224: Vec *vecs_sensi2;
225: Vec *vecs_sensi2p;
226: Vec vec_dir; /* directional vector for optimization */
227: Vec *vecs_fuu, *vecs_guu;
228: Vec *vecs_fup, *vecs_gup;
229: Vec *vecs_fpu, *vecs_gpu;
230: Vec *vecs_fpp, *vecs_gpp;
231: void *ihessianproductctx, *rhshessianproductctx;
232: PetscErrorCode (*ihessianproduct_fuu)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
233: PetscErrorCode (*ihessianproduct_fup)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
234: PetscErrorCode (*ihessianproduct_fpu)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
235: PetscErrorCode (*ihessianproduct_fpp)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
236: PetscErrorCode (*rhshessianproduct_guu)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
237: PetscErrorCode (*rhshessianproduct_gup)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
238: PetscErrorCode (*rhshessianproduct_gpu)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
239: PetscErrorCode (*rhshessianproduct_gpp)(TS, PetscReal, Vec, Vec *, Vec, Vec *, void *);
241: /* specific to forward sensitivity analysis */
242: Mat mat_sensip; /* matrix storing forward sensitivities */
243: Vec vec_sensip_col; /* space for a column of the sensip matrix */
244: Vec *vecs_integral_sensip; /* one vector for each integral */
245: PetscInt num_parameters;
246: PetscInt num_initialvalues;
247: void *vecsrhsjacobianpctx;
248: PetscBool forwardsetupcalled;
249: PetscBool forward_solve;
250: PetscErrorCode (*vecsrhsjacobianp)(TS, PetscReal, Vec, Vec *, void *);
252: /* ---------------------- IMEX support ---------------------------------*/
253: /* These extra slots are only used when the user provides both Implicit and RHS */
254: Mat Arhs; /* Right hand side matrix */
255: Mat Brhs; /* Right hand side matrix used to construct the preconditioner */
256: Vec Frhs; /* Right hand side function value */
258: /* This is a general caching scheme to avoid recomputing the Jacobian at a place that has been previously been evaluated.
259: * The present use case is that TSComputeRHSFunctionLinear() evaluates the Jacobian once and we don't want it to be immeditely re-evaluated.
260: */
261: struct {
262: PetscReal time; /* The time at which the matrices were last evaluated */
263: PetscObjectId Xid; /* Unique ID of solution vector at which the Jacobian was last evaluated */
264: PetscObjectState Xstate; /* State of the solution vector */
265: MatStructure mstructure; /* The structure returned */
266: /* Flag to unshift Jacobian before calling the IJacobian or RHSJacobian functions. This is useful
267: * if the user would like to reuse (part of) the Jacobian from the last evaluation. */
268: PetscBool reuse;
269: PetscReal scale, shift;
270: } rhsjacobian;
272: struct {
273: PetscReal shift; /* The derivative of the lhs wrt to Xdot */
274: } ijacobian;
276: MatStructure axpy_pattern; /* information about the nonzero pattern of the RHS Jacobian in reference to the implicit Jacobian */
277: /* --------------------Nonlinear Iteration------------------------------*/
278: SNES snes;
279: PetscBool usessnes; /* Flag set by each TSType to indicate if the type actually uses a SNES;
280: this works around the design flaw that a SNES is ALWAYS created with TS even when it is not needed.*/
281: PetscInt ksp_its; /* total number of linear solver iterations */
282: PetscInt snes_its; /* total number of nonlinear solver iterations */
283: PetscInt num_snes_failures;
284: PetscInt max_snes_failures;
286: /* --- Logging --- */
287: PetscInt ifuncs, rhsfuncs, ijacs, rhsjacs;
289: /* --- Data that is unique to each particular solver --- */
290: PetscBool setupcalled; /* true if setup has been called */
291: void *data; /* implementationspecific data */
292: void *ctx; /* user context */
294: PetscBool steprollback; /* flag to indicate that the step was rolled back */
295: PetscBool steprestart; /* flag to indicate that the timestepper has to discard any history and restart */
296: PetscBool stepresize; /* flag to indicate that the discretization was resized */
297: PetscInt steps; /* steps taken so far in all successive calls to TSSolve() */
298: PetscReal ptime; /* time at the start of the current step (stage time is internal if it exists) */
299: PetscReal time_step; /* current time increment */
300: PetscReal time_step0; /* proposed time increment at the beginning of the step */
301: PetscReal ptime_prev; /* time at the start of the previous step */
302: PetscReal ptime_prev_rollback; /* time at the start of the 2nd previous step to recover from rollback */
303: PetscReal solvetime; /* time at the conclusion of TSSolve() */
304: PetscBool stifflyaccurate; /* flag to indicate that the method is stiffly accurate */
306: TSConvergedReason reason;
307: PetscBool errorifstepfailed;
308: PetscInt reject, max_reject;
309: TSExactFinalTimeOption exact_final_time;
311: PetscObjectParameterDeclare(PetscReal, rtol); /* Relative and absolute tolerance for local truncation error */
312: PetscObjectParameterDeclare(PetscReal, atol);
313: PetscObjectParameterDeclare(PetscReal, max_time); /* max time allowed */
314: PetscObjectParameterDeclare(PetscInt, max_steps); /* maximum time-step number to execute until (possibly with nonzero starting value) */
315: PetscObjectParameterDeclare(PetscInt, run_steps); /* maximum number of time steps for TSSolve to take on each call */
316: Vec vatol, vrtol; /* Relative and absolute tolerance in vector form */
317: PetscReal cfltime, cfltime_local;
318: PetscInt start_step; /* step number at start of current run */
320: PetscBool testjacobian;
321: PetscBool testjacobiantranspose;
322: /* ------------------- Default work-area management ------------------ */
323: PetscInt nwork;
324: Vec *work;
326: /* ---------------------- RHS splitting support ---------------------------------*/
327: PetscInt num_rhs_splits;
328: TS_RHSSplitLink tsrhssplit;
329: PetscBool use_splitrhsfunction;
330: SNES snesrhssplit;
332: /* ---------------------- Quadrature integration support ---------------------------------*/
333: TS quadraturets;
335: /* ---------------------- Time span support ---------------------------------*/
336: TSEvaluationTimes eval_times;
337: };
339: struct _TSAdaptOps {
340: PetscErrorCode (*choose)(TSAdapt, TS, PetscReal, PetscInt *, PetscReal *, PetscBool *, PetscReal *, PetscReal *, PetscReal *);
341: PetscErrorCode (*destroy)(TSAdapt);
342: PetscErrorCode (*reset)(TSAdapt);
343: PetscErrorCode (*view)(TSAdapt, PetscViewer);
344: PetscErrorCode (*setfromoptions)(TSAdapt, PetscOptionItems);
345: PetscErrorCode (*load)(TSAdapt, PetscViewer);
346: };
348: struct _p_TSAdapt {
349: PETSCHEADER(struct _TSAdaptOps);
350: void *data;
351: PetscErrorCode (*checkstage)(TSAdapt, TS, PetscReal, Vec, PetscBool *);
352: struct {
353: PetscInt n; /* number of candidate schemes, including the one currently in use */
354: PetscBool inuse_set; /* the current scheme has been set */
355: const char *name[16]; /* name of the scheme */
356: PetscInt order[16]; /* classical order of each scheme */
357: PetscInt stageorder[16]; /* stage order of each scheme */
358: PetscReal ccfl[16]; /* stability limit relative to explicit Euler */
359: PetscReal cost[16]; /* relative measure of the amount of work required for each scheme */
360: } candidates;
361: PetscBool always_accept;
362: PetscReal safety; /* safety factor relative to target error/stability goal */
363: PetscReal reject_safety; /* extra safety factor if the last step was rejected */
364: PetscReal clip[2]; /* admissible time step decrease/increase factors */
365: PetscReal dt_min, dt_max; /* admissible minimum and maximum time step */
366: PetscReal ignore_max; /* minimum value of the solution to be considered by the adaptor */
367: PetscBool glee_use_local; /* GLEE adaptor uses global or local error */
368: PetscReal scale_solve_failed; /* scale step by this factor if solver (linear or nonlinear) fails. */
369: PetscReal matchstepfac[2]; /* factors to control the behaviour of matchstep */
370: NormType wnormtype;
371: PetscViewer monitor;
372: PetscInt timestepjustdecreased_delay; /* number of timesteps after a decrease in the timestep before the timestep can be increased */
373: PetscInt timestepjustdecreased;
374: PetscReal dt_eval_times_cached; /* time step before hitting a TS evaluation time point */
375: };
377: typedef struct _p_DMTS *DMTS;
378: typedef struct _DMTSOps *DMTSOps;
379: struct _DMTSOps {
380: TSRHSFunctionFn *rhsfunction;
381: TSRHSJacobianFn *rhsjacobian;
383: TSIFunctionFn *ifunction;
384: PetscErrorCode (*ifunctionview)(void *, PetscViewer);
385: PetscErrorCode (*ifunctionload)(void **, PetscViewer);
387: TSIJacobianFn *ijacobian;
388: PetscErrorCode (*ijacobianview)(void *, PetscViewer);
389: PetscErrorCode (*ijacobianload)(void **, PetscViewer);
391: TSI2FunctionFn *i2function;
392: TSI2JacobianFn *i2jacobian;
394: TSTransientVariableFn *transientvar;
396: TSSolutionFn *solution;
397: TSForcingFn *forcing;
399: PetscErrorCode (*destroy)(DMTS);
400: PetscErrorCode (*duplicate)(DMTS, DMTS);
401: };
403: /*S
404: DMTS - Object held by a `DM` that contains all the callback functions and their contexts needed by a `TS`
406: Level: developer
408: Notes:
409: Users provide callback functions and their contexts to `TS` using, for example, `TSSetIFunction()`. These values are stored
410: in a `DMTS` that is contained in the `DM` associated with the `TS`. If no `DM` was provided by
411: the user with `TSSetDM()` it is automatically created by `TSGetDM()` with `DMShellCreate()`.
413: Users very rarely need to worked directly with the `DMTS` object, rather they work with the `TS` and the `DM` they created
415: Multiple `DM` can share a single `DMTS`, often each `DM` is associated with
416: a grid refinement level. `DMGetDMTS()` returns the `DMTS` associated with a `DM`. `DMGetDMTSWrite()` returns a unique
417: `DMTS` that is only associated with the current `DM`, making a copy of the shared `DMTS` if needed (copy-on-write).
419: See `DMKSP` for details on why there is a needed for `DMTS` instead of simply storing the user callbacks directly in the `DM` or the `TS`
421: Developer Note:
422: The original `dm` inside the `DMTS` is NOT reference counted (to prevent a reference count loop between a `DM` and a `DMSNES`).
423: The `DM` on which this context was first created is cached here to implement one-way
424: copy-on-write. When `DMGetDMTSWrite()` sees a request using a different `DM`, it makes a copy of the `DMTS`.
426: .seealso: [](ch_ts), `TSCreate()`, `DM`, `DMGetDMTSWrite()`, `DMGetDMTS()`, `TSSetIFunction()`, `DMTSSetRHSFunctionContextDestroy()`,
427: `DMTSSetRHSJacobian()`, `DMTSGetRHSJacobian()`, `DMTSSetRHSJacobianContextDestroy()`, `DMTSSetIFunction()`, `DMTSGetIFunction()`,
428: `DMTSSetIFunctionContextDestroy()`, `DMTSSetIJacobian()`, `DMTSGetIJacobian()`, `DMTSSetIJacobianContextDestroy()`,
429: `DMTSSetI2Function()`, `DMTSGetI2Function()`, `DMTSSetI2FunctionContextDestroy()`, `DMTSSetI2Jacobian()`,
430: `DMTSGetI2Jacobian()`, `DMTSSetI2JacobianContextDestroy()`, `DMKSP`, `DMSNES`
431: S*/
432: struct _p_DMTS {
433: PETSCHEADER(struct _DMTSOps);
434: PetscContainer rhsfunctionctxcontainer;
435: PetscContainer rhsjacobianctxcontainer;
437: PetscContainer ifunctionctxcontainer;
438: PetscContainer ijacobianctxcontainer;
440: PetscContainer i2functionctxcontainer;
441: PetscContainer i2jacobianctxcontainer;
443: void *transientvarctx;
445: void *solutionctx;
446: void *forcingctx;
448: void *data;
450: /* See the developer note for DMTS above */
451: DM originaldm;
452: };
454: PETSC_INTERN PetscErrorCode DMTSUnsetRHSFunctionContext_Internal(DM);
455: PETSC_INTERN PetscErrorCode DMTSUnsetRHSJacobianContext_Internal(DM);
456: PETSC_INTERN PetscErrorCode DMTSUnsetIFunctionContext_Internal(DM);
457: PETSC_INTERN PetscErrorCode DMTSUnsetIJacobianContext_Internal(DM);
458: PETSC_INTERN PetscErrorCode DMTSUnsetI2FunctionContext_Internal(DM);
459: PETSC_INTERN PetscErrorCode DMTSUnsetI2JacobianContext_Internal(DM);
461: PETSC_EXTERN PetscErrorCode DMGetDMTS(DM, DMTS *);
462: PETSC_EXTERN PetscErrorCode DMGetDMTSWrite(DM, DMTS *);
463: PETSC_EXTERN PetscErrorCode DMCopyDMTS(DM, DM);
464: PETSC_EXTERN PetscErrorCode DMTSView(DMTS, PetscViewer);
465: PETSC_EXTERN PetscErrorCode DMTSLoad(DMTS, PetscViewer);
466: PETSC_EXTERN PetscErrorCode DMTSCopy(DMTS, DMTS);
468: struct _n_TSEvent {
469: PetscReal *fvalue_prev; /* value of indicator function at the left end-point of the event interval */
470: PetscReal *fvalue; /* value of indicator function at the current point */
471: PetscReal *fvalue_right; /* value of indicator function at the right end-point of the event interval */
472: PetscInt *fsign_prev; /* sign of indicator function at the left end-point of the event interval */
473: PetscInt *fsign; /* sign of indicator function at the current point */
474: PetscInt *fsign_right; /* sign of indicator function at the right end-point of the event interval */
475: PetscReal ptime_prev; /* time at the previous point (left end-point of the event interval) */
476: PetscReal ptime_right; /* time at the right end-point of the event interval */
477: PetscReal ptime_cache; /* the point visited by the TS before the event interval was detected; cached - to reuse if necessary */
478: PetscReal timestep_cache; /* time step considered by the TS before the event interval was detected; cached - to reuse if necessary */
479: PetscInt *side; /* upon bracket subdivision, indicates which sub-bracket is taken further, -1 -> left one, +1 -> right one, +2 -> neither, 0 -> zero-crossing located */
480: PetscInt *side_prev; /* counts the repeating previous side's (with values: -n <=> '-1'*n; +n <=> '+1'*n); used in the Anderson-Bjorck iteration */
481: PetscReal timestep_postevent; /* first time step after the event; can be PETSC_DECIDE */
482: PetscReal timestep_2nd_postevent; /* second time step after the event; can be PETSC_DECIDE */
483: PetscReal timestep_min; /* minimum time step */
484: PetscBool *justrefined_AB; /* this flag shows if the given indicator function i = [0..nevents) participated in Anderson-Bjorck process in the last iteration of TSEventHandler() */
485: PetscReal *gamma_AB; /* cumulative scaling factor for the Anderson-Bjorck iteration */
486: PetscErrorCode (*indicator)(TS, PetscReal, Vec, PetscReal *, void *); /* this callback defines the user function(s) whose sign changes indicate events */
487: PetscErrorCode (*postevent)(TS, PetscInt, PetscInt[], PetscReal, Vec, PetscBool, void *); /* user post-event callback */
488: void *ctx; /* user context for indicator and postevent callbacks */
489: PetscInt *direction; /* zero crossing direction to trigger the event: +1 -> going positive, -1 -> going negative, 0 -> any */
490: PetscBool *terminate; /* 1 -> terminate time stepping on event location, 0 -> continue */
491: PetscInt nevents; /* number of events (indicator functions) to handle on the current MPI process */
492: PetscInt nevents_zero; /* number of events triggered */
493: PetscInt *events_zero; /* list of the events triggered */
494: PetscReal *vtol; /* array of tolerances for the indicator function zero check */
495: PetscInt iterctr; /* iteration counter: used both for reporting and as a status indicator */
496: PetscBool processing; /* this flag shows if the event-resolving iterations are in progress, or the post-event dt handling is in progress */
497: PetscBool revisit_right; /* [sync] "revisit the bracket's right end", if true, then fvalue(s) are not calculated, but are taken from fvalue_right(s) */
498: PetscViewer monitor;
499: /* Struct to record the events */
500: struct {
501: PetscInt ctr; /* Recorder counter */
502: PetscReal *time; /* Event times */
503: PetscInt *stepnum; /* Step numbers */
504: PetscInt *nevents; /* Number of events occurring at the event times */
505: PetscInt **eventidx; /* Local indices of the events in the event list */
506: } recorder;
507: PetscInt recsize; /* Size of recorder stack */
508: PetscInt refct; /* Reference count */
509: };
511: PETSC_EXTERN PetscErrorCode TSEventInitialize(TSEvent, TS, PetscReal, Vec);
512: PETSC_EXTERN PetscErrorCode TSEventDestroy(TSEvent *);
513: PETSC_EXTERN PetscErrorCode TSEventHandler(TS);
514: PETSC_EXTERN PetscErrorCode TSAdjointEventHandler(TS);
516: PETSC_EXTERN PetscLogEvent TS_AdjointStep;
517: PETSC_EXTERN PetscLogEvent TS_Step;
518: PETSC_EXTERN PetscLogEvent TS_PseudoComputeTimeStep;
519: PETSC_EXTERN PetscLogEvent TS_FunctionEval;
520: PETSC_EXTERN PetscLogEvent TS_JacobianEval;
521: PETSC_EXTERN PetscLogEvent TS_ForwardStep;
523: typedef enum {
524: TS_STEP_INCOMPLETE, /* vec_sol, ptime, etc point to beginning of step */
525: TS_STEP_PENDING, /* vec_sol advanced, but step has not been accepted yet */
526: TS_STEP_COMPLETE /* step accepted and ptime, steps, etc have been advanced */
527: } TSStepStatus;
529: struct _n_TSMonitorLGCtx {
530: PetscDrawLG lg;
531: PetscBool semilogy;
532: PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */
533: PetscInt ksp_its, snes_its;
534: char **names;
535: char **displaynames;
536: PetscInt ndisplayvariables;
537: PetscInt *displayvariables;
538: PetscReal *displayvalues;
539: PetscErrorCode (*transform)(void *, Vec, Vec *);
540: PetscCtxDestroyFn *transformdestroy;
541: void *transformctx;
542: };
544: struct _n_TSMonitorSPCtx {
545: PetscDrawSP sp;
546: PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */
547: PetscInt retain; /* Retain n points plotted to show trajectories, or -1 for all points */
548: PetscBool phase; /* Plot in phase space rather than coordinate space */
549: PetscBool multispecies; /* Change scatter point color based on species */
550: PetscInt ksp_its, snes_its;
551: };
553: struct _n_TSMonitorHGCtx {
554: PetscDrawHG *hg;
555: PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */
556: PetscInt Ns; /* The number of species to histogram */
557: PetscBool velocity; /* Plot in velocity space rather than coordinate space */
558: };
560: struct _n_TSMonitorEnvelopeCtx {
561: Vec max, min;
562: };
564: /*
565: Checks if the user provide a TSSetIFunction() but an explicit method is called; generate an error in that case
566: */
567: static inline PetscErrorCode TSCheckImplicitTerm(TS ts)
568: {
569: TSIFunctionFn *ifunction;
570: DM dm;
572: PetscFunctionBegin;
573: PetscCall(TSGetDM(ts, &dm));
574: PetscCall(DMTSGetIFunction(dm, &ifunction, NULL));
575: PetscCheck(!ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_INCOMP, "You are attempting to use an explicit ODE integrator but provided an implicit function definition with TSSetIFunction()");
576: PetscFunctionReturn(PETSC_SUCCESS);
577: }
579: PETSC_INTERN PetscErrorCode TSGetRHSMats_Private(TS, Mat *, Mat *);
580: /* this is declared here as TSHistory is not public */
581: PETSC_EXTERN PetscErrorCode TSAdaptHistorySetTSHistory(TSAdapt, TSHistory, PetscBool);
583: PETSC_INTERN PetscErrorCode TSTrajectoryReconstruct_Private(TSTrajectory, TS, PetscReal, Vec, Vec);
584: PETSC_INTERN PetscErrorCode TSTrajectorySetUp_Basic(TSTrajectory, TS);
586: PETSC_EXTERN PetscLogEvent TSTrajectory_Set;
587: PETSC_EXTERN PetscLogEvent TSTrajectory_Get;
588: PETSC_EXTERN PetscLogEvent TSTrajectory_GetVecs;
589: PETSC_EXTERN PetscLogEvent TSTrajectory_SetUp;
590: PETSC_EXTERN PetscLogEvent TSTrajectory_DiskWrite;
591: PETSC_EXTERN PetscLogEvent TSTrajectory_DiskRead;
593: struct _n_TSMonitorDrawCtx {
594: PetscViewer viewer;
595: Vec initialsolution;
596: PetscBool showinitial;
597: PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */
598: PetscBool showtimestepandtime;
599: };
601: struct _n_TSMonitorVTKCtx {
602: char *filenametemplate;
603: PetscInt interval; /* when > 0 uses step % interval, when negative only final solution plotted */
604: };
606: struct _n_TSMonitorSolutionCtx {
607: PetscBool skip_initial; // Skip the viewer the first time TSMonitorSolution is run (within a single call to `TSSolve()`)
608: };