Actual source code: arkimex.c

  1: /*
  2:   Code for timestepping with additive Runge-Kutta IMEX method

  4:   Notes:
  5:   The general system is written as

  7:   F(t,U,Udot) = G(t,U)

  9:   where F represents the stiff part of the physics and G represents the non-stiff part.

 11: */
 12: #include <petsc/private/tsimpl.h>
 13: #include <petscdm.h>

 15: static TSARKIMEXType  TSARKIMEXDefault = TSARKIMEX3;
 16: static PetscBool      TSARKIMEXRegisterAllCalled;
 17: static PetscBool      TSARKIMEXPackageInitialized;
 18: static PetscErrorCode TSExtrapolate_ARKIMEX(TS, PetscReal, Vec);

 20: typedef struct _ARKTableau *ARKTableau;
 21: struct _ARKTableau {
 22:   char      *name;
 23:   PetscInt   order;                /* Classical approximation order of the method */
 24:   PetscInt   s;                    /* Number of stages */
 25:   PetscBool  stiffly_accurate;     /* The implicit part is stiffly accurate*/
 26:   PetscBool  FSAL_implicit;        /* The implicit part is FSAL*/
 27:   PetscBool  explicit_first_stage; /* The implicit part has an explicit first stage*/
 28:   PetscInt   pinterp;              /* Interpolation order */
 29:   PetscReal *At, *bt, *ct;         /* Stiff tableau */
 30:   PetscReal *A, *b, *c;            /* Non-stiff tableau */
 31:   PetscReal *bembedt, *bembed;     /* Embedded formula of order one less (order-1) */
 32:   PetscReal *binterpt, *binterp;   /* Dense output formula */
 33:   PetscReal  ccfl;                 /* Placeholder for CFL coefficient relative to forward Euler */
 34: };
 35: typedef struct _ARKTableauLink *ARKTableauLink;
 36: struct _ARKTableauLink {
 37:   struct _ARKTableau tab;
 38:   ARKTableauLink     next;
 39: };
 40: static ARKTableauLink ARKTableauList;

 42: typedef struct {
 43:   ARKTableau   tableau;
 44:   Vec         *Y;            /* States computed during the step */
 45:   Vec         *YdotI;        /* Time derivatives for the stiff part */
 46:   Vec         *YdotRHS;      /* Function evaluations for the non-stiff part */
 47:   Vec         *Y_prev;       /* States computed during the previous time step */
 48:   Vec         *YdotI_prev;   /* Time derivatives for the stiff part for the previous time step*/
 49:   Vec         *YdotRHS_prev; /* Function evaluations for the non-stiff part for the previous time step*/
 50:   Vec          Ydot0;        /* Holds the slope from the previous step in FSAL case */
 51:   Vec          Ydot;         /* Work vector holding Ydot during residual evaluation */
 52:   Vec          Z;            /* Ydot = shift(Y-Z) */
 53:   PetscScalar *work;         /* Scalar work */
 54:   PetscReal    scoeff;       /* shift = scoeff/dt */
 55:   PetscReal    stage_time;
 56:   PetscBool    imex;
 57:   PetscBool    extrapolate; /* Extrapolate initial guess from previous time-step stage values */
 58:   TSStepStatus status;
 59: } TS_ARKIMEX;
 60: /*MC
 61:      TSARKIMEXARS122 - Second order ARK IMEX scheme.

 63:      This method has one explicit stage and one implicit stage.

 65:      Options Database Key:
 66: .      -ts_arkimex_type ars122 - set arkimex type to ars122

 68:      Level: advanced

 70:      References:
 71: .    * -  U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).

 73: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
 74: M*/
 75: /*MC
 76:      TSARKIMEXA2 - Second order ARK IMEX scheme with A-stable implicit part.

 78:      This method has an explicit stage and one implicit stage, and has an A-stable implicit scheme. This method was provided by Emil Constantinescu.

 80:      Options Database Key:
 81: .      -ts_arkimex_type a2 - set arkimex type to a2

 83:      Level: advanced

 85: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
 86: M*/
 87: /*MC
 88:      TSARKIMEXL2 - Second order ARK IMEX scheme with L-stable implicit part.

 90:      This method has two implicit stages, and L-stable implicit scheme.

 92:      Options Database Key:
 93: .      -ts_arkimex_type l2 - set arkimex type to l2

 95:      Level: advanced

 97:     References:
 98: .   * -  L. Pareschi, G. Russo, Implicit Explicit Runge Kutta schemes and applications to hyperbolic systems with relaxations. Journal of Scientific Computing Volume: 25, Issue: 1, October, 2005.

100: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
101: M*/
102: /*MC
103:      TSARKIMEX1BEE - First order backward Euler represented as an ARK IMEX scheme with extrapolation as error estimator. This is a 3-stage method.

105:      This method is aimed at starting the integration of implicit DAEs when explicit first-stage ARK methods are used.

107:      Options Database Key:
108: .      -ts_arkimex_type 1bee - set arkimex type to 1bee

110:      Level: advanced

112: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
113: M*/
114: /*MC
115:      TSARKIMEX2C - Second order ARK IMEX scheme with L-stable implicit part.

117:      This method has one explicit stage and two implicit stages. The implicit part is the same as in TSARKIMEX2D and TSARKIMEX2E, but the explicit part has a larger stability region on the negative real axis. This method was provided by Emil Constantinescu.

119:      Options Database Key:
120: .      -ts_arkimex_type 2c - set arkimex type to 2c

122:      Level: advanced

124: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
125: M*/
126: /*MC
127:      TSARKIMEX2D - Second order ARK IMEX scheme with L-stable implicit part.

129:      This method has one explicit stage and two implicit stages. The stability function is independent of the explicit part in the infinity limit of the implicit component. This method was provided by Emil Constantinescu.

131:      Options Database Key:
132: .      -ts_arkimex_type 2d - set arkimex type to 2d

134:      Level: advanced

136: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
137: M*/
138: /*MC
139:      TSARKIMEX2E - Second order ARK IMEX scheme with L-stable implicit part.

141:      This method has one explicit stage and two implicit stages. It is is an optimal method developed by Emil Constantinescu.

143:      Options Database Key:
144: .      -ts_arkimex_type 2e - set arkimex type to 2e

146:     Level: advanced

148: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
149: M*/
150: /*MC
151:      TSARKIMEXPRSSP2 - Second order SSP ARK IMEX scheme.

153:      This method has three implicit stages.

155:      References:
156: .    * -  L. Pareschi, G. Russo, Implicit Explicit Runge Kutta schemes and applications to hyperbolic systems with relaxations. Journal of Scientific Computing Volume: 25, Issue: 1, October, 2005.

158:      This method is referred to as SSP2-(3,3,2) in https://arxiv.org/abs/1110.4375

160:      Options Database Key:
161: .      -ts_arkimex_type prssp2 - set arkimex type to prssp2

163:      Level: advanced

165: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
166: M*/
167: /*MC
168:      TSARKIMEX3 - Third order ARK IMEX scheme with L-stable implicit part.

170:      This method has one explicit stage and three implicit stages.

172:      Options Database Key:
173: .      -ts_arkimex_type 3 - set arkimex type to 3

175:      Level: advanced

177:      References:
178: .    * -  Kennedy and Carpenter 2003.

180: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
181: M*/
182: /*MC
183:      TSARKIMEXARS443 - Third order ARK IMEX scheme.

185:      This method has one explicit stage and four implicit stages.

187:      Options Database Key:
188: .      -ts_arkimex_type ars443 - set arkimex type to ars443

190:      Level: advanced

192:      References:
193: +    * -  U. Ascher, S. Ruuth, R. J. Spiteri, Implicit explicit Runge Kutta methods for time dependent Partial Differential Equations. Appl. Numer. Math. 25, (1997).
194: -    * -  This method is referred to as ARS(4,4,3) in https://arxiv.org/abs/1110.4375

196: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
197: M*/
198: /*MC
199:      TSARKIMEXBPR3 - Third order ARK IMEX scheme.

201:      This method has one explicit stage and four implicit stages.

203:      Options Database Key:
204: .      -ts_arkimex_type bpr3 - set arkimex type to bpr3

206:      Level: advanced

208:      References:
209: .    * - This method is referred to as ARK3 in https://arxiv.org/abs/1110.4375

211: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
212: M*/
213: /*MC
214:      TSARKIMEX4 - Fourth order ARK IMEX scheme with L-stable implicit part.

216:      This method has one explicit stage and four implicit stages.

218:      Options Database Key:
219: .      -ts_arkimex_type 4 - set arkimex type to4

221:      Level: advanced

223:      References:
224: .    * - Kennedy and Carpenter 2003.

226: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
227: M*/
228: /*MC
229:      TSARKIMEX5 - Fifth order ARK IMEX scheme with L-stable implicit part.

231:      This method has one explicit stage and five implicit stages.

233:      Options Database Key:
234: .      -ts_arkimex_type 5 - set arkimex type to 5

236:      Level: advanced

238:      References:
239: .    * - Kennedy and Carpenter 2003.

241: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEXSetType()`
242: M*/

244: /*@C
245:   TSARKIMEXRegisterAll - Registers all of the additive Runge-Kutta implicit-explicit methods in `TSARKIMEX`

247:   Not Collective, but should be called by all processes which will need the schemes to be registered

249:   Level: advanced

251: .seealso: [](chapter_ts), `TS`, `TSARKIMEX`, `TSARKIMEXRegisterDestroy()`
252: @*/
253: PetscErrorCode TSARKIMEXRegisterAll(void)
254: {
255:   if (TSARKIMEXRegisterAllCalled) return 0;
256:   TSARKIMEXRegisterAllCalled = PETSC_TRUE;

258:   {
259:     const PetscReal A[3][3] =
260:       {
261:         {0.0, 0.0, 0.0},
262:         {0.0, 0.0, 0.0},
263:         {0.0, 0.5, 0.0}
264:     },
265:                     At[3][3] = {{1.0, 0.0, 0.0}, {0.0, 0.5, 0.0}, {0.0, 0.5, 0.5}}, b[3] = {0.0, 0.5, 0.5}, bembedt[3] = {1.0, 0.0, 0.0};
266:     TSARKIMEXRegister(TSARKIMEX1BEE, 2, 3, &At[0][0], b, NULL, &A[0][0], b, NULL, bembedt, bembedt, 1, b, NULL);
267:   }
268:   {
269:     const PetscReal A[2][2] =
270:       {
271:         {0.0, 0.0},
272:         {0.5, 0.0}
273:     },
274:                     At[2][2] = {{0.0, 0.0}, {0.0, 0.5}}, b[2] = {0.0, 1.0}, bembedt[2] = {0.5, 0.5};
275:     /* binterpt[2][2] = {{1.0,-1.0},{0.0,1.0}};  second order dense output has poor stability properties and hence it is not currently in use*/
276:     TSARKIMEXRegister(TSARKIMEXARS122, 2, 2, &At[0][0], b, NULL, &A[0][0], b, NULL, bembedt, bembedt, 1, b, NULL);
277:   }
278:   {
279:     const PetscReal A[2][2] =
280:       {
281:         {0.0, 0.0},
282:         {1.0, 0.0}
283:     },
284:                     At[2][2] = {{0.0, 0.0}, {0.5, 0.5}}, b[2] = {0.5, 0.5}, bembedt[2] = {0.0, 1.0};
285:     /* binterpt[2][2] = {{1.0,-0.5},{0.0,0.5}}  second order dense output has poor stability properties and hence it is not currently in use*/
286:     TSARKIMEXRegister(TSARKIMEXA2, 2, 2, &At[0][0], b, NULL, &A[0][0], b, NULL, bembedt, bembedt, 1, b, NULL);
287:   }
288:   {
289:     /* const PetscReal us2 = 1.0-1.0/PetscSqrtReal((PetscReal)2.0);    Direct evaluation: 0.2928932188134524755992. Used below to ensure all values are available at compile time   */
290:     const PetscReal A[2][2] =
291:       {
292:         {0.0, 0.0},
293:         {1.0, 0.0}
294:     },
295:                     At[2][2] = {{0.2928932188134524755992, 0.0}, {1.0 - 2.0 * 0.2928932188134524755992, 0.2928932188134524755992}}, b[2] = {0.5, 0.5}, bembedt[2] = {0.0, 1.0}, binterpt[2][2] = {{(0.2928932188134524755992 - 1.0) / (2.0 * 0.2928932188134524755992 - 1.0), -1 / (2.0 * (1.0 - 2.0 * 0.2928932188134524755992))}, {1 - (0.2928932188134524755992 - 1.0) / (2.0 * 0.2928932188134524755992 - 1.0), -1 / (2.0 * (1.0 - 2.0 * 0.2928932188134524755992))}}, binterp[2][2] = {{1.0, -0.5}, {0.0, 0.5}};
296:     TSARKIMEXRegister(TSARKIMEXL2, 2, 2, &At[0][0], b, NULL, &A[0][0], b, NULL, bembedt, bembedt, 2, binterpt[0], binterp[0]);
297:   }
298:   {
299:     /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0),  Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time   */
300:     const PetscReal A[3][3] =
301:       {
302:         {0,                           0,   0},
303:         {2 - 1.414213562373095048802, 0,   0},
304:         {0.5,                         0.5, 0}
305:     },
306:                     At[3][3] = {{0, 0, 0}, {1 - 1 / 1.414213562373095048802, 1 - 1 / 1.414213562373095048802, 0}, {1 / (2 * 1.414213562373095048802), 1 / (2 * 1.414213562373095048802), 1 - 1 / 1.414213562373095048802}}, bembedt[3] = {(4. - 1.414213562373095048802) / 8., (4. - 1.414213562373095048802) / 8., 1 / (2. * 1.414213562373095048802)}, binterpt[3][2] = {{1.0 / 1.414213562373095048802, -1.0 / (2.0 * 1.414213562373095048802)}, {1.0 / 1.414213562373095048802, -1.0 / (2.0 * 1.414213562373095048802)}, {1.0 - 1.414213562373095048802, 1.0 / 1.414213562373095048802}};
307:     TSARKIMEXRegister(TSARKIMEX2C, 2, 3, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 2, binterpt[0], NULL);
308:   }
309:   {
310:     /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0),  Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time   */
311:     const PetscReal A[3][3] =
312:       {
313:         {0,                           0,    0},
314:         {2 - 1.414213562373095048802, 0,    0},
315:         {0.75,                        0.25, 0}
316:     },
317:                     At[3][3] = {{0, 0, 0}, {1 - 1 / 1.414213562373095048802, 1 - 1 / 1.414213562373095048802, 0}, {1 / (2 * 1.414213562373095048802), 1 / (2 * 1.414213562373095048802), 1 - 1 / 1.414213562373095048802}}, bembedt[3] = {(4. - 1.414213562373095048802) / 8., (4. - 1.414213562373095048802) / 8., 1 / (2. * 1.414213562373095048802)}, binterpt[3][2] = {{1.0 / 1.414213562373095048802, -1.0 / (2.0 * 1.414213562373095048802)}, {1.0 / 1.414213562373095048802, -1.0 / (2.0 * 1.414213562373095048802)}, {1.0 - 1.414213562373095048802, 1.0 / 1.414213562373095048802}};
318:     TSARKIMEXRegister(TSARKIMEX2D, 2, 3, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 2, binterpt[0], NULL);
319:   }
320:   { /* Optimal for linear implicit part */
321:     /* const PetscReal s2 = PetscSqrtReal((PetscReal)2.0),  Direct evaluation: 1.414213562373095048802. Used below to ensure all values are available at compile time   */
322:     const PetscReal A[3][3] =
323:       {
324:         {0,                                     0,                                     0},
325:         {2 - 1.414213562373095048802,           0,                                     0},
326:         {(3 - 2 * 1.414213562373095048802) / 6, (3 + 2 * 1.414213562373095048802) / 6, 0}
327:     },
328:                     At[3][3] = {{0, 0, 0}, {1 - 1 / 1.414213562373095048802, 1 - 1 / 1.414213562373095048802, 0}, {1 / (2 * 1.414213562373095048802), 1 / (2 * 1.414213562373095048802), 1 - 1 / 1.414213562373095048802}}, bembedt[3] = {(4. - 1.414213562373095048802) / 8., (4. - 1.414213562373095048802) / 8., 1 / (2. * 1.414213562373095048802)}, binterpt[3][2] = {{1.0 / 1.414213562373095048802, -1.0 / (2.0 * 1.414213562373095048802)}, {1.0 / 1.414213562373095048802, -1.0 / (2.0 * 1.414213562373095048802)}, {1.0 - 1.414213562373095048802, 1.0 / 1.414213562373095048802}};
329:     TSARKIMEXRegister(TSARKIMEX2E, 2, 3, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 2, binterpt[0], NULL);
330:   }
331:   { /* Optimal for linear implicit part */
332:     const PetscReal A[3][3] =
333:       {
334:         {0,   0,   0},
335:         {0.5, 0,   0},
336:         {0.5, 0.5, 0}
337:     },
338:                     At[3][3] = {{0.25, 0, 0}, {0, 0.25, 0}, {1. / 3, 1. / 3, 1. / 3}};
339:     TSARKIMEXRegister(TSARKIMEXPRSSP2, 2, 3, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, NULL, NULL, 0, NULL, NULL);
340:   }
341:   {
342:     const PetscReal A[4][4] =
343:       {
344:         {0,                                0,                                0,                                 0},
345:         {1767732205903. / 2027836641118.,  0,                                0,                                 0},
346:         {5535828885825. / 10492691773637., 788022342437. / 10882634858940.,  0,                                 0},
347:         {6485989280629. / 16251701735622., -4246266847089. / 9704473918619., 10755448449292. / 10357097424841., 0}
348:     },
349:                     At[4][4] = {{0, 0, 0, 0}, {1767732205903. / 4055673282236., 1767732205903. / 4055673282236., 0, 0}, {2746238789719. / 10658868560708., -640167445237. / 6845629431997., 1767732205903. / 4055673282236., 0}, {1471266399579. / 7840856788654., -4482444167858. / 7529755066697., 11266239266428. / 11593286722821., 1767732205903. / 4055673282236.}}, bembedt[4] = {2756255671327. / 12835298489170., -10771552573575. / 22201958757719., 9247589265047. / 10645013368117., 2193209047091. / 5459859503100.}, binterpt[4][2] = {{4655552711362. / 22874653954995., -215264564351. / 13552729205753.}, {-18682724506714. / 9892148508045., 17870216137069. / 13817060693119.}, {34259539580243. / 13192909600954., -28141676662227. / 17317692491321.}, {584795268549. / 6622622206610., 2508943948391. / 7218656332882.}};
350:     TSARKIMEXRegister(TSARKIMEX3, 3, 4, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 2, binterpt[0], NULL);
351:   }
352:   {
353:     const PetscReal A[5][5] =
354:       {
355:         {0,        0,       0,      0,       0},
356:         {1. / 2,   0,       0,      0,       0},
357:         {11. / 18, 1. / 18, 0,      0,       0},
358:         {5. / 6,   -5. / 6, .5,     0,       0},
359:         {1. / 4,   7. / 4,  3. / 4, -7. / 4, 0}
360:     },
361:                     At[5][5] = {{0, 0, 0, 0, 0}, {0, 1. / 2, 0, 0, 0}, {0, 1. / 6, 1. / 2, 0, 0}, {0, -1. / 2, 1. / 2, 1. / 2, 0}, {0, 3. / 2, -3. / 2, 1. / 2, 1. / 2}}, *bembedt = NULL;
362:     TSARKIMEXRegister(TSARKIMEXARS443, 3, 5, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 0, NULL, NULL);
363:   }
364:   {
365:     const PetscReal A[5][5] =
366:       {
367:         {0,      0,      0,      0, 0},
368:         {1,      0,      0,      0, 0},
369:         {4. / 9, 2. / 9, 0,      0, 0},
370:         {1. / 4, 0,      3. / 4, 0, 0},
371:         {1. / 4, 0,      3. / 5, 0, 0}
372:     },
373:                     At[5][5] = {{0, 0, 0, 0, 0}, {.5, .5, 0, 0, 0}, {5. / 18, -1. / 9, .5, 0, 0}, {.5, 0, 0, .5, 0}, {.25, 0, .75, -.5, .5}}, *bembedt = NULL;
374:     TSARKIMEXRegister(TSARKIMEXBPR3, 3, 5, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 0, NULL, NULL);
375:   }
376:   {
377:     const PetscReal A[6][6] =
378:       {
379:         {0,                               0,                                 0,                                 0,                                0,              0},
380:         {1. / 2,                          0,                                 0,                                 0,                                0,              0},
381:         {13861. / 62500.,                 6889. / 62500.,                    0,                                 0,                                0,              0},
382:         {-116923316275. / 2393684061468., -2731218467317. / 15368042101831., 9408046702089. / 11113171139209.,  0,                                0,              0},
383:         {-451086348788. / 2902428689909., -2682348792572. / 7519795681897.,  12662868775082. / 11960479115383., 3355817975965. / 11060851509271., 0,              0},
384:         {647845179188. / 3216320057751.,  73281519250. / 8382639484533.,     552539513391. / 3454668386233.,    3354512671639. / 8306763924573.,  4040. / 17871., 0}
385:     },
386:                     At[6][6] = {{0, 0, 0, 0, 0, 0}, {1. / 4, 1. / 4, 0, 0, 0, 0}, {8611. / 62500., -1743. / 31250., 1. / 4, 0, 0, 0}, {5012029. / 34652500., -654441. / 2922500., 174375. / 388108., 1. / 4, 0, 0}, {15267082809. / 155376265600., -71443401. / 120774400., 730878875. / 902184768., 2285395. / 8070912., 1. / 4, 0}, {82889. / 524892., 0, 15625. / 83664., 69875. / 102672., -2260. / 8211, 1. / 4}}, bembedt[6] = {4586570599. / 29645900160., 0, 178811875. / 945068544., 814220225. / 1159782912., -3700637. / 11593932., 61727. / 225920.}, binterpt[6][3] = {{6943876665148. / 7220017795957., -54480133. / 30881146., 6818779379841. / 7100303317025.},  {0, 0, 0},
387:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     {7640104374378. / 9702883013639., -11436875. / 14766696., 2173542590792. / 12501825683035.}, {-20649996744609. / 7521556579894., 174696575. / 18121608., -31592104683404. / 5083833661969.},
388:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     {8854892464581. / 2390941311638., -12120380. / 966161., 61146701046299. / 7138195549469.},   {-11397109935349. / 6675773540249., 3843. / 706., -17219254887155. / 4939391667607.}};
389:     TSARKIMEXRegister(TSARKIMEX4, 4, 6, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 3, binterpt[0], NULL);
390:   }
391:   {
392:     const PetscReal A[8][8] =
393:       {
394:         {0,                                  0,                              0,                                 0,                                  0,                               0,                                 0,                               0},
395:         {41. / 100,                          0,                              0,                                 0,                                  0,                               0,                                 0,                               0},
396:         {367902744464. / 2072280473677.,     677623207551. / 8224143866563., 0,                                 0,                                  0,                               0,                                 0,                               0},
397:         {1268023523408. / 10340822734521.,   0,                              1029933939417. / 13636558850479.,  0,                                  0,                               0,                                 0,                               0},
398:         {14463281900351. / 6315353703477.,   0,                              66114435211212. / 5879490589093.,  -54053170152839. / 4284798021562.,  0,                               0,                                 0,                               0},
399:         {14090043504691. / 34967701212078.,  0,                              15191511035443. / 11219624916014., -18461159152457. / 12425892160975., -281667163811. / 9011619295870., 0,                                 0,                               0},
400:         {19230459214898. / 13134317526959.,  0,                              21275331358303. / 2942455364971.,  -38145345988419. / 4862620318723.,  -1. / 8,                         -1. / 8,                           0,                               0},
401:         {-19977161125411. / 11928030595625., 0,                              -40795976796054. / 6384907823539., 177454434618887. / 12078138498510., 782672205425. / 8267701900261.,  -69563011059811. / 9646580694205., 7356628210526. / 4942186776405., 0}
402:     },
403:                     At[8][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, {41. / 200., 41. / 200., 0, 0, 0, 0, 0, 0}, {41. / 400., -567603406766. / 11931857230679., 41. / 200., 0, 0, 0, 0, 0}, {683785636431. / 9252920307686., 0, -110385047103. / 1367015193373., 41. / 200., 0, 0, 0, 0}, {3016520224154. / 10081342136671., 0, 30586259806659. / 12414158314087., -22760509404356. / 11113319521817., 41. / 200., 0, 0, 0}, {218866479029. / 1489978393911., 0, 638256894668. / 5436446318841., -1179710474555. / 5321154724896., -60928119172. / 8023461067671., 41. / 200., 0, 0}, {1020004230633. / 5715676835656., 0, 25762820946817. / 25263940353407., -2161375909145. / 9755907335909., -211217309593. / 5846859502534., -4269925059573. / 7827059040749., 41. / 200, 0}, {-872700587467. / 9133579230613., 0, 0, 22348218063261. / 9555858737531., -1143369518992. / 8141816002931., -39379526789629. / 19018526304540., 32727382324388. / 42900044865799., 41. / 200.}}, bembedt[8] = {-975461918565. / 9796059967033., 0, 0, 78070527104295. / 32432590147079., -548382580838. / 3424219808633., -33438840321285. / 15594753105479., 3629800801594. / 4656183773603., 4035322873751. / 18575991585200.}, binterpt[8][3] = {{-17674230611817. / 10670229744614., 43486358583215. / 12773830924787., -9257016797708. / 5021505065439.}, {0, 0, 0}, {0, 0, 0}, {65168852399939. / 7868540260826., -91478233927265. / 11067650958493., 26096422576131. / 11239449250142.}, {15494834004392. / 5936557850923., -79368583304911. / 10890268929626., 92396832856987. / 20362823103730.}, {-99329723586156. / 26959484932159., -12239297817655. / 9152339842473., 30029262896817. / 10175596800299.}, {-19024464361622. / 5461577185407., 115839755401235. / 10719374521269., -26136350496073. / 3983972220547.}, {-6511271360970. / 6095937251113., 5843115559534. / 2180450260947., -5289405421727. / 3760307252460.}};
404:     TSARKIMEXRegister(TSARKIMEX5, 5, 8, &At[0][0], NULL, NULL, &A[0][0], NULL, NULL, bembedt, bembedt, 3, binterpt[0], NULL);
405:   }
406:   return 0;
407: }

409: /*@C
410:    TSARKIMEXRegisterDestroy - Frees the list of schemes that were registered by `TSARKIMEXRegister()`.

412:    Not Collective

414:    Level: advanced

416: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXRegister()`, `TSARKIMEXRegisterAll()`
417: @*/
418: PetscErrorCode TSARKIMEXRegisterDestroy(void)
419: {
420:   ARKTableauLink link;

422:   while ((link = ARKTableauList)) {
423:     ARKTableau t   = &link->tab;
424:     ARKTableauList = link->next;
425:     PetscFree6(t->At, t->bt, t->ct, t->A, t->b, t->c);
426:     PetscFree2(t->bembedt, t->bembed);
427:     PetscFree2(t->binterpt, t->binterp);
428:     PetscFree(t->name);
429:     PetscFree(link);
430:   }
431:   TSARKIMEXRegisterAllCalled = PETSC_FALSE;
432:   return 0;
433: }

435: /*@C
436:   TSARKIMEXInitializePackage - This function initializes everything in the `TSARKIMEX` package. It is called
437:   from `TSInitializePackage()`.

439:   Level: developer

441: .seealso: [](chapter_ts), `PetscInitialize()`, `TSARKIMEXFinalizePackage()`
442: @*/
443: PetscErrorCode TSARKIMEXInitializePackage(void)
444: {
445:   if (TSARKIMEXPackageInitialized) return 0;
446:   TSARKIMEXPackageInitialized = PETSC_TRUE;
447:   TSARKIMEXRegisterAll();
448:   PetscRegisterFinalize(TSARKIMEXFinalizePackage);
449:   return 0;
450: }

452: /*@C
453:   TSARKIMEXFinalizePackage - This function destroys everything in the `TSARKIMEX` package. It is
454:   called from `PetscFinalize()`.

456:   Level: developer

458: .seealso: [](chapter_ts), `PetscFinalize()`, `TSARKIMEXInitializePackage()`
459: @*/
460: PetscErrorCode TSARKIMEXFinalizePackage(void)
461: {
462:   TSARKIMEXPackageInitialized = PETSC_FALSE;
463:   TSARKIMEXRegisterDestroy();
464:   return 0;
465: }

467: /*@C
468:    TSARKIMEXRegister - register a `TSARKIMEX` scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation

470:    Not Collective, but the same schemes should be registered on all processes on which they will be used

472:    Input Parameters:
473: +  name - identifier for method
474: .  order - approximation order of method
475: .  s - number of stages, this is the dimension of the matrices below
476: .  At - Butcher table of stage coefficients for stiff part (dimension s*s, row-major)
477: .  bt - Butcher table for completing the stiff part of the step (dimension s; NULL to use the last row of At)
478: .  ct - Abscissa of each stiff stage (dimension s, NULL to use row sums of At)
479: .  A - Non-stiff stage coefficients (dimension s*s, row-major)
480: .  b - Non-stiff step completion table (dimension s; NULL to use last row of At)
481: .  c - Non-stiff abscissa (dimension s; NULL to use row sums of A)
482: .  bembedt - Stiff part of completion table for embedded method (dimension s; NULL if not available)
483: .  bembed - Non-stiff part of completion table for embedded method (dimension s; NULL to use bembedt if provided)
484: .  pinterp - Order of the interpolation scheme, equal to the number of columns of binterpt and binterp
485: .  binterpt - Coefficients of the interpolation formula for the stiff part (dimension s*pinterp)
486: -  binterp - Coefficients of the interpolation formula for the non-stiff part (dimension s*pinterp; NULL to reuse binterpt)

488:    Level: advanced

490:    Note:
491:    Several `TSARKIMEX` methods are provided, this function is only needed to create new methods.

493: .seealso: [](chapter_ts), `TSARKIMEX`, `TSType`, `TS`
494: @*/
495: PetscErrorCode TSARKIMEXRegister(TSARKIMEXType name, PetscInt order, PetscInt s, const PetscReal At[], const PetscReal bt[], const PetscReal ct[], const PetscReal A[], const PetscReal b[], const PetscReal c[], const PetscReal bembedt[], const PetscReal bembed[], PetscInt pinterp, const PetscReal binterpt[], const PetscReal binterp[])
496: {
497:   ARKTableauLink link;
498:   ARKTableau     t;
499:   PetscInt       i, j;

501:   TSARKIMEXInitializePackage();
502:   PetscNew(&link);
503:   t = &link->tab;
504:   PetscStrallocpy(name, &t->name);
505:   t->order = order;
506:   t->s     = s;
507:   PetscMalloc6(s * s, &t->At, s, &t->bt, s, &t->ct, s * s, &t->A, s, &t->b, s, &t->c);
508:   PetscArraycpy(t->At, At, s * s);
509:   PetscArraycpy(t->A, A, s * s);
510:   if (bt) PetscArraycpy(t->bt, bt, s);
511:   else
512:     for (i = 0; i < s; i++) t->bt[i] = At[(s - 1) * s + i];
513:   if (b) PetscArraycpy(t->b, b, s);
514:   else
515:     for (i = 0; i < s; i++) t->b[i] = t->bt[i];
516:   if (ct) PetscArraycpy(t->ct, ct, s);
517:   else
518:     for (i = 0; i < s; i++)
519:       for (j = 0, t->ct[i] = 0; j < s; j++) t->ct[i] += At[i * s + j];
520:   if (c) PetscArraycpy(t->c, c, s);
521:   else
522:     for (i = 0; i < s; i++)
523:       for (j = 0, t->c[i] = 0; j < s; j++) t->c[i] += A[i * s + j];
524:   t->stiffly_accurate = PETSC_TRUE;
525:   for (i = 0; i < s; i++)
526:     if (t->At[(s - 1) * s + i] != t->bt[i]) t->stiffly_accurate = PETSC_FALSE;
527:   t->explicit_first_stage = PETSC_TRUE;
528:   for (i = 0; i < s; i++)
529:     if (t->At[i] != 0.0) t->explicit_first_stage = PETSC_FALSE;
530:   /*def of FSAL can be made more precise*/
531:   t->FSAL_implicit = (PetscBool)(t->explicit_first_stage && t->stiffly_accurate);
532:   if (bembedt) {
533:     PetscMalloc2(s, &t->bembedt, s, &t->bembed);
534:     PetscArraycpy(t->bembedt, bembedt, s);
535:     PetscArraycpy(t->bembed, bembed ? bembed : bembedt, s);
536:   }

538:   t->pinterp = pinterp;
539:   PetscMalloc2(s * pinterp, &t->binterpt, s * pinterp, &t->binterp);
540:   PetscArraycpy(t->binterpt, binterpt, s * pinterp);
541:   PetscArraycpy(t->binterp, binterp ? binterp : binterpt, s * pinterp);
542:   link->next     = ARKTableauList;
543:   ARKTableauList = link;
544:   return 0;
545: }

547: /*
548:  The step completion formula is

550:  x1 = x0 - h bt^T YdotI + h b^T YdotRHS

552:  This function can be called before or after ts->vec_sol has been updated.
553:  Suppose we have a completion formula (bt,b) and an embedded formula (bet,be) of different order.
554:  We can write

556:  x1e = x0 - h bet^T YdotI + h be^T YdotRHS
557:      = x1 + h bt^T YdotI - h b^T YdotRHS - h bet^T YdotI + h be^T YdotRHS
558:      = x1 - h (bet - bt)^T YdotI + h (be - b)^T YdotRHS

560:  so we can evaluate the method with different order even after the step has been optimistically completed.
561: */
562: static PetscErrorCode TSEvaluateStep_ARKIMEX(TS ts, PetscInt order, Vec X, PetscBool *done)
563: {
564:   TS_ARKIMEX  *ark = (TS_ARKIMEX *)ts->data;
565:   ARKTableau   tab = ark->tableau;
566:   PetscScalar *w   = ark->work;
567:   PetscReal    h;
568:   PetscInt     s = tab->s, j;

570:   switch (ark->status) {
571:   case TS_STEP_INCOMPLETE:
572:   case TS_STEP_PENDING:
573:     h = ts->time_step;
574:     break;
575:   case TS_STEP_COMPLETE:
576:     h = ts->ptime - ts->ptime_prev;
577:     break;
578:   default:
579:     SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Invalid TSStepStatus");
580:   }
581:   if (order == tab->order) {
582:     if (ark->status == TS_STEP_INCOMPLETE) {
583:       if (!ark->imex && tab->stiffly_accurate) { /* Only the stiffly accurate implicit formula is used */
584:         VecCopy(ark->Y[s - 1], X);
585:       } else { /* Use the standard completion formula (bt,b) */
586:         VecCopy(ts->vec_sol, X);
587:         for (j = 0; j < s; j++) w[j] = h * tab->bt[j];
588:         VecMAXPY(X, s, w, ark->YdotI);
589:         if (ark->imex) { /* Method is IMEX, complete the explicit formula */
590:           for (j = 0; j < s; j++) w[j] = h * tab->b[j];
591:           VecMAXPY(X, s, w, ark->YdotRHS);
592:         }
593:       }
594:     } else VecCopy(ts->vec_sol, X);
595:     if (done) *done = PETSC_TRUE;
596:     return 0;
597:   } else if (order == tab->order - 1) {
598:     if (!tab->bembedt) goto unavailable;
599:     if (ark->status == TS_STEP_INCOMPLETE) { /* Complete with the embedded method (bet,be) */
600:       VecCopy(ts->vec_sol, X);
601:       for (j = 0; j < s; j++) w[j] = h * tab->bembedt[j];
602:       VecMAXPY(X, s, w, ark->YdotI);
603:       for (j = 0; j < s; j++) w[j] = h * tab->bembed[j];
604:       VecMAXPY(X, s, w, ark->YdotRHS);
605:     } else { /* Rollback and re-complete using (bet-be,be-b) */
606:       VecCopy(ts->vec_sol, X);
607:       for (j = 0; j < s; j++) w[j] = h * (tab->bembedt[j] - tab->bt[j]);
608:       VecMAXPY(X, tab->s, w, ark->YdotI);
609:       for (j = 0; j < s; j++) w[j] = h * (tab->bembed[j] - tab->b[j]);
610:       VecMAXPY(X, s, w, ark->YdotRHS);
611:     }
612:     if (done) *done = PETSC_TRUE;
613:     return 0;
614:   }
615: unavailable:
616:   if (done) *done = PETSC_FALSE;
617:   else
618:     SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "ARKIMEX '%s' of order %" PetscInt_FMT " cannot evaluate step at order %" PetscInt_FMT ". Consider using -ts_adapt_type none or a different method that has an embedded estimate.", tab->name,
619:             tab->order, order);
620:   return 0;
621: }

623: static PetscErrorCode TSARKIMEXTestMassIdentity(TS ts, PetscBool *id)
624: {
625:   Vec         Udot, Y1, Y2;
626:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
627:   PetscReal   norm;

629:   VecDuplicate(ts->vec_sol, &Udot);
630:   VecDuplicate(ts->vec_sol, &Y1);
631:   VecDuplicate(ts->vec_sol, &Y2);
632:   TSComputeIFunction(ts, ts->ptime, ts->vec_sol, Udot, Y1, ark->imex);
633:   VecSetRandom(Udot, NULL);
634:   TSComputeIFunction(ts, ts->ptime, ts->vec_sol, Udot, Y2, ark->imex);
635:   VecAXPY(Y2, -1.0, Y1);
636:   VecAXPY(Y2, -1.0, Udot);
637:   VecNorm(Y2, NORM_2, &norm);
638:   if (norm < 100.0 * PETSC_MACHINE_EPSILON) {
639:     *id = PETSC_TRUE;
640:   } else {
641:     *id = PETSC_FALSE;
642:     PetscInfo((PetscObject)ts, "IFunction(Udot = random) - IFunction(Udot = 0) is not near Udot, %g, suspect mass matrix implied in IFunction() is not the identity as required\n", (double)norm);
643:   }
644:   VecDestroy(&Udot);
645:   VecDestroy(&Y1);
646:   VecDestroy(&Y2);
647:   return 0;
648: }

650: static PetscErrorCode TSRollBack_ARKIMEX(TS ts)
651: {
652:   TS_ARKIMEX      *ark = (TS_ARKIMEX *)ts->data;
653:   ARKTableau       tab = ark->tableau;
654:   const PetscInt   s   = tab->s;
655:   const PetscReal *bt = tab->bt, *b = tab->b;
656:   PetscScalar     *w     = ark->work;
657:   Vec             *YdotI = ark->YdotI, *YdotRHS = ark->YdotRHS;
658:   PetscInt         j;
659:   PetscReal        h;

661:   switch (ark->status) {
662:   case TS_STEP_INCOMPLETE:
663:   case TS_STEP_PENDING:
664:     h = ts->time_step;
665:     break;
666:   case TS_STEP_COMPLETE:
667:     h = ts->ptime - ts->ptime_prev;
668:     break;
669:   default:
670:     SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Invalid TSStepStatus");
671:   }
672:   for (j = 0; j < s; j++) w[j] = -h * bt[j];
673:   VecMAXPY(ts->vec_sol, s, w, YdotI);
674:   for (j = 0; j < s; j++) w[j] = -h * b[j];
675:   VecMAXPY(ts->vec_sol, s, w, YdotRHS);
676:   return 0;
677: }

679: static PetscErrorCode TSStep_ARKIMEX(TS ts)
680: {
681:   TS_ARKIMEX      *ark = (TS_ARKIMEX *)ts->data;
682:   ARKTableau       tab = ark->tableau;
683:   const PetscInt   s   = tab->s;
684:   const PetscReal *At = tab->At, *A = tab->A, *ct = tab->ct, *c = tab->c;
685:   PetscScalar     *w = ark->work;
686:   Vec             *Y = ark->Y, *YdotI = ark->YdotI, *YdotRHS = ark->YdotRHS, Ydot = ark->Ydot, Ydot0 = ark->Ydot0, Z = ark->Z;
687:   PetscBool        extrapolate = ark->extrapolate;
688:   TSAdapt          adapt;
689:   SNES             snes;
690:   PetscInt         i, j, its, lits;
691:   PetscInt         rejections = 0;
692:   PetscBool        stageok, accept = PETSC_TRUE;
693:   PetscReal        next_time_step = ts->time_step;

695:   if (ark->extrapolate && !ark->Y_prev) {
696:     VecDuplicateVecs(ts->vec_sol, tab->s, &ark->Y_prev);
697:     VecDuplicateVecs(ts->vec_sol, tab->s, &ark->YdotI_prev);
698:     VecDuplicateVecs(ts->vec_sol, tab->s, &ark->YdotRHS_prev);
699:   }

701:   if (!ts->steprollback) {
702:     if (ts->equation_type >= TS_EQ_IMPLICIT) { /* Save the initial slope for the next step */
703:       VecCopy(YdotI[s - 1], Ydot0);
704:     }
705:     if (ark->extrapolate && !ts->steprestart) { /* Save the Y, YdotI, YdotRHS for extrapolation initial guess */
706:       for (i = 0; i < s; i++) {
707:         VecCopy(Y[i], ark->Y_prev[i]);
708:         VecCopy(YdotRHS[i], ark->YdotRHS_prev[i]);
709:         VecCopy(YdotI[i], ark->YdotI_prev[i]);
710:       }
711:     }
712:   }

714:   if (ts->equation_type >= TS_EQ_IMPLICIT && tab->explicit_first_stage && ts->steprestart) {
715:     TS ts_start;
716:     if (PetscDefined(USE_DEBUG)) {
717:       PetscBool id = PETSC_FALSE;
718:       TSARKIMEXTestMassIdentity(ts, &id);
720:     }
721:     TSClone(ts, &ts_start);
722:     TSSetSolution(ts_start, ts->vec_sol);
723:     TSSetTime(ts_start, ts->ptime);
724:     TSSetMaxSteps(ts_start, ts->steps + 1);
725:     TSSetMaxTime(ts_start, ts->ptime + ts->time_step);
726:     TSSetExactFinalTime(ts_start, TS_EXACTFINALTIME_STEPOVER);
727:     TSSetTimeStep(ts_start, ts->time_step);
728:     TSSetType(ts_start, TSARKIMEX);
729:     TSARKIMEXSetFullyImplicit(ts_start, PETSC_TRUE);
730:     TSARKIMEXSetType(ts_start, TSARKIMEX1BEE);

732:     TSRestartStep(ts_start);
733:     TSSolve(ts_start, ts->vec_sol);
734:     TSGetTime(ts_start, &ts->ptime);
735:     TSGetTimeStep(ts_start, &ts->time_step);

737:     { /* Save the initial slope for the next step */
738:       TS_ARKIMEX *ark_start = (TS_ARKIMEX *)ts_start->data;
739:       VecCopy(ark_start->YdotI[ark_start->tableau->s - 1], Ydot0);
740:     }
741:     ts->steps++;

743:     /* Set the correct TS in SNES */
744:     /* We'll try to bypass this by changing the method on the fly */
745:     {
746:       TSGetSNES(ts, &snes);
747:       TSSetSNES(ts, snes);
748:     }
749:     TSDestroy(&ts_start);
750:   }

752:   ark->status = TS_STEP_INCOMPLETE;
753:   while (!ts->reason && ark->status != TS_STEP_COMPLETE) {
754:     PetscReal t = ts->ptime;
755:     PetscReal h = ts->time_step;
756:     for (i = 0; i < s; i++) {
757:       ark->stage_time = t + h * ct[i];
758:       TSPreStage(ts, ark->stage_time);
759:       if (At[i * s + i] == 0) { /* This stage is explicit */
761:         VecCopy(ts->vec_sol, Y[i]);
762:         for (j = 0; j < i; j++) w[j] = h * At[i * s + j];
763:         VecMAXPY(Y[i], i, w, YdotI);
764:         for (j = 0; j < i; j++) w[j] = h * A[i * s + j];
765:         VecMAXPY(Y[i], i, w, YdotRHS);
766:       } else {
767:         ark->scoeff = 1. / At[i * s + i];
768:         /* Ydot = shift*(Y-Z) */
769:         VecCopy(ts->vec_sol, Z);
770:         for (j = 0; j < i; j++) w[j] = h * At[i * s + j];
771:         VecMAXPY(Z, i, w, YdotI);
772:         for (j = 0; j < i; j++) w[j] = h * A[i * s + j];
773:         VecMAXPY(Z, i, w, YdotRHS);
774:         if (extrapolate && !ts->steprestart) {
775:           /* Initial guess extrapolated from previous time step stage values */
776:           TSExtrapolate_ARKIMEX(ts, c[i], Y[i]);
777:         } else {
778:           /* Initial guess taken from last stage */
779:           VecCopy(i > 0 ? Y[i - 1] : ts->vec_sol, Y[i]);
780:         }
781:         TSGetSNES(ts, &snes);
782:         SNESSolve(snes, NULL, Y[i]);
783:         SNESGetIterationNumber(snes, &its);
784:         SNESGetLinearSolveIterations(snes, &lits);
785:         ts->snes_its += its;
786:         ts->ksp_its += lits;
787:         TSGetAdapt(ts, &adapt);
788:         TSAdaptCheckStage(adapt, ts, ark->stage_time, Y[i], &stageok);
789:         if (!stageok) {
790:           /* We are likely rejecting the step because of solver or function domain problems so we should not attempt to
791:            * use extrapolation to initialize the solves on the next attempt. */
792:           extrapolate = PETSC_FALSE;
793:           goto reject_step;
794:         }
795:       }
796:       if (ts->equation_type >= TS_EQ_IMPLICIT) {
797:         if (i == 0 && tab->explicit_first_stage) {
799:                      ark->tableau->name);
800:           VecCopy(Ydot0, YdotI[0]); /* YdotI = YdotI(tn-1) */
801:         } else {
802:           VecAXPBYPCZ(YdotI[i], -ark->scoeff / h, ark->scoeff / h, 0, Z, Y[i]); /* YdotI = shift*(X-Z) */
803:         }
804:       } else {
805:         if (i == 0 && tab->explicit_first_stage) {
806:           VecZeroEntries(Ydot);
807:           TSComputeIFunction(ts, t + h * ct[i], Y[i], Ydot, YdotI[i], ark->imex); /* YdotI = -G(t,Y,0)   */
808:           VecScale(YdotI[i], -1.0);
809:         } else {
810:           VecAXPBYPCZ(YdotI[i], -ark->scoeff / h, ark->scoeff / h, 0, Z, Y[i]); /* YdotI = shift*(X-Z) */
811:         }
812:         if (ark->imex) {
813:           TSComputeRHSFunction(ts, t + h * c[i], Y[i], YdotRHS[i]);
814:         } else {
815:           VecZeroEntries(YdotRHS[i]);
816:         }
817:       }
818:       TSPostStage(ts, ark->stage_time, i, Y);
819:     }

821:     ark->status = TS_STEP_INCOMPLETE;
822:     TSEvaluateStep_ARKIMEX(ts, tab->order, ts->vec_sol, NULL);
823:     ark->status = TS_STEP_PENDING;
824:     TSGetAdapt(ts, &adapt);
825:     TSAdaptCandidatesClear(adapt);
826:     TSAdaptCandidateAdd(adapt, tab->name, tab->order, 1, tab->ccfl, (PetscReal)tab->s, PETSC_TRUE);
827:     TSAdaptChoose(adapt, ts, ts->time_step, NULL, &next_time_step, &accept);
828:     ark->status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
829:     if (!accept) { /* Roll back the current step */
830:       TSRollBack_ARKIMEX(ts);
831:       ts->time_step = next_time_step;
832:       goto reject_step;
833:     }

835:     ts->ptime += ts->time_step;
836:     ts->time_step = next_time_step;
837:     break;

839:   reject_step:
840:     ts->reject++;
841:     accept = PETSC_FALSE;
842:     if (!ts->reason && ++rejections > ts->max_reject && ts->max_reject >= 0) {
843:       ts->reason = TS_DIVERGED_STEP_REJECTED;
844:       PetscInfo(ts, "Step=%" PetscInt_FMT ", step rejections %" PetscInt_FMT " greater than current TS allowed, stopping solve\n", ts->steps, rejections);
845:     }
846:   }
847:   return 0;
848: }

850: static PetscErrorCode TSInterpolate_ARKIMEX(TS ts, PetscReal itime, Vec X)
851: {
852:   TS_ARKIMEX      *ark = (TS_ARKIMEX *)ts->data;
853:   PetscInt         s = ark->tableau->s, pinterp = ark->tableau->pinterp, i, j;
854:   PetscReal        h;
855:   PetscReal        tt, t;
856:   PetscScalar     *bt, *b;
857:   const PetscReal *Bt = ark->tableau->binterpt, *B = ark->tableau->binterp;

860:   switch (ark->status) {
861:   case TS_STEP_INCOMPLETE:
862:   case TS_STEP_PENDING:
863:     h = ts->time_step;
864:     t = (itime - ts->ptime) / h;
865:     break;
866:   case TS_STEP_COMPLETE:
867:     h = ts->ptime - ts->ptime_prev;
868:     t = (itime - ts->ptime) / h + 1; /* In the interval [0,1] */
869:     break;
870:   default:
871:     SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Invalid TSStepStatus");
872:   }
873:   PetscMalloc2(s, &bt, s, &b);
874:   for (i = 0; i < s; i++) bt[i] = b[i] = 0;
875:   for (j = 0, tt = t; j < pinterp; j++, tt *= t) {
876:     for (i = 0; i < s; i++) {
877:       bt[i] += h * Bt[i * pinterp + j] * tt;
878:       b[i] += h * B[i * pinterp + j] * tt;
879:     }
880:   }
881:   VecCopy(ark->Y[0], X);
882:   VecMAXPY(X, s, bt, ark->YdotI);
883:   VecMAXPY(X, s, b, ark->YdotRHS);
884:   PetscFree2(bt, b);
885:   return 0;
886: }

888: static PetscErrorCode TSExtrapolate_ARKIMEX(TS ts, PetscReal c, Vec X)
889: {
890:   TS_ARKIMEX      *ark = (TS_ARKIMEX *)ts->data;
891:   PetscInt         s = ark->tableau->s, pinterp = ark->tableau->pinterp, i, j;
892:   PetscReal        h, h_prev, t, tt;
893:   PetscScalar     *bt, *b;
894:   const PetscReal *Bt = ark->tableau->binterpt, *B = ark->tableau->binterp;

897:   PetscCalloc2(s, &bt, s, &b);
898:   h      = ts->time_step;
899:   h_prev = ts->ptime - ts->ptime_prev;
900:   t      = 1 + h / h_prev * c;
901:   for (j = 0, tt = t; j < pinterp; j++, tt *= t) {
902:     for (i = 0; i < s; i++) {
903:       bt[i] += h * Bt[i * pinterp + j] * tt;
904:       b[i] += h * B[i * pinterp + j] * tt;
905:     }
906:   }
908:   VecCopy(ark->Y_prev[0], X);
909:   VecMAXPY(X, s, bt, ark->YdotI_prev);
910:   VecMAXPY(X, s, b, ark->YdotRHS_prev);
911:   PetscFree2(bt, b);
912:   return 0;
913: }

915: /*------------------------------------------------------------*/

917: static PetscErrorCode TSARKIMEXTableauReset(TS ts)
918: {
919:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
920:   ARKTableau  tab = ark->tableau;

922:   if (!tab) return 0;
923:   PetscFree(ark->work);
924:   VecDestroyVecs(tab->s, &ark->Y);
925:   VecDestroyVecs(tab->s, &ark->YdotI);
926:   VecDestroyVecs(tab->s, &ark->YdotRHS);
927:   VecDestroyVecs(tab->s, &ark->Y_prev);
928:   VecDestroyVecs(tab->s, &ark->YdotI_prev);
929:   VecDestroyVecs(tab->s, &ark->YdotRHS_prev);
930:   return 0;
931: }

933: static PetscErrorCode TSReset_ARKIMEX(TS ts)
934: {
935:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;

937:   TSARKIMEXTableauReset(ts);
938:   VecDestroy(&ark->Ydot);
939:   VecDestroy(&ark->Ydot0);
940:   VecDestroy(&ark->Z);
941:   return 0;
942: }

944: static PetscErrorCode TSARKIMEXGetVecs(TS ts, DM dm, Vec *Z, Vec *Ydot)
945: {
946:   TS_ARKIMEX *ax = (TS_ARKIMEX *)ts->data;

948:   if (Z) {
949:     if (dm && dm != ts->dm) {
950:       DMGetNamedGlobalVector(dm, "TSARKIMEX_Z", Z);
951:     } else *Z = ax->Z;
952:   }
953:   if (Ydot) {
954:     if (dm && dm != ts->dm) {
955:       DMGetNamedGlobalVector(dm, "TSARKIMEX_Ydot", Ydot);
956:     } else *Ydot = ax->Ydot;
957:   }
958:   return 0;
959: }

961: static PetscErrorCode TSARKIMEXRestoreVecs(TS ts, DM dm, Vec *Z, Vec *Ydot)
962: {
963:   if (Z) {
964:     if (dm && dm != ts->dm) DMRestoreNamedGlobalVector(dm, "TSARKIMEX_Z", Z);
965:   }
966:   if (Ydot) {
967:     if (dm && dm != ts->dm) DMRestoreNamedGlobalVector(dm, "TSARKIMEX_Ydot", Ydot);
968:   }
969:   return 0;
970: }

972: /*
973:   This defines the nonlinear equation that is to be solved with SNES
974:   G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
975: */
976: static PetscErrorCode SNESTSFormFunction_ARKIMEX(SNES snes, Vec X, Vec F, TS ts)
977: {
978:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
979:   DM          dm, dmsave;
980:   Vec         Z, Ydot;
981:   PetscReal   shift = ark->scoeff / ts->time_step;

983:   SNESGetDM(snes, &dm);
984:   TSARKIMEXGetVecs(ts, dm, &Z, &Ydot);
985:   VecAXPBYPCZ(Ydot, -shift, shift, 0, Z, X); /* Ydot = shift*(X-Z) */
986:   dmsave = ts->dm;
987:   ts->dm = dm;

989:   TSComputeIFunction(ts, ark->stage_time, X, Ydot, F, ark->imex);

991:   ts->dm = dmsave;
992:   TSARKIMEXRestoreVecs(ts, dm, &Z, &Ydot);
993:   return 0;
994: }

996: static PetscErrorCode SNESTSFormJacobian_ARKIMEX(SNES snes, Vec X, Mat A, Mat B, TS ts)
997: {
998:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
999:   DM          dm, dmsave;
1000:   Vec         Ydot;
1001:   PetscReal   shift = ark->scoeff / ts->time_step;

1003:   SNESGetDM(snes, &dm);
1004:   TSARKIMEXGetVecs(ts, dm, NULL, &Ydot);
1005:   /* ark->Ydot has already been computed in SNESTSFormFunction_ARKIMEX (SNES guarantees this) */
1006:   dmsave = ts->dm;
1007:   ts->dm = dm;

1009:   TSComputeIJacobian(ts, ark->stage_time, X, Ydot, shift, A, B, ark->imex);

1011:   ts->dm = dmsave;
1012:   TSARKIMEXRestoreVecs(ts, dm, NULL, &Ydot);
1013:   return 0;
1014: }

1016: static PetscErrorCode DMCoarsenHook_TSARKIMEX(DM fine, DM coarse, void *ctx)
1017: {
1018:   return 0;
1019: }

1021: static PetscErrorCode DMRestrictHook_TSARKIMEX(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse, void *ctx)
1022: {
1023:   TS  ts = (TS)ctx;
1024:   Vec Z, Z_c;

1026:   TSARKIMEXGetVecs(ts, fine, &Z, NULL);
1027:   TSARKIMEXGetVecs(ts, coarse, &Z_c, NULL);
1028:   MatRestrict(restrct, Z, Z_c);
1029:   VecPointwiseMult(Z_c, rscale, Z_c);
1030:   TSARKIMEXRestoreVecs(ts, fine, &Z, NULL);
1031:   TSARKIMEXRestoreVecs(ts, coarse, &Z_c, NULL);
1032:   return 0;
1033: }

1035: static PetscErrorCode DMSubDomainHook_TSARKIMEX(DM dm, DM subdm, void *ctx)
1036: {
1037:   return 0;
1038: }

1040: static PetscErrorCode DMSubDomainRestrictHook_TSARKIMEX(DM dm, VecScatter gscat, VecScatter lscat, DM subdm, void *ctx)
1041: {
1042:   TS  ts = (TS)ctx;
1043:   Vec Z, Z_c;

1045:   TSARKIMEXGetVecs(ts, dm, &Z, NULL);
1046:   TSARKIMEXGetVecs(ts, subdm, &Z_c, NULL);

1048:   VecScatterBegin(gscat, Z, Z_c, INSERT_VALUES, SCATTER_FORWARD);
1049:   VecScatterEnd(gscat, Z, Z_c, INSERT_VALUES, SCATTER_FORWARD);

1051:   TSARKIMEXRestoreVecs(ts, dm, &Z, NULL);
1052:   TSARKIMEXRestoreVecs(ts, subdm, &Z_c, NULL);
1053:   return 0;
1054: }

1056: static PetscErrorCode TSARKIMEXTableauSetUp(TS ts)
1057: {
1058:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
1059:   ARKTableau  tab = ark->tableau;

1061:   PetscMalloc1(tab->s, &ark->work);
1062:   VecDuplicateVecs(ts->vec_sol, tab->s, &ark->Y);
1063:   VecDuplicateVecs(ts->vec_sol, tab->s, &ark->YdotI);
1064:   VecDuplicateVecs(ts->vec_sol, tab->s, &ark->YdotRHS);
1065:   if (ark->extrapolate) {
1066:     VecDuplicateVecs(ts->vec_sol, tab->s, &ark->Y_prev);
1067:     VecDuplicateVecs(ts->vec_sol, tab->s, &ark->YdotI_prev);
1068:     VecDuplicateVecs(ts->vec_sol, tab->s, &ark->YdotRHS_prev);
1069:   }
1070:   return 0;
1071: }

1073: static PetscErrorCode TSSetUp_ARKIMEX(TS ts)
1074: {
1075:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
1076:   DM          dm;
1077:   SNES        snes;

1079:   TSARKIMEXTableauSetUp(ts);
1080:   VecDuplicate(ts->vec_sol, &ark->Ydot);
1081:   VecDuplicate(ts->vec_sol, &ark->Ydot0);
1082:   VecDuplicate(ts->vec_sol, &ark->Z);
1083:   TSGetDM(ts, &dm);
1084:   DMCoarsenHookAdd(dm, DMCoarsenHook_TSARKIMEX, DMRestrictHook_TSARKIMEX, ts);
1085:   DMSubDomainHookAdd(dm, DMSubDomainHook_TSARKIMEX, DMSubDomainRestrictHook_TSARKIMEX, ts);
1086:   TSGetSNES(ts, &snes);
1087:   return 0;
1088: }
1089: /*------------------------------------------------------------*/

1091: static PetscErrorCode TSSetFromOptions_ARKIMEX(TS ts, PetscOptionItems *PetscOptionsObject)
1092: {
1093:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;

1095:   PetscOptionsHeadBegin(PetscOptionsObject, "ARKIMEX ODE solver options");
1096:   {
1097:     ARKTableauLink link;
1098:     PetscInt       count, choice;
1099:     PetscBool      flg;
1100:     const char   **namelist;
1101:     for (link = ARKTableauList, count = 0; link; link = link->next, count++)
1102:       ;
1103:     PetscMalloc1(count, (char ***)&namelist);
1104:     for (link = ARKTableauList, count = 0; link; link = link->next, count++) namelist[count] = link->tab.name;
1105:     PetscOptionsEList("-ts_arkimex_type", "Family of ARK IMEX method", "TSARKIMEXSetType", (const char *const *)namelist, count, ark->tableau->name, &choice, &flg);
1106:     if (flg) TSARKIMEXSetType(ts, namelist[choice]);
1107:     PetscFree(namelist);

1109:     flg = (PetscBool)!ark->imex;
1110:     PetscOptionsBool("-ts_arkimex_fully_implicit", "Solve the problem fully implicitly", "TSARKIMEXSetFullyImplicit", flg, &flg, NULL);
1111:     ark->imex = (PetscBool)!flg;
1112:     PetscOptionsBool("-ts_arkimex_initial_guess_extrapolate", "Extrapolate the initial guess for the stage solution from stage values of the previous time step", "", ark->extrapolate, &ark->extrapolate, NULL);
1113:   }
1114:   PetscOptionsHeadEnd();
1115:   return 0;
1116: }

1118: static PetscErrorCode TSView_ARKIMEX(TS ts, PetscViewer viewer)
1119: {
1120:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;
1121:   PetscBool   iascii;

1123:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
1124:   if (iascii) {
1125:     ARKTableau    tab = ark->tableau;
1126:     TSARKIMEXType arktype;
1127:     char          buf[512];
1128:     PetscBool     flg;

1130:     TSARKIMEXGetType(ts, &arktype);
1131:     TSARKIMEXGetFullyImplicit(ts, &flg);
1132:     PetscViewerASCIIPrintf(viewer, "  ARK IMEX %s\n", arktype);
1133:     PetscFormatRealArray(buf, sizeof(buf), "% 8.6f", tab->s, tab->ct);
1134:     PetscViewerASCIIPrintf(viewer, "  Stiff abscissa       ct = %s\n", buf);
1135:     PetscFormatRealArray(buf, sizeof(buf), "% 8.6f", tab->s, tab->c);
1136:     PetscViewerASCIIPrintf(viewer, "Fully implicit: %s\n", flg ? "yes" : "no");
1137:     PetscViewerASCIIPrintf(viewer, "Stiffly accurate: %s\n", tab->stiffly_accurate ? "yes" : "no");
1138:     PetscViewerASCIIPrintf(viewer, "Explicit first stage: %s\n", tab->explicit_first_stage ? "yes" : "no");
1139:     PetscViewerASCIIPrintf(viewer, "FSAL property: %s\n", tab->FSAL_implicit ? "yes" : "no");
1140:     PetscViewerASCIIPrintf(viewer, "  Nonstiff abscissa     c = %s\n", buf);
1141:   }
1142:   return 0;
1143: }

1145: static PetscErrorCode TSLoad_ARKIMEX(TS ts, PetscViewer viewer)
1146: {
1147:   SNES    snes;
1148:   TSAdapt adapt;

1150:   TSGetAdapt(ts, &adapt);
1151:   TSAdaptLoad(adapt, viewer);
1152:   TSGetSNES(ts, &snes);
1153:   SNESLoad(snes, viewer);
1154:   /* function and Jacobian context for SNES when used with TS is always ts object */
1155:   SNESSetFunction(snes, NULL, NULL, ts);
1156:   SNESSetJacobian(snes, NULL, NULL, NULL, ts);
1157:   return 0;
1158: }

1160: /*@C
1161:   TSARKIMEXSetType - Set the type of `TSARKIMEX` scheme

1163:   Logically collective

1165:   Input Parameters:
1166: +  ts - timestepping context
1167: -  arktype - type of `TSARKIMEX` scheme

1169:   Options Database Key:
1170: .  -ts_arkimex_type <1bee,a2,l2,ars122,2c,2d,2e,prssp2,3,bpr3,ars443,4,5> - set `TSARKIMEX` scheme type

1172:   Level: intermediate

1174: .seealso: [](chapter_ts), `TSARKIMEXGetType()`, `TSARKIMEX`, `TSARKIMEXType`, `TSARKIMEX1BEE`, `TSARKIMEXA2`, `TSARKIMEXL2`, `TSARKIMEXARS122`, `TSARKIMEX2C`, `TSARKIMEX2D`, `TSARKIMEX2E`, `TSARKIMEXPRSSP2`,
1175:           `TSARKIMEX3`, `TSARKIMEXBPR3`, `TSARKIMEXARS443`, `TSARKIMEX4`, `TSARKIMEX5`
1176: @*/
1177: PetscErrorCode TSARKIMEXSetType(TS ts, TSARKIMEXType arktype)
1178: {
1181:   PetscTryMethod(ts, "TSARKIMEXSetType_C", (TS, TSARKIMEXType), (ts, arktype));
1182:   return 0;
1183: }

1185: /*@C
1186:   TSARKIMEXGetType - Get the type of `TSARKIMEX` scheme

1188:   Logically collective

1190:   Input Parameter:
1191: .  ts - timestepping context

1193:   Output Parameter:
1194: .  arktype - type of `TSARKIMEX` scheme

1196:   Level: intermediate

1198: .seealso: [](chapter_ts), `TSARKIMEX`c, `TSARKIMEXGetType()`
1199: @*/
1200: PetscErrorCode TSARKIMEXGetType(TS ts, TSARKIMEXType *arktype)
1201: {
1203:   PetscUseMethod(ts, "TSARKIMEXGetType_C", (TS, TSARKIMEXType *), (ts, arktype));
1204:   return 0;
1205: }

1207: /*@
1208:   TSARKIMEXSetFullyImplicit - Solve both parts of the equation implicitly, including the part that is normally solved explicitly

1210:   Logically collective

1212:   Input Parameters:
1213: +  ts - timestepping context
1214: -  flg - `PETSC_TRUE` for fully implicit

1216:   Level: intermediate

1218: .seealso: [](chapter_ts), `TSARKIMEX`, `TSARKIMEXGetType()`, `TSARKIMEXGetFullyImplicit()`
1219: @*/
1220: PetscErrorCode TSARKIMEXSetFullyImplicit(TS ts, PetscBool flg)
1221: {
1224:   PetscTryMethod(ts, "TSARKIMEXSetFullyImplicit_C", (TS, PetscBool), (ts, flg));
1225:   return 0;
1226: }

1228: /*@
1229:   TSARKIMEXGetFullyImplicit - Inquires if both parts of the equation are solved implicitly

1231:   Logically collective

1233:   Input Parameter:
1234: .  ts - timestepping context

1236:   Output Parameter:
1237: .  flg - `PETSC_TRUE` for fully implicit

1239:   Level: intermediate

1241: .seealso: [](chapter_ts), `TSARKIMEXGetType()`, `TSARKIMEXSetFullyImplicit()`
1242: @*/
1243: PetscErrorCode TSARKIMEXGetFullyImplicit(TS ts, PetscBool *flg)
1244: {
1247:   PetscUseMethod(ts, "TSARKIMEXGetFullyImplicit_C", (TS, PetscBool *), (ts, flg));
1248:   return 0;
1249: }

1251: static PetscErrorCode TSARKIMEXGetType_ARKIMEX(TS ts, TSARKIMEXType *arktype)
1252: {
1253:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;

1255:   *arktype = ark->tableau->name;
1256:   return 0;
1257: }
1258: static PetscErrorCode TSARKIMEXSetType_ARKIMEX(TS ts, TSARKIMEXType arktype)
1259: {
1260:   TS_ARKIMEX    *ark = (TS_ARKIMEX *)ts->data;
1261:   PetscBool      match;
1262:   ARKTableauLink link;

1264:   if (ark->tableau) {
1265:     PetscStrcmp(ark->tableau->name, arktype, &match);
1266:     if (match) return 0;
1267:   }
1268:   for (link = ARKTableauList; link; link = link->next) {
1269:     PetscStrcmp(link->tab.name, arktype, &match);
1270:     if (match) {
1271:       if (ts->setupcalled) TSARKIMEXTableauReset(ts);
1272:       ark->tableau = &link->tab;
1273:       if (ts->setupcalled) TSARKIMEXTableauSetUp(ts);
1274:       ts->default_adapt_type = ark->tableau->bembed ? TSADAPTBASIC : TSADAPTNONE;
1275:       return 0;
1276:     }
1277:   }
1278:   SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_UNKNOWN_TYPE, "Could not find '%s'", arktype);
1279: }

1281: static PetscErrorCode TSARKIMEXSetFullyImplicit_ARKIMEX(TS ts, PetscBool flg)
1282: {
1283:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;

1285:   ark->imex = (PetscBool)!flg;
1286:   return 0;
1287: }

1289: static PetscErrorCode TSARKIMEXGetFullyImplicit_ARKIMEX(TS ts, PetscBool *flg)
1290: {
1291:   TS_ARKIMEX *ark = (TS_ARKIMEX *)ts->data;

1293:   *flg = (PetscBool)!ark->imex;
1294:   return 0;
1295: }

1297: static PetscErrorCode TSDestroy_ARKIMEX(TS ts)
1298: {
1299:   TSReset_ARKIMEX(ts);
1300:   if (ts->dm) {
1301:     DMCoarsenHookRemove(ts->dm, DMCoarsenHook_TSARKIMEX, DMRestrictHook_TSARKIMEX, ts);
1302:     DMSubDomainHookRemove(ts->dm, DMSubDomainHook_TSARKIMEX, DMSubDomainRestrictHook_TSARKIMEX, ts);
1303:   }
1304:   PetscFree(ts->data);
1305:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXGetType_C", NULL);
1306:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXSetType_C", NULL);
1307:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXSetFullyImplicit_C", NULL);
1308:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXGetFullyImplicit_C", NULL);
1309:   return 0;
1310: }

1312: /* ------------------------------------------------------------ */
1313: /*MC
1314:       TSARKIMEX - ODE and DAE solver using additive Runge-Kutta IMEX schemes

1316:   These methods are intended for problems with well-separated time scales, especially when a slow scale is strongly
1317:   nonlinear such that it is expensive to solve with a fully implicit method. The user should provide the stiff part
1318:   of the equation using `TSSetIFunction()` and the non-stiff part with `TSSetRHSFunction()`.

1320:   Level: beginner

1322:   Notes:
1323:   The default is `TSARKIMEX3`, it can be changed with `TSARKIMEXSetType()` or -ts_arkimex_type

1325:   If the equation is implicit or a DAE, then `TSSetEquationType()` needs to be set accordingly. Refer to the manual for further information.

1327:   Methods with an explicit stage can only be used with ODE in which the stiff part G(t,X,Xdot) has the form Xdot + Ghat(t,X).

1329:   Consider trying `TSROSW` if the stiff part is linear or weakly nonlinear.

1331: .seealso: [](chapter_ts), `TSCreate()`, `TS`, `TSSetType()`, `TSARKIMEXSetType()`, `TSARKIMEXGetType()`, `TSARKIMEXSetFullyImplicit()`, `TSARKIMEXGetFullyImplicit()`,
1332:           `TSARKIMEX1BEE`, `TSARKIMEX2C`, `TSARKIMEX2D`, `TSARKIMEX2E`, `TSARKIMEX3`, `TSARKIMEXL2`, `TSARKIMEXA2`, `TSARKIMEXARS122`,
1333:           `TSARKIMEX4`, `TSARKIMEX5`, `TSARKIMEXPRSSP2`, `TSARKIMEXARS443`, `TSARKIMEXBPR3`, `TSARKIMEXType`, `TSARKIMEXRegister()`, `TSType`
1334: M*/
1335: PETSC_EXTERN PetscErrorCode TSCreate_ARKIMEX(TS ts)
1336: {
1337:   TS_ARKIMEX *th;

1339:   TSARKIMEXInitializePackage();

1341:   ts->ops->reset          = TSReset_ARKIMEX;
1342:   ts->ops->destroy        = TSDestroy_ARKIMEX;
1343:   ts->ops->view           = TSView_ARKIMEX;
1344:   ts->ops->load           = TSLoad_ARKIMEX;
1345:   ts->ops->setup          = TSSetUp_ARKIMEX;
1346:   ts->ops->step           = TSStep_ARKIMEX;
1347:   ts->ops->interpolate    = TSInterpolate_ARKIMEX;
1348:   ts->ops->evaluatestep   = TSEvaluateStep_ARKIMEX;
1349:   ts->ops->rollback       = TSRollBack_ARKIMEX;
1350:   ts->ops->setfromoptions = TSSetFromOptions_ARKIMEX;
1351:   ts->ops->snesfunction   = SNESTSFormFunction_ARKIMEX;
1352:   ts->ops->snesjacobian   = SNESTSFormJacobian_ARKIMEX;

1354:   ts->usessnes = PETSC_TRUE;

1356:   PetscNew(&th);
1357:   ts->data = (void *)th;
1358:   th->imex = PETSC_TRUE;

1360:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXGetType_C", TSARKIMEXGetType_ARKIMEX);
1361:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXSetType_C", TSARKIMEXSetType_ARKIMEX);
1362:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXSetFullyImplicit_C", TSARKIMEXSetFullyImplicit_ARKIMEX);
1363:   PetscObjectComposeFunction((PetscObject)ts, "TSARKIMEXGetFullyImplicit_C", TSARKIMEXGetFullyImplicit_ARKIMEX);

1365:   TSARKIMEXSetType(ts, TSARKIMEXDefault);
1366:   return 0;
1367: }