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

2u 2u
1 p
u
u
u
+u +v
=
+ 2 + 2
t
x
y
x
y
x
Nondimensionalization

~
2
~
~
~
/
=

p
p
U
/
,
=
t
tU
L
/
,
=
x
x
L
u = u /U ,
2
2
2~
2~
~
~
~
~

U p U u u
U u ~ u ~ u
~ + u ~ + v ~ =
+ 2 ~ 2 + ~ 2
~
x
y
y
L x L x
L t
u~ ~ u~ ~ u~
~
p 1 2u~ 2u~
~ 2 + ~ 2
~ +u ~ +v ~ = ~ +
t
x
y
x UL
y
Re x

WPI

Computational Fluid Dynamics I


The vorticity/stream function equations

The Vorticity Equation

u
u
u
p 1 2 u 2u
+u
+v = +

2 +
2

x
y
x Re x
y
y t
v
v
p 1 2 v 2 v
v
+u +v = +
2 +
2

Re
t
x
y
y
x
y

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=
y

v=
x

which automatically satisfies the incompressibility conditions

by substituting

u v
+
=0
x y

=0
x y y x

WPI

Computational Fluid Dynamics I


The vorticity/stream function equations

Substituting

; v=
u=
y
x
into the definition of the vorticity

v u
=

x y

yields


2 +
2 =
x
y
2

WPI

Computational Fluid Dynamics I


The vorticity/stream function equations

The Navier-Stokes equations in vorticity-stream


function form are:
Advection/diffusion equation
2
2

1
=
+
+
2 +
2

t
y x x y Re x
y

Elliptic equation


2 +
2 =
x
y
2

Computational Fluid Dynamics I


WPIBoundary
Conditions for the Stream Function

At the right and


the left boundary:

u = 0
=0
y
= Constant

Computational Fluid Dynamics I


WPIBoundary
Conditions for the Stream Function

At the top and the


bottom boundary:

v =0
=0
x
= Constant

Computational Fluid Dynamics I


WPIBoundary
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:

=0
v =0
x
At the top boundary:

u = U wall

u = 0
=0
y

= 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 =
x
y
2

wall

= 2
x
2

Similarly, at the top and the bottom boundary:


2 +
2 =
x
y
2

wall

= 2
y

WPI

Computational Fluid Dynamics I


Summary of Boundary Conditions

2
= Uwall ; wall = 2
y
y

=0
x
2

wall

= 2
x

= Constant

2
= 0; wall = 2
y
y

=0
x
wall

= 2
x

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

WPI

Computational Fluid Dynamics I


Finite Difference Approximations

Finite difference approximations

f ( x ) f ( x + h ) f ( x h ) f ( x ) h
=
+
+!
3
2h
6
x
x
3

2 f ( x ) f ( x + h) 2 f ( h) + f ( x h) 4 f ( x ) h 2
=
+
+!
2
2
4
h
x
x 12
f (t) f (t + t) f (t) 2 f (t) t
=
+
+!
2
2
t
t
"" t
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

(x, y)

j+1
j
j-1
i -1

i+1

fi , j = f (x,y)
fi +1, j = f (x + h, y)
fi , j +1 = f (x, y + h)

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 + fi1, j fi, j +1 2 f i, j + fi , j 1
+
=
2
2
h
h
n
i +1, j

n
i 1, j

+ f

n
i, j +1
2

+f
h

n
i , j 1

+f

n
i,j

4f

WPI

Computational Fluid Dynamics I


Finite Difference Approximations

2
2

1
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 in1, j

2
2h
h

in+1, j in1, j
+

2h

n
n
n
n
n

4
+
+
+

1 i +1, j
i 1, j
i , j +1
i , j 1
i, j
+
Re
h2

in, j +1 in, j 1

2
h

WPI

Computational Fluid Dynamics I


Finite Difference Approximations

The vorticity at the new time is given by:


n
n
n
n

i , j 1
i 1, j
i +1, j
in, +j 1 = in, j t i , j +1

2h
2h

in+1, j in1, j in, j +1 in, j 1

2
2
h
h

n
n
n
n
n

4
+
+
+

1 i +1, j
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 =
x
y
2

in+1, j + in1, j + ni, j +1 + ni, j 1 4 i,n j


h

n
i, j

WPI

Computational Fluid Dynamics I


Discretized Domain

Discretize the domain

i, j = 0

j=ny

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

Uwall
i -1

wall = i, j =1

i+1
2

i, j = 2

i, j =1
i , j =1 h 2
3
h+
= i, j =1 +
+ O(h )
2
2
y
y

WPI

Computational Fluid Dynamics I


Discrete Boundary Condition
2

i, j = 2
Using:

i, j =1
i , j =1 h 2
3
h+
= i, j =1 +
+ O(h )
2
2
y
y
2

wall

i, j =1
i, j =1
; Uwall =
=
2
y
y

This becomes:

i, j = 2 = i, j =1 + Uwall h wall

h2
3
+ O(h )
2

Solving for the wall vorticity:

wall

2
2
= ( 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):

wall

2
2
= ( i , j =1 i , j =2 ) 2 + Uwall + O(h)
h
h

Similarly, at the top wall (j=ny):

wall

2
2
= ( 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
i +1, j

n
i 1, j

Rewrite as

n +1
i, j

= 0.25 (

n
i, j +1
2

+
h

n
i +1, j

n
i1, j

i-1

n
i, j 1

j+1
j
j-1
i+1

n
i, j +1

n
i, j

n
i, j 1

n
i, j

n
i, j

+h )

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+1

Successive Over Relaxation (SOR)

i,n +j 1 = 0.25 ( ni+1, j + ni+1,1 j + ni, j +1 + in,+j 11 + h2 in, j )


+ (1 ) i,n j

WPI

Computational Fluid Dynamics I


Time Step Control

Limitations on the time step

t 1
2
4
h

(| u | + | v |) t
2

WPI

Computational Fluid Dynamics I


Solution Algorithm

Initial vorticity given


Solve for the stream function
Find vorticity on boundary
Find RHS of vorticity equation
Update vorticity in interior
t=t+t

for i=1:MaxIterations
for i=2:nx-1; for j=2:ny-1
s(i,j)=SOR for the stream function
end; end
end
v(i,j)=
for i=2:nx-1; for j=2:ny-1
rhs(i,j)=Advection+diffusion
end; end
v(i,j)=v(i,j)+dt*rhs(i,j)

WPI

Computational Fluid Dynamics I


MATLAB Code

1 clf;nx=9; ny=9; MaxStep=60; Visc=0.1; dt=0.02;


2 MaxIt=100; Beta=1.5;MaxErr=0.001;
3 sf=zeros(nx,ny); vt=zeros(nx,ny); w=zeros(nx,ny); h=1.0/(nx-1);t=0.0;
4foristep=1:MaxStep,
5 foriter=1:MaxIt,
6 w=sf;
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
13 end;
14 vt(2:nx-1,1)=-2.0*sf(2:nx-1,2)/(h*h);
15 vt(2:nx-1,ny)=-2.0*sf(2:nx-1,ny-1)/(h*h)-2.0/h;
16 vt(1,2:ny-1)=-2.0*sf(2,2:ny-1)/(h*h);
17 vt(nx,2:ny-1)=-2.0*sf(nx-1,2:ny-1)/(h*h);
18 fori=2:nx-1; forj=2:ny-1
19
w(i,j)=-0.25*((sf(i,j+1)-sf(i,j-1))*(vt(i+1,j)-vt(i-1,j))...
20
-(sf(i+1,j)-sf(i-1,j))*(vt(i,j+1)-vt(i,j-1)))/(h*h)...
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);
22 end; end;
23 vt(2:nx-1,2:ny-1)=vt(2:nx-1,2:ny-1)+dt*w(2:nx-1,2:ny-1);
24 t=t+dt
25 subplot(121), contour(rot90(fliplr(vt))), axis('square');
26 subplot(122), contour(rot90(fliplr(sf))),axis('square');pause(0.01)
27 end;

% resolution & governing parameters


% parameters for SOR iteration
% startthe time integration
% solve forthe streamfunction
% by SOR iteration

% stop ifiteration has converged


%
%
%
%
%
%
%
%
%
%
%
%
%

vorticity on bottom wall


vorticity on top wall
vorticity on right wall
vorticity on left wall
compute
the RHS
of the
vorticity
equation
update the vorticity
print outt
plot vorticity
streamfunction

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

0.5

0.25

0.5

17 17 Grid

0.75

0.5

0.25

0.5

0.75

WPI

Computational Fluid Dynamics I


Accuracy by Resolution
Vorticity at t = 1.2
9 9 Grid

0.5

0.25

0.5

17 17 Grid

0.75

0.5

0.25

0.5

0.75

WPI

Computational Fluid Dynamics I


Mini-Project

Mini-project:
Add the temperature equation to the vorticitystreamfunction equation and compute the
increase in heat transfer rate:

WPI

Computational Fluid Dynamics I


Convection

2 T 2 T
T
T
T
+u
+v
=
2 +
2

t
x
y
x
y
where

D
=
c p

WPI

Computational Fluid Dynamics I


Natural convection

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

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

0.2

Heat Transfer Rate

0.15
0.1

Natural convection

0.05
Conduction only

200

400

600

800

1000

You might also like