Kalland Master

Embed Size (px)

Citation preview

  • 8/7/2019 Kalland Master

    1/142

    A Navier-Stokes Solver for Single- andTwo-Phase Flow

    by

    Kim Motoyoshi Kalland

    THESIS

    for the degree of MASTER OF SCIENCE

    (Master i Anvendt matematikk og mekanikk)

    Faculty of Mathematics and Natural Sciences

    University of Oslo

    September 2008

    Det matematisk- naturvitenskapelige fakultetUniversitetet i Oslo

  • 8/7/2019 Kalland Master

    2/142

  • 8/7/2019 Kalland Master

    3/142

    Contents

    1 Introduction 11

    1.1 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.2 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    2 Navier-Stokes equations 152.1 Conservation of mass . . . . . . . . . . . . . . . . . . . . . . . 162.2 Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.3 Pressure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.4 Viscosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.5 External forces . . . . . . . . . . . . . . . . . . . . . . . . . . 242.6 Other forces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    3 The projection method 253.1 Velocity boundary conditions . . . . . . . . . . . . . . . . . . 293.1.1 No-slip . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.1.2 Symmetry . . . . . . . . . . . . . . . . . . . . . . . . . 293.1.3 Inlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.1.4 Outlet . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.1.5 Prescribed pressure . . . . . . . . . . . . . . . . . . . . 30

    3.2 Poisson boundary conditions . . . . . . . . . . . . . . . . . . 31

    4 Discretisation 334.1 Staggered grid . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    4.2 Ghost cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.3 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.4 Calculating the tentative velocity . . . . . . . . . . . . . . . . 36

    4.4.1 Density . . . . . . . . . . . . . . . . . . . . . . . . . . 384.4.2 Convection . . . . . . . . . . . . . . . . . . . . . . . . 384.4.3 Pressure gradient . . . . . . . . . . . . . . . . . . . . . 404.4.4 Viscosity . . . . . . . . . . . . . . . . . . . . . . . . . 41

    4.5 The pressure Poisson equation . . . . . . . . . . . . . . . . . 424.5.1 Boundary conditions . . . . . . . . . . . . . . . . . . . 42

    4.6 Correcting pressure and velocity . . . . . . . . . . . . . . . . 43

    3

  • 8/7/2019 Kalland Master

    4/142

    4.7 Velocity boundary conditions . . . . . . . . . . . . . . . . . . 44

    4.7.1 Dirichlet boundary conditions . . . . . . . . . . . . . . 444.7.2 Neumann boundary conditions . . . . . . . . . . . . . 454.7.3 Prescribed pressure . . . . . . . . . . . . . . . . . . . . 474.7.4 Obstacle boundary conditions . . . . . . . . . . . . . . 48

    4.8 Time-step restriction . . . . . . . . . . . . . . . . . . . . . . . 494.9 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504.10 Data s tructures . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    4.10.1 The mask array . . . . . . . . . . . . . . . . . . . . . . 51Interior cells . . . . . . . . . . . . . . . . . . . . . . . 51Ghost cells . . . . . . . . . . . . . . . . . . . . . . . . 52

    5 Volume-of-uid method 535.1 Density and viscosity smoothing . . . . . . . . . . . . . . . . 545.2 Boundary conditions . . . . . . . . . . . . . . . . . . . . . . . 55

    5.2.1 Contact angle . . . . . . . . . . . . . . . . . . . . . . . 565.2.2 Inlets . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

    5.3 Piecewise linear interface construction . . . . . . . . . . . . . 565.3.1 Representing a reconstructed interface . . . . . . . . . 575.3.2 Calculating the volume fraction . . . . . . . . . . . . . 585.3.3 Reconstruction when the normal is known . . . . . . . 675.3.4 Estimating the normal with ELVIRA . . . . . . . . . . 71

    5.4 Advection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765.4.1 Time-step restriction . . . . . . . . . . . . . . . . . . . 78

    5.5 Surface tension . . . . . . . . . . . . . . . . . . . . . . . . . . 805.5.1 Continuum surface force . . . . . . . . . . . . . . . . . 825.5.2 Direction averaged curvature . . . . . . . . . . . . . . 845.5.3 Direction averaged curvature with renement . . . . . 875.5.4 Time-step restriction . . . . . . . . . . . . . . . . . . . 92

    6 Verication 936.1 Channel ow . . . . . . . . . . . . . . . . . . . . . . . . . . . 946.2 Pressure driven ow . . . . . . . . . . . . . . . . . . . . . . . 946.3 Square cavity . . . . . . . . . . . . . . . . . . . . . . . . . . . 956.4 Backward facing step . . . . . . . . . . . . . . . . . . . . . . . 976.5 Rising bubble . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026.6 Falling droplet . . . . . . . . . . . . . . . . . . . . . . . . . . 1086.7 Static drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1086.8 Rayleigh-Taylor instability . . . . . . . . . . . . . . . . . . . . 1116.9 Pressure Poisson equation . . . . . . . . . . . . . . . . . . . . 1186.10 Zalesaks rotating disc . . . . . . . . . . . . . . . . . . . . . . 1186.11 Prol ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

    4

  • 8/7/2019 Kalland Master

    5/142

    7 Tutorial 125

    7.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1257.2 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . 1267.3 How do you do, Tellus! . . . . . . . . . . . . . . . . . . . . . . 1277.4 Fluid properties . . . . . . . . . . . . . . . . . . . . . . . . . . 1287.5 Initial and boundary conditions . . . . . . . . . . . . . . . . . 1297.6 Two-phase ow . . . . . . . . . . . . . . . . . . . . . . . . . . 1307.7 Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.8 Tracer uid . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.9 Loading and saving . . . . . . . . . . . . . . . . . . . . . . . . 1317.10 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327.11 Example les . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

    8 Conclusion 135

    5

  • 8/7/2019 Kalland Master

    6/142

    6

  • 8/7/2019 Kalland Master

    7/142

    List of Tables

    1.1 Units of measurement used for uid properties. . . . . . . . . 13

    3.1 Velocity boundary conditions. . . . . . . . . . . . . . . . . . . 304.1 Array sizes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    6.1 Non-dimensionalised coefficients. . . . . . . . . . . . . . . . . 966.2 Results from the channel ow test. . . . . . . . . . . . . . . . 966.3 Results from the pressure driven ow test. . . . . . . . . . . . 966.4 Maximum stream function values from the square cavity test. 966.5 Minimum stream function values from the square cavity test. 1006.6 Backward facing step attachment and detachment lengths. . . 1036.7 Maximum velocity in the static drop test. . . . . . . . . . . . 1156.8 Pressure difference and pressure error in the static drop test. 1156.9 Proling results. . . . . . . . . . . . . . . . . . . . . . . . . . 124

    7.1 Valid combination of boundary conditions (north and south). 1307.2 Valid combination of boundary conditions (east and west). . . 130

    7

  • 8/7/2019 Kalland Master

    8/142

    8

  • 8/7/2019 Kalland Master

    9/142

    List of Figures

    2.1 Volume passing through a surface. . . . . . . . . . . . . . . . 162.2 Flow through the faces of a cube. . . . . . . . . . . . . . . . . 17

    2.3 Control volume with face centred pressure values. . . . . . . . 202.4 Control volume with face centred stress vectors . . . . . . . . 22

    4.1 Non-staggered grid. . . . . . . . . . . . . . . . . . . . . . . . . 344.2 Staggered grid. . . . . . . . . . . . . . . . . . . . . . . . . . . 354.3 Ghost cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364.4 Node indexing. . . . . . . . . . . . . . . . . . . . . . . . . . . 374.5 Discretisation of the convective term. . . . . . . . . . . . . . . 404.6 Discretisation of the pressure gradient term. . . . . . . . . . . 414.7 Discretisation of the viscosity term. . . . . . . . . . . . . . . . 424.8 Dirichlet boundary condition for v on the west boundary. . . 45

    4.9 Neumann boundary condition for v on the west boundary. . . 464.10 Stencil for the tentative velocity on the boundary. . . . . . . 484.11 Velocity on obstacle boundaries. . . . . . . . . . . . . . . . . 494.12 The mask array. . . . . . . . . . . . . . . . . . . . . . . . . . 52

    5.1 Colour function values for an arbitrary interface. . . . . . . . 545.2 Piecewise linear interface construction. . . . . . . . . . . . . . 575.3 Representation of a reconstructed interface. . . . . . . . . . . 585.4 Linear interface in 1D. . . . . . . . . . . . . . . . . . . . . . . 595.5 Reducing a line to a point. . . . . . . . . . . . . . . . . . . . . 605.6 Triangle area A(x, y). . . . . . . . . . . . . . . . . . . . . . . 61

    5.7 Calculating the area of dark uid in a cell. . . . . . . . . . . . 625.8 Reducing a plane to a line. . . . . . . . . . . . . . . . . . . . 635.9 Tetrahedron volume V (x, y, z). . . . . . . . . . . . . . . . . . 655.10 Calculating the volume of dark uid in a cell. . . . . . . . . . 665.11 Piecewise second degree polynomial f (d). . . . . . . . . . . . 685.12 Histograms in the ELVIRA algorithm. . . . . . . . . . . . . . 715.13 Mirroring of column histogram in the ELVIRA algorithm. . . 725.14 Mirroring of row histogram in the ELVIRA algorithm. . . . . 735.15 Possible slopes in the ELVIRA algorithm . . . . . . . . . . . 745.16 The normal corresponding to a given slope and inverse slope. 74

    9

  • 8/7/2019 Kalland Master

    10/142

    5.17 Flipping the normal in the ELVIRA algorithm. . . . . . . . . 74

    5.18 Calculating the interface error in the ELVIRA algorithm. . . 755.19 Horizontal advection. . . . . . . . . . . . . . . . . . . . . . . . 775.20 Naive advection. . . . . . . . . . . . . . . . . . . . . . . . . . 795.21 Overlapping donating regions. . . . . . . . . . . . . . . . . . . 795.22 Calculating a histogram in the DAC method. . . . . . . . . . 855.23 The integral of a semi circle h(x). . . . . . . . . . . . . . . . . 88

    6.1 Streamlines in square cavity test for Re = 1000, 64 64 cells. 986.2 Streamlines in square cavity test for Re = 1000, 64 64 cells. 996.3 Square cavity test for Re = 1, 128 128 cells. . . . . . . . . . 1006.4 Square cavity test for Re = 10, 128 128 cells. . . . . . . . . 1006.5 Square cavity test for Re = 100, 128 128 cells. . . . . . . . . 1016.6 Square cavity test for Re = 1000, 128 128 cells. . . . . . . . 1016.7 Square cavity test for Re = 5000, 128 128 cells. . . . . . . . 1036.8 Streamlines in the backward facing step test. . . . . . . . . . 1046.9 Backward facing step geometry. . . . . . . . . . . . . . . . . . 1056.10 Attachment point . . . . . . . . . . . . . . . . . . . . . . . . . 1056.11 Detachment point . . . . . . . . . . . . . . . . . . . . . . . . 1056.12 Rising bubble at t = 0 .5 for different grid resolutions. . . . . . 1066.13 The speed of the rising bubbles mass centre. . . . . . . . . . 1076.14 Falling droplet at different time levels. . . . . . . . . . . . . . 1096.15 Falling droplet at different time levels, close to the surface. . 110

    6.16 Static drop interface after 200 time steps. . . . . . . . . . . . 1126.17 Static drop pressure after 200 time steps. . . . . . . . . . . . 1136.18 Static drop curvature after 200 time steps. . . . . . . . . . . . 1146.19 Static drop velocity after 200 time steps for CSF. . . . . . . . 1156.20 Static drop velocity after 200 time steps for DAC. . . . . . . 1166.21 Static drop velocity after 200 time steps for DAC with rene-

    ment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176.22 Rayleigh-Taylor instability at different time levels. . . . . . . 1196.23 Zalesaks rotating disc in a 50 50 grid. . . . . . . . . . . . . 1216.24 Zalesaks rotating disc in a 100 100 grid. . . . . . . . . . . . 1216.25 Zalesaks rotating disc in a 150 150 grid. . . . . . . . . . . . 1226.26 Zalesaks rotating disc in a 200 200 grid. . . . . . . . . . . . 122

    10

  • 8/7/2019 Kalland Master

    11/142

    Chapter 1

    Introduction

    In this masters thesis, I have implemented a 2D Navier-Stokes solver, docu-mented in detail the numerical methods used, explained how the solver worksand how it can be used to solve ow problems. The Navier-Stokes equationshave been solved numerically since the 1960s, and consequently there existslots of codes. Most of these are commercial, the best known being Fluent,STAR-CD and ANSYS CFX, which have proved to tackle very complicateduid dynamics problems. One problem with these commercial codes is thedifficulty in modifying the source code and make new software interactingwith the codes. The user interface is also geared towards comprehensive

    GUIs.There are surprisingly few open source codes for solving the incompress-ible Navier-Stokes equations. OpenFOAM is perhaps the best known opensource code in this category. The code is huge and requires the user to pro-gram in C++ to solve a new problem. LifeV is a similar package. FEAT-FLOW is another free alternative, written in Fortran 77. The mentionedopen source codes have a quite steep learning curve and aim at professionalactivity in computational uid dynamics (CFD). For educational purposes,and for numerical methods research, it is handy to have a quite simple code,written in a high-level (Matlab-like) language, supplied with a documen-tation of all numerical methods and tricks in the code (this latter point

    is missing in the literature and in most of the available codes I have seen).With such a simple code, one can learn the solution technology and quiteeasily extend the program to more advanced problems. This experience maybe very valuable before approaching professional CFD codes.

    There is in fact already available an open source Navier-Stokes solverNaSt2D [9] of the type I have implemented. Actually, a fair amount of whatI have implemented is based on the book [9]. However, I would say thatthe solver I present here is more generic, more user friendly, more up-to-date by using C++ and Python instead of C. On the other hand, it alsolacks some features included in NaSt2D. My solver is set up and run from

    11

  • 8/7/2019 Kalland Master

    12/142

    a Python script, which gives much more control and power, but requires

    some programming skills from the user. Hopefully, this text will show howsimple the programming can be to solve new problems with this solver.

    In section 5.5.3, I introduce a new method to reduce spurious currentscaused by inaccurate surface tension. The method is built on the directionaveraged curvature method described in [17, 16].

    The solver is restricted to rectangular domains, and the discretisation isbased on nite differences. Both single- and two-phase laminar, incompress-ible ow can be simulated.

    1.1 Symbols

    In general, I will explain the symbols in the text where they are used so thatthe reader does not have to turn pages back and forth, but there are somesymbols used throughout the text that I will explain here.

    x : space variable in 2D or 3D. x,y,z : the components of the space variable mentioned above. t: time variable. p or p(x , t ): pressure in the uid as function of space and time.

    u or u (x , t ): uid velocity as function of space and time. u,v,w or u(x , t ), v(x , t ), w(x , t ): the components of the velocity func-tion mentioned above. or (x , t ): viscosity as function of space and time. or (x , t ): density as function of space and time. : surface tension coefficient.Boldface, italicised symbols represent matrices if written in upper-case

    and vectors or vector functions if written in lower-case. Scalar values andfunctions are represented by italicised symbols.

    1.2 Operators

    In this text, I write equations both in 2D component form and in vector form.When the same equations are expressed both in component and vector form,I put boxes around each of the forms to group the equations. If the readerdoesnt have much experience with the (del) operator, I believe it is easierto follow the equations in component form. However, these equations will

    12

  • 8/7/2019 Kalland Master

    13/142

    Property Symbol Unit of measurement

    Velocity u m/s metres per secondPressure p Pa=N/m 2 pascalDensity kg/m 3 kilograms per cubic metreDynamic viscosity Pa s pascal secondsKinematic viscosity m2/s sq. metres per sec.Surface tension coeff. N/m newton per metreGravitational acc. g m/s 2 metres per sec. squaredTime t s seconds

    Table 1.1: Units of measurement used for uid properties.

    only be valid in 2D. The operator is dened as = [ x , y ]T in 2D and= [ x ,

    y ,

    z ]

    T in 3D. The equations in vector form are valid in 1D, 2Dand 3D, they are more compact and generally easier to understand andremember.

    13

  • 8/7/2019 Kalland Master

    14/142

    14

  • 8/7/2019 Kalland Master

    15/142

    Chapter 2

    Navier-Stokes equations

    Navier-Stokes equations are a set of partial differential equations that de-scribe the motion of uids as a relationship between ow velocity (or mo-mentum) and pressure. The uid can be a gas or a liquid.

    The general Navier-Stokes equations can be written as:

    ut

    + u u = p + T + f (2.1)t

    + (u ) = 0 (2.2)where

    = density u = velocity p = pressure T = stress tensor f = sum of external forcesThe momentum equation (2.1) is equivalent to Newtons second law of

    motion. The left hand side of the equation is the product of density andacceleration, the right hand side is the sum of forces per volume acting ona uid particle, an innitesimal uid volume.

    The continuity equation (2.2) ensures conservation of mass; mass cannotappear out of or disappear into thin air.

    All the variables above are functions of time t and space ( x,y,z ). Toavoid too many symbols, I omit writing the time variable when the time isxed, and I omit the space variables when the position is xed. The velocityvector u is sometimes split into its components u,v,w , and the stress tensormatrix T into its rows T x , T y , T z or elements T i,j , 1 i, j 3. The 2D casefollows analogously.

    In the following sections, each of the terms in the equations is explained.

    15

  • 8/7/2019 Kalland Master

    16/142

    x z

    y

    x

    y

    n

    u t

    Figure 2.1: Volume passing through a surface during a time step t.

    2.1 Conservation of mass

    t

    + (u ) (2.3)

    The amount of mass owing through a surface during a time interval isgiven by:

    t 1t 0 A u n dA dt (2.4)where A is the surface, n is the surface normal, t0 is the initial time and t1is the nal time.

    The above integral can be approximated with the product:

    t |A| u n (2.5)

    where t = ( t1 t0) and |A| is the surface area, and and u are the densityand velocity in the middle of the surface (see gure 2.1).We look at a small cuboid volume with size x y z. The netamount of mass owing out of the volume must equal the mass loss inside

    the volume. To nd the net amount of mass owing out of the volume, wemust sum together the mass owing through each of the six faces (see gure2.2).

    16

  • 8/7/2019 Kalland Master

    17/142

    y x

    z

    x y

    z

    u(x+ x/2,y,z)

    u(x x/2,y,z)

    u(x,y+ y/2,z)

    u(x,y y/2,z)

    u(x,y,z z/2)

    u(x,y,z+ z/2)

    Figure 2.2: Flow through the faces of a cube.

    m = tyz(e x (u )(x + x/ 2,y, z ) + ( e x ) (u )(x x/ 2,y, z ))+ tzx (e y (u )(x, y + y/ 2, z) + ( e y) (u )(x, y y/ 2, z))+ txy(e z (u )(x,y,z + z/ 2) + ( e z ) (u )(x,y,z z/ 2))= tyz((u)(x + x/ 2,y, z ) (u)(x x/ 2,y, z ))+ txz ((v)(x, y + y/ 2, z) (v)(x, y y/ 2, z))+ txy((w)(x,y,z + z/ 2) (w)(x,y,z z/ 2))

    where m is the mass loss and ( u )(x,y,z ) is the same as u (x,y,z )(x,y,z ).The mass loss inside the volume can be expressed as:

    ((x,y,z , t 1)

    (x,y,z , t 0))d (2.6)

    where is the volume, t0 is the initial time and t1 is the nal time.This can be approximated by

    m = ||((t1) (t0)) = xyz((t1) (t0)) (2.7)

    where || = xyz is the volume size and is the density in the middle of the volume.

    17

  • 8/7/2019 Kalland Master

    18/142

  • 8/7/2019 Kalland Master

    19/142

    u (x,y,z , t ) be the velocity eld, that is, the function u will return the ve-

    locity of a uid particle at any given time and position. Since the particlevelocity v (t) is the time derivative of the position q (t), we must have

    v (t) = q (t) = [qx (t), qy(t), qz (t)]T (2.11)

    By the denition of the velocity eld, we also have

    v (t) = u (qx (t), qy(t), qz (t), t ) (2.12)

    The particle acceleration a (t) is the time derivative of the velocity v (t):

    a (t) = v (t) =ddt

    u (qx (t), qy(t), qz (t), t ) (2.13)

    Differentiate the velocity eld function u with respect to time t:

    v (t) = ddt

    u (qx (t), qy(t), qz (t), t )

    = limst

    1s t

    (u (qx (s), qy(s), qz (s), s ) u (qx (t), qy(t), qz (t), t ))= lim

    st

    1s t

    (u (qx (s), qy(s), qz (s), s) u (qx (s), qy(s), qz (s), t ))+

    1s t

    (u (qx (s), qy(s), qz (s), t ) u (qx (s), qy(s), qz (t), t ))+

    1s t

    (u (qx (s), qy(s), qz (t), t ) u (qx (s), qy(t), qz (t), t ))

    +1

    s t (u (qx (s), qy(t), qz (t), t ) u (qx (t), qy(t), qz (t), t ))= lim

    st

    u (qx (s), qy(s), qz (s), s ) u (qx (s), qy(s), qz (s), t )s t

    +qz (s) qz (t)

    s t u (qx (s), qy(s), qz (s), t ) u (qx (s), qy(s), qz (t), t )

    qz (s) qz (t)+

    qy(s) qy(t)s t

    u (qx (s), qy(s), qz (t), t ) u (qx (s), qy(t), qz (t), t )qy(s) qy(t)

    +qx (s) qx (t)

    s t u (qx (s), qy(t), qz (t), t ) u (qx (t), qy(t), qz (t), t )

    qx (s) qx (t)=

    ut +

    uz

    qzt +

    uy

    qyt +

    ux

    qxt

    The result can also be obtained by using the chain rule for several vari-ables. Since qt is the velocity, we can replace it with u .

    v (t) =ddt

    u (qx (t), qy(t), qz (t), t )

    = ux

    qxt

    + uy

    qyt

    + uz

    qzt

    + ut

    = ux

    u + uy

    v + uz

    w + ut

    = u u + ut

    19

  • 8/7/2019 Kalland Master

    20/142

    y x

    z

    x y

    z

    p(x+ x/2,y,z)

    p(x x/2,y,z)

    p(x,y+ y/2,z)

    p(x,y y/2,z)

    p(x,y,z z/2)

    p(x,y,z+ z/2)

    Figure 2.3: Control volume with face centred pressure values.

    This shows how the left hand side of the momentum equation (2.1) isderived.

    The 2D case is similar. By multiplying the acceleration with the density, we get the same expression as the left hand side of equation (2.1).

    2.3 Pressure

    p (2.14)

    Let p(t,x,y,z ) be the pressure eld. To nd the pressure force acting ona particle, we construct a small, cuboid volume with size x y z. Thevolume and the direction of the pressure forces are shown in gure 2.3.

    Pressure is an amount of force per area, so to nd the force, we must in-tegrate the pressure over the area of each face. The integral is approximatelythe product of the area and the face centred pressure.

    By summing the contribution from all six faces, we get the net pressureforce. Since we are interested in the force per volume, we divide by xyz.

    20

  • 8/7/2019 Kalland Master

    21/142

    Let f p be the pressure force per volume.

    f p(x,y,z ) =yz

    xyz(e x ) p(x + x/ 2,y, z ) +

    yzxyz

    e x p(x x/ 2,y, z )+

    zxxyz

    (e y) p(x, y + y/ 2, z) +zx

    xyze y p(x, y y/ 2, z)

    +xy

    xyz(e z ) p(x,y,z + z/ 2) +

    xyxyz

    e z p(x,y,z z/ 2)

    = 1

    x ( p(x + x/ 2,y, z ) p(x x/ 2,y, z ))1y ( p(x, y + y/ 2, z) p(x, y y/ 2, z))1z

    ( p(x,y,z + z/ 2)

    p(x,y,z

    z/ 2))

    where e x etc. are the unit vectors along each axis. As x, y and z approachzero, we get:

    f p(x,y,z ) = pxpypz

    = p (2.15)

    which is equal to the pressure term in the momentum equation (2.1).

    2.4 Viscosity

    T (2.16)

    Viscosity is the friction within the uid and depends on velocity dif-ferences, or the deformation rate. The viscous stress acting on a surfacedepends on the surface orientation and can be expressed as a product of thestress tensor matrix T and the surface normal vector n . In gure 2.4, theviscous stress is shown for each face.

    Stress is, like pressure, an amount of force per area. The net viscousforce exerted on the volume is found the same way as for pressure. Since weare interested in the force per volume, we divide by xyz. Let f v be theviscous force per volume.

    21

  • 8/7/2019 Kalland Master

    22/142

    y x

    z

    x y

    z

    T x(x+ x/2,y,z)

    T x(x x/2,y,z)

    T y(x,y+ y/2,z)

    T y(x,y y/2,z)

    T z(x,y,z z/2)

    T z(x,y,z+ z/2)

    Figure 2.4: Control volume with face centred stress vectors decomposed intox, y and z-components.

    f v(x,y,z ) =yz

    xyzT x (x + x/ 2,y, z )

    yzxyz

    T x (x x/ 2,y, z )+ zx

    xyzT y(x, y + y/ 2, z) zxxyz T y(x, y y/ 2, z)

    +xy

    xyzT z (x,y,z + z/ 2)

    xyxyz

    T z (x,y,z z/ 2)=

    T x (x + x/ 2,y, z ) T x (x x/ 2,y, z )x

    +T y(x, y + y/ 2, z) T y(x, y y/ 2, z)

    y

    +T z (x,y,z + z/ 2) T z (x,y,z z/ 2)

    z

    where T x is the transpose of the rst row of T etc.As x, y and z approach zero, we get:

    f v(x,y,z ) = T xx

    + T yy

    + T zz

    = T (2.17)

    which is equal to the viscosity term in the momentum equation (2.1).Many uids can be modelled as Newtonian uids. Newtonian uids are

    dened as uids where the viscous stress is proportional to the deformation

    22

  • 8/7/2019 Kalland Master

    23/142

  • 8/7/2019 Kalland Master

    24/142

    2.5 External forces

    f (2.18)

    External forces are forces that are not caused by the interaction betweenuid particles. Examples are gravity, electromagnetic force, centrifugal forceand Coriolis force.

    For gravity, the force is the product of mass and the gravitational accel-eration. The force per volume is thus the density times the gravitationalacceleration g .

    2.6 Other forces

    It is possible to append even more forces to the right hand side of equation(2.1). In multi-phase ow, surface tension is one such force. See section 5.5for more information on surface tension.

    24

  • 8/7/2019 Kalland Master

    25/142

    Chapter 3

    The projection method

    Since there is no known analytical solution of the momentum and continuityequations, they must be solved numerically. If the uid is incompressibleand Newtonian, the equations can be written as:

    ut

    + u u = p + ( u ) + ( u )T + f (3.1)

    u = 0 (3.2)Discretise the momentum equation with respect to time using an explicit

    scheme (forward difference). In an explicit scheme, the following approxi-mates the time derivative:

    ut

    n

    u n +1 u n

    t(3.3)

    where the superscripts n and n + 1 indicate the time level, not exponents.The equations are discretised in space later in section 4. For the time being,it is easier to handle the continuous functions.

    The naive approach, which will not work, is to try to insert the timederivative approximation into equation (3.1):

    u n +1 u n t

    + u n u n = 1

    n pn

    +1

    n n ( u n ) + ( u n )T +

    1n

    f n(3.4)

    25

  • 8/7/2019 Kalland Master

    26/142

    un +1 un t + un u nx + vn u ny = 1n pnx + 2n x n ux+

    1n

    y

    nuy

    +vx

    +1

    nf nx (3.5)

    vn +1 vn t

    + unvn

    x+ vn

    v n

    y=

    1n

    pn

    y+

    1n

    x

    nuy

    +vx

    +2

    n

    yn

    vy

    +1

    nf ny (3.6)

    where u n +1 is the unknown velocity at the next time level, and u n and pnare the known velocity and pressure at the current time level. n and n

    are the known density and viscosity. t is the difference in time betweentime level n and n + 1.

    These equations are easy to solve for u n+1 . n +1 and n +1 are calculatedseparately as described in section 5. However, there are two problems withthe naive approach:

    The continuity equation u = 0 has not been applied, so in general

    u n+1 = 0.

    There is no way to nd the unknown pressure at the next time level, pn +1 , with the naive discretisation since it does not appear in theequations.

    Instead of scrapping the naive approach completely, modify the momen-tum equation slightly. Insert the unknown pressure pn +1 instead of theknown pressure pn . Now both the unknown velocity and the unknown pres-sure at the next time level appear:

    u n +1 u n t

    + u n u n = 1

    n pn +1

    +1

    n n ( u n ) + ( u n )T +

    1n

    f n (3.7)

    u n +1 = 0 (3.8)

    26

  • 8/7/2019 Kalland Master

    27/142

    un +1 un t + un u nx + vn u ny = 1n pn +1x + 2n x n ux+

    1n

    y

    nuy

    +vx

    +1

    nf nx (3.9)

    vn +1 vn t

    + unv n

    x+ vn

    vn

    y=

    1n

    pn +1

    y+

    1n

    x

    nuy

    +vx

    +2

    n

    yn

    vy

    +1

    nf ny (3.10)

    u n +1

    x+

    vn +1

    y= 0 (3.11)

    It looks like one has to solve the equations for u n +1 and pn +1 simultane-ously, which is fully possible, but with the projection method one can solvethe equations in steps. This is more efficient because the linear equationsystems that need to be solved become much smaller.

    In the projection method one rst nds an approximation of the velocityand then nds the pressure and velocity corrections that are needed to fullthe equations.

    The rst step is to nd the approximate velocity, called intermediate ortentative velocity u . Though pn +1 is unknown in equation (3.7), one canestimate the velocity from the terms that are known. Instead of pn +1 , eitheruse pn or ignore the pressure.

    u u n t

    + u n u n = n

    pn +1

    n n ( u n ) + ( u n )T +

    f n

    n(3.12)

    where can be 0 or 1 depending on whether the pressure is included ornot[14].To ensure that u n +1 = 0, project the tentative velocity u intothe solenoidal vector function space ( {f | f = 0 everywhere }). First,subtract equation (3.12) from equation (3.7):

    u n +1 u t

    = 1

    n( pn +1 p n ) (3.13)

    27

  • 8/7/2019 Kalland Master

    28/142

    un +1 u t = 1n x ( pn+1 p n ) (3.14)vn +1 v

    t=

    1n

    y

    ( pn +1 pn ) (3.15)

    For simplicity, let def = pn +1 p n . Take the divergence on each side,then apply the continuity equation u n +1 = 0:

    u n +1

    u

    t = 1

    n ( pn +1

    pn

    ) (3.16)1

    t u=

    1n

    (3.17)

    1 t

    u n +1

    x u

    x=

    x

    1n

    x

    ( pn +1 pn ) (3.18)1

    tvn +1

    y v

    y=

    y

    1n

    y

    ( pn +1 p n ) (3.19)1

    tux

    + vy

    = x

    1n

    x

    + y

    1n

    y

    (3.20)

    If is constant in space, it can be moved outside the divergence operator,and the equation becomes a Poisson equation. This equation is often calledthe pressure Poisson equation (PPE). I will hereafter refer to equation (3.17)as the PPE also when is not constant, even if not strictly correct. Howthe PPE is solved is covered in section 4.5. The PPE can be written as

    ( 1 ) = f where

    f = 1 t u = 1 t u

    x + v

    y (3.21)

    Since u is known, can be found and u n +1 and pn +1 can be calculatedby using equation (3.13) and def = pn +1 pn as follows:

    pn+1 = pn + (3.22)

    u n +1 = u tn

    (3.23)

    28

  • 8/7/2019 Kalland Master

    29/142

    un +1 = u tn x (3.24)vn +1 = v

    tn

    y

    (3.25)

    The tentative velocity u makes the projection method elegant and com-pact, but does not have a particular physical meaning. It may be easier tounderstand the derivation of the projection method without introducing thetentative velocity: u n +1 can be eliminated from equation (3.7) by applyingthe divergence operator directly on both sides. This results in a Poisson-likeequation which can be solved with respect to the unknown pressure pn +1 .The pressure pn +1 can then be plugged back into equation (3.7) which issolved for u n +1 .

    3.1 Velocity boundary conditions

    As usual with differential equations, one needs to supplement the Navier-Stokes equations with boundary conditions. The tentative velocity u canbe found everywhere in the interior of the domain from equation (3.12),

    but not on the boundary. Though the velocity boundary conditions may beknown, one cannot necessarily apply those to the tentative velocity, sincethe tentative velocity does not have a physical interpretation. How veloc-ity boundary conditions, tentative velocity boundary conditions and PPEboundary conditions are connected is explained in section 4.7.

    There are a few typical types of boundaries, each with its own set of velocity boundary conditions.

    3.1.1 No-slip

    The no-slip boundary is used for walls. The velocity is set to zero at thewall boundary. There is no ow across the wall boundary and the ow doesnot slip along the wall.

    3.1.2 Symmetry

    The symmetry boundary is used for symmetry axes or planes. Because of symmetry, the ow through the plane must be zero. The tangential velocitymust be equal on each side of the symmetry plane, therefore the normalderivative of the tangential velocity must be zero.

    29

  • 8/7/2019 Kalland Master

    30/142

    3.1.3 Inlet

    The inlet boundary is used if the ow into (or out of) the domain is known.The normal component of the velocity is set to some prescribed value andthe tangential component is set to zero.

    3.1.4 Outlet

    When uid is to ow freely across the boundary, the velocity gradient at theboundary is set to zero.

    Outlets should be placed far away from any obstacles such that the owis allowed to develop and stabilise before reaching the boundary. If the owis not fully developed at the boundary, the Neumann boundary conditionsused for the velocity would be physically incorrect and lead to inaccurateresults[33].

    3.1.5 Prescribed pressure

    The pressure can be xed at the boundary to induce ow driven by a pressuredifference. The normal derivative of the tangential velocity is set to zero.

    Pressure boundaries should also be placed far away from any obstaclessuch that the ow is allowed to develop and stabilise before reaching theboundary.

    Condition2D 3D

    x-axis y-axis xy-plane yz-plane xz-planeNo-slip u = 0 u = 0 u = 0 u = 0 u = 0

    v = 0 v = 0 v = 0 v = 0 v = 0w = 0 w = 0 w = 0

    Symmetry uy = 0 u = 0uz = 0 u = 0

    uy = 0

    v = 0 vx = 0vz = 0

    vx = 0 v = 0

    w = 0 wx = 0wy = 0

    Inlet u = 0 u = C u = 0 u = C u = 0v = C v = 0 v = 0 v = 0 v = C

    w = C w = 0 w = 0

    Outletuy = 0

    ux = 0

    uz = 0

    ux = 0

    uy = 0v

    y = 0vx = 0

    vz = 0

    vx = 0

    vy = 0

    wz = 0

    wx = 0

    wy = 0

    Pressure uy = 0 p = C uz = 0 p = C

    uy = 0

    p = C vx = 0vz = 0

    vx = 0 p = C

    p = C wx = 0wy = 0

    Table 3.1: Velocity boundary conditions for different boundaries. C is anarbitrary, prescribed value.

    30

  • 8/7/2019 Kalland Master

    31/142

    3.2 Poisson boundary conditions

    The PPE needs boundary conditions consistent with the velocity boundaryconditions. The no-slip condition is described in detail in [8]. A no-slipboundary condition for the PPE can be found in two ways. One way is tomultiply each side of equation (3.13) with a unit normal n on the domainboundary:

    1 t

    n (u n +1 u ) = 1

    nn ( pn +1 p n ) =

    1n

    n (3.26)

    which is a Neumann boundary condition. If u is set equal to the prescibedvelocity u n +1 on the boundary, we get a homogeneous Neumann boundarycondition which is easy to implement.

    Another way to derive a PPE boundary condition is to multiply eachside of equation (3.13) with a unit tangent t on the domain boundary:

    1 t

    t (u n +1 u ) = 1

    nt ( pn +1 p n ) =

    1n

    t (3.27)

    which indirectly leads to a Dirichlet boundary condition. Assume that(u n +1 u ) is known on the boundary. If is xed at one point on theboundary, one can integrate tangentially to nd anywhere else on theboundary.

    I choose to use the homogeneous Neumann boundary condition, since ac-cording to [8], only the Neumann boundary condition is always appropriate.I use the Neumann boundary condition for the PPE on no-slip, symmetryand inlet boundaries. The motivation behind the Neumann boundary con-dition is mathematical, not physical. Thus, the normal derivative of thepressure on the boundary has no physical meaning.

    A Dirichlet boundary condition can be used in the PPE to create apressure difference between two or more boundaries, causing a ow fromhigh to low pressure.

    The mathematical rationale behind the implementation of the outletboundary is vague to me. Anyhow, the homogeneous Neumann boundarycondition is used for the PPE on outlets[9].

    The PPE with only the Neumann boundary condition has either nosolution or innitely many solutions. Let be the uid domain and let be its boundary. As usual, n is the unit normal on the boundary. We havethe following problem:

    1

    n = f where n = 0 on (3.28)

    where f = 1 t u .

    31

  • 8/7/2019 Kalland Master

    32/142

    Integrate both sides over the domain :

    1n d = fd (3.29)The divergence theorem states that:

    1n d = n 1n dS = 1n n =0dS (3.30)

    The last integral is a surface integral over the domain boundary. Sincen = 0, the left hand side of equation (3.29) must be zero, and hencethe right hand side must also be zero. Now, we have shown that if is asolution of the Poisson equation, then fd must be zero. In other words, fd = 0 is necessary for the existence of a solution. This means that theamount owing into the domain as dened by the tentative velocity u must equal the amount owing out of the domain. If this is not the case,the tentative velocity u must be adjusted somehow on the boundary suchthat the net ow becomes zero. How this can be done is described in section4.7.2.

    If there is a solution to the PPE with only Neumann boundary condi-tions, we can add any constant C and still have a solution + C . Thus, thereare innitely many solutions. If the solution method used on the PPE re-quires one unique solution, must be xed in exactly one point, for instanceby setting in the lower left corner to zero: 0,0 = 0.

    fd = 0 is not necessary when using Dirichlet boundary conditioninstead of Neumann on parts of the boundary.

    32

  • 8/7/2019 Kalland Master

    33/142

    Chapter 4

    Discretisation

    The continuous problem must be reduced to a discrete problem so that acomputer can digest it. There are several methods for doing this, but I willuse the simplest one, the nite difference method (FDM).

    I will focus on the 2D Navier-Stokes equations from here, but the discreti-sation in 3D follows analogously. Instead of solving the continuous problem,the problem is solved in a limited number of points or nodes. The nodes arearranged in a grid of rows and columns, and a function value is stored ineach node.

    Both the unknown functions and their derivatives appear in differentialequations. We get rid of the unknown derivatives by using nite differenceapproximations in their place. The accuracy of the solution depends on thedistance between the nodes and the approximations used for the derivatives.

    Mostly central difference is used in the interior of the domain. One-sideddifference is sometimes used on the boundary and for upwind differencingin the interior. Upwind differencing is used to stabilise the simulation.

    4.1 Staggered grid

    In a non-staggered grid, values of different functions are stored at the samelocation. Each node contains both a velocity vector and a pressure value(see gure 4.1). This is the most obvious grid to choose, but will not beused here for reasons explained below.

    The problem with the non-staggered grid when solving Navier-Stokesequations is that we easily get pressure oscillations. We wish to use a centraldifference since it has higher order of accuracy than a one-sided (upwind)difference. The pressure gradient at position ( x, y ) with a central differenceis:

    x

    p(x,y, t ) p(x + x,y, t ) p(x x,y, t )

    2 x(4.1)

    y

    p(x,y, t ) p(x, y + y, t ) p(x, y y, t )

    2 y(4.2)

    33

  • 8/7/2019 Kalland Master

    34/142

    u, v, p

    Figure 4.1: Non-staggered grid. Velocity and pressure values are stored incommon nodes, here shown as black dots.

    where x and y are the horizontal and vertical node spacing respectively.

    We see that the centre node p(x,y, t ) itself does not appear in the ap-proximation of p(x,y, t ). Let the nodes be alternately black and white likeon a chess board. Let each white node have a constant value C W and everyblack node have a different constant value C B . Since every second node isblack and every other second is white, p(x + x,y, t ) p(x x,y, t ) willeither be C W

    C W or C B

    C B , which both evaluates to zero. Similarly

    for the y-component. Evaluating p in any node will now result in a zerovector no matter what value C W and C B have. A zero pressure gradienteld suggests that the pressure is constant, even though we know it oscil-lates between C W and C B . The oscillation problem is described in numerousbooks about numerical solution of differential equations. To learn more, thereader can look up convection-diffusion equation or advection-diffusionequation and upwind differences in any of these books (e.g. [15, 33, 34]).

    To avoid the oscillation problem, we use a staggered grid. In a staggeredgrid, each function is stored in its own subgrid shifted half a cell in one ormore directions relative to the other subgrids. All the subgrids have the

    same grid spacing. In 2D, the staggered grid we are going to use has threesubgrids, one for the pressure p, one for the horizontal velocity componentu and one for the vertical velocity component v. The grid is not staggeredin time. The placement of the subgrids is shown in gure 4.2 where thehorizontal arrows indicate u-nodes, vertical arrows indicate v-nodes and thedots indicate p-nodes. In gure 4.2, each pressure node is placed in themiddle of a square which is called a cell. In the staggered grid, the velocitycomponents can be thought of as ow though cell walls from one cell to itsneighbouring cells.

    In the staggered grid, one can approximate the pressure gradient by

    34

  • 8/7/2019 Kalland Master

    35/142

    p

    v

    u

    Figure 4.2: Staggered grid. p-nodes are shown as black dots, u-nodes ashorizontal arrows and v-nodes as vertical arrows.

    using adjacent nodes:

    x

    p(x,y, t ) p(x + x/ 2,y, t ) p(x x/ 2,y, t )

    x(4.3)

    y

    p(x,y, t ) p(x, y + y/ 2, t ) p(x, y y/ 2, t )

    y(4.4)

    To evaluate px at some location, a pressure node half a cell width to the

    left and to the right of this location are needed. Looking at gure 4.2, onecan see that this location coincides with the u-nodes. Similarly, py can beevaluated at v-nodes.

    4.2 Ghost cells

    Around the simulation domain, a strip of ghost cells is added (see gure4.3). The ghost cells are so called because they are not part of the compu-tational domain, but still contain values that contribute to the calculations.Ghost cells are not strictly needed, but make it easier to impose boundaryconditions. How this is done is explained in section 4.7.

    4.3 Indexing

    It is common to assign indices to the nodes instead of using their coordinates.For some function f , I dene the indexing as follows:

    f ni,jdef = f (x0 + i x, y0 + j y, t 0 + n t) (4.5)

    where (x0, y0) is the location of the bottom left cell centre in the domainand t0 is the initial time. When the time level is obvious, I will omit the

    35

  • 8/7/2019 Kalland Master

    36/142

    Figure 4.3: Ghost cells are shown in grey around a domain of 4 4 cells.Dots indicate p-nodes, horizontal bars indicate u-nodes and vertical barsindicate v-nodes.

    superscript. Looking at gure 4.4, we see that the pressure nodes are indexedas pi,j , u-nodes are indexed as u i+ 1/ 2 ,j and v-nodes as vi,j + 1/ 2 where i and j

    are integers.

    4.4 Calculating the tentative velocity

    The tentative velocity is found by solving equation (3.12).

    uun t

    + unu n

    x+ vn

    u n

    y

    convection=

    n

    pn

    x

    pressure gradient+

    f nxn

    other forces+

    1n

    x

    2nux

    +

    yn

    uy

    +vx

    viscosity (4.6)36

  • 8/7/2019 Kalland Master

    37/142

    i=1 i= i=0 i= i=1

    j=1

    j=

    j=0

    j=

    j=1

    p1,1

    p1,0

    p1,1

    p0,1

    p0,0

    p0,1

    p1,1

    p1,0

    p1,1

    u,1

    u,0

    u,1

    u,1

    u,0

    u,1

    v1,

    v1,

    v0,

    v0,

    v1,

    v1,

    Figure 4.4: Node indexing in the lower left corner of the grid.

    37

  • 8/7/2019 Kalland Master

    38/142

    vvn t

    + unv n

    x+ vn

    vn

    y

    convection=

    n

    pn

    y

    pressure gradient+

    f nyn

    other forces+

    1n

    x

    nuy

    +vx

    +

    y2n

    vy

    viscosity (4.7)The tentative velocity nodes have the same location as the usual velocity

    nodes, that is, u-nodes coincide with u-nodes, v-nodes with v-nodes. Inthis section, I will describe how the tentative velocity is calculated in thedomain interior. The tentative velocity on the boundary and in the ghostcells are set by imposing boundary conditions which will be covered later.

    I will only show how to approximate ui,j , the x-component of the tenta-tive velocity. The y-component is approximated analogously.

    4.4.1 Density

    The density nodes are located with the pressure nodes. The density at

    velocity nodes is approximated by the average of the two closest densitynodes:

    i+ 1/ 2 ,j 12

    (i,j + i+1 ,j ) (4.8)

    4.4.2 Convection

    The convective term does not t snugly into the staggered grid scheme.If one uses the usual central difference to approximate derivatives for thehorizontal component, one nds:

    un un

    x+ vn u

    n

    y i+ 1/ 2 ,j uni+ 1/ 2 ,j

    uni+1 ,j uni,j x+ vni+ 1/ 2 ,j

    uni+ 1/ 2 ,j + 1/ 2 uni+ 1/ 2 ,j 1/ 2 y

    (4.9)

    But u-nodes does not exist every half cell width and height, and thev-nodes are not in the same location as the u-nodes. Solve the problem byrewriting the expression as (u

    2 )x +

    (uv )y and averaging neighbouring nodes[9].

    By simple differentiation using the product rule and applying the continuity

    38

  • 8/7/2019 Kalland Master

    39/142

    equation (3.2):

    (u2)x

    + (uv)

    y= 2 u

    ux

    + uvy

    + vuy

    = uux

    + uvy

    = u u =0+ u

    ux

    + vuy

    (4.10)

    = uux

    + vuy

    This shows that the left hand side is indeed an alternative expressionfor the convective term. Similarly for the vertical component, u vx + v vy = (uv )

    x + (v2 )

    y . The horizontal component of the convective term can thus beapproximated as:

    (u2)x

    + (vu)

    y i+ 1/ 2 ,j u2i+1 ,j u2i,j

    x

    +u i+ 1/ 2 ,j + 1/ 2 vi+ 1/ 2 ,j + 1/ 2 u i+ 1/ 2 ,j 1/ 2 vi+ 1/ 2 ,j 1/ 2

    y(4.11)

    Average neighbouring nodes, for instance u i,j (u i 1/ 2 ,j + u i+ 1/ 2 ,j )/ 2, toget (see gure 4.5):

    (u2)x

    + (vu)

    y i+ 1/ 2 ,j 1

    xu i+ 1/ 2 ,j + u i+ 3/ 2 ,j

    2

    2

    1

    xu i 1/ 2 ,j + u i+ 1/ 2 ,j

    2

    2

    +1

    yu i+ 1/ 2 ,j + u i+ 1/ 2 ,j +1

    2vi,j + 1/ 2 + vi+1 ,j + 1/ 2

    2

    1

    yu i+ 1/ 2 ,j + u i+ 1/ 2 ,j 1

    2vi,j 1/ 2 + vi+1 ,j 1/ 2

    2(4.12)

    This scheme is a second order approximation.In [9], Griebel adds upwind differencing to improve stability, and I choose

    to do the same. An upwind difference is a one-sided difference shifted innegative ow (upwind) direction. Upwind differencing is added to the righthand side of equation (4.12):

    39

  • 8/7/2019 Kalland Master

    40/142

    Figure 4.5: The dots indicate nodes used in the discretisation of the convec-tive term. The crosses are the position of the averaged values.

    (u2)x

    + (vu)

    y i+ 1/ 2 ,j . . .+

    4 x |u i+ 1/ 2 ,j + u i+ 3/ 2 ,j |(u i+ 3/ 2 ,j u i+ 1/ 2 ,j )

    |u i 1/ 2 ,j + u i+ 1/ 2 ,j

    |(u i+ 1/ 2 ,j

    u i 1/ 2 ,j )

    +

    4 y |vi,j + 1/ 2 + vi+1 ,j + 1/ 2 |(u i+ 1/ 2 ,j +1 u i+ 1/ 2 ,j ) |vi,j 1/ 2 + vi+1 ,j 1/ 2 |(u i 1/ 2 ,j u i 1/ 2 ,j 1)

    (4.13)

    where is an constant factor between 0 and 1 deciding how much upwinddifferencing to use. This factor must be adjusted manually by the user. Theupwind differencing scheme is only a rst order approximation, so if stable, = 0 should give the most accurate result.

    4.4.3 Pressure gradient

    The pressure gradient is approximated with a central difference (see gure4.6):

    px i,j

    pi+ 1/ 2 ,j pi 1/ 2 ,j x

    (4.14)

    40

  • 8/7/2019 Kalland Master

    41/142

    Figure 4.6: Nodes used in the discretisation of the pressure gradient term.

    4.4.4 Viscosity

    Viscosity terms are approximated by (see gure 4.7):

    x

    2ux i+ 1/ 2 ,j

    2 x

    ux i+1 ,j

    ux i,j

    (4.15)

    ux i,j

    u i+ 1/ 2 ,j

    u i 1/ 2 ,j

    x (4.16)

    y

    uy

    +vx i+ 1/ 2 ,j

    1 y

    uy

    +vx i+ 1/ 2 ,j + 1/ 2

    1

    y

    uy

    +vx i+ 1/ 2 ,j 1/ 2

    (4.17)

    uy i+ 1/ 2 ,j + 1/ 2

    u i+ 1/ 2 ,j +1 u i+ 1/ 2 ,j y

    (4.18)

    vx i+ 1/ 2 ,j + 1/ 2

    vi+1 ,j + 1/ 2 vi,j + 1/ 2 x

    (4.19)

    i+ 1/ 2 ,j 1/ 2 14

    (i,j + i+1 ,j + i,j +1 + i+1 ,j +1 ) (4.20)

    (4.21)

    The tentative velocity is then the combination of convection, pressuregradient, viscosity terms and additional forces:

    u= un + t(convection pressure gradient + viscosity +f nxn

    ) (4.22)

    41

  • 8/7/2019 Kalland Master

    42/142

    Figure 4.7: Nodes used in the discretisation of the viscosity term. The dotsindicate velocity nodes needed in the discretisation. The crosses indicatethe location of intermediate velocity derivatives.

    4.5 The pressure Poisson equation

    The next step is to nd the pressure at the next time level by solving thePPE (3.17). Substituting derivatives with discrete approximations, the PPEbecomes:

    1 tu

    i+1

    /2,j

    ui

    1

    /2,j x +

    vi,j +

    1

    /2

    v

    i,j 1

    /2

    y

    = ni+ 1/ 2 ,ji+1 ,j i,j

    x2 ni 1/ 2 ,j

    i,j i 1,j x2

    + ni,j + 1/ 2i,j +1 i,j

    y2 ni,j 1/ 2

    i,j i,j 1 y2

    (4.23)

    Since the tentative velocity is known, the left hand side can be calcu-lated. It is not possible to nd the value of in a single node explicitly fromthis equation a linear equation system must be solved, where in eachnode is treated as a separate scalar unknown. When solving the linear equa-tion system resulting from the Poisson equation, one should use a methodsuitable for sparse systems, such as stationary (classic) iterative methods orKrylov subspace methods.

    4.5.1 Boundary conditions

    For pressure nodes near the boundary, some of the neighbouring nodes lieoutside the uid domain. Boundary conditions are used to eliminate anyreference to these nodes.

    If the normal derivative of the pressure is zero on the boundary, thecorresponding term in equation (4.23) can be eliminated. For instance,

    42

  • 8/7/2019 Kalland Master

    43/142

    assume that there is a vertical boundary or obstacle wall passing between

    nodes i 1,j and i,j . Since the normal derivative of the pressure is zero, wehave:

    x

    i,j i 1,j x

    = 0 (4.24)

    This boundary condition eliminates the second of the four addends on theright hand side of equation (4.23), thereby eliminating the reference toi 1,j [9].

    Dirichlet boundary conditions are handled in a similar manner. Assumethat the pressure value on the boundary is pn +1bc . First of all, calculate bc = pn +1bc p nbc. Since the pressure nodes do not coincide with the boundary,the boundary condition must be enforced in an averaged sense. As before,let the boundary pass between the nodes i 1,j and i,j :

    i 1,j + i,j2

    = bci,j i 1,j

    x2=

    2(i,j bc) x2

    (4.25)

    Again, the boundary condition changes the corresponding term on the righthand side of equation (4.23).

    If the normal derivative is zero ( n p = 0) all around the boundary,the linear equation system will only be solvable if the total amount of ow(dened by the tentative velocity u ) into the domain is equal to the totalamount owing out of the domain, that is, the net outow is zero. How the

    outow can be adjusted to achieve zero net outow is described in section4.7. If the net outow is not zero, the best option is probably not to useNeumann boundary condition along the entire boundary, but to prescribethe pressure on some part of it.

    Another problem with Neumann boundary condition on the entire bound-ary is that if there is a solution, there are innitely many solutions. It ispreferable if the pressure stays in the area around zero. If the pressure be-comes large, the accuracy of the oating point calculations is reduced. Theproblem can be solved by requiring that a certain pressure node should bezero or that the average pressure should be zero.

    4.6 Correcting pressure and velocity

    The pressure at the next time level is found by the denition of .

    pn+1i,j = pni,j + i,j (4.26)

    The velocity is found by solving equation (3.13) with respect to u n +1 :

    u n +1 = u tn

    (4.27)

    43

  • 8/7/2019 Kalland Master

    44/142

    Using a central difference for the derivatives, these equations can be

    approximated as:

    un +1i+ 1/ 2 ,j = u

    i+ 1/ 2 ,j t

    ni+ 1/ 2 ,j

    i+1 ,j i,j x

    (4.28)

    vn +1i,j + 1/ 2 = v

    i,j + 1/ 2 t

    ni,j + 1/ 2

    i,j +1 i,j y

    (4.29)

    Velocity nodes on the boundary should also be corrected. If the normalpressure derivative is zero, for instance on the west boundary, then un +1 =u there. If is known on the west boundary, then:

    un +1 1/ 2 ,j = u 1/ 2 ,j 2 tn 1/ 2 ,j

    0,j bc x (4.30)

    where bc is the known value of on the boundary. Note the factor 2.

    4.7 Velocity boundary conditions

    Boundary conditions are imposed simply by setting the values of the nodeson the boundary and in the ghost cells directly. To calculate the tentativevelocity u in a node, one needs to access neighbouring velocity nodes asshown in the gures in section 4.4. This could be problematic near theboundary where the nodes do not have neighbours in all directions. This isthe reason behind the ghost cells. Instead of trying to access non-existentneighbour nodes, one can read from nodes in the ghost cells. The values inthe ghost cells are adjusted to t the boundary conditions. First, the ten-tative velocity u is calculated for all interior nodes. The tentative velocityu on the boundary is updated depending on the boundary conditions. ThePPE is then solved and the velocity at the next time level u n +1 is calculated.Finally, boundary conditions are applied to u n +1 on the boundary and inthe ghost cells.

    I explain how boundary conditions are implemented for the west bound-ary. This can be extended analogously to the east, north and south bound-ary.

    4.7.1 Dirichlet boundary conditions

    The tentative velocity is calculated as usual.Assume that the normal velocity un +1 is known on the west boundary.

    The value of un+1 is calculated from u and as shown in equation (3.13).Therefore, uand must be chosen such that the known un +1 is the result.This is accomplished by setting u = un +1 and using the homogeneousNeumann condition x (0,j 1,j )/ x = 0 on this boundary[9].

    44

  • 8/7/2019 Kalland Master

    45/142

    v1, jvwall v0, j

    Figure 4.8: Dirichlet boundary condition for v on the west boundary.

    If the tangential velocity vn +1 has a prescribed value vn +1wall on the westboundary, the values in ghost cell nodes must be adjusted such that the

    average on the boundary is vn +1wall [9] (see gure 4.8):

    vn +1 1,j 2vn +1wall vn +10,j (4.31)The indicates an explicit assignment.

    4.7.2 Neumann boundary conditions

    The tentative velocity is calculated as usual.If the normal derivative of the tangential velocity v

    n +1

    x is zero on thewest boundary, the tangential velocity inside the domain is extrapolated tothe ghost cells[9]. (see gure 4.9):

    vn +1 1,j vn +10,j (4.32)How to implement the Neumann boundary condition for the normal ve-

    locity un +1 is described in [9, 33], but the method is somewhat questionablemathematically.

    Let ux = 0 on the west boundary. A one sided difference is used toapproximate the derivative:

    0 =ux 1/ 2 ,j

    u1/ 2 ,j u 1/ 2 ,j x

    (4.33)

    45

  • 8/7/2019 Kalland Master

    46/142

    v1, j v0, j

    Figure 4.9: Neumann boundary condition for v on the west boundary.

    Solving for the boundary velocity u 1/ 2 ,j :

    u 1/ 2 ,j = u 1/ 2 ,j (4.34)

    The question now is whether to impose this condition on uor un +1 , andhow. In [9, 33], the boundary condition is applied explicitly to un +1 ; afterun +1 has been calculated for the domain interior, it is extrapolated to theboundary ( un +1 1/ 2 ,j = u

    n +11/ 2 ,j ). In addition, in [33], the velocity on the boundary

    is scaled such that the net outow becomes zero. However, overwriting thevelocity on the boundary will prevent the continuity equation from beingfullled in cells next to the boundary. Nonetheless, if the boundary is placedfar away from obstacles such that the ow is allowed to develop and stabilisebefore reaching the boundary, as advised in [33], the error should become

    minimal.The question of how to treat the tentative velocity u on the boundary

    remains. In [9, 33], u = u n on the west boundary. If the PPE is going to besolvable, the net outow dened by the tentative velocity u must be zero.If the normal velocity component is prescribed on the entire boundary, andthe net outow is not zero, the problem is inconsistent and a solution doesnot exist. If the normal velocity component is not prescribed on some partof the boundary, the ow there can be adjusted somehow to make the netoutow zero. In the description of the SIMPLE algorithm in [33], the ve-locity at outlets is scaled such that the net outow becomes zero. However,

    46

  • 8/7/2019 Kalland Master

    47/142

    the SIMPLE algorithm is not transient 1, so it is not clear if this approach

    is satisfactory for transient simulations. Another option is to add some con-stant to the velocity at the outlets such that the net outow becomes zero.This is done in the NaSt3DGP ow solver. I implemented both methodsand let the user choose which one to use.

    1. Scale : Calculate the inow M in and outow M out . The outow is thenscaled by a factor c such that cM out = M in . Scale the velocities onoutlet boundaries as follows[33]:

    u new u oldM inM out

    (4.35)

    Clearly, this will only work if M out = 0. If M out = 0, add a value toall outlets instead, as described below.

    2. Add : Calculate the inow M in , outow M out and the total length Lof the outlets. Adjust the velocity at vertical outlets by:

    unew uold yL

    (M in M out ) (4.36)and at horizontal outlets by:

    vnew vold xL

    (M in M out ) (4.37)

    Since the method in [9, 33] is probably thoroughly tested, I use it foroutlets where the pressure is unknown. However, for pressure driven ows,extrapolating the velocity to the boundary will eliminate the effect of apressure gradient across the boundary.

    4.7.3 Prescribed pressure

    I failed to nd a good explanation on how to implement pressure drivenows. In [33], the pressure at nodes just inside the boundary are xed whensolving the PPE. This will cause those pressure nodes to work as sources

    or sinks, and the continuity equation is thus not fullled for these cells.Therefore, the velocity on the boundary is adjusted to full the continuityequation afterwards. Unfortunately, the pressure nodes are half a cell widthaway from the boundary. I prefer a solution where the pressure is set exactlyon the boundary. I therefore made my own scheme based on the one in [33].

    To allow pressure driven ows, I use the same equation (3.12) to calculatethe tentative velocity uon the boundary as inside the domain. After solvingthe PPE, I correct uon the boundary to give un +1 the same way as in the

    1 Transient algorithms calculate how the ow develops over time, while non-transientalgorithms calculate steady ows.

    47

  • 8/7/2019 Kalland Master

    48/142

  • 8/7/2019 Kalland Master

    49/142

    Obstacle cells

    Fluid cells

    v=0 v=0

    uwall

    =0

    u=0

    u=0

    vwall

    =0

    Figure 4.11: The velocity is set to zero on the boundary between obstaclecells and uid cells.

    4.8 Time-step restriction

    If t is too large, the simulation becomes unstable. The simulation maybecome non-physical and oscillating at best, or the otaing point valuesmay become exponentially larger and in the end reach NaN and inf.There are a few criteria that will help us determine a safe t value.

    The rst is the Courant-Friedrichs-Lewy (CFL) condition which statesthat a uid particle may not travel farther than one cell during one timestep [9, 29]:

    | t u| < x (4.39)

    | t

    v

    |< y (4.40)

    The viscous term in the momentum equation can be thought of as dif-fusion of velocity. The viscous term produces a force which evens out sharpchanges in velocity. The force causes the velocity to move towards an equi-librium state. However, if the time-step t is too large, the velocity willswing past the equilibrium state and farther beyond than in the rst place.Thus, the time-step must be restricted based on diffusion. The requirementis [9, 29, 6]:

    t 0 (5.36)

    Let f i,j be the volume fraction for the i, j -th cell. The area of dark uidinside the cell is (see gure 5.7):

    f i,j x y = A(x i 1/ 2 , y j 1/ 2 ) A(x i+ 1/ 2 , y j 1/ 2 )A(x i 1/ 2 , y j + 1/ 2 ) + A(x i+ 1/ 2 , y j + 1/ 2 ) (5.37)

    If the interface line is almost horizontal or vertical, the calculations canbecome unacceptably inaccurate. It might be better to treat such lines as

    62

  • 8/7/2019 Kalland Master

    63/142

    Figure 5.8: If the plane is parallel to one of the main axes, the 3D problemis reduced to 2D.

    completely horizontal or vertical. If one of the normal components is belowsome small value , set the component to exact zero and reduce the problemto 1D.

    3D: Let V (x, y, z; nx , n y , n z , d) be the 3D-equivalent of A(x, y; nx , n y , d)and L(x; d). Assume that the x-axis points to the right, the y-axis into thepaper and the z-axis up. V (x, y, z; nx , n y , n z , d) returns the volume to theright of x, farther away than y, above z and below the interface plane. Moreprecisely:

    V (x, y, z; nx , n y , n z , d) = x y z (x ,y,z ; nx , n y , n z , d)dz dy dx= lim

    X Xx limY Y y limZ Z z (x ,y,z ; nx , n y , n z , d)dz dy dx(5.38)

    where

    (x,y,z ; nx , n y , n z , d) =1 : nx x + nyy + n zz + d 00 : nx x + nyy + n zz + d > 0 (5.39)

    V (x, y, z; nx , n y , n z , d) is only dened if the limits exist. If nx , ny or n zis zero, that is, the plane is parallel to the x-, y- or z-axis, the problem isreduced to 2D (see gure 5.8).

    I will hereafter only write V (x, y, z) instead of V (x, y, z; nx , n y , n z , d)when it is clear which interface is used.

    63

  • 8/7/2019 Kalland Master

    64/142

    Let the i,j ,k -th cell be [x i 1/ 2 , x i+ 1/ 2 ] [y j 1/ 2 , y j + 1/ 2 ] [zk 1/ 2 , zk+ 1/ 2 ].Let f i,j,k be the volume fraction for the i,j ,k -th cell. Let x i 1/ 2 , x i+ 1/ 2 ,y j 1/ 2 , y j + 1/ 2 , zk 1/ 2 and zk+ 1/ 2 be the cell coordinates relative to the cellcentre:

    x i 1/ 2 = x i 1/ 2 12

    (x i 1/ 2 + x i+ 1/ 2 ) = x2

    (5.40)

    x i+ 1/ 2 = x i+ 1/ 2 12

    (x i 1/ 2 + x i+ 1/ 2 ) = x2

    (5.41)

    y j 1/ 2 = y j 1/ 2 12

    (y j 1/ 2 + y j + 1/ 2 ) = y2

    (5.42)

    y j + 1/ 2 = y j + 1/ 2 12

    (y j 1/ 2 + y j + 1/ 2 ) = y2

    (5.43)

    zk 1/ 2 = zk 1/ 2 12

    (zk 1/ 2 + zk+ 1/ 2 ) = z2

    (5.44)

    zk+ 1/ 2 = zk+ 1/ 2 12

    (zk 1/ 2 + zk+ 1/ 2 ) = z2

    (5.45)

    If only nx = 0, the volume fraction is:

    f i,j,k =A(y j 1/ 2 , zk 1/ 2 ; ny , n z , d) A(y j + 1/ 2 , zk 1/ 2 ; ny , n z , d)

    y x

    +A(y j + 1/ 2 , zk+ 1/ 2 ; ny , n z , d) A(y j 1/ 2 , zk+ 1/ 2 ; ny , n z , d)

    y x(5.46)

    If only ny = 0, the volume fraction is:

    f i,j,k =A(x i 1/ 2 , zk 1/ 2 ; nx , n z , d) A(x i+ 1/ 2 , zk 1/ 2 ; nx , n z , d)

    x z

    +A(x i+ 1/ 2 , zk+ 1/ 2 ; nx , n z , d) A(x i 1/ 2 , zk+ 1/ 2 ; nx , n z , d)

    x z(5.47)

    If only n z = 0, the volume fraction is:

    f i,j,k =A(x i 1/ 2 , y j 1/ 2 ; nx , n y , d) A(x i+ 1/ 2 , y j 1/ 2 ; nx , n y , d)

    x y

    + A(x i+1/ 2 , y j + 1/ 2 ; nx , n y , d) A(x i 1/ 2 , y j + 1/ 2 ; nx , n y , d) x y (5.48)

    If both nx = ny = 0, the problem is reduced to 1D:

    f i,j,k =L(zk 1/ 2 ; d) L(zk+ 1/ 2 ; d)

    z(5.49)

    etc.If all the normal components are strictly positive, V (x, y, z) becomes the

    volume of the tetrahedron shown in gure 5.9.

    64

  • 8/7/2019 Kalland Master

    65/142

    ( x, y, z)

    ( x, yintersect , z)

    ( xintersect , y, z)

    ( x, y, zintersect )

    V ( x, y, z)

    i n t e r f a c e p l a n e

    Figure 5.9: V (x, y, z) returns the volume of the tetrahedron below the in-terface plane, to the right of x, farther away than y and above z.

    If the point ( x, y, z) is above the interface plane, that is, nx x + ny y +n z z + d > 0, the tetrahedron volume is zero. Otherwise, x intersect , yintersectand zintersect must be found to calculate the volume. Insert the known andunknown values into the plane equation:

    nx x intersect + ny y + n z z + d = 0 (5.50)nx x + ny yintersect + n z z + d = 0 (5.51)nx x + ny y + n z zintersect + d = 0 (5.52)

    Solve for x intersect , yintersect and zintersect :

    x intersect = ny y + n z z + d

    nx(5.53)

    yintersect = nx x + n z z + d

    ny(5.54)

    zintersect = n

    xx + n

    yy + d

    n z (5.55)

    Calculate the tetrahedron volume:16

    (x intersect x)( yintersect y)( zintersect z) = (nx x + ny y + n z z + d)3

    6nx nynz(5.56)

    V (x, y, z; nx , n y , n z , d) is:

    V (x, y, z) = (n x x+ n y y+ n z z+ d)3

    6n x n y n z : nx x + ny y + nz z + d 00 : nx x + ny y + nz z + d > 0

    (5.57)

    65

  • 8/7/2019 Kalland Master

    66/142

    =

    =

    =

    =

    + =

    + =

    + =

    V ( xi , y j , zk ) V ( xi , y j , zk+ )

    V ( xi+ , y j , zk+ )

    V ( xi+ , y j , zk )

    V ( xi+ , y j+ , zk )

    V ( xi+ , y j+ , zk+ )

    V ( xi , y j+ , zk+ )

    V ( xi , y j+ , zk )

    Figure 5.10: By adding and subtracting the right volumes, the volume of dark uid inside the cell is found.

    The volume of dark uid inside the cell is (see gure 5.10):

    f i,j,k x y z = V (x i 1/ 2 , y j 1/ 2 , zk 1/ 2 ) V (x i+ 1/ 2 , y j 1/ 2 , zk 1/ 2 )V (x i 1/ 2 , y j + 1/ 2 , zk 1/ 2 ) + V (x i+ 1/ 2 , y j + 1/ 2 , zk 1/ 2 )V (x i 1/ 2 , y j 1/ 2 , zk+ 1/ 2 ) + V (x i+ 1/ 2 , y j 1/ 2 , zk+ 1/ 2 )+ V (x i 1/ 2 , y j + 1/ 2 , zk+ 1/ 2 ) V (x i+ 1/ 2 , y j + 1/ 2 , zk+ 1/ 2 )(5.58)

    Like in 2D, the calculations can become inaccurate when one of thenormal components is close to zero. If one of the normal components isbelow some small value , set the component to exact zero and reduce theproblem to 2D.

    66

  • 8/7/2019 Kalland Master

    67/142

    5.3.3 Reconstruction when the normal is known

    The difference between PLIC reconstruction algorithms is how the interfacenormal is approximated. Normal estimation will be covered in the nextsection, but for now, assume that the normal is known. The reconstructedlinear interface must be adjusted such that the volume fraction below theline or plane in a cell is equal to the cells colour function value. Since thenormal n is xed, only d needs to be found.

    2D: Let f i,j be the colour function value for the i, j -th cell. The volumefraction in a cell can be expressed as a function f of the interfaces signeddistance d from the cell centre. One has to solve the following equation with

    respect to d: f (d) = f i,j (5.59)

    Because f (d) is a linear combination of piecewise second degree polynomialfunctions A(x, y; nx , n y , d), f (d) itself is a piecewise second degree polyno-mial. Since f (d) calculates an area, it can be thought of as an integralof a function f (d). Because f (d) is of second degree, f (d) is a piecewiselinear function as shown in gure 5.11. It is not too difficult to see thatf (d) is monotonically decreasing. The polynomials are joined at d1, d2, d3and d4 where the interface line intersects the cell corners, as shown in thegure. Note that d increases to the left in the gure. Because of symmetry,d1 =

    d4, d2 =

    d3 and f (d) = 1

    f (

    d).

    The four d-values are found by inserting the cells corner coordinates intothe line equation and solving for d. Let ( x, y) be one of the corners relativeto the cells centre:

    d = (nx x + ny y) (5.60)Sort the four d-values in descending order. The function f can be split

    into ve polynomials, one for each interval ( , d4], [d4, d3], [d3, d2], [d2, d1]and [d1, ). Call the functions f 5 to f 1 respectively. By using the fact thatf and its derivative are continuous, one gets the following information fromwhich the ve functions can be constructed:

    f 1(d1) = f 2(d1) = 0

    f 1(d1) = f 2(d1) f 2(d2) = f 3(d2) f 2(d2) = f 3(d2) f 3(d3) = f 4(d3) f 3(d3) = f 4(d3) f 4(d4) = f 5(d4) = 1

    67

  • 8/7/2019 Kalland Master

    68/142

    d 1 d 2 d 3 d 4

    d

    d

    f (d )

    f (d )

    polynomialdegree

    0th 2nd

    1st

    2nd 0th

    0.0

    0.5

    1.0

    i n t e r f a c e l i n

    e

    d = 0

    Figure 5.11: f (d) is a piecewise second degree polynomial.

    68

  • 8/7/2019 Kalland Master

    69/142

    f 4(d4) = f 5(d4) f 1 and f 5 are constant. f 2 and f 4 are second degree polynomials. f 3 is linear.The functions can be constructed using clever observations, or by using

    a general method that I will explain here. Let:

    f 1(d) = a1

    f 2(d) = a2d2 + b2d + c2

    f 3(d) = a3d + b3 f 4(d) = a4d2 + b4d + c4 f 5(d) = a5There are ten unknown coefficients, a, b and c with indices. Differentiate

    the functions:

    f 1(d) = 0

    f

    2(d) = 2 a

    2d + b

    2

    f 3(d) = a3 f 4(d) = 2 a4d + b4 f 5(d) = 0Use the previous information to set up a linear equation system:

    f 1(d1) = f 2(d1) = 0 a1 = 0a2d21 + b2d1 + c2 = 0 f 1(d1) = f 2(d1)0 = 2a2d1 + b2 f 2(d2) = f 3(d2)a2d22 + b2d2 + c2 = a3d2 + b3 f 2(d2) = f 3(d2)2a2d2 + b2 = a3 f 3(d3) = f 4(d3)a3d3 + b3 = a4d23 + b4d3 + c4 f 3(d3) = f 4(d3)a3 = 2 a4d3 + b4 f 4(d4) = f 5(d4) = 1 a4d24 + b4d4 + c4 = 1 a5 = 1 f 4(d4) = f 5(d4)2a4d4 + b4 = 0

    69

  • 8/7/2019 Kalland Master

    70/142

    By solving the system and replacing d4 with d1 and d3 with d2, thefunction f can be expressed as [32]:

    f (d) =

    f 1(d) = 0 d d1f 2(d) = (d d1 )

    2

    2(d21 d22 )

    d1 > d > d 2f 3(d) = 12 dd1 + d2 d2 d d3f 4(d) = 1 (d+ d1 )

    2

    2(d21 d22 )

    d3 > d > d 4f 5(d) = 1 d4 d

    (5.61)

    Note that the inequality signs are chosen such that division by zero shouldnot occur, even if d1 = d2.

    Compare f i,j with f (d1) = 0, f (d2), f (d3) and f (d4) = 1 to nd out inwhich interval the solution lies. One of the following three cases apply:

    0 f i,j < f (d2): In this interval, f (d) = f 2(d) = (d d1 )2

    2(d21 d22 )

    . Solve for d:

    d = d1 2(d21 d22)f i,j (5.62)Since d1 is the highest possible value for d (any higher, and the inter-face does not intersect the cell at all), must be turned into .

    f (d

    3) < f

    i,j 1: In this interval, the f (d) = f

    4(d) = 1

    (d+ d1 )2

    2(d21 d

    22 )

    .Solve for d:

    d = d1 2(d21 d22)(1 f i,j ) (5.63)Since d4 = d1 is the lowest possible value for d, must be turnedinto +.

    f (d2) f i,j f (d3): In this interval, f (d) = f 3(d) = 12 dd1 + d2 . Solvefor d:d = ( d1 + d2)

    12 f i,j (5.64)

    A numerical method could also be used to solve the equation f (d) = f i,j ,for instance the bisection method is guaranteed to succeed, but convergesslowly. Rider and Kothe [24] suggest using Brents method.

    3D Let f i,j,k be the colour function value for the i,j ,k -th cell. The volumefraction in a cell can be expressed as a function f of the interfaces signeddistance d from the cell centre. One has to solve the following equation withrespect to d:

    f (d) = f i,j,k (5.65)

    70

  • 8/7/2019 Kalland Master

    71/142

    1.00 1.00 0.44

    1.00 0.93 0.11

    0.44 0.11 0.00

    2.44 2.04 0.55

    2.44

    2.04

    0.55

    Figure 5.12: Histograms in the ELVIRA algorithm.

    Because f (d) is a linear combination of piecewise third degree polynomialfunctions V (x, y, z; nx , n y , n z , d), f (d) itself is a piecewise third degree poly-nomial. As in the 2D case, f (d) is monotonically decreasing. The polynomi-als are joined at d1 to d8 where the interface plane intersects the cell corners.Because of symmetry, d1 =

    d8, d2 =

    d7 etc. and f (d) = 1

    f (

    d). The

    equation can be solved numerically with a root-nding method, but as in2D, it is possible to nd the interval in which the solution lies, construct apolynomial for this interval, and solve the equation analytically.

    5.3.4 Estimating the normal with ELVIRA

    I chose to use the ELVIRA reconstruction algorithm found by Pilliod becauseit is reasonably fast and simple, and second order accurate according to [13].It will reconstruct linear interfaces exactly. However, ELVIRA does not workwell on too coarse grids and is only described for 2D.

    In the ELVIRA algorithm, one nds six normal candidates, and then one

    picks the normal which gives the smallest error.Consider a 3 3 block of cells about the i, j -th cell. Sum the colourfunction values in each row and column to create two histograms as shown

    in gure 5.12.The purpose of summing the colour function values in each column is

    to create a discrete height function that approximates the interface. Ineffect, all the dark uid in each column is moved to the bottom of thecolumn. However, if the dark uid was mostly at the top of the 3 3 block,moving the dark uid to the bottom of each column would give a poorapproximation to the interface. When most of the dark uid is at the top,

    71

  • 8/7/2019 Kalland Master

    72/142

    0.00 0.11 0.44

    0.11 0.93 1.00

    0.44 1.00 1.00

    0.55 2.04 2.44

    0.44 1.00 1.00

    0.11 0.93 1.00

    0.00 0.11 0.44

    Figure 5.13: Histograms in the ELVIRA algorithm sometimes need to bemirrored.

    I choose to implicitly 2 ip the 3 3 block upside down as shown in gure5.13. Similarly for the rows as shown in gure 5.14. After I have found anormal, I ip the normal back to match the original 3 3 block. Anotheroption is to rotate the 3

    3 block until most of the dark uid is in the lower

    left corner, calculate the normal and rotate the normal back.To nd out if most of the dark uid is at the top or bottom, I simply

    compare the top row sum with the bottom row sum. If the top row sum islarger, then I assume that most of the dark uid is at the top. Similarly,if the left column sum is larger than the right column sum, I assume thatmost of the dark uid is to the left and vice versa.

    Let C 1, C 2 and C 3 be column sums from left to right, and let R1, R2 andR3 be the row sums from bottom to top. Assuming C 1, C 2 and C 3 are goodapproximations to the interface, we can estimate the slope a of the interfacewith a forward, backward and central difference (see gure 5.15):

    a y(C 2 C 1) xa

    y(C 3 C 1)2 x

    a y(C 3 C 2)

    x

    where x and y are the grid spacing.2 There is no need to actually move the colour function values around. The column

    sums are the same either way.

    72

  • 8/7/2019 Kalland Master

    73/142

    0.00 0.11 0.44

    0.11 0.93 1.00

    0.44 1.00 1.00

    0.55

    2.04

    2.44

    0.000.110.44

    0.110.931.00

    0.441.001.00

    Figure 5.14: Histograms in the ELVIRA algorithm sometimes need to bemirrored.

    The normalised normal corresponding to the slope a is (see gure 5.16):

    n =1

    1 + a2 a

    1 (5.66)

    If the normal must be ipped upside down, just negate the y-componentof the normal.

    Similarly for the rows, the inverse slope 3 a 1 may be approximated:

    a 1 x(R2 R1)

    y

    a 1 x(R3 R1)

    2 y

    a 1 x(R3 R2)

    y

    The normalised normal corresponding to the inverse slope a 1 is:

    n =1

    1 + ( a 1)21

    a 1(5.67)

    If the normal must be ipped sideways, just negate the x-component of the normal.

    Now there are six candidates for the normal in the centre cell. The erroris calculated for each normal, then the normal resulting in the smallest error

    3 Since the slope generally is dydx , I calldxdy the inverse slope.

    73

  • 8/7/2019 Kalland Master

    74/142

    Figure 5.15: Forward, backward and central difference give three possibleslopes.

    a

    a

    a1

    a1

    1

    1

    1

    1

    Figure 5.16: The normal corresponding to a given slope and inverse slope.

    Figure 5.17: If most of the dark uid is at the top of the 3 3 block, ipthe block, nd a normal, and ip the normal back.

    74

  • 8/7/2019 Kalland Master

    75/142

    0.93 0.93

    1.00 1.00 0.95

    1.00 0.93 0.21

    0.91 0.18 0.00

    1.00 1.00 0.44

    1.00 0.93 0.11

    0.44 0.11 0.00

    0.00 0.00 0.47

    0.00 0.00 0.07

    0.47 0.07 0.00

    0.00 2 0.00 2 0.47 2

    0.00 2 0.00 2 0.07 2

    0.47 2 0.07 2 0.00 2

    ( )0.5 = 0.67

    =

    f f

    Figure 5.18: Extend the reconstructed interface and calculate the volumefraction for each cell based on the reconstructed interface. Compare withthe colour function values and calculate the L2-norm (Euclidean norm) of the difference.

    is chosen. The error is calculated as follows: Given a normal n , the interfaceis reconstructed for the i, j -th cell, which is the centre cell in the 3 3 block,as described in the section about PLIC reconstruction. Extend the linearinterface to the edges of the entire 3 3 block and calculate the volumefraction f for each of the nine cells. Note that the signed distance must becalculated for each of the cells, as described in the last paragraph in section5.3.1, so that each cell gets its own line equation relative to its centre. Theerror is (see gure 5.18):

    E =i+1

    k= i 1

    j +1

    l= j 1

    (f k,l f k,l )212

    (5.68)

    Use the chosen normal only to reconstruct the interface in the centrecell. Near the boundary, the 3 3 block will include cells that are outsidethe uid domain. These cells, or ghost cells, must have values in accordance

    75

  • 8/7/2019 Kalland Master

    76/142

    with the boundary conditions, as described in section 5.2.

    5.4 Advection

    Advection of the colour function eld can be described mathematically as:

    f t

    + (f u ) = 0 (5.69)where u is the velocity eld and f the colour function eld. I describeadvection only in 2D, but advection in 3D is analogous. Discretise theequation with central difference in space and forward difference in time:

    f n +1i,j f ni,j t

    +(fu )ni+ 1/ 2 ,j (fu )ni 1/ 2 ,j

    x+

    (fv )ni,j + 1/ 2 (fv )ni,j 1/ 2 y

    = 0 (5.70)

    f n +1i,j = f ni,j

    t x

    (fu )ni+ 1/ 2 ,j (fu )ni 1/ 2 ,j t y

    (fv )ni,j + 1/ 2 (fv )ni,j 1/ 2(5.71)

    where f ni,j is the colour function value for the i, j -th cell at the n-th time level,u is the horizontal component of the velocity and v the vertical component.Think of f u and f v as ux rate, that is, an amount of dark uid passingthrough each of the cells four faces per time.

    To keep the interface sharp, the advection algorithm takes the recon-structed linear interface into account when calculating the face centred val-ues of f (f i+ 1/ 2 ,j , f i 1/ 2 ,j , f i,j + 1/ 2 and f i,j 1/ 2 ). Operator splitting seems tobe the simplest approach[26, 32], though unsplit methods are presented in[24, 11]. The advection is split into a horizontal advection and a verticaladvection. If the order of the splitting alternates for each time step, the ad-vection algorithm is second order accurate in time, according to [26]. Thatis, if the colour function is advected horizontally, then vertically at one timelevel, it should be advected vertically, then horizontally at the next timelevel and vice versa. In 3D, the advection is split into three 1D advectionswhere the order is interchanged each time level (3! = 6 different orders)[17].

    Since the advection is performed in two steps, we rst calculate some inter-mediate colour function f , then nally the colour function at the next timelevel f n +1 .

    First, consider horizontal advection through a vertical face between twocells. The horizontal velocity component u i+ 1/ 2 ,j at the centre of the face isknown. During the next time step t, the uid at a distance of less than

    | t u i+ 1/ 2 ,j | upstream from the face will travel through the face as shownin gure 5.19. The hatched area in the gure is the donating region [32].The amount of dark uid inside the donating region can be calculated usingthe method described in the section about calculating volume fractions. Let

    76

  • 8/7/2019 Kalland Master

    77/142

    i i+ i+1

    j ui+,j

    | tu i+,j |

    Figure 5.19: The uid in the hatched area is advected from the left cell tothe right during time step t.

    F i+ 1/ 2 ,j be the volume fraction of dark uid in this region. It is tempting tocalculate the intermediate colour function as:

    f i, j = f ni,j t x

    ((F u)ni+ 1/ 2 ,j (F u)ni 1/ 2 ,j ) (5.72)

    However, in 1D, the uid is not incompressible, and the amount of darkand light uid does not necessarily add up to x y any more. To nd theintermediate colour function, which should be in the interval [0 , 1], one musttherefore divide the amount of dark uid by the total amount of uid[27]:

    f i, j =f ni,j t/ x)(( F u)ni+ 1/ 2 ,j (F u)ni 1/ 2 ,j

    1 ( t/ x) (1 u)ni+ 1/ 2 ,j (1 u)ni 1/ 2 ,j

    =

    f ni,j t/ x)(( F u)ni+ 1/ 2 ,j (F u)ni 1/ 2 ,j1 ( t/ x) uni+ 1/ 2 ,j uni 1/ 2 ,j (5.73)

    Once the intermediate colour function f is found, the interface is re-constructed again from f . The vertical advection remains. Now, the uxthrough the top and bottom face of the cell must be found. Let Gi,j + 1/ 2be the volume fraction of dark uid in the donating region | tv i,j + 1/ 2 | up-stream from the face between the i, j -th and i, j + 1-th cell. Undo thescaling of f to get back the actual amount of dark uid (divided by thecell size) and add the change from vertical advection. Remember that

    77

  • 8/7/2019 Kalland Master

    78/142

    (u i+ 1/ 2 ,j u i 1/ 2 ,j )/ x + ( vi,j + 1/ 2 vi,j 1/ 2 )/ y = 0:

    f n +1i,j = f i,j 1

    t x

    (uni+ 1/ 2 ,j uni 1/ 2 ,j ) t y

    (G v)ni,j + 1/ 2 (G v)ni,j 1/ 2= f i,j f i,j

    t x

    (uni+ 1/ 2 ,j uni 1/ 2 ,j ) t y

    (G v)ni,j + 1/ 2 (G v)ni,j 1/ 2= f i,j + f

    i,j t y

    (vni,j + 1/ 2 vni,j 1/ 2 ) t y

    (G v)ni,j + 1/ 2 (G v)ni,j 1/ 2= f i,j +

    t y

    f i,j (vni,j + 1/ 2 vni,j 1/ 2 ) (G v)ni,j + 1/ 2 (G v)ni,j 1/ 2

    (5.74)

    By inserting equation (5.73) into equation 5.74, one can see that thecombined equation is of the same form as the discretised advection equation(5.71). If one does not use operator splitting, but instead calculates bothhorizontal and vertical uxes naively from the same colour function f n ,some uid will be advected twice as shown in gure 5.20. This easily causesovershoots and undershoots, that is, the colour function value goes belowzero or above one. Unsplit advection algorithms are usually more complex toavoid advecting uid twice. According to [32], overshoots and undershootscan still occur using operator splitting. In such cases, I clamp the colourfunction values to the range [0 , 1]. Thus mass will generally not be conservedexactly, but the errors are relatively small. In [32], the errors are said to be

    in the order of 10 4 in general, which agrees with my experience.

    5.4.1 Time-step restriction

    There are certain apparent conditions that need to be fullled if the advec-tion is to be numerically stable:

    A donating region must not be larger than one cell. Two donating regions must not overlap.The rst condition can be fullled by ensuring that:

    | t u| x (5.75)| t v| y (5.76)

    Two donating regions might overlap if the velocity at the centre of twoopposing faces points away from the cell, as shown in gure 5.21. Theoverlap can be avoided by ensuring that[13]: