Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - The three body problem

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 27th Feb 2012 21:17
The two-body problem consists of two objects under gravitational attraction. The attraction force is the newtonian force G*M1*M2/distance^2, where G is some constant, M is mass and distance is the distance between the two objects.

In the two-body problem, the path of each particle can be solved for explicitly. We can get circles or ellipses, like the earth's path around the sun, or we can get escape trajectories where the two particles pass by and never collide with each other again, taking the form of hyperbolas and parabolas.

The three-body problem is more difficult though. Three-bodies can cause chaos, and you can't solve it explicitly for hyperbolas/parabolas. The only way to solve the general case is to simulate the system over time.

It's hard to get to grips with the chaos of the three body problem, but this program tries to depict it.

It starts out with three bodies with the same mass/radius and sends them off in different velocities. From this, you can calculate the future path of the particles. This program shows the path of three objects over the full simulation, so instead of viewing three circles (the particles current positions), you see three lines (the particles paths given the starting conditions). As the initial velocity varies over time, the paths vary, and you get chaotic looking results.

When two particles collide, they're combined into the same particle, so this explains when paths combine into one line. It also explains why the problem is chaotic - if two particles are going to collide, a small nudge one way or the other could lead to a whole alternate timeline where they don't collide.



Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 28th Feb 2012 13:23 Edited at: 28th Feb 2012 15:26
Nice!

Inspired by your code, I tried to do something similar, and I think it worked out well - although it seems to behave somewhat different to yours, so it's quite possible that I got the physics wrong or something.
Anyway, might be worth a look.



And now that I think about it - won't this chaotic behaviour make a good fractal (like mandelbrot or julia) when visualized differently?
Consider the case that the initial direction of each particle is fixed but the initial speed is varied (as currently done in my code), then the input parameters would span a 3-dimensional space. Now execute the iteration for some time for each point in this space (e.g. the cube from -1 to 1 in each dimension) and color the point depending on the output (e.g. the minimal difference between any of the three particles and (0,0)).
It's certainly possible that the result is messy and seemingly random, but who knows, maybe it looks cool. Probably somebody has already done this before, but it might be worth a try nontheless.


I also uploaded a Video of the effect.

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 28th Feb 2012 17:00
so the blur effect in your program draws batches of N frames at a time? Awesome, and I really like the fading out trail

I think the reason your simulation is more shakey is twofold: Timestep size and integration type. For the time step, in my program the timestep was .1 seconds and each dot was 4 seconds apart, so there were 80 frames inbetween each dot, and there were 4800 dots total, so the resolution was a lot higher.

The second thing is that your integration technique of:
acceleration+=force/mass
velocity+=acceleration
position+=velocity

assumes zero acceleration between frames. In my program I assume linear acceleration. So linear acceleration leads to:
acceleration+=force/mass
velocity+=acceleration*dtime

but then as dtime ranges from 0 to timestep, you have a triangle, so the final position integration ends up becoming:
position+=velocity*dtime PLUS .5*acceleration*dtime^2

so you just have:
acceleration+=force/mass
velocity+=acceleration*timestep
position+=velocity*timestep+.5*acceleration*timestep^2

which gives slightly more accurate results. There are also other types of better integration but I don't know how they work or why they're more accurate (Runge-Katta integration for one)

So: Smaller timestep, assume constant acceleration over the timestep instead of constant velocity. There's also the issue of colliding particles - there are some cases where the particles WILL reach infinite velocity and you'll get division by zero, so the best way to cope with that is to make a collision. What my code did was account for conservation of momentum:
Velocity1*Mass1+Velocity2*Mass2=NewVelocity*(Mass1+Mass2)

I was thinking about doing a fractal but I couldn't think of how. That would be pretty awesome: each pixel represents an inital velocity vector, and is colored based on the distance of some arbitrary particle to another.


Also: I did some plots of velocity instead of position, and it's kinda cool. The good part about velocity plots is that it's easier to see chaos. When two paths spiral around each other, that's actually one of the least chaotic possible states, but in the velocity graph that's easy to see because it just forms a circle/oval.

Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 28th Feb 2012 17:07
Quote: "There's also the issue of colliding particles - there are some cases where the particles WILL reach infinite velocity and you'll get division by zero, so the best way to cope with that is to make a collision."


I solved that by defining a minimal distance between to particles. In case they are nearer to each other than this constant value (2.5 pixels in the code above), the value is chosen instead of the actual distance. This is obviously physical BS, but makes it a bit more controllable.
I have to admit I don't really like the idea of collisions, since I usually consider "particles" as entities with size 0 (position and velocity only, no spatial elongation), that's why I avoided collisions the way you implemented them.


Quote: "Also: I did some plots of velocity instead of position, and it's kinda cool. The good part about velocity plots is that it's easier to see chaos. When two paths spiral around each other, that's actually one of the least chaotic possible states, but in the velocity graph that's easy to see because it just forms a circle/oval."


Another cool idea, I did not think that far, but it surely must be interesting.

I'll see if I can work out a 2D fractal visualization - maybe it works well, maybe it doesn't, we'll see.

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 28th Feb 2012 17:42 Edited at: 28th Feb 2012 17:45
I don't like the idea of collisions either, but I like division by zero even less xD

I'm rendering the system where pixel position is based on velocity and color is based on the number of bodies that are left:


It's taking a long time to render because I have the accuracy so high (probably unnecessarily high)

The red regions are where only two particles are left, the pink regions are where only one particle is left, and the blue regions are the interesting ones where no particles have collided. So, probably around each red region is an unavoidable division-by-zero event.

Attachments

Login to view attachments
Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 28th Feb 2012 17:51 Edited at: 28th Feb 2012 19:16
Here's a snapshot of my fractal visualization. The x/y-coordinates of each pixel determine the velocity of two of the three particles. The third could be animated over time, but is set to (0,0) for this screenshot.
The color depends on the amount of iterations used to reach divergence, which again is set to be true if all three particles leave the screen area (which is a rather arbitrary condition, but anyway).



Certainly doesn't look as cool as Mandelbrot, but, who knows, maybe it gets better when changing other variables than velocity or something like that.

- - - - -

One of the problems seems to be that it, as opposed to mandelbrot, (probably) always diverges. However, we still can visualize the amount of iterations. After zooming out a bit I got quite an interesting structure.



It seems to be symmetrical, which makes sense given the way I set up the particles depending on screen coordinates.

All in all pretty fascinating stuff if you ask me.



Edit: Some more visualizations









noobnerd
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 28th Feb 2012 20:38
WOW awesome stuff here!
nice idea neuro

really cool fractals MR K !
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 29th Feb 2012 07:09
Oh man, those are awesome!

I'm working on a javascript version but I'm still learning the language so it might take a while.

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Mar 2012 10:42

Login to post a reply

Server time is: 2024-03-29 07:21:14
Your offset time is: 2024-03-29 07:21:14