You are on page 1of 27

Lecture 1: Fundamentals 1/3: Discretization of

Convection-Diffusion Equation by Finite Difference Method

V.Vuorinen
Aalto University School of Engineering
CFD Course, Spring 2018

January 8th 2018, Otaniemi


ville.vuorinen@aalto.fi
Overview
• Convection-diffusion equation
• Finite difference discretization
• Numerical discretization error
Part 1: Convection-Diffusion Equation
Convection-Diffusion Equation

∂ϕ1
+ ∇⋅(u ϕ1)=∇⋅( ν ∇ ϕ1 )
∂t

∂ϕ 2
+ ∇⋅( u ϕ2)=∇⋅( ν ∇ ϕ 2)
∂t

Scalar sources
(small amount of
released colored smoke)
Convection-Diffusion Equation
• It is of crucial importance to understand scalar transport and mixing
in a flow. E.g. energy, combustion, heat transfer etc.
• E.g. fluid temperature and concentration are transported scalars
• In fluid mechanics, transport phenomena are described in terms of
partial differential equations (PDE’s)
• Transport mechanisms: convection and diffusion
• Convection: velocity field transports a scalar
• Diffusion: molecular diffusivity transports a scalar
• CD eqn is highly relevant transport equation because most fluid
dynamical processes (CD, Navier-Stokes) can be described with it
Convection-Diffusion Equation
ν=molecular diffusivity, [ν]=m2/s
u=fluid velocity

∂ϕ
+∇⋅(u ϕ)=∇⋅( ν ∇ ϕ)
∂t

Scalar changes Scalar changes


in time in a given position in space because of
molecular diffusivity + 2nd derivatives

Scalar changes
in space because of
velocity field + 1st derivatives
Transport Phenomena Explained in 1d
ϕ=ϕ( x−ut )
Case 1: Convection time=t
∂ϕ ∂ϕ
+u =0
∂t ∂x
x Shape → unchanged
Case 2: Diffusion Amplitude → unchanged
Position → moves to the direction of velocity
∂ϕ ∂2 ϕ

∂t ∂ x2 x Shape → spreads/diffuses
Amplitude → decreases
Case 3: Convection-diffusion Position → center fixed but “spreads”
∂ϕ ∂ϕ ∂2 ϕ
+u =ν
∂t ∂x ∂ x2

Shape → spreads/diffuses
Amplitude → decreases
Position → moves to the direction of velocity
x
Recap of Notation and Opening Up the Differential
Operators
The gradient is a vector operator:

∇= ⃗i ∂ + ⃗j ∂ + ⃗
k ∂
∂x ∂y ∂z

Gradient of a scalar function is a vector:


∂ϕ ⃗ ∂ϕ ⃗ ∂ϕ
∇ ϕ= ⃗i +j +k
∂x ∂y ∂z

Divergence of a vector is the scalar product of gradient with vector which is


a scalar:
∂ u1 ∂ u2 ∂ u3
∇⋅u= + +
∂ x ∂ y ∂z
Recap of Notation and Opening Up the Differential
Operators

Convection term is the divergence of product of scalar and vector functions


∇⋅(u ϕ)=ϕ ∇⋅u+u⋅∇ ϕ Note: Only 1st derivatives

Diffusion term (assuming constant diffusivity) is the divergence of scalar gradient


∂2 ϕ ∂2 ϕ ∂2 ϕ
∇⋅(ν ∇ ϕ)=ν ∇⋅∇ ϕ=ν +ν +ν Note: Only 2nd derivatives
∂ x2 ∂ y2 ∂ z2
Part 2: Finite Difference Discretization of
Convection Equation (Here assume diffusivity = 0)

Note: This is a brief first look and we will go more into detail later on.
Basic idea of numerical computation of space
derivative using the finite difference method
Numerical derivative df/dx Original function f(x)=sin(x)
Vuorinen et al. Comp.&Fluids (2013)

1st order upwind method (UW1)

( )
∂f f i−f i−1
≈ + O(Δ x )
∂x i Δx
2nd order central difference method (CD2)
L2-error (squared)

( )
∂f f i+1−f i −1 2
1 N points ≈ +O( Δ x )
ϵ 2= Σi=1 (g analytic ( x i)−gnumeric (x i ))2 ∂x i 2Δ x
N points
Examples of some basic simulation approaches to solve convection
equation in 1d

∂c ∂c
+u =0
∂t ∂x
1st order Euler method in time Explicit update scheme
2nd order explicit central difference in space (CD2)

n+1 n n n
c i −c i c −c i−1 c ni+1−c ni−1
+ u i +1 =0 c n +1 n
=c −Δ t u
Δt 2Δ x i i
2Δ x

1st order Euler, implicit Central Space (CD2) Implicit scheme – linear solver for
a tri-diagonal equation Ax=b required
c n+1 n n+1 n+ 1
−c c −c
i i
+ u i +1 i−1
=0 n +1
A c i =c i
n
Δt 2Δ x

ci a vector containing
N elements of the solution
Impact of 2nd order space scheme to convection
u=1 m/ s
∂c ∂c
+u =0 u Δt Courant
∂t ∂x Co= =0.1
Δx number
c n+1 −c n
c n
−c n CD2
i i
+ u i +1 i−1
=0 (2nd order central) L
Δ x= =1 m/ 100
Δt 2Δ x N

2 2

Initial condition c o ( x)=e


−((x−0.25 ) )/(2⋅0.06 )
Shape after one pass-by time T =L/ u

Initial function (Gaussian) can Numerical dispersion from the


be understood as a sum of front side of the pulse also
Fourier-components i.e. Increases the height above
trigonometric, orthogonal maximum initial value (= 1).
functions

Periodic boundary conditions High frequency Fourier-components


With wavelength close to grid resolutions
are not numerically resolved and “left behind”
→ dispersive wave characteristics
(waves move at different speeds)
Impact of 1st order space scheme to convection
u=1 m/ s
∂c ∂c
+u =0 u Δt Courant
∂t ∂x Co= =0.1
Δx number
c n+1 −c n
c n
−c n UW1
i i
+u i i −1
=0 (1st order upwind) L
Δ x= =1 m/ 100
Δt Δx N

2 2

Initial condition c o ( x)=e


−((x−0.25 ) )/(2⋅0.06 )
Shape after one pass-by time T =L/ u

Initial function (Gaussian) can Numerical diffusion decreases


be understood as a sum of the height about 40% below
Fourier-components i.e. the maximum initial value (= 1).
trigonometric, orthogonal
functions

Periodic boundary conditions The very strong numerical diffusion of


1st order upwind scheme cases
damps all Fourier-components
→ diffusive wave characteristics
(waves don't even have time to disperse
As they are damped so quickly)
Key Take-Away Message on Convection Equation
Discretization
- Even order convection schemes (e.g. CD2) are dispersive (in particular
smaller wavelengths are left behind)
- Odd order convection schemes (e.g. UW1) are diffusive (heavy damping of
all wavelengths, damping strongest for small wavelengths)
- High space-time resolution and good grid quality needed to resolve the
flow and in order to minimize numerical diffusion and dispersion
Numerical diffusion in pure convection Numerical dispersion in pure convection
problem (1st order upwind - UW1) problem (2nd order central - CD2)
Part 3: Implementation hints in Matlab
Discrete Representation of the
values of solution c(x) in an array
i=1 i=2 i=3 i=N+1 i=N+2
t=t c1 c2 c3 cN+1 cN+2
n+2

t=t c1 c2 c3 cN+1 cN+2


n+1

t=t c1 c2 c3 cN+1 cN+2


n

t n=n Δ t ,n=0,1,2,...
Boundary Condition Types
• The problem: some numerical value needs to be assigned to the ”ghost cells”
• Otherwise: we can not calculate space derivative in cells i=2 and i=N+1
• Case 1: Given value (Dirichlet or “fixed value”) → flux through boundary
• Case 2: Isolated (Neumann or “zero-gradient”) → no flux through boundary
• Case 3: Given flux → flux through boundary

Ghost i=2 i=3 i=N+1 Ghost


cell c2 c3 cN+1 cell
i=1 i=N+2

x=0 x=L
Case 1: n
(c +c )/2=c L
1
n
2 (c nN +1 +c nN +2 )/2=c R
Case 2: c n1=c n2 c nN +1=c nN +2
Case 3: −k (c n2−c1n)/Δ x=q L k (c nN +2−cnN +1)/ Δ x =q R
→ In all the cases a “ghost cell” value is needed.
→ Ghost cell: we can imagine a virtual cell outside the domain
where we enter a temperature value so that the desired BC
becomes exactly fulfilled.
Summary of the Numerical Solution
Scheme for 1d Convection Equation
1) Set boundary conditions to cells 1 and N+2 c ni (Known)
using T from step n.

n+1 n c ni+1−c ni−1


c =c −Δ t u
2) Update new solution at timestep n+1 in the i i
2Δ x
internal cells 2...N+1
c n+1
i

3) Update time according to t = t + dt t n+1 =t n +Δ t

4) Go back to 1)
Extremely Short to Program for
Solving CD-Eqn in Matlab
Program: explicit Euler in time and CD2 in space
Boundary conditions: periodic

Main for-loop:
L = 1; N=100; dt = 0.0001;
u=1; dx = L/N;

for(t=1:K)
% update solution in inner points
c(in) = c(in) - (dt*u)*(c(rhs)-c(lhs))/(2*dx);
end

Note: I use constantly the “trick” which makes Matlab-programs often very fast.
% define a table which refers to the 'inner points' Example for N+1 = 5
in = 1:N; rhs = in+1; lhs=in-1; rhs(N)=1; lhs(1)=N;
Part 4: In Depth Analysis (Most likely left for
lectures later on)
Fourier analysis of convection diffusion equation
The convection diffusion equation
∂c ∂c ∂2 c 1st derivative is responsible for convection,
+u =ν 2nd derivative is responsible for diffusion
∂t ∂x ∂x2

Insert Fourier Ansatz into the convection diffusion equation


2
o ik ( x−u t ) −k ν t
c^ k =^c e
k e Solution to the equation

Interpretation 1 – imaginary component of the mode e ik (x− u t)


corresponds to traveling wave part of the solution:

Interpretation 2 – real component of the mode corresponds 2


−k ν t
to wave damping part of the solution: e
Numerical truncation error terms have an overall effect that actually
“numerics solves different equations than continuum equations”

Focus on the convection equation


∂c ∂c
+u =0
∂t ∂x
Analytically, this equation describes wave transport i.e. shifting in space.
Thus, the Fourier mode would be purely dispersive i.e. wave transport but no damping

c^ k =c^ ok e ik ( x−u t )
Discretizing convection equation with CD2 (O(dx^2)) leads to error term
proportional to third derivative. So-called “modified equation” can be easily derived
3
∂c ∂c 2 ∂ c
+u =C 1 u Δ x + H . O .T .
∂t ∂x ∂x 3

So previously the dispersive effect of CD2 is explained by the appearing


odd derivatives changing the propagation velocity of higher wave-components.
The H.O.T. include higher order terms with odd derivatives as well.
Similar exercise with UW1 (O(dx)) would leads to error term proportional to second
derivative. So-called “modified equation” can be derived for 1st order UW as well:

∂c ∂c ∂2 c
+u =C 2 u Δ x + H . O .T .
∂t ∂x ∂x 2

So previously the diffusive effect of UW1 is explained by the appearing


even derivatives damping the wave-components. The H.O.T. include higher order
terms with even derivatives as well.

So, numerical discretization leads to solving ... … when the real equation
3 one would like to solve is:
∂c ∂c 2 ∂ c
Dispersive +u =C 1 u Δ x + H . O .T . ∂c ∂c
∂t ∂x ∂x 3
+u =0
∂t ∂x
∂c ∂c ∂2 c
Diffusive +u =C 2 u Δ x + H . O .T . → Motivation towards high order
∂t ∂x ∂x 2
methods to minimize errors.
Thank you for your attention!

You might also like