#include "gtest/gtest.h"
TEST(Integrators, ABStiff) {
std::function<Eigen::VectorXd(double, const Eigen::VectorXd&)> func =
[](double, const Eigen::VectorXd& y) {
return -20*(y-2*Eigen::VectorXd::Ones(1));
};
Eigen::VectorXd y0(1); y0 << 3.0;
double tmax = 10.0;
double step = 0.005;
func, 0.0, tmax, step, y0);
auto stepper_ab1 = solver_ab1.begin();
auto stepper_ab2 = solver_ab2.begin();
auto stepper_ab3 = solver_ab3.begin();
auto stepper_ab4 = solver_ab4.begin();
auto stepper_ab5 = solver_ab5.begin();
while (stepper_ab4) {
++stepper_ab1;
++stepper_ab2;
++stepper_ab3;
++stepper_ab4;
++stepper_ab5;
}
double correct = 2.0 + std::exp(-20.0*stepper_ab4.time());
EXPECT_NEAR(correct, stepper_ab1.value()[0], 1e-4);
EXPECT_NEAR(correct, stepper_ab2.value()[0], 1e-5);
EXPECT_NEAR(correct, stepper_ab3.value()[0], 1e-6);
EXPECT_NEAR(correct, stepper_ab4.value()[0], 1e-7);
EXPECT_NEAR(correct, stepper_ab5.value()[0], 1e-8);
}
TEST(Integrators, ABCircle) {
std::function<Eigen::VectorXd(double, const Eigen::VectorXd&)> func =
[](double, const Eigen::VectorXd& y) {
Eigen::VectorXd r(2);
r(0) = -y(1);
r(1) = y(0);
return r;
};
double step = 0.1;
Eigen::VectorXd y0(2); y0 << 1.0, 0.0;
func, 0.0, tmax, step, y0);
auto stepper_ab1 = solver_ab1.begin();
auto stepper_ab2 = solver_ab2.begin();
auto stepper_ab3 = solver_ab3.begin();
auto stepper_ab4 = solver_ab4.begin();
auto stepper_ab5 = solver_ab5.begin();
while (stepper_ab4) {
++stepper_ab1;
++stepper_ab2;
++stepper_ab3;
++stepper_ab4;
++stepper_ab5;
}
double correctx = std::cos(stepper_ab4.time()), correcty = std::sin(stepper_ab4.time());
EXPECT_NEAR(correctx, stepper_ab4.value()[0], 5e-4);
EXPECT_NEAR(correcty, stepper_ab4.value()[1], 5e-4);
EXPECT_NEAR(correctx, stepper_ab3.value()[0], 5e-3);
EXPECT_NEAR(correcty, stepper_ab3.value()[1], 5e-3);
EXPECT_NEAR(correctx, stepper_ab2.value()[0], 5e-2);
EXPECT_NEAR(correcty, stepper_ab2.value()[1], 5e-2);
EXPECT_NEAR(correctx, stepper_ab1.value()[0], 1e-0);
EXPECT_NEAR(correcty, stepper_ab1.value()[1], 1e-0);
}
TEST(Integrators, ExplicitOfOrderAB) {
integrators::ExplicitMultistep::of_order<1>();
integrators::ExplicitMultistep::of_order<2>();
integrators::ExplicitMultistep::of_order<3>();
integrators::ExplicitMultistep::of_order<4>();
integrators::ExplicitMultistep::of_order<5>();
}
TEST(Integrators, DISABLED_ABUsageExample) {
std::function<Eigen::VectorXd(double, const Eigen::VectorXd&)> func =
[](double, const Eigen::VectorXd& y) {
Eigen::VectorXd r(2);
r(0) = -y(1);
r(1) = y(0);
return r;
};
double dt = 0.1;
Eigen::VectorXd y0(2); y0 << 1.0, 0.0;
std::cout << solver << std::endl;
for (auto step : solver) {
std::cout << step << std::endl;
if (step.is_last()) {
std::cout << "This is the last step." << std::endl;
}
step.time();
step.value();
}
}
}