xref: /petsc/src/ts/impls/explicit/rk/rk.c (revision cef76868a9b561b92ddb855d7a42c003b3b23296)
1e4dd521cSBarry Smith /*
22e698f8bSDebojyoti Ghosh   Code for time stepping with the Runge-Kutta method
3f68a32c8SEmil Constantinescu 
4f68a32c8SEmil Constantinescu   Notes:
5f68a32c8SEmil Constantinescu   The general system is written as
6f68a32c8SEmil Constantinescu 
72e698f8bSDebojyoti Ghosh   Udot = F(t,U)
8f68a32c8SEmil Constantinescu 
9e4dd521cSBarry Smith */
10b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>                /*I   "petscts.h"   I*/
11f68a32c8SEmil Constantinescu #include <petscdm.h>
12f68a32c8SEmil Constantinescu 
13484bcdc7SDebojyoti Ghosh static TSRKType  TSRKDefault = TSRK3BS;
14f68a32c8SEmil Constantinescu static PetscBool TSRKRegisterAllCalled;
15f68a32c8SEmil Constantinescu static PetscBool TSRKPackageInitialized;
16f68a32c8SEmil Constantinescu static PetscInt  explicit_stage_time_id;
17f68a32c8SEmil Constantinescu 
18f68a32c8SEmil Constantinescu typedef struct _RKTableau *RKTableau;
19f68a32c8SEmil Constantinescu struct _RKTableau {
20f68a32c8SEmil Constantinescu   char      *name;
21d760c35bSDebojyoti Ghosh   PetscInt   order;               /* Classical approximation order of the method i              */
22f68a32c8SEmil Constantinescu   PetscInt   s;                   /* Number of stages                                           */
23d760c35bSDebojyoti Ghosh   PetscBool  FSAL;                /* flag to indicate if tableau is FSAL                        */
24f68a32c8SEmil Constantinescu   PetscInt   pinterp;             /* Interpolation order                                        */
25f68a32c8SEmil Constantinescu   PetscReal *A,*b,*c;             /* Tableau                                                    */
26f68a32c8SEmil Constantinescu   PetscReal *bembed;              /* Embedded formula of order one less (order-1)               */
27f68a32c8SEmil Constantinescu   PetscReal *binterp;             /* Dense output formula                                       */
28f68a32c8SEmil Constantinescu   PetscReal  ccfl;                /* Placeholder for CFL coefficient relative to forward Euler  */
29f68a32c8SEmil Constantinescu };
30f68a32c8SEmil Constantinescu typedef struct _RKTableauLink *RKTableauLink;
31f68a32c8SEmil Constantinescu struct _RKTableauLink {
32f68a32c8SEmil Constantinescu   struct _RKTableau tab;
33f68a32c8SEmil Constantinescu   RKTableauLink     next;
34f68a32c8SEmil Constantinescu };
35f68a32c8SEmil Constantinescu static RKTableauLink RKTableauList;
36e4dd521cSBarry Smith 
37e4dd521cSBarry Smith typedef struct {
38f68a32c8SEmil Constantinescu   RKTableau   tableau;
39f68a32c8SEmil Constantinescu   Vec          *Y;               /* States computed during the step */
40f68a32c8SEmil Constantinescu   Vec          *YdotRHS;         /* Function evaluations for the non-stiff part */
41ad8e2604SHong Zhang   Vec          *VecDeltaLam;     /* Increment of the adjoint sensitivity w.r.t IC at stage*/
42ad8e2604SHong Zhang   Vec          *VecDeltaMu;      /* Increment of the adjoint sensitivity w.r.t P at stage*/
43b18ea86cSHong Zhang   Vec          *VecSensiTemp;    /* Vector to be timed with Jacobian transpose*/
44f68a32c8SEmil Constantinescu   PetscScalar  *work;            /* Scalar work */
45f68a32c8SEmil Constantinescu   PetscReal    stage_time;
46f68a32c8SEmil Constantinescu   TSStepStatus status;
475f70b478SJed Brown } TS_RK;
48e4dd521cSBarry Smith 
49f68a32c8SEmil Constantinescu /*MC
50f68a32c8SEmil Constantinescu      TSRK1 - First order forward Euler scheme.
51262ff7bbSBarry Smith 
52f68a32c8SEmil Constantinescu      This method has one stage.
53f68a32c8SEmil Constantinescu 
54f68a32c8SEmil Constantinescu      Level: advanced
55f68a32c8SEmil Constantinescu 
56f68a32c8SEmil Constantinescu .seealso: TSRK
57f68a32c8SEmil Constantinescu M*/
58f68a32c8SEmil Constantinescu /*MC
592109b73fSDebojyoti Ghosh      TSRK2A - Second order RK scheme.
60f68a32c8SEmil Constantinescu 
61f68a32c8SEmil Constantinescu      This method has two stages.
62f68a32c8SEmil Constantinescu 
63f68a32c8SEmil Constantinescu      Level: advanced
64f68a32c8SEmil Constantinescu 
65f68a32c8SEmil Constantinescu .seealso: TSRK
66f68a32c8SEmil Constantinescu M*/
67f68a32c8SEmil Constantinescu /*MC
68f68a32c8SEmil Constantinescu      TSRK3 - Third order RK scheme.
69f68a32c8SEmil Constantinescu 
70f68a32c8SEmil Constantinescu      This method has three stages.
71f68a32c8SEmil Constantinescu 
72f68a32c8SEmil Constantinescu      Level: advanced
73f68a32c8SEmil Constantinescu 
74f68a32c8SEmil Constantinescu .seealso: TSRK
75f68a32c8SEmil Constantinescu M*/
76f68a32c8SEmil Constantinescu /*MC
772109b73fSDebojyoti Ghosh      TSRK3BS - Third order RK scheme of Bogacki-Shampine with 2nd order embedded method.
782109b73fSDebojyoti Ghosh 
792109b73fSDebojyoti Ghosh      This method has four stages.
802109b73fSDebojyoti Ghosh 
812109b73fSDebojyoti Ghosh      Level: advanced
822109b73fSDebojyoti Ghosh 
832109b73fSDebojyoti Ghosh .seealso: TSRK
842109b73fSDebojyoti Ghosh M*/
852109b73fSDebojyoti Ghosh /*MC
86f68a32c8SEmil Constantinescu      TSRK4 - Fourth order RK scheme.
87f68a32c8SEmil Constantinescu 
882e698f8bSDebojyoti Ghosh      This is the classical Runge-Kutta method with four stages.
89f68a32c8SEmil Constantinescu 
90f68a32c8SEmil Constantinescu      Level: advanced
91f68a32c8SEmil Constantinescu 
92f68a32c8SEmil Constantinescu .seealso: TSRK
93f68a32c8SEmil Constantinescu M*/
94f68a32c8SEmil Constantinescu /*MC
952e698f8bSDebojyoti Ghosh      TSRK5F - Fifth order Fehlberg RK scheme with a 4th order embedded method.
96f68a32c8SEmil Constantinescu 
97f68a32c8SEmil Constantinescu      This method has six stages.
98f68a32c8SEmil Constantinescu 
99f68a32c8SEmil Constantinescu      Level: advanced
100f68a32c8SEmil Constantinescu 
101f68a32c8SEmil Constantinescu .seealso: TSRK
102f68a32c8SEmil Constantinescu M*/
1032109b73fSDebojyoti Ghosh /*MC
1042e698f8bSDebojyoti Ghosh      TSRK5DP - Fifth order Dormand-Prince RK scheme with the 4th order embedded method.
1052109b73fSDebojyoti Ghosh 
1062109b73fSDebojyoti Ghosh      This method has seven stages.
1072109b73fSDebojyoti Ghosh 
1082109b73fSDebojyoti Ghosh      Level: advanced
1092109b73fSDebojyoti Ghosh 
1102109b73fSDebojyoti Ghosh .seealso: TSRK
1112109b73fSDebojyoti Ghosh M*/
112262ff7bbSBarry Smith 
113262ff7bbSBarry Smith #undef __FUNCT__
114f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKRegisterAll"
115f68a32c8SEmil Constantinescu /*@C
116f68a32c8SEmil Constantinescu   TSRKRegisterAll - Registers all of the Runge-Kutta explicit methods in TSRK
117262ff7bbSBarry Smith 
118f68a32c8SEmil Constantinescu   Not Collective, but should be called by all processes which will need the schemes to be registered
119262ff7bbSBarry Smith 
120f68a32c8SEmil Constantinescu   Level: advanced
121262ff7bbSBarry Smith 
122f68a32c8SEmil Constantinescu .keywords: TS, TSRK, register, all
123262ff7bbSBarry Smith 
124f68a32c8SEmil Constantinescu .seealso:  TSRKRegisterDestroy()
125262ff7bbSBarry Smith @*/
126f68a32c8SEmil Constantinescu PetscErrorCode TSRKRegisterAll(void)
127262ff7bbSBarry Smith {
1284ac538c5SBarry Smith   PetscErrorCode ierr;
129262ff7bbSBarry Smith 
130262ff7bbSBarry Smith   PetscFunctionBegin;
131f68a32c8SEmil Constantinescu   if (TSRKRegisterAllCalled) PetscFunctionReturn(0);
132f68a32c8SEmil Constantinescu   TSRKRegisterAllCalled = PETSC_TRUE;
133e4dd521cSBarry Smith 
134e4dd521cSBarry Smith   {
135f68a32c8SEmil Constantinescu     const PetscReal
136f68a32c8SEmil Constantinescu       A[1][1] = {{0.0}},
137f68a32c8SEmil Constantinescu       b[1]    = {1.0};
138f68a32c8SEmil Constantinescu     ierr = TSRKRegister(TSRK1FE,1,1,&A[0][0],b,NULL,NULL,1,b);CHKERRQ(ierr);
139e4dd521cSBarry Smith   }
140db046809SBarry Smith   {
141f68a32c8SEmil Constantinescu     const PetscReal
142f68a32c8SEmil Constantinescu       A[2][2]     = {{0.0,0.0},
143f68a32c8SEmil Constantinescu                     {1.0,0.0}},
144f68a32c8SEmil Constantinescu       b[2]        = {0.5,0.5},
145f68a32c8SEmil Constantinescu       bembed[2]   = {1.0,0};
146fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK2A,2,2,&A[0][0],b,NULL,bembed,2,b);CHKERRQ(ierr);
147db046809SBarry Smith   }
148f68a32c8SEmil Constantinescu   {
149f68a32c8SEmil Constantinescu     const PetscReal
150f68a32c8SEmil Constantinescu       A[3][3] = {{0,0,0},
151f68a32c8SEmil Constantinescu                  {2.0/3.0,0,0},
152f68a32c8SEmil Constantinescu                  {-1.0/3.0,1.0,0}},
153f68a32c8SEmil Constantinescu       b[3]    = {0.25,0.5,0.25};
154fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK3,3,3,&A[0][0],b,NULL,NULL,3,b);CHKERRQ(ierr);
155fdefd5e5SDebojyoti Ghosh   }
156fdefd5e5SDebojyoti Ghosh   {
157fdefd5e5SDebojyoti Ghosh     const PetscReal
158fdefd5e5SDebojyoti Ghosh       A[4][4] = {{0,0,0,0},
159fdefd5e5SDebojyoti Ghosh                  {1.0/2.0,0,0,0},
160fdefd5e5SDebojyoti Ghosh                  {0,3.0/4.0,0,0},
161fdefd5e5SDebojyoti Ghosh                  {2.0/9.0,1.0/3.0,4.0/9.0,0}},
162fdefd5e5SDebojyoti Ghosh       b[4]    = {2.0/9.0,1.0/3.0,4.0/9.0,0},
163fdefd5e5SDebojyoti Ghosh       bembed[4] = {7.0/24.0,1.0/4.0,1.0/3.0,1.0/8.0};
164fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK3BS,3,4,&A[0][0],b,NULL,bembed,3,b);CHKERRQ(ierr);
165db046809SBarry Smith   }
166f68a32c8SEmil Constantinescu   {
167f68a32c8SEmil Constantinescu     const PetscReal
168f68a32c8SEmil Constantinescu       A[4][4] = {{0,0,0,0},
169f68a32c8SEmil Constantinescu                  {0.5,0,0,0},
170f68a32c8SEmil Constantinescu                  {0,0.5,0,0},
171f68a32c8SEmil Constantinescu                  {0,0,1.0,0}},
172f68a32c8SEmil Constantinescu       b[4]    = {1.0/6.0,1.0/3.0,1.0/3.0,1.0/6.0};
173fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK4,4,4,&A[0][0],b,NULL,NULL,4,b);CHKERRQ(ierr);
174db046809SBarry Smith   }
175f68a32c8SEmil Constantinescu   {
176f68a32c8SEmil Constantinescu     const PetscReal
177f68a32c8SEmil Constantinescu       A[6][6]   = {{0,0,0,0,0,0},
178f68a32c8SEmil Constantinescu                    {0.25,0,0,0,0,0},
179f68a32c8SEmil Constantinescu                    {3.0/32.0,9.0/32.0,0,0,0,0},
180f68a32c8SEmil Constantinescu                    {1932.0/2197.0,-7200.0/2197.0,7296.0/2197.0,0,0,0},
181f68a32c8SEmil Constantinescu                    {439.0/216.0,-8.0,3680.0/513.0,-845.0/4104.0,0,0},
182f68a32c8SEmil Constantinescu                    {-8.0/27.0,2.0,-3544.0/2565.0,1859.0/4104.0,-11.0/40.0,0}},
183f68a32c8SEmil Constantinescu       b[6]      = {16.0/135.0,0,6656.0/12825.0,28561.0/56430.0,-9.0/50.0,2.0/55.0},
184f68a32c8SEmil Constantinescu       bembed[6] = {25.0/216.0,0,1408.0/2565.0,2197.0/4104.0,-1.0/5.0,0};
185fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK5F,5,6,&A[0][0],b,NULL,bembed,5,b);CHKERRQ(ierr);
186fdefd5e5SDebojyoti Ghosh   }
187fdefd5e5SDebojyoti Ghosh   {
188fdefd5e5SDebojyoti Ghosh     const PetscReal
189fdefd5e5SDebojyoti Ghosh       A[7][7]   = {{0,0,0,0,0,0,0},
190fdefd5e5SDebojyoti Ghosh                    {1.0/5.0,0,0,0,0,0,0},
191fdefd5e5SDebojyoti Ghosh                    {3.0/40.0,9.0/40.0,0,0,0,0,0},
192fdefd5e5SDebojyoti Ghosh                    {44.0/45.0,-56.0/15.0,32.0/9.0,0,0,0,0},
193fdefd5e5SDebojyoti Ghosh                    {19372.0/6561.0,-25360.0/2187.0,64448.0/6561.0,-212.0/729.0,0,0,0},
194fdefd5e5SDebojyoti Ghosh                    {9017.0/3168.0,-355.0/33.0,46732.0/5247.0,49.0/176.0,-5103.0/18656.0,0,0},
195fdefd5e5SDebojyoti Ghosh                    {35.0/384.0,0,500.0/1113.0,125.0/192.0,-2187.0/6784.0,11.0/84.0,0}},
196fdefd5e5SDebojyoti Ghosh       b[7]      = {35.0/384.0,0,500.0/1113.0,125.0/192.0,-2187.0/6784.0,11.0/84.0,0},
197fdefd5e5SDebojyoti Ghosh       bembed[7] = {5179.0/57600.0,0,7571.0/16695.0,393.0/640.0,-92097.0/339200.0,187.0/2100.0,1.0/40.0};
198fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK5DP,5,7,&A[0][0],b,NULL,bembed,5,b);CHKERRQ(ierr);
199f68a32c8SEmil Constantinescu   }
200db046809SBarry Smith   PetscFunctionReturn(0);
201db046809SBarry Smith }
202db046809SBarry Smith 
203e4dd521cSBarry Smith #undef __FUNCT__
204f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKRegisterDestroy"
205f68a32c8SEmil Constantinescu /*@C
206f68a32c8SEmil Constantinescu    TSRKRegisterDestroy - Frees the list of schemes that were registered by TSRKRegister().
207f68a32c8SEmil Constantinescu 
208f68a32c8SEmil Constantinescu    Not Collective
209f68a32c8SEmil Constantinescu 
210f68a32c8SEmil Constantinescu    Level: advanced
211f68a32c8SEmil Constantinescu 
212f68a32c8SEmil Constantinescu .keywords: TSRK, register, destroy
213f68a32c8SEmil Constantinescu .seealso: TSRKRegister(), TSRKRegisterAll()
214f68a32c8SEmil Constantinescu @*/
215f68a32c8SEmil Constantinescu PetscErrorCode TSRKRegisterDestroy(void)
216e4dd521cSBarry Smith {
217f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
218f68a32c8SEmil Constantinescu   RKTableauLink link;
219f68a32c8SEmil Constantinescu 
220f68a32c8SEmil Constantinescu   PetscFunctionBegin;
221f68a32c8SEmil Constantinescu   while ((link = RKTableauList)) {
222f68a32c8SEmil Constantinescu     RKTableau t = &link->tab;
223f68a32c8SEmil Constantinescu     RKTableauList = link->next;
224f68a32c8SEmil Constantinescu     ierr = PetscFree3(t->A,t->b,t->c);  CHKERRQ(ierr);
225f68a32c8SEmil Constantinescu     ierr = PetscFree (t->bembed);       CHKERRQ(ierr);
226f68a32c8SEmil Constantinescu     ierr = PetscFree (t->binterp);      CHKERRQ(ierr);
227f68a32c8SEmil Constantinescu     ierr = PetscFree (t->name);         CHKERRQ(ierr);
228f68a32c8SEmil Constantinescu     ierr = PetscFree (link);            CHKERRQ(ierr);
229f68a32c8SEmil Constantinescu   }
230f68a32c8SEmil Constantinescu   TSRKRegisterAllCalled = PETSC_FALSE;
231f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
232f68a32c8SEmil Constantinescu }
233f68a32c8SEmil Constantinescu 
234f68a32c8SEmil Constantinescu #undef __FUNCT__
235f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKInitializePackage"
236f68a32c8SEmil Constantinescu /*@C
237f68a32c8SEmil Constantinescu   TSRKInitializePackage - This function initializes everything in the TSRK package. It is called
238f68a32c8SEmil Constantinescu   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_RK()
239f68a32c8SEmil Constantinescu   when using static libraries.
240f68a32c8SEmil Constantinescu 
241f68a32c8SEmil Constantinescu   Level: developer
242f68a32c8SEmil Constantinescu 
243f68a32c8SEmil Constantinescu .keywords: TS, TSRK, initialize, package
244f68a32c8SEmil Constantinescu .seealso: PetscInitialize()
245f68a32c8SEmil Constantinescu @*/
246f68a32c8SEmil Constantinescu PetscErrorCode TSRKInitializePackage(void)
247f68a32c8SEmil Constantinescu {
248186e87acSLisandro Dalcin   PetscErrorCode ierr;
249e4dd521cSBarry Smith 
250e4dd521cSBarry Smith   PetscFunctionBegin;
251f68a32c8SEmil Constantinescu   if (TSRKPackageInitialized) PetscFunctionReturn(0);
252f68a32c8SEmil Constantinescu   TSRKPackageInitialized = PETSC_TRUE;
253f68a32c8SEmil Constantinescu   ierr = TSRKRegisterAll();CHKERRQ(ierr);
254f68a32c8SEmil Constantinescu   ierr = PetscObjectComposedDataRegister(&explicit_stage_time_id);CHKERRQ(ierr);
255f68a32c8SEmil Constantinescu   ierr = PetscRegisterFinalize(TSRKFinalizePackage);CHKERRQ(ierr);
256f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
257f68a32c8SEmil Constantinescu }
258186e87acSLisandro Dalcin 
259f68a32c8SEmil Constantinescu #undef __FUNCT__
260f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKFinalizePackage"
261f68a32c8SEmil Constantinescu /*@C
262f68a32c8SEmil Constantinescu   TSRKFinalizePackage - This function destroys everything in the TSRK package. It is
263f68a32c8SEmil Constantinescu   called from PetscFinalize().
264186e87acSLisandro Dalcin 
265f68a32c8SEmil Constantinescu   Level: developer
266f68a32c8SEmil Constantinescu 
267f68a32c8SEmil Constantinescu .keywords: Petsc, destroy, package
268f68a32c8SEmil Constantinescu .seealso: PetscFinalize()
269f68a32c8SEmil Constantinescu @*/
270f68a32c8SEmil Constantinescu PetscErrorCode TSRKFinalizePackage(void)
271f68a32c8SEmil Constantinescu {
272f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
273f68a32c8SEmil Constantinescu 
274f68a32c8SEmil Constantinescu   PetscFunctionBegin;
275f68a32c8SEmil Constantinescu   TSRKPackageInitialized = PETSC_FALSE;
276f68a32c8SEmil Constantinescu   ierr = TSRKRegisterDestroy();CHKERRQ(ierr);
277f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
278f68a32c8SEmil Constantinescu }
279f68a32c8SEmil Constantinescu 
280f68a32c8SEmil Constantinescu #undef __FUNCT__
281f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKRegister"
282f68a32c8SEmil Constantinescu /*@C
283f68a32c8SEmil Constantinescu    TSRKRegister - register an RK scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation
284f68a32c8SEmil Constantinescu 
285f68a32c8SEmil Constantinescu    Not Collective, but the same schemes should be registered on all processes on which they will be used
286f68a32c8SEmil Constantinescu 
287f68a32c8SEmil Constantinescu    Input Parameters:
288f68a32c8SEmil Constantinescu +  name - identifier for method
289f68a32c8SEmil Constantinescu .  order - approximation order of method
290f68a32c8SEmil Constantinescu .  s - number of stages, this is the dimension of the matrices below
291f68a32c8SEmil Constantinescu .  A - stage coefficients (dimension s*s, row-major)
292f68a32c8SEmil Constantinescu .  b - step completion table (dimension s; NULL to use last row of A)
293f68a32c8SEmil Constantinescu .  c - abscissa (dimension s; NULL to use row sums of A)
294f68a32c8SEmil Constantinescu .  bembed - completion table for embedded method (dimension s; NULL if not available)
295f68a32c8SEmil Constantinescu .  pinterp - Order of the interpolation scheme, equal to the number of columns of binterp
296f68a32c8SEmil Constantinescu -  binterp - Coefficients of the interpolation formula (dimension s*pinterp; NULL to reuse binterpt)
297f68a32c8SEmil Constantinescu 
298f68a32c8SEmil Constantinescu    Notes:
299f68a32c8SEmil Constantinescu    Several RK methods are provided, this function is only needed to create new methods.
300f68a32c8SEmil Constantinescu 
301f68a32c8SEmil Constantinescu    Level: advanced
302f68a32c8SEmil Constantinescu 
303f68a32c8SEmil Constantinescu .keywords: TS, register
304f68a32c8SEmil Constantinescu 
305f68a32c8SEmil Constantinescu .seealso: TSRK
306f68a32c8SEmil Constantinescu @*/
307f68a32c8SEmil Constantinescu PetscErrorCode TSRKRegister(TSRKType name,PetscInt order,PetscInt s,
308f68a32c8SEmil Constantinescu                                  const PetscReal A[],const PetscReal b[],const PetscReal c[],
309f68a32c8SEmil Constantinescu                                  const PetscReal bembed[],
310f68a32c8SEmil Constantinescu                                  PetscInt pinterp,const PetscReal binterp[])
311f68a32c8SEmil Constantinescu {
312f68a32c8SEmil Constantinescu   PetscErrorCode  ierr;
313f68a32c8SEmil Constantinescu   RKTableauLink   link;
314f68a32c8SEmil Constantinescu   RKTableau       t;
315f68a32c8SEmil Constantinescu   PetscInt        i,j;
316f68a32c8SEmil Constantinescu 
317f68a32c8SEmil Constantinescu   PetscFunctionBegin;
318f68a32c8SEmil Constantinescu   ierr     = PetscMalloc(sizeof(*link),&link);CHKERRQ(ierr);
319f68a32c8SEmil Constantinescu   ierr     = PetscMemzero(link,sizeof(*link));CHKERRQ(ierr);
320f68a32c8SEmil Constantinescu   t        = &link->tab;
321f68a32c8SEmil Constantinescu   ierr     = PetscStrallocpy(name,&t->name);CHKERRQ(ierr);
322f68a32c8SEmil Constantinescu   t->order = order;
323f68a32c8SEmil Constantinescu   t->s     = s;
324dcca6d9dSJed Brown   ierr     = PetscMalloc3(s*s,&t->A,s,&t->b,s,&t->c);CHKERRQ(ierr);
325f68a32c8SEmil Constantinescu   ierr     = PetscMemcpy(t->A,A,s*s*sizeof(A[0]));CHKERRQ(ierr);
326f68a32c8SEmil Constantinescu   if (b)  { ierr = PetscMemcpy(t->b,b,s*sizeof(b[0]));CHKERRQ(ierr); }
327f68a32c8SEmil Constantinescu   else for (i=0; i<s; i++) t->b[i] = A[(s-1)*s+i];
328f68a32c8SEmil Constantinescu   if (c)  { ierr = PetscMemcpy(t->c,c,s*sizeof(c[0]));CHKERRQ(ierr); }
329f68a32c8SEmil Constantinescu   else for (i=0; i<s; i++) for (j=0,t->c[i]=0; j<s; j++) t->c[i] += A[i*s+j];
330d760c35bSDebojyoti Ghosh   t->FSAL = PETSC_TRUE;
331d760c35bSDebojyoti Ghosh   for (i=0; i<s; i++) if (t->A[(s-1)*s+i] != t->b[i]) t->FSAL = PETSC_FALSE;
332f68a32c8SEmil Constantinescu   if (bembed) {
333785e854fSJed Brown     ierr = PetscMalloc1(s,&t->bembed);CHKERRQ(ierr);
334f68a32c8SEmil Constantinescu     ierr = PetscMemcpy(t->bembed,bembed,s*sizeof(bembed[0]));CHKERRQ(ierr);
335f68a32c8SEmil Constantinescu   }
336f68a32c8SEmil Constantinescu 
337f68a32c8SEmil Constantinescu   t->pinterp     = pinterp;
338785e854fSJed Brown   ierr           = PetscMalloc1(s*pinterp,&t->binterp);CHKERRQ(ierr);
339f68a32c8SEmil Constantinescu   ierr           = PetscMemcpy(t->binterp,binterp,s*pinterp*sizeof(binterp[0]));CHKERRQ(ierr);
340f68a32c8SEmil Constantinescu   link->next     = RKTableauList;
341f68a32c8SEmil Constantinescu   RKTableauList = link;
342f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
343f68a32c8SEmil Constantinescu }
344f68a32c8SEmil Constantinescu 
345f68a32c8SEmil Constantinescu #undef __FUNCT__
346f68a32c8SEmil Constantinescu #define __FUNCT__ "TSEvaluateStep_RK"
347e4dd521cSBarry Smith /*
348f68a32c8SEmil Constantinescu  The step completion formula is
349e4dd521cSBarry Smith 
350f68a32c8SEmil Constantinescu  x1 = x0 + h b^T YdotRHS
351f68a32c8SEmil Constantinescu 
352f68a32c8SEmil Constantinescu  This function can be called before or after ts->vec_sol has been updated.
353f68a32c8SEmil Constantinescu  Suppose we have a completion formula (b) and an embedded formula (be) of different order.
354f68a32c8SEmil Constantinescu  We can write
355f68a32c8SEmil Constantinescu 
356f68a32c8SEmil Constantinescu  x1e = x0 + h be^T YdotRHS
357f68a32c8SEmil Constantinescu      = x1 - h b^T YdotRHS + h be^T YdotRHS
358f68a32c8SEmil Constantinescu      = x1 + h (be - b)^T YdotRHS
359f68a32c8SEmil Constantinescu 
360f68a32c8SEmil Constantinescu  so we can evaluate the method with different order even after the step has been optimistically completed.
361e4dd521cSBarry Smith */
362f68a32c8SEmil Constantinescu static PetscErrorCode TSEvaluateStep_RK(TS ts,PetscInt order,Vec X,PetscBool *done)
363f68a32c8SEmil Constantinescu {
364f68a32c8SEmil Constantinescu   TS_RK         *rk   = (TS_RK*)ts->data;
365f68a32c8SEmil Constantinescu   RKTableau      tab  = rk->tableau;
366f68a32c8SEmil Constantinescu   PetscScalar   *w    = rk->work;
367f68a32c8SEmil Constantinescu   PetscReal      h;
368f68a32c8SEmil Constantinescu   PetscInt       s    = tab->s,j;
369f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
370f68a32c8SEmil Constantinescu 
371f68a32c8SEmil Constantinescu   PetscFunctionBegin;
372f68a32c8SEmil Constantinescu   switch (rk->status) {
373f68a32c8SEmil Constantinescu   case TS_STEP_INCOMPLETE:
374f68a32c8SEmil Constantinescu   case TS_STEP_PENDING:
375f68a32c8SEmil Constantinescu     h = ts->time_step; break;
376f68a32c8SEmil Constantinescu   case TS_STEP_COMPLETE:
377f68a32c8SEmil Constantinescu     h = ts->time_step_prev; break;
378f68a32c8SEmil Constantinescu   default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
379f68a32c8SEmil Constantinescu   }
380f68a32c8SEmil Constantinescu   if (order == tab->order) {
381f68a32c8SEmil Constantinescu     if (rk->status == TS_STEP_INCOMPLETE) {
382f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);
383f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = h*tab->b[j];
384f68a32c8SEmil Constantinescu       ierr = VecMAXPY(X,s,w,rk->YdotRHS);CHKERRQ(ierr);
385f68a32c8SEmil Constantinescu     } else {ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);}
386f68a32c8SEmil Constantinescu     PetscFunctionReturn(0);
387f68a32c8SEmil Constantinescu   } else if (order == tab->order-1) {
388f68a32c8SEmil Constantinescu     if (!tab->bembed) goto unavailable;
389f68a32c8SEmil Constantinescu     if (rk->status == TS_STEP_INCOMPLETE) { /* Complete with the embedded method (be) */
390f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);
391f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = h*tab->bembed[j];
392f68a32c8SEmil Constantinescu       ierr = VecMAXPY(X,s,w,rk->YdotRHS);CHKERRQ(ierr);
393f68a32c8SEmil Constantinescu     } else {                    /* Rollback and re-complete using (be-b) */
394f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);
395f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = h*(tab->bembed[j] - tab->b[j]);
396f68a32c8SEmil Constantinescu       ierr = VecMAXPY(X,s,w,rk->YdotRHS);CHKERRQ(ierr);
397f68a32c8SEmil Constantinescu     }
398f68a32c8SEmil Constantinescu     if (done) *done = PETSC_TRUE;
399f68a32c8SEmil Constantinescu     PetscFunctionReturn(0);
400f68a32c8SEmil Constantinescu   }
401f68a32c8SEmil Constantinescu unavailable:
402f68a32c8SEmil Constantinescu   if (done) *done = PETSC_FALSE;
403f68a32c8SEmil Constantinescu   else SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"RK '%s' of order %D cannot evaluate step at order %D",tab->name,tab->order,order);
404f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
405f68a32c8SEmil Constantinescu }
406f68a32c8SEmil Constantinescu 
407f68a32c8SEmil Constantinescu #undef __FUNCT__
408f68a32c8SEmil Constantinescu #define __FUNCT__ "TSStep_RK"
409f68a32c8SEmil Constantinescu static PetscErrorCode TSStep_RK(TS ts)
410f68a32c8SEmil Constantinescu {
411f68a32c8SEmil Constantinescu   TS_RK           *rk   = (TS_RK*)ts->data;
412f68a32c8SEmil Constantinescu   RKTableau        tab  = rk->tableau;
413f68a32c8SEmil Constantinescu   const PetscInt   s    = tab->s;
414f68a32c8SEmil Constantinescu   const PetscReal *A = tab->A,*b = tab->b,*c = tab->c;
415f68a32c8SEmil Constantinescu   PetscScalar     *w    = rk->work;
416f68a32c8SEmil Constantinescu   Vec             *Y    = rk->Y,*YdotRHS = rk->YdotRHS;
417f68a32c8SEmil Constantinescu   TSAdapt          adapt;
418f68a32c8SEmil Constantinescu   PetscInt         i,j,reject,next_scheme;
419f68a32c8SEmil Constantinescu   PetscReal        next_time_step;
420f68a32c8SEmil Constantinescu   PetscReal        t;
421f68a32c8SEmil Constantinescu   PetscBool        accept;
422f68a32c8SEmil Constantinescu   PetscErrorCode   ierr;
423f68a32c8SEmil Constantinescu 
424f68a32c8SEmil Constantinescu   PetscFunctionBegin;
425f68a32c8SEmil Constantinescu 
426f68a32c8SEmil Constantinescu   next_time_step = ts->time_step;
427f68a32c8SEmil Constantinescu   t              = ts->ptime;
428f68a32c8SEmil Constantinescu   accept         = PETSC_TRUE;
429f68a32c8SEmil Constantinescu   rk->status     = TS_STEP_INCOMPLETE;
430f68a32c8SEmil Constantinescu 
431f68a32c8SEmil Constantinescu 
432f68a32c8SEmil Constantinescu   for (reject=0; reject<ts->max_reject && !ts->reason; reject++,ts->reject++) {
433f68a32c8SEmil Constantinescu     PetscReal h = ts->time_step;
434f68a32c8SEmil Constantinescu     ierr = TSPreStep(ts);CHKERRQ(ierr);
435f68a32c8SEmil Constantinescu     for (i=0; i<s; i++) {
4369be3e283SDebojyoti Ghosh       rk->stage_time = t + h*c[i];
4379be3e283SDebojyoti Ghosh       ierr = TSPreStage(ts,rk->stage_time); CHKERRQ(ierr);
438f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,Y[i]);CHKERRQ(ierr);
439f68a32c8SEmil Constantinescu       for (j=0; j<i; j++) w[j] = h*A[i*s+j];
440f68a32c8SEmil Constantinescu       ierr = VecMAXPY(Y[i],i,w,YdotRHS);CHKERRQ(ierr);
4419be3e283SDebojyoti Ghosh       ierr = TSPostStage(ts,rk->stage_time,i,Y); CHKERRQ(ierr);
442f68a32c8SEmil Constantinescu       ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
443f68a32c8SEmil Constantinescu       ierr = TSAdaptCheckStage(adapt,ts,&accept);CHKERRQ(ierr);
444f68a32c8SEmil Constantinescu       if (!accept) goto reject_step;
445f68a32c8SEmil Constantinescu       ierr = TSComputeRHSFunction(ts,t+h*c[i],Y[i],YdotRHS[i]);CHKERRQ(ierr);
446f68a32c8SEmil Constantinescu     }
447f68a32c8SEmil Constantinescu     ierr = TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);CHKERRQ(ierr);
448f68a32c8SEmil Constantinescu     rk->status = TS_STEP_PENDING;
449f68a32c8SEmil Constantinescu 
450f68a32c8SEmil Constantinescu     /* Register only the current method as a candidate because we're not supporting multiple candidates yet. */
451f68a32c8SEmil Constantinescu     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
452f68a32c8SEmil Constantinescu     ierr = TSAdaptCandidatesClear(adapt);CHKERRQ(ierr);
453f68a32c8SEmil Constantinescu     ierr = TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,1.*tab->s,PETSC_TRUE);CHKERRQ(ierr);
454f68a32c8SEmil Constantinescu     ierr = TSAdaptChoose(adapt,ts,ts->time_step,&next_scheme,&next_time_step,&accept);CHKERRQ(ierr);
455f68a32c8SEmil Constantinescu     if (accept) {
456f68a32c8SEmil Constantinescu       /* ignore next_scheme for now */
457f68a32c8SEmil Constantinescu       ts->ptime    += ts->time_step;
458f68a32c8SEmil Constantinescu       ts->time_step = next_time_step;
459e3caeda6SBarry Smith       ts->steps++;
460f68a32c8SEmil Constantinescu       rk->status = TS_STEP_COMPLETE;
461f68a32c8SEmil Constantinescu       ierr = PetscObjectComposedDataSetReal((PetscObject)ts->vec_sol,explicit_stage_time_id,ts->ptime);CHKERRQ(ierr);
462f68a32c8SEmil Constantinescu       break;
463f68a32c8SEmil Constantinescu     } else {                    /* Roll back the current step */
464f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = -h*b[j];
465f68a32c8SEmil Constantinescu       ierr = VecMAXPY(ts->vec_sol,s,w,rk->YdotRHS);CHKERRQ(ierr);
466f68a32c8SEmil Constantinescu       ts->time_step = next_time_step;
467f68a32c8SEmil Constantinescu       rk->status   = TS_STEP_INCOMPLETE;
468f68a32c8SEmil Constantinescu     }
469f68a32c8SEmil Constantinescu reject_step: continue;
470f68a32c8SEmil Constantinescu   }
471f68a32c8SEmil Constantinescu   if (rk->status != TS_STEP_COMPLETE && !ts->reason) ts->reason = TS_DIVERGED_STEP_REJECTED;
472f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
473e4dd521cSBarry Smith }
474e4dd521cSBarry Smith 
475f68a32c8SEmil Constantinescu #undef __FUNCT__
476f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp_RK"
477f6a906c0SBarry Smith static PetscErrorCode TSAdjointSetUp_RK(TS ts)
478f6a906c0SBarry Smith {
479f6a906c0SBarry Smith   TS_RK         *rk = (TS_RK*)ts->data;
480f6a906c0SBarry Smith   RKTableau      tab;
481f6a906c0SBarry Smith   PetscInt       s;
482f6a906c0SBarry Smith   PetscErrorCode ierr;
483f6a906c0SBarry Smith 
484f6a906c0SBarry Smith   PetscFunctionBegin;
485f6a906c0SBarry Smith   if (ts->adjointsetupcalled++) PetscFunctionReturn(0);
486f6a906c0SBarry Smith   tab  = rk->tableau;
487f6a906c0SBarry Smith   s    = tab->s;
488f6a906c0SBarry Smith   ierr = VecDuplicateVecs(ts->vecs_sensi[0],s*ts->numberadjs,&rk->VecDeltaLam);CHKERRQ(ierr);
489f6a906c0SBarry Smith   if(ts->vecs_sensip) {
490f6a906c0SBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensip[0],s*ts->numberadjs,&rk->VecDeltaMu);CHKERRQ(ierr);
491f6a906c0SBarry Smith   }
492f6a906c0SBarry Smith   ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numberadjs,&rk->VecSensiTemp);CHKERRQ(ierr);
493f6a906c0SBarry Smith   PetscFunctionReturn(0);
494f6a906c0SBarry Smith }
495f6a906c0SBarry Smith 
496f6a906c0SBarry Smith #undef __FUNCT__
49742f2b339SBarry Smith #define __FUNCT__ "TSAdjointStep_RK"
49842f2b339SBarry Smith static PetscErrorCode TSAdjointStep_RK(TS ts)
499d2daff3dSHong Zhang {
500c235aa8dSHong Zhang   TS_RK           *rk   = (TS_RK*)ts->data;
501c235aa8dSHong Zhang   RKTableau        tab  = rk->tableau;
502c235aa8dSHong Zhang   const PetscInt   s    = tab->s;
503c235aa8dSHong Zhang   const PetscReal *A = tab->A,*b = tab->b,*c = tab->c;
504c235aa8dSHong Zhang   PetscScalar     *w    = rk->work;
505ad8e2604SHong Zhang   Vec             *Y    = rk->Y,*VecDeltaLam = rk->VecDeltaLam,*VecDeltaMu = rk->VecDeltaMu,*VecSensiTemp = rk->VecSensiTemp;
506b18ea86cSHong Zhang   PetscInt         i,j,nadj;
507c235aa8dSHong Zhang   PetscReal        t;
508d2daff3dSHong Zhang   PetscErrorCode   ierr;
509*cef76868SBarry Smith   PetscReal        h = ts->time_step;
510*cef76868SBarry Smith   Mat              J,Jp;
511c235aa8dSHong Zhang 
512d2daff3dSHong Zhang   PetscFunctionBegin;
513c235aa8dSHong Zhang   t          = ts->ptime;
514c235aa8dSHong Zhang   rk->status = TS_STEP_INCOMPLETE;
515*cef76868SBarry Smith   h = ts->time_step;
516c235aa8dSHong Zhang   ierr = TSPreStep(ts);CHKERRQ(ierr);
517c235aa8dSHong Zhang   for (i=s-1; i>=0; i--) {
518c235aa8dSHong Zhang     rk->stage_time = t + h*(1.0-c[i]);
519b18ea86cSHong Zhang     for (nadj=0; nadj<ts->numberadjs; nadj++) {
520b18ea86cSHong Zhang       ierr = VecCopy(ts->vecs_sensi[nadj],VecSensiTemp[nadj]);CHKERRQ(ierr);
521b18ea86cSHong Zhang       ierr = VecScale(VecSensiTemp[nadj],-h*b[i]);
522c235aa8dSHong Zhang       for (j=i+1; j<s; j++) {
523b18ea86cSHong Zhang         ierr = VecAXPY(VecSensiTemp[nadj],-h*A[j*s+i],VecDeltaLam[nadj*s+j]);
524b18ea86cSHong Zhang       }
525c235aa8dSHong Zhang     }
526ad8e2604SHong Zhang     /* Stage values of lambda */
527c235aa8dSHong Zhang     ierr = TSGetRHSJacobian(ts,&J,&Jp,NULL,NULL);CHKERRQ(ierr);
528c235aa8dSHong Zhang     ierr = TSComputeRHSJacobian(ts,rk->stage_time,Y[i],J,Jp);CHKERRQ(ierr);
529b18ea86cSHong Zhang     for (nadj=0; nadj<ts->numberadjs; nadj++) {
530b18ea86cSHong Zhang       ierr = MatMultTranspose(J,VecSensiTemp[nadj],VecDeltaLam[nadj*s+i]);CHKERRQ(ierr);
531b18ea86cSHong Zhang     }
5326497ce14SHong Zhang 
533ad8e2604SHong Zhang     /* Stage values of mu */
5346497ce14SHong Zhang     if(ts->vecs_sensip) {
5355bf8c567SBarry Smith       ierr = TSAdjointComputeRHSJacobian(ts,rk->stage_time,Y[i],ts->Jacp);CHKERRQ(ierr);
536ad8e2604SHong Zhang       for (nadj=0; nadj<ts->numberadjs; nadj++) {
537ad8e2604SHong Zhang         ierr = MatMultTranspose(ts->Jacp,VecSensiTemp[nadj],VecDeltaMu[nadj*s+i]);CHKERRQ(ierr);
538ad8e2604SHong Zhang       }
539c235aa8dSHong Zhang     }
5406497ce14SHong Zhang   }
541c235aa8dSHong Zhang 
542c235aa8dSHong Zhang   for (j=0; j<s; j++) w[j] = 1.0;
543b18ea86cSHong Zhang   for (nadj=0; nadj<ts->numberadjs; nadj++) {
544b18ea86cSHong Zhang     ierr = VecMAXPY(ts->vecs_sensi[nadj],s,w,&VecDeltaLam[nadj*s]);CHKERRQ(ierr);
5456497ce14SHong Zhang     if(ts->vecs_sensip) {
546ad8e2604SHong Zhang       ierr = VecMAXPY(ts->vecs_sensip[nadj],s,w,&VecDeltaMu[nadj*s]);CHKERRQ(ierr);
547b18ea86cSHong Zhang     }
5486497ce14SHong Zhang   }
549c235aa8dSHong Zhang   ts->ptime += ts->time_step;
550c235aa8dSHong Zhang   ts->steps++;
551c235aa8dSHong Zhang   rk->status = TS_STEP_COMPLETE;
552b18ea86cSHong Zhang   ierr = PetscObjectComposedDataSetReal((PetscObject)ts->vecs_sensi,explicit_stage_time_id,ts->ptime);CHKERRQ(ierr);
5536497ce14SHong Zhang   if(ts->vecs_sensip) {
554ad8e2604SHong Zhang     ierr = PetscObjectComposedDataSetReal((PetscObject)ts->vecs_sensip,explicit_stage_time_id,ts->ptime);CHKERRQ(ierr);
5556497ce14SHong Zhang   }
556d2daff3dSHong Zhang   PetscFunctionReturn(0);
557d2daff3dSHong Zhang }
558d2daff3dSHong Zhang 
559d2daff3dSHong Zhang #undef __FUNCT__
560f68a32c8SEmil Constantinescu #define __FUNCT__ "TSInterpolate_RK"
561f68a32c8SEmil Constantinescu static PetscErrorCode TSInterpolate_RK(TS ts,PetscReal itime,Vec X)
562f68a32c8SEmil Constantinescu {
563f68a32c8SEmil Constantinescu   TS_RK           *rk = (TS_RK*)ts->data;
564f68a32c8SEmil Constantinescu   PetscInt         s  = rk->tableau->s,pinterp = rk->tableau->pinterp,i,j;
565f68a32c8SEmil Constantinescu   PetscReal        h;
566f68a32c8SEmil Constantinescu   PetscReal        tt,t;
567f68a32c8SEmil Constantinescu   PetscScalar     *b;
568f68a32c8SEmil Constantinescu   const PetscReal *B = rk->tableau->binterp;
569f68a32c8SEmil Constantinescu   PetscErrorCode   ierr;
570e4dd521cSBarry Smith 
571f68a32c8SEmil Constantinescu   PetscFunctionBegin;
572f68a32c8SEmil Constantinescu   if (!B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRK %s does not have an interpolation formula",rk->tableau->name);
573f68a32c8SEmil Constantinescu   switch (rk->status) {
574f68a32c8SEmil Constantinescu   case TS_STEP_INCOMPLETE:
575f68a32c8SEmil Constantinescu   case TS_STEP_PENDING:
576f68a32c8SEmil Constantinescu     h = ts->time_step;
577f68a32c8SEmil Constantinescu     t = (itime - ts->ptime)/h;
578f68a32c8SEmil Constantinescu     break;
579f68a32c8SEmil Constantinescu   case TS_STEP_COMPLETE:
580f68a32c8SEmil Constantinescu     h = ts->time_step_prev;
581f68a32c8SEmil Constantinescu     t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
582f68a32c8SEmil Constantinescu     break;
583f68a32c8SEmil Constantinescu   default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
584e4dd521cSBarry Smith   }
585785e854fSJed Brown   ierr = PetscMalloc1(s,&b);CHKERRQ(ierr);
586f68a32c8SEmil Constantinescu   for (i=0; i<s; i++) b[i] = 0;
587f68a32c8SEmil Constantinescu   for (j=0,tt=t; j<pinterp; j++,tt*=t) {
588f68a32c8SEmil Constantinescu     for (i=0; i<s; i++) {
589f68a32c8SEmil Constantinescu       b[i]  += h * B[i*pinterp+j] * tt;
590e4dd521cSBarry Smith     }
591f68a32c8SEmil Constantinescu   }
592f68a32c8SEmil Constantinescu   ierr = VecCopy(rk->Y[0],X);CHKERRQ(ierr);
593f68a32c8SEmil Constantinescu   ierr = VecMAXPY(X,s,b,rk->YdotRHS);CHKERRQ(ierr);
594f68a32c8SEmil Constantinescu   ierr = PetscFree(b);CHKERRQ(ierr);
595e4dd521cSBarry Smith   PetscFunctionReturn(0);
596e4dd521cSBarry Smith }
597e4dd521cSBarry Smith 
598e4dd521cSBarry Smith /*------------------------------------------------------------*/
599e4dd521cSBarry Smith #undef __FUNCT__
600277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset_RK"
601277b19d0SLisandro Dalcin static PetscErrorCode TSReset_RK(TS ts)
602e4dd521cSBarry Smith {
6035f70b478SJed Brown   TS_RK         *rk = (TS_RK*)ts->data;
604f68a32c8SEmil Constantinescu   PetscInt       s;
6056849ba73SBarry Smith   PetscErrorCode ierr;
606e4dd521cSBarry Smith 
607e4dd521cSBarry Smith   PetscFunctionBegin;
608f68a32c8SEmil Constantinescu   if (!rk->tableau) PetscFunctionReturn(0);
609f68a32c8SEmil Constantinescu   s    = rk->tableau->s;
610f68a32c8SEmil Constantinescu   ierr = VecDestroyVecs(s,&rk->Y);CHKERRQ(ierr);
611f68a32c8SEmil Constantinescu   ierr = VecDestroyVecs(s,&rk->YdotRHS);CHKERRQ(ierr);
612b18ea86cSHong Zhang   ierr = VecDestroyVecs(s*ts->numberadjs,&rk->VecDeltaLam);CHKERRQ(ierr);
613ad8e2604SHong Zhang   ierr = VecDestroyVecs(s*ts->numberadjs,&rk->VecDeltaMu);CHKERRQ(ierr);
614b18ea86cSHong Zhang   ierr = VecDestroyVecs(ts->numberadjs,&rk->VecSensiTemp);CHKERRQ(ierr);
615f68a32c8SEmil Constantinescu   ierr = PetscFree(rk->work);CHKERRQ(ierr);
616277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
617e4dd521cSBarry Smith }
618277b19d0SLisandro Dalcin 
619277b19d0SLisandro Dalcin #undef __FUNCT__
620277b19d0SLisandro Dalcin #define __FUNCT__ "TSDestroy_RK"
621277b19d0SLisandro Dalcin static PetscErrorCode TSDestroy_RK(TS ts)
622277b19d0SLisandro Dalcin {
623277b19d0SLisandro Dalcin   PetscErrorCode ierr;
624277b19d0SLisandro Dalcin 
625277b19d0SLisandro Dalcin   PetscFunctionBegin;
626277b19d0SLisandro Dalcin   ierr = TSReset_RK(ts);CHKERRQ(ierr);
627277b19d0SLisandro Dalcin   ierr = PetscFree(ts->data);CHKERRQ(ierr);
628f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKGetType_C",NULL);CHKERRQ(ierr);
629f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKSetType_C",NULL);CHKERRQ(ierr);
630f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
631f68a32c8SEmil Constantinescu }
632f68a32c8SEmil Constantinescu 
633f68a32c8SEmil Constantinescu 
634f68a32c8SEmil Constantinescu #undef __FUNCT__
635f68a32c8SEmil Constantinescu #define __FUNCT__ "DMCoarsenHook_TSRK"
636f68a32c8SEmil Constantinescu static PetscErrorCode DMCoarsenHook_TSRK(DM fine,DM coarse,void *ctx)
637f68a32c8SEmil Constantinescu {
638f68a32c8SEmil Constantinescu   PetscFunctionBegin;
639f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
640f68a32c8SEmil Constantinescu }
641f68a32c8SEmil Constantinescu 
642f68a32c8SEmil Constantinescu #undef __FUNCT__
643f68a32c8SEmil Constantinescu #define __FUNCT__ "DMRestrictHook_TSRK"
644f68a32c8SEmil Constantinescu static PetscErrorCode DMRestrictHook_TSRK(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
645f68a32c8SEmil Constantinescu {
646f68a32c8SEmil Constantinescu   PetscFunctionBegin;
647f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
648f68a32c8SEmil Constantinescu }
649f68a32c8SEmil Constantinescu 
650f68a32c8SEmil Constantinescu 
651f68a32c8SEmil Constantinescu #undef __FUNCT__
652f68a32c8SEmil Constantinescu #define __FUNCT__ "DMSubDomainHook_TSRK"
653f68a32c8SEmil Constantinescu static PetscErrorCode DMSubDomainHook_TSRK(DM dm,DM subdm,void *ctx)
654f68a32c8SEmil Constantinescu {
655f68a32c8SEmil Constantinescu   PetscFunctionBegin;
656f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
657f68a32c8SEmil Constantinescu }
658f68a32c8SEmil Constantinescu 
659f68a32c8SEmil Constantinescu #undef __FUNCT__
660f68a32c8SEmil Constantinescu #define __FUNCT__ "DMSubDomainRestrictHook_TSRK"
661f68a32c8SEmil Constantinescu static PetscErrorCode DMSubDomainRestrictHook_TSRK(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
662f68a32c8SEmil Constantinescu {
663f68a32c8SEmil Constantinescu 
664f68a32c8SEmil Constantinescu   PetscFunctionBegin;
665f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
666f68a32c8SEmil Constantinescu }
667c235aa8dSHong Zhang /*
668f68a32c8SEmil Constantinescu #undef __FUNCT__
669d2daff3dSHong Zhang #define __FUNCT__ "RKSetAdjCoe"
670d2daff3dSHong Zhang static PetscErrorCode RKSetAdjCoe(RKTableau tab)
671d2daff3dSHong Zhang {
672d2daff3dSHong Zhang   PetscReal *A,*b;
673d2daff3dSHong Zhang   PetscInt        s,i,j;
674d2daff3dSHong Zhang   PetscErrorCode  ierr;
675d2daff3dSHong Zhang 
676d2daff3dSHong Zhang   PetscFunctionBegin;
677d2daff3dSHong Zhang   s    = tab->s;
678d2daff3dSHong Zhang   ierr = PetscMalloc2(s*s,&A,s,&b);CHKERRQ(ierr);
679d2daff3dSHong Zhang 
680d2daff3dSHong Zhang   for (i=0; i<s; i++)
681d2daff3dSHong Zhang     for (j=0; j<s; j++) {
682d2daff3dSHong Zhang       A[i*s+j] = (tab->b[s-1-i]==0) ? 0: -tab->A[s-1-i+(s-1-j)*s] * tab->b[s-1-j] / tab->b[s-1-i];
683d2daff3dSHong Zhang       ierr = PetscPrintf(PETSC_COMM_WORLD,"Coefficients: A[%D][%D]=%.6f\n",i,j,A[i*s+j]);CHKERRQ(ierr);
684d2daff3dSHong Zhang     }
685d2daff3dSHong Zhang 
686d2daff3dSHong Zhang   for (i=0; i<s; i++) b[i] = (tab->b[s-1-i]==0)? 0: -tab->b[s-1-i];
687d2daff3dSHong Zhang 
688d2daff3dSHong Zhang   ierr  = PetscMemcpy(tab->A,A,s*s*sizeof(A[0]));CHKERRQ(ierr);
689d2daff3dSHong Zhang   ierr  = PetscMemcpy(tab->b,b,s*sizeof(b[0]));CHKERRQ(ierr);
690d2daff3dSHong Zhang   ierr  = PetscFree2(A,b);CHKERRQ(ierr);
691d2daff3dSHong Zhang   PetscFunctionReturn(0);
692d2daff3dSHong Zhang }
693c235aa8dSHong Zhang */
694d2daff3dSHong Zhang #undef __FUNCT__
695f68a32c8SEmil Constantinescu #define __FUNCT__ "TSSetUp_RK"
696f68a32c8SEmil Constantinescu static PetscErrorCode TSSetUp_RK(TS ts)
697f68a32c8SEmil Constantinescu {
698f68a32c8SEmil Constantinescu   TS_RK         *rk = (TS_RK*)ts->data;
699f68a32c8SEmil Constantinescu   RKTableau      tab;
700f68a32c8SEmil Constantinescu   PetscInt       s;
701f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
702f68a32c8SEmil Constantinescu   DM             dm;
703f68a32c8SEmil Constantinescu 
704f68a32c8SEmil Constantinescu   PetscFunctionBegin;
705f68a32c8SEmil Constantinescu   if (!rk->tableau) {
706f68a32c8SEmil Constantinescu     ierr = TSRKSetType(ts,TSRKDefault);CHKERRQ(ierr);
707f68a32c8SEmil Constantinescu   }
708f68a32c8SEmil Constantinescu   tab  = rk->tableau;
709f68a32c8SEmil Constantinescu   s    = tab->s;
710f68a32c8SEmil Constantinescu   ierr = VecDuplicateVecs(ts->vec_sol,s,&rk->Y);CHKERRQ(ierr);
711f68a32c8SEmil Constantinescu   ierr = VecDuplicateVecs(ts->vec_sol,s,&rk->YdotRHS);CHKERRQ(ierr);
712785e854fSJed Brown   ierr = PetscMalloc1(s,&rk->work);CHKERRQ(ierr);
713f68a32c8SEmil Constantinescu   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
714f68a32c8SEmil Constantinescu   if (dm) {
715f68a32c8SEmil Constantinescu     ierr = DMCoarsenHookAdd(dm,DMCoarsenHook_TSRK,DMRestrictHook_TSRK,ts);CHKERRQ(ierr);
716f68a32c8SEmil Constantinescu     ierr = DMSubDomainHookAdd(dm,DMSubDomainHook_TSRK,DMSubDomainRestrictHook_TSRK,ts);CHKERRQ(ierr);
717f68a32c8SEmil Constantinescu   }
718e4dd521cSBarry Smith   PetscFunctionReturn(0);
719e4dd521cSBarry Smith }
720d2daff3dSHong Zhang 
721f6a906c0SBarry Smith 
722e4dd521cSBarry Smith /*------------------------------------------------------------*/
723e4dd521cSBarry Smith 
724e4dd521cSBarry Smith #undef __FUNCT__
7255f70b478SJed Brown #define __FUNCT__ "TSSetFromOptions_RK"
7268c34d3f5SBarry Smith static PetscErrorCode TSSetFromOptions_RK(PetscOptions *PetscOptionsObject,TS ts)
727e4dd521cSBarry Smith {
728dfbe8321SBarry Smith   PetscErrorCode ierr;
729f68a32c8SEmil Constantinescu   char           rktype[256];
730262ff7bbSBarry Smith 
731e4dd521cSBarry Smith   PetscFunctionBegin;
732e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"RK ODE solver options");CHKERRQ(ierr);
733f68a32c8SEmil Constantinescu   {
734f68a32c8SEmil Constantinescu     RKTableauLink  link;
735f68a32c8SEmil Constantinescu     PetscInt       count,choice;
736f68a32c8SEmil Constantinescu     PetscBool      flg;
737f68a32c8SEmil Constantinescu     const char   **namelist;
738f68a32c8SEmil Constantinescu     ierr = PetscStrncpy(rktype,TSRKDefault,sizeof(rktype));CHKERRQ(ierr);
739f68a32c8SEmil Constantinescu     for (link=RKTableauList,count=0; link; link=link->next,count++) ;
740785e854fSJed Brown     ierr = PetscMalloc1(count,&namelist);CHKERRQ(ierr);
741f68a32c8SEmil Constantinescu     for (link=RKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
742f68a32c8SEmil Constantinescu     ierr      = PetscOptionsEList("-ts_rk_type","Family of RK method","TSRKSetType",(const char*const*)namelist,count,rktype,&choice,&flg);CHKERRQ(ierr);
743f68a32c8SEmil Constantinescu     ierr      = TSRKSetType(ts,flg ? namelist[choice] : rktype);CHKERRQ(ierr);
744f68a32c8SEmil Constantinescu     ierr      = PetscFree(namelist);CHKERRQ(ierr);
745f68a32c8SEmil Constantinescu   }
746262ff7bbSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
747e4dd521cSBarry Smith   PetscFunctionReturn(0);
748e4dd521cSBarry Smith }
749e4dd521cSBarry Smith 
750e4dd521cSBarry Smith #undef __FUNCT__
751f68a32c8SEmil Constantinescu #define __FUNCT__ "PetscFormatRealArray"
752f68a32c8SEmil Constantinescu static PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[])
753f68a32c8SEmil Constantinescu {
754f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
755f68a32c8SEmil Constantinescu   PetscInt       i;
756f68a32c8SEmil Constantinescu   size_t         left,count;
757f68a32c8SEmil Constantinescu   char           *p;
758f68a32c8SEmil Constantinescu 
759f68a32c8SEmil Constantinescu   PetscFunctionBegin;
760f68a32c8SEmil Constantinescu   for (i=0,p=buf,left=len; i<n; i++) {
761f68a32c8SEmil Constantinescu     ierr = PetscSNPrintfCount(p,left,fmt,&count,x[i]);CHKERRQ(ierr);
762f68a32c8SEmil Constantinescu     if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer");
763f68a32c8SEmil Constantinescu     left -= count;
764f68a32c8SEmil Constantinescu     p    += count;
765f68a32c8SEmil Constantinescu     *p++  = ' ';
766f68a32c8SEmil Constantinescu   }
767f68a32c8SEmil Constantinescu   p[i ? 0 : -1] = 0;
768f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
769f68a32c8SEmil Constantinescu }
770f68a32c8SEmil Constantinescu 
771f68a32c8SEmil Constantinescu #undef __FUNCT__
7725f70b478SJed Brown #define __FUNCT__ "TSView_RK"
7735f70b478SJed Brown static PetscErrorCode TSView_RK(TS ts,PetscViewer viewer)
774e4dd521cSBarry Smith {
7755f70b478SJed Brown   TS_RK         *rk   = (TS_RK*)ts->data;
776f68a32c8SEmil Constantinescu   RKTableau      tab  = rk->tableau;
7778370ee3bSLisandro Dalcin   PetscBool      iascii;
778dfbe8321SBarry Smith   PetscErrorCode ierr;
779f68a32c8SEmil Constantinescu   TSAdapt        adapt;
7802cdf8120Sasbjorn 
781e4dd521cSBarry Smith   PetscFunctionBegin;
782251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7838370ee3bSLisandro Dalcin   if (iascii) {
784f68a32c8SEmil Constantinescu     TSRKType rktype;
785f68a32c8SEmil Constantinescu     char     buf[512];
786f68a32c8SEmil Constantinescu     ierr = TSRKGetType(ts,&rktype);CHKERRQ(ierr);
787f68a32c8SEmil Constantinescu     ierr = PetscViewerASCIIPrintf(viewer,"  RK %s\n",rktype);CHKERRQ(ierr);
788f68a32c8SEmil Constantinescu     ierr = PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->c);CHKERRQ(ierr);
789f68a32c8SEmil Constantinescu     ierr = PetscViewerASCIIPrintf(viewer,"  Abscissa     c = %s\n",buf);CHKERRQ(ierr);
790d760c35bSDebojyoti Ghosh     ierr = PetscViewerASCIIPrintf(viewer,"FSAL: %s\n",tab->FSAL ? "yes" : "no");CHKERRQ(ierr);
7918370ee3bSLisandro Dalcin   }
792f68a32c8SEmil Constantinescu   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
793f68a32c8SEmil Constantinescu   ierr = TSAdaptView(adapt,viewer);CHKERRQ(ierr);
794f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
795f68a32c8SEmil Constantinescu }
796f68a32c8SEmil Constantinescu 
797f68a32c8SEmil Constantinescu #undef __FUNCT__
798f68a32c8SEmil Constantinescu #define __FUNCT__ "TSLoad_RK"
799f68a32c8SEmil Constantinescu static PetscErrorCode TSLoad_RK(TS ts,PetscViewer viewer)
800f68a32c8SEmil Constantinescu {
801f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
802f68a32c8SEmil Constantinescu   TSAdapt        tsadapt;
803f68a32c8SEmil Constantinescu 
804f68a32c8SEmil Constantinescu   PetscFunctionBegin;
805f68a32c8SEmil Constantinescu   ierr = TSGetAdapt(ts,&tsadapt);CHKERRQ(ierr);
806f68a32c8SEmil Constantinescu   ierr = TSAdaptLoad(tsadapt,viewer);CHKERRQ(ierr);
807f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
808f68a32c8SEmil Constantinescu }
809f68a32c8SEmil Constantinescu 
810f68a32c8SEmil Constantinescu #undef __FUNCT__
811f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKSetType"
812f68a32c8SEmil Constantinescu /*@C
813f68a32c8SEmil Constantinescu   TSRKSetType - Set the type of RK scheme
814f68a32c8SEmil Constantinescu 
815f68a32c8SEmil Constantinescu   Logically collective
816f68a32c8SEmil Constantinescu 
817f68a32c8SEmil Constantinescu   Input Parameter:
818f68a32c8SEmil Constantinescu +  ts - timestepping context
819f68a32c8SEmil Constantinescu -  rktype - type of RK-scheme
820f68a32c8SEmil Constantinescu 
821f68a32c8SEmil Constantinescu   Level: intermediate
822f68a32c8SEmil Constantinescu 
823f68a32c8SEmil Constantinescu .seealso: TSRKGetType(), TSRK, TSRK2, TSRK3, TSRKPRSSP2, TSRK4, TSRK5
824f68a32c8SEmil Constantinescu @*/
825f68a32c8SEmil Constantinescu PetscErrorCode TSRKSetType(TS ts,TSRKType rktype)
826f68a32c8SEmil Constantinescu {
827f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
828f68a32c8SEmil Constantinescu 
829f68a32c8SEmil Constantinescu   PetscFunctionBegin;
830f68a32c8SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
831f68a32c8SEmil Constantinescu   ierr = PetscTryMethod(ts,"TSRKSetType_C",(TS,TSRKType),(ts,rktype));CHKERRQ(ierr);
832f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
833f68a32c8SEmil Constantinescu }
834f68a32c8SEmil Constantinescu 
835f68a32c8SEmil Constantinescu #undef __FUNCT__
836f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKGetType"
837f68a32c8SEmil Constantinescu /*@C
838f68a32c8SEmil Constantinescu   TSRKGetType - Get the type of RK scheme
839f68a32c8SEmil Constantinescu 
840f68a32c8SEmil Constantinescu   Logically collective
841f68a32c8SEmil Constantinescu 
842f68a32c8SEmil Constantinescu   Input Parameter:
843f68a32c8SEmil Constantinescu .  ts - timestepping context
844f68a32c8SEmil Constantinescu 
845f68a32c8SEmil Constantinescu   Output Parameter:
846f68a32c8SEmil Constantinescu .  rktype - type of RK-scheme
847f68a32c8SEmil Constantinescu 
848f68a32c8SEmil Constantinescu   Level: intermediate
849f68a32c8SEmil Constantinescu 
850f68a32c8SEmil Constantinescu .seealso: TSRKGetType()
851f68a32c8SEmil Constantinescu @*/
852f68a32c8SEmil Constantinescu PetscErrorCode TSRKGetType(TS ts,TSRKType *rktype)
853f68a32c8SEmil Constantinescu {
854f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
855f68a32c8SEmil Constantinescu 
856f68a32c8SEmil Constantinescu   PetscFunctionBegin;
857f68a32c8SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
858f68a32c8SEmil Constantinescu   ierr = PetscUseMethod(ts,"TSRKGetType_C",(TS,TSRKType*),(ts,rktype));CHKERRQ(ierr);
859f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
860f68a32c8SEmil Constantinescu }
861f68a32c8SEmil Constantinescu 
862f68a32c8SEmil Constantinescu #undef __FUNCT__
863f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKGetType_RK"
864f68a32c8SEmil Constantinescu PetscErrorCode  TSRKGetType_RK(TS ts,TSRKType *rktype)
865f68a32c8SEmil Constantinescu {
866f68a32c8SEmil Constantinescu   TS_RK     *rk = (TS_RK*)ts->data;
867f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
868f68a32c8SEmil Constantinescu 
869f68a32c8SEmil Constantinescu   PetscFunctionBegin;
870f68a32c8SEmil Constantinescu   if (!rk->tableau) {
871f68a32c8SEmil Constantinescu     ierr = TSRKSetType(ts,TSRKDefault);CHKERRQ(ierr);
872f68a32c8SEmil Constantinescu   }
873f68a32c8SEmil Constantinescu   *rktype = rk->tableau->name;
874f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
875f68a32c8SEmil Constantinescu }
876f68a32c8SEmil Constantinescu #undef __FUNCT__
877f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKSetType_RK"
878f68a32c8SEmil Constantinescu PetscErrorCode  TSRKSetType_RK(TS ts,TSRKType rktype)
879f68a32c8SEmil Constantinescu {
880f68a32c8SEmil Constantinescu   TS_RK     *rk = (TS_RK*)ts->data;
881f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
882f68a32c8SEmil Constantinescu   PetscBool      match;
883f68a32c8SEmil Constantinescu   RKTableauLink link;
884f68a32c8SEmil Constantinescu 
885f68a32c8SEmil Constantinescu   PetscFunctionBegin;
886f68a32c8SEmil Constantinescu   if (rk->tableau) {
887f68a32c8SEmil Constantinescu     ierr = PetscStrcmp(rk->tableau->name,rktype,&match);CHKERRQ(ierr);
888f68a32c8SEmil Constantinescu     if (match) PetscFunctionReturn(0);
889f68a32c8SEmil Constantinescu   }
890f68a32c8SEmil Constantinescu   for (link = RKTableauList; link; link=link->next) {
891f68a32c8SEmil Constantinescu     ierr = PetscStrcmp(link->tab.name,rktype,&match);CHKERRQ(ierr);
892f68a32c8SEmil Constantinescu     if (match) {
893f68a32c8SEmil Constantinescu       ierr = TSReset_RK(ts);CHKERRQ(ierr);
894f68a32c8SEmil Constantinescu       rk->tableau = &link->tab;
895f68a32c8SEmil Constantinescu       PetscFunctionReturn(0);
896f68a32c8SEmil Constantinescu     }
897f68a32c8SEmil Constantinescu   }
898f68a32c8SEmil Constantinescu   SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",rktype);
899e4dd521cSBarry Smith   PetscFunctionReturn(0);
900e4dd521cSBarry Smith }
901e4dd521cSBarry Smith 
902ff22ae23SHong Zhang #undef __FUNCT__
903ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages_RK"
904ff22ae23SHong Zhang static PetscErrorCode  TSGetStages_RK(TS ts,PetscInt *ns,Vec **Y)
905ff22ae23SHong Zhang {
906ff22ae23SHong Zhang   TS_RK          *rk = (TS_RK*)ts->data;
907ff22ae23SHong Zhang 
908ff22ae23SHong Zhang   PetscFunctionBegin;
909ff22ae23SHong Zhang   *ns = rk->tableau->s;
910d2daff3dSHong Zhang   if(Y) *Y  = rk->Y;
911ff22ae23SHong Zhang   PetscFunctionReturn(0);
912ff22ae23SHong Zhang }
913ff22ae23SHong Zhang 
914ff22ae23SHong Zhang 
915e4dd521cSBarry Smith /* ------------------------------------------------------------ */
916ebe3b25bSBarry Smith /*MC
917f68a32c8SEmil Constantinescu       TSRK - ODE and DAE solver using Runge-Kutta schemes
918ebe3b25bSBarry Smith 
919f68a32c8SEmil Constantinescu   The user should provide the right hand side of the equation
920f68a32c8SEmil Constantinescu   using TSSetRHSFunction().
921ebe3b25bSBarry Smith 
922f68a32c8SEmil Constantinescu   Notes:
923f68a32c8SEmil Constantinescu   The default is TSRK3, it can be changed with TSRKSetType() or -ts_rk_type
924ebe3b25bSBarry Smith 
925d41222bbSBarry Smith   Level: beginner
926d41222bbSBarry Smith 
927f68a32c8SEmil Constantinescu .seealso:  TSCreate(), TS, TSSetType(), TSRKSetType(), TSRKGetType(), TSRKSetFullyImplicit(), TSRK2D, TTSRK2E, TSRK3,
928f68a32c8SEmil Constantinescu            TSRK4, TSRK5, TSRKPRSSP2, TSRKBPR3, TSRKType, TSRKRegister()
929ebe3b25bSBarry Smith 
930ebe3b25bSBarry Smith M*/
931e4dd521cSBarry Smith #undef __FUNCT__
9325f70b478SJed Brown #define __FUNCT__ "TSCreate_RK"
9338cc058d9SJed Brown PETSC_EXTERN PetscErrorCode TSCreate_RK(TS ts)
934e4dd521cSBarry Smith {
935f68a32c8SEmil Constantinescu   TS_RK     *th;
936dfbe8321SBarry Smith   PetscErrorCode ierr;
937e4dd521cSBarry Smith 
938e4dd521cSBarry Smith   PetscFunctionBegin;
939f68a32c8SEmil Constantinescu #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
940f68a32c8SEmil Constantinescu   ierr = TSRKInitializePackage();CHKERRQ(ierr);
941f68a32c8SEmil Constantinescu #endif
942f68a32c8SEmil Constantinescu 
943f68a32c8SEmil Constantinescu   ts->ops->reset          = TSReset_RK;
9445f70b478SJed Brown   ts->ops->destroy        = TSDestroy_RK;
9455f70b478SJed Brown   ts->ops->view           = TSView_RK;
946f68a32c8SEmil Constantinescu   ts->ops->load           = TSLoad_RK;
947f68a32c8SEmil Constantinescu   ts->ops->setup          = TSSetUp_RK;
94842f2b339SBarry Smith   ts->ops->adjointsetup   = TSAdjointSetUp_RK;
949f68a32c8SEmil Constantinescu   ts->ops->step           = TSStep_RK;
950f68a32c8SEmil Constantinescu   ts->ops->interpolate    = TSInterpolate_RK;
951f68a32c8SEmil Constantinescu   ts->ops->evaluatestep   = TSEvaluateStep_RK;
952f68a32c8SEmil Constantinescu   ts->ops->setfromoptions = TSSetFromOptions_RK;
953ff22ae23SHong Zhang   ts->ops->getstages      = TSGetStages_RK;
95442f2b339SBarry Smith   ts->ops->adjointstep    = TSAdjointStep_RK;
955e4dd521cSBarry Smith 
956b00a9115SJed Brown   ierr = PetscNewLog(ts,&th);CHKERRQ(ierr);
957f68a32c8SEmil Constantinescu   ts->data = (void*)th;
958e4dd521cSBarry Smith 
959f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKGetType_C",TSRKGetType_RK);CHKERRQ(ierr);
960f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKSetType_C",TSRKSetType_RK);CHKERRQ(ierr);
961e4dd521cSBarry Smith   PetscFunctionReturn(0);
962e4dd521cSBarry Smith }
963