Actual source code: arkimex.c
petsc-3.10.5 2019-03-28
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
68: References:
69: . 1. - 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
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
95: References:
96: . 1. - 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
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
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
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
145:
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: . 1. - 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 http://arxiv.org/abs/1110.4375
160: Options Database:
161: . -ts_arkimex_type 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
175: References:
176: . 1. - 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
190: References:
191: + 1. - U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).
192: - 2. - This method is referred to as ARS(4,4,3) in http://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
206: References:
207: . This method is referred to as ARK3 in http://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
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
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: .keywords: TS, TSARKIMEX, register, all
253: .seealso: TSARKIMEXRegisterDestroy()
254: @*/
255: PetscErrorCode TSARKIMEXRegisterAll(void)
256: {
260: if (TSARKIMEXRegisterAllCalled) return(0);
261: TSARKIMEXRegisterAllCalled = PETSC_TRUE;
263: {
264: const PetscReal
265: A[3][3] = {{0.0,0.0,0.0},
266: {0.0,0.0,0.0},
267: {0.0,0.5,0.0}},
268: At[3][3] = {{1.0,0.0,0.0},
269: {0.0,0.5,0.0},
270: {0.0,0.5,0.5}},
271: b[3] = {0.0,0.5,0.5},
272: bembedt[3] = {1.0,0.0,0.0};
273: TSARKIMEXRegister(TSARKIMEX1BEE,2,3,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
274: }
275: {
276: const PetscReal
277: A[2][2] = {{0.0,0.0},
278: {0.5,0.0}},
279: At[2][2] = {{0.0,0.0},
280: {0.0,0.5}},
281: b[2] = {0.0,1.0},
282: bembedt[2] = {0.5,0.5};
283: /* 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*/
284: TSARKIMEXRegister(TSARKIMEXARS122,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
285: }
286: {
287: const PetscReal
288: A[2][2] = {{0.0,0.0},
289: {1.0,0.0}},
290: At[2][2] = {{0.0,0.0},
291: {0.5,0.5}},
292: b[2] = {0.5,0.5},
293: bembedt[2] = {0.0,1.0};
294: /* 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*/
295: TSARKIMEXRegister(TSARKIMEXA2,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
296: }
297: {
298: /* 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 */
299: const PetscReal
300: A[2][2] = {{0.0,0.0},
301: {1.0,0.0}},
302: At[2][2] = {{0.2928932188134524755992,0.0},
303: {1.0-2.0*0.2928932188134524755992,0.2928932188134524755992}},
304: b[2] = {0.5,0.5},
305: bembedt[2] = {0.0,1.0},
306: binterpt[2][2] = {{ (0.2928932188134524755992-1.0)/(2.0*0.2928932188134524755992-1.0),-1/(2.0*(1.0-2.0*0.2928932188134524755992))},
307: {1-(0.2928932188134524755992-1.0)/(2.0*0.2928932188134524755992-1.0),-1/(2.0*(1.0-2.0*0.2928932188134524755992))}},
308: binterp[2][2] = {{1.0,-0.5},{0.0,0.5}};
309: TSARKIMEXRegister(TSARKIMEXL2,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,2,binterpt[0],binterp[0]);
310: }
311: {
312: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
313: const PetscReal
314: A[3][3] = {{0,0,0},
315: {2-1.414213562373095048802,0,0},
316: {0.5,0.5,0}},
317: At[3][3] = {{0,0,0},
318: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
319: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
320: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
321: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
322: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
323: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
324: TSARKIMEXRegister(TSARKIMEX2C,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
325: }
326: {
327: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
328: const PetscReal
329: A[3][3] = {{0,0,0},
330: {2-1.414213562373095048802,0,0},
331: {0.75,0.25,0}},
332: At[3][3] = {{0,0,0},
333: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
334: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
335: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
336: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
337: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
338: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
339: TSARKIMEXRegister(TSARKIMEX2D,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
340: }
341: { /* Optimal for linear implicit part */
342: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
343: const PetscReal
344: A[3][3] = {{0,0,0},
345: {2-1.414213562373095048802,0,0},
346: {(3-2*1.414213562373095048802)/6,(3+2*1.414213562373095048802)/6,0}},
347: At[3][3] = {{0,0,0},
348: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
349: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
350: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
351: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
352: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
353: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
354: TSARKIMEXRegister(TSARKIMEX2E,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
355: }
356: { /* Optimal for linear implicit part */
357: const PetscReal
358: A[3][3] = {{0,0,0},
359: {0.5,0,0},
360: {0.5,0.5,0}},
361: At[3][3] = {{0.25,0,0},
362: {0,0.25,0},
363: {1./3,1./3,1./3}};
364: TSARKIMEXRegister(TSARKIMEXPRSSP2,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,NULL,NULL,0,NULL,NULL);
365: }
366: {
367: const PetscReal
368: A[4][4] = {{0,0,0,0},
369: {1767732205903./2027836641118.,0,0,0},
370: {5535828885825./10492691773637.,788022342437./10882634858940.,0,0},
371: {6485989280629./16251701735622.,-4246266847089./9704473918619.,10755448449292./10357097424841.,0}},
372: At[4][4] = {{0,0,0,0},
373: {1767732205903./4055673282236.,1767732205903./4055673282236.,0,0},
374: {2746238789719./10658868560708.,-640167445237./6845629431997.,1767732205903./4055673282236.,0},
375: {1471266399579./7840856788654.,-4482444167858./7529755066697.,11266239266428./11593286722821.,1767732205903./4055673282236.}},
376: bembedt[4] = {2756255671327./12835298489170.,-10771552573575./22201958757719.,9247589265047./10645013368117.,2193209047091./5459859503100.},
377: binterpt[4][2] = {{4655552711362./22874653954995., -215264564351./13552729205753.},
378: {-18682724506714./9892148508045.,17870216137069./13817060693119.},
379: {34259539580243./13192909600954.,-28141676662227./17317692491321.},
380: {584795268549./6622622206610., 2508943948391./7218656332882.}};
381: TSARKIMEXRegister(TSARKIMEX3,3,4,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
382: }
383: {
384: const PetscReal
385: A[5][5] = {{0,0,0,0,0},
386: {1./2,0,0,0,0},
387: {11./18,1./18,0,0,0},
388: {5./6,-5./6,.5,0,0},
389: {1./4,7./4,3./4,-7./4,0}},
390: At[5][5] = {{0,0,0,0,0},
391: {0,1./2,0,0,0},
392: {0,1./6,1./2,0,0},
393: {0,-1./2,1./2,1./2,0},
394: {0,3./2,-3./2,1./2,1./2}},
395: *bembedt = NULL;
396: TSARKIMEXRegister(TSARKIMEXARS443,3,5,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,0,NULL,NULL);
397: }
398: {
399: const PetscReal
400: A[5][5] = {{0,0,0,0,0},
401: {1,0,0,0,0},
402: {4./9,2./9,0,0,0},
403: {1./4,0,3./4,0,0},
404: {1./4,0,3./5,0,0}},
405: At[5][5] = {{0,0,0,0,0},
406: {.5,.5,0,0,0},
407: {5./18,-1./9,.5,0,0},
408: {.5,0,0,.5,0},
409: {.25,0,.75,-.5,.5}},
410: *bembedt = NULL;
411: TSARKIMEXRegister(TSARKIMEXBPR3,3,5,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,0,NULL,NULL);
412: }
413: {
414: const PetscReal
415: A[6][6] = {{0,0,0,0,0,0},
416: {1./2,0,0,0,0,0},
417: {13861./62500.,6889./62500.,0,0,0,0},
418: {-116923316275./2393684061468.,-2731218467317./15368042101831.,9408046702089./11113171139209.,0,0,0},
419: {-451086348788./2902428689909.,-2682348792572./7519795681897.,12662868775082./11960479115383.,3355817975965./11060851509271.,0,0},
420: {647845179188./3216320057751.,73281519250./8382639484533.,552539513391./3454668386233.,3354512671639./8306763924573.,4040./17871.,0}},
421: At[6][6] = {{0,0,0,0,0,0},
422: {1./4,1./4,0,0,0,0},
423: {8611./62500.,-1743./31250.,1./4,0,0,0},
424: {5012029./34652500.,-654441./2922500.,174375./388108.,1./4,0,0},
425: {15267082809./155376265600.,-71443401./120774400.,730878875./902184768.,2285395./8070912.,1./4,0},
426: {82889./524892.,0,15625./83664.,69875./102672.,-2260./8211,1./4}},
427: bembedt[6] = {4586570599./29645900160.,0,178811875./945068544.,814220225./1159782912.,-3700637./11593932.,61727./225920.},
428: binterpt[6][3] = {{6943876665148./7220017795957.,-54480133./30881146.,6818779379841./7100303317025.},
429: {0,0,0},
430: {7640104374378./9702883013639.,-11436875./14766696.,2173542590792./12501825683035.},
431: {-20649996744609./7521556579894.,174696575./18121608.,-31592104683404./5083833661969.},
432: {8854892464581./2390941311638.,-12120380./966161.,61146701046299./7138195549469.},
433: {-11397109935349./6675773540249.,3843./706.,-17219254887155./4939391667607.}};
434: TSARKIMEXRegister(TSARKIMEX4,4,6,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,3,binterpt[0],NULL);
435: }
436: {
437: const PetscReal
438: A[8][8] = {{0,0,0,0,0,0,0,0},
439: {41./100,0,0,0,0,0,0,0},
440: {367902744464./2072280473677.,677623207551./8224143866563.,0,0,0,0,0,0},
441: {1268023523408./10340822734521.,0,1029933939417./13636558850479.,0,0,0,0,0},
442: {14463281900351./6315353703477.,0,66114435211212./5879490589093.,-54053170152839./4284798021562.,0,0,0,0},
443: {14090043504691./34967701212078.,0,15191511035443./11219624916014.,-18461159152457./12425892160975.,-281667163811./9011619295870.,0,0,0},
444: {19230459214898./13134317526959.,0,21275331358303./2942455364971.,-38145345988419./4862620318723.,-1./8,-1./8,0,0},
445: {-19977161125411./11928030595625.,0,-40795976796054./6384907823539.,177454434618887./12078138498510.,782672205425./8267701900261.,-69563011059811./9646580694205.,7356628210526./4942186776405.,0}},
446: At[8][8] = {{0,0,0,0,0,0,0,0},
447: {41./200.,41./200.,0,0,0,0,0,0},
448: {41./400.,-567603406766./11931857230679.,41./200.,0,0,0,0,0},
449: {683785636431./9252920307686.,0,-110385047103./1367015193373.,41./200.,0,0,0,0},
450: {3016520224154./10081342136671.,0,30586259806659./12414158314087.,-22760509404356./11113319521817.,41./200.,0,0,0},
451: {218866479029./1489978393911.,0,638256894668./5436446318841.,-1179710474555./5321154724896.,-60928119172./8023461067671.,41./200.,0,0},
452: {1020004230633./5715676835656.,0,25762820946817./25263940353407.,-2161375909145./9755907335909.,-211217309593./5846859502534.,-4269925059573./7827059040749.,41./200,0},
453: {-872700587467./9133579230613.,0,0,22348218063261./9555858737531.,-1143369518992./8141816002931.,-39379526789629./19018526304540.,32727382324388./42900044865799.,41./200.}},
454: bembedt[8] = {-975461918565./9796059967033.,0,0,78070527104295./32432590147079.,-548382580838./3424219808633.,-33438840321285./15594753105479.,3629800801594./4656183773603.,4035322873751./18575991585200.},
455: binterpt[8][3] = {{-17674230611817./10670229744614., 43486358583215./12773830924787., -9257016797708./5021505065439.},
456: {0, 0, 0 },
457: {0, 0, 0 },
458: {65168852399939./7868540260826., -91478233927265./11067650958493., 26096422576131./11239449250142.},
459: {15494834004392./5936557850923., -79368583304911./10890268929626., 92396832856987./20362823103730.},
460: {-99329723586156./26959484932159., -12239297817655./9152339842473., 30029262896817./10175596800299.},
461: {-19024464361622./5461577185407., 115839755401235./10719374521269., -26136350496073./3983972220547.},
462: {-6511271360970./6095937251113., 5843115559534./2180450260947., -5289405421727./3760307252460. }};
463: TSARKIMEXRegister(TSARKIMEX5,5,8,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,3,binterpt[0],NULL);
464: }
465: return(0);
466: }
468: /*@C
469: TSARKIMEXRegisterDestroy - Frees the list of schemes that were registered by TSARKIMEXRegister().
471: Not Collective
473: Level: advanced
475: .keywords: TSARKIMEX, register, destroy
476: .seealso: TSARKIMEXRegister(), TSARKIMEXRegisterAll()
477: @*/
478: PetscErrorCode TSARKIMEXRegisterDestroy(void)
479: {
481: ARKTableauLink link;
484: while ((link = ARKTableauList)) {
485: ARKTableau t = &link->tab;
486: ARKTableauList = link->next;
487: PetscFree6(t->At,t->bt,t->ct,t->A,t->b,t->c);
488: PetscFree2(t->bembedt,t->bembed);
489: PetscFree2(t->binterpt,t->binterp);
490: PetscFree(t->name);
491: PetscFree(link);
492: }
493: TSARKIMEXRegisterAllCalled = PETSC_FALSE;
494: return(0);
495: }
497: /*@C
498: TSARKIMEXInitializePackage - This function initializes everything in the TSARKIMEX package. It is called
499: from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_ARKIMEX()
500: when using static libraries.
502: Level: developer
504: .keywords: TS, TSARKIMEX, initialize, package
505: .seealso: PetscInitialize()
506: @*/
507: PetscErrorCode TSARKIMEXInitializePackage(void)
508: {
512: if (TSARKIMEXPackageInitialized) return(0);
513: TSARKIMEXPackageInitialized = PETSC_TRUE;
514: TSARKIMEXRegisterAll();
515: PetscRegisterFinalize(TSARKIMEXFinalizePackage);
516: return(0);
517: }
519: /*@C
520: TSARKIMEXFinalizePackage - This function destroys everything in the TSARKIMEX package. It is
521: called from PetscFinalize().
523: Level: developer
525: .keywords: Petsc, destroy, package
526: .seealso: PetscFinalize()
527: @*/
528: PetscErrorCode TSARKIMEXFinalizePackage(void)
529: {
533: TSARKIMEXPackageInitialized = PETSC_FALSE;
534: TSARKIMEXRegisterDestroy();
535: return(0);
536: }
538: /*@C
539: TSARKIMEXRegister - register an ARK IMEX scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation
541: Not Collective, but the same schemes should be registered on all processes on which they will be used
543: Input Parameters:
544: + name - identifier for method
545: . order - approximation order of method
546: . s - number of stages, this is the dimension of the matrices below
547: . At - Butcher table of stage coefficients for stiff part (dimension s*s, row-major)
548: . bt - Butcher table for completing the stiff part of the step (dimension s; NULL to use the last row of At)
549: . ct - Abscissa of each stiff stage (dimension s, NULL to use row sums of At)
550: . A - Non-stiff stage coefficients (dimension s*s, row-major)
551: . b - Non-stiff step completion table (dimension s; NULL to use last row of At)
552: . c - Non-stiff abscissa (dimension s; NULL to use row sums of A)
553: . bembedt - Stiff part of completion table for embedded method (dimension s; NULL if not available)
554: . bembed - Non-stiff part of completion table for embedded method (dimension s; NULL to use bembedt if provided)
555: . pinterp - Order of the interpolation scheme, equal to the number of columns of binterpt and binterp
556: . binterpt - Coefficients of the interpolation formula for the stiff part (dimension s*pinterp)
557: - binterp - Coefficients of the interpolation formula for the non-stiff part (dimension s*pinterp; NULL to reuse binterpt)
559: Notes:
560: Several ARK IMEX methods are provided, this function is only needed to create new methods.
562: Level: advanced
564: .keywords: TS, register
566: .seealso: TSARKIMEX
567: @*/
568: PetscErrorCode TSARKIMEXRegister(TSARKIMEXType name,PetscInt order,PetscInt s,
569: const PetscReal At[],const PetscReal bt[],const PetscReal ct[],
570: const PetscReal A[],const PetscReal b[],const PetscReal c[],
571: const PetscReal bembedt[],const PetscReal bembed[],
572: PetscInt pinterp,const PetscReal binterpt[],const PetscReal binterp[])
573: {
575: ARKTableauLink link;
576: ARKTableau t;
577: PetscInt i,j;
580: TSARKIMEXInitializePackage();
581: PetscCalloc1(1,&link);
582: t = &link->tab;
583: PetscStrallocpy(name,&t->name);
584: t->order = order;
585: t->s = s;
586: PetscMalloc6(s*s,&t->At,s,&t->bt,s,&t->ct,s*s,&t->A,s,&t->b,s,&t->c);
587: PetscMemcpy(t->At,At,s*s*sizeof(At[0]));
588: PetscMemcpy(t->A,A,s*s*sizeof(A[0]));
589: if (bt) { PetscMemcpy(t->bt,bt,s*sizeof(bt[0])); }
590: else for (i=0; i<s; i++) t->bt[i] = At[(s-1)*s+i];
591: if (b) { PetscMemcpy(t->b,b,s*sizeof(b[0])); }
592: else for (i=0; i<s; i++) t->b[i] = t->bt[i];
593: if (ct) { PetscMemcpy(t->ct,ct,s*sizeof(ct[0])); }
594: else for (i=0; i<s; i++) for (j=0,t->ct[i]=0; j<s; j++) t->ct[i] += At[i*s+j];
595: if (c) { PetscMemcpy(t->c,c,s*sizeof(c[0])); }
596: else for (i=0; i<s; i++) for (j=0,t->c[i]=0; j<s; j++) t->c[i] += A[i*s+j];
597: t->stiffly_accurate = PETSC_TRUE;
598: for (i=0; i<s; i++) if (t->At[(s-1)*s+i] != t->bt[i]) t->stiffly_accurate = PETSC_FALSE;
599: t->explicit_first_stage = PETSC_TRUE;
600: for (i=0; i<s; i++) if (t->At[i] != 0.0) t->explicit_first_stage = PETSC_FALSE;
601: /*def of FSAL can be made more precise*/
602: t->FSAL_implicit = (PetscBool)(t->explicit_first_stage && t->stiffly_accurate);
603: if (bembedt) {
604: PetscMalloc2(s,&t->bembedt,s,&t->bembed);
605: PetscMemcpy(t->bembedt,bembedt,s*sizeof(bembedt[0]));
606: PetscMemcpy(t->bembed,bembed ? bembed : bembedt,s*sizeof(bembed[0]));
607: }
609: t->pinterp = pinterp;
610: PetscMalloc2(s*pinterp,&t->binterpt,s*pinterp,&t->binterp);
611: PetscMemcpy(t->binterpt,binterpt,s*pinterp*sizeof(binterpt[0]));
612: PetscMemcpy(t->binterp,binterp ? binterp : binterpt,s*pinterp*sizeof(binterpt[0]));
613: link->next = ARKTableauList;
614: ARKTableauList = link;
615: return(0);
616: }
618: /*
619: The step completion formula is
621: x1 = x0 - h bt^T YdotI + h b^T YdotRHS
623: This function can be called before or after ts->vec_sol has been updated.
624: Suppose we have a completion formula (bt,b) and an embedded formula (bet,be) of different order.
625: We can write
627: x1e = x0 - h bet^T YdotI + h be^T YdotRHS
628: = x1 + h bt^T YdotI - h b^T YdotRHS - h bet^T YdotI + h be^T YdotRHS
629: = x1 - h (bet - bt)^T YdotI + h (be - b)^T YdotRHS
631: so we can evaluate the method with different order even after the step has been optimistically completed.
632: */
633: static PetscErrorCode TSEvaluateStep_ARKIMEX(TS ts,PetscInt order,Vec X,PetscBool *done)
634: {
635: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
636: ARKTableau tab = ark->tableau;
637: PetscScalar *w = ark->work;
638: PetscReal h;
639: PetscInt s = tab->s,j;
643: switch (ark->status) {
644: case TS_STEP_INCOMPLETE:
645: case TS_STEP_PENDING:
646: h = ts->time_step; break;
647: case TS_STEP_COMPLETE:
648: h = ts->ptime - ts->ptime_prev; break;
649: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
650: }
651: if (order == tab->order) {
652: if (ark->status == TS_STEP_INCOMPLETE) {
653: if (!ark->imex && tab->stiffly_accurate) { /* Only the stiffly accurate implicit formula is used */
654: VecCopy(ark->Y[s-1],X);
655: } else { /* Use the standard completion formula (bt,b) */
656: VecCopy(ts->vec_sol,X);
657: for (j=0; j<s; j++) w[j] = h*tab->bt[j];
658: VecMAXPY(X,s,w,ark->YdotI);
659: if (ark->imex) { /* Method is IMEX, complete the explicit formula */
660: for (j=0; j<s; j++) w[j] = h*tab->b[j];
661: VecMAXPY(X,s,w,ark->YdotRHS);
662: }
663: }
664: } else {VecCopy(ts->vec_sol,X);}
665: if (done) *done = PETSC_TRUE;
666: return(0);
667: } else if (order == tab->order-1) {
668: if (!tab->bembedt) goto unavailable;
669: if (ark->status == TS_STEP_INCOMPLETE) { /* Complete with the embedded method (bet,be) */
670: VecCopy(ts->vec_sol,X);
671: for (j=0; j<s; j++) w[j] = h*tab->bembedt[j];
672: VecMAXPY(X,s,w,ark->YdotI);
673: for (j=0; j<s; j++) w[j] = h*tab->bembed[j];
674: VecMAXPY(X,s,w,ark->YdotRHS);
675: } else { /* Rollback and re-complete using (bet-be,be-b) */
676: VecCopy(ts->vec_sol,X);
677: for (j=0; j<s; j++) w[j] = h*(tab->bembedt[j] - tab->bt[j]);
678: VecMAXPY(X,tab->s,w,ark->YdotI);
679: for (j=0; j<s; j++) w[j] = h*(tab->bembed[j] - tab->b[j]);
680: VecMAXPY(X,s,w,ark->YdotRHS);
681: }
682: if (done) *done = PETSC_TRUE;
683: return(0);
684: }
685: unavailable:
686: if (done) *done = PETSC_FALSE;
687: else SETERRQ3(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);
688: return(0);
689: }
691: static PetscErrorCode TSRollBack_ARKIMEX(TS ts)
692: {
693: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
694: ARKTableau tab = ark->tableau;
695: const PetscInt s = tab->s;
696: const PetscReal *bt = tab->bt,*b = tab->b;
697: PetscScalar *w = ark->work;
698: Vec *YdotI = ark->YdotI,*YdotRHS = ark->YdotRHS;
699: PetscInt j;
700: PetscReal h;
701: PetscErrorCode ierr;
704: switch (ark->status) {
705: case TS_STEP_INCOMPLETE:
706: case TS_STEP_PENDING:
707: h = ts->time_step; break;
708: case TS_STEP_COMPLETE:
709: h = ts->ptime - ts->ptime_prev; break;
710: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
711: }
712: for (j=0; j<s; j++) w[j] = -h*bt[j];
713: VecMAXPY(ts->vec_sol,s,w,YdotI);
714: for (j=0; j<s; j++) w[j] = -h*b[j];
715: VecMAXPY(ts->vec_sol,s,w,YdotRHS);
716: return(0);
717: }
719: static PetscErrorCode TSStep_ARKIMEX(TS ts)
720: {
721: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
722: ARKTableau tab = ark->tableau;
723: const PetscInt s = tab->s;
724: const PetscReal *At = tab->At,*A = tab->A,*ct = tab->ct,*c = tab->c;
725: PetscScalar *w = ark->work;
726: Vec *Y = ark->Y,*YdotI = ark->YdotI,*YdotRHS = ark->YdotRHS,Ydot = ark->Ydot,Ydot0 = ark->Ydot0,Z = ark->Z;
727: PetscBool extrapolate = ark->extrapolate;
728: TSAdapt adapt;
729: SNES snes;
730: PetscInt i,j,its,lits;
731: PetscInt rejections = 0;
732: PetscBool stageok,accept = PETSC_TRUE;
733: PetscReal next_time_step = ts->time_step;
734: PetscErrorCode ierr;
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: TSClone(ts,&ts_start);
759: TSSetSolution(ts_start,ts->vec_sol);
760: TSSetTime(ts_start,ts->ptime);
761: TSSetMaxSteps(ts_start,ts->steps+1);
762: TSSetMaxTime(ts_start,ts->ptime+ts->time_step);
763: TSSetExactFinalTime(ts_start,TS_EXACTFINALTIME_STEPOVER);
764: TSSetTimeStep(ts_start,ts->time_step);
765: TSSetType(ts_start,TSARKIMEX);
766: TSARKIMEXSetFullyImplicit(ts_start,PETSC_TRUE);
767: TSARKIMEXSetType(ts_start,TSARKIMEX1BEE);
769: TSRestartStep(ts_start);
770: TSSolve(ts_start,ts->vec_sol);
771: TSGetTime(ts_start,&ts->ptime);
772: TSGetTimeStep(ts_start,&ts->time_step);
774: { /* Save the initial slope for the next step */
775: TS_ARKIMEX *ark_start = (TS_ARKIMEX*)ts_start->data;
776: VecCopy(ark_start->YdotI[ark_start->tableau->s-1],Ydot0);
777: }
778: ts->steps++;
780: /* Set the correct TS in SNES */
781: /* We'll try to bypass this by changing the method on the fly */
782: {
783: TSGetSNES(ts,&snes);
784: TSSetSNES(ts,snes);
785: }
786: TSDestroy(&ts_start);
787: }
789: ark->status = TS_STEP_INCOMPLETE;
790: while (!ts->reason && ark->status != TS_STEP_COMPLETE) {
791: PetscReal t = ts->ptime;
792: PetscReal h = ts->time_step;
793: for (i=0; i<s; i++) {
794: ark->stage_time = t + h*ct[i];
795: TSPreStage(ts,ark->stage_time);
796: if (At[i*s+i] == 0) { /* This stage is explicit */
797: if (i!=0 && ts->equation_type >= TS_EQ_IMPLICIT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Explicit stages other than the first one are not supported for implicit problems");
798: VecCopy(ts->vec_sol,Y[i]);
799: for (j=0; j<i; j++) w[j] = h*At[i*s+j];
800: VecMAXPY(Y[i],i,w,YdotI);
801: for (j=0; j<i; j++) w[j] = h*A[i*s+j];
802: VecMAXPY(Y[i],i,w,YdotRHS);
803: } else {
804: ark->scoeff = 1./At[i*s+i];
805: /* Ydot = shift*(Y-Z) */
806: VecCopy(ts->vec_sol,Z);
807: for (j=0; j<i; j++) w[j] = h*At[i*s+j];
808: VecMAXPY(Z,i,w,YdotI);
809: for (j=0; j<i; j++) w[j] = h*A[i*s+j];
810: VecMAXPY(Z,i,w,YdotRHS);
811: if (extrapolate && !ts->steprestart) {
812: /* Initial guess extrapolated from previous time step stage values */
813: TSExtrapolate_ARKIMEX(ts,c[i],Y[i]);
814: } else {
815: /* Initial guess taken from last stage */
816: VecCopy(i>0 ? Y[i-1] : ts->vec_sol,Y[i]);
817: }
818: TSGetSNES(ts,&snes);
819: SNESSolve(snes,NULL,Y[i]);
820: SNESGetIterationNumber(snes,&its);
821: SNESGetLinearSolveIterations(snes,&lits);
822: ts->snes_its += its; ts->ksp_its += lits;
823: TSGetAdapt(ts,&adapt);
824: TSAdaptCheckStage(adapt,ts,ark->stage_time,Y[i],&stageok);
825: if (!stageok) {
826: /* We are likely rejecting the step because of solver or function domain problems so we should not attempt to
827: * use extrapolation to initialize the solves on the next attempt. */
828: extrapolate = PETSC_FALSE;
829: goto reject_step;
830: }
831: }
832: if (ts->equation_type >= TS_EQ_IMPLICIT) {
833: if (i==0 && tab->explicit_first_stage) {
834: if (!tab->stiffly_accurate ) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSARKIMEX %s is not stiffly accurate and therefore explicit-first stage methods cannot be used if the equation is implicit because the slope cannot be evaluated",ark->tableau->name);
835: VecCopy(Ydot0,YdotI[0]); /* YdotI = YdotI(tn-1) */
836: } else {
837: VecAXPBYPCZ(YdotI[i],-ark->scoeff/h,ark->scoeff/h,0,Z,Y[i]); /* YdotI = shift*(X-Z) */
838: }
839: } else {
840: if (i==0 && tab->explicit_first_stage) {
841: VecZeroEntries(Ydot);
842: TSComputeIFunction(ts,t+h*ct[i],Y[i],Ydot,YdotI[i],ark->imex);/* YdotI = -G(t,Y,0) */
843: VecScale(YdotI[i],-1.0);
844: } else {
845: VecAXPBYPCZ(YdotI[i],-ark->scoeff/h,ark->scoeff/h,0,Z,Y[i]); /* YdotI = shift*(X-Z) */
846: }
847: if (ark->imex) {
848: TSComputeRHSFunction(ts,t+h*c[i],Y[i],YdotRHS[i]);
849: } else {
850: VecZeroEntries(YdotRHS[i]);
851: }
852: }
853: TSPostStage(ts,ark->stage_time,i,Y);
854: }
856: ark->status = TS_STEP_INCOMPLETE;
857: TSEvaluateStep_ARKIMEX(ts,tab->order,ts->vec_sol,NULL);
858: ark->status = TS_STEP_PENDING;
859: TSGetAdapt(ts,&adapt);
860: TSAdaptCandidatesClear(adapt);
861: TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,(PetscReal)tab->s,PETSC_TRUE);
862: TSAdaptChoose(adapt,ts,ts->time_step,NULL,&next_time_step,&accept);
863: ark->status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
864: if (!accept) { /* Roll back the current step */
865: TSRollBack_ARKIMEX(ts);
866: ts->time_step = next_time_step;
867: goto reject_step;
868: }
870: ts->ptime += ts->time_step;
871: ts->time_step = next_time_step;
872: break;
874: reject_step:
875: ts->reject++; accept = PETSC_FALSE;
876: if (!ts->reason && ++rejections > ts->max_reject && ts->max_reject >= 0) {
877: ts->reason = TS_DIVERGED_STEP_REJECTED;
878: PetscInfo2(ts,"Step=%D, step rejections %D greater than current TS allowed, stopping solve\n",ts->steps,rejections);
879: }
880: }
881: return(0);
882: }
884: static PetscErrorCode TSInterpolate_ARKIMEX(TS ts,PetscReal itime,Vec X)
885: {
886: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
887: PetscInt s = ark->tableau->s,pinterp = ark->tableau->pinterp,i,j;
888: PetscReal h;
889: PetscReal tt,t;
890: PetscScalar *bt,*b;
891: const PetscReal *Bt = ark->tableau->binterpt,*B = ark->tableau->binterp;
892: PetscErrorCode ierr;
895: if (!Bt || !B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSARKIMEX %s does not have an interpolation formula",ark->tableau->name);
896: switch (ark->status) {
897: case TS_STEP_INCOMPLETE:
898: case TS_STEP_PENDING:
899: h = ts->time_step;
900: t = (itime - ts->ptime)/h;
901: break;
902: case TS_STEP_COMPLETE:
903: h = ts->ptime - ts->ptime_prev;
904: t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
905: break;
906: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
907: }
908: PetscMalloc2(s,&bt,s,&b);
909: for (i=0; i<s; i++) bt[i] = b[i] = 0;
910: for (j=0,tt=t; j<pinterp; j++,tt*=t) {
911: for (i=0; i<s; i++) {
912: bt[i] += h * Bt[i*pinterp+j] * tt;
913: b[i] += h * B[i*pinterp+j] * tt;
914: }
915: }
916: VecCopy(ark->Y[0],X);
917: VecMAXPY(X,s,bt,ark->YdotI);
918: VecMAXPY(X,s,b,ark->YdotRHS);
919: PetscFree2(bt,b);
920: return(0);
921: }
923: static PetscErrorCode TSExtrapolate_ARKIMEX(TS ts,PetscReal c,Vec X)
924: {
925: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
926: PetscInt s = ark->tableau->s,pinterp = ark->tableau->pinterp,i,j;
927: PetscReal h,h_prev,t,tt;
928: PetscScalar *bt,*b;
929: const PetscReal *Bt = ark->tableau->binterpt,*B = ark->tableau->binterp;
930: PetscErrorCode ierr;
933: if (!Bt || !B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSARKIMEX %s does not have an interpolation formula",ark->tableau->name);
934: PetscCalloc2(s,&bt,s,&b);
935: h = ts->time_step;
936: h_prev = ts->ptime - ts->ptime_prev;
937: t = 1 + h/h_prev*c;
938: for (j=0,tt=t; j<pinterp; j++,tt*=t) {
939: for (i=0; i<s; i++) {
940: bt[i] += h * Bt[i*pinterp+j] * tt;
941: b[i] += h * B[i*pinterp+j] * tt;
942: }
943: }
944: if (!ark->Y_prev) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Stages from previous step have not been stored");
945: VecCopy(ark->Y_prev[0],X);
946: VecMAXPY(X,s,bt,ark->YdotI_prev);
947: VecMAXPY(X,s,b,ark->YdotRHS_prev);
948: PetscFree2(bt,b);
949: return(0);
950: }
952: /*------------------------------------------------------------*/
954: static PetscErrorCode TSARKIMEXTableauReset(TS ts)
955: {
956: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
957: ARKTableau tab = ark->tableau;
961: if (!tab) return(0);
962: PetscFree(ark->work);
963: VecDestroyVecs(tab->s,&ark->Y);
964: VecDestroyVecs(tab->s,&ark->YdotI);
965: VecDestroyVecs(tab->s,&ark->YdotRHS);
966: VecDestroyVecs(tab->s,&ark->Y_prev);
967: VecDestroyVecs(tab->s,&ark->YdotI_prev);
968: VecDestroyVecs(tab->s,&ark->YdotRHS_prev);
969: return(0);
970: }
972: static PetscErrorCode TSReset_ARKIMEX(TS ts)
973: {
974: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
978: TSARKIMEXTableauReset(ts);
979: VecDestroy(&ark->Ydot);
980: VecDestroy(&ark->Ydot0);
981: VecDestroy(&ark->Z);
982: return(0);
983: }
985: static PetscErrorCode TSARKIMEXGetVecs(TS ts,DM dm,Vec *Z,Vec *Ydot)
986: {
987: TS_ARKIMEX *ax = (TS_ARKIMEX*)ts->data;
991: if (Z) {
992: if (dm && dm != ts->dm) {
993: DMGetNamedGlobalVector(dm,"TSARKIMEX_Z",Z);
994: } else *Z = ax->Z;
995: }
996: if (Ydot) {
997: if (dm && dm != ts->dm) {
998: DMGetNamedGlobalVector(dm,"TSARKIMEX_Ydot",Ydot);
999: } else *Ydot = ax->Ydot;
1000: }
1001: return(0);
1002: }
1005: static PetscErrorCode TSARKIMEXRestoreVecs(TS ts,DM dm,Vec *Z,Vec *Ydot)
1006: {
1010: if (Z) {
1011: if (dm && dm != ts->dm) {
1012: DMRestoreNamedGlobalVector(dm,"TSARKIMEX_Z",Z);
1013: }
1014: }
1015: if (Ydot) {
1016: if (dm && dm != ts->dm) {
1017: DMRestoreNamedGlobalVector(dm,"TSARKIMEX_Ydot",Ydot);
1018: }
1019: }
1020: return(0);
1021: }
1023: /*
1024: This defines the nonlinear equation that is to be solved with SNES
1025: G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
1026: */
1027: static PetscErrorCode SNESTSFormFunction_ARKIMEX(SNES snes,Vec X,Vec F,TS ts)
1028: {
1029: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1030: DM dm,dmsave;
1031: Vec Z,Ydot;
1032: PetscReal shift = ark->scoeff / ts->time_step;
1036: SNESGetDM(snes,&dm);
1037: TSARKIMEXGetVecs(ts,dm,&Z,&Ydot);
1038: VecAXPBYPCZ(Ydot,-shift,shift,0,Z,X); /* Ydot = shift*(X-Z) */
1039: dmsave = ts->dm;
1040: ts->dm = dm;
1042: TSComputeIFunction(ts,ark->stage_time,X,Ydot,F,ark->imex);
1044: ts->dm = dmsave;
1045: TSARKIMEXRestoreVecs(ts,dm,&Z,&Ydot);
1046: return(0);
1047: }
1049: static PetscErrorCode SNESTSFormJacobian_ARKIMEX(SNES snes,Vec X,Mat A,Mat B,TS ts)
1050: {
1051: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1052: DM dm,dmsave;
1053: Vec Ydot;
1054: PetscReal shift = ark->scoeff / ts->time_step;
1058: SNESGetDM(snes,&dm);
1059: TSARKIMEXGetVecs(ts,dm,NULL,&Ydot);
1060: /* ark->Ydot has already been computed in SNESTSFormFunction_ARKIMEX (SNES guarantees this) */
1061: dmsave = ts->dm;
1062: ts->dm = dm;
1064: TSComputeIJacobian(ts,ark->stage_time,X,Ydot,shift,A,B,ark->imex);
1066: ts->dm = dmsave;
1067: TSARKIMEXRestoreVecs(ts,dm,NULL,&Ydot);
1068: return(0);
1069: }
1071: static PetscErrorCode DMCoarsenHook_TSARKIMEX(DM fine,DM coarse,void *ctx)
1072: {
1074: return(0);
1075: }
1077: static PetscErrorCode DMRestrictHook_TSARKIMEX(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
1078: {
1079: TS ts = (TS)ctx;
1081: Vec Z,Z_c;
1084: TSARKIMEXGetVecs(ts,fine,&Z,NULL);
1085: TSARKIMEXGetVecs(ts,coarse,&Z_c,NULL);
1086: MatRestrict(restrct,Z,Z_c);
1087: VecPointwiseMult(Z_c,rscale,Z_c);
1088: TSARKIMEXRestoreVecs(ts,fine,&Z,NULL);
1089: TSARKIMEXRestoreVecs(ts,coarse,&Z_c,NULL);
1090: return(0);
1091: }
1094: static PetscErrorCode DMSubDomainHook_TSARKIMEX(DM dm,DM subdm,void *ctx)
1095: {
1097: return(0);
1098: }
1100: static PetscErrorCode DMSubDomainRestrictHook_TSARKIMEX(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
1101: {
1102: TS ts = (TS)ctx;
1104: Vec Z,Z_c;
1107: TSARKIMEXGetVecs(ts,dm,&Z,NULL);
1108: TSARKIMEXGetVecs(ts,subdm,&Z_c,NULL);
1110: VecScatterBegin(gscat,Z,Z_c,INSERT_VALUES,SCATTER_FORWARD);
1111: VecScatterEnd(gscat,Z,Z_c,INSERT_VALUES,SCATTER_FORWARD);
1113: TSARKIMEXRestoreVecs(ts,dm,&Z,NULL);
1114: TSARKIMEXRestoreVecs(ts,subdm,&Z_c,NULL);
1115: return(0);
1116: }
1118: static PetscErrorCode TSARKIMEXTableauSetUp(TS ts)
1119: {
1120: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1121: ARKTableau tab = ark->tableau;
1125: PetscMalloc1(tab->s,&ark->work);
1126: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y);
1127: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI);
1128: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS);
1129: if (ark->extrapolate) {
1130: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y_prev);
1131: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI_prev);
1132: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS_prev);
1133: }
1134: return(0);
1135: }
1137: static PetscErrorCode TSSetUp_ARKIMEX(TS ts)
1138: {
1139: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1141: DM dm;
1142: SNES snes;
1145: TSARKIMEXTableauSetUp(ts);
1146: VecDuplicate(ts->vec_sol,&ark->Ydot);
1147: VecDuplicate(ts->vec_sol,&ark->Ydot0);
1148: VecDuplicate(ts->vec_sol,&ark->Z);
1149: TSGetDM(ts,&dm);
1150: DMCoarsenHookAdd(dm,DMCoarsenHook_TSARKIMEX,DMRestrictHook_TSARKIMEX,ts);
1151: DMSubDomainHookAdd(dm,DMSubDomainHook_TSARKIMEX,DMSubDomainRestrictHook_TSARKIMEX,ts);
1152: TSGetSNES(ts,&snes);
1153: return(0);
1154: }
1155: /*------------------------------------------------------------*/
1157: static PetscErrorCode TSSetFromOptions_ARKIMEX(PetscOptionItems *PetscOptionsObject,TS ts)
1158: {
1159: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1163: PetscOptionsHead(PetscOptionsObject,"ARKIMEX ODE solver options");
1164: {
1165: ARKTableauLink link;
1166: PetscInt count,choice;
1167: PetscBool flg;
1168: const char **namelist;
1169: for (link=ARKTableauList,count=0; link; link=link->next,count++) ;
1170: PetscMalloc1(count,(char***)&namelist);
1171: for (link=ARKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
1172: PetscOptionsEList("-ts_arkimex_type","Family of ARK IMEX method","TSARKIMEXSetType",(const char*const*)namelist,count,ark->tableau->name,&choice,&flg);
1173: if (flg) {TSARKIMEXSetType(ts,namelist[choice]);}
1174: PetscFree(namelist);
1176: flg = (PetscBool) !ark->imex;
1177: PetscOptionsBool("-ts_arkimex_fully_implicit","Solve the problem fully implicitly","TSARKIMEXSetFullyImplicit",flg,&flg,NULL);
1178: ark->imex = (PetscBool) !flg;
1179: 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);
1180: }
1181: PetscOptionsTail();
1182: return(0);
1183: }
1185: static PetscErrorCode TSView_ARKIMEX(TS ts,PetscViewer viewer)
1186: {
1187: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1188: PetscBool iascii;
1192: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1193: if (iascii) {
1194: ARKTableau tab = ark->tableau;
1195: TSARKIMEXType arktype;
1196: char buf[512];
1197: TSARKIMEXGetType(ts,&arktype);
1198: PetscViewerASCIIPrintf(viewer," ARK IMEX %s\n",arktype);
1199: PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->ct);
1200: PetscViewerASCIIPrintf(viewer," Stiff abscissa ct = %s\n",buf);
1201: PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->c);
1202: PetscViewerASCIIPrintf(viewer,"Stiffly accurate: %s\n",tab->stiffly_accurate ? "yes" : "no");
1203: PetscViewerASCIIPrintf(viewer,"Explicit first stage: %s\n",tab->explicit_first_stage ? "yes" : "no");
1204: PetscViewerASCIIPrintf(viewer,"FSAL property: %s\n",tab->FSAL_implicit ? "yes" : "no");
1205: PetscViewerASCIIPrintf(viewer," Nonstiff abscissa c = %s\n",buf);
1206: }
1207: return(0);
1208: }
1210: static PetscErrorCode TSLoad_ARKIMEX(TS ts,PetscViewer viewer)
1211: {
1213: SNES snes;
1214: TSAdapt adapt;
1217: TSGetAdapt(ts,&adapt);
1218: TSAdaptLoad(adapt,viewer);
1219: TSGetSNES(ts,&snes);
1220: SNESLoad(snes,viewer);
1221: /* function and Jacobian context for SNES when used with TS is always ts object */
1222: SNESSetFunction(snes,NULL,NULL,ts);
1223: SNESSetJacobian(snes,NULL,NULL,NULL,ts);
1224: return(0);
1225: }
1227: /*@C
1228: TSARKIMEXSetType - Set the type of ARK IMEX scheme
1230: Logically collective
1232: Input Parameter:
1233: + ts - timestepping context
1234: - arktype - type of ARK-IMEX scheme
1236: Options Database:
1237: . -ts_arkimex_type <1bee,a2,l2,ars122,2c,2d,2e,prssp2,3,bpr3,ars443,4,5>
1239: Level: intermediate
1241: .seealso: TSARKIMEXGetType(), TSARKIMEX, TSARKIMEXType, TSARKIMEX1BEE, TSARKIMEXA2, TSARKIMEXL2, TSARKIMEXARS122, TSARKIMEX2C, TSARKIMEX2D, TSARKIMEX2E, TSARKIMEXPRSSP2,
1242: TSARKIMEX3, TSARKIMEXBPR3, TSARKIMEXARS443, TSARKIMEX4, TSARKIMEX5
1243: @*/
1244: PetscErrorCode TSARKIMEXSetType(TS ts,TSARKIMEXType arktype)
1245: {
1251: PetscTryMethod(ts,"TSARKIMEXSetType_C",(TS,TSARKIMEXType),(ts,arktype));
1252: return(0);
1253: }
1255: /*@C
1256: TSARKIMEXGetType - Get the type of ARK IMEX scheme
1258: Logically collective
1260: Input Parameter:
1261: . ts - timestepping context
1263: Output Parameter:
1264: . arktype - type of ARK-IMEX scheme
1266: Level: intermediate
1268: .seealso: TSARKIMEXGetType()
1269: @*/
1270: PetscErrorCode TSARKIMEXGetType(TS ts,TSARKIMEXType *arktype)
1271: {
1276: PetscUseMethod(ts,"TSARKIMEXGetType_C",(TS,TSARKIMEXType*),(ts,arktype));
1277: return(0);
1278: }
1280: /*@
1281: TSARKIMEXSetFullyImplicit - Solve both parts of the equation implicitly
1283: Logically collective
1285: Input Parameter:
1286: + ts - timestepping context
1287: - flg - PETSC_TRUE for fully implicit
1289: Level: intermediate
1291: .seealso: TSARKIMEXGetType()
1292: @*/
1293: PetscErrorCode TSARKIMEXSetFullyImplicit(TS ts,PetscBool flg)
1294: {
1299: PetscTryMethod(ts,"TSARKIMEXSetFullyImplicit_C",(TS,PetscBool),(ts,flg));
1300: return(0);
1301: }
1303: static PetscErrorCode TSARKIMEXGetType_ARKIMEX(TS ts,TSARKIMEXType *arktype)
1304: {
1305: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1308: *arktype = ark->tableau->name;
1309: return(0);
1310: }
1311: static PetscErrorCode TSARKIMEXSetType_ARKIMEX(TS ts,TSARKIMEXType arktype)
1312: {
1313: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1315: PetscBool match;
1316: ARKTableauLink link;
1319: if (ark->tableau) {
1320: PetscStrcmp(ark->tableau->name,arktype,&match);
1321: if (match) return(0);
1322: }
1323: for (link = ARKTableauList; link; link=link->next) {
1324: PetscStrcmp(link->tab.name,arktype,&match);
1325: if (match) {
1326: if (ts->setupcalled) {TSARKIMEXTableauReset(ts);}
1327: ark->tableau = &link->tab;
1328: if (ts->setupcalled) {TSARKIMEXTableauSetUp(ts);}
1329: ts->default_adapt_type = ark->tableau->bembed ? TSADAPTBASIC : TSADAPTNONE;
1330: return(0);
1331: }
1332: }
1333: SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",arktype);
1334: return(0);
1335: }
1337: static PetscErrorCode TSARKIMEXSetFullyImplicit_ARKIMEX(TS ts,PetscBool flg)
1338: {
1339: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1342: ark->imex = (PetscBool)!flg;
1343: return(0);
1344: }
1346: static PetscErrorCode TSDestroy_ARKIMEX(TS ts)
1347: {
1351: TSReset_ARKIMEX(ts);
1352: if (ts->dm) {
1353: DMCoarsenHookRemove(ts->dm,DMCoarsenHook_TSARKIMEX,DMRestrictHook_TSARKIMEX,ts);
1354: DMSubDomainHookRemove(ts->dm,DMSubDomainHook_TSARKIMEX,DMSubDomainRestrictHook_TSARKIMEX,ts);
1355: }
1356: PetscFree(ts->data);
1357: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetType_C",NULL);
1358: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetType_C",NULL);
1359: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",NULL);
1360: return(0);
1361: }
1363: /* ------------------------------------------------------------ */
1364: /*MC
1365: TSARKIMEX - ODE and DAE solver using Additive Runge-Kutta IMEX schemes
1367: These methods are intended for problems with well-separated time scales, especially when a slow scale is strongly
1368: nonlinear such that it is expensive to solve with a fully implicit method. The user should provide the stiff part
1369: of the equation using TSSetIFunction() and the non-stiff part with TSSetRHSFunction().
1371: Notes:
1372: The default is TSARKIMEX3, it can be changed with TSARKIMEXSetType() or -ts_arkimex_type
1374: If the equation is implicit or a DAE, then TSSetEquationType() needs to be set accordingly. Refer to the manual for further information.
1376: 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).
1378: Consider trying TSROSW if the stiff part is linear or weakly nonlinear.
1380: Level: beginner
1382: .seealso: TSCreate(), TS, TSSetType(), TSARKIMEXSetType(), TSARKIMEXGetType(), TSARKIMEXSetFullyImplicit(), TSARKIMEX1BEE,
1383: TSARKIMEX2C, TSARKIMEX2D, TSARKIMEX2E, TSARKIMEX3, TSARKIMEXL2, TSARKIMEXA2, TSARKIMEXARS122,
1384: TSARKIMEX4, TSARKIMEX5, TSARKIMEXPRSSP2, TSARKIMEXARS443, TSARKIMEXBPR3, TSARKIMEXType, TSARKIMEXRegister()
1386: M*/
1387: PETSC_EXTERN PetscErrorCode TSCreate_ARKIMEX(TS ts)
1388: {
1389: TS_ARKIMEX *th;
1393: TSARKIMEXInitializePackage();
1395: ts->ops->reset = TSReset_ARKIMEX;
1396: ts->ops->destroy = TSDestroy_ARKIMEX;
1397: ts->ops->view = TSView_ARKIMEX;
1398: ts->ops->load = TSLoad_ARKIMEX;
1399: ts->ops->setup = TSSetUp_ARKIMEX;
1400: ts->ops->step = TSStep_ARKIMEX;
1401: ts->ops->interpolate = TSInterpolate_ARKIMEX;
1402: ts->ops->evaluatestep = TSEvaluateStep_ARKIMEX;
1403: ts->ops->rollback = TSRollBack_ARKIMEX;
1404: ts->ops->setfromoptions = TSSetFromOptions_ARKIMEX;
1405: ts->ops->snesfunction = SNESTSFormFunction_ARKIMEX;
1406: ts->ops->snesjacobian = SNESTSFormJacobian_ARKIMEX;
1408: ts->usessnes = PETSC_TRUE;
1410: PetscNewLog(ts,&th);
1411: ts->data = (void*)th;
1412: th->imex = PETSC_TRUE;
1414: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetType_C",TSARKIMEXGetType_ARKIMEX);
1415: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetType_C",TSARKIMEXSetType_ARKIMEX);
1416: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",TSARKIMEXSetFullyImplicit_ARKIMEX);
1418: TSARKIMEXSetType(ts,TSARKIMEXDefault);
1419: return(0);
1420: }