Numerical.cpp
Go to the documentation of this file.
00001 #include "tensor_field_nav_core/Numerical.h"
00002 
00003 
00005 void rkck(Vec_I_DP &y, Vec_I_DP &dydx, const DP x,
00006           const DP h, Vec_O_DP &yout, Vec_O_DP &yerr,
00007           void derivs(const DP, Vec_I_DP &, Vec_O_DP &))
00008 {
00009     static const DP a2=0.2, a3=0.3, a4=0.6, a5=1.0, a6=0.875,
00010         b21=0.2, b31=3.0/40.0, b32=9.0/40.0, b41=0.3, b42 = -0.9,
00011         b43=1.2, b51 = -11.0/54.0, b52=2.5, b53 = -70.0/27.0,
00012         b54=35.0/27.0, b61=1631.0/55296.0, b62=175.0/512.0,
00013         b63=575.0/13824.0, b64=44275.0/110592.0, b65=253.0/4096.0,
00014         c1=37.0/378.0, c3=250.0/621.0, c4=125.0/594.0, c6=512.0/1771.0,
00015         dc1=c1-2825.0/27648.0, dc3=c3-18575.0/48384.0,
00016         dc4=c4-13525.0/55296.0, dc5 = -277.00/14336.0, dc6=c6-0.25;
00017     int i;
00018 
00019     int n=y.size();
00020     Vec_DP ak2(n),ak3(n),ak4(n),ak5(n),ak6(n),ytemp(n);
00021     for (i=0;i<n;i++)
00022         ytemp[i]=y[i]+b21*h*dydx[i];
00023     derivs(x+a2*h,ytemp,ak2);
00024     for (i=0;i<n;i++)
00025         ytemp[i]=y[i]+h*(b31*dydx[i]+b32*ak2[i]);
00026     derivs(x+a3*h,ytemp,ak3);
00027     for (i=0;i<n;i++)
00028         ytemp[i]=y[i]+h*(b41*dydx[i]+b42*ak2[i]+b43*ak3[i]);
00029     derivs(x+a4*h,ytemp,ak4);
00030     for (i=0;i<n;i++)
00031         ytemp[i]=y[i]+h*(b51*dydx[i]+b52*ak2[i]+b53*ak3[i]+b54*ak4[i]);
00032     derivs(x+a5*h,ytemp,ak5);
00033     for (i=0;i<n;i++)
00034         ytemp[i]=y[i]+h*(b61*dydx[i]+b62*ak2[i]+b63*ak3[i]+b64*ak4[i]+b65*ak5[i]);
00035     derivs(x+a6*h,ytemp,ak6);
00036     for (i=0;i<n;i++)
00037         yout[i]=y[i]+h*(c1*dydx[i]+c3*ak3[i]+c4*ak4[i]+c6*ak6[i]);
00038     for (i=0;i<n;i++)
00039         yerr[i]=h*(dc1*dydx[i]+dc3*ak3[i]+dc4*ak4[i]+dc5*ak5[i]+dc6*ak6[i]);
00040 }
00041 
00042 void rkqs(Vec_IO_DP &y, Vec_IO_DP &dydx, DP &x, const DP htry,
00043           const DP eps, Vec_I_DP &yscal, DP &hdid, DP &hnext,
00044           void derivs(const DP, Vec_I_DP &, Vec_O_DP &))
00045 {
00046     const DP SAFETY=0.9, PGROW=-0.2, PSHRNK=-0.25, ERRCON=1.89e-4;
00047     int i;
00048     DP errmax,h,htemp,xnew;
00049 
00050     int n=y.size();
00051     h=htry;
00052     Vec_DP yerr(n),ytemp(n);
00053     for (;;) {
00054         rkck(y,dydx,x,h,ytemp,yerr,derivs);
00055         errmax=0.0;
00056         for (i=0;i<n;i++) errmax=max(errmax,fabs(yerr[i]/yscal[i]));
00057         errmax /= eps;
00058         if (errmax <= 1.0) break;
00059         htemp=SAFETY*h*pow(errmax,PSHRNK);
00060         h=(h >= 0.0 ? max(htemp,0.1*h) : min(htemp,0.1*h));
00061         xnew=x+h;
00062         if (xnew == x) printf("stepsize underflow in rkqs");
00063     }
00064     if (errmax > ERRCON) hnext=SAFETY*h*pow(errmax,PGROW);
00065     else hnext=5.0*h;
00066     x += (hdid=h);
00067     for (i=0;i<n;i++) y[i]=ytemp[i];
00068 }
00069 
00070 
00071 
00072 


tensor_field_nav_core
Author(s): Lintao Zheng, Kai Xu
autogenerated on Thu Jun 6 2019 19:50:56