Video Games Fluid Flow Simulations Towards...

64
UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow Simulations Towards Automation Smoothed Particle Hydrodynamics Ann Johansson

Transcript of Video Games Fluid Flow Simulations Towards...

Page 1: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

UPTEC-F-14008

Examensarbete 30 hpMars 2014

Video Games Fluid Flow Simulations Towards Automation Smoothed Particle Hydrodynamics

Ann Johansson

Page 2: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Teknisk- naturvetenskaplig fakultet UTH-enheten Besöksadress: Ångströmlaboratoriet Lägerhyddsvägen 1 Hus 4, Plan 0 Postadress: Box 536 751 21 Uppsala Telefon: 018 – 471 30 03 Telefax: 018 – 471 30 00 Hemsida: http://www.teknat.uu.se/student

Abstract

Video Games Fluid Flow Simulations TowardsAutomation

Ann Johansson

A complete understanding of the cooling process when hot rolling steel is essential to understanding how the quality of the steel is connected to the cooling. This is why it is of great interest to simulate this process. However traditional CFD methods are too expensive in terms of CPU time. Knowing that video games successfully simulate fluids in reasonable time, those methods could be useful for simulating the cooling process in steel manufacturing. This would mean a loss in accuracy that could be acceptable. In this thesis different methods used for fluid simulations have been studied. The Smoothed Particle Hydrodynamics (SPH) method has been chosen. The method has been implemented for simulating the cooling process in MATLAB, which is a matrix operation based programming tool. Convincing results have been achieved for a big scale, but problems still remain for an implementation on a small scale.

Sponsor: ABB AB, Corporate Research, Västerås ISSN: 1401-5757, UPTEC F 14008Examinator: Tomas NybergÄmnesgranskare: Per LötstedtHandledare: Rebei Bel Fdhila

Page 3: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Sammanfattning

En full förståelse för kylningsprocessen vid hetvalsning av stål är väsentlig för att förstå hur

kvaliteten på stålet är kopplat till kylningen. Det är därför av stort intresse att simulera den

här processen. Däremot är traditionella CFD metoder alldeles för dyra i termer av CPU tid.

Men vi vet att dataspelsutvecklare har lyckats simulera flöden i rimlig tid. Dessa metoder

skulle kunna vara användbara för att simulera kylningsprocessen vid ståltillverkning. Detta

skulle innebära en förlust i noggrannhet som skulle kunna vara acceptabel. I det här

examensarbetet har olika metoder för att simulera flöden studerats. SPH (Smoothed Particle

Hydrodynamics) metoden har valts ut. Metoden har implementerats för att simulera

kylningsprocessen i MATLAB, som är ett matrisbaserat programmeringsverktyg. Trovärdiga

resultat har uppnåtts, men problem kvarstår för implementering på liten skala.

Page 4: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Uppsala University

Master Thesis

Video Games Fluid Flow SimulationsTowards Automation

Author:

Ann Johansson

Supervisors:

Rebei Bel Fdhila

Anders Daneryd

A thesis submitted in fulfilment of the requirements

for the degree of Master of Science in Engineering Physics

in the

Division of Scientific Computing

Department of Information Technology

March 2014

Page 5: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

“We can do anything we want to do if we stick to it long enough.”

Helen Keller

Page 6: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

UPPSALA UNIVERSITY

Abstract

Faculty of Science and Technology

Department of Information Technology

Master of Science in Engineering Physics

Video Games Fluid Flow Simulations Towards Automation

by Ann Johansson

A complete understanding of the cooling process when hot rolling steel is essential to

understanding how the quality of the steel is connected to the cooling. This is why it

is of great interest to simulate this process. However traditional CFD methods are too

expensive in terms of CPU time. Knowing that video games successfully simulate fluids

in reasonable time, those methods could be useful for simulating the cooling process in

steel manufacturing. This would mean a loss in accuracy that could be acceptable. In

this thesis different methods used for fluid simulations have been studied. The Smoothed

Particle Hydrodynamics (SPH) method has been chosen. The method has been imple-

mented for simulating the cooling process in MATLAB, which is a matrix operation based

programming tool. Convincing results have been achieved for a big scale, but problems

still remain for an implementation on a small scale.

Page 7: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Acknowledgements

Thanks to Rebei Bel Fdhila, Anders Daneryd and Lokman Hosain at ABB and Per

Lotstedt at Uppsala University for the support and help in completing this thesis.

iii

Page 8: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Contents

Abstract ii

Acknowledgements iii

Contents iv

List of Figures vi

Abbreviations viii

Physical Constants ix

Symbols x

1 Introduction 1

2 Problem Setting 3

3 Theory 5

3.1 Navier-Stokes equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

4 Methods 6

4.1 Why SPH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

5 Smoothed Particle Hydrodynamics 9

5.1 Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5.2 Kernel functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

5.3 History of SPH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

5.4 Solving the Navier-Stokes equation using SPH . . . . . . . . . . . . . . . . 14

5.4.1 Density . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5.4.2 Pressure force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5.4.3 Viscosity force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.4.4 External forces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.4.4.1 Gravity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.4.4.2 Surface Tension . . . . . . . . . . . . . . . . . . . . . . . 18

5.4.5 A simple Navier-Stokes solver . . . . . . . . . . . . . . . . . . . . . 20

iv

Page 9: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Contents v

6 Implementation 22

6.1 Fast Nearest Neighbour search . . . . . . . . . . . . . . . . . . . . . . . . 22

6.1.1 Hash tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

6.1.2 Our own neighbour search algorithm . . . . . . . . . . . . . . . . . 23

6.1.3 Neighbour search algorithm . . . . . . . . . . . . . . . . . . . . . . 25

6.2 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

6.2.1 Leap-Frog Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

6.3 Boundary problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

6.4 Parameter values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

6.4.1 Smoothing length . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

6.4.2 Viscosity coefficient . . . . . . . . . . . . . . . . . . . . . . . . . . 29

6.4.3 Mass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

6.4.4 Gas stiffness constant . . . . . . . . . . . . . . . . . . . . . . . . . 30

6.4.5 Density . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

6.5 Implementation issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

6.5.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

6.5.2 Particles ”stick together” . . . . . . . . . . . . . . . . . . . . . . . 32

6.5.3 Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

6.5.4 Incompressibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

7 Rendering 34

7.1 Our implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

7.2 Surface tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

7.3 Marching cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

7.4 Point Splatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

7.5 Ray Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

8 Results 36

8.1 Code structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

8.2 Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

8.2.1 Water jet straight down . . . . . . . . . . . . . . . . . . . . . . . . 38

8.2.1.1 With Surface Tension . . . . . . . . . . . . . . . . . . . . 38

8.2.1.2 Without Surface Tension . . . . . . . . . . . . . . . . . . 39

8.2.2 Water jet hitting an inclining plane . . . . . . . . . . . . . . . . . . 41

8.2.3 Dam break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

8.2.4 Water jet hitting a wall . . . . . . . . . . . . . . . . . . . . . . . . 43

8.2.5 Water jet in small scale . . . . . . . . . . . . . . . . . . . . . . . . 44

9 Discussion 45

10 Conclusion 47

A Kernels 48

Bibliography 49

Page 10: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

List of Figures

2.1 (a) 3D schematics of the Runout Table. (b) 2D schematics of water jetsshown from the side. (c) Photo taken of the Runout Table. . . . . . . . . 4

4.1 The left figure illustrates the Eulerian viewpoint with a grid and the rightfigure illustrates the Lagrangian viewpoint with particles. . . . . . . . . . 6

5.1 Dirac delta function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5.2 The three smoothing kernels from left to right; Wpoly6, Wspiky andWviscosity.The thick lines show the kernels, the thin lines their gradients and thedashed lines their Laplacians [1]. . . . . . . . . . . . . . . . . . . . . . . . 12

5.3 High density, hence repulsive pressure force. . . . . . . . . . . . . . . . . . 16

5.4 Low density, hence attractive pressure force. . . . . . . . . . . . . . . . . . 16

5.5 Rest density, hence no pressure force. . . . . . . . . . . . . . . . . . . . . . 16

5.6 Surface tension is caused by an imbalance of forces at the surface of theliquid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

5.7 Colour field. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

6.1 Example of a hash table used for storing phone numbers [2]. . . . . . . . . 23

6.2 Simulation area is divided into three-dimensional boxes with side h. . . . 24

6.3 All neighbours will be found in the 3 × 3 × 3 boxes that constitutes thecurrent box and the adjacent boxes. . . . . . . . . . . . . . . . . . . . . . 24

6.4 The neighbours of the particle are inside the red circle, all the particlesinside the green square will be potential neighbours. . . . . . . . . . . . . 25

6.5 Arrays boxId and usedBoxes illustrated. . . . . . . . . . . . . . . . . . . . 26

6.6 Illustration of the Leap-Frog integration scheme, x is position, v velocityand t time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

6.7 Kernel function plotted with smoothing length h = 1.0 and h = 1.5. . . . 29

6.8 To the left, particles edge to edge, with a density lower than the densityof water. To the right, particles that inter-penetrate, with a density closerto the density of water. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

6.9 To the left, three layers of particles initialized close after each other. Tothe right, three layers of particles initialized further apart from each other. 31

6.10 From left to right; the particles first experience an attraction force, inthe next time step they have advanced and are too close to each other,inducing a very high pressure force causing them to repel from each other. 32

7.1 The different cube configurations used in the Marching cubes algorithm [3]. 35

8.1 Main code structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

vi

Page 11: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

List of Figures vii

8.2 Simulation including surface tension at times; t = 0.0s, t = 0.36s, t =0.72s, t = 1.08s, t = 1.44s and t = 1.80s. . . . . . . . . . . . . . . . . . . . 39

8.3 Simulation not including surface tension at times; t = 0.0s, t = 0.36s,t = 0.72s, t = 1.08s, t = 1.44s and t = 1.80s. . . . . . . . . . . . . . . . . 40

8.4 Simulation of inclining plane at times; t = 0.27s, t = 0.54s, t = 0.81s,t = 1.08s, t = 1.35s and t = 1.62s. . . . . . . . . . . . . . . . . . . . . . . 41

8.5 Simulation of a dam break at times; t = 0.0s, t = 0.3s, t = 0.6s, t = 0.9s,t = 1.2s, t = 1.5s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

8.6 Simulation of a jet hitting a wall at times; t = 0.36s, t = 0.72s, t = 1.08s,t = 1.44s, t = 1.80s and t = 2.16s. . . . . . . . . . . . . . . . . . . . . . . 43

8.7 Simulation in small scale at times; t = 0.00s, t = 0.04s, t = 0.08s,t = 0.12s, t = 0.16s and t = 0.20s. . . . . . . . . . . . . . . . . . . . . . . 44

Page 12: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Abbreviations

CFD Computational Fluid Dynamics

SPH Smoothed Particle Hydrodynamics

ROT RunOut Table

viii

Page 13: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Physical Constants

Gravitational acceleration g = 9.81 m/s2

Dynamic viscosity of water µ = 1.003× 10−3 Pa · s

Density of water ρ = 998.29 kg/m3

Surface tension coefficient water/air σ = 0.0728 N/m

ix

Page 14: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Symbols

p pressure Pa

m mass kg

r distance m

h smoothing length m

f force N

u velocity m/s

µ dynamic viscosity Pa·s

ρ density kg/m3

σ surface tension coefficient N/m

x

Page 15: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

1. Introduction

In high quality steel production the cooling process is of great importance for the steel

composition. A full understanding of the cooling is essential and the modelling, simula-

tion and prediction of the process are vital to improving it.

The cooling is the last part of the steel production process and is performed right before

the steel is coiled up. It occurs on the so called Runout Table (ROT), where the hot

steel is cooled by being sprayed with water jets.

Computational Fluid Dynamics (CFD) is often used in Research and Development

(R&D) where there is a need for high accuracy. Because of the complexity in sim-

ulating fluids, high accuracy means computationally heavy and very time consuming

simulations. Simulating larger parts of the Runout Table in the cooling process with

several water jets and important 3D effects, becomes too expensive in terms of CPU

time and can not be handled in an R&D industrial project.

Improving the cooling process could mean adjusting parameters in the problem setting.

However, testing this in reality is very expensive, therefore it is preferable to first use

simulations. This way, unnecessary tests can be avoided. Nevertheless, accurate simula-

tions require enormous amounts of computations resulting in extremely slow run times.

These sorts of simulations are currently used in the software Fluent, as an example

simulating one water jet can take almost a week.

On the other hand we know that developers in the gaming community have achieved real-

time fluid flow simulations with convincing results but with a loss in accuracy. Knowing

that industrial design, monitoring or control, often do not need the high accuracy that

the CFD methods can provide, the video games techniques to simulate and predict the

flow can be sufficient for many industrial processes.

1

Page 16: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Introduction 2

There are mainly three different aspects when speeding up computations: the software,

the hardware and parallelism/cloud computing. The focus in this thesis is the software.

The goal of this project is to explore the techniques used in video games when simulating

flow and to see to what extent they can be used to model the cooling process of the

Runout Table.

The main objectives of this thesis are to:

1. Investigate which numerical methods are used in video games when simulating

fluids, in particular when simulating liquids.

2. Choose a method suitable for simulating the cooling process on the ROT.

3. Implement the chosen method for a simplified version of the ROT.

4. Add complexity to the implementation to approach the real-world problem.

Page 17: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

2. Problem Setting

When hot rolling steel is manufactured, the steel is heated then pressed together to

a thin plate, cooled down and finally rolled up. The cooling occurs on the so called

Runout Table, (ROT), which is shown in Figure 2.1. The upper image shows a 3D

schematics illustrating several water jets. The lower left image shows a 2D schematics

with two water jets and the lower right image shows a picture taken of the ROT. In

these images some of the different parameters are illustrated, for example the distance

between water jets, the height above the surface and the diameter of the inlet. All the

different parameter values could be adjusted in order to get a better result. Taking all

the parameters into account there are a great number of ways they can be coupled.

When cooling the steel, the most important aspect is the quality of the steel. Ideally,

the cooling will lower the temperature as much as needed, with as little water as possible

and without risking deterioration of the quality of the steel.

Ultimately the goal is to simulate the ROT in its entirety, i.e. several water jets, in

3D with thermodynamic effects. This thesis will scale down the problem and focus

on implementing a simulation with one water jet in 3D excluding temperature. More

specifically, the main problem setting will be one water jet hitting a plane surface from

25 cm above the surface. Other problem settings will also be simulated in order to

investigate the accuracy of the implementation. Those settings will be a water jet

hitting an inclining surface, a water jet hitting a wall and a dam break.

3

Page 18: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Problem Setting 4

Figure 2.1: (a) 3D schematics of the Runout Table. (b) 2D schematics of water jetsshown from the side. (c) Photo taken of the Runout Table.

Page 19: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

3. Theory

The simulation of fluids plays an important role in a lot of different areas. It is used for

predicting weather, designing airplanes and cars, studying blood flow, studying magne-

tohydrodynamics and for developing video games. Almost all fluid simulations are based

on the governing equations called the Navier-Stokes equations.

3.1 Navier-Stokes equations

In the 19th century, Claude Navier and George Stokes formulated the famous Navier-

Stokes equations that describe the dynamics of fluids. They are derived from applying

Newton’s second law, conservation of momentum, to fluid motion and coupled with

the conservation of mass. The basic formulation of the Navier-Stokes equations for an

incompressible fluid is

ρ

(∂u

∂t+ u · ∇u

)= −∇p+ µ∇2u + f (3.1)

∇ · u = 0, (3.2)

where ρ is the density, u velocity, p pressure, µ viscosity and f external forces. The

second equation is the continuity equation for incompressible flow, which is derived

from the conservation of mass. In the general case these equations are impossible to

solve analytically. Therefore a lot of different numerical methods have been developed

to solve the Navier-Stokes equations.

5

Page 20: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

4. Methods

There are two main categories of methods when numerically solving the Navier-Stokes

equations; grid-based Eulerian methods and particle-based Lagrangian methods. In the

Eulerian viewpoint, there is a fixed grid and the fluid is observed flowing in and out of

the grid cells. In the Lagrangian viewpoint the particles themselves and their movements

are observed. These concepts are illustrated in figure 4.1.

Figure 4.1: The left figure illustrates the Eulerian viewpoint with a grid and the rightfigure illustrates the Lagrangian viewpoint with particles.

A fluid is a continuum and the Navier-Stokes equations describe the motion of that

continuum. Since, in most cases, they are impossible to solve analytically numerical

methods must be used. This means that a discretization is necessary. One way of

discretizing is dividing the domain into a grid, which is what is done in Eulerian meth-

ods. Another approach is dividing the fluid into particles, which is done in Lagrangian

methods.

6

Page 21: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Methods 7

In areas such as physics, engineering and mathematics there is a need for high accuracy

and so accurate numerical methods are used. We will briefly discuss three of these

namely; Finite Differences, Finite-Element and Finite Volume Methods[4]. All of them

are grid-based Eulerian methods.

In Finite Differences partial spatial and temporal derivatives appearing in the equations

are approximated through Taylor series. These methods are typically employed only on

Cartesian geometries.

Finite-element methods require a 2D or 3D mesh. At each mesh element, a basis function

is used. It should locally describe the solution of the weak form of the governing equation

to be approximated. The finite-element method aims to minimize the difference between

the exact solution and the collection of basis functions.

The most commonly used method in science and engineering when simulating flow is

the Finite Volume Method (FVM), where the principle is local conservation. To solve

the equations numerically the computational domain is divided into cells. The partial

derivatives expressing a conservation principle can be rewritten at each cell as an alge-

braic contribution. The governing equation is reformulated at each computational cell

into a set of linear algebraic equations, which may be solved in an iterative manner.

In video games however there is no need for high accuracy, instead good performance

and plausible results are of importance. When it comes to liquid simulations the most

common methods are Eulerian grid-based methods, Smoothed Particle Hydrodynamics

(SPH) methods, Vorticity-based methods and Lattice-Boltzmann methods. These meth-

ods originated in the computational fluid dynamics community but have steadily been

adopted by the graphics community.

The Eulerian grid-based methods that are used in video games are mostly based on

Jos Stam’s Stable Fluids method [5]. This method uses a semi-Lagrangian advection

technique and implicit integration of viscosity to provide unconditionally stable behavior.

To be able to use an Eulerian grid-based method for simulating liquid, a surface tracking

method is also needed, such as the Level Set Method (LSM) or the Marker and Cell

(MAC) method.

Page 22: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Methods 8

Vorticity-based methods use particles representing small vortices, called vortons, which

induce a velocity field which in turn moves the vortons. To model the fluids motion it

solves the vorticity equation instead of the momentum equation [6].

Instead of solving the Navier-Stokes equations, which describe the macroscopic prop-

erties of a fluid, the Lattice-Boltzmann method solves the Boltzmann equation which

describes the microscopic behavior of a fluid. The method provides a first-order explicit

discretization of the Boltzmann equation in a discrete phase space. The simulation re-

gion is divided into a Cartesian grid of cells, each of which only interacts with cells of

its direct neighborhood [7].

Smoothed Particle Hydrodynamics is a mesh-free Lagrangian method. The principal

idea is to use a so called smoothing kernel, which approximates the Dirac delta function,

to interpolate properties of particles. This means that a particle’s physical properties

depend on the particles that lie within a certain distance from that particle.

4.1 Why SPH

The Smoothed Particle Hydrodynamics method was chosen for this project because it

has a number of advantages. Since it uses particles to represent the fluid there is no

need for the convective term in the Navier-Stokes equations. All particles represent a

constant mass, therefore mass conservation is ensured. As opposed to Eulerian methods

there is no need for a specific surface-tracking method and the particles can spread freely

in space, not bounded by a grid. It is also reasonably easy to understand and implement

and it is the most popular method in computer graphics for physically based simulation

algorithms [8].

Page 23: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

5. Smoothed Particle

Hydrodynamics

Smoothed Particle Hydrodynamics (SPH) was developed in 1977 and was originally used

for astrophysical computations [9], but has since then been adopted in many different

areas [10]. It is a mesh-free Lagrangian method and as such models fluids by particles.

SPH is used to obtain approximate numerical solutions of the equations of fluid dynamics

by simulating the fluid with particles. Mathematically the particles can be seen as

interpolation points from which the properties of the fluid can be calculated. Physically

the particles can be seen as material particles. The particles can carry concrete entities

such as mass, position and velocity. They can also carry field quantities such as density,

pressure and temperature.

9

Page 24: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 10

5.1 Derivation

The following subsection covers the derivation of the interpolating function used in SPH.

We know that the Dirac delta function, δ(x), (see figure 5.1),

Figure 5.1: Dirac delta function.

has the property that any function, A(x), can be evaluated at any point, x, as

A(x) =

∫A(x′)δ(x− x′)dx′. (5.1)

We introduce the smoothing kernel function,W (r, h), where r is position and h is the

smoothing distance. The kernel function is a normalized, positive and symmetric func-

tion

∫W (r, h)dr = 1 (5.2)

W (r, h) ≥ 0 (5.3)

W (r, h) = W (−r, h) (5.4)

and it approaches the Dirac delta function as h→ 0,

limh→0

W (r, h)→ δ(r). (5.5)

We replace the Dirac delta function in (5.1) with the kernel function. Thus, we have

that any scalar function, A(r), can be approximated at any point,r, by

A(r) =

∫A(r′)W (r− r′, h)dr′. (5.6)

Page 25: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 11

We discretize (5.6) by using the midpoint rule and instead of an integral we get the

summation

A(r) =∑j

A(rj)W (r− rj , h)∆Vj . (5.7)

We replace ∆Vj bymj

ρj, where mj is the mass of particle j and ρj is the density of particle

j and get

A(r) =∑j

mjAjρjW (r− rj , h). (5.8)

This is the interpolating function that is used in SPH when evaluating any scalar quan-

tity. If we are interested in the gradient or the Laplacian, since mj , ρj and Aj are

independent of r, we would get for the gradient

∇A(r) =∑j

mjAjρj∇W (r− rj , h) (5.9)

and for the Laplacian

∇2A(r) =∑j

mjAjρj∇2W (r− rj , h). (5.10)

These summations are over all particles j but only the particles within a radial distance

h will contribute to the sum. This is because the kernel function W will go to zero for

r ≥ h. This will reduce the computational cost from O(N2) to O(mN) where N is the

number of particles and m is the number of neighbours within a distance h.

Page 26: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 12

5.2 Kernel functions

Many different Kernel functions exist and we will here introduce three kernel functions

that are discussed in [1];

Wpoly6(r, h) =315

64πh9

(h2 − ||r||2)3 0 ≤ ||r|| ≤ h

0 otherwise

(5.11)

Wspiky(r, h) =15

πh6

(h− ||r||)3 0 ≤ ||r|| ≤ h

0 otherwise

(5.12)

Wviscosity(r, h) =15

2πh3

−||r||32h3

+ ||r||2h2

+ h2||r|| − 1 0 < ||r|| ≤ h

0 otherwise

(5.13)

These kernel functions, their gradients and their Laplacians can be seen in Figure 5.2.

Note the different scales on the y-axis. The kernel function Wpoly6 is used in the general

Figure 5.2: The three smoothing kernels from left to right; Wpoly6, Wspiky andWviscosity. The thick lines show the kernels, the thin lines their gradients and the

dashed lines their Laplacians [1].

case. It has been noted that if Wpoly6 is used for the pressure term, which includes the

gradient of the kernel function, particles tend to cluster together under high pressure.

This is because the gradient of Wpoly6 goes to zero for small distances and hence the

repulsive force disappears. Therefore the spiky kernel, which has a gradient that does

not approach zero when r → 0, is used for calculating pressure. We see in Figure 5.2

that the Laplacian of Wpoly6 becomes negative for small values of r. If Wpoly6 is used for

the viscous term, which includes the Laplacian, negative values can appear and increase

Page 27: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 13

the relative velocity thereby introducing energy to the system. This is non-physical

and to avoid it the viscosity kernel, which has a positive definite Laplacian, is used for

the viscous term. The kernels, its gradients and Laplacians are explicitly written in

Appendix A.

5.3 History of SPH

The Smoothed Particle Hydrodynamics method was introduced by Gingold and Mon-

aghan and independently by Lucy in 1977 when they were working on astrophysical

problems [9]. They derived the equations of motion of fluid dynamics using a kernel

estimation technique to estimate probability densities from sample values. Using this

technique for interpolation yielded an estimation of a function at any point using the

values of the function at the particles. This estimate could be differentiated exactly

provided the kernel function was differentiable. This meant the gradient terms in the

equations of fluid dynamics could be written in terms of the properties of the particles.

Because of the close relation to statistical ideas the method was described as a Monte-

Carlo method. However, in subsequent papers it was discovered that the errors were

much smaller than the predicted probability estimates.

Originally the numerical schemes proposed did not conserve linear and angular momen-

tum exactly but gave good results for the astrophysical problems that were considered

too difficult for the techniques available. In 1982 Gingold and Monaghan improved

the basic SPH algorithm to conserve linear and angular momentum exactly using the

particle equivalent of the Lagrangian for a compressible non-dissipative fluid [9].

Although SPH was originally introduced for use in astrophysical problems with com-

pressible flow. It has since been used in simulations of incompressible or nearly incom-

pressible flow. The technique has steadily been adopted in a number of different areas.

Some examples are simulations including elasticity and fractures, multifluid problems,

non-Newtonian fluids, virtual surgery and molecular dynamics [10].

SPH was introduced to the computer graphics community by Stam and Fiume to de-

pict fire and other gaseous phenomena [11]. Desbrun later used SPH to animate highly

Page 28: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 14

deformable bodies [12]. Muller et al. extended Desbruns method focusing on the sim-

ulation of fluids [1]. They also designed special purpose kernels and proposed a way to

model surface tension forces.

5.4 Solving the Navier-Stokes equation using SPH

The basic formulation of the Navier-Stokes equations for an incompressible fluid is

ρ

(∂u

∂t+ u · ∇u

)= −∇p+ µ∇2u + f (5.14)

∇ · u = 0 (5.15)

where u ·∇u is the convective term. Since we are using a particle system the convective

term is not needed because the flow will carry the particles along. The continuity

equation, which states the conservation of mass, is omitted because the particles will

carry the mass hence ensuring its conservation. So we are left with

ρ∂u

∂t= −∇p+ µ∇2u + f. (5.16)

The three terms on the left hand side represent the pressure force, the viscous force and

external forces. We denote these forces by F;

F = fpressure + fviscosity + fexternal (5.17)

and we get for particle i;

ai =duidt

=Fiρi

(5.18)

where ai is the acceleration of particle i. We integrate the right hand side to get the

velocity. Once we have the velocity we can integrate again to get the position. The

algorithm is simply computing these three forces for each particle, summing them up

and integrating.

Page 29: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 15

5.4.1 Density

The density is computed according to SPH as

ρi =∑j

ρjmj

ρjW (ri − rj , h) (5.19)

ρi =∑j

mjW (ri − rj , h), (5.20)

where ρj is the density for particle j and mj is the mass for particle j. The summation

is over all particles j within a radial distance h of particle i.

5.4.2 Pressure force

The ideal gas law states that

pV = nRT (5.21)

where p is pressure, V is volume per unit mass, n is number of gas particles in mol,

R is the universal gas constant and T is the temperature. In an isothermal fluid with

constant mass the right hand side will be kept constant and we replace it by a gas

stiffness constant k. The equation then becomes

pV = k (5.22)

p1

ρ= k (5.23)

p = kρ (5.24)

where ρ is the density. Desbrun and Cani suggest subtracting a “rest density” from the

density in the pressure equation, giving

p = k(ρ− ρ0) (5.25)

where ρ0 is the density when the fluid is at rest [12]. This new pressure equation will

introduce a pressure force that is repulsive when the density is higher than the rest

density, attractive when the density is lower than the rest density and zero if the fluid

is at rest. This is illustrated in figure 5.3, 5.4 and 5.5 [13].

Page 30: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 16

Figure 5.3: High density, hence repulsive pressure force.

Figure 5.4: Low density, hence attractive pressure force.

Figure 5.5: Rest density, hence no pressure force.

The pressure force is the gradient of pressure;

fp = −∇p. (5.26)

Page 31: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 17

So the pressure force at particle i in SPH notation becomes

fpi = −∑j 6=i

pjmj

ρj∇W (ri − rj , h). (5.27)

This pressure force is not symmetrical, which is non-physical since two interacting par-

ticles affect each other with the same pressure in opposite direction. There are two

different ways to correct this, one way is to replace the pressure in the equation by the

arithmetic mean of the pressure of particle i and particle j. This gives the symmetric

pressure force

fpi = −∑j 6=i

pi + pj2

mj

ρj∇W (ri − rj , h). (5.28)

Another solutions is to use the following equation

fpi = −ρi∑j 6=i

(pjρ2j

+piρ2i

)mj∇W (ri − rj , h), (5.29)

which we will now derive. We start by rewriting the gradient of a function A divided by

the density ρ as

∇(A

ρ

)=

1

ρ∇A− A

ρ2∇ρ. (5.30)

Rearranging gives us1

ρ∇A = ∇

(A

ρ

)+A

ρ2∇ρ. (5.31)

In SPH the expression becomes

1

ρ∇A =

∑j

Ajρj

mj

ρj∇W (r− rj , h) +

A

ρ2

∑j

ρjmj

ρj∇W (r− rj , h) (5.32)

∇A = ρ∑j

(Ajρ2j

+A

ρ2

)mj∇W (r− rj , h). (5.33)

We now replace A in equation 5.33 by −pi and get

−∇pi = −ρi∑j

(pjρ2j

+piρ2i

)mj∇W (ri − rj , h). (5.34)

However, according to Colin et al. [14] the arithmetic mean gives more accurate differ-

entials, hence (5.28) will be used for the pressure force.

Page 32: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 18

5.4.3 Viscosity force

Viscosity is a measure of the resistance to deformation by shear stress. For liquids, this

is often referred to as the thickness. The viscosity force in the Navier-Stokes equation is

fviscosityi = µ∇2u(ri). (5.35)

In SPH this becomes

fviscosityi = µ∑j 6=i

ujmj

ρj∇2W (ri − rj , h). (5.36)

This viscous force is not symmetrical which is physically inaccurate. It can be adjusted

by replacing the velocity of particle j with the velocity difference ui − uj . This is accu-

rate since the viscosity only depends on the velocity difference between the interacting

particles. The viscosity force therefore becomes

fviscosityi = µ∑j 6=i

(ui − uj)mj

ρj∇2W (ri − rj , h). (5.37)

5.4.4 External forces

External forces are all the forces affecting the fluid particles that are not explicitly in

the Navier-Stokes equation.

5.4.4.1 Gravity

The gravitational force fgravity acts on all particles straight downwards and is simply

added to the other forces.

fgravity = ρig (5.38)

where g is the gravitational acceleration.

5.4.4.2 Surface Tension

Surface tension occurs at the interface between fluids. Inside the liquid, water molecules

attract each other with equal forces and balance each other out. An imbalance of that

Page 33: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 19

attraction occur on the water surface where the surface molecules will only be attracted

by the molecules inside the fluid hence inducing a net force inward, see figure 5.6. The

surface tension force works to minimize the surface area. This is why liquid droplets

tend to form spheres at zero gravity. The net force tends to minimize curvature, which

is why the greater the curvature, the higher the force.

Figure 5.6: Surface tension is caused by an imbalance of forces at the surface of theliquid.

Muller et al [1] use the surface tension force

fsurface = σκn = −σ∇2csn

|n|(5.39)

where σ is the surface tension coefficient which depends on the fluids that form the

surface, in this case water and air, κ is the curvature of the surface, n is the surface

normal field and cs is the smoothed colour field. The colour field is a field quantity that

is 1 where there are particles and 0 elsewhere. This is illustrated in Figure 5.7.

Figure 5.7: Colour field.

Page 34: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 20

The SPH formulation of this quantity, using (5.8), becomes

cs(r) =∑j

mj1

ρjW (r− rj , h). (5.40)

The surface normal field, n, is obtained by taking the gradient of the smoothed colour

field

n = ∇cs (5.41)

using (5.9) and the divergence of n measures the curvature of the surface

κ = −∇n|n|

= −∇2c

|n|. (5.42)

The surface tension force will only apply to surface particles and a particle is identified

as a surface particle if

|n(ri)| > l, (5.43)

where l is a threshold parameter. The surface tension force is normally not a part of the

Navier-Stokes equation because it is regarded as a boundary condition.

5.4.5 A simple Navier-Stokes solver

Using the derived equations for density, pressure, pressure force and viscosity force a

simple Navier-Stokes solver would look as follows;

� Step 1: Compute density

ρi = ρ(ri) =∑j

mjWpoly6(ri − rj , h) (5.44)

� Step 2: Compute pressure

pi = p(ri) = k(ρ(ri)− ρ0) (5.45)

Page 35: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Smoothed Particle Hydrodynamics 21

� Step 3: Compute interaction forces

fpressurei = −∇p(ri) = −∑j

mjpi + pj

2ρj∇Wspiky(ri − rj , h) (5.46)

fviscosityi = µ∇2u(ri) =∑j

mjuj − uiρj

∇2Wviscosity(ri − rj , h) (5.47)

Sum up Fi = fpressurei + fviscosityi + fexternali (5.48)

� Step 4: Solve ODEs

vi =Fiρi, xi = vi (5.49)

� Step 5: Go to step 1

The ODEs in step 4 can be solved with any numerical integration technique; leap-

frog would be a good choice because it is of second order and does not require many

calculations.

Page 36: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

6. Implementation

The implementation of the Smoothed Particle Hydrodynamics method was based on

pseudo code taken mainly from Muller’s Particle-Based Fluid Simulation for Interactive

Applications [1] and from Kelager’s Lagrangian Fluid Dynamics Using Smoothed Particle

Hydrodynamics [13].

The SPH method was implemented in MATLAB. The particle values such as density,

velocity and force were stored in arrays or matrices. Each particle is uniquely identified

by its index value. For example particle i has density dens(i) and velocity v(:, i), where

v is a 3 × N matrix where the first row represents the x-values, the second row the

y-values and the third row the z-value. Each column thus represents a particles x-, y-

and z-values of its velocity.

The simulation will run through a predetermined number of iterations, store the value

of the particle positions at the end of each iteration and then plot the particles at each

time step.

6.1 Fast Nearest Neighbour search

In particle simulations, the interacting forces between particles are computed in order

to calculate the force each particle will experience and then use that force to move

the particle in space. All particles will affect each other with forces but the further

away a particle is the less the effect will be. So to reduce the computational cost only

the particles closest, the neighbours, will be taken into account and the others will be

ignored. A particle is considered a neighbour if it is within a radial distance, h, called the

smoothing length. The problem of finding the neighbours can be very time consuming

so an efficient algorithm is needed.

22

Page 37: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 23

In each iteration in the algorithm the neighbours need to be found and stored because

they will be used in the computation of the densities and the forces.

6.1.1 Hash tables

Finding the nearest neighbours can typically be done using hash tables. The general

idea of a hash table is to use a hash function that takes a key as input and returns an

index to a bucket in the hash table, where the value is stored, thus mapping keys to

values, see figure 6.1. The risk with using hash tables are hash collisions, i.e. when two

keys are mapped to the same value.

Hash tables was the first method implemented in this project, using the coordinate

values of each particle as a hash key. The method is described in [15]. However to avoid

the risk of hash collisions and to save space from storing an unnecessary large hash table

another approach has been implemented.

Figure 6.1: Example of a hash table used for storing phone numbers [2].

6.1.2 Our own neighbour search algorithm

We divide our three dimensional simulation area into cells or boxes with a side length

equal to the smoothing length, h, as seen in figure 6.2. We number the boxes from left

to right and bottom up so all the boxes have a unique id number. The particles will

be associated to the boxes they are in by their coordinates, meaning all particles will

lie in a certain box. Since the neighbours of a particle will lie within a distance h, all

neighbours will be either in the same box or in an adjacent box. This means that you

Page 38: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 24

Figure 6.2: Simulation area is divided into three-dimensional boxes with side h.

are sure to find all neighbours in the 27 boxes that constitute the adjacent boxes and

the current box, see figure 6.3. This also means that instead of saving the neighbours of

Figure 6.3: All neighbours will be found in the 3 × 3 × 3 boxes that constitutes thecurrent box and the adjacent boxes.

every particle it suffices to save the neighbours of every used box. When the neighbours

need to be found for computations, simply check amongst the stored particles which

ones are within a distance h. Note that these boxes are imaginary and are not created

and stored.

We illustrate this idea of neighbours and boxes and particles in 2D in figure 6.4. All the

boxes that contain particles will be used boxes, in the figure these boxes are shown with

a yellow background. We want to find the neighbours of the particle in the middle. All

its neighbours are inside the red circle. However all the particles in the neighbouring

boxes, i.e. all the particles inside the green square, will be stored as potential neighbours.

When the neighbours are to be found for computations, the distance between the particle

in question and the potential neighbours will be computed and compared against the

smoothing length h.

Page 39: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 25

Figure 6.4: The neighbours of the particle are inside the red circle, all the particlesinside the green square will be potential neighbours.

6.1.3 Neighbour search algorithm

This neighbour search can be summarized as:

� For each particle i, get box id and store in array ‘boxId’.

� Check which boxes are used by getting the unique values in the array ‘boxId’ and

store in the array ’usedBoxes’. See figure 6.5.

� Create matrix ‘neighb’ for storing neighbours, where the number of rows equal

the number of used boxes and the number of columns equal a maximum allowed

number of neighbours.

� For each used box get neighbours by computing the ids of the adjacent boxes and

comparing them against the array ’boxId’. Store the neighbours in matrix ‘neighb’.

Each row will now contain all the neighbours of a specific box.

In the algorithm used for solving the Navier-Stokes equation we need to loop over all

particles when computing the density and the forces. Instead of looping over all particles

from start to end, we loop over all used boxes and then we loop over all the particles in

each particular box. This is efficient because all the particles in the same box will have

almost the same neighbours.

In this solution there is a maximum number of neighbours each box can store. This

maximum number is a user parameter initialized in the beginning of the simulation. If,

for some reason, the maximum number of neighbours is attained before having checked

Page 40: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 26

Figure 6.5: Arrays boxId and usedBoxes illustrated.

all the neighbouring boxing, the algorithm will be satisfied with the neighbours it has. To

make sure that all the particles in one box are neighbours, the center box is checked first.

This is needed because it is crucial that the particles are neighbours with themselves

for the summation that occurs in the density computation. It is important to note a

consequence of this, which is that particle i can have particle j as a neighbour, but it

does not necessarily mean that the reverse is true.

6.2 Integration

After having computed the forces for each particle we have the acceleration and we need

to integrate in time in order to get the velocity and then integrate again to get the

position. That is, we need to solve the ODEs

vi =Fiρi

and xi =viρi. (6.1)

When integrating numerically there are different integration schemes that can be used.

Some common schemes are the Explicit Euler Scheme, the Implicit Euler Scheme, the

Page 41: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 27

Verlet Scheme and the Leap-Frog Scheme. Leap-Frog was chosen since it is of second

order accuracy and it requires few evaluations per step.

6.2.1 Leap-Frog Scheme

In Leap-Frog the positions and the velocities are updated at interleaved time points.

They “leapfrog” over each other. For example if the positions are updated at integer

time steps and the velocities are updated at integer plus a half time steps. This is

illustrated in figure 6.6.

Figure 6.6: Illustration of the Leap-Frog integration scheme, x is position, v velocityand t time.

First the velocity at time t+ 12∆t is computed

vt+ 12

∆t = vt− 12

∆t + ∆tat (6.2)

then the position at time t+ ∆t is computed using the velocity at time t+ 12∆t

rt+∆t = rt + ∆tvt+ 12

∆t. (6.3)

The first iteration requires the velocity at negative time −12∆t, this velocity is given by

one Euler step.

v− 12

∆t = v0 −1

2∆ta0. (6.4)

To get the velocity at time t we take the average of vt+ 12

∆t and vt− 12

∆t

vt =vt− 1

2∆t + vt+ 1

2∆t

2. (6.5)

Page 42: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 28

6.3 Boundary problems

In this implementation, the main problem setting has been one simple water jet origi-

nating from an inlet above a planar surface and pointing straight down. The boundaries

have not been very complex.

There are different ways of dealing with the boundaries. One way is ghost particles or

boundary particles. They are implemented particles that represent the boundaries and

sometimes even the air. The idea is to simulate the air or the surface of the boundary

with particles that will affect the water particles the same way as the water particles

affect each other. In [16] Hagit Schechter and Robert Bridson propose a Ghost SPH

scheme where they create a narrow layer of particles in the surrounding air and solid.

Another possible way to deal with the boundaries is to use the Lennard-Jones potential

at the boundaries. This will attract particles far away and repel particles too close. The

risk is to have non-physical solutions as water tends to “stick” to surfaces when there is

a no-slip condition.

A very easy solution the boundary problem is to simply put the perpendicular component

of the velocity to zero or reflect a small percentage when a particle hits the surface. The

amount that is reflected is specified by the coefficient of restitution, cr. This solution

was chosen in this implementation. In each iteration after having moved the particles

by updating their positions, their coordinates are checked against the boundaries. If a

particle has hit the surface its perpendicular component is reflected by a small percentage

and if a particle is outside the simulation domain it is removed.

6.4 Parameter values

6.4.1 Smoothing length

The smoothing length, h, is of great importance in the Smooth Particle Hydrodynamics

method. Because the SPH interpolation formula use a kernel function that increases as

r decreases, the closer a particle is the more it will affect the sum. If the smoothing

length is too small the particles will have too few neighbours or no neighbours at all,

which will lead to imprecise results. On the other hand, the smoothing length may not

Page 43: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 29

be too large either. If h is too large the kernel function will be stretched out and the

particles close will not affect the sum as much. This is illustrated in figure 6.7, where the

higher red line corresponds to h = 1.0 and the dotted black line corresponds to h = 1.5.

It is clear from the plot that the greater the smoothing length is the less important the

particles at small distances will be.

Figure 6.7: Kernel function plotted with smoothing length h = 1.0 and h = 1.5.

In [9] Monaghan suggests the smoothing length,

h = σ

(m

ρ

)1/d

, (6.6)

where d is the number of dimensions and σ is a constant ∼ 1.3. There is however no

absolute best way to decide the smoothing length.

6.4.2 Viscosity coefficient

The viscosity used here, µ, is the dynamic viscosity. For water the dynamic viscosity is

µ = 1.003× 10−3. (6.7)

However, we will use a viscosity that is about a 1000 times higher than the physically

correct value. The reason for this is damping and numerical stability. Physically, the

viscosity term simulates the viscous behaviour of a fluid. Numerically, it damps the

Page 44: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 30

simulation and provides stability to the system. It is common to add a damping term

to the velocity when integrating. This is omitted and instead the only damping will be

from the viscosity.

6.4.3 Mass

In this implementation all particles have the same mass. It is determined from the rest

density, ρ0, and from the particle radius, r,

massparticle = ρ0V = ρ04πr3

3, (6.8)

where V is the volume of a particle.

6.4.4 Gas stiffness constant

We stated in 5.4.2 that the gas stiffness constant is

k = nRT, (6.9)

where n is the number of gas molecules in mol, R is the universal gas constant and

T is the temperature in Kelvin. The universal gas constant is R = 8.3144 JmolK , the

molar mass for water is 0.018 kgmol and 20 °C in Kelvin is 293.15K. This gives for our gas

constant

k =8.3144 · 293.15

0.018

[J mol K

kg mol K

]= 135, 409

[J

kg

]= 135, 409

[m2

s2

]. (6.10)

A gas stiffness constant of that size would require an extremely small integration time

step to avoid numerical instabilities. This constant is typically set in the order ∼ 1−10.

If k increases the time step, dt, must decrease.

6.4.5 Density

The rest density, ρ0, was set to the density of water

ρwater = 998.29 kg/m3. (6.11)

Page 45: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 31

It is worth to note that to obtain a density for the particles close to the real density

of water the particles must be allowed to inter-penetrate. This means that the distance

between the centre of two particles is allowed to be smaller than two radii. This concept

is illustrated in figure 6.8, where the particles on the left will all have density smaller

than the density of water and the particles on the right, which are closer, will have a

density much closer to that of water.

Figure 6.8: To the left, particles edge to edge, with a density lower than the density ofwater. To the right, particles that inter-penetrate, with a density closer to the density

of water.

6.5 Implementation issues

6.5.1 Initialization

The main problem setting is a water jet situated above a plain surface. The particles

are initialized in each iteration step in a circle at the top of the simulation area with an

initial velocity directed downwards. If the time step is small, the velocity is small and

new particles are initialized each iteration, the particles initialized in the previous step

will not have moved noticeably before the next set of particles are initialized. This is

illustrated in figure 6.9.

Figure 6.9: To the left, three layers of particles initialized close after each other. Tothe right, three layers of particles initialized further apart from each other.

If the particles are initialized too closely the simulation will be unstable due to the fact

that the pressure force is inversely proportional to the distance between the particles,

Page 46: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 32

r. If the particles are situated too close to each other the pressure force will explode.

This can be avoided by either initializing the particles more rarely or by increasing the

time step. However the time step can not be too large. If two particles are at a distance

from each other where they will experience an attraction force and the time step is large

the particles will be too close in the next iteration. This will induce a great repulsive

pressure force and the particles will be completely repelled. See figure 6.10.

Figure 6.10: From left to right; the particles first experience an attraction force, inthe next time step they have advanced and are too close to each other, inducing a very

high pressure force causing them to repel from each other.

6.5.2 Particles ”stick together”

If, for some reason, two particles end up in the same position i.e. they have the exact

same coordinates they will no longer be separable. The forces acting on the particles

depend on the distance between them, so if that distance is zero the force will be zero

and the particles will behave as one. The reason why the particles stick together is

unclear. An easy way to deal with this problem is to simply move one of the particles

slightly when it is detected that they are in the same place.

6.5.3 Scaling

SPH can not simply be rescaled in space and time. It requires parameter tuning, which

can be difficult. The parameters that need to be tuned are mainly the radius of the

particles, r, the time step, dt, the smoothing length, h, and the gas constant, k. This

has been a particularly difficult problem, since there are no absolute guidelines to how

to determine these parameters.

The main problem setting to be modelled was a water jet hitting a plane surface from

a distance of 25 cm above the surface. The jet originating from an inlet with diameter

3 cm. To be able to fit enough particles inside the inlet the particle radius was set to

Page 47: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Implementation 33

∼ 1− 3 mm. These small sizes meant a need for a really small time step, in the order of

∼ 0.0001 s. However the simulations with these parameter values didn’t look right and

were difficult to stabilize. Therefore the problem was scaled up 10 times. With an inlet

diameter of 30 cm, particle radius of 1 cm and a time step of 0.003 s the simulations

looked right and were stable.

6.5.4 Incompressibility

The biggest issue with the SPH method is the level of compressibility. Originally the

method was used for computations of compressible fluids and has since been adopted

for incompressible flow. For implementations in video games the compressibility is of

no concern as long as the simulations look convincing. However when used for other

purposes the compressibility can be a problem. Efforts have been made to enforce

incompressibility or to minimize it. For example, in [17] Markus Becker and Matthias

Teschner present a weakly compressible form of the SPH method for fluid flow based on

the Tait equation. Enforcing incompressibility means adding extra computations and

usually a smaller time step.

Page 48: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

7. Rendering

7.1 Our implementation

In this thesis the rendering has been done by using the built-in MATLAB function plot. No

specific rendering technique has been implemented. This is because the implementation

was done in MATLAB, which does not provide any suitable graphics tools for rendering

water in a real-world manner.

There are however, different methods available for rendering fluid surfaces and we will

here describe a surface tracking technique and some of the most common techniques

available for rendering fluid surfaces.

7.2 Surface tracking

The colour field, cs, and its gradient field, n = ∇cs, introduced in Section 5.4.4.2 can be

used to track the surface. A particle is identified as a surface particle if

|n(ri)| > l, (7.1)

where l is a threshold parameter.

7.3 Marching cubes

One way of visualizing the surface is by rendering an iso surface of the colour field, cs.

If you have a field with scalar values, rendering an iso surface means you show a surface

34

Page 49: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Rendering 35

where the scalar values are the same. Each point in space is either inside or outside of

the surface.

Marching Cubes is a contouring algorithm to create surfaces of constant scalar value

in 3D [18]. The algorithm proceeds through the scalar field, taking eight neighbour

locations at a time. The eight values form the eight corners of an imaginary cube. Then

the polygon/polygons needed to represent the part of the iso surface that passes through

the cube are determined. The individual polygons are then fused into the desired surface.

The concept of marching cubes are illustrated in figure 7.1, where the yellow corners of

the cubes represent points inside the surface. The rest of the corners are outside of the

surface and surface elements are shown inside the cubes.

Figure 7.1: The different cube configurations used in the Marching cubes algorithm[3].

7.4 Point Splatting

Point Splatting is a technique where every volume element is splatted onto the viewing

surface in back to front order. These splats are rendered as disks whose properties

(colour and transparency) vary diametrically in normal (Gaussian) manner.

7.5 Ray Casting

The basic idea of ray casting is to determine the value of each pixel in the image by

sending a ray through the pixel into the scene. Then evaluate the data encountered

along the ray using some specified function in order to compute the pixel value.

Page 50: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

8. Results

The goal with this project has been to implement a method used in video games fluid

flow simulations. When simulating flow in video games there are two important aspects.

The simulations need to be visually convincing and they need to be fast. It is of little or

no concern that simulations are physically accurate. When implementing this method

the focus has been mainly on the visual aspect, i.e. whether or not the simulations look

accurate.

8.1 Code structure

The structure of the code is illustrated in figure 8.1. The main function runSPH.m

initializes all the parameters, sets up the problem setting and calls the function SPH.m.

SPH.m initializes all the arrays containing mass, density, velocity etc. and starts the

iteration. In each iteration step new particles are initialized, the density, pressure and

forces are computed, the integration is performed, the limits are checked and the particles

positions are saved. There is a predetermined number of iterations. When they are

finished, the coordinate values are returned to runSPH.m which plots the particles and

stores the simulation.

36

Page 51: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 37

Compute Forces

Compute Pressure

Compute Density

Get Neighbours

Initialize Particles

SPH.m

runSPH.m

Compute Surface Tension

Integrate

Check Limits

Save Output

Plot Result

Figure 8.1: Main code structure.

Page 52: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 38

8.2 Simulations

The simulations in this section have used mainly the parameter values shown in table

8.1. If other values were used it is indicated in the text. All simulations have been

performed on a personal computer with an Intel Core i7-3630QM processor (4 cores),

2.40 GHz, 12.0 GB RAM and 64-bit operating system, Windows 8.1.

Description Symbol Value Unit

Rest Density ρ0 998.29 kg/m3

Particle Radius r 0.01 m

Inlet Radius R 0.15 m

Time Step dt 0.006 s

Dynamic Viscosity µ 3.5 Pa · s

Gas Stiffness Constant k 3 m2/s2

Coefficient of restitution cr 0.001 []

Surface Tension σ 0.0728 N/m

Smoothing Length h 1.3 · (m/ρ0)1/3 m

Initial Velocity v0 5 m/s

Surface Threshold l 7.065 []

Height above surface H 2.5 m

Table 8.1: Parameter values used in simulations.

8.2.1 Water jet straight down

8.2.1.1 With Surface Tension

This simulation was done with 40, 000 particles during 500 iterations. With dt = 0.006s

that is a total real-world time of 3.0s. The simulation took 8, 613s, that is 2 hours, 23

minutes and 33 seconds. In figure 8.2 the plots are shown from time t = 0.0s, t = 0.36s,

t = 0.72s, t = 1.08s, t = 1.44s and t = 1.80s.

The same simulation was run again after having used the built-in tool MATLAB Coder to

mex the files. This means that the files are first translated to C code and then compiled.

The compiled files are then used in the simulations. This is done in order to get a better

Page 53: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 39

performance. Because MATLAB code is compiled at runtime it is usually slower to run

than compiled C code. After mexing, the simulation took 7, 089s, that is 1 hours, 58

minutes and 9 seconds.

A simple check for accuracy was done using the equation

s = v0t+1

2at2, (8.1)

where s is the distance travelled, v0 the initial velocity, a the acceleration and t the

time. By noting the time it took for the first particle to hit the bottom surface then

using this to compute the distance travelled. This distance was calculated to be 2.54m.

In comparison with the actual distance 2.50m, the computed distance is off by 1.6%.

Figure 8.2: Simulation including surface tension at times; t = 0.0s, t = 0.36s, t =0.72s, t = 1.08s, t = 1.44s and t = 1.80s.

8.2.1.2 Without Surface Tension

In order to see how much the surface tension affects the simulation, the same simulation

was done once more but having removed the surface tension force as well as all the

computations it requires. This simulation was also done with 40, 000 particles during

500 iterations. With dt = 0.006s this is a total real-world time of 3.0s. The simulation

took 8, 616s, that is 2 hours, 23 minutes and 36 seconds. So there is no speed up in

comparison with the simulation with surface tension. In figure 8.3 the plots are shown

from time t = 0.0s, t = 0.36s, t = 0.72s, t = 1.08s, t = 1.44s and t = 1.80s. Comparing

the plots from the simulation with and without surface tension, there is very little

Page 54: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 40

difference in the two. This is most likely due to the fact that the water spreads out so

there is no thick fluid that produces an unambiguous surface.

For consistency this simulation was also done after mexing and the simulation time was

7, 372s, that is 2 hours, 2 minutes and 52 seconds.

Figure 8.3: Simulation not including surface tension at times; t = 0.0s, t = 0.36s,t = 0.72s, t = 1.08s, t = 1.44s and t = 1.80s.

Page 55: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 41

8.2.2 Water jet hitting an inclining plane

The problem setting was changed so that the bottom surface is subject to an inclination

angle, α, in this case α = 1°. This simulation was done with 40, 000 particles during

500 iterations. With dt = 0.006s that is a total real-world time of 3.0s. The simulation

took 5, 520s, that is 1 hours, 32 minutes and 0 seconds. After having mexed the files the

simulation was performed and took 4, 546s, that is 1 hours, 15 minutes and 46 seconds.

However at time t = 1.764s all the particles have left the simulation area. In figure 8.4

the plots are shown from time t = 0.27s, t = 0.54s, t = 0.81s, t = 1.08s, t = 1.35s and

t = 1.62s.

Figure 8.4: Simulation of inclining plane at times; t = 0.27s, t = 0.54s, t = 0.81s,t = 1.08s, t = 1.35s and t = 1.62s.

8.2.3 Dam break

This simulation was done with 11, 560 particles during 500 iterations. With dt = 0.003s

that is a total real-world time of 1.5s. The simulation area was a box of size 0.5× 0.5×

0.5m. The simulation took 3, 255s, that is 54 minutes and 15 seconds. After having

mexed the files the simulation was performed and took 1, 108s, that is 18 minutes and

28 seconds. In figure 8.5 the plots are shown from time t = 0.0s, t = 0.3s, t = 0.6s,

t = 0.9s, t = 1.2s and t = 1.5s.

To get a sense of how compressible the water is, the simulation was run for 2000 iterations

and at the end checking the height of the water level. Using this to compute the density

Page 56: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 42

as

ρ =m ·NV

, (8.2)

where m is the particle mass, N the total number of particles and V the volume of the

water. The density was computed to be 1940 kg/m3, which is 1.94 times as much as the

density of water, ρwater = 998.29 kg/m3. This clearly shows that the compressibility is

an issue in this implementation.

Figure 8.5: Simulation of a dam break at times; t = 0.0s, t = 0.3s, t = 0.6s, t = 0.9s,t = 1.2s, t = 1.5s.

Page 57: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 43

8.2.4 Water jet hitting a wall

This simulation was done with 40, 000 particles during 500 iterations. With dt = 0.006s

that is a total real-world time of 3.0s. The velocity was 10m/s and the smoothing length

was h = 1.69 · (m/ρ0)1/3. The simulation took 7, 421s, that is 2 hours, 3 minutes and 41

seconds. After having mexed the files the simulation was performed and took 5, 647s,

that is 1 hours, 34 minutes and 7 seconds. In figure 8.6 the plots are shown from time

t = 0.36s, t = 0.72s, t = 1.08s, t = 1.44s, t = 1.80s and t = 2.16s.

Figure 8.6: Simulation of a jet hitting a wall at times; t = 0.36s, t = 0.72s, t = 1.08s,t = 1.44s, t = 1.80s and t = 2.16s.

Page 58: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Results 44

8.2.5 Water jet in small scale

This simulation was done with 50, 000 particles during 1000 iterations. With dt =

0.0002s that is a total real-world time of 0.2s. The particles were initialized every sixth

iteration. The particle radius was r = 0.001m and the inlet radius was R = 0.015m and

the height above the surface was H = 0.25m. The coefficient of restitution was set to

zero, cr = 0.0. The simulation took 21, 576s, that is 5 hours, 59 minutes and 36 seconds.

After having mexed the files the simulation was performed and took 17, 446s, that is 4

hours, 50 minutes and 46 seconds. In figure 8.7 the plots are shown from time t = 0.00s,

t = 0.04s, t = 0.08s, t = 0.12s, t = 0.16s and t = 0.20s.

Figure 8.7: Simulation in small scale at times; t = 0.00s, t = 0.04s, t = 0.08s,t = 0.12s, t = 0.16s and t = 0.20s.

Comparing these plots with the ones in figure 8.2 and figure 8.3 it is clear that the

simulation in the small scale is more turbulent and not as smooth. Most likely this is

due to a poor choice in parameter values. It is worth to note that the smaller the particle

radius is, the smaller the time step needs to be and more iterations are needed so the

simulation will take longer time. Because the simulations become so slow it is very time

consuming to test different parameter values and the effect they have on the simulations.

Further investigation of the parameter values for this problem size will probably result

in more accurate simulations.

Page 59: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

9. Discussion

In this thesis the SPH method has been implemented as it is used in video games.

Realistic results have been achieved for larger scales. However, it has been difficult to

achieve plausible results in smaller scales. This is because some parameters need to be

tuned, but there are no guidelines to know how they should be chosen.

So we have noted that we cannot simply have the same ratio and rescale our problem

setting. This means that the method is not linear. In order to understand how different

parameter values affect the simulation it would be wise to change one parameter at a

time and observe the result or derive some theoretical support for the scaling.

In particle methods such as SPH, the most time consuming part of an implementation is

usually the neighbour search. Typically the neighbour search is done using a hash table.

However, using hash tables always includes a risk of hash collisions and you need to store

a big hash table. To avoid this our own neighbour search algorithm was implemented.

In order to decide whether or not the SPH formulation, as it is used in Video Games

today, is suitable for the simulation of the ROT a deeper analysis of the accuracy needs

to be made. A relevant way to do this would be to simulate the same problem setting

using an SPH implementation and comparing with the currently used software Fluent.

Since the SPH method does not ensure incompressibility, this needs to be taken into

account in a future implementation.

One reason that the simulations are so time consuming in our implementation is the size

of the arrays. The size is fixed and is equal to the total number of particles used in the

simulation. However in all the examples, except for the dam break, the particles that

leave the simulation area are removed. This means that the array size is unnecessarily

large, because all the particles will not be in the simulation at the same time. In MATLAB

45

Page 60: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Discussion 46

the arrays can be of variable size, but not if the code is to be mexed, i.e. translated to

C code.

The purpose of investigating alternative methods for simulating the flow of the ROT

was to explore if an increase in performance is possible. If performance is of importance

in these types of simulations, it is recommended to use the computational power of

the Graphical Processing Unit, GPU. Using the GPU will most likely speed up the

simulation substantially. The algorithm could probably also achieve an additional speed

up by exploiting the parallelism of the SPH method.

Page 61: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

10. Conclusion

The purpose of this project was to investigate whether or not the techniques used in

video games for simulating fluids could be used for simulating the cooling process of the

Runout Table (ROT). The method chosen and implemented was the Smoothed Particle

Hydrodynamics (SPH) method. The implementation did not reach a level of complexity

where it would make sense to compare the simulation against those in the currently used

software Fluent. This is mainly due to lack of time. Because of this we can not come to

the conclusion that SPH would be useful for simulating the ROT nor that it wouldn’t

be. We can only conclude that more research is needed.

It has been noted that the SPH method is not easily rescaled. In order to rescale an

implementation the parameter values need to be adjusted and there are no guidelines to

follow in order to know how to choose good parameter values.

However, comparing the simulation time of our implementation with that of Fluent, our

implementation is somewhere between 10− 100 times faster.

SPH is definitely a promising method for simulating fluid and has been used in a wide

variety of implementations and there are different techniques and methods when im-

plementing SPH. When applied in the field of science the accuracy is more important

and the performance is secondary, so the implementation is more focused on accuracy.

However, when applied in Video Games the performance is utmost important but the

accuracy is less important and so the focus is on performance. How much worse the

accuracy is remains to be investigated.

47

Page 62: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

A. Kernels

Wpoly6(r, h) =315

64πh9

(h2 − ||r||2)3 0 ≤ ||r|| ≤ h

0 otherwise

(A.1)

∇Wpoly6(r, h) = − 945

32πh9

(h2 − ||r||2)2r 0 ≤ ||r|| ≤ h

0 otherwise

(A.2)

∇2Wpoly6(r, h) = − 945

32πh9

(h2 − ||r||2)(3h2 − 7||r||2) 0 ≤ ||r|| ≤ h

0 otherwise

(A.3)

Wspiky(r, h) =15

πh6

(h− ||r||)3 0 ≤ ||r|| ≤ h

0 otherwise

(A.4)

∇Wspiky(r, h) = − 45

πh6

r

||r||(h− ||r||)2 0 < ||r|| ≤ h

0 otherwise

(A.5)

∇2Wspiky(r, h) =90

πh6

1

||r||(h− ||r||)(h− 2||r||) 0 < ||r|| ≤ h

0 otherwise

(A.6)

Wviscosity(r, h) =15

2πh3

−||r||32h3

+ ||r||2h2

+ h2||r|| − 1 0 < ||r|| ≤ h

0 otherwise

(A.7)

∇Wviscosity(r, h) =15

2πh3

r(−3||r||3

2h3+ 2

h2− h

2||r||3

)0 ≤ ||r|| ≤ h

0 otherwise

(A.8)

∇2Wviscosity(r, h) =45

πh6

(h− ||r||) 0 ≤ ||r|| ≤ h

0 otherwise

(A.9)

48

Page 63: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Bibliography

[1] Matthias Muller, David Charypar, and Markus Gross. Particle-based fluid simula-

tion for interactive applications. In Proceedings of the 2003 ACM SIGGRAPH/Eu-

rographics symposium on Computer animation, pages 154–159. Eurographics Asso-

ciation, 2003.

[2] URL http://upload.wikimedia.org/wikipedia/commons/7/7d/Hash_table_3_

1_1_0_1_0_0_SP.svg.

[3] URL http://upload.wikimedia.org/wikipedia/commons/a/a7/

MarchingCubes.svg.

[4] Bengt Andersson, Ronnie Andersson, Love Hakansson, Mikael Mortensen, Rahman

Sudiyo, and Berend van Wachem. Computational Fluid Dynamics for Engineers.

Cambridge University Press, 2012.

[5] Jos Stam. Stable fluids. In Proceedings of the 26th annual conference on Computer

graphics and interactive techniques, pages 121–128. ACM Press/Addison-Wesley

Publishing Co., 1999.

[6] Michael J Gourlay. Fluid simulation for video games. Intel online articles, 2012.

[7] Youngmin Kwak. Advanced liquid simulation techniques for computer graphics ap-

plications. PhD thesis, University of Southern California, 2010.

[8] Bart Adams and Martin Wicke. Meshless approximation methods and applications

in physics based modeling and animation. In Eurographics 2009-Tutorials, pages

213–239. The Eurographics Association, 2009.

[9] J J Monaghan. Smoothed particle hydrodynamics. Reports on Progress in Physics,

68(8):1703, 2005. URL http://stacks.iop.org/0034-4885/68/i=8/a=R01.

49

Page 64: Video Games Fluid Flow Simulations Towards Automationuu.diva-portal.org/smash/get/diva2:703754/FULLTEXT01.pdf · UPTEC-F-14008 Examensarbete 30 hp Mars 2014 Video Games Fluid Flow

Bibliography 50

[10] J.J. Monaghan. Smoothed particle hydrodynamics and its diverse applica-

tions. Annual Review of Fluid Mechanics, 44(1):323–346, 2012. doi: 10.1146/

annurev-fluid-120710-101220. URL http://www.annualreviews.org/doi/abs/

10.1146/annurev-fluid-120710-101220.

[11] Jos Stam and Eugene Fiume. Depicting fire and other gaseous phenomena using

diffusion processes. In Proceedings of the 22nd Annual Conference on Computer

Graphics and Interactive Techniques, pages 129–136. ACM, 1995.

[12] Mathieu Desbrun and Marie-Paule Gascuel. Smoothed particles: A new paradigm

for animating highly deformable bodies. In Computer Animation and Simulation’96,

pages 61–76. Springer, 1996.

[13] Micky Kelager. Lagrangian fluid dynamics using smoothed particle hydrodynamics.

Master’s thesis, University of Copenhagen, 2006. URL http://image.diku.dk/

projects/media/kelager.06.pdf.

[14] Fabrice Colin, Richard Egli, and Feng Ying Lin. Computing a null divergence

velocity field using smoothed particle hydrodynamics. Journal of Computational

Physics, 217(2):680–692, 2006.

[15] Matthias Muller, Bruno Teschner, Matthias Heidelberger, Danat Pomeranets, and

Markus Gross. Optimized spatial hashing for collision detection of deformable ob-

jects. Technical report, Computer Graphics Laboratory, ETH Zurich, Switzerland,

2003.

[16] Hagit Schechter and Robert Bridson. Ghost SPH for animating water. ACM Trans-

actions on Graphics (TOG), 31(4):61, 2012.

[17] Markus Becker and Matthias Teschner. Weakly compressible SPH for free surface

flows. In Proceedings of the 2007 ACM SIGGRAPH/Eurographics symposium on

Computer Animation, pages 209–217. Eurographics Association, 2007.

[18] Will Schroeder, Ken Martin, and Bill Lorensen. An Object-Oriented Approach To

3D Graphics. Prentice Hall, 1997.