Convection Up Wind CFD

download Convection Up Wind CFD

of 24

Transcript of Convection Up Wind CFD

  • 8/9/2019 Convection Up Wind CFD

    1/24

    One Dimensional Convection:Interpolation Models for CFD

    Gerald Recktenwald

    January 28, 2014

    Abstract

    A finite-volume model of the one-dimensional convection-diffusion equa-

    tion is described and implemented in Matlab. This well-known problem

    has an exact solution, which is used to compare the behavior and accuracyof the central difference and upwind difference schemes. Matlab codes

    for both schemes are developed and numerical solutions are presented on

    sequences of finer meshes. As the mesh size is reduced, the dependency

    of the truncation error on mesh size for both schemes is verified. The ex-

    istence and cause of oscillatory solutions for the central difference scheme

    are explained. The central difference scheme is shown to be superior the

    upwind method contrary to older versions of conventional wisdom so

    long as the mesh is suitably refined. The rich literature of alternative

    convective modeling schemes is not addressed here.

    1 Introduction

    Finite volume methods are widely used in computational fluid dynamics (CFD)codes. The elementary finite volume method uses a cell-centered mesh andfinite-difference approximations of first order derivatives. This paper shows howthe finite volume method is applied to a simple model of convective transport:the one-dimensional convection-diffusion equation.

    There are two primary goals of this paper. The first is to expose the finitevolume method. Readers interested in additional details, including applicationto the Navier-Stokes equations, should consult the classic text by Patankar [4].Ferziger and Peric [2] give a more up-to-date discussion of finite volume meth-ods, but without the low level details presented in this paper. Versteeg andMalalasekera [5] provide a detailed discussion of the topics described in thispaper, although their presentation does not deal with the effect of non-uniformmeshes. Abbott and Basco [1] provide a basic analysis of convection modeling

    for the transient version of the one-dimensional convection-diffusion equation.Wesseling [7] gives a mathematically rigorous treatment of the finite volumemethod, including a discussion of different approximations to the convectiveterms.

    Mechanical and Materials Engineering Department, Portland State University, Portland,OR, 97201, [email protected]

  • 8/9/2019 Convection Up Wind CFD

    2/24

    2 THE CONVECTION-DIFFUSION EQUATION 2

    0 0.2 0.4 0.6 0.8 10

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    x

    50

    5

    1

    0

    1

    5

    50

    Figure 1: Exact solution to Equation (1) subject to the boundary conditions

    (0) = 1 and (1) = 0. Parameter labels on the curves are values ofP eL.

    The second goal of this paper is to introduce and compare the central dif-ference scheme and the upwind scheme for modeling the convective term intransport equations. The one-dimensional convection-diffusion equation is acompact, though somewhat non-physical, model of transport of heat, mass andother passive scalars. Applying the finite volume method to this equation allowsdifferent schemes for approximating the convection term to be compared.

    This paper should be considered a brief introduction to the topic of convec-tion modeling schemes. Only the two schemes are considered and the analysisonly involves one model equation.

    2 The Convection-Diffusion Equation

    The one-dimensional convection-diffusion equation is

    d

    dx(u)

    d

    dx

    d

    dx

    S= 0 (1)

    The dependent variable is a scalar that is transported by the velocity u, whichis constant. The diffusion coefficient is , and Sis a volumetric source term.

    ForS= 0 and the boundary conditions

    (0) =0 (L) = L, (2)

    the exact solution to Equation (1) is

    0L 0

    =exp(ux/) 1

    exp(PeL) 1 (3)

    where

    PeL =uL

    (4)

  • 8/9/2019 Convection Up Wind CFD

    3/24

    3 THE FINITE VOLUME MESH 3

    is the Pecletnumber, the dimensionless parameter that describes the relativestrength of convection (u) to diffusion (/L).

    Figure 1 shows a family of solutions to Equation (1) with boundary condi-tions(0) = 1 and (1) = 0. For large PeL, the distribution is nearly uniform

    except for a thin layer near the x = L boundary. Negative u (velocity fromright to left) causes the profile to be shifted to the left instead of the right.

    As PeL 0 the effect of convection disappears and the solution to Equa-tion (1), subject to the boundary conditions in (2), is

    0L 0

    = x

    L (5)

    When u = 0 and S = 0, Equation (1) becomes the one-dimensional Laplaceequation, which describes heat conduction through a slab with uniform con-ductivity. Equation (5) is the solution to the one-dimensional heat conductionproblem with fixed end temperatures.

    3 The Finite Volume Mesh

    In the finite difference method, the mesh is defined by the location of nodes inspace (and possibly time). In the finite volume method, the spatial domain ofthe physical problem is subdivided into non-overlapping cells or control volumes.A single node is located at the geometric centroid of the control volume 1. In thefinite volume method, the numerical approximation is obtained by integratingthe governing equation over the control volume. The nodal volumes are used tocompute the flux of dependent variable from one control volume into the next.

    Figure 2 shows a typical control volume in a Cartesian coordinate system.Since we are only concerned with one-dimensional diffusion problems, the nodesabove and below the control volume are not shown.

    Figure 2 also introduces compass point notation. The node at xi is referredto as point P. Relative to P, the node at xi+1 is labeled E for east, and the nodeatxi1 is labeled W for west. The cell face between P and E is at xe, and thecell face between W and P is at xw. The convention is that upper case letters(P, E, W) refer to the location of the nodes, and the lower case letters (e, w)refer to the cell faces.

    The use of the dual notation may seem cumbersome at first. However,compass point notation is very convenient for the derivation of the discreteapproximation to Equation (1). The use of nodal indices such as i, i+ 1, andi 1 is still important, especially when implementing the finite volume methodin a computer code.

    Figure 3 depicts a group of control volumes along the x axis. The left andright edges are the boundaries of the domain. Two nodes, i = 1 and i = m,

    are on the boundaries, and do not have cell volumes2

    . These nodes are used toimplement boundary conditions.

    1There are other ways to define the locations of nodes relative to the boundaries of thecontrol volume. Locating the node at the centroid is a popular scheme.

    2An alternative view is that the control volumes for i = 1 and i = m have zero width, andhence zero volume.

  • 8/9/2019 Convection Up Wind CFD

    4/24

    4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 4

    4 The Central-Difference Finite Volume Model

    To obtain the finite volume model, Equation (1) is integrated over the controlvolume shown in Figure 2. xe

    xw

    d(u)

    dx dx

    xexw

    d

    dx

    d

    dx

    dx

    xexw

    S dx= 0 (6)

    In the following sections, each term in this equation is evaluated and simplifiedseparately. The parts are then reassembled into a discrete equation relatingat node P to the values at nodes E and W.

    4.1 The Diffusion Term

    The second term in Equation (6) expresses the balance of transport by diffusioninto the control volume3. The integral can be evaluated exactly.

    xexw

    d

    dx

    d

    dx

    dx=

    d

    dxe

    d

    dxw

    (7)

    The two diffusive fluxes are replaced by finite-difference approximations

    d

    dx

    e

    eE P

    xe=De(E P)

    d

    dx

    w

    wP W

    xw=Dw(P W)

    where

    De= exe

    Dw = wxw

    (8)

    andxe = xE xP xw = xP xW. (9)

    3For example, if = T (temperature) and = k (thermal conductivity), the diffusion termis the net conduction of heat into the control volume.

    x

    xi+1xi-1

    P EW

    xexw

    xi

    y

    x

    y

    xe

    xw

    Figure 2: One-dimensional control volume.

  • 8/9/2019 Convection Up Wind CFD

    5/24

    4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 5

    xe,2xw,2

    x2

    i= 1 2 3

    xm1

    xe,m1xw,m1

    m 2 m1 m

    . . .

    . . .

    Figure 3: A one-dimensional mesh used to solve Equation (1). The mesh isdepicted as uniform, but the finite volume method is not restricted to uniformmeshes.

    Remember that P, E, and Ware the values of at the nodes P, E, and Win Figure 2. These are the discrete unknowns that are obtained by solution ofthe finite volume model equations.

    In this paper only the case of uniform is considered, so e = w = .

    Nonuniform is easily handled by the finite volume method [4]. Using theterms just defined, Equation (7) becomes xexw

    x

    x

    dx De(E P) Dw(P W) (10)

    4.2 The Source Term

    The discrete contribution of the source term is obtained by assuming that Shasthe uniform value ofSP throughout the control volume. Thus, xe

    xw

    S dx SPxp. (11)

    The distribution ofSPis supplied as an input to the model. The finite volumemethod also allows source terms to depend on . (See, e.g., [4]).

    4.3 The Convection Term

    The convective term in Equation (6) can be integrated once exactly. xexw

    d(u)

    dx dx= (u)e (u)w (12)

    To evaluate the right hand side of the preceding expression, the values ofeandwneed to be estimated. In the finite volume method, the values of are storedonly at the nodes P, E, and W. The method for determining an interface value

    (say, e) from the nodal values (say, P and E) has important consequencesfor the accuracy of the numerical model of Equation (1).

    A straightforward method for estimating e in terms of the nodal valuesEandP is linear interpolation, as depicted in Figure 4. The linear interpolationformula can be written

    e= eE+ (1 e)P (13)

  • 8/9/2019 Convection Up Wind CFD

    6/24

    4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 6

    xE

    xW

    xP

    xe

    xw

    W

    P

    E

    Figure 4: Linear interpolation to obtain interface values w and e for thecentral difference approximation.

    where

    e= xe xPxE xP

    (14)

    Equations (13) and (14) constitute the central difference scheme for approxi-mating the derivatives4.

    Using linear interpolation to estimate w in terms ofW andP gives

    w = wW+ (1 w)P (15)

    where

    w = xP xwxP xW

    . (16)

    If the mesh is uniform and the nodes are located midway between the cell faces,thenw = e= 1/2.

    Substituting Equation (13) and Equation (15) into Equation (12) and rear-ranging gives xe

    xw

    d(u)

    dx dx= uee(E P) uww(W P) + ueP uwP (17)

    The last two terms in the preceding equation cancel because u is a uniformparameter, i.e. ue = uw. Therefore, Equation (17) simplifies to5 xe

    xw

    d(u)

    dx dx= uee(E P) uww(W P) (18)

    4.4 The Discrete Equation

    Substituting Equation (10), Equation (11) and Equation (18) into Equation (6)and simplifying gives

    aEE+ aPP aWW =b (19)

    4The title comes from the finite-difference approach to modeling Equation (1). The first

    order central difference approximation to the convective term at the interface (x= xe) isd(u)

    dx

    e

    (u)E (u)P

    xE xP

    5Repeating this derivation for two- or three-dimensional convection models requires invoca-tion of the discrete form of the continuity equation. The one-dimensional continuity equationis du/dx = 0. Integrating this equation over the control volume gives ue uw = 0. Thiscomputation is not necessary in the one-dimensional case, because u is a fixed parameter andu= ue= uw.

  • 8/9/2019 Convection Up Wind CFD

    7/24

    4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 7

    where

    aE= 1

    xP(De uee) (20)

    aW = 1

    xP(Dw+ uww) (21)

    aP =aE+ aW (22)

    b= SP (23)

    Equation (19) applies to each internal node in the computational domain.The system of equations for m nodes (including boundary nodes) can be

    written in matrix notation as

    aP,1 aE,1

    aW,2 aP,2 aE,2. . .

    . . . . . .

    aW,i aP,i aE,i. . .

    . . . . . .

    aW,m aP,m

    1

    2...i...

    m

    =

    b1

    b2...bi...

    bm

    (24)

    The system is tridiagonal, and is easily solved with a direct method called thetridiagonal matrix algorithm.

    4.5 Boundary Conditions

    The boundary conditions in Equation (2) are enforced by modifying the coef-

    ficients in Equation (24). For nodes 2 throughm 1, Equations (20) through(23) define the coefficients in the matrix and the right hand side vector. Imple-mentation of boundary conditions only concerns the first row and the last rowin Equation (24). Those equations are

    aP,11 aE,12 = b1

    aW,mm1+ aP,mm = bm

    Setting aP,1 = 1, aE,1 = 0, b1 = 0, and aW,m= 0, aP,m = 1, bm = L. givesthe trivial equations

    1 = 0

    m = L.

    Thus, modifying the coefficients and source terms of the equations for the bound-ary nodes allows the values those nodes to be specified. When the modifiedsys-tem of equations is solved, the boundary nodes are fixed, and the values of theinterior nodes are consistent with the specified boundary values. It is relativelystraightforward to modifying the boundary node equations for other types ofboundary conditions. The procedures for doing so will not be discussed here.

  • 8/9/2019 Convection Up Wind CFD

    8/24

    4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 8

    4.6 Matlab Implementation

    Numerical solutions to Equations (1) and (2) are obtained with the Matlabfunctionscentral1Dand demoConvect1D. The source codes for central1dand

    demoConvect1Dare given in Listing 1 and Listing 2 at the end of this article.The central1D function defines a one-dimensional mesh and evaluates thefinite volume coefficients in Equation (20) through Equation (23) for given valuesof u, , and S. The demoConvect1D function calls central1D or upwind1D(described later) to obtain the finite volume coefficients. demoConvect1D thenuses the tridiagSolve function from the NMM toolbox. The finite volumesolution is then compared with the exact solution.

    4.7 Measuring the Truncation Error

    Since the exact solution is given by Equation (3), the truncation error of thenumerical solution can be computed. Designate the exact solution at cell i as(xi). The error at cell i obtained with the central difference scheme is

    ec,i= c,i (xi)

    wherec,i is the value of at celli obtained with the central difference scheme,i.e.,, by the solution to Equation (24). The largest error in the domain is

    maxi|ec,i| = ||ec||

    The width of the internal control volumes in the domain is x. ReplacingL with x in Equation (4) gives the mesh Peclet number.

    P ex =ux

    (25)

    which describes the localstrength of the convection and diffusion terms for anindividual control volume. PeL is the only true parameter of Equation (1). Pexis an artifact of the mesh used to obtain the numerical approximation to thesolution. As the mesh is refined, x 0 and Pex 0, while PeLis independentof the mesh.

    4.8 Performance of the Central Difference Scheme

    Running demoConvect1D with the default input parameters produces the plotin Figure 5. The numerical solution oscillates with increasing magnitude as xincreases toward 1. By any reasonable measure, this numerical solution is a verybad approximation to the exact solution.

    The oscillations in the numerical solution can be reduced by solving the

    problem with a finer mesh. For example>> demoConvect1D(CDS,32)

    produces the plot in Figure 6. Reducing the control volume width from x= 0.1to x = 0.0333 reduces the mesh Peclet number from 5 to 1 .7. The centraldifference solution to Equation (1) will not oscillate as long as P ex < 2.

  • 8/9/2019 Convection Up Wind CFD

    9/24

    4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 9

    0 0.2 0.4 0.6 0.8 10

    0.5

    1

    1.5

    x

    PeL = 50.0, Pex = 5.0

    CDS scheme, Max error = 1.583

    CDS solution

    exact

    Figure 5: Central difference solutions to Equation (1) for P eL = 50, P ex = 5.

    0 0.2 0.4 0.6 0.8 10

    0.5

    1

    1.5

    x

    PeL = 50.0, Pex = 1.7

    CDS scheme, Max error = 0.268

    CDS solution

    exact

    Figure 6: Central difference solutions to Equation (1) for P eL = 50,P ex = 1.7.

  • 8/9/2019 Convection Up Wind CFD

    10/24

    5 UPWIND DIFFERENCING: A CURE WITH A COST 10

    4.9 Consequences of Negative Coefficients

    The oscillatory numerical solution shown in Figure 5 can be explained by theexistence of negative aE or aW in Equation (19). Consider the possible values

    taken by aE, which is defined by Equation (20). For a uniform mesh, theinterpolation coefficient e is

    e =xP/2

    xe

    and Equation (20) can be rearranged as

    aE= 1

    xP(De uee) =

    1

    xP

    exe

    uexP/2

    xe

    = e

    xPxe

    1

    P ex2

    Since e/(xPxe) > 0 always, the magnitude of P ex determines the signof aE. Specifically, when P ex < 2, aE > 0, and when P ex 2, aE 0.WhenaE 2 is a nagging problem for thecentral difference scheme. The oscillations can be completely eliminated with asurprisingly simple modification. Unfortunately, this simple modification resultsin a severe loss of accuracy in the computed result.

    In 4.9, it was asserted that the existence of a negative coefficients causesthe oscillation in the solution. A quick fix to this problem is to change theinterpolation scheme so that De eue > 0 for any combination ofue, xPand e. The upwind schemeguarantees positiveaE(and positiveaW) with thefollowing choice ofe and w:

    e=

    0 ifue 0

    1 ifue< 0(26)

    w =

    1 ifuw 0

    0 ifuw < 0(27)

  • 8/9/2019 Convection Up Wind CFD

    11/24

    5 UPWIND DIFFERENCING: A CURE WITH A COST 11

    xE

    xW

    xP

    xexw

    W

    P

    E

    xE

    xW

    xP

    xexw

    W

    P

    E

    ue > 0uw > 0 ue < 0uw < 0

    Figure 7: Upwind interpolation to obtain interface values w and e. For clar-ity, we choose W > P > E, though the magnitude of the values is notimportant. The sign ofu determines the value of assumed at the interface.

    Figure 7 provides a graphical representation of the upwind difference scheme.Compare the step-like(x) profiles for the upwind scheme in Figure 7 with thelinear profiles for the central difference scheme in Figure 4.

    In the left half of Figure 7 the velocities are positive. Whenue > 0, Equa-tion (26) and Equation (13) combine to give e = P. Thus, the value of atthe east interface of the control volume is taken to be the nearest nodal valueon the upwind side of the interface.

    Similarly, when uw > 0, Equation (27) and Equation (15) give w = W.At both interfaces, the value of is determined by the upwind neighbor. Inthe right half of Figure 7 the sign of the velocities is reversed, and the upwindneighbors lie to the right of both interfaces.

    5.1 Matlab Implementation

    The coefficients of the upwind scheme are evaluated in the upwind1D function,in Listing 3. The substantial difference between upwind1D and central1d is

    that the coefficients are computed with Equations (26) and (27) instead ofEquations (14) and (16).The upwind difference solutions corresponding to Figure 5 and Figure 6 are

    obtained with the following command line inputs

    >> demoConvect1D(UDS)

    >> demoConvect1D(UDS,32)

    The output from the preceding commands is not shown here.The compConvect1D function in Listing 4 evaluates both the central differ-

    ence and upwind difference solutions, and plots these solutions along with theexact solution. The following command line inputs create the plots in Figure 8.

    >> compConvect1D

    >> compConvect1D(32)

    For both Pex = 5 and Pex = 1.7 the upwind difference scheme is free from oscil-

    lations. In addition, the upwind difference scheme produces a smaller truncationerror than the central difference scheme for both of these meshes.

  • 8/9/2019 Convection Up Wind CFD

    12/24

    5 UPWIND DIFFERENCING: A CURE WITH A COST 12

    0 0.2 0.4 0.6 0.8 10.5

    0

    0.5

    1

    1.5

    2

    x

    PeL= 50.0, Pe

    x= 5.0

    ||ec|| = 1.583, ||e

    u|| = 0.204

    CentralUpwindExact

    0 0.2 0.4 0.6 0.8 10.5

    0

    0.5

    1

    1.5

    2

    x

    PeL= 50.0, Pe

    x= 1.7

    ||ec|| = 0.268, ||e

    u|| = 0.122

    CentralUpwindExact

    Figure 8: Numerical solutions to Equation (1) with the upwind difference schemeand the central difference scheme for two different mesh sizes. The cell Pecletnumber decreases as the number of cells increases.

  • 8/9/2019 Convection Up Wind CFD

    13/24

    6 EFFECT OF MESH REFINEMENT 13

    6 Effect of Mesh Refinement

    In the preceding section, the numerical solutions obtained with the upwindscheme are free of oscillations. Furthermore, the upwind solutions for Pex = 5

    and Pex = 1.7 have smaller measured truncation errors than the central dif-ference solutions. This apparent accuracy advantage is not true in all circum-stances, however.

    Mathematical analysis shows that the truncation errors for the upwind dif-ference scheme and central difference scheme are O (x) and O

    x2

    , respec-

    tively. (See, Ferziger and Peric [2, 4.4] for a straightforward analysis.) Thus,as the mesh is refined, we expect the error in the central difference solution todecrease much more rapidly than the error in the upwind solution.

    Table 1 shows the results of mesh refinement for the upwind and centraldifference solutions to Equation (1). The same data is plotted in Figure 9. Thetable and plot are constructed with the refineConvect1Dfunction in Listing 5.For large x(largeP ex) the upwind difference scheme has a smaller error thanthe central difference scheme. As x and P ex are reduced, the error in the

    central difference solution is reduced much more rapidly than the error in theupwind solution.

    The columns in Table 1 labeled eu ratio and ec ratio confirm the theo-retical prediction of truncation errors. For a given value ofm (given row in thetable), the value in the eu ratio column is the ratio of||eu|| for the precedingrow (previous m) to the value of ||eu|| for the current m. For example, form= 512

    eu ratio =0.0320

    0.0169= 1.89.

    As x 0 the values ofm in subsequent rows differ by a factor of two. Theupwind scheme has a theoretical truncation error that is O (x). By halving thecontrol volume width (by doubling m) one expects the truncation error, whichis proportional to ||eu||, to be reduced by a factor of two. As m increases, the

    values in the eu ratio column are approaching two. The value of the eu ratio atlarge m is more representative of the truncation error because the truncationerror estimate holds as x 0.

    The central difference scheme has a theoretical truncation error that isO

    x2

    . Halving the control volume widths reduces the truncation error by afactor of four, as indicated by the values in the last column of Table 1. Thus,although the upwind scheme is more accurate than the central difference schemeon the coarsest mesh, the improvementin accuracy as xis reduced shows thatthe central difference scheme is superior.

    6.1 Non-uniform Meshes

    Uniform refinement of a uniform mesh is often not the best use of computa-tional resources (memory and processing time). For the one-dimensional prob-lem discussed in this article, the computational cost of mesh refinement is ofno practical concern. However, for industrial applications of CFD, non-uniformmeshes are often essential in order to obtain acceptably accurate results withmodest computing resources.

    The basic idea of non-uniform mesh refinement is to use smaller controlvolume widths (smaller node spacing) in regions where the gradient of the de-

  • 8/9/2019 Convection Up Wind CFD

    14/24

    6 EFFECT OF MESH REFINEMENT 14

    Table 1: Variation of error with mesh spacing for upwind and central differenceschemes. Numerical solutions obtained at P eL = 50. m 2 is the number ofinternal control volumes (cells) in the model.

    Upwind Central Difference

    m x P ex ||eu|| eu ratio ||ec|| ec ratio

    8 0.166667 8.33 0.1780 3.4154

    16 0.071429 3.57 0.1913 0.93 0.9534 3.58

    32 0.033333 1.67 0.1225 1.56 0.2679 3.56

    64 0.016129 0.81 0.0962 1.27 0.0714 3.75

    128 0.007937 0.40 0.0569 1.69 0.0184 3.87

    256 0.003937 0.20 0.0320 1.78 0.0047 3.93

    512 0.001961 0.10 0.0169 1.89 0.0012 3.97

    1024 0.000978 0.05 0.0087 1.94 0.0003 3.98

    2048 0.000489 0.02 0.0044 1.97 0.00007 3.99

    104

    103

    102

    101

    10010

    5

    104

    103

    102

    101

    100

    101

    x

    Maxerror

    Central

    Upwind

    Figure 9: Reduction in error as mesh is refined for upwind and central differenceschemes. The plot data is from the x, ||eu||, and ||ec|| columns in Table 1.

  • 8/9/2019 Convection Up Wind CFD

    15/24

    6 EFFECT OF MESH REFINEMENT 15

    pendent variable is large. For the solution to the boundary value problem showngraphically in Figure 8, the steepest gradients are near thex = L boundary foru > 0 (or PeL > 0). We expect that locally refining the mesh nearx = L shouldimprove the accuracy of the solution.

    Figure 10 shows one common method of creating a non-uniform mesh. Thewidth of adjacent control volumes differs by a constant factor, i.e.

    xi+1xi

    =r (28)

    wherer is a fixed constant. To create such a mesh, one specifies the total lengthL of the region to be subdivided, the stretching ratio r, and the number ofcontrol volumes n. The widths of the control volumes must add up to L, viz.

    L=

    ni=1

    xi= x1+ rx1+ r2x1+ . . . + r

    n1x1

    = x1 1 + r+ r2 + . . . + rn1Define

    S= 1 + r+ r2 + . . . + rn1 =1 rn

    1 r (29)

    where the second equality is an identity. Thus, L = x1Sor

    x1 =L

    S. (30)

    To create a mesh with a geometric progression of sizes

    1. Specify L, r, and n.

    2. Compute x1 from Equation (30).

    3. Compute the remaining cell widths with a loop.

    These calculations are performed by the fvMesh function in Listing 6. ThedemoStretchMeshfunction uses the fvMesh function to create and plot a one-dimensional with r >1 and another with r > compConvect1D(40,5,0.1,0.95)

    PeL = 50.000 Pex_ave, max(Pex), min(Pex) = 1.316, 2.915, 0.437

    Max error = 2.222e-002 for CDS scheme

    Max error = 6.476e-002 for UDS scheme

    The solutions are plotted in Figure 11. Note that the averagecell Pexis less than2, but the maximum Pex is greater than two. This shows that the oscillationsin the central difference solution depend on the gradient of the solution as wellas the local Pex.

  • 8/9/2019 Convection Up Wind CFD

    16/24

    7 SUMMARY 16

    7 Summary

    The results of computations presented in this paper support the following con-clusions.

    Numerical solutions to Equation (1) obtained with the central differencescheme on a uniform mesh will oscillate ifP ex > 2.

    Numerical solutions to Equation (1) obtained with the upwind differencescheme never oscillate for any value ofP ex.

    The stability provided by the upwind difference scheme is obtained witha loss of accuracy. The upwind difference scheme has a truncation errorthat is O (x).

    The truncation error of the central difference scheme is O

    x2

    .

    A non-uniform mesh can be used to reduce or eliminate oscillations in thecomputed solution if the cells with smaller size (closer mesh spacing) isconcentrated in regions with steep gradients in the solutions

    The mesh refinement exercise verifies that the central difference scheme isindeed more accurate than the upwind scheme for sufficiently fine meshes. Thisis especially apparent from the plot of errors in Figure 9. The existence ofoscillatory solutions from the central difference scheme is a worry. However,since one goal of most numerical modeling is (or should be) to obtain mesh-independent solutions, the more rapidly convergent central difference scheme ispreferred.

    The reader should be aware that there are many more schemes for inter-polation in the convection-diffusion equation. A recent paper by Wang andHutter [6] compares no fewer than twelve methods. In addition to accuracy, onemust be concerned about computational cost for multidimensional problems and

    numerical stability.Ferziger and Peric caution against relying too heavily on performance com-

    parisons obtained from solving Equation (1). They state [2, 3.11]

    Indeed, use of this problem as a test cast has probably producedmore poor choices of method than any other in the field. Despitethese difficulties, we shall consider this problem as some of the issuesit raises are worthy of attention.

    The toy codes presented in this paper provide sample implementations ofthe central difference and upwind difference schemes for the one-dimensionaladvection-diffusion equation. Numerical results demonstrate the oscillationsproduced by the central difference scheme on coarse meshes, as well as the

    superior reduction in truncation error obtained by the central difference schemeas the mesh is refined.

    References

    [1] Michael B. Abbot and D.R. Basco. Computational Fluid Dynamics: AnIntroduction for Engineers. Longman, Essex, UK, 1989.

  • 8/9/2019 Convection Up Wind CFD

    17/24

    REFERENCES 17

    [2] Joel H. Ferziger and Milovan Peric. Computational Methods for Fluid Dy-namics. Springer-Verlag, Berlin, third edition, 2001.

    [3] Joe D. Hoffman.Numerical Methods for Engineers and Scientists. McGraw-

    Hill, New York, 1992.[4] S.V. Patankar.Numerical Heat Transfer and Fluid Flow. Hemisphere, Wash-

    ington D.C., 1980.

    [5] H.K. Versteeg and W. Malalasekera.An Introduction to Computational FluidDynamics: The Finite Volume Method. Longman, Essex, UK, 1995.

    [6] Yongqi Wang and Kolumban Hutter. Comparisons of numerical methodswith respect to convectively dominanted problems. International Journal

    for Numerical Methods in Fluids, 37:721745, 2001.

    [7] Pieter Wesseling. Principles of Computational Fluid Dynamics. Springer,Heidelberg, 2001.

    Appendix: Code Listings

    Table 2: Matlab functions used to implement and test the finite-volume ap-proximation to one-dimensional, convection-diffusion equation.

    m-file Description

    central1D Evaluate control-volume, finite-difference coefficientsusing the central difference scheme for the convectionterms.

    compConvect1D Compare central difference and upwind difference so-lutions to the model problem.

    demoConvect1D Solve the model problem with central difference orupwind difference schemes. Compute and print thetruncation error.

    demoStretchMesh Create visual representation of stretched meshes.

    fvMesh1D Create variables that define a one-dimensional finite-volume mesh. Uniform and stretched meshes can becreated.

    refineConvect1D Solve the model problem with central difference orupwind difference schemes on a sequence of finermeshes. Compute and print the truncation error.

    upwind1D Evaluate control-volume, finite-difference coefficientsusing the upwind difference scheme for the convec-tion terms.

  • 8/9/2019 Convection Up Wind CFD

    18/24

    REFERENCES 18

    x1 x2 x3 xn

    L

    Figure 10: Non-uniform mesh based on geometric progression of control volumewidths.

    0 0.2 0.4 0.6 0.8 10.5

    0

    0.5

    1

    1.5

    2

    x

    PeL= 50.0, Pe

    x= 1.3

    ||ec|| = 0.022, ||e

    u|| = 0.065

    Central

    UpwindExact

    Figure 11: Solution of the model problem on a nonuniform mesh with 38 controlvolumes and a stretch ratio of 0.95.

  • 8/9/2019 Convection Up Wind CFD

    19/24

    REFERENCES 19

    function [aw,ap,ae,b] = central1D(u,gam,x,xw,dx,delxw,phib,src)% central1D Central difference coefficients for 1D advection-diffusion equation%% Synopsis: [aw,ap,ae,b] = central1D(u,gam,x,xw,phib)% [aw,ap,ae,b] = central1D(u,gam,x,xw,phib,src)%% Input: u = (scalar) uniform velocity.% gam = (scalar) uniform diffusion coefficient% x = vector of positions of cell centers. There are m-2 interior cells.% x w = vect or of posi tio ns of west fa ces of cel ls.% phib = two-element vector containing boundary values. phib(1) = phi at x=0;% phib(2) = phi at x=xlen% src = (optional) source term. If no value is given, src=0 is assumed. If% src is a scalar (constant), it is replicated as a uniform source term.% O ther wis e, src can be a row or col umn vec tor wit h m el eme nts. T he i=1% and i=m elements are ignored, as these correspond to boundary nodes.%% Output: aw,ap,ae = coefficients of 3 point central difference scheme% b = right hand side vector% x = ve ctor of lo cat ions of ce ll c ent ers

    if nargin replicate for all cells

    b = src*ones(m,1);elseif numel(src) == m % src is properly sized

    b = src(:); % make sure its a column vectorelse

    error(size(src) = %d %d is incompatible with mesh definition,m);end

    % --- Apply boundary conditionsap(1) = 1; ae(1) = 0; b(1) = phib(1); % prescribed phi at west boundaryaw(m) = 0; ap(m) = 1; b(m) = phib(2); % prescribed phi at east boundary

    Listing 1: Thecentral1d function computes the finite volume coefficients forone-dimensional, convection diffusion equation using central differencing for theconvection term.

  • 8/9/2019 Convection Up Wind CFD

    20/24

    REFERENCES 20

    function demoConvect1D(scheme,m,u,gam,r)% demoConvect1D Test finite volume solution to 1D advection-diffusion equation%% Synopsis: demoConvect1D

    % demoConvect1D(scheme)% demoConvect1D(scheme,m)% d emo Conv ect 1D( sche me, m,u ,)% d emo Conv ect 1D( sche me, m,u ,gam )% demoConvect1D(scheme,m,u,gam,r)%% Input: scheme = (optional,string) indicates convection modeling scheme% scheme = UDS for upwind differencing% CDS for central differencing% m = ( opti ona l) tota l n umb er o f n ode s; Def aul t: m = 12% Internal cell width = 1/(m-2). Domain length is 1% u = ( opti ona l, scal ar) un ifor m v elo cit y. Def aul t: u = 5% gam = (optional, scalar) diffusion coefficient; Default: gam = 0.1% r = m esh str etc hing ra tio . If r =1, me sh i s u nif orm% I f r> 1 c ont rol vol ume wid ths in cre ase wit h x . I f r

  • 8/9/2019 Convection Up Wind CFD

    21/24

    REFERENCES 21

    function [aw,ap,ae,b] = upwind1D(u,gam,x,xw,dx,delxw,phib,src)% upwind1D Upwind difference coefficients for 1D advection-diffusion equation%% Synopsis: [aw,ap,ae,b] = upwind1D(u,gam,x,xw,phib)% [aw,ap,ae,b] = upwind1D(u,gam,x,xw,phib,src)%% Input: u = (scalar) uniform velocity.% gam = (scalar) uniform diffusion coefficient% x = vector of positions of cell centers. There are m-2 interior cells.% x w = vect or of posi tio ns of west fa ces of cel ls.% phib = two-element vector containing boundary values. phib(1) = phi at x=0;% phib(2) = phi at x=xlen% src = (optional) source term. If no value is given, src=0 is assumed. If% src is a scalar (constant), it is replicated as a uniform source term.% O ther wis e, src can be a row or col umn vec tor wit h m el eme nts. T he i=1% and i=m elements are ignored, as these correspond to boundary nodes.%% Output: aw,ap,ae = coefficients of 3 point upwind difference scheme% b = right hand side vector% x = ve ctor of lo cat ions of ce ll c ent ers

    if nargin0

    bw = 1; be = 0 ;else

    bw = 0; be = 1 ;endfor i=2:m-1

    ae(i) = (gam/delxw(i+1) - u*be)/dx(i);aw(i) = (gam/delxw(i) + u*bw)/dx(i);

    endap = ae + aw; % ap is a vector with same shape as ae and aw

    % --- Create right hand side vectorif numel(src) == 1 % src is a scalar ==> replicate for all cells

    b = src*ones(m,1);elseif numel(src) == m % src is properly sized

    b = src(:); % make sure its a column vectorelse

    error(size(src) = %d %d is incompatible with mesh definition,m);end

    % --- Apply boundary conditionsap(1) = 1; ae(1) = 0; b(1) = phib(1); % prescribed phi at west boundaryaw(m) = 0; ap(m) = 1; b(m) = phib(2); % prescribed phi at east boundary

    Listing 3: The upwind1d function computes the finite volume coefficients forone-dimensional, convection diffusion equation using upwind differencing forthe convection term.

  • 8/9/2019 Convection Up Wind CFD

    22/24

    REFERENCES 22

    function compConvect1D(m,u,gam,r)% compConvect1D Compare CDS and UDS schemes for 1D advection-diffusion equation.% Plot a comparision with exact solution, and print truncation errors.%% Synopsis: compConvect1D% compConvect1D(m)% compConvect1D(m,u)% compConvect1D(m,u,gam)% compConvect1D(m,u,gam,r)%% Input: m = total number of nodes; Number of interior cells is m-2% u = (scalar) uniform velocity.% gam = (scalar) uniform diffusion coefficient% r = m esh str etc hin g ra tio . If r =1, me sh i s u nif orm%% Output: Plot of central difference and upwind difference solutions. Print% out of truncation errors

    if nargin

  • 8/9/2019 Convection Up Wind CFD

    23/24

    REFERENCES 23

    function refineConvect1D(mm,u,gam)

    % refineConvect1D Mesh refinement of solutions to 1D advection-diffusion equation.% Obtain CDS and UDS solutions at different mesh sizes.%% Synopsis: refineConvect1D% refineConvect1D(mm)% refineConvect1D(mm,u)% r efi neCo nve ct1 D(mm ,u, gam )%% Input: mm = vector of m values. m is the number of nodes (including boundary% nodes) in the domain. Default: mm = [8 16 32 64 128 256 512].% CDS and UDS solutions are obtained for each m in mm.% u = (opt ion al, sca lar ) u nif orm vel oci ty. De fau lt: u = 5% gam = (optional, scalar) diffusion coefficient; Default: gam = 0.1%% Output: Table of truncation errors versus mesh size. Plot of same data.

    if nargin

  • 8/9/2019 Convection Up Wind CFD

    24/24

    REFERENCES 24

    function [x,xw,dx,delxw] = fvMesh1D(nx,xlen,r)% fvMesh1D Create one-dimensional finite-volume mesh. Uniform meshes and% stretched non-uniform meshes are supported.%% Synopsis: [x,xw] = fvMesh1D% [x,xw] = fvMesh1D(nx)% [x,xw] = fvMesh1D(nx,xlen)% [x,xw] = fvMesh1D(nx,xlen,r)% [x,xw,dx] = fvMesh1D(...)% [x, xw,d x,d elx w] = fv Mes h1D (... )%% Input: nx = Number of cells (internal CVs, not nodes). Default: nx=10% x len = over all le ngth of th e m esh. D efa ult: x len = 1% r = growth ratio: dx(i+1) = r*dx(i). Default: r = 1, i.e mesh% is unif orm . If r>1 con tro l vo lum e w idth s i ncr eas e wi th x.% If r