From 65855caecd904695b17a12052936d84f6eb33636 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Sat, 17 Jan 2026 10:45:04 -0500 Subject: [PATCH] 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) --- src/config_loader.cpp | 7 +++++++ src/simulation.cpp | 27 ++++++--------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/config_loader.cpp b/src/config_loader.cpp index 415a27e..db567bc 100644 --- a/src/config_loader.cpp +++ b/src/config_loader.cpp @@ -125,6 +125,13 @@ bool load_system_config(SimulationState* sim, const char* filepath) { toml_free(result); 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; diff --git a/src/simulation.cpp b/src/simulation.cpp index 728121d..73b9d55 100644 --- a/src/simulation.cpp +++ b/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); } -// FIXME: remove this function since we're already looping in calculate_initial_velocities -static void set_child_bodies_velocity(SimulationState* sim) { +void calculate_initial_velocities(SimulationState* sim) { for (int i = 0; i < sim->body_count; i++) { CelestialBody* body = &sim->bodies[i]; if (body->parent_index == -1) { - continue; - } - - if (body->parent_index >= 0 && body->parent_index < sim->body_count) { + body->velocity = {0.0, 0.0, 0.0}; + } else if (body->parent_index >= 0 && body->parent_index < sim->body_count) { CelestialBody* parent = &sim->bodies[body->parent_index]; 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 +// r_soi = a * (m/M)^(2/5) where a = semi-major axis, m = body mass, M = parent mass void calculate_soi_radii(SimulationState* sim) { for (int i = 0; i < sim->body_count; i++) { CelestialBody* body = &sim->bodies[i]; @@ -232,13 +220,12 @@ void calculate_soi_radii(SimulationState* sim) { body->soi_radius = 1e15; } else if (body->parent_index >= 0 && body->parent_index < sim->body_count) { 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) { for (int i = 0; i < sim->body_count; i++) { CelestialBody* body = &sim->bodies[i]; @@ -261,8 +248,6 @@ void compute_global_coordinates(SimulationState* sim) { if (body->parent_index == -1) { body->position = body->local_position; 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) { CelestialBody* parent = &sim->bodies[body->parent_index]; body->position = vec3_add(body->local_position, parent->position);