r/Python • u/[deleted] • Nov 17 '19
This is one of the most interesting outputs of the particle simulation :)
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
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
Nov 18 '19
I'm going to post some more videos later today. I'll add this to the list ;)
11
Nov 18 '19
!remindme 11 hours
4
u/Pannuba Nov 18 '19
It's been 11 hours 👀
4
Nov 18 '19
2
u/Pannuba Nov 18 '19
Cut it and post it to /r/perfectloops and /r/loadingicon for maximum karma gain!
Pretty cool though.
2
5
u/allomanticpush Nov 18 '19
I like it! At first I thought it was a new buffering indicator on imgur, haha
1
u/RemindMeBot Nov 18 '19 edited Nov 18 '19
I will be messaging you on 2019-11-18 16:39:03 UTC to remind you of this link
9 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
There is currently another bot called u/kzreminderbot that is duplicating the functionality of this bot. Since it replies to the same RemindMe! trigger phrase, you may receive a second message from it with the same reminder. If this is annoying to you, please click this link to send feedback to that bot author and ask him to use a different trigger.
Info Custom Your Reminders Feedback 0
u/kzreminderbot Nov 18 '19
There is a 2 hours delay to fetch comment reminders. Thanks for your patience! See KZReminderBot Stats for more statistics. PMs are unaffected by delay. You can also use this tool to manually load reminder using Reddit link.
chrismit3s, your reminder arrives in 9 hours on 2019-11-18 16:39:03Z 🛫
r/Python: This is one of the most interesting outputs of the particle simulation :) #1
CLICK THIS LINK to also be reminded. Thread has 1 reminder and 1/4 confirmation comments.
OP can Delete Comment · Delete Reminder · Get Details · Update Time · Update Message · Add Timezone · Add Email
KZReminderTool · Create Reminder · Your Reminders · Give Feedback
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.
1
u/flutefreak7 Dec 07 '19
I think specific initial conditions of the 3-body problem for example have been solved, so it's not just the n>2 that makes it "unsolvable" but the fact that you permit so many degrees of freedom for each particle. Like if you have a big mass that is at rest and two orbiting it at opposite locations with equal mass and orbital periods, then that's a stable, predictable 3-body system. If you change anything about the mass or velocity of the 2 orbiters, then the two aren't balanced and they cause the middle one to move and you get chaos. You could also have 3 equal masses perfectly orbiting their shared center of mass. I have to imagine there are other more complex yet stable patterns, but they all require extra assumptions to constrain the problem.
1
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
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, thenpos).I think
combinationsdoesn'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
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!
-2
u/icbint Nov 18 '19
so what you're saying is that the moon will eventually hit the earth... great...
1
19
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
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?
1
Nov 18 '19
Python and opencv, the
Canvasclass handles the drawing stuff.1
u/ComradePruski Nov 18 '19
Thanks! I've used Canvas in a couple of my classes but it gives me a headache. I can't imagine how annoying it would be to do something like this.
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
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
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
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
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
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
Nov 17 '19
You can export a video with OpenCV. The
canvasclass 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
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
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
0
Nov 18 '19
[deleted]
2
u/dansin scientist Nov 18 '19
Yeah, thats why it would be checking whether the simulation conserves momentum. I'm assuming OP is using euler integration as a first pass
2
u/iamjoshmcguire Nov 18 '19
I have no idea how people do this stuff, i cant even build a functioning calculator.
2
2
u/Loethor Apr 05 '20
How do you do these animations? I tried matplotlib animation but i hate it..
1
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
Canvasclass, the ticking in theUniverseclass.
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:
3
9
8
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
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
Nov 18 '19
Well, I think it's more of a super massive star, as the densities are the same all particles.
2
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
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
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
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.
1
Nov 18 '19
Yep! I guess you can do a lot of tinkering/experimentation with the ratio/balance of those two forces :)
1
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
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
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
3
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
1
1
1
1
1
1
1
1
1
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
1
1
1
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
-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
0
u/Peanutbutter_Warrior Nov 18 '19
They don't though. It looks like they start out either moving in random directions or still. Therefore there is no net angular momentum so it should end up as a still ball
0
-1
-10
0
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
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
1
0
Nov 17 '19
[deleted]
1
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.
72
u/[deleted] Nov 17 '19
Source code: https://gitlab.com/chrismit3s/just-dust
Inspired by this post by u/mittwochmensch :)