No description
Find a file
2026-10-10 22:39:30 +00:00
media update README.md, and test with numba jit (no parralilization) + remove deepcopy 2026-09-26 13:58:36 -07:00
n_body destroyed everything, TODO: fix n_body class implementation (rk) 2026-05-11 00:01:44 -07:00
old change n_body sim to use numpy struct, and organizational changes 2026-07-27 21:25:32 -07:00
test reorganize 2026-10-10 15:20:57 -07:00
.gitignore updated README.md, and uploaded some plots 2026-05-21 04:04:00 -07:00
barnes_hut_n_body.py incomplete barnes hut. finish implementing it. 2026-09-27 17:56:22 -07:00
body.py energy conservation complete (mostly - collision wip) 2026-05-18 01:25:57 -07:00
damp_osc_with_rk_single.py created three body.py 2026-04-15 21:15:11 -07:00
draw.py Fixed panda3d cannot invert Lmatrix4 error, and updated README.md 2026-05-21 05:12:56 -07:00
draw.py.bak llm generated code for testing paarllization and numba fastmath+jit with 3000 bodies 2026-09-26 15:28:38 -07:00
draw_llm.py llm generated code for testing paarllization and numba fastmath+jit with 3000 bodies 2026-09-26 15:28:38 -07:00
gravity_sim.py change draw api to start and end frame; implement axes 2026-04-14 20:53:06 -07:00
n_body_gravity.py fix local changes 2026-09-26 19:21:21 -07:00
new_energy_n_body.py update README.md, and test with numba jit (no parralilization) + remove deepcopy 2026-09-26 13:58:36 -07:00
one_body.py n_body.py works for 3 bodies. Still need to implement collision (when r=0, it currently results in NaN). Also need to add functions to support moving CM, changing mass overtime, and energy support (to make it accurate to real life). 2026-04-17 06:52:32 -07:00
README.md Update README.md 2026-10-10 22:39:30 +00:00
rk.py n_body.py works for 3 bodies. Still need to implement collision (when r=0, it currently results in NaN). Also need to add functions to support moving CM, changing mass overtime, and energy support (to make it accurate to real life). 2026-04-17 06:52:32 -07:00
spring.py added semi-correct implementation of rk in rk.py 2026-04-12 20:51:24 -07:00
test.py energy conservation complete (mostly - collision wip) 2026-05-18 01:25:57 -07:00
test_particle_sim.py change n_body sim to use numpy struct, and organizational changes 2026-07-27 21:25:32 -07:00

Setup

pip install numpy pyglet matplotlib panda3d

matplotlib is not necessary if not plotting

Note:

This is meant to be an educational project to further our knowledge of the topics we encounter while building a physics engine, hence there are sub-optimal implementations of certain components (like RK4).

new_energy_n_body.py and n_body_gravity.py are the only ones that will currently run due to changes made to rendering. Other files haven't been updated with the new syntax yet. There is some LLM generated code, and such code has been clearly marked as LLM generated (only instances are some test code, optimized draw.py, and some plotting). We are currently working on implementing the fast multipole method (as well as Barnes-Hut for fun).

Additionally, we are thinking if it would be possible to use a modified fluid sim (something like FLIP) to compute the potential of varios fields (like electromagnetic, or gravitational) for one way of simulating the n-body problem in O(n) runtime while preserving causality (which our current method, and fast multipole method don't do).

To-do

  • Gravity on Earth
  • Build a 2d renderer based on Pyglet
    • Implement multi-object rendering
    • Reuse old shapes
  • Implement inelastic collision models
    • Come up with different models for bodies colliding
    • Supoort for different types of collisions (and respective energy+momentum changes)
  • [WIP] Build a 3d renderer based on Panda3D
  • Springs
  • Diff eq. support
  • Two-body problems
    • One fixed body
  • N-body problems
    • basic N-body problems
    • implement fast multipole method
    • parallelization
    • energy conservation
  • Particle simulation
    • similar to N-body sims, with perfectly elastic collisions
  • Fluid simulation

Switched from RK4 to velocity verlet for significant performance gains, and energy+momentum conservation since veocity verlet is a symplectic integrator and preserves volume in phase space transformations. We considered other integrators like 4-th order Yoshida, but they are much more computationally expensive, and deemed that the performance tradeoff isn't worth the accuracy gains. Instead, we are working on Bodies are stored as numpy structured arrays instead of a class objects for optimization purposes.

Example System

Energy and momentum graphs for an 27 body gravitational system with velocity verlet (with perfectly inelastic collision) (time step = 0.05): Total energy = -24902.30544431605; Total energy final (1000 seconds) = -24902.25451983801

Note: The change in energy is due to inelastic collision b/w colliding bodies. Additionally, the radii have a minimum radius of 1, despite "true" raidus being proportional to cbrt of mass, which would give sub pixel radii. This 1000 seconds run took about 214 seconds to finish on a single core.

Zoomed in energy plot

<p_x, p_y, p_z> represent each of the 3 lines. Total momentum = <-733.67653462, 3488.15404837, 0.>; Total momentum final (1000 seconds) = <-733.67653456, 3488.15404354, 0.>. Change in momentum over the course of 1000s = <0.00000006, −0.00000483, 0.>