<<

WPI Computational Dynamics I A Finite Difference Code for the Navier-Stokes Equations in Vorticity/ Formulation Instructor: Hong G. Im University of Michigan Fall 2001 Computational I WPPII 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 Computational Fluid Dynamics I WPPII 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 WPIPI Computational Fluid Dynamics I WPI The Driven Cavity Problem

Moving wall

Stationary walls Computational Fluid Dynamics I WPPI∂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∂ ∂ ~ 2 u~ = u /U∂, ~x = x / L, t = tU∂/ L, ~p = p / ρU ∂ ν ∂ 2 ~ ~ ~ 2 ~ 2 ~ ∂ 2 ~ U ∂ u ~ u ~ u  U ∂p U  u u   + 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 ∂ ∂u ∂u ∂u ∂p 1  ∂ 2u ∂ 2u − + u + v =− + + ∂y ∂t ∂x ∂y ∂x Re  ∂x2 ∂y2 

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

∂ω ∂ω ∂ω 1  ∂ 2ω ∂ 2ω  + u + v = + ∂t ∂x ∂y Re  ∂x2 ∂y2  ∂ ∂ ω = 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ψ + =−ω ∂x2 ∂y2 WPI Computational Fluid Dynamics I The vorticity/stream function equations

The Navier-Stokes equations in vorticity-stream function form are: / equation ∂ω ∂ψ ∂ω ∂ψ ∂ω  ∂ 2ω ∂ 2ω  =− + + 1  + ∂t ∂y ∂x ∂x ∂y Re  ∂x2 ∂y2 

Elliptic equation ∂ 2ψ ∂ 2ψ + =−ω ∂x2 ∂y2 WPI Computational Fluid Dynamics I Boundary Conditions for the Stream Function

At the right and the left boundary: ∂ψ u = 0 ⇒ = 0 ∂y ⇒ ψ = Constant WPI Computational Fluid Dynamics I Boundary Conditions for the Stream Function

At the top and the bottom boundary: ∂ψ v = 0 ⇒ = 0 ∂x ⇒ ψ = Constant WPI Computational Fluid Dynamics I Boundary 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 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 ⇒ = U wall ∂y wall 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ψ + =−ω ⇒ ω =− ∂x2 ∂y2 wall ∂x 2 Similarly, at the top and the bottom boundary: ∂ 2ψ ∂ 2ψ ∂ 2ψ ⇒ ω =− + =−ω wall 2 ∂x2 ∂y2 ∂y Computational Fluid Dynamics I WPI Summary of Boundary Conditions ∂ψ ∂ 2ψ = U ; ω =− ∂y wall wall ∂y2

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

∂ψ ∂ 2ψ = 0; ω =− ∂y wall ∂y2 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. Computational Fluid Dynamics I WPI 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)

hh WPI Computational Fluid Dynamics I Finite Difference Approximations ∂ Finite difference approximations ∂ ∂ + − − ∂ 3 2 f (x) = f (x h) f (x h) + f (x) h + ∂x 2h ∂x3 6 ! 2 + − + − ∂ 4 2 f (x) = f (x h) 2 f (h) f (x h) + f (x) h + x2 h2 ∂x4 12 ! ∂ +∆ − ∂ 2 ∆ f(t) = f(t t) f(t) + f(t) t + ∂t ∆t ∂t 2 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 (x, y) j+1 = 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 ∂ 2 f + f = ∂x2 ∂y2 f n − 2 f n + f n f n − 2 f n + f n i +1, j i, j i−1,j + i, j +1 i, j i , j −1 = h 2 h2 n + n + n + n − n fi +1, j fi −1,j fi, j +1 fi , j −1 4 fi , j h2 W∂ωPI 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: ψn+1 − n i, j ψi, j = ω ∆t ω  n n  n n  ψ n n  n n  + − − + − − + ψ− − + − − −  i, jω1 i, j 1  i 1, j i 1, j  +  i 1, j i 1, j ω i, j 1 i, j 1   ω    ω   2h  ω2h   2h  2h  ω  n + n + n + n − 4ω n  + 1  i+1, j i−1, j i, j+1 i, j−1 i, j   2  Re  h  WPI Computational Fluid Dynamics I Finite Difference Approximations ω The vorticityω at theψ new time is given by: ψ ω ω  n − n  n − n  n+1 = ψn − ∆  i, j+1 i, j−1  i+1, j i−1, j  i, j i, j t ψ    2ωh 2h  ω   n n  n n  + − − + − − − i 1,ωj i 1, j  i, j 1 i, j 1   ω   2h  ω2h  ω  n + n + n + n − 4ω n  + 1  i+1, j i−1, j i, j+1 i, j−1 i, j   2  Re  h  WPI Computational Fluid Dynamics I Finite Difference Approximations

The stream function equation is:

∂ 2ψ ∂ 2ψ + =−ω ∂x2 ∂y2

n n n n n ψ + +ψ − +ψ + +ψ − − 4ψ i 1, j i 1, j i, j 1 i, j 1 i, j =−ω n h 2 i, j 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 2 ∂ψ = ∂ ψ = h ψ =ψ + i, j 1 h + i , j 1 + O(h3 ) i, j = 2 i, j =1 ∂y ∂y2 2 WPI Computational Fluid Dynamics I Discrete Boundary Condition

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

At the bottom wall (j=1): 2 2 ω = ()ψ = −ψ = +U +O(h) wall i , j 1 i , j 2 h 2 wall h Similarly,ω at the top wall (j=ny): ψ ()2 2 = = −ψ = − −U + O(h) wall i, j ny i, j ny 1 h2 wall 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 − 4ψ n i +1, j i −1, j i, j +1 i, j −1 i, j =−ω n h 2 i, j 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 ii+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 ii+1

Successive Over Relaxation (SOR) ψ n+1 = β ψ n +ψ n +1 +ψ n +ψ n+1 + 2ω n i, j 0.25 ( i+1,j i−1,j i, j +1 i , j −1 h i , j ) + − β ψ n (1 ) i, j WPI Computational Fluid Dynamics I Time Step Control

Limitations on the time step

ν∆t 1 (|u | + | v |)∆t ≤ ≤ 2 h2 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 Update vorticity in interior v(i,j)=v(i,j)+dt*rhs(i,j)

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 para meters 2 MaxIt=100; Beta=1.5; MaxErr=0.001; % para meters for SOR iteration 3 sf=zeros(nx,ny); vt=zeros(nx,ny); w=zeros(nx,ny); h=1.0/(nx-1); t=0.0; 4for istep=1:MaxStep, % start the time integration 5 for iter=1: M a xIt, % 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 ifErr <= MaxErr, break, end % stop if iteration has converged 13 end; 14 vt(2:nx-1,1)=-2.0*sf(2:nx-1,2)/(h*h); % vorticity on botto m w all 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 for i=2:nx-1; for j=2:n y-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 out t 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 × 9Grid 17 × 17 Grid 1 1

y 0.5 y 0.5

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 × 9Grid 17 × 17 Grid 1 1 y y 0.5 0.5

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 equation to the vorticity- streamfunction equation and compute the increase in heat transfer rate: WPI Computational Fluid Dynamics I

∂T ∂T ∂T ∂ 2T ∂ 2T + u + v = α + ∂t ∂x ∂y  ∂x2 ∂y2  where α = D ρ cp 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