Actual source code: arkimex.c
1: /*
2: Code for timestepping with additive Runge-Kutta IMEX method
4: Notes:
5: The general system is written as
7: F(t,U,Udot) = G(t,U)
9: where F represents the stiff part of the physics and G represents the non-stiff part.
11: */
12: #include <petsc/private/tsimpl.h>
13: #include <petscdm.h>
15: static TSARKIMEXType TSARKIMEXDefault = TSARKIMEX3;
16: static PetscBool TSARKIMEXRegisterAllCalled;
17: static PetscBool TSARKIMEXPackageInitialized;
18: static PetscErrorCode TSExtrapolate_ARKIMEX(TS,PetscReal,Vec);
20: typedef struct _ARKTableau *ARKTableau;
21: struct _ARKTableau {
22: char *name;
23: PetscInt order; /* Classical approximation order of the method */
24: PetscInt s; /* Number of stages */
25: PetscBool stiffly_accurate; /* The implicit part is stiffly accurate*/
26: PetscBool FSAL_implicit; /* The implicit part is FSAL*/
27: PetscBool explicit_first_stage; /* The implicit part has an explicit first stage*/
28: PetscInt pinterp; /* Interpolation order */
29: PetscReal *At,*bt,*ct; /* Stiff tableau */
30: PetscReal *A,*b,*c; /* Non-stiff tableau */
31: PetscReal *bembedt,*bembed; /* Embedded formula of order one less (order-1) */
32: PetscReal *binterpt,*binterp; /* Dense output formula */
33: PetscReal ccfl; /* Placeholder for CFL coefficient relative to forward Euler */
34: };
35: typedef struct _ARKTableauLink *ARKTableauLink;
36: struct _ARKTableauLink {
37: struct _ARKTableau tab;
38: ARKTableauLink next;
39: };
40: static ARKTableauLink ARKTableauList;
42: typedef struct {
43: ARKTableau tableau;
44: Vec *Y; /* States computed during the step */
45: Vec *YdotI; /* Time derivatives for the stiff part */
46: Vec *YdotRHS; /* Function evaluations for the non-stiff part */
47: Vec *Y_prev; /* States computed during the previous time step */
48: Vec *YdotI_prev; /* Time derivatives for the stiff part for the previous time step*/
49: Vec *YdotRHS_prev; /* Function evaluations for the non-stiff part for the previous time step*/
50: Vec Ydot0; /* Holds the slope from the previous step in FSAL case */
51: Vec Ydot; /* Work vector holding Ydot during residual evaluation */
52: Vec Z; /* Ydot = shift(Y-Z) */
53: PetscScalar *work; /* Scalar work */
54: PetscReal scoeff; /* shift = scoeff/dt */
55: PetscReal stage_time;
56: PetscBool imex;
57: PetscBool extrapolate; /* Extrapolate initial guess from previous time-step stage values */
58: TSStepStatus status;
59: } TS_ARKIMEX;
60: /*MC
61: TSARKIMEXARS122 - Second order ARK IMEX scheme.
63: This method has one explicit stage and one implicit stage.
65: Options Database:
66: . -ts_arkimex_type ars122 - set arkimex type to ars122
68: References:
69: . * - U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).
71: Level: advanced
73: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
74: M*/
75: /*MC
76: TSARKIMEXA2 - Second order ARK IMEX scheme with A-stable implicit part.
78: This method has an explicit stage and one implicit stage, and has an A-stable implicit scheme. This method was provided by Emil Constantinescu.
80: Options Database:
81: . -ts_arkimex_type a2 - set arkimex type to a2
83: Level: advanced
85: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
86: M*/
87: /*MC
88: TSARKIMEXL2 - Second order ARK IMEX scheme with L-stable implicit part.
90: This method has two implicit stages, and L-stable implicit scheme.
92: Options Database:
93: . -ts_arkimex_type l2 - set arkimex type to l2
95: References:
96: . * - L. Pareschi, G. Russo, Implicit Explicit Runge Kutta schemes and applications to hyperbolic systems with relaxations. Journal of Scientific Computing Volume: 25, Issue: 1, October, 2005.
98: Level: advanced
100: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
101: M*/
102: /*MC
103: TSARKIMEX1BEE - First order backward Euler represented as an ARK IMEX scheme with extrapolation as error estimator. This is a 3-stage method.
105: This method is aimed at starting the integration of implicit DAEs when explicit first-stage ARK methods are used.
107: Options Database:
108: . -ts_arkimex_type 1bee - set arkimex type to 1bee
110: Level: advanced
112: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
113: M*/
114: /*MC
115: TSARKIMEX2C - Second order ARK IMEX scheme with L-stable implicit part.
117: This method has one explicit stage and two implicit stages. The implicit part is the same as in TSARKIMEX2D and TSARKIMEX2E, but the explicit part has a larger stability region on the negative real axis. This method was provided by Emil Constantinescu.
119: Options Database:
120: . -ts_arkimex_type 2c - set arkimex type to 2c
122: Level: advanced
124: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
125: M*/
126: /*MC
127: TSARKIMEX2D - Second order ARK IMEX scheme with L-stable implicit part.
129: This method has one explicit stage and two implicit stages. The stability function is independent of the explicit part in the infinity limit of the implict component. This method was provided by Emil Constantinescu.
131: Options Database:
132: . -ts_arkimex_type 2d - set arkimex type to 2d
134: Level: advanced
136: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
137: M*/
138: /*MC
139: TSARKIMEX2E - Second order ARK IMEX scheme with L-stable implicit part.
141: This method has one explicit stage and two implicit stages. It is is an optimal method developed by Emil Constantinescu.
143: Options Database:
144: . -ts_arkimex_type 2e - set arkimex type to 2e
146: Level: advanced
148: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
149: M*/
150: /*MC
151: TSARKIMEXPRSSP2 - Second order SSP ARK IMEX scheme.
153: This method has three implicit stages.
155: References:
156: . * - L. Pareschi, G. Russo, Implicit Explicit Runge Kutta schemes and applications to hyperbolic systems with relaxations. Journal of Scientific Computing Volume: 25, Issue: 1, October, 2005.
158: This method is referred to as SSP2-(3,3,2) in https://arxiv.org/abs/1110.4375
160: Options Database:
161: . -ts_arkimex_type prssp2 - set arkimex type to prssp2
163: Level: advanced
165: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
166: M*/
167: /*MC
168: TSARKIMEX3 - Third order ARK IMEX scheme with L-stable implicit part.
170: This method has one explicit stage and three implicit stages.
172: Options Database:
173: . -ts_arkimex_type 3 - set arkimex type to 3
175: References:
176: . * - Kennedy and Carpenter 2003.
178: Level: advanced
180: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
181: M*/
182: /*MC
183: TSARKIMEXARS443 - Third order ARK IMEX scheme.
185: This method has one explicit stage and four implicit stages.
187: Options Database:
188: . -ts_arkimex_type ars443 - set arkimex type to ars443
190: References:
191: + * - U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).
192: - * - This method is referred to as ARS(4,4,3) in https://arxiv.org/abs/1110.4375
194: Level: advanced
196: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
197: M*/
198: /*MC
199: TSARKIMEXBPR3 - Third order ARK IMEX scheme.
201: This method has one explicit stage and four implicit stages.
203: Options Database:
204: . -ts_arkimex_type bpr3 - set arkimex type to bpr3
206: References:
207: . * - This method is referred to as ARK3 in https://arxiv.org/abs/1110.4375
209: Level: advanced
211: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
212: M*/
213: /*MC
214: TSARKIMEX4 - Fourth order ARK IMEX scheme with L-stable implicit part.
216: This method has one explicit stage and four implicit stages.
218: Options Database:
219: . -ts_arkimex_type 4 - set arkimex type to4
221: References:
222: . * - Kennedy and Carpenter 2003.
224: Level: advanced
226: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
227: M*/
228: /*MC
229: TSARKIMEX5 - Fifth order ARK IMEX scheme with L-stable implicit part.
231: This method has one explicit stage and five implicit stages.
233: Options Database:
234: . -ts_arkimex_type 5 - set arkimex type to 5
236: References:
237: . * - Kennedy and Carpenter 2003.
239: Level: advanced
241: .seealso: TSARKIMEX, TSARKIMEXType, TSARKIMEXSetType()
242: M*/
244: /*@C
245: TSARKIMEXRegisterAll - Registers all of the additive Runge-Kutta implicit-explicit methods in TSARKIMEX
247: Not Collective, but should be called by all processes which will need the schemes to be registered
249: Level: advanced
251: .seealso: TSARKIMEXRegisterDestroy()
252: @*/
253: PetscErrorCode TSARKIMEXRegisterAll(void)
254: {
255: if (TSARKIMEXRegisterAllCalled) return 0;
256: TSARKIMEXRegisterAllCalled = PETSC_TRUE;
258: {
259: const PetscReal
260: A[3][3] = {{0.0,0.0,0.0},
261: {0.0,0.0,0.0},
262: {0.0,0.5,0.0}},
263: At[3][3] = {{1.0,0.0,0.0},
264: {0.0,0.5,0.0},
265: {0.0,0.5,0.5}},
266: b[3] = {0.0,0.5,0.5},
267: bembedt[3] = {1.0,0.0,0.0};
268: TSARKIMEXRegister(TSARKIMEX1BEE,2,3,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
269: }
270: {
271: const PetscReal
272: A[2][2] = {{0.0,0.0},
273: {0.5,0.0}},
274: At[2][2] = {{0.0,0.0},
275: {0.0,0.5}},
276: b[2] = {0.0,1.0},
277: bembedt[2] = {0.5,0.5};
278: /* binterpt[2][2] = {{1.0,-1.0},{0.0,1.0}}; second order dense output has poor stability properties and hence it is not currently in use*/
279: TSARKIMEXRegister(TSARKIMEXARS122,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
280: }
281: {
282: const PetscReal
283: A[2][2] = {{0.0,0.0},
284: {1.0,0.0}},
285: At[2][2] = {{0.0,0.0},
286: {0.5,0.5}},
287: b[2] = {0.5,0.5},
288: bembedt[2] = {0.0,1.0};
289: /* binterpt[2][2] = {{1.0,-0.5},{0.0,0.5}} second order dense output has poor stability properties and hence it is not currently in use*/
290: TSARKIMEXRegister(TSARKIMEXA2,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
291: }
292: {
293: /* const PetscReal us2 = 1.0-1.0/PetscSqrtReal((PetscReal)2.0); Direct evaluation: 0.2928932188134524755992. Used below to ensure all values are available at compile time */
294: const PetscReal
295: A[2][2] = {{0.0,0.0},
296: {1.0,0.0}},
297: At[2][2] = {{0.2928932188134524755992,0.0},
298: {1.0-2.0*0.2928932188134524755992,0.2928932188134524755992}},
299: b[2] = {0.5,0.5},
300: bembedt[2] = {0.0,1.0},
301: binterpt[2][2] = {{ (0.2928932188134524755992-1.0)/(2.0*0.2928932188134524755992-1.0),-1/(2.0*(1.0-2.0*0.2928932188134524755992))},
302: {1-(0.2928932188134524755992-1.0)/(2.0*0.2928932188134524755992-1.0),-1/(2.0*(1.0-2.0*0.2928932188134524755992))}},
303: binterp[2][2] = {{1.0,-0.5},{0.0,0.5}};
304: TSARKIMEXRegister(TSARKIMEXL2,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,2,binterpt[0],binterp[0]);
305: }
306: {
307: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
308: const PetscReal
309: A[3][3] = {{0,0,0},
310: {2-1.414213562373095048802,0,0},
311: {0.5,0.5,0}},
312: At[3][3] = {{0,0,0},
313: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
314: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
315: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
316: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
317: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
318: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
319: TSARKIMEXRegister(TSARKIMEX2C,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
320: }
321: {
322: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
323: const PetscReal
324: A[3][3] = {{0,0,0},
325: {2-1.414213562373095048802,0,0},
326: {0.75,0.25,0}},
327: At[3][3] = {{0,0,0},
328: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
329: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
330: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
331: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
332: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
333: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
334: TSARKIMEXRegister(TSARKIMEX2D,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
335: }
336: { /* Optimal for linear implicit part */
337: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
338: const PetscReal
339: A[3][3] = {{0,0,0},
340: {2-1.414213562373095048802,0,0},
341: {(3-2*1.414213562373095048802)/6,(3+2*1.414213562373095048802)/6,0}},
342: At[3][3] = {{0,0,0},
343: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
344: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
345: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
346: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
347: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
348: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
349: TSARKIMEXRegister(TSARKIMEX2E,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
350: }
351: { /* Optimal for linear implicit part */
352: const PetscReal
353: A[3][3] = {{0,0,0},
354: {0.5,0,0},
355: {0.5,0.5,0}},
356: At[3][3] = {{0.25,0,0},
357: {0,0.25,0},
358: {1./3,1./3,1./3}};
359: TSARKIMEXRegister(TSARKIMEXPRSSP2,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,NULL,NULL,0,NULL,NULL);
360: }
361: {
362: const PetscReal
363: A[4][4] = {{0,0,0,0},
364: {1767732205903./2027836641118.,0,0,0},
365: {5535828885825./10492691773637.,788022342437./10882634858940.,0,0},
366: {6485989280629./16251701735622.,-4246266847089./9704473918619.,10755448449292./10357097424841.,0}},
367: At[4][4] = {{0,0,0,0},
368: {1767732205903./4055673282236.,1767732205903./4055673282236.,0,0},
369: {2746238789719./10658868560708.,-640167445237./6845629431997.,1767732205903./4055673282236.,0},
370: {1471266399579./7840856788654.,-4482444167858./7529755066697.,11266239266428./11593286722821.,1767732205903./4055673282236.}},
371: bembedt[4] = {2756255671327./12835298489170.,-10771552573575./22201958757719.,9247589265047./10645013368117.,2193209047091./5459859503100.},
372: binterpt[4][2] = {{4655552711362./22874653954995., -215264564351./13552729205753.},
373: {-18682724506714./9892148508045.,17870216137069./13817060693119.},
374: {34259539580243./13192909600954.,-28141676662227./17317692491321.},
375: {584795268549./6622622206610., 2508943948391./7218656332882.}};
376: TSARKIMEXRegister(TSARKIMEX3,3,4,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
377: }
378: {
379: const PetscReal
380: A[5][5] = {{0,0,0,0,0},
381: {1./2,0,0,0,0},
382: {11./18,1./18,0,0,0},
383: {5./6,-5./6,.5,0,0},
384: {1./4,7./4,3./4,-7./4,0}},
385: At[5][5] = {{0,0,0,0,0},
386: {0,1./2,0,0,0},
387: {0,1./6,1./2,0,0},
388: {0,-1./2,1./2,1./2,0},
389: {0,3./2,-3./2,1./2,1./2}},
390: *bembedt = NULL;
391: TSARKIMEXRegister(TSARKIMEXARS443,3,5,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,0,NULL,NULL);
392: }
393: {
394: const PetscReal
395: A[5][5] = {{0,0,0,0,0},
396: {1,0,0,0,0},
397: {4./9,2./9,0,0,0},
398: {1./4,0,3./4,0,0},
399: {1./4,0,3./5,0,0}},
400: At[5][5] = {{0,0,0,0,0},
401: {.5,.5,0,0,0},
402: {5./18,-1./9,.5,0,0},
403: {.5,0,0,.5,0},
404: {.25,0,.75,-.5,.5}},
405: *bembedt = NULL;
406: TSARKIMEXRegister(TSARKIMEXBPR3,3,5,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,0,NULL,NULL);
407: }
408: {
409: const PetscReal
410: A[6][6] = {{0,0,0,0,0,0},
411: {1./2,0,0,0,0,0},
412: {13861./62500.,6889./62500.,0,0,0,0},
413: {-116923316275./2393684061468.,-2731218467317./15368042101831.,9408046702089./11113171139209.,0,0,0},
414: {-451086348788./2902428689909.,-2682348792572./7519795681897.,12662868775082./11960479115383.,3355817975965./11060851509271.,0,0},
415: {647845179188./3216320057751.,73281519250./8382639484533.,552539513391./3454668386233.,3354512671639./8306763924573.,4040./17871.,0}},
416: At[6][6] = {{0,0,0,0,0,0},
417: {1./4,1./4,0,0,0,0},
418: {8611./62500.,-1743./31250.,1./4,0,0,0},
419: {5012029./34652500.,-654441./2922500.,174375./388108.,1./4,0,0},
420: {15267082809./155376265600.,-71443401./120774400.,730878875./902184768.,2285395./8070912.,1./4,0},
421: {82889./524892.,0,15625./83664.,69875./102672.,-2260./8211,1./4}},
422: bembedt[6] = {4586570599./29645900160.,0,178811875./945068544.,814220225./1159782912.,-3700637./11593932.,61727./225920.},
423: binterpt[6][3] = {{6943876665148./7220017795957.,-54480133./30881146.,6818779379841./7100303317025.},
424: {0,0,0},
425: {7640104374378./9702883013639.,-11436875./14766696.,2173542590792./12501825683035.},
426: {-20649996744609./7521556579894.,174696575./18121608.,-31592104683404./5083833661969.},
427: {8854892464581./2390941311638.,-12120380./966161.,61146701046299./7138195549469.},
428: {-11397109935349./6675773540249.,3843./706.,-17219254887155./4939391667607.}};
429: TSARKIMEXRegister(TSARKIMEX4,4,6,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,3,binterpt[0],NULL);
430: }
431: {
432: const PetscReal
433: A[8][8] = {{0,0,0,0,0,0,0,0},
434: {41./100,0,0,0,0,0,0,0},
435: {367902744464./2072280473677.,677623207551./8224143866563.,0,0,0,0,0,0},
436: {1268023523408./10340822734521.,0,1029933939417./13636558850479.,0,0,0,0,0},
437: {14463281900351./6315353703477.,0,66114435211212./5879490589093.,-54053170152839./4284798021562.,0,0,0,0},
438: {14090043504691./34967701212078.,0,15191511035443./11219624916014.,-18461159152457./12425892160975.,-281667163811./9011619295870.,0,0,0},
439: {19230459214898./13134317526959.,0,21275331358303./2942455364971.,-38145345988419./4862620318723.,-1./8,-1./8,0,0},
440: {-19977161125411./11928030595625.,0,-40795976796054./6384907823539.,177454434618887./12078138498510.,782672205425./8267701900261.,-69563011059811./9646580694205.,7356628210526./4942186776405.,0}},
441: At[8][8] = {{0,0,0,0,0,0,0,0},
442: {41./200.,41./200.,0,0,0,0,0,0},
443: {41./400.,-567603406766./11931857230679.,41./200.,0,0,0,0,0},
444: {683785636431./9252920307686.,0,-110385047103./1367015193373.,41./200.,0,0,0,0},
445: {3016520224154./10081342136671.,0,30586259806659./12414158314087.,-22760509404356./11113319521817.,41./200.,0,0,0},
446: {218866479029./1489978393911.,0,638256894668./5436446318841.,-1179710474555./5321154724896.,-60928119172./8023461067671.,41./200.,0,0},
447: {1020004230633./5715676835656.,0,25762820946817./25263940353407.,-2161375909145./9755907335909.,-211217309593./5846859502534.,-4269925059573./7827059040749.,41./200,0},
448: {-872700587467./9133579230613.,0,0,22348218063261./9555858737531.,-1143369518992./8141816002931.,-39379526789629./19018526304540.,32727382324388./42900044865799.,41./200.}},
449: bembedt[8] = {-975461918565./9796059967033.,0,0,78070527104295./32432590147079.,-548382580838./3424219808633.,-33438840321285./15594753105479.,3629800801594./4656183773603.,4035322873751./18575991585200.},
450: binterpt[8][3] = {{-17674230611817./10670229744614., 43486358583215./12773830924787., -9257016797708./5021505065439.},
451: {0, 0, 0 },
452: {0, 0, 0 },
453: {65168852399939./7868540260826., -91478233927265./11067650958493., 26096422576131./11239449250142.},
454: {15494834004392./5936557850923., -79368583304911./10890268929626., 92396832856987./20362823103730.},
455: {-99329723586156./26959484932159., -12239297817655./9152339842473., 30029262896817./10175596800299.},
456: {-19024464361622./5461577185407., 115839755401235./10719374521269., -26136350496073./3983972220547.},
457: {-6511271360970./6095937251113., 5843115559534./2180450260947., -5289405421727./3760307252460. }};
458: TSARKIMEXRegister(TSARKIMEX5,5,8,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,3,binterpt[0],NULL);
459: }
460: return 0;
461: }
463: /*@C
464: TSARKIMEXRegisterDestroy - Frees the list of schemes that were registered by TSARKIMEXRegister().
466: Not Collective
468: Level: advanced
470: .seealso: TSARKIMEXRegister(), TSARKIMEXRegisterAll()
471: @*/
472: PetscErrorCode TSARKIMEXRegisterDestroy(void)
473: {
474: ARKTableauLink link;
476: while ((link = ARKTableauList)) {
477: ARKTableau t = &link->tab;
478: ARKTableauList = link->next;
479: PetscFree6(t->At,t->bt,t->ct,t->A,t->b,t->c);
480: PetscFree2(t->bembedt,t->bembed);
481: PetscFree2(t->binterpt,t->binterp);
482: PetscFree(t->name);
483: PetscFree(link);
484: }
485: TSARKIMEXRegisterAllCalled = PETSC_FALSE;
486: return 0;
487: }
489: /*@C
490: TSARKIMEXInitializePackage - This function initializes everything in the TSARKIMEX package. It is called
491: from TSInitializePackage().
493: Level: developer
495: .seealso: PetscInitialize()
496: @*/
497: PetscErrorCode TSARKIMEXInitializePackage(void)
498: {
499: if (TSARKIMEXPackageInitialized) return 0;
500: TSARKIMEXPackageInitialized = PETSC_TRUE;
501: TSARKIMEXRegisterAll();
502: PetscRegisterFinalize(TSARKIMEXFinalizePackage);
503: return 0;
504: }
506: /*@C
507: TSARKIMEXFinalizePackage - This function destroys everything in the TSARKIMEX package. It is
508: called from PetscFinalize().
510: Level: developer
512: .seealso: PetscFinalize()
513: @*/
514: PetscErrorCode TSARKIMEXFinalizePackage(void)
515: {
516: TSARKIMEXPackageInitialized = PETSC_FALSE;
517: TSARKIMEXRegisterDestroy();
518: return 0;
519: }
521: /*@C
522: TSARKIMEXRegister - register an ARK IMEX scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation
524: Not Collective, but the same schemes should be registered on all processes on which they will be used
526: Input Parameters:
527: + name - identifier for method
528: . order - approximation order of method
529: . s - number of stages, this is the dimension of the matrices below
530: . At - Butcher table of stage coefficients for stiff part (dimension s*s, row-major)
531: . bt - Butcher table for completing the stiff part of the step (dimension s; NULL to use the last row of At)
532: . ct - Abscissa of each stiff stage (dimension s, NULL to use row sums of At)
533: . A - Non-stiff stage coefficients (dimension s*s, row-major)
534: . b - Non-stiff step completion table (dimension s; NULL to use last row of At)
535: . c - Non-stiff abscissa (dimension s; NULL to use row sums of A)
536: . bembedt - Stiff part of completion table for embedded method (dimension s; NULL if not available)
537: . bembed - Non-stiff part of completion table for embedded method (dimension s; NULL to use bembedt if provided)
538: . pinterp - Order of the interpolation scheme, equal to the number of columns of binterpt and binterp
539: . binterpt - Coefficients of the interpolation formula for the stiff part (dimension s*pinterp)
540: - binterp - Coefficients of the interpolation formula for the non-stiff part (dimension s*pinterp; NULL to reuse binterpt)
542: Notes:
543: Several ARK IMEX methods are provided, this function is only needed to create new methods.
545: Level: advanced
547: .seealso: TSARKIMEX
548: @*/
549: PetscErrorCode TSARKIMEXRegister(TSARKIMEXType name,PetscInt order,PetscInt s,
550: const PetscReal At[],const PetscReal bt[],const PetscReal ct[],
551: const PetscReal A[],const PetscReal b[],const PetscReal c[],
552: const PetscReal bembedt[],const PetscReal bembed[],
553: PetscInt pinterp,const PetscReal binterpt[],const PetscReal binterp[])
554: {
555: ARKTableauLink link;
556: ARKTableau t;
557: PetscInt i,j;
559: TSARKIMEXInitializePackage();
560: PetscNew(&link);
561: t = &link->tab;
562: PetscStrallocpy(name,&t->name);
563: t->order = order;
564: t->s = s;
565: PetscMalloc6(s*s,&t->At,s,&t->bt,s,&t->ct,s*s,&t->A,s,&t->b,s,&t->c);
566: PetscArraycpy(t->At,At,s*s);
567: PetscArraycpy(t->A,A,s*s);
568: if (bt) PetscArraycpy(t->bt,bt,s);
569: else for (i=0; i<s; i++) t->bt[i] = At[(s-1)*s+i];
570: if (b) PetscArraycpy(t->b,b,s);
571: else for (i=0; i<s; i++) t->b[i] = t->bt[i];
572: if (ct) PetscArraycpy(t->ct,ct,s);
573: else for (i=0; i<s; i++) for (j=0,t->ct[i]=0; j<s; j++) t->ct[i] += At[i*s+j];
574: if (c) PetscArraycpy(t->c,c,s);
575: else for (i=0; i<s; i++) for (j=0,t->c[i]=0; j<s; j++) t->c[i] += A[i*s+j];
576: t->stiffly_accurate = PETSC_TRUE;
577: for (i=0; i<s; i++) if (t->At[(s-1)*s+i] != t->bt[i]) t->stiffly_accurate = PETSC_FALSE;
578: t->explicit_first_stage = PETSC_TRUE;
579: for (i=0; i<s; i++) if (t->At[i] != 0.0) t->explicit_first_stage = PETSC_FALSE;
580: /*def of FSAL can be made more precise*/
581: t->FSAL_implicit = (PetscBool)(t->explicit_first_stage && t->stiffly_accurate);
582: if (bembedt) {
583: PetscMalloc2(s,&t->bembedt,s,&t->bembed);
584: PetscArraycpy(t->bembedt,bembedt,s);
585: PetscArraycpy(t->bembed,bembed ? bembed : bembedt,s);
586: }
588: t->pinterp = pinterp;
589: PetscMalloc2(s*pinterp,&t->binterpt,s*pinterp,&t->binterp);
590: PetscArraycpy(t->binterpt,binterpt,s*pinterp);
591: PetscArraycpy(t->binterp,binterp ? binterp : binterpt,s*pinterp);
592: link->next = ARKTableauList;
593: ARKTableauList = link;
594: return 0;
595: }
597: /*
598: The step completion formula is
600: x1 = x0 - h bt^T YdotI + h b^T YdotRHS
602: This function can be called before or after ts->vec_sol has been updated.
603: Suppose we have a completion formula (bt,b) and an embedded formula (bet,be) of different order.
604: We can write
606: x1e = x0 - h bet^T YdotI + h be^T YdotRHS
607: = x1 + h bt^T YdotI - h b^T YdotRHS - h bet^T YdotI + h be^T YdotRHS
608: = x1 - h (bet - bt)^T YdotI + h (be - b)^T YdotRHS
610: so we can evaluate the method with different order even after the step has been optimistically completed.
611: */
612: static PetscErrorCode TSEvaluateStep_ARKIMEX(TS ts,PetscInt order,Vec X,PetscBool *done)
613: {
614: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
615: ARKTableau tab = ark->tableau;
616: PetscScalar *w = ark->work;
617: PetscReal h;
618: PetscInt s = tab->s,j;
620: switch (ark->status) {
621: case TS_STEP_INCOMPLETE:
622: case TS_STEP_PENDING:
623: h = ts->time_step; break;
624: case TS_STEP_COMPLETE:
625: h = ts->ptime - ts->ptime_prev; break;
626: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
627: }
628: if (order == tab->order) {
629: if (ark->status == TS_STEP_INCOMPLETE) {
630: if (!ark->imex && tab->stiffly_accurate) { /* Only the stiffly accurate implicit formula is used */
631: VecCopy(ark->Y[s-1],X);
632: } else { /* Use the standard completion formula (bt,b) */
633: VecCopy(ts->vec_sol,X);
634: for (j=0; j<s; j++) w[j] = h*tab->bt[j];
635: VecMAXPY(X,s,w,ark->YdotI);
636: if (ark->imex) { /* Method is IMEX, complete the explicit formula */
637: for (j=0; j<s; j++) w[j] = h*tab->b[j];
638: VecMAXPY(X,s,w,ark->YdotRHS);
639: }
640: }
641: } else VecCopy(ts->vec_sol,X);
642: if (done) *done = PETSC_TRUE;
643: return 0;
644: } else if (order == tab->order-1) {
645: if (!tab->bembedt) goto unavailable;
646: if (ark->status == TS_STEP_INCOMPLETE) { /* Complete with the embedded method (bet,be) */
647: VecCopy(ts->vec_sol,X);
648: for (j=0; j<s; j++) w[j] = h*tab->bembedt[j];
649: VecMAXPY(X,s,w,ark->YdotI);
650: for (j=0; j<s; j++) w[j] = h*tab->bembed[j];
651: VecMAXPY(X,s,w,ark->YdotRHS);
652: } else { /* Rollback and re-complete using (bet-be,be-b) */
653: VecCopy(ts->vec_sol,X);
654: for (j=0; j<s; j++) w[j] = h*(tab->bembedt[j] - tab->bt[j]);
655: VecMAXPY(X,tab->s,w,ark->YdotI);
656: for (j=0; j<s; j++) w[j] = h*(tab->bembed[j] - tab->b[j]);
657: VecMAXPY(X,s,w,ark->YdotRHS);
658: }
659: if (done) *done = PETSC_TRUE;
660: return 0;
661: }
662: unavailable:
663: if (done) *done = PETSC_FALSE;
664: else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"ARKIMEX '%s' of order %D cannot evaluate step at order %D. Consider using -ts_adapt_type none or a different method that has an embedded estimate.",tab->name,tab->order,order);
665: return 0;
666: }
668: static PetscErrorCode TSARKIMEXTestMassIdentity(TS ts,PetscBool *id)
669: {
670: Vec Udot,Y1,Y2;
671: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
672: PetscReal norm;
674: VecDuplicate(ts->vec_sol,&Udot);
675: VecDuplicate(ts->vec_sol,&Y1);
676: VecDuplicate(ts->vec_sol,&Y2);
677: TSComputeIFunction(ts,ts->ptime,ts->vec_sol,Udot,Y1,ark->imex);
678: VecSetRandom(Udot,NULL);
679: TSComputeIFunction(ts,ts->ptime,ts->vec_sol,Udot,Y2,ark->imex);
680: VecAXPY(Y2,-1.0,Y1);
681: VecAXPY(Y2,-1.0,Udot);
682: VecNorm(Y2,NORM_2,&norm);
683: if (norm < 100.0*PETSC_MACHINE_EPSILON) {
684: *id = PETSC_TRUE;
685: } else {
686: *id = PETSC_FALSE;
687: PetscInfo((PetscObject)ts,"IFunction(Udot = random) - IFunction(Udot = 0) is not near Udot, %g, suspect mass matrix implied in IFunction() is not the identity as required\n",(double)norm);
688: }
689: VecDestroy(&Udot);
690: VecDestroy(&Y1);
691: VecDestroy(&Y2);
692: return 0;
693: }
695: static PetscErrorCode TSRollBack_ARKIMEX(TS ts)
696: {
697: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
698: ARKTableau tab = ark->tableau;
699: const PetscInt s = tab->s;
700: const PetscReal *bt = tab->bt,*b = tab->b;
701: PetscScalar *w = ark->work;
702: Vec *YdotI = ark->YdotI,*YdotRHS = ark->YdotRHS;
703: PetscInt j;
704: PetscReal h;
706: switch (ark->status) {
707: case TS_STEP_INCOMPLETE:
708: case TS_STEP_PENDING:
709: h = ts->time_step; break;
710: case TS_STEP_COMPLETE:
711: h = ts->ptime - ts->ptime_prev; break;
712: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
713: }
714: for (j=0; j<s; j++) w[j] = -h*bt[j];
715: VecMAXPY(ts->vec_sol,s,w,YdotI);
716: for (j=0; j<s; j++) w[j] = -h*b[j];
717: VecMAXPY(ts->vec_sol,s,w,YdotRHS);
718: return 0;
719: }
721: static PetscErrorCode TSStep_ARKIMEX(TS ts)
722: {
723: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
724: ARKTableau tab = ark->tableau;
725: const PetscInt s = tab->s;
726: const PetscReal *At = tab->At,*A = tab->A,*ct = tab->ct,*c = tab->c;
727: PetscScalar *w = ark->work;
728: Vec *Y = ark->Y,*YdotI = ark->YdotI,*YdotRHS = ark->YdotRHS,Ydot = ark->Ydot,Ydot0 = ark->Ydot0,Z = ark->Z;
729: PetscBool extrapolate = ark->extrapolate;
730: TSAdapt adapt;
731: SNES snes;
732: PetscInt i,j,its,lits;
733: PetscInt rejections = 0;
734: PetscBool stageok,accept = PETSC_TRUE;
735: PetscReal next_time_step = ts->time_step;
737: if (ark->extrapolate && !ark->Y_prev) {
738: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y_prev);
739: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI_prev);
740: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS_prev);
741: }
743: if (!ts->steprollback) {
744: if (ts->equation_type >= TS_EQ_IMPLICIT) { /* Save the initial slope for the next step */
745: VecCopy(YdotI[s-1],Ydot0);
746: }
747: if (ark->extrapolate && !ts->steprestart) { /* Save the Y, YdotI, YdotRHS for extrapolation initial guess */
748: for (i = 0; i<s; i++) {
749: VecCopy(Y[i],ark->Y_prev[i]);
750: VecCopy(YdotRHS[i],ark->YdotRHS_prev[i]);
751: VecCopy(YdotI[i],ark->YdotI_prev[i]);
752: }
753: }
754: }
756: if (ts->equation_type >= TS_EQ_IMPLICIT && tab->explicit_first_stage && ts->steprestart) {
757: TS ts_start;
758: if (PetscDefined(USE_DEBUG)) {
759: PetscBool id = PETSC_FALSE;
760: TSARKIMEXTestMassIdentity(ts,&id);
762: }
763: TSClone(ts,&ts_start);
764: TSSetSolution(ts_start,ts->vec_sol);
765: TSSetTime(ts_start,ts->ptime);
766: TSSetMaxSteps(ts_start,ts->steps+1);
767: TSSetMaxTime(ts_start,ts->ptime+ts->time_step);
768: TSSetExactFinalTime(ts_start,TS_EXACTFINALTIME_STEPOVER);
769: TSSetTimeStep(ts_start,ts->time_step);
770: TSSetType(ts_start,TSARKIMEX);
771: TSARKIMEXSetFullyImplicit(ts_start,PETSC_TRUE);
772: TSARKIMEXSetType(ts_start,TSARKIMEX1BEE);
774: TSRestartStep(ts_start);
775: TSSolve(ts_start,ts->vec_sol);
776: TSGetTime(ts_start,&ts->ptime);
777: TSGetTimeStep(ts_start,&ts->time_step);
779: { /* Save the initial slope for the next step */
780: TS_ARKIMEX *ark_start = (TS_ARKIMEX*)ts_start->data;
781: VecCopy(ark_start->YdotI[ark_start->tableau->s-1],Ydot0);
782: }
783: ts->steps++;
785: /* Set the correct TS in SNES */
786: /* We'll try to bypass this by changing the method on the fly */
787: {
788: TSGetSNES(ts,&snes);
789: TSSetSNES(ts,snes);
790: }
791: TSDestroy(&ts_start);
792: }
794: ark->status = TS_STEP_INCOMPLETE;
795: while (!ts->reason && ark->status != TS_STEP_COMPLETE) {
796: PetscReal t = ts->ptime;
797: PetscReal h = ts->time_step;
798: for (i=0; i<s; i++) {
799: ark->stage_time = t + h*ct[i];
800: TSPreStage(ts,ark->stage_time);
801: if (At[i*s+i] == 0) { /* This stage is explicit */
803: VecCopy(ts->vec_sol,Y[i]);
804: for (j=0; j<i; j++) w[j] = h*At[i*s+j];
805: VecMAXPY(Y[i],i,w,YdotI);
806: for (j=0; j<i; j++) w[j] = h*A[i*s+j];
807: VecMAXPY(Y[i],i,w,YdotRHS);
808: } else {
809: ark->scoeff = 1./At[i*s+i];
810: /* Ydot = shift*(Y-Z) */
811: VecCopy(ts->vec_sol,Z);
812: for (j=0; j<i; j++) w[j] = h*At[i*s+j];
813: VecMAXPY(Z,i,w,YdotI);
814: for (j=0; j<i; j++) w[j] = h*A[i*s+j];
815: VecMAXPY(Z,i,w,YdotRHS);
816: if (extrapolate && !ts->steprestart) {
817: /* Initial guess extrapolated from previous time step stage values */
818: TSExtrapolate_ARKIMEX(ts,c[i],Y[i]);
819: } else {
820: /* Initial guess taken from last stage */
821: VecCopy(i>0 ? Y[i-1] : ts->vec_sol,Y[i]);
822: }
823: TSGetSNES(ts,&snes);
824: SNESSolve(snes,NULL,Y[i]);
825: SNESGetIterationNumber(snes,&its);
826: SNESGetLinearSolveIterations(snes,&lits);
827: ts->snes_its += its; ts->ksp_its += lits;
828: TSGetAdapt(ts,&adapt);
829: TSAdaptCheckStage(adapt,ts,ark->stage_time,Y[i],&stageok);
830: if (!stageok) {
831: /* We are likely rejecting the step because of solver or function domain problems so we should not attempt to
832: * use extrapolation to initialize the solves on the next attempt. */
833: extrapolate = PETSC_FALSE;
834: goto reject_step;
835: }
836: }
837: if (ts->equation_type >= TS_EQ_IMPLICIT) {
838: if (i==0 && tab->explicit_first_stage) {
840: VecCopy(Ydot0,YdotI[0]); /* YdotI = YdotI(tn-1) */
841: } else {
842: VecAXPBYPCZ(YdotI[i],-ark->scoeff/h,ark->scoeff/h,0,Z,Y[i]); /* YdotI = shift*(X-Z) */
843: }
844: } else {
845: if (i==0 && tab->explicit_first_stage) {
846: VecZeroEntries(Ydot);
847: TSComputeIFunction(ts,t+h*ct[i],Y[i],Ydot,YdotI[i],ark->imex);/* YdotI = -G(t,Y,0) */
848: VecScale(YdotI[i],-1.0);
849: } else {
850: VecAXPBYPCZ(YdotI[i],-ark->scoeff/h,ark->scoeff/h,0,Z,Y[i]); /* YdotI = shift*(X-Z) */
851: }
852: if (ark->imex) {
853: TSComputeRHSFunction(ts,t+h*c[i],Y[i],YdotRHS[i]);
854: } else {
855: VecZeroEntries(YdotRHS[i]);
856: }
857: }
858: TSPostStage(ts,ark->stage_time,i,Y);
859: }
861: ark->status = TS_STEP_INCOMPLETE;
862: TSEvaluateStep_ARKIMEX(ts,tab->order,ts->vec_sol,NULL);
863: ark->status = TS_STEP_PENDING;
864: TSGetAdapt(ts,&adapt);
865: TSAdaptCandidatesClear(adapt);
866: TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,(PetscReal)tab->s,PETSC_TRUE);
867: TSAdaptChoose(adapt,ts,ts->time_step,NULL,&next_time_step,&accept);
868: ark->status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
869: if (!accept) { /* Roll back the current step */
870: TSRollBack_ARKIMEX(ts);
871: ts->time_step = next_time_step;
872: goto reject_step;
873: }
875: ts->ptime += ts->time_step;
876: ts->time_step = next_time_step;
877: break;
879: reject_step:
880: ts->reject++; accept = PETSC_FALSE;
881: if (!ts->reason && ++rejections > ts->max_reject && ts->max_reject >= 0) {
882: ts->reason = TS_DIVERGED_STEP_REJECTED;
883: PetscInfo(ts,"Step=%D, step rejections %D greater than current TS allowed, stopping solve\n",ts->steps,rejections);
884: }
885: }
886: return 0;
887: }
889: static PetscErrorCode TSInterpolate_ARKIMEX(TS ts,PetscReal itime,Vec X)
890: {
891: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
892: PetscInt s = ark->tableau->s,pinterp = ark->tableau->pinterp,i,j;
893: PetscReal h;
894: PetscReal tt,t;
895: PetscScalar *bt,*b;
896: const PetscReal *Bt = ark->tableau->binterpt,*B = ark->tableau->binterp;
899: switch (ark->status) {
900: case TS_STEP_INCOMPLETE:
901: case TS_STEP_PENDING:
902: h = ts->time_step;
903: t = (itime - ts->ptime)/h;
904: break;
905: case TS_STEP_COMPLETE:
906: h = ts->ptime - ts->ptime_prev;
907: t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
908: break;
909: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
910: }
911: PetscMalloc2(s,&bt,s,&b);
912: for (i=0; i<s; i++) bt[i] = b[i] = 0;
913: for (j=0,tt=t; j<pinterp; j++,tt*=t) {
914: for (i=0; i<s; i++) {
915: bt[i] += h * Bt[i*pinterp+j] * tt;
916: b[i] += h * B[i*pinterp+j] * tt;
917: }
918: }
919: VecCopy(ark->Y[0],X);
920: VecMAXPY(X,s,bt,ark->YdotI);
921: VecMAXPY(X,s,b,ark->YdotRHS);
922: PetscFree2(bt,b);
923: return 0;
924: }
926: static PetscErrorCode TSExtrapolate_ARKIMEX(TS ts,PetscReal c,Vec X)
927: {
928: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
929: PetscInt s = ark->tableau->s,pinterp = ark->tableau->pinterp,i,j;
930: PetscReal h,h_prev,t,tt;
931: PetscScalar *bt,*b;
932: const PetscReal *Bt = ark->tableau->binterpt,*B = ark->tableau->binterp;
935: PetscCalloc2(s,&bt,s,&b);
936: h = ts->time_step;
937: h_prev = ts->ptime - ts->ptime_prev;
938: t = 1 + h/h_prev*c;
939: for (j=0,tt=t; j<pinterp; j++,tt*=t) {
940: for (i=0; i<s; i++) {
941: bt[i] += h * Bt[i*pinterp+j] * tt;
942: b[i] += h * B[i*pinterp+j] * tt;
943: }
944: }
946: VecCopy(ark->Y_prev[0],X);
947: VecMAXPY(X,s,bt,ark->YdotI_prev);
948: VecMAXPY(X,s,b,ark->YdotRHS_prev);
949: PetscFree2(bt,b);
950: return 0;
951: }
953: /*------------------------------------------------------------*/
955: static PetscErrorCode TSARKIMEXTableauReset(TS ts)
956: {
957: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
958: ARKTableau tab = ark->tableau;
960: if (!tab) return 0;
961: PetscFree(ark->work);
962: VecDestroyVecs(tab->s,&ark->Y);
963: VecDestroyVecs(tab->s,&ark->YdotI);
964: VecDestroyVecs(tab->s,&ark->YdotRHS);
965: VecDestroyVecs(tab->s,&ark->Y_prev);
966: VecDestroyVecs(tab->s,&ark->YdotI_prev);
967: VecDestroyVecs(tab->s,&ark->YdotRHS_prev);
968: return 0;
969: }
971: static PetscErrorCode TSReset_ARKIMEX(TS ts)
972: {
973: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
975: TSARKIMEXTableauReset(ts);
976: VecDestroy(&ark->Ydot);
977: VecDestroy(&ark->Ydot0);
978: VecDestroy(&ark->Z);
979: return 0;
980: }
982: static PetscErrorCode TSARKIMEXGetVecs(TS ts,DM dm,Vec *Z,Vec *Ydot)
983: {
984: TS_ARKIMEX *ax = (TS_ARKIMEX*)ts->data;
986: if (Z) {
987: if (dm && dm != ts->dm) {
988: DMGetNamedGlobalVector(dm,"TSARKIMEX_Z",Z);
989: } else *Z = ax->Z;
990: }
991: if (Ydot) {
992: if (dm && dm != ts->dm) {
993: DMGetNamedGlobalVector(dm,"TSARKIMEX_Ydot",Ydot);
994: } else *Ydot = ax->Ydot;
995: }
996: return 0;
997: }
999: static PetscErrorCode TSARKIMEXRestoreVecs(TS ts,DM dm,Vec *Z,Vec *Ydot)
1000: {
1001: if (Z) {
1002: if (dm && dm != ts->dm) {
1003: DMRestoreNamedGlobalVector(dm,"TSARKIMEX_Z",Z);
1004: }
1005: }
1006: if (Ydot) {
1007: if (dm && dm != ts->dm) {
1008: DMRestoreNamedGlobalVector(dm,"TSARKIMEX_Ydot",Ydot);
1009: }
1010: }
1011: return 0;
1012: }
1014: /*
1015: This defines the nonlinear equation that is to be solved with SNES
1016: G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
1017: */
1018: static PetscErrorCode SNESTSFormFunction_ARKIMEX(SNES snes,Vec X,Vec F,TS ts)
1019: {
1020: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1021: DM dm,dmsave;
1022: Vec Z,Ydot;
1023: PetscReal shift = ark->scoeff / ts->time_step;
1025: SNESGetDM(snes,&dm);
1026: TSARKIMEXGetVecs(ts,dm,&Z,&Ydot);
1027: VecAXPBYPCZ(Ydot,-shift,shift,0,Z,X); /* Ydot = shift*(X-Z) */
1028: dmsave = ts->dm;
1029: ts->dm = dm;
1031: TSComputeIFunction(ts,ark->stage_time,X,Ydot,F,ark->imex);
1033: ts->dm = dmsave;
1034: TSARKIMEXRestoreVecs(ts,dm,&Z,&Ydot);
1035: return 0;
1036: }
1038: static PetscErrorCode SNESTSFormJacobian_ARKIMEX(SNES snes,Vec X,Mat A,Mat B,TS ts)
1039: {
1040: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1041: DM dm,dmsave;
1042: Vec Ydot;
1043: PetscReal shift = ark->scoeff / ts->time_step;
1045: SNESGetDM(snes,&dm);
1046: TSARKIMEXGetVecs(ts,dm,NULL,&Ydot);
1047: /* ark->Ydot has already been computed in SNESTSFormFunction_ARKIMEX (SNES guarantees this) */
1048: dmsave = ts->dm;
1049: ts->dm = dm;
1051: TSComputeIJacobian(ts,ark->stage_time,X,Ydot,shift,A,B,ark->imex);
1053: ts->dm = dmsave;
1054: TSARKIMEXRestoreVecs(ts,dm,NULL,&Ydot);
1055: return 0;
1056: }
1058: static PetscErrorCode DMCoarsenHook_TSARKIMEX(DM fine,DM coarse,void *ctx)
1059: {
1060: return 0;
1061: }
1063: static PetscErrorCode DMRestrictHook_TSARKIMEX(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
1064: {
1065: TS ts = (TS)ctx;
1066: Vec Z,Z_c;
1068: TSARKIMEXGetVecs(ts,fine,&Z,NULL);
1069: TSARKIMEXGetVecs(ts,coarse,&Z_c,NULL);
1070: MatRestrict(restrct,Z,Z_c);
1071: VecPointwiseMult(Z_c,rscale,Z_c);
1072: TSARKIMEXRestoreVecs(ts,fine,&Z,NULL);
1073: TSARKIMEXRestoreVecs(ts,coarse,&Z_c,NULL);
1074: return 0;
1075: }
1077: static PetscErrorCode DMSubDomainHook_TSARKIMEX(DM dm,DM subdm,void *ctx)
1078: {
1079: return 0;
1080: }
1082: static PetscErrorCode DMSubDomainRestrictHook_TSARKIMEX(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
1083: {
1084: TS ts = (TS)ctx;
1085: Vec Z,Z_c;
1087: TSARKIMEXGetVecs(ts,dm,&Z,NULL);
1088: TSARKIMEXGetVecs(ts,subdm,&Z_c,NULL);
1090: VecScatterBegin(gscat,Z,Z_c,INSERT_VALUES,SCATTER_FORWARD);
1091: VecScatterEnd(gscat,Z,Z_c,INSERT_VALUES,SCATTER_FORWARD);
1093: TSARKIMEXRestoreVecs(ts,dm,&Z,NULL);
1094: TSARKIMEXRestoreVecs(ts,subdm,&Z_c,NULL);
1095: return 0;
1096: }
1098: static PetscErrorCode TSARKIMEXTableauSetUp(TS ts)
1099: {
1100: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1101: ARKTableau tab = ark->tableau;
1103: PetscMalloc1(tab->s,&ark->work);
1104: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y);
1105: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI);
1106: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS);
1107: if (ark->extrapolate) {
1108: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y_prev);
1109: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI_prev);
1110: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS_prev);
1111: }
1112: return 0;
1113: }
1115: static PetscErrorCode TSSetUp_ARKIMEX(TS ts)
1116: {
1117: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1118: DM dm;
1119: SNES snes;
1121: TSARKIMEXTableauSetUp(ts);
1122: VecDuplicate(ts->vec_sol,&ark->Ydot);
1123: VecDuplicate(ts->vec_sol,&ark->Ydot0);
1124: VecDuplicate(ts->vec_sol,&ark->Z);
1125: TSGetDM(ts,&dm);
1126: DMCoarsenHookAdd(dm,DMCoarsenHook_TSARKIMEX,DMRestrictHook_TSARKIMEX,ts);
1127: DMSubDomainHookAdd(dm,DMSubDomainHook_TSARKIMEX,DMSubDomainRestrictHook_TSARKIMEX,ts);
1128: TSGetSNES(ts,&snes);
1129: return 0;
1130: }
1131: /*------------------------------------------------------------*/
1133: static PetscErrorCode TSSetFromOptions_ARKIMEX(PetscOptionItems *PetscOptionsObject,TS ts)
1134: {
1135: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1137: PetscOptionsHead(PetscOptionsObject,"ARKIMEX ODE solver options");
1138: {
1139: ARKTableauLink link;
1140: PetscInt count,choice;
1141: PetscBool flg;
1142: const char **namelist;
1143: for (link=ARKTableauList,count=0; link; link=link->next,count++) ;
1144: PetscMalloc1(count,(char***)&namelist);
1145: for (link=ARKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
1146: PetscOptionsEList("-ts_arkimex_type","Family of ARK IMEX method","TSARKIMEXSetType",(const char*const*)namelist,count,ark->tableau->name,&choice,&flg);
1147: if (flg) TSARKIMEXSetType(ts,namelist[choice]);
1148: PetscFree(namelist);
1150: flg = (PetscBool) !ark->imex;
1151: PetscOptionsBool("-ts_arkimex_fully_implicit","Solve the problem fully implicitly","TSARKIMEXSetFullyImplicit",flg,&flg,NULL);
1152: ark->imex = (PetscBool) !flg;
1153: PetscOptionsBool("-ts_arkimex_initial_guess_extrapolate","Extrapolate the initial guess for the stage solution from stage values of the previous time step","",ark->extrapolate,&ark->extrapolate,NULL);
1154: }
1155: PetscOptionsTail();
1156: return 0;
1157: }
1159: static PetscErrorCode TSView_ARKIMEX(TS ts,PetscViewer viewer)
1160: {
1161: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1162: PetscBool iascii;
1164: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1165: if (iascii) {
1166: ARKTableau tab = ark->tableau;
1167: TSARKIMEXType arktype;
1168: char buf[512];
1169: PetscBool flg;
1171: TSARKIMEXGetType(ts,&arktype);
1172: TSARKIMEXGetFullyImplicit(ts,&flg);
1173: PetscViewerASCIIPrintf(viewer," ARK IMEX %s\n",arktype);
1174: PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->ct);
1175: PetscViewerASCIIPrintf(viewer," Stiff abscissa ct = %s\n",buf);
1176: PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->c);
1177: PetscViewerASCIIPrintf(viewer,"Fully implicit: %s\n",flg ? "yes" : "no");
1178: PetscViewerASCIIPrintf(viewer,"Stiffly accurate: %s\n",tab->stiffly_accurate ? "yes" : "no");
1179: PetscViewerASCIIPrintf(viewer,"Explicit first stage: %s\n",tab->explicit_first_stage ? "yes" : "no");
1180: PetscViewerASCIIPrintf(viewer,"FSAL property: %s\n",tab->FSAL_implicit ? "yes" : "no");
1181: PetscViewerASCIIPrintf(viewer," Nonstiff abscissa c = %s\n",buf);
1182: }
1183: return 0;
1184: }
1186: static PetscErrorCode TSLoad_ARKIMEX(TS ts,PetscViewer viewer)
1187: {
1188: SNES snes;
1189: TSAdapt adapt;
1191: TSGetAdapt(ts,&adapt);
1192: TSAdaptLoad(adapt,viewer);
1193: TSGetSNES(ts,&snes);
1194: SNESLoad(snes,viewer);
1195: /* function and Jacobian context for SNES when used with TS is always ts object */
1196: SNESSetFunction(snes,NULL,NULL,ts);
1197: SNESSetJacobian(snes,NULL,NULL,NULL,ts);
1198: return 0;
1199: }
1201: /*@C
1202: TSARKIMEXSetType - Set the type of ARK IMEX scheme
1204: Logically collective
1206: Input Parameters:
1207: + ts - timestepping context
1208: - arktype - type of ARK-IMEX scheme
1210: Options Database:
1211: . -ts_arkimex_type <1bee,a2,l2,ars122,2c,2d,2e,prssp2,3,bpr3,ars443,4,5> - set ARK IMEX scheme type
1213: Level: intermediate
1215: .seealso: TSARKIMEXGetType(), TSARKIMEX, TSARKIMEXType, TSARKIMEX1BEE, TSARKIMEXA2, TSARKIMEXL2, TSARKIMEXARS122, TSARKIMEX2C, TSARKIMEX2D, TSARKIMEX2E, TSARKIMEXPRSSP2,
1216: TSARKIMEX3, TSARKIMEXBPR3, TSARKIMEXARS443, TSARKIMEX4, TSARKIMEX5
1217: @*/
1218: PetscErrorCode TSARKIMEXSetType(TS ts,TSARKIMEXType arktype)
1219: {
1222: PetscTryMethod(ts,"TSARKIMEXSetType_C",(TS,TSARKIMEXType),(ts,arktype));
1223: return 0;
1224: }
1226: /*@C
1227: TSARKIMEXGetType - Get the type of ARK IMEX scheme
1229: Logically collective
1231: Input Parameter:
1232: . ts - timestepping context
1234: Output Parameter:
1235: . arktype - type of ARK-IMEX scheme
1237: Level: intermediate
1239: .seealso: TSARKIMEXGetType()
1240: @*/
1241: PetscErrorCode TSARKIMEXGetType(TS ts,TSARKIMEXType *arktype)
1242: {
1244: PetscUseMethod(ts,"TSARKIMEXGetType_C",(TS,TSARKIMEXType*),(ts,arktype));
1245: return 0;
1246: }
1248: /*@
1249: TSARKIMEXSetFullyImplicit - Solve both parts of the equation implicitly
1251: Logically collective
1253: Input Parameters:
1254: + ts - timestepping context
1255: - flg - PETSC_TRUE for fully implicit
1257: Level: intermediate
1259: .seealso: TSARKIMEXGetType(), TSARKIMEXGetFullyImplicit()
1260: @*/
1261: PetscErrorCode TSARKIMEXSetFullyImplicit(TS ts,PetscBool flg)
1262: {
1265: PetscTryMethod(ts,"TSARKIMEXSetFullyImplicit_C",(TS,PetscBool),(ts,flg));
1266: return 0;
1267: }
1269: /*@
1270: TSARKIMEXGetFullyImplicit - Inquires if both parts of the equation are solved implicitly
1272: Logically collective
1274: Input Parameter:
1275: . ts - timestepping context
1277: Output Parameter:
1278: . flg - PETSC_TRUE for fully implicit
1280: Level: intermediate
1282: .seealso: TSARKIMEXGetType(), TSARKIMEXSetFullyImplicit()
1283: @*/
1284: PetscErrorCode TSARKIMEXGetFullyImplicit(TS ts,PetscBool *flg)
1285: {
1288: PetscUseMethod(ts,"TSARKIMEXGetFullyImplicit_C",(TS,PetscBool*),(ts,flg));
1289: return 0;
1290: }
1292: static PetscErrorCode TSARKIMEXGetType_ARKIMEX(TS ts,TSARKIMEXType *arktype)
1293: {
1294: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1296: *arktype = ark->tableau->name;
1297: return 0;
1298: }
1299: static PetscErrorCode TSARKIMEXSetType_ARKIMEX(TS ts,TSARKIMEXType arktype)
1300: {
1301: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1302: PetscBool match;
1303: ARKTableauLink link;
1305: if (ark->tableau) {
1306: PetscStrcmp(ark->tableau->name,arktype,&match);
1307: if (match) return 0;
1308: }
1309: for (link = ARKTableauList; link; link=link->next) {
1310: PetscStrcmp(link->tab.name,arktype,&match);
1311: if (match) {
1312: if (ts->setupcalled) TSARKIMEXTableauReset(ts);
1313: ark->tableau = &link->tab;
1314: if (ts->setupcalled) TSARKIMEXTableauSetUp(ts);
1315: ts->default_adapt_type = ark->tableau->bembed ? TSADAPTBASIC : TSADAPTNONE;
1316: return 0;
1317: }
1318: }
1319: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",arktype);
1320: }
1322: static PetscErrorCode TSARKIMEXSetFullyImplicit_ARKIMEX(TS ts,PetscBool flg)
1323: {
1324: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1326: ark->imex = (PetscBool)!flg;
1327: return 0;
1328: }
1330: static PetscErrorCode TSARKIMEXGetFullyImplicit_ARKIMEX(TS ts,PetscBool *flg)
1331: {
1332: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1334: *flg = (PetscBool)!ark->imex;
1335: return 0;
1336: }
1338: static PetscErrorCode TSDestroy_ARKIMEX(TS ts)
1339: {
1340: TSReset_ARKIMEX(ts);
1341: if (ts->dm) {
1342: DMCoarsenHookRemove(ts->dm,DMCoarsenHook_TSARKIMEX,DMRestrictHook_TSARKIMEX,ts);
1343: DMSubDomainHookRemove(ts->dm,DMSubDomainHook_TSARKIMEX,DMSubDomainRestrictHook_TSARKIMEX,ts);
1344: }
1345: PetscFree(ts->data);
1346: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetType_C",NULL);
1347: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetType_C",NULL);
1348: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",NULL);
1349: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",NULL);
1350: return 0;
1351: }
1353: /* ------------------------------------------------------------ */
1354: /*MC
1355: TSARKIMEX - ODE and DAE solver using additive Runge-Kutta IMEX schemes
1357: These methods are intended for problems with well-separated time scales, especially when a slow scale is strongly
1358: nonlinear such that it is expensive to solve with a fully implicit method. The user should provide the stiff part
1359: of the equation using TSSetIFunction() and the non-stiff part with TSSetRHSFunction().
1361: Notes:
1362: The default is TSARKIMEX3, it can be changed with TSARKIMEXSetType() or -ts_arkimex_type
1364: If the equation is implicit or a DAE, then TSSetEquationType() needs to be set accordingly. Refer to the manual for further information.
1366: Methods with an explicit stage can only be used with ODE in which the stiff part G(t,X,Xdot) has the form Xdot + Ghat(t,X).
1368: Consider trying TSROSW if the stiff part is linear or weakly nonlinear.
1370: Level: beginner
1372: .seealso: TSCreate(), TS, TSSetType(), TSARKIMEXSetType(), TSARKIMEXGetType(), TSARKIMEXSetFullyImplicit(), TSARKIMEXGetFullyImplicit(),
1373: TSARKIMEX1BEE, TSARKIMEX2C, TSARKIMEX2D, TSARKIMEX2E, TSARKIMEX3, TSARKIMEXL2, TSARKIMEXA2, TSARKIMEXARS122,
1374: TSARKIMEX4, TSARKIMEX5, TSARKIMEXPRSSP2, TSARKIMEXARS443, TSARKIMEXBPR3, TSARKIMEXType, TSARKIMEXRegister()
1376: M*/
1377: PETSC_EXTERN PetscErrorCode TSCreate_ARKIMEX(TS ts)
1378: {
1379: TS_ARKIMEX *th;
1381: TSARKIMEXInitializePackage();
1383: ts->ops->reset = TSReset_ARKIMEX;
1384: ts->ops->destroy = TSDestroy_ARKIMEX;
1385: ts->ops->view = TSView_ARKIMEX;
1386: ts->ops->load = TSLoad_ARKIMEX;
1387: ts->ops->setup = TSSetUp_ARKIMEX;
1388: ts->ops->step = TSStep_ARKIMEX;
1389: ts->ops->interpolate = TSInterpolate_ARKIMEX;
1390: ts->ops->evaluatestep = TSEvaluateStep_ARKIMEX;
1391: ts->ops->rollback = TSRollBack_ARKIMEX;
1392: ts->ops->setfromoptions = TSSetFromOptions_ARKIMEX;
1393: ts->ops->snesfunction = SNESTSFormFunction_ARKIMEX;
1394: ts->ops->snesjacobian = SNESTSFormJacobian_ARKIMEX;
1396: ts->usessnes = PETSC_TRUE;
1398: PetscNewLog(ts,&th);
1399: ts->data = (void*)th;
1400: th->imex = PETSC_TRUE;
1402: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetType_C",TSARKIMEXGetType_ARKIMEX);
1403: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetType_C",TSARKIMEXSetType_ARKIMEX);
1404: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",TSARKIMEXSetFullyImplicit_ARKIMEX);
1405: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetFullyImplicit_C",TSARKIMEXGetFullyImplicit_ARKIMEX);
1407: TSARKIMEXSetType(ts,TSARKIMEXDefault);
1408: return 0;
1409: }