Actual source code: mprk.c
petsc-3.14.6 2021-03-30
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;
74: for (k=0; k<ratio; k++) {
75: /* diagonal blocks */
76: for (i=0; i<s; i++)
77: for (j=0; j<s; j++) {
78: A1[(k*s+i)*ratio*s+k*s+j] = Abase[i*s+j];
79: A2[(k*s+i)*ratio*s+k*s+j] = Abase[i*s+j]/ratio;
80: }
81: /* off diagonal blocks */
82: for (l=0; l<k; l++)
83: for (i=0; i<s; i++)
84: for (j=0; j<s; j++)
85: A2[(k*s+i)*ratio*s+l*s+j] = bbase[j]/ratio;
86: for (j=0; j<s; j++) {
87: b1[k*s+j] = bbase[j]/ratio;
88: b2[k*s+j] = bbase[j]/ratio;
89: }
90: }
91: return(0);
92: }
94: 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[])
95: {
96: PetscInt i,j,k,l,m,n;
99: for (k=0; k<ratio; k++) { /* diagonal blocks of size ratio*s by ratio*s */
100: for (l=0; l<ratio; l++) /* diagonal sub-blocks of size s by s */
101: for (i=0; i<s; i++)
102: for (j=0; j<s; j++) {
103: A1[((k*ratio+l)*s+i)*ratio*ratio*s+(k*ratio+l)*s+j] = Abase[i*s+j];
104: A2[((k*ratio+l)*s+i)*ratio*ratio*s+(k*ratio+l)*s+j] = Abase[i*s+j]/ratio;
105: A3[((k*ratio+l)*s+i)*ratio*ratio*s+(k*ratio+l)*s+j] = Abase[i*s+j]/ratio/ratio;
106: }
107: for (l=0; l<k; l++) /* off-diagonal blocks of size ratio*s by ratio*s */
108: for (m=0; m<ratio; m++)
109: for (n=0; n<ratio; n++)
110: for (i=0; i<s; i++)
111: for (j=0; j<s; j++) {
112: A2[((k*ratio+m)*s+i)*ratio*ratio*s+(l*ratio+n)*s+j] = bbase[j]/ratio/ratio;
113: A3[((k*ratio+m)*s+i)*ratio*ratio*s+(l*ratio+n)*s+j] = bbase[j]/ratio/ratio;
114: }
115: for (m=0; m<ratio; m++)
116: for (n=0; n<m; n++) /* off-diagonal sub-blocks of size s by s in the diagonal blocks */
117: for (i=0; i<s; i++)
118: for (j=0; j<s; j++)
119: A3[((k*ratio+m)*s+i)*ratio*ratio*s+(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
120: for (n=0; n<ratio; n++)
121: for (j=0; j<s; j++) {
122: b1[(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
123: b2[(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
124: b3[(k*ratio+n)*s+j] = bbase[j]/ratio/ratio;
125: }
126: }
127: return(0);
128: }
130: /*MC
131: TSMPRK2A22 - Second Order Multirate Partitioned Runge Kutta scheme based on RK2A.
133: This method has four stages for slow and fast parts. The refinement factor of the stepsize is 2.
134: r = 2, np = 2
135: Options database:
136: . -ts_mprk_type 2a22
138: Level: advanced
140: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
141: M*/
142: /*MC
143: TSMPRK2A23 - Second Order Multirate Partitioned Runge-Kutta scheme based on RK2A.
145: This method has eight stages for slow and medium and fast parts. The refinement factor of the stepsize is 2.
146: r = 2, np = 3
147: Options database:
148: . -ts_mprk_type 2a23
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
159: Options database:
160: . -ts_mprk_type 2a32
162: Level: advanced
164: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
165: M*/
166: /*MC
167: TSMPRK2A33 - Second Order Multirate Partitioned Runge-Kutta scheme based on RK2A.
169: This method has eight stages for slow and medium and fast parts. The refinement factor of the stepsize is 3.
170: r = 3, np = 3
171: Options database:
172: . -ts_mprk_type 2a33
174: Level: advanced
176: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
177: M*/
178: /*MC
179: TSMPRK3P2M - Third Order Multirate Partitioned Runge-Kutta scheme.
181: This method has eight stages for both slow and fast parts.
183: Options database:
184: . -ts_mprk_type pm3 (put here temporarily)
186: Level: advanced
188: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
189: M*/
190: /*MC
191: TSMPRKP2 - Second Order Multirate Partitioned Runge-Kutta scheme.
193: This method has five stages for both slow and fast parts.
195: Options database:
196: . -ts_mprk_type p2
198: Level: advanced
200: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
201: M*/
202: /*MC
203: TSMPRKP3 - Third Order Multirate Partitioned Runge-Kutta scheme.
205: This method has ten stages for both slow and fast parts.
207: Options database:
208: . -ts_mprk_type p3
210: Level: advanced
212: .seealso: TSMPRK, TSMPRKType, TSMPRKSetType()
213: M*/
215: /*@C
216: TSMPRKRegisterAll - Registers all of the Partirioned Runge-Kutta explicit methods in TSMPRK
218: Not Collective, but should be called by all processes which will need the schemes to be registered
220: Level: advanced
222: .seealso: TSMPRKRegisterDestroy()
223: @*/
224: PetscErrorCode TSMPRKRegisterAll(void)
225: {
229: if (TSMPRKRegisterAllCalled) return(0);
230: TSMPRKRegisterAllCalled = PETSC_TRUE;
232: #define RC PetscRealConstant
233: {
234: const PetscReal
235: Abase[2][2] = {{0,0},
236: {RC(1.0),0}},
237: bbase[2] = {RC(0.5),RC(0.5)};
238: PetscReal
239: Asb[4][4] = {{0}},Af[4][4] = {{0}},bsb[4] = {0},bf[4] = {0};
240: PetscInt
241: rsb[4] = {0,0,1,2};
242: TSMPRKGenerateTableau2(2,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Af[0][0],bf);
243: TSMPRKRegister(TSMPRK2A22,2,2,2,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
244: }
245: {
246: const PetscReal
247: Abase[2][2] = {{0,0},
248: {RC(1.0),0}},
249: bbase[2] = {RC(0.5),RC(0.5)};
250: PetscReal
251: Asb[8][8] = {{0}},Amb[8][8] = {{0}},Af[8][8] = {{0}},bsb[8] ={0},bmb[8] = {0},bf[8] = {0};
252: PetscInt
253: rsb[8] = {0,0,1,2,1,2,1,2},rmb[8] = {0,0,1,2,0,0,5,6};
254: TSMPRKGenerateTableau3(2,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Amb[0][0],bmb,&Af[0][0],bf);
255: TSMPRKRegister(TSMPRK2A23,2,2,2,2,&Asb[0][0],bsb,NULL,rsb,&Amb[0][0],bmb,NULL,rmb,&Af[0][0],bf,NULL);
256: }
257: {
258: const PetscReal
259: Abase[2][2] = {{0,0},
260: {RC(1.0),0}},
261: bbase[2] = {RC(0.5),RC(0.5)};
262: PetscReal
263: Asb[6][6] = {{0}},Af[6][6] = {{0}},bsb[6] = {0},bf[6] = {0};
264: PetscInt
265: rsb[6] = {0,0,1,2,1,2};
266: TSMPRKGenerateTableau2(3,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Af[0][0],bf);
267: TSMPRKRegister(TSMPRK2A32,2,2,3,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
268: }
269: {
270: const PetscReal
271: Abase[2][2] = {{0,0},
272: {RC(1.0),0}},
273: bbase[2] = {RC(0.5),RC(0.5)};
274: PetscReal
275: Asb[18][18] = {{0}},Amb[18][18] = {{0}},Af[18][18] = {{0}},bsb[18] ={0},bmb[18] = {0},bf[18] = {0};
276: PetscInt
277: 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};
278: TSMPRKGenerateTableau3(3,2,&Abase[0][0],bbase,&Asb[0][0],bsb,&Amb[0][0],bmb,&Af[0][0],bf);
279: TSMPRKRegister(TSMPRK2A33,2,2,3,3,&Asb[0][0],bsb,NULL,rsb,&Amb[0][0],bmb,NULL,rmb,&Af[0][0],bf,NULL);
280: }
281: /*
282: PetscReal
283: Asb[8][8] = {{Abase[0][0],Abase[0][1],0,0,0,0,0,0},
284: {Abase[1][0],Abase[1][1],0,0,0,0,0,0},
285: {0,0,Abase[0][0],Abase[0][1],0,0,0,0},
286: {0,0,Abase[1][0],Abase[1][1],0,0,0,0},
287: {0,0,0,0,Abase[0][0],Abase[0][1],0,0},
288: {0,0,0,0,Abase[1][0],Abase[1][1],0,0},
289: {0,0,0,0,0,0,Abase[0][0],Abase[0][1]},
290: {0,0,0,0,0,0,Abase[1][0],Abase[1][1]}},
291: Amb[8][8] = {{Abase[0][0]/m,Abase[0][1]/m,0,0,0,0,0,0},
292: {Abase[1][0]/m,Abase[1][1]/m,0,0,0,0,0,0},
293: {0,0,Abase[0][0]/m,Abase[0][1]/m,0,0,0,0},
294: {0,0,Abase[1][0]/m,Abase[1][1]/m,0,0,0,0},
295: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,Abase[0][0]/m,Abase[0][1]/m,0,0},
296: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,Abase[1][0]/m,Abase[1][1]/m,0,0},
297: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,0,0,Abase[0][0]/m,Abase[0][1]/m},
298: {bbase[0]/m,bbase[1]/m,bbase[0]/m,bbase[1]/m,0,0,Abase[1][0]/m,Abase[1][1]/m}},
299: Af[8][8] = {{Abase[0][0]/m/m,Abase[0][1]/m/m,0,0,0,0,0,0},
300: {Abase[1][0]/m/m,Abase[1][1]/m/m,0,0,0,0,0,0},
301: {0,0,Abase[0][0]/m/m,Abase[0][1]/m/m,0,0,0,0},
302: {0,0,Abase[1][0]/m/m,Abase[1][1]/m/m,0,0,0,0},
303: {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},
304: {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},
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[0][0]/m,Abase[0][1]/m},
306: {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}},
307: 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},
308: 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},
309: 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},
310: */
311: /*{
312: const PetscReal
313: As[8][8] = {{0,0,0,0,0,0,0,0},
314: {RC(1.0)/RC(2.0),0,0,0,0,0,0,0},
315: {RC(-1.0)/RC(6.0),RC(2.0)/RC(3.0),0,0,0,0,0,0},
316: {RC(1.0)/RC(3.0),RC(-1.0)/RC(3.0),RC(1.0),0,0,0,0,0},
317: {0,0,0,0,0,0,0,0},
318: {0,0,0,0,RC(1.0)/RC(2.0),0,0,0},
319: {0,0,0,0,RC(-1.0)/RC(6.0),RC(2.0)/RC(3.0),0,0},
320: {0,0,0,0,RC(1.0)/RC(3.0),RC(-1.0)/RC(3.0),RC(1.0),0}},
321: A[8][8] = {{0,0,0,0,0,0,0,0},
322: {RC(1.0)/RC(4.0),0,0,0,0,0,0,0},
323: {RC(-1.0)/RC(12.0),RC(1.0)/RC(3.0),0,0,0,0,0,0},
324: {RC(1.0)/RC(6.0),RC(-1.0)/RC(6.0),RC(1.0)/RC(2.0),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),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(4.0),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(12.0),RC(1.0)/RC(3.0),0,0},
328: {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}},
329: 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)},
330: 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)};
331: TSMPRKRegister(TSMPRKPM3,3,8,&As[0][0],bs,NULL,&A[0][0],b,NULL);
332: }*/
334: {
335: const PetscReal
336: Asb[5][5] = {{0,0,0,0,0},
337: {RC(1.0)/RC(2.0),0,0,0,0},
338: {RC(1.0)/RC(2.0),0,0,0,0},
339: {RC(1.0),0,0,0,0},
340: {RC(1.0),0,0,0,0}},
341: Af[5][5] = {{0,0,0,0,0},
342: {RC(1.0)/RC(2.0),0,0,0,0},
343: {RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),0,0,0},
344: {RC(1.0)/RC(4.0),RC(1.0)/RC(4.0),RC(1.0)/RC(2.0),0,0},
345: {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}},
346: bsb[5] = {RC(1.0)/RC(2.0),0,0,0,RC(1.0)/RC(2.0)},
347: 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};
348: const PetscInt
349: rsb[5] = {0,0,2,0,4};
350: TSMPRKRegister(TSMPRKP2,2,5,1,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
351: }
353: {
354: const PetscReal
355: Asb[10][10] = {{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(4.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(2.0),0,0,0,0,0,0,0,0,0},
360: {RC(-1.0)/RC(6.0),0,0,0,RC(2.0)/RC(3.0),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(12.0),0,0,0,RC(1.0)/RC(6.0),RC(1.0)/RC(2.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: {RC(1.0)/RC(3.0),0,0,0,RC(-1.0)/RC(3.0),RC(1.0),0,0,0,0}},
365: Af[10][10] = {{0,0,0,0,0,0,0,0,0,0},
366: {RC(1.0)/RC(4.0),0,0,0,0,0,0,0,0,0},
367: {RC(-1.0)/RC(12.0),RC(1.0)/RC(3.0),0,0,0,0,0,0,0,0},
368: {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},
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,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(4.0),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(12.0),RC(1.0)/RC(3.0),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(6.0),RC(-1.0)/RC(6.0),RC(1.0)/RC(2.0),0,0},
374: {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}},
375: 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)},
376: 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};
377: const PetscInt
378: rsb[10] = {0,0,2,0,4,0,0,7,0,9};
379: TSMPRKRegister(TSMPRKP3,3,5,2,1,&Asb[0][0],bsb,NULL,rsb,NULL,NULL,NULL,NULL,&Af[0][0],bf,NULL);
380: }
381: #undef RC
382: return(0);
383: }
385: /*@C
386: TSMPRKRegisterDestroy - Frees the list of schemes that were registered by TSMPRKRegister().
388: Not Collective
390: Level: advanced
392: .seealso: TSMPRKRegister(), TSMPRKRegisterAll()
393: @*/
394: PetscErrorCode TSMPRKRegisterDestroy(void)
395: {
397: MPRKTableauLink link;
400: while ((link = MPRKTableauList)) {
401: MPRKTableau t = &link->tab;
402: MPRKTableauList = link->next;
403: PetscFree3(t->Asb,t->bsb,t->csb);
404: PetscFree3(t->Amb,t->bmb,t->cmb);
405: PetscFree3(t->Af,t->bf,t->cf);
406: PetscFree(t->rsb);
407: PetscFree(t->rmb);
408: PetscFree(t->name);
409: PetscFree(link);
410: }
411: TSMPRKRegisterAllCalled = PETSC_FALSE;
412: return(0);
413: }
415: /*@C
416: TSMPRKInitializePackage - This function initializes everything in the TSMPRK package. It is called
417: from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_MPRK()
418: when using static libraries.
420: Level: developer
422: .seealso: PetscInitialize()
423: @*/
424: PetscErrorCode TSMPRKInitializePackage(void)
425: {
429: if (TSMPRKPackageInitialized) return(0);
430: TSMPRKPackageInitialized = PETSC_TRUE;
431: TSMPRKRegisterAll();
432: PetscRegisterFinalize(TSMPRKFinalizePackage);
433: return(0);
434: }
436: /*@C
437: TSMPRKFinalizePackage - This function destroys everything in the TSMPRK package. It is
438: called from PetscFinalize().
440: Level: developer
442: .seealso: PetscFinalize()
443: @*/
444: PetscErrorCode TSMPRKFinalizePackage(void)
445: {
449: TSMPRKPackageInitialized = PETSC_FALSE;
450: TSMPRKRegisterDestroy();
451: return(0);
452: }
454: /*@C
455: TSMPRKRegister - register a MPRK scheme by providing the entries in the Butcher tableau
457: Not Collective, but the same schemes should be registered on all processes on which they will be used
459: Input Parameters:
460: + name - identifier for method
461: . order - approximation order of method
462: . s - number of stages in the base methods
463: . ratio1 - stepsize ratio at 1st level (e.g. slow/medium)
464: . ratio2 - stepsize ratio at 2nd level (e.g. medium/fast)
465: . Af - stage coefficients for fast components(dimension s*s, row-major)
466: . bf - step completion table for fast components(dimension s)
467: . cf - abscissa for fast components(dimension s)
468: . As - stage coefficients for slow components(dimension s*s, row-major)
469: . bs - step completion table for slow components(dimension s)
470: - cs - abscissa for slow components(dimension s)
472: Notes:
473: Several MPRK methods are provided, this function is only needed to create new methods.
475: Level: advanced
477: .seealso: TSMPRK
478: @*/
479: PetscErrorCode TSMPRKRegister(TSMPRKType name,PetscInt order,
480: PetscInt sbase,PetscInt ratio1,PetscInt ratio2,
481: const PetscReal Asb[],const PetscReal bsb[],const PetscReal csb[],const PetscInt rsb[],
482: const PetscReal Amb[],const PetscReal bmb[],const PetscReal cmb[],const PetscInt rmb[],
483: const PetscReal Af[],const PetscReal bf[],const PetscReal cf[])
484: {
485: MPRKTableauLink link;
486: MPRKTableau t;
487: PetscInt s,i,j;
488: PetscErrorCode ierr;
504: PetscNew(&link);
505: t = &link->tab;
507: PetscStrallocpy(name,&t->name);
508: s = sbase*ratio1*ratio2; /* this is the dimension of the matrices below */
509: t->order = order;
510: t->sbase = sbase;
511: t->s = s;
512: t->np = 2;
514: PetscMalloc3(s*s,&t->Af,s,&t->bf,s,&t->cf);
515: PetscArraycpy(t->Af,Af,s*s);
516: if (bf) {
517: PetscArraycpy(t->bf,bf,s);
518: } else
519: for (i=0; i<s; i++) t->bf[i] = Af[(s-1)*s+i];
520: if (cf) {
521: PetscArraycpy(t->cf,cf,s);
522: } else {
523: for (i=0; i<s; i++)
524: for (j=0,t->cf[i]=0; j<s; j++)
525: t->cf[i] += Af[i*s+j];
526: }
528: if (Amb) {
529: t->np = 3;
530: PetscMalloc3(s*s,&t->Amb,s,&t->bmb,s,&t->cmb);
531: PetscArraycpy(t->Amb,Amb,s*s);
532: if (bmb) {
533: PetscArraycpy(t->bmb,bmb,s);
534: } else {
535: for (i=0; i<s; i++) t->bmb[i] = Amb[(s-1)*s+i];
536: }
537: if (cmb) {
538: PetscArraycpy(t->cmb,cmb,s);
539: } else {
540: for (i=0; i<s; i++)
541: for (j=0,t->cmb[i]=0; j<s; j++)
542: t->cmb[i] += Amb[i*s+j];
543: }
544: if (rmb) {
545: PetscMalloc1(s,&t->rmb);
546: PetscArraycpy(t->rmb,rmb,s);
547: } else {
548: PetscCalloc1(s,&t->rmb);
549: }
550: }
552: PetscMalloc3(s*s,&t->Asb,s,&t->bsb,s,&t->csb);
553: PetscArraycpy(t->Asb,Asb,s*s);
554: if (bsb) {
555: PetscArraycpy(t->bsb,bsb,s);
556: } else
557: for (i=0; i<s; i++) t->bsb[i] = Asb[(s-1)*s+i];
558: if (csb) {
559: PetscArraycpy(t->csb,csb,s);
560: } else {
561: for (i=0; i<s; i++)
562: for (j=0,t->csb[i]=0; j<s; j++)
563: t->csb[i] += Asb[i*s+j];
564: }
565: if (rsb) {
566: PetscMalloc1(s,&t->rsb);
567: PetscArraycpy(t->rsb,rsb,s);
568: } else {
569: PetscCalloc1(s,&t->rsb);
570: }
571: link->next = MPRKTableauList;
572: MPRKTableauList = link;
573: return(0);
574: }
576: static PetscErrorCode TSMPRKSetSplits(TS ts)
577: {
578: TS_MPRK *mprk = (TS_MPRK*)ts->data;
579: MPRKTableau tab = mprk->tableau;
580: DM dm,subdm,newdm;
584: TSRHSSplitGetSubTS(ts,"slow",&mprk->subts_slow);
585: TSRHSSplitGetSubTS(ts,"fast",&mprk->subts_fast);
586: if (!mprk->subts_slow || !mprk->subts_fast) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must set up the RHSFunctions for 'slow' and 'fast' components using TSRHSSplitSetRHSFunction() or calling TSSetRHSFunction() for each sub-TS");
588: /* Only copy the DM */
589: TSGetDM(ts,&dm);
591: TSRHSSplitGetSubTS(ts,"slowbuffer",&mprk->subts_slowbuffer);
592: if (!mprk->subts_slowbuffer) {
593: mprk->subts_slowbuffer = mprk->subts_slow;
594: mprk->subts_slow = NULL;
595: }
596: if (mprk->subts_slow) {
597: DMClone(dm,&newdm);
598: TSGetDM(mprk->subts_slow,&subdm);
599: DMCopyDMTS(subdm,newdm);
600: DMCopyDMSNES(subdm,newdm);
601: TSSetDM(mprk->subts_slow,newdm);
602: DMDestroy(&newdm);
603: }
604: DMClone(dm,&newdm);
605: TSGetDM(mprk->subts_slowbuffer,&subdm);
606: DMCopyDMTS(subdm,newdm);
607: DMCopyDMSNES(subdm,newdm);
608: TSSetDM(mprk->subts_slowbuffer,newdm);
609: DMDestroy(&newdm);
611: DMClone(dm,&newdm);
612: TSGetDM(mprk->subts_fast,&subdm);
613: DMCopyDMTS(subdm,newdm);
614: DMCopyDMSNES(subdm,newdm);
615: TSSetDM(mprk->subts_fast,newdm);
616: DMDestroy(&newdm);
618: if (tab->np == 3) {
619: TSRHSSplitGetSubTS(ts,"medium",&mprk->subts_medium);
620: TSRHSSplitGetSubTS(ts,"mediumbuffer",&mprk->subts_mediumbuffer);
621: if (mprk->subts_medium && !mprk->subts_mediumbuffer) {
622: mprk->subts_mediumbuffer = mprk->subts_medium;
623: mprk->subts_medium = NULL;
624: }
625: if (mprk->subts_medium) {
626: DMClone(dm,&newdm);
627: TSGetDM(mprk->subts_medium,&subdm);
628: DMCopyDMTS(subdm,newdm);
629: DMCopyDMSNES(subdm,newdm);
630: TSSetDM(mprk->subts_medium,newdm);
631: DMDestroy(&newdm);
632: }
633: DMClone(dm,&newdm);
634: TSGetDM(mprk->subts_mediumbuffer,&subdm);
635: DMCopyDMTS(subdm,newdm);
636: DMCopyDMSNES(subdm,newdm);
637: TSSetDM(mprk->subts_mediumbuffer,newdm);
638: DMDestroy(&newdm);
639: }
640: return(0);
641: }
643: /*
644: This if for nonsplit RHS MPRK
645: The step completion formula is
647: x1 = x0 + h b^T YdotRHS
649: */
650: static PetscErrorCode TSEvaluateStep_MPRK(TS ts,PetscInt order,Vec X,PetscBool *done)
651: {
652: TS_MPRK *mprk = (TS_MPRK*)ts->data;
653: MPRKTableau tab = mprk->tableau;
654: PetscScalar *wf = mprk->work_fast;
655: PetscReal h = ts->time_step;
656: PetscInt s = tab->s,j;
660: for (j=0; j<s; j++) wf[j] = h*tab->bf[j];
661: VecCopy(ts->vec_sol,X);
662: VecMAXPY(X,s,wf,mprk->YdotRHS);
663: return(0);
664: }
666: static PetscErrorCode TSStep_MPRK(TS ts)
667: {
668: TS_MPRK *mprk = (TS_MPRK*)ts->data;
669: Vec *Y = mprk->Y,*YdotRHS = mprk->YdotRHS,*YdotRHS_fast = mprk->YdotRHS_fast,*YdotRHS_slow = mprk->YdotRHS_slow,*YdotRHS_slowbuffer = mprk->YdotRHS_slowbuffer;
670: Vec Yslow,Yslowbuffer,Yfast;
671: MPRKTableau tab = mprk->tableau;
672: const PetscInt s = tab->s;
673: const PetscReal *Af = tab->Af,*cf = tab->cf,*Asb = tab->Asb,*csb = tab->csb;
674: PetscScalar *wf = mprk->work_fast,*wsb = mprk->work_slowbuffer;
675: PetscInt i,j;
676: PetscReal next_time_step = ts->time_step,t = ts->ptime,h = ts->time_step;
677: PetscErrorCode ierr;
680: for (i=0; i<s; i++) {
681: mprk->stage_time = t + h*cf[i];
682: TSPreStage(ts,mprk->stage_time);
683: VecCopy(ts->vec_sol,Y[i]);
685: /* slow buffer region */
686: for (j=0; j<i; j++) wsb[j] = h*Asb[i*s+j];
687: for (j=0; j<i; j++) {
688: VecGetSubVector(YdotRHS[j],mprk->is_slowbuffer,&YdotRHS_slowbuffer[j]);
689: }
690: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
691: VecMAXPY(Yslowbuffer,i,wsb,mprk->YdotRHS_slowbuffer);
692: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
693: for (j=0; j<i; j++) {
694: VecRestoreSubVector(YdotRHS[j],mprk->is_slowbuffer,&YdotRHS_slowbuffer[j]);
695: }
696: /* slow region */
697: if (mprk->is_slow) {
698: for (j=0; j<i; j++) {
699: VecGetSubVector(YdotRHS[j],mprk->is_slow,&YdotRHS_slow[j]);
700: }
701: VecGetSubVector(Y[i],mprk->is_slow,&Yslow);
702: VecMAXPY(Yslow,i,wsb,mprk->YdotRHS_slow);
703: VecRestoreSubVector(Y[i],mprk->is_slow,&Yslow);
704: for (j=0; j<i; j++) {
705: VecRestoreSubVector(YdotRHS[j],mprk->is_slow,&YdotRHS_slow[j]);
706: }
707: }
709: /* fast region */
710: for (j=0; j<i; j++) wf[j] = h*Af[i*s+j];
711: for (j=0; j<i; j++) {
712: VecGetSubVector(YdotRHS[j],mprk->is_fast,&YdotRHS_fast[j]);
713: }
714: VecGetSubVector(Y[i],mprk->is_fast,&Yfast);
715: VecMAXPY(Yfast,i,wf,mprk->YdotRHS_fast);
716: VecRestoreSubVector(Y[i],mprk->is_fast,&Yfast);
717: for (j=0; j<i; j++) {
718: VecRestoreSubVector(YdotRHS[j],mprk->is_fast,&YdotRHS_fast[j]);
719: }
720: if (tab->np == 3) {
721: Vec *YdotRHS_medium = mprk->YdotRHS_medium,*YdotRHS_mediumbuffer = mprk->YdotRHS_mediumbuffer;
722: Vec Ymedium,Ymediumbuffer;
723: PetscScalar *wmb = mprk->work_mediumbuffer;
725: for (j=0; j<i; j++) wmb[j] = h*tab->Amb[i*s+j];
726: /* medium region */
727: if (mprk->is_medium) {
728: for (j=0; j<i; j++) {
729: VecGetSubVector(YdotRHS[j],mprk->is_medium,&YdotRHS_medium[j]);
730: }
731: VecGetSubVector(Y[i],mprk->is_medium,&Ymedium);
732: VecMAXPY(Ymedium,i,wmb,mprk->YdotRHS_medium);
733: VecRestoreSubVector(Y[i],mprk->is_medium,&Ymedium);
734: for (j=0; j<i; j++) {
735: VecRestoreSubVector(YdotRHS[j],mprk->is_medium,&YdotRHS_medium[j]);
736: }
737: }
738: /* medium buffer region */
739: for (j=0; j<i; j++) {
740: VecGetSubVector(YdotRHS[j],mprk->is_mediumbuffer,&YdotRHS_mediumbuffer[j]);
741: }
742: VecGetSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
743: VecMAXPY(Ymediumbuffer,i,wmb,mprk->YdotRHS_mediumbuffer);
744: VecRestoreSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
745: for (j=0; j<i; j++) {
746: VecRestoreSubVector(YdotRHS[j],mprk->is_mediumbuffer,&YdotRHS_mediumbuffer[j]);
747: }
748: }
749: TSPostStage(ts,mprk->stage_time,i,Y);
750: /* compute the stage RHS by fast and slow tableau respectively */
751: TSComputeRHSFunction(ts,t+h*csb[i],Y[i],YdotRHS[i]);
752: }
753: TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);
754: ts->ptime += ts->time_step;
755: ts->time_step = next_time_step;
756: return(0);
757: }
759: /*
760: This if for the case when split RHS is used
761: The step completion formula is
762: x1 = x0 + h b^T YdotRHS
763: */
764: static PetscErrorCode TSEvaluateStep_MPRKSPLIT(TS ts,PetscInt order,Vec X,PetscBool *done)
765: {
766: TS_MPRK *mprk = (TS_MPRK*)ts->data;
767: MPRKTableau tab = mprk->tableau;
768: Vec Xslow,Xfast,Xslowbuffer; /* subvectors for slow and fast componets in X respectively */
769: PetscScalar *wf = mprk->work_fast,*ws = mprk->work_slow,*wsb = mprk->work_slowbuffer;
770: PetscReal h = ts->time_step;
771: PetscInt s = tab->s,j,computedstages;
775: VecCopy(ts->vec_sol,X);
777: /* slow region */
778: if (mprk->is_slow) {
779: computedstages = 0;
780: for (j=0; j<s; j++) {
781: if (tab->rsb[j]) ws[tab->rsb[j]-1] += h*tab->bsb[j];
782: else ws[computedstages++] = h*tab->bsb[j];
783: }
784: VecGetSubVector(X,mprk->is_slow,&Xslow);
785: VecMAXPY(Xslow,computedstages,ws,mprk->YdotRHS_slow);
786: VecRestoreSubVector(X,mprk->is_slow,&Xslow);
787: }
789: if (tab->np == 3 && mprk->is_medium) {
790: computedstages = 0;
791: for (j=0; j<s; j++) {
792: if (tab->rmb[j]) wsb[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += h*tab->bsb[j];
793: else wsb[computedstages++] = h*tab->bsb[j];
794: }
795: VecGetSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
796: VecMAXPY(Xslowbuffer,computedstages,wsb,mprk->YdotRHS_slowbuffer);
797: VecRestoreSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
798: } else {
799: /* slow buffer region */
800: for (j=0; j<s; j++) wsb[j] = h*tab->bsb[j];
801: VecGetSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
802: VecMAXPY(Xslowbuffer,s,wsb,mprk->YdotRHS_slowbuffer);
803: VecRestoreSubVector(X,mprk->is_slowbuffer,&Xslowbuffer);
804: }
805: if (tab->np == 3) {
806: Vec Xmedium,Xmediumbuffer;
807: PetscScalar *wm = mprk->work_medium,*wmb = mprk->work_mediumbuffer;
808: /* medium region and slow buffer region */
809: if (mprk->is_medium) {
810: computedstages = 0;
811: for (j=0; j<s; j++) {
812: if (tab->rmb[j]) wm[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += h*tab->bmb[j];
813: else wm[computedstages++] = h*tab->bmb[j];
814: }
815: VecGetSubVector(X,mprk->is_medium,&Xmedium);
816: VecMAXPY(Xmedium,computedstages,wm,mprk->YdotRHS_medium);
817: VecRestoreSubVector(X,mprk->is_medium,&Xmedium);
818: }
819: /* medium buffer region */
820: for (j=0; j<s; j++) wmb[j] = h*tab->bmb[j];
821: VecGetSubVector(X,mprk->is_mediumbuffer,&Xmediumbuffer);
822: VecMAXPY(Xmediumbuffer,s,wmb,mprk->YdotRHS_mediumbuffer);
823: VecRestoreSubVector(X,mprk->is_mediumbuffer,&Xmediumbuffer);
824: }
825: /* fast region */
826: for (j=0; j<s; j++) wf[j] = h*tab->bf[j];
827: VecGetSubVector(X,mprk->is_fast,&Xfast);
828: VecMAXPY(Xfast,s,wf,mprk->YdotRHS_fast);
829: VecRestoreSubVector(X,mprk->is_fast,&Xfast);
830: return(0);
831: }
833: static PetscErrorCode TSStep_MPRKSPLIT(TS ts)
834: {
835: TS_MPRK *mprk = (TS_MPRK*)ts->data;
836: MPRKTableau tab = mprk->tableau;
837: Vec *Y = mprk->Y,*YdotRHS_fast = mprk->YdotRHS_fast,*YdotRHS_slow = mprk->YdotRHS_slow,*YdotRHS_slowbuffer = mprk->YdotRHS_slowbuffer;
838: Vec Yslow,Yslowbuffer,Yfast; /* subvectors for slow and fast components in Y[i] respectively */
839: PetscInt s = tab->s;
840: const PetscReal *Af = tab->Af,*cf = tab->cf,*Asb = tab->Asb,*csb = tab->csb;
841: PetscScalar *wf = mprk->work_fast,*ws = mprk->work_slow,*wsb = mprk->work_slowbuffer;
842: PetscInt i,j,computedstages;
843: PetscReal next_time_step = ts->time_step,t = ts->ptime,h = ts->time_step;
844: PetscErrorCode ierr;
847: for (i=0; i<s; i++) {
848: mprk->stage_time = t + h*cf[i];
849: TSPreStage(ts,mprk->stage_time);
850: /* calculate the stage value for fast and slow components respectively */
851: VecCopy(ts->vec_sol,Y[i]);
852: for (j=0; j<i; j++) wsb[j] = h*Asb[i*s+j];
854: /* slow buffer region */
855: if (tab->np == 3 && mprk->is_medium) {
856: if (tab->rmb[i]) {
857: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
858: VecISCopy(Y[tab->rmb[i]-1],mprk->is_slowbuffer,SCATTER_REVERSE,Yslowbuffer);
859: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
860: } else {
861: PetscScalar *wm = mprk->work_medium;
862: computedstages = 0;
863: for (j=0; j<i; j++) {
864: if (tab->rmb[j]) wm[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += wsb[j];
865: else wm[computedstages++] = wsb[j];
866: }
867: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
868: VecMAXPY(Yslowbuffer,computedstages,wm,YdotRHS_slowbuffer);
869: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
870: }
871: } else {
872: VecGetSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
873: VecMAXPY(Yslowbuffer,i,wsb,YdotRHS_slowbuffer);
874: VecRestoreSubVector(Y[i],mprk->is_slowbuffer,&Yslowbuffer);
875: }
877: /* slow region */
878: if (mprk->is_slow) {
879: if (tab->rsb[i]) { /* repeat previous stage */
880: VecGetSubVector(Y[i],mprk->is_slow,&Yslow);
881: VecISCopy(Y[tab->rsb[i]-1],mprk->is_slow,SCATTER_REVERSE,Yslow);
882: VecRestoreSubVector(Y[i],mprk->is_slow,&Yslow);
883: } else {
884: computedstages = 0;
885: for (j=0; j<i; j++) {
886: if (tab->rsb[j]) ws[tab->rsb[j]-1] += wsb[j];
887: else ws[computedstages++] = wsb[j];
888: }
889: VecGetSubVector(Y[i],mprk->is_slow,&Yslow);
890: VecMAXPY(Yslow,computedstages,ws,YdotRHS_slow);
891: VecRestoreSubVector(Y[i],mprk->is_slow,&Yslow);
892: /* only depends on the slow buffer region */
893: TSComputeRHSFunction(mprk->subts_slow,t+h*csb[i],Y[i],YdotRHS_slow[computedstages]);
894: }
895: }
897: /* fast region */
898: for (j=0; j<i; j++) wf[j] = h*Af[i*s+j];
899: VecGetSubVector(Y[i],mprk->is_fast,&Yfast);
900: VecMAXPY(Yfast,i,wf,YdotRHS_fast);
901: VecRestoreSubVector(Y[i],mprk->is_fast,&Yfast);
903: if (tab->np == 3) {
904: Vec *YdotRHS_medium = mprk->YdotRHS_medium,*YdotRHS_mediumbuffer = mprk->YdotRHS_mediumbuffer;
905: Vec Ymedium,Ymediumbuffer;
906: const PetscReal *Amb = tab->Amb,*cmb = tab->cmb;
907: PetscScalar *wm = mprk->work_medium,*wmb = mprk->work_mediumbuffer;
909: for (j=0; j<i; j++) wmb[j] = h*Amb[i*s+j];
910: /* medium buffer region */
911: VecGetSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
912: VecMAXPY(Ymediumbuffer,i,wmb,YdotRHS_mediumbuffer);
913: VecRestoreSubVector(Y[i],mprk->is_mediumbuffer,&Ymediumbuffer);
914: /* medium region */
915: if (mprk->is_medium) {
916: if (tab->rmb[i]) { /* repeat previous stage */
917: VecGetSubVector(Y[i],mprk->is_medium,&Ymedium);
918: VecISCopy(Y[tab->rmb[i]-1],mprk->is_medium,SCATTER_REVERSE,Ymedium);
919: VecRestoreSubVector(Y[i],mprk->is_medium,&Ymedium);
920: } else {
921: computedstages = 0;
922: for (j=0; j<i; j++) {
923: if (tab->rmb[j]) wm[computedstages-tab->sbase+(tab->rmb[j]-1)%tab->sbase] += wmb[j];
924: else wm[computedstages++] = wmb[j];
926: }
927: VecGetSubVector(Y[i],mprk->is_medium,&Ymedium);
928: VecMAXPY(Ymedium,computedstages,wm,YdotRHS_medium);
929: VecRestoreSubVector(Y[i],mprk->is_medium,&Ymedium);
930: /* only depends on the medium buffer region */
931: TSComputeRHSFunction(mprk->subts_medium,t+h*cmb[i],Y[i],YdotRHS_medium[computedstages]);
932: /* must be computed after all regions are updated in Y */
933: TSComputeRHSFunction(mprk->subts_slowbuffer,t+h*csb[i],Y[i],YdotRHS_slowbuffer[computedstages]);
934: }
935: }
936: /* must be computed after fast region and slow region are updated in Y */
937: TSComputeRHSFunction(mprk->subts_mediumbuffer,t+h*cmb[i],Y[i],YdotRHS_mediumbuffer[i]);
938: }
939: if (!(tab->np == 3 && mprk->is_medium)) {
940: TSComputeRHSFunction(mprk->subts_slowbuffer,t+h*csb[i],Y[i],YdotRHS_slowbuffer[i]);
941: }
942: TSComputeRHSFunction(mprk->subts_fast,t+h*cf[i],Y[i],YdotRHS_fast[i]);
943: }
945: TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);
946: ts->ptime += ts->time_step;
947: ts->time_step = next_time_step;
948: return(0);
949: }
951: static PetscErrorCode TSMPRKTableauReset(TS ts)
952: {
953: TS_MPRK *mprk = (TS_MPRK*)ts->data;
954: MPRKTableau tab = mprk->tableau;
958: if (!tab) return(0);
959: PetscFree(mprk->work_fast);
960: PetscFree(mprk->work_slow);
961: PetscFree(mprk->work_slowbuffer);
962: PetscFree(mprk->work_medium);
963: PetscFree(mprk->work_mediumbuffer);
964: VecDestroyVecs(tab->s,&mprk->Y);
965: if (ts->use_splitrhsfunction) {
966: VecDestroyVecs(tab->s,&mprk->YdotRHS_fast);
967: VecDestroyVecs(tab->s,&mprk->YdotRHS_slow);
968: VecDestroyVecs(tab->s,&mprk->YdotRHS_slowbuffer);
969: VecDestroyVecs(tab->s,&mprk->YdotRHS_medium);
970: VecDestroyVecs(tab->s,&mprk->YdotRHS_mediumbuffer);
971: } else {
972: VecDestroyVecs(tab->s,&mprk->YdotRHS);
973: if (mprk->is_slow) {
974: PetscFree(mprk->YdotRHS_slow);
975: }
976: PetscFree(mprk->YdotRHS_slowbuffer);
977: if (tab->np == 3) {
978: if (mprk->is_medium) {
979: PetscFree(mprk->YdotRHS_medium);
980: }
981: PetscFree(mprk->YdotRHS_mediumbuffer);
982: }
983: PetscFree(mprk->YdotRHS_fast);
984: }
985: return(0);
986: }
988: static PetscErrorCode TSReset_MPRK(TS ts)
989: {
993: TSMPRKTableauReset(ts);
994: return(0);
995: }
997: static PetscErrorCode DMCoarsenHook_TSMPRK(DM fine,DM coarse,void *ctx)
998: {
1000: return(0);
1001: }
1003: static PetscErrorCode DMRestrictHook_TSMPRK(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
1004: {
1006: return(0);
1007: }
1009: static PetscErrorCode DMSubDomainHook_TSMPRK(DM dm,DM subdm,void *ctx)
1010: {
1012: return(0);
1013: }
1015: static PetscErrorCode DMSubDomainRestrictHook_TSMPRK(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
1016: {
1018: return(0);
1019: }
1021: static PetscErrorCode TSMPRKTableauSetUp(TS ts)
1022: {
1023: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1024: MPRKTableau tab = mprk->tableau;
1025: Vec YdotRHS_slow,YdotRHS_slowbuffer,YdotRHS_medium,YdotRHS_mediumbuffer,YdotRHS_fast;
1029: VecDuplicateVecs(ts->vec_sol,tab->s,&mprk->Y);
1030: if (mprk->is_slow) {
1031: PetscMalloc1(tab->s,&mprk->work_slow);
1032: }
1033: PetscMalloc1(tab->s,&mprk->work_slowbuffer);
1034: if (tab->np == 3) {
1035: if (mprk->is_medium) {
1036: PetscMalloc1(tab->s,&mprk->work_medium);
1037: }
1038: PetscMalloc1(tab->s,&mprk->work_mediumbuffer);
1039: }
1040: PetscMalloc1(tab->s,&mprk->work_fast);
1042: if (ts->use_splitrhsfunction) {
1043: if (mprk->is_slow) {
1044: VecGetSubVector(ts->vec_sol,mprk->is_slow,&YdotRHS_slow);
1045: VecDuplicateVecs(YdotRHS_slow,tab->s,&mprk->YdotRHS_slow);
1046: VecRestoreSubVector(ts->vec_sol,mprk->is_slow,&YdotRHS_slow);
1047: }
1048: VecGetSubVector(ts->vec_sol,mprk->is_slowbuffer,&YdotRHS_slowbuffer);
1049: VecDuplicateVecs(YdotRHS_slowbuffer,tab->s,&mprk->YdotRHS_slowbuffer);
1050: VecRestoreSubVector(ts->vec_sol,mprk->is_slowbuffer,&YdotRHS_slowbuffer);
1051: if (tab->np == 3) {
1052: if (mprk->is_medium) {
1053: VecGetSubVector(ts->vec_sol,mprk->is_medium,&YdotRHS_medium);
1054: VecDuplicateVecs(YdotRHS_medium,tab->s,&mprk->YdotRHS_medium);
1055: VecRestoreSubVector(ts->vec_sol,mprk->is_medium,&YdotRHS_medium);
1056: }
1057: VecGetSubVector(ts->vec_sol,mprk->is_mediumbuffer,&YdotRHS_mediumbuffer);
1058: VecDuplicateVecs(YdotRHS_mediumbuffer,tab->s,&mprk->YdotRHS_mediumbuffer);
1059: VecRestoreSubVector(ts->vec_sol,mprk->is_mediumbuffer,&YdotRHS_mediumbuffer);
1060: }
1061: VecGetSubVector(ts->vec_sol,mprk->is_fast,&YdotRHS_fast);
1062: VecDuplicateVecs(YdotRHS_fast,tab->s,&mprk->YdotRHS_fast);
1063: VecRestoreSubVector(ts->vec_sol,mprk->is_fast,&YdotRHS_fast);
1064: } else {
1065: VecDuplicateVecs(ts->vec_sol,tab->s,&mprk->YdotRHS);
1066: if (mprk->is_slow) {
1067: PetscMalloc1(tab->s,&mprk->YdotRHS_slow);
1068: }
1069: PetscMalloc1(tab->s,&mprk->YdotRHS_slowbuffer);
1070: if (tab->np == 3) {
1071: if (mprk->is_medium) {
1072: PetscMalloc1(tab->s,&mprk->YdotRHS_medium);
1073: }
1074: PetscMalloc1(tab->s,&mprk->YdotRHS_mediumbuffer);
1075: }
1076: PetscMalloc1(tab->s,&mprk->YdotRHS_fast);
1077: }
1078: return(0);
1079: }
1081: static PetscErrorCode TSSetUp_MPRK(TS ts)
1082: {
1083: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1084: MPRKTableau tab = mprk->tableau;
1085: DM dm;
1089: TSRHSSplitGetIS(ts,"slow",&mprk->is_slow);
1090: TSRHSSplitGetIS(ts,"fast",&mprk->is_fast);
1091: if (!mprk->is_slow || !mprk->is_fast) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must set up RHSSplits with TSRHSSplitSetIS() using split names 'slow' and 'fast' respectively in order to use the method '%s'",tab->name);
1093: if (tab->np == 3) {
1094: TSRHSSplitGetIS(ts,"medium",&mprk->is_medium);
1095: if (!mprk->is_medium) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must set up RHSSplits with TSRHSSplitSetIS() using split names 'slow' and 'medium' and 'fast' respectively in order to use the method '%s'",tab->name);
1096: TSRHSSplitGetIS(ts,"mediumbuffer",&mprk->is_mediumbuffer);
1097: if (!mprk->is_mediumbuffer) { /* let medium buffer cover whole medium region */
1098: mprk->is_mediumbuffer = mprk->is_medium;
1099: mprk->is_medium = NULL;
1100: }
1101: }
1103: /* If users do not provide buffer region settings, the solver will do them automatically, but with a performance penalty */
1104: TSRHSSplitGetIS(ts,"slowbuffer",&mprk->is_slowbuffer);
1105: if (!mprk->is_slowbuffer) { /* let slow buffer cover whole slow region */
1106: mprk->is_slowbuffer = mprk->is_slow;
1107: mprk->is_slow = NULL;
1108: }
1109: TSCheckImplicitTerm(ts);
1110: TSMPRKTableauSetUp(ts);
1111: TSGetDM(ts,&dm);
1112: DMCoarsenHookAdd(dm,DMCoarsenHook_TSMPRK,DMRestrictHook_TSMPRK,ts);
1113: DMSubDomainHookAdd(dm,DMSubDomainHook_TSMPRK,DMSubDomainRestrictHook_TSMPRK,ts);
1114: if (ts->use_splitrhsfunction) {
1115: ts->ops->step = TSStep_MPRKSPLIT;
1116: ts->ops->evaluatestep = TSEvaluateStep_MPRKSPLIT;
1117: TSMPRKSetSplits(ts);
1118: } else {
1119: ts->ops->step = TSStep_MPRK;
1120: ts->ops->evaluatestep = TSEvaluateStep_MPRK;
1121: }
1122: return(0);
1123: }
1125: static PetscErrorCode TSSetFromOptions_MPRK(PetscOptionItems *PetscOptionsObject,TS ts)
1126: {
1127: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1131: PetscOptionsHead(PetscOptionsObject,"PRK ODE solver options");
1132: {
1133: MPRKTableauLink link;
1134: PetscInt count,choice;
1135: PetscBool flg;
1136: const char **namelist;
1137: for (link=MPRKTableauList,count=0; link; link=link->next,count++) ;
1138: PetscMalloc1(count,(char***)&namelist);
1139: for (link=MPRKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
1140: PetscOptionsEList("-ts_mprk_type","Family of MPRK method","TSMPRKSetType",(const char*const*)namelist,count,mprk->tableau->name,&choice,&flg);
1141: if (flg) {TSMPRKSetType(ts,namelist[choice]);}
1142: PetscFree(namelist);
1143: }
1144: PetscOptionsTail();
1145: return(0);
1146: }
1148: static PetscErrorCode TSView_MPRK(TS ts,PetscViewer viewer)
1149: {
1150: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1151: PetscBool iascii;
1155: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1156: if (iascii) {
1157: MPRKTableau tab = mprk->tableau;
1158: TSMPRKType mprktype;
1159: char fbuf[512];
1160: char sbuf[512];
1161: PetscInt i;
1162: TSMPRKGetType(ts,&mprktype);
1163: PetscViewerASCIIPrintf(viewer," MPRK type %s\n",mprktype);
1164: PetscViewerASCIIPrintf(viewer," Order: %D\n",tab->order);
1166: PetscFormatRealArray(fbuf,sizeof(fbuf),"% 8.6f",tab->s,tab->cf);
1167: PetscViewerASCIIPrintf(viewer," Abscissa cf = %s\n",fbuf);
1168: PetscViewerASCIIPrintf(viewer," Af = \n");
1169: for (i=0; i<tab->s; i++) {
1170: PetscFormatRealArray(fbuf,sizeof(fbuf),"% 8.6f",tab->s,&tab->Af[i*tab->s]);
1171: PetscViewerASCIIPrintf(viewer," %s\n",fbuf);
1172: }
1173: PetscFormatRealArray(fbuf,sizeof(fbuf),"% 8.6f",tab->s,tab->bf);
1174: PetscViewerASCIIPrintf(viewer," bf = %s\n",fbuf);
1176: PetscFormatRealArray(sbuf,sizeof(sbuf),"% 8.6f",tab->s,tab->csb);
1177: PetscViewerASCIIPrintf(viewer," Abscissa csb = %s\n",sbuf);
1178: PetscViewerASCIIPrintf(viewer," Asb = \n");
1179: for (i=0; i<tab->s; i++) {
1180: PetscFormatRealArray(sbuf,sizeof(sbuf),"% 8.6f",tab->s,&tab->Asb[i*tab->s]);
1181: PetscViewerASCIIPrintf(viewer," %s\n",sbuf);
1182: }
1183: PetscFormatRealArray(sbuf,sizeof(sbuf),"% 8.6f",tab->s,tab->bsb);
1184: PetscViewerASCIIPrintf(viewer," bsb = %s\n",sbuf);
1186: if (tab->np == 3) {
1187: char mbuf[512];
1188: PetscFormatRealArray(mbuf,sizeof(mbuf),"% 8.6f",tab->s,tab->cmb);
1189: PetscViewerASCIIPrintf(viewer," Abscissa cmb = %s\n",mbuf);
1190: PetscViewerASCIIPrintf(viewer," Amb = \n");
1191: for (i=0; i<tab->s; i++) {
1192: PetscFormatRealArray(mbuf,sizeof(mbuf),"% 8.6f",tab->s,&tab->Amb[i*tab->s]);
1193: PetscViewerASCIIPrintf(viewer," %s\n",mbuf);
1194: }
1195: PetscFormatRealArray(mbuf,sizeof(mbuf),"% 8.6f",tab->s,tab->bmb);
1196: PetscViewerASCIIPrintf(viewer," bmb = %s\n",mbuf);
1197: }
1198: }
1199: return(0);
1200: }
1202: static PetscErrorCode TSLoad_MPRK(TS ts,PetscViewer viewer)
1203: {
1205: TSAdapt adapt;
1208: TSGetAdapt(ts,&adapt);
1209: TSAdaptLoad(adapt,viewer);
1210: return(0);
1211: }
1213: /*@C
1214: TSMPRKSetType - Set the type of MPRK scheme
1216: Not collective
1218: Input Parameter:
1219: + ts - timestepping context
1220: - mprktype - type of MPRK-scheme
1222: Options Database:
1223: . -ts_mprk_type - <pm2,p2,p3>
1225: Level: intermediate
1227: .seealso: TSMPRKGetType(), TSMPRK, TSMPRKType
1228: @*/
1229: PetscErrorCode TSMPRKSetType(TS ts,TSMPRKType mprktype)
1230: {
1236: PetscTryMethod(ts,"TSMPRKSetType_C",(TS,TSMPRKType),(ts,mprktype));
1237: return(0);
1238: }
1240: /*@C
1241: TSMPRKGetType - Get the type of MPRK scheme
1243: Not collective
1245: Input Parameter:
1246: . ts - timestepping context
1248: Output Parameter:
1249: . mprktype - type of MPRK-scheme
1251: Level: intermediate
1253: .seealso: TSMPRKGetType()
1254: @*/
1255: PetscErrorCode TSMPRKGetType(TS ts,TSMPRKType *mprktype)
1256: {
1261: PetscUseMethod(ts,"TSMPRKGetType_C",(TS,TSMPRKType*),(ts,mprktype));
1262: return(0);
1263: }
1265: static PetscErrorCode TSMPRKGetType_MPRK(TS ts,TSMPRKType *mprktype)
1266: {
1267: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1270: *mprktype = mprk->tableau->name;
1271: return(0);
1272: }
1274: static PetscErrorCode TSMPRKSetType_MPRK(TS ts,TSMPRKType mprktype)
1275: {
1276: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1277: PetscBool match;
1278: MPRKTableauLink link;
1279: PetscErrorCode ierr;
1282: if (mprk->tableau) {
1283: PetscStrcmp(mprk->tableau->name,mprktype,&match);
1284: if (match) return(0);
1285: }
1286: for (link = MPRKTableauList; link; link=link->next) {
1287: PetscStrcmp(link->tab.name,mprktype,&match);
1288: if (match) {
1289: if (ts->setupcalled) {TSMPRKTableauReset(ts);}
1290: mprk->tableau = &link->tab;
1291: if (ts->setupcalled) {TSMPRKTableauSetUp(ts);}
1292: return(0);
1293: }
1294: }
1295: SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",mprktype);
1296: }
1298: static PetscErrorCode TSGetStages_MPRK(TS ts,PetscInt *ns,Vec **Y)
1299: {
1300: TS_MPRK *mprk = (TS_MPRK*)ts->data;
1303: *ns = mprk->tableau->s;
1304: if (Y) *Y = mprk->Y;
1305: return(0);
1306: }
1308: static PetscErrorCode TSDestroy_MPRK(TS ts)
1309: {
1313: TSReset_MPRK(ts);
1314: if (ts->dm) {
1315: DMCoarsenHookRemove(ts->dm,DMCoarsenHook_TSMPRK,DMRestrictHook_TSMPRK,ts);
1316: DMSubDomainHookRemove(ts->dm,DMSubDomainHook_TSMPRK,DMSubDomainRestrictHook_TSMPRK,ts);
1317: }
1318: PetscFree(ts->data);
1319: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKGetType_C",NULL);
1320: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKSetType_C",NULL);
1321: return(0);
1322: }
1324: /*MC
1325: TSMPRK - ODE solver using Multirate Partitioned Runge-Kutta schemes
1327: The user should provide the right hand side of the equation
1328: using TSSetRHSFunction().
1330: Notes:
1331: The default is TSMPRKPM2, it can be changed with TSMPRKSetType() or -ts_mprk_type
1333: Level: beginner
1335: .seealso: TSCreate(), TS, TSSetType(), TSMPRKSetType(), TSMPRKGetType(), TSMPRKType, TSMPRKRegister(), TSMPRKSetMultirateType()
1336: TSMPRKM2, TSMPRKM3, TSMPRKRFSMR3, TSMPRKRFSMR2
1338: M*/
1339: PETSC_EXTERN PetscErrorCode TSCreate_MPRK(TS ts)
1340: {
1341: TS_MPRK *mprk;
1345: TSMPRKInitializePackage();
1347: ts->ops->reset = TSReset_MPRK;
1348: ts->ops->destroy = TSDestroy_MPRK;
1349: ts->ops->view = TSView_MPRK;
1350: ts->ops->load = TSLoad_MPRK;
1351: ts->ops->setup = TSSetUp_MPRK;
1352: ts->ops->step = TSStep_MPRK;
1353: ts->ops->evaluatestep = TSEvaluateStep_MPRK;
1354: ts->ops->setfromoptions = TSSetFromOptions_MPRK;
1355: ts->ops->getstages = TSGetStages_MPRK;
1357: PetscNewLog(ts,&mprk);
1358: ts->data = (void*)mprk;
1360: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKGetType_C",TSMPRKGetType_MPRK);
1361: PetscObjectComposeFunction((PetscObject)ts,"TSMPRKSetType_C",TSMPRKSetType_MPRK);
1363: TSMPRKSetType(ts,TSMPRKDefault);
1364: return(0);
1365: }