Actual source code: glle.c
1: #include <../src/ts/impls/implicit/glle/glle.h>
2: #include <petscdm.h>
3: #include <petscblaslapack.h>
5: static const char *TSGLLEErrorDirections[] = {"FORWARD", "BACKWARD", "TSGLLEErrorDirection", "TSGLLEERROR_", NULL};
6: static PetscFunctionList TSGLLEList;
7: static PetscFunctionList TSGLLEAcceptList;
8: static PetscBool TSGLLEPackageInitialized;
9: static PetscBool TSGLLERegisterAllCalled;
11: /* This function is pure */
12: static PetscScalar Factorial(PetscInt n)
13: {
14: PetscInt i;
15: if (n < 12) { /* Can compute with 32-bit integers */
16: PetscInt f = 1;
17: for (i = 2; i <= n; i++) f *= i;
18: return (PetscScalar)f;
19: } else {
20: PetscScalar f = 1.;
21: for (i = 2; i <= n; i++) f *= (PetscScalar)i;
22: return f;
23: }
24: }
26: /* This function is pure */
27: static PetscScalar CPowF(PetscScalar c, PetscInt p)
28: {
29: return PetscPowRealInt(PetscRealPart(c), p) / Factorial(p);
30: }
32: static PetscErrorCode TSGLLEGetVecs(TS ts, DM dm, Vec *Z, Vec *Ydotstage)
33: {
34: TS_GLLE *gl = (TS_GLLE *)ts->data;
36: PetscFunctionBegin;
37: if (Z) {
38: if (dm && dm != ts->dm) PetscCall(DMGetNamedGlobalVector(dm, "TSGLLE_Z", Z));
39: else *Z = gl->Z;
40: }
41: if (Ydotstage) {
42: if (dm && dm != ts->dm) PetscCall(DMGetNamedGlobalVector(dm, "TSGLLE_Ydot", Ydotstage));
43: else *Ydotstage = gl->Ydot[gl->stage];
44: }
45: PetscFunctionReturn(PETSC_SUCCESS);
46: }
48: static PetscErrorCode TSGLLERestoreVecs(TS ts, DM dm, Vec *Z, Vec *Ydotstage)
49: {
50: PetscFunctionBegin;
51: if (Z) {
52: if (dm && dm != ts->dm) PetscCall(DMRestoreNamedGlobalVector(dm, "TSGLLE_Z", Z));
53: }
54: if (Ydotstage) {
55: if (dm && dm != ts->dm) PetscCall(DMRestoreNamedGlobalVector(dm, "TSGLLE_Ydot", Ydotstage));
56: }
57: PetscFunctionReturn(PETSC_SUCCESS);
58: }
60: static PetscErrorCode DMCoarsenHook_TSGLLE(DM fine, DM coarse, PetscCtx ctx)
61: {
62: PetscFunctionBegin;
63: PetscFunctionReturn(PETSC_SUCCESS);
64: }
66: static PetscErrorCode DMRestrictHook_TSGLLE(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse, PetscCtx ctx)
67: {
68: TS ts = (TS)ctx;
69: Vec Ydot, Ydot_c;
71: PetscFunctionBegin;
72: PetscCall(TSGLLEGetVecs(ts, fine, NULL, &Ydot));
73: PetscCall(TSGLLEGetVecs(ts, coarse, NULL, &Ydot_c));
74: PetscCall(MatRestrict(restrct, Ydot, Ydot_c));
75: PetscCall(VecPointwiseMult(Ydot_c, rscale, Ydot_c));
76: PetscCall(TSGLLERestoreVecs(ts, fine, NULL, &Ydot));
77: PetscCall(TSGLLERestoreVecs(ts, coarse, NULL, &Ydot_c));
78: PetscFunctionReturn(PETSC_SUCCESS);
79: }
81: static PetscErrorCode DMSubDomainHook_TSGLLE(DM dm, DM subdm, PetscCtx ctx)
82: {
83: PetscFunctionBegin;
84: PetscFunctionReturn(PETSC_SUCCESS);
85: }
87: static PetscErrorCode DMSubDomainRestrictHook_TSGLLE(DM dm, VecScatter gscat, VecScatter lscat, DM subdm, PetscCtx ctx)
88: {
89: TS ts = (TS)ctx;
90: Vec Ydot, Ydot_s;
92: PetscFunctionBegin;
93: PetscCall(TSGLLEGetVecs(ts, dm, NULL, &Ydot));
94: PetscCall(TSGLLEGetVecs(ts, subdm, NULL, &Ydot_s));
96: PetscCall(VecScatterBegin(gscat, Ydot, Ydot_s, INSERT_VALUES, SCATTER_FORWARD));
97: PetscCall(VecScatterEnd(gscat, Ydot, Ydot_s, INSERT_VALUES, SCATTER_FORWARD));
99: PetscCall(TSGLLERestoreVecs(ts, dm, NULL, &Ydot));
100: PetscCall(TSGLLERestoreVecs(ts, subdm, NULL, &Ydot_s));
101: PetscFunctionReturn(PETSC_SUCCESS);
102: }
104: static PetscErrorCode TSGLLESchemeCreate(PetscInt p, PetscInt q, PetscInt r, PetscInt s, const PetscScalar *c, const PetscScalar *a, const PetscScalar *b, const PetscScalar *u, const PetscScalar *v, TSGLLEScheme *inscheme)
105: {
106: TSGLLEScheme scheme;
107: PetscInt j;
109: PetscFunctionBegin;
110: PetscCheck(p >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Scheme order must be positive");
111: PetscCheck(r >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "At least one item must be carried between steps");
112: PetscCheck(s >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "At least one stage is required");
113: PetscAssertPointer(inscheme, 10);
114: *inscheme = NULL;
115: PetscCall(PetscNew(&scheme));
116: scheme->p = p;
117: scheme->q = q;
118: scheme->r = r;
119: scheme->s = s;
121: PetscCall(PetscMalloc5(s, &scheme->c, s * s, &scheme->a, r * s, &scheme->b, r * s, &scheme->u, r * r, &scheme->v));
122: PetscCall(PetscArraycpy(scheme->c, c, s));
123: for (j = 0; j < s * s; j++) scheme->a[j] = (PetscAbsScalar(a[j]) < 1e-12) ? 0 : a[j];
124: for (j = 0; j < r * s; j++) scheme->b[j] = (PetscAbsScalar(b[j]) < 1e-12) ? 0 : b[j];
125: for (j = 0; j < s * r; j++) scheme->u[j] = (PetscAbsScalar(u[j]) < 1e-12) ? 0 : u[j];
126: for (j = 0; j < r * r; j++) scheme->v[j] = (PetscAbsScalar(v[j]) < 1e-12) ? 0 : v[j];
128: PetscCall(PetscMalloc6(r, &scheme->alpha, r, &scheme->beta, r, &scheme->gamma, 3 * s, &scheme->phi, 3 * r, &scheme->psi, r, &scheme->stage_error));
129: {
130: PetscInt i, j, k, ss = s + 2;
131: PetscBLASInt m, n, one = 1, *ipiv, lwork, ldb;
132: PetscReal rcond, *sing, *workreal;
133: PetscScalar *ImV, *H, *bmat, *workscalar, *c = scheme->c, *a = scheme->a, *b = scheme->b, *u = scheme->u, *v = scheme->v;
134: PetscBLASInt rank, info;
136: PetscCall(PetscBLASIntCast(4 * ((s + 3) * 3 + 3), &lwork));
137: PetscCall(PetscMalloc7(PetscSqr(r), &ImV, 3 * s, &H, 3 * ss, &bmat, lwork, &workscalar, 5 * (3 + r), &workreal, r + s, &sing, r + s, &ipiv));
139: /* column-major input */
140: for (i = 0; i < r - 1; i++) {
141: for (j = 0; j < r - 1; j++) ImV[i + j * r] = 1.0 * (i == j) - v[(i + 1) * r + j + 1];
142: }
143: /* Build right-hand side for alpha (tp - glm.B(2:end,:)*(glm.c.^(p)./factorial(p))) */
144: for (i = 1; i < r; i++) {
145: scheme->alpha[i] = 1. / Factorial(p + 1 - i);
146: for (j = 0; j < s; j++) scheme->alpha[i] -= b[i * s + j] * CPowF(c[j], p);
147: }
148: PetscCall(PetscBLASIntCast(r - 1, &m));
149: PetscCall(PetscBLASIntCast(r, &n));
150: PetscCallBLAS("LAPACKgesv", LAPACKgesv_(&m, &one, ImV, &n, ipiv, scheme->alpha + 1, &n, &info));
151: PetscCheck(info >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error in LAPACK argument %" PetscBLASInt_FMT, -info);
152: PetscCheck(info <= 0, PETSC_COMM_SELF, PETSC_ERR_MAT_LU_ZRPVT, "Bad factorization: zero pivot in row %" PetscBLASInt_FMT, info - 1);
154: /* Build right-hand side for beta (tp1 - glm.B(2:end,:)*(glm.c.^(p+1)./factorial(p+1)) - e.alpha) */
155: for (i = 1; i < r; i++) {
156: scheme->beta[i] = 1. / Factorial(p + 2 - i) - scheme->alpha[i];
157: for (j = 0; j < s; j++) scheme->beta[i] -= b[i * s + j] * CPowF(c[j], p + 1);
158: }
159: PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_("No transpose", &m, &one, ImV, &n, ipiv, scheme->beta + 1, &n, &info));
161: /* Build stage_error vector
162: xi = glm.c.^(p+1)/factorial(p+1) - glm.A*glm.c.^p/factorial(p) + glm.U(:,2:end)*e.alpha;
163: */
164: for (i = 0; i < s; i++) {
165: scheme->stage_error[i] = CPowF(c[i], p + 1);
166: for (j = 0; j < s; j++) scheme->stage_error[i] -= a[i * s + j] * CPowF(c[j], p);
167: for (j = 1; j < r; j++) scheme->stage_error[i] += u[i * r + j] * scheme->alpha[j];
168: }
170: /* alpha[0] (epsilon in B,J,W 2007)
171: epsilon = 1/factorial(p+1) - B(1,:)*c.^p/factorial(p) + V(1,2:end)*e.alpha;
172: */
173: scheme->alpha[0] = 1. / Factorial(p + 1);
174: for (j = 0; j < s; j++) scheme->alpha[0] -= b[0 * s + j] * CPowF(c[j], p);
175: for (j = 1; j < r; j++) scheme->alpha[0] += v[0 * r + j] * scheme->alpha[j];
177: /* right-hand side for gamma (glm.B(2:end,:)*e.xi - e.epsilon*eye(s-1,1)) */
178: for (i = 1; i < r; i++) {
179: scheme->gamma[i] = (i == 1 ? -1. : 0) * scheme->alpha[0];
180: for (j = 0; j < s; j++) scheme->gamma[i] += b[i * s + j] * scheme->stage_error[j];
181: }
182: PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_("No transpose", &m, &one, ImV, &n, ipiv, scheme->gamma + 1, &n, &info));
184: /* beta[0] (rho in B,J,W 2007)
185: e.rho = 1/factorial(p+2) - glm.B(1,:)*glm.c.^(p+1)/factorial(p+1) ...
186: + glm.V(1,2:end)*e.beta;% - e.epsilon;
187: % Note: The paper (B,J,W 2007) includes the last term in their definition
188: * */
189: scheme->beta[0] = 1. / Factorial(p + 2);
190: for (j = 0; j < s; j++) scheme->beta[0] -= b[0 * s + j] * CPowF(c[j], p + 1);
191: for (j = 1; j < r; j++) scheme->beta[0] += v[0 * r + j] * scheme->beta[j];
193: /* gamma[0] (sigma in B,J,W 2007)
194: * e.sigma = glm.B(1,:)*e.xi + glm.V(1,2:end)*e.gamma;
195: * */
196: scheme->gamma[0] = 0.0;
197: for (j = 0; j < s; j++) scheme->gamma[0] += b[0 * s + j] * scheme->stage_error[j];
198: for (j = 1; j < r; j++) scheme->gamma[0] += v[0 * s + j] * scheme->gamma[j];
200: /* Assemble H
201: * % " PetscInt_FMT "etermine the error estimators phi
202: H = [[cpow(glm.c,p) + C*e.alpha] [cpow(glm.c,p+1) + C*e.beta] ...
203: [e.xi - C*(e.gamma + 0*e.epsilon*eye(s-1,1))]]';
204: % Paper has formula above without the 0, but that term must be left
205: % out to satisfy the conditions they propose and to make the
206: % example schemes work
207: e.H = H;
208: e.phi = (H \ [1 0 0;1 1 0;0 0 -1])';
209: e.psi = -e.phi*C;
210: * */
211: for (j = 0; j < s; j++) {
212: H[0 + j * 3] = CPowF(c[j], p);
213: H[1 + j * 3] = CPowF(c[j], p + 1);
214: H[2 + j * 3] = scheme->stage_error[j];
215: for (k = 1; k < r; k++) {
216: H[0 + j * 3] += CPowF(c[j], k - 1) * scheme->alpha[k];
217: H[1 + j * 3] += CPowF(c[j], k - 1) * scheme->beta[k];
218: H[2 + j * 3] -= CPowF(c[j], k - 1) * scheme->gamma[k];
219: }
220: }
221: bmat[0 + 0 * ss] = 1.;
222: bmat[0 + 1 * ss] = 0.;
223: bmat[0 + 2 * ss] = 0.;
224: bmat[1 + 0 * ss] = 1.;
225: bmat[1 + 1 * ss] = 1.;
226: bmat[1 + 2 * ss] = 0.;
227: bmat[2 + 0 * ss] = 0.;
228: bmat[2 + 1 * ss] = 0.;
229: bmat[2 + 2 * ss] = -1.;
230: m = 3;
231: PetscCall(PetscBLASIntCast(s, &n));
232: PetscCall(PetscBLASIntCast(ss, &ldb));
233: rcond = 1e-12;
234: #if defined(PETSC_USE_COMPLEX)
235: PetscCallLAPACKInfo("LAPACKgelss", LAPACKgelss_(&m, &n, &m, H, &m, bmat, &ldb, sing, &rcond, &rank, workscalar, &lwork, workreal, &info));
236: #else
237: PetscCallLAPACKInfo("LAPACKgelss", LAPACKgelss_(&m, &n, &m, H, &m, bmat, &ldb, sing, &rcond, &rank, workscalar, &lwork, &info));
238: #endif
240: for (j = 0; j < 3; j++) {
241: for (k = 0; k < s; k++) scheme->phi[k + j * s] = bmat[k + j * ss];
242: }
244: /* the other part of the error estimator, psi in B,J,W 2007 */
245: scheme->psi[0 * r + 0] = 0.;
246: scheme->psi[1 * r + 0] = 0.;
247: scheme->psi[2 * r + 0] = 0.;
248: for (j = 1; j < r; j++) {
249: scheme->psi[0 * r + j] = 0.;
250: scheme->psi[1 * r + j] = 0.;
251: scheme->psi[2 * r + j] = 0.;
252: for (k = 0; k < s; k++) {
253: scheme->psi[0 * r + j] -= CPowF(c[k], j - 1) * scheme->phi[0 * s + k];
254: scheme->psi[1 * r + j] -= CPowF(c[k], j - 1) * scheme->phi[1 * s + k];
255: scheme->psi[2 * r + j] -= CPowF(c[k], j - 1) * scheme->phi[2 * s + k];
256: }
257: }
258: PetscCall(PetscFree7(ImV, H, bmat, workscalar, workreal, sing, ipiv));
259: }
260: /* Check which properties are satisfied */
261: scheme->stiffly_accurate = PETSC_TRUE;
262: if (scheme->c[s - 1] != 1.) scheme->stiffly_accurate = PETSC_FALSE;
263: for (j = 0; j < s; j++)
264: if (a[(s - 1) * s + j] != b[j]) scheme->stiffly_accurate = PETSC_FALSE;
265: for (j = 0; j < r; j++)
266: if (u[(s - 1) * r + j] != v[j]) scheme->stiffly_accurate = PETSC_FALSE;
267: scheme->fsal = scheme->stiffly_accurate; /* FSAL is stronger */
268: for (j = 0; j < s - 1; j++)
269: if (r > 1 && b[1 * s + j] != 0.) scheme->fsal = PETSC_FALSE;
270: if (b[1 * s + r - 1] != 1.) scheme->fsal = PETSC_FALSE;
271: for (j = 0; j < r; j++)
272: if (r > 1 && v[1 * r + j] != 0.) scheme->fsal = PETSC_FALSE;
274: *inscheme = scheme;
275: PetscFunctionReturn(PETSC_SUCCESS);
276: }
278: static PetscErrorCode TSGLLESchemeDestroy(TSGLLEScheme sc)
279: {
280: PetscFunctionBegin;
281: PetscCall(PetscFree5(sc->c, sc->a, sc->b, sc->u, sc->v));
282: PetscCall(PetscFree6(sc->alpha, sc->beta, sc->gamma, sc->phi, sc->psi, sc->stage_error));
283: PetscCall(PetscFree(sc));
284: PetscFunctionReturn(PETSC_SUCCESS);
285: }
287: static PetscErrorCode TSGLLEDestroy_Default(TS_GLLE *gl)
288: {
289: PetscInt i;
291: PetscFunctionBegin;
292: for (i = 0; i < gl->nschemes; i++) {
293: if (gl->schemes[i]) PetscCall(TSGLLESchemeDestroy(gl->schemes[i]));
294: }
295: PetscCall(PetscFree(gl->schemes));
296: gl->nschemes = 0;
297: PetscCall(PetscMemzero(gl->type_name, sizeof(gl->type_name)));
298: PetscFunctionReturn(PETSC_SUCCESS);
299: }
301: static PetscErrorCode TSGLLEViewTable_Private(PetscViewer viewer, PetscInt m, PetscInt n, const PetscScalar a[], const char name[])
302: {
303: PetscBool isascii;
304: PetscInt i, j;
306: PetscFunctionBegin;
307: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
308: if (isascii) {
309: PetscCall(PetscViewerASCIIPrintf(viewer, "%30s = [", name));
310: for (i = 0; i < m; i++) {
311: if (i) PetscCall(PetscViewerASCIIPrintf(viewer, "%30s [", ""));
312: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
313: for (j = 0; j < n; j++) PetscCall(PetscViewerASCIIPrintf(viewer, " %12.8g", (double)PetscRealPart(a[i * n + j])));
314: PetscCall(PetscViewerASCIIPrintf(viewer, "]\n"));
315: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
316: }
317: }
318: PetscFunctionReturn(PETSC_SUCCESS);
319: }
321: static PetscErrorCode TSGLLESchemeView(TSGLLEScheme sc, PetscBool view_details, PetscViewer viewer)
322: {
323: PetscBool isascii;
325: PetscFunctionBegin;
326: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
327: PetscCheck(isascii, PETSC_COMM_SELF, PETSC_ERR_SUP, "Viewer type %s not supported", ((PetscObject)viewer)->type_name);
328: PetscCall(PetscViewerASCIIPrintf(viewer, "GL scheme p,q,r,s = %" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT "\n", sc->p, sc->q, sc->r, sc->s));
329: PetscCall(PetscViewerASCIIPushTab(viewer));
330: PetscCall(PetscViewerASCIIPrintf(viewer, "Stiffly accurate: %s, FSAL: %s\n", sc->stiffly_accurate ? "yes" : "no", sc->fsal ? "yes" : "no"));
331: PetscCall(PetscViewerASCIIPrintf(viewer, "Leading error constants: %10.3e %10.3e %10.3e\n", (double)PetscRealPart(sc->alpha[0]), (double)PetscRealPart(sc->beta[0]), (double)PetscRealPart(sc->gamma[0])));
332: PetscCall(TSGLLEViewTable_Private(viewer, 1, sc->s, sc->c, "Abscissas c"));
333: if (view_details) {
334: PetscCall(TSGLLEViewTable_Private(viewer, sc->s, sc->s, sc->a, "A"));
335: PetscCall(TSGLLEViewTable_Private(viewer, sc->r, sc->s, sc->b, "B"));
336: PetscCall(TSGLLEViewTable_Private(viewer, sc->s, sc->r, sc->u, "U"));
337: PetscCall(TSGLLEViewTable_Private(viewer, sc->r, sc->r, sc->v, "V"));
339: PetscCall(TSGLLEViewTable_Private(viewer, 3, sc->s, sc->phi, "Error estimate phi"));
340: PetscCall(TSGLLEViewTable_Private(viewer, 3, sc->r, sc->psi, "Error estimate psi"));
341: PetscCall(TSGLLEViewTable_Private(viewer, 1, sc->r, sc->alpha, "Modify alpha"));
342: PetscCall(TSGLLEViewTable_Private(viewer, 1, sc->r, sc->beta, "Modify beta"));
343: PetscCall(TSGLLEViewTable_Private(viewer, 1, sc->r, sc->gamma, "Modify gamma"));
344: PetscCall(TSGLLEViewTable_Private(viewer, 1, sc->s, sc->stage_error, "Stage error xi"));
345: }
346: PetscCall(PetscViewerASCIIPopTab(viewer));
347: PetscFunctionReturn(PETSC_SUCCESS);
348: }
350: static PetscErrorCode TSGLLEEstimateHigherMoments_Default(TSGLLEScheme sc, PetscReal h, Vec Ydot[], Vec Xold[], Vec hm[])
351: {
352: PetscInt i;
354: PetscFunctionBegin;
355: PetscCheck(sc->r <= 64 && sc->s <= 64, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Ridiculous number of stages or items passed between stages");
356: /* build error vectors*/
357: for (i = 0; i < 3; i++) {
358: PetscScalar phih[64];
359: PetscInt j;
360: for (j = 0; j < sc->s; j++) phih[j] = sc->phi[i * sc->s + j] * h;
361: PetscCall(VecZeroEntries(hm[i]));
362: PetscCall(VecMAXPY(hm[i], sc->s, phih, Ydot));
363: PetscCall(VecMAXPY(hm[i], sc->r, &sc->psi[i * sc->r], Xold));
364: }
365: PetscFunctionReturn(PETSC_SUCCESS);
366: }
368: static PetscErrorCode TSGLLECompleteStep_Rescale(TSGLLEScheme sc, PetscReal h, TSGLLEScheme next_sc, PetscReal next_h, Vec Ydot[], Vec Xold[], Vec X[])
369: {
370: PetscScalar brow[32], vrow[32];
371: PetscInt i, j, r, s;
373: PetscFunctionBegin;
374: /* Build the new solution from (X,Ydot) */
375: r = sc->r;
376: s = sc->s;
377: for (i = 0; i < r; i++) {
378: PetscCall(VecZeroEntries(X[i]));
379: for (j = 0; j < s; j++) brow[j] = h * sc->b[i * s + j];
380: PetscCall(VecMAXPY(X[i], s, brow, Ydot));
381: for (j = 0; j < r; j++) vrow[j] = sc->v[i * r + j];
382: PetscCall(VecMAXPY(X[i], r, vrow, Xold));
383: }
384: PetscFunctionReturn(PETSC_SUCCESS);
385: }
387: static PetscErrorCode TSGLLECompleteStep_RescaleAndModify(TSGLLEScheme sc, PetscReal h, TSGLLEScheme next_sc, PetscReal next_h, Vec Ydot[], Vec Xold[], Vec X[])
388: {
389: PetscScalar brow[32], vrow[32];
390: PetscReal ratio;
391: PetscInt i, j, p, r, s;
393: PetscFunctionBegin;
394: /* Build the new solution from (X,Ydot) */
395: p = sc->p;
396: r = sc->r;
397: s = sc->s;
398: ratio = next_h / h;
399: for (i = 0; i < r; i++) {
400: PetscCall(VecZeroEntries(X[i]));
401: for (j = 0; j < s; j++) {
402: brow[j] = h * (PetscPowRealInt(ratio, i) * sc->b[i * s + j] + (PetscPowRealInt(ratio, i) - PetscPowRealInt(ratio, p + 1)) * (+sc->alpha[i] * sc->phi[0 * s + j]) + (PetscPowRealInt(ratio, i) - PetscPowRealInt(ratio, p + 2)) * (+sc->beta[i] * sc->phi[1 * s + j] + sc->gamma[i] * sc->phi[2 * s + j]));
403: }
404: PetscCall(VecMAXPY(X[i], s, brow, Ydot));
405: for (j = 0; j < r; j++) {
406: vrow[j] = (PetscPowRealInt(ratio, i) * sc->v[i * r + j] + (PetscPowRealInt(ratio, i) - PetscPowRealInt(ratio, p + 1)) * (+sc->alpha[i] * sc->psi[0 * r + j]) + (PetscPowRealInt(ratio, i) - PetscPowRealInt(ratio, p + 2)) * (+sc->beta[i] * sc->psi[1 * r + j] + sc->gamma[i] * sc->psi[2 * r + j]));
407: }
408: PetscCall(VecMAXPY(X[i], r, vrow, Xold));
409: }
410: if (r < next_sc->r) {
411: PetscCheck(r + 1 == next_sc->r, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Cannot accommodate jump in r greater than 1");
412: PetscCall(VecZeroEntries(X[r]));
413: for (j = 0; j < s; j++) brow[j] = h * PetscPowRealInt(ratio, p + 1) * sc->phi[0 * s + j];
414: PetscCall(VecMAXPY(X[r], s, brow, Ydot));
415: for (j = 0; j < r; j++) vrow[j] = PetscPowRealInt(ratio, p + 1) * sc->psi[0 * r + j];
416: PetscCall(VecMAXPY(X[r], r, vrow, Xold));
417: }
418: PetscFunctionReturn(PETSC_SUCCESS);
419: }
421: static PetscErrorCode TSGLLECreate_IRKS(TS ts)
422: {
423: TS_GLLE *gl = (TS_GLLE *)ts->data;
425: PetscFunctionBegin;
426: gl->Destroy = TSGLLEDestroy_Default;
427: gl->EstimateHigherMoments = TSGLLEEstimateHigherMoments_Default;
428: gl->CompleteStep = TSGLLECompleteStep_RescaleAndModify;
429: PetscCall(PetscMalloc1(10, &gl->schemes));
430: gl->nschemes = 0;
432: {
433: /* p=1,q=1, r=s=2, A- and L-stable with error estimates of order 2 and 3
434: * Listed in Butcher & Podhaisky 2006. On error estimation in general linear methods for stiff ODE.
435: * irks(0.3,0,[.3,1],[1],1)
436: * Note: can be made to have classical order (not stage order) 2 by replacing 0.3 with 1-sqrt(1/2)
437: * but doing so would sacrifice the error estimator.
438: */
439: const PetscScalar c[2] = {3. / 10., 1.};
440: const PetscScalar a[2][2] = {
441: {3. / 10., 0 },
442: {7. / 10., 3. / 10.}
443: };
444: const PetscScalar b[2][2] = {
445: {7. / 10., 3. / 10.},
446: {0, 1 }
447: };
448: const PetscScalar u[2][2] = {
449: {1, 0},
450: {1, 0}
451: };
452: const PetscScalar v[2][2] = {
453: {1, 0},
454: {0, 0}
455: };
456: PetscCall(TSGLLESchemeCreate(1, 1, 2, 2, c, *a, *b, *u, *v, &gl->schemes[gl->nschemes++]));
457: }
459: {
460: /* p=q=2, r=s=3: irks(4/9,0,[1:3]/3,[0.33852],1) */
461: /* http://www.math.auckland.ac.nz/~hpod/atlas/i2a.html */
462: const PetscScalar c[3] = {1. / 3., 2. / 3., 1};
463: const PetscScalar a[3][3] = {
464: {4. / 9., 0, 0 },
465: {1.03750643704090e+00, 4. / 9., 0 },
466: {7.67024779410304e-01, -3.81140216918943e-01, 4. / 9.}
467: };
468: const PetscScalar b[3][3] = {
469: {0.767024779410304, -0.381140216918943, 4. / 9. },
470: {0.000000000000000, 0.000000000000000, 1.000000000000000},
471: {-2.075048385225385, 0.621728385225383, 1.277197204924873}
472: };
473: const PetscScalar u[3][3] = {
474: {1.0000000000000000, -0.1111111111111109, -0.0925925925925922},
475: {1.0000000000000000, -0.8152842148186744, -0.4199095530877056},
476: {1.0000000000000000, 0.1696709930641948, 0.0539741070314165 }
477: };
478: const PetscScalar v[3][3] = {
479: {1.0000000000000000, 0.1696709930641948, 0.0539741070314165},
480: {0.000000000000000, 0.000000000000000, 0.000000000000000 },
481: {0.000000000000000, 0.176122795075129, 0.000000000000000 }
482: };
483: PetscCall(TSGLLESchemeCreate(2, 2, 3, 3, c, *a, *b, *u, *v, &gl->schemes[gl->nschemes++]));
484: }
485: {
486: /* p=q=3, r=s=4: irks(9/40,0,[1:4]/4,[0.3312 1.0050],[0.49541 1;1 0]) */
487: const PetscScalar c[4] = {0.25, 0.5, 0.75, 1.0};
488: const PetscScalar a[4][4] = {
489: {9. / 40., 0, 0, 0 },
490: {2.11286958887701e-01, 9. / 40., 0, 0 },
491: {9.46338294287584e-01, -3.42942861246094e-01, 9. / 40., 0 },
492: {0.521490453970721, -0.662474225622980, 0.490476425459734, 9. / 40.}
493: };
494: const PetscScalar b[4][4] = {
495: {0.521490453970721, -0.662474225622980, 0.490476425459734, 9. / 40. },
496: {0.000000000000000, 0.000000000000000, 0.000000000000000, 1.000000000000000},
497: {-0.084677029310348, 1.390757514776085, -1.568157386206001, 2.023192696767826},
498: {0.465383797936408, 1.478273530625148, -1.930836081010182, 1.644872111193354}
499: };
500: const PetscScalar u[4][4] = {
501: {1.00000000000000000, 0.02500000000001035, -0.02499999999999053, -0.00442708333332865},
502: {1.00000000000000000, 0.06371304111232945, -0.04032173972189845, -0.01389438413189452},
503: {1.00000000000000000, -0.07839543304147778, 0.04738685705116663, 0.02032603595928376 },
504: {1.00000000000000000, 0.42550734619251651, 0.10800718022400080, -0.01726712647760034}
505: };
506: const PetscScalar v[4][4] = {
507: {1.00000000000000000, 0.42550734619251651, 0.10800718022400080, -0.01726712647760034},
508: {0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000 },
509: {0.000000000000000, -1.761115796027561, -0.521284157173780, 0.258249384305463 },
510: {0.000000000000000, -1.657693358744728, -1.052227765232394, 0.521284157173780 }
511: };
512: PetscCall(TSGLLESchemeCreate(3, 3, 4, 4, c, *a, *b, *u, *v, &gl->schemes[gl->nschemes++]));
513: }
514: {
515: /* p=q=4, r=s=5:
516: irks(3/11,0,[1:5]/5, [0.1715 -0.1238 0.6617],...
517: [ -0.0812 0.4079 1.0000
518: 1.0000 0 0
519: 0.8270 1.0000 0])
520: */
521: const PetscScalar c[5] = {0.2, 0.4, 0.6, 0.8, 1.0};
522: const PetscScalar a[5][5] = {
523: {2.72727272727352e-01, 0.00000000000000e+00, 0.00000000000000e+00, 0.00000000000000e+00, 0.00000000000000e+00},
524: {-1.03980153733431e-01, 2.72727272727405e-01, 0.00000000000000e+00, 0.00000000000000e+00, 0.00000000000000e+00},
525: {-1.58615400341492e+00, 7.44168951881122e-01, 2.72727272727309e-01, 0.00000000000000e+00, 0.00000000000000e+00},
526: {-8.73658042865628e-01, 5.37884671894595e-01, -1.63298538799523e-01, 2.72727272726996e-01, 0.00000000000000e+00},
527: {2.95489397443992e-01, -1.18481693910097e+00, -6.68029812659953e-01, 1.00716687860943e+00, 2.72727272727288e-01}
528: };
529: const PetscScalar b[5][5] = {
530: {2.95489397443992e-01, -1.18481693910097e+00, -6.68029812659953e-01, 1.00716687860943e+00, 2.72727272727288e-01},
531: {0.00000000000000e+00, 1.11022302462516e-16, -2.22044604925031e-16, 0.00000000000000e+00, 1.00000000000000e+00},
532: {-4.05882503986005e+00, -4.00924006567769e+00, -1.38930610972481e+00, 4.45223930308488e+00, 6.32331093108427e-01},
533: {8.35690179937017e+00, -2.26640927349732e+00, 6.86647884973826e+00, -5.22595158025740e+00, 4.50893068837431e+00},
534: {1.27656267027479e+01, 2.80882153840821e+00, 8.91173096522890e+00, -1.07936444078906e+01, 4.82534148988854e+00}
535: };
536: const PetscScalar u[5][5] = {
537: {1.00000000000000e+00, -7.27272727273551e-02, -3.45454545454419e-02, -4.12121212119565e-03, -2.96969696964014e-04},
538: {1.00000000000000e+00, 2.31252881006154e-01, -8.29487834416481e-03, -9.07191207681020e-03, -1.70378403743473e-03},
539: {1.00000000000000e+00, 1.16925777880663e+00, 3.59268562942635e-02, -4.09013451730615e-02, -1.02411119670164e-02},
540: {1.00000000000000e+00, 1.02634463704356e+00, 1.59375044913405e-01, 1.89673015035370e-03, -4.89987231897569e-03},
541: {1.00000000000000e+00, 1.27746320298021e+00, 2.37186008132728e-01, -8.28694373940065e-02, -5.34396510196430e-02}
542: };
543: const PetscScalar v[5][5] = {
544: {1.00000000000000e+00, 1.27746320298021e+00, 2.37186008132728e-01, -8.28694373940065e-02, -5.34396510196430e-02},
545: {0.00000000000000e+00, -1.77635683940025e-15, -1.99840144432528e-15, -9.99200722162641e-16, -3.33066907387547e-16},
546: {0.00000000000000e+00, 4.37280081906924e+00, 5.49221645016377e-02, -8.88913177394943e-02, 1.12879077989154e-01 },
547: {0.00000000000000e+00, -1.22399504837280e+01, -5.21287338448645e+00, -8.03952325565291e-01, 4.60298678047147e-01 },
548: {0.00000000000000e+00, -1.85178762883829e+01, -5.21411849862624e+00, -1.04283436528809e+00, 7.49030161063651e-01 }
549: };
550: PetscCall(TSGLLESchemeCreate(4, 4, 5, 5, c, *a, *b, *u, *v, &gl->schemes[gl->nschemes++]));
551: }
552: {
553: /* p=q=5, r=s=6;
554: irks(1/3,0,[1:6]/6,...
555: [-0.0489 0.4228 -0.8814 0.9021],...
556: [-0.3474 -0.6617 0.6294 0.2129
557: 0.0044 -0.4256 -0.1427 -0.8936
558: -0.8267 0.4821 0.1371 -0.2557
559: -0.4426 -0.3855 -0.7514 0.3014])
560: */
561: const PetscScalar c[6] = {1. / 6, 2. / 6, 3. / 6, 4. / 6, 5. / 6, 1.};
562: const PetscScalar a[6][6] = {
563: {3.33333333333940e-01, 0, 0, 0, 0, 0 },
564: {-8.64423857333350e-02, 3.33333333332888e-01, 0, 0, 0, 0 },
565: {-2.16850174258252e+00, -2.23619072028839e+00, 3.33333333335204e-01, 0, 0, 0 },
566: {-4.73160970138997e+00, -3.89265344629268e+00, -2.76318716520933e-01, 3.33333333335759e-01, 0, 0 },
567: {-6.75187540297338e+00, -7.90756533769377e+00, 7.90245051802259e-01, -4.48352364517632e-01, 3.33333333328483e-01, 0 },
568: {-4.26488287921548e+00, -1.19320395589302e+01, 3.38924509887755e+00, -2.23969848002481e+00, 6.62807710124007e-01, 3.33333333335440e-01}
569: };
570: const PetscScalar b[6][6] = {
571: {-4.26488287921548e+00, -1.19320395589302e+01, 3.38924509887755e+00, -2.23969848002481e+00, 6.62807710124007e-01, 3.33333333335440e-01 },
572: {-8.88178419700125e-16, 4.44089209850063e-16, -1.54737334057131e-15, -8.88178419700125e-16, 0.00000000000000e+00, 1.00000000000001e+00 },
573: {-2.87780425770651e+01, -1.13520448264971e+01, 2.62002318943161e+01, 2.56943874812797e+01, -3.06702268304488e+01, 6.68067773510103e+00 },
574: {5.47971245256474e+01, 6.80366875868284e+01, -6.50952588861999e+01, -8.28643975339097e+01, 8.17416943896414e+01, -1.17819043489036e+01},
575: {-2.33332114788869e+02, 6.12942539462634e+01, -4.91850135865944e+01, 1.82716844135480e+02, -1.29788173979395e+02, 3.09968095651099e+01 },
576: {-1.72049132343751e+02, 8.60194713593999e+00, 7.98154219170200e-01, 1.50371386053218e+02, -1.18515423962066e+02, 2.50898277784663e+01 }
577: };
578: const PetscScalar u[6][6] = {
579: {1.00000000000000e+00, -1.66666666666870e-01, -4.16666666664335e-02, -3.85802469124815e-03, -2.25051440302250e-04, -9.64506172339142e-06},
580: {1.00000000000000e+00, 8.64423857327162e-02, -4.11484912671353e-02, -1.11450903217645e-02, -1.47651050487126e-03, -1.34395070766826e-04},
581: {1.00000000000000e+00, 4.57135912953434e+00, 1.06514719719137e+00, 1.33517564218007e-01, 1.11365952968659e-02, 6.12382756769504e-04 },
582: {1.00000000000000e+00, 9.23391519753404e+00, 2.22431212392095e+00, 2.91823807741891e-01, 2.52058456411084e-02, 1.22800542949647e-03 },
583: {1.00000000000000e+00, 1.48175480533865e+01, 3.73439117461835e+00, 5.14648336541804e-01, 4.76430038853402e-02, 2.56798515502156e-03 },
584: {1.00000000000000e+00, 1.50512347758335e+01, 4.10099701165164e+00, 5.66039141003603e-01, 3.91213893800891e-02, -2.99136269067853e-03}
585: };
586: const PetscScalar v[6][6] = {
587: {1.00000000000000e+00, 1.50512347758335e+01, 4.10099701165164e+00, 5.66039141003603e-01, 3.91213893800891e-02, -2.99136269067853e-03},
588: {0.00000000000000e+00, -4.88498130835069e-15, -6.43929354282591e-15, -3.55271367880050e-15, -1.22124532708767e-15, -3.12250225675825e-16},
589: {0.00000000000000e+00, 1.22250171233141e+01, -1.77150760606169e+00, 3.54516769879390e-01, 6.22298845883398e-01, 2.31647447450276e-01 },
590: {0.00000000000000e+00, -4.48339457331040e+01, -3.57363126641880e-01, 5.18750173123425e-01, 6.55727990241799e-02, 1.63175368287079e-01 },
591: {0.00000000000000e+00, 1.37297394708005e+02, -1.60145272991317e+00, -5.05319555199441e+00, 1.55328940390990e-01, 9.16629423682464e-01 },
592: {0.00000000000000e+00, 1.05703241119022e+02, -1.16610260983038e+00, -2.99767252773859e+00, -1.13472315553890e-01, 1.09742849254729e+00 }
593: };
594: PetscCall(TSGLLESchemeCreate(5, 5, 6, 6, c, *a, *b, *u, *v, &gl->schemes[gl->nschemes++]));
595: }
596: PetscFunctionReturn(PETSC_SUCCESS);
597: }
599: /*@
600: TSGLLESetType - sets the class of general linear method, `TSGLLE` to use for time-stepping
602: Collective
604: Input Parameters:
605: + ts - the `TS` context
606: - type - a method, currently only `TSGLLE_IRKS` is available
608: Options Database Key:
609: . -ts_gl_type (irks) - sets the method
611: Level: intermediate
613: .seealso: [](ch_ts), `TS`, `TSGLLEType`, `TSGLLE`, `TSGLLERegister()`, `TSGLLE_IRKS`, `TSGLLEGetAcceptType()`,
614: `TSGLLESetAcceptType()`, `TSGLLEAcceptType()`
615: @*/
616: PetscErrorCode TSGLLESetType(TS ts, TSGLLEType type)
617: {
618: PetscFunctionBegin;
620: PetscAssertPointer(type, 2);
621: PetscTryMethod(ts, "TSGLLESetType_C", (TS, TSGLLEType), (ts, type));
622: PetscFunctionReturn(PETSC_SUCCESS);
623: }
625: /*@C
626: TSGLLESetAcceptType - sets the acceptance test for `TSGLLE`
628: Logically Collective
630: Input Parameters:
631: + ts - the `TS` context
632: - type - the type
634: Options Database Key:
635: . -ts_gl_accept_type (always) - sets the method used to determine whether to accept or reject a step
637: Level: intermediate
639: Notes:
640: Time integrators that need to control error must have the option to reject a time step based
641: on local error estimates. This function allows different schemes to be set.
643: .seealso: [](ch_ts), `TS`, `TSGLLE`, `TSGLLEAcceptRegister()`, `TSGLLEAdapt`
644: @*/
645: PetscErrorCode TSGLLESetAcceptType(TS ts, TSGLLEAcceptType type)
646: {
647: PetscFunctionBegin;
649: PetscAssertPointer(type, 2);
650: PetscTryMethod(ts, "TSGLLESetAcceptType_C", (TS, TSGLLEAcceptType), (ts, type));
651: PetscFunctionReturn(PETSC_SUCCESS);
652: }
654: /*@
655: TSGLLEGetAdapt - gets the `TSGLLEAdapt` object from the `TS`
657: Not Collective
659: Input Parameter:
660: . ts - the `TS` context
662: Output Parameter:
663: . adapt - the `TSGLLEAdapt` context
665: Level: advanced
667: Note:
668: This allows the user set options on the `TSGLLEAdapt` object. Usually it is better to do this
669: using the options database, so this function is rarely needed.
671: .seealso: [](ch_ts), `TS`, `TSGLLE`, `TSGLLEAdapt`, `TSGLLEAdaptRegister()`
672: @*/
673: PetscErrorCode TSGLLEGetAdapt(TS ts, TSGLLEAdapt *adapt)
674: {
675: PetscFunctionBegin;
677: PetscAssertPointer(adapt, 2);
678: PetscUseMethod(ts, "TSGLLEGetAdapt_C", (TS, TSGLLEAdapt *), (ts, adapt));
679: PetscFunctionReturn(PETSC_SUCCESS);
680: }
682: static PetscErrorCode TSGLLEAccept_Always(TS ts, PetscReal tleft, PetscReal h, const PetscReal enorms[], PetscBool *accept)
683: {
684: PetscFunctionBegin;
685: *accept = PETSC_TRUE;
686: PetscFunctionReturn(PETSC_SUCCESS);
687: }
689: static PetscErrorCode TSGLLEUpdateWRMS(TS ts)
690: {
691: TS_GLLE *gl = (TS_GLLE *)ts->data;
692: PetscScalar *x, *w;
693: PetscInt n, i;
695: PetscFunctionBegin;
696: PetscCall(VecGetArray(gl->X[0], &x));
697: PetscCall(VecGetArray(gl->W, &w));
698: PetscCall(VecGetLocalSize(gl->W, &n));
699: for (i = 0; i < n; i++) w[i] = 1. / (gl->wrms_atol + gl->wrms_rtol * PetscAbsScalar(x[i]));
700: PetscCall(VecRestoreArray(gl->X[0], &x));
701: PetscCall(VecRestoreArray(gl->W, &w));
702: PetscFunctionReturn(PETSC_SUCCESS);
703: }
705: static PetscErrorCode TSGLLEVecNormWRMS(TS ts, Vec X, PetscReal *nrm)
706: {
707: TS_GLLE *gl = (TS_GLLE *)ts->data;
708: PetscScalar *x, *w;
709: PetscReal sum = 0.0, gsum;
710: PetscInt n, N, i;
712: PetscFunctionBegin;
713: PetscCall(VecGetArray(X, &x));
714: PetscCall(VecGetArray(gl->W, &w));
715: PetscCall(VecGetLocalSize(gl->W, &n));
716: for (i = 0; i < n; i++) sum += PetscAbsScalar(PetscSqr(x[i] * w[i]));
717: PetscCall(VecRestoreArray(X, &x));
718: PetscCall(VecRestoreArray(gl->W, &w));
719: PetscCallMPI(MPIU_Allreduce(&sum, &gsum, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)ts)));
720: PetscCall(VecGetSize(gl->W, &N));
721: *nrm = PetscSqrtReal(gsum / (1. * N));
722: PetscFunctionReturn(PETSC_SUCCESS);
723: }
725: static PetscErrorCode TSGLLESetType_GLLE(TS ts, TSGLLEType type)
726: {
727: PetscBool same;
728: TS_GLLE *gl = (TS_GLLE *)ts->data;
729: PetscErrorCode (*r)(TS);
731: PetscFunctionBegin;
732: if (gl->type_name[0]) {
733: PetscCall(PetscStrcmp(gl->type_name, type, &same));
734: if (same) PetscFunctionReturn(PETSC_SUCCESS);
735: PetscCall((*gl->Destroy)(gl));
736: }
738: PetscCall(PetscFunctionListFind(TSGLLEList, type, &r));
739: PetscCheck(r, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TSGLLE type \"%s\" given", type);
740: PetscCall((*r)(ts));
741: PetscCall(PetscStrncpy(gl->type_name, type, sizeof(gl->type_name)));
742: PetscFunctionReturn(PETSC_SUCCESS);
743: }
745: static PetscErrorCode TSGLLESetAcceptType_GLLE(TS ts, TSGLLEAcceptType type)
746: {
747: TSGLLEAcceptFn *r;
748: TS_GLLE *gl = (TS_GLLE *)ts->data;
750: PetscFunctionBegin;
751: PetscCall(PetscFunctionListFind(TSGLLEAcceptList, type, &r));
752: PetscCheck(r, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TSGLLEAccept type \"%s\" given", type);
753: gl->Accept = r;
754: PetscCall(PetscStrncpy(gl->accept_name, type, sizeof(gl->accept_name)));
755: PetscFunctionReturn(PETSC_SUCCESS);
756: }
758: static PetscErrorCode TSGLLEGetAdapt_GLLE(TS ts, TSGLLEAdapt *adapt)
759: {
760: TS_GLLE *gl = (TS_GLLE *)ts->data;
762: PetscFunctionBegin;
763: if (!gl->adapt) {
764: PetscCall(TSGLLEAdaptCreate(PetscObjectComm((PetscObject)ts), &gl->adapt));
765: PetscCall(PetscObjectIncrementTabLevel((PetscObject)gl->adapt, (PetscObject)ts, 1));
766: }
767: *adapt = gl->adapt;
768: PetscFunctionReturn(PETSC_SUCCESS);
769: }
771: static PetscErrorCode TSGLLEChooseNextScheme(TS ts, PetscReal h, const PetscReal hmnorm[], PetscInt *next_scheme, PetscReal *next_h, PetscBool *finish)
772: {
773: TS_GLLE *gl = (TS_GLLE *)ts->data;
774: PetscInt i, n, cur_p, cur, next_sc, candidates[64], orders[64];
775: PetscReal errors[64], costs[64], tleft;
777: PetscFunctionBegin;
778: cur = -1;
779: cur_p = gl->schemes[gl->current_scheme]->p;
780: tleft = ts->max_time - (ts->ptime + ts->time_step);
781: for (i = 0, n = 0; i < gl->nschemes; i++) {
782: TSGLLEScheme sc = gl->schemes[i];
783: if (sc->p < gl->min_order || gl->max_order < sc->p) continue;
784: if (sc->p == cur_p - 1) errors[n] = PetscAbsScalar(sc->alpha[0]) * hmnorm[0];
785: else if (sc->p == cur_p) errors[n] = PetscAbsScalar(sc->alpha[0]) * hmnorm[1];
786: else if (sc->p == cur_p + 1) errors[n] = PetscAbsScalar(sc->alpha[0]) * (hmnorm[2] + hmnorm[3]);
787: else continue;
788: candidates[n] = i;
789: orders[n] = PetscMin(sc->p, sc->q); /* order of global truncation error */
790: costs[n] = sc->s; /* estimate the cost as the number of stages */
791: if (i == gl->current_scheme) cur = n;
792: n++;
793: }
794: PetscCheck(cur >= 0 && gl->nschemes > cur, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Current scheme not found in scheme list");
795: PetscCall(TSGLLEAdaptChoose(gl->adapt, n, orders, errors, costs, cur, h, tleft, &next_sc, next_h, finish));
796: *next_scheme = candidates[next_sc];
797: PetscCall(PetscInfo(ts, "Adapt chose scheme %" PetscInt_FMT " (%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT ") with step size %6.2e, finish=%s\n", *next_scheme, gl->schemes[*next_scheme]->p, gl->schemes[*next_scheme]->q,
798: gl->schemes[*next_scheme]->r, gl->schemes[*next_scheme]->s, (double)*next_h, PetscBools[*finish]));
799: PetscFunctionReturn(PETSC_SUCCESS);
800: }
802: static PetscErrorCode TSGLLEGetMaxSizes(TS ts, PetscInt *max_r, PetscInt *max_s)
803: {
804: TS_GLLE *gl = (TS_GLLE *)ts->data;
806: PetscFunctionBegin;
807: *max_r = gl->schemes[gl->nschemes - 1]->r;
808: *max_s = gl->schemes[gl->nschemes - 1]->s;
809: PetscFunctionReturn(PETSC_SUCCESS);
810: }
812: static PetscErrorCode TSSolve_GLLE(TS ts)
813: {
814: TS_GLLE *gl = (TS_GLLE *)ts->data;
815: PetscInt i, k, its, lits, max_r, max_s;
816: PetscBool final_step, finish;
817: SNESConvergedReason snesreason;
819: PetscFunctionBegin;
820: PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol));
822: PetscCall(TSGLLEGetMaxSizes(ts, &max_r, &max_s));
823: PetscCall(VecCopy(ts->vec_sol, gl->X[0]));
824: for (i = 1; i < max_r; i++) PetscCall(VecZeroEntries(gl->X[i]));
825: PetscCall(TSGLLEUpdateWRMS(ts));
827: if (0) {
828: /* Find consistent initial data for DAE */
829: gl->stage_time = ts->ptime + ts->time_step;
830: gl->scoeff = 1.;
831: gl->stage = 0;
833: PetscCall(VecCopy(ts->vec_sol, gl->Z));
834: PetscCall(VecCopy(ts->vec_sol, gl->Y));
835: PetscCall(SNESSolve(ts->snes, NULL, gl->Y));
836: PetscCall(SNESGetIterationNumber(ts->snes, &its));
837: PetscCall(SNESGetLinearSolveIterations(ts->snes, &lits));
838: PetscCall(SNESGetConvergedReason(ts->snes, &snesreason));
840: ts->snes_its += its;
841: ts->ksp_its += lits;
842: if (snesreason < 0 && ts->max_snes_failures != PETSC_UNLIMITED && ++ts->num_snes_failures >= ts->max_snes_failures) {
843: ts->reason = TS_DIVERGED_NONLINEAR_SOLVE;
844: PetscCall(PetscInfo(ts, "Step=%" PetscInt_FMT ", nonlinear solve failures %" PetscInt_FMT " greater than current TS allowed, stopping solve\n", ts->steps, ts->num_snes_failures));
845: PetscFunctionReturn(PETSC_SUCCESS);
846: }
847: }
849: PetscCheck(gl->current_scheme >= 0, PETSC_COMM_SELF, PETSC_ERR_ORDER, "A starting scheme has not been provided");
851: for (k = 0, final_step = PETSC_FALSE, finish = PETSC_FALSE; k < ts->max_steps && !finish; k++) {
852: PetscInt j, r, s, next_scheme = 0, rejections;
853: PetscReal h, hmnorm[4], enorm[3], next_h;
854: PetscBool accept;
855: const PetscScalar *c, *a, *u;
856: Vec *X, *Ydot, Y;
857: TSGLLEScheme scheme = gl->schemes[gl->current_scheme];
859: r = scheme->r;
860: s = scheme->s;
861: c = scheme->c;
862: a = scheme->a;
863: u = scheme->u;
864: h = ts->time_step;
865: X = gl->X;
866: Ydot = gl->Ydot;
867: Y = gl->Y;
869: if (ts->ptime > ts->max_time) break;
871: /*
872: We only call PreStep at the start of each STEP, not each STAGE. This is because it is
873: possible to fail (have to restart a step) after multiple stages.
874: */
875: PetscCall(TSPreStep(ts));
877: rejections = 0;
878: while (1) {
879: for (i = 0; i < s; i++) {
880: PetscScalar shift;
881: gl->scoeff = 1. / PetscRealPart(a[i * s + i]);
882: shift = gl->scoeff / ts->time_step;
883: gl->stage = i;
884: gl->stage_time = ts->ptime + PetscRealPart(c[i]) * h;
886: /*
887: * Stage equation: Y = h A Y' + U X
888: * We assume that A is lower-triangular so that we can solve the stages (Y,Y') sequentially
889: * Build the affine vector z_i = -[1/(h a_ii)](h sum_j a_ij y'_j + sum_j u_ij x_j)
890: * Then y'_i = z + 1/(h a_ii) y_i
891: */
892: PetscCall(VecZeroEntries(gl->Z));
893: for (j = 0; j < r; j++) PetscCall(VecAXPY(gl->Z, -shift * u[i * r + j], X[j]));
894: for (j = 0; j < i; j++) PetscCall(VecAXPY(gl->Z, -shift * h * a[i * s + j], Ydot[j]));
895: /* Note: Z is used within function evaluation, Ydot = Z + shift*Y */
897: /* Compute an estimate of Y to start Newton iteration */
898: if (gl->extrapolate) {
899: if (i == 0) {
900: /* Linear extrapolation on the first stage */
901: PetscCall(VecWAXPY(Y, c[i] * h, X[1], X[0]));
902: } else {
903: /* Linear extrapolation from the last stage */
904: PetscCall(VecAXPY(Y, (c[i] - c[i - 1]) * h, Ydot[i - 1]));
905: }
906: } else if (i == 0) { /* Directly use solution from the last step, otherwise reuse the last stage (do nothing) */
907: PetscCall(VecCopy(X[0], Y));
908: }
910: /* Solve this stage (Ydot[i] is computed during function evaluation) */
911: PetscCall(SNESSolve(ts->snes, NULL, Y));
912: PetscCall(SNESGetIterationNumber(ts->snes, &its));
913: PetscCall(SNESGetLinearSolveIterations(ts->snes, &lits));
914: PetscCall(SNESGetConvergedReason(ts->snes, &snesreason));
915: ts->snes_its += its;
916: ts->ksp_its += lits;
917: if (snesreason < 0 && ts->max_snes_failures != PETSC_UNLIMITED && ++ts->num_snes_failures >= ts->max_snes_failures) {
918: ts->reason = TS_DIVERGED_NONLINEAR_SOLVE;
919: PetscCall(PetscInfo(ts, "Step=%" PetscInt_FMT ", nonlinear solve failures %" PetscInt_FMT " greater than current TS allowed, stopping solve\n", ts->steps, ts->num_snes_failures));
920: PetscFunctionReturn(PETSC_SUCCESS);
921: }
922: }
924: gl->stage_time = ts->ptime + ts->time_step;
926: PetscCall((*gl->EstimateHigherMoments)(scheme, h, Ydot, gl->X, gl->himom));
927: /* hmnorm[i] = h^{p+i}x^{(p+i)} with i=0,1,2; hmnorm[3] = h^{p+2}(dx'/dx) x^{(p+1)} */
928: for (i = 0; i < 3; i++) PetscCall(TSGLLEVecNormWRMS(ts, gl->himom[i], &hmnorm[i + 1]));
929: enorm[0] = PetscRealPart(scheme->alpha[0]) * hmnorm[1];
930: enorm[1] = PetscRealPart(scheme->beta[0]) * hmnorm[2];
931: enorm[2] = PetscRealPart(scheme->gamma[0]) * hmnorm[3];
932: PetscCall((*gl->Accept)(ts, ts->max_time - gl->stage_time, h, enorm, &accept));
933: if (accept) goto accepted;
934: rejections++;
935: PetscCall(PetscInfo(ts, "Step %" PetscInt_FMT " (t=%g) not accepted, rejections=%" PetscInt_FMT "\n", k, (double)gl->stage_time, rejections));
936: if (rejections > gl->max_step_rejections) break;
937: /*
938: There are lots of reasons why a step might be rejected, including solvers not converging and other factors that
939: TSGLLEChooseNextScheme does not support. Additionally, the error estimates may be very screwed up, so I'm not
940: convinced that it's safe to just compute a new error estimate using the same interface as the current adaptor
941: (the adaptor interface probably has to change). Here we make an arbitrary and naive choice. This assumes that
942: steps were written in Nordsieck form. The "correct" method would be to re-complete the previous time step with
943: the correct "next" step size. It is unclear to me whether the present ad-hoc method of rescaling X is stable.
944: */
945: h *= 0.5;
946: for (i = 1; i < scheme->r; i++) PetscCall(VecScale(X[i], PetscPowRealInt(0.5, i)));
947: }
948: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_CONV_FAILED, "Time step %" PetscInt_FMT " (t=%g) not accepted after %" PetscInt_FMT " failures", k, (double)gl->stage_time, rejections);
950: accepted:
951: /* This term is not error, but it *would* be the leading term for a lower order method */
952: PetscCall(TSGLLEVecNormWRMS(ts, gl->X[scheme->r - 1], &hmnorm[0]));
953: /* Correct scaling so that these are equivalent to norms of the Nordsieck vectors */
955: PetscCall(PetscInfo(ts, "Last moment norm %10.2e, estimated error norms %10.2e %10.2e %10.2e\n", (double)hmnorm[0], (double)enorm[0], (double)enorm[1], (double)enorm[2]));
956: if (!final_step) {
957: PetscCall(TSGLLEChooseNextScheme(ts, h, hmnorm, &next_scheme, &next_h, &final_step));
958: } else {
959: /* Dummy values to complete the current step in a consistent manner */
960: next_scheme = gl->current_scheme;
961: next_h = h;
962: finish = PETSC_TRUE;
963: }
965: X = gl->Xold;
966: gl->Xold = gl->X;
967: gl->X = X;
968: PetscCall((*gl->CompleteStep)(scheme, h, gl->schemes[next_scheme], next_h, Ydot, gl->Xold, gl->X));
970: PetscCall(TSGLLEUpdateWRMS(ts));
972: /* Post the solution for the user, we could avoid this copy with a small bit of cleverness */
973: PetscCall(VecCopy(gl->X[0], ts->vec_sol));
974: ts->ptime += h;
975: ts->steps++;
977: PetscCall(TSPostEvaluate(ts));
978: PetscCall(TSPostStep(ts));
979: PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol));
981: gl->current_scheme = next_scheme;
982: ts->time_step = next_h;
983: }
984: PetscFunctionReturn(PETSC_SUCCESS);
985: }
987: static PetscErrorCode TSReset_GLLE(TS ts)
988: {
989: TS_GLLE *gl = (TS_GLLE *)ts->data;
990: PetscInt max_r, max_s;
992: PetscFunctionBegin;
993: if (gl->setupcalled) {
994: PetscCall(TSGLLEGetMaxSizes(ts, &max_r, &max_s));
995: PetscCall(VecDestroyVecs(max_r, &gl->Xold));
996: PetscCall(VecDestroyVecs(max_r, &gl->X));
997: PetscCall(VecDestroyVecs(max_s, &gl->Ydot));
998: PetscCall(VecDestroyVecs(3, &gl->himom));
999: PetscCall(VecDestroy(&gl->W));
1000: PetscCall(VecDestroy(&gl->Y));
1001: PetscCall(VecDestroy(&gl->Z));
1002: }
1003: PetscCall(TSGLLEAdaptDestroy(&gl->adapt));
1004: if (gl->Destroy) PetscCall((*gl->Destroy)(gl));
1005: gl->setupcalled = PETSC_FALSE;
1006: PetscFunctionReturn(PETSC_SUCCESS);
1007: }
1009: static PetscErrorCode TSDestroy_GLLE(TS ts)
1010: {
1011: PetscFunctionBegin;
1012: PetscCall(TSReset_GLLE(ts));
1013: if (ts->dm) {
1014: PetscCall(DMCoarsenHookRemove(ts->dm, DMCoarsenHook_TSGLLE, DMRestrictHook_TSGLLE, ts));
1015: PetscCall(DMSubDomainHookRemove(ts->dm, DMSubDomainHook_TSGLLE, DMSubDomainRestrictHook_TSGLLE, ts));
1016: }
1017: PetscCall(PetscFree(ts->data));
1018: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSGLLESetType_C", NULL));
1019: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSGLLESetAcceptType_C", NULL));
1020: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSGLLEGetAdapt_C", NULL));
1021: PetscFunctionReturn(PETSC_SUCCESS);
1022: }
1024: /*
1025: This defines the nonlinear equation that is to be solved with SNES
1026: g(x) = f(t,x,z+shift*x) = 0
1027: */
1028: static PetscErrorCode SNESTSFormFunction_GLLE(SNES snes, Vec x, Vec f, TS ts)
1029: {
1030: TS_GLLE *gl = (TS_GLLE *)ts->data;
1031: Vec Z, Ydot;
1032: DM dm, dmsave;
1034: PetscFunctionBegin;
1035: PetscCall(SNESGetDM(snes, &dm));
1036: PetscCall(TSGLLEGetVecs(ts, dm, &Z, &Ydot));
1037: PetscCall(VecWAXPY(Ydot, gl->scoeff / ts->time_step, x, Z));
1038: dmsave = ts->dm;
1039: ts->dm = dm;
1040: PetscCall(TSComputeIFunction(ts, gl->stage_time, x, Ydot, f, PETSC_FALSE));
1041: ts->dm = dmsave;
1042: PetscCall(TSGLLERestoreVecs(ts, dm, &Z, &Ydot));
1043: PetscFunctionReturn(PETSC_SUCCESS);
1044: }
1046: static PetscErrorCode SNESTSFormJacobian_GLLE(SNES snes, Vec x, Mat A, Mat B, TS ts)
1047: {
1048: TS_GLLE *gl = (TS_GLLE *)ts->data;
1049: Vec Z, Ydot;
1050: DM dm, dmsave;
1052: PetscFunctionBegin;
1053: PetscCall(SNESGetDM(snes, &dm));
1054: PetscCall(TSGLLEGetVecs(ts, dm, &Z, &Ydot));
1055: dmsave = ts->dm;
1056: ts->dm = dm;
1057: /* gl->Xdot will have already been computed in SNESTSFormFunction_GLLE */
1058: PetscCall(TSComputeIJacobian(ts, gl->stage_time, x, gl->Ydot[gl->stage], gl->scoeff / ts->time_step, A, B, PETSC_FALSE));
1059: ts->dm = dmsave;
1060: PetscCall(TSGLLERestoreVecs(ts, dm, &Z, &Ydot));
1061: PetscFunctionReturn(PETSC_SUCCESS);
1062: }
1064: static PetscErrorCode TSSetUp_GLLE(TS ts)
1065: {
1066: TS_GLLE *gl = (TS_GLLE *)ts->data;
1067: PetscInt max_r, max_s;
1068: DM dm;
1070: PetscFunctionBegin;
1071: if (!gl->type_name[0]) PetscCall(TSGLLESetType(ts, TSGLLE_IRKS));
1072: gl->setupcalled = PETSC_TRUE;
1073: PetscCall(TSGLLEGetMaxSizes(ts, &max_r, &max_s));
1074: PetscCall(VecDuplicateVecs(ts->vec_sol, max_r, &gl->X));
1075: PetscCall(VecDuplicateVecs(ts->vec_sol, max_r, &gl->Xold));
1076: PetscCall(VecDuplicateVecs(ts->vec_sol, max_s, &gl->Ydot));
1077: PetscCall(VecDuplicateVecs(ts->vec_sol, 3, &gl->himom));
1078: PetscCall(VecDuplicate(ts->vec_sol, &gl->W));
1079: PetscCall(VecDuplicate(ts->vec_sol, &gl->Y));
1080: PetscCall(VecDuplicate(ts->vec_sol, &gl->Z));
1082: /* Default acceptance tests and adaptivity */
1083: if (!gl->Accept) PetscCall(TSGLLESetAcceptType(ts, TSGLLEACCEPT_ALWAYS));
1084: if (!gl->adapt) PetscCall(TSGLLEGetAdapt(ts, &gl->adapt));
1086: if (gl->current_scheme < 0) {
1087: PetscInt i;
1088: for (i = 0;; i++) {
1089: if (gl->schemes[i]->p == gl->start_order) break;
1090: PetscCheck(i + 1 != gl->nschemes, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No schemes available with requested start order %" PetscInt_FMT, i);
1091: }
1092: gl->current_scheme = i;
1093: }
1094: PetscCall(TSGetDM(ts, &dm));
1095: PetscCall(DMCoarsenHookAdd(dm, DMCoarsenHook_TSGLLE, DMRestrictHook_TSGLLE, ts));
1096: PetscCall(DMSubDomainHookAdd(dm, DMSubDomainHook_TSGLLE, DMSubDomainRestrictHook_TSGLLE, ts));
1097: PetscFunctionReturn(PETSC_SUCCESS);
1098: }
1100: static PetscErrorCode TSSetFromOptions_GLLE(TS ts, PetscOptionItems PetscOptionsObject)
1101: {
1102: TS_GLLE *gl = (TS_GLLE *)ts->data;
1103: char tname[256] = TSGLLE_IRKS, completef[256] = "rescale-and-modify";
1105: PetscFunctionBegin;
1106: PetscOptionsHeadBegin(PetscOptionsObject, "General Linear ODE solver options");
1107: {
1108: PetscBool flg;
1109: PetscCall(PetscOptionsFList("-ts_gl_type", "Type of GL method", "TSGLLESetType", TSGLLEList, gl->type_name[0] ? gl->type_name : tname, tname, sizeof(tname), &flg));
1110: if (flg || !gl->type_name[0]) PetscCall(TSGLLESetType(ts, tname));
1111: PetscCall(PetscOptionsInt("-ts_gl_max_step_rejections", "Maximum number of times to attempt a step", "None", gl->max_step_rejections, &gl->max_step_rejections, NULL));
1112: PetscCall(PetscOptionsInt("-ts_gl_max_order", "Maximum order to try", "TSGLLESetMaxOrder", gl->max_order, &gl->max_order, NULL));
1113: PetscCall(PetscOptionsInt("-ts_gl_min_order", "Minimum order to try", "TSGLLESetMinOrder", gl->min_order, &gl->min_order, NULL));
1114: PetscCall(PetscOptionsInt("-ts_gl_start_order", "Initial order to try", "TSGLLESetMinOrder", gl->start_order, &gl->start_order, NULL));
1115: PetscCall(PetscOptionsEnum("-ts_gl_error_direction", "Which direction to look when estimating error", "TSGLLESetErrorDirection", TSGLLEErrorDirections, (PetscEnum)gl->error_direction, (PetscEnum *)&gl->error_direction, NULL));
1116: PetscCall(PetscOptionsBool("-ts_gl_extrapolate", "Extrapolate stage solution from previous solution (sometimes unstable)", "TSGLLESetExtrapolate", gl->extrapolate, &gl->extrapolate, NULL));
1117: PetscCall(PetscOptionsReal("-ts_gl_atol", "Absolute tolerance", "TSGLLESetTolerances", gl->wrms_atol, &gl->wrms_atol, NULL));
1118: PetscCall(PetscOptionsReal("-ts_gl_rtol", "Relative tolerance", "TSGLLESetTolerances", gl->wrms_rtol, &gl->wrms_rtol, NULL));
1119: PetscCall(PetscOptionsString("-ts_gl_complete", "Method to use for completing the step", "none", completef, completef, sizeof(completef), &flg));
1120: if (flg) {
1121: PetscBool match1, match2;
1122: PetscCall(PetscStrcmp(completef, "rescale", &match1));
1123: PetscCall(PetscStrcmp(completef, "rescale-and-modify", &match2));
1124: if (match1) gl->CompleteStep = TSGLLECompleteStep_Rescale;
1125: else if (match2) gl->CompleteStep = TSGLLECompleteStep_RescaleAndModify;
1126: else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_UNKNOWN_TYPE, "%s", completef);
1127: }
1128: {
1129: char type[256] = TSGLLEACCEPT_ALWAYS;
1130: PetscCall(PetscOptionsFList("-ts_gl_accept_type", "Method to use for determining whether to accept a step", "TSGLLESetAcceptType", TSGLLEAcceptList, gl->accept_name[0] ? gl->accept_name : type, type, sizeof(type), &flg));
1131: if (flg || !gl->accept_name[0]) PetscCall(TSGLLESetAcceptType(ts, type));
1132: }
1133: {
1134: TSGLLEAdapt adapt;
1135: PetscCall(TSGLLEGetAdapt(ts, &adapt));
1136: PetscCall(TSGLLEAdaptSetFromOptions(adapt, PetscOptionsObject));
1137: }
1138: }
1139: PetscOptionsHeadEnd();
1140: PetscFunctionReturn(PETSC_SUCCESS);
1141: }
1143: static PetscErrorCode TSView_GLLE(TS ts, PetscViewer viewer)
1144: {
1145: TS_GLLE *gl = (TS_GLLE *)ts->data;
1146: PetscBool isascii, details;
1148: PetscFunctionBegin;
1149: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1150: if (isascii) {
1151: PetscCall(PetscViewerASCIIPrintf(viewer, " min order %" PetscInt_FMT ", max order %" PetscInt_FMT ", current order %" PetscInt_FMT "\n", gl->min_order, gl->max_order, gl->schemes[gl->current_scheme]->p));
1152: PetscCall(PetscViewerASCIIPrintf(viewer, " Error estimation: %s\n", TSGLLEErrorDirections[gl->error_direction]));
1153: PetscCall(PetscViewerASCIIPrintf(viewer, " Extrapolation: %s\n", gl->extrapolate ? "yes" : "no"));
1154: PetscCall(PetscViewerASCIIPrintf(viewer, " Acceptance test: %s\n", gl->accept_name[0] ? gl->accept_name : "(not yet set)"));
1155: PetscCall(PetscViewerASCIIPushTab(viewer));
1156: PetscCall(TSGLLEAdaptView(gl->adapt, viewer));
1157: PetscCall(PetscViewerASCIIPopTab(viewer));
1158: PetscCall(PetscViewerASCIIPrintf(viewer, " type: %s\n", gl->type_name[0] ? gl->type_name : "(not yet set)"));
1159: PetscCall(PetscViewerASCIIPrintf(viewer, "Schemes within family (%" PetscInt_FMT "):\n", gl->nschemes));
1160: details = PETSC_FALSE;
1161: PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_gl_view_detailed", &details, NULL));
1162: PetscCall(PetscViewerASCIIPushTab(viewer));
1163: for (PetscInt i = 0; i < gl->nschemes; i++) PetscCall(TSGLLESchemeView(gl->schemes[i], details, viewer));
1164: if (gl->View) PetscCall((*gl->View)(gl, viewer));
1165: PetscCall(PetscViewerASCIIPopTab(viewer));
1166: }
1167: PetscFunctionReturn(PETSC_SUCCESS);
1168: }
1170: /*@C
1171: TSGLLERegister - adds a `TSGLLE` implementation
1173: Not Collective, No Fortran Support
1175: Input Parameters:
1176: + sname - name of user-defined general linear scheme
1177: - function - routine to create method context
1179: Level: advanced
1181: Note:
1182: `TSGLLERegister()` may be called multiple times to add several user-defined families.
1184: Example Usage:
1185: .vb
1186: TSGLLERegister("my_scheme", MySchemeCreate);
1187: .ve
1189: Then, your scheme can be chosen with the procedural interface via
1190: .vb
1191: TSGLLESetType(ts, "my_scheme")
1192: .ve
1193: or at runtime via the option
1194: .vb
1195: -ts_gl_type my_scheme
1196: .ve
1198: .seealso: [](ch_ts), `TSGLLE`, `TSGLLEType`, `TSGLLERegisterAll()`, `TSGLLESetType()`
1199: @*/
1200: PetscErrorCode TSGLLERegister(const char sname[], PetscErrorCode (*function)(TS))
1201: {
1202: PetscFunctionBegin;
1203: PetscCall(TSGLLEInitializePackage());
1204: PetscCall(PetscFunctionListAdd(&TSGLLEList, sname, function));
1205: PetscFunctionReturn(PETSC_SUCCESS);
1206: }
1208: /*@C
1209: TSGLLEAcceptRegister - adds a `TSGLLE` acceptance scheme
1211: Not Collective
1213: Input Parameters:
1214: + sname - name of user-defined acceptance scheme
1215: - function - routine to create method context, see `TSGLLEAcceptFn` for the calling sequence
1217: Level: advanced
1219: Note:
1220: `TSGLLEAcceptRegister()` may be called multiple times to add several user-defined families.
1222: Example Usage:
1223: .vb
1224: TSGLLEAcceptRegister("my_scheme", MySchemeCreate);
1225: .ve
1227: Then, your scheme can be chosen with the procedural interface via
1228: .vb
1229: TSGLLESetAcceptType(ts, "my_scheme")
1230: .ve
1231: or at runtime via the option `-ts_gl_accept_type my_scheme`
1233: .seealso: [](ch_ts), `TSGLLE`, `TSGLLEType`, `TSGLLERegisterAll()`, `TSGLLEAcceptFn`
1234: @*/
1235: PetscErrorCode TSGLLEAcceptRegister(const char sname[], TSGLLEAcceptFn *function)
1236: {
1237: PetscFunctionBegin;
1238: PetscCall(PetscFunctionListAdd(&TSGLLEAcceptList, sname, function));
1239: PetscFunctionReturn(PETSC_SUCCESS);
1240: }
1242: /*@C
1243: TSGLLERegisterAll - Registers all of the general linear methods in `TSGLLE`
1245: Not Collective
1247: Level: advanced
1249: .seealso: [](ch_ts), `TSGLLE`, `TSGLLERegisterDestroy()`
1250: @*/
1251: PetscErrorCode TSGLLERegisterAll(void)
1252: {
1253: PetscFunctionBegin;
1254: if (TSGLLERegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
1255: TSGLLERegisterAllCalled = PETSC_TRUE;
1257: PetscCall(TSGLLERegister(TSGLLE_IRKS, TSGLLECreate_IRKS));
1258: PetscCall(TSGLLEAcceptRegister(TSGLLEACCEPT_ALWAYS, TSGLLEAccept_Always));
1259: PetscFunctionReturn(PETSC_SUCCESS);
1260: }
1262: /*@C
1263: TSGLLEInitializePackage - This function initializes everything in the `TSGLLE` package. It is called
1264: from `TSInitializePackage()`.
1266: Level: developer
1268: .seealso: [](ch_ts), `PetscInitialize()`, `TSInitializePackage()`, `TSGLLEFinalizePackage()`
1269: @*/
1270: PetscErrorCode TSGLLEInitializePackage(void)
1271: {
1272: PetscFunctionBegin;
1273: if (TSGLLEPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
1274: TSGLLEPackageInitialized = PETSC_TRUE;
1275: PetscCall(TSGLLERegisterAll());
1276: PetscCall(PetscRegisterFinalize(TSGLLEFinalizePackage));
1277: PetscFunctionReturn(PETSC_SUCCESS);
1278: }
1280: /*@C
1281: TSGLLEFinalizePackage - This function destroys everything in the `TSGLLE` package. It is
1282: called from `PetscFinalize()`.
1284: Level: developer
1286: .seealso: [](ch_ts), `PetscFinalize()`, `TSGLLEInitializePackage()`, `TSInitializePackage()`
1287: @*/
1288: PetscErrorCode TSGLLEFinalizePackage(void)
1289: {
1290: PetscFunctionBegin;
1291: PetscCall(PetscFunctionListDestroy(&TSGLLEList));
1292: PetscCall(PetscFunctionListDestroy(&TSGLLEAcceptList));
1293: TSGLLEPackageInitialized = PETSC_FALSE;
1294: TSGLLERegisterAllCalled = PETSC_FALSE;
1295: PetscFunctionReturn(PETSC_SUCCESS);
1296: }
1298: /*MC
1299: TSGLLE - DAE solver using implicit General Linear methods {cite}`butcher_2007` {cite}`butcher2016numerical`
1301: Options Database Keys:
1302: + -ts_gl_type (irks) - the class of general linear method
1303: . -ts_gl_rtol tol - relative error
1304: . -ts_gl_atol tol - absolute error
1305: . -ts_gl_min_order p - minimum order method to consider (default=1)
1306: . -ts_gl_max_order p - maximum order method to consider (default=3)
1307: . -ts_gl_start_order p - order of starting method (default=1)
1308: . -ts_gl_complete (rescale|rescale-and-modify) - method to use for completing the step (rescale-and-modify or rescale)
1309: - -ts_adapt_type (basic|dsp|none|cfl|glee|history) - adaptive controller to use (none step both)
1311: Level: beginner
1313: Notes:
1314: These methods contain Runge-Kutta and multistep schemes as special cases. These special cases
1315: have some fundamental limitations. For example, diagonally implicit Runge-Kutta cannot have
1316: stage order greater than 1 which limits their applicability to very stiff systems.
1317: Meanwhile, multistep methods cannot be A-stable for order greater than 2 and BDF are not
1318: 0-stable for order greater than 6. GL methods can be A- and L-stable with arbitrarily high
1319: stage order and reliable error estimates for both 1 and 2 orders higher to facilitate
1320: adaptive step sizes and adaptive order schemes. All this is possible while preserving a
1321: singly diagonally implicit structure.
1323: This integrator can be applied to DAE.
1325: Diagonally implicit general linear (DIGL) methods are a generalization of diagonally implicit
1326: Runge-Kutta (DIRK). They are represented by the tableau
1328: .vb
1329: A | U
1330: -------
1331: B | V
1332: .ve
1334: combined with a vector c of abscissa. "Diagonally implicit" means that $A$ is lower
1335: triangular. A step of the general method reads
1337: $$
1338: \begin{align*}
1339: [ Y ] = [A U] [ Y' ] \\
1340: [X^k] = [B V] [X^{k-1}]
1341: \end{align*}
1342: $$
1344: where Y is the multivector of stage values, $Y'$ is the multivector of stage derivatives, $X^k$
1345: is the Nordsieck vector of the solution at step $k$. The Nordsieck vector consists of the first
1346: $r$ moments of the solution, given by
1348: $$
1349: X = [x_0,x_1,...,x_{r-1}] = [x, h x', h^2 x'', ..., h^{r-1} x^{(r-1)} ]
1350: $$
1352: If $A$ is lower triangular, we can solve the stages $(Y, Y')$ sequentially
1354: $$
1355: y_i = h \sum_{j=0}^{s-1} (a_{ij} y'_j) + \sum_{j=0}^{r-1} u_{ij} x_j, \, \, i=0,...,{s-1}
1356: $$
1358: and then construct the pieces to carry to the next step
1360: $$
1361: xx_i = h \sum_{j=0}^{s-1} b_{ij} y'_j + \sum_{j=0}^{r-1} v_{ij} x_j, \, \, i=0,...,{r-1}
1362: $$
1364: Note that when the equations are cast in implicit form, we are using the stage equation to
1365: define $y'_i$ in terms of $y_i$ and known stuff ($y_j$ for $j<i$ and $x_j$ for all $j$).
1367: Error estimation
1369: At present, the most attractive GL methods for stiff problems are singly diagonally implicit
1370: schemes which posses Inherent Runge-Kutta Stability (`TSIRKS`). These methods have $r=s$, the
1371: number of items passed between steps is equal to the number of stages. The order and
1372: stage-order are one less than the number of stages. We use the error estimates in the 2007
1373: paper which provide the following estimates
1375: $$
1376: \begin{align*}
1377: h^{p+1} X^{(p+1)} = \phi_0^T Y' + [0 \psi_0^T] Xold \\
1378: h^{p+2} X^{(p+2)} = \phi_1^T Y' + [0 \psi_1^T] Xold \\
1379: h^{p+2} (dx'/dx) X^{(p+1)} = \phi_2^T Y' + [0 \psi_2^T] Xold
1380: \end{align*}
1381: $$
1383: These estimates are accurate to $ O(h^{p+3})$.
1385: Changing the step size
1387: Uses the generalized "rescale and modify" scheme, see equation (4.5) of {cite}`butcher_2007`.
1389: .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSSetType()`, `TSType`, `TSGLLEType`, `TSGLLESetType()`, `TSGLLESetAcceptType()`, `TSGLLEGetAdapt()`
1390: M*/
1391: PETSC_EXTERN PetscErrorCode TSCreate_GLLE(TS ts)
1392: {
1393: TS_GLLE *gl;
1395: PetscFunctionBegin;
1396: PetscCall(TSGLLEInitializePackage());
1398: PetscCall(PetscNew(&gl));
1399: ts->data = (void *)gl;
1401: ts->ops->reset = TSReset_GLLE;
1402: ts->ops->destroy = TSDestroy_GLLE;
1403: ts->ops->view = TSView_GLLE;
1404: ts->ops->setup = TSSetUp_GLLE;
1405: ts->ops->solve = TSSolve_GLLE;
1406: ts->ops->setfromoptions = TSSetFromOptions_GLLE;
1407: ts->ops->snesfunction = SNESTSFormFunction_GLLE;
1408: ts->ops->snesjacobian = SNESTSFormJacobian_GLLE;
1410: ts->usessnes = PETSC_TRUE;
1412: gl->max_step_rejections = 1;
1413: gl->min_order = 1;
1414: gl->max_order = 3;
1415: gl->start_order = 1;
1416: gl->current_scheme = -1;
1417: gl->extrapolate = PETSC_FALSE;
1419: gl->wrms_atol = 1e-8;
1420: gl->wrms_rtol = 1e-5;
1422: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSGLLESetType_C", &TSGLLESetType_GLLE));
1423: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSGLLESetAcceptType_C", &TSGLLESetAcceptType_GLLE));
1424: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSGLLEGetAdapt_C", &TSGLLEGetAdapt_GLLE));
1425: PetscFunctionReturn(PETSC_SUCCESS);
1426: }