Vorticity 2d - Cfd - Matlab

download Vorticity 2d - Cfd - Matlab

of 47

Transcript of Vorticity 2d - Cfd - Matlab

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    1/47

    Computational Fluid Dynamics IPPPPIIIIWWWW

    A Finite Difference Code

    for the Navier-StokesEquations in

    Vorticity/Stream Function

    FormulationInstructor: Hong G. Im

    University of Michigan

    Fall 2001

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    2/47

    Computational Fluid Dynamics IPPPPIIIIWWWW

    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

    Objectives:

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    3/47

    Computational Fluid Dynamics IPPPPIIIIWWWW

    • 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

    Outline

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    4/47

    Computational Fluid Dynamics IPPPPIIIIWWWW

    Moving wall

    Stationarywalls

    The Driven Cavity Problem

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    5/47

    Computational Fluid Dynamics IPPPPIIIIWWWW

       

      

     ++−=++ 2

    2

    2

    21

     y

    u

     x

    u

     x

     p

     y

    u

    v x

    u

    ut 

    u

    ∂ 

    ∂ 

    ∂ 

    ∂ ν ∂ 

    ∂ 

    ρ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    Nondimensional N-S Equation

    Incompressible N-S Equation in 2-D

    Nondimensionalization

    ,/~ U uu =   ,/~  L x x =   2/~ U  p p   ρ=,/~  LtU t  =

       

     

     

     ++−=

       

     

     

     ++

    2

    2

    2

    2

    2

    22

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~~

    ~

    ~~

    ~

    ~

     y

    u

     x

    u

     L

     x

     p

     L

     y

    uv

     x

    uu

    u

     L

    ∂ 

    ∂ 

    ∂ 

    ∂ ν 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

       

     

     

     

    ++−=++ 2

    2

    2

    2

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~~

    ~

    ~~

    ~

    ~

     y

    u

     x

    u

    UL x

     p

     y

    uv

     x

    uu

    u

    ∂ 

    ∂ 

    ∂ 

    ∂ ν 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    Re

    1

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    6/47

    Computational Fluid Dynamics IPIW

    ∂ u

    ∂ t  + u∂ u

    ∂  x + v∂ u

    ∂  y = −∂  p

    ∂  x +  1

    Re

    ∂ 2u

    ∂  x2 + ∂ 2u

    ∂  y2 

    ∂ v

    ∂ t  + u∂ v

    ∂  x + v∂ v

    ∂  y = −∂  p

    ∂  y +  1

    Re

    ∂ 2v

    ∂  x2  +∂ 2v

    ∂  y2 

    − ∂ 

    ∂  y

    ∂ ∂  x

    ∂ω ∂ t 

    + u ∂ω ∂  x

    + v ∂ω ∂  y

    =   1Re

    ∂ 2

    ω ∂  x

    2   + ∂ 2

    ω ∂  y

    ω  = ∂ v∂  x − ∂ u∂  y

    The vorticity/stream function equations

    The Vorticity Equation

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    7/47

    Computational Fluid Dynamics IPIW

    0=+

     y

    v

     x

    u

    ∂ 

    ∂ 

    ∂ 

    ∂ 

     xv

     yu

    ∂ 

    ∂ψ 

    ∂ 

    ∂ψ  −==   ,

    which automatically satisfies the incompressibility conditions

    Define the stream function

    ∂ 

    ∂  x

    ∂ψ 

    ∂  y −

      ∂ 

    ∂  y

    ∂ψ 

    ∂  x = 0

    by substituting

    The vorticity/stream function equations

    The Stream Function Equation

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    8/47

    Computational Fluid Dynamics IPIW

    ∂ 2ψ 

    ∂  x2   +

     ∂ 2ψ 

    ∂  y2   = −ω 

    ω  = ∂ v

    ∂  x

    − ∂ u

    ∂  y

    u = ∂ψ ∂  y

    ;   v = − ∂ψ ∂  x

    Substituting

    into the definition of the vorticity

    yields

    The vorticity/stream function equations

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    9/47

    Computational Fluid Dynamics IPIW

    ∂ω ∂ t 

    = − ∂ψ ∂  y

    ∂ω ∂  x

    + ∂ψ ∂  x

    ∂ω ∂  y

    +   1Re

    ∂ 2

    ω ∂  x

    2   + ∂ 2

    ω ∂  y

    ∂ 2ψ 

    ∂  x2   +

     ∂ 2ψ 

    ∂  y2   = −ω 

    The Navier-Stokes equations in vorticity-stream

    function form are:

    Elliptic equation

     Advection/diffusion equation

    The vorticity/stream function equations

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    10/47

    Computational Fluid Dynamics IPIWBoundary Conditions for the Stream Function

    u = 0 ⇒ ∂ψ 

    ∂  y = 0

    ⇒ψ 

     = Constant

     At the right and

    the left boundary:

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    11/47

    Computational Fluid Dynamics IPIW

    v = 0 ⇒ ∂ψ 

    ∂  x = 0

    ⇒ ψ  = Constant

     At the top and the

    bottom boundary:

    Boundary Conditions for the Stream Function

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    12/47

    Computational Fluid Dynamics IPIW

    Since the boundaries

    meet, the constantmust be the same on

    all boundaries

    ψ  = Constant

    Boundary Conditions for the Stream Function

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    13/47

    Computational Fluid Dynamics IPIWBoundary Conditions for the Vorticity

     At the right and left boundary:

     At the top boundary:

    The normal velocity is zero since the stream function

    is a constant on the wall, but the zero tangential

    velocity must be enforced:

    v = 0 ⇒ ∂ψ ∂  x

    = 0   u = 0 ⇒  ∂ψ ∂  y

    = 0

     At the bottom boundary:

    wall wall    U  y

    U u   =∂

    ⇒=  ∂ψ 

    PIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    14/47

    Computational Fluid Dynamics IPIW

     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

    PIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    15/47

    Computational Fluid Dynamics IPIW

    ∂ψ 

    ∂  y= U wall ;   ω wall  = −

    ∂ 2ψ 

    ∂  y2

    ∂ψ ∂  y

    = 0;   ω wall  = − ∂ 2

    ψ ∂  y2

    ∂ψ 

    ∂  x = 0

    ω wall 

     = − ∂ 2ψ 

    ∂  x2

    Summary of Boundary Conditions

    ψ  = Constant

    ∂ψ 

    ∂  x= 0

    ω wall  = −∂ 

    2ψ 

    ∂  x2

    PIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    16/47

    Computational Fluid Dynamics IPIWFinite 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.

    PIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    17/47

    Computational Fluid Dynamics IPIW

    i=1 i=2 i=NX

     j=1 j=2

     j=NY

    ψ i, j and ω 

    i , j

    Stored ateach grid

    point

    Discretizing the Domain

    Uniform mesh (h=constant)

    PIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    18/47

    Computational Fluid Dynamics IPIW

    When using FINITE DIFFERENCE approximations,

    the values of f are stored at discrete points and thederivatives 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)

    Finite Difference Approximations

    h h

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

    PIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    19/47

    Computational Fluid Dynamics IPIW

    !++−−+=6

    )(

    2

    )()()(   2

    3

    3 h

     x

     x f 

    h

    h x f h x f 

     x

     x f 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

    Finite difference approximations

    Finite Difference Approximations

    !++−+−+=12

    )()()(2)()(   2

    4

    4

    22

    2 h

     x

     x f 

    h

    h x f h f h x f 

     x

     x f 

    ∂ 

    ∂ 

    ∂ 

    ∂ 

     

    ∂  f (t )∂ t 

      =   f (t  + ∆t ) −   f (t )∆t 

      +  ∂ 2 f (t )

    ∂ t 2∆t 2

      +!

    2nd order in space, 1st order in time

    CPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    20/47

    Computational Fluid Dynamics IPIWFinite Difference Approximations

    For a two-dimensional flow discretize the variables on

    a two-dimensional grid

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

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

    ( x, y)

    i -1   i i+1

     j+1

     j

     j-1

    C t ti l Fl id D i IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    21/47

    Computational Fluid Dynamics IPIWFinite Difference Approximations

    Laplacian

    ∂ 2 f 

    ∂  x2 + ∂ 

    2 f 

    ∂  y2  =

     f i +1, jn − 2 f i, jn +   f i−1, jn

    h2

      +   f i, j +1n − 2 f i, jn + f i , j −1n

    h2

      =

     f i +1, jn +   f i −1, jn + f i, j +1n + f i , j −1n − 4 f i , jnh2

    C t ti l Fl id D i IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    22/47

    Computational Fluid Dynamics IPIW

        

         −++++

        

         −  

      

         −+  

      

         −  

      

         −−

    =∆−

    −+−+

    −+−+−+−+

    +

    2,1,1,,1,1

    1,1,,1,1,1,11,1,

    ,1

    ,

    4Re1

    2222

    h

    hhhh

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n

     ji

    n ji

    n ji

    ω ω ω ω ω 

    ω ω ψ ψ ω ω ψ ψ 

    ω ω 

    Using these approximations, the vorticity equation becomes:

    Finite Difference Approximations

       

     

     

     

    +++−= 22

    2

    2

    Re

    1

     y x y x x yt    ∂ 

    ω ∂ 

    ∂ 

    ω ∂ 

    ∂ 

    ∂ω 

    ∂ 

    ∂ψ 

    ∂ 

    ∂ω 

    ∂ 

    ∂ψ 

    ∂ 

    ∂ω 

    C t ti l Fl id D i IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    23/47

    Computational Fluid Dynamics IPIW

        

         −++++

       

      

        −   

      

        −−

       

      

        −   

      

        −∆−=

    −+−+

    −+−+

    −+−++

    2

    ,1,1,,1,1

    1,1,,1,1

    ,1,11,1,

    ,

    1

    ,

    4

    Re

    1

    22

    22

    h

    hh

    hht 

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n ji

    n jin

     ji

    n

     ji

    ω ω ω ω ω 

    ω ω ψ ψ 

    ω ω ψ ψ ω ω 

    The vorticity at the new time is given by:

    Finite Difference Approximations

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    24/47

    Computational Fluid Dynamics IPIW

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

    h2

      = −ω i, j

    n

    The stream function equation is:

    ∂ 2ψ 

    ∂  x2  +

     ∂ 2ψ 

    ∂  y2  = −ω 

    Finite Difference Approximations

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    25/47

    Computational Fluid Dynamics IPIW

    Discretize the domain

    ψ i, j  = 0

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

     j=2

     j=ny

    i =1i = nx

     j =1 j = ny

    Discretized Domain

    for 

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    26/47

    Computational Fluid Dynamics IPIW

    i -1   i i+1

    Discrete Boundary Condition

     j=3

     j=2

     j=1

    ψ i, j = 2 = ψ i, j =1 + ∂ψ  i, j =1

    ∂  yh + ∂ ψ i , j =1

    ∂  y2

    h2

    2+ O(h3 )

    ω wall  = ω i, j =1U wall 

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    27/47

    Computational Fluid Dynamics IPIW

    ψ i, j = 2 = ψ i, j =1 + U 

    wall h

     −ω wall h2

    2 + O(h3)

    ω wall  =   ψ i , j =1 − ψ i , j =2( )2

    h2 +U wall 

    2

    h +O(h)

    ω wall  = −

    ∂ ψ i, j =1

    ∂  y2   ;   U wall  =

     ∂ψ i, j =1

    ∂  y

    Solving for the wall vorticity:

    Using:

    This becomes:

    Discrete Boundary Condition

    ψ i, j=2 = ψ i, j

    =1 +

     ∂ψ  i, j =1

    ∂  yh +

     ∂ ψ i , j =1

    ∂  y2

    h2

    2+ O(h3 )

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    28/47

    Computational Fluid Dynamics IPIW

    ω wall  =

      ψ i , j =1 −

    ψ i , j =2( )

    2

    h2 +U wall 2

    h +O(h)

     At the bottom wall ( j=1):

    Discrete Boundary Condition

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

     At the left wall (i=1):

     At the right wall (i=nx):

    Fill the blank

    Fill the blank

    ( )   )(22

    21,,   hOhU h  wall ny jiny jiwall    +−−=   −==   ψ ψ ω 

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    29/47

    Computational Fluid Dynamics IPIW

    ψ 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

    Solving the elliptic equation:

    Rewrite as

     j

     j-1

     j+1

    i i+1i-1

    Solving the Stream Function Equation

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    30/47

    Computational Fluid Dynamics IPIW

    ψ i, jn+1 = β 0.25 (ψ i+1, j

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

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

    n )

    + (1− β )ψ i, jn

    Successive Over Relaxation (SOR)

    If the grid points are done in order, half of the points have

    already been updated:

     j

     j-1

     j+1

    i i+1i-1

    Solving the Stream Function Equation

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    31/47

    Computational Fluid Dynamics IPIW

    Limitations on the time step

    ν ∆t h2   ≤

     1

    4

    (|u | + | v |)∆t ν    ≤ 2

    Time Step Control

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    32/47

    Computational Fluid Dynamics IPIW

    for i=1:MaxIterations

    for i=2:nx-1; for j=2:ny-1s(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

    Solution Algorithm

    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)

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    33/47

    p yPIW

    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, % start the time integration

    5 foriter=1:MaxIt, % solve for the streamfunction6 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 ifErr

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    34/47

    p yPIW

    nx=17, ny=17, dt = 0.005, Re = 10, Uwall = 1.0

    Solution Fields

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    35/47

    p yPIW

    nx=17, ny=17, dt = 0.005, Re = 10, Uwall = 1.0

    Solution Fields

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    36/47

    p yPIW

    Stream Function at t = 1.2

     Accuracy by Resolution

    x

         y

    0 0.25 0.5 0.750

    0.5

    1

    9 × 9 Grid

    x

         y

    0 0.25 0.5 0.750

    0.5

    1

    17 × 17 Grid

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    37/47

    PIW

    Vorticity at t = 1.2

     Accuracy by Resolution

    x

         y

    0 0.25 0.5 0.750

    0.5

    1

    9 × 9 Grid

    x

         y

    0 0.25 0.5 0.750

    0.5

    1

    17 × 17 Grid

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    38/47

    PIWMini-Project

     Add the temperature equation to the vorticity-

    streamfunction equation and compute the

    increase in heat transfer rate:

    Mini-project:

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    39/47

    PIWConvection

    ∂ T 

    ∂ t  + u∂ T 

    ∂  x + v∂ T 

    ∂  y = α   ∂ 2T 

    ∂  x2   +

    ∂ 2T 

    ∂  y2

     

    α  =   Dρc p

    where

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    40/47

    PIWNatural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    0

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    41/47

    PIWNatural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    125

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    42/47

    Natural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    250

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    43/47

    Natural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    375

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    44/47

    Natural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    500

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    45/47

    Natural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    750

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    46/47

    Natural convection

    Natural convection in a

    closed cavity. The left

    vertical wall is heated.

    1000

    Computational Fluid Dynamics IPIW

  • 8/17/2019 Vorticity 2d - Cfd - Matlab

    47/47

    200 400 600 800 10000

    0.05

    0.1

    0.15

    0.2

    Natural convection

    Heat Transfer Rate

    Conduction only

    Natural convection