[go: up one dir, main page]

Menu

[r303]: / libpetey / rk_dumb_ts.cc  Maximize  Restore  History

Download this file

108 lines (88 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
//"thread safe" version
namespace libpetey {
//derivative functions are compatible with the GSL
//(return value is ignored)
template <class ind_type, class dep_type>
dep_type **rk_dumb(ind_type t0, dep_type *xinit, long nx, ind_type dt, long nt, void *param,
int (* derivs) (ind_type, dep_type *, dep_type *, void *) ) {
dep_type **xvec;
ind_type t, tph;
dep_type dxall[nx], dx[nx];
dep_type xint[nx];
long i, j;
xvec=new dep_type * [nt+1];
xvec[0]=new dep_type [nx];
for (j=0;j<nx;j++) xvec[0][j]=xinit[j];
t=t0;
for (i=1;i<=nt;i++) {
//allocate space for the next vector of dep. values:
xvec[i]=new dep_type[nx];
//take the first set of derivatives:
(* derivs) (t, xvec[i-1], dx, param);
for (j=0;j<nx;j++) {
xint[j]=xvec[i-1][j]+dx[j]*dt/2; //calculate intermediate dep. values
dxall[j]=dx[j]/6; //start calculating the total deriv
}
//take the second set of derivatives:
tph=dt/2.;
tph=t+tph;
(* derivs) (tph, xint, dx, param);
for (j=0;j<nx;j++) {
xint[j]=xvec[i-1][j]+dx[j]*dt/2;
dxall[j]=dxall[j]+dx[j]/3;
}
//take the third set of derivatives:
(* derivs) (tph, xint, dx, param);
for (j=0;j<nx;j++) {
xint[j]=xvec[i-1][j]+dx[j]*dt;
dxall[j]=dxall[j]+dx[j]/3;
}
//take the fourth set of derivatives:
t=t+dt;
(* derivs) (t, xint, dx, param);
for (j=0;j<nx;j++) {
xvec[i][j]=xvec[i-1][j]+(dxall[j]+dx[j]/6)*dt; //calculate next vector
}
}
return xvec;
}
template <class ind_type, class dep_type>
void rk_dumb(ind_type t0, dep_type *xinit, long nx, ind_type dt, long nt,
dep_type ** xvec, void *param,
int (* derivs) (ind_type, dep_type *, dep_type *, void *) ) {
ind_type t, tph;
dep_type dxall[nx], dx[nx];
dep_type xint[nx];
long i, j;
for (j=0;j<nx;j++) xvec[0][j]=xinit[j];
t=t0;
for (i=1;i<=nt;i++) {
//take the first set of derivatives:
(* derivs) (t, xvec[i-1], dx, param);
for (j=0;j<nx;j++) {
xint[j]=xvec[i-1][j]+dx[j]*dt/2; //calculate intermediate dep. values
dxall[j]=dx[j]/6; //start calculating the total deriv
}
//take the second set of derivatives:
tph=dt/2.;
tph=t+tph;
(* derivs) (tph, xint, dx, param);
for (j=0;j<nx;j++) {
xint[j]=xvec[i-1][j]+dx[j]*dt/2;
dxall[j]=dxall[j]+dx[j]/3;
}
//take the third set of derivatives:
(* derivs) (tph, xint, dx, param);
for (j=0;j<nx;j++) {
xint[j]=xvec[i-1][j]+dx[j]*dt;
dxall[j]=dxall[j]+dx[j]/3;
}
//take the fourth set of derivatives:
t=t+dt;
(* derivs) (t, xint, dx, param);
for (j=0;j<nx;j++) {
xvec[i][j]=xvec[i-1][j]+(dxall[j]+dx[j]/6)*dt; //calculate next vector
}
}
}
} //end namespace libpetey