
Show HN: Harmony of the Spheres – Gravity Simulator - the_happy_koala
https://thehappykoala.github.io/Harmony-of-the-Spheres/
======
the_happy_koala
I just realized that I'm yet to properly introduce myself to you folks; name's
Darrell (the Happy Koala - I love koalas the way other people seem to love
cats!). I'm a self taught developer and the project this thread is concerned
with is actually the reason, pretty much, I started dabbling with code. If you
want more of a background, you can check out an article I wrote over at Medium
about the evolution of this project and how it led to me becoming a web
developer - [https://medium.com/@darrell.arjuna.huffman/how-an-
interest-i...](https://medium.com/@darrell.arjuna.huffman/how-an-interest-in-
physics-led-me-to-a-new-career-in-web-development-f863ea118099) (please bear
with any spelling or grammatical mistakes as English is not my first
language). I'm still very much of a junior empty stack developer, but I love
coding and intend to continue doing so, so hopefully we'll have lots of
interesting discussions here on Hacker News in the future! Having said that,
have a nice start to the week!

~~~
PaulHoule
(I used to simulate classical mechanics for a living, so...)

It's pretty good except it runs a lot slower than it has to.

The default step size is too small for most cases so you have to wait a long
time for the planets to move around.

When I tried increasing the step size with the slider I'd inevitably increase
it too much and the planets would fly away.

Personally I always use RK4 and similar algorithms with adaptive step size,
mostly because it is a pain to tune the step size by hand

[https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods#Ad...](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods#Adaptive_Runge%E2%80%93Kutta_methods)

also if planets are passing close to each other you may need a shorter step
when you are close and a longer step when you are far away.

RK4 or RK5 with adaptive timesteps will make your demo much better with very
little work.

There are methods that use higher order derivatives and other tricks to take
large steps, for instance I like this method

[https://github.com/alexrudy/bsint](https://github.com/alexrudy/bsint)

If you were integrating the solar system for millions of years you would want
a symplectic integrator.

[https://en.wikipedia.org/wiki/Symplectic_integrator](https://en.wikipedia.org/wiki/Symplectic_integrator)

this might not be more accurate than another integrator but it will preserve
phase space area which makes it possible that the long-term dynamics will
"look like" the real long-term dynamics. (Otherwise it certainly won't!)

If it were me in cases where there is a central large mass I would integrate
in spherical or cylindrical coordinates, probably I would use a predictor-
corrector method.

~~~
lilgreenland
Any thoughts on Leapfrog integration? I've got a super basic n-body simulator
that I'm using for gravity and for coulomb's law. I also need to advance to
adaptive time steps.

[https://landgreen.github.io/physics/notes/electromagnetism/p...](https://landgreen.github.io/physics/notes/electromagnetism/potential/index.html#three-
potential-sims)

~~~
the_happy_koala
Can't stop myself from staring at those modulating peaks; hypnotizing stuff!
Leapfrog integration, as foxes pointed out, is not symplectic if you apply an
adaptive time step. I'm not that concerned about the integrator being
symplectic as it's not my intention to run simulations that last for millions
of years. If it's possible to add comments to a thread here on hacker news
(new to this site!) indefinitely, I'll give you a heads up when I've
implemented an integrator with an adaptive time step, and in case you get to
it before I do, do share :D!

------
pavel_lishin
Any idea why Juno appears to be pre-programmed to start with a pretty huge
radial outward velocity?

~~~
the_happy_koala
Hi Pavel!

Every body in the simulation start out with a set of initial position and
velocity vectors. For the solar system scenarios, I got these vectors from
NASA JPL's Horizon System
([https://ssd.jpl.nasa.gov/horizons.cgi#top](https://ssd.jpl.nasa.gov/horizons.cgi#top)),
so the orbit of Juno that you are observing is its actual orbit... Its
eccentricity (the extent to which the shape of the orbit departs from a
perfect circle) is extremely high; at its closest point, the orbit brings Juno
right above the cloud tops of Jupiter, and at its furthest point Juno finds
itself beyond Themisto (one of Jupiter's irregular Moons which I should
perhaps add to the scenario). Juno was meant to have a less eccentric orbit,
but as technical problems were encountered when they were going to fire Juno's
thrusters to reduce the eccentricity of the orbit, NASA played it safe and let
Juno stay on its eccentric orbit. One of the advantages of this is that Juno
only spends a very short amount of time in Jupiter's insanely powerful and to
a spacecraft damaging magnetic field, so the technical mishap is probably one
of the reasons JunoCam is still operating and giving us beautiful images of
Jupiter's swirling clouds! Here's a gif from WikiPedia showing how stretched
out Juno's orbit is
[https://en.wikipedia.org/wiki/Juno_(spacecraft)#/media/File:...](https://en.wikipedia.org/wiki/Juno_\(spacecraft\)#/media/File:Animation_of_Juno_trajectory_around_Jupiter.gif).
Sorry for the long rant, but I find these things to be awesomely fascinating.

~~~
pavel_lishin
> _Sorry for the long rant_

You've got nothing to apologize for, that was fascinating! I knew its orbit
had some eccentricity, but I didn't know it was _that_ much!

------
enriquto
I would love if kerbal, simplerockets and the like used this realistic gravity
simulation. The piecewise conic approximation is great, but after a few weeks
of playing it gets very tiresome and too obviously limited...

~~~
magnetic
Have you looked into Orbiter 2016?
[http://orbit.medphys.ucl.ac.uk](http://orbit.medphys.ucl.ac.uk) -

I recently learned about it and wanted to try it after playing with KSP, but
it seems it's Windows only.

~~~
rzzzt
Bach's Air is now permanently ingrained into my brain, paired with the
soothing rumbling of booster rockets.

------
jasmcole
For long simulations I'd like to increase the timestep, but then RK4 becomes
less accurate. For the n body problem, do symplectic integrators work well?

~~~
alkonaut
Yes, the difference is enormous. Use e.g Verlet integration and it’s much more
stable. That it’s also much simpler and more elegant RK4 is an added bonus.

~~~
the_happy_koala
If for whatever reason you have time and would like to make a contribution, I
think doing so should be quite easy as I think you'd just have to subclass the
Euler class. You can get an idea of how to do this by looking at how I did it
with the RK4 subclass ([https://github.com/TheHappyKoala/Harmony-of-the-
Spheres/tree...](https://github.com/TheHappyKoala/Harmony-of-the-
Spheres/tree/master/src/js/Physics/Integrators)). If not, I'll add Verlet
integration myself one of these days, but right now I'm working on introducing
a particle system so that I can simulate the rings of Saturn (the particles
will feel the gravity of all the masses in the simulation, but they won't
exert a gravitational force on the other particles and masses in the
simulation; this way you should be able to have fun distorting the rings of
Saturn with a celestial object of your choice).

------
xixixao
This actually works suprisingly well on my phone! Well done, love being able
to modify masses mid-scenario and see the whole system disintegrate.

~~~
the_happy_koala
Thankie!

------
gerdesj
I've just spent some time on this. The Oumuamua scenario is superb - that
alone is worth every penny.

Thank you.

------
budhajeewa
Couldn't try this as I am phone. But I do find gravity simulators oddly
satisfying.

~~~
the_happy_koala
My CSS skills aren't what they should be, but I'm going to try to make a
mobile friendly version of the simulator at some point once I know more about
flexboxes, media queries and what have you.

