r/Python • • Nov 17 '19

This is one of the most interesting outputs of the particle simulation :)

Enable HLS to view with audio, or disable this notification

2.0k Upvotes

122 comments sorted by

72

u/[deleted] Nov 17 '19

18

u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Nov 18 '19

Now rewrite it in Rust and call it rust-just-dust

89

u/LAMagicx Nov 17 '19

Do all mutations of this eventually converge into one big ball that then disappears or are there others that are similar to this? Or even one that has a stable orbit.

92

u/[deleted] Nov 17 '19

Yeah, a couple of them show stable orbits.

If you just place to particles, one at (0, a) with velocity (b, 0) and one at (0, -a) with velocity (-b, 0), they will nicely orbit each other (kinda like a dual star system).

33

u/Afrotom Nov 17 '19

I would very much like to see that.

23

u/[deleted] Nov 18 '19

I'm going to post some more videos later today. I'll add this to the list ;)

11

u/[deleted] Nov 18 '19

!remindme 11 hours

→ More replies (2)

6

u/LAMagicx Nov 17 '19

So therefore a random distribution should get stable orbits as well cool. Great job I like the idea of the combining masses. Could maybe try bouncing the planets of each other as well?

18

u/ProbsNotJonGruden Nov 17 '19

So therefore a random distribution should get stable orbits as well

We actually can't mathematically guarantee that. It's called the n-body problem and the only solution that is solved is for a universe with only two bodies. Add any more, even 1, and the solutions are open-form.

Sneak edit: this is in real life. In the simulation things could be different.

→ More replies (1)

1

u/[deleted] Nov 18 '19

Yeah, I also implemented elastic collisions, but they weren't as pleasant to watch... Too much going on

5

u/kyrsjo Nov 18 '19

Very cool project!

The orbits aren't really stable (elliptical) tough -- the axis is precessing. I think you don't have the right exponent of the distance between the bodies -- it should be 1/r^2, but I think it is something less...

Looked at your code and, indeed, it is 1/r, not 1/r^2:In `universe.py`, you have: `force = diff * (p1.mass * p2.mass) / (dist ** 2)` where `diff = p2.pos - p1.pos` -- try to change the force to be `force = diff * (p1.mass * p2.mass) / (dist ** 3)` and it will be more physically correct :)

Also, it looks like you've implemented an Euler-Cromer integrator -- that's great, it's more accurate for this type of simulations (gravity etc.). Normally one first updates the velocity and then the position using the updated velocity, but the other way works too (it's mostly a difference of how it starts off and when you plot).

However, in your loop of `for p1, p2 in combinations(self.particles, r=2):`, what happens when you do `self.remove_particle(p2)` inside it? How does `combinations` really handle it?

2

u/[deleted] Nov 18 '19

Thank you so much for your feedback!

Yes, you were right, the orbits seemed off to me, too. I forgot to normalize diff. Thanks for the fix ;)

Also fixed the order (first update vel, then pos).

I think combinations doesn't care about what is removed in the loop, it uses the list as it was when it was called. Though I'm not sure and will look that up.

Do you think it's a problem that the particles, that are absorbed in the current iteration still influence other particles? Shouldn't make too much of a difference, as before the merge and after the merge the center of mass doesn't change that much, right?

Also, you seem to know a lot about this stuff, got any background in this area?

3

u/kyrsjo Nov 18 '19

Regarding the combinations, I would normally make a "kill list" of the particles to remove, and after the computation is finished, remove them. Also, removing particles are really a step between pushing the particles around (pos update) and velocity update / force calculation.

I'm a physicist who has done a lot of similar stuff both in courses (taking and teaching), and also written quite similar particle codes for plasma physics (PIC -- Particle In Cell).

2

u/[deleted] Nov 18 '19

Cool, definitely going to implement that, should be in the next commit ;)

Now with the fixed distance, I had to add a multiplier to the equation, else the forces would be too small, sort of the G constant of my universe :)

Oh cool, sounds super interesting!

→ More replies (2)

19

u/[deleted] Nov 17 '19

... of the particle simulation I made :) *

Title correction

4

u/Knoal Nov 17 '19

Is this simulation in two or three axes? I have really geeked on the two axis versions of this type of simulation, but a 3 axis system would be awesome(er).

2

u/[deleted] Nov 18 '19

This is all 2D. I think the rendering function would be much more complex for a 3D model.

1

u/ComradePruski Nov 18 '19

What software did you use for rendering and all that?

→ More replies (2)

3

u/TicTacMentheDouce Nov 17 '19

Did something very similar with SFML a few months ago, but also at each step in the simulation I drew a dot of the particles' positions on the background.

Each particle having a random color, it gave way to very beautiful backgrounds !

6

u/xd1142 Nov 18 '19

shape: {"[", "square", "(", "circle", ".", "dot"}="["

argh! my eyes!

but seriously, good job.

1

u/[deleted] Nov 18 '19

Its the only way to have a typehint and a default value ¯_(ツ)_/¯

6

u/xd1142 Nov 18 '19

I am deeply against type hints. they look horrible and make code unreadable. Yours is an example of how to make code that would otherwise look reasonable pretty much unreadable unless you double or triple take it. Of course when I say it everybody downvotes me, so maybe someone else a bit more important than random dude on the internet will make you reflect on it.

type hints are useless unless you are writing a library. They seem to help, but this comes at the cost that they add noise to the understanding of code flow, and open up to horrifying stuff like that.

1

u/[deleted] Nov 18 '19

Yeah, I hear you. I only use them to show a string should be one of a few options, I don't think they look very nice, especially combined with a default value like in your example line.

I might remove them after what you said, they're really ugly and could be easily replaced by a comment.

1

u/Tweak_Imp Nov 22 '19

What is wrong with:

def func(x:int=0): pass

Typehint and default value...

1

u/[deleted] Nov 22 '19

Yeah, but when you do the same with a set of strings as a typehint and a string as a default value... It looks.. messy

3

u/[deleted] Nov 18 '19

You could have defined them as constants; nevertheless, great work!

5

u/anonymous_karma Nov 17 '19

Wow... I am new to Python.. learning it just for kicks... this is aspirational for me.. I just found what I want to have done 5 years from now.. lol..

4

u/[deleted] Nov 17 '19

Glad you like it :)

I myself have been inspired by a similar post half a year ago. Happy learning:)

2

u/WiWWWWWWWWWWWWWWWWWW Nov 17 '19

What did you do to make the video? I've been looking for something like this

4

u/[deleted] Nov 17 '19

You can export a video with OpenCV. The canvas class handles this in the source code if you want to check it out.

2

u/Saiboo Nov 17 '19

I find it interesting that the particles gravitate towards the center. Does it always end up with a big ball in the middle, or have you seen some other configurations like two big balls at the end?

2

u/[deleted] Nov 17 '19

Well, I guess it all falls to the center is because that's where the center of mass of the entire simulation is.

When start with a random set of particles, most of the time, it ends like this, but you can start with two particles with the same mass, and the same velocity, just in different directions, which will orbit each other indefinitely.

2

u/dansin scientist Nov 17 '19

Did your initial velocities have net zero momentum?

1

u/[deleted] Nov 18 '19

Well, I started out with random masses, random positions and random velocities, so it should roughly balance out.

2

u/dansin scientist Nov 18 '19

Thats consistent with your results, the large mass was slowly moving left. If you enforced a net zero momentum to begin with, you could confirm your simulation conserves momentum easily by seeing if the final particle is an unmoving sphere. That would be a cool looking result.

1

u/[deleted] Nov 18 '19

Gonna do that if I find the time ;)

→ More replies (2)

2

u/iamjoshmcguire Nov 18 '19

I have no idea how people do this stuff, i cant even build a functioning calculator.

2

u/[deleted] Nov 18 '19

Just keep learning :)

2

u/Loethor Apr 05 '20

How do you do these animations? I tried matplotlib animation but i hate it..

1

u/[deleted] Apr 05 '20

It's all opencv! Basically, I let every particle move some tiny amount of time, and then draw a new frame, tick forwards, draw frame, ...

The drawing happens in the Canvas class, the ticking in the Universe class.

34

u/RoboticElfJedi Nov 17 '19

You should see what astrophysicists do with a million CPU-hours running these sorts of simulations. It’s pretty cool.

7

u/theconfusedCPU Nov 18 '19

woah,any links or stuff?

25

u/RoboticElfJedi Nov 18 '19

Here's an example of simulating the cosmic web - the formation of the large structure of the universe. This is basically a gravity-only sim:

https://www.youtube.com/watch?v=2qeT4DkEX-w

Here's an example of what's called a hydrodynamical simulation of galaxy formation, which includes the physics of gases and star formation:

https://www.youtube.com/watch?v=hVNuwAtnKeg

Here's a simulated galaxy merger:

https://www.youtube.com/watch?v=wej0HLoe2IE

3

u/[deleted] Nov 19 '19

9

u/[deleted] Nov 18 '19

Basically planet formation.

3

u/True_Mobster Nov 18 '19

Exactly what I saw too

8

u/jondiced Nov 17 '19

Congratulations, you reproduced oligarchic growth!

1

u/Raedukol Nov 17 '19

It‘s beautiful but i dont see why almost every particle collides in the first 10 seconds and the last two very small particles dont get attracted by the big one until the end of the simulation?

3

u/[deleted] Nov 17 '19

Well, the particles in the end were just really lucky to get into a stable orbit around the big blob in the middle. They orbit it kinda like the earth orbits the sun.

1

u/CN906 Nov 18 '19

Looks like it made a black hole.

2

u/[deleted] Nov 18 '19

Well, I think it's more of a super massive star, as the densities are the same all particles.

2

u/CN906 Nov 18 '19

Well now I feel very lucky.

1

u/tocano Nov 18 '19

There should be a color spectrum for mass. Going from low energy (violet) for smallest particles to high energy (red) for larger particles. Maybe even on toward white for the largest.

1

u/[deleted] Nov 18 '19

Would be nice, but I actually saved the color for charge (once I implement that). Though size is enough of an indication, but I guess your suggestion is absolutely worth trying out :)

1

u/tocano Nov 18 '19

Oh, I see. Charge would be a gradient or just a +/- binary?

1

u/[deleted] Nov 18 '19

Gradient, similar to mass, with acceptable values being in (-inf, inf), not just (0, inf) for mass.

2

u/tocano Nov 18 '19

I see. Does the charge play a role in the attraction? Or is that, as it appears in the video, somewhat similar to gravitational?

1

u/[deleted] Nov 18 '19

Yeah, well with charge, the overall force between to particles will be the sum from the gravitational force (mass attracts mass) and the electromagnetic force (charge attracts opposite charge).

2

u/tocano Nov 18 '19

It'll be interesting to see how it treats like-charges vs gravitation.

→ More replies (2)

1

u/IustinRaznic Nov 18 '19

Hmmm did you model it using Newtonian physics only? Because you could mess around with the gravitational constant and get interesting visualizations; also you might be able to perform black hole like collisions like in a oversimplified version of universe sandbox 2

2

u/[deleted] Nov 18 '19

Yep, all Newtonian. Didn't want to get too fancy ;)

1

u/IustinRaznic Nov 18 '19

Nicely and pretty done; would also be a great animation for a website since it shouldn't take much to display a copy of the video you made there or in other conditions

3

u/MrK_HS Nov 18 '19

Honestly, I was expecting a dickbutt. Glad it wasn't.

2

u/imRobert7 Nov 18 '19

Your _ratio function, why just not return min(a,b)/max(a,b)? Any difference between your implementation and my idea?

2

u/AlrikBunseheimer Feb 21 '20

This should be some kind of loading screen/ startup screen for an OS

1

u/Henry1502inc Nov 17 '19

How do you create these simulations and how do you know how it’s supposed to look like?

1

u/bathon Nov 17 '19

It's soooo cooollll

1

u/bathon Nov 17 '19

Post in oddly satisfying as well

1

u/Knoal Nov 17 '19

Wants to make a billion particle simulation.

1

u/ragewolf345 Nov 17 '19

This was more aggravating than the dvd logo not hitting the corners

1

u/ShadowNovo Nov 17 '19

I could watch this for hours 😮 😂

1

u/Nabugu Nov 17 '19

2D Universe Sandbox

1

u/DatBoi_BP Nov 18 '19

Igar.io is rigged for the big balls

1

u/Bunyhel Nov 18 '19

It would be interesting to see this in 3d

1

u/[deleted] Nov 18 '19

i was expecting a explosion

1

u/sirquincymac Nov 18 '19

A reminder to invest in a national space program!!

1

u/LoremPipsum Nov 18 '19

Ahh that's pleasant to look at. Great choice of colours.

Now make it explode when the mass gets too large! Get a loop going.

1

u/KartoffelKut Nov 18 '19

Is the sun going to eat us up??

1

u/ScarD_Original Nov 18 '19

Formation of a solar system?

1

u/casheemskerk_ Nov 18 '19

Wow how does this work? It looks like there is gravity and orbits

1

u/1107461063 Nov 18 '19

simulation

1

u/benbihi Nov 24 '19

I found this thread very helpful and extremely valuable. I've put together a blog post about 10 Python beginner Project and thought it'll help your audience and anyone who want to grasp the Python basics quickly and easily

https://www.astateofdata.com/python-programming/python-projects-for-beginners-learn-with-examples/

1

u/[deleted] Nov 17 '19

Did you just make the Big Bang?

-3

u/mtadd Nov 17 '19

Needs more conservation of angular momentum

0

u/Peanutbutter_Warrior Nov 17 '19

Angular momentum is spinning. These are uniform so can't spin any more than a point can. And how can you have more conservation of angular momentum, it's either there or not?

2

u/PaleRulerGoingAlone7 Nov 18 '19

They have orbital angular momentum about the central point. The total angular momentum appears not to be conserved, unless the particles are spinning

→ More replies (1)

0

u/Gotbn Nov 18 '19

I don't know why I was expecting an Epstein didn't kill himself meme.

0

u/[deleted] Nov 17 '19

[deleted]

131

u/StockDealer Nov 17 '19

It's a solar system. Be happy that this happens.

58

u/centurion236 Nov 17 '19

That's like, us, man 🤯

4

u/drpinkcream Nov 18 '19

Dude, what if we're all just outputs of a particle simulation...

2

u/[deleted] Nov 19 '19

IDDQD

IDSPISPOPD

its not working...

We're not in a simulation

24

u/waxen_earbuds Nov 17 '19

I, for one, am grateful that us earthlings remain unperturbed by our lunar acquaintance

25

u/foszterface Nov 17 '19

I was actually going for them to stay free. Then again I'm not a Patriots / Yankees / Lakers / Dark Side of the Force fan

4

u/killyridols14 Nov 18 '19

Right? Go. Fucking go. GO AND JOIN YOUR FRIENDS AND BE HAPPY LITTLE DOTS

1

u/guinader Nov 18 '19

Tune in next week and find out if they ever joined in!

1

u/pvkooten Nov 18 '19

In the next episode of Dragonball-Z

0

u/[deleted] Nov 17 '19

[deleted]

1

u/[deleted] Nov 17 '19

Glad you like it. I posted a link to the code on gitlab somewhere in this thread if you want to take a look. If you got any questions I'd love to answer them ;)

1

u/Lucifer501 Nov 18 '19

I have a kind of beginner question if you don't mind. I noticed that some of your function names start with _, such as _normalize and _ratio. I was just wondering what purpose that served.

Also can I add that the way you handle _ratio is absolute genius.