xref: /petsc/src/ts/impls/explicit/rk/rk.c (revision cb9d8021f64c572d262e10034bbd2fb15469a2b9)
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);
443*cb9d8021SPierre Barbier de Reuille       ierr = TSAdaptCheckStage(adapt,ts,rk->stage_time,i,Y,&accept);CHKERRQ(ierr);
444*cb9d8021SPierre Barbier de Reuille       if (!accept) break;
445f68a32c8SEmil Constantinescu       ierr = TSComputeRHSFunction(ts,t+h*c[i],Y[i],YdotRHS[i]);CHKERRQ(ierr);
446f68a32c8SEmil Constantinescu     }
447*cb9d8021SPierre Barbier de Reuille     if(!accept) continue;
448f68a32c8SEmil Constantinescu     ierr = TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);CHKERRQ(ierr);
449f68a32c8SEmil Constantinescu     rk->status = TS_STEP_PENDING;
450f68a32c8SEmil Constantinescu 
451f68a32c8SEmil Constantinescu     /* Register only the current method as a candidate because we're not supporting multiple candidates yet. */
452f68a32c8SEmil Constantinescu     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
453f68a32c8SEmil Constantinescu     ierr = TSAdaptCandidatesClear(adapt);CHKERRQ(ierr);
454f68a32c8SEmil Constantinescu     ierr = TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,1.*tab->s,PETSC_TRUE);CHKERRQ(ierr);
455f68a32c8SEmil Constantinescu     ierr = TSAdaptChoose(adapt,ts,ts->time_step,&next_scheme,&next_time_step,&accept);CHKERRQ(ierr);
456f68a32c8SEmil Constantinescu     if (accept) {
457f68a32c8SEmil Constantinescu       /* ignore next_scheme for now */
458f68a32c8SEmil Constantinescu       ts->ptime    += ts->time_step;
459f68a32c8SEmil Constantinescu       ts->time_step = next_time_step;
460e3caeda6SBarry Smith       ts->steps++;
461f68a32c8SEmil Constantinescu       rk->status = TS_STEP_COMPLETE;
462f68a32c8SEmil Constantinescu       ierr = PetscObjectComposedDataSetReal((PetscObject)ts->vec_sol,explicit_stage_time_id,ts->ptime);CHKERRQ(ierr);
463f68a32c8SEmil Constantinescu       break;
464f68a32c8SEmil Constantinescu     } else {                    /* Roll back the current step */
465f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = -h*b[j];
466f68a32c8SEmil Constantinescu       ierr = VecMAXPY(ts->vec_sol,s,w,rk->YdotRHS);CHKERRQ(ierr);
467f68a32c8SEmil Constantinescu       ts->time_step = next_time_step;
468f68a32c8SEmil Constantinescu       rk->status   = TS_STEP_INCOMPLETE;
469f68a32c8SEmil Constantinescu     }
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;
488abc2977eSBarry Smith   ierr = VecDuplicateVecs(ts->vecs_sensi[0],s*ts->numcost,&rk->VecDeltaLam);CHKERRQ(ierr);
489f6a906c0SBarry Smith   if(ts->vecs_sensip) {
490abc2977eSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensip[0],s*ts->numcost,&rk->VecDeltaMu);CHKERRQ(ierr);
491f6a906c0SBarry Smith   }
492abc2977eSBarry Smith   ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&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;
509cef76868SBarry Smith   PetscReal        h = ts->time_step;
510cef76868SBarry Smith   Mat              J,Jp;
511c235aa8dSHong Zhang 
512d2daff3dSHong Zhang   PetscFunctionBegin;
513c235aa8dSHong Zhang   t          = ts->ptime;
514c235aa8dSHong Zhang   rk->status = TS_STEP_INCOMPLETE;
515cef76868SBarry 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]);
519abc2977eSBarry Smith     for (nadj=0; nadj<ts->numcost; 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);
529abc2977eSBarry Smith     for (nadj=0; nadj<ts->numcost; 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);
536abc2977eSBarry Smith       for (nadj=0; nadj<ts->numcost; 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;
543abc2977eSBarry Smith   for (nadj=0; nadj<ts->numcost; 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;
552d2daff3dSHong Zhang   PetscFunctionReturn(0);
553d2daff3dSHong Zhang }
554d2daff3dSHong Zhang 
555d2daff3dSHong Zhang #undef __FUNCT__
556f68a32c8SEmil Constantinescu #define __FUNCT__ "TSInterpolate_RK"
557f68a32c8SEmil Constantinescu static PetscErrorCode TSInterpolate_RK(TS ts,PetscReal itime,Vec X)
558f68a32c8SEmil Constantinescu {
559f68a32c8SEmil Constantinescu   TS_RK           *rk = (TS_RK*)ts->data;
560f68a32c8SEmil Constantinescu   PetscInt         s  = rk->tableau->s,pinterp = rk->tableau->pinterp,i,j;
561f68a32c8SEmil Constantinescu   PetscReal        h;
562f68a32c8SEmil Constantinescu   PetscReal        tt,t;
563f68a32c8SEmil Constantinescu   PetscScalar     *b;
564f68a32c8SEmil Constantinescu   const PetscReal *B = rk->tableau->binterp;
565f68a32c8SEmil Constantinescu   PetscErrorCode   ierr;
566e4dd521cSBarry Smith 
567f68a32c8SEmil Constantinescu   PetscFunctionBegin;
568f68a32c8SEmil Constantinescu   if (!B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRK %s does not have an interpolation formula",rk->tableau->name);
569f68a32c8SEmil Constantinescu   switch (rk->status) {
570f68a32c8SEmil Constantinescu   case TS_STEP_INCOMPLETE:
571f68a32c8SEmil Constantinescu   case TS_STEP_PENDING:
572f68a32c8SEmil Constantinescu     h = ts->time_step;
573f68a32c8SEmil Constantinescu     t = (itime - ts->ptime)/h;
574f68a32c8SEmil Constantinescu     break;
575f68a32c8SEmil Constantinescu   case TS_STEP_COMPLETE:
576f68a32c8SEmil Constantinescu     h = ts->time_step_prev;
577f68a32c8SEmil Constantinescu     t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
578f68a32c8SEmil Constantinescu     break;
579f68a32c8SEmil Constantinescu   default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
580e4dd521cSBarry Smith   }
581785e854fSJed Brown   ierr = PetscMalloc1(s,&b);CHKERRQ(ierr);
582f68a32c8SEmil Constantinescu   for (i=0; i<s; i++) b[i] = 0;
583f68a32c8SEmil Constantinescu   for (j=0,tt=t; j<pinterp; j++,tt*=t) {
584f68a32c8SEmil Constantinescu     for (i=0; i<s; i++) {
585f68a32c8SEmil Constantinescu       b[i]  += h * B[i*pinterp+j] * tt;
586e4dd521cSBarry Smith     }
587f68a32c8SEmil Constantinescu   }
588f68a32c8SEmil Constantinescu   ierr = VecCopy(rk->Y[0],X);CHKERRQ(ierr);
589f68a32c8SEmil Constantinescu   ierr = VecMAXPY(X,s,b,rk->YdotRHS);CHKERRQ(ierr);
590f68a32c8SEmil Constantinescu   ierr = PetscFree(b);CHKERRQ(ierr);
591e4dd521cSBarry Smith   PetscFunctionReturn(0);
592e4dd521cSBarry Smith }
593e4dd521cSBarry Smith 
594e4dd521cSBarry Smith /*------------------------------------------------------------*/
595e4dd521cSBarry Smith #undef __FUNCT__
596277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset_RK"
597277b19d0SLisandro Dalcin static PetscErrorCode TSReset_RK(TS ts)
598e4dd521cSBarry Smith {
5995f70b478SJed Brown   TS_RK         *rk = (TS_RK*)ts->data;
600f68a32c8SEmil Constantinescu   PetscInt       s;
6016849ba73SBarry Smith   PetscErrorCode ierr;
602e4dd521cSBarry Smith 
603e4dd521cSBarry Smith   PetscFunctionBegin;
604f68a32c8SEmil Constantinescu   if (!rk->tableau) PetscFunctionReturn(0);
605f68a32c8SEmil Constantinescu   s    = rk->tableau->s;
606f68a32c8SEmil Constantinescu   ierr = VecDestroyVecs(s,&rk->Y);CHKERRQ(ierr);
607f68a32c8SEmil Constantinescu   ierr = VecDestroyVecs(s,&rk->YdotRHS);CHKERRQ(ierr);
608abc2977eSBarry Smith   ierr = VecDestroyVecs(s*ts->numcost,&rk->VecDeltaLam);CHKERRQ(ierr);
609abc2977eSBarry Smith   ierr = VecDestroyVecs(s*ts->numcost,&rk->VecDeltaMu);CHKERRQ(ierr);
610abc2977eSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&rk->VecSensiTemp);CHKERRQ(ierr);
611f68a32c8SEmil Constantinescu   ierr = PetscFree(rk->work);CHKERRQ(ierr);
612277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
613e4dd521cSBarry Smith }
614277b19d0SLisandro Dalcin 
615277b19d0SLisandro Dalcin #undef __FUNCT__
616277b19d0SLisandro Dalcin #define __FUNCT__ "TSDestroy_RK"
617277b19d0SLisandro Dalcin static PetscErrorCode TSDestroy_RK(TS ts)
618277b19d0SLisandro Dalcin {
619277b19d0SLisandro Dalcin   PetscErrorCode ierr;
620277b19d0SLisandro Dalcin 
621277b19d0SLisandro Dalcin   PetscFunctionBegin;
622277b19d0SLisandro Dalcin   ierr = TSReset_RK(ts);CHKERRQ(ierr);
623277b19d0SLisandro Dalcin   ierr = PetscFree(ts->data);CHKERRQ(ierr);
624f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKGetType_C",NULL);CHKERRQ(ierr);
625f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKSetType_C",NULL);CHKERRQ(ierr);
626f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
627f68a32c8SEmil Constantinescu }
628f68a32c8SEmil Constantinescu 
629f68a32c8SEmil Constantinescu 
630f68a32c8SEmil Constantinescu #undef __FUNCT__
631f68a32c8SEmil Constantinescu #define __FUNCT__ "DMCoarsenHook_TSRK"
632f68a32c8SEmil Constantinescu static PetscErrorCode DMCoarsenHook_TSRK(DM fine,DM coarse,void *ctx)
633f68a32c8SEmil Constantinescu {
634f68a32c8SEmil Constantinescu   PetscFunctionBegin;
635f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
636f68a32c8SEmil Constantinescu }
637f68a32c8SEmil Constantinescu 
638f68a32c8SEmil Constantinescu #undef __FUNCT__
639f68a32c8SEmil Constantinescu #define __FUNCT__ "DMRestrictHook_TSRK"
640f68a32c8SEmil Constantinescu static PetscErrorCode DMRestrictHook_TSRK(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
641f68a32c8SEmil Constantinescu {
642f68a32c8SEmil Constantinescu   PetscFunctionBegin;
643f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
644f68a32c8SEmil Constantinescu }
645f68a32c8SEmil Constantinescu 
646f68a32c8SEmil Constantinescu 
647f68a32c8SEmil Constantinescu #undef __FUNCT__
648f68a32c8SEmil Constantinescu #define __FUNCT__ "DMSubDomainHook_TSRK"
649f68a32c8SEmil Constantinescu static PetscErrorCode DMSubDomainHook_TSRK(DM dm,DM subdm,void *ctx)
650f68a32c8SEmil Constantinescu {
651f68a32c8SEmil Constantinescu   PetscFunctionBegin;
652f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
653f68a32c8SEmil Constantinescu }
654f68a32c8SEmil Constantinescu 
655f68a32c8SEmil Constantinescu #undef __FUNCT__
656f68a32c8SEmil Constantinescu #define __FUNCT__ "DMSubDomainRestrictHook_TSRK"
657f68a32c8SEmil Constantinescu static PetscErrorCode DMSubDomainRestrictHook_TSRK(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
658f68a32c8SEmil Constantinescu {
659f68a32c8SEmil Constantinescu 
660f68a32c8SEmil Constantinescu   PetscFunctionBegin;
661f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
662f68a32c8SEmil Constantinescu }
663c235aa8dSHong Zhang /*
664f68a32c8SEmil Constantinescu #undef __FUNCT__
665d2daff3dSHong Zhang #define __FUNCT__ "RKSetAdjCoe"
666d2daff3dSHong Zhang static PetscErrorCode RKSetAdjCoe(RKTableau tab)
667d2daff3dSHong Zhang {
668d2daff3dSHong Zhang   PetscReal *A,*b;
669d2daff3dSHong Zhang   PetscInt        s,i,j;
670d2daff3dSHong Zhang   PetscErrorCode  ierr;
671d2daff3dSHong Zhang 
672d2daff3dSHong Zhang   PetscFunctionBegin;
673d2daff3dSHong Zhang   s    = tab->s;
674d2daff3dSHong Zhang   ierr = PetscMalloc2(s*s,&A,s,&b);CHKERRQ(ierr);
675d2daff3dSHong Zhang 
676d2daff3dSHong Zhang   for (i=0; i<s; i++)
677d2daff3dSHong Zhang     for (j=0; j<s; j++) {
678d2daff3dSHong 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];
679d2daff3dSHong Zhang       ierr = PetscPrintf(PETSC_COMM_WORLD,"Coefficients: A[%D][%D]=%.6f\n",i,j,A[i*s+j]);CHKERRQ(ierr);
680d2daff3dSHong Zhang     }
681d2daff3dSHong Zhang 
682d2daff3dSHong Zhang   for (i=0; i<s; i++) b[i] = (tab->b[s-1-i]==0)? 0: -tab->b[s-1-i];
683d2daff3dSHong Zhang 
684d2daff3dSHong Zhang   ierr  = PetscMemcpy(tab->A,A,s*s*sizeof(A[0]));CHKERRQ(ierr);
685d2daff3dSHong Zhang   ierr  = PetscMemcpy(tab->b,b,s*sizeof(b[0]));CHKERRQ(ierr);
686d2daff3dSHong Zhang   ierr  = PetscFree2(A,b);CHKERRQ(ierr);
687d2daff3dSHong Zhang   PetscFunctionReturn(0);
688d2daff3dSHong Zhang }
689c235aa8dSHong Zhang */
690d2daff3dSHong Zhang #undef __FUNCT__
691f68a32c8SEmil Constantinescu #define __FUNCT__ "TSSetUp_RK"
692f68a32c8SEmil Constantinescu static PetscErrorCode TSSetUp_RK(TS ts)
693f68a32c8SEmil Constantinescu {
694f68a32c8SEmil Constantinescu   TS_RK         *rk = (TS_RK*)ts->data;
695f68a32c8SEmil Constantinescu   RKTableau      tab;
696f68a32c8SEmil Constantinescu   PetscInt       s;
697f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
698f68a32c8SEmil Constantinescu   DM             dm;
699f68a32c8SEmil Constantinescu 
700f68a32c8SEmil Constantinescu   PetscFunctionBegin;
701f68a32c8SEmil Constantinescu   if (!rk->tableau) {
702f68a32c8SEmil Constantinescu     ierr = TSRKSetType(ts,TSRKDefault);CHKERRQ(ierr);
703f68a32c8SEmil Constantinescu   }
704f68a32c8SEmil Constantinescu   tab  = rk->tableau;
705f68a32c8SEmil Constantinescu   s    = tab->s;
706f68a32c8SEmil Constantinescu   ierr = VecDuplicateVecs(ts->vec_sol,s,&rk->Y);CHKERRQ(ierr);
707f68a32c8SEmil Constantinescu   ierr = VecDuplicateVecs(ts->vec_sol,s,&rk->YdotRHS);CHKERRQ(ierr);
708785e854fSJed Brown   ierr = PetscMalloc1(s,&rk->work);CHKERRQ(ierr);
709f68a32c8SEmil Constantinescu   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
710f68a32c8SEmil Constantinescu   if (dm) {
711f68a32c8SEmil Constantinescu     ierr = DMCoarsenHookAdd(dm,DMCoarsenHook_TSRK,DMRestrictHook_TSRK,ts);CHKERRQ(ierr);
712f68a32c8SEmil Constantinescu     ierr = DMSubDomainHookAdd(dm,DMSubDomainHook_TSRK,DMSubDomainRestrictHook_TSRK,ts);CHKERRQ(ierr);
713f68a32c8SEmil Constantinescu   }
714e4dd521cSBarry Smith   PetscFunctionReturn(0);
715e4dd521cSBarry Smith }
716d2daff3dSHong Zhang 
717f6a906c0SBarry Smith 
718e4dd521cSBarry Smith /*------------------------------------------------------------*/
719e4dd521cSBarry Smith 
720e4dd521cSBarry Smith #undef __FUNCT__
7215f70b478SJed Brown #define __FUNCT__ "TSSetFromOptions_RK"
7228c34d3f5SBarry Smith static PetscErrorCode TSSetFromOptions_RK(PetscOptions *PetscOptionsObject,TS ts)
723e4dd521cSBarry Smith {
724dfbe8321SBarry Smith   PetscErrorCode ierr;
725f68a32c8SEmil Constantinescu   char           rktype[256];
726262ff7bbSBarry Smith 
727e4dd521cSBarry Smith   PetscFunctionBegin;
728e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"RK ODE solver options");CHKERRQ(ierr);
729f68a32c8SEmil Constantinescu   {
730f68a32c8SEmil Constantinescu     RKTableauLink  link;
731f68a32c8SEmil Constantinescu     PetscInt       count,choice;
732f68a32c8SEmil Constantinescu     PetscBool      flg;
733f68a32c8SEmil Constantinescu     const char   **namelist;
734f68a32c8SEmil Constantinescu     ierr = PetscStrncpy(rktype,TSRKDefault,sizeof(rktype));CHKERRQ(ierr);
735f68a32c8SEmil Constantinescu     for (link=RKTableauList,count=0; link; link=link->next,count++) ;
736785e854fSJed Brown     ierr = PetscMalloc1(count,&namelist);CHKERRQ(ierr);
737f68a32c8SEmil Constantinescu     for (link=RKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
738f68a32c8SEmil Constantinescu     ierr      = PetscOptionsEList("-ts_rk_type","Family of RK method","TSRKSetType",(const char*const*)namelist,count,rktype,&choice,&flg);CHKERRQ(ierr);
739f68a32c8SEmil Constantinescu     ierr      = TSRKSetType(ts,flg ? namelist[choice] : rktype);CHKERRQ(ierr);
740f68a32c8SEmil Constantinescu     ierr      = PetscFree(namelist);CHKERRQ(ierr);
741f68a32c8SEmil Constantinescu   }
742262ff7bbSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
743e4dd521cSBarry Smith   PetscFunctionReturn(0);
744e4dd521cSBarry Smith }
745e4dd521cSBarry Smith 
746e4dd521cSBarry Smith #undef __FUNCT__
747f68a32c8SEmil Constantinescu #define __FUNCT__ "PetscFormatRealArray"
748f68a32c8SEmil Constantinescu static PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[])
749f68a32c8SEmil Constantinescu {
750f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
751f68a32c8SEmil Constantinescu   PetscInt       i;
752f68a32c8SEmil Constantinescu   size_t         left,count;
753f68a32c8SEmil Constantinescu   char           *p;
754f68a32c8SEmil Constantinescu 
755f68a32c8SEmil Constantinescu   PetscFunctionBegin;
756f68a32c8SEmil Constantinescu   for (i=0,p=buf,left=len; i<n; i++) {
757f68a32c8SEmil Constantinescu     ierr = PetscSNPrintfCount(p,left,fmt,&count,x[i]);CHKERRQ(ierr);
758f68a32c8SEmil Constantinescu     if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer");
759f68a32c8SEmil Constantinescu     left -= count;
760f68a32c8SEmil Constantinescu     p    += count;
761f68a32c8SEmil Constantinescu     *p++  = ' ';
762f68a32c8SEmil Constantinescu   }
763f68a32c8SEmil Constantinescu   p[i ? 0 : -1] = 0;
764f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
765f68a32c8SEmil Constantinescu }
766f68a32c8SEmil Constantinescu 
767f68a32c8SEmil Constantinescu #undef __FUNCT__
7685f70b478SJed Brown #define __FUNCT__ "TSView_RK"
7695f70b478SJed Brown static PetscErrorCode TSView_RK(TS ts,PetscViewer viewer)
770e4dd521cSBarry Smith {
7715f70b478SJed Brown   TS_RK         *rk   = (TS_RK*)ts->data;
772f68a32c8SEmil Constantinescu   RKTableau      tab  = rk->tableau;
7738370ee3bSLisandro Dalcin   PetscBool      iascii;
774dfbe8321SBarry Smith   PetscErrorCode ierr;
775f68a32c8SEmil Constantinescu   TSAdapt        adapt;
7762cdf8120Sasbjorn 
777e4dd521cSBarry Smith   PetscFunctionBegin;
778251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7798370ee3bSLisandro Dalcin   if (iascii) {
780f68a32c8SEmil Constantinescu     TSRKType rktype;
781f68a32c8SEmil Constantinescu     char     buf[512];
782f68a32c8SEmil Constantinescu     ierr = TSRKGetType(ts,&rktype);CHKERRQ(ierr);
783f68a32c8SEmil Constantinescu     ierr = PetscViewerASCIIPrintf(viewer,"  RK %s\n",rktype);CHKERRQ(ierr);
784f68a32c8SEmil Constantinescu     ierr = PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->c);CHKERRQ(ierr);
785f68a32c8SEmil Constantinescu     ierr = PetscViewerASCIIPrintf(viewer,"  Abscissa     c = %s\n",buf);CHKERRQ(ierr);
786d760c35bSDebojyoti Ghosh     ierr = PetscViewerASCIIPrintf(viewer,"FSAL: %s\n",tab->FSAL ? "yes" : "no");CHKERRQ(ierr);
7878370ee3bSLisandro Dalcin   }
788f68a32c8SEmil Constantinescu   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
789f68a32c8SEmil Constantinescu   ierr = TSAdaptView(adapt,viewer);CHKERRQ(ierr);
790f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
791f68a32c8SEmil Constantinescu }
792f68a32c8SEmil Constantinescu 
793f68a32c8SEmil Constantinescu #undef __FUNCT__
794f68a32c8SEmil Constantinescu #define __FUNCT__ "TSLoad_RK"
795f68a32c8SEmil Constantinescu static PetscErrorCode TSLoad_RK(TS ts,PetscViewer viewer)
796f68a32c8SEmil Constantinescu {
797f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
798f68a32c8SEmil Constantinescu   TSAdapt        tsadapt;
799f68a32c8SEmil Constantinescu 
800f68a32c8SEmil Constantinescu   PetscFunctionBegin;
801f68a32c8SEmil Constantinescu   ierr = TSGetAdapt(ts,&tsadapt);CHKERRQ(ierr);
802f68a32c8SEmil Constantinescu   ierr = TSAdaptLoad(tsadapt,viewer);CHKERRQ(ierr);
803f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
804f68a32c8SEmil Constantinescu }
805f68a32c8SEmil Constantinescu 
806f68a32c8SEmil Constantinescu #undef __FUNCT__
807f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKSetType"
808f68a32c8SEmil Constantinescu /*@C
809f68a32c8SEmil Constantinescu   TSRKSetType - Set the type of RK scheme
810f68a32c8SEmil Constantinescu 
811f68a32c8SEmil Constantinescu   Logically collective
812f68a32c8SEmil Constantinescu 
813f68a32c8SEmil Constantinescu   Input Parameter:
814f68a32c8SEmil Constantinescu +  ts - timestepping context
815f68a32c8SEmil Constantinescu -  rktype - type of RK-scheme
816f68a32c8SEmil Constantinescu 
817f68a32c8SEmil Constantinescu   Level: intermediate
818f68a32c8SEmil Constantinescu 
819f68a32c8SEmil Constantinescu .seealso: TSRKGetType(), TSRK, TSRK2, TSRK3, TSRKPRSSP2, TSRK4, TSRK5
820f68a32c8SEmil Constantinescu @*/
821f68a32c8SEmil Constantinescu PetscErrorCode TSRKSetType(TS ts,TSRKType rktype)
822f68a32c8SEmil Constantinescu {
823f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
824f68a32c8SEmil Constantinescu 
825f68a32c8SEmil Constantinescu   PetscFunctionBegin;
826f68a32c8SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
827f68a32c8SEmil Constantinescu   ierr = PetscTryMethod(ts,"TSRKSetType_C",(TS,TSRKType),(ts,rktype));CHKERRQ(ierr);
828f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
829f68a32c8SEmil Constantinescu }
830f68a32c8SEmil Constantinescu 
831f68a32c8SEmil Constantinescu #undef __FUNCT__
832f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKGetType"
833f68a32c8SEmil Constantinescu /*@C
834f68a32c8SEmil Constantinescu   TSRKGetType - Get the type of RK scheme
835f68a32c8SEmil Constantinescu 
836f68a32c8SEmil Constantinescu   Logically collective
837f68a32c8SEmil Constantinescu 
838f68a32c8SEmil Constantinescu   Input Parameter:
839f68a32c8SEmil Constantinescu .  ts - timestepping context
840f68a32c8SEmil Constantinescu 
841f68a32c8SEmil Constantinescu   Output Parameter:
842f68a32c8SEmil Constantinescu .  rktype - type of RK-scheme
843f68a32c8SEmil Constantinescu 
844f68a32c8SEmil Constantinescu   Level: intermediate
845f68a32c8SEmil Constantinescu 
846f68a32c8SEmil Constantinescu .seealso: TSRKGetType()
847f68a32c8SEmil Constantinescu @*/
848f68a32c8SEmil Constantinescu PetscErrorCode TSRKGetType(TS ts,TSRKType *rktype)
849f68a32c8SEmil Constantinescu {
850f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
851f68a32c8SEmil Constantinescu 
852f68a32c8SEmil Constantinescu   PetscFunctionBegin;
853f68a32c8SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
854f68a32c8SEmil Constantinescu   ierr = PetscUseMethod(ts,"TSRKGetType_C",(TS,TSRKType*),(ts,rktype));CHKERRQ(ierr);
855f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
856f68a32c8SEmil Constantinescu }
857f68a32c8SEmil Constantinescu 
858f68a32c8SEmil Constantinescu #undef __FUNCT__
859f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKGetType_RK"
860f68a32c8SEmil Constantinescu PetscErrorCode  TSRKGetType_RK(TS ts,TSRKType *rktype)
861f68a32c8SEmil Constantinescu {
862f68a32c8SEmil Constantinescu   TS_RK     *rk = (TS_RK*)ts->data;
863f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
864f68a32c8SEmil Constantinescu 
865f68a32c8SEmil Constantinescu   PetscFunctionBegin;
866f68a32c8SEmil Constantinescu   if (!rk->tableau) {
867f68a32c8SEmil Constantinescu     ierr = TSRKSetType(ts,TSRKDefault);CHKERRQ(ierr);
868f68a32c8SEmil Constantinescu   }
869f68a32c8SEmil Constantinescu   *rktype = rk->tableau->name;
870f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
871f68a32c8SEmil Constantinescu }
872f68a32c8SEmil Constantinescu #undef __FUNCT__
873f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKSetType_RK"
874f68a32c8SEmil Constantinescu PetscErrorCode  TSRKSetType_RK(TS ts,TSRKType rktype)
875f68a32c8SEmil Constantinescu {
876f68a32c8SEmil Constantinescu   TS_RK     *rk = (TS_RK*)ts->data;
877f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
878f68a32c8SEmil Constantinescu   PetscBool      match;
879f68a32c8SEmil Constantinescu   RKTableauLink link;
880f68a32c8SEmil Constantinescu 
881f68a32c8SEmil Constantinescu   PetscFunctionBegin;
882f68a32c8SEmil Constantinescu   if (rk->tableau) {
883f68a32c8SEmil Constantinescu     ierr = PetscStrcmp(rk->tableau->name,rktype,&match);CHKERRQ(ierr);
884f68a32c8SEmil Constantinescu     if (match) PetscFunctionReturn(0);
885f68a32c8SEmil Constantinescu   }
886f68a32c8SEmil Constantinescu   for (link = RKTableauList; link; link=link->next) {
887f68a32c8SEmil Constantinescu     ierr = PetscStrcmp(link->tab.name,rktype,&match);CHKERRQ(ierr);
888f68a32c8SEmil Constantinescu     if (match) {
889f68a32c8SEmil Constantinescu       ierr = TSReset_RK(ts);CHKERRQ(ierr);
890f68a32c8SEmil Constantinescu       rk->tableau = &link->tab;
891f68a32c8SEmil Constantinescu       PetscFunctionReturn(0);
892f68a32c8SEmil Constantinescu     }
893f68a32c8SEmil Constantinescu   }
894f68a32c8SEmil Constantinescu   SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",rktype);
895e4dd521cSBarry Smith   PetscFunctionReturn(0);
896e4dd521cSBarry Smith }
897e4dd521cSBarry Smith 
898ff22ae23SHong Zhang #undef __FUNCT__
899ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages_RK"
900ff22ae23SHong Zhang static PetscErrorCode  TSGetStages_RK(TS ts,PetscInt *ns,Vec **Y)
901ff22ae23SHong Zhang {
902ff22ae23SHong Zhang   TS_RK          *rk = (TS_RK*)ts->data;
903ff22ae23SHong Zhang 
904ff22ae23SHong Zhang   PetscFunctionBegin;
905ff22ae23SHong Zhang   *ns = rk->tableau->s;
906d2daff3dSHong Zhang   if(Y) *Y  = rk->Y;
907ff22ae23SHong Zhang   PetscFunctionReturn(0);
908ff22ae23SHong Zhang }
909ff22ae23SHong Zhang 
910ff22ae23SHong Zhang 
911e4dd521cSBarry Smith /* ------------------------------------------------------------ */
912ebe3b25bSBarry Smith /*MC
913f68a32c8SEmil Constantinescu       TSRK - ODE and DAE solver using Runge-Kutta schemes
914ebe3b25bSBarry Smith 
915f68a32c8SEmil Constantinescu   The user should provide the right hand side of the equation
916f68a32c8SEmil Constantinescu   using TSSetRHSFunction().
917ebe3b25bSBarry Smith 
918f68a32c8SEmil Constantinescu   Notes:
919f68a32c8SEmil Constantinescu   The default is TSRK3, it can be changed with TSRKSetType() or -ts_rk_type
920ebe3b25bSBarry Smith 
921d41222bbSBarry Smith   Level: beginner
922d41222bbSBarry Smith 
923f68a32c8SEmil Constantinescu .seealso:  TSCreate(), TS, TSSetType(), TSRKSetType(), TSRKGetType(), TSRKSetFullyImplicit(), TSRK2D, TTSRK2E, TSRK3,
924f68a32c8SEmil Constantinescu            TSRK4, TSRK5, TSRKPRSSP2, TSRKBPR3, TSRKType, TSRKRegister()
925ebe3b25bSBarry Smith 
926ebe3b25bSBarry Smith M*/
927e4dd521cSBarry Smith #undef __FUNCT__
9285f70b478SJed Brown #define __FUNCT__ "TSCreate_RK"
9298cc058d9SJed Brown PETSC_EXTERN PetscErrorCode TSCreate_RK(TS ts)
930e4dd521cSBarry Smith {
931f68a32c8SEmil Constantinescu   TS_RK     *th;
932dfbe8321SBarry Smith   PetscErrorCode ierr;
933e4dd521cSBarry Smith 
934e4dd521cSBarry Smith   PetscFunctionBegin;
935f68a32c8SEmil Constantinescu #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
936f68a32c8SEmil Constantinescu   ierr = TSRKInitializePackage();CHKERRQ(ierr);
937f68a32c8SEmil Constantinescu #endif
938f68a32c8SEmil Constantinescu 
939f68a32c8SEmil Constantinescu   ts->ops->reset          = TSReset_RK;
9405f70b478SJed Brown   ts->ops->destroy        = TSDestroy_RK;
9415f70b478SJed Brown   ts->ops->view           = TSView_RK;
942f68a32c8SEmil Constantinescu   ts->ops->load           = TSLoad_RK;
943f68a32c8SEmil Constantinescu   ts->ops->setup          = TSSetUp_RK;
94442f2b339SBarry Smith   ts->ops->adjointsetup   = TSAdjointSetUp_RK;
945f68a32c8SEmil Constantinescu   ts->ops->step           = TSStep_RK;
946f68a32c8SEmil Constantinescu   ts->ops->interpolate    = TSInterpolate_RK;
947f68a32c8SEmil Constantinescu   ts->ops->evaluatestep   = TSEvaluateStep_RK;
948f68a32c8SEmil Constantinescu   ts->ops->setfromoptions = TSSetFromOptions_RK;
949ff22ae23SHong Zhang   ts->ops->getstages      = TSGetStages_RK;
95042f2b339SBarry Smith   ts->ops->adjointstep    = TSAdjointStep_RK;
951e4dd521cSBarry Smith 
952b00a9115SJed Brown   ierr = PetscNewLog(ts,&th);CHKERRQ(ierr);
953f68a32c8SEmil Constantinescu   ts->data = (void*)th;
954e4dd521cSBarry Smith 
955f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKGetType_C",TSRKGetType_RK);CHKERRQ(ierr);
956f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKSetType_C",TSRKSetType_RK);CHKERRQ(ierr);
957e4dd521cSBarry Smith   PetscFunctionReturn(0);
958e4dd521cSBarry Smith }
959