Skip to content

Commit

Permalink
- Refactored conversion functions to return double
Browse files Browse the repository at this point in the history
NOTE
====
epoch::julian_type is an enum *class* (not a plain enum). This is necessary to avoid implicit conversion from julian_type to an integral type.
  • Loading branch information
cantordust committed Sep 30, 2023
1 parent f4621e0 commit d35973c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 51 deletions.
2 changes: 0 additions & 2 deletions include/kep3/core_astro/convert_julian_dates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <chrono>
namespace kep3
{

// inline double jd2mjd(double in) { return (in - 2400000.5); }
// inline double jd2mjd2000(double in) { return (in - 2451544.5); }
// inline double mjd2jd(double in) { return (in + 2400000.5); }
Expand Down Expand Up @@ -49,7 +48,6 @@ namespace kep3
{
return in + 4453401600s;
}

} // namespace kep3

#endif // CONVERT_JULIAN_DATES_HPP
45 changes: 34 additions & 11 deletions include/kep3/epoch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,44 @@ class kep3_DLL_PUBLIC epoch {
epoch(const kep_clock::time_point &time_point);
epoch(kep_clock::time_point &&time_point);

// // Constructor for microseconds
// template <class Int,
// typename std::enable_if<std::is_integral<Int>::value, Int>::type * = nullptr>
// epoch(const Int us)
// : tp{ kep_clock::time_point{chr::microseconds(us)} }
// {
// }

// Constructor for microseconds
epoch(const int y,const int d, const int h = 0, const int min = 0, const int s = 0, const int ms = 0, const int us = 0);

/** Computing non-gregorian dates */
[[nodiscard]] kep_clock::time_point mjd2000() const;
[[nodiscard]] kep_clock::time_point jd() const;
[[nodiscard]] kep_clock::time_point mjd() const;
/// jd getter.
/**
* Returns the julian date
*
* @return double containing the julian date
*
*/
constexpr double jd() const
{
return chr::duration<double, std::ratio<86400>>(tp.time_since_epoch() + 211813444800s).count();
}

/// mjd getter.
/**
* Returns the modified julian date
*
* @return double containing the modified julian date
*
*/
constexpr double mjd() const
{
return chr::duration<double, std::ratio<86400>>(tp.time_since_epoch() + 4453401600s).count();
}

/// mjd2000 getter.
/**
* Gets the modified julian date 2000
* @return const reference to mjd2000
*/
constexpr double mjd2000() const
{
return chr::duration<double, std::ratio<86400>>(tp.time_since_epoch()).count();
}


/** Interface to boost::posix_time::ptime */
// [[nodiscard]] boost::posix_time::ptime get_posix_time() const;
Expand Down
62 changes: 31 additions & 31 deletions src/epoch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,39 @@ namespace kep3
throw;
}
}
/// jd getter.
/**
* Returns the julian date
*
* @return double containing the julian date
*
*/
kep_clock::time_point epoch::jd() const
{
return mjd20002jd( tp );
}
// /// jd getter.
// /**
// * Returns the julian date
// *
// * @return double containing the julian date
// *
// */
// constexpr double epoch::jd() const
// {
// return chr::duration<double, std::ratio<86400>>(tp.time_since_epoch() + 211813444800s).count();
// }

/// mjd getter.
/**
* Returns the modified julian date
*
* @return double containing the modified julian date
*
*/
kep_clock::time_point epoch::mjd() const
{
return mjd20002mjd( tp );
}
// /// mjd getter.
// /**
// * Returns the modified julian date
// *
// * @return double containing the modified julian date
// *
// */
// constexpr double epoch::mjd() const
// {
// return chr::duration<double, std::ratio<86400>>(tp.time_since_epoch() + 4453401600s).count();
// }

/// mjd2000 getter.
/**
* Gets the modified julian date 2000
* @return const reference to mjd2000
*/
kep_clock::time_point epoch::mjd2000() const
{
return tp;
}
// /// mjd2000 getter.
// /**
// * Gets the modified julian date 2000
// * @return const reference to mjd2000
// */
// constexpr double epoch::mjd2000() const
// {
// return chr::duration<double, std::ratio<86400>>(tp.time_since_epoch()).count();
// }

/// Extracts the posix time
/**
Expand Down
2 changes: 1 addition & 1 deletion src/planets/jpl_lp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ double jpl_lp::get_radius() const { return m_radius; }
double jpl_lp::get_safe_radius() const { return m_safe_radius; }

std::string jpl_lp::get_extra_info() const {
kep3::epoch ep{0., kep3::epoch::MJD2000};
kep3::epoch ep{0., kep3::epoch::julian_type::MJD2000};
auto par = elements(ep);
auto pos_vel = eph(ep);

Expand Down
2 changes: 1 addition & 1 deletion src/planets/keplerian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ std::string keplerian::get_extra_info() const {
kep3::f2m(par[5], par[1]) * kep3::RAD2DEG);
}
retval += fmt::format("Elements reference epoch (MJD2000): {}\n",
m_ref_epoch.mjd2000().time_since_epoch().count()) +
m_ref_epoch) +
fmt::format("Elements reference epoch (date): {}\n", m_ref_epoch) +
fmt::format("r at ref. = {}\n", m_pos_vel_0[0]) +
fmt::format("v at ref. = {}\n", m_pos_vel_0[1]);
Expand Down
10 changes: 5 additions & 5 deletions test/planet_jpl_lp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using kep3::udpla::jpl_lp;

TEST_CASE("constructor") {
REQUIRE_NOTHROW(jpl_lp{});
kep3::epoch ref_epoch{12.22, kep3::epoch::MJD2000};
kep3::epoch ref_epoch{12.22, kep3::epoch::julian_type::MJD2000};
// From name
REQUIRE_NOTHROW(jpl_lp{"Mars"});
REQUIRE_NOTHROW(jpl_lp{"mars"});
Expand All @@ -39,7 +39,7 @@ TEST_CASE("constructor") {

TEST_CASE("eph") {
// We use 2030-01-01 as a reference epoch for all these tests
kep3::epoch ref_epoch{2458849.5, kep3::epoch::JD};
kep3::epoch ref_epoch{2458849.5, kep3::epoch::julian_type::JD};
{
// This is Mercury w.r.t. the Sun queried from JPL Horizon at
// 2020-01-01
Expand Down Expand Up @@ -136,12 +136,12 @@ TEST_CASE("eph") {
REQUIRE(kep3_tests::floating_point_error_vector(v, pos_vel_0[1]) < 0.01);
}
jpl_lp udpla{"uranus"};
REQUIRE_THROWS_AS(udpla.eph(kep3::epoch(5347534, kep3::epoch::MJD2000)),
REQUIRE_THROWS_AS(udpla.eph(kep3::epoch(5347534, kep3::epoch::julian_type::MJD2000)),
std::domain_error);
}

TEST_CASE("elements") {
kep3::epoch ref_epoch{12.22, kep3::epoch::MJD2000};
kep3::epoch ref_epoch{12.22, kep3::epoch::julian_type::MJD2000};
// We use Neptune
jpl_lp udpla{"nePTUne"}; // casing is not important
auto pos_vel = udpla.eph(ref_epoch);
Expand Down Expand Up @@ -192,7 +192,7 @@ TEST_CASE("stream_operator") {

TEST_CASE("serialization_test") {
// Instantiate a generic jpl_lp
kep3::epoch ref_epoch{2423.4343, kep3::epoch::MJD2000};
kep3::epoch ref_epoch{2423.4343, kep3::epoch::julian_type::MJD2000};
jpl_lp udpla{"neptune"};

// Store the string representation.
Expand Down

0 comments on commit d35973c

Please sign in to comment.