Actual source code: arkimex.c
petsc-3.7.7 2017-09-25
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> /*I "petscts.h" I*/
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: References:
66: . 1. - U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).
68: Level: advanced
70: .seealso: TSARKIMEX
71: M*/
72: /*MC
73: TSARKIMEXA2 - Second order ARK IMEX scheme with A-stable implicit part.
75: This method has an explicit stage and one implicit stage, and has an A-stable implicit scheme. This method was provided by Emil Constantinescu.
77: Level: advanced
79: .seealso: TSARKIMEX
80: M*/
81: /*MC
82: TSARKIMEXL2 - Second order ARK IMEX scheme with L-stable implicit part.
84: This method has two implicit stages, and L-stable implicit scheme.
86: References:
87: . 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.
89: Level: advanced
91: .seealso: TSARKIMEX
92: M*/
93: /*MC
94: TSARKIMEX1BEE - First order Backward Euler represented as an ARK IMEX scheme with extrapolation as error estimator. This is a 3-stage method.
96: This method is aimed at starting the integration of implicit DAEs when explicit first-stage ARK methods are used.
98: Level: advanced
100: .seealso: TSARKIMEX
101: M*/
102: /*MC
103: TSARKIMEX2C - Second order ARK IMEX scheme with L-stable implicit part.
105: 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.
107: Level: advanced
109: .seealso: TSARKIMEX
110: M*/
111: /*MC
112: TSARKIMEX2D - Second order ARK IMEX scheme with L-stable implicit part.
114: 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.
116: Level: advanced
118: .seealso: TSARKIMEX
119: M*/
120: /*MC
121: TSARKIMEX2E - Second order ARK IMEX scheme with L-stable implicit part.
123: This method has one explicit stage and two implicit stages. It is is an optimal method developed by Emil Constantinescu.
125: Level: advanced
127: .seealso: TSARKIMEX
128: M*/
129: /*MC
130: TSARKIMEXPRSSP2 - Second order SSP ARK IMEX scheme.
132: This method has three implicit stages.
134: References:
135: . 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.
137: This method is referred to as SSP2-(3,3,2) in http://arxiv.org/abs/1110.4375
139: Level: advanced
141: .seealso: TSARKIMEX
142: M*/
143: /*MC
144: TSARKIMEX3 - Third order ARK IMEX scheme with L-stable implicit part.
146: This method has one explicit stage and three implicit stages.
148: References:
149: . 1. - Kennedy and Carpenter 2003.
151: Level: advanced
153: .seealso: TSARKIMEX
154: M*/
155: /*MC
156: TSARKIMEXARS443 - Third order ARK IMEX scheme.
158: This method has one explicit stage and four implicit stages.
160: References:
161: + 1. - U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).
162: - 2. - This method is referred to as ARS(4,4,3) in http://arxiv.org/abs/1110.4375
164: Level: advanced
166: .seealso: TSARKIMEX
167: M*/
168: /*MC
169: TSARKIMEXBPR3 - Third order ARK IMEX scheme.
171: This method has one explicit stage and four implicit stages.
173: References:
174: . This method is referred to as ARK3 in http://arxiv.org/abs/1110.4375
176: Level: advanced
178: .seealso: TSARKIMEX
179: M*/
180: /*MC
181: TSARKIMEX4 - Fourth order ARK IMEX scheme with L-stable implicit part.
183: This method has one explicit stage and four implicit stages.
185: References:
186: . Kennedy and Carpenter 2003.
188: Level: advanced
190: .seealso: TSARKIMEX
191: M*/
192: /*MC
193: TSARKIMEX5 - Fifth order ARK IMEX scheme with L-stable implicit part.
195: This method has one explicit stage and five implicit stages.
197: References:
198: . Kennedy and Carpenter 2003.
200: Level: advanced
202: .seealso: TSARKIMEX
203: M*/
207: /*@C
208: TSARKIMEXRegisterAll - Registers all of the additive Runge-Kutta implicit-explicit methods in TSARKIMEX
210: Not Collective, but should be called by all processes which will need the schemes to be registered
212: Level: advanced
214: .keywords: TS, TSARKIMEX, register, all
216: .seealso: TSARKIMEXRegisterDestroy()
217: @*/
218: PetscErrorCode TSARKIMEXRegisterAll(void)
219: {
223: if (TSARKIMEXRegisterAllCalled) return(0);
224: TSARKIMEXRegisterAllCalled = PETSC_TRUE;
226: {
227: const PetscReal
228: A[3][3] = {{0.0,0.0,0.0},
229: {0.0,0.0,0.0},
230: {0.0,0.5,0.0}},
231: At[3][3] = {{1.0,0.0,0.0},
232: {0.0,0.5,0.0},
233: {0.0,0.5,0.5}},
234: b[3] = {0.0,0.5,0.5},
235: bembedt[3] = {1.0,0.0,0.0};
236: TSARKIMEXRegister(TSARKIMEX1BEE,2,3,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
237: }
238: {
239: const PetscReal
240: A[2][2] = {{0.0,0.0},
241: {0.5,0.0}},
242: At[2][2] = {{0.0,0.0},
243: {0.0,0.5}},
244: b[2] = {0.0,1.0},
245: bembedt[2] = {0.5,0.5};
246: /* 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*/
247: TSARKIMEXRegister(TSARKIMEXARS122,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
248: }
249: {
250: const PetscReal
251: A[2][2] = {{0.0,0.0},
252: {1.0,0.0}},
253: At[2][2] = {{0.0,0.0},
254: {0.5,0.5}},
255: b[2] = {0.5,0.5},
256: bembedt[2] = {0.0,1.0};
257: /* 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*/
258: TSARKIMEXRegister(TSARKIMEXA2,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,1,b,NULL);
259: }
260: {
261: /* 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 */
262: const PetscReal
263: A[2][2] = {{0.0,0.0},
264: {1.0,0.0}},
265: At[2][2] = {{0.2928932188134524755992,0.0},
266: {1.0-2.0*0.2928932188134524755992,0.2928932188134524755992}},
267: b[2] = {0.5,0.5},
268: bembedt[2] = {0.0,1.0},
269: binterpt[2][2] = {{ (0.2928932188134524755992-1.0)/(2.0*0.2928932188134524755992-1.0),-1/(2.0*(1.0-2.0*0.2928932188134524755992))},
270: {1-(0.2928932188134524755992-1.0)/(2.0*0.2928932188134524755992-1.0),-1/(2.0*(1.0-2.0*0.2928932188134524755992))}},
271: binterp[2][2] = {{1.0,-0.5},{0.0,0.5}};
272: TSARKIMEXRegister(TSARKIMEXL2,2,2,&At[0][0],b,NULL,&A[0][0],b,NULL,bembedt,bembedt,2,binterpt[0],binterp[0]);
273: }
274: {
275: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
276: const PetscReal
277: A[3][3] = {{0,0,0},
278: {2-1.414213562373095048802,0,0},
279: {0.5,0.5,0}},
280: At[3][3] = {{0,0,0},
281: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
282: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
283: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
284: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
285: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
286: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
287: TSARKIMEXRegister(TSARKIMEX2C,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
288: }
289: {
290: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
291: const PetscReal
292: A[3][3] = {{0,0,0},
293: {2-1.414213562373095048802,0,0},
294: {0.75,0.25,0}},
295: At[3][3] = {{0,0,0},
296: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
297: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
298: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
299: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
300: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
301: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
302: TSARKIMEXRegister(TSARKIMEX2D,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
303: }
304: { /* Optimal for linear implicit part */
305: /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0), Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time */
306: const PetscReal
307: A[3][3] = {{0,0,0},
308: {2-1.414213562373095048802,0,0},
309: {(3-2*1.414213562373095048802)/6,(3+2*1.414213562373095048802)/6,0}},
310: At[3][3] = {{0,0,0},
311: {1-1/1.414213562373095048802,1-1/1.414213562373095048802,0},
312: {1/(2*1.414213562373095048802),1/(2*1.414213562373095048802),1-1/1.414213562373095048802}},
313: bembedt[3] = {(4.-1.414213562373095048802)/8.,(4.-1.414213562373095048802)/8.,1/(2.*1.414213562373095048802)},
314: binterpt[3][2] = {{1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
315: {1.0/1.414213562373095048802,-1.0/(2.0*1.414213562373095048802)},
316: {1.0-1.414213562373095048802,1.0/1.414213562373095048802}};
317: TSARKIMEXRegister(TSARKIMEX2E,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
318: }
319: { /* Optimal for linear implicit part */
320: const PetscReal
321: A[3][3] = {{0,0,0},
322: {0.5,0,0},
323: {0.5,0.5,0}},
324: At[3][3] = {{0.25,0,0},
325: {0,0.25,0},
326: {1./3,1./3,1./3}};
327: TSARKIMEXRegister(TSARKIMEXPRSSP2,2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,NULL,NULL,0,NULL,NULL);
328: }
329: {
330: const PetscReal
331: A[4][4] = {{0,0,0,0},
332: {1767732205903./2027836641118.,0,0,0},
333: {5535828885825./10492691773637.,788022342437./10882634858940.,0,0},
334: {6485989280629./16251701735622.,-4246266847089./9704473918619.,10755448449292./10357097424841.,0}},
335: At[4][4] = {{0,0,0,0},
336: {1767732205903./4055673282236.,1767732205903./4055673282236.,0,0},
337: {2746238789719./10658868560708.,-640167445237./6845629431997.,1767732205903./4055673282236.,0},
338: {1471266399579./7840856788654.,-4482444167858./7529755066697.,11266239266428./11593286722821.,1767732205903./4055673282236.}},
339: bembedt[4] = {2756255671327./12835298489170.,-10771552573575./22201958757719.,9247589265047./10645013368117.,2193209047091./5459859503100.},
340: binterpt[4][2] = {{4655552711362./22874653954995., -215264564351./13552729205753.},
341: {-18682724506714./9892148508045.,17870216137069./13817060693119.},
342: {34259539580243./13192909600954.,-28141676662227./17317692491321.},
343: {584795268549./6622622206610., 2508943948391./7218656332882.}};
344: TSARKIMEXRegister(TSARKIMEX3,3,4,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,2,binterpt[0],NULL);
345: }
346: {
347: const PetscReal
348: A[5][5] = {{0,0,0,0,0},
349: {1./2,0,0,0,0},
350: {11./18,1./18,0,0,0},
351: {5./6,-5./6,.5,0,0},
352: {1./4,7./4,3./4,-7./4,0}},
353: At[5][5] = {{0,0,0,0,0},
354: {0,1./2,0,0,0},
355: {0,1./6,1./2,0,0},
356: {0,-1./2,1./2,1./2,0},
357: {0,3./2,-3./2,1./2,1./2}},
358: *bembedt = NULL;
359: TSARKIMEXRegister(TSARKIMEXARS443,3,5,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,0,NULL,NULL);
360: }
361: {
362: const PetscReal
363: A[5][5] = {{0,0,0,0,0},
364: {1,0,0,0,0},
365: {4./9,2./9,0,0,0},
366: {1./4,0,3./4,0,0},
367: {1./4,0,3./5,0,0}},
368: At[5][5] = {{0,0,0,0,0},
369: {.5,.5,0,0,0},
370: {5./18,-1./9,.5,0,0},
371: {.5,0,0,.5,0},
372: {.25,0,.75,-.5,.5}},
373: *bembedt = NULL;
374: TSARKIMEXRegister(TSARKIMEXBPR3,3,5,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,0,NULL,NULL);
375: }
376: {
377: const PetscReal
378: A[6][6] = {{0,0,0,0,0,0},
379: {1./2,0,0,0,0,0},
380: {13861./62500.,6889./62500.,0,0,0,0},
381: {-116923316275./2393684061468.,-2731218467317./15368042101831.,9408046702089./11113171139209.,0,0,0},
382: {-451086348788./2902428689909.,-2682348792572./7519795681897.,12662868775082./11960479115383.,3355817975965./11060851509271.,0,0},
383: {647845179188./3216320057751.,73281519250./8382639484533.,552539513391./3454668386233.,3354512671639./8306763924573.,4040./17871.,0}},
384: At[6][6] = {{0,0,0,0,0,0},
385: {1./4,1./4,0,0,0,0},
386: {8611./62500.,-1743./31250.,1./4,0,0,0},
387: {5012029./34652500.,-654441./2922500.,174375./388108.,1./4,0,0},
388: {15267082809./155376265600.,-71443401./120774400.,730878875./902184768.,2285395./8070912.,1./4,0},
389: {82889./524892.,0,15625./83664.,69875./102672.,-2260./8211,1./4}},
390: bembedt[6] = {4586570599./29645900160.,0,178811875./945068544.,814220225./1159782912.,-3700637./11593932.,61727./225920.},
391: binterpt[6][3] = {{6943876665148./7220017795957.,-54480133./30881146.,6818779379841./7100303317025.},
392: {0,0,0},
393: {7640104374378./9702883013639.,-11436875./14766696.,2173542590792./12501825683035.},
394: {-20649996744609./7521556579894.,174696575./18121608.,-31592104683404./5083833661969.},
395: {8854892464581./2390941311638.,-12120380./966161.,61146701046299./7138195549469.},
396: {-11397109935349./6675773540249.,3843./706.,-17219254887155./4939391667607.}};
397: TSARKIMEXRegister(TSARKIMEX4,4,6,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,3,binterpt[0],NULL);
398: }
399: {
400: const PetscReal
401: A[8][8] = {{0,0,0,0,0,0,0,0},
402: {41./100,0,0,0,0,0,0,0},
403: {367902744464./2072280473677.,677623207551./8224143866563.,0,0,0,0,0,0},
404: {1268023523408./10340822734521.,0,1029933939417./13636558850479.,0,0,0,0,0},
405: {14463281900351./6315353703477.,0,66114435211212./5879490589093.,-54053170152839./4284798021562.,0,0,0,0},
406: {14090043504691./34967701212078.,0,15191511035443./11219624916014.,-18461159152457./12425892160975.,-281667163811./9011619295870.,0,0,0},
407: {19230459214898./13134317526959.,0,21275331358303./2942455364971.,-38145345988419./4862620318723.,-1./8,-1./8,0,0},
408: {-19977161125411./11928030595625.,0,-40795976796054./6384907823539.,177454434618887./12078138498510.,782672205425./8267701900261.,-69563011059811./9646580694205.,7356628210526./4942186776405.,0}},
409: At[8][8] = {{0,0,0,0,0,0,0,0},
410: {41./200.,41./200.,0,0,0,0,0,0},
411: {41./400.,-567603406766./11931857230679.,41./200.,0,0,0,0,0},
412: {683785636431./9252920307686.,0,-110385047103./1367015193373.,41./200.,0,0,0,0},
413: {3016520224154./10081342136671.,0,30586259806659./12414158314087.,-22760509404356./11113319521817.,41./200.,0,0,0},
414: {218866479029./1489978393911.,0,638256894668./5436446318841.,-1179710474555./5321154724896.,-60928119172./8023461067671.,41./200.,0,0},
415: {1020004230633./5715676835656.,0,25762820946817./25263940353407.,-2161375909145./9755907335909.,-211217309593./5846859502534.,-4269925059573./7827059040749.,41./200,0},
416: {-872700587467./9133579230613.,0,0,22348218063261./9555858737531.,-1143369518992./8141816002931.,-39379526789629./19018526304540.,32727382324388./42900044865799.,41./200.}},
417: bembedt[8] = {-975461918565./9796059967033.,0,0,78070527104295./32432590147079.,-548382580838./3424219808633.,-33438840321285./15594753105479.,3629800801594./4656183773603.,4035322873751./18575991585200.},
418: binterpt[8][3] = {{-17674230611817./10670229744614., 43486358583215./12773830924787., -9257016797708./5021505065439.},
419: {0, 0, 0 },
420: {0, 0, 0 },
421: {65168852399939./7868540260826., -91478233927265./11067650958493., 26096422576131./11239449250142.},
422: {15494834004392./5936557850923., -79368583304911./10890268929626., 92396832856987./20362823103730.},
423: {-99329723586156./26959484932159., -12239297817655./9152339842473., 30029262896817./10175596800299.},
424: {-19024464361622./5461577185407., 115839755401235./10719374521269., -26136350496073./3983972220547.},
425: {-6511271360970./6095937251113., 5843115559534./2180450260947., -5289405421727./3760307252460. }};
426: TSARKIMEXRegister(TSARKIMEX5,5,8,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembedt,3,binterpt[0],NULL);
427: }
428: return(0);
429: }
433: /*@C
434: TSARKIMEXRegisterDestroy - Frees the list of schemes that were registered by TSARKIMEXRegister().
436: Not Collective
438: Level: advanced
440: .keywords: TSARKIMEX, register, destroy
441: .seealso: TSARKIMEXRegister(), TSARKIMEXRegisterAll()
442: @*/
443: PetscErrorCode TSARKIMEXRegisterDestroy(void)
444: {
446: ARKTableauLink link;
449: while ((link = ARKTableauList)) {
450: ARKTableau t = &link->tab;
451: ARKTableauList = link->next;
452: PetscFree6(t->At,t->bt,t->ct,t->A,t->b,t->c);
453: PetscFree2(t->bembedt,t->bembed);
454: PetscFree2(t->binterpt,t->binterp);
455: PetscFree(t->name);
456: PetscFree(link);
457: }
458: TSARKIMEXRegisterAllCalled = PETSC_FALSE;
459: return(0);
460: }
464: /*@C
465: TSARKIMEXInitializePackage - This function initializes everything in the TSARKIMEX package. It is called
466: from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_ARKIMEX()
467: when using static libraries.
469: Level: developer
471: .keywords: TS, TSARKIMEX, initialize, package
472: .seealso: PetscInitialize()
473: @*/
474: PetscErrorCode TSARKIMEXInitializePackage(void)
475: {
479: if (TSARKIMEXPackageInitialized) return(0);
480: TSARKIMEXPackageInitialized = PETSC_TRUE;
481: TSARKIMEXRegisterAll();
482: PetscRegisterFinalize(TSARKIMEXFinalizePackage);
483: return(0);
484: }
488: /*@C
489: TSARKIMEXFinalizePackage - This function destroys everything in the TSARKIMEX package. It is
490: called from PetscFinalize().
492: Level: developer
494: .keywords: Petsc, destroy, package
495: .seealso: PetscFinalize()
496: @*/
497: PetscErrorCode TSARKIMEXFinalizePackage(void)
498: {
502: TSARKIMEXPackageInitialized = PETSC_FALSE;
503: TSARKIMEXRegisterDestroy();
504: return(0);
505: }
509: /*@C
510: TSARKIMEXRegister - register an ARK IMEX scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation
512: Not Collective, but the same schemes should be registered on all processes on which they will be used
514: Input Parameters:
515: + name - identifier for method
516: . order - approximation order of method
517: . s - number of stages, this is the dimension of the matrices below
518: . At - Butcher table of stage coefficients for stiff part (dimension s*s, row-major)
519: . bt - Butcher table for completing the stiff part of the step (dimension s; NULL to use the last row of At)
520: . ct - Abscissa of each stiff stage (dimension s, NULL to use row sums of At)
521: . A - Non-stiff stage coefficients (dimension s*s, row-major)
522: . b - Non-stiff step completion table (dimension s; NULL to use last row of At)
523: . c - Non-stiff abscissa (dimension s; NULL to use row sums of A)
524: . bembedt - Stiff part of completion table for embedded method (dimension s; NULL if not available)
525: . bembed - Non-stiff part of completion table for embedded method (dimension s; NULL to use bembedt if provided)
526: . pinterp - Order of the interpolation scheme, equal to the number of columns of binterpt and binterp
527: . binterpt - Coefficients of the interpolation formula for the stiff part (dimension s*pinterp)
528: - binterp - Coefficients of the interpolation formula for the non-stiff part (dimension s*pinterp; NULL to reuse binterpt)
530: Notes:
531: Several ARK IMEX methods are provided, this function is only needed to create new methods.
533: Level: advanced
535: .keywords: TS, register
537: .seealso: TSARKIMEX
538: @*/
539: PetscErrorCode TSARKIMEXRegister(TSARKIMEXType name,PetscInt order,PetscInt s,
540: const PetscReal At[],const PetscReal bt[],const PetscReal ct[],
541: const PetscReal A[],const PetscReal b[],const PetscReal c[],
542: const PetscReal bembedt[],const PetscReal bembed[],
543: PetscInt pinterp,const PetscReal binterpt[],const PetscReal binterp[])
544: {
546: ARKTableauLink link;
547: ARKTableau t;
548: PetscInt i,j;
551: PetscCalloc1(1,&link);
552: t = &link->tab;
553: PetscStrallocpy(name,&t->name);
554: t->order = order;
555: t->s = s;
556: PetscMalloc6(s*s,&t->At,s,&t->bt,s,&t->ct,s*s,&t->A,s,&t->b,s,&t->c);
557: PetscMemcpy(t->At,At,s*s*sizeof(At[0]));
558: PetscMemcpy(t->A,A,s*s*sizeof(A[0]));
559: if (bt) { PetscMemcpy(t->bt,bt,s*sizeof(bt[0])); }
560: else for (i=0; i<s; i++) t->bt[i] = At[(s-1)*s+i];
561: if (b) { PetscMemcpy(t->b,b,s*sizeof(b[0])); }
562: else for (i=0; i<s; i++) t->b[i] = t->bt[i];
563: if (ct) { PetscMemcpy(t->ct,ct,s*sizeof(ct[0])); }
564: else for (i=0; i<s; i++) for (j=0,t->ct[i]=0; j<s; j++) t->ct[i] += At[i*s+j];
565: if (c) { PetscMemcpy(t->c,c,s*sizeof(c[0])); }
566: else for (i=0; i<s; i++) for (j=0,t->c[i]=0; j<s; j++) t->c[i] += A[i*s+j];
567: t->stiffly_accurate = PETSC_TRUE;
568: for (i=0; i<s; i++) if (t->At[(s-1)*s+i] != t->bt[i]) t->stiffly_accurate = PETSC_FALSE;
569: t->explicit_first_stage = PETSC_TRUE;
570: for (i=0; i<s; i++) if (t->At[i] != 0.0) t->explicit_first_stage = PETSC_FALSE;
571: /*def of FSAL can be made more precise*/
572: t->FSAL_implicit = (PetscBool)(t->explicit_first_stage && t->stiffly_accurate);
573: if (bembedt) {
574: PetscMalloc2(s,&t->bembedt,s,&t->bembed);
575: PetscMemcpy(t->bembedt,bembedt,s*sizeof(bembedt[0]));
576: PetscMemcpy(t->bembed,bembed ? bembed : bembedt,s*sizeof(bembed[0]));
577: }
579: t->pinterp = pinterp;
580: PetscMalloc2(s*pinterp,&t->binterpt,s*pinterp,&t->binterp);
581: PetscMemcpy(t->binterpt,binterpt,s*pinterp*sizeof(binterpt[0]));
582: PetscMemcpy(t->binterp,binterp ? binterp : binterpt,s*pinterp*sizeof(binterpt[0]));
583: link->next = ARKTableauList;
584: ARKTableauList = link;
585: return(0);
586: }
590: /*
591: The step completion formula is
593: x1 = x0 - h bt^T YdotI + h b^T YdotRHS
595: This function can be called before or after ts->vec_sol has been updated.
596: Suppose we have a completion formula (bt,b) and an embedded formula (bet,be) of different order.
597: We can write
599: x1e = x0 - h bet^T YdotI + h be^T YdotRHS
600: = x1 + h bt^T YdotI - h b^T YdotRHS - h bet^T YdotI + h be^T YdotRHS
601: = x1 - h (bet - bt)^T YdotI + h (be - b)^T YdotRHS
603: so we can evaluate the method with different order even after the step has been optimistically completed.
604: */
605: static PetscErrorCode TSEvaluateStep_ARKIMEX(TS ts,PetscInt order,Vec X,PetscBool *done)
606: {
607: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
608: ARKTableau tab = ark->tableau;
609: PetscScalar *w = ark->work;
610: PetscReal h;
611: PetscInt s = tab->s,j;
615: switch (ark->status) {
616: case TS_STEP_INCOMPLETE:
617: case TS_STEP_PENDING:
618: h = ts->time_step; break;
619: case TS_STEP_COMPLETE:
620: h = ts->ptime - ts->ptime_prev; break;
621: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
622: }
623: if (order == tab->order) {
624: if (ark->status == TS_STEP_INCOMPLETE) {
625: if (!ark->imex && tab->stiffly_accurate) { /* Only the stiffly accurate implicit formula is used */
626: VecCopy(ark->Y[s-1],X);
627: } else { /* Use the standard completion formula (bt,b) */
628: VecCopy(ts->vec_sol,X);
629: for (j=0; j<s; j++) w[j] = h*tab->bt[j];
630: VecMAXPY(X,s,w,ark->YdotI);
631: if (ark->imex) { /* Method is IMEX, complete the explicit formula */
632: for (j=0; j<s; j++) w[j] = h*tab->b[j];
633: VecMAXPY(X,s,w,ark->YdotRHS);
634: }
635: }
636: } else {VecCopy(ts->vec_sol,X);}
637: if (done) *done = PETSC_TRUE;
638: return(0);
639: } else if (order == tab->order-1) {
640: if (!tab->bembedt) goto unavailable;
641: if (ark->status == TS_STEP_INCOMPLETE) { /* Complete with the embedded method (bet,be) */
642: VecCopy(ts->vec_sol,X);
643: for (j=0; j<s; j++) w[j] = h*tab->bembedt[j];
644: VecMAXPY(X,s,w,ark->YdotI);
645: for (j=0; j<s; j++) w[j] = h*tab->bembed[j];
646: VecMAXPY(X,s,w,ark->YdotRHS);
647: } else { /* Rollback and re-complete using (bet-be,be-b) */
648: VecCopy(ts->vec_sol,X);
649: for (j=0; j<s; j++) w[j] = h*(tab->bembedt[j] - tab->bt[j]);
650: VecMAXPY(X,tab->s,w,ark->YdotI);
651: for (j=0; j<s; j++) w[j] = h*(tab->bembed[j] - tab->b[j]);
652: VecMAXPY(X,s,w,ark->YdotRHS);
653: }
654: if (done) *done = PETSC_TRUE;
655: return(0);
656: }
657: unavailable:
658: if (done) *done = PETSC_FALSE;
659: 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);
660: return(0);
661: }
665: static PetscErrorCode TSRollBack_ARKIMEX(TS ts)
666: {
667: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
668: ARKTableau tab = ark->tableau;
669: const PetscInt s = tab->s;
670: const PetscReal *bt = tab->bt,*b = tab->b;
671: PetscScalar *w = ark->work;
672: Vec *YdotI = ark->YdotI,*YdotRHS = ark->YdotRHS;
673: PetscInt j;
674: PetscReal h;
675: PetscErrorCode ierr;
678: switch (ark->status) {
679: case TS_STEP_INCOMPLETE:
680: case TS_STEP_PENDING:
681: h = ts->time_step; break;
682: case TS_STEP_COMPLETE:
683: h = ts->ptime - ts->ptime_prev; break;
684: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
685: }
686: for (j=0; j<s; j++) w[j] = -h*bt[j];
687: VecMAXPY(ts->vec_sol,s,w,YdotI);
688: for (j=0; j<s; j++) w[j] = -h*b[j];
689: VecMAXPY(ts->vec_sol,s,w,YdotRHS);
690: return(0);
691: }
695: static PetscErrorCode TSStep_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 *At = tab->At,*A = tab->A,*ct = tab->ct,*c = tab->c;
701: PetscScalar *w = ark->work;
702: Vec *Y = ark->Y,*YdotI = ark->YdotI,*YdotRHS = ark->YdotRHS,Ydot = ark->Ydot,Ydot0 = ark->Ydot0,Z = ark->Z;
703: PetscBool extrapolate = ark->extrapolate;
704: TSAdapt adapt;
705: SNES snes;
706: PetscInt i,j,its,lits;
707: PetscInt rejections = 0;
708: PetscBool stageok,accept = PETSC_TRUE;
709: PetscReal next_time_step = ts->time_step;
710: PetscErrorCode ierr;
713: if (ark->extrapolate && !ark->Y_prev) {
714: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y_prev);
715: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI_prev);
716: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS_prev);
717: }
719: if (!ts->steprollback) {
720: if (ts->equation_type >= TS_EQ_IMPLICIT) { /* Save the initial slope for the next step */
721: VecCopy(YdotI[s-1],Ydot0);
722: }
723: if (ark->extrapolate && !ts->steprestart) { /* Save the Y, YdotI, YdotRHS for extrapolation initial guess */
724: for (i = 0; i<s; i++) {
725: VecCopy(Y[i],ark->Y_prev[i]);
726: VecCopy(YdotRHS[i],ark->YdotRHS_prev[i]);
727: VecCopy(YdotI[i],ark->YdotI_prev[i]);
728: }
729: }
730: }
732: if (ts->equation_type >= TS_EQ_IMPLICIT && tab->explicit_first_stage && ts->steprestart) {
733: TS ts_start;
734: TSClone(ts,&ts_start);
735: TSSetSolution(ts_start,ts->vec_sol);
736: TSSetTime(ts_start,ts->ptime);
737: TSSetDuration(ts_start,1,ts->ptime+ts->time_step);
738: TSSetExactFinalTime(ts_start,TS_EXACTFINALTIME_STEPOVER);
739: TSSetTimeStep(ts_start,ts->time_step);
740: TSSetType(ts_start,TSARKIMEX);
741: TSARKIMEXSetFullyImplicit(ts_start,PETSC_TRUE);
742: TSARKIMEXSetType(ts_start,TSARKIMEX1BEE);
744: TSSolve(ts_start,ts->vec_sol);
745: TSGetTime(ts_start,&ts->ptime);
746: TSGetTimeStep(ts_start,&ts->time_step);
748: { /* Save the initial slope for the next step */
749: TS_ARKIMEX *ark_start = (TS_ARKIMEX*)ts_start->data;
750: VecCopy(ark_start->YdotI[ark_start->tableau->s-1],Ydot0);
751: }
752: ts->steps++;
753: ts->total_steps++;
755: /* Set the correct TS in SNES */
756: /* We'll try to bypass this by changing the method on the fly */
757: {
758: TSGetSNES(ts,&snes);
759: TSSetSNES(ts,snes);
760: }
761: TSDestroy(&ts_start);
762: }
764: ark->status = TS_STEP_INCOMPLETE;
765: while (!ts->reason && ark->status != TS_STEP_COMPLETE) {
766: PetscReal t = ts->ptime;
767: PetscReal h = ts->time_step;
768: for (i=0; i<s; i++) {
769: ark->stage_time = t + h*ct[i];
770: TSPreStage(ts,ark->stage_time);
771: if (At[i*s+i] == 0) { /* This stage is explicit */
772: 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");
773: VecCopy(ts->vec_sol,Y[i]);
774: for (j=0; j<i; j++) w[j] = h*At[i*s+j];
775: VecMAXPY(Y[i],i,w,YdotI);
776: for (j=0; j<i; j++) w[j] = h*A[i*s+j];
777: VecMAXPY(Y[i],i,w,YdotRHS);
778: } else {
779: ark->scoeff = 1./At[i*s+i];
780: /* Ydot = shift*(Y-Z) */
781: VecCopy(ts->vec_sol,Z);
782: for (j=0; j<i; j++) w[j] = h*At[i*s+j];
783: VecMAXPY(Z,i,w,YdotI);
784: for (j=0; j<i; j++) w[j] = h*A[i*s+j];
785: VecMAXPY(Z,i,w,YdotRHS);
786: if (extrapolate && !ts->steprestart) {
787: /* Initial guess extrapolated from previous time step stage values */
788: TSExtrapolate_ARKIMEX(ts,c[i],Y[i]);
789: } else {
790: /* Initial guess taken from last stage */
791: VecCopy(i>0 ? Y[i-1] : ts->vec_sol,Y[i]);
792: }
793: TSGetSNES(ts,&snes);
794: SNESSolve(snes,NULL,Y[i]);
795: SNESGetIterationNumber(snes,&its);
796: SNESGetLinearSolveIterations(snes,&lits);
797: ts->snes_its += its; ts->ksp_its += lits;
798: TSGetAdapt(ts,&adapt);
799: TSAdaptCheckStage(adapt,ts,ark->stage_time,Y[i],&stageok);
800: if (!stageok) {
801: /* We are likely rejecting the step because of solver or function domain problems so we should not attempt to
802: * use extrapolation to initialize the solves on the next attempt. */
803: extrapolate = PETSC_FALSE;
804: goto reject_step;
805: }
806: }
807: if (ts->equation_type >= TS_EQ_IMPLICIT) {
808: if (i==0 && tab->explicit_first_stage) {
809: 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);
810: VecCopy(Ydot0,YdotI[0]); /* YdotI = YdotI(tn-1) */
811: } else {
812: VecAXPBYPCZ(YdotI[i],-ark->scoeff/h,ark->scoeff/h,0,Z,Y[i]); /* YdotI = shift*(X-Z) */
813: }
814: } else {
815: if (i==0 && tab->explicit_first_stage) {
816: VecZeroEntries(Ydot);
817: TSComputeIFunction(ts,t+h*ct[i],Y[i],Ydot,YdotI[i],ark->imex);/* YdotI = -G(t,Y,0) */
818: VecScale(YdotI[i],-1.0);
819: } else {
820: VecAXPBYPCZ(YdotI[i],-ark->scoeff/h,ark->scoeff/h,0,Z,Y[i]); /* YdotI = shift*(X-Z) */
821: }
822: if (ark->imex) {
823: TSComputeRHSFunction(ts,t+h*c[i],Y[i],YdotRHS[i]);
824: } else {
825: VecZeroEntries(YdotRHS[i]);
826: }
827: }
828: TSPostStage(ts,ark->stage_time,i,Y);
829: }
831: ark->status = TS_STEP_INCOMPLETE;
832: TSEvaluateStep_ARKIMEX(ts,tab->order,ts->vec_sol,NULL);
833: ark->status = TS_STEP_PENDING;
834: TSGetAdapt(ts,&adapt);
835: TSAdaptCandidatesClear(adapt);
836: TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,(PetscReal)tab->s,PETSC_TRUE);
837: TSAdaptChoose(adapt,ts,ts->time_step,NULL,&next_time_step,&accept);
838: ark->status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
839: if (!accept) { /* Roll back the current step */
840: TSRollBack_ARKIMEX(ts);
841: ts->time_step = next_time_step;
842: goto reject_step;
843: }
845: ts->ptime += ts->time_step;
846: ts->time_step = next_time_step;
847: break;
849: reject_step:
850: ts->reject++; accept = PETSC_FALSE;
851: if (!ts->reason && ++rejections > ts->max_reject && ts->max_reject >= 0) {
852: ts->reason = TS_DIVERGED_STEP_REJECTED;
853: PetscInfo2(ts,"Step=%D, step rejections %D greater than current TS allowed, stopping solve\n",ts->steps,rejections);
854: }
855: }
856: return(0);
857: }
861: static PetscErrorCode TSInterpolate_ARKIMEX(TS ts,PetscReal itime,Vec X)
862: {
863: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
864: PetscInt s = ark->tableau->s,pinterp = ark->tableau->pinterp,i,j;
865: PetscReal h;
866: PetscReal tt,t;
867: PetscScalar *bt,*b;
868: const PetscReal *Bt = ark->tableau->binterpt,*B = ark->tableau->binterp;
869: PetscErrorCode ierr;
872: if (!Bt || !B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSARKIMEX %s does not have an interpolation formula",ark->tableau->name);
873: switch (ark->status) {
874: case TS_STEP_INCOMPLETE:
875: case TS_STEP_PENDING:
876: h = ts->time_step;
877: t = (itime - ts->ptime)/h;
878: break;
879: case TS_STEP_COMPLETE:
880: h = ts->ptime - ts->ptime_prev;
881: t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
882: break;
883: default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
884: }
885: PetscMalloc2(s,&bt,s,&b);
886: for (i=0; i<s; i++) bt[i] = b[i] = 0;
887: for (j=0,tt=t; j<pinterp; j++,tt*=t) {
888: for (i=0; i<s; i++) {
889: bt[i] += h * Bt[i*pinterp+j] * tt;
890: b[i] += h * B[i*pinterp+j] * tt;
891: }
892: }
893: VecCopy(ark->Y[0],X);
894: VecMAXPY(X,s,bt,ark->YdotI);
895: VecMAXPY(X,s,b,ark->YdotRHS);
896: PetscFree2(bt,b);
897: return(0);
898: }
902: static PetscErrorCode TSExtrapolate_ARKIMEX(TS ts,PetscReal c,Vec X)
903: {
904: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
905: PetscInt s = ark->tableau->s,pinterp = ark->tableau->pinterp,i,j;
906: PetscReal h,h_prev,t,tt;
907: PetscScalar *bt,*b;
908: const PetscReal *Bt = ark->tableau->binterpt,*B = ark->tableau->binterp;
909: PetscErrorCode ierr;
912: if (!Bt || !B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSARKIMEX %s does not have an interpolation formula",ark->tableau->name);
913: PetscCalloc2(s,&bt,s,&b);
914: h = ts->time_step;
915: h_prev = ts->ptime - ts->ptime_prev;
916: t = 1 + h/h_prev*c;
917: for (j=0,tt=t; j<pinterp; j++,tt*=t) {
918: for (i=0; i<s; i++) {
919: bt[i] += h * Bt[i*pinterp+j] * tt;
920: b[i] += h * B[i*pinterp+j] * tt;
921: }
922: }
923: if (!ark->Y_prev) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Stages from previous step have not been stored");
924: VecCopy(ark->Y_prev[0],X);
925: VecMAXPY(X,s,bt,ark->YdotI_prev);
926: VecMAXPY(X,s,b,ark->YdotRHS_prev);
927: PetscFree2(bt,b);
928: return(0);
929: }
931: /*------------------------------------------------------------*/
935: static PetscErrorCode TSARKIMEXTableauReset(TS ts)
936: {
937: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
938: ARKTableau tab = ark->tableau;
942: if (!tab) return(0);
943: PetscFree(ark->work);
944: VecDestroyVecs(tab->s,&ark->Y);
945: VecDestroyVecs(tab->s,&ark->YdotI);
946: VecDestroyVecs(tab->s,&ark->YdotRHS);
947: VecDestroyVecs(tab->s,&ark->Y_prev);
948: VecDestroyVecs(tab->s,&ark->YdotI_prev);
949: VecDestroyVecs(tab->s,&ark->YdotRHS_prev);
950: return(0);
951: }
955: static PetscErrorCode TSReset_ARKIMEX(TS ts)
956: {
957: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
961: TSARKIMEXTableauReset(ts);
962: VecDestroy(&ark->Ydot);
963: VecDestroy(&ark->Ydot0);
964: VecDestroy(&ark->Z);
965: return(0);
966: }
970: static PetscErrorCode TSDestroy_ARKIMEX(TS ts)
971: {
975: TSReset_ARKIMEX(ts);
976: PetscFree(ts->data);
977: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetType_C",NULL);
978: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetType_C",NULL);
979: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",NULL);
980: return(0);
981: }
986: static PetscErrorCode TSARKIMEXGetVecs(TS ts,DM dm,Vec *Z,Vec *Ydot)
987: {
988: TS_ARKIMEX *ax = (TS_ARKIMEX*)ts->data;
992: if (Z) {
993: if (dm && dm != ts->dm) {
994: DMGetNamedGlobalVector(dm,"TSARKIMEX_Z",Z);
995: } else *Z = ax->Z;
996: }
997: if (Ydot) {
998: if (dm && dm != ts->dm) {
999: DMGetNamedGlobalVector(dm,"TSARKIMEX_Ydot",Ydot);
1000: } else *Ydot = ax->Ydot;
1001: }
1002: return(0);
1003: }
1008: static PetscErrorCode TSARKIMEXRestoreVecs(TS ts,DM dm,Vec *Z,Vec *Ydot)
1009: {
1013: if (Z) {
1014: if (dm && dm != ts->dm) {
1015: DMRestoreNamedGlobalVector(dm,"TSARKIMEX_Z",Z);
1016: }
1017: }
1018: if (Ydot) {
1019: if (dm && dm != ts->dm) {
1020: DMRestoreNamedGlobalVector(dm,"TSARKIMEX_Ydot",Ydot);
1021: }
1022: }
1023: return(0);
1024: }
1026: /*
1027: This defines the nonlinear equation that is to be solved with SNES
1028: G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
1029: */
1032: static PetscErrorCode SNESTSFormFunction_ARKIMEX(SNES snes,Vec X,Vec F,TS ts)
1033: {
1034: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1035: DM dm,dmsave;
1036: Vec Z,Ydot;
1037: PetscReal shift = ark->scoeff / ts->time_step;
1041: SNESGetDM(snes,&dm);
1042: TSARKIMEXGetVecs(ts,dm,&Z,&Ydot);
1043: VecAXPBYPCZ(Ydot,-shift,shift,0,Z,X); /* Ydot = shift*(X-Z) */
1044: dmsave = ts->dm;
1045: ts->dm = dm;
1047: TSComputeIFunction(ts,ark->stage_time,X,Ydot,F,ark->imex);
1049: ts->dm = dmsave;
1050: TSARKIMEXRestoreVecs(ts,dm,&Z,&Ydot);
1051: return(0);
1052: }
1056: static PetscErrorCode SNESTSFormJacobian_ARKIMEX(SNES snes,Vec X,Mat A,Mat B,TS ts)
1057: {
1058: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1059: DM dm,dmsave;
1060: Vec Ydot;
1061: PetscReal shift = ark->scoeff / ts->time_step;
1065: SNESGetDM(snes,&dm);
1066: TSARKIMEXGetVecs(ts,dm,NULL,&Ydot);
1067: /* ark->Ydot has already been computed in SNESTSFormFunction_ARKIMEX (SNES guarantees this) */
1068: dmsave = ts->dm;
1069: ts->dm = dm;
1071: TSComputeIJacobian(ts,ark->stage_time,X,Ydot,shift,A,B,ark->imex);
1073: ts->dm = dmsave;
1074: TSARKIMEXRestoreVecs(ts,dm,NULL,&Ydot);
1075: return(0);
1076: }
1080: static PetscErrorCode DMCoarsenHook_TSARKIMEX(DM fine,DM coarse,void *ctx)
1081: {
1083: return(0);
1084: }
1088: static PetscErrorCode DMRestrictHook_TSARKIMEX(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
1089: {
1090: TS ts = (TS)ctx;
1092: Vec Z,Z_c;
1095: TSARKIMEXGetVecs(ts,fine,&Z,NULL);
1096: TSARKIMEXGetVecs(ts,coarse,&Z_c,NULL);
1097: MatRestrict(restrct,Z,Z_c);
1098: VecPointwiseMult(Z_c,rscale,Z_c);
1099: TSARKIMEXRestoreVecs(ts,fine,&Z,NULL);
1100: TSARKIMEXRestoreVecs(ts,coarse,&Z_c,NULL);
1101: return(0);
1102: }
1107: static PetscErrorCode DMSubDomainHook_TSARKIMEX(DM dm,DM subdm,void *ctx)
1108: {
1110: return(0);
1111: }
1115: static PetscErrorCode DMSubDomainRestrictHook_TSARKIMEX(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
1116: {
1117: TS ts = (TS)ctx;
1119: Vec Z,Z_c;
1122: TSARKIMEXGetVecs(ts,dm,&Z,NULL);
1123: TSARKIMEXGetVecs(ts,subdm,&Z_c,NULL);
1125: VecScatterBegin(gscat,Z,Z_c,INSERT_VALUES,SCATTER_FORWARD);
1126: VecScatterEnd(gscat,Z,Z_c,INSERT_VALUES,SCATTER_FORWARD);
1128: TSARKIMEXRestoreVecs(ts,dm,&Z,NULL);
1129: TSARKIMEXRestoreVecs(ts,subdm,&Z_c,NULL);
1130: return(0);
1131: }
1135: static PetscErrorCode TSARKIMEXTableauSetUp(TS ts)
1136: {
1137: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1138: ARKTableau tab = ark->tableau;
1142: PetscMalloc1(tab->s,&ark->work);
1143: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y);
1144: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI);
1145: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS);
1146: if (ark->extrapolate) {
1147: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->Y_prev);
1148: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotI_prev);
1149: VecDuplicateVecs(ts->vec_sol,tab->s,&ark->YdotRHS_prev);
1150: }
1151: return(0);
1152: }
1156: static PetscErrorCode TSSetUp_ARKIMEX(TS ts)
1157: {
1158: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1160: DM dm;
1161: SNES snes;
1164: TSARKIMEXTableauSetUp(ts);
1165: VecDuplicate(ts->vec_sol,&ark->Ydot);
1166: VecDuplicate(ts->vec_sol,&ark->Ydot0);
1167: VecDuplicate(ts->vec_sol,&ark->Z);
1168: TSGetDM(ts,&dm);
1169: if (dm) {
1170: DMCoarsenHookAdd(dm,DMCoarsenHook_TSARKIMEX,DMRestrictHook_TSARKIMEX,ts);
1171: DMSubDomainHookAdd(dm,DMSubDomainHook_TSARKIMEX,DMSubDomainRestrictHook_TSARKIMEX,ts);
1172: }
1173: TSGetSNES(ts,&snes);
1174: return(0);
1175: }
1176: /*------------------------------------------------------------*/
1180: static PetscErrorCode TSSetFromOptions_ARKIMEX(PetscOptionItems *PetscOptionsObject,TS ts)
1181: {
1182: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1186: PetscOptionsHead(PetscOptionsObject,"ARKIMEX ODE solver options");
1187: {
1188: ARKTableauLink link;
1189: PetscInt count,choice;
1190: PetscBool flg;
1191: const char **namelist;
1192: for (link=ARKTableauList,count=0; link; link=link->next,count++) ;
1193: PetscMalloc1(count,&namelist);
1194: for (link=ARKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
1195: PetscOptionsEList("-ts_arkimex_type","Family of ARK IMEX method","TSARKIMEXSetType",(const char*const*)namelist,count,ark->tableau->name,&choice,&flg);
1196: if (flg) {TSARKIMEXSetType(ts,namelist[choice]);}
1197: PetscFree(namelist);
1199: flg = (PetscBool) !ark->imex;
1200: PetscOptionsBool("-ts_arkimex_fully_implicit","Solve the problem fully implicitly","TSARKIMEXSetFullyImplicit",flg,&flg,NULL);
1201: ark->imex = (PetscBool) !flg;
1202: 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);
1203: }
1204: PetscOptionsTail();
1205: return(0);
1206: }
1210: static PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[])
1211: {
1213: PetscInt i;
1214: size_t left,count;
1215: char *p;
1218: for (i=0,p=buf,left=len; i<n; i++) {
1219: PetscSNPrintfCount(p,left,fmt,&count,(double)x[i]);
1220: if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer");
1221: left -= count;
1222: p += count;
1223: *p++ = ' ';
1224: }
1225: p[i ? 0 : -1] = 0;
1226: return(0);
1227: }
1231: static PetscErrorCode TSView_ARKIMEX(TS ts,PetscViewer viewer)
1232: {
1233: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1234: PetscBool iascii;
1238: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1239: if (iascii) {
1240: ARKTableau tab = ark->tableau;
1241: TSARKIMEXType arktype;
1242: char buf[512];
1243: TSARKIMEXGetType(ts,&arktype);
1244: PetscViewerASCIIPrintf(viewer," ARK IMEX %s\n",arktype);
1245: PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->ct);
1246: PetscViewerASCIIPrintf(viewer," Stiff abscissa ct = %s\n",buf);
1247: PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->c);
1248: PetscViewerASCIIPrintf(viewer,"Stiffly accurate: %s\n",tab->stiffly_accurate ? "yes" : "no");
1249: PetscViewerASCIIPrintf(viewer,"Explicit first stage: %s\n",tab->explicit_first_stage ? "yes" : "no");
1250: PetscViewerASCIIPrintf(viewer,"FSAL property: %s\n",tab->FSAL_implicit ? "yes" : "no");
1251: PetscViewerASCIIPrintf(viewer," Nonstiff abscissa c = %s\n",buf);
1252: }
1253: if (ts->adapt) {TSAdaptView(ts->adapt,viewer);}
1254: if (ts->snes) {SNESView(ts->snes,viewer);}
1255: return(0);
1256: }
1260: static PetscErrorCode TSLoad_ARKIMEX(TS ts,PetscViewer viewer)
1261: {
1263: SNES snes;
1264: TSAdapt adapt;
1267: TSGetAdapt(ts,&adapt);
1268: TSAdaptLoad(adapt,viewer);
1269: TSGetSNES(ts,&snes);
1270: SNESLoad(snes,viewer);
1271: /* function and Jacobian context for SNES when used with TS is always ts object */
1272: SNESSetFunction(snes,NULL,NULL,ts);
1273: SNESSetJacobian(snes,NULL,NULL,NULL,ts);
1274: return(0);
1275: }
1279: /*@C
1280: TSARKIMEXSetType - Set the type of ARK IMEX scheme
1282: Logically collective
1284: Input Parameter:
1285: + ts - timestepping context
1286: - arktype - type of ARK-IMEX scheme
1288: Level: intermediate
1290: .seealso: TSARKIMEXGetType(), TSARKIMEX, TSARKIMEX2D, TSARKIMEX2E, TSARKIMEXPRSSP2, TSARKIMEX3, TSARKIMEXBPR3, TSARKIMEXARS443, TSARKIMEX4, TSARKIMEX5
1291: @*/
1292: PetscErrorCode TSARKIMEXSetType(TS ts,TSARKIMEXType arktype)
1293: {
1298: PetscTryMethod(ts,"TSARKIMEXSetType_C",(TS,TSARKIMEXType),(ts,arktype));
1299: return(0);
1300: }
1304: /*@C
1305: TSARKIMEXGetType - Get the type of ARK IMEX scheme
1307: Logically collective
1309: Input Parameter:
1310: . ts - timestepping context
1312: Output Parameter:
1313: . arktype - type of ARK-IMEX scheme
1315: Level: intermediate
1317: .seealso: TSARKIMEXGetType()
1318: @*/
1319: PetscErrorCode TSARKIMEXGetType(TS ts,TSARKIMEXType *arktype)
1320: {
1325: PetscUseMethod(ts,"TSARKIMEXGetType_C",(TS,TSARKIMEXType*),(ts,arktype));
1326: return(0);
1327: }
1331: /*@
1332: TSARKIMEXSetFullyImplicit - Solve both parts of the equation implicitly
1334: Logically collective
1336: Input Parameter:
1337: + ts - timestepping context
1338: - flg - PETSC_TRUE for fully implicit
1340: Level: intermediate
1342: .seealso: TSARKIMEXGetType()
1343: @*/
1344: PetscErrorCode TSARKIMEXSetFullyImplicit(TS ts,PetscBool flg)
1345: {
1350: PetscTryMethod(ts,"TSARKIMEXSetFullyImplicit_C",(TS,PetscBool),(ts,flg));
1351: return(0);
1352: }
1356: static PetscErrorCode TSARKIMEXGetType_ARKIMEX(TS ts,TSARKIMEXType *arktype)
1357: {
1358: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1361: *arktype = ark->tableau->name;
1362: return(0);
1363: }
1366: static PetscErrorCode TSARKIMEXSetType_ARKIMEX(TS ts,TSARKIMEXType arktype)
1367: {
1368: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1370: PetscBool match;
1371: ARKTableauLink link;
1374: if (ark->tableau) {
1375: PetscStrcmp(ark->tableau->name,arktype,&match);
1376: if (match) return(0);
1377: }
1378: for (link = ARKTableauList; link; link=link->next) {
1379: PetscStrcmp(link->tab.name,arktype,&match);
1380: if (match) {
1381: if (ts->setupcalled) {TSARKIMEXTableauReset(ts);}
1382: ark->tableau = &link->tab;
1383: if (ts->setupcalled) {TSARKIMEXTableauSetUp(ts);}
1384: return(0);
1385: }
1386: }
1387: SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",arktype);
1388: return(0);
1389: }
1393: static PetscErrorCode TSARKIMEXSetFullyImplicit_ARKIMEX(TS ts,PetscBool flg)
1394: {
1395: TS_ARKIMEX *ark = (TS_ARKIMEX*)ts->data;
1398: ark->imex = (PetscBool)!flg;
1399: return(0);
1400: }
1402: /* ------------------------------------------------------------ */
1403: /*MC
1404: TSARKIMEX - ODE and DAE solver using Additive Runge-Kutta IMEX schemes
1406: These methods are intended for problems with well-separated time scales, especially when a slow scale is strongly
1407: nonlinear such that it is expensive to solve with a fully implicit method. The user should provide the stiff part
1408: of the equation using TSSetIFunction() and the non-stiff part with TSSetRHSFunction().
1410: Notes:
1411: The default is TSARKIMEX3, it can be changed with TSARKIMEXSetType() or -ts_arkimex_type
1413: If the equation is implicit or a DAE, then TSSetEquationType() needs to be set accordingly. Refer to the manual for further information.
1415: 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).
1417: Consider trying TSROSW if the stiff part is linear or weakly nonlinear.
1419: Level: beginner
1421: .seealso: TSCreate(), TS, TSSetType(), TSARKIMEXSetType(), TSARKIMEXGetType(), TSARKIMEXSetFullyImplicit(), TSARKIMEX1BEE,
1422: TSARKIMEX2C, TSARKIMEX2D, TSARKIMEX2E, TSARKIMEX3, TSARKIMEXL2, TSARKIMEXA2, TSARKIMEXARS122,
1423: TSARKIMEX4, TSARKIMEX5, TSARKIMEXPRSSP2, TSARKIMEXARS443, TSARKIMEXBPR3, TSARKIMEXType, TSARKIMEXRegister()
1425: M*/
1428: PETSC_EXTERN PetscErrorCode TSCreate_ARKIMEX(TS ts)
1429: {
1430: TS_ARKIMEX *th;
1434: TSARKIMEXInitializePackage();
1436: ts->ops->reset = TSReset_ARKIMEX;
1437: ts->ops->destroy = TSDestroy_ARKIMEX;
1438: ts->ops->view = TSView_ARKIMEX;
1439: ts->ops->load = TSLoad_ARKIMEX;
1440: ts->ops->setup = TSSetUp_ARKIMEX;
1441: ts->ops->step = TSStep_ARKIMEX;
1442: ts->ops->interpolate = TSInterpolate_ARKIMEX;
1443: ts->ops->evaluatestep = TSEvaluateStep_ARKIMEX;
1444: ts->ops->rollback = TSRollBack_ARKIMEX;
1445: ts->ops->setfromoptions = TSSetFromOptions_ARKIMEX;
1446: ts->ops->snesfunction = SNESTSFormFunction_ARKIMEX;
1447: ts->ops->snesjacobian = SNESTSFormJacobian_ARKIMEX;
1449: PetscNewLog(ts,&th);
1450: ts->data = (void*)th;
1451: th->imex = PETSC_TRUE;
1453: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXGetType_C",TSARKIMEXGetType_ARKIMEX);
1454: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetType_C",TSARKIMEXSetType_ARKIMEX);
1455: PetscObjectComposeFunction((PetscObject)ts,"TSARKIMEXSetFullyImplicit_C",TSARKIMEXSetFullyImplicit_ARKIMEX);
1457: TSARKIMEXSetType(ts,TSARKIMEXDefault);
1458: return(0);
1459: }