6.5 Water Implementation
6.5.1 Spring-Mass System Implementation
The spring-mass particle model is most often used in cloth simulation [108] [32] [7], but mod- ifying the method slightly can give a number of different effects. In particular, spring-mass equations can be used to model a simple fluid simulation by adding certain restrictions on the forces being applied.
As particles move towards each other and collide, a repulsion force is applied using soft- body collision dynamics. If the distance between two particles is smaller than the sum of the two particles’ radii, they must be colliding at this particular timestep. An elastic spring force can be applied by assuming the length of the spring at rest is equal to the sum of the two parti- cles’ radii. Thus, as a collision occurs between particles, the spring is essentially compressed the distance of the the rest distance minus the current distance between the two particles at collision. It should be clarified that “compression” technically does not happen - rather, the discrete timestep of the integration method has caused both particles to partially occupy each other’s space. The force which subsequently separates these two particles, however, is calcu- lated based on the standard spring-force calculations given by Hooke’s Law [113] and includes shear, drag and damping forces, as described in Equation 4.6.
The spring-mass method for simulating fluids is not as accurate as other methods because there are no fluid dynamics equations used. This means that physical behaviour such as fluid
SECTION 6.5: WATER IMPLEMENTATION 111
viscous forces are not considered. Nevertheless, the method can still provide a surprisingly good effect especially when the number of particles in the simulation is relatively large. The effect produced by this method would be similar to the behaviour of plastic balls in a child’s ball pen, or a bucket of marbles.
Much of the code involved is calculating the spring-mass forces is similar to that in Listing 6.4. Pseudo-code is highly preferable in this case, where the underlying basics of the code remain the same for all of the simulations, and better descriptive context is required for the subtle, but important, force calculations. The pseudo-code for the application of the spatial grid to spring-mass model has already been described in Algorithm 3 in Chapter 4, although it does not elaborate on the force calculation, instead focusing on a more general overview. Algorithm 11 shows a more precise description of the method.
There are a couple of things to note about this algorithm. Firstly, it is assumed that the radius and mass of both particles, which are colliding in any one instance, are identical. This is an acceptable assumption for the water simulation. Secondly, it is assumed both particles have the same material properties and therefore use the same force constants. This is fine for simulating a single fluid, but simulating the interactions between multiple particle types (for example mixing two fluids) must take into account all sets of different material properties.
As shown in Algorithm 11, the spatial grid is used to simulate the spring-mass system in real-time (similar to the plasma simulation) by dividing the simulation space up into manage- able chunks. Unlike the plasma system however, the size of the spatial grid cell does not need to accommodate multiple particles. The optimum spatial grid cell size should be twice the actual particle radius. This means that at any one time, there should technically only be one particle allowed within any one cell. In practice, this does not hold one hundred percent of the time as again, due to the discrete timestep of the integration method, particles may be able to move far enough into the next cell. In any case, the number of potential force calculations is greatly reduced and times where more than around 26 calculations per timestep (one per neighbouring spatial grid cell) are few and far between.
When calculating the spring and shear forces, notice that force modifications only occur
when the distancedbetween the two particles is less than the rest distance2R. This means that
once the particles have collided and rebounded, they are free to move away without any spring-
Parameter Value Particle Number 512,000 Timestep 0.005 Spring Constant 0.4 Shear Constant 0.2 Damping 0.95 Gravity 9.8 Particle Mass 1.0
Algorithm 11In-depth spring-mass particle model force calculation algorithm
Given array P ofnparticle positions
Given array V ofnparticle velocities
Given arrays F and L of sorted particle indices (First and Last) Given simulation constants K - spring, shear and damping Given particle radius R and mass M
Acceleration A (force) = 0
//Spatial grid arrays have already been sorted
//Current cell position is a function of particle position P
foreach particle in PARALLEL, with thread indexido
Cell C = func(P[i])
//Find neighbours. Neighbouring cells are -1 to +1 in all directions for all-1≤x≤+1, -1≤y≤+1, -1≤z≤+1do
Neighbour N = C + Vec(x, y, z)
//Use neighbouring cell indices to obtain list of neighbouring particles in F and L
cell index= cellfunc(N)
f= F[cell index]
l= L[cell index]
//Loop through all neighbouring particles forp=f;p≤l;p++do
Relative positionPrel= P[i] - P[p] Letdbe the length of the vectorPrel
//Collision occurs when d is less than thecollision-orrest distance
//If two particles are equal size, collide whenat mosttwice the radius R
ifd<2Rthen
Relative velocityVrel= V[p] - V[i]
//Rebound direction (normal) is the inverse of the normalizedPrel
norm = -Prel/d
Tangental velocityVtan=Vrel- dot(V rel, norm) * norm
//Acceleration - spring along norm, shear alongVtan, damping alongVrel
A[i] += -Kspring * (2R -d) * norm A[i] +=Kshear*Vtan
A[i] +=Kdamp*Vrel else
No collision, no force is applied end if
end for end for //F = ma A[i] *= M
Add external force E where applicable end for
SECTION 6.5: WATER IMPLEMENTATION 113
extensive force applied. After the spring-mass force calculation is complete, external forces are applied to produce the final force calculation and then the integration step is performed. Table 6.1 shows the simulation parameters used for a spring-mass model to produce a standard water simulation.