Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

The finite-difference method

For an unsteady problem in one dimension

Numerical Fluid
analysis Mechanics

CFD

Computer
Science
Recall the example problem in Lecture 1 …...

2
An example: development of a transient laminar flow in a channel
1. Physical assumptions & governing principles:

² Unidirectional laminar flow of incompressible fluid in a channel

² Initial velocity =0
²The flow is driven by a body force (or constant pressure gradient) starting at t=0
²Governing equations:
y
mass conservation – continuity equation x
2L

momentum equation – Navier-Stokes equations


Physical parameters

² Boundary conditions: zero velocity on the walls

3
An example: development of a transient laminar flow in a channel
2. Mathematical model
y
2L x

Long-time solution implies

This problem can be solved analytically by the method of separation of variables


& numerically by CFD

4
Analytical solution (the method of separation of variables)

Exact solution
u " y2 % ∞ 4(−1)k ( (k + 0.5)2 π 2ν t + ( y+
= $1− 2 ' − ∑ exp *− - cos *)(k + 0.5)π -
u0 # L & k=0 [π (k + 0.5)]3 ) L2 , L,

Note the format of solution


u "y t %
= f$ , 2 '
u0 # L L /ν &

Clearly, the time scale in the problem is


L2 / ν
3. Numerical algorithm: finite-difference method (one of many choices)
Distribution of lattice nodes & Implementation of boundary conditions

PDE has been converted into algebraic equations


3. Numerical algorithm: simulation parameters

PDE has been converted into algebraic equations


4. Realization of computation: a computer code
Program main do it=1, nsteps
implicit none
! on wall implementation, center difference do j=2,N
! Euler time integration u(j) = uold(j) + CFL*(uold(j-1)-2.d0*uold(j)+uold(j+1)) &
!integer,parameter :: N=80, Ntime=1024 + dt*2.d0*u0*anu/aL**2.d0
!real*8,parameter :: dt = 0.002d0 end do
!integer,parameter :: N=40, Ntime=256
!real*8,parameter :: dt = 0.008d0 do j=2,N
integer,parameter :: N=20, Ntime=64 uold(j) = u(j)
real*8,parameter :: dt = 0.032d0 enddo

real*8,parameter :: u0=1.0d0, aL=1.0d0, anu=0.1 t = t +dt


! u0 maximum velocity,aL channel half width,anu kinematic viscosity ! print out solutions
! body force is then g = 2*nu*u0/al^2 if(mod(it,NTime).eq.0) then
real*8, dimension(1:N+1) :: uold, u, uana ! analytical solution with the same N
real*8 ::theta0, theta1, theory1,ss,ycc,CFL do j = 1,N+1
integer :: j,it,nsteps,k ycc = real(j-1)*dy - 1.0d0
real*8 :: dy,pi,t,Tend theory1 = (1.d0- ycc**2.d0)
! I used 100 terms
pi = 4.0d0*atan(1.0d0) do k =0,100
dy = 2.d0*aL/real(N) ss = (0.5d0+real(k))*pi
! initial condition theta0 = 4.0d0*(-1.0)**k/(pi*(real(k)+0.5d0))**3.d0
u = 0.0d0 theta1 =theta0 *exp(-ss*ss*anu*t)*cos(ss*ycc)
uold = u theory1 = theory1 - theta1
CFL = dt*anu/dy/dy end do

write(*,*) 'anu, dt, aL, dy, CFL=', anu, dt, aL, dy, CFL write(55,100)ycc,theory1,u(j)/u0,abs(u(j)-theory1)/theory1
t = 0.d0 end do
Tend = 3.0d0*aL*aL/anu !!!!!!!!!!
nsteps = Tend / dt end if
write(*,*) 'Tend, dt, Ntime, nsteps=', Tend, dt, Ntime, nsteps
100 format(2x, 4f16.12)
enddo
end program

Transient_Laminar_Flow_FD_Codes/Channel_FD_onwall0_class.f90
8
5. Verification and validation

9
Truncation errors
The original PDE The finite-difference approximation
𝜕𝑢 𝜕 "𝑢 𝑢#$%& − 𝑢#$ $
𝑢#'& − 2𝑢#$ + 𝑢#%&
$
= g! + ν " = g! + ν
𝜕𝑡 𝜕𝑦 Δ𝑡 Δ𝑦 "
What are the leading-order truncation-error terms?
(This is related to Homework 2(b))

What we are actually solving by the numerical method is

"𝑢 Δ𝑡 ) Δ𝑡 " ) " +


Δ𝑦 " ,
Δ𝑦 +
𝜕( 𝑢 + 𝜕(( ⋅ + 𝜕((( 𝑢 ⋅ + 𝑂 Δ𝑡 = 𝑔 + 𝜈 ⋅ 𝜕** 𝑢 + 𝜈 ⋅ 𝜕**** 𝑢 ⋅ + 𝜈 ⋅ 𝜕****** 𝑢 ⋅ + 𝑂(Δ𝑦 +,
2 6 12 360

Can we predict the truncation errors?


The concept of the modified equation (1)
" 𝑢 ⋅ -( ) -( ! -.! -."
𝜕( 𝑢 + 𝜕(( + 𝜕((( 𝑢 ⋅ +𝑂 Δ𝑡 ) =𝑔+𝜈 " 𝑢
⋅ 𝜕** +𝜈 +
⋅ 𝜕**** 𝑢 ⋅ +𝜈 ,
⋅ 𝜕****** 𝑢 ⋅ + 𝑂(Δ𝑦 ,)
" , &" ),/

% %
Δ𝑡 &
Δ𝑡 % '
Δ𝑦 % (
Δ𝑦 '
𝜕# 𝑢 = 𝑔 + 𝜈 ⋅ 𝜕$$ 𝑢 − 𝜕## 𝑢 ⋅ − 𝜕### 𝑢 ⋅ + 𝜈 ⋅ 𝜕$$$$ 𝑢 ⋅ + 𝜈 ⋅ 𝜕$$$$$$ 𝑢 ⋅ + 𝑂 Δ𝑡 & + 𝑂 Δ𝑦 ( [𝐴]
2 6 12 360

Our goal is to retain all terms up to 𝑂 Δ𝑡 " , 𝑂(Δ𝑦 +), 𝑂 Δ𝑡 ⋅ Δ𝑦 " ,

"𝑢 ) 𝑢
Δ𝑡 ) 𝑢0
Δ𝑦 "
𝐹𝑟𝑜𝑚 𝐴 , 𝜕(( =𝜈 −⋅ 𝜕**( 𝜕(((
⋅ + 𝜈 ⋅ 𝜕****( 𝑢 ⋅ + 𝑂 Δ𝑡 " + 𝑂 Δ𝑦 + , [𝐵]
2 12
) + + Δ𝑡 ,
Δ𝑦 "
𝐹𝑟𝑜𝑚 𝐴 , 𝜕**( 𝑢 = 𝜈 ⋅ 𝜕**** 𝑢 − 𝜕**(( 𝑢 ⋅ + 𝜈 ⋅ 𝜕****** 𝑢 ⋅ + 𝑂 Δ𝑡 " + 𝑂 Δ𝑦 + , [𝐶]
2 12
' )
𝐹𝑟𝑜𝑚 𝐵 , 𝑡ℎ𝑒𝑛 𝐴 , 𝜕$$## 𝑢 = 𝜈 ⋅ 𝜕$$$$# 𝑢 + 𝑂 Δ𝑡 + 𝑂 Δ𝑦 % = 𝜈 % ⋅ 𝜕$$$$$$
(
𝑢 + 𝑂 Δ𝑡 + 𝑂 Δ𝑦 % , [𝐷]

& ' % (
Δ𝑡 (
Δ𝑦 %
𝐹𝑟𝑜𝑚 𝐶 + [𝐷], 𝑡ℎ𝑒𝑛 𝜕$$# 𝑢 =𝜈⋅ 𝜕$$$$ 𝑢 −𝜈 ⋅ 𝜕$$$$$$ 𝑢 ⋅ + 𝜈 ⋅ 𝜕$$$$$$ 𝑢 ⋅ + 𝑂 Δ𝑡 % + 𝑂(Δ𝑡 ⋅ Δ𝑦 % ) + 𝑂(Δ𝑦 ' )
2 12

𝐹𝑟𝑜𝑚 𝐵 + 𝐷 , ) 𝑢 = 𝜈 ⋅ 𝜕 + 𝑢 + 𝑂 Δ𝑡 + 𝑂 Δ𝑦 " = 𝜈 ) ⋅ 𝜕 ,
𝜕((( "
**(( ****** 𝑢 + 𝑂 Δ𝑡 + 𝑂 Δ𝑦
The concept of the modified equation (2)
" 𝑢 ⋅ -( ) -( ! -.! -."
𝜕( 𝑢 + 𝜕(( + 𝜕((( 𝑢 ⋅ +𝑂 Δ𝑡 ) =𝑔+ " 𝑢
𝜈𝜕** + +
𝜈𝜕**** 𝑢 ⋅ +𝜈 ,
⋅ 𝜕****** 𝑢 ⋅ + 𝑂(Δ𝑦 +)
" , &" ),/

Therefore,
"𝑢 𝛥𝑦 " ,
𝜕(( = 𝜈 "𝜕****
+ 𝑢 − 𝜈 )𝜕******
, 𝑢 ⋅ Δt + 𝜈" 𝜕****** 𝑢 + 𝑂 Δ𝑡 " + 𝑂 Δ𝑡 ⋅ Δ𝑦 " + 𝑂(Δ𝑦 +),
6

" 𝑢
𝜈Δ𝑡 𝛥𝑦 " + "
Δ𝑡 " Δ𝑡 ⋅ Δ𝑦 " Δ𝑦 + ,
𝜕( 𝑢 = 𝑔 + 𝜈𝜕** −𝜈⋅ − 𝜕**** 𝑢 + 𝜈 ⋅ 𝜈 ⋅ −𝜈⋅ + 𝜕 𝑢
2 12 3 12 360 ******
+𝑂 Δ𝑡 ) + 𝑂 Δ𝑡 " ⋅ Δ𝑦 " + 𝑂 Δ𝑡 ⋅ Δ𝑦 + + 𝑂 Δ𝑦 ,
νΔ𝑡
We call this the modified equation 𝛼≡
Δ𝑦 !

" 𝑢
𝜈 ⋅ Δ𝑦 " +
𝜈 ⋅ Δ𝑦 +
𝜕( 𝑢 = 𝑔 + 𝜈𝜕** − ⋅ 6𝛼 − 1 𝜕**** 𝑢 + ⋅ 120𝛼 " − 30𝛼 + 1 𝜕******
, 𝑢
12 360
The steady-state solution of the modified equation (1)
" 𝑢
𝜈 ⋅ Δ𝑦 " +
𝜈 ⋅ Δ𝑦 +
𝜕( 𝑢 = 𝑔 + 𝜈𝜕**− ⋅ 6𝛼 − 1 𝜕**** 𝑢 + ⋅ 120𝛼 " − 30𝛼 + 1 𝜕******
, 𝑢
12 360
+𝑂 Δ𝑡 ) + 𝑂 Δ𝑡 " ⋅ Δ𝑦 " + 𝑂 Δ𝑡 ⋅ Δ𝑦 + + 𝑂 Δ𝑦 ,

The steady state solution:


"
𝜈 ⋅ Δ𝑦 " +
𝜈 ⋅ Δ𝑦 +
0 = 𝑔 + 𝜈𝜕** 𝑢 − ⋅ 6𝛼 − 1 𝜕**** 𝑢 + ⋅ 120𝛼 " − 30𝛼 + 1 𝜕******
, 𝑢
12 360

𝑦" 𝑦+ 𝑦,
𝑢 = 𝑢/ 1−𝑎 " −𝑏 + −𝑐 ,
𝐿 𝐿 𝐿
Boundary conditions:
𝑎+𝑏+𝑐 =1
And
0
1 𝑦" 𝑦+ 𝜈 ⋅ Δ𝑦 " 1 𝑦"
= 𝑔 + 𝜈𝑢/ −2𝑎 " − 12𝑏 + − 30𝑐 , − ⋅ 6𝛼 − 1 −24𝑏 + − 360𝑐 ,
𝐿 𝐿 𝐿 12 𝐿 𝐿
𝜈 ⋅ Δ𝑦 + 1
+ ⋅ 120𝛼 " − 30𝛼 + 1 −720𝑐 ,
360 𝐿
The steady-state solution of the modified equation (2)
0
1 𝑦" 𝑦+ 𝜈 ⋅ Δ𝑦 " 1 𝑦" 𝜈 ⋅ Δ𝑦 +
= 𝑔 + 𝜈𝑢/ −2𝑎 " − 12𝑏 + − 30𝑐 , − ⋅ 6𝛼 − 1 −24𝑏 + − 360𝑐 , +
𝐿 𝐿 𝐿 12 𝐿 𝐿 360
1
⋅ 120𝛼 " − 30𝛼 + 1 −720𝑐 ,
𝐿
Therefore
𝑦+
0 = 𝜈𝑢/ −30𝑐 ,
𝐿
𝑦 " 𝜈 ⋅ Δ𝑦 " 𝑦"
0 = 𝜈𝑢/ −12𝑏 + − ⋅ 6𝛼 − 1 −360𝑐 ,
𝐿 12 𝐿

1 𝜈 ⋅ Δ𝑦 " 1 𝜈 ⋅ Δ𝑦 + 1
0 = 𝑔 + 𝜈𝑢/ −2𝑎 " − ⋅ 6𝛼 − 1 −24𝑏 + + ⋅ 120𝛼 " − 30𝛼 + 1 −720𝑐
𝐿 12 𝐿 360 𝐿,
Finally,

1
𝑐 = 0, 𝑏 = 0, 𝑎 = 1, 0 = 𝑔 + 𝜈𝑢/ −2
𝐿"
The steady state solution is unchanged.
The transient solution of the modified equation
" 𝑢
𝜈 ⋅ Δ𝑦 " +
𝜈 ⋅ Δ𝑦 +
𝜕( 𝑢 = 𝑔 + 𝜈𝜕** − ⋅ 6𝛼 − 1 𝜕**** 𝑢 + ⋅ 120𝛼 " − 30𝛼 + 1 𝜕******
, 𝑢
12 360

4
𝑢1 𝑦" 𝑘 + 0.5 "𝜋 "𝜈1 𝑡 𝑦
= 1 − " + K 𝐶2 exp − cos 𝑘 + 0.5 𝜋
𝑢/ 𝐿 𝐿" 𝐿
23/

Δ𝑦 " 𝑘 + 0.5 "𝜋 " Δ𝑦 + "


𝑘 + 0.5 +𝜋 +
𝜈1 = 𝜈 1 + ⋅ 6𝛼 − 1 + ⋅ 120𝛼 − 30𝛼 + 1
12 𝐿" 360 𝐿+

4 ⋅ −1 2
𝐶2 = − )
𝜋 𝑘 + 0.5

Alternatively,
𝛼 1 2" 𝑘 + 0.5 "𝜋 " 𝛼" 𝛼 1 2+ ⋅ 𝑘 + 0.5 +𝜋 +
𝜈1 = 𝜈 1 + − + − + ,
2 12 𝑁" 3 12 360 𝑁+
νΔ𝑡
𝛼≡ "
Δ𝑦
The theoretical prediction of relative error (1)
Theoretical solution of the modified equation
4
𝑢1 𝑦" 𝑘 + 0.5 "𝜋 "𝜈1 𝑡 𝑦
= 1 − " + K 𝐶2 exp − cos 𝑘 + 0.5 𝜋
𝑢/ 𝐿 𝐿" 𝐿
23/
4 ⋅ −1 2
𝐶2 = − )
𝜋 𝑘 + 0.5
𝛼 1 4 𝑘 + 0.5 "𝜋 " 𝛼 " 16 𝑘 + 0.5 +𝜋 + νΔ𝑡
𝜈1 = 𝜈 1 + − + , 𝛼≡
2 12 𝑁" 3 𝑁+ Δ𝑦 "

Theoretical solution of the original problem


4
𝑢 𝑦" 𝑘 + 0.5 "𝜋 "𝜈𝑡 𝑦
= 1 − " + K 𝐶2 exp − cos 𝑘 + 0.5 𝜋
𝑢/ 𝐿 𝐿" 𝐿
23/

𝑢1 − 𝑢
Predicted relative error =
𝑢
The theoretical prediction of relative error (2)
νΔ𝑡
For fixed 𝛼 ≡
Δ𝑦 "
4
𝑢1 𝑦" 𝑘 + 0.5 "𝜋 "𝜈1 𝑡 𝑦
= 1 − " + K 𝐶2 exp − cos 𝑘 + 0.5 𝜋
𝑢/ 𝐿 𝐿" 𝐿
23/

𝑘 + 0.5 ! 𝜋 ! 𝜈" 𝑡 𝑘 + 0.5 ! 𝜋 ! 𝜈𝑡 𝜈" − 𝜈 𝑘 + 0.5 ! 𝜋 ! 𝜈𝑡 𝑘 + 0.5 ! 𝜋 ! 𝜈𝑡 𝜈" − 𝜈


exp − ! = exp − ! 1+ ≈ exp − ⋅ 1 −
𝐿 𝐿 𝜈 𝐿! 𝐿! 𝜈

.
𝑢* − 𝑢 1 𝛼 1 𝑘 + 0.5 % 𝜋 % 𝜈𝑡 4 𝑘 + 0.5 % 𝜋 % 𝑘 + 0.5 % 𝜋 % 𝜈𝑡 𝑦
=− − C 𝐶+ ⋅ 𝑒𝑥𝑝 − 𝑐𝑜𝑠 𝑘 + 0.5 𝜋
𝑢 𝑢 2 12 𝐿% 𝑁% 𝐿% 𝐿
+,-
1
~
𝑁"
Theoretically, we prove that
(1) The relative error ~ 1/N2
(2) The relative error decreases with dimensionless time
The theoretical prediction of relative error
Last night’s results Today’s corrected results

CFL=0.32, dimensionless time = 0.5


Numerical instability:

A numerical algorithm can become unstable when implemented on


computer, due to computer round-off errors [computer only uses a finite
number of digits to represent a real number], truncation errors, mistakes,
etc.

Computer rounds a number off – making approximation to a number

A single precision number: only about 7 to 8 digits are meaningful


A double precision number: only about 14 to 16 digits are meaningful
[a little demonstration, EX3]
On my MacBook ….

Single precision Double precision

x=4.*atan(1.0) double precision x


write(*,*) x x=4.d0*datan(1.0d0)
stop write(*,*) x
end stop
end
3.14159274
3.1415926535897931

The true value of pi = 3.141592653589793238462643383279502884…


The consequence of round-off errors …

àNumerical instability

àUnphysical results [I have a good example, even wrote a paper!]


Numerical stability
𝑢#$%& = 𝑢#$ + g ⋅ Δ𝑡 + α 𝑢#'&
$
− 2𝑢#$ + 𝑢#%&
$
, 𝑓𝑜𝑟 𝑗 = 2, 3, … , 𝑁

The round-off error equation

𝛿𝑢#$%& = 𝛿𝑢#$ + α 𝛿𝑢#'&


$
− 2𝛿𝑢#$ + 𝛿𝑢#%&
$
= 1 − 2𝛼 𝛿𝑢#$ + α 𝛿𝑢#'&
$ $
+ 𝛿𝑢#%&

The von-Neumann linear stability analysis


/
𝛿𝑢#$ ~𝑒 F( ⋅ 𝑒 G 2⋅*0
~𝑒 F⋅$-( ⋅ 𝑒 G 2⋅#-*

𝑒 1⋅ 345 6#
⋅ 𝑒7 +⋅86$
= 1 − 2𝛼 𝑒 1⋅36# ⋅ 𝑒 7 +⋅86$
+ α 𝑒 1⋅36# ⋅ 𝑒 7 +⋅ 895 6$
+ 𝑒 1⋅36# ⋅ 𝑒 7 +⋅ 845 6$

𝑒 F⋅-( = 1 − 2𝛼 + α 𝑒 G '2⋅-* + 𝑒G 2⋅-* = 1 − 2𝛼 + 2α cos 𝑘Δ𝑥

Stability requirement Stability condition


1 − 2𝛼 + 2α cos 𝑘Δ𝑥 ≤1 1
−1 ≤ 1 − 4𝛼 ≤ 1 𝛼≤
1
2
𝛼≤
2 Conditionally stable
Explicit versus Implicit time integration

𝑢!"#$ − 𝑢!" "


𝑢!%$ − 2𝑢!" + 𝑢!#$
"
= g +ν Explicit
Δ𝑡 Δ𝑦 &

𝑢!"#$ − 𝑢!" "#$


𝑢!%$ − 2𝑢!"#$ + 𝑢!#$
"#$
= g +ν Implicit
Δ𝑡 Δ𝑦 &
Stability condition for the implicit time integration
𝑢#$%& = 𝑢#$ + g ⋅ Δ𝑡 + α 𝑢#'&
$%&
− 2𝑢#$%& + 𝑢#%&
$%&
, 𝑓𝑜𝑟 𝑗 = 2, 3, … , 𝑁

The round-off error equation

𝛿𝑢#$%& = 𝛿𝑢#$ + α 𝛿𝑢#'&


$%&
− 2𝛿𝑢#$%& + 𝛿𝑢#%&
$%&

The von-Neumann linear stability analysis


/
𝛿𝑢#$ ~𝑒 F( ⋅ 𝑒 G 2⋅*0
~𝑒 F⋅$-( ⋅ 𝑒 G 2⋅#-*

𝑒 1⋅ 345 6#
⋅ 𝑒7 +⋅86$
= 𝑒 1⋅36# ⋅ 𝑒 7 +⋅86$
+ α ⋅ 𝑒 1⋅ 345 6#
𝑒7 +⋅ 895 6$
− 2𝑒 7 +⋅86$
+ 𝑒7 +⋅ 845 6$

𝑒 F⋅-( = 1 + 2α −1 + cos 𝑘Δ𝑥 𝑒 F-(

Stability requirement
1 Always met for any α
𝑒 F⋅-( =
1 + 2α 1 − cos 𝑘Δ𝑥
⇒ unconditionally stable
1 1
≤ ≤1
1 + 4α 1 + 2α 1 − cos 𝑘Δ𝑥
Code implementation of the implicit scheme
𝑢#$%& = 𝑢#$ + g ⋅ Δ𝑡 + α 𝑢#'&
$%&
− 2𝑢#$%& + 𝑢#%&
$%&
, 𝑓𝑜𝑟 𝑗 = 2, 3, … , 𝑁

$%&
−α ⋅ 𝑢#'& + 1 + 2𝛼 ⋅ 𝑢#$%& − α ⋅ 𝑢#%&
$%&
= 𝑢#$ + g ⋅ Δ𝑡 , 𝑗 = 2,3,4,5, … . . , 𝑁

Note the boundary condition u&$ = 0, $


𝑢H%& =0

𝑢"$%& 𝑢"$ + 𝑔𝛥𝑡


1 + 2𝛼 −𝛼
𝑢)$%& 𝑢)$ + 𝑔𝛥𝑡
−𝛼 1 + 2𝛼 −𝛼
𝑢+$%& 𝑢+$ + 𝑔𝛥𝑡
−𝛼 1 + 2𝛼 −𝛼
… = …
… … …
… …
−𝛼 1 + 2𝛼 −𝛼 $%& $
𝑢H'& 𝑢H'& + 𝑔𝛥𝑡
−𝛼 1 + 2𝛼 $
𝑢H$%& 𝑢H + 𝑔𝛥𝑡

A tri-diagonal system can be solved directly.


The tri-diagonal solver:
Thomas algorithm
tri_diagonal.f90
Main program Subroutine à

The first time we introduce the concept of subroutine


Summary
We apply the finite-difference & Finite-Volume methods to 1D transient
diffusion problem (Parabolic PDE), where the analytical solution also exists.

Several aspects are considered and illustrated:


Accuracy (truncation error)
Numerical stability

We even predicted the errors in the numerical method!


Explicit vs implicit time-integration methods
No numerical instability in the implicit method

Implicit time-integration leads to a tri-diagonal system

You might also like