feat: physics stability, k-NN information metrics & save system#149
Merged
Conversation
refactor(energy): deterministic pair loops refactor(forces): stage gravity pairs and document realtime limits
- energy: shared pairwise helpers used by charges/thermal\n- energy: reusable Local staging contexts + O(N) thermal sanity checks\n- forces: SoA staging path for one-way gravity hot loop\n- save_system: atomic writes + checkpoint/rollback world APIs\n- information: baseline entropy/MI estimator traits + deterministic tests\n\nPhysics behavior unchanged; focused on determinism/perf hygiene and observability. feat(energy): add optional strict pairwise neighbor determinism - add PairwiseDeterminismConfig (default off)\n- add shared ordered-neighbor emission helper\n- wire charges/thermal pairwise loops to strict mode\n- keep realtime fast path when strict mode is disabled\n\nThis closes inner-neighbor ordering nondeterminism for replay/campaign runs.
## N-Body Physics Fixes - Fixed orbital instability: star mass was 200k, scaled to 10k (matches reference) - Energy drift reduced from +19% to +0.017% (1000x improvement!) - Mars now maintains orbit within 1% of initial radius (no escape) - Orbital velocity formula verified correct; configuration was root cause ## Integration & Control - Decoupled speed_multiplier from integration timestep (keeps dt stable at ~0.01667s) - Fixed camera to stay at origin (no tracking drift) - Scaled Coulomb constant from 8.99e9 to 1.0 (simulation units) - Added comprehensive file-based physics diagnostics (energy, momentum, orbital drift) ## UI & Code Quality - Added bevy_egui 0.39 integration with interactive simulation controls - EguiPrimaryContextPass schedule (required for bevy_egui + Bevy 0.18) - Implemented charged planets with Coulomb forces alongside gravity - Cleaned dead code in information/fractals/renderer.rs - Removed unused glam dependency - All 41 MPM unit tests passing ## Physics Validation - Two-body action-reaction forces verified (Newton's 3rd law) - Momentum conservation: |p| < 0.02 over 60+ seconds - System stable at dt=0.01667s with Symplectic Euler - Plummer softening (ε=8.0) verified mathematically correct ## Configuration - Star mass = 10,000 (IRL-scaled) - Planets at realistic distances (Mercury 80, Mars 260, Neptune 520) - Orbital velocities match formula: v = r·√(G·M/(r²+ε²)^1.5)
Extend shannon.rs and mutual/calculation.rs with non-parametric k-NN based continuous entropy and mutual information estimation (Kraskov et al. 2004). ## What's Added - knn_estimators.rs: Core k-NN distance calculations, digamma approximation - Shannon::continuous_entropy() - entropy for continuous variables - Shannon::continuous_joint_entropy() - joint entropy for continuous data - MutualInfo::continuous_knn() - mutual information via k-NN - MutualInfo::continuous_conditional_knn() - conditional MI via k-NN ## Why This Matters - Replaces discrete binning with proper continuous estimation - Measure information flow from simulation data: 'bits exchanged between agent & environment' - Foundation for evolution selection based on actual information coupling - No external dependencies (pure Rust, algorithms only) ## Technical Details - k-NN algorithm in continuous space (Kraskov et al. 2004) - Noise addition to handle near-duplicate points - Digamma function for entropy calculation (asymptotic expansion) - All methods return entropy in bits (natural log divided by ln(2)) - Removed unused avgdigamma() dead code (inlined into continuous_knn)
Standardized documentation pattern across all 6 crate READMEs: - Core Principles (or Core API/Modules) - clear intent and scope - Scope & Limits - explicit constraints and approximations - Status - production-ready vs early-stage, known limitations Crates updated: - energy/: renamed "Scope and units" → "Core Principles", clarified status - forces/: removed unsupported Barnes-Hut reference, clarified Plummer softening formula - information/: already standardized (Feb 2026) - matter/: already standardized (Feb 2026) - systems/: already standardized (Feb 2026) - utils/: already standardized (Feb 2026) Added architectural TODO/FIXME/NOTE comments to document: - forces/src/lib.rs: integrator upgrade to Velocity Verlet, contact physics gap - energy/src/electromagnetism/charges.rs: Coulomb singularity workaround and fix roadmap - matter/src/lib.rs: comprehensive blocking issues (MPM, collision physics, constitutive models) - examples/basic_forces.rs: full rewrite needed for parameter exposure - information/src/lib.rs: encoding scaffold, evolution operators, reaction-diffusion roadmap - systems/src/lib.rs: MPM integration, acoustics coupling, AI information connection All notes follow consistent pattern: TODO/FIXME/NOTE with clear context and rationale.
- Velocity Verlet integrator (2nd-order, ~0.01% energy drift) - Fix silent query failure from missing PreviousAcceleration component - All physics to FixedUpdate; GravityForceMode default -> Mutual - Softened Coulomb at r < epsilon, bounded camera zoom with CoM
- Fix digamma Stirling expansion; KLDivergence with JS, TV, cross-entropy + tests - Discrete + continuous estimator traits; KnnEntropy/MI (default k=3) - PersistentId(u64), seeded counter, atomic write fix, VecDeque events - Chained version migration; remove dead helpers; basic_save.rs updated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Stabilize N-body simulation, add information-theoretic measurement tools
(k-NN entropy/MI, KL/JS divergence), introduce a production-grade save system
with stable entity identity, and clean up spatial indexing and pairwise staging.
Implementation
utils
forces / energy
PreviousAccelerationcomponent (silent Bevy query failure froze all particles)F = k·q/(r² + ε²)^1.5— no singularity at r < εFixedUpdate;GravityForceModedefault →Mutualcore / pairwise
information crate
KLDivergence:divergence(),jensen_shannon(),cross_entropy(),total_variation()save_system crate (new)
PersistentId(u64)+ seeded counter — stable cross-session entity identity, no UUIDstd::fs::rename, VecDeque event buffer, chained version migrationget_saved_entity_components()query by PersistentIddocs
Testing
cargo test --workspace)basic_saveexample: save → reload → entity lookup → migration, end-to-end verifiedVisual Changes
Technical Notes