2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2023-08-31 11:03:37 -07:00
|
|
|
#include "frc/EigenCore.h"
|
2021-01-06 21:40:25 -08:00
|
|
|
#include "frc/system/NumericalIntegration.h"
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2024-11-07 23:46:52 -08:00
|
|
|
// Test that integrating dx/dt = eˣ works
|
2021-01-06 21:40:25 -08:00
|
|
|
TEST(NumericalIntegrationTest, Exponential) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<1> y0{0.0};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<1> y1 = frc::RK4(
|
|
|
|
|
[](const frc::Vectord<1>& x) { return frc::Vectord<1>{std::exp(x(0))}; },
|
2020-08-14 23:40:33 -07:00
|
|
|
y0, 0.1_s);
|
|
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 23:46:52 -08:00
|
|
|
// Test that integrating dx/dt = eˣ works when we provide a u
|
2021-01-06 21:40:25 -08:00
|
|
|
TEST(NumericalIntegrationTest, ExponentialWithU) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<1> y0{0.0};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<1> y1 = frc::RK4(
|
|
|
|
|
[](const frc::Vectord<1>& x, const frc::Vectord<1>& u) {
|
|
|
|
|
return frc::Vectord<1>{std::exp(u(0) * x(0))};
|
2020-08-14 23:40:33 -07:00
|
|
|
},
|
2022-04-29 22:29:20 -07:00
|
|
|
y0, frc::Vectord<1>{1.0}, 0.1_s);
|
2020-08-14 23:40:33 -07:00
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 23:46:52 -08:00
|
|
|
// Tests RK4 with a time varying solution. From
|
|
|
|
|
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
|
|
|
|
//
|
|
|
|
|
// dx/dt = x (2 / (eᵗ + 1) - 1)
|
|
|
|
|
//
|
|
|
|
|
// The true (analytical) solution is:
|
|
|
|
|
//
|
|
|
|
|
// x(t) = 12eᵗ/(eᵗ + 1)²
|
|
|
|
|
TEST(NumericalIntegrationTest, RK4TimeVarying) {
|
|
|
|
|
frc::Vectord<1> y0{12.0 * std::exp(5.0) / std::pow(std::exp(5.0) + 1.0, 2.0)};
|
|
|
|
|
|
|
|
|
|
frc::Vectord<1> y1 = frc::RK4(
|
|
|
|
|
[](units::second_t t, const frc::Vectord<1>& x) {
|
|
|
|
|
return frc::Vectord<1>{x(0) *
|
|
|
|
|
(2.0 / (std::exp(t.value()) + 1.0) - 1.0)};
|
|
|
|
|
},
|
|
|
|
|
5_s, y0, 1_s);
|
|
|
|
|
EXPECT_NEAR(y1(0), 12.0 * std::exp(6.0) / std::pow(std::exp(6.0) + 1.0, 2.0),
|
|
|
|
|
1e-3);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 17:00:09 -07:00
|
|
|
// Tests that integrating dx/dt = 0 works with RKDP
|
|
|
|
|
TEST(NumericalIntegrationTest, ZeroRKDP) {
|
|
|
|
|
frc::Vectord<1> y1 = frc::RKDP(
|
|
|
|
|
[](const frc::Vectord<1>& x, const frc::Vectord<1>& u) {
|
|
|
|
|
return frc::Vectord<1>::Zero();
|
|
|
|
|
},
|
|
|
|
|
frc::Vectord<1>{0.0}, frc::Vectord<1>{0.0}, 0.1_s);
|
|
|
|
|
EXPECT_NEAR(y1(0), 0.0, 1e-3);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 23:46:52 -08:00
|
|
|
// Tests that integrating dx/dt = eˣ works with RKDP
|
2021-07-11 10:42:33 -04:00
|
|
|
TEST(NumericalIntegrationTest, ExponentialRKDP) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<1> y0{0.0};
|
2021-07-11 10:42:33 -04:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<1> y1 = frc::RKDP(
|
|
|
|
|
[](const frc::Vectord<1>& x, const frc::Vectord<1>& u) {
|
|
|
|
|
return frc::Vectord<1>{std::exp(x(0))};
|
2021-07-11 10:42:33 -04:00
|
|
|
},
|
2022-04-29 22:29:20 -07:00
|
|
|
y0, frc::Vectord<1>{0.0}, 0.1_s);
|
2021-07-11 10:42:33 -04:00
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
2021-01-06 21:40:25 -08:00
|
|
|
}
|
2024-11-07 23:46:52 -08:00
|
|
|
|
|
|
|
|
// Tests RKDP with a time varying solution. From
|
|
|
|
|
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
|
|
|
|
//
|
|
|
|
|
// dx/dt = x(2/(eᵗ + 1) - 1)
|
|
|
|
|
//
|
|
|
|
|
// The true (analytical) solution is:
|
|
|
|
|
//
|
|
|
|
|
// x(t) = 12eᵗ/(eᵗ + 1)²
|
|
|
|
|
TEST(NumericalIntegrationTest, RKDPTimeVarying) {
|
|
|
|
|
frc::Vectord<1> y0{12.0 * std::exp(5.0) / std::pow(std::exp(5.0) + 1.0, 2.0)};
|
|
|
|
|
|
|
|
|
|
frc::Vectord<1> y1 = frc::RKDP(
|
|
|
|
|
[](units::second_t t, const frc::Vectord<1>& x) {
|
|
|
|
|
return frc::Vectord<1>{x(0) *
|
|
|
|
|
(2.0 / (std::exp(t.value()) + 1.0) - 1.0)};
|
|
|
|
|
},
|
|
|
|
|
5_s, y0, 1_s, 1e-12);
|
|
|
|
|
EXPECT_NEAR(y1(0), 12.0 * std::exp(6.0) / std::pow(std::exp(6.0) + 1.0, 2.0),
|
|
|
|
|
1e-3);
|
|
|
|
|
}
|