//======================================================================= // Chapter 9, code 3: a second-order ODE solved with adaptive RKF45. // Damped spring: mass*x'' + damping*x' + spring*x = 0. // Introduce velocity v = x': // x' = v // v' = -(spring/mass)*x - (damping/mass)*v // Position is y[0], velocity is y[1]; C++ indices start at zero. // // To use another physical model: // 1. Edit the parameters and initial data in the driver. // 2. Edit the acceleration equation in equations. // 3. Disable check_exact or replace the analytic comparison. // The adaptive RKF45 stages and step control need no changes. // h_max gives enough points for plotting; tolerance controls local error. //======================================================================= #include #include #include #include #include #include #include void equations(double t, const std::vector& y, std::vector& dy, double mass, double damping, double spring); bool rkf45_system(void (*f)(double,const std::vector&,std::vector&,double,double,double), int n, double t0, const std::vector& x0, double t_end, double h_first, double h_max, double tolerance, double mass, double damping, double spring, std::ofstream& data, std::vector& xf, int& accepted, int& rejected); int main() { //--------------- Parameters and initial data: edit here -------------- const int n = 2; double mass = 1.0; double spring = 1.0; double damping = 0.2; double t0 = 0.0; double t_end = 20.0; std::vector y0(n); y0[0] = 1.0; // Initial position y0[1] = 0.0; // Initial velocity double h_first = 0.1; double h_max = 0.1; // Maximum step for plotting the oscillations double tolerance = 1.0e-8; bool check_exact = true; // Analytic comparison for an underdamped spring //------------------------------------------------------------------- if (mass <= 0.0 || spring <= 0.0 || damping < 0.0) { std::cerr << "Use positive mass and spring, and nonnegative damping.\n"; return 1; } if (check_exact && damping*damping >= 4.0*mass*spring) { std::cerr << "Exact comparison requires an underdamped spring; " << "disable check_exact.\n"; return 1; } std::ofstream data("ch09_rkf45_spring.dat"); if (!data) { std::cerr << "Cannot open ch09_rkf45_spring.dat.\n"; return 1; } std::cout << "Adaptive RKF45: damped spring\n" << "Tolerance: " << std::scientific << std::setprecision(4) << tolerance << '\n'; data << std::scientific << std::setprecision(16); std::vector yf(n); int accepted, rejected; if (!rkf45_system(equations,n,t0,y0,t_end,h_first,h_max,tolerance, mass,damping,spring,data,yf,accepted,rejected)) return 1; data.close(); std::cout << "Accepted steps: " << std::setw(8) << accepted << '\n' << "Rejected trials:" << std::setw(8) << rejected << '\n'; if (check_exact) { // Exact underdamped solution, including zero damping. // tau measures time from the initial point. // x = exp(-gamma*tau)*(a*cos(omega*tau)+b*sin(omega*tau)). double gamma = damping/(2.0*mass); double omega = std::sqrt(spring/mass-gamma*gamma); double a = y0[0]; double b = (y0[1]+gamma*a)/omega; double tau = t_end-t0; double decay = std::exp(-gamma*tau); std::vector exact(n); exact[0] = decay*(a*std::cos(omega*tau)+b*std::sin(omega*tau)); exact[1] = -gamma*exact[0] + decay*omega*(-a*std::sin(omega*tau)+b*std::cos(omega*tau)); const char* names[] = {"position","velocity"}; std::cout << std::setw(10) << "variable" << ' ' << std::setw(18) << "numerical" << ' ' << std::setw(18) << "exact" << ' ' << std::setw(12) << "abs error" << '\n'; for (int j = 0; j < n; ++j) { std::cout << std::setw(10) << names[j] << ' ' << std::scientific << std::setprecision(10) << std::setw(18) << yf[j] << ' ' << std::setw(18) << exact[j] << ' ' << std::setprecision(4) << std::setw(12) << std::abs(yf[j]-exact[j]) << '\n'; } } std::cout << "Data file: ch09_rkf45_spring.dat\n"; return 0; } void equations(double t, const std::vector& y, std::vector& dy, double mass, double damping, double spring) { // y[0] = position, y[1] = velocity. Parameters are passed explicitly. // For another force, change the acceleration dy[1]. // t is available for a force that depends explicitly on time. (void)t; dy[0] = y[1]; dy[1] = -(spring/mass)*y[0]-(damping/mass)*y[1]; } bool rkf45_system(void (*f)(double,const std::vector&,std::vector&,double,double,double), int n, double t0, const std::vector& x0, double t_end, double h_first, double h_max, double tolerance, double mass, double damping, double spring, std::ofstream& data, std::vector& xf, int& accepted, int& rejected) { // Integrate the position/velocity system with adaptive Fehlberg RKF45. // h_max caps output spacing; accuracy may require smaller steps. // Use one absolute tolerance and the largest component error. // This assumes comparable component scales; rescale variables if needed. // Six stages give fourth- and fifth-order estimates. Advance with // the FIFTH-order value; their difference estimates local error. // The tolerance does not bound total accumulated error. // xf and the counters are returned through reference arguments (&). // Return false if the integration cannot be completed. const int max_attempts = 100000; const double h_min = 1.0e-12; if (n < 1 || t_end <= t0 || h_first <= 0.0 || h_max <= 0.0 || tolerance <= 0.0) { std::cerr << "Use n > 0, t_end > t0, positive steps, and positive tolerance.\n"; return false; } double t = t0; std::vector x = x0; std::vector work(n), dx(n), trial(n); std::vector k1(n), k2(n), k3(n), k4(n), k5(n), k6(n); double h = h_first; accepted = 0; rejected = 0; int attempts = 0; // Output: time, position, velocity, and the step used to reach the point. std::cout << std::setw(10) << "t" << ' ' << std::setw(18) << "position" << ' ' << std::setw(18) << "velocity" << ' ' << std::setw(18) << "h" << '\n'; data << "# t position velocity h\n"; std::cout << std::fixed << std::setprecision(6) << std::setw(10) << t; data << t; for (int j = 0; j < n; ++j) { std::cout << ' ' << std::scientific << std::setprecision(10) << std::setw(18) << x[j]; data << ' ' << x[j]; } std::cout << ' ' << std::setw(18) << 0.0 << '\n'; data << ' ' << 0.0 << '\n'; while (t < t_end) { // Ignore only a machine-rounding gap at the endpoint. double roundoff = 64.0*std::numeric_limits::epsilon() * std::max(1.0,std::abs(t_end)); if (t_end-t <= roundoff) break; if (attempts >= max_attempts) { std::cerr << "Maximum number of trials exceeded.\n"; return false; } h = std::min(h,std::min(h_max,t_end-t)); double tf = t+h; if (h < h_min || tf <= t) { std::cerr << "Step too small to advance time.\n"; return false; } ++attempts; // Fill the complete intermediate state before evaluating equations. f(t,x,dx,mass,damping,spring); for (int j = 0; j < n; ++j) { k1[j] = h*dx[j]; work[j] = x[j]+k1[j]/4.0; } f(t+h/4.0,work,dx,mass,damping,spring); for (int j = 0; j < n; ++j) { k2[j] = h*dx[j]; work[j] = x[j]+3.0*k1[j]/32.0+9.0*k2[j]/32.0; } f(t+3.0*h/8.0,work,dx,mass,damping,spring); for (int j = 0; j < n; ++j) { k3[j] = h*dx[j]; work[j] = x[j]+1932.0*k1[j]/2197.0-7200.0*k2[j]/2197.0 +7296.0*k3[j]/2197.0; } f(t+12.0*h/13.0,work,dx,mass,damping,spring); for (int j = 0; j < n; ++j) { k4[j] = h*dx[j]; work[j] = x[j]+439.0*k1[j]/216.0-8.0*k2[j] +3680.0*k3[j]/513.0-845.0*k4[j]/4104.0; } f(t+h,work,dx,mass,damping,spring); for (int j = 0; j < n; ++j) { k5[j] = h*dx[j]; work[j] = x[j]-8.0*k1[j]/27.0+2.0*k2[j]-3544.0*k3[j]/2565.0 +1859.0*k4[j]/4104.0-11.0*k5[j]/40.0; } f(t+h/2.0,work,dx,mass,damping,spring); for (int j = 0; j < n; ++j) k6[j] = h*dx[j]; // Fifth-order candidates and largest embedded component error. double error = 0.0; for (int j = 0; j < n; ++j) { trial[j] = x[j]+16.0*k1[j]/135.0+6656.0*k3[j]/12825.0 +28561.0*k4[j]/56430.0-9.0*k5[j]/50.0+2.0*k6[j]/55.0; double component_error = std::abs(k1[j]/360.0-128.0*k3[j]/4275.0 -2197.0*k4[j]/75240.0+k5[j]/50.0+2.0*k6[j]/55.0); error = std::max(error,component_error); } if (error <= tolerance) { if (h == t_end-t) tf = t_end; t = tf; x = trial; ++accepted; // h is the step used to reach this accepted point. std::cout << std::fixed << std::setprecision(6) << std::setw(10) << t; data << t; for (int j = 0; j < n; ++j) { std::cout << ' ' << std::scientific << std::setprecision(10) << std::setw(18) << x[j]; data << ' ' << x[j]; } std::cout << ' ' << std::setw(18) << h << '\n'; data << ' ' << h << '\n'; } else { ++rejected; // Keep both t and x unchanged. } // Embedded error scales approximately as h^5. // Safety factor 0.9; limit growth and shrinkage. double factor; if (error == 0.0) factor = 2.0; else factor = 0.9*std::pow(tolerance/error,0.2); if (error <= tolerance) h *= std::min(2.0,factor); else h *= std::max(0.2,factor); } xf = x; return true; }