You are on page 1of 47

WPI Computational Fluid Dynamics I

A Finite Difference Code


for the Navier-Stokes
Equations in
Vorticity/Stream Function
Formulation
Instructor: Hong G. Im
University of Michigan
Fall 2001
WPI Computational Fluid Dynamics I
Objectives:

Develop an understanding of the steps involved in


solving the Navier-Stokes equations using a numerical
method

Write a simple code to solve the “driven cavity”


problem using the Navier-Stokes equations in vorticity
form
WPI Computational Fluid Dynamics I
Outline

• The Driven Cavity Problem


• The Navier-Stokes Equations in Vorticity/Stream
Function form
• Boundary Conditions
• Finite Difference Approximations to the Derivatives
• The Grid
• Finite Difference Approximation of the
Vorticity/Streamfunction equations
• Finite Difference Approximation of the Boundary
Conditions
• Iterative Solution of the Elliptic Equation
• The Code
• Results
• Convergence Under Grid Refinement
WPI Computational Fluid Dynamics I
The Driven Cavity Problem

Moving wall

Stationary
walls
WPI Computational Fluid Dynamics I
Nondimensional N-S Equation

Incompressible N-S Equation in 2-D

∂u ∂u ∂u 1 ∂p  ∂ 2u ∂ 2u 
+u +v =− + ν  2 + 2 
∂t ∂x ∂y ρ ∂x  ∂x ∂y 
Nondimensionalization
~ ~ ~ ~ = ρ
u = u /U , x = x / L , t = tU / L , p p / U 2

 ~
U ∂u ~ ∂u ~ ∂ u
2 ~ ~  ~
U ∂p νU ∂ u ∂ u 
2
 2~ 2~

 ~ + u ~ + v ~  = − ~ + 2  ~ 2 + ~ 2 
L  ∂t ∂x ∂y  L ∂x L  ∂ x ∂y 
∂u~ ~ ∂u~ ~ ∂u~ ∂~
p ν1  ∂ 2u~ ∂ 2u~ 
~ +u ~ +v ~ = − ~ +  ~ 2 + ~ 2 
∂t ∂x ∂y ∂x ULRe  ∂x ∂y 
WPI Computational Fluid Dynamics I
The vorticity/stream function equations

The Vorticity Equation


∂ ∂u ∂u ∂u ∂p 1  ∂ 2 u ∂ 2u 
− +u +v = − + 2 +
∂y ∂ t ∂x ∂y 
∂x Re ∂x ∂y 
2

∂ ∂v ∂v ∂v ∂p 1  ∂ 2 v ∂ 2 v 
+u +v = − + 2 +
∂ x ∂ t ∂ x ∂ y ∂ y Re  ∂ x ∂y 2

∂ω ∂ω ∂ω 1 

2
∂ ω ∂ ω2
+u +v = 2 + 2
∂t ∂x ∂y Re  ∂x ∂y 
∂v ∂u
ω= −
∂x ∂y
WPI Computational Fluid Dynamics I
The vorticity/stream function equations

The Stream Function Equation


Define the stream function
∂ψ ∂ψ
u= , v=−
∂y ∂x
which automatically satisfies the incompressibility conditions
∂u ∂v
+ =0
∂ x ∂y
by substituting
∂ ∂ψ ∂ ∂ψ
− =0
∂x ∂ y ∂y ∂x
WPI Computational Fluid Dynamics I
The vorticity/stream function equations

Substituting
∂ψ ∂ψ
u= ; v=−
∂y ∂x
into the definition of the vorticity
∂ v ∂u
ω= −
∂ x ∂y
yields

∂ψ ∂ψ
2 2

2 + 2 = −ω
∂x ∂y
WPI Computational Fluid Dynamics I
The vorticity/stream function equations

The Navier-Stokes equations in vorticity-stream


function form are:
Advection/diffusion equation

∂ω ∂ψ ∂ω ∂ψ ∂ω 
1  ∂ 2
ω ∂ 2
ω
=− + + 2 +
∂t 
∂y ∂x ∂x ∂y Re ∂x ∂y 2

Elliptic equation
∂ψ ∂ψ
2 2

2 + 2 = −ω
∂x ∂y
WPIBoundary
Computational Fluid Dynamics I
Conditions for the Stream Function

At the right and


the left boundary:

∂ψ
u = 0⇒ =0
∂y
⇒ ψ = Constant
WPIBoundary
Computational Fluid Dynamics I
Conditions for the Stream Function

At the top and the


bottom boundary:

∂ψ
v =0⇒ =0
∂x
⇒ ψ = Constant
WPIBoundary
Computational Fluid Dynamics I
Conditions for the Stream Function

Since the boundaries


meet, the constant
must be the same on
all boundaries

ψ = Constant
WPI Computational Fluid Dynamics I
Boundary Conditions for the Vorticity

The normal velocity is zero since the stream function


is a constant on the wall, but the zero tangential
velocity must be enforced:
At the right and left boundary: At the bottom boundary:

∂ψ ∂ψ
v =0⇒ =0 u = 0⇒ =0
∂x ∂y
At the top boundary:
∂ψ
u = U wall ⇒ = U wall
∂y
WPI Computational Fluid Dynamics I
Boundary Conditions for the Vorticity

The wall vorticity must be found from the streamfunction.


The stream function is constant on the walls.

At the right and the left boundary:

∂ψ ∂ψ
2 2
∂ψ 2

2 + 2 = −ω ⇒ ω wall =− 2
∂x ∂y ∂x
Similarly, at the top and the bottom boundary:
2
∂ψ ∂ψ
2 2 ∂ψ
2 + 2 = −ω
⇒ ω wall =− 2
∂x ∂y ∂y
WPI Computational Fluid Dynamics I
Summary of Boundary Conditions

∂ψ ∂ 2ψ
= Uwall ; ω wall = − 2
∂y ∂y

∂ψ ∂ψ
=0 =0
∂x ∂x
2
ψ = Constant
∂ψ 2
∂ψ
ω wall =− 2 ω wall =− 2
∂x ∂x

∂ψ ∂ 2ψ
= 0; ω wall = − 2
∂y ∂y
WPI Computational Fluid Dynamics I
Finite Difference Approximations

To compute an approximate solution numerically,


the continuum equations must be discretized.
There are a few different ways to do this, but we
will use FINITE DIFFERENCE approximations
here.
WPI Computational Fluid Dynamics I
Discretizing the Domain

Uniform mesh (h=constant)

j=NY

ψ i, j and ω i , j
Stored at
each grid
point

j=2
j=1
i=1 i=2 i=NX
WPI Computational Fluid Dynamics I
Finite Difference Approximations

When using FINITE DIFFERENCE approximations,


the values of f are stored at discrete points and the
derivatives of the function are approximated using a
Taylor series:

Start by expressing the value of f(x+h) and f(x-h)


in terms of f(x)

f(x-h) f(x) f(x+h)

h h
WPI Computational Fluid Dynamics I
Finite Difference Approximations

Finite difference approximations

∂f ( x ) f ( x + h ) − f ( x − h ) ∂ f ( x ) h
3 2
= + +!
∂x 2h ∂x 3
6
∂ 2 f ( x ) f ( x + h) − 2 f ( h) + f ( x − h) ∂ 4 f ( x ) h 2
= + +!
∂x 2
h 2
∂x 12
4

∂f (t) f (t + ∆t) − f (t) ∂ 2 f (t) ∆t


= + 2 +!
"" ∂t ∆t ∂t 2

2nd order in space, 1st order in time


WPI Computational Fluid Dynamics I
Finite Difference Approximations

For a two-dimensional flow discretize the variables on


a two-dimensional grid

j+1
(x, y)
fi , j = f (x,y)
j fi +1, j = f (x + h, y)
j-1 fi , j +1 = f (x, y + h)
i -1 i i+1
WPI Computational Fluid Dynamics I
Finite Difference Approximations

Laplacian

∂2 f ∂ 2 f
2 + 2 =
∂x ∂y
n n n n n n
fi +1, j − 2 fi, j + fi−1, j fi, j +1 − 2 f i, j + fi , j −1
2 + 2 =
h h
n n n n n
f
i +1, j + f
i −1, j +fi, j +1 +f
i , j −1 − 4f
i,j
2
h
WPI Computational Fluid Dynamics I
Finite Difference Approximations

∂ω ∂ψ ∂ω ∂ψ ∂ω 1 ∂ ω ∂ ω 
 2 2
=− + +  2 + 2 
∂t ∂y ∂x ∂x ∂y Re  ∂x ∂y 
Using these approximations, the vorticity equation becomes:
ω in, +j 1 − ω in, j
=
∆t
 ψ in, j +1 − ψ in, j −1  ω in+1, j − ω in−1, j   ψ in+1, j −ψ in−1, j  ω in, j +1 − ω in, j −1 
−  +  
 2 h  2h   2h  2 h 
     
 ω n
1  i +1, j + ω n
+ ω n
+ ω n
− 4ω n

+
i −1, j i , j +1 i , j −1 i, j

Re  h2 

WPI Computational Fluid Dynamics I
Finite Difference Approximations

The vorticity at the new time is given by:

  ψ n
− ψ n
 ω n
− ω n

ω in, +j 1 = ω in, j − ∆t  i , j +1 i , j −1
 i +1, j

i −1, j


 2h  2h 
 ψ in+1, j −ψ in−1, j  ω in, j +1 − ω in, j −1 
−  
 2 h  2 h 
  
 ω n
1  i +1, j + ω n
+ ω n
+ ω n
− 4ω n

+
i −1, j i , j +1 i , j −1 i, j

Re  h2 

WPI Computational Fluid Dynamics I
Finite Difference Approximations

The stream function equation is:

∂ψ ∂ψ
2 2

2 + 2 = −ω
∂x ∂y

ψ in+1, j + ψ in−1, j + ψ ni, j +1 + ψ ni, j −1 − 4ψ i,n j n


2 = −ω i, j
h
WPI Computational Fluid Dynamics I
Discretized Domain

Discretize the domain


j=ny ψ i, j = 0
for
i =1
i = nx
j =1
j = ny
j=2
j=1
i=1 i=2 i=nx
WPI Computational Fluid Dynamics I
Discrete Boundary Condition

j=3

j=2

j=1 ω wall = ω i, j =1
Uwall
i -1 i i+1
2
∂ψ i, j =1 ∂ ψ i , j =1 h 2 3
ψ i, j = 2 = ψ i, j =1 + h+ 2 + O(h )
∂y ∂y 2
WPI Computational Fluid Dynamics I
Discrete Boundary Condition

2
∂ψ i, j =1 ∂ ψ i , j =1 h 2 3
ψ i, j = 2 = ψ i, j =1 + h+ 2 + O(h )
∂y ∂y 2
Using: 2
∂ ψ i, j =1 ∂ψ i, j =1
ω wall =− 2 ; Uwall =
∂y ∂y
This becomes:
h2 3
ψ i, j = 2 = ψ i, j =1 + Uwall h − ω wall + O(h )
2
Solving for the wall vorticity:
2 2
ω wall = (ψ i , j =1 − ψ i , j =2 ) 2 + Uwall + O(h)
h h
WPI Computational Fluid Dynamics I
Discrete Boundary Condition

At the bottom wall (j=1):


2 2
ω wall = (ψ i , j =1 − ψ i , j =2 ) 2 + Uwall + O(h)
h h
Similarly, at the top wall (j=ny):
2 2
ω wall = (ψ i , j = ny −ψ i , j = ny −1 ) 2 − U wall + O(h)
h h
At the left wall (i=1):

Fill the blank


At the right wall (i=nx):
Fill the blank
WPI Computational Fluid Dynamics I
Solving the Stream Function Equation

Solving the elliptic equation:

n n n n n
ψ i +1, j +ψ i −1, j +ψ i, j +1 +ψ i, j −1 − 4ψ i, j n
2 = −ω i, j
h
Rewrite as
n +1 n n n n 2 n
ψ i, j = 0.25 (ψ i +1, j +ψ i−1, j +ψ i, j +1 +ψ i, j −1 +h ω ) i, j

j+1
j
j-1
i-1 i i+1
WPI Computational Fluid Dynamics I
Solving the Stream Function Equation

If the grid points are done in order, half of the points have
already been updated:

j+1
j
j-1
i-1 i i+1

Successive Over Relaxation (SOR)

ψ i,n +j 1 = β 0.25 (ψ ni+1, j + ψ ni−+1,1 j + ψ ni, j +1 + ψ in,+j 1−1 + h2ω in, j )


+ (1 − β )ψ i,n j
WPI Computational Fluid Dynamics I
Time Step Control

Limitations on the time step

ν ∆t 1 (| u | + | v |) ∆t
2 ≤
≤2
h 4 ν
WPI Computational Fluid Dynamics I
Solution Algorithm

Initial vorticity given


for i=1:MaxIterations
for i=2:nx-1; for j=2:ny-1
s(i,j)=SOR for the stream function
Solve for the stream function end; end
end

Find vorticity on boundary v(i,j)=

for i=2:nx-1; for j=2:ny-1


Find RHS of vorticity equation rhs(i,j)=Advection+diffusion
end; end

v(i,j)=v(i,j)+dt*rhs(i,j)
Update vorticity in interior

t=t+∆t
WPI Computational Fluid Dynamics I
MATLAB Code

1 clf;nx=9; ny=9; MaxStep=60; Visc=0.1; dt=0.02; % resolution & governing parameters


2 MaxIt=100; Beta=1.5;MaxErr=0.001; % parameters for SOR iteration
3 sf=zeros(nx,ny); vt=zeros(nx,ny); w=zeros(nx,ny); h=1.0/(nx-1);t=0.0;
4foristep=1:MaxStep, % startthe time integration
5 foriter=1:MaxIt, % solve forthe streamfunction
6 w=sf; % by SOR iteration
7 fori=2:nx-1; forj=2:ny-1
8 sf(i,j)=0.25*Beta*(sf(i+1,j)+sf(i-1,j)...
9 +sf(i,j+1)+sf(i,j-1)+h*h*vt(i,j))+(1.0-Beta)*sf(i,j);
10 end; end;
11 Err=0.0;fori=1:nx; forj=1:ny, Err=Err+abs(w(i,j)-sf(i,j)); end; end;
12 if Err <= MaxErr, break, end % stop ifiteration has converged
13 end;
14 vt(2:nx-1,1)=-2.0*sf(2:nx-1,2)/(h*h); % vorticity on bottom wall
15 vt(2:nx-1,ny)=-2.0*sf(2:nx-1,ny-1)/(h*h)-2.0/h; % vorticity on top wall
16 vt(1,2:ny-1)=-2.0*sf(2,2:ny-1)/(h*h); % vorticity on right wall
17 vt(nx,2:ny-1)=-2.0*sf(nx-1,2:ny-1)/(h*h); % vorticity on left wall
18 fori=2:nx-1; forj=2:ny-1 % compute
19 w(i,j)=-0.25*((sf(i,j+1)-sf(i,j-1))*(vt(i+1,j)-vt(i-1,j))... % the RHS
20 -(sf(i+1,j)-sf(i-1,j))*(vt(i,j+1)-vt(i,j-1)))/(h*h)... % of the
21 +Visc*(vt(i+1,j)+vt(i-1,j)+vt(i,j+1)+vt(i,j-1)-4.0*vt(i,j))/(h*h); % vorticity
22 end; end; % equation
23 vt(2:nx-1,2:ny-1)=vt(2:nx-1,2:ny-1)+dt*w(2:nx-1,2:ny-1); % update the vorticity
24 t=t+dt % print outt
25 subplot(121), contour(rot90(fliplr(vt))), axis('square'); % plot vorticity
26 subplot(122), contour(rot90(fliplr(sf))),axis('square');pause(0.01) % streamfunction
27 end;
WPI Computational Fluid Dynamics I
Solution Fields
nx=17, ny=17, dt = 0.005, Re = 10, Uwall = 1.0
WPI Computational Fluid Dynamics I
Solution Fields
nx=17, ny=17, dt = 0.005, Re = 10, Uwall = 1.0
WPI Computational Fluid Dynamics I
Accuracy by Resolution
Stream Function at t = 1.2

9 × 9 Grid 17 × 17 Grid
1 1

0.5 0.5
y

0 0
0 0.25 0.5 0.75 0 0.25 0.5 0.75
x x
WPI Computational Fluid Dynamics I
Accuracy by Resolution
Vorticity at t = 1.2

9 × 9 Grid 17 × 17 Grid
1 1

0.5 0.5
y
y

0 0
0 0.25 0.5 0.75 0 0.25 0.5 0.75
x x
WPI Computational Fluid Dynamics I
Mini-Project

Mini-project:
Add the temperature equation to the vorticity-
streamfunction equation and compute the
increase in heat transfer rate:
WPI Computational Fluid Dynamics I
Convection

∂T ∂T ∂T  ∂ 2 T ∂ 2 T 
+u +v =α 2 +
∂t ∂x ∂y  ∂x ∂y 
2

where
D
α=
ρc p
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

0
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

125
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

250
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

375
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

500
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

750
WPI Computational Fluid Dynamics I
Natural convection

Natural convection in a
closed cavity. The left
vertical wall is heated.

1000
WPI Computational Fluid Dynamics I
Natural convection

Heat Transfer Rate


0.2

0.15

0.1 Natural convection

0.05
Conduction only
0
200 400 600 800 1000

You might also like