Actual source code: tsdiscgrad.c
1: /*
2: Code for timestepping with discrete gradient integrators
3: */
4: #include <petsc/private/tsimpl.h>
5: #include <petscdm.h>
6: #include <petsc/private/snesimpl.h>
8: PetscBool DGCite = PETSC_FALSE;
9: const char DGCitation[] = "@article{Gonzalez1996,\n"
10: " title = {Time integration and discrete Hamiltonian systems},\n"
11: " author = {Oscar Gonzalez},\n"
12: " journal = {Journal of Nonlinear Science},\n"
13: " volume = {6},\n"
14: " pages = {449--467},\n"
15: " doi = {10.1007/978-1-4612-1246-1_10},\n"
16: " year = {1996}\n}\n";
18: const char *DGTypes[] = {"gonzalez", "average", "none", "TSDGType", "DG_", NULL};
20: typedef struct {
21: PetscReal stage_time;
22: Vec X0, X, Xdot;
23: void *funcCtx;
24: TSDGType discgrad; /* Type of electrostatic model */
25: PetscErrorCode (*Sfunc)(TS, PetscReal, Vec, Mat, void *);
26: PetscErrorCode (*Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *);
27: PetscErrorCode (*Gfunc)(TS, PetscReal, Vec, Vec, void *);
28: PetscErrorCode (*IGfunc)(TS, PetscReal, Vec, Vec, Vec, void *);
29: PetscErrorCode (*IGjac)(TS, PetscReal, Vec, Vec, PetscReal, Mat, Mat, void *);
30: } TS_DiscGrad;
32: /*@
33: TSDiscGradGetX0AndXdot - Gets the last solution and current time derivative held inside the `TS`
35: Collective
37: Input Parameters:
38: + ts - The `TS`
39: - dm - The `DM`, or `NULL` to use the embedded `DM`
41: Output Parameters:
42: + X0 - The solution from the last timestep
43: - Xdot - The current solution time derivative
45: Level: advanced
47: .seealso: [](ch_ts), `TSDISCGRAD`, `TSDiscGradRestoreX0AndXdot()`
48: @*/
49: PetscErrorCode TSDiscGradGetX0AndXdot(TS ts, DM dm, Vec *X0, Vec *Xdot)
50: {
51: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
53: PetscFunctionBegin;
54: if (X0) {
55: if (dm && dm != ts->dm) PetscCall(DMGetNamedGlobalVector(dm, "TSDiscGrad_X0", X0));
56: else *X0 = ts->vec_sol;
57: }
58: if (Xdot) {
59: if (dm && dm != ts->dm) PetscCall(DMGetNamedGlobalVector(dm, "TSDiscGrad_Xdot", Xdot));
60: else *Xdot = dg->Xdot;
61: }
62: PetscFunctionReturn(PETSC_SUCCESS);
63: }
65: /*@
66: TSDiscGradRestoreX0AndXdot - Restores the last solution and current time derivative held inside the `TS`
68: Collective
70: Input Parameters:
71: + ts - The `TS`
72: . dm - The `DM`, or `NULL` to use the embedded `DM`
73: . X0 - The solution from the last timestep
74: - Xdot - The current solution time derivative
76: Level: advanced
78: .seealso: [](ch_ts), `TSDISCGRAD`, `TSDiscGradGetX0AndXdot()`
79: @*/
80: PetscErrorCode TSDiscGradRestoreX0AndXdot(TS ts, DM dm, Vec *X0, Vec *Xdot)
81: {
82: PetscFunctionBegin;
83: if (X0) {
84: if (dm && dm != ts->dm) PetscCall(DMRestoreNamedGlobalVector(dm, "TSDiscGrad_X0", X0));
85: }
86: if (Xdot) {
87: if (dm && dm != ts->dm) PetscCall(DMRestoreNamedGlobalVector(dm, "TSDiscGrad_Xdot", Xdot));
88: }
89: PetscFunctionReturn(PETSC_SUCCESS);
90: }
92: static PetscErrorCode DestroyInnerTS_Private(PetscCtxRt ptr)
93: {
94: PetscFunctionBegin;
95: PetscCall(TSDestroy((TS *)ptr));
96: PetscFunctionReturn(PETSC_SUCCESS);
97: }
99: static PetscErrorCode DMCoarsenHook_TSDiscGrad(DM fine, DM coarse, PetscCtx ctx)
100: {
101: DMSNES dmsnesFine, dmsnesCoarse;
103: PetscFunctionBegin;
104: PetscCall(DMGetDMSNES(fine, &dmsnesFine));
105: PetscCall(DMGetDMSNES(coarse, &dmsnesCoarse));
106: if (dmsnesCoarse->ops->computefunction == SNESTSFormFunction) {
107: // The context for this callback is a TS, which we need to recreate for the coarse problem
108: TS tsFine, tsCoarse;
109: MPI_Comm comm;
111: PetscCall(PetscContainerGetPointer(dmsnesFine->functionctxcontainer, &tsFine));
112: PetscCall(TSCreate(PetscObjectComm((PetscObject)tsFine), &tsCoarse));
113: PetscCall(TSSetType(tsCoarse, TSDISCGRAD));
114: PetscCall(TSSetDM(tsCoarse, coarse));
115: PetscCall(TSSetFromOptions(tsCoarse));
116: PetscCall(TSSetUp(tsCoarse));
117: {
118: TS_DiscGrad *dgFine = (TS_DiscGrad *)tsFine->data;
119: TS_DiscGrad *dgCoarse = (TS_DiscGrad *)tsCoarse->data;
121: tsCoarse->steps = tsFine->steps;
122: tsCoarse->ptime = tsFine->ptime;
123: tsCoarse->time_step = tsFine->time_step;
124: tsCoarse->time_step0 = tsFine->time_step0;
125: tsCoarse->ptime_prev = tsFine->ptime_prev;
126: tsCoarse->ptime_prev_rollback = tsFine->ptime_prev_rollback;
127: tsCoarse->solvetime = tsFine->solvetime;
129: dgCoarse->stage_time = dgFine->stage_time;
130: dgCoarse->funcCtx = dgFine->funcCtx;
131: dgCoarse->discgrad = dgFine->discgrad;
132: dgCoarse->Sfunc = dgFine->Sfunc;
133: dgCoarse->Gfunc = dgFine->Gfunc;
134: dgCoarse->Ffunc = dgFine->Ffunc;
135: dgCoarse->IGfunc = dgFine->IGfunc;
136: dgCoarse->IGjac = dgFine->IGjac;
137: }
138: PetscCall(PetscObjectGetComm((PetscObject)dmsnesCoarse->functionctxcontainer, &comm));
139: // Check this destruction for memory problems
140: PetscCall(PetscContainerDestroy(&dmsnesCoarse->functionctxcontainer));
141: PetscCall(PetscContainerCreate(comm, &dmsnesCoarse->functionctxcontainer));
142: PetscCall(PetscContainerSetPointer(dmsnesCoarse->functionctxcontainer, tsCoarse));
143: PetscCall(PetscContainerSetCtxDestroy(dmsnesCoarse->functionctxcontainer, DestroyInnerTS_Private));
144: }
145: PetscFunctionReturn(PETSC_SUCCESS);
146: }
148: static PetscErrorCode DMRestrictHook_TSDiscGrad(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse, PetscCtx ctx)
149: {
150: TS ts = (TS)ctx;
151: Vec X0, Xdot, X0_c, Xdot_c;
153: PetscFunctionBegin;
154: PetscCall(TSDiscGradGetX0AndXdot(ts, fine, &X0, &Xdot));
155: PetscCall(TSDiscGradGetX0AndXdot(ts, coarse, &X0_c, &Xdot_c));
156: PetscCall(MatRestrict(restrct, X0, X0_c));
157: PetscCall(MatRestrict(restrct, Xdot, Xdot_c));
158: PetscCall(VecPointwiseMult(X0_c, rscale, X0_c));
159: PetscCall(VecPointwiseMult(Xdot_c, rscale, Xdot_c));
160: PetscCall(TSDiscGradRestoreX0AndXdot(ts, fine, &X0, &Xdot));
161: PetscCall(TSDiscGradRestoreX0AndXdot(ts, coarse, &X0_c, &Xdot_c));
162: PetscFunctionReturn(PETSC_SUCCESS);
163: }
165: static PetscErrorCode DMSubDomainHook_TSDiscGrad(DM dm, DM subdm, PetscCtx ctx)
166: {
167: PetscFunctionBegin;
168: PetscFunctionReturn(PETSC_SUCCESS);
169: }
171: static PetscErrorCode DMSubDomainRestrictHook_TSDiscGrad(DM dm, VecScatter gscat, VecScatter lscat, DM subdm, PetscCtx ctx)
172: {
173: TS ts = (TS)ctx;
174: Vec X0, Xdot, X0_sub, Xdot_sub;
176: PetscFunctionBegin;
177: PetscCall(TSDiscGradGetX0AndXdot(ts, dm, &X0, &Xdot));
178: PetscCall(TSDiscGradGetX0AndXdot(ts, subdm, &X0_sub, &Xdot_sub));
180: PetscCall(VecScatterBegin(gscat, X0, X0_sub, INSERT_VALUES, SCATTER_FORWARD));
181: PetscCall(VecScatterEnd(gscat, X0, X0_sub, INSERT_VALUES, SCATTER_FORWARD));
183: PetscCall(VecScatterBegin(gscat, Xdot, Xdot_sub, INSERT_VALUES, SCATTER_FORWARD));
184: PetscCall(VecScatterEnd(gscat, Xdot, Xdot_sub, INSERT_VALUES, SCATTER_FORWARD));
186: PetscCall(TSDiscGradRestoreX0AndXdot(ts, dm, &X0, &Xdot));
187: PetscCall(TSDiscGradRestoreX0AndXdot(ts, subdm, &X0_sub, &Xdot_sub));
188: PetscFunctionReturn(PETSC_SUCCESS);
189: }
191: static PetscErrorCode TSSetUp_DiscGrad(TS ts)
192: {
193: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
194: DM dm;
196: PetscFunctionBegin;
197: if (!dg->X) PetscCall(VecDuplicate(ts->vec_sol, &dg->X));
198: if (!dg->X0) PetscCall(VecDuplicate(ts->vec_sol, &dg->X0));
199: if (!dg->Xdot) PetscCall(VecDuplicate(ts->vec_sol, &dg->Xdot));
201: PetscCall(TSGetDM(ts, &dm));
202: PetscCall(DMCoarsenHookAdd(dm, DMCoarsenHook_TSDiscGrad, DMRestrictHook_TSDiscGrad, ts));
203: PetscCall(DMSubDomainHookAdd(dm, DMSubDomainHook_TSDiscGrad, DMSubDomainRestrictHook_TSDiscGrad, ts));
204: PetscFunctionReturn(PETSC_SUCCESS);
205: }
207: static PetscErrorCode TSSetFromOptions_DiscGrad(TS ts, PetscOptionItems PetscOptionsObject)
208: {
209: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
211: PetscFunctionBegin;
212: PetscOptionsHeadBegin(PetscOptionsObject, "Discrete Gradients ODE solver options");
213: {
214: PetscCall(PetscOptionsEnum("-ts_discgrad_type", "Type of discrete gradient solver", "TSDiscGradSetDGType", DGTypes, (PetscEnum)dg->discgrad, (PetscEnum *)&dg->discgrad, NULL));
215: }
216: PetscOptionsHeadEnd();
217: PetscFunctionReturn(PETSC_SUCCESS);
218: }
220: static PetscErrorCode TSView_DiscGrad(TS ts, PetscViewer viewer)
221: {
222: PetscBool isascii;
224: PetscFunctionBegin;
225: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
226: if (isascii) PetscCall(PetscViewerASCIIPrintf(viewer, " Discrete Gradients\n"));
227: PetscFunctionReturn(PETSC_SUCCESS);
228: }
230: static PetscErrorCode TSDiscGradGetType_DiscGrad(TS ts, TSDGType *dgtype)
231: {
232: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
234: PetscFunctionBegin;
235: *dgtype = dg->discgrad;
236: PetscFunctionReturn(PETSC_SUCCESS);
237: }
239: static PetscErrorCode TSDiscGradSetType_DiscGrad(TS ts, TSDGType dgtype)
240: {
241: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
243: PetscFunctionBegin;
244: dg->discgrad = dgtype;
245: PetscFunctionReturn(PETSC_SUCCESS);
246: }
248: static PetscErrorCode TSReset_DiscGrad(TS ts)
249: {
250: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
252: PetscFunctionBegin;
253: PetscCall(VecDestroy(&dg->X));
254: PetscCall(VecDestroy(&dg->X0));
255: PetscCall(VecDestroy(&dg->Xdot));
256: PetscFunctionReturn(PETSC_SUCCESS);
257: }
259: static PetscErrorCode TSDestroy_DiscGrad(TS ts)
260: {
261: DM dm;
263: PetscFunctionBegin;
264: PetscCall(TSReset_DiscGrad(ts));
265: PetscCall(TSGetDM(ts, &dm));
266: if (dm) {
267: PetscCall(DMCoarsenHookRemove(dm, DMCoarsenHook_TSDiscGrad, DMRestrictHook_TSDiscGrad, ts));
268: PetscCall(DMSubDomainHookRemove(dm, DMSubDomainHook_TSDiscGrad, DMSubDomainRestrictHook_TSDiscGrad, ts));
269: }
270: PetscCall(PetscFree(ts->data));
271: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradGetFormulation_C", NULL));
272: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradSetFormulation_C", NULL));
273: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradGetType_C", NULL));
274: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradSetType_C", NULL));
275: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradSetImplicitFormulation_C", NULL));
276: PetscFunctionReturn(PETSC_SUCCESS);
277: }
279: static PetscErrorCode TSInterpolate_DiscGrad(TS ts, PetscReal t, Vec X)
280: {
281: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
282: PetscReal dt = t - ts->ptime;
284: PetscFunctionBegin;
285: PetscCall(VecCopy(ts->vec_sol, dg->X));
286: PetscCall(VecWAXPY(X, dt, dg->Xdot, dg->X));
287: PetscFunctionReturn(PETSC_SUCCESS);
288: }
290: static PetscErrorCode TSDiscGrad_SNESSolve(TS ts, Vec b, Vec x)
291: {
292: SNES snes;
293: PetscInt nits, lits;
295: PetscFunctionBegin;
296: PetscCall(TSGetSNES(ts, &snes));
297: PetscCall(SNESSolve(snes, b, x));
298: PetscCall(SNESGetIterationNumber(snes, &nits));
299: PetscCall(SNESGetLinearSolveIterations(snes, &lits));
300: ts->snes_its += nits;
301: ts->ksp_its += lits;
302: PetscFunctionReturn(PETSC_SUCCESS);
303: }
305: static PetscErrorCode TSStep_DiscGrad(TS ts)
306: {
307: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
308: TSAdapt adapt;
309: TSStepStatus status = TS_STEP_INCOMPLETE;
310: PetscInt rejections = 0;
311: PetscBool stageok, accept = PETSC_TRUE;
312: PetscReal next_time_step = ts->time_step;
314: PetscFunctionBegin;
315: PetscCall(TSGetAdapt(ts, &adapt));
316: if (!ts->steprollback) PetscCall(VecCopy(ts->vec_sol, dg->X0));
318: while (!ts->reason && status != TS_STEP_COMPLETE) {
319: PetscReal shift = 1 / (0.5 * ts->time_step);
321: dg->stage_time = ts->ptime + 0.5 * ts->time_step;
323: PetscCall(VecCopy(dg->X0, dg->X));
324: PetscCall(TSPreStage(ts, dg->stage_time));
325: PetscCall(TSDiscGrad_SNESSolve(ts, NULL, dg->X));
326: PetscCall(TSPostStage(ts, dg->stage_time, 0, &dg->X));
327: PetscCall(TSAdaptCheckStage(adapt, ts, dg->stage_time, dg->X, &stageok));
328: if (!stageok) goto reject_step;
330: PetscCall(VecAXPBYPCZ(dg->Xdot, -shift, shift, 0, dg->X0, dg->X));
331: PetscCall(VecAXPY(ts->vec_sol, ts->time_step, dg->Xdot));
332: PetscCall(TSAdaptChoose(adapt, ts, ts->time_step, NULL, &next_time_step, &accept));
333: status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
334: if (!accept) {
335: PetscCall(VecCopy(dg->X0, ts->vec_sol));
336: ts->time_step = next_time_step;
337: goto reject_step;
338: }
339: ts->ptime += ts->time_step;
340: ts->time_step = next_time_step;
341: break;
343: reject_step:
344: ts->reject++;
345: accept = PETSC_FALSE;
346: if (!ts->reason && ts->max_reject >= 0 && ++rejections > ts->max_reject) {
347: ts->reason = TS_DIVERGED_STEP_REJECTED;
348: PetscCall(PetscInfo(ts, "Step=%" PetscInt_FMT ", step rejections %" PetscInt_FMT " greater than current TS allowed, stopping solve\n", ts->steps, rejections));
349: }
350: }
351: PetscFunctionReturn(PETSC_SUCCESS);
352: }
354: static PetscErrorCode TSGetStages_DiscGrad(TS ts, PetscInt *ns, Vec **Y)
355: {
356: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
358: PetscFunctionBegin;
359: if (ns) *ns = 1;
360: if (Y) *Y = &dg->X;
361: PetscFunctionReturn(PETSC_SUCCESS);
362: }
364: /*
365: This defines the nonlinear equation that is to be solved with SNES
366: G(U) = F[t0 + 0.5*dt, U, (U-U0)/dt] = 0
367: */
369: /* x = (x+x')/2 */
370: /* NEED TO CALCULATE x_{n+1} from x and x_{n}*/
371: static PetscErrorCode SNESTSFormFunction_DiscGrad(SNES snes, Vec x, Vec y, TS ts)
372: {
373: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
374: PetscReal norm, shift = 1 / (0.5 * ts->time_step);
375: PetscInt n, dim;
376: Vec X0, Xdot, Xp, Xdiff;
377: Mat S;
378: PetscInt *S_prealloc_arr;
379: PetscReal Snorm;
380: PetscScalar F = 0, F0 = 0, Gp;
381: Vec G, SgF;
382: DM dm, dmsave;
383: MPI_Comm comm;
385: PetscFunctionBegin;
386: PetscCall(SNESGetDM(snes, &dm));
387: PetscCall(DMGetDimension(dm, &dim));
388: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
390: PetscCall(VecDuplicate(y, &Xp));
391: PetscCall(VecDuplicate(y, &Xdiff));
392: PetscCall(VecDuplicate(y, &SgF));
393: PetscCall(VecDuplicate(y, &G));
395: PetscCall(PetscObjectSetName((PetscObject)x, "x"));
396: PetscCall(VecViewFromOptions(x, NULL, "-x_view"));
398: PetscCall(VecGetLocalSize(y, &n));
399: PetscCall(MatCreate(comm, &S));
400: PetscCall(MatSetSizes(S, n, n, PETSC_DECIDE, PETSC_DECIDE));
401: PetscCall(MatSetFromOptions(S));
402: PetscCall(PetscMalloc1(n, &S_prealloc_arr));
403: for (PetscInt i = 0; i < n; ++i) S_prealloc_arr[i] = 2;
404: PetscCall(MatXAIJSetPreallocation(S, 1, S_prealloc_arr, NULL, NULL, NULL));
405: PetscCall(MatSetUp(S));
406: PetscCheck(dg->Sfunc, comm, PETSC_ERR_ARG_WRONGSTATE, "Sfunc is not set, call TSDiscGradSetFormulation()");
407: PetscCall((*dg->Sfunc)(ts, dg->stage_time, x, S, dg->funcCtx));
408: PetscCall(PetscFree(S_prealloc_arr));
409: PetscCall(PetscObjectSetName((PetscObject)S, "S"));
410: PetscCall(MatViewFromOptions(S, NULL, "-S_view"));
411: PetscCall(MatNorm(S, NORM_FROBENIUS, &Snorm));
412: PetscCall(TSDiscGradGetX0AndXdot(ts, dm, &X0, &Xdot));
413: PetscCall(VecAXPBYPCZ(Xdot, -shift, shift, 0, X0, x)); /* Xdot = shift (x - X0) */
415: PetscCall(VecAXPBYPCZ(Xp, -1, 2, 0, X0, x)); /* Xp = 2*x - X0 + (0)*Xp */
416: PetscCall(VecAXPBYPCZ(Xdiff, -1, 1, 0, X0, Xp)); /* Xdiff = xp - X0 + (0)*Xdiff */
418: PetscCall(PetscObjectSetName((PetscObject)X0, "X0"));
419: PetscCall(PetscObjectSetName((PetscObject)Xp, "Xp"));
420: PetscCall(VecViewFromOptions(X0, NULL, "-X0_view"));
421: PetscCall(VecViewFromOptions(Xp, NULL, "-Xp_view"));
423: if (Snorm == 0.) {
424: PetscCall(VecZeroEntries(G));
425: PetscCall(VecViewFromOptions(x, NULL, "-y_view"));
426: PetscCall(VecViewFromOptions(Xdot, NULL, "-Xdot_view"));
427: PetscCheck(dg->IGfunc, comm, PETSC_ERR_ARG_WRONGSTATE, "IGfunc is not set, call TSDiscGradSetImplicitFormulation()");
428: PetscCall((*dg->IGfunc)(ts, dg->stage_time, Xp, Xdot, y, dg->funcCtx));
429: goto end;
430: }
431: if (dg->discgrad == TS_DG_AVERAGE) {
432: /* Average Value DG:
433: \overline{\nabla} F (x_{n+1},x_{n}) = \int_0^1 \nabla F ((1-\xi)*x_{n+1} + \xi*x_{n}) d \xi */
434: PetscQuadrature quad;
435: PetscInt Nq;
436: const PetscReal *wq, *xq;
437: Vec Xquad, den;
439: PetscCheck(dg->Gfunc, comm, PETSC_ERR_ARG_WRONGSTATE, "Gfunc is not set, call TSDiscGradSetFormulation()");
440: PetscCall(PetscObjectSetName((PetscObject)G, "G"));
441: PetscCall(VecDuplicate(G, &Xquad));
442: PetscCall(VecDuplicate(G, &den));
443: PetscCall(VecZeroEntries(G));
445: /* \overline{\nabla} F = \nabla F ((1-\xi) x_{n} + \xi x_{n+1})*/
446: PetscCall(PetscDTGaussTensorQuadrature(dim, 1, 2, 0.0, 1.0, &quad));
447: PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &xq, &wq));
448: for (PetscInt q = 0; q < Nq; ++q) {
449: PetscReal xi = xq[q], xim1 = 1 - xq[q];
450: PetscCall(VecZeroEntries(Xquad));
451: PetscCall(VecAXPBYPCZ(Xquad, xi, xim1, 1.0, X0, Xp));
452: PetscCall((*dg->Gfunc)(ts, dg->stage_time, Xquad, den, dg->funcCtx));
453: PetscCall(VecAXPY(G, wq[q], den));
454: PetscCall(PetscObjectSetName((PetscObject)den, "den"));
455: PetscCall(VecViewFromOptions(den, NULL, "-den_view"));
456: }
457: PetscCall(VecDestroy(&Xquad));
458: PetscCall(VecDestroy(&den));
459: PetscCall(PetscQuadratureDestroy(&quad));
460: } else if (dg->discgrad == TS_DG_GONZALEZ) {
461: PetscCheck(dg->Ffunc, comm, PETSC_ERR_ARG_WRONGSTATE, "Ffunc is not set, call TSDiscGradSetFormulation()");
462: PetscCheck(dg->Gfunc, comm, PETSC_ERR_ARG_WRONGSTATE, "Gfunc is not set, call TSDiscGradSetFormulation()");
463: PetscCall((*dg->Ffunc)(ts, dg->stage_time, Xp, &F, dg->funcCtx));
464: PetscCall((*dg->Ffunc)(ts, dg->stage_time, X0, &F0, dg->funcCtx));
465: PetscCall((*dg->Gfunc)(ts, dg->stage_time, x, G, dg->funcCtx));
467: /* Adding Extra Gonzalez Term */
468: PetscCall(VecDot(Xdiff, G, &Gp));
469: PetscCall(VecNorm(Xdiff, NORM_2, &norm));
470: if (norm < PETSC_SQRT_MACHINE_EPSILON) {
471: Gp = 0;
472: } else {
473: /* Gp = (1/|xn+1 - xn|^2) * (F(xn+1) - F(xn) - Gp) */
474: Gp = (F - F0 - Gp) / PetscSqr(norm);
475: }
476: PetscCall(VecAXPY(G, Gp, Xdiff));
477: } else if (dg->discgrad == TS_DG_NONE) {
478: PetscCheck(dg->Gfunc, comm, PETSC_ERR_ARG_WRONGSTATE, "Gfunc is not set, call TSDiscGradSetFormulation()");
479: PetscCall((*dg->Gfunc)(ts, dg->stage_time, x, G, dg->funcCtx));
480: } else {
481: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DG type not supported.");
482: }
483: PetscCall(MatMult(S, G, SgF)); /* Xdot = S*gradF */
485: PetscCall(PetscObjectSetName((PetscObject)G, "G"));
486: PetscCall(VecViewFromOptions(G, NULL, "-G_view"));
487: PetscCall(PetscObjectSetName((PetscObject)SgF, "SgF"));
488: PetscCall(VecViewFromOptions(SgF, NULL, "-SgF_view"));
489: /* DM monkey-business allows user code to call TSGetDM() inside of functions evaluated on levels of FAS */
490: dmsave = ts->dm;
491: ts->dm = dm;
492: PetscCall(VecAXPBYPCZ(y, 1, -1, 0, Xdot, SgF));
494: ts->dm = dmsave;
495: end:
496: PetscCall(TSDiscGradRestoreX0AndXdot(ts, dm, &X0, &Xdot));
498: PetscCall(VecDestroy(&Xp));
499: PetscCall(VecDestroy(&Xdiff));
500: PetscCall(VecDestroy(&SgF));
501: PetscCall(VecDestroy(&G));
502: PetscCall(MatDestroy(&S));
503: PetscFunctionReturn(PETSC_SUCCESS);
504: }
506: static PetscErrorCode SNESTSFormJacobian_DiscGrad(SNES snes, Vec x, Mat A, Mat B, TS ts)
507: {
508: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
509: PetscReal shift = 1 / (0.5 * ts->time_step);
510: Vec Xdot;
511: DM dm, dmsave;
513: PetscFunctionBegin;
514: PetscCall(SNESGetDM(snes, &dm));
515: /* Xdot has already been computed in SNESTSFormFunction_DiscGrad (SNES guarantees this) */
516: PetscCall(TSDiscGradGetX0AndXdot(ts, dm, NULL, &Xdot));
518: dmsave = ts->dm;
519: ts->dm = dm;
520: if (dg->IGjac) PetscCall(TSComputeIJacobian_Internal(ts, dg->IGjac, NULL, dg->funcCtx, dg->stage_time, x, Xdot, shift, A, B, PETSC_FALSE));
521: else PetscCall(TSComputeIJacobian(ts, dg->stage_time, x, Xdot, shift, A, B, PETSC_FALSE));
522: ts->dm = dmsave;
523: PetscCall(TSDiscGradRestoreX0AndXdot(ts, dm, NULL, &Xdot));
524: PetscFunctionReturn(PETSC_SUCCESS);
525: }
527: static PetscErrorCode TSDiscGradGetFormulation_DiscGrad(TS ts, PetscErrorCode (**Sfunc)(TS, PetscReal, Vec, Mat, void *), PetscErrorCode (**Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *), PetscErrorCode (**Gfunc)(TS, PetscReal, Vec, Vec, void *), PetscCtx ctx)
528: {
529: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
531: PetscFunctionBegin;
532: *Sfunc = dg->Sfunc;
533: *Ffunc = dg->Ffunc;
534: *Gfunc = dg->Gfunc;
535: PetscFunctionReturn(PETSC_SUCCESS);
536: }
538: static PetscErrorCode TSDiscGradSetFormulation_DiscGrad(TS ts, PetscErrorCode (*Sfunc)(TS, PetscReal, Vec, Mat, void *), PetscErrorCode (*Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *), PetscErrorCode (*Gfunc)(TS, PetscReal, Vec, Vec, void *), PetscCtx ctx)
539: {
540: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
542: PetscFunctionBegin;
543: dg->Sfunc = Sfunc;
544: dg->Ffunc = Ffunc;
545: dg->Gfunc = Gfunc;
546: dg->funcCtx = ctx;
547: PetscFunctionReturn(PETSC_SUCCESS);
548: }
550: static PetscErrorCode TSDiscGradSetImplicitFormulation_DiscGrad(TS ts, PetscErrorCode (*IGfunc)(TS ts, PetscReal time, Vec u, Vec u_t, Vec G, void *ctx), PetscErrorCode (*IGjac)(TS ts, PetscReal time, Vec u, Vec u_t, PetscReal shift, Mat J, Mat Jp, void *ctx))
551: {
552: TS_DiscGrad *dg = (TS_DiscGrad *)ts->data;
554: PetscFunctionBegin;
555: dg->IGfunc = IGfunc;
556: dg->IGjac = IGjac;
557: PetscFunctionReturn(PETSC_SUCCESS);
558: }
560: /*MC
561: TSDISCGRAD - ODE solver using the discrete gradients version of the implicit midpoint method
563: Level: intermediate
565: Notes:
566: This is the implicit midpoint rule, with an optional term that guarantees the discrete
567: gradient property. This timestepper applies to systems of the form $u_t = S(u) \nabla F(u)$
568: where $S(u)$ is a linear operator, and $F$ is a functional of $u$.
570: For Hamiltonian systems designed to conserve the first integral (energy),
571: but also has the property for some systems of monotonicity in a functional.
573: .seealso: [](ch_ts), `TSCreate()`, `TSSetType()`, `TS`, `TSType`, `TSDiscGradSetFormulation()`, `TSDiscGradGetFormulation()`,
574: `TSDiscGradSetType()`, `TSDiscGradGetType()`, `TSDGType`
575: M*/
576: PETSC_EXTERN PetscErrorCode TSCreate_DiscGrad(TS ts)
577: {
578: TS_DiscGrad *th;
580: PetscFunctionBegin;
581: PetscCall(PetscCitationsRegister(DGCitation, &DGCite));
582: ts->ops->reset = TSReset_DiscGrad;
583: ts->ops->destroy = TSDestroy_DiscGrad;
584: ts->ops->view = TSView_DiscGrad;
585: ts->ops->setfromoptions = TSSetFromOptions_DiscGrad;
586: ts->ops->setup = TSSetUp_DiscGrad;
587: ts->ops->step = TSStep_DiscGrad;
588: ts->ops->interpolate = TSInterpolate_DiscGrad;
589: ts->ops->getstages = TSGetStages_DiscGrad;
590: ts->ops->snesfunction = SNESTSFormFunction_DiscGrad;
591: ts->ops->snesjacobian = SNESTSFormJacobian_DiscGrad;
592: ts->default_adapt_type = TSADAPTNONE;
594: ts->usessnes = PETSC_TRUE;
596: PetscCall(PetscNew(&th));
597: ts->data = (void *)th;
599: th->discgrad = TS_DG_NONE;
601: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradGetFormulation_C", TSDiscGradGetFormulation_DiscGrad));
602: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradSetFormulation_C", TSDiscGradSetFormulation_DiscGrad));
603: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradGetType_C", TSDiscGradGetType_DiscGrad));
604: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradSetType_C", TSDiscGradSetType_DiscGrad));
605: PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSDiscGradSetImplicitFormulation_C", TSDiscGradSetImplicitFormulation_DiscGrad));
606: PetscFunctionReturn(PETSC_SUCCESS);
607: }
609: /*@
610: TSDiscGradGetFormulation - Get the construction method for S, F, and grad F from the
611: formulation $u_t = S \nabla F$ for `TSDISCGRAD`
613: Not Collective
615: Input Parameter:
616: . ts - timestepping context
618: Output Parameters:
619: + Sfunc - constructor for the S matrix from the formulation
620: . Ffunc - functional F from the formulation
621: . Gfunc - constructor for the gradient of F from the formulation
622: - ctx - the application context
624: Calling sequence of `Sfunc`:
625: + ts - the integrator
626: . time - the current time
627: . u - the solution
628: . S - the S-matrix from the formulation
629: - ctx - the application context
631: Calling sequence of `Ffunc`:
632: + ts - the integrator
633: . time - the current time
634: . u - the solution
635: . F - the computed function from the formulation
636: - ctx - the application context
638: Calling sequence of `Gfunc`:
639: + ts - the integrator
640: . time - the current time
641: . u - the solution
642: . G - the gradient of the computed function from the formulation
643: - ctx - the application context
645: Level: intermediate
647: .seealso: [](ch_ts), `TS`, `TSDISCGRAD`, `TSDiscGradSetFormulation()`, `TSDiscGradSetImplicitFormulation()`
648: @*/
649: PetscErrorCode TSDiscGradGetFormulation(TS ts, PetscErrorCode (**Sfunc)(TS ts, PetscReal time, Vec u, Mat S, PetscCtx ctx), PetscErrorCode (**Ffunc)(TS ts, PetscReal time, Vec u, PetscScalar *F, PetscCtx ctx), PetscErrorCode (**Gfunc)(TS ts, PetscReal time, Vec u, Vec G, PetscCtx ctx), PetscCtx ctx)
650: {
651: PetscFunctionBegin;
653: PetscAssertPointer(Sfunc, 2);
654: PetscAssertPointer(Ffunc, 3);
655: PetscAssertPointer(Gfunc, 4);
656: PetscUseMethod(ts, "TSDiscGradGetFormulation_C", (TS, PetscErrorCode (**Sfunc)(TS, PetscReal, Vec, Mat, void *), PetscErrorCode (**Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *), PetscErrorCode (**Gfunc)(TS, PetscReal, Vec, Vec, void *), void *), (ts, Sfunc, Ffunc, Gfunc, ctx));
657: PetscFunctionReturn(PETSC_SUCCESS);
658: }
660: /*@
661: TSDiscGradSetFormulation - Set the construction method for S, F, and grad F from the
662: formulation $u_t = S(u) \nabla F(u)$ for `TSDISCGRAD`
664: Not Collective
666: Input Parameters:
667: + ts - timestepping context
668: . Sfunc - constructor for the S matrix from the formulation
669: . Ffunc - functional F from the formulation
670: . Gfunc - constructor for the gradient of F from the formulation
671: - ctx - optional context for the functions
673: Calling sequence of `Sfunc`:
674: + ts - the integrator
675: . time - the current time
676: . u - the solution
677: . S - the S-matrix from the formulation
678: - ctx - the application context
680: Calling sequence of `Ffunc`:
681: + ts - the integrator
682: . time - the current time
683: . u - the solution
684: . F - the computed function from the formulation
685: - ctx - the application context
687: Calling sequence of `Gfunc`:
688: + ts - the integrator
689: . time - the current time
690: . u - the solution
691: . G - the gradient of the computed function from the formulation
692: - ctx - the application context
694: Level: intermediate
696: .seealso: [](ch_ts), `TSDISCGRAD`, `TSDiscGradGetFormulation()`, `TSDiscGradSetImplicitFormulation()`
697: @*/
698: PetscErrorCode TSDiscGradSetFormulation(TS ts, PetscErrorCode (*Sfunc)(TS ts, PetscReal time, Vec u, Mat S, PetscCtx ctx), PetscErrorCode (*Ffunc)(TS ts, PetscReal time, Vec u, PetscScalar *F, PetscCtx ctx), PetscErrorCode (*Gfunc)(TS ts, PetscReal time, Vec u, Vec G, PetscCtx ctx), PetscCtx ctx)
699: {
700: PetscFunctionBegin;
705: PetscTryMethod(ts, "TSDiscGradSetFormulation_C", (TS, PetscErrorCode (*Sfunc)(TS, PetscReal, Vec, Mat, void *), PetscErrorCode (*Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *), PetscErrorCode (*Gfunc)(TS, PetscReal, Vec, Vec, void *), void *), (ts, Sfunc, Ffunc, Gfunc, ctx));
706: PetscFunctionReturn(PETSC_SUCCESS);
707: }
709: /*@
710: TSDiscGradGetType - Checks for which discrete gradient to use in formulation for `TSDISCGRAD`
712: Not Collective
714: Input Parameter:
715: . ts - timestepping context
717: Output Parameter:
718: . dgtype - Discrete gradient type <none, gonzalez, average>
720: Level: advanced
722: .seealso: [](ch_ts), `TSDISCGRAD`, `TSDiscGradSetType()`
723: @*/
724: PetscErrorCode TSDiscGradGetType(TS ts, TSDGType *dgtype)
725: {
726: PetscFunctionBegin;
728: PetscAssertPointer(dgtype, 2);
729: PetscUseMethod(ts, "TSDiscGradGetType_C", (TS, TSDGType *), (ts, dgtype));
730: PetscFunctionReturn(PETSC_SUCCESS);
731: }
733: /*@
734: TSDiscGradSetType - Sets discrete gradient formulation.
736: Not Collective
738: Input Parameters:
739: + ts - timestepping context
740: - dgtype - Discrete gradient type <none, gonzalez, average>
742: Options Database Key:
743: . -ts_discgrad_type (gonzalez|average|none) - flag to choose discrete gradient type
745: Level: intermediate
747: Notes:
748: Without `dgtype` or with type `none`, the discrete gradients timestepper is just implicit midpoint.
750: .seealso: [](ch_ts), `TSDISCGRAD`, `TSDGType`
751: @*/
752: PetscErrorCode TSDiscGradSetType(TS ts, TSDGType dgtype)
753: {
754: PetscFunctionBegin;
756: PetscTryMethod(ts, "TSDiscGradSetType_C", (TS, TSDGType), (ts, dgtype));
757: PetscFunctionReturn(PETSC_SUCCESS);
758: }
760: /*@
761: TSDiscGradSetImplicitFormulation - Set the construction method for IG and its Jacobian from the
762: formulation $IG(t) = u_t - S(u) \nabla F(u)$ for `TSDISCGRAD`
764: Not Collective
766: Input Parameters:
767: + ts - timestepping context
768: . IGfunc - implicit formulation
769: - IGjac - implicit Jacobian
771: Calling sequence of `IGfunc`:
772: + ts - the integrator
773: . time - the current time
774: . u - the solution
775: . u_t - the time derivative
776: . G - the LHS of the formulation
777: - ctx - the user context
779: Calling sequence of `IGjac`:
780: + ts - the integrator
781: . time - the current time
782: . u - the solution
783: . u_t - the time derivative
784: . shift - the multiplier for the time derivative part
785: . J - the Jacobian of the LHS
786: . Jp - the Jacobian preconditioner of the LHS
787: - ctx - the user context
789: Level: intermediate
791: Note:
792: This allows the DG formulation to be given in the PETSc style for fully implicit solvers.
794: .seealso: [](ch_ts), `TSDISCGRAD`, `TSDiscGradGetFormulation()`, `TSDiscGradSetFormulation()`
795: @*/
796: PetscErrorCode TSDiscGradSetImplicitFormulation(TS ts, PetscErrorCode (*IGfunc)(TS ts, PetscReal time, Vec u, Vec u_t, Vec G, void *ctx), PetscErrorCode (*IGjac)(TS ts, PetscReal time, Vec u, Vec u_t, PetscReal shift, Mat J, Mat Jp, void *ctx))
797: {
798: PetscFunctionBegin;
802: PetscTryMethod(ts, "TSDiscGradSetImplicitFormulation_C", (TS, PetscErrorCode (*)(TS, PetscReal, Vec, Vec, Vec, void *), PetscErrorCode (*)(TS, PetscReal, Vec, Vec, PetscReal, Mat, Mat, void *)), (ts, IGfunc, IGjac));
803: PetscFunctionReturn(PETSC_SUCCESS);
804: }