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.
95 lines
3.2 KiB
95 lines
3.2 KiB
#include <catch2/catch_test_macros.hpp> |
|
#include <catch2/matchers/catch_matchers_floating_point.hpp> |
|
#include "../src/physics.h" |
|
#include "../src/simulation.h" |
|
#include "../src/config_loader.h" |
|
#include "../src/test_utilities.h" |
|
#include <cmath> |
|
|
|
using Catch::Matchers::WithinAbs; |
|
|
|
SCENARIO("Orbital period measurement with analytical propagation", |
|
"[period][analytical]") { |
|
const double TIME_STEP = 60.0; |
|
const double SECONDS_PER_DAY = 86400.0; |
|
|
|
SimulationState* sim = create_simulation(10, 0, 0, TIME_STEP); |
|
REQUIRE(load_system_config(sim, "tests/test_orbital_period.toml")); |
|
|
|
SECTION("Earth completes one orbit in ~365 days") { |
|
OrbitTracker* tracker = create_orbit_tracker(1); |
|
CelestialBody* earth = &sim->bodies[1]; |
|
CelestialBody* sun = &sim->bodies[0]; |
|
|
|
const double MAX_DAYS = 400.0; |
|
const double max_time = MAX_DAYS * SECONDS_PER_DAY; |
|
|
|
while (sim->time < max_time && !tracker->orbit_completed) { |
|
update_simulation(sim); |
|
update_orbit_tracker(tracker, earth, sun, sim->time); |
|
} |
|
|
|
REQUIRE(tracker->orbit_completed); |
|
|
|
double measured_days = tracker->time_at_completion / SECONDS_PER_DAY; |
|
INFO("Measured period: " << measured_days << " days"); |
|
|
|
REQUIRE_THAT(measured_days, WithinAbs(365.2105, 0.1)); |
|
|
|
destroy_orbit_tracker(tracker); |
|
} |
|
|
|
SECTION("Mars completes one orbit in ~671 days") { |
|
OrbitTracker* tracker = create_orbit_tracker(2); |
|
CelestialBody* mars = &sim->bodies[2]; |
|
CelestialBody* sun = &sim->bodies[0]; |
|
|
|
const double MAX_DAYS = 750.0; |
|
const double max_time = MAX_DAYS * SECONDS_PER_DAY; |
|
|
|
while (sim->time < max_time && !tracker->orbit_completed) { |
|
update_simulation(sim); |
|
update_orbit_tracker(tracker, mars, sun, sim->time); |
|
} |
|
|
|
REQUIRE(tracker->orbit_completed); |
|
|
|
double measured_days = tracker->time_at_completion / SECONDS_PER_DAY; |
|
INFO("Measured period: " << measured_days << " days"); |
|
|
|
REQUIRE_THAT(measured_days, WithinAbs(670.9345, 0.1)); |
|
|
|
destroy_orbit_tracker(tracker); |
|
} |
|
} |
|
|
|
SCENARIO("Orbit direction for zero inclination", |
|
"[direction][sanity]") { |
|
const double TIME_STEP = 60.0; |
|
const double TEST_DURATION_DAYS = 1.0; |
|
const int STEPS = (int)(TEST_DURATION_DAYS * 86400.0 / TIME_STEP); |
|
|
|
SimulationState* sim = create_simulation(2, 0, 0, TIME_STEP); |
|
REQUIRE(load_system_config(sim, "tests/test_energy.toml")); |
|
|
|
CelestialBody* sun = &sim->bodies[0]; |
|
CelestialBody* earth = &sim->bodies[1]; |
|
|
|
Vec3 initial_rel = vec3_sub(earth->global_position, sun->global_position); |
|
double theta_start = atan2(initial_rel.y, initial_rel.x); |
|
|
|
for (int i = 0; i < STEPS; i++) { |
|
update_simulation(sim); |
|
} |
|
|
|
Vec3 final_rel = vec3_sub(earth->global_position, sun->global_position); |
|
double theta_final = atan2(final_rel.y, final_rel.x); |
|
double delta = theta_final - theta_start; |
|
|
|
INFO("Initial angle: " << theta_start << " rad"); |
|
INFO("Final angle: " << theta_final << " rad"); |
|
INFO("Delta: " << delta << " rad"); |
|
|
|
REQUIRE_THAT(delta, WithinAbs(0.0172042841, 1e-6)); |
|
REQUIRE(delta > 0.0); |
|
}
|
|
|