POV-Ray : Newsgroups : povray.off-topic : Liquid Physics : Re: Liquid Physics Server Time
1 Oct 2024 09:21:28 EDT (-0400)
  Re: Liquid Physics  
From: scott
Date: 31 Mar 2008 02:13:05
Message: <47f08f01$1@news.povray.org>
> Once I'm done computing interactions, I loop through all the particles, 
> add the force to the velocity, and reset the force to zero.

Firstly, I wouldn't use integration like this, it's called Euler integration 
and is basically rubbish and well known for being unstable.  A far better 
method, and the de-facto standard in real-time applications, is Runge-Kutta 
4, or RK4 for short.  It is far more stable and much more accurate per CPU 
time than the Euler method.

There are numerous tutorials and explanations online of how to use RK4 in 
exactly the sort of simulation you are doing, but one of my favourites is 
here, it's very clear.  It's got small code snippets in C++ but they are 
very simple and should be easy enough to convert to any language.

http://www.gaffer.org/game-physics/integration-basics

> The way I see it (and granted, I've never written a liquid sim before), 
> there are two forces I need to deal with, pertaining to two stages that 
> occur when liquid particles collide:
>
> 1) When the particles are farther apart, they attract each other.  This 
> weak attraction would be the driving force behind surface tension.
>
> 2) When the particles are closer together, they push against each other. 
> This force must be much stronger than the first in order to maintain some 
> volume.
>
> Now, so far I'm only actually implementing the second force above.

When I did this sort of thing before, I did both forces as one using a 
single function.  Something like:

Force = A * (1/d^4) - B * (1/d^2)

Where d is the distance between two particles, and A,B are constants.

> However, what I see is that the particles tend to settle pretty quickly to 
> the floor, and then they clump together.  Apparently, the particles on the 
> outside "force" the particles in the middle of the clump closer and 
> closer, until they suddenly explode and things go flying everywhere.

Your physics time-step is too big.  WHat is happening is that from one frame 
to the next two particles are moving from far apart to *very* close 
together.  IRL (and with a smaller time-step or better integrator) this 
wouldn't happen, because there would be inbetween steps that prevent the 
particles getting this close together.

There are several ways to fix this (I would do all of them if I were you):

1) Use RK4 instead of Euler (this will let you get away with much bigger 
time-steps without explosion)
2) Use smaller time-steps, eg update the physics 10 times for every graphics 
update
3) Limit the maximum force artificially in the code to avoid explosions

So long as your time step and integration method are accurate enough to 
avoid explosions, it doesn't matter what the formula for Force is, so long 
as you apply it equally and opposite to each pair of particles momentum will 
be conserved.

I would also add some damping force, proportional to speed or speed squared, 
this will also help calm things down.


Post a reply to this message

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