xref: /petsc/src/ts/impls/implicit/discgrad/tsdiscgrad.c (revision dfd676b1a855b7f967ece75a22ee7f6626d10f89) !
1 /*
2   Code for timestepping with discrete gradient integrators
3 */
4 #include <petsc/private/tsimpl.h>                /*I   "petscts.h"   I*/
5 #include <petscdm.h>
6 
7 PetscBool DGCite = PETSC_FALSE;
8 const char DGCitation[] = "@article{Gonzalez1996,\n"
9                           "  title   = {Time integration and discrete Hamiltonian systems},\n"
10                           "  author  = {Oscar Gonzalez},\n"
11                           "  journal = {Journal of Nonlinear Science},\n"
12                           "  volume  = {6},\n"
13                           "  pages   = {449--467},\n"
14                           "  doi     = {10.1007/978-1-4612-1246-1_10},\n"
15                           "  year    = {1996}\n}\n";
16 
17 typedef struct {
18   PetscReal    stage_time;
19   Vec          X0, X, Xdot;
20   void        *funcCtx;
21   PetscBool    gonzalez;
22   PetscErrorCode (*Sfunc)(TS, PetscReal, Vec, Mat, void *);
23   PetscErrorCode (*Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *);
24   PetscErrorCode (*Gfunc)(TS, PetscReal, Vec, Vec, void *);
25 } TS_DiscGrad;
26 
27 static PetscErrorCode TSDiscGradGetX0AndXdot(TS ts, DM dm, Vec *X0, Vec *Xdot)
28 {
29   TS_DiscGrad   *dg = (TS_DiscGrad *) ts->data;
30 
31   PetscFunctionBegin;
32   if (X0) {
33     if (dm && dm != ts->dm) PetscCall(DMGetNamedGlobalVector(dm, "TSDiscGrad_X0", X0));
34     else                    {*X0  = ts->vec_sol;}
35   }
36   if (Xdot) {
37     if (dm && dm != ts->dm) PetscCall(DMGetNamedGlobalVector(dm, "TSDiscGrad_Xdot", Xdot));
38     else                    {*Xdot = dg->Xdot;}
39   }
40   PetscFunctionReturn(0);
41 }
42 
43 static PetscErrorCode TSDiscGradRestoreX0AndXdot(TS ts, DM dm, Vec *X0, Vec *Xdot)
44 {
45   PetscFunctionBegin;
46   if (X0) {
47     if (dm && dm != ts->dm) PetscCall(DMRestoreNamedGlobalVector(dm, "TSDiscGrad_X0", X0));
48   }
49   if (Xdot) {
50     if (dm && dm != ts->dm) PetscCall(DMRestoreNamedGlobalVector(dm, "TSDiscGrad_Xdot", Xdot));
51   }
52   PetscFunctionReturn(0);
53 }
54 
55 static PetscErrorCode DMCoarsenHook_TSDiscGrad(DM fine, DM coarse, void *ctx)
56 {
57   PetscFunctionBegin;
58   PetscFunctionReturn(0);
59 }
60 
61 static PetscErrorCode DMRestrictHook_TSDiscGrad(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse, void *ctx)
62 {
63   TS             ts = (TS) ctx;
64   Vec            X0, Xdot, X0_c, Xdot_c;
65 
66   PetscFunctionBegin;
67   PetscCall(TSDiscGradGetX0AndXdot(ts, fine, &X0, &Xdot));
68   PetscCall(TSDiscGradGetX0AndXdot(ts, coarse, &X0_c, &Xdot_c));
69   PetscCall(MatRestrict(restrct, X0, X0_c));
70   PetscCall(MatRestrict(restrct, Xdot, Xdot_c));
71   PetscCall(VecPointwiseMult(X0_c, rscale, X0_c));
72   PetscCall(VecPointwiseMult(Xdot_c, rscale, Xdot_c));
73   PetscCall(TSDiscGradRestoreX0AndXdot(ts, fine, &X0, &Xdot));
74   PetscCall(TSDiscGradRestoreX0AndXdot(ts, coarse, &X0_c, &Xdot_c));
75   PetscFunctionReturn(0);
76 }
77 
78 static PetscErrorCode DMSubDomainHook_TSDiscGrad(DM dm, DM subdm, void *ctx)
79 {
80   PetscFunctionBegin;
81   PetscFunctionReturn(0);
82 }
83 
84 static PetscErrorCode DMSubDomainRestrictHook_TSDiscGrad(DM dm, VecScatter gscat, VecScatter lscat, DM subdm, void *ctx)
85 {
86   TS             ts = (TS) ctx;
87   Vec            X0, Xdot, X0_sub, Xdot_sub;
88 
89   PetscFunctionBegin;
90   PetscCall(TSDiscGradGetX0AndXdot(ts, dm, &X0, &Xdot));
91   PetscCall(TSDiscGradGetX0AndXdot(ts, subdm, &X0_sub, &Xdot_sub));
92 
93   PetscCall(VecScatterBegin(gscat, X0, X0_sub, INSERT_VALUES, SCATTER_FORWARD));
94   PetscCall(VecScatterEnd(gscat, X0, X0_sub, INSERT_VALUES, SCATTER_FORWARD));
95 
96   PetscCall(VecScatterBegin(gscat, Xdot, Xdot_sub, INSERT_VALUES, SCATTER_FORWARD));
97   PetscCall(VecScatterEnd(gscat, Xdot, Xdot_sub, INSERT_VALUES, SCATTER_FORWARD));
98 
99   PetscCall(TSDiscGradRestoreX0AndXdot(ts, dm, &X0, &Xdot));
100   PetscCall(TSDiscGradRestoreX0AndXdot(ts, subdm, &X0_sub, &Xdot_sub));
101   PetscFunctionReturn(0);
102 }
103 
104 static PetscErrorCode TSSetUp_DiscGrad(TS ts)
105 {
106   TS_DiscGrad   *dg = (TS_DiscGrad *) ts->data;
107   DM             dm;
108 
109   PetscFunctionBegin;
110   if (!dg->X)    PetscCall(VecDuplicate(ts->vec_sol, &dg->X));
111   if (!dg->X0)   PetscCall(VecDuplicate(ts->vec_sol, &dg->X0));
112   if (!dg->Xdot) PetscCall(VecDuplicate(ts->vec_sol, &dg->Xdot));
113 
114   PetscCall(TSGetDM(ts, &dm));
115   PetscCall(DMCoarsenHookAdd(dm, DMCoarsenHook_TSDiscGrad, DMRestrictHook_TSDiscGrad, ts));
116   PetscCall(DMSubDomainHookAdd(dm, DMSubDomainHook_TSDiscGrad, DMSubDomainRestrictHook_TSDiscGrad, ts));
117   PetscFunctionReturn(0);
118 }
119 
120 static PetscErrorCode TSSetFromOptions_DiscGrad(PetscOptionItems *PetscOptionsObject, TS ts)
121 {
122   TS_DiscGrad   *dg = (TS_DiscGrad *) ts->data;
123 
124   PetscFunctionBegin;
125   PetscOptionsHeadBegin(PetscOptionsObject, "Discrete Gradients ODE solver options");
126   {
127     PetscCall(PetscOptionsBool("-ts_discgrad_gonzalez","Use Gonzalez term in discrete gradients formulation","TSDiscGradUseGonzalez",dg->gonzalez,&dg->gonzalez,NULL));
128   }
129   PetscOptionsHeadEnd();
130   PetscFunctionReturn(0);
131 }
132 
133 static PetscErrorCode TSView_DiscGrad(TS ts,PetscViewer viewer)
134 {
135   PetscBool      iascii;
136 
137   PetscFunctionBegin;
138   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii));
139   if (iascii) {
140     PetscCall(PetscViewerASCIIPrintf(viewer,"  Discrete Gradients\n"));
141   }
142   PetscFunctionReturn(0);
143 }
144 
145 static PetscErrorCode TSDiscGradIsGonzalez_DiscGrad(TS ts,PetscBool *gonzalez)
146 {
147   TS_DiscGrad *dg = (TS_DiscGrad*)ts->data;
148 
149   PetscFunctionBegin;
150   *gonzalez = dg->gonzalez;
151   PetscFunctionReturn(0);
152 }
153 
154 static PetscErrorCode TSDiscGradUseGonzalez_DiscGrad(TS ts,PetscBool flg)
155 {
156   TS_DiscGrad *dg = (TS_DiscGrad*)ts->data;
157 
158   PetscFunctionBegin;
159   dg->gonzalez = flg;
160   PetscFunctionReturn(0);
161 }
162 
163 static PetscErrorCode TSReset_DiscGrad(TS ts)
164 {
165   TS_DiscGrad   *dg = (TS_DiscGrad *) ts->data;
166 
167   PetscFunctionBegin;
168   PetscCall(VecDestroy(&dg->X));
169   PetscCall(VecDestroy(&dg->X0));
170   PetscCall(VecDestroy(&dg->Xdot));
171   PetscFunctionReturn(0);
172 }
173 
174 static PetscErrorCode TSDestroy_DiscGrad(TS ts)
175 {
176   DM             dm;
177 
178   PetscFunctionBegin;
179   PetscCall(TSReset_DiscGrad(ts));
180   PetscCall(TSGetDM(ts, &dm));
181   if (dm) {
182     PetscCall(DMCoarsenHookRemove(dm, DMCoarsenHook_TSDiscGrad, DMRestrictHook_TSDiscGrad, ts));
183     PetscCall(DMSubDomainHookRemove(dm, DMSubDomainHook_TSDiscGrad, DMSubDomainRestrictHook_TSDiscGrad, ts));
184   }
185   PetscCall(PetscFree(ts->data));
186   PetscCall(PetscObjectComposeFunction((PetscObject)ts,"TSDiscGradGetFormulation_C",NULL));
187   PetscCall(PetscObjectComposeFunction((PetscObject)ts,"TSDiscGradSetFormulation_C",NULL));
188   PetscFunctionReturn(0);
189 }
190 
191 static PetscErrorCode TSInterpolate_DiscGrad(TS ts, PetscReal t, Vec X)
192 {
193   TS_DiscGrad   *dg = (TS_DiscGrad*)ts->data;
194   PetscReal      dt = t - ts->ptime;
195 
196   PetscFunctionBegin;
197   PetscCall(VecCopy(ts->vec_sol, dg->X));
198   PetscCall(VecWAXPY(X, dt, dg->Xdot, dg->X));
199   PetscFunctionReturn(0);
200 }
201 
202 static PetscErrorCode TSDiscGrad_SNESSolve(TS ts, Vec b, Vec x)
203 {
204   SNES           snes;
205   PetscInt       nits, lits;
206 
207   PetscFunctionBegin;
208   PetscCall(TSGetSNES(ts, &snes));
209   PetscCall(SNESSolve(snes, b, x));
210   PetscCall(SNESGetIterationNumber(snes, &nits));
211   PetscCall(SNESGetLinearSolveIterations(snes, &lits));
212   ts->snes_its += nits;
213   ts->ksp_its  += lits;
214   PetscFunctionReturn(0);
215 }
216 
217 static PetscErrorCode TSStep_DiscGrad(TS ts)
218 {
219   TS_DiscGrad   *dg = (TS_DiscGrad *) ts->data;
220   TSAdapt        adapt;
221   TSStepStatus   status          = TS_STEP_INCOMPLETE;
222   PetscInt       rejections      = 0;
223   PetscBool      stageok, accept = PETSC_TRUE;
224   PetscReal      next_time_step  = ts->time_step;
225 
226   PetscFunctionBegin;
227   PetscCall(TSGetAdapt(ts, &adapt));
228   if (!ts->steprollback) PetscCall(VecCopy(ts->vec_sol, dg->X0));
229 
230   while (!ts->reason && status != TS_STEP_COMPLETE) {
231     PetscReal shift = 1/(0.5*ts->time_step);
232 
233     dg->stage_time = ts->ptime + 0.5*ts->time_step;
234 
235     PetscCall(VecCopy(dg->X0, dg->X));
236     PetscCall(TSPreStage(ts, dg->stage_time));
237     PetscCall(TSDiscGrad_SNESSolve(ts, NULL, dg->X));
238     PetscCall(TSPostStage(ts, dg->stage_time, 0, &dg->X));
239     PetscCall(TSAdaptCheckStage(adapt, ts, dg->stage_time, dg->X, &stageok));
240     if (!stageok) goto reject_step;
241 
242     status = TS_STEP_PENDING;
243     PetscCall(VecAXPBYPCZ(dg->Xdot, -shift, shift, 0, dg->X0, dg->X));
244     PetscCall(VecAXPY(ts->vec_sol, ts->time_step, dg->Xdot));
245     PetscCall(TSAdaptChoose(adapt, ts, ts->time_step, NULL, &next_time_step, &accept));
246     status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
247     if (!accept) {
248       PetscCall(VecCopy(dg->X0, ts->vec_sol));
249       ts->time_step = next_time_step;
250       goto reject_step;
251     }
252     ts->ptime    += ts->time_step;
253     ts->time_step = next_time_step;
254     break;
255 
256   reject_step:
257     ts->reject++; accept = PETSC_FALSE;
258     if (!ts->reason && ts->max_reject >= 0 && ++rejections > ts->max_reject) {
259       ts->reason = TS_DIVERGED_STEP_REJECTED;
260       PetscCall(PetscInfo(ts, "Step=%" PetscInt_FMT ", step rejections %" PetscInt_FMT " greater than current TS allowed, stopping solve\n", ts->steps, rejections));
261     }
262   }
263   PetscFunctionReturn(0);
264 }
265 
266 static PetscErrorCode TSGetStages_DiscGrad(TS ts, PetscInt *ns, Vec **Y)
267 {
268   TS_DiscGrad *dg = (TS_DiscGrad *) ts->data;
269 
270   PetscFunctionBegin;
271   if (ns) *ns = 1;
272   if (Y)  *Y  = &(dg->X);
273   PetscFunctionReturn(0);
274 }
275 
276 /*
277   This defines the nonlinear equation that is to be solved with SNES
278     G(U) = F[t0 + 0.5*dt, U, (U-U0)/dt] = 0
279 */
280 
281 /* x = (x+x')/2 */
282 /* NEED TO CALCULATE x_{n+1} from x and x_{n}*/
283 static PetscErrorCode SNESTSFormFunction_DiscGrad(SNES snes, Vec x, Vec y, TS ts)
284 {
285 
286   TS_DiscGrad   *dg    = (TS_DiscGrad *) ts->data;
287   PetscReal      norm, shift = 1/(0.5*ts->time_step);
288   PetscInt       n;
289   Vec            X0, Xdot, Xp, Xdiff;
290   Mat            S;
291   PetscScalar    F=0, F0=0, Gp;
292   Vec            G, SgF;
293   DM             dm, dmsave;
294 
295   PetscFunctionBegin;
296   PetscCall(SNESGetDM(snes, &dm));
297 
298   PetscCall(VecDuplicate(y, &Xp));
299   PetscCall(VecDuplicate(y, &Xdiff));
300   PetscCall(VecDuplicate(y, &SgF));
301   PetscCall(VecDuplicate(y, &G));
302 
303   PetscCall(VecGetLocalSize(y, &n));
304   PetscCall(MatCreate(PETSC_COMM_WORLD, &S));
305   PetscCall(MatSetSizes(S, PETSC_DECIDE, PETSC_DECIDE, n, n));
306   PetscCall(MatSetFromOptions(S));
307   PetscCall(MatSetUp(S));
308   PetscCall(MatAssemblyBegin(S,MAT_FINAL_ASSEMBLY));
309   PetscCall(MatAssemblyEnd(S,MAT_FINAL_ASSEMBLY));
310 
311   PetscCall(TSDiscGradGetX0AndXdot(ts, dm, &X0, &Xdot));
312   PetscCall(VecAXPBYPCZ(Xdot, -shift, shift, 0, X0, x)); /* Xdot = shift (x - X0) */
313 
314   PetscCall(VecAXPBYPCZ(Xp, -1, 2, 0, X0, x)); /* Xp = 2*x - X0 + (0)*Xmid */
315   PetscCall(VecAXPBYPCZ(Xdiff, -1, 1, 0, X0, Xp)); /* Xdiff = xp - X0 + (0)*Xdiff */
316 
317   if (dg->gonzalez) {
318     PetscCall((*dg->Sfunc)(ts, dg->stage_time, x ,   S,  dg->funcCtx));
319     PetscCall((*dg->Ffunc)(ts, dg->stage_time, Xp,  &F,  dg->funcCtx));
320     PetscCall((*dg->Ffunc)(ts, dg->stage_time, X0,  &F0, dg->funcCtx));
321     PetscCall((*dg->Gfunc)(ts, dg->stage_time, x ,   G,  dg->funcCtx));
322 
323     /* Adding Extra Gonzalez Term */
324     PetscCall(VecDot(Xdiff, G, &Gp));
325     PetscCall(VecNorm(Xdiff, NORM_2, &norm));
326     if (norm < PETSC_SQRT_MACHINE_EPSILON) {
327       Gp = 0;
328     } else {
329       /* Gp = (1/|xn+1 - xn|^2) * (F(xn+1) - F(xn) - Gp) */
330       Gp = (F - F0 - Gp)/PetscSqr(norm);
331     }
332     PetscCall(VecAXPY(G, Gp, Xdiff));
333     PetscCall(MatMult(S, G , SgF)); /* S*gradF */
334 
335   } else {
336     PetscCall((*dg->Sfunc)(ts, dg->stage_time, x, S,  dg->funcCtx));
337     PetscCall((*dg->Gfunc)(ts, dg->stage_time, x, G,  dg->funcCtx));
338 
339     PetscCall(MatMult(S, G , SgF)); /* Xdot = S*gradF */
340   }
341   /* DM monkey-business allows user code to call TSGetDM() inside of functions evaluated on levels of FAS */
342   dmsave = ts->dm;
343   ts->dm = dm;
344   PetscCall(VecAXPBYPCZ(y, 1, -1, 0, Xdot, SgF));
345   ts->dm = dmsave;
346   PetscCall(TSDiscGradRestoreX0AndXdot(ts, dm, &X0, &Xdot));
347 
348   PetscCall(VecDestroy(&Xp));
349   PetscCall(VecDestroy(&Xdiff));
350   PetscCall(VecDestroy(&SgF));
351   PetscCall(VecDestroy(&G));
352   PetscCall(MatDestroy(&S));
353 
354   PetscFunctionReturn(0);
355 }
356 
357 static PetscErrorCode SNESTSFormJacobian_DiscGrad(SNES snes, Vec x, Mat A, Mat B, TS ts)
358 {
359   TS_DiscGrad   *dg    = (TS_DiscGrad *) ts->data;
360   PetscReal      shift = 1/(0.5*ts->time_step);
361   Vec            Xdot;
362   DM             dm,dmsave;
363 
364   PetscFunctionBegin;
365   PetscCall(SNESGetDM(snes, &dm));
366   /* Xdot has already been computed in SNESTSFormFunction_DiscGrad (SNES guarantees this) */
367   PetscCall(TSDiscGradGetX0AndXdot(ts, dm, NULL, &Xdot));
368 
369   dmsave = ts->dm;
370   ts->dm = dm;
371   PetscCall(TSComputeIJacobian(ts, dg->stage_time, x, Xdot, shift, A, B, PETSC_FALSE));
372   ts->dm = dmsave;
373   PetscCall(TSDiscGradRestoreX0AndXdot(ts, dm, NULL, &Xdot));
374   PetscFunctionReturn(0);
375 }
376 
377 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 *), void *ctx)
378 {
379   TS_DiscGrad *dg = (TS_DiscGrad *) ts->data;
380 
381   PetscFunctionBegin;
382   *Sfunc = dg->Sfunc;
383   *Ffunc = dg->Ffunc;
384   *Gfunc = dg->Gfunc;
385   PetscFunctionReturn(0);
386 }
387 
388 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 *), void *ctx)
389 {
390   TS_DiscGrad *dg = (TS_DiscGrad *) ts->data;
391 
392   PetscFunctionBegin;
393   dg->Sfunc = Sfunc;
394   dg->Ffunc = Ffunc;
395   dg->Gfunc = Gfunc;
396   dg->funcCtx = ctx;
397   PetscFunctionReturn(0);
398 }
399 
400 /*MC
401   TSDISCGRAD - ODE solver using the discrete gradients version of the implicit midpoint method
402 
403   Notes:
404   This is the implicit midpoint rule, with an optional term that guarantees the discrete gradient property. This
405   timestepper applies to systems of the form
406 $ u_t = S(u) grad F(u)
407   where S(u) is a linear operator, and F is a functional of u.
408 
409   Level: intermediate
410 
411 .seealso: TSCreate(), TSSetType(), TS, TSDISCGRAD, TSDiscGradSetFormulation()
412 M*/
413 PETSC_EXTERN PetscErrorCode TSCreate_DiscGrad(TS ts)
414 {
415   TS_DiscGrad       *th;
416 
417   PetscFunctionBegin;
418   PetscCall(PetscCitationsRegister(DGCitation, &DGCite));
419   ts->ops->reset           = TSReset_DiscGrad;
420   ts->ops->destroy         = TSDestroy_DiscGrad;
421   ts->ops->view            = TSView_DiscGrad;
422   ts->ops->setfromoptions  = TSSetFromOptions_DiscGrad;
423   ts->ops->setup           = TSSetUp_DiscGrad;
424   ts->ops->step            = TSStep_DiscGrad;
425   ts->ops->interpolate     = TSInterpolate_DiscGrad;
426   ts->ops->getstages       = TSGetStages_DiscGrad;
427   ts->ops->snesfunction    = SNESTSFormFunction_DiscGrad;
428   ts->ops->snesjacobian    = SNESTSFormJacobian_DiscGrad;
429   ts->default_adapt_type   = TSADAPTNONE;
430 
431   ts->usessnes = PETSC_TRUE;
432 
433   PetscCall(PetscNewLog(ts,&th));
434   ts->data = (void*)th;
435 
436   th->gonzalez = PETSC_FALSE;
437 
438   PetscCall(PetscObjectComposeFunction((PetscObject)ts,"TSDiscGradGetFormulation_C",TSDiscGradGetFormulation_DiscGrad));
439   PetscCall(PetscObjectComposeFunction((PetscObject)ts,"TSDiscGradSetFormulation_C",TSDiscGradSetFormulation_DiscGrad));
440   PetscCall(PetscObjectComposeFunction((PetscObject)ts,"TSDiscGradIsGonzalez_C",TSDiscGradIsGonzalez_DiscGrad));
441   PetscCall(PetscObjectComposeFunction((PetscObject)ts,"TSDiscGradUseGonzalez_C",TSDiscGradUseGonzalez_DiscGrad));
442   PetscFunctionReturn(0);
443 }
444 
445 /*@C
446   TSDiscGradGetFormulation - Get the construction method for S, F, and grad F from the formulation u_t = S grad F
447 
448   Not Collective
449 
450   Input Parameter:
451 . ts - timestepping context
452 
453   Output Parameters:
454 + Sfunc - constructor for the S matrix from the formulation
455 . Ffunc - functional F from the formulation
456 . Gfunc - constructor for the gradient of F from the formulation
457 - ctx   - the user context
458 
459   Calling sequence of Sfunc:
460 $ PetscErrorCode func(TS ts, PetscReal time, Vec u, Mat S, void *)
461 
462   Calling sequence of Ffunc:
463 $ PetscErrorCode func(TS ts, PetscReal time, Vec u, PetscScalar *F, void *)
464 
465   Calling sequence of Gfunc:
466 $ PetscErrorCode func(TS ts, PetscReal time, Vec u, Vec G, void *)
467 
468   Level: intermediate
469 
470 .seealso: TSDiscGradSetFormulation()
471 @*/
472 PetscErrorCode TSDiscGradGetFormulation(TS ts, PetscErrorCode (**Sfunc)(TS, PetscReal, Vec, Mat, void *), PetscErrorCode (**Ffunc)(TS, PetscReal, Vec, PetscScalar *, void *), PetscErrorCode (**Gfunc)(TS, PetscReal, Vec, Vec, void *), void *ctx)
473 {
474   PetscFunctionBegin;
475   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
476   PetscValidPointer(Sfunc, 2);
477   PetscValidPointer(Ffunc, 3);
478   PetscValidPointer(Gfunc, 4);
479   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));
480   PetscFunctionReturn(0);
481 }
482 
483 /*@C
484   TSDiscGradSetFormulation - Set the construction method for S, F, and grad F from the formulation u_t = S(u) grad F(u)
485 
486   Not Collective
487 
488   Input Parameters:
489 + ts    - timestepping context
490 . Sfunc - constructor for the S matrix from the formulation
491 . Ffunc - functional F from the formulation
492 - Gfunc - constructor for the gradient of F from the formulation
493   Calling sequence of Sfunc:
494 $ PetscErrorCode func(TS ts, PetscReal time, Vec u, Mat S, void *)
495 
496   Calling sequence of Ffunc:
497 $ PetscErrorCode func(TS ts, PetscReal time, Vec u, PetscScalar *F, void *)
498 
499   Calling sequence of Gfunc:
500 $ PetscErrorCode func(TS ts, PetscReal time, Vec u, Vec G, void *)
501 
502   Level: Intermediate
503 
504 .seealso: TSDiscGradGetFormulation()
505 @*/
506 PetscErrorCode TSDiscGradSetFormulation(TS ts, PetscErrorCode (*Sfunc)(TS, PetscReal, Vec, Mat, void *), PetscErrorCode (*Ffunc)(TS, PetscReal, Vec , PetscScalar *, void *), PetscErrorCode (*Gfunc)(TS, PetscReal, Vec, Vec, void *), void *ctx)
507 {
508   PetscFunctionBegin;
509   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
510   PetscValidFunction(Sfunc, 2);
511   PetscValidFunction(Ffunc, 3);
512   PetscValidFunction(Gfunc, 4);
513   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));
514   PetscFunctionReturn(0);
515 }
516 
517 /*@
518   TSDiscGradIsGonzalez - Checks flag for whether to use additional conservative terms in discrete gradient formulation.
519 
520   Not Collective
521 
522   Input Parameter:
523 .  ts - timestepping context
524 
525   Output Parameter:
526 .  gonzalez - PETSC_TRUE when using the Gonzalez term
527 
528   Level: Advanced
529 
530 .seealso: TSDiscGradUseGonzalez(), TSDISCGRAD
531 @*/
532 PetscErrorCode TSDiscGradIsGonzalez(TS ts,PetscBool *gonzalez)
533 {
534   PetscFunctionBegin;
535   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
536   PetscValidBoolPointer(gonzalez,2);
537   PetscUseMethod(ts,"TSDiscGradIsGonzalez_C",(TS,PetscBool*),(ts,gonzalez));
538   PetscFunctionReturn(0);
539 }
540 
541 /*@
542   TSDiscGradUseGonzalez - Sets discrete gradient formulation with or without additional conservative terms.  Without flag, the discrete gradients timestepper is just backwards euler
543 
544   Not Collective
545 
546   Input Parameters:
547 + ts - timestepping context
548 - flg - PETSC_TRUE to use the Gonzalez term
549 
550   Options Database:
551 . -ts_discgrad_gonzalez <flg> - use the Gonzalez term for the discrete gradient formulation
552 
553   Level: Intermediate
554 
555 .seealso: TSDISCGRAD
556 @*/
557 PetscErrorCode TSDiscGradUseGonzalez(TS ts,PetscBool flg)
558 {
559   PetscFunctionBegin;
560   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
561   PetscTryMethod(ts,"TSDiscGradUseGonzalez_C",(TS,PetscBool),(ts,flg));
562   PetscFunctionReturn(0);
563 }
564