Actual source code: rk.c
1: /*
2: * Code for Timestepping with Runge Kutta
3: *
4: * Written by
5: * Asbjorn Hoiland Aarrestad
6: * asbjorn@aarrestad.com
7: * http://asbjorn.aarrestad.com/
8: *
9: */
10: #include src/ts/tsimpl.h
11: #include "time.h"
13: typedef struct {
14: Vec y1,y2; /* work wectors for the two rk permuations */
15: PetscInt nok,nnok; /* counters for ok and not ok steps */
16: PetscReal maxerror; /* variable to tell the maxerror allowed */
17: PetscReal ferror; /* variable to tell (global maxerror)/(total time) */
18: PetscReal tolerance; /* initial value set for maxerror by user */
19: Vec tmp,tmp_y,*k; /* two temp vectors and the k vectors for rk */
20: PetscScalar a[7][6]; /* rk scalars */
21: PetscScalar b1[7],b2[7]; /* rk scalars */
22: PetscReal c[7]; /* rk scalars */
23: PetscInt p,s; /* variables to tell the size of the runge-kutta solver */
24: } TS_Rk;
29: PetscErrorCode TSRKSetTolerance_RK(TS ts,PetscReal aabs)
30: {
31: TS_Rk *rk = (TS_Rk*)ts->data;
32:
34: rk->tolerance = aabs;
35: return(0);
36: }
41: /*@
42: TSRKSetTolerance - Sets the total error the RK explicit time integrators
43: will allow over the given time interval.
45: Collective on TS
47: Input parameters:
48: + ts - the time-step context
49: - aabs - the absolute tolerance
51: Level: intermediate
53: .keywords: RK, tolerance
55: .seealso: TSPVodeSetTolerance()
57: @*/
58: PetscErrorCode TSRKSetTolerance(TS ts,PetscReal aabs)
59: {
60: PetscErrorCode ierr,(*f)(TS,PetscReal);
61:
63: PetscObjectQueryFunction((PetscObject)ts,"TSRKSetTolerance_C",(void (**)(void))&f);
64: if (f) {
65: (*f)(ts,aabs);
66: }
67: return(0);
68: }
73: static PetscErrorCode TSSetUp_Rk(TS ts)
74: {
75: TS_Rk *rk = (TS_Rk*)ts->data;
79: rk->nok = 0;
80: rk->nnok = 0;
81: rk->maxerror = rk->tolerance;
83: /* fixing maxerror: global vs local */
84: rk->ferror = rk->maxerror / (ts->max_time - ts->ptime);
86: /* 34.0/45.0 gives double precision division */
87: /* defining variables needed for Runge-Kutta computing */
88: /* when changing below, please remember to change a, b1, b2 and c above! */
89: /* Found in table on page 171: Dormand-Prince 5(4) */
91: /* are these right? */
92: rk->p=6;
93: rk->s=7;
95: rk->a[1][0]=1.0/5.0;
96: rk->a[2][0]=3.0/40.0;
97: rk->a[2][1]=9.0/40.0;
98: rk->a[3][0]=44.0/45.0;
99: rk->a[3][1]=-56.0/15.0;
100: rk->a[3][2]=32.0/9.0;
101: rk->a[4][0]=19372.0/6561.0;
102: rk->a[4][1]=-25360.0/2187.0;
103: rk->a[4][2]=64448.0/6561.0;
104: rk->a[4][3]=-212.0/729.0;
105: rk->a[5][0]=9017.0/3168.0;
106: rk->a[5][1]=-355.0/33.0;
107: rk->a[5][2]=46732.0/5247.0;
108: rk->a[5][3]=49.0/176.0;
109: rk->a[5][4]=-5103.0/18656.0;
110: rk->a[6][0]=35.0/384.0;
111: rk->a[6][1]=0.0;
112: rk->a[6][2]=500.0/1113.0;
113: rk->a[6][3]=125.0/192.0;
114: rk->a[6][4]=-2187.0/6784.0;
115: rk->a[6][5]=11.0/84.0;
118: rk->c[0]=0.0;
119: rk->c[1]=1.0/5.0;
120: rk->c[2]=3.0/10.0;
121: rk->c[3]=4.0/5.0;
122: rk->c[4]=8.0/9.0;
123: rk->c[5]=1.0;
124: rk->c[6]=1.0;
125:
126: rk->b1[0]=35.0/384.0;
127: rk->b1[1]=0.0;
128: rk->b1[2]=500.0/1113.0;
129: rk->b1[3]=125.0/192.0;
130: rk->b1[4]=-2187.0/6784.0;
131: rk->b1[5]=11.0/84.0;
132: rk->b1[6]=0.0;
134: rk->b2[0]=5179.0/57600.0;
135: rk->b2[1]=0.0;
136: rk->b2[2]=7571.0/16695.0;
137: rk->b2[3]=393.0/640.0;
138: rk->b2[4]=-92097.0/339200.0;
139: rk->b2[5]=187.0/2100.0;
140: rk->b2[6]=1.0/40.0;
141:
142:
143: /* Found in table on page 170: Fehlberg 4(5) */
144: /*
145: rk->p=5;
146: rk->s=6;
148: rk->a[1][0]=1.0/4.0;
149: rk->a[2][0]=3.0/32.0;
150: rk->a[2][1]=9.0/32.0;
151: rk->a[3][0]=1932.0/2197.0;
152: rk->a[3][1]=-7200.0/2197.0;
153: rk->a[3][2]=7296.0/2197.0;
154: rk->a[4][0]=439.0/216.0;
155: rk->a[4][1]=-8.0;
156: rk->a[4][2]=3680.0/513.0;
157: rk->a[4][3]=-845.0/4104.0;
158: rk->a[5][0]=-8.0/27.0;
159: rk->a[5][1]=2.0;
160: rk->a[5][2]=-3544.0/2565.0;
161: rk->a[5][3]=1859.0/4104.0;
162: rk->a[5][4]=-11.0/40.0;
164: rk->c[0]=0.0;
165: rk->c[1]=1.0/4.0;
166: rk->c[2]=3.0/8.0;
167: rk->c[3]=12.0/13.0;
168: rk->c[4]=1.0;
169: rk->c[5]=1.0/2.0;
171: rk->b1[0]=25.0/216.0;
172: rk->b1[1]=0.0;
173: rk->b1[2]=1408.0/2565.0;
174: rk->b1[3]=2197.0/4104.0;
175: rk->b1[4]=-1.0/5.0;
176: rk->b1[5]=0.0;
177:
178: rk->b2[0]=16.0/135.0;
179: rk->b2[1]=0.0;
180: rk->b2[2]=6656.0/12825.0;
181: rk->b2[3]=28561.0/56430.0;
182: rk->b2[4]=-9.0/50.0;
183: rk->b2[5]=2.0/55.0;
184: */
185: /* Found in table on page 169: Merson 4("5") */
186: /*
187: rk->p=4;
188: rk->s=5;
189: rk->a[1][0] = 1.0/3.0;
190: rk->a[2][0] = 1.0/6.0;
191: rk->a[2][1] = 1.0/6.0;
192: rk->a[3][0] = 1.0/8.0;
193: rk->a[3][1] = 0.0;
194: rk->a[3][2] = 3.0/8.0;
195: rk->a[4][0] = 1.0/2.0;
196: rk->a[4][1] = 0.0;
197: rk->a[4][2] = -3.0/2.0;
198: rk->a[4][3] = 2.0;
200: rk->c[0] = 0.0;
201: rk->c[1] = 1.0/3.0;
202: rk->c[2] = 1.0/3.0;
203: rk->c[3] = 0.5;
204: rk->c[4] = 1.0;
206: rk->b1[0] = 1.0/2.0;
207: rk->b1[1] = 0.0;
208: rk->b1[2] = -3.0/2.0;
209: rk->b1[3] = 2.0;
210: rk->b1[4] = 0.0;
212: rk->b2[0] = 1.0/6.0;
213: rk->b2[1] = 0.0;
214: rk->b2[2] = 0.0;
215: rk->b2[3] = 2.0/3.0;
216: rk->b2[4] = 1.0/6.0;
217: */
219: /* making b2 -> e=b1-b2 */
220: /*
221: for(i=0;i<rk->s;i++){
222: rk->b2[i] = (rk->b1[i]) - (rk->b2[i]);
223: }
224: */
225: rk->b2[0]=71.0/57600.0;
226: rk->b2[1]=0.0;
227: rk->b2[2]=-71.0/16695.0;
228: rk->b2[3]=71.0/1920.0;
229: rk->b2[4]=-17253.0/339200.0;
230: rk->b2[5]=22.0/525.0;
231: rk->b2[6]=-1.0/40.0;
233: /* initializing vectors */
234: VecDuplicate(ts->vec_sol,&rk->y1);
235: VecDuplicate(ts->vec_sol,&rk->y2);
236: VecDuplicate(rk->y1,&rk->tmp);
237: VecDuplicate(rk->y1,&rk->tmp_y);
238: VecDuplicateVecs(rk->y1,rk->s,&rk->k);
240: return(0);
241: }
243: /*------------------------------------------------------------*/
246: PetscErrorCode TSRkqs(TS ts,PetscReal t,PetscReal h)
247: {
248: TS_Rk *rk = (TS_Rk*)ts->data;
250: PetscInt j,l;
251: PetscReal tmp_t=t;
252: PetscScalar null=0.0,hh=h;
255: /* k[0]=0 */
256: VecSet(&null,rk->k[0]);
257:
258: /* k[0] = derivs(t,y1) */
259: TSComputeRHSFunction(ts,t,rk->y1,rk->k[0]);
260: /* looping over runge-kutta variables */
261: /* building the k - array of vectors */
262: for(j = 1 ; j < rk->s ; j++){
264: /* rk->tmp = 0 */
265: VecSet(&null,rk->tmp);
267: for(l=0;l<j;l++){
268: /* tmp += a(j,l)*k[l] */
269: VecAXPY(&rk->a[j][l],rk->k[l],rk->tmp);
270: }
272: /* VecView(rk->tmp,PETSC_VIEWER_STDOUT_WORLD); */
273:
274: /* k[j] = derivs(t+c(j)*h,y1+h*tmp,k(j)) */
275: /* I need the following helpers:
276: PetscScalar tmp_t=t+c(j)*h
277: Vec tmp_y=h*tmp+y1
278: */
280: tmp_t = t + rk->c[j] * h;
282: /* tmp_y = h * tmp + y1 */
283: VecWAXPY(&hh,rk->tmp,rk->y1,rk->tmp_y);
285: /* rk->k[j]=0 */
286: VecSet(&null,rk->k[j]);
287: TSComputeRHSFunction(ts,tmp_t,rk->tmp_y,rk->k[j]);
288: }
290: /* tmp=0 and tmp_y=0 */
291: VecSet(&null,rk->tmp);
292: VecSet(&null,rk->tmp_y);
293:
294: for(j = 0 ; j < rk->s ; j++){
295: /* tmp=b1[j]*k[j]+tmp */
296: VecAXPY(&rk->b1[j],rk->k[j],rk->tmp);
297: /* tmp_y=b2[j]*k[j]+tmp_y */
298: VecAXPY(&rk->b2[j],rk->k[j],rk->tmp_y);
299: }
301: /* y2 = hh * tmp_y */
302: VecSet(&null,rk->y2);
303: VecAXPY(&hh,rk->tmp_y,rk->y2);
304: /* y1 = hh*tmp + y1 */
305: VecAXPY(&hh,rk->tmp,rk->y1);
306: /* Finding difference between y1 and y2 */
308: return(0);
309: }
313: static PetscErrorCode TSStep_Rk(TS ts,PetscInt *steps,PetscReal *ptime)
314: {
315: TS_Rk *rk = (TS_Rk*)ts->data;
317: PetscReal dt = 0.001; /* fixed first step guess */
318: PetscReal norm=0.0,dt_fac=0.0,fac = 0.0/*,ttmp=0.0*/;
321: ierr=VecCopy(ts->vec_sol,rk->y1);
322: *steps = -ts->steps;
323: /* trying to save the vector */
324: TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);
325: /* while loop to get from start to stop */
326: while (ts->ptime < ts->max_time){
327: /* calling rkqs */
328: /*
329: -- input
330: ts - pointer to ts
331: ts->ptime - current time
332: dt - try this timestep
333: y1 - solution for this step
335: --output
336: y1 - suggested solution
337: y2 - check solution (runge - kutta second permutation)
338: */
339: TSRkqs(ts,ts->ptime,dt);
340: /* checking for maxerror */
341: /* comparing difference to maxerror */
342: VecNorm(rk->y2,NORM_2,&norm);
343: /* modifying maxerror to satisfy this timestep */
344: rk->maxerror = rk->ferror * dt;
345: /* PetscPrintf(PETSC_COMM_WORLD,"norm err: %f maxerror: %f dt: %f",norm,rk->maxerror,dt); */
347: /* handling ok and not ok */
348: if(norm < rk->maxerror){
349: /* if ok: */
350: ierr=VecCopy(rk->y1,ts->vec_sol); /* saves the suggested solution to current solution */
351: ts->ptime += dt; /* storing the new current time */
352: rk->nok++;
353: fac=5.0;
354: /* trying to save the vector */
355: /* calling monitor */
356: TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);
357: }else{
358: /* if not OK */
359: rk->nnok++;
360: fac=1.0;
361: ierr=VecCopy(ts->vec_sol,rk->y1); /* restores old solution */
362: }
364: /*Computing next stepsize. See page 167 in Solving ODE 1
365: *
366: * h_new = h * min( facmax , max( facmin , fac * (tol/err)^(1/(p+1)) ) )
367: * facmax set above
368: * facmin
369: */
370: dt_fac = exp(log((rk->maxerror) / norm) / ((rk->p) + 1) ) * 0.9 ;
372: if(dt_fac > fac){
373: /*PetscPrintf(PETSC_COMM_WORLD,"changing fac %f\n",fac);*/
374: dt_fac = fac;
375: }
377: /* computing new dt */
378: dt = dt * dt_fac;
380: if(ts->ptime+dt > ts->max_time){
381: dt = ts->max_time - ts->ptime;
382: }
384: if(dt < 1e-14){
385: PetscPrintf(PETSC_COMM_WORLD,"Very small steps: %f\n",dt);
386: dt = 1e-14;
387: }
389: /* trying to purify h */
390: /* (did not give any visible result) */
391: /* ttmp = ts->ptime + dt;
392: dt = ttmp - ts->ptime; */
393:
394: /* counting steps */
395: ts->steps++;
396: }
397:
398: ierr=VecCopy(rk->y1,ts->vec_sol);
399: *steps += ts->steps;
400: *ptime = ts->ptime;
401: return(0);
402: }
404: /*------------------------------------------------------------*/
407: static PetscErrorCode TSDestroy_Rk(TS ts)
408: {
409: TS_Rk *rk = (TS_Rk*)ts->data;
411: PetscInt i;
413: /* REMEMBER TO DESTROY ALL */
414:
416: if (rk->y1) {VecDestroy(rk->y1);}
417: if (rk->y2) {VecDestroy(rk->y2);}
418: if (rk->tmp) {VecDestroy(rk->tmp);}
419: if (rk->tmp_y) {VecDestroy(rk->tmp_y);}
420: for(i=0;i<rk->s;i++){
421: if (rk->k[i]) {VecDestroy(rk->k[i]);}
422: }
423: PetscFree(rk);
424: return(0);
425: }
426: /*------------------------------------------------------------*/
430: static PetscErrorCode TSSetFromOptions_Rk(TS ts)
431: {
432: TS_Rk *rk = (TS_Rk*)ts->data;
436: PetscOptionsHead("RK ODE solver options");
437: PetscOptionsReal("-ts_rk_tol","Tolerance for convergence","TSRKSetTolerance",rk->tolerance,&rk->tolerance,PETSC_NULL);
438: PetscOptionsTail();
439: return(0);
440: }
444: static PetscErrorCode TSView_Rk(TS ts,PetscViewer viewer)
445: {
446: TS_Rk *rk = (TS_Rk*)ts->data;
448:
450: PetscPrintf(PETSC_COMM_WORLD," number of ok steps: %D\n",rk->nok);
451: PetscPrintf(PETSC_COMM_WORLD," number of rejected steps: %D\n",rk->nnok);
452: return(0);
453: }
455: /* ------------------------------------------------------------ */
456: /*MC
457: TS_RK - ODE solver using the explicit Runge-Kutta methods
459: Options Database:
460: . -ts_rk_tol <tol> Tolerance for convergence
462: Contributed by: Asbjorn Hoiland Aarrestad, asbjorn@aarrestad.com, http://asbjorn.aarrestad.com/
464: Level: beginner
466: .seealso: TSCreate(), TS, TSSetType(), TS_EULER, TSRKSetTolerance()
468: M*/
473: PetscErrorCode TSCreate_Rk(TS ts)
474: {
475: TS_Rk *rk;
479: ts->ops->setup = TSSetUp_Rk;
480: ts->ops->step = TSStep_Rk;
481: ts->ops->destroy = TSDestroy_Rk;
482: ts->ops->setfromoptions = TSSetFromOptions_Rk;
483: ts->ops->view = TSView_Rk;
485: PetscNew(TS_Rk,&rk);
486: PetscLogObjectMemory(ts,sizeof(TS_Rk));
487: ts->data = (void*)rk;
489: PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSRKSetTolerance_C","TSRKSetTolerance_RK",TSRKSetTolerance_RK);
491: return(0);
492: }