9
Computational Fluid Dynamics A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction Form Grétar Tryggvason Spring 2011 http://www.nd.edu/~gtryggva/CFD-Course/ Computational Fluid Dynamics 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 cavityproblem using the Navier-Stokes equations in vorticity form Short discussion about why looking at the vorticity is sometimes helpful Objectives Computational Fluid Dynamics The Driven Cavity Problem The Navier-Stokes Equations in Vorticity/ Streamfunction form Boundary Conditions 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 Outline Computational Fluid Dynamics Moving wall

Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Embed Size (px)

Citation preview

Page 1: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

A Finite Difference Code for the Navier-Stokes Equations in Vorticity/

Streamfunction!Form!

Grétar Tryggvason !Spring 2011!

http://www.nd.edu/~gtryggva/CFD-Course/!Computational Fluid Dynamics

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!

Short discussion about why looking at the vorticity is sometimes helpful!

Objectives!

Computational Fluid Dynamics

•  The Driven Cavity Problem!•  The Navier-Stokes Equations in Vorticity/

Streamfunction form!•  Boundary Conditions!•  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!

Outline!Computational Fluid Dynamics

Moving wall!

Stationary walls!

The Driven Cavity Problem!

Computational Fluid Dynamics

!u!t

+ u!u!x

+ v!u!y

= "!p!x

+1Re

! 2u!x2

+! 2u!y2

# $ % &

'

!v!t

+ u!v!x

+ v!v!y

= "!p!y

+1Re

! 2v!x2

+! 2v!y2

# $ % &

'

!""y

!!x

!"!t

+ u!"!x

+ v!"!y

=1Re

! 2"!x2

+! 2"!y2

# $ % &

'

! ="v"x

#"u"y

The vorticity/streamfunction equations:!Computational Fluid Dynamics

!u!x

+!v!y

= 0

u =!"!y; v = #

!"!x

Solve the incompressibility conditions!

by introducing the stream function!

!!x

!"!y

#!!y

!"!x

= 0

Substituting:!

The vorticity/streamfunction equations:!

Page 2: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

! 2"!x2

+! 2"!y2

= #$

! ="v"x

#"u"y

u =!"!y; v = #

!"!x

Substituting!

into the definition of the vorticity!

yields!

The vorticity/streamfunction equations:!Computational Fluid Dynamics

!"!t

= #!$!y

!"!x

+!$!x

!"!y

+1Re

! 2"!x2

+! 2"!y2

% & ' (

)

! 2"!x2

+! 2"!y2

= #$

The Navier-Stokes equations in vorticity-stream function form are:!

Elliptic equation!

Advection/diffusion equation!

The vorticity/streamfunction equations:!

∂f∂t

+ u ∂f∂x

+ v ∂f∂y

= D ∂2 f∂x2 +

∂2 f∂y2

⎝⎜⎞

⎠⎟Recall the advection-diffusion equation!

Computational Fluid Dynamics Boundary Conditions for the Streamfunction!

u = 0! "#"y

= 0

!# = Constant

At the right and !the left boundary:!

Computational Fluid Dynamics

v = 0!"#$#x

= 0

!$ = Constant

At the top and the!bottom boundary:!

Boundary Conditions for the Streamfunction!

Computational Fluid Dynamics

Since the boundaries meet, the constant must be the same on all boundaries!

! = Constant

Boundary Conditions for the Streamfunction!Computational Fluid Dynamics Boundary Conditions for the Vorticity!

The normal velocity is zero since the streamfunction is a constant on the wall, but the zero tangential velocity must be enforced:!

At the right and left boundary:!

v = 0!"#$#x

= 0

u = 0! "#"y

= 0

At the bottom boundary:!

At the top boundary:!

u =Uwall !"#"y

=Uwall

Page 3: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

At the right and the left boundary:!

Similarly, at the top and the bottom boundary:!

! " wall = #$ 2%$x 2

! " wall = #$ 2%$y2

The wall vorticity must be found from the streamfunction. The stream function is constant on the walls. !

! 2"!x2

+! 2"!y2

= #$

! 2"!x2

+! 2"!y2

= #$

Boundary Conditions for the Vorticity!Computational Fluid Dynamics

!"!y

=Uwall; #wall = $! 2"!y2

!"!y

= 0; #wall = $! 2"!y2

!"!x

= 0

#wall = $ !2"!x 2

Summary of Boundary Conditions!

! = Constant

!"!x

= 0

#wall = $ !2"!x 2

Computational Fluid Dynamics

i=1! i=2! i=NX!j=1!j=2!

j=NY!

! i, j and " i , j Grid boundaries coincide with domain boundaries!

Discretizing the Domain!

To compute an approximate solution numerically, we start by laying down a discrete grid:!

stored at each grid point!

Computational Fluid Dynamics Finite Difference Approximations!

Then we replace the equations at each grid point by a finite difference approximation!

!"!t

# $ i, j

n

= %!&!y

!"!x

# $ i, j

n

+!&!x

!"!y

# $ i, j

n

+1Re

! 2"!x 2

+! 2"!y2

' ( ) #

$ * i, j

n

! 2"!x 2

# $ % i, j

n

+! 2"!y 2

# $ % i, j

n

= &' i, jn

Computational Fluid Dynamics

!f (x)!x

= f (x + h) " f (x " h)2h

" !3 f (x)!x 3

h2

12+!

Finite difference approximations!

! 2 f (x)!x 2

= f (x + h) " 2 f (h) + f (x " h)h2

" ! 4 f (x)!x 4

h2

xx+!

!f (t)!t

= f (t + "t) # f (t)"t

# !2 f (t)!t 2

"t2

+!

Finite Difference Approximations!Computational Fluid Dynamics

Use the notation developed earlier:!

fi , j = f (x,y)

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

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

i -1 i i+1

j+1

j

j-1

Finite Difference Approximations!

Page 4: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

Laplacian!

! 2 f!x2

+! 2 f!y2

=

fi+1, jn ! 2 fi, j

n + fi!1, jn

h2+fi, j+1n ! 2 fi, j

n + fi , j!1n

h2=

fi+1, jn + fi!1, j

n + fi, j+1n + fi , j!1

n ! 4 fi , jn

h2

Finite Difference Approximations!Computational Fluid Dynamics

The advection equation is:!

!"!t

= #!$!y

!"!x

+!$!x

!"!y

+1Re

! 2"!x2

+! 2"!y2

% & ' (

)

! i, jn+1 "! i, j

n

#t=

"$i, j+1

n "$ i, j"1n

2h

%

& '

(

) * ! i+1, j

n "! i"1, jn

2h

%

& '

(

) * +

$i+1, jn "$ i"1, j

n

2h

%

& '

(

) * ! i, j+1

n "! i, j"1n

2h

%

& '

(

) *

+ 1Re

! i+1, jn + ! i"1, j

n + ! i, j+1n + ! i, j"1

n " 4! i, jn

h2%

& '

(

) *

Finite Difference Approximations!

Computational Fluid Dynamics

ω i, jn+1 =ω i, j

n + Δt −ψ i, j+1

n −ψ i, j−1n

2h

⎝⎜

⎠⎟

ω i+1, jn −ω i−1, j

n

2h

⎝⎜

⎠⎟

⎣⎢⎢

+ψ i+1, j

n −ψ i−1, jn

2h

⎝⎜

⎠⎟

ω i, j+1n −ω i, j−1

n

2h

⎝⎜

⎠⎟

+1

Reω i+1, j

n +ω i−1, jn +ω i, j+1

n +ω i, j−1n − 4ω i, j

n

h2

⎝⎜

⎠⎟⎤

⎦⎥⎥

The vorticity at the new time is given by:!

Finite Difference Approximations!Computational Fluid Dynamics

ψ i+1, jn +ψ i−1, j

n +ψ i, j+1n +ψ i, j−1

n − 4ψ i, jn

h2 = −ω i, jn

The elliptic equation is:!

∂ 2ψ∂x2 +

∂ 2ψ∂ y2 = −ω

Finite Difference Approximations!

Computational Fluid Dynamics

These equations allow us to obtain the solution at interior points!

! i, j = 0

i=1! i=2! i=nx!j=1!j=2!

j=ny!

on the boundary!

Need vorticity on the

boundary!!

Finite Difference Approximations!Computational Fluid Dynamics

i -1 i i+1

Discrete Boundary Condition!

j=3

j=2

j=1

!wall =! i, j=1

Uwall

Consider the bottom wall (j=1):!

Need to find!

given:!

!"!y

=Uwall; #wall = $!2"

!y 2

! = Constant

Page 5: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

i -1 i i+1

j=3

j=2

j=1 Uwall

given:!

!"!y

=Uwall; #wall = $!2"

!y 2

! = Constant

! i, j= 2 =! i, j=1 +"! i, j=1

"yh +

" 2! i , j=1

"y2h2

2+O(h3 )

Expand the streamfunction!

Discrete Boundary Condition!Computational Fluid Dynamics

! i, j= 2 =! i, j=1 +Uwallh "#wallh2

2+O(h3)

!wall = " i , j=1 #" i , j=2( ) 2h2 +Uwall2h+O(h)

!wall = "# 2$ i, j=1

#y2; Uwall =

#$ i, j=1

#y

Solving for the wall vorticity:!

Using:!

this becomes:!

! i, j= 2 =! i, j=1 +"! i, j=1

"yh +

" 2! i , j=1

"y2h2

2+O(h3 )

Discrete Boundary Condition!

Computational Fluid Dynamics

! i, jn+1 = 0.25 (! i+1, j

n +! i"1, jn +! i, j+1

n +! i, j"1n + h2# i, j

n )

! i+1, jn +! i"1, j

n +! i, j+1n +! i, j"1

n " 4! i, jn

h2= "# i, j

n

The elliptic equation:!

Rewrite as!

Solving the elliptic equation!

Solve by SOR!

! i, j" +1 = #0.25 (! i+1, j

" +! i$1, j" +1 +!i, j+1

" +! i, j$1" +1 + h2% i, j

n )

+ (1$# )! i, j"

Computational Fluid Dynamics

Limitations on the time step!

!"th2

#14

(|u | + | v |)!t"

# 2

Time Step!

Computational Fluid Dynamics

Solve for the stream function!

Find vorticity on boundary!

Find RHS of vorticity equation!

Initial vorticity given!

t=t+t!

Update vorticity in interior!

Solution Strategy!Computational Fluid Dynamics

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!

for i=2:nx-1; for j=2:ny-1! rhs(i,j)=Advection+diffusion!end; end!

Solve for the stream function!

Find vorticity on boundary!

Find RHS of vorticity equation!

Initial vorticity given!

t=t+t!

Update vorticity in interior!

v(i,j)=…!

v(i,j)=v(i,j)+dt*rhs(i,j)!

Solution Strategy!

Page 6: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics The 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; 4 for istep=1:MaxStep, % start the time integration 5 for iter=1:MaxIt, % solve for the streamfunction 6 w=sf; % by SOR iteration 7 for i=2:nx-1; for j=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; for i=1:nx; for j=1:ny, Err=Err+abs(w(i,j)-sf(i,j)); end; end;12 if Err <= MaxErr, break, end % stop if iteration has converged13 end;14 vt(2:nx-1,1)=-2.0*sf(2:nx-1,2)/(h*h); % vorticity on bottom wall15 vt(2:nx-1,ny)=-2.0*sf(2:nx-1,ny-1)/(h*h)-2.0/h; % vorticity on top wall16 vt(1,2:ny-1)=-2.0*sf(2,2:ny-1)/(h*h); % vorticity on right wall17 vt(nx,2:ny-1)=-2.0*sf(nx-1,2:ny-1)/(h*h); % vorticity on left wall18 for i=2:nx-1; for j=2:ny-1 % compute19 w(i,j)=-0.25*((sf(i,j+1)-sf(i,j-1))*(vt(i+1,j)-vt(i-1,j))... % the RHS20 -(sf(i+1,j)-sf(i-1,j))*(vt(i,j+1)-vt(i,j-1)))/(h*h)... % of the21 +Visc*(vt(i+1,j)+vt(i-1,j)+vt(i,j+1)+vt(i,j-1)-4.0*vt(i,j))/(h*h); % vorticity22 end; end; % equation23 vt(2:nx-1,2:ny-1)=vt(2:nx-1,2:ny-1)+dt*w(2:nx-1,2:ny-1); % update the vorticity24 t=t+dt % print out t25 subplot(121), contour(rot90(fliplr(vt))), axis('square'); % plot vorticity26 subplot(122), contour(rot90(fliplr(sf))), axis('square');pause(0.01) % streamfunction27 end;

Computational Fluid Dynamics

17 by 17!Dt=0.01!D=0.1!

Results:!

Computational Fluid Dynamics

05

1015

20

0

5

10

15

20-10

-5

0

5

10

15

20

25

Vorticity!

05

1015

20

0

5

10

15

200

0.02

0.04

0.06

0.08

0.1

Streamfunction!

Results:!Computational Fluid Dynamics

17 by 17!Dt=0.01!D=0.1!

ui, j = !"!y

#" i, j+1 $"i, j$1

2h

vi, j = $!"!x

# $" i+1, j $" i$1, j

2h

Results:!

Computational Fluid Dynamics

9 by 9 grid! 17 by 17 grid!

Results:!

Vorticity at t = 1.2!

Streamfunction at t = 1.2!

Computational Fluid Dynamics

Why is vorticity !important?!

Page 7: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

u =∇φ + ∇ ×Ψ

Helmholtz decomposition:!Any vector field can be written as a sum of !

Take the curl!

∇ ⋅u = ∇⋅∇φ = ∇ 2φ = 0

∇ × u = ∇ × ∇ ×Ψ( ) = ω

Take divergence!

By a Gauge transform this can be written as!

∇ 2Ψ = −ω

Vorticity!Computational Fluid Dynamics

∂ω∂t

+ u∇ ⋅ω = ω ⋅∇( )u + ν∇ 2ω

For incompressible flow with constant density and viscosity, taking the curl of the momentum equation yields:!

or:!

DωDt

= ω ⋅∇( )u + ν∇ 2ω

Helmholtzʼs theorem:!Inviscid Irrotational flow remains irrotational!

Vorticity!

Computational Fluid Dynamics

Ψ = (0,0,ψ)In two-dimensions:!

or:!

ω =∂v∂x

−∂u∂y

ω = (0,0,ω)

DωDt

=ν∇ 2ω

∇ 2ψ = −ω

DωDt

= 0Zero viscosity: ! The vorticity of a fluid particle does not change!!

Vorticity!Computational Fluid Dynamics Flow over a body!

Irrotational outer flow!

Boundary layer: the flow is viscous and rotational!

Rotational wake!

Computational Fluid Dynamics

Advection and diffusion—!

Boundary layers!

Computational Fluid Dynamics

∂u∂t

+ u ∂u∂x

+ v∂u∂y

= −1ρ∂P∂x

+µρ

∂ 2u∂x2

+∂ 2u∂y2

⎛ ⎝ ⎜ ⎞

∂f∂t

+ U∂f∂x

= D∂ 2 f∂x 2

Boundary Layers!

Page 8: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

Consider the steady state balance of advection and diffusion!

U ∂f∂x

= D∂ 2 f∂x2

Solve this equation analytically!dfdx

=DUd2 fdx2

ddx

f − DUdfdx

⎛ ⎝

⎞ ⎠ = 0

f − DUdfdx

= C1Integrate:!

f = 0x = 0

f =1x = L

U

Governed by:!

Boundary Layers!Computational Fluid Dynamics

or!

f − C1 =DUdfdx

Rearrange!1

f − C1( )dfdx

=UD

dff − C1( ) =

UDdx

Integrate!df

f − C1( )∫ =UDdx∫ ln f − C1( ) = U

Dx + C2

f = exp Ux / D( ) × exp C2( ) + C1

Boundary Layers!

Computational Fluid Dynamics

Boundary conditions!

f = exp Ux / D( ) × exp C2( ) + C1

x = 0 : f = 0 0 = exp C2( ) + C1x = L : f = 1 1 = exp UL / D( ) × exp C2( ) + C1

At!

At!

⇒ C1 = −exp C2( )

⇒ 1 = exp UL / D( ) × exp C2( ) − exp C2( )⇒ 1 = exp C2( ) exp U / D( ) −1[ ]⇒ exp C2( ) = 1

exp UL / D( ) −1

Boundary Layers!Computational Fluid Dynamics

f =exp Ux /D( ) −1exp UL /D( ) −1

f = exp Ux / D( ) × exp C2( ) + C1

f = exp Rx / L( ) − 1exp R( ) −1 R =

ULD

C1 = −exp C2( )

exp C2( ) =1

exp UL /D( ) −1

Boundary Layers!

Computational Fluid Dynamics

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

R=1!R=5!R=10!R=20!

r=20;for i=1:100,x(i)=(i-1)/99;end;!for i=1:100,f(i)=(exp(r*x(i))-1)/(exp(r)-1);end;!plot(x,f)!

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Boundary Layers!Computational Fluid Dynamics

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

R=1 !L/R=1!R=5 !L/R=0.2!R=10 !L/R=0.1!

R=20 !L/R=0.05!

dfdx

=DUd2 fdx2

=DU1δ 2

⇒δ ≈DLUL

=LR

Scaling:!

Estimate the thickness of the “boundary Layer”!

Boundary Layers!

Page 9: Objectives A Finite Difference Code for the Navier …gtryggva/CFD-Course/2011-Lecture-5.pdf · A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Streamfunction!

Computational Fluid Dynamics

Solution of:!

f=1

f=0

U=1!D=0.025!

δ ≈ DU

∂f∂x

= 0

U ∂ f

∂x= D ∂ 2 f

∂x2 +∂ 2 f∂ y2

⎝⎜⎞

⎠⎟2D!Boundary Layers!

Computational Fluid Dynamics

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!

Short discussion about why looking at the vorticity is sometimes helpful!

Objectives!