Skip to content

Commit

Permalink
planet tests added for period
Browse files Browse the repository at this point in the history
  • Loading branch information
darioizzo committed Sep 20, 2023
1 parent fd1d4ef commit 7ce3441
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/kep3/planet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class kep3_DLL_PUBLIC planet {
[[nodiscard]] double get_mu_self() const;
[[nodiscard]] double get_radius() const;
[[nodiscard]] double get_safe_radius() const;
[[nodiscard]] double period(const kep3::epoch &) const;
[[nodiscard]] double period(const kep3::epoch & = kep3::epoch()) const;
};

// Streaming operator for algorithm.
Expand Down
4 changes: 4 additions & 0 deletions src/planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ std::array<std::array<double, 3>, 2> planet::eph(const epoch &ep) {
return ptr()->eph(ep);
}

double planet::period(const epoch &ep) const {
return ptr()->period(ep);
}

std::string planet::get_name() const { return ptr()->get_name(); }

std::string planet::get_extra_info() const { return ptr()->get_extra_info(); }
Expand Down
86 changes: 83 additions & 3 deletions test/planet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <kep3/planet.hpp>

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

using kep3::epoch;
using kep3::planet;
Expand All @@ -29,6 +30,7 @@ using kep3::detail::udpla_has_get_mu_self_v;
using kep3::detail::udpla_has_get_name_v;
using kep3::detail::udpla_has_get_radius_v;
using kep3::detail::udpla_has_get_safe_radius_v;
using kep3::detail::udpla_has_period_v;

struct simple_udpla {
simple_udpla() = default;
Expand All @@ -46,6 +48,44 @@ struct simple_udpla {
};
kep3_S11N_PLANET_EXPORT(simple_udpla);

struct simple_udpla_mu {
simple_udpla_mu() = default;
static std::array<std::array<double, 3>, 2> eph(const epoch &) {
std::array<double, 3> pos = {1., 0., 0.};
std::array<double, 3> vel = {0., 1., 0.};
return {pos, vel};
};
static std::string get_name() { return "A simple planet with mu"; }
static std::string get_extra_info() {
return "The simplest planet ever but with mu!";
}
static double get_mu_central_body() { return 1.; }

private:
friend class boost::serialization::access;
template <typename Archive> void serialize(Archive &, unsigned) {}
};
kep3_S11N_PLANET_EXPORT(simple_udpla_mu);

struct simple_udpla_mu_h {
simple_udpla_mu_h() = default;
static std::array<std::array<double, 3>, 2> eph(const epoch &) {
std::array<double, 3> pos = {1., 0., 0.};
std::array<double, 3> vel = {0., 10., 0.};
return {pos, vel};
};
static std::string get_name() { return "A simple planet with mu"; }
static std::string get_extra_info() {
return "The simplest planet ever but with mu!";
}
static double get_mu_central_body() { return 1.; }

private:
friend class boost::serialization::access;
template <typename Archive> void serialize(Archive &, unsigned) {}
};
kep3_S11N_PLANET_EXPORT(simple_udpla_mu_h);

struct complete_udpla {
explicit complete_udpla(std::array<double, 4> physical_properties = {-1., -1.,
-1.,
Expand All @@ -65,6 +105,9 @@ struct complete_udpla {
[[nodiscard]] double get_mu_self() const { return m_mu_self; }
[[nodiscard]] double get_radius() const { return m_radius; }
[[nodiscard]] double get_safe_radius() const { return m_safe_radius; }
[[nodiscard]] double period(const epoch &) const {
return m_radius - m_radius;
}

[[nodiscard]] std::string get_extra_info() const {
return fmt::format(
Expand Down Expand Up @@ -108,6 +151,7 @@ TEST_CASE("construction") {
REQUIRE_THROWS_AS((pla.get_mu_self()), kep3::not_implemented_error);
REQUIRE_THROWS_AS((pla.get_radius()), kep3::not_implemented_error);
REQUIRE_THROWS_AS((pla.get_safe_radius()), kep3::not_implemented_error);
REQUIRE_THROWS_AS((pla.period()), kep3::not_implemented_error);
REQUIRE(pla.extract<null_udpla>() != nullptr);
}
{
Expand All @@ -124,6 +168,36 @@ TEST_CASE("construction") {
REQUIRE_THROWS_AS((pla.get_mu_self()), kep3::not_implemented_error);
REQUIRE_THROWS_AS((pla.get_radius()), kep3::not_implemented_error);
REQUIRE_THROWS_AS((pla.get_safe_radius()), kep3::not_implemented_error);
REQUIRE_THROWS_AS((pla.period()), kep3::not_implemented_error);
}
{
// Constructor from a more complete udpla
complete_udpla udpla({1., 2., -1., 4.});
REQUIRE_NOTHROW(planet(udpla));
planet pla(udpla);
auto pos_vel = pla.eph(epoch(0.));
REQUIRE(pos_vel[0] == std::array<double, 3>{1., 0., 0.});
REQUIRE(pos_vel[1] == std::array<double, 3>{0., 1., 0.});
REQUIRE(pla.get_name() == "A complete, albeit simple Planet");
REQUIRE(pla.get_mu_central_body() == 1.);
REQUIRE(pla.get_mu_self() == 2.);
REQUIRE_THROWS_AS((pla.get_radius()), kep3::not_implemented_error);
REQUIRE(pla.get_safe_radius() == 4.);
}
{
// Constructor from a simple udpla with mu and hyperbolic orbit
simple_udpla_mu_h udpla{};
REQUIRE_NOTHROW(planet(udpla));
planet pla(udpla);
REQUIRE_THROWS_AS((pla.period()), std::logic_error);
}
{
// Constructor from a simple udpla with mu and elliptical orbit
simple_udpla_mu udpla{};
REQUIRE_NOTHROW(planet(udpla));
planet pla(udpla);
REQUIRE(kep3_tests::floating_point_error(pla.period(), kep3::pi * 2.) <
1e-14);
}
{
// Constructor from a more complete udpla
Expand Down Expand Up @@ -208,6 +282,11 @@ TEST_CASE("type_traits") {
REQUIRE(!udpla_has_get_safe_radius_v<simple_udpla>);
REQUIRE(!udpla_has_get_safe_radius_v<double>);
REQUIRE(udpla_has_get_safe_radius_v<complete_udpla>);
// check the udpla_has_period_v type trait.
REQUIRE(!udpla_has_period_v<null_udpla>);
REQUIRE(!udpla_has_period_v<simple_udpla>);
REQUIRE(!udpla_has_period_v<double>);
REQUIRE(udpla_has_period_v<complete_udpla>);
}

TEST_CASE("copy_constructor_test") {
Expand Down Expand Up @@ -345,9 +424,9 @@ TEST_CASE("generic_assignment") {

TEST_CASE("type_index") {
planet p0 = planet{null_udpla{}};
//REQUIRE(p0.get_type_index() == std::type_index(typeid(null_udpla)));
//p0 = planet{simple_udpla{}};
//REQUIRE(p0.get_type_index() == std::type_index(typeid(simple_udpla)));
// REQUIRE(p0.get_type_index() == std::type_index(typeid(null_udpla)));
// p0 = planet{simple_udpla{}};
// REQUIRE(p0.get_type_index() == std::type_index(typeid(simple_udpla)));
}

TEST_CASE("get_ptr") {
Expand Down Expand Up @@ -377,6 +456,7 @@ TEST_CASE("planet_astro_methods_test") {
REQUIRE(pla.get_mu_self() == 2.3);
REQUIRE(pla.get_radius() == 4.02);
REQUIRE(pla.get_safe_radius() == 4.5);
REQUIRE(pla.period(kep3::epoch(0.)) == 0.);
}

TEST_CASE("serialization_test") {
Expand Down

0 comments on commit 7ce3441

Please sign in to comment.