Skip to content

Commit

Permalink
passing the tests in osx locally
Browse files Browse the repository at this point in the history
  • Loading branch information
darioizzo committed Sep 20, 2023
1 parent d45eaee commit f185603
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .clangd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CompileFlags:
Add: '-std=c++17'
Add: '-std=c++20'
10 changes: 1 addition & 9 deletions include/kep3/epoch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,7 @@ kep3_DLL_PUBLIC epoch epoch_from_iso_string(const std::string &date);

kep3_DLL_PUBLIC std::ostream &operator<<(std::ostream &s,
const epoch &epoch_in);
kep3_DLL_PUBLIC bool operator>(const epoch &c1, const epoch &c2);
kep3_DLL_PUBLIC bool operator<(const epoch &c1, const epoch &c2);
kep3_DLL_PUBLIC bool operator>=(const epoch &c1, const epoch &c2);
kep3_DLL_PUBLIC bool operator<=(const epoch &c1, const epoch &c2);
kep3_DLL_PUBLIC bool operator==(const epoch &c1, const epoch &c2);
kep3_DLL_PUBLIC bool operator!=(const epoch &c1, const epoch &c2);
kep3_DLL_PUBLIC epoch operator+(epoch lhs, double rhs);
kep3_DLL_PUBLIC epoch operator-(epoch lhs, double rhs);
kep3_DLL_PUBLIC double operator-(const epoch &lhs, const epoch &rhs);


} // end of namespace kep3

Expand Down
83 changes: 31 additions & 52 deletions test/propagate_lagrangian_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

#include <boost/math/constants/constants.hpp>

#include <kep3/core_astro/constants.hpp>
#include <kep3/core_astro/ic2par2ic.hpp>
#include <kep3/core_astro/kepler_equations.hpp>
#include <kep3/core_astro/propagate_lagrangian.hpp>

#include "catch.hpp"
#include "test_helpers.hpp"

using Catch::Matchers::WithinAbs;
using kep3::propagate_lagrangian;
Expand Down Expand Up @@ -87,42 +89,16 @@ void test_propagate_lagrangian(
// Engines
// NOLINTNEXTLINE(cert-msc32-c, cert-msc51-cpp)
std::mt19937 rng_engine(12201203u);
{ // Random istribution of the initial Cartesian state (will mainly produce
// hyperbolas)
std::uniform_real_distribution<double> r_d(-2., 2.);
std::uniform_real_distribution<double> v_d(-2., 2.);
std::uniform_real_distribution<double> time(0.1, 20.);
// Testing on N random calls
for (auto i = 0u; i < N; ++i) {
std::array<double, 3> pos = {r_d(rng_engine), r_d(rng_engine),
r_d(rng_engine)};
std::array<double, 3> vel = {v_d(rng_engine), v_d(rng_engine),
v_d(rng_engine)};
std::array<std::array<double, 3>, 2> pos_vel = {pos, vel};
auto par_before = kep3::ic2par(pos_vel, 1.0);
// We filter out cases of little significance (too close to singularity)
if (std::abs(par_before[0]) > 0.5 && std::abs(par_before[0]) < 10. &&
std::abs(1 - par_before[1]) > 1e-1) {
propagate(pos_vel, time(rng_engine), 1.);
auto par_after = kep3::ic2par(pos_vel, 1.0);
if (std::isfinite(par_before[0]) && std::isfinite(par_after[0])) {
REQUIRE_THAT(par_before[0], WithinAbs(par_after[0], 1e-8));
REQUIRE_THAT(par_before[1], WithinAbs(par_after[1], 1e-8));
REQUIRE_THAT(par_before[2], WithinAbs(par_after[2], 1e-8));
REQUIRE_THAT(par_before[3], WithinAbs(par_after[3], 1e-8));
REQUIRE_THAT(par_before[4], WithinAbs(par_after[4], 1e-8));
}
}
}
}

{ // Targeting Ellipses
std::uniform_real_distribution<double> sma_d(1.1, 100.);
std::uniform_real_distribution<double> ecc_d(0, 0.99);
std::uniform_real_distribution<double> sma_d(1.1, 10.);
std::uniform_real_distribution<double> ecc_d(0, 0.9);
std::uniform_real_distribution<double> incl_d(0., pi);
std::uniform_real_distribution<double> Omega_d(0, 2 * pi);
std::uniform_real_distribution<double> omega_d(0., pi);
std::uniform_real_distribution<double> f_d(0, 2 * pi);
std::uniform_real_distribution<double> time_d(0.1, 20.);
std::uniform_real_distribution<double> time_d(-2. * kep3::pi,
2. * kep3::pi);

// Testing on N random calls on ellipses
for (auto i = 0u; i < N; ++i) {
Expand All @@ -133,16 +109,14 @@ void test_propagate_lagrangian(
double omega = omega_d(rng_engine);
double f = f_d(rng_engine);

std::array<double, 6> par_before = {sma, ecc, incl, Omega, omega, f};
auto pos_vel = kep3::par2ic(par_before, 1.);

propagate(pos_vel, time_d(rng_engine), 1.);
auto par_after = kep3::ic2par(pos_vel, 1.0);
REQUIRE_THAT(par_before[0], WithinAbs(par_after[0], 1e-8));
REQUIRE_THAT(par_before[1], WithinAbs(par_after[1], 1e-8));
REQUIRE_THAT(par_before[2], WithinAbs(par_after[2], 1e-8));
REQUIRE_THAT(par_before[3], WithinAbs(par_after[3], 1e-8));
REQUIRE_THAT(par_before[4], WithinAbs(par_after[4], 1e-8));
std::array<double, 6> par = {sma, ecc, incl, Omega, omega, f};
auto pos_vel = kep3::par2ic(par, 1.);
double tof = time_d(rng_engine);
auto pos_vel_after = pos_vel;
propagate(pos_vel_after, tof, 1.);
propagate(pos_vel_after, -tof, 1.);
REQUIRE(kep3_tests::floating_point_error_vector(
pos_vel[0], pos_vel_after[0]) < 1e-13);
}
}

Expand All @@ -163,16 +137,14 @@ void test_propagate_lagrangian(
double omega = omega_d(rng_engine);
double f = f_d(rng_engine);
if (std::cos(f) > -1 / ecc) {
std::array<double, 6> par_before = {-sma, ecc, incl, Omega, omega, f};
auto pos_vel = kep3::par2ic(par_before, 1.);

propagate(pos_vel, time_d(rng_engine), 1.);
auto par_after = kep3::ic2par(pos_vel, 1.0);
REQUIRE_THAT(par_before[0], WithinAbs(par_after[0], 1e-8));
REQUIRE_THAT(par_before[1], WithinAbs(par_after[1], 1e-8));
REQUIRE_THAT(par_before[2], WithinAbs(par_after[2], 1e-8));
REQUIRE_THAT(par_before[3], WithinAbs(par_after[3], 1e-8));
REQUIRE_THAT(par_before[4], WithinAbs(par_after[4], 1e-8));
std::array<double, 6> par = {-sma, ecc, incl, Omega, omega, f};
auto pos_vel = kep3::par2ic(par, 1.);
double tof = time_d(rng_engine);
auto pos_vel_after = pos_vel;
propagate(pos_vel_after, tof, 1.);
propagate(pos_vel_after, -tof, 1.);
REQUIRE(kep3_tests::floating_point_error_vector(
pos_vel[0], pos_vel_after[0]) < 1e-13);
}
}
}
Expand All @@ -197,5 +169,12 @@ TEST_CASE("extreme_orbit") {
{{0.8086322075411211, -1.3297145067523164, -2.4969299661382585},
{-0.02869546877795607, 0.05765808202641542, -0.03999826575867087}}};
double tof = 4.454030166101634;
kep3::propagate_lagrangian_u(pos_vel, tof, 1.);
propagate_lagrangian_u(pos_vel, tof, 1.);
}

TEST_CASE("propagate_lagrangian_high_tofs") {
// This is not a test just a placeholder to reproduce the problem with long propagation times
std::array<std::array<double, 3>, 2> pos_vel = {{{1., 0, 0.}, {0., 1., 0.}}};
propagate_lagrangian(pos_vel, 1e8 * 2 * kep3::pi, 1.);
fmt::print("\n In propagate_lagrangian_high_tofs\nAfter One Revolution: {}\n", pos_vel);
}
11 changes: 11 additions & 0 deletions test/test_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
#define kep3_TEST_HELPERS_H

#include <algorithm>
#include <array>
#include <cmath>

namespace kep3_tests {
// This is a float test which, while controversial, will test for abs differences in small numbers,
// relative otherwise.
inline double floating_point_error(double a, double b) {
return std::abs(a - b) / std::max(1., std::max(a, b));
}

// This tests how close two vectors are in the euclidean metric. err = r2-r1
inline double floating_point_error_vector(const std::array<double, 3> &r1, const std::array<double, 3> &r2) {
double R1 = std::sqrt(r1[0] * r1[0] + r1[1] * r1[1] * r1[2] * r1[2]);
std::array<double, 3> r12 = {{r2[0]-r1[0], r2[1]-r1[1], r2[2]-r1[2]}};
double R12 = std::sqrt(r12[0] * r12[0] + r12[1] * r12[1] * r12[2] * r12[2]);
return R12 / std::max(1., R1);
}
} // namespace kep3_tests

#endif // kep3_TEST_HELPERS_H

0 comments on commit f185603

Please sign in to comment.