POV-Ray : Newsgroups : povray.off-topic : Liquid Physics : Re: Liquid Physics Server Time
4 Nov 2024 13:25:06 EST (-0500)
  Re: Liquid Physics  
From: scott
Date: 31 Mar 2008 09:22:09
Message: <47f0f391$1@news.povray.org>
>> http://www.gaffer.org/game-physics/integration-basics
>
> Mmm, that's a nice article. I'm still having trouble following it though. 
> Perhaps a Haskell example?
>
>   integrate t dt a (v,p) = (v + dt*(a t v p), p + dt*v)
>
> OK, well that seems trivial enough. So how about RK4?
>
>   evaluate (v,p) t dt (dv,dp) =
>     let
>       v' = v + dt*dv
>       p' = p + dt*dp
>     in  (v', a (t+dt) v' p')
>
>   integrate t dt a (v,p) =
>     let
>       a = evaluate (v,p) t dt -- er... where's the rest?

The only difference is that in the evaluate function without the supplied 
derivative, you don't need to update the state, simply return the derivative 
at time t using the initial state.

>       b = evaluate (v,p) t (dt/2) a
>       c = evaluate (v,p) t (dt/2) b
>       d = evaluate (v,p) t dt     c
>       dv = (fst a + 2 * (fst b + fst c) + fst d) / 6
>       dp = (snd a + 2 * (snd b + snd c) + snd d) / 6
>     in (v + dt*dv, p + dt*dp)
>
> Er... is that actually correct? And what's with the formula for A? I'm not 
> really understanding how this is supposed to work...

What it's doing is this:

1) Work out the derivative at time t.  This is simply a matter of 
calculating the new acceleration at time t (as the derivative of velocity) 
based on the current state, and returning the current velocity (as the 
derivative of position).

2) Using the derivative you just calculated, a, calculate a new state at 
time t+dt/2 using normal Euler integration.  Return the derivative of the 
new state.

3) Same as step 2, but use b instead of a, and store the result as 
derivative c.

4) Same as 2, but this time use the derivative c to do the whole step dt 
from the first point.

5) Take a weighted average of all the derivatives you just caluclated, a-d, 
this gives the most accurate derivative.

6) Use this weighted average to actually advance your state by dt using 
normal Euler integration.

If you want to actually understand the maths behind it, well that's another 
story (and another Google search, or two :-).

> I intended (but never got round to) implementing a system where the 
> computer integrates a time step, then integrates it again with twice the 
> resolution, and if the results differ too much, it subdivides further...

A good idea, it's called "adaptive time step".


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.