Jump to content
  • Advertisement
Sign in to follow this  
StillmatikX

Implementing Runge-Kutta(RK4) into my n-body simulation

This topic is 3968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I know there are a lot of information and discussions out there regarding using the RK4 method but I am having the hardest time truly understanding them to the point where I can actually implement them in my simulation. Anyway, I am programming an n-body simulation of a solar system (upto 100 planets orbiting around a central star with upto 5 moons orbiting around each planet). These numbers are pretty arbitrary but I need some place to start. I have coded the simulation up using Euler Integration and have taken care of the graphical interface but, lets just say, the paths the planets are traveling as they orbit their sun are less then spectacularly precise. Thus, I definitely need to implement a more precise approximation method and whats more precise than the Runge-Kutta (RK4) method, right! Anyway, I understand that the Runge-Kutta method is basically just calculating the Euler integration of a variable multiple times over a given timestep but I just can't seem to understand how to handle the acceleration part of it. My equations are as follows: A(acceleration_ = F(total force) / m(mass of body); F = Summationof(G(gravitational constant)*m1(mass of some body)*m2(mass of another body) / d(distance between two bodies)^2;.........the summing is done for all bodies over all bodies V(velocity) = v_previous + A*t(timestep); X(position) = x_previous + v*t + (0.5*A*t^2); *Note A,F,d,v,and x are vectors..... Can SOMEONE!!!ANYONE!!! please help me understand this..I've been googling and reading discussions from forums for so long I'm daydreaming in text....code would be greatly appreciated!!!

Share this post


Link to post
Share on other sites
Advertisement
I don't have time to write much, but one thing you have to understand is that you need to integrate the system as a whole when using RK4. This means you can't implement the integration as a function that operates on individual bodies one at a time, even though you can get away with that with Euler. The reason for this is that the forces at the "intermediate times" depend upon the "intermediate positions" of not only the body you're trying to integrate, but also of all the other bodies in the system.

I think the Baraff SIGGRAPH notes explain things reasonably well, IIRC.




Share this post


Link to post
Share on other sites
Quote:
Original post by STEVIE G
Use Verlet intergtation- much more accurate than Euler and faster than RK4 with comparable accuracy.


Thanks for all the responses guys. I think STEVIE G is right though. I probably should give up on RK4 and just use Verlet Integration. Verlet Integration was my first choice but I wanted to do it big so I decided to give RK4 a try. The problem is that I didn't know that RK4 would require me to have to calculate the force within timesteps and thus have to iterate over all the bodies in the system multiple times within a give timestep(not sure I have the CPU cycles for all that or that I even want to sacrifice speed for a bit more accuracy). Anyway, can anyone comment on the O(delta-t^4) in the equation. I understand that this is big-O notation but I'm not quite sure what I am supposed to do with it with respect to the rest of the equation......Thanks.

Share this post


Link to post
Share on other sites
You shouldn't ever calculate force in a n-body problem. You just need accelerations, and these could be computed without bothering with computation of "force".

You should use sixth order integrator (because it's painless to implement), to avoid problems. You would be probably unable to create some "stable" simulations even with sixth order integrator without an energy dampening.

Have you calculated accuracy requirements? 64 bit floating point number has finite accuracy, and square root would kill it quite a bit. Also summing too low and too high number would result in original high number, the small one is simply thrown out.

You might look at bob jenkins page, he has few quite easy to write integrators, and he provided requirements for accuracy. (steps per orbit, divergence of the integrators...)

BTW this integrator is symplectic.

velocity = prevVelocity + acceleration*delta
Position = prevPosition + velocity*delta

(spirals out)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!