If we want to simulate small bodies of water with particles, it is quite essential that the particles feel each other. Otherwise, they all aggregate at a single spot in a corner or along an edge. In general, an interaction force for particles has the form
f (xi, xj) = F (|xi− xj|) ·
xi− xj
|xi− xj|, (9.3) where F () is the magnitude of the force depending only on the distance between the particles. The force acts along the connection between the particles, otherwise it would introduce a torque. A popular choice is the Lennard-Jones
9.3. SPH 65 force, often used in molecular dynamics simulations
f (xi, xj) = k1 |xi− xj|m − k2 |xi− xj|n · xi− xj |xi− xj|, (9.4) with k1, k2, m and n control parameters. Popular choices are k1 = k2 = k, m = 4 and n = 2.
With N particles, there are O(N2) possible interactions which we have to evaluate. This quadratic complexity can be the show stopper. Imagine we were able to simulate ten thousand particles without interactions in real time. This means we would now have to evaluate hundred million interactions also in real-time. To avoid quadratic complexity one introduces a cutoff distance d beyond which the particles do not feel each other anymore. In order to keep the simulation stable it is important that the function F () is not just cut off to zero at distance d but that it is C0and C1 continuous at d as well. Now at each time step, the particles are filled into a regular grid with cell spacing d. After this, potential interaction partners for a given particle only need to be searched for in the same or in adjacent cells. If the particles are evenly distributed and the number of particles per cell is bound by a constant, the interactions can be evaluated in linear time.
9.3
SPH
The next question we ask is, can we do better than using Lennard-Jones interaction forces? Is it possible to use the Navier-Stokes equations and solve them on the particles? The answer is yes [MCG03]. The first problem we have to solve is to generate smooth, continuous fields from quantities that are only given on the particles, i.e. at discrete locations in space. The Smoothed Particle Hydrodynamics method, originally devised for the simulation of stars [Mon92], solves this problem. At its core, it defines a way to smooth discretely sampled attribute fields. This is done via so-called smoothing kernels W (r). The kernel defines a scalar weighting function in the vicinity of the position xiof particle i via W (|x−xi|). Defined this way, the kernel is symmetric around the particle because it only depends
on the distance to the particle. The kernel function also needs to be normalized meaning thatR W (|x − xi|)dx = 1.
A popular choice is the poly6 kernel
Wpoly6(r) = 315 64πd9 ( (d2− r2)3 0 ≤ r ≤ d 0 otherwise, (9.5)
because r only appears squared and no square root has to be evaluated. We now have all the ingredients to compute a smooth density field from the individual positions and masses of the particles:
ρ(x) =X
j
mjW (|x − xj|). (9.6)
The density of particle i is then simply ρi = ρ(xi). Now it becomes apparent why the kernels need to be normalized.
With this restriction, the total mass of the system computed as the integral of the density field yields Z ρ(x)dx =X j mj Z W (|x − xj|)dx =X j mj (9.7)
Having the density values of the individual particles, we can now compute smoothed fields Asof arbitrary attributes
Aiof the particles as As(x) = X j mj Aj ρj W (|x − xj|). (9.8)
66 CHAPTER 9. SMOOTHED PARTICLE HYDRODYNAMICS A nice property of this formulation is that the gradient of such a field can easily be computed by replacing the kernel by the gradient of the kernel
∇As(x) = X j mj Aj ρj ∇W (|x − xj|). (9.9)
In the Eulerian (grid based) formulation, fluids are described by a velocity field v, a density field ρ and a pressure field p. The evolution of these quantities over time is given by two equations. The first equation assures conservation of mass
∂ρ
∂t + ∇ · (ρv) = 0, (9.10)
while the Navier-Stokes equation formulates conservation of momentum ρ ∂v
∂t + v · ∇v
= −∇p + ρg + µ∇2v, (9.11) where g is an external body force and µ the viscosity of the fluid. The use of particles instead of a stationary grid simplifies these two equations substantially. First, because the number of particles is constant and each particle has a constant mass, mass conservation is guaranteed and the first equation can be omitted completely. Second, the expression ∂v/∂t + v · ∇v on the left hand side of the Navier-Stokes equation can be replaced by the substantial derivative Dv/Dt. Since the particles move with the fluid, the substantial derivative of the velocity field is simply the time derivative of the velocity of the particles meaning that the convective term v · ∇v is not needed for particle systems.
There are three body forces (unit [N/m3]) left on the right hand side of the Navier-Stokes equation modeling pressure (−∇p), external forces (ρg) and viscosity (µ∇2v). The sum of these body forces f = −∇p+ρg+µ∇2v determines the change of momentum ρ∂v∂t of the particles on the left hand side. For the acceleration of particle i we, thus, get:
ai = ∂vi ∂t = fi ρi , (9.12)
where vi is the velocity of particle i and fi and ρi are the body force and the density field evaluated at the location
of particle i, repectively. We will now see how the body forces can be evaluated using SPH.
9.3.1 Pressure
Application of the SPH rule described in Eqn. 9.9 to the pressure term −∇p yields fipressure= −∇p(xi) = − X j mj pj ρj ∇W (|xi− xj|). (9.13)
Unfortunately, this force is not symmetric as can be seen when only two particles interact. Since the gradient of the kernel is zero at its center, particle i only uses the pressure of particle j to compute its pressure force and vice versa. Because the pressures at the locations of the two particles are not equal in general, the pressure forces will not be symmetric. Different ways of symmetrization of Eqn. 9.13 have been proposed in the literature. Here is a very simple solution which is stable and fast to compute:
fipressure= −X j mj pi+ pj 2ρj ∇W (|xi− xj|). (9.14)
9.4. RENDERING 67 Since particles only carry the three quantities mass, position and velocity, the pressure at particle locations has to be evaluated first. This is done in two steps. Eqn.9.6 yields the density at the location of the particle. Then, the pressure can be computed via the ideal gas state equation
p = k(ρ − ρ0), (9.15)
where k is a gas constant that depends on the temperature and ρ0is the environmental pressure. Since pressure forces
depend on the gradient of the pressure field, the offset mathematically has no effect on pressure forces. However, the offset does influence the gradient of a field smoothed by SPH and makes the simulation numerically more stable. However, incompressibility is not enforced strictly as in the Eulerian case. Pressure forces are only generated after the fact, when density variations have already formed. This is clearly a penalty method and yields a bouncy behavior of the fluid. To avoid this effect, one can predict the densities and compute pressure forces on predicted densities or estimate the divergence of the velocity field using SPH and then solving the Poisson equation on the particles [PTB+03].
9.3.2 Viscosity
Application of the SPH rule to the viscosity term µ∇2v again yields asymmetric forces fiviscosity= µ∇2v(xi) = µ X j mj vj ρj ∇2W (|xi− xj|). (9.16)
because the velocity field varies from particle to particle. Since viscosity forces are only dependent on velocity differences and not on absolute velocities, there is a natural way to symmetrize the viscosity forces by using velocity differences: fiviscosity= µX j mj vj− vi ρj ∇2W (|x i− xj|). (9.17)
A possible interpretation of Eqn. 9.17 is to look at the neighbors of particle i from i’s own moving frame of reference. Then particle i is accelerated in the direction of the relative speed of its environment.
9.3.3 External Forces
External forces such as gravity, collision forces and forces caused by user interaction are applied directly to the particles as in the case of simple particle systems without the use of SPH.
9.4
Rendering
There are many ways to render fluids which are defined via particles. One of the simplest and fastest methods is to use sprites. If a smoothing filter is applied to the depth buffer before the sprites are drawn, the granularity of the particles can be hidden to a certain degree. Another way of rendering particle fluids is to draw an iso surface of the density field and triangulate it using the marching cubes algorithm.
Appendix A
Background
A.1
Vector Calculus
The following three differential operators are fundamental to vector calculus: the gradient ∇, the divergence ∇·, and the curl ∇×. They occasionally are written in equations as grad, div, and curl instead.