You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.9 KiB
83 lines
2.9 KiB
#include <catch2/catch_test_macros.hpp> |
|
#include "../src/physics.h" |
|
#include "../src/bodies.h" |
|
#include "../src/test_utilities.h" |
|
#include <cmath> |
|
|
|
TEST_CASE("Orbital period - Earth (RK4)", "[period][rk4]") { |
|
const double TIME_STEP = 60.0; |
|
const double EXPECTED_PERIOD_DAYS = 365.0; |
|
const double SECONDS_PER_DAY = 86400.0; |
|
const double MAX_SIMULATION_DAYS = 400.0; |
|
|
|
SimulationState* sim = create_simulation(10, TIME_STEP); |
|
|
|
Vec3 sun_pos = {0, 0, 0}; |
|
Vec3 sun_vel = {0, 0, 0}; |
|
add_body(sim, "Sun", 1.989e30, 6.96e8, sun_pos, sun_vel, -1, 1.0, 1.0, 0.0, 0, 0); |
|
|
|
Vec3 earth_pos = {1.496e11, 0, 0}; |
|
Vec3 earth_vel = {0, 29789, 0}; |
|
add_body(sim, "Earth", 5.972e24, 6.371e6, earth_pos, earth_vel, 0, 0.0, 0.5, 1.0, 0, 1.496e11); |
|
|
|
OrbitTracker* tracker = create_orbit_tracker(1); |
|
|
|
double max_time = MAX_SIMULATION_DAYS * SECONDS_PER_DAY; |
|
while (sim->time < max_time && !tracker->orbit_completed) { |
|
update_simulation(sim); |
|
update_orbit_tracker(tracker, &sim->bodies[1], &sim->bodies[0], sim->time); |
|
} |
|
|
|
REQUIRE(tracker->orbit_completed); |
|
|
|
double measured_period_days = tracker->time_at_completion / SECONDS_PER_DAY; |
|
double period_error_days = fabs(measured_period_days - EXPECTED_PERIOD_DAYS); |
|
|
|
INFO("Expected period: " << EXPECTED_PERIOD_DAYS << " days"); |
|
INFO("Measured period: " << measured_period_days << " days"); |
|
INFO("Error: " << period_error_days << " days"); |
|
|
|
REQUIRE(period_error_days < 5.0); |
|
|
|
destroy_orbit_tracker(tracker); |
|
destroy_simulation(sim); |
|
} |
|
|
|
TEST_CASE("Orbital period - Mars (RK4)", "[period][rk4]") { |
|
const double TIME_STEP = 60.0; |
|
const double EXPECTED_PERIOD_DAYS = 687.0; |
|
const double SECONDS_PER_DAY = 86400.0; |
|
const double MAX_SIMULATION_DAYS = 750.0; |
|
|
|
SimulationState* sim = create_simulation(10, TIME_STEP); |
|
|
|
Vec3 sun_pos = {0, 0, 0}; |
|
Vec3 sun_vel = {0, 0, 0}; |
|
add_body(sim, "Sun", 1.989e30, 6.96e8, sun_pos, sun_vel, -1, 1.0, 1.0, 0.0, 0, 0); |
|
|
|
Vec3 mars_pos = {2.244e11, 0, 0}; |
|
Vec3 mars_vel = {0, 24323, 0}; |
|
add_body(sim, "Mars", 6.39e23, 3.3895e6, mars_pos, mars_vel, 0, 0.8, 0.3, 0.1, 0, 2.244e11); |
|
|
|
OrbitTracker* tracker = create_orbit_tracker(1); |
|
|
|
double max_time = MAX_SIMULATION_DAYS * SECONDS_PER_DAY; |
|
while (sim->time < max_time && !tracker->orbit_completed) { |
|
update_simulation(sim); |
|
update_orbit_tracker(tracker, &sim->bodies[1], &sim->bodies[0], sim->time); |
|
} |
|
|
|
REQUIRE(tracker->orbit_completed); |
|
|
|
double measured_period_days = tracker->time_at_completion / SECONDS_PER_DAY; |
|
double period_error_days = fabs(measured_period_days - EXPECTED_PERIOD_DAYS); |
|
|
|
INFO("Expected period: " << EXPECTED_PERIOD_DAYS << " days"); |
|
INFO("Measured period: " << measured_period_days << " days"); |
|
INFO("Error: " << period_error_days << " days"); |
|
|
|
REQUIRE(period_error_days < 25.0); |
|
|
|
destroy_orbit_tracker(tracker); |
|
destroy_simulation(sim); |
|
}
|
|
|