xref: /petsc/src/ts/impls/explicit/rk/rk.c (revision c235aa8dcc08e09f20ce58350062050a1d3caf1f)
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 */
41*c235aa8dSHong Zhang   Vec          *VecDeltaLam;     /* Increament of the adjoint sensitivity variable at stage*/
42*c235aa8dSHong Zhang   Vec          VecSensiTemp;     /* Vector to be timed with Jacobian transpose*/
43f68a32c8SEmil Constantinescu   PetscScalar  *work;            /* Scalar work */
44f68a32c8SEmil Constantinescu   PetscReal    stage_time;
45f68a32c8SEmil Constantinescu   TSStepStatus status;
465f70b478SJed Brown } TS_RK;
47e4dd521cSBarry Smith 
48f68a32c8SEmil Constantinescu /*MC
49f68a32c8SEmil Constantinescu      TSRK1 - First order forward Euler scheme.
50262ff7bbSBarry Smith 
51f68a32c8SEmil Constantinescu      This method has one stage.
52f68a32c8SEmil Constantinescu 
53f68a32c8SEmil Constantinescu      Level: advanced
54f68a32c8SEmil Constantinescu 
55f68a32c8SEmil Constantinescu .seealso: TSRK
56f68a32c8SEmil Constantinescu M*/
57f68a32c8SEmil Constantinescu /*MC
582109b73fSDebojyoti Ghosh      TSRK2A - Second order RK scheme.
59f68a32c8SEmil Constantinescu 
60f68a32c8SEmil Constantinescu      This method has two stages.
61f68a32c8SEmil Constantinescu 
62f68a32c8SEmil Constantinescu      Level: advanced
63f68a32c8SEmil Constantinescu 
64f68a32c8SEmil Constantinescu .seealso: TSRK
65f68a32c8SEmil Constantinescu M*/
66f68a32c8SEmil Constantinescu /*MC
67f68a32c8SEmil Constantinescu      TSRK3 - Third order RK scheme.
68f68a32c8SEmil Constantinescu 
69f68a32c8SEmil Constantinescu      This method has three stages.
70f68a32c8SEmil Constantinescu 
71f68a32c8SEmil Constantinescu      Level: advanced
72f68a32c8SEmil Constantinescu 
73f68a32c8SEmil Constantinescu .seealso: TSRK
74f68a32c8SEmil Constantinescu M*/
75f68a32c8SEmil Constantinescu /*MC
762109b73fSDebojyoti Ghosh      TSRK3BS - Third order RK scheme of Bogacki-Shampine with 2nd order embedded method.
772109b73fSDebojyoti Ghosh 
782109b73fSDebojyoti Ghosh      This method has four stages.
792109b73fSDebojyoti Ghosh 
802109b73fSDebojyoti Ghosh      Level: advanced
812109b73fSDebojyoti Ghosh 
822109b73fSDebojyoti Ghosh .seealso: TSRK
832109b73fSDebojyoti Ghosh M*/
842109b73fSDebojyoti Ghosh /*MC
85f68a32c8SEmil Constantinescu      TSRK4 - Fourth order RK scheme.
86f68a32c8SEmil Constantinescu 
872e698f8bSDebojyoti Ghosh      This is the classical Runge-Kutta method with four stages.
88f68a32c8SEmil Constantinescu 
89f68a32c8SEmil Constantinescu      Level: advanced
90f68a32c8SEmil Constantinescu 
91f68a32c8SEmil Constantinescu .seealso: TSRK
92f68a32c8SEmil Constantinescu M*/
93f68a32c8SEmil Constantinescu /*MC
942e698f8bSDebojyoti Ghosh      TSRK5F - Fifth order Fehlberg RK scheme with a 4th order embedded method.
95f68a32c8SEmil Constantinescu 
96f68a32c8SEmil Constantinescu      This method has six stages.
97f68a32c8SEmil Constantinescu 
98f68a32c8SEmil Constantinescu      Level: advanced
99f68a32c8SEmil Constantinescu 
100f68a32c8SEmil Constantinescu .seealso: TSRK
101f68a32c8SEmil Constantinescu M*/
1022109b73fSDebojyoti Ghosh /*MC
1032e698f8bSDebojyoti Ghosh      TSRK5DP - Fifth order Dormand-Prince RK scheme with the 4th order embedded method.
1042109b73fSDebojyoti Ghosh 
1052109b73fSDebojyoti Ghosh      This method has seven stages.
1062109b73fSDebojyoti Ghosh 
1072109b73fSDebojyoti Ghosh      Level: advanced
1082109b73fSDebojyoti Ghosh 
1092109b73fSDebojyoti Ghosh .seealso: TSRK
1102109b73fSDebojyoti Ghosh M*/
111262ff7bbSBarry Smith 
112262ff7bbSBarry Smith #undef __FUNCT__
113f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKRegisterAll"
114f68a32c8SEmil Constantinescu /*@C
115f68a32c8SEmil Constantinescu   TSRKRegisterAll - Registers all of the Runge-Kutta explicit methods in TSRK
116262ff7bbSBarry Smith 
117f68a32c8SEmil Constantinescu   Not Collective, but should be called by all processes which will need the schemes to be registered
118262ff7bbSBarry Smith 
119f68a32c8SEmil Constantinescu   Level: advanced
120262ff7bbSBarry Smith 
121f68a32c8SEmil Constantinescu .keywords: TS, TSRK, register, all
122262ff7bbSBarry Smith 
123f68a32c8SEmil Constantinescu .seealso:  TSRKRegisterDestroy()
124262ff7bbSBarry Smith @*/
125f68a32c8SEmil Constantinescu PetscErrorCode TSRKRegisterAll(void)
126262ff7bbSBarry Smith {
1274ac538c5SBarry Smith   PetscErrorCode ierr;
128262ff7bbSBarry Smith 
129262ff7bbSBarry Smith   PetscFunctionBegin;
130f68a32c8SEmil Constantinescu   if (TSRKRegisterAllCalled) PetscFunctionReturn(0);
131f68a32c8SEmil Constantinescu   TSRKRegisterAllCalled = PETSC_TRUE;
132e4dd521cSBarry Smith 
133e4dd521cSBarry Smith   {
134f68a32c8SEmil Constantinescu     const PetscReal
135f68a32c8SEmil Constantinescu       A[1][1] = {{0.0}},
136f68a32c8SEmil Constantinescu       b[1]    = {1.0};
137f68a32c8SEmil Constantinescu     ierr = TSRKRegister(TSRK1FE,1,1,&A[0][0],b,NULL,NULL,1,b);CHKERRQ(ierr);
138e4dd521cSBarry Smith   }
139db046809SBarry Smith   {
140f68a32c8SEmil Constantinescu     const PetscReal
141f68a32c8SEmil Constantinescu       A[2][2]     = {{0.0,0.0},
142f68a32c8SEmil Constantinescu                     {1.0,0.0}},
143f68a32c8SEmil Constantinescu       b[2]        = {0.5,0.5},
144f68a32c8SEmil Constantinescu       bembed[2]   = {1.0,0};
145fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK2A,2,2,&A[0][0],b,NULL,bembed,2,b);CHKERRQ(ierr);
146db046809SBarry Smith   }
147f68a32c8SEmil Constantinescu   {
148f68a32c8SEmil Constantinescu     const PetscReal
149f68a32c8SEmil Constantinescu       A[3][3] = {{0,0,0},
150f68a32c8SEmil Constantinescu                  {2.0/3.0,0,0},
151f68a32c8SEmil Constantinescu                  {-1.0/3.0,1.0,0}},
152f68a32c8SEmil Constantinescu       b[3]    = {0.25,0.5,0.25};
153fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK3,3,3,&A[0][0],b,NULL,NULL,3,b);CHKERRQ(ierr);
154fdefd5e5SDebojyoti Ghosh   }
155fdefd5e5SDebojyoti Ghosh   {
156fdefd5e5SDebojyoti Ghosh     const PetscReal
157fdefd5e5SDebojyoti Ghosh       A[4][4] = {{0,0,0,0},
158fdefd5e5SDebojyoti Ghosh                  {1.0/2.0,0,0,0},
159fdefd5e5SDebojyoti Ghosh                  {0,3.0/4.0,0,0},
160fdefd5e5SDebojyoti Ghosh                  {2.0/9.0,1.0/3.0,4.0/9.0,0}},
161fdefd5e5SDebojyoti Ghosh       b[4]    = {2.0/9.0,1.0/3.0,4.0/9.0,0},
162fdefd5e5SDebojyoti Ghosh       bembed[4] = {7.0/24.0,1.0/4.0,1.0/3.0,1.0/8.0};
163fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK3BS,3,4,&A[0][0],b,NULL,bembed,3,b);CHKERRQ(ierr);
164db046809SBarry Smith   }
165f68a32c8SEmil Constantinescu   {
166f68a32c8SEmil Constantinescu     const PetscReal
167f68a32c8SEmil Constantinescu       A[4][4] = {{0,0,0,0},
168f68a32c8SEmil Constantinescu                  {0.5,0,0,0},
169f68a32c8SEmil Constantinescu                  {0,0.5,0,0},
170f68a32c8SEmil Constantinescu                  {0,0,1.0,0}},
171f68a32c8SEmil Constantinescu       b[4]    = {1.0/6.0,1.0/3.0,1.0/3.0,1.0/6.0};
172fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK4,4,4,&A[0][0],b,NULL,NULL,4,b);CHKERRQ(ierr);
173db046809SBarry Smith   }
174f68a32c8SEmil Constantinescu   {
175f68a32c8SEmil Constantinescu     const PetscReal
176f68a32c8SEmil Constantinescu       A[6][6]   = {{0,0,0,0,0,0},
177f68a32c8SEmil Constantinescu                    {0.25,0,0,0,0,0},
178f68a32c8SEmil Constantinescu                    {3.0/32.0,9.0/32.0,0,0,0,0},
179f68a32c8SEmil Constantinescu                    {1932.0/2197.0,-7200.0/2197.0,7296.0/2197.0,0,0,0},
180f68a32c8SEmil Constantinescu                    {439.0/216.0,-8.0,3680.0/513.0,-845.0/4104.0,0,0},
181f68a32c8SEmil Constantinescu                    {-8.0/27.0,2.0,-3544.0/2565.0,1859.0/4104.0,-11.0/40.0,0}},
182f68a32c8SEmil Constantinescu       b[6]      = {16.0/135.0,0,6656.0/12825.0,28561.0/56430.0,-9.0/50.0,2.0/55.0},
183f68a32c8SEmil Constantinescu       bembed[6] = {25.0/216.0,0,1408.0/2565.0,2197.0/4104.0,-1.0/5.0,0};
184fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK5F,5,6,&A[0][0],b,NULL,bembed,5,b);CHKERRQ(ierr);
185fdefd5e5SDebojyoti Ghosh   }
186fdefd5e5SDebojyoti Ghosh   {
187fdefd5e5SDebojyoti Ghosh     const PetscReal
188fdefd5e5SDebojyoti Ghosh       A[7][7]   = {{0,0,0,0,0,0,0},
189fdefd5e5SDebojyoti Ghosh                    {1.0/5.0,0,0,0,0,0,0},
190fdefd5e5SDebojyoti Ghosh                    {3.0/40.0,9.0/40.0,0,0,0,0,0},
191fdefd5e5SDebojyoti Ghosh                    {44.0/45.0,-56.0/15.0,32.0/9.0,0,0,0,0},
192fdefd5e5SDebojyoti Ghosh                    {19372.0/6561.0,-25360.0/2187.0,64448.0/6561.0,-212.0/729.0,0,0,0},
193fdefd5e5SDebojyoti Ghosh                    {9017.0/3168.0,-355.0/33.0,46732.0/5247.0,49.0/176.0,-5103.0/18656.0,0,0},
194fdefd5e5SDebojyoti Ghosh                    {35.0/384.0,0,500.0/1113.0,125.0/192.0,-2187.0/6784.0,11.0/84.0,0}},
195fdefd5e5SDebojyoti 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},
196fdefd5e5SDebojyoti 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};
197fdefd5e5SDebojyoti Ghosh     ierr = TSRKRegister(TSRK5DP,5,7,&A[0][0],b,NULL,bembed,5,b);CHKERRQ(ierr);
198f68a32c8SEmil Constantinescu   }
199db046809SBarry Smith   PetscFunctionReturn(0);
200db046809SBarry Smith }
201db046809SBarry Smith 
202e4dd521cSBarry Smith #undef __FUNCT__
203f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKRegisterDestroy"
204f68a32c8SEmil Constantinescu /*@C
205f68a32c8SEmil Constantinescu    TSRKRegisterDestroy - Frees the list of schemes that were registered by TSRKRegister().
206f68a32c8SEmil Constantinescu 
207f68a32c8SEmil Constantinescu    Not Collective
208f68a32c8SEmil Constantinescu 
209f68a32c8SEmil Constantinescu    Level: advanced
210f68a32c8SEmil Constantinescu 
211f68a32c8SEmil Constantinescu .keywords: TSRK, register, destroy
212f68a32c8SEmil Constantinescu .seealso: TSRKRegister(), TSRKRegisterAll()
213f68a32c8SEmil Constantinescu @*/
214f68a32c8SEmil Constantinescu PetscErrorCode TSRKRegisterDestroy(void)
215e4dd521cSBarry Smith {
216f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
217f68a32c8SEmil Constantinescu   RKTableauLink link;
218f68a32c8SEmil Constantinescu 
219f68a32c8SEmil Constantinescu   PetscFunctionBegin;
220f68a32c8SEmil Constantinescu   while ((link = RKTableauList)) {
221f68a32c8SEmil Constantinescu     RKTableau t = &link->tab;
222f68a32c8SEmil Constantinescu     RKTableauList = link->next;
223f68a32c8SEmil Constantinescu     ierr = PetscFree3(t->A,t->b,t->c);  CHKERRQ(ierr);
224f68a32c8SEmil Constantinescu     ierr = PetscFree (t->bembed);       CHKERRQ(ierr);
225f68a32c8SEmil Constantinescu     ierr = PetscFree (t->binterp);      CHKERRQ(ierr);
226f68a32c8SEmil Constantinescu     ierr = PetscFree (t->name);         CHKERRQ(ierr);
227f68a32c8SEmil Constantinescu     ierr = PetscFree (link);            CHKERRQ(ierr);
228f68a32c8SEmil Constantinescu   }
229f68a32c8SEmil Constantinescu   TSRKRegisterAllCalled = PETSC_FALSE;
230f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
231f68a32c8SEmil Constantinescu }
232f68a32c8SEmil Constantinescu 
233f68a32c8SEmil Constantinescu #undef __FUNCT__
234f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKInitializePackage"
235f68a32c8SEmil Constantinescu /*@C
236f68a32c8SEmil Constantinescu   TSRKInitializePackage - This function initializes everything in the TSRK package. It is called
237f68a32c8SEmil Constantinescu   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to TSCreate_RK()
238f68a32c8SEmil Constantinescu   when using static libraries.
239f68a32c8SEmil Constantinescu 
240f68a32c8SEmil Constantinescu   Level: developer
241f68a32c8SEmil Constantinescu 
242f68a32c8SEmil Constantinescu .keywords: TS, TSRK, initialize, package
243f68a32c8SEmil Constantinescu .seealso: PetscInitialize()
244f68a32c8SEmil Constantinescu @*/
245f68a32c8SEmil Constantinescu PetscErrorCode TSRKInitializePackage(void)
246f68a32c8SEmil Constantinescu {
247186e87acSLisandro Dalcin   PetscErrorCode ierr;
248e4dd521cSBarry Smith 
249e4dd521cSBarry Smith   PetscFunctionBegin;
250f68a32c8SEmil Constantinescu   if (TSRKPackageInitialized) PetscFunctionReturn(0);
251f68a32c8SEmil Constantinescu   TSRKPackageInitialized = PETSC_TRUE;
252f68a32c8SEmil Constantinescu   ierr = TSRKRegisterAll();CHKERRQ(ierr);
253f68a32c8SEmil Constantinescu   ierr = PetscObjectComposedDataRegister(&explicit_stage_time_id);CHKERRQ(ierr);
254f68a32c8SEmil Constantinescu   ierr = PetscRegisterFinalize(TSRKFinalizePackage);CHKERRQ(ierr);
255f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
256f68a32c8SEmil Constantinescu }
257186e87acSLisandro Dalcin 
258f68a32c8SEmil Constantinescu #undef __FUNCT__
259f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKFinalizePackage"
260f68a32c8SEmil Constantinescu /*@C
261f68a32c8SEmil Constantinescu   TSRKFinalizePackage - This function destroys everything in the TSRK package. It is
262f68a32c8SEmil Constantinescu   called from PetscFinalize().
263186e87acSLisandro Dalcin 
264f68a32c8SEmil Constantinescu   Level: developer
265f68a32c8SEmil Constantinescu 
266f68a32c8SEmil Constantinescu .keywords: Petsc, destroy, package
267f68a32c8SEmil Constantinescu .seealso: PetscFinalize()
268f68a32c8SEmil Constantinescu @*/
269f68a32c8SEmil Constantinescu PetscErrorCode TSRKFinalizePackage(void)
270f68a32c8SEmil Constantinescu {
271f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
272f68a32c8SEmil Constantinescu 
273f68a32c8SEmil Constantinescu   PetscFunctionBegin;
274f68a32c8SEmil Constantinescu   TSRKPackageInitialized = PETSC_FALSE;
275f68a32c8SEmil Constantinescu   ierr = TSRKRegisterDestroy();CHKERRQ(ierr);
276f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
277f68a32c8SEmil Constantinescu }
278f68a32c8SEmil Constantinescu 
279f68a32c8SEmil Constantinescu #undef __FUNCT__
280f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKRegister"
281f68a32c8SEmil Constantinescu /*@C
282f68a32c8SEmil Constantinescu    TSRKRegister - register an RK scheme by providing the entries in the Butcher tableau and optionally embedded approximations and interpolation
283f68a32c8SEmil Constantinescu 
284f68a32c8SEmil Constantinescu    Not Collective, but the same schemes should be registered on all processes on which they will be used
285f68a32c8SEmil Constantinescu 
286f68a32c8SEmil Constantinescu    Input Parameters:
287f68a32c8SEmil Constantinescu +  name - identifier for method
288f68a32c8SEmil Constantinescu .  order - approximation order of method
289f68a32c8SEmil Constantinescu .  s - number of stages, this is the dimension of the matrices below
290f68a32c8SEmil Constantinescu .  A - stage coefficients (dimension s*s, row-major)
291f68a32c8SEmil Constantinescu .  b - step completion table (dimension s; NULL to use last row of A)
292f68a32c8SEmil Constantinescu .  c - abscissa (dimension s; NULL to use row sums of A)
293f68a32c8SEmil Constantinescu .  bembed - completion table for embedded method (dimension s; NULL if not available)
294f68a32c8SEmil Constantinescu .  pinterp - Order of the interpolation scheme, equal to the number of columns of binterp
295f68a32c8SEmil Constantinescu -  binterp - Coefficients of the interpolation formula (dimension s*pinterp; NULL to reuse binterpt)
296f68a32c8SEmil Constantinescu 
297f68a32c8SEmil Constantinescu    Notes:
298f68a32c8SEmil Constantinescu    Several RK methods are provided, this function is only needed to create new methods.
299f68a32c8SEmil Constantinescu 
300f68a32c8SEmil Constantinescu    Level: advanced
301f68a32c8SEmil Constantinescu 
302f68a32c8SEmil Constantinescu .keywords: TS, register
303f68a32c8SEmil Constantinescu 
304f68a32c8SEmil Constantinescu .seealso: TSRK
305f68a32c8SEmil Constantinescu @*/
306f68a32c8SEmil Constantinescu PetscErrorCode TSRKRegister(TSRKType name,PetscInt order,PetscInt s,
307f68a32c8SEmil Constantinescu                                  const PetscReal A[],const PetscReal b[],const PetscReal c[],
308f68a32c8SEmil Constantinescu                                  const PetscReal bembed[],
309f68a32c8SEmil Constantinescu                                  PetscInt pinterp,const PetscReal binterp[])
310f68a32c8SEmil Constantinescu {
311f68a32c8SEmil Constantinescu   PetscErrorCode  ierr;
312f68a32c8SEmil Constantinescu   RKTableauLink   link;
313f68a32c8SEmil Constantinescu   RKTableau       t;
314f68a32c8SEmil Constantinescu   PetscInt        i,j;
315f68a32c8SEmil Constantinescu 
316f68a32c8SEmil Constantinescu   PetscFunctionBegin;
317f68a32c8SEmil Constantinescu   ierr     = PetscMalloc(sizeof(*link),&link);CHKERRQ(ierr);
318f68a32c8SEmil Constantinescu   ierr     = PetscMemzero(link,sizeof(*link));CHKERRQ(ierr);
319f68a32c8SEmil Constantinescu   t        = &link->tab;
320f68a32c8SEmil Constantinescu   ierr     = PetscStrallocpy(name,&t->name);CHKERRQ(ierr);
321f68a32c8SEmil Constantinescu   t->order = order;
322f68a32c8SEmil Constantinescu   t->s     = s;
323dcca6d9dSJed Brown   ierr     = PetscMalloc3(s*s,&t->A,s,&t->b,s,&t->c);CHKERRQ(ierr);
324f68a32c8SEmil Constantinescu   ierr     = PetscMemcpy(t->A,A,s*s*sizeof(A[0]));CHKERRQ(ierr);
325f68a32c8SEmil Constantinescu   if (b)  { ierr = PetscMemcpy(t->b,b,s*sizeof(b[0]));CHKERRQ(ierr); }
326f68a32c8SEmil Constantinescu   else for (i=0; i<s; i++) t->b[i] = A[(s-1)*s+i];
327f68a32c8SEmil Constantinescu   if (c)  { ierr = PetscMemcpy(t->c,c,s*sizeof(c[0]));CHKERRQ(ierr); }
328f68a32c8SEmil 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];
329d760c35bSDebojyoti Ghosh   t->FSAL = PETSC_TRUE;
330d760c35bSDebojyoti Ghosh   for (i=0; i<s; i++) if (t->A[(s-1)*s+i] != t->b[i]) t->FSAL = PETSC_FALSE;
331f68a32c8SEmil Constantinescu   if (bembed) {
332785e854fSJed Brown     ierr = PetscMalloc1(s,&t->bembed);CHKERRQ(ierr);
333f68a32c8SEmil Constantinescu     ierr = PetscMemcpy(t->bembed,bembed,s*sizeof(bembed[0]));CHKERRQ(ierr);
334f68a32c8SEmil Constantinescu   }
335f68a32c8SEmil Constantinescu 
336f68a32c8SEmil Constantinescu   t->pinterp     = pinterp;
337785e854fSJed Brown   ierr           = PetscMalloc1(s*pinterp,&t->binterp);CHKERRQ(ierr);
338f68a32c8SEmil Constantinescu   ierr           = PetscMemcpy(t->binterp,binterp,s*pinterp*sizeof(binterp[0]));CHKERRQ(ierr);
339f68a32c8SEmil Constantinescu   link->next     = RKTableauList;
340f68a32c8SEmil Constantinescu   RKTableauList = link;
341f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
342f68a32c8SEmil Constantinescu }
343f68a32c8SEmil Constantinescu 
344f68a32c8SEmil Constantinescu #undef __FUNCT__
345f68a32c8SEmil Constantinescu #define __FUNCT__ "TSEvaluateStep_RK"
346e4dd521cSBarry Smith /*
347f68a32c8SEmil Constantinescu  The step completion formula is
348e4dd521cSBarry Smith 
349f68a32c8SEmil Constantinescu  x1 = x0 + h b^T YdotRHS
350f68a32c8SEmil Constantinescu 
351f68a32c8SEmil Constantinescu  This function can be called before or after ts->vec_sol has been updated.
352f68a32c8SEmil Constantinescu  Suppose we have a completion formula (b) and an embedded formula (be) of different order.
353f68a32c8SEmil Constantinescu  We can write
354f68a32c8SEmil Constantinescu 
355f68a32c8SEmil Constantinescu  x1e = x0 + h be^T YdotRHS
356f68a32c8SEmil Constantinescu      = x1 - h b^T YdotRHS + h be^T YdotRHS
357f68a32c8SEmil Constantinescu      = x1 + h (be - b)^T YdotRHS
358f68a32c8SEmil Constantinescu 
359f68a32c8SEmil Constantinescu  so we can evaluate the method with different order even after the step has been optimistically completed.
360e4dd521cSBarry Smith */
361f68a32c8SEmil Constantinescu static PetscErrorCode TSEvaluateStep_RK(TS ts,PetscInt order,Vec X,PetscBool *done)
362f68a32c8SEmil Constantinescu {
363f68a32c8SEmil Constantinescu   TS_RK         *rk   = (TS_RK*)ts->data;
364f68a32c8SEmil Constantinescu   RKTableau      tab  = rk->tableau;
365f68a32c8SEmil Constantinescu   PetscScalar   *w    = rk->work;
366f68a32c8SEmil Constantinescu   PetscReal      h;
367f68a32c8SEmil Constantinescu   PetscInt       s    = tab->s,j;
368f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
369f68a32c8SEmil Constantinescu 
370f68a32c8SEmil Constantinescu   PetscFunctionBegin;
371f68a32c8SEmil Constantinescu   switch (rk->status) {
372f68a32c8SEmil Constantinescu   case TS_STEP_INCOMPLETE:
373f68a32c8SEmil Constantinescu   case TS_STEP_PENDING:
374f68a32c8SEmil Constantinescu     h = ts->time_step; break;
375f68a32c8SEmil Constantinescu   case TS_STEP_COMPLETE:
376f68a32c8SEmil Constantinescu     h = ts->time_step_prev; break;
377f68a32c8SEmil Constantinescu   default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
378f68a32c8SEmil Constantinescu   }
379f68a32c8SEmil Constantinescu   if (order == tab->order) {
380f68a32c8SEmil Constantinescu     if (rk->status == TS_STEP_INCOMPLETE) {
381f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);
382f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = h*tab->b[j];
383f68a32c8SEmil Constantinescu       ierr = VecMAXPY(X,s,w,rk->YdotRHS);CHKERRQ(ierr);
384f68a32c8SEmil Constantinescu     } else {ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);}
385f68a32c8SEmil Constantinescu     PetscFunctionReturn(0);
386f68a32c8SEmil Constantinescu   } else if (order == tab->order-1) {
387f68a32c8SEmil Constantinescu     if (!tab->bembed) goto unavailable;
388f68a32c8SEmil Constantinescu     if (rk->status == TS_STEP_INCOMPLETE) { /* Complete with the embedded method (be) */
389f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);
390f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = h*tab->bembed[j];
391f68a32c8SEmil Constantinescu       ierr = VecMAXPY(X,s,w,rk->YdotRHS);CHKERRQ(ierr);
392f68a32c8SEmil Constantinescu     } else {                    /* Rollback and re-complete using (be-b) */
393f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,X);CHKERRQ(ierr);
394f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = h*(tab->bembed[j] - tab->b[j]);
395f68a32c8SEmil Constantinescu       ierr = VecMAXPY(X,s,w,rk->YdotRHS);CHKERRQ(ierr);
396f68a32c8SEmil Constantinescu     }
397f68a32c8SEmil Constantinescu     if (done) *done = PETSC_TRUE;
398f68a32c8SEmil Constantinescu     PetscFunctionReturn(0);
399f68a32c8SEmil Constantinescu   }
400f68a32c8SEmil Constantinescu unavailable:
401f68a32c8SEmil Constantinescu   if (done) *done = PETSC_FALSE;
402f68a32c8SEmil 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);
403f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
404f68a32c8SEmil Constantinescu }
405f68a32c8SEmil Constantinescu 
406f68a32c8SEmil Constantinescu #undef __FUNCT__
407f68a32c8SEmil Constantinescu #define __FUNCT__ "TSStep_RK"
408f68a32c8SEmil Constantinescu static PetscErrorCode TSStep_RK(TS ts)
409f68a32c8SEmil Constantinescu {
410f68a32c8SEmil Constantinescu   TS_RK           *rk   = (TS_RK*)ts->data;
411f68a32c8SEmil Constantinescu   RKTableau        tab  = rk->tableau;
412f68a32c8SEmil Constantinescu   const PetscInt   s    = tab->s;
413f68a32c8SEmil Constantinescu   const PetscReal *A = tab->A,*b = tab->b,*c = tab->c;
414f68a32c8SEmil Constantinescu   PetscScalar     *w    = rk->work;
415f68a32c8SEmil Constantinescu   Vec             *Y    = rk->Y,*YdotRHS = rk->YdotRHS;
416f68a32c8SEmil Constantinescu   TSAdapt          adapt;
417f68a32c8SEmil Constantinescu   PetscInt         i,j,reject,next_scheme;
418f68a32c8SEmil Constantinescu   PetscReal        next_time_step;
419f68a32c8SEmil Constantinescu   PetscReal        t;
420f68a32c8SEmil Constantinescu   PetscBool        accept;
421f68a32c8SEmil Constantinescu   PetscErrorCode   ierr;
422f68a32c8SEmil Constantinescu 
423f68a32c8SEmil Constantinescu   PetscFunctionBegin;
424f68a32c8SEmil Constantinescu 
425f68a32c8SEmil Constantinescu   next_time_step = ts->time_step;
426f68a32c8SEmil Constantinescu   t              = ts->ptime;
427f68a32c8SEmil Constantinescu   accept         = PETSC_TRUE;
428f68a32c8SEmil Constantinescu   rk->status     = TS_STEP_INCOMPLETE;
429f68a32c8SEmil Constantinescu 
430f68a32c8SEmil Constantinescu 
431f68a32c8SEmil Constantinescu   for (reject=0; reject<ts->max_reject && !ts->reason; reject++,ts->reject++) {
432f68a32c8SEmil Constantinescu     PetscReal h = ts->time_step;
433f68a32c8SEmil Constantinescu     ierr = TSPreStep(ts);CHKERRQ(ierr);
434f68a32c8SEmil Constantinescu     for (i=0; i<s; i++) {
4359be3e283SDebojyoti Ghosh       rk->stage_time = t + h*c[i];
4369be3e283SDebojyoti Ghosh       ierr = TSPreStage(ts,rk->stage_time); CHKERRQ(ierr);
437f68a32c8SEmil Constantinescu       ierr = VecCopy(ts->vec_sol,Y[i]);CHKERRQ(ierr);
438f68a32c8SEmil Constantinescu       for (j=0; j<i; j++) w[j] = h*A[i*s+j];
439f68a32c8SEmil Constantinescu       ierr = VecMAXPY(Y[i],i,w,YdotRHS);CHKERRQ(ierr);
4409be3e283SDebojyoti Ghosh       ierr = TSPostStage(ts,rk->stage_time,i,Y); CHKERRQ(ierr);
441f68a32c8SEmil Constantinescu       ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
442f68a32c8SEmil Constantinescu       ierr = TSAdaptCheckStage(adapt,ts,&accept);CHKERRQ(ierr);
443f68a32c8SEmil Constantinescu       if (!accept) goto reject_step;
444f68a32c8SEmil Constantinescu       ierr = TSComputeRHSFunction(ts,t+h*c[i],Y[i],YdotRHS[i]);CHKERRQ(ierr);
445f68a32c8SEmil Constantinescu     }
446f68a32c8SEmil Constantinescu     ierr = TSEvaluateStep(ts,tab->order,ts->vec_sol,NULL);CHKERRQ(ierr);
447f68a32c8SEmil Constantinescu     rk->status = TS_STEP_PENDING;
448f68a32c8SEmil Constantinescu 
449f68a32c8SEmil Constantinescu     /* Register only the current method as a candidate because we're not supporting multiple candidates yet. */
450f68a32c8SEmil Constantinescu     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
451f68a32c8SEmil Constantinescu     ierr = TSAdaptCandidatesClear(adapt);CHKERRQ(ierr);
452f68a32c8SEmil Constantinescu     ierr = TSAdaptCandidateAdd(adapt,tab->name,tab->order,1,tab->ccfl,1.*tab->s,PETSC_TRUE);CHKERRQ(ierr);
453f68a32c8SEmil Constantinescu     ierr = TSAdaptChoose(adapt,ts,ts->time_step,&next_scheme,&next_time_step,&accept);CHKERRQ(ierr);
454f68a32c8SEmil Constantinescu     if (accept) {
455f68a32c8SEmil Constantinescu       /* ignore next_scheme for now */
456f68a32c8SEmil Constantinescu       ts->ptime    += ts->time_step;
457f68a32c8SEmil Constantinescu       ts->time_step = next_time_step;
458e3caeda6SBarry Smith       ts->steps++;
459f68a32c8SEmil Constantinescu       rk->status = TS_STEP_COMPLETE;
460f68a32c8SEmil Constantinescu       ierr = PetscObjectComposedDataSetReal((PetscObject)ts->vec_sol,explicit_stage_time_id,ts->ptime);CHKERRQ(ierr);
461f68a32c8SEmil Constantinescu       break;
462f68a32c8SEmil Constantinescu     } else {                    /* Roll back the current step */
463f68a32c8SEmil Constantinescu       for (j=0; j<s; j++) w[j] = -h*b[j];
464f68a32c8SEmil Constantinescu       ierr = VecMAXPY(ts->vec_sol,s,w,rk->YdotRHS);CHKERRQ(ierr);
465f68a32c8SEmil Constantinescu       ts->time_step = next_time_step;
466f68a32c8SEmil Constantinescu       rk->status   = TS_STEP_INCOMPLETE;
467f68a32c8SEmil Constantinescu     }
468f68a32c8SEmil Constantinescu reject_step: continue;
469f68a32c8SEmil Constantinescu   }
470f68a32c8SEmil Constantinescu   if (rk->status != TS_STEP_COMPLETE && !ts->reason) ts->reason = TS_DIVERGED_STEP_REJECTED;
471f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
472e4dd521cSBarry Smith }
473e4dd521cSBarry Smith 
474f68a32c8SEmil Constantinescu #undef __FUNCT__
475d2daff3dSHong Zhang #define __FUNCT__ "TSStepAdj_RK"
476*c235aa8dSHong Zhang static PetscErrorCode TSStepAdj_RK(TS ts)
477d2daff3dSHong Zhang {
478*c235aa8dSHong Zhang   TS_RK           *rk   = (TS_RK*)ts->data;
479*c235aa8dSHong Zhang   RKTableau        tab  = rk->tableau;
480*c235aa8dSHong Zhang   const PetscInt   s    = tab->s;
481*c235aa8dSHong Zhang   const PetscReal *A = tab->A,*b = tab->b,*c = tab->c;
482*c235aa8dSHong Zhang   PetscScalar     *w    = rk->work;
483*c235aa8dSHong Zhang   Vec             *Y    = rk->Y,*VecDeltaLam = rk->VecDeltaLam,VecSensiTemp= rk->VecSensiTemp;
484*c235aa8dSHong Zhang   PetscInt         i,j;
485*c235aa8dSHong Zhang   PetscReal        t;
486d2daff3dSHong Zhang   PetscErrorCode   ierr;
487*c235aa8dSHong Zhang 
488d2daff3dSHong Zhang   PetscFunctionBegin;
489d2daff3dSHong Zhang 
490*c235aa8dSHong Zhang   //ierr = TSStep_RK(ts);CHKERRQ(ierr); // reuse TSStep
491d2daff3dSHong Zhang 
492*c235aa8dSHong Zhang   t          = ts->ptime;
493*c235aa8dSHong Zhang   rk->status = TS_STEP_INCOMPLETE;
494*c235aa8dSHong Zhang 
495*c235aa8dSHong Zhang   PetscReal h = ts->time_step; // already obtained from checkpointing
496*c235aa8dSHong Zhang   ierr = TSPreStep(ts);CHKERRQ(ierr);
497*c235aa8dSHong Zhang   for (i=s-1; i>=0; i--) {
498*c235aa8dSHong Zhang     Mat J,Jp;
499*c235aa8dSHong Zhang     rk->stage_time = t + h*(1.0-c[i]);
500*c235aa8dSHong Zhang     ierr = VecCopy(ts->vec_sensi,VecSensiTemp);CHKERRQ(ierr);
501*c235aa8dSHong Zhang     ierr = VecScale(VecSensiTemp,-h*b[i]);
502*c235aa8dSHong Zhang     for (j=i+1; j<s; j++) {
503*c235aa8dSHong Zhang       ierr = VecAXPY(VecSensiTemp,-h*A[j*s+i],VecDeltaLam[j]);
504*c235aa8dSHong Zhang     }
505*c235aa8dSHong Zhang     ierr = TSGetRHSJacobian(ts,&J,&Jp,NULL,NULL);CHKERRQ(ierr);
506*c235aa8dSHong Zhang     ierr = TSComputeRHSJacobian(ts,rk->stage_time,Y[i],J,Jp);CHKERRQ(ierr);
507*c235aa8dSHong Zhang     ierr = MatMultTranspose(J,VecSensiTemp,VecDeltaLam[i]);CHKERRQ(ierr);
508*c235aa8dSHong Zhang   }
509*c235aa8dSHong Zhang 
510*c235aa8dSHong Zhang   for (j=0; j<s; j++) w[j] = 1.0;
511*c235aa8dSHong Zhang   ierr = VecMAXPY(ts->vec_sensi,s,w,VecDeltaLam);CHKERRQ(ierr);
512*c235aa8dSHong Zhang 
513*c235aa8dSHong Zhang   ts->ptime += ts->time_step;
514*c235aa8dSHong Zhang   ts->steps++;
515*c235aa8dSHong Zhang   rk->status = TS_STEP_COMPLETE;
516*c235aa8dSHong Zhang   ierr = PetscObjectComposedDataSetReal((PetscObject)ts->vec_sensi,explicit_stage_time_id,ts->ptime);CHKERRQ(ierr);
517d2daff3dSHong Zhang   PetscFunctionReturn(0);
518d2daff3dSHong Zhang }
519d2daff3dSHong Zhang 
520d2daff3dSHong Zhang #undef __FUNCT__
521f68a32c8SEmil Constantinescu #define __FUNCT__ "TSInterpolate_RK"
522f68a32c8SEmil Constantinescu static PetscErrorCode TSInterpolate_RK(TS ts,PetscReal itime,Vec X)
523f68a32c8SEmil Constantinescu {
524f68a32c8SEmil Constantinescu   TS_RK           *rk = (TS_RK*)ts->data;
525f68a32c8SEmil Constantinescu   PetscInt         s  = rk->tableau->s,pinterp = rk->tableau->pinterp,i,j;
526f68a32c8SEmil Constantinescu   PetscReal        h;
527f68a32c8SEmil Constantinescu   PetscReal        tt,t;
528f68a32c8SEmil Constantinescu   PetscScalar     *b;
529f68a32c8SEmil Constantinescu   const PetscReal *B = rk->tableau->binterp;
530f68a32c8SEmil Constantinescu   PetscErrorCode   ierr;
531e4dd521cSBarry Smith 
532f68a32c8SEmil Constantinescu   PetscFunctionBegin;
533f68a32c8SEmil Constantinescu   if (!B) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRK %s does not have an interpolation formula",rk->tableau->name);
534f68a32c8SEmil Constantinescu   switch (rk->status) {
535f68a32c8SEmil Constantinescu   case TS_STEP_INCOMPLETE:
536f68a32c8SEmil Constantinescu   case TS_STEP_PENDING:
537f68a32c8SEmil Constantinescu     h = ts->time_step;
538f68a32c8SEmil Constantinescu     t = (itime - ts->ptime)/h;
539f68a32c8SEmil Constantinescu     break;
540f68a32c8SEmil Constantinescu   case TS_STEP_COMPLETE:
541f68a32c8SEmil Constantinescu     h = ts->time_step_prev;
542f68a32c8SEmil Constantinescu     t = (itime - ts->ptime)/h + 1; /* In the interval [0,1] */
543f68a32c8SEmil Constantinescu     break;
544f68a32c8SEmil Constantinescu   default: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Invalid TSStepStatus");
545e4dd521cSBarry Smith   }
546785e854fSJed Brown   ierr = PetscMalloc1(s,&b);CHKERRQ(ierr);
547f68a32c8SEmil Constantinescu   for (i=0; i<s; i++) b[i] = 0;
548f68a32c8SEmil Constantinescu   for (j=0,tt=t; j<pinterp; j++,tt*=t) {
549f68a32c8SEmil Constantinescu     for (i=0; i<s; i++) {
550f68a32c8SEmil Constantinescu       b[i]  += h * B[i*pinterp+j] * tt;
551e4dd521cSBarry Smith     }
552f68a32c8SEmil Constantinescu   }
553f68a32c8SEmil Constantinescu   ierr = VecCopy(rk->Y[0],X);CHKERRQ(ierr);
554f68a32c8SEmil Constantinescu   ierr = VecMAXPY(X,s,b,rk->YdotRHS);CHKERRQ(ierr);
555f68a32c8SEmil Constantinescu   ierr = PetscFree(b);CHKERRQ(ierr);
556e4dd521cSBarry Smith   PetscFunctionReturn(0);
557e4dd521cSBarry Smith }
558e4dd521cSBarry Smith 
559e4dd521cSBarry Smith /*------------------------------------------------------------*/
560e4dd521cSBarry Smith #undef __FUNCT__
561277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset_RK"
562277b19d0SLisandro Dalcin static PetscErrorCode TSReset_RK(TS ts)
563e4dd521cSBarry Smith {
5645f70b478SJed Brown   TS_RK         *rk = (TS_RK*)ts->data;
565f68a32c8SEmil Constantinescu   PetscInt       s;
5666849ba73SBarry Smith   PetscErrorCode ierr;
567e4dd521cSBarry Smith 
568e4dd521cSBarry Smith   PetscFunctionBegin;
569f68a32c8SEmil Constantinescu   if (!rk->tableau) PetscFunctionReturn(0);
570f68a32c8SEmil Constantinescu   s    = rk->tableau->s;
571f68a32c8SEmil Constantinescu   ierr = VecDestroyVecs(s,&rk->Y);CHKERRQ(ierr);
572f68a32c8SEmil Constantinescu   ierr = VecDestroyVecs(s,&rk->YdotRHS);CHKERRQ(ierr);
573*c235aa8dSHong Zhang   if(ts->reverse_mode) {
574*c235aa8dSHong Zhang     ierr = VecDestroyVecs(s,&rk->VecDeltaLam);CHKERRQ(ierr);
575*c235aa8dSHong Zhang     ierr = VecDestroy(&rk->VecSensiTemp);CHKERRQ(ierr);
576*c235aa8dSHong Zhang   }
577f68a32c8SEmil Constantinescu   ierr = PetscFree(rk->work);CHKERRQ(ierr);
578277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
579e4dd521cSBarry Smith }
580277b19d0SLisandro Dalcin 
581277b19d0SLisandro Dalcin #undef __FUNCT__
582277b19d0SLisandro Dalcin #define __FUNCT__ "TSDestroy_RK"
583277b19d0SLisandro Dalcin static PetscErrorCode TSDestroy_RK(TS ts)
584277b19d0SLisandro Dalcin {
585277b19d0SLisandro Dalcin   PetscErrorCode ierr;
586277b19d0SLisandro Dalcin 
587277b19d0SLisandro Dalcin   PetscFunctionBegin;
588277b19d0SLisandro Dalcin   ierr = TSReset_RK(ts);CHKERRQ(ierr);
589277b19d0SLisandro Dalcin   ierr = PetscFree(ts->data);CHKERRQ(ierr);
590f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKGetType_C",NULL);CHKERRQ(ierr);
591f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKSetType_C",NULL);CHKERRQ(ierr);
592f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
593f68a32c8SEmil Constantinescu }
594f68a32c8SEmil Constantinescu 
595f68a32c8SEmil Constantinescu 
596f68a32c8SEmil Constantinescu #undef __FUNCT__
597f68a32c8SEmil Constantinescu #define __FUNCT__ "DMCoarsenHook_TSRK"
598f68a32c8SEmil Constantinescu static PetscErrorCode DMCoarsenHook_TSRK(DM fine,DM coarse,void *ctx)
599f68a32c8SEmil Constantinescu {
600f68a32c8SEmil Constantinescu   PetscFunctionBegin;
601f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
602f68a32c8SEmil Constantinescu }
603f68a32c8SEmil Constantinescu 
604f68a32c8SEmil Constantinescu #undef __FUNCT__
605f68a32c8SEmil Constantinescu #define __FUNCT__ "DMRestrictHook_TSRK"
606f68a32c8SEmil Constantinescu static PetscErrorCode DMRestrictHook_TSRK(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
607f68a32c8SEmil Constantinescu {
608f68a32c8SEmil Constantinescu   PetscFunctionBegin;
609f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
610f68a32c8SEmil Constantinescu }
611f68a32c8SEmil Constantinescu 
612f68a32c8SEmil Constantinescu 
613f68a32c8SEmil Constantinescu #undef __FUNCT__
614f68a32c8SEmil Constantinescu #define __FUNCT__ "DMSubDomainHook_TSRK"
615f68a32c8SEmil Constantinescu static PetscErrorCode DMSubDomainHook_TSRK(DM dm,DM subdm,void *ctx)
616f68a32c8SEmil Constantinescu {
617f68a32c8SEmil Constantinescu   PetscFunctionBegin;
618f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
619f68a32c8SEmil Constantinescu }
620f68a32c8SEmil Constantinescu 
621f68a32c8SEmil Constantinescu #undef __FUNCT__
622f68a32c8SEmil Constantinescu #define __FUNCT__ "DMSubDomainRestrictHook_TSRK"
623f68a32c8SEmil Constantinescu static PetscErrorCode DMSubDomainRestrictHook_TSRK(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
624f68a32c8SEmil Constantinescu {
625f68a32c8SEmil Constantinescu 
626f68a32c8SEmil Constantinescu   PetscFunctionBegin;
627f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
628f68a32c8SEmil Constantinescu }
629*c235aa8dSHong Zhang /*
630f68a32c8SEmil Constantinescu #undef __FUNCT__
631d2daff3dSHong Zhang #define __FUNCT__ "RKSetAdjCoe"
632d2daff3dSHong Zhang static PetscErrorCode RKSetAdjCoe(RKTableau tab)
633d2daff3dSHong Zhang {
634d2daff3dSHong Zhang   PetscReal *A,*b;
635d2daff3dSHong Zhang   PetscInt        s,i,j;
636d2daff3dSHong Zhang   PetscErrorCode  ierr;
637d2daff3dSHong Zhang 
638d2daff3dSHong Zhang   PetscFunctionBegin;
639d2daff3dSHong Zhang   s    = tab->s;
640d2daff3dSHong Zhang   ierr = PetscMalloc2(s*s,&A,s,&b);CHKERRQ(ierr);
641d2daff3dSHong Zhang 
642d2daff3dSHong Zhang   for (i=0; i<s; i++)
643d2daff3dSHong Zhang     for (j=0; j<s; j++) {
644d2daff3dSHong 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];
645d2daff3dSHong Zhang       ierr = PetscPrintf(PETSC_COMM_WORLD,"Coefficients: A[%D][%D]=%.6f\n",i,j,A[i*s+j]);CHKERRQ(ierr);
646d2daff3dSHong Zhang     }
647d2daff3dSHong Zhang 
648d2daff3dSHong Zhang   for (i=0; i<s; i++) b[i] = (tab->b[s-1-i]==0)? 0: -tab->b[s-1-i];
649d2daff3dSHong Zhang 
650d2daff3dSHong Zhang   ierr  = PetscMemcpy(tab->A,A,s*s*sizeof(A[0]));CHKERRQ(ierr);
651d2daff3dSHong Zhang   ierr  = PetscMemcpy(tab->b,b,s*sizeof(b[0]));CHKERRQ(ierr);
652d2daff3dSHong Zhang   ierr  = PetscFree2(A,b);CHKERRQ(ierr);
653d2daff3dSHong Zhang   PetscFunctionReturn(0);
654d2daff3dSHong Zhang }
655*c235aa8dSHong Zhang */
656d2daff3dSHong Zhang #undef __FUNCT__
657f68a32c8SEmil Constantinescu #define __FUNCT__ "TSSetUp_RK"
658f68a32c8SEmil Constantinescu static PetscErrorCode TSSetUp_RK(TS ts)
659f68a32c8SEmil Constantinescu {
660f68a32c8SEmil Constantinescu   TS_RK         *rk = (TS_RK*)ts->data;
661f68a32c8SEmil Constantinescu   RKTableau      tab;
662f68a32c8SEmil Constantinescu   PetscInt       s;
663f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
664f68a32c8SEmil Constantinescu   DM             dm;
665f68a32c8SEmil Constantinescu 
666f68a32c8SEmil Constantinescu   PetscFunctionBegin;
667f68a32c8SEmil Constantinescu   if (!rk->tableau) {
668f68a32c8SEmil Constantinescu     ierr = TSRKSetType(ts,TSRKDefault);CHKERRQ(ierr);
669f68a32c8SEmil Constantinescu   }
670f68a32c8SEmil Constantinescu   tab  = rk->tableau;
671f68a32c8SEmil Constantinescu   s    = tab->s;
672*c235aa8dSHong Zhang   // old
673*c235aa8dSHong Zhang   //if (ts->reverse_mode) {
674*c235aa8dSHong Zhang   //  ierr = RKSetAdjCoe(tab);CHKERRQ(ierr);
675*c235aa8dSHong Zhang   //}
676f68a32c8SEmil Constantinescu   ierr = VecDuplicateVecs(ts->vec_sol,s,&rk->Y);CHKERRQ(ierr);
677f68a32c8SEmil Constantinescu   ierr = VecDuplicateVecs(ts->vec_sol,s,&rk->YdotRHS);CHKERRQ(ierr);
678*c235aa8dSHong Zhang   if (ts->reverse_mode) {
679*c235aa8dSHong Zhang     ierr = VecDuplicateVecs(ts->vec_sensi,s,&rk->VecDeltaLam);CHKERRQ(ierr);
680*c235aa8dSHong Zhang     ierr = VecDuplicate(ts->vec_sensi,&rk->VecSensiTemp);CHKERRQ(ierr);
681*c235aa8dSHong Zhang   }
682785e854fSJed Brown   ierr = PetscMalloc1(s,&rk->work);CHKERRQ(ierr);
683f68a32c8SEmil Constantinescu   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
684f68a32c8SEmil Constantinescu   if (dm) {
685f68a32c8SEmil Constantinescu     ierr = DMCoarsenHookAdd(dm,DMCoarsenHook_TSRK,DMRestrictHook_TSRK,ts);CHKERRQ(ierr);
686f68a32c8SEmil Constantinescu     ierr = DMSubDomainHookAdd(dm,DMSubDomainHook_TSRK,DMSubDomainRestrictHook_TSRK,ts);CHKERRQ(ierr);
687f68a32c8SEmil Constantinescu   }
688e4dd521cSBarry Smith   PetscFunctionReturn(0);
689e4dd521cSBarry Smith }
690d2daff3dSHong Zhang 
691e4dd521cSBarry Smith /*------------------------------------------------------------*/
692e4dd521cSBarry Smith 
693e4dd521cSBarry Smith #undef __FUNCT__
6945f70b478SJed Brown #define __FUNCT__ "TSSetFromOptions_RK"
6958c34d3f5SBarry Smith static PetscErrorCode TSSetFromOptions_RK(PetscOptions *PetscOptionsObject,TS ts)
696e4dd521cSBarry Smith {
697dfbe8321SBarry Smith   PetscErrorCode ierr;
698f68a32c8SEmil Constantinescu   char           rktype[256];
699262ff7bbSBarry Smith 
700e4dd521cSBarry Smith   PetscFunctionBegin;
701e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"RK ODE solver options");CHKERRQ(ierr);
702f68a32c8SEmil Constantinescu   {
703f68a32c8SEmil Constantinescu     RKTableauLink  link;
704f68a32c8SEmil Constantinescu     PetscInt       count,choice;
705f68a32c8SEmil Constantinescu     PetscBool      flg;
706f68a32c8SEmil Constantinescu     const char   **namelist;
707f68a32c8SEmil Constantinescu     ierr = PetscStrncpy(rktype,TSRKDefault,sizeof(rktype));CHKERRQ(ierr);
708f68a32c8SEmil Constantinescu     for (link=RKTableauList,count=0; link; link=link->next,count++) ;
709785e854fSJed Brown     ierr = PetscMalloc1(count,&namelist);CHKERRQ(ierr);
710f68a32c8SEmil Constantinescu     for (link=RKTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
711f68a32c8SEmil Constantinescu     ierr      = PetscOptionsEList("-ts_rk_type","Family of RK method","TSRKSetType",(const char*const*)namelist,count,rktype,&choice,&flg);CHKERRQ(ierr);
712f68a32c8SEmil Constantinescu     ierr      = TSRKSetType(ts,flg ? namelist[choice] : rktype);CHKERRQ(ierr);
713f68a32c8SEmil Constantinescu     ierr      = PetscFree(namelist);CHKERRQ(ierr);
714f68a32c8SEmil Constantinescu   }
715262ff7bbSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
716e4dd521cSBarry Smith   PetscFunctionReturn(0);
717e4dd521cSBarry Smith }
718e4dd521cSBarry Smith 
719e4dd521cSBarry Smith #undef __FUNCT__
720f68a32c8SEmil Constantinescu #define __FUNCT__ "PetscFormatRealArray"
721f68a32c8SEmil Constantinescu static PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[])
722f68a32c8SEmil Constantinescu {
723f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
724f68a32c8SEmil Constantinescu   PetscInt       i;
725f68a32c8SEmil Constantinescu   size_t         left,count;
726f68a32c8SEmil Constantinescu   char           *p;
727f68a32c8SEmil Constantinescu 
728f68a32c8SEmil Constantinescu   PetscFunctionBegin;
729f68a32c8SEmil Constantinescu   for (i=0,p=buf,left=len; i<n; i++) {
730f68a32c8SEmil Constantinescu     ierr = PetscSNPrintfCount(p,left,fmt,&count,x[i]);CHKERRQ(ierr);
731f68a32c8SEmil Constantinescu     if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer");
732f68a32c8SEmil Constantinescu     left -= count;
733f68a32c8SEmil Constantinescu     p    += count;
734f68a32c8SEmil Constantinescu     *p++  = ' ';
735f68a32c8SEmil Constantinescu   }
736f68a32c8SEmil Constantinescu   p[i ? 0 : -1] = 0;
737f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
738f68a32c8SEmil Constantinescu }
739f68a32c8SEmil Constantinescu 
740f68a32c8SEmil Constantinescu #undef __FUNCT__
7415f70b478SJed Brown #define __FUNCT__ "TSView_RK"
7425f70b478SJed Brown static PetscErrorCode TSView_RK(TS ts,PetscViewer viewer)
743e4dd521cSBarry Smith {
7445f70b478SJed Brown   TS_RK         *rk   = (TS_RK*)ts->data;
745f68a32c8SEmil Constantinescu   RKTableau      tab  = rk->tableau;
7468370ee3bSLisandro Dalcin   PetscBool      iascii;
747dfbe8321SBarry Smith   PetscErrorCode ierr;
748f68a32c8SEmil Constantinescu   TSAdapt        adapt;
7492cdf8120Sasbjorn 
750e4dd521cSBarry Smith   PetscFunctionBegin;
751251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7528370ee3bSLisandro Dalcin   if (iascii) {
753f68a32c8SEmil Constantinescu     TSRKType rktype;
754f68a32c8SEmil Constantinescu     char     buf[512];
755f68a32c8SEmil Constantinescu     ierr = TSRKGetType(ts,&rktype);CHKERRQ(ierr);
756f68a32c8SEmil Constantinescu     ierr = PetscViewerASCIIPrintf(viewer,"  RK %s\n",rktype);CHKERRQ(ierr);
757f68a32c8SEmil Constantinescu     ierr = PetscFormatRealArray(buf,sizeof(buf),"% 8.6f",tab->s,tab->c);CHKERRQ(ierr);
758f68a32c8SEmil Constantinescu     ierr = PetscViewerASCIIPrintf(viewer,"  Abscissa     c = %s\n",buf);CHKERRQ(ierr);
759d760c35bSDebojyoti Ghosh     ierr = PetscViewerASCIIPrintf(viewer,"FSAL: %s\n",tab->FSAL ? "yes" : "no");CHKERRQ(ierr);
7608370ee3bSLisandro Dalcin   }
761f68a32c8SEmil Constantinescu   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
762f68a32c8SEmil Constantinescu   ierr = TSAdaptView(adapt,viewer);CHKERRQ(ierr);
763f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
764f68a32c8SEmil Constantinescu }
765f68a32c8SEmil Constantinescu 
766f68a32c8SEmil Constantinescu #undef __FUNCT__
767f68a32c8SEmil Constantinescu #define __FUNCT__ "TSLoad_RK"
768f68a32c8SEmil Constantinescu static PetscErrorCode TSLoad_RK(TS ts,PetscViewer viewer)
769f68a32c8SEmil Constantinescu {
770f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
771f68a32c8SEmil Constantinescu   TSAdapt        tsadapt;
772f68a32c8SEmil Constantinescu 
773f68a32c8SEmil Constantinescu   PetscFunctionBegin;
774f68a32c8SEmil Constantinescu   ierr = TSGetAdapt(ts,&tsadapt);CHKERRQ(ierr);
775f68a32c8SEmil Constantinescu   ierr = TSAdaptLoad(tsadapt,viewer);CHKERRQ(ierr);
776f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
777f68a32c8SEmil Constantinescu }
778f68a32c8SEmil Constantinescu 
779f68a32c8SEmil Constantinescu #undef __FUNCT__
780f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKSetType"
781f68a32c8SEmil Constantinescu /*@C
782f68a32c8SEmil Constantinescu   TSRKSetType - Set the type of RK scheme
783f68a32c8SEmil Constantinescu 
784f68a32c8SEmil Constantinescu   Logically collective
785f68a32c8SEmil Constantinescu 
786f68a32c8SEmil Constantinescu   Input Parameter:
787f68a32c8SEmil Constantinescu +  ts - timestepping context
788f68a32c8SEmil Constantinescu -  rktype - type of RK-scheme
789f68a32c8SEmil Constantinescu 
790f68a32c8SEmil Constantinescu   Level: intermediate
791f68a32c8SEmil Constantinescu 
792f68a32c8SEmil Constantinescu .seealso: TSRKGetType(), TSRK, TSRK2, TSRK3, TSRKPRSSP2, TSRK4, TSRK5
793f68a32c8SEmil Constantinescu @*/
794f68a32c8SEmil Constantinescu PetscErrorCode TSRKSetType(TS ts,TSRKType rktype)
795f68a32c8SEmil Constantinescu {
796f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
797f68a32c8SEmil Constantinescu 
798f68a32c8SEmil Constantinescu   PetscFunctionBegin;
799f68a32c8SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
800f68a32c8SEmil Constantinescu   ierr = PetscTryMethod(ts,"TSRKSetType_C",(TS,TSRKType),(ts,rktype));CHKERRQ(ierr);
801f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
802f68a32c8SEmil Constantinescu }
803f68a32c8SEmil Constantinescu 
804f68a32c8SEmil Constantinescu #undef __FUNCT__
805f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKGetType"
806f68a32c8SEmil Constantinescu /*@C
807f68a32c8SEmil Constantinescu   TSRKGetType - Get the type of RK scheme
808f68a32c8SEmil Constantinescu 
809f68a32c8SEmil Constantinescu   Logically collective
810f68a32c8SEmil Constantinescu 
811f68a32c8SEmil Constantinescu   Input Parameter:
812f68a32c8SEmil Constantinescu .  ts - timestepping context
813f68a32c8SEmil Constantinescu 
814f68a32c8SEmil Constantinescu   Output Parameter:
815f68a32c8SEmil Constantinescu .  rktype - type of RK-scheme
816f68a32c8SEmil Constantinescu 
817f68a32c8SEmil Constantinescu   Level: intermediate
818f68a32c8SEmil Constantinescu 
819f68a32c8SEmil Constantinescu .seealso: TSRKGetType()
820f68a32c8SEmil Constantinescu @*/
821f68a32c8SEmil Constantinescu PetscErrorCode TSRKGetType(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 = PetscUseMethod(ts,"TSRKGetType_C",(TS,TSRKType*),(ts,rktype));CHKERRQ(ierr);
828f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
829f68a32c8SEmil Constantinescu }
830f68a32c8SEmil Constantinescu 
831f68a32c8SEmil Constantinescu #undef __FUNCT__
832f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKGetType_RK"
833f68a32c8SEmil Constantinescu PetscErrorCode  TSRKGetType_RK(TS ts,TSRKType *rktype)
834f68a32c8SEmil Constantinescu {
835f68a32c8SEmil Constantinescu   TS_RK     *rk = (TS_RK*)ts->data;
836f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
837f68a32c8SEmil Constantinescu 
838f68a32c8SEmil Constantinescu   PetscFunctionBegin;
839f68a32c8SEmil Constantinescu   if (!rk->tableau) {
840f68a32c8SEmil Constantinescu     ierr = TSRKSetType(ts,TSRKDefault);CHKERRQ(ierr);
841f68a32c8SEmil Constantinescu   }
842f68a32c8SEmil Constantinescu   *rktype = rk->tableau->name;
843f68a32c8SEmil Constantinescu   PetscFunctionReturn(0);
844f68a32c8SEmil Constantinescu }
845f68a32c8SEmil Constantinescu #undef __FUNCT__
846f68a32c8SEmil Constantinescu #define __FUNCT__ "TSRKSetType_RK"
847f68a32c8SEmil Constantinescu PetscErrorCode  TSRKSetType_RK(TS ts,TSRKType rktype)
848f68a32c8SEmil Constantinescu {
849f68a32c8SEmil Constantinescu   TS_RK     *rk = (TS_RK*)ts->data;
850f68a32c8SEmil Constantinescu   PetscErrorCode ierr;
851f68a32c8SEmil Constantinescu   PetscBool      match;
852f68a32c8SEmil Constantinescu   RKTableauLink link;
853f68a32c8SEmil Constantinescu 
854f68a32c8SEmil Constantinescu   PetscFunctionBegin;
855f68a32c8SEmil Constantinescu   if (rk->tableau) {
856f68a32c8SEmil Constantinescu     ierr = PetscStrcmp(rk->tableau->name,rktype,&match);CHKERRQ(ierr);
857f68a32c8SEmil Constantinescu     if (match) PetscFunctionReturn(0);
858f68a32c8SEmil Constantinescu   }
859f68a32c8SEmil Constantinescu   for (link = RKTableauList; link; link=link->next) {
860f68a32c8SEmil Constantinescu     ierr = PetscStrcmp(link->tab.name,rktype,&match);CHKERRQ(ierr);
861f68a32c8SEmil Constantinescu     if (match) {
862f68a32c8SEmil Constantinescu       ierr = TSReset_RK(ts);CHKERRQ(ierr);
863f68a32c8SEmil Constantinescu       rk->tableau = &link->tab;
864f68a32c8SEmil Constantinescu       PetscFunctionReturn(0);
865f68a32c8SEmil Constantinescu     }
866f68a32c8SEmil Constantinescu   }
867f68a32c8SEmil Constantinescu   SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",rktype);
868e4dd521cSBarry Smith   PetscFunctionReturn(0);
869e4dd521cSBarry Smith }
870e4dd521cSBarry Smith 
871ff22ae23SHong Zhang #undef __FUNCT__
872ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages_RK"
873ff22ae23SHong Zhang static PetscErrorCode  TSGetStages_RK(TS ts,PetscInt *ns,Vec **Y)
874ff22ae23SHong Zhang {
875ff22ae23SHong Zhang   TS_RK          *rk = (TS_RK*)ts->data;
876ff22ae23SHong Zhang 
877ff22ae23SHong Zhang   PetscFunctionBegin;
878ff22ae23SHong Zhang   *ns = rk->tableau->s;
879d2daff3dSHong Zhang   if(Y) *Y  = rk->Y;
880ff22ae23SHong Zhang   PetscFunctionReturn(0);
881ff22ae23SHong Zhang }
882ff22ae23SHong Zhang 
883ff22ae23SHong Zhang 
884e4dd521cSBarry Smith /* ------------------------------------------------------------ */
885ebe3b25bSBarry Smith /*MC
886f68a32c8SEmil Constantinescu       TSRK - ODE and DAE solver using Runge-Kutta schemes
887ebe3b25bSBarry Smith 
888f68a32c8SEmil Constantinescu   The user should provide the right hand side of the equation
889f68a32c8SEmil Constantinescu   using TSSetRHSFunction().
890ebe3b25bSBarry Smith 
891f68a32c8SEmil Constantinescu   Notes:
892f68a32c8SEmil Constantinescu   The default is TSRK3, it can be changed with TSRKSetType() or -ts_rk_type
893ebe3b25bSBarry Smith 
894d41222bbSBarry Smith   Level: beginner
895d41222bbSBarry Smith 
896f68a32c8SEmil Constantinescu .seealso:  TSCreate(), TS, TSSetType(), TSRKSetType(), TSRKGetType(), TSRKSetFullyImplicit(), TSRK2D, TTSRK2E, TSRK3,
897f68a32c8SEmil Constantinescu            TSRK4, TSRK5, TSRKPRSSP2, TSRKBPR3, TSRKType, TSRKRegister()
898ebe3b25bSBarry Smith 
899ebe3b25bSBarry Smith M*/
900e4dd521cSBarry Smith #undef __FUNCT__
9015f70b478SJed Brown #define __FUNCT__ "TSCreate_RK"
9028cc058d9SJed Brown PETSC_EXTERN PetscErrorCode TSCreate_RK(TS ts)
903e4dd521cSBarry Smith {
904f68a32c8SEmil Constantinescu   TS_RK     *th;
905dfbe8321SBarry Smith   PetscErrorCode ierr;
906e4dd521cSBarry Smith 
907e4dd521cSBarry Smith   PetscFunctionBegin;
908f68a32c8SEmil Constantinescu #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
909f68a32c8SEmil Constantinescu   ierr = TSRKInitializePackage();CHKERRQ(ierr);
910f68a32c8SEmil Constantinescu #endif
911f68a32c8SEmil Constantinescu 
912f68a32c8SEmil Constantinescu   ts->ops->reset          = TSReset_RK;
9135f70b478SJed Brown   ts->ops->destroy        = TSDestroy_RK;
9145f70b478SJed Brown   ts->ops->view           = TSView_RK;
915f68a32c8SEmil Constantinescu   ts->ops->load           = TSLoad_RK;
916f68a32c8SEmil Constantinescu   ts->ops->setup          = TSSetUp_RK;
917f68a32c8SEmil Constantinescu   ts->ops->step           = TSStep_RK;
918f68a32c8SEmil Constantinescu   ts->ops->interpolate    = TSInterpolate_RK;
919f68a32c8SEmil Constantinescu   ts->ops->evaluatestep   = TSEvaluateStep_RK;
920f68a32c8SEmil Constantinescu   ts->ops->setfromoptions = TSSetFromOptions_RK;
921ff22ae23SHong Zhang   ts->ops->getstages      = TSGetStages_RK;
922d2daff3dSHong Zhang   ts->ops->stepadj        = TSStepAdj_RK;
923e4dd521cSBarry Smith 
924b00a9115SJed Brown   ierr = PetscNewLog(ts,&th);CHKERRQ(ierr);
925f68a32c8SEmil Constantinescu   ts->data = (void*)th;
926e4dd521cSBarry Smith 
927f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKGetType_C",TSRKGetType_RK);CHKERRQ(ierr);
928f68a32c8SEmil Constantinescu   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSRKSetType_C",TSRKSetType_RK);CHKERRQ(ierr);
929e4dd521cSBarry Smith   PetscFunctionReturn(0);
930e4dd521cSBarry Smith }
931