Lecture 4: Finite Volume Methods 1/2

V.Vuorinen Aalto University School of CFD Course, Spring 2019

February 4th 2019, Otaniemi [email protected] Overall context

• Typically, OF user confront e.g. NS/CD-eqn discretization by finite volume method via the fvSchemes dictionary • In this and next lecture we will gain insight to this file Overall context

• In the pimpleFoam solver the NS-eqn looks like something like this in the Ueqn.H file • Normally we are not in moving reference frame (MRF) so many of the terms can be ignored • Can you see the connection to NS eqn ? Objectives:

• Hirsch/Chapters 5&6 • Short summary of method for fluid dynamics • From differential form to conservation from • Integration and linkage to Gauss’ theorem • The finite volume method and the concept of Part 1: Summary of finite difference methods in fluid flow context in 2d NS context (generalization to 3d is straightforward)

2 2 x-momentum 2 2 equation

The previous timestep values u v and p enable numerical i,j i,j i,j computation of derivatives in each point (i,j) of the grid. For example (Note: this is the basic principle and much higher accuracy time/space schemes are recommended in practice ): (i,j+1) n+1 n i , j i , j (i-1,j) (i,j) (i+1,j)

n n n n i+1, j i+1 , j i−1 , j i −1, j (i,j-1)

2 n n i , j+1 i , j i , j−1 2 2 How do we get pressure in projection method ? NS in symbolic short hand notation for convection (C), pressure gradient (P) and diffusion (D) terms: ∂u =−C +P+D ∂ t Step 1: find intermediate velocity u* by first ignoring pressure

* n i , j i , j n n (i,j+1) Step 2: calculate div(u*) and recall Poisson equation for pressure (i-1,j) (i,j) (i+1,j) Note 1: requires solution of Ax = b Δ p=∇⋅u * Note 2: x = pressure → p i,j Step 3: correct intermediate velocity to find the div-free final (i,j-1) velocity un+ 1=u *−∇ p ion roject Helmholtz-Hodgea of p Decomposition: A Means of Generating ic ide ll bas ReDivergence-Freeca Vector Fields d metho Any vector field can be written as a sum of two vectorfields: u* u ∇ p u*=u +∇ q such that ∇⋅u*≠0 in general (does not have to be but can be) ∇⋅u=0 Δ q=∇⋅u* ∇×∇ q=0

Since Poisson equation for q is easy to solve we now have a simple algorithm, the projection method, to “project” any vectorfield to a free field “close” To the original field but divergence free.

Step 1: choose any u* Step 2: solve q from Poisson equation Step 3: compute gradient of q * Step 4: u=u*−∇ q u=u −∇ p * * ∂ ui ∂ u j Σi , j =-Δ p ∂ x j ∂ xi Source: original Author unknown Example on usage of projection method: Vorticity field visualization in 2d quasi-turbulent case

t=10s t=30s

Analogy: Mixing of milk in a coffee cup t=90s Part 2: Conservation form of fluid dynamical equations ϕ(x , y ,z ,t) could be a small concentration of moisture = cloud u( x, y , z ,t ) is the velocity of air flow assumed incompressible Differential form Volume average over the differential form ∂ϕ 1 ∂ ϕ 1 1 +∇⋅(u ϕ)=∇⋅(ν ∇ ϕ) dV + ∇⋅(u ϕ)dV = ∇⋅(ν ∇ ϕ)dV ∂t V ∫V ∂ t V ∫V V ∫V

∂u 1 ∂ u 1 1 1 +∇⋅uu=- ∇ p+νΔ u ∫ dV + ∫ ∇⋅uu dV =- ∫ ∇ pdV + ∫ ν Δ udV ∂ t V V ∂t V V V V V V

In finite volume method the governing equations (in differential form) are volume averaged by integrating them over each volume element of the grid. Differential form Conservative form ∂ϕ 1 ∂ ϕ 1 1 +∇⋅(u ϕ)=∇⋅(ν ∇ ϕ) dV + (u ϕ)⋅n dS= ν ∇ ϕ⋅ndS ∂t V ∫V ∂ t V ∫S V ∫S

∂u 1 ∂ u 1 1 1 +∇⋅uu=- ∇ p+ν Δ u ∫ dV + ∫ (u u)⋅ndS=− ∫ pn dS+ ∫ ν ∇ u⋅n dS ∂ t V V ∂t V S V S V S

By Gauss’ theorem volume are converted to of concentration (CD-eqn) or momentum (NS-eqn) over the volume surface boundaries. Next, we discuss:

1) integration in 1d

2) integration in 1d, 2d or 3d is just utilization of Gauss’ theorem

3) how to derive finite volume method for convection-diffusion equation

4) that on uniform grid the finite volume method is equal to finite difference method Integration in 1d

+ x + ∂ϕ x + - ∫ - dx=/ - ϕ=ϕ(x )−ϕ(x ) x ∂ x x

x x- x+ Δ x x+=x+Δ x/2

x+=x−Δ x/2

→ The values of the function at the end points of the interval determine the output of the integral.

→ Note the “heuristic”: if we would integrate a function over a volume, then the values of the integral function at the surface boundaries could possibly determine the output of the integral. Example: The finite volume method for 1d convection- diffusion problem on a uniform grid (u=constant) ∂ϕ ∂ ϕ ∂2ϕ +u =ν u ∂t ∂ x ∂ x2 x = cell face

xi−1 xi xi+1 = cell center where solution is defined

x- x+ Δ x Let us integrate the CD-equation over the length of Δ x

+ 1 x+ ∂ϕ 1 x+ ∂ ϕ 1 x+ ∂2ϕ x =x+Δ x/2 - dx+ - u dx= - ν dx ∫x ∫x ∫x 2 Δ x ∂ t Δ x ∂ x Δ x ∂ x x-=x−Δ x/2

Integration gives:

+ x + + 1 ∂ 1 x 1 x ∂ ϕ - ϕ dx+ / - u ϕ= / - ν Δ x ∂t ∫x Δ x x Δ x x ∂ x Integral in time-derivative term: 1 x+ - ϕ dx≈ϕ Δ x ∫x

The time-derivative term is thus approximated “as it was”: ∂ 1 x+ ∂ϕ - ϕ dx≈ ∂t Δ x ∫x ∂ t

Substitution of left and right values gives for convection term:

+ - 1 x+ ϕ(x )−ϕ(x ) / - uϕ=u Δ x x Δ x

Substitution of left and right values gives for diffusion term:

+ - 1 x + ∂ ϕ 1 ∂ ϕ(x ) ∂ ϕ(x ) / - ν = (ν −ν ) Δ x x ∂ x Δ x ∂ x ∂ x The terms below are interpolants of the transported quantity on the cell face ϕ( x+),ϕ (x-) The terms below are interpolants of the gradients of the transported quantity on the cell face ∂ϕ (x+) ∂ϕ (x-) ν , ν ∂ x ∂ x

How could you get those interpolants ? The terms below are interpolants of the transported quantity on the cell face ϕ( x+),ϕ (x-) The terms below are interpolants of the gradients of the transported quantity on the cell face ∂ϕ (x+) ∂ϕ (x-) ν , ν ∂ x ∂ x

→ On uniform grid one could simply linearly interpolate

+ - ϕ( x )≈0.5(ϕ i+1+ϕi ) ϕ( x )≈0.5(ϕ i+ϕi−1 )

∂ϕ (x+) ϕ −ϕ ∂ϕ (x-) ϕ −ϕ ν ≈ν i +1 i ν ≈ ν i i−1 ∂ x Δ x ∂ x Δ x Thus we started from this integral ... 1 x+ ∂ϕ 1 x + ∂ ϕ 1 x+ ∂2ϕ ∫ - dx+ ∫ - u dx= ∫ - ν dx Δ x x ∂ t Δ x x ∂ x Δ x x ∂ x2

… and find the semidiscrete form (time is not discretized yet):

∂ϕ ϕi +1−ϕ i−1 ϕi+1−2ϕi +ϕi−1 +u =ν ∂t 2Δ x Δ x2

Equivalent ∂ϕ Equivalent ∂2ϕ u ν to the term ∂ x to the term ∂ x2 discretized by finite difference discretized by finite difference method (2nd order central method (2nd order central difference formula for 1st difference formula for 2nd derivative) derivative) → Time discretization could then be applied in various ways e.g. Euler, Runge-Kutta, Crank-Nicolson, …

→ In the simplest case we could use the explicit to get ϕn+1−ϕn ϕn −ϕn ϕn −2ϕ n+ϕn i i +u i+1 i−1 =ν i+1 i i−1 Δ t 2 Δ x Δ x2 which allows directly evaluating the solution at timestep n+1 in each grid point I similar to what will be done in the programming assignment.

Conclusion: by integrating the CD-eqn over an interval dx we have just derived the finite volume method (2nd order space accuracy) for uniform grids in 1d and shown that the outcome is exactly the same as for finite difference method. The Gauss Theorem i.e. or Just

• N Simply: “Generalization of Integration”

• Gauss theorem to convert volume integrals to surface integrals

∇⋅ = ⋅ ∫V F dV ∫S F n dS

• Convection terms are of the divergence form ∇⋅(u ϕ)dV = (u ϕ)⋅n dS V (volume) ∫V ∫S dS(surfacearea) • Diffusion terms are also of the divergence form n ∇⋅(ν ∇ ϕ) = ∇ ϕ⋅ ∫V dV ∫S n dS

Observation: Transport equations can be written in flux form so that the rate of change of a quantity within a control volume V depends on the fluxes through the boundary S → the basic idea of finite volume method. Part 4: About homework 2 Free boundary layer (topic of HW2)

Hot metal plates

Confined flow between plates

U ∞

T∞

Figure: courtesy of P.Peltonen Motivation for HW 2: Fluid solid interfaces (boundary layers) are crucial in heat transfer, flow friction, turbulence production etc

Source for pictures from instructor resources: Incropera, de Witt, τ Heat and Mass Transfer = s Friction coefficient C f 2 ρ U ∞ /2 (local)

Dynamic ∂ u Surface viscosity τs =μ μ=ρ ν ( ∂ y ) y=0 shear stress q''=h(T −T ) Newton's s s ∞ cooling law ∂T q''=−k Fourier's s T ( ∂ ) law y y=0

kT Fluid thermal conductivity =νT ρ c p Part 5: Gauss’ theorem and the finite volume method in OpenFOAM (to be discussed in Lecture 5) The Gauss Theorem i.e. Divergence Theorem or Just

• N Simply: “Generalization of Integration”

• Gauss theorem to convert volume integrals to surface integrals

∇⋅ = ⋅ ∫V F dV ∫S F n dS

• Convection terms are of the divergence form ∇⋅(u ϕ)dV = (u ϕ)⋅n dS V (volume) ∫V ∫S dS(surfacearea) • Diffusion terms are also of the divergence form n ∇⋅(ν ∇ ϕ) = ∇ ϕ⋅ ∫V dV ∫S n dS

Observation: Transport equations can be written in flux form so that the rate of change of a quantity within a control volume V depends on the fluxes through the boundary S → the basic idea of finite volume method. OpenFOAM has unstructured grid topology Vector and scalar quantities stored in cell centers (point P) while fluxes can be interpolated on cell faces from adjacent points. OpenFOAM has unstructured grid topology “Unstructured” grid topology implemented with very handy data types

The link between data types and OpenFOAM top-level code syntax? dC = dt*(fvc::laplacian(nu, C) ± fvc::div(phi,C));

The link between OpenFOAM top-level code syntax and mathematical eqs?

∂ϕ +∇⋅(u ϕ)=∇⋅ν ∇ ϕ ∂t The Gauss-theorem forms the basics of the finite volume method – the method in OpenFOAM Differential form Conservation form 1 ∂ϕ 1 ∂ ϕ 1 1 +∇⋅(u ϕ)=∇⋅(ν ∇ ϕ) ∫ dV + ∫ (u ϕ)⋅ndS= ∫ ν ∇ ϕ⋅ndS V ∫V ∂t V V ∂ t V S V S

∂u 1 ∂ u 1 1 1 1 +∇⋅uu=- ∇ p+ν Δ u ∫ dV + ∫ (u u)⋅ndS=− ∫ pndS+ ∫ ν ∇ u⋅n dS ∂ t V V ∂t V S V S V S V ∫V

In the finite volume discretization method the fluxes on the cell faces are numerically estimated utilizing interpolation functions. Integration replaced by summation. 1 1 (u ϕ)⋅n dS≈ Σ (u ϕ) ⋅n dS V ∫S V faces f f f Here the subscript “f” refers to cell face center. 1 1 ν ∇ ϕ⋅n dS≈ Σ (ν ∇ ϕ) ⋅n dS V ∫S V faces f f f Example: Discretize Derivative of a Function u=u(x) Using the FVM

∂ u 1 1 1 ui +1/ 2−ui−1/ 2 ux= ≈ ∫ ux dx dy dz= ∫ ux dx= ∫ udS nx= ∂ x Δ x Δ y Δ z Δx Δ x Δ x Δ x Δ x Δ x

(-1,0,0) (1,0,0)

x=i Δ x Summary of Some Discretization Options (uniform grid considered here) ∂u ∂uu ∂ vu ∂ wu −1 ∂ p ∂2u ∂2 u ∂2u + + + = ρ +ν 2 +ν 2 +ν 2 ∂t ∂ x ∂ y ∂ z ∂ x ∂ x ∂ y ∂ z

Option 1: ∂u ui +1−ui−1 ui+1/ 2−ui −1/ 2 ≈ = u =0.5(u +u ) Finite ∂ x 2Δ x Δ x i +1/ 2 i+1 i Difference Method (here 2nd order) Gauss-theorem Option 2: ∂ u 1 1 ui+1/2−ui−1/ 2 Finite u = ≈ u d Ω= udSn = x Ω ∫Ω x Ω∫∂Ω x Volume Method ∂ x Δ x (here 2nd order)

For example the convection term: C=∇⋅(uu) Note: conservative, in OpenFOAM: C = fvc::div(phi,U); skew-symmetric form Some Options for Numerical Techniques

∂u ∂ u ∂ u ∂u −1 ∂ p ∂2 u ∂2u ∂2 u +u +v +w = ρ +ν 2 +ν 2 +ν 2 ∂t ∂ x ∂ y ∂ z ∂ x ∂ x ∂ y ∂ z

Option 3: Fourier ik⋅r ux=Σk ik x uk (t)e Pseudo-Spectral Methods or in Matlab: dUdx = ifft2(i*KX.*fft2(U));

Vuorinen et al., DNSLAB: A Gateway to Turbulent Flows in Matlab, Computer Phys. Communications (2016) Thank you for your attention!