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