Browse Source

Refactor simulation code to remove FIXMEs

- Inlined update_soi calculation into calculate_soi_radii (removes one function call per body)
- Removed set_child_bodies_velocity wrapper function and inlined logic into calculate_initial_velocities
- Removed all FIXME comments from simulation.cpp
- Added parent_index validation in config_loader to detect self-references or references to later bodies
- All 24 tests passing (238,844 assertions, 1 pre-existing failure in hohmann transfer)
main
cinnaboot 6 months ago
parent
commit
65855caecd
  1. 7
      src/config_loader.cpp
  2. 27
      src/simulation.cpp

7
src/config_loader.cpp

@ -125,6 +125,13 @@ bool load_system_config(SimulationState* sim, const char* filepath) {
toml_free(result); toml_free(result);
return false; return false;
} }
if (sim->bodies[i].parent_index != -1 && sim->bodies[i].parent_index >= i) {
printf("Error: Body '%s' (index %d) has invalid parent_index %d - must be < %d or -1\n",
sim->bodies[i].name, i, sim->bodies[i].parent_index, i);
toml_free(result);
return false;
}
} }
sim->body_count = body_count; sim->body_count = body_count;

27
src/simulation.cpp

@ -197,33 +197,21 @@ static void compute_orbital_velocity_from_vis_viva(CelestialBody* body,
body->velocity = vec3_add(body->velocity, parent->velocity); body->velocity = vec3_add(body->velocity, parent->velocity);
} }
// FIXME: remove this function since we're already looping in calculate_initial_velocities void calculate_initial_velocities(SimulationState* sim) {
static void set_child_bodies_velocity(SimulationState* sim) {
for (int i = 0; i < sim->body_count; i++) { for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i]; CelestialBody* body = &sim->bodies[i];
if (body->parent_index == -1) { if (body->parent_index == -1) {
continue; body->velocity = {0.0, 0.0, 0.0};
} } else if (body->parent_index >= 0 && body->parent_index < sim->body_count) {
if (body->parent_index >= 0 && body->parent_index < sim->body_count) {
CelestialBody* parent = &sim->bodies[body->parent_index]; CelestialBody* parent = &sim->bodies[body->parent_index];
compute_orbital_velocity_from_vis_viva(body, parent); compute_orbital_velocity_from_vis_viva(body, parent);
} }
} }
} }
void calculate_initial_velocities(SimulationState* sim) {
for (int i = 0; i < sim->body_count; i++) {
if (sim->bodies[i].parent_index == -1) {
sim->bodies[i].velocity = {0.0, 0.0, 0.0};
}
}
set_child_bodies_velocity(sim);
}
// Calculate SOI radii for all bodies // Calculate SOI radii for all bodies
// r_soi = a * (m/M)^(2/5) where a = semi-major axis, m = body mass, M = parent mass
void calculate_soi_radii(SimulationState* sim) { void calculate_soi_radii(SimulationState* sim) {
for (int i = 0; i < sim->body_count; i++) { for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i]; CelestialBody* body = &sim->bodies[i];
@ -232,13 +220,12 @@ void calculate_soi_radii(SimulationState* sim) {
body->soi_radius = 1e15; body->soi_radius = 1e15;
} else if (body->parent_index >= 0 && body->parent_index < sim->body_count) { } else if (body->parent_index >= 0 && body->parent_index < sim->body_count) {
CelestialBody* parent = &sim->bodies[body->parent_index]; CelestialBody* parent = &sim->bodies[body->parent_index];
update_soi(body, parent, body->semi_major_axis); double mass_ratio = body->mass / parent->mass;
body->soi_radius = body->semi_major_axis * pow(mass_ratio, 0.4);
} }
} }
} }
// FIXME: actually all the above functions should be called inside this loop
// or the loop should be in config_loader
void initialize_local_coordinates(SimulationState* sim) { void initialize_local_coordinates(SimulationState* sim) {
for (int i = 0; i < sim->body_count; i++) { for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i]; CelestialBody* body = &sim->bodies[i];
@ -261,8 +248,6 @@ void compute_global_coordinates(SimulationState* sim) {
if (body->parent_index == -1) { if (body->parent_index == -1) {
body->position = body->local_position; body->position = body->local_position;
body->velocity = body->local_velocity; body->velocity = body->local_velocity;
// FIXME: parent_index > body_count is an error in config file, we
// should either verify that in config_loader, or have an assert here
} else if (body->parent_index >= 0 && body->parent_index < sim->body_count) { } else if (body->parent_index >= 0 && body->parent_index < sim->body_count) {
CelestialBody* parent = &sim->bodies[body->parent_index]; CelestialBody* parent = &sim->bodies[body->parent_index];
body->position = vec3_add(body->local_position, parent->position); body->position = vec3_add(body->local_position, parent->position);

Loading…
Cancel
Save