We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Runge Kutta (RK4) for dummies
Page Index Toggle Pages: 1
Runge Kutta (RK4) for dummies (Read 5043 times)
Runge Kutta (RK4) for dummies
Jun 29th, 2007, 10:58am
 
I looked at the thread where mflux asked how traer.physics works and this url is dead:

http://www.pixar.com/companyinfo/research/pbm2001/notesc.pdf

And my math tutor at school labelled us idiots and refused to teach us properly so I can't understand the example on the wikipedia page for RK4:

http://en.wikipedia.org/wiki/Runge_kutta#The_classical_fourth-order_Runge.E2.80.93Kutta_method

I couldn't even understand Verlet Integration once, but when I saw the snippet of code that makes it work, it was perectly clear to me.

My problem is that I can only understand equations when they are written in program code.

Does anyone have any leads on some RK4 code? I'll be using it to port to Flash and also do other things, any help would be appreciated.
Re: Runge Kutta (RK4) for dummies
Reply #1 - Jun 29th, 2007, 1:27pm
 
You're in luck. Archive.org indexed the file.
http://web.archive.org/web/20060502060419/http://www.pixar.com/companyinfo/research/pbm2001/notesc.pdf
Re: Runge Kutta (RK4) for dummies
Reply #2 - Jun 30th, 2007, 11:27am
 
Ah thanks.

Not really in luck though. They don't mention the RK4 method, and I've no idea what this 6 point phase space they're going on about is.

And still I can't read their equations.

I complained to MathWorld about this and they said, "go look up a greek alphabet". Which is precisely useless advice, because it didn't help.
Re: Runge Kutta (RK4) for dummies
Reply #3 - Jun 30th, 2007, 1:43pm
 
That's a shame. I hope somebody else can help you with the Runge Kutta.

I'm experiencing the same problems. I'm just not able to make the translation from equation into code. It's frustrating as hell. Because I see the same cool algorithms in different places without a pseudo code example for dummies.

I guess I should really make an effort to learn the Greek alphabet.

"Math is hard. Let's go shopping!"

Re: Runge Kutta (RK4) for dummies
Reply #4 - Jul 10th, 2007, 11:10pm
 
st33d -

If you "speak" C, then Numerical Recipes has an RK4 implementation along with an explanation - http://www.nrbook.com/a/bookcpdf.php is where the online version lives these days, though for some reason I can't see anything when I download the relevant pdf (it's in section 16, btw).  Unfortunately NR algorithms are not GPL friendly, and they are also not Java.

At http://os-physics.cvs.sourceforge.net/os-physics/src/src/org/opensourcephysics/numerics/RK4.java?hideattic=0&view=markup there is an RK4 implementation, but you're going to have to jump through some hoops to figure out what's going on there...I'm not sure if it's really the best source.  There's also another C implementation at http://farside.ph.utexas.edu/teaching/329/lectures/node36.html that uses some external library for data holders; if you can make out the C (++ in that case) stuff and make the translation to Java, it's a decent version that might make some sense, though the translation may be a little weird considering it uses void* pointers to pass functions around.  The math stuff is straightforward, though I don't imagine the code is probably GPL, either.  But this is kind of a boilerplate algorithm, so going to Java alone would probably be such a change that it wouldn't matter (can't copyright an algorithm, right?).

I've put together RK4 integrators before in C, and the numerical stuff translates pretty easily, so at some point if I can dig around on my old computer and translate one, I'll put the source online.

I should also note, don't feel bad if you don't understand why any of the Runge Kutta schemes work, the constants are all "magic numbers" that are carefully chosen to minimize the integration error, and their source is somewhat obscure.  If you can intuitively understand why the midpoint method is better than Euler, you've gotten about as much understanding as you should probably hope for when it comes to RK4; the rest is just a bunch of nasty equations and a dirty optimization of parameters.

I'm curious, what are you doing that Verlet integration won't suffice for?  I've tended to use Verlet instead of RK4 lately simply because constraints and collision handling are usually much easier to implement stably.  Also, (this will be more an issue in Flash) RK4 can involve quite a few method calls, which you may need to inline if you're having performance problems; it's a very robust integrator, but part of its utility comes from the ability to have very large timesteps, and if you are using small timesteps you may be burning a lot of processor power that you don't need to be (sometimes calling the integrator only once every few frames is the best way to handle things, and you could never get away with this using Euler).  But it all depends what you need to use it for, I suppose.
Re: Runge Kutta (RK4) for dummies
Reply #5 - Jul 11th, 2007, 11:04am
 
Thanks, I'll look through those at my leisure, although I haven't grasped pointers yet so I'm a little worried.

My concern was that we're working on one of those classic thrust games with attractors in and I was unsure how to put it all together with Verlets. RK4 I'd like to learn mainly for the benefit of my career.

However, I've actually got quite a stable physics engine together in Flash now with Particles, Springs, Attractors, Vector classes. I've also put together a pixel based collision method that uses the Bresenham algorithm (to resolve the direction to repel a Particle after penetration) - which works but it's fussy. The system I've figured out is fairly low overhead, so I aim to convert it to Java and share it, but I won't make a library of it. Libraries are great but they don't teach a hell of a lot and you can't make them go faster.
Re: Runge Kutta (RK4) for dummies
Reply #6 - Jul 13th, 2007, 12:22am
 
If you've been programming in Java, you actually probably have a better intuitive understanding of pointers than you realize, though you don't know the C syntax.  But that's neither here nor there, as I've been procrastinating on some work that I have to do, and I ended up translating the C++ implementation from UTexas into Java so I could keep from getting back to it for a little longer...

First, I noticed you asked about 6D phase space.  All this means is that you consider your array of "positions" to also include velocities, i.e. for each particle, you consider its "location" to be (x,y,z,vx,vy,vz).  That means that "velocities" end up being (vx,vy,vz,ax,ay,az).  These integrators work with first order differential equations in general, and it so happens that physical equations are often better solved as first order ODEs in 6 dimensions than second order ODEs in 3 (first order would be dx/dt = v, second order would be d^2x/dt^2 = a - physics usually defines equations using the second order ones, but you can always "unroll" second order ODEs into twice as many first order ones; it so happens that second order solvers are considerably nastier than first order ones, so we tend to do this unrolling when simulating).  Below I'll show you what this actually means to you in code, in case that doesn't make sense.

Also, I'm sorry that the MathWorld folks were unhelpful; mathematicians in general can be real jerks (the word I'm really thinking of is a lot more colorful, but I'll keep it PG-13 here...), especially to programmers.  But I do suggest that learning the Greek alphabet probably will be worth your time, especially if you're doing physics stuff; there's only so many letters in the regular alphabet, so we tend to run out pretty quickly!  And it's hard to follow equations when you can't subvocalize what you're reading, so it helps to know the difference between a zeta and a xi.  Eventually when you see them in equations you'll stop panicking and realize that the equations are not really any more difficult than the normal looking ones you're used to seeing.

Anyhow, here is the code, translated from C++ into the form I would probably use if I were using RK4 in Java (I also changed x->t and y->x to conform to my expectation that t=time and x=location).  This won't run as is, because there are some things you need to fill in (mainly you need to provide something to set the accelerations of the particles), and possibly because I've made a mistake somewhere (I didn't actually try to get this to run yet).  But this should at least give you an idea what's going on in the algorithm.

Code:

float[] x = coordinates of all particles in PHASE SPACE, i.e. including velocities:
x[0] = particle[0].position.x, x[1] = particle[0].position.y,
x[2] = particle[0].velocity.vx, x[3] = particle[0].velocity.vy,
x[4] = particle[1].position.x, etc.

float t = current time
float h = time step

//evaluate derivatives wrt t of each variable in x array, load into dxdt (assumed pre-initialized to correct length)
void rhs_eval(float t, float[] x, float[] dxdt){
if (dxdt.length != x.length) { return; } //add some error-handling code here, because array lengths must be equal
int POSX = 0; //defined merely for clarity below
int POSY = 1;
int VELX = 2;
int VELY = 3;
for (int i=0; i<x.length; i += 4){
dxdt[i+POSX] = x[i+VELX]; //derivative of position is velocity
dxdt[i+POSY] = x[i+VELY];
dxdt[i+VELX] = [x component of the acceleration for particle i/4 based on time and configuration passed (t and x)];
dxdt[i+VELY] = [same for y component];
}
}

//depends on rhs_eval function - Java doesn't have a simple mechanism for passing functions, so we treat it as a global function
void rk4_fixed (float t, float[] x, float h){
int n = x.length;

// Declare local arrays
float[] k1, k2, k3, k4, f, dxdt;

k1 = new float[n];
k2 = new float[n];
k3 = new float[n];
k4 = new float[n];
f = new float[n];
dxdt = new float[n];

// Zeroth intermediate step
rhs_eval(t, x,dxdt);
for (int j = 0; j < n; j++){
k1[j] = h * dxdt[j];
f[j] = x[j] + k1[j] / 2.;
}

// First intermediate step
rhs_eval(t + h / 2., f,dxdt);
for (int j = 0; j < n; j++){
k2[j] = h * dxdt[j];
f[j] = x[j] + k2[j] / 2.;
}

// Second intermediate step
rhs_eval(t + h / 2., f,dxdt);
for (int j = 0; j < n; j++){
k3[j] = h * dxdt[j];
f[j] = x[j] + k3[j];
}

// Third intermediate step
rhs_eval(x + h, f, dxdt);
for (int j = 0; j < n; j++){
k4[j] = h * dxdt[j];
}

// Actual step
for (int j = 0; j < n; j++){
x[j] += k1[j] / 6. + k2[j] / 3. + k3[j] / 3. + k4[j] / 6.;
}
x += h;

return;
}


It's not very OO, but it is what it is.  Any questions, feel free to ask.

Also, I've spent more time messing around with Verlet integration than I probably should have, so if you have any questions there, I might be able to help, conceptually at least, if not with actual code; I've implemented many different interactions in that framework, and if you're having trouble with something I very well may have fought through the same thing before.

Good luck!
Re: Runge Kutta (RK4) for dummies
Reply #7 - Apr 21st, 2009, 12:59am
 
hi all
has anybody tried awjordan code?
is there any issue with runge kutta?
i'm trying to do a bunch of nice streamlines..
//currently i'm doing a few but quite tricky way..

thanks!
Page Index Toggle Pages: 1