14 const long N =
static_cast<long>(xold.size());
22 bool forwards_integration = ((tmax - tmin) > 0);
24 if (!forwards_integration) {
28 double max_error = NAN;
30 std::vector<double> xnew1(N), xnew2(N), xnew3(N), xnew4(N), xnew5(N), f1(N), f2(N), f3(N), f4(N), f5(N), f6(N), error(N), xnew(N);
41 bool stepAccepted =
false, disableAdaptive =
false;
43 while (!stepAccepted) {
46 disableAdaptive =
false;
50 if (forwards_integration && (t0 + h > tmax)) {
51 disableAdaptive =
true;
54 if (!forwards_integration && (t0 + h < tmax)) {
55 disableAdaptive =
true;
66 Eigen::Map<Eigen::VectorXd> xold_w(&(xold[0]), N);
68 if (std::abs(h) < hmin && !disableAdaptive) {
70 h = (forwards_integration) ? hmin : -hmin;
71 disableAdaptive =
true;
80 Eigen::Map<Eigen::VectorXd> xnew1_w(&(xnew1[0]), N), f1_w(&(f1[0]), N);
81 xnew1_w = xold_w + h * (1.0 / 5.0) * f1_w;
83 ode.
derivs(t0 + 1.0 / 5.0 * h, xnew1, f2);
84 Eigen::Map<Eigen::VectorXd> xnew2_w(&(xnew2[0]), N), f2_w(&(f2[0]), N);
85 xnew2_w = xold_w + h * (+3.0 / 40.0 * f1_w + 9.0 / 40.0 * f2_w);
87 ode.
derivs(t0 + 3.0 / 10.0 * h, xnew2, f3);
88 Eigen::Map<Eigen::VectorXd> xnew3_w(&(xnew3[0]), N), f3_w(&(f3[0]), N);
89 xnew3_w = xold_w + h * (3.0 / 10.0 * f1_w - 9.0 / 10.0 * f2_w + 6.0 / 5.0 * f3_w);
91 ode.
derivs(t0 + 3.0 / 5.0 * h, xnew3, f4);
92 Eigen::Map<Eigen::VectorXd> xnew4_w(&(xnew4[0]), N), f4_w(&(f4[0]), N);
93 xnew4_w = xold_w + h * (-11.0 / 54.0 * f1_w + 5.0 / 2.0 * f2_w - 70.0 / 27.0 * f3_w + 35.0 / 27.0 * f4_w);
95 ode.
derivs(t0 + h, xnew4, f5);
96 Eigen::Map<Eigen::VectorXd> xnew5_w(&(xnew5[0]), N), f5_w(&(f5[0]), N);
99 + h * (1631.0 / 55296 * f1_w + 175.0 / 512.0 * f2_w + 575.0 / 13824.0 * f3_w + 44275.0 / 110592.0 * f4_w + 253.0 / 4096.0 * f5_w);
102 ode.
derivs(t0 + 7.0 / 8.0 * h, xnew5, f6);
103 Eigen::Map<Eigen::VectorXd> xnew_w(&(xnew[0]), N), f6_w(&(f6[0]), N);
104 xnew_w = xold_w + h * (37.0 / 378.0 * f1_w + 250.0 / 621.0 * f3_w + 125.0 / 594.0 * f4_w + 512.0 / 1771.0 * f6_w);
106 Eigen::Map<Eigen::VectorXd> error_w(&(error[0]), N);
109 * (-277.0 / 64512.0 * f1_w + 6925.0 / 370944.0 * f3_w - 6925.0 / 202752.0 * f4_w - 277.0 / 14336.0 * f5_w + 277.0 / 7084.0 * f6_w);
111 max_error = error_w.norm();
115 if (disableAdaptive) {
120 if (max_error > eps_allowed) {
124 h *= std::min(step_relax * pow(eps_allowed / max_error, 0.3), 0.999);
125 stepAccepted =
false;
132 std::cout <<
format(
"accepted");
144 if (max_error < eps_allowed && disableAdaptive == false && max_error > 0) {
147 h *= step_relax * pow(eps_allowed / max_error, 0.2);
151 if (forwards_integration) {
152 h = std::min(h, hmax);
154 h = -std::min(std::abs(h), hmax);
158 if (forwards_integration && (t0 - tmax > +1e-3)) {
161 if (!forwards_integration && (t0 - tmax < -1e-3)) {
164 }
while (((forwards_integration) && t0 < tmax - 1e-10) || ((!forwards_integration) && t0 > tmax + 1e-10));
170#if defined(ENABLE_CATCH)
171# include <catch2/catch_all.hpp>
173TEST_CASE(
"Integrate y'=y",
"[ODEIntegrator]") {
177 std::vector<double> t, h, y;
180 return std::vector<double>(1, 1);
188 this->t.push_back(t);
189 this->h.push_back(h);
190 this->y.push_back(y[0]);
197 void derivs(
double t, std::vector<double>& y, std::vector<double>& yprime)
override {
202 SimpleODEIntegrator simple;
204 double yfinal_integration = simple.y[simple.y.size() - 1];
205 double tfinal_integration = simple.t[simple.t.size() - 1];
207 double yfinal_analytic = exp(4.0);
208 double error = yfinal_integration / yfinal_analytic - 1;
210 CAPTURE(yfinal_analytic);
211 CAPTURE(yfinal_integration);
212 CAPTURE(tfinal_integration);
213 CHECK(std::abs(error) < 1e-6);
214 CHECK(std::abs(tfinal_integration - 4) < 1e-10);