Actual source code: tshistory.c
1: #include <petsc/private/tshistoryimpl.h>
3: struct _n_TSHistory {
4: MPI_Comm comm; /* used for runtime collective checks */
5: PetscReal *hist; /* time history */
6: PetscInt *hist_id; /* stores the stepid in time history */
7: PetscCount n; /* current number of steps registered */
8: PetscBool sorted; /* if the history is sorted in ascending order */
9: PetscCount c; /* current capacity of history */
10: PetscCount s; /* reallocation size */
11: };
13: PetscErrorCode TSHistoryGetNumSteps(TSHistory tsh, PetscInt *n)
14: {
15: PetscFunctionBegin;
16: PetscAssertPointer(n, 2);
17: PetscCall(PetscIntCast(tsh->n, n));
18: PetscFunctionReturn(PETSC_SUCCESS);
19: }
21: PetscErrorCode TSHistoryUpdate(TSHistory tsh, PetscInt id, PetscReal time)
22: {
23: PetscFunctionBegin;
24: if (tsh->n == tsh->c) { /* reallocation */
25: tsh->c += tsh->s;
26: PetscCall(PetscRealloc(tsh->c * sizeof(*tsh->hist), &tsh->hist));
27: PetscCall(PetscRealloc(tsh->c * sizeof(*tsh->hist_id), &tsh->hist_id));
28: }
29: tsh->sorted = (PetscBool)(tsh->sorted && (tsh->n ? (PetscBool)(time >= tsh->hist[tsh->n - 1]) : PETSC_TRUE));
30: if (PetscDefined(USE_DEBUG) && tsh->n) { /* id should be unique */
31: PetscInt loc, *ids;
33: PetscCall(PetscMalloc1(tsh->n, &ids));
34: PetscCall(PetscArraycpy(ids, tsh->hist_id, tsh->n));
35: PetscCall(PetscSortInt(tsh->n, ids));
36: PetscCall(PetscFindInt(id, tsh->n, ids, &loc));
37: PetscCall(PetscFree(ids));
38: PetscCheck(loc < 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "History id should be unique");
39: }
40: tsh->hist[tsh->n] = time;
41: tsh->hist_id[tsh->n] = id;
42: tsh->n += 1;
43: PetscFunctionReturn(PETSC_SUCCESS);
44: }
46: PetscErrorCode TSHistoryGetTime(TSHistory tsh, PetscBool backward, PetscInt step, PetscReal *t)
47: {
48: PetscFunctionBegin;
49: if (!t) PetscFunctionReturn(PETSC_SUCCESS);
50: PetscAssertPointer(t, 4);
51: if (!tsh->sorted) {
52: PetscCall(PetscSortRealWithArrayInt(tsh->n, tsh->hist, tsh->hist_id));
53: tsh->sorted = PETSC_TRUE;
54: }
55: PetscCheck(step >= 0 && step < tsh->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Given time step %" PetscInt_FMT " does not match any in history [0,%" PetscCount_FMT "]", step, tsh->n);
56: if (!backward) *t = tsh->hist[step];
57: else *t = tsh->hist[tsh->n - step - 1];
58: PetscFunctionReturn(PETSC_SUCCESS);
59: }
61: PetscErrorCode TSHistoryGetTimeStep(TSHistory tsh, PetscBool backward, PetscInt step, PetscReal *dt)
62: {
63: PetscFunctionBegin;
64: if (!dt) PetscFunctionReturn(PETSC_SUCCESS);
65: PetscAssertPointer(dt, 4);
66: if (!tsh->sorted) {
67: PetscCall(PetscSortRealWithArrayInt(tsh->n, tsh->hist, tsh->hist_id));
68: tsh->sorted = PETSC_TRUE;
69: }
70: PetscCheck(step >= 0 && step <= tsh->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Given time step %" PetscInt_FMT " does not match any in history [0,%" PetscCount_FMT "]", step, tsh->n);
71: if (!backward) *dt = tsh->hist[PetscMin(step + 1, tsh->n - 1)] - tsh->hist[PetscMin(step, tsh->n - 1)];
72: else *dt = tsh->hist[PetscMax(tsh->n - step - 1, 0)] - tsh->hist[PetscMax(tsh->n - step - 2, 0)];
73: PetscFunctionReturn(PETSC_SUCCESS);
74: }
76: PetscErrorCode TSHistoryGetLocFromTime(TSHistory tsh, PetscReal time, PetscInt *loc)
77: {
78: PetscFunctionBegin;
79: PetscAssertPointer(loc, 3);
80: if (!tsh->sorted) {
81: PetscCall(PetscSortRealWithArrayInt(tsh->n, tsh->hist, tsh->hist_id));
82: tsh->sorted = PETSC_TRUE;
83: }
84: PetscCall(PetscFindReal(time, tsh->n, tsh->hist, PETSC_SMALL, loc));
85: PetscFunctionReturn(PETSC_SUCCESS);
86: }
88: PetscErrorCode TSHistorySetHistory(TSHistory tsh, PetscInt n, PetscReal hist[], PetscInt hist_id[], PetscBool sorted)
89: {
90: PetscFunctionBegin;
92: PetscCheck(n >= 0, tsh->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot request a negative size for history storage");
93: if (n) PetscAssertPointer(hist, 3);
94: PetscCall(PetscFree(tsh->hist));
95: PetscCall(PetscFree(tsh->hist_id));
96: tsh->n = (size_t)n;
97: tsh->c = (size_t)n;
98: PetscCall(PetscMalloc1(tsh->n, &tsh->hist));
99: PetscCall(PetscMalloc1(tsh->n, &tsh->hist_id));
100: for (PetscInt i = 0; i < n; i++) {
101: tsh->hist[i] = hist[i];
102: tsh->hist_id[i] = hist_id ? hist_id[i] : i;
103: }
104: if (!sorted) PetscCall(PetscSortRealWithArrayInt(tsh->n, tsh->hist, tsh->hist_id));
105: tsh->sorted = PETSC_TRUE;
106: PetscFunctionReturn(PETSC_SUCCESS);
107: }
109: PetscErrorCode TSHistoryGetHistory(TSHistory tsh, PetscInt *n, const PetscReal *hist[], const PetscInt *hist_id[], PetscBool *sorted)
110: {
111: PetscFunctionBegin;
112: if (n) PetscCall(PetscIntCast(tsh->n, n));
113: if (hist) *hist = tsh->hist;
114: if (hist_id) *hist_id = tsh->hist_id;
115: if (sorted) *sorted = tsh->sorted;
116: PetscFunctionReturn(PETSC_SUCCESS);
117: }
119: PetscErrorCode TSHistoryDestroy(TSHistory *tsh)
120: {
121: PetscFunctionBegin;
122: if (!*tsh) PetscFunctionReturn(PETSC_SUCCESS);
123: PetscCall(PetscFree((*tsh)->hist));
124: PetscCall(PetscFree((*tsh)->hist_id));
125: PetscCall(PetscCommDestroy(&(*tsh)->comm));
126: PetscCall(PetscFree(*tsh));
127: *tsh = NULL;
128: PetscFunctionReturn(PETSC_SUCCESS);
129: }
131: PetscErrorCode TSHistoryCreate(MPI_Comm comm, TSHistory *hst)
132: {
133: TSHistory tsh;
135: PetscFunctionBegin;
136: PetscAssertPointer(hst, 2);
137: PetscCall(PetscNew(&tsh));
138: PetscCall(PetscCommDuplicate(comm, &tsh->comm, NULL));
140: tsh->c = 1024; /* capacity */
141: tsh->s = 1024; /* reallocation size */
142: tsh->sorted = PETSC_TRUE;
144: PetscCall(PetscMalloc1(tsh->c, &tsh->hist));
145: PetscCall(PetscMalloc1(tsh->c, &tsh->hist_id));
146: *hst = tsh;
147: PetscFunctionReturn(PETSC_SUCCESS);
148: }