Actual source code: mprk.c
1: /*
2: Code for time stepping with the Multirate Partitioned Runge-Kutta method
4: Notes:
5: 1) The general system is written as
6: Udot = F(t,U)
7: if one does not split the RHS function, but gives the indexes for both slow and fast components;
8: 2) The general system is written as
9: Usdot = Fs(t,Us,Uf)
10: Ufdot = Ff(t,Us,Uf)
11: for component-wise partitioned system,
12: users should split the RHS function themselves and also provide the indexes for both slow and fast components.
13: 3) To correct The confusing terminology in the paper, we use 'slow method', 'slow buffer method' and 'fast method' to denote the methods applied to 'slow region', 'slow buffer region' and 'fast region' respectively. The 'slow method' in the original paper actually means the 'slow buffer method'.
14: 4) Why does the buffer region have to be inside the slow region? The buffer region is treated with a slow method essentially. Applying the slow method to a region with a fast characteristic time scale is apparently not a good choice.
16: Reference:
17: Emil M. Constantinescu, Adrian Sandu, Multirate Timestepping Methods for Hyperbolic Conservation Laws, Journal of Scientific Computing 2007
18: */
20: #include <petsc/private/tsimpl.h>
21: #include <petscdm.h>
23: static TSMPRKType TSMPRKDefault = TSMPRK2A22;
24: static PetscBool TSMPRKRegisterAllCalled;
25: static PetscBool TSMPRKPackageInitialized;
27: typedef struct _MPRKTableau *MPRKTableau;
28: struct _MPRKTableau {
29: char *name;
30: PetscInt order; /* Classical approximation order of the method i */
31: PetscInt sbase; /* Number of stages in the base method*/
32: PetscInt s; /* Number of stages */
33: PetscInt np; /* Number of partitions */
34: PetscReal *Af,*bf,*cf; /* Tableau for fast components */
35: PetscReal *Amb,*bmb,*cmb; /* Tableau for medium components */
36: PetscInt *rmb; /* Array of flags for repeated stages in medium method */
37: PetscReal *Asb,*bsb,*csb; /* Tableau for slow components */
38: PetscInt *rsb; /* Array of flags for repeated staged in slow method*/
39: };
40: typedef struct _MPRKTableauLink *MPRKTableauLink;
41: struct _MPRKTableauLink {
42: struct _MPRKTableau tab;
43: MPRKTableauLink next;
44: };
45: static MPRKTableauLink MPRKTableauList;
47: typedef struct {
48: MPRKTableau tableau;
49: Vec *Y; /* States computed during the step */
50: Vec *YdotRHS;
51: Vec *YdotRHS_slow; /* Function evaluations by slow tableau for slow components */
52: Vec *YdotRHS_slowbuffer; /* Function evaluations by slow tableau for slow components */
53: Vec *YdotRHS_medium; /* Function evaluations by slow tableau for slow components */
54: Vec *YdotRHS_mediumbuffer; /* Function evaluations by slow tableau for slow components */
55: Vec *YdotRHS_fast; /* Function evaluations by fast tableau for fast components */
56: PetscScalar *work_slow; /* Scalar work_slow by slow tableau */
57: PetscScalar *work_slowbuffer; /* Scalar work_slow by slow tableau */
58: PetscScalar *work_medium; /* Scalar work_slow by medium tableau */
59: PetscScalar *work_mediumbuffer; /* Scalar work_slow by medium tableau */
60: PetscScalar *work_fast; /* Scalar work_fast by fast tableau */
61: PetscReal stage_time;
62: TSStepStatus status;
63: PetscReal ptime;
64: PetscReal time_step;
65: IS is_slow,is_slowbuffer,is_medium,is_mediumbuffer,is_fast;
66: TS subts_slow,subts_slowbuffer,subts_medium,subts_mediumbuffer,subts_fast;
67: } TS_MPRK;
69: static PetscErrorCode TSMPRKGenerateTableau2(PetscInt ratio,PetscInt s,const PetscReal Abase[],const PetscReal bbase[],PetscReal A1[],PetscReal b1[],PetscReal A2[],PetscReal b2[])
70: {
71: PetscInt i,j,k,l;
73: for (k=0; k<ratio; k++) {
74: /* diagonal blocks */
75: for (i=0; i<s; i++)
76: for (j=0; j<s; j++) {
77: A1[(k*s+i)*ratio*s+k*s+j] = Abase[i*s+j];
78: A2[(k*s+i)*ratio*s+k*s+j] = Abase[i*s+j]/ratio;
79: }
80: /* off diagonal blocks */
81: for (l=0; l<k; l++)
82: for (i=0; i<s; i++)
83: for (j=0; j<s; j++)
84: A2[(k*s+i)*ratio*s+l*s+j] = bbase[j]/ratio;
85: for (j=0; j<s; j++) {
86: b1[k*s+j] = bbase[j]/ratio;
87: b2[k*s+j] = bbase[j]/ratio;
88: }
89: }
90: return 0;
91: }
93: static PetscErrorCode TSMPRKGenerateTableau3(PetscInt ratio,PetscInt s,const PetscReal Abase[],const PetscReal bbase[],PetscReal A1[],PetscReal b1[],PetscReal A2[],PetscReal b2[],PetscReal A3[],PetscReal b3[])
94: {
95: PetscInt i,j,k,l,m,n;
97: for (k=0; k<ratio; k++) { /* diagonal blocks of size ratio*s by ratio*s */
98: for (l=0; l<ratio; l++) /* diagonal sub-blocks of size s by s */
99: for (i=0; i<s; i++)
100: for (j=0; j<s; j++) {
101: A1[((k*ratio+l)*s+i)*ratio*ratio*s+(k*ratio+l)*s+j] = Abase[i*s+j];
102: A2[((k*ratio+l)*s+i)*ratio*ratio*s+(k*ratio+l)*s+j] = Abase[i*s+j]/ratio;
103: A3[((k*ratio+l)*s+i)*ratio*ratio*s+(k*ratio+l)*s+j] = Abase[i*s+j]/ratio/ratio;
104: }
105: for (l=0; l<k; l++) /* off-diagonal blocks of size ratio*s by ratio*s */
106: for (m=0; m<ratio; m++)
107: for (n=0; n<ratio; n++)
108: for (i=0; i<s; i++)
109: for (j=0; j<s; j++) {
110: A2[((k*ratio+m)*s+i)*ratio*ratio*s+(l*ratio+n)*s+j] = bbase[j]/ratio/ratio;
111: A3[((k*ratio+m)*s+i)*ratio*ratio*s+(l*ratio+n)*s+j] = bbase[j]/ratio/ratio;
112: }
113: for (m=0; m<ratio; m++)
114: for (n=0; n<m; n++) /* off-diagonal sub-blocks of size s by s in the diagonal blocks */
115: for (i=0; i<s; i++)
116: for (j=0; j<s; j++)
117: A3[((k*ratio+m)*s+i)*ratio*ratio*s+(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
118: for (n=0; n<ratio; n++)
119: for (j=0; j<s; j++) {
120: b1[(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
121: b2[(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
122: b3[(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
123: }
124: }
125: return 0;
126: }
128: /*MC
129: TSMPRK2A22 - Second Order Multirate Partitioned Runge Kutta scheme based on RK2A.
131: This method has four stages for slow and fast parts. The refinement factor of the stepsize is 2.
132: r = 2, np = 2
134: Options database:
135: . -ts_mprk_type 2a22 - select this scheme
137: Level: advanced
139: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
140: M*/
141: /*MC
142: TSMPRK2A23 - Second Order Multirate Partitioned Runge-Kutta scheme based on RK2A.
144: This method has eight stages for slow and medium and fast parts. The refinement factor of the stepsize is 2.
145: r = 2, np = 3
147: Options database:
148: . -ts_mprk_type 2a23 - select this scheme
150: Level: advanced
152: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
153: M*/
154: /*MC
155: TSMPRK2A32 - Second Order Multirate Partitioned Runge-Kutta scheme based on RK2A.
157: This method has four stages for slow and fast parts. The refinement factor of the stepsize is 3.
158: r = 3, np = 2
160: Options database:
161: . -ts_mprk_type 2a32 - select this scheme
163: Level: advanced
165: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
166: M*/
167: /*MC
168: TSMPRK2A33 - Second Order Multirate Partitioned Runge-Kutta scheme based on RK2A.
170: This method has eight stages for slow and medium and fast parts. The refinement factor of the stepsize is 3.
171: r = 3, np = 3
173: Options database:
174: . -ts_mprk_type 2a33- select this scheme
176: Level: advanced
178: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
179: M*/
180: /*MC
181: TSMPRK3P2M - Third Order Multirate Partitioned Runge-Kutta scheme.
183: This method has eight stages for both slow and fast parts.
185: Options database:
186: . -ts_mprk_type pm3 - select this scheme
188: Level: advanced
190: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
191: M*/
192: /*MC
193: TSMPRKP2 - Second Order Multirate Partitioned Runge-Kutta scheme.
195: This method has five stages for both slow and fast parts.
197: Options database:
198: . -ts_mprk_type p2 - select this scheme
200: Level: advanced
202: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
203: M*/
204: /*MC
205: TSMPRKP3 - Third Order Multirate Partitioned Runge-Kutta scheme.
207: This method has ten stages for both slow and fast parts.
209: Options database:
210: . -ts_mprk_type p3 - select this scheme
212: Level: advanced
214: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
215: M*/
217: /*@C
218: TSMPRKRegisterAll - Registers all of the Partirioned Runge-Kutta explicit methods in TSMPRK
220: Not Collective, but should be called by all processes which will need the schemes to be registered
222: Level: advanced
224: .seealso: TSMPRKRegisterDestroy()
225: @*/
226: PetscErrorCode TSMPRKRegisterAll(void)
227: {
228: if (TSMPRKRegisterAllCalled) return 0;
229: TSMPRKRegisterAllCalled = PETSC_TRUE;
231: #define RC PetscRealConstant
232: {
233: const PetscReal
234: Abase[2][2] = {{0,0},
235: {RC(1.0),0}},
236: bbase[2] = {RC(0.5),RC(0.5)};
237: PetscReal
238: Asb[4][4] = {{0}},Af[4][4] = {{0}},bsb[4] = {0},bf[4] = {0};
239: PetscInt
240: rsb[4] = {0,0,1,2};
241: TSMPRKGenerateTableau2(2,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Af[0][0],bf);
242: TSMPRKRegister(TSMPRK2A22,2,2,2,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
243: }
244: {
245: const PetscReal
246: Abase[2][2] = {{0,0},
247: {RC(1.0),0}},
248: bbase[2] = {RC(0.5),RC(0.5)};
249: PetscReal
250: Asb[8][8] = {{0}},Amb[8][8] = {{0}},Af[8][8] = {{0}},bsb[8] ={0},bmb[8] = {0},bf[8] = {0};
251: PetscInt
252: rsb[8] = {0,0,1,2,1,2,1,2},rmb[8] = {0,0,1,2,0,0,5,6};
253: TSMPRKGenerateTableau3(2,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Amb[0][0],bmb,&Af[0][0],bf);
254: TSMPRKRegister(TSMPRK2A23,2,2,2,2,&Asb[0][0],bsb,NULL,rsb,&Amb[0][0],bmb,NULL,rmb,&Af[0][0],bf,NULL);
255: }
256: {
257: const PetscReal
258: Abase[2][2] = {{0,0},
259: {RC(1.0),0}},
260: bbase[2] = {RC(0.5),RC(0.5)};
261: PetscReal
262: Asb[6][6] = {{0}},Af[6][6] = {{0}},bsb[6] = {0},bf[6] = {0};
263: PetscInt
264: rsb[6] = {0,0,1,2,1,2};
265: TSMPRKGenerateTableau2(3,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Af[0][0],bf);
266: TSMPRKRegister(TSMPRK2A32,2,2,3,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
267: }
268: {
269: const PetscReal
270: Abase[2][2] = {{0,0},
271: {RC(1.0),0}},
272: bbase[2] = {RC(0.5),RC(0.5)};
273: PetscReal
274: Asb[18][18] = {{0}},Amb[18][18] = {{0}},Af[18][18] = {{0}},bsb[18] ={0},bmb[18] = {0},bf[18] = {0};
275: PetscInt
276: rsb[18] = {0,0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2},rmb[18] = {0,0,1,2,1,2,0,0,7,8,7,8,0,0,13,14,13,14};
277: TSMPRKGenerateTableau3(3,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Amb[0][0],bmb,&Af[0][0],bf);
278: TSMPRKRegister(TSMPRK2A33,2,2,3,3,&Asb[0][0],bsb,NULL,rsb,&Amb[0][0],bmb,NULL,rmb,&Af[0][0],bf,NULL);
279: }
280: /*
281: PetscReal
282: Asb[8][8] = {{Abase[0][0],Abase[0][1],0,0,0,0,0,0},
283: {Abase[1][0],Abase[1][1],0,0,0,0,0,0},
284: {0,0,Abase[0][0],Abase[0][1],0,0,0,0},
285: {0,0,Abase[1][0],Abase[1][1],0,0,0,0},
286: {0,0,0,0,Abase[0][0],Abase[0][1],0,0},
287: {0,0,0,0,Abase[1][0],Abase[1][1],0,0},
288: {0,0,0,0,0,0,Abase[0][0],Abase[0][1]},
289: {0,0,0,0,0,0,Abase[1][0],Abase[1][1]}},
290: Amb[8][8] = {{Abase[0][0]/m,Abase[0][1]/m,0,0,0,0,0,0},
291: {Abase[1][0]/m,Abase[1][1]/m,0,0,0,0,0,0},
292: {0,0,Abase[0][0]/m,Abase[0][1]/m,0,0,0,0},
293: {0,0,Abase[1][0]/m,Abase[1][1]/m,0,0,0,0},
294: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,Abase[0][0]/m,Abase[0][1]/m,0,0},
295: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,Abase[1][0]/m,Abase[1][1]/m,0,0},
296: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,0,0,Abase[0][0]/m,Abase[0][1]/m},
297: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,0,0,Abase[1][0]/m,Abase[1][1]/m}},
298: Af[8][8] = {{Abase[0][0]/m/m,Abase[0][1]/m/m,0,0,0,0,0,0},
299: {Abase[1][0]/m/m,Abase[1][1]/m/m,0,0,0,0,0,0},
300: {0,0,Abase[0][0]/m/m,Abase[0][1]/m/m,0,0,0,0},
301: {0,0,Abase[1][0]/m/m,Abase[1][1]/m/m,0,0,0,0},
302: {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,Abase[0][0]/m/m,Abase[0][1]/m/m,0,0},
303: {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,Abase[1][0]/m/m,Abase[1][1]/m/m,0,0},
304: {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,Abase[0][0]/m,Abase[0][1]/m},
305: {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,Abase[1][0]/m,Abase[1][1]/m}},
306: bsb[8] = {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m},
307: bmb[8] = {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m,bbase[1]/m/m},
308: bf[8] = {bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m/m,bbase[0]/m/m,bbase[1]/m,bbase[0]/m/m,bbase[1]/m/m},
309: */
310: /*{
311: const PetscReal
312: As[8][8] = {{0,0,0,0,0,0,0,0},
313: {RC(1.0)/RC(2.0),0,0,0,0,0,0,0},
314: {RC(-1.0)/RC(6.0),RC(2.0)/RC(3.0),0,0,0,0,0,0},
315: {RC(1.0)/RC(3.0),RC(-1.0)/RC(3.0),RC(1.0),0,0,0,0,0},
316: {0,0,0,0,0,0,0,0},
317: {0,0,0,0,RC(1.0)/RC(2.0),0,0,0},
318: {0,0,0,0,RC(-1.0)/RC(6.0),RC(2.0)/RC(3.0),0,0},
319: {0,0,0,0,RC(1.0)/RC(3.0),RC(-1.0)/RC(3.0),RC(1.0),0}},
320: A[8][8] = {{0,0,0,0,0,0,0,0},
321: {RC(1.0)/RC(4.0),0,0,0,0,0,0,0},
322: {RC(-1.0)/RC(12.0),RC(1.0)/RC(3.0),0,0,0,0,0,0},
323: {RC(1.0)/RC(6.0),RC(-1.0)/RC(6.0),RC(1.0)/RC(2.0),0,0,0,0,0},
324: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,0,0,0},
325: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),RC(1.0)/RC(4.0),0,0,0},
326: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),RC(-1.0)/RC(12.0),RC(1.0)/RC(3.0),0,0},
327: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(-1.0)/RC(6.0),RC(1.0)/RC(2.0),0}},
328: bs[8] = {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0)},
329: b[8] = {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0)};
330: TSMPRKRegister(TSMPRKPM3,3,8,&As[0][0],bs,NULL,&A[0][0],b,NULL);
331: }*/
333: {
334: const PetscReal
335: Asb[5][5] = {{0,0,0,0,0},
336: {RC(1.0)/RC(2.0),0,0,0,0},
337: {RC(1.0)/RC(2.0),0,0,0,0},
338: {RC(1.0),0,0,0,0},
339: {RC(1.0),0,0,0,0}},
340: Af[5][5] = {{0,0,0,0,0},
341: {RC(1.0)/RC(2.0),0,0,0,0},
342: {RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),0,0,0},
343: {RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),RC(1.0)/RC(2.0),0,0},
344: {RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),0}},
345: bsb[5] = {RC(1.0)/RC(2.0),0,0,0,RC(1.0)/RC(2.0)},
346: bf[5] = {RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),0};
347: const PetscInt
348: rsb[5] = {0,0,2,0,4};
349: TSMPRKRegister(TSMPRKP2,2,5,1,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
350: }
352: {
353: const PetscReal
354: Asb[10][10] = {{0,0,0,0,0,0,0,0,0,0},
355: {RC(1.0)/RC(4.0),0,0,0,0,0,0,0,0,0},
356: {RC(1.0)/RC(4.0),0,0,0,0,0,0,0,0,0},
357: {RC(1.0)/RC(2.0),0,0,0,0,0,0,0,0,0},
358: {RC(1.0)/RC(2.0),0,0,0,0,0,0,0,0,0},
359: {RC(-1.0)/RC(6.0),0,0,0,RC(2.0)/RC(3.0),0,0,0,0,0},
360: {RC(1.0)/RC(12.0),0,0,0,RC(1.0)/RC(6.0),RC(1.0)/RC(2.0),0,0,0,0},
361: {RC(1.0)/RC(12.0),0,0,0,RC(1.0)/RC(6.0),RC(1.0)/RC(2.0),0,0,0,0},
362: {RC(1.0)/RC(3.0),0,0,0,RC(-1.0)/RC(3.0),RC(1.0),0,0,0,0},
363: {RC(1.0)/RC(3.0),0,0,0,RC(-1.0)/RC(3.0),RC(1.0),0,0,0,0}},
364: Af[10][10] = {{0,0,0,0,0,0,0,0,0,0},
365: {RC(1.0)/RC(4.0),0,0,0,0,0,0,0,0,0},
366: {RC(-1.0)/RC(12.0),RC(1.0)/RC(3.0),0,0,0,0,0,0,0,0},
367: {RC(1.0)/RC(6.0),RC(-1.0)/RC(6.0),RC(1.0)/RC(2.0),0,0,0,0,0,0,0},
368: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,0,0,0,0,0},
369: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,0,0,0,0,0},
370: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,RC(1.0)/RC(4.0),0,0,0,0},
371: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,RC(-1.0)/RC(12.0),RC(1.0)/RC(3.0),0,0,0},
372: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,RC(1.0)/RC(6.0),RC(-1.0)/RC(6.0),RC(1.0)/RC(2.0),0,0},
373: {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0}},
374: bsb[10] = {RC(1.0)/RC(6.0),0,0,0,RC(1.0)/RC(3.0),RC(1.0)/RC(3.0),0,0,0,RC(1.0)/RC(6.0)},
375: bf[10] = {RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0,RC(1.0)/RC(12.0),RC(1.0)/RC(6.0),RC(1.0)/RC(6.0),RC(1.0)/RC(12.0),0};
376: const PetscInt
377: rsb[10] = {0,0,2,0,4,0,0,7,0,9};
378: TSMPRKRegister(TSMPRKP3,3,5,2,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
379: }
380: #undef RC
381: return 0;
382: }
384: /*@C
385: TSMPRKRegisterDestroy - Frees the list of schemes that were registered by TSMPRKRegister().
387: Not Collective
389: Level: advanced
391: .seealso: TSMPRKRegister(), TSMPRKRegisterAll()
392: @*/
393: PetscErrorCode TSMPRKRegisterDestroy(void)
394: {
395: MPRKTableauLink link;
397: while ((link = MPRKTableauList)) {
398: MPRKTableau t = &link->tab;
399: MPRKTableauList = link->next;
400: PetscFree3(t->Asb,t->bsb,t->csb);
401: PetscFree3(t->Amb,t->bmb,t->cmb);
402: PetscFree3(t->Af,t->bf,t->cf);
403: PetscFree(t->rsb);
404: PetscFree(t->rmb);
405: PetscFree(t->name);
406: PetscFree(link);
407: }
408: TSMPRKRegisterAllCalled = PETSC_FALSE;
409: return 0;
410: }
412: /*@C
413: TSMPRKInitializePackage - This function initializes everything in the TSMPRK package. It is called
414: from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_MPRK()
415: when using static libraries.
417: Level: developer
419: .seealso: PetscInitialize()
420: @*/
421: PetscErrorCode TSMPRKInitializePackage(void)
422: {
423: if (TSMPRKPackageInitialized) return 0;
424: TSMPRKPackageInitialized = PETSC_TRUE;
425: TSMPRKRegisterAll();
426: PetscRegisterFinalize(TSMPRKFinalizePackage);
427: return 0;
428: }
430: /*@C
431: TSMPRKFinalizePackage - This function destroys everything in the TSMPRK package. It is
432: called from PetscFinalize().
434: Level: developer
436: .seealso: PetscFinalize()
437: @*/
438: PetscErrorCode TSMPRKFinalizePackage(void)
439: {
440: TSMPRKPackageInitialized = PETSC_FALSE;
441: TSMPRKRegisterDestroy();
442: return 0;
443: }
445: /*@C
446: TSMPRKRegister - register a MPRK scheme by providing the entries in the Butcher tableau
448: Not Collective, but the same schemes should be registered on all processes on which they will be used
450: Input Parameters:
451: + name - identifier for method
452: . order - approximation order of method
453: . s - number of stages in the base methods
454: . ratio1 - stepsize ratio at 1st level (e.g. slow/medium)
455: . ratio2 - stepsize ratio at 2nd level (e.g. medium/fast)
456: . Af - stage coefficients for fast components(dimension s*s, row-major)
457: . bf - step completion table for fast components(dimension s)
458: . cf - abscissa for fast components(dimension s)
459: . As - stage coefficients for slow components(dimension s*s, row-major)
460: . bs - step completion table for slow components(dimension s)
461: - cs - abscissa for slow components(dimension s)
463: Notes:
464: Several MPRK methods are provided, this function is only needed to create new methods.
466: Level: advanced
468: .seealso: TSMPRK
469: @*/
470: PetscErrorCode TSMPRKRegister(TSMPRKType name,PetscInt order,
471: PetscInt sbase,PetscInt ratio1,PetscInt ratio2,
472: const PetscReal Asb[],const PetscReal bsb[],const PetscReal csb[],const PetscInt rsb[],
473: const PetscReal Amb[],const PetscReal bmb[],const PetscReal cmb[],const PetscInt rmb[],
474: const PetscReal Af[],const PetscReal bf[],const PetscReal cf[])
475: {
476: MPRKTableauLink link;
477: MPRKTableau t;
478: PetscInt s,i,j;
493: PetscNew(&link);
494: t = &link->tab;
496: PetscStrallocpy(name,&t->name);
497: s = sbase*ratio1*ratio2; /* this is the dimension of the matrices below */
498: t->order = order;
499: t->sbase = sbase;
500: t->s = s;
501: t->np = 2;
503: PetscMalloc3(s*s,&t->Af,s,&t->bf,s,&t->cf);
504: PetscArraycpy(t->Af,Af,s*s);
505: if (bf) {
506: PetscArraycpy(t->bf,bf,s);
507: } else
508: for (i=0; i<s; i++) t->bf[i] = Af[(s-1)*s+i];
509: if (cf) {
510: PetscArraycpy(t->cf,cf,s);
511: } else {
512: for (i=0; i<s; i++)
513: for (j=0,t->cf[i]=0; j<s; j++)
514: t->cf[i] += Af[i*s+j];
515: }
517: if (Amb) {
518: t->np = 3;
519: PetscMalloc3(s*s,&t->Amb,s,&t->bmb,s,&t->cmb);
520: PetscArraycpy(t->Amb,Amb,s*s);
521: if (bmb) {
522: PetscArraycpy(t->bmb,bmb,s);
523: } else {
524: for (i=0; i<s; i++) t->bmb[i] = Amb[(s-1)*s+i];
525: }
526: if (cmb) {
527: PetscArraycpy(t->cmb,cmb,s);
528: } else {
529: for (i=0; i<s; i++)
530: for (j=0,t->cmb[i]=0; j<s; j++)
531: t->cmb[i] += Amb[i*s+j];
532: }
533: if (rmb) {
534: PetscMalloc1(s,&t->rmb);
535: PetscArraycpy(t->rmb,rmb,s);
536: } else {
537: PetscCalloc1(s,&t->rmb);
538: }
539: }
541: PetscMalloc3(s*s,&t->Asb,s,&t->bsb,s,&t->csb);
542: PetscArraycpy(t->Asb,Asb,s*s);
543: if (bsb) {
544: PetscArraycpy(t->bsb,bsb,s);
545: } else
546: for (i=0; i<s; i++) t->bsb[i] = Asb[(s-1)*s+i];
547: if (csb) {
548: PetscArraycpy(t->csb,csb,s);
549: } else {
550: for (i=0; i<s; i++)
551: for (j=0,t->csb[i]=0; j<s; j++)
552: t->csb[i] += Asb[i*s+j];
553: }
554: if (rsb) {
555: PetscMalloc1(s,&t->rsb);
556: PetscArraycpy(t->rsb,rsb,s);
557: } else {
558: PetscCalloc1(s,&t->rsb);
559: }
560: link->next = MPRKTableauList;
561: MPRKTableauList = link;
562: return 0;
563: }
565: static PetscErrorCode TSMPRKSetSplits(TS ts)
566: {
567: TS_MPRK *mprk = (TS_MPRK*)ts->data;
568: MPRKTableau tab = mprk->tableau;
569: DM dm,subdm,newdm;
571: TSRHSSplitGetSubTS(ts,"slow",&mprk->subts_slow);
572: TSRHSSplitGetSubTS(ts,"fast",&mprk->subts_fast);
575: /* Only copy the DM */
576: TSGetDM(ts,&dm);
578: TSRHSSplitGetSubTS(ts,"slowbuffer",&mprk->subts_slowbuffer);
579: if (!mprk->subts_slowbuffer) {
580: mprk->subts_slowbuffer = mprk->subts_slow;
581: mprk->subts_slow = NULL;
582: }
583: if (mprk->subts_slow) {
584: DMClone(dm,&newdm);
585: TSGetDM(mprk->subts_slow,&subdm);
586: DMCopyDMTS(subdm,newdm);
587: DMCopyDMSNES(subdm,newdm);
588: TSSetDM(mprk->subts_slow,newdm);
589: DMDestroy(&newdm);
590: }
591: DMClone(dm,&newdm);
592: TSGetDM(mprk->subts_slowbuffer,&subdm);
593: DMCopyDMTS(subdm,newdm);
594: DMCopyDMSNES(subdm,newdm);
595: TSSetDM(mprk->subts_slowbuffer,newdm);
596: DMDestroy(&newdm);
598: DMClone(dm,&newdm);
599: TSGetDM(mprk->subts_fast,&subdm);
600: DMCopyDMTS(subdm,newdm);
601: DMCopyDMSNES(subdm,newdm);
602: TSSetDM(mprk->subts_fast,newdm);
603: DMDestroy(&newdm);
605: if (tab->np == 3) {
606: TSRHSSplitGetSubTS(ts,"medium",&mprk->subts_medium);
607: TSRHSSplitGetSubTS(ts,"mediumbuffer",&mprk->subts_mediumbuffer);
608: if (mprk->subts_medium && !mprk->subts_mediumbuffer) {
609: mprk->subts_mediumbuffer = mprk->subts_medium;
610: mprk->subts_medium = NULL;
611: }
612: if (mprk->subts_medium) {
613: DMClone(dm,&newdm);
614: TSGetDM(mprk->subts_medium,&subdm);
615: DMCopyDMTS(subdm,newdm);
616: DMCopyDMSNES(subdm,newdm);
617: TSSetDM(mprk->subts_medium,newdm);
618: DMDestroy(&newdm);
619: }
620: DMClone(dm,&newdm);
621: TSGetDM(mprk->subts_mediumbuffer,&subdm);
622: DMCopyDMTS(subdm,newdm);
623: DMCopyDMSNES(subdm,newdm);
624: TSSetDM(mprk->subts_mediumbuffer,newdm);
625: DMDestroy(&newdm);
626: }
627: return 0;
628: }
630: /*
631: This if for nonsplit RHS MPRK
632: The step completion formula is
634: x1 = x0 + h b^T YdotRHS
636: */
637: static PetscErrorCode TSEvaluateStep_MPRK(TS ts,PetscInt order,Vec X,PetscBool *done)
638: {
639: TS_MPRK *mprk = (TS_MPRK*)ts->data;
640: MPRKTableau tab = mprk->tableau;
641: PetscScalar *wf = mprk->work_fast;
642: PetscReal h = ts->time_step;
643: PetscInt s = tab->s,j;
645: for (j=0; j<s; j++) wf[j] = h*tab->bf[j];
646: VecCopy(ts->vec_sol,X);
647: VecMAXPY(X,s,wf,mprk->YdotRHS);
648: return 0;
649: }
651: static PetscErrorCode TSStep_MPRK(TS ts)
652: {
653: TS_MPRK *mprk = (TS_MPRK*)ts->data;
654: Vec *Y = mprk->Y,*YdotRHS = mprk->YdotRHS,*YdotRHS_fast = mprk->YdotRHS_fast,*YdotRHS_slow = mprk->YdotRHS_slow,*YdotRHS_slowbuffer = mprk->YdotRHS_slowbuffer;
655: Vec Yslow,Yslowbuffer,Yfast;
656: MPRKTableau tab = mprk->tableau;
657: const PetscInt s = tab->s;
658: const PetscReal *Af = tab->Af,*cf = tab->cf,*Asb = tab->Asb,*csb = tab->csb;
659: PetscScalar *wf = mprk->work_fast,*wsb = mprk->work_slowbuffer;
660: PetscInt i,j;
661: PetscReal next_time_step = ts->time_step,t = ts->ptime,h = ts->time_step;
663: for (i=0; i<s; i++) {
664: mprk->stage_time = t + h*cf[i];
665: TSPreStage(ts,mprk->stage_time);
666: VecCopy(ts->vec_sol,Y[i]);
668: /* slow buffer region */
669: for (j=0; j<i; j++) wsb[j] = h*Asb[i*s+j];
670: for (j=0; j<i; j++) {
671: VecGetSubVector(YdotRHS[j],mprk->is_slowbuffer,&YdotRHS_slowbuffer[j]);
672: }
673: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
674: VecMAXPY(Yslowbuffer,i,wsb,mprk->YdotRHS_slowbuffer);
675: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
676: for (j=0; j<i; j++) {
677: VecRestoreSubVector(YdotRHS[j],mprk->is_slowbuffer,&YdotRHS_slowbuffer[j]);
678: }
679: /* slow region */
680: if (mprk->is_slow) {
681: for (j=0; j<i; j++) {
682: VecGetSubVector(YdotRHS[j],mprk->is_slow,&YdotRHS_slow[j]);
683: }
684: VecGetSubVector(Y[i],mprk->is_slow,&Yslow);
685: VecMAXPY(Yslow,i,wsb,mprk->YdotRHS_slow);
686: VecRestoreSubVector(Y[i],mprk->is_slow,&Yslow);
687: for (j=0; j<i; j++) {
688: VecRestoreSubVector(YdotRHS[j],mprk->is_slow,&YdotRHS_slow[j]);
689: }
690: }
692: /* fast region */
693: for (j=0; j<i; j++) wf[j] = h*Af[i*s+j];
694: for (j=0; j<i; j++) {
695: VecGetSubVector(YdotRHS[j],mprk->is_fast,&YdotRHS_fast[j]);
696: }
697: VecGetSubVector(Y[i],mprk->is_fast,&Yfast);
698: VecMAXPY(Yfast,i,wf,mprk->YdotRHS_fast);
699: VecRestoreSubVector(Y[i],mprk->is_fast,&Yfast);
700: for (j=0; j<i; j++) {
701: VecRestoreSubVector(YdotRHS[j],mprk->is_fast,&YdotRHS_fast[j]);
702: }
703: if (tab->np == 3) {
704: Vec *YdotRHS_medium = mprk->YdotRHS_medium,*YdotRHS_mediumbuffer = mprk->YdotRHS_mediumbuffer;
705: Vec Ymedium,Ymediumbuffer;
706: PetscScalar *wmb = mprk->work_mediumbuffer;
708: for (j=0; j<i; j++) wmb[j] = h*tab->Amb[i*s+j];
709: /* medium region */
710: if (mprk->is_medium) {
711: for (j=0; j<i; j++) {
712: VecGetSubVector(YdotRHS[j],mprk->is_medium,&YdotRHS_medium[j]);
713: }
714: VecGetSubVector(Y[i],mprk->is_medium,&Ymedium);
715: VecMAXPY(Ymedium,i,wmb,mprk->YdotRHS_medium);
716: VecRestoreSubVector(Y[i],mprk->is_medium,&Ymedium);
717: for (j=0; j<i; j++) {
718: VecRestoreSubVector(YdotRHS[j],mprk->is_medium,&YdotRHS_medium[j]);
719: }
720: }
721: /* medium buffer region */
722: for (j=0; j<i; j++) {
723: VecGetSubVector(YdotRHS[j],mprk->is_mediumbuffer,&YdotRHS_mediumbuffer[j]);
724: }
725: VecGetSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
726: VecMAXPY(Ymediumbuffer,i,wmb,mprk->YdotRHS_mediumbuffer);
727: VecRestoreSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
728: for (j=0; j<i; j++) {
729: VecRestoreSubVector(YdotRHS[j],mprk->is_mediumbuffer,&YdotRHS_mediumbuffer[j]);
730: }
731: }
732: TSPostStage(ts,mprk->stage_time,i,Y);
733: /* compute the stage RHS by fast and slow tableau respectively */
734: TSComputeRHSFunction(ts,t+h*csb[i],Y[i],YdotRHS[i]);
735: }
736: TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);
737: ts->ptime += ts->time_step;
738: ts->time_step = next_time_step;
739: return 0;
740: }
742: /*
743: This if for the case when split RHS is used
744: The step completion formula is
745: x1 = x0 + h b^T YdotRHS
746: */
747: static PetscErrorCode TSEvaluateStep_MPRKSPLIT(TS ts,PetscInt order,Vec X,PetscBool *done)
748: {
749: TS_MPRK *mprk = (TS_MPRK*)ts->data;
750: MPRKTableau tab = mprk->tableau;
751: Vec Xslow,Xfast,Xslowbuffer; /* subvectors for slow and fast components in X respectively */
752: PetscScalar *wf = mprk->work_fast,*ws = mprk->work_slow,*wsb = mprk->work_slowbuffer;
753: PetscReal h = ts->time_step;
754: PetscInt s = tab->s,j,computedstages;
756: VecCopy(ts->vec_sol,X);
758: /* slow region */
759: if (mprk->is_slow) {
760: computedstages = 0;
761: for (j=0; j<s; j++) {
762: if (tab->rsb[j]) ws[tab->rsb[j]-1] += h*tab->bsb[j];
763: else ws[computedstages++] = h*tab->bsb[j];
764: }
765: VecGetSubVector(X,mprk->is_slow,&Xslow);
766: VecMAXPY(Xslow,computedstages,ws,mprk->YdotRHS_slow);
767: VecRestoreSubVector(X,mprk->is_slow,&Xslow);
768: }
770: if (tab->np == 3 && mprk->is_medium) {
771: computedstages = 0;
772: for (j=0; j<s; j++) {
773: if (tab->rmb[j]) wsb[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += h*tab->bsb[j];
774: else wsb[computedstages++] = h*tab->bsb[j];
775: }
776: VecGetSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
777: VecMAXPY(Xslowbuffer,computedstages,wsb,mprk->YdotRHS_slowbuffer);
778: VecRestoreSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
779: } else {
780: /* slow buffer region */
781: for (j=0; j<s; j++) wsb[j] = h*tab->bsb[j];
782: VecGetSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
783: VecMAXPY(Xslowbuffer,s,wsb,mprk->YdotRHS_slowbuffer);
784: VecRestoreSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
785: }
786: if (tab->np == 3) {
787: Vec Xmedium,Xmediumbuffer;
788: PetscScalar *wm = mprk->work_medium,*wmb = mprk->work_mediumbuffer;
789: /* medium region and slow buffer region */
790: if (mprk->is_medium) {
791: computedstages = 0;
792: for (j=0; j<s; j++) {
793: if (tab->rmb[j]) wm[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += h*tab->bmb[j];
794: else wm[computedstages++] = h*tab->bmb[j];
795: }
796: VecGetSubVector(X,mprk->is_medium,&Xmedium);
797: VecMAXPY(Xmedium,computedstages,wm,mprk->YdotRHS_medium);
798: VecRestoreSubVector(X,mprk->is_medium,&Xmedium);
799: }
800: /* medium buffer region */
801: for (j=0; j<s; j++) wmb[j] = h*tab->bmb[j];
802: VecGetSubVector(X,mprk->is_mediumbuffer,&Xmediumbuffer);
803: VecMAXPY(Xmediumbuffer,s,wmb,mprk->YdotRHS_mediumbuffer);
804: VecRestoreSubVector(X,mprk->is_mediumbuffer,&Xmediumbuffer);
805: }
806: /* fast region */
807: for (j=0; j<s; j++) wf[j] = h*tab->bf[j];
808: VecGetSubVector(X,mprk->is_fast,&Xfast);
809: VecMAXPY(Xfast,s,wf,mprk->YdotRHS_fast);
810: VecRestoreSubVector(X,mprk->is_fast,&Xfast);
811: return 0;
812: }
814: static PetscErrorCode TSStep_MPRKSPLIT(TS ts)
815: {
816: TS_MPRK *mprk = (TS_MPRK*)ts->data;
817: MPRKTableau tab = mprk->tableau;
818: Vec *Y = mprk->Y,*YdotRHS_fast = mprk->YdotRHS_fast,*YdotRHS_slow = mprk->YdotRHS_slow,*YdotRHS_slowbuffer = mprk->YdotRHS_slowbuffer;
819: Vec Yslow,Yslowbuffer,Yfast; /* subvectors for slow and fast components in Y[i] respectively */
820: PetscInt s = tab->s;
821: const PetscReal *Af = tab->Af,*cf = tab->cf,*Asb = tab->Asb,*csb = tab->csb;
822: PetscScalar *wf = mprk->work_fast,*ws = mprk->work_slow,*wsb = mprk->work_slowbuffer;
823: PetscInt i,j,computedstages;
824: PetscReal next_time_step = ts->time_step,t = ts->ptime,h = ts->time_step;
826: for (i=0; i<s; i++) {
827: mprk->stage_time = t + h*cf[i];
828: TSPreStage(ts,mprk->stage_time);
829: /* calculate the stage value for fast and slow components respectively */
830: VecCopy(ts->vec_sol,Y[i]);
831: for (j=0; j<i; j++) wsb[j] = h*Asb[i*s+j];
833: /* slow buffer region */
834: if (tab->np == 3 && mprk->is_medium) {
835: if (tab->rmb[i]) {
836: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
837: VecISCopy(Y[tab->rmb[i]-1],mprk->is_slowbuffer,SCATTER_REVERSE,Yslowbuffer);
838: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
839: } else {
840: PetscScalar *wm = mprk->work_medium;
841: computedstages = 0;
842: for (j=0; j<i; j++) {
843: if (tab->rmb[j]) wm[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += wsb[j];
844: else wm[computedstages++] = wsb[j];
845: }
846: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
847: VecMAXPY(Yslowbuffer,computedstages,wm,YdotRHS_slowbuffer);
848: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
849: }
850: } else {
851: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
852: VecMAXPY(Yslowbuffer,i,wsb,YdotRHS_slowbuffer);
853: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
854: }
856: /* slow region */
857: if (mprk->is_slow) {
858: if (tab->rsb[i]) { /* repeat previous stage */
859: VecGetSubVector(Y[i],mprk->is_slow,&Yslow);
860: VecISCopy(Y[tab->rsb[i]-1],mprk->is_slow,SCATTER_REVERSE,Yslow);
861: VecRestoreSubVector(Y[i],mprk->is_slow,&Yslow);
862: } else {
863: computedstages = 0;
864: for (j=0; j<i; j++) {
865: if (tab->rsb[j]) ws[tab->rsb[j]-1] += wsb[j];
866: else ws[computedstages++] = wsb[j];
867: }
868: VecGetSubVector(Y[i],mprk->is_slow,&Yslow);
869: VecMAXPY(Yslow,computedstages,ws,YdotRHS_slow);
870: VecRestoreSubVector(Y[i],mprk->is_slow,&Yslow);
871: /* only depends on the slow buffer region */
872: TSComputeRHSFunction(mprk->subts_slow,t+h*csb[i],Y[i],YdotRHS_slow[computedstages]);
873: }
874: }
876: /* fast region */
877: for (j=0; j<i; j++) wf[j] = h*Af[i*s+j];
878: VecGetSubVector(Y[i],mprk->is_fast,&Yfast);
879: VecMAXPY(Yfast,i,wf,YdotRHS_fast);
880: VecRestoreSubVector(Y[i],mprk->is_fast,&Yfast);
882: if (tab->np == 3) {
883: Vec *YdotRHS_medium = mprk->YdotRHS_medium,*YdotRHS_mediumbuffer = mprk->YdotRHS_mediumbuffer;
884: Vec Ymedium,Ymediumbuffer;
885: const PetscReal *Amb = tab->Amb,*cmb = tab->cmb;
886: PetscScalar *wm = mprk->work_medium,*wmb = mprk->work_mediumbuffer;
888: for (j=0; j<i; j++) wmb[j] = h*Amb[i*s+j];
889: /* medium buffer region */
890: VecGetSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
891: VecMAXPY(Ymediumbuffer,i,wmb,YdotRHS_mediumbuffer);
892: VecRestoreSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
893: /* medium region */
894: if (mprk->is_medium) {
895: if (tab->rmb[i]) { /* repeat previous stage */
896: VecGetSubVector(Y[i],mprk->is_medium,&Ymedium);
897: VecISCopy(Y[tab->rmb[i]-1],mprk->is_medium,SCATTER_REVERSE,Ymedium);
898: VecRestoreSubVector(Y[i],mprk->is_medium,&Ymedium);
899: } else {
900: computedstages = 0;
901: for (j=0; j<i; j++) {
902: if (tab->rmb[j]) wm[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += wmb[j];
903: else wm[computedstages++] = wmb[j];
905: }
906: VecGetSubVector(Y[i],mprk->is_medium,&Ymedium);
907: VecMAXPY(Ymedium,computedstages,wm,YdotRHS_medium);
908: VecRestoreSubVector(Y[i],mprk->is_medium,&Ymedium);
909: /* only depends on the medium buffer region */
910: TSComputeRHSFunction(mprk->subts_medium,t+h*cmb[i],Y[i],YdotRHS_medium[computedstages]);
911: /* must be computed after all regions are updated in Y */
912: TSComputeRHSFunction(mprk->subts_slowbuffer,t+h*csb[i],Y[i],YdotRHS_slowbuffer[computedstages]);
913: }
914: }
915: /* must be computed after fast region and slow region are updated in Y */
916: TSComputeRHSFunction(mprk->subts_mediumbuffer,t+h*cmb[i],Y[i],YdotRHS_mediumbuffer[i]);
917: }
918: if (!(tab->np == 3 && mprk->is_medium)) {
919: TSComputeRHSFunction(mprk->subts_slowbuffer,t+h*csb[i],Y[i],YdotRHS_slowbuffer[i]);
920: }
921: TSComputeRHSFunction(mprk->subts_fast,t+h*cf[i],Y[i],YdotRHS_fast[i]);
922: }
924: TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);
925: ts->ptime += ts->time_step;
926: ts->time_step = next_time_step;
927: return 0;
928: }
930: static PetscErrorCode TSMPRKTableauReset(TS ts)
931: {
932: TS_MPRK *mprk = (TS_MPRK*)ts->data;
933: MPRKTableau tab = mprk->tableau;
935: if (!tab) return 0;
936: PetscFree(mprk->work_fast);
937: PetscFree(mprk->work_slow);
938: PetscFree(mprk->work_slowbuffer);
939: PetscFree(mprk->work_medium);
940: PetscFree(mprk->work_mediumbuffer);
941: VecDestroyVecs(tab->s,&mprk->Y);
942: if (ts->use_splitrhsfunction) {
943: VecDestroyVecs(tab->s,&mprk->YdotRHS_fast);
944: VecDestroyVecs(tab->s,&mprk->YdotRHS_slow);
945: VecDestroyVecs(tab->s,&mprk->YdotRHS_slowbuffer);
946: VecDestroyVecs(tab->s,&mprk->YdotRHS_medium);
947: VecDestroyVecs(tab->s,&mprk->YdotRHS_mediumbuffer);
948: } else {
949: VecDestroyVecs(tab->s,&mprk->YdotRHS);
950: if (mprk->is_slow) {
951: PetscFree(mprk->YdotRHS_slow);
952: }
953: PetscFree(mprk->YdotRHS_slowbuffer);
954: if (tab->np == 3) {
955: if (mprk->is_medium) {
956: PetscFree(mprk->YdotRHS_medium);
957: }
958: PetscFree(mprk->YdotRHS_mediumbuffer);
959: }
960: PetscFree(mprk->YdotRHS_fast);
961: }
962: return 0;
963: }
965: static PetscErrorCode TSReset_MPRK(TS ts)
966: {
967: TSMPRKTableauReset(ts);
968: return 0;
969: }
971: static PetscErrorCode DMCoarsenHook_TSMPRK(DM fine,DM coarse,void *ctx)
972: {
973: return 0;
974: }
976: static PetscErrorCode DMRestrictHook_TSMPRK(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
977: {
978: return 0;
979: }
981: static PetscErrorCode DMSubDomainHook_TSMPRK(DM dm,DM subdm,void *ctx)
982: {
983: return 0;
984: }
986: static PetscErrorCode DMSubDomainRestrictHook_TSMPRK(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
987: {
988: return 0;
989: }
991: static PetscErrorCode TSMPRKTableauSetUp(TS ts)
992: {
993: TS_MPRK *mprk = (TS_MPRK*)ts->data;
994: MPRKTableau tab = mprk->tableau;
995: Vec YdotRHS_slow,YdotRHS_slowbuffer,YdotRHS_medium,YdotRHS_mediumbuffer,YdotRHS_fast;
997: VecDuplicateVecs(ts->vec_sol,tab->s,&mprk->Y);
998: if (mprk->is_slow) {
999: PetscMalloc1(tab->s,&mprk->work_slow);
1000: }
1001: PetscMalloc1(tab->s,&mprk->work_slowbuffer);
1002: if (tab->np == 3) {
1003: if (mprk->is_medium) {
1004: PetscMalloc1(tab->s,&mprk->work_medium);
1005: }
1006: PetscMalloc1(tab->s,&mprk->work_mediumbuffer);
1007: }
1008: PetscMalloc1(tab->s,&mprk->work_fast);
1010: if (ts->use_splitrhsfunction) {
1011: if (mprk->is_slow) {
1012: VecGetSubVector(ts->vec_sol,mprk->is_slow,&YdotRHS_slow);
1013: VecDuplicateVecs(YdotRHS_slow,tab->s,&mprk->YdotRHS_slow);
1014: VecRestoreSubVector(ts->vec_sol,mprk->is_slow,&YdotRHS_slow);
1015: }
1016: VecGetSubVector(ts->vec_sol,mprk->is_slowbuffer,&YdotRHS_slowbuffer);
1017: VecDuplicateVecs(YdotRHS_slowbuffer,tab->s,&mprk->YdotRHS_slowbuffer);
1018: VecRestoreSubVector(ts->vec_sol,mprk->is_slowbuffer,&YdotRHS_slowbuffer);
1019: if (tab->np == 3) {
1020: if (mprk->is_medium) {
1021: VecGetSubVector(ts->vec_sol,mprk->is_medium,&YdotRHS_medium);
1022: VecDuplicateVecs(YdotRHS_medium,tab->s,&mprk->YdotRHS_medium);
1023: VecRestoreSubVector(ts->vec_sol,mprk->is_medium,&YdotRHS_medium);
1024: }
1025: VecGetSubVector(ts->vec_sol,mprk->is_mediumbuffer,&YdotRHS_mediumbuffer);
1026: VecDuplicateVecs(YdotRHS_mediumbuffer,tab->s,&mprk->YdotRHS_mediumbuffer);
1027: VecRestoreSubVector(ts->vec_sol,mprk->is_mediumbuffer,&YdotRHS_mediumbuffer);
1028: }
1029: VecGetSubVector(ts->vec_sol,mprk->is_fast,&YdotRHS_fast);
1030: VecDuplicateVecs(YdotRHS_fast,tab->s,&mprk->YdotRHS_fast);
1031: VecRestoreSubVector(ts->vec_sol,mprk->is_fast,&YdotRHS_fast);
1032: } else {
1033: VecDuplicateVecs(ts->vec_sol,tab->s,&mprk->YdotRHS);
1034: if (mprk->is_slow) {
1035: PetscMalloc1(tab->s,&mprk->YdotRHS_slow);
1036: }
1037: PetscMalloc1(tab->s,&mprk->YdotRHS_slowbuffer);
1038: if (tab->np == 3) {
1039: if (mprk->is_medium) {
1040: PetscMalloc1(tab->s,&mprk->YdotRHS_medium);
1041: }
1042: PetscMalloc1(tab->s,&mprk->YdotRHS_mediumbuffer);
1043: }
1044: PetscMalloc1(tab->s,&mprk->YdotRHS_fast);
1045: }
1046: return 0;
1047: }
1049: static PetscErrorCode TSSetUp_MPRK(TS ts)
1050: {
1051: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1052: MPRKTableau tab = mprk->tableau;
1053: DM dm;
1055: TSRHSSplitGetIS(ts,"slow",&mprk->is_slow);
1056: TSRHSSplitGetIS(ts,"fast",&mprk->is_fast);
1059: if (tab->np == 3) {
1060: TSRHSSplitGetIS(ts,"medium",&mprk->is_medium);
1062: TSRHSSplitGetIS(ts,"mediumbuffer",&mprk->is_mediumbuffer);
1063: if (!mprk->is_mediumbuffer) { /* let medium buffer cover whole medium region */
1064: mprk->is_mediumbuffer = mprk->is_medium;
1065: mprk->is_medium = NULL;
1066: }
1067: }
1069: /* If users do not provide buffer region settings, the solver will do them automatically, but with a performance penalty */
1070: TSRHSSplitGetIS(ts,"slowbuffer",&mprk->is_slowbuffer);
1071: if (!mprk->is_slowbuffer) { /* let slow buffer cover whole slow region */
1072: mprk->is_slowbuffer = mprk->is_slow;
1073: mprk->is_slow = NULL;
1074: }
1075: TSCheckImplicitTerm(ts);
1076: TSMPRKTableauSetUp(ts);
1077: TSGetDM(ts,&dm);
1078: DMCoarsenHookAdd(dm,DMCoarsenHook_TSMPRK,DMRestrictHook_TSMPRK,ts);
1079: DMSubDomainHookAdd(dm,DMSubDomainHook_TSMPRK,DMSubDomainRestrictHook_TSMPRK,ts);
1080: if (ts->use_splitrhsfunction) {
1081: ts->ops->step = TSStep_MPRKSPLIT;
1082: ts->ops->evaluatestep = TSEvaluateStep_MPRKSPLIT;
1083: TSMPRKSetSplits(ts);
1084: } else {
1085: ts->ops->step = TSStep_MPRK;
1086: ts->ops->evaluatestep = TSEvaluateStep_MPRK;
1087: }
1088: return 0;
1089: }
1091: static PetscErrorCode TSSetFromOptions_MPRK(PetscOptionItems *PetscOptionsObject,TS ts)
1092: {
1093: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1095: PetscOptionsHead(PetscOptionsObject,"PRK ODE solver options");
1096: {
1097: MPRKTableauLink link;
1098: PetscInt count,choice;
1099: PetscBool flg;
1100: const char **namelist;
1101: for (link=MPRKTableauList,count=0; link; link=link->next,count++) ;
1102: PetscMalloc1(count,(char***)&namelist);
1103: for (link=MPRKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
1104: PetscOptionsEList("-ts_mprk_type","Family of MPRK method","TSMPRKSetType",(const char*const*)namelist,count,mprk->tableau->name,&choice,&flg);
1105: if (flg) TSMPRKSetType(ts,namelist[choice]);
1106: PetscFree(namelist);
1107: }
1108: PetscOptionsTail();
1109: return 0;
1110: }
1112: static PetscErrorCode TSView_MPRK(TS ts,PetscViewer viewer)
1113: {
1114: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1115: PetscBool iascii;
1117: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1118: if (iascii) {
1119: MPRKTableau tab = mprk->tableau;
1120: TSMPRKType mprktype;
1121: char fbuf[512];
1122: char sbuf[512];
1123: PetscInt i;
1124: TSMPRKGetType(ts,&mprktype);
1125: PetscViewerASCIIPrintf(viewer," MPRK type %s\n",mprktype);
1126: PetscViewerASCIIPrintf(viewer," Order: %D\n",tab->order);
1128: PetscFormatRealArray(fbuf,sizeof(fbuf),"% 8.6f",tab->s,tab->cf);
1129: PetscViewerASCIIPrintf(viewer," Abscissa cf = %s\n",fbuf);
1130: PetscViewerASCIIPrintf(viewer," Af = \n");
1131: for (i=0; i<tab->s; i++) {
1132: PetscFormatRealArray(fbuf,sizeof(fbuf),"% 8.6f",tab->s,&tab->Af[i*tab->s]);
1133: PetscViewerASCIIPrintf(viewer," %s\n",fbuf);
1134: }
1135: PetscFormatRealArray(fbuf,sizeof(fbuf),"% 8.6f",tab->s,tab->bf);
1136: PetscViewerASCIIPrintf(viewer," bf = %s\n",fbuf);
1138: PetscFormatRealArray(sbuf,sizeof(sbuf),"% 8.6f",tab->s,tab->csb);
1139: PetscViewerASCIIPrintf(viewer," Abscissa csb = %s\n",sbuf);
1140: PetscViewerASCIIPrintf(viewer," Asb = \n");
1141: for (i=0; i<tab->s; i++) {
1142: PetscFormatRealArray(sbuf,sizeof(sbuf),"% 8.6f",tab->s,&tab->Asb[i*tab->s]);
1143: PetscViewerASCIIPrintf(viewer," %s\n",sbuf);
1144: }
1145: PetscFormatRealArray(sbuf,sizeof(sbuf),"% 8.6f",tab->s,tab->bsb);
1146: PetscViewerASCIIPrintf(viewer," bsb = %s\n",sbuf);
1148: if (tab->np == 3) {
1149: char mbuf[512];
1150: PetscFormatRealArray(mbuf,sizeof(mbuf),"% 8.6f",tab->s,tab->cmb);
1151: PetscViewerASCIIPrintf(viewer," Abscissa cmb = %s\n",mbuf);
1152: PetscViewerASCIIPrintf(viewer," Amb = \n");
1153: for (i=0; i<tab->s; i++) {
1154: PetscFormatRealArray(mbuf,sizeof(mbuf),"% 8.6f",tab->s,&tab->Amb[i*tab->s]);
1155: PetscViewerASCIIPrintf(viewer," %s\n",mbuf);
1156: }
1157: PetscFormatRealArray(mbuf,sizeof(mbuf),"% 8.6f",tab->s,tab->bmb);
1158: PetscViewerASCIIPrintf(viewer," bmb = %s\n",mbuf);
1159: }
1160: }
1161: return 0;
1162: }
1164: static PetscErrorCode TSLoad_MPRK(TS ts,PetscViewer viewer)
1165: {
1166: TSAdapt adapt;
1168: TSGetAdapt(ts,&adapt);
1169: TSAdaptLoad(adapt,viewer);
1170: return 0;
1171: }
1173: /*@C
1174: TSMPRKSetType - Set the type of MPRK scheme
1176: Not collective
1178: Input Parameters:
1179: + ts - timestepping context
1180: - mprktype - type of MPRK-scheme
1182: Options Database:
1183: . -ts_mprk_type - <pm2,p2,p3> - select the specific scheme
1185: Level: intermediate
1187: .seealso: TSMPRKGetType(), TSMPRK, TSMPRKType
1188: @*/
1189: PetscErrorCode TSMPRKSetType(TS ts,TSMPRKType mprktype)
1190: {
1193: PetscTryMethod(ts,"TSMPRKSetType_C",(TS,TSMPRKType),(ts,mprktype));
1194: return 0;
1195: }
1197: /*@C
1198: TSMPRKGetType - Get the type of MPRK scheme
1200: Not collective
1202: Input Parameter:
1203: . ts - timestepping context
1205: Output Parameter:
1206: . mprktype - type of MPRK-scheme
1208: Level: intermediate
1210: .seealso: TSMPRKGetType()
1211: @*/
1212: PetscErrorCode TSMPRKGetType(TS ts,TSMPRKType *mprktype)
1213: {
1215: PetscUseMethod(ts,"TSMPRKGetType_C",(TS,TSMPRKType*),(ts,mprktype));
1216: return 0;
1217: }
1219: static PetscErrorCode TSMPRKGetType_MPRK(TS ts,TSMPRKType *mprktype)
1220: {
1221: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1223: *mprktype = mprk->tableau->name;
1224: return 0;
1225: }
1227: static PetscErrorCode TSMPRKSetType_MPRK(TS ts,TSMPRKType mprktype)
1228: {
1229: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1230: PetscBool match;
1231: MPRKTableauLink link;
1233: if (mprk->tableau) {
1234: PetscStrcmp(mprk->tableau->name,mprktype,&match);
1235: if (match) return 0;
1236: }
1237: for (link = MPRKTableauList; link; link=link->next) {
1238: PetscStrcmp(link->tab.name,mprktype,&match);
1239: if (match) {
1240: if (ts->setupcalled) TSMPRKTableauReset(ts);
1241: mprk->tableau = &link->tab;
1242: if (ts->setupcalled) TSMPRKTableauSetUp(ts);
1243: return 0;
1244: }
1245: }
1246: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",mprktype);
1247: }
1249: static PetscErrorCode TSGetStages_MPRK(TS ts,PetscInt *ns,Vec **Y)
1250: {
1251: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1253: *ns = mprk->tableau->s;
1254: if (Y) *Y = mprk->Y;
1255: return 0;
1256: }
1258: static PetscErrorCode TSDestroy_MPRK(TS ts)
1259: {
1260: TSReset_MPRK(ts);
1261: if (ts->dm) {
1262: DMCoarsenHookRemove(ts->dm,DMCoarsenHook_TSMPRK,DMRestrictHook_TSMPRK,ts);
1263: DMSubDomainHookRemove(ts->dm,DMSubDomainHook_TSMPRK,DMSubDomainRestrictHook_TSMPRK,ts);
1264: }
1265: PetscFree(ts->data);
1266: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKGetType_C",NULL);
1267: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKSetType_C",NULL);
1268: return 0;
1269: }
1271: /*MC
1272: TSMPRK - ODE solver using Multirate Partitioned Runge-Kutta schemes
1274: The user should provide the right hand side of the equation
1275: using TSSetRHSFunction().
1277: Notes:
1278: The default is TSMPRKPM2, it can be changed with TSMPRKSetType() or -ts_mprk_type
1280: Level: beginner
1282: .seealso: TSCreate(), TS, TSSetType(), TSMPRKSetType(), TSMPRKGetType(), TSMPRKType, TSMPRKRegister(), TSMPRKSetMultirateType()
1283: TSMPRKM2, TSMPRKM3, TSMPRKRFSMR3, TSMPRKRFSMR2
1285: M*/
1286: PETSC_EXTERN PetscErrorCode TSCreate_MPRK(TS ts)
1287: {
1288: TS_MPRK *mprk;
1290: TSMPRKInitializePackage();
1292: ts->ops->reset = TSReset_MPRK;
1293: ts->ops->destroy = TSDestroy_MPRK;
1294: ts->ops->view = TSView_MPRK;
1295: ts->ops->load = TSLoad_MPRK;
1296: ts->ops->setup = TSSetUp_MPRK;
1297: ts->ops->step = TSStep_MPRK;
1298: ts->ops->evaluatestep = TSEvaluateStep_MPRK;
1299: ts->ops->setfromoptions = TSSetFromOptions_MPRK;
1300: ts->ops->getstages = TSGetStages_MPRK;
1302: PetscNewLog(ts,&mprk);
1303: ts->data = (void*)mprk;
1305: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKGetType_C",TSMPRKGetType_MPRK);
1306: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKSetType_C",TSMPRKSetType_MPRK);
1308: TSMPRKSetType(ts,TSMPRKDefault);
1309: return 0;
1310: }