This is a trivial test for the Oscillator.
#include <cmath>
#include <memory>
#include <gtest/gtest.h>
using std::shared_ptr;
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_real_distribution<> distr(min, max);
return distr(eng);
}
TEST(SecondOrderSystemTest, dynamicsTest)
{
try
{
shared_ptr<SecondOrderSystem> oscillator;
size_t nTests = 100;
for (
size_t i = 0;
i < nTests;
i++)
{
zeta = 0.0;
oscillator->checkParameters();
double w_d = std::sqrt(w_n * w_n - zeta * zeta);
double phase = 0;
double A = 1.0;
auto solution = [w_d, zeta, phase, A](
Time t) {
return A * std::sin(w_d *
t + phase); };
auto der = [w_d, zeta, phase, A](
Time t) {
return A * std::cos(w_d *
t + phase) * w_d; };
size_t nSamples = 100;
control.setZero();
for (size_t j = 0; j < nSamples; j++)
{
state(0) = solution(t);
state(1) = der(t);
oscillator->computeControlledDynamics(state, t, control, derivative);
double derivativeNumDiff;
double dtNumDiff = 1e-6;
derivativeNumDiff = (solution(t + dtNumDiff) - solution(t - dtNumDiff)) / (2 * dtNumDiff);
double derivativeAnalytic = der(t);
ASSERT_NEAR(derivative(0), derivativeNumDiff, 1e-3);
ASSERT_NEAR(derivativeAnalytic, derivativeNumDiff, 1e-3);
}
}
} catch (...)
{
std::cout << "Caught exception." << std::endl;
FAIL();
}
}
int main(
int argc,
char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}