TE
TechEcho
StartseiteTop 24hNeuesteBesteFragenZeigenJobs
GitHubTwitter
Startseite

TechEcho

Eine mit Next.js erstellte Technologie-Nachrichtenplattform, die globale Technologienachrichten und Diskussionen bietet.

GitHubTwitter

Startseite

StartseiteNeuesteBesteFragenZeigenJobs

Ressourcen

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. Alle Rechte vorbehalten.

Writing N-body gravity simulations code in Python

150 Punktevon dargscisyhpvor 5 Tagen

12 comments

mkoubaavor 2 Tagen
My favorite thing about this kind of code is that people are constantly inventing new techniques to do time integration. It's the sort of thing you'd think was a solved problem but then when you read about time integration strategies you realize how rich the space of solutions are. And they are more like engineering problems than pure math, with tradeoffs and better fits based on the kind of system.
评论 #43968985 未加载
评论 #43971940 未加载
umpalumpaaavor 2 Tagen
Every time I see anything on the N-Body problem I am reminded by my final high school project... I had 2-3 weeks of time to write an n-body simulation. Back then I used C++ and my hardware was really bad (2 GHz single core CPU or so…). The result was not really impressive because it did not really work. :D But I was able to show that my code correctly predicted that the moon and the earth would eventually collide without any initial velocity given to both bodies. I went into this totally naive and blind but it was a ton of fun.
评论 #43968461 未加载
antogninivor 2 Tagen
Once you have the matrix implementation in Step 2 (Implementation 3) it&#x27;s rather straightforward to extend your N-body simulator to run on a GPU with Jax --- you can just add `import jax.numpy as jnp` and replace all the `np.`s with `jnp`s.<p>For a few-body system (e.g., the Solar System) this probably won&#x27;t provide any speedup. But once you get to ~100 bodies you should start to see substantial speedups by running the simulator on a GPU.
评论 #43968385 未加载
评论 #43971240 未加载
评论 #43968977 未加载
the__alchemistvor 2 Tagen
Next logical optimization: Barnes Hut? Groups source bodies using a recursive tree of cubes. Gives huge speedups with high body counts. FMM is a step after, which also groups target bodies. Much more complicated to implement.
评论 #43968926 未加载
bgwaltervor 1 Tag
For comparison, the famous Debian language benchmark competition:<p><a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;performance&#x2F;nbody.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a>
评论 #43975684 未加载
munchlervor 2 Tagen
In Step 2 (Gravity), why are we summing over the <i>cube</i> of the distance between the bodies in the denominator?<p>Edit: To answer myself, I think this is because one of the factors is to normalize the vector between the two bodies to length 1, and the other two factors are the standard inverse square relationship.
评论 #43969188 未加载
igouyvor 1 Tag
Also:<p>ParallelNBodyPerformance Public<p><a href="https:&#x2F;&#x2F;github.com&#x2F;MarkCLewis&#x2F;ParallelNBodyPerformance">https:&#x2F;&#x2F;github.com&#x2F;MarkCLewis&#x2F;ParallelNBodyPerformance</a>
eesmithvor 1 Tag
Back in the early 1990s I was going through books in the college library and found one on numerical results of different 3-body starting configurations.<p>I vividly remember the Pythagorean three-body problem example, and how it required special treatment for the close interactions.<p>Which made me very pleased to see that configuration used as the example here.
mclau157vor 2 Tagen
Very well done!
quantadevvor 2 Tagen
I was fascinated with doing particle simulations in &quot;C&quot; language as a teenager in the late 1980s on my VGA monitor! I would do both gravity and charged particles.<p>Once particles accelerate they&#x27;ll just &#x27;jump by&#x27; each other of course rather than collide, if you have no repulsive forces. I realized you had to take smaller and smaller time-slices to get things to slow down and keep running when the singularities &quot;got near&quot;. I had a theory even back then that this is what nature is doing with General Relativity making time slow down near masses. Slowing down to avoid singularities. It&#x27;s a legitimate theory. Maybe this difficulty is why &quot;God&quot; (if there&#x27;s a creator) decided to make everything actually &quot;waves&quot; as much as particles, because waves don&#x27;t have the problem of singularities.
kristel100vor 1 Tag
N-body problems are the gateway drug into numerical physics. Writing one from scratch is a rite of passage. Bonus points if you hit that sweet spot where performance doesn’t completely tank.
评论 #43971735 未加载
gitroomvor 2 Tagen
Man I tried doing this once and my brain nearly melted lol props for actually making it work