Actual source code: traj.c
1: #include <petsc/private/tsimpl.h>
2: #include <petsc/private/tshistoryimpl.h>
3: #include <petscdm.h>
5: PetscFunctionList TSTrajectoryList = NULL;
6: PetscBool TSTrajectoryRegisterAllCalled = PETSC_FALSE;
7: PetscClassId TSTRAJECTORY_CLASSID;
8: PetscLogEvent TSTrajectory_Set, TSTrajectory_Get, TSTrajectory_GetVecs, TSTrajectory_SetUp;
10: /*@
11: TSTrajectoryRegister - Adds a way of storing trajectories to the `TS` package
13: Not Collective, No Fortran Support
15: Input Parameters:
16: + sname - the name of a new user-defined creation routine
17: - function - the creation routine itself
19: Level: developer
21: Note:
22: `TSTrajectoryRegister()` may be called multiple times to add several user-defined tses.
24: .seealso: [](ch_ts), `TSTrajectoryRegisterAll()`
25: @*/
26: PetscErrorCode TSTrajectoryRegister(const char sname[], PetscErrorCode (*function)(TSTrajectory, TS))
27: {
28: PetscFunctionBegin;
29: PetscCall(PetscFunctionListAdd(&TSTrajectoryList, sname, function));
30: PetscFunctionReturn(PETSC_SUCCESS);
31: }
33: /*@
34: TSTrajectorySet - Sets a vector of state in the trajectory object
36: Collective
38: Input Parameters:
39: + tj - the trajectory object
40: . ts - the time stepper object (optional)
41: . stepnum - the step number
42: . time - the current time
43: - X - the current solution
45: Level: developer
47: Note:
48: Usually one does not call this routine, it is called automatically during `TSSolve()`
50: .seealso: [](ch_ts), `TSTrajectorySetUp()`, `TSTrajectoryDestroy()`, `TSTrajectorySetType()`, `TSTrajectorySetVariableNames()`, `TSGetTrajectory()`, `TSTrajectoryGet()`, `TSTrajectoryGetVecs()`
51: @*/
52: PetscErrorCode TSTrajectorySet(TSTrajectory tj, TS ts, PetscInt stepnum, PetscReal time, Vec X)
53: {
54: PetscFunctionBegin;
55: if (!tj) PetscFunctionReturn(PETSC_SUCCESS);
61: PetscCheck(tj->setupcalled, PetscObjectComm((PetscObject)tj), PETSC_ERR_ORDER, "TSTrajectorySetUp should be called first");
62: if (tj->monitor) PetscCall(PetscViewerASCIIPrintf(tj->monitor, "TSTrajectorySet: stepnum %" PetscInt_FMT ", time %g (stages %" PetscInt_FMT ")\n", stepnum, (double)time, (PetscInt)!tj->solution_only));
63: PetscCall(PetscLogEventBegin(TSTrajectory_Set, tj, ts, 0, 0));
64: PetscUseTypeMethod(tj, set, ts, stepnum, time, X);
65: PetscCall(PetscLogEventEnd(TSTrajectory_Set, tj, ts, 0, 0));
66: if (tj->usehistory) PetscCall(TSHistoryUpdate(tj->tsh, stepnum, time));
67: if (tj->lag.caching) tj->lag.Udotcached.time = PETSC_MIN_REAL;
68: PetscFunctionReturn(PETSC_SUCCESS);
69: }
71: /*
72: TSTrajectoryResetHistory_Private - Discards the step history and the cached interpolation data recorded by a previous TSSolve(),
73: so that a new TSSolve() starting from step 0 can reuse the trajectory
74: */
75: PetscErrorCode TSTrajectoryResetHistory_Private(TSTrajectory tj)
76: {
77: PetscFunctionBegin;
78: if (!tj) PetscFunctionReturn(PETSC_SUCCESS);
79: PetscCall(TSHistoryDestroy(&tj->tsh));
80: PetscCall(TSHistoryCreate(PetscObjectComm((PetscObject)tj), &tj->tsh));
81: if (tj->lag.T)
82: for (PetscInt i = 0; i <= tj->lag.order; i++) tj->lag.T[i] = PETSC_MAX_REAL;
83: tj->lag.Ucached.time = PETSC_MIN_REAL;
84: tj->lag.Ucached.step = PETSC_INT_MAX;
85: tj->lag.Udotcached.time = PETSC_MIN_REAL;
86: tj->lag.Udotcached.step = PETSC_INT_MAX;
87: PetscFunctionReturn(PETSC_SUCCESS);
88: }
90: /*@
91: TSTrajectoryGetNumSteps - Return the number of steps registered in the `TSTrajectory` via `TSTrajectorySet()`.
93: Not Collective.
95: Input Parameter:
96: . tj - the trajectory object
98: Output Parameter:
99: . steps - the number of steps
101: Level: developer
103: .seealso: [](ch_ts), `TS`, `TSTrajectorySet()`
104: @*/
105: PetscErrorCode TSTrajectoryGetNumSteps(TSTrajectory tj, PetscInt *steps)
106: {
107: PetscFunctionBegin;
109: PetscAssertPointer(steps, 2);
110: PetscCall(TSHistoryGetNumSteps(tj->tsh, steps));
111: PetscFunctionReturn(PETSC_SUCCESS);
112: }
114: /*@
115: TSTrajectoryGet - Updates the solution vector of a time stepper object by querying the `TSTrajectory`
117: Collective
119: Input Parameters:
120: + tj - the trajectory object
121: . ts - the time stepper object
122: - stepnum - the step number
124: Output Parameter:
125: . time - the time associated with the step number
127: Level: developer
129: Note:
130: Usually one does not call this routine, it is called automatically during `TSSolve()`
132: .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSTrajectorySetUp()`, `TSTrajectoryDestroy()`, `TSTrajectorySetType()`, `TSTrajectorySetVariableNames()`, `TSGetTrajectory()`, `TSTrajectorySet()`, `TSTrajectoryGetVecs()`, `TSGetSolution()`
133: @*/
134: PetscErrorCode TSTrajectoryGet(TSTrajectory tj, TS ts, PetscInt stepnum, PetscReal *time)
135: {
136: PetscFunctionBegin;
137: PetscCheck(tj, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TS solver did not save trajectory");
141: PetscAssertPointer(time, 4);
142: PetscCheck(tj->setupcalled, PetscObjectComm((PetscObject)tj), PETSC_ERR_ORDER, "TSTrajectorySetUp should be called first");
143: PetscCheck(stepnum >= 0, PetscObjectComm((PetscObject)tj), PETSC_ERR_PLIB, "Requesting negative step number");
144: if (tj->monitor) {
145: PetscCall(PetscViewerASCIIPrintf(tj->monitor, "TSTrajectoryGet: stepnum %" PetscInt_FMT ", stages %" PetscInt_FMT "\n", stepnum, (PetscInt)!tj->solution_only));
146: PetscCall(PetscViewerFlush(tj->monitor));
147: }
148: PetscCall(PetscLogEventBegin(TSTrajectory_Get, tj, ts, 0, 0));
149: PetscUseTypeMethod(tj, get, ts, stepnum, time);
150: PetscCall(PetscLogEventEnd(TSTrajectory_Get, tj, ts, 0, 0));
151: PetscFunctionReturn(PETSC_SUCCESS);
152: }
154: /*@
155: TSTrajectoryGetVecs - Reconstructs the vector of state and its time derivative using information from the `TSTrajectory` and, possibly, from the `TS`
157: Collective
159: Input Parameters:
160: + tj - the trajectory object
161: . ts - the time stepper object (optional)
162: - stepnum - the requested step number
164: Output Parameters:
165: + time - On input time for the step if step number is `PETSC_DECIDE`, on output the time associated with the step number
166: . U - state vector (can be `NULL`)
167: - Udot - time derivative of state vector (can be `NULL`)
169: Level: developer
171: Notes:
172: If the step number is `PETSC_DECIDE`, the time argument is used to inquire the trajectory.
173: If the requested time does not match any in the trajectory, Lagrangian interpolations are returned.
175: .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSTrajectorySetUp()`, `TSTrajectoryDestroy()`, `TSTrajectorySetType()`, `TSTrajectorySetVariableNames()`, `TSGetTrajectory()`, `TSTrajectorySet()`, `TSTrajectoryGet()`
176: @*/
177: PetscErrorCode TSTrajectoryGetVecs(TSTrajectory tj, TS ts, PetscInt stepnum, PetscReal *time, Vec U, Vec Udot)
178: {
179: PetscFunctionBegin;
180: PetscCheck(tj, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TS solver did not save trajectory");
184: PetscAssertPointer(time, 4);
187: if (!U && !Udot) PetscFunctionReturn(PETSC_SUCCESS);
188: PetscCheck(tj->setupcalled, PetscObjectComm((PetscObject)tj), PETSC_ERR_ORDER, "TSTrajectorySetUp should be called first");
189: PetscCall(PetscLogEventBegin(TSTrajectory_GetVecs, tj, ts, 0, 0));
190: if (tj->monitor) {
191: PetscInt pU, pUdot;
192: pU = U ? 1 : 0;
193: pUdot = Udot ? 1 : 0;
194: PetscCall(PetscViewerASCIIPrintf(tj->monitor, "Requested by GetVecs %" PetscInt_FMT " %" PetscInt_FMT ": stepnum %" PetscInt_FMT ", time %g\n", pU, pUdot, stepnum, (double)*time));
195: PetscCall(PetscViewerFlush(tj->monitor));
196: }
197: if (U && tj->lag.caching) {
198: PetscObjectId id;
199: PetscObjectState state;
201: PetscCall(PetscObjectStateGet((PetscObject)U, &state));
202: PetscCall(PetscObjectGetId((PetscObject)U, &id));
203: if (stepnum == PETSC_DECIDE) {
204: if (id == tj->lag.Ucached.id && *time == tj->lag.Ucached.time && state == tj->lag.Ucached.state) U = NULL;
205: } else {
206: if (id == tj->lag.Ucached.id && stepnum == tj->lag.Ucached.step && state == tj->lag.Ucached.state) U = NULL;
207: }
208: if (tj->monitor && !U) {
209: PetscCall(PetscViewerASCIIPushTab(tj->monitor));
210: PetscCall(PetscViewerASCIIPrintf(tj->monitor, "State vector cached\n"));
211: PetscCall(PetscViewerASCIIPopTab(tj->monitor));
212: PetscCall(PetscViewerFlush(tj->monitor));
213: }
214: }
215: if (Udot && tj->lag.caching) {
216: PetscObjectId id;
217: PetscObjectState state;
219: PetscCall(PetscObjectStateGet((PetscObject)Udot, &state));
220: PetscCall(PetscObjectGetId((PetscObject)Udot, &id));
221: if (stepnum == PETSC_DECIDE) {
222: if (id == tj->lag.Udotcached.id && *time == tj->lag.Udotcached.time && state == tj->lag.Udotcached.state) Udot = NULL;
223: } else {
224: if (id == tj->lag.Udotcached.id && stepnum == tj->lag.Udotcached.step && state == tj->lag.Udotcached.state) Udot = NULL;
225: }
226: if (tj->monitor && !Udot) {
227: PetscCall(PetscViewerASCIIPushTab(tj->monitor));
228: PetscCall(PetscViewerASCIIPrintf(tj->monitor, "Derivative vector cached\n"));
229: PetscCall(PetscViewerASCIIPopTab(tj->monitor));
230: PetscCall(PetscViewerFlush(tj->monitor));
231: }
232: }
233: if (!U && !Udot) {
234: PetscCall(PetscLogEventEnd(TSTrajectory_GetVecs, tj, ts, 0, 0));
235: PetscFunctionReturn(PETSC_SUCCESS);
236: }
238: if (stepnum == PETSC_DECIDE || Udot) { /* reverse search for requested time in TSHistory */
239: if (tj->monitor) PetscCall(PetscViewerASCIIPushTab(tj->monitor));
240: /* cached states will be updated in the function */
241: PetscCall(TSTrajectoryReconstruct_Private(tj, ts, *time, U, Udot));
242: if (tj->monitor) {
243: PetscCall(PetscViewerASCIIPopTab(tj->monitor));
244: PetscCall(PetscViewerFlush(tj->monitor));
245: }
246: } else if (U) { /* we were asked to load from stepnum, use TSTrajectoryGet */
247: TS fakets = ts;
248: Vec U2;
250: /* use a fake TS if ts is missing */
251: if (!ts) {
252: PetscCall(PetscObjectQuery((PetscObject)tj, "__fake_ts", (PetscObject *)&fakets));
253: if (!fakets) {
254: PetscCall(TSCreate(PetscObjectComm((PetscObject)tj), &fakets));
255: PetscCall(PetscObjectCompose((PetscObject)tj, "__fake_ts", (PetscObject)fakets));
256: PetscCall(PetscObjectDereference((PetscObject)fakets));
257: PetscCall(VecDuplicate(U, &U2));
258: PetscCall(TSSetSolution(fakets, U2));
259: PetscCall(PetscObjectDereference((PetscObject)U2));
260: }
261: }
262: PetscCall(TSTrajectoryGet(tj, fakets, stepnum, time));
263: PetscCall(TSGetSolution(fakets, &U2));
264: PetscCall(VecCopy(U2, U));
265: PetscCall(PetscObjectStateGet((PetscObject)U, &tj->lag.Ucached.state));
266: PetscCall(PetscObjectGetId((PetscObject)U, &tj->lag.Ucached.id));
267: tj->lag.Ucached.time = *time;
268: tj->lag.Ucached.step = stepnum;
269: }
270: PetscCall(PetscLogEventEnd(TSTrajectory_GetVecs, tj, ts, 0, 0));
271: PetscFunctionReturn(PETSC_SUCCESS);
272: }
274: /*@
275: TSTrajectoryViewFromOptions - View a `TSTrajectory` based on values in the options database
277: Collective
279: Input Parameters:
280: + A - the `TSTrajectory` context
281: . obj - optional object that provides prefix used for option name, pass `NULL` to use the options prefix of `A`
282: - name - command line option
284: Options Database Key:
285: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`
287: Level: intermediate
289: Note:
290: This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
291: rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.
293: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryView()`, `PetscObjectViewFromOptions()`, `TSTrajectoryCreate()`, `PetscOptionsCreateViewer()`
294: @*/
295: PetscErrorCode TSTrajectoryViewFromOptions(TSTrajectory A, PetscObject obj, const char name[])
296: {
297: PetscFunctionBegin;
299: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
300: PetscFunctionReturn(PETSC_SUCCESS);
301: }
303: /*@
304: TSTrajectoryView - Prints information about the trajectory object
306: Collective
308: Input Parameters:
309: + tj - the `TSTrajectory` context obtained from `TSTrajectoryCreate()`
310: - viewer - visualization context
312: Options Database Key:
313: . -ts_trajectory_view - calls `TSTrajectoryView()` at end of `TSAdjointStep()`
315: Level: developer
317: Notes:
318: The available visualization contexts include
319: + `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
320: - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
321: output where only the first processor opens
322: the file. All other processors send their
323: data to the first processor to print.
325: The user can open an alternative visualization context with
326: `PetscViewerASCIIOpen()` - output to a specified file.
328: .seealso: [](ch_ts), `TS`, `TSTrajectory`, `PetscViewer`, `PetscViewerASCIIOpen()`
329: @*/
330: PetscErrorCode TSTrajectoryView(TSTrajectory tj, PetscViewer viewer)
331: {
332: PetscBool isascii;
334: PetscFunctionBegin;
336: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)tj), &viewer));
338: PetscCheckSameComm(tj, 1, viewer, 2);
340: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
341: if (isascii) {
342: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tj, viewer));
343: PetscCall(PetscViewerASCIIPrintf(viewer, " total number of recomputations for adjoint calculation = %" PetscInt_FMT "\n", tj->recomps));
344: PetscCall(PetscViewerASCIIPrintf(viewer, " disk checkpoint reads = %" PetscInt_FMT "\n", tj->diskreads));
345: PetscCall(PetscViewerASCIIPrintf(viewer, " disk checkpoint writes = %" PetscInt_FMT "\n", tj->diskwrites));
346: PetscCall(PetscViewerASCIIPushTab(viewer));
347: PetscTryTypeMethod(tj, view, viewer);
348: PetscCall(PetscViewerASCIIPopTab(viewer));
349: }
350: PetscFunctionReturn(PETSC_SUCCESS);
351: }
353: /*@
354: TSTrajectorySetVariableNames - Sets the name of each component in the solution vector so that it may be saved with the trajectory
356: Collective
358: Input Parameters:
359: + ctx - the trajectory context
360: - names - the names of the components, final string must be `NULL`
362: Level: intermediate
364: Fortran Notes:
365: Fortran interface is not possible because of the string array argument
367: .seealso: [](ch_ts), `TSTrajectory`, `TSGetTrajectory()`
368: @*/
369: PetscErrorCode TSTrajectorySetVariableNames(TSTrajectory ctx, const char *const *names)
370: {
371: PetscFunctionBegin;
373: PetscAssertPointer(names, 2);
374: PetscCall(PetscStrArrayDestroy(&ctx->names));
375: PetscCall(PetscStrArrayallocpy(names, &ctx->names));
376: PetscFunctionReturn(PETSC_SUCCESS);
377: }
379: /*@
380: TSTrajectorySetTransform - Solution vector will be transformed by provided function before being saved to disk
382: Collective
384: Input Parameters:
385: + tj - the `TSTrajectory` context
386: . transform - the transform function
387: . destroy - function to destroy the optional context
388: - tctx - optional context used by transform function
390: Level: intermediate
392: .seealso: [](ch_ts), `TSTrajectorySetVariableNames()`, `TSTrajectory`, `TSMonitorLGSetTransform()`
393: @*/
394: PetscErrorCode TSTrajectorySetTransform(TSTrajectory tj, PetscErrorCode (*transform)(void *, Vec, Vec *), PetscCtxDestroyFn *destroy, void *tctx)
395: {
396: PetscFunctionBegin;
398: tj->transform = transform;
399: tj->transformdestroy = destroy;
400: tj->transformctx = tctx;
401: PetscFunctionReturn(PETSC_SUCCESS);
402: }
404: /*@
405: TSTrajectoryCreate - This function creates an empty trajectory object used to store the time dependent solution of an ODE/DAE
407: Collective
409: Input Parameter:
410: . comm - the communicator
412: Output Parameter:
413: . tj - the trajectory object
415: Level: developer
417: Notes:
418: Usually one does not call this routine, it is called automatically when one calls `TSSetSaveTrajectory()`.
420: .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSTrajectorySetUp()`, `TSTrajectoryDestroy()`, `TSTrajectorySetType()`, `TSTrajectorySetVariableNames()`, `TSGetTrajectory()`, `TSTrajectorySetKeepFiles()`
421: @*/
422: PetscErrorCode TSTrajectoryCreate(MPI_Comm comm, TSTrajectory *tj)
423: {
424: TSTrajectory t;
426: PetscFunctionBegin;
427: PetscAssertPointer(tj, 2);
428: PetscCall(TSInitializePackage());
430: PetscCall(PetscHeaderCreate(t, TSTRAJECTORY_CLASSID, "TSTrajectory", "Time stepping", "TS", comm, TSTrajectoryDestroy, TSTrajectoryView));
431: t->setupcalled = PETSC_FALSE;
432: PetscCall(TSHistoryCreate(comm, &t->tsh));
433: t->lag.order = 1;
434: t->lag.L = NULL;
435: t->lag.T = NULL;
436: t->lag.W = NULL;
437: t->lag.WW = NULL;
438: t->lag.TW = NULL;
439: t->lag.TT = NULL;
440: t->lag.caching = PETSC_TRUE;
441: t->lag.Ucached.id = 0;
442: t->lag.Ucached.state = -1;
443: t->lag.Ucached.time = PETSC_MIN_REAL;
444: t->lag.Ucached.step = PETSC_INT_MAX;
445: t->lag.Udotcached.id = 0;
446: t->lag.Udotcached.state = -1;
447: t->lag.Udotcached.time = PETSC_MIN_REAL;
448: t->lag.Udotcached.step = PETSC_INT_MAX;
449: t->adjoint_solve_mode = PETSC_TRUE;
450: t->solution_only = PETSC_FALSE;
451: t->keepfiles = PETSC_FALSE;
452: t->usehistory = PETSC_TRUE;
453: *tj = t;
454: PetscCall(TSTrajectorySetFiletemplate(t, "TS-%06" PetscInt_FMT ".bin"));
455: PetscFunctionReturn(PETSC_SUCCESS);
456: }
458: /*@
459: TSTrajectorySetType - Sets the storage method to be used as in a trajectory
461: Collective
463: Input Parameters:
464: + tj - the `TSTrajectory` context
465: . ts - the `TS` context
466: - type - a known method
468: Options Database Key:
469: . -ts_trajectory_type (basic|singlefile|memory|visualization) - Sets the trajectory type
471: Level: developer
473: Developer Notes:
474: Why does this option require access to the `TS`
476: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryType`, `TS`, `TSTrajectoryCreate()`, `TSTrajectorySetFromOptions()`, `TSTrajectoryDestroy()`, `TSTrajectoryGetType()`
477: @*/
478: PetscErrorCode TSTrajectorySetType(TSTrajectory tj, TS ts, TSTrajectoryType type)
479: {
480: PetscErrorCode (*r)(TSTrajectory, TS);
481: PetscBool match;
483: PetscFunctionBegin;
485: PetscCall(PetscObjectTypeCompare((PetscObject)tj, type, &match));
486: if (match) PetscFunctionReturn(PETSC_SUCCESS);
488: PetscCall(PetscFunctionListFind(TSTrajectoryList, type, &r));
489: PetscCheck(r, PetscObjectComm((PetscObject)tj), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TSTrajectory type: %s", type);
490: PetscTryTypeMethod(tj, destroy);
491: tj->ops->destroy = NULL;
492: PetscCall(PetscMemzero(tj->ops, sizeof(*tj->ops)));
494: PetscCall(PetscObjectChangeTypeName((PetscObject)tj, type));
495: PetscCall((*r)(tj, ts));
496: PetscFunctionReturn(PETSC_SUCCESS);
497: }
499: /*@
500: TSTrajectoryGetType - Gets the trajectory type
502: Collective
504: Input Parameters:
505: + tj - the `TSTrajectory` context
506: - ts - the `TS` context
508: Output Parameter:
509: . type - a known method
511: Level: developer
513: .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSTrajectoryCreate()`, `TSTrajectorySetFromOptions()`, `TSTrajectoryDestroy()`, `TSTrajectorySetType()`
514: @*/
515: PetscErrorCode TSTrajectoryGetType(TSTrajectory tj, TS ts, TSTrajectoryType *type)
516: {
517: PetscFunctionBegin;
519: if (type) *type = ((PetscObject)tj)->type_name;
520: PetscFunctionReturn(PETSC_SUCCESS);
521: }
523: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory, TS);
524: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Singlefile(TSTrajectory, TS);
525: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Memory(TSTrajectory, TS);
526: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Visualization(TSTrajectory, TS);
528: /*@
529: TSTrajectoryRegisterAll - Registers all of the `TSTrajectory` storage schecmes in the `TS` package.
531: Not Collective
533: Level: developer
535: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryRegister()`
536: @*/
537: PetscErrorCode TSTrajectoryRegisterAll(void)
538: {
539: PetscFunctionBegin;
540: if (TSTrajectoryRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
541: TSTrajectoryRegisterAllCalled = PETSC_TRUE;
543: PetscCall(TSTrajectoryRegister(TSTRAJECTORYBASIC, TSTrajectoryCreate_Basic));
544: PetscCall(TSTrajectoryRegister(TSTRAJECTORYSINGLEFILE, TSTrajectoryCreate_Singlefile));
545: PetscCall(TSTrajectoryRegister(TSTRAJECTORYMEMORY, TSTrajectoryCreate_Memory));
546: PetscCall(TSTrajectoryRegister(TSTRAJECTORYVISUALIZATION, TSTrajectoryCreate_Visualization));
547: PetscFunctionReturn(PETSC_SUCCESS);
548: }
550: /*@
551: TSTrajectoryReset - Resets a trajectory context
553: Collective
555: Input Parameter:
556: . tj - the `TSTrajectory` context obtained from `TSGetTrajectory()`
558: Level: developer
560: .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSTrajectoryCreate()`, `TSTrajectorySetUp()`
561: @*/
562: PetscErrorCode TSTrajectoryReset(TSTrajectory tj)
563: {
564: PetscFunctionBegin;
565: if (!tj) PetscFunctionReturn(PETSC_SUCCESS);
567: PetscTryTypeMethod(tj, reset);
568: PetscCall(PetscFree(tj->dirfiletemplate));
569: PetscCall(TSHistoryDestroy(&tj->tsh));
570: PetscCall(TSHistoryCreate(PetscObjectComm((PetscObject)tj), &tj->tsh));
571: tj->setupcalled = PETSC_FALSE;
572: PetscFunctionReturn(PETSC_SUCCESS);
573: }
575: /*@
576: TSTrajectoryDestroy - Destroys a trajectory context
578: Collective
580: Input Parameter:
581: . tj - the `TSTrajectory` context obtained from `TSTrajectoryCreate()`
583: Level: developer
585: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryCreate()`, `TSTrajectorySetUp()`
586: @*/
587: PetscErrorCode TSTrajectoryDestroy(TSTrajectory *tj)
588: {
589: PetscFunctionBegin;
590: if (!*tj) PetscFunctionReturn(PETSC_SUCCESS);
592: if (--((PetscObject)*tj)->refct > 0) {
593: *tj = NULL;
594: PetscFunctionReturn(PETSC_SUCCESS);
595: }
597: PetscCall(TSTrajectoryReset(*tj));
598: PetscCall(TSHistoryDestroy(&(*tj)->tsh));
599: PetscCall(VecDestroyVecs((*tj)->lag.order + 1, &(*tj)->lag.W));
600: PetscCall(PetscFree5((*tj)->lag.L, (*tj)->lag.T, (*tj)->lag.WW, (*tj)->lag.TT, (*tj)->lag.TW));
601: PetscCall(VecDestroy(&(*tj)->U));
602: PetscCall(VecDestroy(&(*tj)->Udot));
604: if ((*tj)->transformdestroy) PetscCall((*(*tj)->transformdestroy)(&(*tj)->transformctx));
605: PetscTryTypeMethod(*tj, destroy);
606: if (!(*tj)->keepfiles) {
607: PetscMPIInt rank;
608: MPI_Comm comm;
610: PetscCall(PetscObjectGetComm((PetscObject)*tj, &comm));
611: PetscCallMPI(MPI_Comm_rank(comm, &rank));
612: if (rank == 0 && (*tj)->dirname) { /* we own the directory, so we run PetscRMTree on it */
613: PetscCall(PetscRMTree((*tj)->dirname));
614: }
615: }
616: PetscCall(PetscStrArrayDestroy(&(*tj)->names));
617: PetscCall(PetscFree((*tj)->dirname));
618: PetscCall(PetscFree((*tj)->filetemplate));
619: PetscCall(PetscHeaderDestroy(tj));
620: PetscFunctionReturn(PETSC_SUCCESS);
621: }
623: /*
624: TSTrajectorySetTypeFromOptions_Private - Sets the type of `TSTrajectory` from user options.
626: Collective
628: Input Parameter:
629: + tj - the `TSTrajectory` context
630: - ts - the TS context
632: Options Database Key:
633: . -ts_trajectory_type (basic|memory|singlefile|visualization) - the method of storing the trajectory
635: Level: developer
637: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryType`, `TSTrajectorySetFromOptions()`, `TSTrajectorySetType()`
638: */
639: static PetscErrorCode TSTrajectorySetTypeFromOptions_Private(PetscOptionItems PetscOptionsObject, TSTrajectory tj, TS ts)
640: {
641: PetscBool opt;
642: const char *defaultType;
643: char typeName[256];
645: PetscFunctionBegin;
646: if (((PetscObject)tj)->type_name) defaultType = ((PetscObject)tj)->type_name;
647: else defaultType = TSTRAJECTORYBASIC;
649: PetscCall(TSTrajectoryRegisterAll());
650: PetscCall(PetscOptionsFList("-ts_trajectory_type", "TSTrajectory method", "TSTrajectorySetType", TSTrajectoryList, defaultType, typeName, sizeof(typeName), &opt));
651: if (opt) {
652: PetscCall(TSTrajectorySetType(tj, ts, typeName));
653: } else {
654: PetscCall(TSTrajectorySetType(tj, ts, defaultType));
655: }
656: PetscFunctionReturn(PETSC_SUCCESS);
657: }
659: /*@
660: TSTrajectorySetUseHistory - Use `TSHistory` in `TSTrajectory`
662: Collective
664: Input Parameters:
665: + tj - the `TSTrajectory` context
666: - flg - `PETSC_TRUE` to save, `PETSC_FALSE` to disable
668: Options Database Key:
669: . -ts_trajectory_use_history - have it use `TSHistory`
671: Level: advanced
673: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`, `TSTrajectorySetUp()`
674: @*/
675: PetscErrorCode TSTrajectorySetUseHistory(TSTrajectory tj, PetscBool flg)
676: {
677: PetscFunctionBegin;
680: tj->usehistory = flg;
681: PetscFunctionReturn(PETSC_SUCCESS);
682: }
684: /*@
685: TSTrajectorySetMonitor - Monitor the schedules generated by the `TSTrajectory` checkpointing controller
687: Collective
689: Input Parameters:
690: + tj - the `TSTrajectory` context
691: - flg - `PETSC_TRUE` to active a monitor, `PETSC_FALSE` to disable
693: Options Database Key:
694: . -ts_trajectory_monitor - print `TSTrajectory` information
696: Level: developer
698: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`, `TSTrajectorySetUp()`
699: @*/
700: PetscErrorCode TSTrajectorySetMonitor(TSTrajectory tj, PetscBool flg)
701: {
702: PetscFunctionBegin;
705: if (flg) tj->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)tj));
706: else tj->monitor = NULL;
707: PetscFunctionReturn(PETSC_SUCCESS);
708: }
710: /*@
711: TSTrajectorySetKeepFiles - Keep the files generated by the `TSTrajectory` once the program is done
713: Collective
715: Input Parameters:
716: + tj - the `TSTrajectory` context
717: - flg - `PETSC_TRUE` to save, `PETSC_FALSE` to disable
719: Options Database Key:
720: . -ts_trajectory_keep_files - have it keep the files
722: Level: advanced
724: Note:
725: By default the `TSTrajectory` used for adjoint computations, `TSTRAJECTORYBASIC`, removes the files it generates at the end of the run. This causes the files to be kept.
727: .seealso: [](ch_ts), `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`, `TSTrajectorySetUp()`, `TSTrajectorySetMonitor()`
728: @*/
729: PetscErrorCode TSTrajectorySetKeepFiles(TSTrajectory tj, PetscBool flg)
730: {
731: PetscFunctionBegin;
734: tj->keepfiles = flg;
735: PetscFunctionReturn(PETSC_SUCCESS);
736: }
738: /*@
739: TSTrajectorySetDirname - Specify the name of the directory where `TSTrajectory` disk checkpoints are stored.
741: Collective
743: Input Parameters:
744: + tj - the `TSTrajectory` context
745: - dirname - the directory name
747: Options Database Key:
748: . -ts_trajectory_dirname - set the directory name
750: Level: developer
752: Notes:
753: The final location of the files is determined by dirname/filetemplate where filetemplate was provided by `TSTrajectorySetFiletemplate()`
755: If this is not called `TSTrajectory` selects a unique new name for the directory
757: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectorySetFiletemplate()`, `TSTrajectorySetUp()`
758: @*/
759: PetscErrorCode TSTrajectorySetDirname(TSTrajectory tj, const char dirname[])
760: {
761: PetscBool flg;
763: PetscFunctionBegin;
765: PetscCall(PetscStrcmp(tj->dirname, dirname, &flg));
766: PetscCheck(flg || !tj->dirfiletemplate, PetscObjectComm((PetscObject)tj), PETSC_ERR_ARG_WRONGSTATE, "Cannot set directoryname after TSTrajectory has been setup");
767: if (!flg) tj->dircreated = PETSC_FALSE;
768: PetscCall(PetscFree(tj->dirname));
769: PetscCall(PetscStrallocpy(dirname, &tj->dirname));
770: PetscFunctionReturn(PETSC_SUCCESS);
771: }
773: /*@
774: TSTrajectorySetFiletemplate - Specify the name template for the files storing `TSTrajectory` checkpoints.
776: Collective
778: Input Parameters:
779: + tj - the `TSTrajectory` context
780: - filetemplate - the template
782: Options Database Key:
783: . -ts_trajectory_file_template - set the file name template
785: Level: developer
787: Notes:
788: The name template should be of the form, for example filename-%06" PetscInt_FMT ".bin It should not begin with a leading /
790: The final location of the files is determined by dirname/filetemplate where dirname was provided by `TSTrajectorySetDirname()`. The %06" PetscInt_FMT " is replaced by the
791: timestep counter
793: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectorySetDirname()`, `TSTrajectorySetUp()`
794: @*/
795: PetscErrorCode TSTrajectorySetFiletemplate(TSTrajectory tj, const char filetemplate[])
796: {
797: const char *ptr = NULL, *ptr2 = NULL;
799: PetscFunctionBegin;
801: PetscAssertPointer(filetemplate, 2);
802: PetscCheck(!tj->dirfiletemplate, PetscObjectComm((PetscObject)tj), PETSC_ERR_ARG_WRONGSTATE, "Cannot set filetemplate after TSTrajectory has been setup");
804: PetscCheck(filetemplate[0], PetscObjectComm((PetscObject)tj), PETSC_ERR_USER, "-ts_trajectory_file_template requires a file name template, e.g. filename-%%06" PetscInt_FMT ".bin");
805: /* Do some cursory validation of the input. */
806: PetscCall(PetscStrstr(filetemplate, "%", (char **)&ptr));
807: PetscCheck(ptr, PetscObjectComm((PetscObject)tj), PETSC_ERR_USER, "-ts_trajectory_file_template requires a file name template, e.g. filename-%%06" PetscInt_FMT ".bin");
808: for (ptr++; ptr && *ptr; ptr++) {
809: PetscCall(PetscStrchr(PetscInt_FMT "DiouxX", *ptr, (char **)&ptr2));
810: PetscCheck(ptr2 || (*ptr >= '0' && *ptr <= '9'), PetscObjectComm((PetscObject)tj), PETSC_ERR_USER, "Invalid file template argument to -ts_trajectory_file_template, should look like filename-%%06" PetscInt_FMT ".bin");
811: if (ptr2) break;
812: }
813: PetscCall(PetscFree(tj->filetemplate));
814: PetscCall(PetscStrallocpy(filetemplate, &tj->filetemplate));
815: PetscFunctionReturn(PETSC_SUCCESS);
816: }
818: /*@
819: TSTrajectorySetFromOptions - Sets various `TSTrajectory` parameters from user options.
821: Collective
823: Input Parameters:
824: + tj - the `TSTrajectory` context obtained from `TSGetTrajectory()`
825: - ts - the `TS` context
827: Options Database Keys:
828: + -ts_trajectory_type (basic|singlefile|memory|visualization) - how to manage the trajectory
829: . -ts_trajectory_keep_files (true|false) - keep the files generated by the code after the program ends. This is true by default for singlefile and visualization
830: - -ts_trajectory_monitor - print `TSTrajectory` information
832: Level: developer
834: Note:
835: This is not normally called directly by users
837: .seealso: [](ch_ts), `TSTrajectory`, `TSSetSaveTrajectory()`, `TSTrajectorySetUp()`
838: @*/
839: PetscErrorCode TSTrajectorySetFromOptions(TSTrajectory tj, TS ts)
840: {
841: PetscBool set, flg;
842: char dirname[PETSC_MAX_PATH_LEN], filetemplate[PETSC_MAX_PATH_LEN];
844: PetscFunctionBegin;
847: PetscObjectOptionsBegin((PetscObject)tj);
848: PetscCall(TSTrajectorySetTypeFromOptions_Private(PetscOptionsObject, tj, ts));
849: PetscCall(PetscOptionsBool("-ts_trajectory_use_history", "Turn on/off usage of TSHistory", NULL, tj->usehistory, &tj->usehistory, NULL));
850: PetscCall(PetscOptionsBool("-ts_trajectory_monitor", "Print checkpointing schedules", "TSTrajectorySetMonitor", tj->monitor ? PETSC_TRUE : PETSC_FALSE, &flg, &set));
851: if (set) PetscCall(TSTrajectorySetMonitor(tj, flg));
852: PetscCall(PetscOptionsInt("-ts_trajectory_reconstruction_order", "Interpolation order for reconstruction", NULL, tj->lag.order, &tj->lag.order, NULL));
853: PetscCall(PetscOptionsBool("-ts_trajectory_reconstruction_caching", "Turn on/off caching of TSTrajectoryGetVecs input", NULL, tj->lag.caching, &tj->lag.caching, NULL));
854: PetscCall(PetscOptionsBool("-ts_trajectory_adjointmode", "Instruct the trajectory that will be used in a TSAdjointSolve()", NULL, tj->adjoint_solve_mode, &tj->adjoint_solve_mode, NULL));
855: PetscCall(PetscOptionsBool("-ts_trajectory_solution_only", "Checkpoint solution only", "TSTrajectorySetSolutionOnly", tj->solution_only, &tj->solution_only, NULL));
856: PetscCall(PetscOptionsBool("-ts_trajectory_keep_files", "Keep any trajectory files generated during the run", "TSTrajectorySetKeepFiles", tj->keepfiles, &flg, &set));
857: if (set) PetscCall(TSTrajectorySetKeepFiles(tj, flg));
859: PetscCall(PetscOptionsString("-ts_trajectory_dirname", "Directory name for TSTrajectory file", "TSTrajectorySetDirname", NULL, dirname, sizeof(dirname) - 14, &set));
860: if (set) PetscCall(TSTrajectorySetDirname(tj, dirname));
862: PetscCall(PetscOptionsString("-ts_trajectory_file_template", "Template for TSTrajectory file name, use filename-%06" PetscInt_FMT ".bin", "TSTrajectorySetFiletemplate", NULL, filetemplate, sizeof(filetemplate), &set));
863: if (set) PetscCall(TSTrajectorySetFiletemplate(tj, filetemplate));
865: /* Handle specific TSTrajectory options */
866: PetscTryTypeMethod(tj, setfromoptions, PetscOptionsObject);
867: PetscOptionsEnd();
868: PetscFunctionReturn(PETSC_SUCCESS);
869: }
871: /*@
872: TSTrajectorySetUp - Sets up the internal data structures, e.g. stacks, for the later use
873: of a `TS` `TSTrajectory`.
875: Collective
877: Input Parameters:
878: + tj - the `TSTrajectory` context
879: - ts - the TS context obtained from `TSCreate()`
881: Level: developer
883: .seealso: [](ch_ts), `TSTrajectory`, `TSSetSaveTrajectory()`, `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`
884: @*/
885: PetscErrorCode TSTrajectorySetUp(TSTrajectory tj, TS ts)
886: {
887: size_t s1, s2;
889: PetscFunctionBegin;
890: if (!tj) PetscFunctionReturn(PETSC_SUCCESS);
893: if (tj->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
895: PetscCall(PetscLogEventBegin(TSTrajectory_SetUp, tj, ts, 0, 0));
896: if (!((PetscObject)tj)->type_name) PetscCall(TSTrajectorySetType(tj, ts, TSTRAJECTORYBASIC));
897: PetscTryTypeMethod(tj, setup, ts);
899: tj->setupcalled = PETSC_TRUE;
901: /* Set the counters to zero */
902: tj->recomps = 0;
903: tj->diskreads = 0;
904: tj->diskwrites = 0;
905: PetscCall(PetscStrlen(tj->dirname, &s1));
906: PetscCall(PetscStrlen(tj->filetemplate, &s2));
907: PetscCall(PetscFree(tj->dirfiletemplate));
908: PetscCall(PetscMalloc((s1 + s2 + 10) * sizeof(char), &tj->dirfiletemplate));
909: PetscCall(PetscSNPrintf(tj->dirfiletemplate, s1 + s2 + 10, "%s/%s", tj->dirname, tj->filetemplate));
910: PetscCall(PetscLogEventEnd(TSTrajectory_SetUp, tj, ts, 0, 0));
911: PetscFunctionReturn(PETSC_SUCCESS);
912: }
914: /*@
915: TSTrajectorySetSolutionOnly - Tells the trajectory to store just the solution, and not any intermediate stage information
917: Collective
919: Input Parameters:
920: + tj - the `TSTrajectory` context obtained with `TSGetTrajectory()`
921: - solution_only - the boolean flag
923: Level: developer
925: .seealso: [](ch_ts), `TSTrajectory`, `TSSetSaveTrajectory()`, `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`, `TSTrajectoryGetSolutionOnly()`
926: @*/
927: PetscErrorCode TSTrajectorySetSolutionOnly(TSTrajectory tj, PetscBool solution_only)
928: {
929: PetscFunctionBegin;
932: tj->solution_only = solution_only;
933: PetscFunctionReturn(PETSC_SUCCESS);
934: }
936: /*@
937: TSTrajectoryGetSolutionOnly - Gets the value set with `TSTrajectorySetSolutionOnly()`.
939: Logically Collective
941: Input Parameter:
942: . tj - the `TSTrajectory` context
944: Output Parameter:
945: . solution_only - the boolean flag
947: Level: developer
949: .seealso: [](ch_ts), `TSTrajectory`, `TSSetSaveTrajectory()`, `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`, `TSTrajectorySetSolutionOnly()`
950: @*/
951: PetscErrorCode TSTrajectoryGetSolutionOnly(TSTrajectory tj, PetscBool *solution_only)
952: {
953: PetscFunctionBegin;
955: PetscAssertPointer(solution_only, 2);
956: *solution_only = tj->solution_only;
957: PetscFunctionReturn(PETSC_SUCCESS);
958: }
960: /*@
961: TSTrajectoryGetUpdatedHistoryVecs - Get updated state and time-derivative history vectors.
963: Collective
965: Input Parameters:
966: + tj - the `TSTrajectory` context
967: . ts - the `TS` solver context
968: - time - the requested time
970: Output Parameters:
971: + U - state vector at given time (can be interpolated)
972: - Udot - time-derivative vector at given time (can be interpolated)
974: Level: developer
976: Notes:
977: The vectors are interpolated if time does not match any time step stored in the `TSTrajectory()`. Pass NULL to not request a vector.
979: This function differs from `TSTrajectoryGetVecs()` since the vectors obtained cannot be modified, and they need to be returned by
980: calling `TSTrajectoryRestoreUpdatedHistoryVecs()`.
982: .seealso: [](ch_ts), `TSTrajectory`, `TSSetSaveTrajectory()`, `TSTrajectoryCreate()`, `TSTrajectoryDestroy()`, `TSTrajectoryRestoreUpdatedHistoryVecs()`, `TSTrajectoryGetVecs()`
983: @*/
984: PetscErrorCode TSTrajectoryGetUpdatedHistoryVecs(TSTrajectory tj, TS ts, PetscReal time, Vec *U, Vec *Udot)
985: {
986: PetscFunctionBegin;
990: if (U) PetscAssertPointer(U, 4);
991: if (Udot) PetscAssertPointer(Udot, 5);
992: if (U && !tj->U) {
993: DM dm;
995: PetscCall(TSGetDM(ts, &dm));
996: PetscCall(DMCreateGlobalVector(dm, &tj->U));
997: }
998: if (Udot && !tj->Udot) {
999: DM dm;
1001: PetscCall(TSGetDM(ts, &dm));
1002: PetscCall(DMCreateGlobalVector(dm, &tj->Udot));
1003: }
1004: PetscCall(TSTrajectoryGetVecs(tj, ts, PETSC_DECIDE, &time, U ? tj->U : NULL, Udot ? tj->Udot : NULL));
1005: if (U) {
1006: PetscCall(VecLockReadPush(tj->U));
1007: *U = tj->U;
1008: }
1009: if (Udot) {
1010: PetscCall(VecLockReadPush(tj->Udot));
1011: *Udot = tj->Udot;
1012: }
1013: PetscFunctionReturn(PETSC_SUCCESS);
1014: }
1016: /*@
1017: TSTrajectoryRestoreUpdatedHistoryVecs - Restores updated state and time-derivative history vectors obtained with `TSTrajectoryGetUpdatedHistoryVecs()`.
1019: Collective
1021: Input Parameters:
1022: + tj - the `TSTrajectory` context
1023: . U - state vector at given time (can be interpolated)
1024: - Udot - time-derivative vector at given time (can be interpolated)
1026: Level: developer
1028: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectoryGetUpdatedHistoryVecs()`
1029: @*/
1030: PetscErrorCode TSTrajectoryRestoreUpdatedHistoryVecs(TSTrajectory tj, Vec *U, Vec *Udot)
1031: {
1032: PetscFunctionBegin;
1036: PetscCheck(!U || *U == tj->U, PetscObjectComm((PetscObject)*U), PETSC_ERR_USER, "U was not obtained from TSTrajectoryGetUpdatedHistoryVecs()");
1037: PetscCheck(!Udot || *Udot == tj->Udot, PetscObjectComm((PetscObject)*Udot), PETSC_ERR_USER, "Udot was not obtained from TSTrajectoryGetUpdatedHistoryVecs()");
1038: if (U) {
1039: PetscCall(VecLockReadPop(tj->U));
1040: *U = NULL;
1041: }
1042: if (Udot) {
1043: PetscCall(VecLockReadPop(tj->Udot));
1044: *Udot = NULL;
1045: }
1046: PetscFunctionReturn(PETSC_SUCCESS);
1047: }