Skip to content

Commit

Permalink
Pos vel tests passing and added magic enum
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasadesus committed Dec 30, 2024
1 parent ff4dae5 commit 6b6347d
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 144 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ function(target_link_libraries_system target)
endfunction(target_link_libraries_system)

# Link dependencies
target_link_libraries_system(${PROJECT_NAME} Eigen3::Eigen matplot autodiff::autodiff cspice yaml-cpp::yaml-cpp)
target_link_libraries_system(
${PROJECT_NAME}
Eigen3::Eigen
matplot
autodiff::autodiff
cspice
yaml-cpp::yaml-cpp
magic_enum::magic_enum
)
target_link_libraries(${PROJECT_NAME} PUBLIC HighFive)

# Link Torch
Expand Down
26 changes: 0 additions & 26 deletions include/lupnt/core/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,32 +191,6 @@ namespace lupnt {
NEPTUNE = 899,
};

std::map<NaifId, std::string> naifId2string
= {{NaifId::SSB, "SSB"},
{NaifId::SOLAR_SYSTEM_BARYCENTER, "SOLAR_SYSTEM_BARYCENTER"},
{NaifId::MERCURY_BARYCENTER, "MERCURY_BARYCENTER"},
{NaifId::VENUS_BARYCENTER, "VENUS_BARYCENTER"},
{NaifId::EMB, "EMB"},
{NaifId::EARTH_MOON_BARYCENTER, "EARTH_MOON_BARYCENTER"},
{NaifId::MARS_BARYCENTER, "MARS_BARYCENTER"},
{NaifId::JUPITER_BARYCENTER, "JUPITER_BARYCENTER"},
{NaifId::SATURN_BARYCENTER, "SATURN_BARYCENTER"},
{NaifId::URANUS_BARYCENTER, "URANUS_BARYCENTER"},
{NaifId::NEPTUNE_BARYCENTER, "NEPTUNE_BARYCENTER"},
{NaifId::PLUTO_BARYCENTER, "PLUTO_BARYCENTER"},
{NaifId::SUN, "SUN"},
{NaifId::MERCURY, "MERCURY"},
{NaifId::VENUS, "VENUS"},
{NaifId::EARTH, "EARTH"},
{NaifId::MOON, "MOON"},
{NaifId::MARS, "MARS"},
{NaifId::PHOBOS, "PHOBOS"},
{NaifId::DEIMOS, "DEIMOS"},
{NaifId::JUPITER, "JUPITER"},
{NaifId::SATURN, "SATURN"},
{NaifId::URANUS, "URANUS"},
{NaifId::NEPTUNE, "NEPTUNE"}};

double GetBodyRadius(NaifId body);
const std::ostream& operator<<(std::ostream& os, NaifId id);

Expand Down
9 changes: 9 additions & 0 deletions include/lupnt/core/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
*
*/
#pragma once
#define MAGIC_ENUM_RANGE_MAX 1024

#include <Eigen/Core>
#include <Eigen/Dense>
#include <autodiff/forward/real.hpp>
#include <autodiff/forward/real/eigen.hpp>
#include <filesystem>
#include <iostream>
#include <magic_enum/magic_enum.hpp>
#include <memory>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -102,4 +104,11 @@ namespace lupnt {
static Eigen::IOFormat FMT_COMPACT(Eigen::StreamPrecision, Eigen::DontAlignCols, ", ", ";\n ", "",
"", "[", "]");

using magic_enum::enum_cast;
using magic_enum::enum_integer;
using magic_enum::enum_name;
using magic_enum::enum_names;
using magic_enum::enum_value;
using magic_enum::enum_values;

} // namespace lupnt
2 changes: 0 additions & 2 deletions include/lupnt/physics/frame_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ namespace lupnt {
std::ostream &operator<<(std::ostream &os, Frame frame);

extern const std::map<Frame, NaifId> frame_centers;
extern const std::map<std::string, Frame> string2frame;
extern const std::map<Frame, std::string> frame2string;

extern std::map<std::pair<Frame, Frame>, std::function<Vec6(Real, const Vec6 &rv)>>
frame_conversions;
Expand Down
2 changes: 1 addition & 1 deletion source/cpp/data/kernels.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ namespace lupnt {
rv_center = rv_target / (1. + emr);
} else if (center == NaifId::MOON && target == NaifId::EMB) {
rv_center = GetBodyPosVelKernel(t_tdb, EphemID::MOON);
rv_target = rv_center * (1. + emr);
rv_target = rv_center / (1. + emr);

} else if (center == NaifId::EMB && target == NaifId::EARTH) {
rv_center = GetBodyPosVelKernel(t_tdb, EphemID::MOON) / (1. + emr);
Expand Down
2 changes: 1 addition & 1 deletion source/cpp/numerics/integrator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace lupnt {
(order_ + 1) / order_); // J.C. Butcher, Numerical Methods for
// Ordinary Differential Equations, p291

for (size_t i = 0; i < x_new_low.size(); ++i) {
for (int i = 0; i < x_new_low.size(); ++i) {
error = std::abs(x_new_high(i).val() - x_new_low(i).val());
tol = std::max<double>(params_.reltol * std::abs(x_new_high(i).val()), params_.abstol);
max_error = std::max(max_error, error / tol);
Expand Down
36 changes: 0 additions & 36 deletions source/cpp/physics/frame_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,6 @@ namespace lupnt {
{Frame::VENUS_FIXED, NaifId::VENUS},
};

const std::map<std::string, Frame> string2frame = {
// Earth
{"ITRF", Frame::ITRF},
{"ECEF", Frame::ECEF},
{"GCRF", Frame::GCRF},
{"EME", Frame::EME},
{"ECI", Frame::ECI},
{"ICRF", Frame::ICRF},
// Moon
{"MOON_ME", Frame::MOON_ME},
{"MOON_CI", Frame::MOON_CI},
{"MOON_PA", Frame::MOON_PA},
{"MOON_OP", Frame::MOON_OP},
// Solar System
{"MARS_FIXED", Frame::MARS_FIXED},
{"VENUS_FIXED", Frame::VENUS_FIXED},
};

const std::map<Frame, std::string> frame2string = {
// Earth
{Frame::ITRF, "ITRF"},
{Frame::ECEF, "ECEF"},
{Frame::GCRF, "GCRF"},
{Frame::EME, "EME"},
{Frame::ECI, "ECI"},
{Frame::ICRF, "ICRF"},
// Moon
{Frame::MOON_ME, "MOON_ME"},
{Frame::MOON_CI, "MOON_CI"},
{Frame::MOON_PA, "MOON_PA"},
{Frame::MOON_OP, "MOON_OP"},
// Solar System
{Frame::MARS_FIXED, "MARS_FIXED"},
{Frame::VENUS_FIXED, "VENUS_FIXED"},
};

std::ostream& operator<<(std::ostream& os, Frame frame) {
switch (frame) {
case Frame::ITRF: os << "ITRF"; break;
Expand Down
6 changes: 4 additions & 2 deletions source/cpp/simulation/sim_config_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "lupnt/simulation/sim_config_reader.h"

#include <magic_enum/magic_enum.hpp>

#include "lupnt/agents/agent.h"
#include "lupnt/core/constants.h"
#include "lupnt/core/definitions.h"
Expand Down Expand Up @@ -359,12 +361,12 @@ namespace lupnt {
// - .frame and .init.frame are required
std::string final_frame_key
= LoadRequiredField<std::string>(state_node["frame"], prior_key + "frame");
Frame frame_final = FindStrFrameMap(string2frame, final_frame_key);
Frame frame_final = magic_enum::enum_cast<Frame>(final_frame_key).value();

// .init.frame
std::string init_frame_key
= LoadRequiredField<std::string>(state_node["init"]["frame"], prior_key + "init.frame");
Frame frame_init = FindStrFrameMap(string2frame, init_frame_key);
Frame frame_init = magic_enum::enum_cast<Frame>(init_frame_key).value();

// center body from frame_init
NaifId center_init = FindFrameCenterMap(frame_centers, frame_init);
Expand Down
23 changes: 23 additions & 0 deletions test/cpp/core/test_enums.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <lupnt/core/constants.h>

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <iostream>
#include <vector>

#include "../utils.cc"

using namespace lupnt;
using namespace Catch::Matchers;

const double epsilon = 1e-6;

TEST_CASE("core.enums") {
auto vals = enum_values<NaifId>();
REQUIRE(vals.size() > 20);
for (auto naif_id : vals) {
auto naif_id_str = enum_name(naif_id);
auto naif_id_val = enum_cast<NaifId>(naif_id_str).value();
REQUIRE(naif_id == naif_id_val);
}
}
38 changes: 28 additions & 10 deletions test/cpp/data/test_kernels.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <lupnt/core/constants.h>
#include <lupnt/data/kernels.h>

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <iostream>
#include <magic_enum/magic_enum.hpp>
#include <vector>

#include "../utils.cc"
Expand All @@ -15,16 +17,32 @@ const double epsilon = 1e-6;
// Test cases for time conversion
TEST_CASE("data.Kernels") {
// Spice
file = OpenTestDataFile("body_pos_vel_cspice.txt");
auto file = OpenTestDataFile("body_pos_vel_cspice.txt");
std::string line;

// Time
std::getline(file, line);
std::istringstream iss(line);
std::string tmp;
double t_tai;
iss >> tmp >> t_tai;

// Header
std::getline(file, line);

// Values
while (std::getline(file, line)) {
std::string time, date_in;
double t_diff_in;
std::istringstream iss(line);
iss >> time >> date_in >> t_diff_in;
if (time == "UTC") t_utc = Gregorian2Time(date_in);
Real t = ConvertTime(t_utc, Time::UTC, string2time.at(time));
Real t_j2000 = ConvertTime(0, Time::TT, string2time.at(time));
Real t_diff = t - t_j2000;
RequireNear(t_diff, t_diff_in, epsilon);
std::string center_str, target_str;
double lt, x, y, z, vx, vy, vz;
iss.clear();
iss.str(line);
iss >> center_str >> target_str >> lt >> x >> y >> z >> vx >> vy >> vz;
Vec6 rv_in{x, y, z, vx, vy, vz};
auto center_tmp = enum_cast<NaifId>(center_str);
auto target_tmp = enum_cast<NaifId>(target_str);
auto center = enum_cast<NaifId>(center_str).value();
auto target = enum_cast<NaifId>(target_str).value();
Vec6 rv = GetBodyPosVel(t_tai, center, target, Frame::GCRF);
RequireNear(rv, rv_in, epsilon);
}
}
62 changes: 32 additions & 30 deletions test/data/body_pos_vel_cspice.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
SSB SUN 4.306222834e+00 -7.419872695e+05 +9.659292737e+05 +4.278400954e+05 -1.408381994e-02 -5.627820863e-03 -2.001044848e-03
SSB EMB 5.032556135e+02 -7.604591656e+07 -1.195584954e+08 -5.181907715e+07 +2.534386321e+01 -1.370093397e+01 -5.938948538e+00
SSB EARTH 5.032710330e+02 -7.604958762e+07 -1.195615714e+08 -5.182005163e+07 +2.535189040e+01 -1.370869149e+01 -5.943122540e+00
SSB MOON 5.020021891e+02 -7.574745701e+07 -1.193084156e+08 -5.173985179e+07 +2.469124806e+01 -1.307024395e+01 -5.599599808e+00
SSB MARS_BARYCENTER 7.038906758e+02 +7.098457857e+07 -1.798830097e+08 -8.445829605e+07 +2.370522429e+01 +9.564791120e+00 +3.747645442e+00
SUN SSB 4.306222834e+00 +7.419872695e+05 -9.659292737e+05 -4.278400954e+05 +1.408381994e-02 +5.627820863e-03 +2.001044848e-03
SUN EMB 5.050667010e+02 -7.530392929e+07 -1.205244247e+08 -5.224691725e+07 +2.535794703e+01 -1.369530615e+01 -5.936947493e+00
SUN EARTH 5.050820798e+02 -7.530760035e+07 -1.205275007e+08 -5.224789172e+07 +2.536597422e+01 -1.370306366e+01 -5.941121495e+00
SUN MOON 5.038165897e+02 -7.500546974e+07 -1.202743449e+08 -5.216769189e+07 +2.470533188e+01 -1.306461613e+01 -5.597598763e+00
SUN MARS_BARYCENTER 7.080419077e+02 +7.172656584e+07 -1.808489389e+08 -8.488613615e+07 +2.371930811e+01 +9.570418941e+00 +3.749646487e+00
EMB SSB 5.032556135e+02 +7.604591656e+07 +1.195584954e+08 +5.181907715e+07 -2.534386321e+01 +1.370093397e+01 +5.938948538e+00
EMB SUN 5.050667010e+02 +7.530392929e+07 +1.205244247e+08 +5.224691725e+07 -2.535794703e+01 +1.369530615e+01 +5.936947493e+00
EMB EARTH 1.630307316e-02 -3.671063554e+03 -3.075991310e+03 -9.744748361e+02 +8.027190535e-03 -7.757510644e-03 -4.174001945e-03
EMB MOON 1.325449111e+00 +2.984595529e+05 +2.500798414e+05 +7.922535789e+04 -6.526151518e-01 +6.306900233e-01 +3.393487299e-01
EMB MARS_BARYCENTER 5.411796773e+02 +1.470304951e+08 -6.032451425e+07 -3.263921890e+07 -1.638638918e+00 +2.326572509e+01 +9.686593980e+00
EARTH SSB 5.032710330e+02 +7.604958762e+07 +1.195615714e+08 +5.182005163e+07 -2.535189040e+01 +1.370869149e+01 +5.943122540e+00
EARTH SUN 5.050820798e+02 +7.530760035e+07 +1.205275007e+08 +5.224789172e+07 -2.536597422e+01 +1.370306366e+01 +5.941121495e+00
EARTH EMB 1.630307316e-02 +3.671063554e+03 +3.075991310e+03 +9.744748361e+02 -8.027190535e-03 +7.757510644e-03 +4.174001945e-03
EARTH MOON 1.341752184e+00 +3.021306164e+05 +2.531558327e+05 +8.019983273e+04 -6.606423423e-01 +6.384475340e-01 +3.435227319e-01
EARTH MARS_BARYCENTER 5.411863059e+02 +1.470341662e+08 -6.032143826e+07 -3.263824442e+07 -1.646666108e+00 +2.327348260e+01 +9.690767982e+00
MOON SSB 5.020021891e+02 +7.574745701e+07 +1.193084156e+08 +5.173985179e+07 -2.469124806e+01 +1.307024395e+01 +5.599599808e+00
MOON SUN 5.038165897e+02 +7.500546974e+07 +1.202743449e+08 +5.216769189e+07 -2.470533188e+01 +1.306461613e+01 +5.597598763e+00
MOON EMB 1.325449111e+00 -2.984595529e+05 -2.500798414e+05 -7.922535789e+04 +6.526151518e-01 -6.306900233e-01 -3.393487299e-01
MOON EARTH 1.341752184e+00 -3.021306164e+05 -2.531558327e+05 -8.019983273e+04 +6.606423423e-01 -6.384475340e-01 -3.435227319e-01
MOON MARS_BARYCENTER 5.406421457e+02 +1.467320356e+08 -6.057459409e+07 -3.271844426e+07 -9.860237662e-01 +2.263503507e+01 +9.347245250e+00
MARS_BARYCENTER SSB 7.038906758e+02 -7.098457857e+07 +1.798830097e+08 +8.445829605e+07 -2.370522429e+01 -9.564791120e+00 -3.747645442e+00
MARS_BARYCENTER SUN 7.080419077e+02 -7.172656584e+07 +1.808489389e+08 +8.488613615e+07 -2.371930811e+01 -9.570418941e+00 -3.749646487e+00
MARS_BARYCENTER EMB 5.411796773e+02 -1.470304951e+08 +6.032451425e+07 +3.263921890e+07 +1.638638918e+00 -2.326572509e+01 -9.686593980e+00
MARS_BARYCENTER EARTH 5.411863059e+02 -1.470341662e+08 +6.032143826e+07 +3.263824442e+07 +1.646666108e+00 -2.327348260e+01 -9.690767982e+00
MARS_BARYCENTER MOON 5.406421457e+02 -1.467320356e+08 +6.057459409e+07 +3.271844426e+07 +9.860237662e-01 -2.263503507e+01 -9.347245250e+00
t_tai 6.4329496000000024e+08
center target lt [s] x [km] y [km] z [km] vx [km/s] vy [km/s] vz [km/s]
SSB SUN 4.306222834e+00 -741987.269464739 +965929.273698082 +427840.095432443 -0.014083820 -0.005627821 -0.002001045
SSB EMB 5.032556135e+02 -76045916.558535278 -119558495.426486567 -51819077.151945584 +25.343863212 -13.700933974 -5.938948538
SSB EARTH 5.032710330e+02 -76049587.622088879 -119561571.417796656 -51820051.626781650 +25.351890403 -13.708691485 -5.943122540
SSB MOON 5.020021891e+02 -75747457.005650803 -119308415.585131600 -51739851.794055827 +24.691248060 -13.070243951 -5.599599808
SSB MARS_BARYCENTER 7.038906758e+02 +70984578.570678294 -179883009.673950434 -84458296.051201954 +23.705224294 +9.564791120 +3.747645442
SUN SSB 4.306222834e+00 +741987.269464739 -965929.273698082 -427840.095432443 +0.014083820 +0.005627821 +0.002001045
SUN EMB 5.050667010e+02 -75303929.289070532 -120524424.700184643 -52246917.247378029 +25.357947032 -13.695306154 -5.936947493
SUN EARTH 5.050820798e+02 -75307600.352624133 -120527500.691494733 -52247891.722214095 +25.365974223 -13.703063664 -5.941121495
SUN MOON 5.038165897e+02 -75005469.736186057 -120274344.858829677 -52167691.889488272 +24.705331880 -13.064616130 -5.597598763
SUN MARS_BARYCENTER 7.080419077e+02 +71726565.840143040 -180848938.947648525 -84886136.146634400 +23.719308114 +9.570418941 +3.749646487
EMB SSB 5.032556135e+02 +76045916.558535278 +119558495.426486567 +51819077.151945584 -25.343863212 +13.700933974 +5.938948538
EMB SUN 5.050667010e+02 +75303929.289070532 +120524424.700184643 +52246917.247378029 -25.357947032 +13.695306154 +5.936947493
EMB EARTH 1.630307316e-02 -3671.063553595 -3075.991310093 -974.474836066 +0.008027191 -0.007757511 -0.004174002
EMB MOON 1.325449111e+00 +298459.552884478 +250079.841354972 +79225.357889756 -0.652615152 +0.630690023 +0.339348730
EMB MARS_BARYCENTER 5.411796773e+02 +147030495.129213572 -60324514.247463867 -32639218.899256371 -1.638638918 +23.265725094 +9.686593980
EARTH SSB 5.032710330e+02 +76049587.622088879 +119561571.417796656 +51820051.626781650 -25.351890403 +13.708691485 +5.943122540
EARTH SUN 5.050820798e+02 +75307600.352624133 +120527500.691494733 +52247891.722214095 -25.365974223 +13.703063664 +5.941121495
EARTH EMB 1.630307316e-02 +3671.063553595 +3075.991310093 +974.474836066 -0.008027191 +0.007757511 +0.004174002
EARTH MOON 1.341752184e+00 +302130.616438072 +253155.832665065 +80199.832725823 -0.660642342 +0.638447534 +0.343522732
EARTH MARS_BARYCENTER 5.411863059e+02 +147034166.192767173 -60321438.256153777 -32638244.424420305 -1.646666108 +23.273482605 +9.690767982
MOON SSB 5.020021891e+02 +75747457.005650803 +119308415.585131600 +51739851.794055827 -24.691248060 +13.070243951 +5.599599808
MOON SUN 5.038165897e+02 +75005469.736186057 +120274344.858829677 +52167691.889488272 -24.705331880 +13.064616130 +5.597598763
MOON EMB 1.325449111e+00 -298459.552884478 -250079.841354972 -79225.357889756 +0.652615152 -0.630690023 -0.339348730
MOON EARTH 1.341752184e+00 -302130.616438072 -253155.832665065 -80199.832725823 +0.660642342 -0.638447534 -0.343522732
MOON MARS_BARYCENTER 5.406421457e+02 +146732035.576329112 -60574594.088818833 -32718444.257146128 -0.986023766 +22.635035071 +9.347245250
MARS_BARYCENTER SSB 7.038906758e+02 -70984578.570678294 +179883009.673950434 +84458296.051201954 -23.705224294 -9.564791120 -3.747645442
MARS_BARYCENTER SUN 7.080419077e+02 -71726565.840143040 +180848938.947648525 +84886136.146634400 -23.719308114 -9.570418941 -3.749646487
MARS_BARYCENTER EMB 5.411796773e+02 -147030495.129213572 +60324514.247463867 +32639218.899256371 +1.638638918 -23.265725094 -9.686593980
MARS_BARYCENTER EARTH 5.411863059e+02 -147034166.192767173 +60321438.256153777 +32638244.424420305 +1.646666108 -23.273482605 -9.690767982
MARS_BARYCENTER MOON 5.406421457e+02 -146732035.576329112 +60574594.088818833 +32718444.257146128 +0.986023766 -22.635035071 -9.347245250
Loading

0 comments on commit 6b6347d

Please sign in to comment.