Actual source code: rosw.c

petsc-3.7.3 2016-07-24
Report Typos and Errors
  1: /*
  2:   Code for timestepping with Rosenbrock W methods

  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.
 10:   This method is designed to be linearly implicit on F and can use an approximate and lagged Jacobian.

 12: */
 13: #include <petsc/private/tsimpl.h>                /*I   "petscts.h"   I*/
 14: #include <petscdm.h>

 16: #include <petsc/private/kernels/blockinvert.h>

 18: static TSRosWType TSRosWDefault = TSROSWRA34PW2;
 19: static PetscBool  TSRosWRegisterAllCalled;
 20: static PetscBool  TSRosWPackageInitialized;

 22: typedef struct _RosWTableau *RosWTableau;
 23: struct _RosWTableau {
 24:   char      *name;
 25:   PetscInt  order;              /* Classical approximation order of the method */
 26:   PetscInt  s;                  /* Number of stages */
 27:   PetscInt  pinterp;            /* Interpolation order */
 28:   PetscReal *A;                 /* Propagation table, strictly lower triangular */
 29:   PetscReal *Gamma;             /* Stage table, lower triangular with nonzero diagonal */
 30:   PetscBool *GammaZeroDiag;     /* Diagonal entries that are zero in stage table Gamma, vector indicating explicit statages */
 31:   PetscReal *GammaExplicitCorr; /* Coefficients for correction terms needed for explicit stages in transformed variables*/
 32:   PetscReal *b;                 /* Step completion table */
 33:   PetscReal *bembed;            /* Step completion table for embedded method of order one less */
 34:   PetscReal *ASum;              /* Row sum of A */
 35:   PetscReal *GammaSum;          /* Row sum of Gamma, only needed for non-autonomous systems */
 36:   PetscReal *At;                /* Propagation table in transformed variables */
 37:   PetscReal *bt;                /* Step completion table in transformed variables */
 38:   PetscReal *bembedt;           /* Step completion table of order one less in transformed variables */
 39:   PetscReal *GammaInv;          /* Inverse of Gamma, used for transformed variables */
 40:   PetscReal ccfl;               /* Placeholder for CFL coefficient relative to forward Euler */
 41:   PetscReal *binterpt;          /* Dense output formula */
 42: };
 43: typedef struct _RosWTableauLink *RosWTableauLink;
 44: struct _RosWTableauLink {
 45:   struct _RosWTableau tab;
 46:   RosWTableauLink     next;
 47: };
 48: static RosWTableauLink RosWTableauList;

 50: typedef struct {
 51:   RosWTableau  tableau;
 52:   Vec          *Y;               /* States computed during the step, used to complete the step */
 53:   Vec          Ydot;             /* Work vector holding Ydot during residual evaluation */
 54:   Vec          Ystage;           /* Work vector for the state value at each stage */
 55:   Vec          Zdot;             /* Ydot = Zdot + shift*Y */
 56:   Vec          Zstage;           /* Y = Zstage + Y */
 57:   Vec          vec_sol_prev;     /* Solution from the previous step (used for interpolation and rollback)*/
 58:   PetscScalar  *work;            /* Scalar work space of length number of stages, used to prepare VecMAXPY() */
 59:   PetscReal    scoeff;           /* shift = scoeff/dt */
 60:   PetscReal    stage_time;
 61:   PetscReal    stage_explicit;     /* Flag indicates that the current stage is explicit */
 62:   PetscBool    recompute_jacobian; /* Recompute the Jacobian at each stage, default is to freeze the Jacobian at the start of each step */
 63:   TSStepStatus status;
 64: } TS_RosW;

 66: /*MC
 67:      TSROSWTHETA1 - One stage first order L-stable Rosenbrock-W scheme (aka theta method).

 69:      Only an approximate Jacobian is needed.

 71:      Level: intermediate

 73: .seealso: TSROSW
 74: M*/

 76: /*MC
 77:      TSROSWTHETA2 - One stage second order A-stable Rosenbrock-W scheme (aka theta method).

 79:      Only an approximate Jacobian is needed.

 81:      Level: intermediate

 83: .seealso: TSROSW
 84: M*/

 86: /*MC
 87:      TSROSW2M - Two stage second order L-stable Rosenbrock-W scheme.

 89:      Only an approximate Jacobian is needed. By default, it is only recomputed once per step. This method is a reflection of TSROSW2P.

 91:      Level: intermediate

 93: .seealso: TSROSW
 94: M*/

 96: /*MC
 97:      TSROSW2P - Two stage second order L-stable Rosenbrock-W scheme.

 99:      Only an approximate Jacobian is needed. By default, it is only recomputed once per step. This method is a reflection of TSROSW2M.

101:      Level: intermediate

103: .seealso: TSROSW
104: M*/

106: /*MC
107:      TSROSWRA3PW - Three stage third order Rosenbrock-W scheme for PDAE of index 1.

109:      Only an approximate Jacobian is needed. By default, it is only recomputed once per step.

111:      This is strongly A-stable with R(infty) = 0.73. The embedded method of order 2 is strongly A-stable with R(infty) = 0.73.

113:      References:
114: .  1. -   Rang and Angermann, New Rosenbrock W methods of order 3 for partial differential algebraic equations of index 1, 2005.

116:      Level: intermediate

118: .seealso: TSROSW
119: M*/

121: /*MC
122:      TSROSWRA34PW2 - Four stage third order L-stable Rosenbrock-W scheme for PDAE of index 1.

124:      Only an approximate Jacobian is needed. By default, it is only recomputed once per step.

126:      This is strongly A-stable with R(infty) = 0. The embedded method of order 2 is strongly A-stable with R(infty) = 0.48.

128:      References:
129: .  1. -   Rang and Angermann, New Rosenbrock W methods of order 3 for partial differential algebraic equations of index 1, 2005.

131:      Level: intermediate

133: .seealso: TSROSW
134: M*/

136: /*MC
137:      TSROSWRODAS3 - Four stage third order L-stable Rosenbrock scheme

139:      By default, the Jacobian is only recomputed once per step.

141:      Both the third order and embedded second order methods are stiffly accurate and L-stable.

143:      References:
144: .  1. -   Sandu et al, Benchmarking stiff ODE solvers for atmospheric chemistry problems II, Rosenbrock solvers, 1997.

146:      Level: intermediate

148: .seealso: TSROSW, TSROSWSANDU3
149: M*/

151: /*MC
152:      TSROSWSANDU3 - Three stage third order L-stable Rosenbrock scheme

154:      By default, the Jacobian is only recomputed once per step.

156:      The third order method is L-stable, but not stiffly accurate.
157:      The second order embedded method is strongly A-stable with R(infty) = 0.5.
158:      The internal stages are L-stable.
159:      This method is called ROS3 in the paper.

161:      References:
162: .  1. -   Sandu et al, Benchmarking stiff ODE solvers for atmospheric chemistry problems II, Rosenbrock solvers, 1997.

164:      Level: intermediate

166: .seealso: TSROSW, TSROSWRODAS3
167: M*/

169: /*MC
170:      TSROSWASSP3P3S1C - A-stable Rosenbrock-W method with SSP explicit part, third order, three stages

172:      By default, the Jacobian is only recomputed once per step.

174:      A-stable SPP explicit order 3, 3 stages, CFL 1 (eff = 1/3)

176:      References:
177: .     Emil Constantinescu

179:      Level: intermediate

181: .seealso: TSROSW, TSROSWLASSP3P4S2C, TSROSWLLSSP3P4S2C, SSP
182: M*/

184: /*MC
185:      TSROSWLASSP3P4S2C - L-stable Rosenbrock-W method with SSP explicit part, third order, four stages

187:      By default, the Jacobian is only recomputed once per step.

189:      L-stable (A-stable embedded) SPP explicit order 3, 4 stages, CFL 2 (eff = 1/2)

191:      References:
192: .     Emil Constantinescu

194:      Level: intermediate

196: .seealso: TSROSW, TSROSWASSP3P3S1C, TSROSWLLSSP3P4S2C, TSSSP
197: M*/

199: /*MC
200:      TSROSWLLSSP3P4S2C - L-stable Rosenbrock-W method with SSP explicit part, third order, four stages

202:      By default, the Jacobian is only recomputed once per step.

204:      L-stable (L-stable embedded) SPP explicit order 3, 4 stages, CFL 2 (eff = 1/2)

206:      References:
207: .     Emil Constantinescu

209:      Level: intermediate

211: .seealso: TSROSW, TSROSWASSP3P3S1C, TSROSWLASSP3P4S2C, TSSSP
212: M*/

214: /*MC
215:      TSROSWGRK4T - four stage, fourth order Rosenbrock (not W) method from Kaps and Rentrop

217:      By default, the Jacobian is only recomputed once per step.

219:      A(89.3 degrees)-stable, |R(infty)| = 0.454.

221:      This method does not provide a dense output formula.

223:      References:
224: +   1. -  Kaps and Rentrop, Generalized Runge Kutta methods of order four with stepsize control for stiff ordinary differential equations, 1979.
225: -   2. -  Hairer and Wanner, Solving Ordinary Differential Equations II, Section 4 Table 7.2.

227:      Hairer's code ros4.f

229:      Level: intermediate

231: .seealso: TSROSW, TSROSWSHAMP4, TSROSWVELDD4, TSROSW4L
232: M*/

234: /*MC
235:      TSROSWSHAMP4 - four stage, fourth order Rosenbrock (not W) method from Shampine

237:      By default, the Jacobian is only recomputed once per step.

239:      A-stable, |R(infty)| = 1/3.

241:      This method does not provide a dense output formula.

243:      References:
244: +   1. -  Shampine, Implementation of Rosenbrock methods, 1982.
245: -   2. -  Hairer and Wanner, Solving Ordinary Differential Equations II, Section 4 Table 7.2.

247:      Hairer's code ros4.f

249:      Level: intermediate

251: .seealso: TSROSW, TSROSWGRK4T, TSROSWVELDD4, TSROSW4L
252: M*/

254: /*MC
255:      TSROSWVELDD4 - four stage, fourth order Rosenbrock (not W) method from van Veldhuizen

257:      By default, the Jacobian is only recomputed once per step.

259:      A(89.5 degrees)-stable, |R(infty)| = 0.24.

261:      This method does not provide a dense output formula.

263:      References:
264: +   1. -  van Veldhuizen, D stability and Kaps Rentrop methods, 1984.
265: -   2. -  Hairer and Wanner, Solving Ordinary Differential Equations II, Section 4 Table 7.2.

267:      Hairer's code ros4.f

269:      Level: intermediate

271: .seealso: TSROSW, TSROSWGRK4T, TSROSWSHAMP4, TSROSW4L
272: M*/

274: /*MC
275:      TSROSW4L - four stage, fourth order Rosenbrock (not W) method

277:      By default, the Jacobian is only recomputed once per step.

279:      A-stable and L-stable

281:      This method does not provide a dense output formula.

283:      References:
284: .  1. -   Hairer and Wanner, Solving Ordinary Differential Equations II, Section 4 Table 7.2.

286:      Hairer's code ros4.f

288:      Level: intermediate

290: .seealso: TSROSW, TSROSWGRK4T, TSROSWSHAMP4, TSROSW4L
291: M*/

295: /*@C
296:   TSRosWRegisterAll - Registers all of the Rosenbrock-W methods in TSRosW

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

300:   Level: advanced

302: .keywords: TS, TSRosW, register, all

304: .seealso:  TSRosWRegisterDestroy()
305: @*/
306: PetscErrorCode TSRosWRegisterAll(void)
307: {

311:   if (TSRosWRegisterAllCalled) return(0);
312:   TSRosWRegisterAllCalled = PETSC_TRUE;

314:   {
315:     const PetscReal A = 0;
316:     const PetscReal Gamma = 1;
317:     const PetscReal b = 1;
318:     const PetscReal binterpt=1;

320:     TSRosWRegister(TSROSWTHETA1,1,1,&A,&Gamma,&b,NULL,1,&binterpt);
321:   }

323:   {
324:     const PetscReal A = 0;
325:     const PetscReal Gamma = 0.5;
326:     const PetscReal b = 1;
327:     const PetscReal binterpt=1;

329:     TSRosWRegister(TSROSWTHETA2,2,1,&A,&Gamma,&b,NULL,1,&binterpt);
330:   }

332:   {
333:     /*const PetscReal g = 1. + 1./PetscSqrtReal(2.0);   Direct evaluation: 1.707106781186547524401. Used for setting up arrays of values known at compile time below. */
334:     const PetscReal
335:       A[2][2]     = {{0,0}, {1.,0}},
336:       Gamma[2][2] = {{1.707106781186547524401,0}, {-2.*1.707106781186547524401,1.707106781186547524401}},
337:       b[2]        = {0.5,0.5},
338:       b1[2]       = {1.0,0.0};
339:     PetscReal binterpt[2][2];
340:     binterpt[0][0] = 1.707106781186547524401 - 1.0;
341:     binterpt[1][0] = 2.0 - 1.707106781186547524401;
342:     binterpt[0][1] = 1.707106781186547524401 - 1.5;
343:     binterpt[1][1] = 1.5 - 1.707106781186547524401;

345:     TSRosWRegister(TSROSW2P,2,2,&A[0][0],&Gamma[0][0],b,b1,2,&binterpt[0][0]);
346:   }
347:   {
348:     /*const PetscReal g = 1. - 1./PetscSqrtReal(2.0);   Direct evaluation: 0.2928932188134524755992. Used for setting up arrays of values known at compile time below. */
349:     const PetscReal
350:       A[2][2]     = {{0,0}, {1.,0}},
351:       Gamma[2][2] = {{0.2928932188134524755992,0}, {-2.*0.2928932188134524755992,0.2928932188134524755992}},
352:       b[2]        = {0.5,0.5},
353:       b1[2]       = {1.0,0.0};
354:     PetscReal binterpt[2][2];
355:     binterpt[0][0] = 0.2928932188134524755992 - 1.0;
356:     binterpt[1][0] = 2.0 - 0.2928932188134524755992;
357:     binterpt[0][1] = 0.2928932188134524755992 - 1.5;
358:     binterpt[1][1] = 1.5 - 0.2928932188134524755992;

360:     TSRosWRegister(TSROSW2M,2,2,&A[0][0],&Gamma[0][0],b,b1,2,&binterpt[0][0]);
361:   }
362:   {
363:     /*const PetscReal g = 7.8867513459481287e-01; Directly written in-place below */
364:     PetscReal binterpt[3][2];
365:     const PetscReal
366:       A[3][3] = {{0,0,0},
367:                  {1.5773502691896257e+00,0,0},
368:                  {0.5,0,0}},
369:       Gamma[3][3] = {{7.8867513459481287e-01,0,0},
370:                      {-1.5773502691896257e+00,7.8867513459481287e-01,0},
371:                      {-6.7075317547305480e-01,-1.7075317547305482e-01,7.8867513459481287e-01}},
372:       b[3]  = {1.0566243270259355e-01,4.9038105676657971e-02,8.4529946162074843e-01},
373:       b2[3] = {-1.7863279495408180e-01,1./3.,8.4529946162074843e-01};

375:       binterpt[0][0] = -0.8094010767585034;
376:       binterpt[1][0] = -0.5;
377:       binterpt[2][0] = 2.3094010767585034;
378:       binterpt[0][1] = 0.9641016151377548;
379:       binterpt[1][1] = 0.5;
380:       binterpt[2][1] = -1.4641016151377548;

382:       TSRosWRegister(TSROSWRA3PW,3,3,&A[0][0],&Gamma[0][0],b,b2,2,&binterpt[0][0]);
383:   }
384:   {
385:     PetscReal  binterpt[4][3];
386:     /*const PetscReal g = 4.3586652150845900e-01; Directly written in-place below */
387:     const PetscReal
388:       A[4][4] = {{0,0,0,0},
389:                  {8.7173304301691801e-01,0,0,0},
390:                  {8.4457060015369423e-01,-1.1299064236484185e-01,0,0},
391:                  {0,0,1.,0}},
392:       Gamma[4][4] = {{4.3586652150845900e-01,0,0,0},
393:                      {-8.7173304301691801e-01,4.3586652150845900e-01,0,0},
394:                      {-9.0338057013044082e-01,5.4180672388095326e-02,4.3586652150845900e-01,0},
395:                      {2.4212380706095346e-01,-1.2232505839045147e+00,5.4526025533510214e-01,4.3586652150845900e-01}},
396:       b[4]  = {2.4212380706095346e-01,-1.2232505839045147e+00,1.5452602553351020e+00,4.3586652150845900e-01},
397:       b2[4] = {3.7810903145819369e-01,-9.6042292212423178e-02,5.0000000000000000e-01,2.1793326075422950e-01};

399:     binterpt[0][0]=1.0564298455794094;
400:     binterpt[1][0]=2.296429974281067;
401:     binterpt[2][0]=-1.307599564525376;
402:     binterpt[3][0]=-1.045260255335102;
403:     binterpt[0][1]=-1.3864882699759573;
404:     binterpt[1][1]=-8.262611700275677;
405:     binterpt[2][1]=7.250979895056055;
406:     binterpt[3][1]=2.398120075195581;
407:     binterpt[0][2]=0.5721822314575016;
408:     binterpt[1][2]=4.742931142090097;
409:     binterpt[2][2]=-4.398120075195578;
410:     binterpt[3][2]=-0.9169932983520199;

412:     TSRosWRegister(TSROSWRA34PW2,3,4,&A[0][0],&Gamma[0][0],b,b2,3,&binterpt[0][0]);
413:   }
414:   {
415:     /* const PetscReal g = 0.5;       Directly written in-place below */
416:     const PetscReal
417:       A[4][4] = {{0,0,0,0},
418:                  {0,0,0,0},
419:                  {1.,0,0,0},
420:                  {0.75,-0.25,0.5,0}},
421:       Gamma[4][4] = {{0.5,0,0,0},
422:                      {1.,0.5,0,0},
423:                      {-0.25,-0.25,0.5,0},
424:                      {1./12,1./12,-2./3,0.5}},
425:       b[4]  = {5./6,-1./6,-1./6,0.5},
426:       b2[4] = {0.75,-0.25,0.5,0};

428:     TSRosWRegister(TSROSWRODAS3,3,4,&A[0][0],&Gamma[0][0],b,b2,0,NULL);
429:   }
430:   {
431:     /*const PetscReal g = 0.43586652150845899941601945119356;       Directly written in-place below */
432:     const PetscReal
433:       A[3][3] = {{0,0,0},
434:                  {0.43586652150845899941601945119356,0,0},
435:                  {0.43586652150845899941601945119356,0,0}},
436:       Gamma[3][3] = {{0.43586652150845899941601945119356,0,0},
437:                      {-0.19294655696029095575009695436041,0.43586652150845899941601945119356,0},
438:                      {0,1.74927148125794685173529749738960,0.43586652150845899941601945119356}},
439:       b[3]  = {-0.75457412385404315829818998646589,1.94100407061964420292840123379419,-0.18642994676560104463021124732829},
440:       b2[3] = {-1.53358745784149585370766523913002,2.81745131148625772213931745457622,-0.28386385364476186843165221544619};

442:     PetscReal binterpt[3][2];
443:     binterpt[0][0] = 3.793692883777660870425141387941;
444:     binterpt[1][0] = -2.918692883777660870425141387941;
445:     binterpt[2][0] = 0.125;
446:     binterpt[0][1] = -0.725741064379812106687651020584;
447:     binterpt[1][1] = 0.559074397713145440020984353917;
448:     binterpt[2][1] = 0.16666666666666666666666666666667;

450:     TSRosWRegister(TSROSWSANDU3,3,3,&A[0][0],&Gamma[0][0],b,b2,2,&binterpt[0][0]);
451:   }
452:   {
453:     /*const PetscReal s3 = PetscSqrtReal(3.),g = (3.0+s3)/6.0;
454:      * Direct evaluation: s3 = 1.732050807568877293527;
455:      *                     g = 0.7886751345948128822546;
456:      * Values are directly inserted below to ensure availability at compile time (compiler warnings otherwise...) */
457:     const PetscReal
458:       A[3][3] = {{0,0,0},
459:                  {1,0,0},
460:                  {0.25,0.25,0}},
461:       Gamma[3][3] = {{0,0,0},
462:                      {(-3.0-1.732050807568877293527)/6.0,0.7886751345948128822546,0},
463:                      {(-3.0-1.732050807568877293527)/24.0,(-3.0-1.732050807568877293527)/8.0,0.7886751345948128822546}},
464:       b[3]  = {1./6.,1./6.,2./3.},
465:       b2[3] = {1./4.,1./4.,1./2.};
466:     PetscReal binterpt[3][2];

468:     binterpt[0][0]=0.089316397477040902157517886164709;
469:     binterpt[1][0]=-0.91068360252295909784248211383529;
470:     binterpt[2][0]=1.8213672050459181956849642276706;
471:     binterpt[0][1]=0.077350269189625764509148780501957;
472:     binterpt[1][1]=1.077350269189625764509148780502;
473:     binterpt[2][1]=-1.1547005383792515290182975610039;

475:     TSRosWRegister(TSROSWASSP3P3S1C,3,3,&A[0][0],&Gamma[0][0],b,b2,2,&binterpt[0][0]);
476:   }

478:   {
479:     const PetscReal
480:       A[4][4] = {{0,0,0,0},
481:                  {1./2.,0,0,0},
482:                  {1./2.,1./2.,0,0},
483:                  {1./6.,1./6.,1./6.,0}},
484:       Gamma[4][4] = {{1./2.,0,0,0},
485:                      {0.0,1./4.,0,0},
486:                      {-2.,-2./3.,2./3.,0},
487:                      {1./2.,5./36.,-2./9,0}},
488:       b[4]  = {1./6.,1./6.,1./6.,1./2.},
489:       b2[4] = {1./8.,3./4.,1./8.,0};
490:     PetscReal binterpt[4][3];

492:     binterpt[0][0]=6.25;
493:     binterpt[1][0]=-30.25;
494:     binterpt[2][0]=1.75;
495:     binterpt[3][0]=23.25;
496:     binterpt[0][1]=-9.75;
497:     binterpt[1][1]=58.75;
498:     binterpt[2][1]=-3.25;
499:     binterpt[3][1]=-45.75;
500:     binterpt[0][2]=3.6666666666666666666666666666667;
501:     binterpt[1][2]=-28.333333333333333333333333333333;
502:     binterpt[2][2]=1.6666666666666666666666666666667;
503:     binterpt[3][2]=23.;

505:     TSRosWRegister(TSROSWLASSP3P4S2C,3,4,&A[0][0],&Gamma[0][0],b,b2,3,&binterpt[0][0]);
506:   }

508:   {
509:     const PetscReal
510:       A[4][4] = {{0,0,0,0},
511:                  {1./2.,0,0,0},
512:                  {1./2.,1./2.,0,0},
513:                  {1./6.,1./6.,1./6.,0}},
514:       Gamma[4][4] = {{1./2.,0,0,0},
515:                      {0.0,3./4.,0,0},
516:                      {-2./3.,-23./9.,2./9.,0},
517:                      {1./18.,65./108.,-2./27,0}},
518:       b[4]  = {1./6.,1./6.,1./6.,1./2.},
519:       b2[4] = {3./16.,10./16.,3./16.,0};
520:     PetscReal binterpt[4][3];

522:     binterpt[0][0]=1.6911764705882352941176470588235;
523:     binterpt[1][0]=3.6813725490196078431372549019608;
524:     binterpt[2][0]=0.23039215686274509803921568627451;
525:     binterpt[3][0]=-4.6029411764705882352941176470588;
526:     binterpt[0][1]=-0.95588235294117647058823529411765;
527:     binterpt[1][1]=-6.2401960784313725490196078431373;
528:     binterpt[2][1]=-0.31862745098039215686274509803922;
529:     binterpt[3][1]=7.5147058823529411764705882352941;
530:     binterpt[0][2]=-0.56862745098039215686274509803922;
531:     binterpt[1][2]=2.7254901960784313725490196078431;
532:     binterpt[2][2]=0.25490196078431372549019607843137;
533:     binterpt[3][2]=-2.4117647058823529411764705882353;

535:     TSRosWRegister(TSROSWLLSSP3P4S2C,3,4,&A[0][0],&Gamma[0][0],b,b2,3,&binterpt[0][0]);
536:   }

538:   {
539:     PetscReal A[4][4],Gamma[4][4],b[4],b2[4];
540:     PetscReal binterpt[4][3];

542:     Gamma[0][0]=0.4358665215084589994160194475295062513822671686978816;
543:     Gamma[0][1]=0; Gamma[0][2]=0; Gamma[0][3]=0;
544:     Gamma[1][0]=-1.997527830934941248426324674704153457289527280554476;
545:     Gamma[1][1]=0.4358665215084589994160194475295062513822671686978816;
546:     Gamma[1][2]=0; Gamma[1][3]=0;
547:     Gamma[2][0]=-1.007948511795029620852002345345404191008352770119903;
548:     Gamma[2][1]=-0.004648958462629345562774289390054679806993396798458131;
549:     Gamma[2][2]=0.4358665215084589994160194475295062513822671686978816;
550:     Gamma[2][3]=0;
551:     Gamma[3][0]=-0.6685429734233467180451604600279552604364311322650783;
552:     Gamma[3][1]=0.6056625986449338476089525334450053439525178740492984;
553:     Gamma[3][2]=-0.9717899277217721234705114616271378792182450260943198;
554:     Gamma[3][3]=0;

556:     A[0][0]=0; A[0][1]=0; A[0][2]=0; A[0][3]=0;
557:     A[1][0]=0.8717330430169179988320388950590125027645343373957631;
558:     A[1][1]=0; A[1][2]=0; A[1][3]=0;
559:     A[2][0]=0.5275890119763004115618079766722914408876108660811028;
560:     A[2][1]=0.07241098802369958843819203208518599088698057726988732;
561:     A[2][2]=0; A[2][3]=0;
562:     A[3][0]=0.3990960076760701320627260685975778145384666450351314;
563:     A[3][1]=-0.4375576546135194437228463747348862825846903771419953;
564:     A[3][2]=1.038461646937449311660120300601880176655352737312713;
565:     A[3][3]=0;

567:     b[0]=0.1876410243467238251612921333138006734899663569186926;
568:     b[1]=-0.5952974735769549480478230473706443582188442040780541;
569:     b[2]=0.9717899277217721234705114616271378792182450260943198;
570:     b[3]=0.4358665215084589994160194475295062513822671686978816;

572:     b2[0]=0.2147402862233891404862383521089097657790734483804460;
573:     b2[1]=-0.4851622638849390928209050538171743017757490232519684;
574:     b2[2]=0.8687250025203875511662123688667549217531982787600080;
575:     b2[3]=0.4016969751411624011684543450940068201770721128357014;

577:     binterpt[0][0]=2.2565812720167954547104627844105;
578:     binterpt[1][0]=1.349166413351089573796243820819;
579:     binterpt[2][0]=-2.4695174540533503758652847586647;
580:     binterpt[3][0]=-0.13623023131453465264142184656474;
581:     binterpt[0][1]=-3.0826699111559187902922463354557;
582:     binterpt[1][1]=-2.4689115685996042534544925650515;
583:     binterpt[2][1]=5.7428279814696677152129332773553;
584:     binterpt[3][1]=-0.19124650171414467146619437684812;
585:     binterpt[0][2]=1.0137296634858471607430756831148;
586:     binterpt[1][2]=0.52444768167155973161042570784064;
587:     binterpt[2][2]=-2.3015205996945452158771370439586;
588:     binterpt[3][2]=0.76334325453713832352363565300308;

590:     TSRosWRegister(TSROSWARK3,3,4,&A[0][0],&Gamma[0][0],b,b2,3,&binterpt[0][0]);
591:   }
592:   TSRosWRegisterRos4(TSROSWGRK4T,0.231,PETSC_DEFAULT,PETSC_DEFAULT,0,-0.1282612945269037e+01);
593:   TSRosWRegisterRos4(TSROSWSHAMP4,0.5,PETSC_DEFAULT,PETSC_DEFAULT,0,125./108.);
594:   TSRosWRegisterRos4(TSROSWVELDD4,0.22570811482256823492,PETSC_DEFAULT,PETSC_DEFAULT,0,-1.355958941201148);
595:   TSRosWRegisterRos4(TSROSW4L,0.57282,PETSC_DEFAULT,PETSC_DEFAULT,0,-1.093502252409163);
596:   return(0);
597: }



603: /*@C
604:    TSRosWRegisterDestroy - Frees the list of schemes that were registered by TSRosWRegister().

606:    Not Collective

608:    Level: advanced

610: .keywords: TSRosW, register, destroy
611: .seealso: TSRosWRegister(), TSRosWRegisterAll()
612: @*/
613: PetscErrorCode TSRosWRegisterDestroy(void)
614: {
615:   PetscErrorCode  ierr;
616:   RosWTableauLink link;

619:   while ((link = RosWTableauList)) {
620:     RosWTableau t = &link->tab;
621:     RosWTableauList = link->next;
622:     PetscFree5(t->A,t->Gamma,t->b,t->ASum,t->GammaSum);
623:     PetscFree5(t->At,t->bt,t->GammaInv,t->GammaZeroDiag,t->GammaExplicitCorr);
624:     PetscFree2(t->bembed,t->bembedt);
625:     PetscFree(t->binterpt);
626:     PetscFree(t->name);
627:     PetscFree(link);
628:   }
629:   TSRosWRegisterAllCalled = PETSC_FALSE;
630:   return(0);
631: }

635: /*@C
636:   TSRosWInitializePackage - This function initializes everything in the TSRosW package. It is called
637:   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_RosW()
638:   when using static libraries.

640:   Level: developer

642: .keywords: TS, TSRosW, initialize, package
643: .seealso: PetscInitialize()
644: @*/
645: PetscErrorCode TSRosWInitializePackage(void)
646: {

650:   if (TSRosWPackageInitialized) return(0);
651:   TSRosWPackageInitialized = PETSC_TRUE;
652:   TSRosWRegisterAll();
653:   PetscRegisterFinalize(TSRosWFinalizePackage);
654:   return(0);
655: }

659: /*@C
660:   TSRosWFinalizePackage - This function destroys everything in the TSRosW package. It is
661:   called from PetscFinalize().

663:   Level: developer

665: .keywords: Petsc, destroy, package
666: .seealso: PetscFinalize()
667: @*/
668: PetscErrorCode TSRosWFinalizePackage(void)
669: {

673:   TSRosWPackageInitialized = PETSC_FALSE;
674:   TSRosWRegisterDestroy();
675:   return(0);
676: }

680: /*@C
681:    TSRosWRegister - register a Rosenbrock W scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation

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

685:    Input Parameters:
686: +  name - identifier for method
687: .  order - approximation order of method
688: .  s - number of stages, this is the dimension of the matrices below
689: .  A - Table of propagated stage coefficients (dimension s*s, row-major), strictly lower triangular
690: .  Gamma - Table of coefficients in implicit stage equations (dimension s*s, row-major), lower triangular with nonzero diagonal
691: .  b - Step completion table (dimension s)
692: .  bembed - Step completion table for a scheme of order one less (dimension s, NULL if no embedded scheme is available)
693: .  pinterp - Order of the interpolation scheme, equal to the number of columns of binterpt
694: -  binterpt - Coefficients of the interpolation formula (dimension s*pinterp)

696:    Notes:
697:    Several Rosenbrock W methods are provided, this function is only needed to create new methods.

699:    Level: advanced

701: .keywords: TS, register

703: .seealso: TSRosW
704: @*/
705: PetscErrorCode TSRosWRegister(TSRosWType name,PetscInt order,PetscInt s,const PetscReal A[],const PetscReal Gamma[],const PetscReal b[],const PetscReal bembed[],
706:                               PetscInt pinterp,const PetscReal binterpt[])
707: {
708:   PetscErrorCode  ierr;
709:   RosWTableauLink link;
710:   RosWTableau     t;
711:   PetscInt        i,j,k;
712:   PetscScalar     *GammaInv;


721:   PetscCalloc1(1,&link);
722:   t        = &link->tab;
723:   PetscStrallocpy(name,&t->name);
724:   t->order = order;
725:   t->s     = s;
726:   PetscMalloc5(s*s,&t->A,s*s,&t->Gamma,s,&t->b,s,&t->ASum,s,&t->GammaSum);
727:   PetscMalloc5(s*s,&t->At,s,&t->bt,s*s,&t->GammaInv,s,&t->GammaZeroDiag,s*s,&t->GammaExplicitCorr);
728:   PetscMemcpy(t->A,A,s*s*sizeof(A[0]));
729:   PetscMemcpy(t->Gamma,Gamma,s*s*sizeof(Gamma[0]));
730:   PetscMemcpy(t->GammaExplicitCorr,Gamma,s*s*sizeof(Gamma[0]));
731:   PetscMemcpy(t->b,b,s*sizeof(b[0]));
732:   if (bembed) {
733:     PetscMalloc2(s,&t->bembed,s,&t->bembedt);
734:     PetscMemcpy(t->bembed,bembed,s*sizeof(bembed[0]));
735:   }
736:   for (i=0; i<s; i++) {
737:     t->ASum[i]     = 0;
738:     t->GammaSum[i] = 0;
739:     for (j=0; j<s; j++) {
740:       t->ASum[i]     += A[i*s+j];
741:       t->GammaSum[i] += Gamma[i*s+j];
742:     }
743:   }
744:   PetscMalloc1(s*s,&GammaInv); /* Need to use Scalar for inverse, then convert back to Real */
745:   for (i=0; i<s*s; i++) GammaInv[i] = Gamma[i];
746:   for (i=0; i<s; i++) {
747:     if (Gamma[i*s+i] == 0.0) {
748:       GammaInv[i*s+i] = 1.0;
749:       t->GammaZeroDiag[i] = PETSC_TRUE;
750:     } else {
751:       t->GammaZeroDiag[i] = PETSC_FALSE;
752:     }
753:   }

755:   switch (s) {
756:   case 1: GammaInv[0] = 1./GammaInv[0]; break;
757:   case 2: PetscKernel_A_gets_inverse_A_2(GammaInv,0,PETSC_FALSE,NULL); break;
758:   case 3: PetscKernel_A_gets_inverse_A_3(GammaInv,0,PETSC_FALSE,NULL); break;
759:   case 4: PetscKernel_A_gets_inverse_A_4(GammaInv,0,PETSC_FALSE,NULL); break;
760:   case 5: {
761:     PetscInt  ipvt5[5];
762:     MatScalar work5[5*5];
763:     PetscKernel_A_gets_inverse_A_5(GammaInv,ipvt5,work5,0,PETSC_FALSE,NULL); break;
764:   }
765:   case 6: PetscKernel_A_gets_inverse_A_6(GammaInv,0,PETSC_FALSE,NULL); break;
766:   case 7: PetscKernel_A_gets_inverse_A_7(GammaInv,0,PETSC_FALSE,NULL); break;
767:   default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented for %D stages",s);
768:   }
769:   for (i=0; i<s*s; i++) t->GammaInv[i] = PetscRealPart(GammaInv[i]);
770:   PetscFree(GammaInv);

772:   for (i=0; i<s; i++) {
773:     for (k=0; k<i+1; k++) {
774:       t->GammaExplicitCorr[i*s+k]=(t->GammaExplicitCorr[i*s+k])*(t->GammaInv[k*s+k]);
775:       for (j=k+1; j<i+1; j++) {
776:         t->GammaExplicitCorr[i*s+k]+=(t->GammaExplicitCorr[i*s+j])*(t->GammaInv[j*s+k]);
777:       }
778:     }
779:   }

781:   for (i=0; i<s; i++) {
782:     for (j=0; j<s; j++) {
783:       t->At[i*s+j] = 0;
784:       for (k=0; k<s; k++) {
785:         t->At[i*s+j] += t->A[i*s+k] * t->GammaInv[k*s+j];
786:       }
787:     }
788:     t->bt[i] = 0;
789:     for (j=0; j<s; j++) {
790:       t->bt[i] += t->b[j] * t->GammaInv[j*s+i];
791:     }
792:     if (bembed) {
793:       t->bembedt[i] = 0;
794:       for (j=0; j<s; j++) {
795:         t->bembedt[i] += t->bembed[j] * t->GammaInv[j*s+i];
796:       }
797:     }
798:   }
799:   t->ccfl = 1.0;                /* Fix this */

801:   t->pinterp = pinterp;
802:   PetscMalloc1(s*pinterp,&t->binterpt);
803:   PetscMemcpy(t->binterpt,binterpt,s*pinterp*sizeof(binterpt[0]));
804:   link->next = RosWTableauList;
805:   RosWTableauList = link;
806:   return(0);
807: }

811: /*@C
812:    TSRosWRegisterRos4 - register a fourth order Rosenbrock scheme by providing paramter choices

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

816:    Input Parameters:
817: +  name - identifier for method
818: .  gamma - leading coefficient (diagonal entry)
819: .  a2 - design parameter, see Table 7.2 of Hairer&Wanner
820: .  a3 - design parameter or PETSC_DEFAULT to satisfy one of the order five conditions (Eq 7.22)
821: .  b3 - design parameter, see Table 7.2 of Hairer&Wanner
822: .  beta43 - design parameter or PETSC_DEFAULT to use Equation 7.21 of Hairer&Wanner
823: .  e4 - design parameter for embedded method, see coefficient E4 in ros4.f code from Hairer

825:    Notes:
826:    This routine encodes the design of fourth order Rosenbrock methods as described in Hairer and Wanner volume 2.
827:    It is used here to implement several methods from the book and can be used to experiment with new methods.
828:    It was written this way instead of by copying coefficients in order to provide better than double precision satisfaction of the order conditions.

830:    Level: developer

832: .keywords: TS, register

834: .seealso: TSRosW, TSRosWRegister()
835: @*/
836: PetscErrorCode TSRosWRegisterRos4(TSRosWType name,PetscReal gamma,PetscReal a2,PetscReal a3,PetscReal b3,PetscReal e4)
837: {
839:   /* Declare numeric constants so they can be quad precision without being truncated at double */
840:   const PetscReal one = 1,two = 2,three = 3,four = 4,five = 5,six = 6,eight = 8,twelve = 12,twenty = 20,twentyfour = 24,
841:     p32 = one/six - gamma + gamma*gamma,
842:     p42 = one/eight - gamma/three,
843:     p43 = one/twelve - gamma/three,
844:     p44 = one/twentyfour - gamma/two + three/two*gamma*gamma - gamma*gamma*gamma,
845:     p56 = one/twenty - gamma/four;
846:   PetscReal   a4,a32,a42,a43,b1,b2,b4,beta2p,beta3p,beta4p,beta32,beta42,beta43,beta32beta2p,beta4jbetajp;
847:   PetscReal   A[4][4],Gamma[4][4],b[4],bm[4];
848:   PetscScalar M[3][3],rhs[3];

851:   /* Step 1: choose Gamma (input) */
852:   /* Step 2: choose a2,a3,a4; b1,b2,b3,b4 to satisfy order conditions */
853:   if (a3 == PETSC_DEFAULT) a3 = (one/five - a2/four)/(one/four - a2/three); /* Eq 7.22 */
854:   a4 = a3;                                                  /* consequence of 7.20 */

856:   /* Solve order conditions 7.15a, 7.15c, 7.15e */
857:   M[0][0] = one; M[0][1] = one;      M[0][2] = one;      /* 7.15a */
858:   M[1][0] = 0.0; M[1][1] = a2*a2;    M[1][2] = a4*a4;    /* 7.15c */
859:   M[2][0] = 0.0; M[2][1] = a2*a2*a2; M[2][2] = a4*a4*a4; /* 7.15e */
860:   rhs[0]  = one - b3;
861:   rhs[1]  = one/three - a3*a3*b3;
862:   rhs[2]  = one/four - a3*a3*a3*b3;
863:   PetscKernel_A_gets_inverse_A_3(&M[0][0],0,PETSC_FALSE,NULL);
864:   b1      = PetscRealPart(M[0][0]*rhs[0] + M[0][1]*rhs[1] + M[0][2]*rhs[2]);
865:   b2      = PetscRealPart(M[1][0]*rhs[0] + M[1][1]*rhs[1] + M[1][2]*rhs[2]);
866:   b4      = PetscRealPart(M[2][0]*rhs[0] + M[2][1]*rhs[1] + M[2][2]*rhs[2]);

868:   /* Step 3 */
869:   beta43       = (p56 - a2*p43) / (b4*a3*a3*(a3 - a2)); /* 7.21 */
870:   beta32beta2p =  p44 / (b4*beta43);                    /* 7.15h */
871:   beta4jbetajp = (p32 - b3*beta32beta2p) / b4;
872:   M[0][0]      = b2;                                    M[0][1] = b3;                 M[0][2] = b4;
873:   M[1][0]      = a4*a4*beta32beta2p-a3*a3*beta4jbetajp; M[1][1] = a2*a2*beta4jbetajp; M[1][2] = -a2*a2*beta32beta2p;
874:   M[2][0]      = b4*beta43*a3*a3-p43;                   M[2][1] = -b4*beta43*a2*a2;   M[2][2] = 0;
875:   rhs[0]       = one/two - gamma; rhs[1] = 0; rhs[2] = -a2*a2*p32;
876:   PetscKernel_A_gets_inverse_A_3(&M[0][0],0,PETSC_FALSE,NULL);
877:   beta2p       = PetscRealPart(M[0][0]*rhs[0] + M[0][1]*rhs[1] + M[0][2]*rhs[2]);
878:   beta3p       = PetscRealPart(M[1][0]*rhs[0] + M[1][1]*rhs[1] + M[1][2]*rhs[2]);
879:   beta4p       = PetscRealPart(M[2][0]*rhs[0] + M[2][1]*rhs[1] + M[2][2]*rhs[2]);

881:   /* Step 4: back-substitute */
882:   beta32 = beta32beta2p / beta2p;
883:   beta42 = (beta4jbetajp - beta43*beta3p) / beta2p;

885:   /* Step 5: 7.15f and 7.20, then 7.16 */
886:   a43 = 0;
887:   a32 = p42 / (b3*a3*beta2p + b4*a4*beta2p);
888:   a42 = a32;

890:   A[0][0]     = 0;          A[0][1] = 0;   A[0][2] = 0;   A[0][3] = 0;
891:   A[1][0]     = a2;         A[1][1] = 0;   A[1][2] = 0;   A[1][3] = 0;
892:   A[2][0]     = a3-a32;     A[2][1] = a32; A[2][2] = 0;   A[2][3] = 0;
893:   A[3][0]     = a4-a43-a42; A[3][1] = a42; A[3][2] = a43; A[3][3] = 0;
894:   Gamma[0][0] = gamma;                        Gamma[0][1] = 0;              Gamma[0][2] = 0;              Gamma[0][3] = 0;
895:   Gamma[1][0] = beta2p-A[1][0];               Gamma[1][1] = gamma;          Gamma[1][2] = 0;              Gamma[1][3] = 0;
896:   Gamma[2][0] = beta3p-beta32-A[2][0];        Gamma[2][1] = beta32-A[2][1]; Gamma[2][2] = gamma;          Gamma[2][3] = 0;
897:   Gamma[3][0] = beta4p-beta42-beta43-A[3][0]; Gamma[3][1] = beta42-A[3][1]; Gamma[3][2] = beta43-A[3][2]; Gamma[3][3] = gamma;
898:   b[0] = b1; b[1] = b2; b[2] = b3; b[3] = b4;

900:   /* Construct embedded formula using given e4. We are solving Equation 7.18. */
901:   bm[3] = b[3] - e4*gamma;                                          /* using definition of E4 */
902:   bm[2] = (p32 - beta4jbetajp*bm[3]) / (beta32*beta2p);             /* fourth row of 7.18 */
903:   bm[1] = (one/two - gamma - beta3p*bm[2] - beta4p*bm[3]) / beta2p; /* second row */
904:   bm[0] = one - bm[1] - bm[2] - bm[3];                              /* first row */

906:   {
907:     const PetscReal misfit = a2*a2*bm[1] + a3*a3*bm[2] + a4*a4*bm[3] - one/three;
908:     if (PetscAbs(misfit) > PETSC_SMALL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Assumptions violated, could not construct a third order embedded method");
909:   }
910:   TSRosWRegister(name,4,4,&A[0][0],&Gamma[0][0],b,bm,0,NULL);
911:   return(0);
912: }

916: /*
917:  The step completion formula is

919:  x1 = x0 + b^T Y

921:  where Y is the multi-vector of stages corrections. This function can be called before or after ts->vec_sol has been
922:  updated. Suppose we have a completion formula b and an embedded formula be of different order. We can write

924:  x1e = x0 + be^T Y
925:      = x1 - b^T Y + be^T Y
926:      = x1 + (be - b)^T Y

928:  so we can evaluate the method of different order even after the step has been optimistically completed.
929: */
930: static PetscErrorCode TSEvaluateStep_RosW(TS ts,PetscInt order,Vec U,PetscBool *done)
931: {
932:   TS_RosW        *ros = (TS_RosW*)ts->data;
933:   RosWTableau    tab  = ros->tableau;
934:   PetscScalar    *w   = ros->work;
935:   PetscInt       i;

939:   if (order == tab->order) {
940:     if (ros->status == TS_STEP_INCOMPLETE) { /* Use standard completion formula */
941:       VecCopy(ts->vec_sol,U);
942:       for (i=0; i<tab->s; i++) w[i] = tab->bt[i];
943:       VecMAXPY(U,tab->s,w,ros->Y);
944:     } else {VecCopy(ts->vec_sol,U);}
945:     if (done) *done = PETSC_TRUE;
946:     return(0);
947:   } else if (order == tab->order-1) {
948:     if (!tab->bembedt) goto unavailable;
949:     if (ros->status == TS_STEP_INCOMPLETE) { /* Use embedded completion formula */
950:       VecCopy(ts->vec_sol,U);
951:       for (i=0; i<tab->s; i++) w[i] = tab->bembedt[i];
952:       VecMAXPY(U,tab->s,w,ros->Y);
953:     } else {                    /* Use rollback-and-recomplete formula (bembedt - bt) */
954:       for (i=0; i<tab->s; i++) w[i] = tab->bembedt[i] - tab->bt[i];
955:       VecCopy(ts->vec_sol,U);
956:       VecMAXPY(U,tab->s,w,ros->Y);
957:     }
958:     if (done) *done = PETSC_TRUE;
959:     return(0);
960:   }
961:   unavailable:
962:   if (done) *done = PETSC_FALSE;
963:   else SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Rosenbrock-W '%s' of order %D cannot evaluate step at order %D. Consider using -ts_adapt_type none or a different method that has an embedded estimate.",tab->name,tab->order,order);
964:   return(0);
965: }

969: static PetscErrorCode TSRollBack_RosW(TS ts)
970: {
971:   TS_RosW        *ros = (TS_RosW*)ts->data;

975:   VecCopy(ros->vec_sol_prev,ts->vec_sol);
976:   return(0);
977: }

981: static PetscErrorCode TSStep_RosW(TS ts)
982: {
983:   TS_RosW         *ros = (TS_RosW*)ts->data;
984:   RosWTableau     tab  = ros->tableau;
985:   const PetscInt  s    = tab->s;
986:   const PetscReal *At  = tab->At,*Gamma = tab->Gamma,*ASum = tab->ASum,*GammaInv = tab->GammaInv;
987:   const PetscReal *GammaExplicitCorr = tab->GammaExplicitCorr;
988:   const PetscBool *GammaZeroDiag = tab->GammaZeroDiag;
989:   PetscScalar     *w   = ros->work;
990:   Vec             *Y   = ros->Y,Ydot = ros->Ydot,Zdot = ros->Zdot,Zstage = ros->Zstage;
991:   SNES            snes;
992:   TSAdapt         adapt;
993:   PetscInt        i,j,its,lits;
994:   PetscInt        rejections = 0;
995:   PetscBool       stageok,accept = PETSC_TRUE;
996:   PetscReal       next_time_step = ts->time_step;
997:   PetscErrorCode  ierr;

1000:   if (!ts->steprollback) {
1001:     VecCopy(ts->vec_sol,ros->vec_sol_prev);
1002:   }

1004:   ros->status = TS_STEP_INCOMPLETE;
1005:   while (!ts->reason && ros->status != TS_STEP_COMPLETE) {
1006:     const PetscReal h = ts->time_step;
1007:     for (i=0; i<s; i++) {
1008:       ros->stage_time = ts->ptime + h*ASum[i];
1009:       TSPreStage(ts,ros->stage_time);
1010:       if (GammaZeroDiag[i]) {
1011:         ros->stage_explicit = PETSC_TRUE;
1012:         ros->scoeff         = 1.;
1013:       } else {
1014:         ros->stage_explicit = PETSC_FALSE;
1015:         ros->scoeff         = 1./Gamma[i*s+i];
1016:       }

1018:       VecCopy(ts->vec_sol,Zstage);
1019:       for (j=0; j<i; j++) w[j] = At[i*s+j];
1020:       VecMAXPY(Zstage,i,w,Y);

1022:       for (j=0; j<i; j++) w[j] = 1./h * GammaInv[i*s+j];
1023:       VecZeroEntries(Zdot);
1024:       VecMAXPY(Zdot,i,w,Y);

1026:       /* Initial guess taken from last stage */
1027:       VecZeroEntries(Y[i]);

1029:       if (!ros->stage_explicit) {
1030:         TSGetSNES(ts,&snes);
1031:         if (!ros->recompute_jacobian && !i) {
1032:           SNESSetLagJacobian(snes,-2); /* Recompute the Jacobian on this solve, but not again */
1033:         }
1034:         SNESSolve(snes,NULL,Y[i]);
1035:         SNESGetIterationNumber(snes,&its);
1036:         SNESGetLinearSolveIterations(snes,&lits);
1037:         ts->snes_its += its; ts->ksp_its += lits;
1038:       } else {
1039:         Mat J,Jp;
1040:         VecZeroEntries(Ydot); /* Evaluate Y[i]=G(t,Ydot=0,Zstage) */
1041:         TSComputeIFunction(ts,ros->stage_time,Zstage,Ydot,Y[i],PETSC_FALSE);
1042:         VecScale(Y[i],-1.0);
1043:         VecAXPY(Y[i],-1.0,Zdot); /*Y[i] = F(Zstage)-Zdot[=GammaInv*Y]*/

1045:         VecZeroEntries(Zstage); /* Zstage = GammaExplicitCorr[i,j] * Y[j] */
1046:         for (j=0; j<i; j++) w[j] = GammaExplicitCorr[i*s+j];
1047:         VecMAXPY(Zstage,i,w,Y);

1049:         /* Y[i] = Y[i] + Jac*Zstage[=Jac*GammaExplicitCorr[i,j] * Y[j]] */
1050:         TSGetIJacobian(ts,&J,&Jp,NULL,NULL);
1051:         TSComputeIJacobian(ts,ros->stage_time,ts->vec_sol,Ydot,0,J,Jp,PETSC_FALSE);
1052:         MatMult(J,Zstage,Zdot);
1053:         VecAXPY(Y[i],-1.0,Zdot);
1054:         ts->ksp_its += 1;

1056:         VecScale(Y[i],h);
1057:       }
1058:       TSPostStage(ts,ros->stage_time,i,Y);
1059:       TSGetAdapt(ts,&adapt);
1060:       TSAdaptCheckStage(adapt,ts,ros->stage_time,Y[i],&stageok);
1061:       if (!stageok) goto reject_step;
1062:     }

1064:     ros->status = TS_STEP_INCOMPLETE;
1065:     TSEvaluateStep_RosW(ts,tab->order,ts->vec_sol,NULL);
1066:     ros->status = TS_STEP_PENDING;
1067:     TSGetAdapt(ts,&adapt);
1068:     TSAdaptCandidatesClear(adapt);
1069:     TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,1.*tab->s,PETSC_TRUE);
1070:     TSAdaptChoose(adapt,ts,ts->time_step,NULL,&next_time_step,&accept);
1071:     ros->status = accept ? TS_STEP_COMPLETE : TS_STEP_INCOMPLETE;
1072:     if (!accept) { /* Roll back the current step */
1073:       TSRollBack_RosW(ts);
1074:       ts->time_step = next_time_step;
1075:       goto reject_step;
1076:     }

1078:     ts->ptime += ts->time_step;
1079:     ts->time_step = next_time_step;
1080:     break;

1082:   reject_step:
1083:     ts->reject++; accept = PETSC_FALSE;
1084:     if (!ts->reason && ++rejections > ts->max_reject && ts->max_reject >= 0) {
1085:       ts->reason = TS_DIVERGED_STEP_REJECTED;
1086:       PetscInfo2(ts,"Step=%D, step rejections %D greater than current TS allowed, stopping solve\n",ts->steps,rejections);
1087:     }
1088:   }
1089:   return(0);
1090: }

1094: static PetscErrorCode TSInterpolate_RosW(TS ts,PetscReal itime,Vec U)
1095: {
1096:   TS_RosW         *ros = (TS_RosW*)ts->data;
1097:   PetscInt        s    = ros->tableau->s,pinterp = ros->tableau->pinterp,i,j;
1098:   PetscReal       h;
1099:   PetscReal       tt,t;
1100:   PetscScalar     *bt;
1101:   const PetscReal *Bt = ros->tableau->binterpt;
1102:   PetscErrorCode  ierr;
1103:   const PetscReal *GammaInv = ros->tableau->GammaInv;
1104:   PetscScalar     *w        = ros->work;
1105:   Vec             *Y        = ros->Y;

1108:   if (!Bt) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRosW %s does not have an interpolation formula",ros->tableau->name);

1110:   switch (ros->status) {
1111:   case TS_STEP_INCOMPLETE:
1112:   case TS_STEP_PENDING:
1113:     h = ts->time_step;
1114:     t = (itime - ts->ptime)/h;
1115:     break;
1116:   case TS_STEP_COMPLETE:
1117:     h = ts->ptime - ts->ptime_prev;
1118:     t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
1119:     break;
1120:   default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
1121:   }
1122:   PetscMalloc1(s,&bt);
1123:   for (i=0; i<s; i++) bt[i] = 0;
1124:   for (j=0,tt=t; j<pinterp; j++,tt*=t) {
1125:     for (i=0; i<s; i++) {
1126:       bt[i] += Bt[i*pinterp+j] * tt;
1127:     }
1128:   }

1130:   /* y(t+tt*h) = y(t) + Sum bt(tt) * GammaInv * Ydot */
1131:   /* U <- 0*/
1132:   VecZeroEntries(U);
1133:   /* U <- Sum bt_i * GammaInv(i,1:i) * Y(1:i) */
1134:   for (j=0; j<s; j++) w[j] = 0;
1135:   for (j=0; j<s; j++) {
1136:     for (i=j; i<s; i++) {
1137:       w[j] +=  bt[i]*GammaInv[i*s+j];
1138:     }
1139:   }
1140:   VecMAXPY(U,i,w,Y);
1141:   /* U <- y(t) + U */
1142:   VecAXPY(U,1,ros->vec_sol_prev);

1144:   PetscFree(bt);
1145:   return(0);
1146: }

1148: /*------------------------------------------------------------*/

1152: static PetscErrorCode TSRosWTableauReset(TS ts)
1153: {
1154:   TS_RosW        *ros = (TS_RosW*)ts->data;
1155:   RosWTableau    tab  = ros->tableau;

1159:   if (!tab) return(0);
1160:   VecDestroyVecs(tab->s,&ros->Y);
1161:   PetscFree(ros->work);
1162:   return(0);
1163: }

1167: static PetscErrorCode TSReset_RosW(TS ts)
1168: {
1169:   TS_RosW        *ros = (TS_RosW*)ts->data;

1173:   TSRosWTableauReset(ts);
1174:   VecDestroy(&ros->Ydot);
1175:   VecDestroy(&ros->Ystage);
1176:   VecDestroy(&ros->Zdot);
1177:   VecDestroy(&ros->Zstage);
1178:   VecDestroy(&ros->vec_sol_prev);
1179:   return(0);
1180: }

1184: static PetscErrorCode TSDestroy_RosW(TS ts)
1185: {

1189:   TSReset_RosW(ts);
1190:   PetscFree(ts->data);
1191:   PetscObjectComposeFunction((PetscObject)ts,"TSRosWGetType_C",NULL);
1192:   PetscObjectComposeFunction((PetscObject)ts,"TSRosWSetType_C",NULL);
1193:   PetscObjectComposeFunction((PetscObject)ts,"TSRosWSetRecomputeJacobian_C",NULL);
1194:   return(0);
1195: }


1200: static PetscErrorCode TSRosWGetVecs(TS ts,DM dm,Vec *Ydot,Vec *Zdot,Vec *Ystage,Vec *Zstage)
1201: {
1202:   TS_RosW        *rw = (TS_RosW*)ts->data;

1206:   if (Ydot) {
1207:     if (dm && dm != ts->dm) {
1208:       DMGetNamedGlobalVector(dm,"TSRosW_Ydot",Ydot);
1209:     } else *Ydot = rw->Ydot;
1210:   }
1211:   if (Zdot) {
1212:     if (dm && dm != ts->dm) {
1213:       DMGetNamedGlobalVector(dm,"TSRosW_Zdot",Zdot);
1214:     } else *Zdot = rw->Zdot;
1215:   }
1216:   if (Ystage) {
1217:     if (dm && dm != ts->dm) {
1218:       DMGetNamedGlobalVector(dm,"TSRosW_Ystage",Ystage);
1219:     } else *Ystage = rw->Ystage;
1220:   }
1221:   if (Zstage) {
1222:     if (dm && dm != ts->dm) {
1223:       DMGetNamedGlobalVector(dm,"TSRosW_Zstage",Zstage);
1224:     } else *Zstage = rw->Zstage;
1225:   }
1226:   return(0);
1227: }


1232: static PetscErrorCode TSRosWRestoreVecs(TS ts,DM dm,Vec *Ydot,Vec *Zdot, Vec *Ystage, Vec *Zstage)
1233: {

1237:   if (Ydot) {
1238:     if (dm && dm != ts->dm) {
1239:       DMRestoreNamedGlobalVector(dm,"TSRosW_Ydot",Ydot);
1240:     }
1241:   }
1242:   if (Zdot) {
1243:     if (dm && dm != ts->dm) {
1244:       DMRestoreNamedGlobalVector(dm,"TSRosW_Zdot",Zdot);
1245:     }
1246:   }
1247:   if (Ystage) {
1248:     if (dm && dm != ts->dm) {
1249:       DMRestoreNamedGlobalVector(dm,"TSRosW_Ystage",Ystage);
1250:     }
1251:   }
1252:   if (Zstage) {
1253:     if (dm && dm != ts->dm) {
1254:       DMRestoreNamedGlobalVector(dm,"TSRosW_Zstage",Zstage);
1255:     }
1256:   }
1257:   return(0);
1258: }

1262: static PetscErrorCode DMCoarsenHook_TSRosW(DM fine,DM coarse,void *ctx)
1263: {
1265:   return(0);
1266: }

1270: static PetscErrorCode DMRestrictHook_TSRosW(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
1271: {
1272:   TS             ts = (TS)ctx;
1274:   Vec            Ydot,Zdot,Ystage,Zstage;
1275:   Vec            Ydotc,Zdotc,Ystagec,Zstagec;

1278:   TSRosWGetVecs(ts,fine,&Ydot,&Ystage,&Zdot,&Zstage);
1279:   TSRosWGetVecs(ts,coarse,&Ydotc,&Ystagec,&Zdotc,&Zstagec);
1280:   MatRestrict(restrct,Ydot,Ydotc);
1281:   VecPointwiseMult(Ydotc,rscale,Ydotc);
1282:   MatRestrict(restrct,Ystage,Ystagec);
1283:   VecPointwiseMult(Ystagec,rscale,Ystagec);
1284:   MatRestrict(restrct,Zdot,Zdotc);
1285:   VecPointwiseMult(Zdotc,rscale,Zdotc);
1286:   MatRestrict(restrct,Zstage,Zstagec);
1287:   VecPointwiseMult(Zstagec,rscale,Zstagec);
1288:   TSRosWRestoreVecs(ts,fine,&Ydot,&Ystage,&Zdot,&Zstage);
1289:   TSRosWRestoreVecs(ts,coarse,&Ydotc,&Ystagec,&Zdotc,&Zstagec);
1290:   return(0);
1291: }


1296: static PetscErrorCode DMSubDomainHook_TSRosW(DM fine,DM coarse,void *ctx)
1297: {
1299:   return(0);
1300: }

1304: static PetscErrorCode DMSubDomainRestrictHook_TSRosW(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
1305: {
1306:   TS             ts = (TS)ctx;
1308:   Vec            Ydot,Zdot,Ystage,Zstage;
1309:   Vec            Ydots,Zdots,Ystages,Zstages;

1312:   TSRosWGetVecs(ts,dm,&Ydot,&Ystage,&Zdot,&Zstage);
1313:   TSRosWGetVecs(ts,subdm,&Ydots,&Ystages,&Zdots,&Zstages);

1315:   VecScatterBegin(gscat,Ydot,Ydots,INSERT_VALUES,SCATTER_FORWARD);
1316:   VecScatterEnd(gscat,Ydot,Ydots,INSERT_VALUES,SCATTER_FORWARD);

1318:   VecScatterBegin(gscat,Ystage,Ystages,INSERT_VALUES,SCATTER_FORWARD);
1319:   VecScatterEnd(gscat,Ystage,Ystages,INSERT_VALUES,SCATTER_FORWARD);

1321:   VecScatterBegin(gscat,Zdot,Zdots,INSERT_VALUES,SCATTER_FORWARD);
1322:   VecScatterEnd(gscat,Zdot,Zdots,INSERT_VALUES,SCATTER_FORWARD);

1324:   VecScatterBegin(gscat,Zstage,Zstages,INSERT_VALUES,SCATTER_FORWARD);
1325:   VecScatterEnd(gscat,Zstage,Zstages,INSERT_VALUES,SCATTER_FORWARD);

1327:   TSRosWRestoreVecs(ts,dm,&Ydot,&Ystage,&Zdot,&Zstage);
1328:   TSRosWRestoreVecs(ts,subdm,&Ydots,&Ystages,&Zdots,&Zstages);
1329:   return(0);
1330: }

1332: /*
1333:   This defines the nonlinear equation that is to be solved with SNES
1334:   G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
1335: */
1338: static PetscErrorCode SNESTSFormFunction_RosW(SNES snes,Vec U,Vec F,TS ts)
1339: {
1340:   TS_RosW        *ros = (TS_RosW*)ts->data;
1342:   Vec            Ydot,Zdot,Ystage,Zstage;
1343:   PetscReal      shift = ros->scoeff / ts->time_step;
1344:   DM             dm,dmsave;

1347:   SNESGetDM(snes,&dm);
1348:   TSRosWGetVecs(ts,dm,&Ydot,&Zdot,&Ystage,&Zstage);
1349:   VecWAXPY(Ydot,shift,U,Zdot);    /* Ydot = shift*U + Zdot */
1350:   VecWAXPY(Ystage,1.0,U,Zstage);  /* Ystage = U + Zstage */
1351:   dmsave = ts->dm;
1352:   ts->dm = dm;
1353:   TSComputeIFunction(ts,ros->stage_time,Ystage,Ydot,F,PETSC_FALSE);
1354:   ts->dm = dmsave;
1355:   TSRosWRestoreVecs(ts,dm,&Ydot,&Zdot,&Ystage,&Zstage);
1356:   return(0);
1357: }

1361: static PetscErrorCode SNESTSFormJacobian_RosW(SNES snes,Vec U,Mat A,Mat B,TS ts)
1362: {
1363:   TS_RosW        *ros = (TS_RosW*)ts->data;
1364:   Vec            Ydot,Zdot,Ystage,Zstage;
1365:   PetscReal      shift = ros->scoeff / ts->time_step;
1367:   DM             dm,dmsave;

1370:   /* ros->Ydot and ros->Ystage have already been computed in SNESTSFormFunction_RosW (SNES guarantees this) */
1371:   SNESGetDM(snes,&dm);
1372:   TSRosWGetVecs(ts,dm,&Ydot,&Zdot,&Ystage,&Zstage);
1373:   dmsave = ts->dm;
1374:   ts->dm = dm;
1375:   TSComputeIJacobian(ts,ros->stage_time,Ystage,Ydot,shift,A,B,PETSC_TRUE);
1376:   ts->dm = dmsave;
1377:   TSRosWRestoreVecs(ts,dm,&Ydot,&Zdot,&Ystage,&Zstage);
1378:   return(0);
1379: }

1383: static PetscErrorCode TSRosWTableauSetUp(TS ts)
1384: {
1385:   TS_RosW        *ros = (TS_RosW*)ts->data;
1386:   RosWTableau    tab  = ros->tableau;

1390:   VecDuplicateVecs(ts->vec_sol,tab->s,&ros->Y);
1391:   PetscMalloc1(tab->s,&ros->work);
1392:   return(0);
1393: }

1397: static PetscErrorCode TSSetUp_RosW(TS ts)
1398: {
1399:   TS_RosW        *ros = (TS_RosW*)ts->data;
1401:   DM             dm;
1402:   SNES           snes;

1405:   TSRosWTableauSetUp(ts);
1406:   VecDuplicate(ts->vec_sol,&ros->Ydot);
1407:   VecDuplicate(ts->vec_sol,&ros->Ystage);
1408:   VecDuplicate(ts->vec_sol,&ros->Zdot);
1409:   VecDuplicate(ts->vec_sol,&ros->Zstage);
1410:   VecDuplicate(ts->vec_sol,&ros->vec_sol_prev);
1411:   TSGetDM(ts,&dm);
1412:   if (dm) {
1413:     DMCoarsenHookAdd(dm,DMCoarsenHook_TSRosW,DMRestrictHook_TSRosW,ts);
1414:     DMSubDomainHookAdd(dm,DMSubDomainHook_TSRosW,DMSubDomainRestrictHook_TSRosW,ts);
1415:   }
1416:   /* Rosenbrock methods are linearly implicit, so set that unless the user has specifically asked for something else */
1417:   TSGetSNES(ts,&snes);
1418:   if (!((PetscObject)snes)->type_name) {
1419:     SNESSetType(snes,SNESKSPONLY);
1420:   }
1421:   return(0);
1422: }
1423: /*------------------------------------------------------------*/

1427: static PetscErrorCode TSSetFromOptions_RosW(PetscOptionItems *PetscOptionsObject,TS ts)
1428: {
1429:   TS_RosW        *ros = (TS_RosW*)ts->data;
1431:   SNES           snes;

1434:   PetscOptionsHead(PetscOptionsObject,"RosW ODE solver options");
1435:   {
1436:     RosWTableauLink link;
1437:     PetscInt        count,choice;
1438:     PetscBool       flg;
1439:     const char      **namelist;

1441:     for (link=RosWTableauList,count=0; link; link=link->next,count++) ;
1442:     PetscMalloc1(count,&namelist);
1443:     for (link=RosWTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
1444:     PetscOptionsEList("-ts_rosw_type","Family of Rosenbrock-W method","TSRosWSetType",(const char*const*)namelist,count,ros->tableau->name,&choice,&flg);
1445:     if (flg) {TSRosWSetType(ts,namelist[choice]);}
1446:     PetscFree(namelist);

1448:     PetscOptionsBool("-ts_rosw_recompute_jacobian","Recompute the Jacobian at each stage","TSRosWSetRecomputeJacobian",ros->recompute_jacobian,&ros->recompute_jacobian,NULL);
1449:   }
1450:   PetscOptionsTail();
1451:   /* Rosenbrock methods are linearly implicit, so set that unless the user has specifically asked for something else */
1452:   TSGetSNES(ts,&snes);
1453:   if (!((PetscObject)snes)->type_name) {
1454:     SNESSetType(snes,SNESKSPONLY);
1455:   }
1456:   return(0);
1457: }

1461: static PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[])
1462: {
1464:   PetscInt       i;
1465:   size_t         left,count;
1466:   char           *p;

1469:   for (i=0,p=buf,left=len; i<n; i++) {
1470:     PetscSNPrintfCount(p,left,fmt,&count,x[i]);
1471:     if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer");
1472:     left -= count;
1473:     p    += count;
1474:     *p++  = ' ';
1475:   }
1476:   p[i ? 0 : -1] = 0;
1477:   return(0);
1478: }

1482: static PetscErrorCode TSView_RosW(TS ts,PetscViewer viewer)
1483: {
1484:   TS_RosW        *ros = (TS_RosW*)ts->data;
1485:   PetscBool      iascii;

1489:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1490:   if (iascii) {
1491:     RosWTableau tab  = ros->tableau;
1492:     TSRosWType  rostype;
1493:     char        buf[512];
1494:     PetscInt    i;
1495:     PetscReal   abscissa[512];
1496:     TSRosWGetType(ts,&rostype);
1497:     PetscViewerASCIIPrintf(viewer,"  Rosenbrock-W %s\n",rostype);
1498:     PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->ASum);
1499:     PetscViewerASCIIPrintf(viewer,"  Abscissa of A       = %s\n",buf);
1500:     for (i=0; i<tab->s; i++) abscissa[i] = tab->ASum[i] + tab->Gamma[i];
1501:     PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,abscissa);
1502:     PetscViewerASCIIPrintf(viewer,"  Abscissa of A+Gamma = %s\n",buf);
1503:   }
1504:   if (ts->adapt) {TSAdaptView(ts->adapt,viewer);}
1505:   if (ts->snes)  {SNESView(ts->snes,viewer);}
1506:   return(0);
1507: }

1511: static PetscErrorCode TSLoad_RosW(TS ts,PetscViewer viewer)
1512: {
1514:   SNES           snes;
1515:   TSAdapt        adapt;

1518:   TSGetAdapt(ts,&adapt);
1519:   TSAdaptLoad(adapt,viewer);
1520:   TSGetSNES(ts,&snes);
1521:   SNESLoad(snes,viewer);
1522:   /* function and Jacobian context for SNES when used with TS is always ts object */
1523:   SNESSetFunction(snes,NULL,NULL,ts);
1524:   SNESSetJacobian(snes,NULL,NULL,NULL,ts);
1525:   return(0);
1526: }

1530: /*@C
1531:   TSRosWSetType - Set the type of Rosenbrock-W scheme

1533:   Logically collective

1535:   Input Parameter:
1536: +  ts - timestepping context
1537: -  rostype - type of Rosenbrock-W scheme

1539:   Level: beginner

1541: .seealso: TSRosWGetType(), TSROSW, TSROSW2M, TSROSW2P, TSROSWRA3PW, TSROSWRA34PW2, TSROSWRODAS3, TSROSWSANDU3, TSROSWASSP3P3S1C, TSROSWLASSP3P4S2C, TSROSWLLSSP3P4S2C, TSROSWARK3
1542: @*/
1543: PetscErrorCode TSRosWSetType(TS ts,TSRosWType rostype)
1544: {

1549:   PetscTryMethod(ts,"TSRosWSetType_C",(TS,TSRosWType),(ts,rostype));
1550:   return(0);
1551: }

1555: /*@C
1556:   TSRosWGetType - Get the type of Rosenbrock-W scheme

1558:   Logically collective

1560:   Input Parameter:
1561: .  ts - timestepping context

1563:   Output Parameter:
1564: .  rostype - type of Rosenbrock-W scheme

1566:   Level: intermediate

1568: .seealso: TSRosWGetType()
1569: @*/
1570: PetscErrorCode TSRosWGetType(TS ts,TSRosWType *rostype)
1571: {

1576:   PetscUseMethod(ts,"TSRosWGetType_C",(TS,TSRosWType*),(ts,rostype));
1577:   return(0);
1578: }

1582: /*@C
1583:   TSRosWSetRecomputeJacobian - Set whether to recompute the Jacobian at each stage. The default is to update the Jacobian once per step.

1585:   Logically collective

1587:   Input Parameter:
1588: +  ts - timestepping context
1589: -  flg - PETSC_TRUE to recompute the Jacobian at each stage

1591:   Level: intermediate

1593: .seealso: TSRosWGetType()
1594: @*/
1595: PetscErrorCode TSRosWSetRecomputeJacobian(TS ts,PetscBool flg)
1596: {

1601:   PetscTryMethod(ts,"TSRosWSetRecomputeJacobian_C",(TS,PetscBool),(ts,flg));
1602:   return(0);
1603: }

1607: static PetscErrorCode  TSRosWGetType_RosW(TS ts,TSRosWType *rostype)
1608: {
1609:   TS_RosW        *ros = (TS_RosW*)ts->data;

1612:   *rostype = ros->tableau->name;
1613:   return(0);
1614: }

1618: static PetscErrorCode  TSRosWSetType_RosW(TS ts,TSRosWType rostype)
1619: {
1620:   TS_RosW         *ros = (TS_RosW*)ts->data;
1621:   PetscErrorCode  ierr;
1622:   PetscBool       match;
1623:   RosWTableauLink link;

1626:   if (ros->tableau) {
1627:     PetscStrcmp(ros->tableau->name,rostype,&match);
1628:     if (match) return(0);
1629:   }
1630:   for (link = RosWTableauList; link; link=link->next) {
1631:     PetscStrcmp(link->tab.name,rostype,&match);
1632:     if (match) {
1633:       if (ts->setupcalled) {TSRosWTableauReset(ts);}
1634:       ros->tableau = &link->tab;
1635:       if (ts->setupcalled) {TSRosWTableauSetUp(ts);}
1636:       return(0);
1637:     }
1638:   }
1639:   SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",rostype);
1640:   return(0);
1641: }

1645: static PetscErrorCode  TSRosWSetRecomputeJacobian_RosW(TS ts,PetscBool flg)
1646: {
1647:   TS_RosW *ros = (TS_RosW*)ts->data;

1650:   ros->recompute_jacobian = flg;
1651:   return(0);
1652: }


1655: /* ------------------------------------------------------------ */
1656: /*MC
1657:       TSROSW - ODE solver using Rosenbrock-W schemes

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

1663:   Notes:
1664:   This method currently only works with autonomous ODE and DAE.

1666:   Consider trying TSARKIMEX if the stiff part is strongly nonlinear.

1668:   Developer notes:
1669:   Rosenbrock-W methods are typically specified for autonomous ODE

1671: $  udot = f(u)

1673:   by the stage equations

1675: $  k_i = h f(u_0 + sum_j alpha_ij k_j) + h J sum_j gamma_ij k_j

1677:   and step completion formula

1679: $  u_1 = u_0 + sum_j b_j k_j

1681:   with step size h and coefficients alpha_ij, gamma_ij, and b_i. Implementing the method in this form would require f(u)
1682:   and the Jacobian J to be available, in addition to the shifted matrix I - h gamma_ii J. Following Hairer and Wanner,
1683:   we define new variables for the stage equations

1685: $  y_i = gamma_ij k_j

1687:   The k_j can be recovered because Gamma is invertible. Let C be the lower triangular part of Gamma^{-1} and define

1689: $  A = Alpha Gamma^{-1}, bt^T = b^T Gamma^{-1}

1691:   to rewrite the method as

1693: $  [M/(h gamma_ii) - J] y_i = f(u_0 + sum_j a_ij y_j) + M sum_j (c_ij/h) y_j
1694: $  u_1 = u_0 + sum_j bt_j y_j

1696:    where we have introduced the mass matrix M. Continue by defining

1698: $  ydot_i = 1/(h gamma_ii) y_i - sum_j (c_ij/h) y_j

1700:    or, more compactly in tensor notation

1702: $  Ydot = 1/h (Gamma^{-1} \otimes I) Y .

1704:    Note that Gamma^{-1} is lower triangular. With this definition of Ydot in terms of known quantities and the current
1705:    stage y_i, the stage equations reduce to performing one Newton step (typically with a lagged Jacobian) on the
1706:    equation

1708: $  g(u_0 + sum_j a_ij y_j + y_i, ydot_i) = 0

1710:    with initial guess y_i = 0.

1712:   Level: beginner

1714: .seealso:  TSCreate(), TS, TSSetType(), TSRosWSetType(), TSRosWRegister(), TSROSWTHETA1, TSROSWTHETA2, TSROSW2M, TSROSW2P, TSROSWRA3PW, TSROSWRA34PW2, TSROSWRODAS3,
1715:            TSROSWSANDU3, TSROSWASSP3P3S1C, TSROSWLASSP3P4S2C, TSROSWLLSSP3P4S2C, TSROSWGRK4T, TSROSWSHAMP4, TSROSWVELDD4, TSROSW4L
1716: M*/
1719: PETSC_EXTERN PetscErrorCode TSCreate_RosW(TS ts)
1720: {
1721:   TS_RosW        *ros;

1725:   TSRosWInitializePackage();

1727:   ts->ops->reset          = TSReset_RosW;
1728:   ts->ops->destroy        = TSDestroy_RosW;
1729:   ts->ops->view           = TSView_RosW;
1730:   ts->ops->load           = TSLoad_RosW;
1731:   ts->ops->setup          = TSSetUp_RosW;
1732:   ts->ops->step           = TSStep_RosW;
1733:   ts->ops->interpolate    = TSInterpolate_RosW;
1734:   ts->ops->evaluatestep   = TSEvaluateStep_RosW;
1735:   ts->ops->rollback       = TSRollBack_RosW;
1736:   ts->ops->setfromoptions = TSSetFromOptions_RosW;
1737:   ts->ops->snesfunction   = SNESTSFormFunction_RosW;
1738:   ts->ops->snesjacobian   = SNESTSFormJacobian_RosW;

1740:   PetscNewLog(ts,&ros);
1741:   ts->data = (void*)ros;

1743:   PetscObjectComposeFunction((PetscObject)ts,"TSRosWGetType_C",TSRosWGetType_RosW);
1744:   PetscObjectComposeFunction((PetscObject)ts,"TSRosWSetType_C",TSRosWSetType_RosW);
1745:   PetscObjectComposeFunction((PetscObject)ts,"TSRosWSetRecomputeJacobian_C",TSRosWSetRecomputeJacobian_RosW);

1747:   TSRosWSetType(ts,TSRosWDefault);
1748:   return(0);
1749: }