Actual source code: theta.c
petsc-dev 2014-02-02
1: /*
2: Code for timestepping with implicit Theta method
3: */
4: #define PETSC_DESIRE_COMPLEX
5: #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/
6: #include <petscsnesfas.h>
7: #include <petscdm.h>
9: typedef struct {
10: Vec X,Xdot; /* Storage for one stage */
11: Vec X0; /* work vector to store X0 */
12: Vec affine; /* Affine vector needed for residual at beginning of step */
13: PetscBool extrapolate;
14: PetscBool endpoint;
15: PetscReal Theta;
16: PetscReal stage_time;
17: TSStepStatus status;
18: char *name;
19: PetscInt order;
20: PetscReal ccfl; /* Placeholder for CFL coefficient relative to forward Euler */
21: PetscBool adapt; /* use time-step adaptivity ? */
22: } TS_Theta;
26: static PetscErrorCode TSThetaGetX0AndXdot(TS ts,DM dm,Vec *X0,Vec *Xdot)
27: {
28: TS_Theta *th = (TS_Theta*)ts->data;
32: if (X0) {
33: if (dm && dm != ts->dm) {
34: DMGetNamedGlobalVector(dm,"TSTheta_X0",X0);
35: } else *X0 = ts->vec_sol;
36: }
37: if (Xdot) {
38: if (dm && dm != ts->dm) {
39: DMGetNamedGlobalVector(dm,"TSTheta_Xdot",Xdot);
40: } else *Xdot = th->Xdot;
41: }
42: return(0);
43: }
48: static PetscErrorCode TSThetaRestoreX0AndXdot(TS ts,DM dm,Vec *X0,Vec *Xdot)
49: {
53: if (X0) {
54: if (dm && dm != ts->dm) {
55: DMRestoreNamedGlobalVector(dm,"TSTheta_X0",X0);
56: }
57: }
58: if (Xdot) {
59: if (dm && dm != ts->dm) {
60: DMRestoreNamedGlobalVector(dm,"TSTheta_Xdot",Xdot);
61: }
62: }
63: return(0);
64: }
68: static PetscErrorCode DMCoarsenHook_TSTheta(DM fine,DM coarse,void *ctx)
69: {
72: return(0);
73: }
77: static PetscErrorCode DMRestrictHook_TSTheta(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
78: {
79: TS ts = (TS)ctx;
81: Vec X0,Xdot,X0_c,Xdot_c;
84: TSThetaGetX0AndXdot(ts,fine,&X0,&Xdot);
85: TSThetaGetX0AndXdot(ts,coarse,&X0_c,&Xdot_c);
86: MatRestrict(restrct,X0,X0_c);
87: MatRestrict(restrct,Xdot,Xdot_c);
88: VecPointwiseMult(X0_c,rscale,X0_c);
89: VecPointwiseMult(Xdot_c,rscale,Xdot_c);
90: TSThetaRestoreX0AndXdot(ts,fine,&X0,&Xdot);
91: TSThetaRestoreX0AndXdot(ts,coarse,&X0_c,&Xdot_c);
92: return(0);
93: }
97: static PetscErrorCode DMSubDomainHook_TSTheta(DM dm,DM subdm,void *ctx)
98: {
101: return(0);
102: }
106: static PetscErrorCode DMSubDomainRestrictHook_TSTheta(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
107: {
108: TS ts = (TS)ctx;
110: Vec X0,Xdot,X0_sub,Xdot_sub;
113: TSThetaGetX0AndXdot(ts,dm,&X0,&Xdot);
114: TSThetaGetX0AndXdot(ts,subdm,&X0_sub,&Xdot_sub);
116: VecScatterBegin(gscat,X0,X0_sub,INSERT_VALUES,SCATTER_FORWARD);
117: VecScatterEnd(gscat,X0,X0_sub,INSERT_VALUES,SCATTER_FORWARD);
119: VecScatterBegin(gscat,Xdot,Xdot_sub,INSERT_VALUES,SCATTER_FORWARD);
120: VecScatterEnd(gscat,Xdot,Xdot_sub,INSERT_VALUES,SCATTER_FORWARD);
122: TSThetaRestoreX0AndXdot(ts,dm,&X0,&Xdot);
123: TSThetaRestoreX0AndXdot(ts,subdm,&X0_sub,&Xdot_sub);
124: return(0);
125: }
129: static PetscErrorCode TSEvaluateStep_Theta(TS ts,PetscInt order,Vec U,PetscBool *done)
130: {
132: TS_Theta *th = (TS_Theta*)ts->data;
135: if (order == 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"No time-step adaptivity implemented for 1st order theta method; Run with -ts_adapt_type none");
136: if (order == th->order) {
137: if (th->endpoint) {
138: VecCopy(th->X,U);
139: } else {
140: PetscReal shift = 1./(th->Theta*ts->time_step);
141: VecAXPBYPCZ(th->Xdot,-shift,shift,0,U,th->X);
142: VecAXPY(U,ts->time_step,th->Xdot);
143: }
144: } else if (order == th->order-1 && order) {
145: VecWAXPY(U,ts->time_step,th->Xdot,th->X0);
146: }
147: return(0);
148: }
152: static PetscErrorCode TSStep_Theta(TS ts)
153: {
154: TS_Theta *th = (TS_Theta*)ts->data;
155: PetscInt its,lits,reject,next_scheme;
156: PetscReal next_time_step;
157: SNESConvergedReason snesreason;
158: PetscErrorCode ierr;
159: TSAdapt adapt;
160: PetscBool accept;
163: th->status = TS_STEP_INCOMPLETE;
164: VecCopy(ts->vec_sol,th->X0);
165: for (reject=0; reject<ts->max_reject && !ts->reason && th->status != TS_STEP_COMPLETE; reject++,ts->reject++) {
166: PetscReal shift = 1./(th->Theta*ts->time_step);
167: next_time_step = ts->time_step;
168: th->stage_time = ts->ptime + (th->endpoint ? 1. : th->Theta)*ts->time_step;
169: TSPreStep(ts);
170: TSPreStage(ts,th->stage_time);
172: if (th->endpoint) { /* This formulation assumes linear time-independent mass matrix */
173: VecZeroEntries(th->Xdot);
174: if (!th->affine) {VecDuplicate(ts->vec_sol,&th->affine);}
175: TSComputeIFunction(ts,ts->ptime,ts->vec_sol,th->Xdot,th->affine,PETSC_FALSE);
176: VecScale(th->affine,(th->Theta-1.)/th->Theta);
177: }
178: if (th->extrapolate) {
179: VecWAXPY(th->X,1./shift,th->Xdot,ts->vec_sol);
180: } else {
181: VecCopy(ts->vec_sol,th->X);
182: }
183: SNESSolve(ts->snes,th->affine,th->X);
184: SNESGetIterationNumber(ts->snes,&its);
185: SNESGetLinearSolveIterations(ts->snes,&lits);
186: SNESGetConvergedReason(ts->snes,&snesreason);
187: TSPostStage(ts,th->stage_time,0,&(th->X));
188: ts->snes_its += its; ts->ksp_its += lits;
189: TSGetAdapt(ts,&adapt);
190: TSAdaptCheckStage(adapt,ts,&accept);
191: if (!accept) continue;
192: TSEvaluateStep(ts,th->order,ts->vec_sol,NULL);
193: /* Register only the current method as a candidate because we're not supporting multiple candidates yet. */
194: TSGetAdapt(ts,&adapt);
195: TSAdaptCandidatesClear(adapt);
196: TSAdaptCandidateAdd(adapt,NULL,th->order,1,th->ccfl,1.0,PETSC_TRUE);
197: TSAdaptChoose(adapt,ts,ts->time_step,&next_scheme,&next_time_step,&accept);
199: if (accept) {
200: /* ignore next_scheme for now */
201: ts->ptime += ts->time_step;
202: ts->time_step = next_time_step;
203: ts->steps++;
204: th->status = TS_STEP_COMPLETE;
205: } else { /* Roll back the current step */
206: VecCopy(th->X0,ts->vec_sol);
207: ts->time_step = next_time_step;
208: th->status = TS_STEP_INCOMPLETE;
209: }
210: }
211: return(0);
212: }
216: static PetscErrorCode TSInterpolate_Theta(TS ts,PetscReal t,Vec X)
217: {
218: TS_Theta *th = (TS_Theta*)ts->data;
219: PetscReal alpha = t - ts->ptime;
223: VecCopy(ts->vec_sol,th->X);
224: if (th->endpoint) alpha *= th->Theta;
225: VecWAXPY(X,alpha,th->Xdot,th->X);
226: return(0);
227: }
229: /*------------------------------------------------------------*/
232: static PetscErrorCode TSReset_Theta(TS ts)
233: {
234: TS_Theta *th = (TS_Theta*)ts->data;
238: VecDestroy(&th->X);
239: VecDestroy(&th->Xdot);
240: VecDestroy(&th->X0);
241: VecDestroy(&th->affine);
242: return(0);
243: }
247: static PetscErrorCode TSDestroy_Theta(TS ts)
248: {
252: TSReset_Theta(ts);
253: PetscFree(ts->data);
254: PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetTheta_C",NULL);
255: PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetTheta_C",NULL);
256: PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetEndpoint_C",NULL);
257: PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetEndpoint_C",NULL);
258: return(0);
259: }
261: /*
262: This defines the nonlinear equation that is to be solved with SNES
263: G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
264: */
267: static PetscErrorCode SNESTSFormFunction_Theta(SNES snes,Vec x,Vec y,TS ts)
268: {
269: TS_Theta *th = (TS_Theta*)ts->data;
271: Vec X0,Xdot;
272: DM dm,dmsave;
273: PetscReal shift = 1./(th->Theta*ts->time_step);
276: SNESGetDM(snes,&dm);
277: /* When using the endpoint variant, this is actually 1/Theta * Xdot */
278: TSThetaGetX0AndXdot(ts,dm,&X0,&Xdot);
279: VecAXPBYPCZ(Xdot,-shift,shift,0,X0,x);
281: /* DM monkey-business allows user code to call TSGetDM() inside of functions evaluated on levels of FAS */
282: dmsave = ts->dm;
283: ts->dm = dm;
284: TSComputeIFunction(ts,th->stage_time,x,Xdot,y,PETSC_FALSE);
285: ts->dm = dmsave;
286: TSThetaRestoreX0AndXdot(ts,dm,&X0,&Xdot);
287: return(0);
288: }
292: static PetscErrorCode SNESTSFormJacobian_Theta(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *str,TS ts)
293: {
294: TS_Theta *th = (TS_Theta*)ts->data;
296: Vec Xdot;
297: DM dm,dmsave;
298: PetscReal shift = 1./(th->Theta*ts->time_step);
301: SNESGetDM(snes,&dm);
303: /* th->Xdot has already been computed in SNESTSFormFunction_Theta (SNES guarantees this) */
304: TSThetaGetX0AndXdot(ts,dm,NULL,&Xdot);
306: dmsave = ts->dm;
307: ts->dm = dm;
308: TSComputeIJacobian(ts,th->stage_time,x,Xdot,shift,A,B,str,PETSC_FALSE);
309: ts->dm = dmsave;
310: TSThetaRestoreX0AndXdot(ts,dm,NULL,&Xdot);
311: return(0);
312: }
316: static PetscErrorCode TSSetUp_Theta(TS ts)
317: {
318: TS_Theta *th = (TS_Theta*)ts->data;
320: SNES snes;
321: DM dm;
324: VecDuplicate(ts->vec_sol,&th->X);
325: VecDuplicate(ts->vec_sol,&th->Xdot);
326: VecDuplicate(ts->vec_sol,&th->X0);
327: TSGetSNES(ts,&snes);
328: TSGetDM(ts,&dm);
329: if (dm) {
330: DMCoarsenHookAdd(dm,DMCoarsenHook_TSTheta,DMRestrictHook_TSTheta,ts);
331: DMSubDomainHookAdd(dm,DMSubDomainHook_TSTheta,DMSubDomainRestrictHook_TSTheta,ts);
332: }
333: if (th->Theta == 0.5 && th->endpoint) th->order = 2;
334: else th->order = 1;
336: if (!th->adapt) {
337: TSAdapt adapt;
338: TSAdaptDestroy(&ts->adapt);
339: TSGetAdapt(ts,&adapt);
340: TSAdaptSetType(adapt,TSADAPTNONE);
341: }
342: return(0);
343: }
344: /*------------------------------------------------------------*/
348: static PetscErrorCode TSSetFromOptions_Theta(TS ts)
349: {
350: TS_Theta *th = (TS_Theta*)ts->data;
354: PetscOptionsHead("Theta ODE solver options");
355: {
356: PetscOptionsReal("-ts_theta_theta","Location of stage (0<Theta<=1)","TSThetaSetTheta",th->Theta,&th->Theta,NULL);
357: PetscOptionsBool("-ts_theta_extrapolate","Extrapolate stage solution from previous solution (sometimes unstable)","TSThetaSetExtrapolate",th->extrapolate,&th->extrapolate,NULL);
358: PetscOptionsBool("-ts_theta_endpoint","Use the endpoint instead of midpoint form of the Theta method","TSThetaSetEndpoint",th->endpoint,&th->endpoint,NULL);
359: PetscOptionsBool("-ts_theta_adapt","Use time-step adaptivity with the Theta method","",th->adapt,&th->adapt,NULL);
360: SNESSetFromOptions(ts->snes);
361: }
362: PetscOptionsTail();
363: return(0);
364: }
368: static PetscErrorCode TSView_Theta(TS ts,PetscViewer viewer)
369: {
370: TS_Theta *th = (TS_Theta*)ts->data;
371: PetscBool iascii;
375: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
376: if (iascii) {
377: PetscViewerASCIIPrintf(viewer," Theta=%g\n",(double)th->Theta);
378: PetscViewerASCIIPrintf(viewer," Extrapolation=%s\n",th->extrapolate ? "yes" : "no");
379: }
380: SNESView(ts->snes,viewer);
381: return(0);
382: }
386: PetscErrorCode TSThetaGetTheta_Theta(TS ts,PetscReal *theta)
387: {
388: TS_Theta *th = (TS_Theta*)ts->data;
391: *theta = th->Theta;
392: return(0);
393: }
397: PetscErrorCode TSThetaSetTheta_Theta(TS ts,PetscReal theta)
398: {
399: TS_Theta *th = (TS_Theta*)ts->data;
402: if (theta <= 0 || 1 < theta) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Theta %g not in range (0,1]",(double)theta);
403: th->Theta = theta;
404: return(0);
405: }
409: PetscErrorCode TSThetaGetEndpoint_Theta(TS ts,PetscBool *endpoint)
410: {
411: TS_Theta *th = (TS_Theta*)ts->data;
414: *endpoint = th->endpoint;
415: return(0);
416: }
420: PetscErrorCode TSThetaSetEndpoint_Theta(TS ts,PetscBool flg)
421: {
422: TS_Theta *th = (TS_Theta*)ts->data;
425: th->endpoint = flg;
426: return(0);
427: }
429: #if defined(PETSC_HAVE_COMPLEX)
432: static PetscErrorCode TSComputeLinearStability_Theta(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
433: {
434: PetscComplex z = xr + xi*PETSC_i,f;
435: TS_Theta *th = (TS_Theta*)ts->data;
436: const PetscReal one = 1.0;
439: f = (one + (one - th->Theta)*z)/(one - th->Theta*z);
440: *yr = PetscRealPartComplex(f);
441: *yi = PetscImaginaryPartComplex(f);
442: return(0);
443: }
444: #endif
447: /* ------------------------------------------------------------ */
448: /*MC
449: TSTHETA - DAE solver using the implicit Theta method
451: Level: beginner
453: Options Database:
454: -ts_theta_theta <Theta> - Location of stage (0<Theta<=1)
455: -ts_theta_extrapolate <flg> Extrapolate stage solution from previous solution (sometimes unstable)
456: -ts_theta_endpoint <flag> - Use the endpoint (like Crank-Nicholson) instead of midpoint form of the Theta method
458: Notes:
459: $ -ts_type theta -ts_theta_theta 1.0 corresponds to backward Euler (TSBEULER)
460: $ -ts_type theta -ts_theta_theta 0.5 corresponds to the implicit midpoint rule
461: $ -ts_type theta -ts_theta_theta 0.5 -ts_theta_endpoint corresponds to Crank-Nicholson (TSCN)
465: This method can be applied to DAE.
467: This method is cast as a 1-stage implicit Runge-Kutta method.
469: .vb
470: Theta | Theta
471: -------------
472: | 1
473: .ve
475: For the default Theta=0.5, this is also known as the implicit midpoint rule.
477: When the endpoint variant is chosen, the method becomes a 2-stage method with first stage explicit:
479: .vb
480: 0 | 0 0
481: 1 | 1-Theta Theta
482: -------------------
483: | 1-Theta Theta
484: .ve
486: For the default Theta=0.5, this is the trapezoid rule (also known as Crank-Nicolson, see TSCN).
488: To apply a diagonally implicit RK method to DAE, the stage formula
490: $ Y_i = X + h sum_j a_ij Y'_j
492: is interpreted as a formula for Y'_i in terms of Y_i and known values (Y'_j, j<i)
494: .seealso: TSCreate(), TS, TSSetType(), TSCN, TSBEULER, TSThetaSetTheta(), TSThetaSetEndpoint()
496: M*/
499: PETSC_EXTERN PetscErrorCode TSCreate_Theta(TS ts)
500: {
501: TS_Theta *th;
505: ts->ops->reset = TSReset_Theta;
506: ts->ops->destroy = TSDestroy_Theta;
507: ts->ops->view = TSView_Theta;
508: ts->ops->setup = TSSetUp_Theta;
509: ts->ops->step = TSStep_Theta;
510: ts->ops->interpolate = TSInterpolate_Theta;
511: ts->ops->evaluatestep = TSEvaluateStep_Theta;
512: ts->ops->setfromoptions = TSSetFromOptions_Theta;
513: ts->ops->snesfunction = SNESTSFormFunction_Theta;
514: ts->ops->snesjacobian = SNESTSFormJacobian_Theta;
515: #if defined(PETSC_HAVE_COMPLEX)
516: ts->ops->linearstability = TSComputeLinearStability_Theta;
517: #endif
519: PetscNewLog(ts,&th);
520: ts->data = (void*)th;
522: th->extrapolate = PETSC_FALSE;
523: th->Theta = 0.5;
524: th->ccfl = 1.0;
525: th->adapt = PETSC_FALSE;
526: PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetTheta_C",TSThetaGetTheta_Theta);
527: PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetTheta_C",TSThetaSetTheta_Theta);
528: PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetEndpoint_C",TSThetaGetEndpoint_Theta);
529: PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetEndpoint_C",TSThetaSetEndpoint_Theta);
530: return(0);
531: }
535: /*@
536: TSThetaGetTheta - Get the abscissa of the stage in (0,1].
538: Not Collective
540: Input Parameter:
541: . ts - timestepping context
543: Output Parameter:
544: . theta - stage abscissa
546: Note:
547: Use of this function is normally only required to hack TSTHETA to use a modified integration scheme.
549: Level: Advanced
551: .seealso: TSThetaSetTheta()
552: @*/
553: PetscErrorCode TSThetaGetTheta(TS ts,PetscReal *theta)
554: {
560: PetscUseMethod(ts,"TSThetaGetTheta_C",(TS,PetscReal*),(ts,theta));
561: return(0);
562: }
566: /*@
567: TSThetaSetTheta - Set the abscissa of the stage in (0,1].
569: Not Collective
571: Input Parameter:
572: + ts - timestepping context
573: - theta - stage abscissa
575: Options Database:
576: . -ts_theta_theta <theta>
578: Level: Intermediate
580: .seealso: TSThetaGetTheta()
581: @*/
582: PetscErrorCode TSThetaSetTheta(TS ts,PetscReal theta)
583: {
588: PetscTryMethod(ts,"TSThetaSetTheta_C",(TS,PetscReal),(ts,theta));
589: return(0);
590: }
594: /*@
595: TSThetaGetEndpoint - Gets whether to use the endpoint variant of the method (e.g. trapezoid/Crank-Nicolson instead of midpoint rule).
597: Not Collective
599: Input Parameter:
600: . ts - timestepping context
602: Output Parameter:
603: . endpoint - PETSC_TRUE when using the endpoint variant
605: Level: Advanced
607: .seealso: TSThetaSetEndpoint(), TSTHETA, TSCN
608: @*/
609: PetscErrorCode TSThetaGetEndpoint(TS ts,PetscBool *endpoint)
610: {
616: PetscTryMethod(ts,"TSThetaGetEndpoint_C",(TS,PetscBool*),(ts,endpoint));
617: return(0);
618: }
622: /*@
623: TSThetaSetEndpoint - Sets whether to use the endpoint variant of the method (e.g. trapezoid/Crank-Nicolson instead of midpoint rule).
625: Not Collective
627: Input Parameter:
628: + ts - timestepping context
629: - flg - PETSC_TRUE to use the endpoint variant
631: Options Database:
632: . -ts_theta_endpoint <flg>
634: Level: Intermediate
636: .seealso: TSTHETA, TSCN
637: @*/
638: PetscErrorCode TSThetaSetEndpoint(TS ts,PetscBool flg)
639: {
644: PetscTryMethod(ts,"TSThetaSetEndpoint_C",(TS,PetscBool),(ts,flg));
645: return(0);
646: }
648: /*
649: * TSBEULER and TSCN are straightforward specializations of TSTHETA.
650: * The creation functions for these specializations are below.
651: */
655: static PetscErrorCode TSView_BEuler(TS ts,PetscViewer viewer)
656: {
660: SNESView(ts->snes,viewer);
661: return(0);
662: }
664: /*MC
665: TSBEULER - ODE solver using the implicit backward Euler method
667: Level: beginner
669: Notes:
670: TSBEULER is equivalent to TSTHETA with Theta=1.0
672: $ -ts_type theta -ts_theta_theta 1.
674: .seealso: TSCreate(), TS, TSSetType(), TSEULER, TSCN, TSTHETA
676: M*/
679: PETSC_EXTERN PetscErrorCode TSCreate_BEuler(TS ts)
680: {
684: TSCreate_Theta(ts);
685: TSThetaSetTheta(ts,1.0);
686: ts->ops->view = TSView_BEuler;
687: return(0);
688: }
692: static PetscErrorCode TSView_CN(TS ts,PetscViewer viewer)
693: {
697: SNESView(ts->snes,viewer);
698: return(0);
699: }
701: /*MC
702: TSCN - ODE solver using the implicit Crank-Nicolson method.
704: Level: beginner
706: Notes:
707: TSCN is equivalent to TSTHETA with Theta=0.5 and the "endpoint" option set. I.e.
709: $ -ts_type theta -ts_theta_theta 0.5 -ts_theta_endpoint
711: .seealso: TSCreate(), TS, TSSetType(), TSBEULER, TSTHETA
713: M*/
716: PETSC_EXTERN PetscErrorCode TSCreate_CN(TS ts)
717: {
721: TSCreate_Theta(ts);
722: TSThetaSetTheta(ts,0.5);
723: TSThetaSetEndpoint(ts,PETSC_TRUE);
724: ts->ops->view = TSView_CN;
725: return(0);
726: }