You are on page 1of 24

One Dimensional Convection: Interpolation Models for CFD

Gerald Recktenwald January 29, 2006

Abstract The nite-volume method is used to obtain numerical solutions of the one-dimensional convection-diusion equation. This well-known problem has an exact solution, and is used to compare the behavior and accuracy of the central dierence and upwind dierence schemes. Matlab codes for both schemes are developed and numerical solutions are presented on sequences of ner meshes. As the mesh size is reduced the dependency of the truncation error on mesh size for both schemes is veried. The existence and cause of oscillatory solutions for the central dierence scheme are explained. The superior performance of the central dierence method under suitable mesh renement is demonstrated.

Introduction

Finite volume methods are widely used in computational uid dynamics (CFD) codes. The typical nite volume method uses a cell-centered mesh and nitedierence approximations of rst order derivatives. This paper shows how the nite volume method is applied to a model of convective transport: the onedimensional convection-diusion equation. There are two primary goals of this paper. The rst is to expose the nite volume method. Readers interested in additional details, including application to the Navier-Stokes equations, should consult the classic text by Patankar [4]. Ferziger and Peri [2] give a more up-to-date discussion of nite volume methc ods, but without the low level details presented in this paper. Versteeg and Malalasekera [5] provide a detailed discussion of the topics described in this paper, although their presentation does not deal with the eect of non-uniform meshes. Abbott and Basco [1] provide a basic analysis of convection modeling for the transient version of the one-dimensional convection-diusion equation. Wesseling [7] gives a mathematically rigorous treatment of the nite volume method, including a discussion of dierent approximations to convective terms. The second goal of this paper is to introduce and compare the central difference scheme and the upwind scheme for modeling the convective term in transport equations. The one-dimensional convection-diusion equation is a
Mechanical and Materials Engineering Department, Portland State University, Portland, OR, 97201, gerry@me.pdx.edu

THE CONVECTION-DIFFUSION EQUATION

compact, though somewhat non-physical, model of transport. Applying the nite volume method to this equation allows dierent schemes for approximating the convection term to be compared.

The Convection-Diusion Equation


d d (u) dx dx d dx

The one-dimensional convection-diusion equation is S =0 (1)

The dependent variable is a scalar that is transported by the velocity u, which is constant. The diusion coecient is , and S is a volumetric source term. For S = 0 and the boundary conditions (0) = 0 the exact solution to Equation (1) is 0 exp(ux/) 1 = L 0 exp(PeL ) 1 where PeL = (3) (L) = L , (2)

uL (4) is the Peclet number, the dimensionless parameter that describes the relative strength of convection (u) to diusion (/L). As PeL 0 the eect of convection disappears and the solution to Equation (1), subject to the boundary conditions in (2), is 0 x (5) = L 0 L

1 0.9 0.8 0.7


1 5

50

0.6 0.5 0.4 0.3 0.2 0.1 0 0


50 5 1

0.2

0.4 x

0.6

0.8

Figure 1: Exact solution to Equation (1) subject to the boundary conditions (0) = 1 and (1) = 0. Parameter labels on the curves are the values of P eL .

THE FINITE VOLUME MESH


xw xe

W xi-1 xi

E xi+1

y x xw x xe

Figure 2: One-dimensional control volume. When u = 0 and S = 0, Equation (1) becomes the one-dimensional Laplace equation, which describes heat conduction through a slab with uniform conductivity. Equation (5) is the solution to the one-dimensional heat conduction problem with xed end temperatures. Figure 1 shows a family of solutions to Equation (1) with boundary conditions (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 from right to left) causes the prole to be shifted to the left instead of the right.

The Finite Volume Mesh

In the nite dierence method, the mesh is dened by the location of nodes in space (and possibly time). In the nite volume method, the spatial domain of the physical problem is subdivided into non-overlapping cells or control volumes. A single node is located at the geometric centroid of the control volume1 . Figure 2 shows a typical control volume in a Cartesian coordinate system. Since we are only concerned with one-dimensional diusion problems, the nodes above and below the control volume are not shown. Figure 2 also introduces compass point notation. The node at xi is referred to as point P. Relative to P, the node at xi+1 is labelled E for east, and the node at xi1 is labelled W for west. The cell face between P and E is at xe , and the cell 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 rst. However, compass point notation is very convenient for the derivation of the discrete approximation to Equation (1). The use of nodal indices such as i, i + 1, and i 1 is still important, especially when implementing the nite volume method in a computer code. Figure 3 depicts a group of control volumes along the x axis. The left and right edges are the boundaries of the domain. Two nodes, i = 1 and i = m,
1 There are other ways to dene the locations of nodes relative to the boundaries of the control volume. Locating the node at the centroid is a popular scheme.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL


x2 ... i=1 2 3 ...
xw,2 xe,2 xw,m1 xe,m1

xm1 m 2 m1 m

Figure 3: A one-dimensional mesh used to solve Equation (1). The mesh is depicted as uniform, but the nite volume method is not restricted to uniform meshes. are on the boundaries, and do not have cell volumes2 . These nodes are used to implement boundary conditions.

The Central-Dierence Finite Volume Model

To obtain the nite volume model, Equation (1) is integrated over the control volume shown in Figure 2.
xe xw

d(u) dx dx

xe xw

d dx

d dx

xe

dx
xw

S dx = 0

(6)

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

4.1

The Diusion Term

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

d dx

d dx

dx =

d dx

d dx

(7)
w

The two diusive uxes are replaced by nite-dierence approximations d dx e


e

E P = De (E P ) xe P W = Dw (P W ) xw w xw

where

d dx

w
w

De =

e xe

Dw =

(8)

2 An alternative view is that the control volumes for i = 1 and i = m have zero width, and hence zero volume. 3 For example, if = T (temperature) and = k (thermal conductivity), the diusion term is the net conduction of heat into the control volume.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

and xe = xE xP xw = xP xW . (9) Remember that P , E , and W are the values of at the nodes P, E, and W in Figure 2. These are the discrete unknowns that are obtained by solution of the nite volume model equations. In this paper only the case of uniform is considered, so e = w = . Nonuniform is easily handled by the nite volume method [4]. Using the terms just dened, Equation (7) becomes
xe xw

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 S has the uniform value of SP throughout the control volume. Thus,
xe

S dx SP xp .
xw

(11)

The distribution of SP is supplied as an input to the model. The nite volume method 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 without approximation. xe d(u) dx = (u)e (u)w (12) dx xw To evaluate the right hand side of the preceding expression, the values of e and w need to be estimated. In the nite volume method, the values of are stored only 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 consequences for the accuracy of the numerical model of Equation (1). A straightforward method for estimating e in terms of the nodal values E and P is linear interpolation, as depicted in Figure 4. The linear interpolation formula can be written e = e E + (1 e )P where e = xe xP xE xP (13)

(14)

Equations (13) and (14) constitute the central dierence scheme for approximating the derivatives4 .
4 This title comes from the nite-dierence approach to modeling Equation (1). The rst order central dierence approximation to the convective term at the interface (x = xe ) is d(u) (u)E (u)P dx e xE xP

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL


W P E
xe xE

xW

xw

xP

Figure 4: Linear interpolation to obtain interface values w and e for the central dierence approximation. Using linear interpolation to estimate w in terms of W and P gives w = w W + (1 w )P where w = xP xw . xP xW (15)

(16)

If the mesh is uniform and the nodes are located midway between the cell faces, then w = e = 1/2. Substituting Equation (13) and Equation (15) into Equation (12) and rearranging gives
xe xw

d(u) dx = ue e (E P ) uw w (W P ) + ue P uw P dx

(17)

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

d(u) dx = ue e (E P ) uw w (W P ) dx

(18)

4.4

The Discrete Equation

Substituting Equation (10), Equation (11) and Equation (18) into Equation (6) and simplifying gives aE E + aP P aW W = b (19)

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

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

where aE = 1 (De ue e ) xP 1 (Dw + uw w ) xP (20)

aW =

(21) (22) (23)

aP = aE + aW b = SP

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 b1 1 aP,1 aE,1 2 b2 aW,2 aP,2 aE,2 . . .. .. .. . . . . . . = . (24) i bi aW,i aP,i aE,i .. . . .. .. . . . . . . . bm m aW,m aP,m The system is tridiagonal, and is easily solved with a direct method.

4.5

Boundary Conditions

The boundary conditions in Equation (2) are enforced by modifying the coefcients in Equation (24). For nodes 2 through m 1, Equations (20) through (23) dene the coecients in the matrix and the right hand side vector. Implementation of boundary conditions only concerns the rst row and the last row in Equation (24). Those equations are aP,1 1 aE,1 2 = b1 aW,m m1 + aP,m m = bm Setting aP,1 = 1, aE,1 = 0, b1 = 0 , and aW,m = 0, aP,m = 1, bm = L . gives the trivial equations 1 = 0 m = L . Thus, modifying the coecients and source terms of the equations for the boundary nodes allows the values those nodes to be specied. When the modied system of equations is solved, the boundary nodes are xed, and the values of the interior nodes are consistent with the specied boundary values. It is relatively straightforward to modifying the boundary node equations for other types of boundary conditions. The procedures for doing so will not be discussed here.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

4.6

Matlab Implementation

Numerical solutions to Equations (1) and (2) are obtained with the Matlab functions central1D and demoConvect1D. The source code for central1d and demoConvect1D is given in Listing 1 and Listing 2 at the end of this article. The central1D function denes a one-dimensional mesh and evaluates the nite volume coecients in Equation (20) through Equation (23) for given values of u, , and S. The demoConvect1D function calls central1D or upwind1D (described later) to obtain the nite volume coecients. demoConvect1D then uses the tridiagSolve function from the NMM toolbox. The nite volume solution 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 the numerical solution can be computed. Designate the exact solution at cell i as (xi ). The error at cell i obtained with the central dierence scheme is ec,i = c,i (xi ) where c,i is the value of at cell i obtained with the central dierence scheme. The largest error in the domain is max |ec,i | = ||ec ||
i

The width of the internal control volumes in the domain is x. Replacing L with x in Equation (4) gives the mesh Peclet number. P ex = ux (25)

which describes the local strength of the convection and diusion terms for an individual control volume. PeL is the only true parameter of Equation (1). Pex is an artifact of the mesh used to obtain the numerical approximation to the solution. As the mesh is rened, x 0 and Pex 0, while PeL is independent of the mesh.

4.8

Performance of the Central Dierence Scheme

Running demoConvect1D with the default input parameters produces the plot in Figure 5. The numerical solution oscillates with increasing magnitude as x increases toward 1. By any reasonable measure, this numerical solution is a very bad approximation to the exact solution. The oscillations in the numerical solution can be reduced by solving the problem with a ner mesh. For example
>> demoConvect1D(CDS,32)

produces the plot in Figure 6. Reducing the control volume width from x = 0.1 to x = 0.0333 reduces the mesh Peclet number from 5 to 1.7. The central dierence solution will not oscillate as long as P ex < 2.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

1.5 CDS solution exact

0.5 PeL = 50.0, Pex = 5.0 CDS scheme, Max error = 1.583

0 0

0.2

0.4 x

0.6

0.8

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

1.5 CDS solution exact

0.5 PeL = 50.0, Pex = 1.7 CDS scheme, Max error = 0.268

0 0

0.2

0.4 x

0.6

0.8

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

UPWIND DIFFERENCING: A CURE WITH A COST

10

4.9

Consequences of Negative Coecients

The oscillatory numerical solution shown in Figure 5 can be explained by the existence of negative aE or aW in Equation (19). Consider the possible values taken by aE , which is dened by Equation (20). For a uniform mesh, the interpolation coecient e is e = xP /2 xe e xP /2 ue xe xe 1 P ex 2

and Equation (20) can be rearranged as aE = 1 1 (De ue e ) = xP xP =

e xP xe

Since e /(xP xe ) > 0 always, the magnitude of P ex determines the sign of aE . Specically, when P ex < 2, aE > 0, and when P ex 2, aE 0. When aE < 0, the eigenvalues of the coecient matrix in Equation (24) become complex, and the numerical solution oscillates. See, e.g. Homan [3, Chapter 14] or Wesseling [7, Chapter 4] for a proof. The magnitude of P ex can always be kept below the threshold value of two by choosing a suciently ne mesh. For one-dimensional problems, this is not an issue. However, this P ex limit is also true for two- and three-dimensional problems. In the early days of CFD, reducing the mesh spacing to guarantee P ex < 2 was not always an option because memory was limited, and solutions on ne meshes took too much time. Modern computers have enough memory and oating point performance that selecting a suciently ne mesh to minimize (or eliminate) oscillations is usually not a problem. Nonetheless, a more robust formulation is still desirable for many practical problems, especially for exploratory calculations.

Upwind Dierencing: A Cure with a Cost

The existence of oscillatory solutions for P ex > 2 is a nagging problem for the central dierence scheme. The oscillations can be completely eliminated with a surprisingly simple modication. Unfortunately, this simple modication results in a severe loss of accuracy in the computed result. In 4.9, it was asserted that the existence of a negative coecients causes the oscillation in the solution. A quick x to this problem is to change the interpolation scheme so that De e ue > 0 for any combination of ue , xP and e . The upwind scheme guarantees positive aE (and positive aW ) with the following choice of e and w : e = 0 if ue 0 1 if ue < 0 1 if uw 0 0 if uw < 0 (26)

w =

(27)

UPWIND DIFFERENCING: A CURE WITH A COST

11

uw > 0

ue > 0

W E

uw < 0

ue < 0

P
xW xw xP xe

P
xW xw xP xe

E
xE

xE

Figure 7: Upwind interpolation to obtain interface values w and e . For clarity, we choose W > P > E , though the magnitude of the values is not important. The sign of u determines the value of assumed at the interface. Figure 7 provides a graphical representation of the upwind dierence scheme. Compare the step-like (x) proles for the upwind scheme in Figure 7 with the linear proles for the central dierence scheme in Figure 4. In the left half of Figure 7 the velocities are positive. When ue > 0, Equation (26) and Equation (13) combine to give e = P . Thus, the value of at the east interface of the control volume is taken to be the nearest nodal value on 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. In the right half of Figure 7 the sign of the velocities is reversed, and the upwind neighbors lie to the right of both interfaces.

5.1

Matlab Implementation

The coecients of the upwind scheme are evaluated in the upwind1D function, in Listing 3. The substantial dierence between upwind1D and central1d is that the coecients are computed with Equations (26) and (27) instead of Equations (14) and (16). The upwind dierence 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 dierence and upwind dierence solutions, and plots these solutions along with the exact 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 dierence scheme is free from oscillations. In addition, the upwind dierence scheme produces a smaller truncation error than the central dierence scheme for both of these meshes.

UPWIND DIFFERENCING: A CURE WITH A COST

12

2 Central Upwind Exact 1.5

1 0.5 PeL = 50.0, Pex = 5.0

||ec|| = 1.583,

||eu|| = 0.204

0.5 0

0.2

0.4 x

0.6

0.8

2 Central Upwind Exact 1.5

1 0.5 PeL = 50.0, Pex = 1.7

||ec|| = 0.268,

||eu|| = 0.122

0.5 0

0.2

0.4 x

0.6

0.8

Figure 8: Numerical solutions to Equation (1) with the upwind dierence scheme and the central dierence scheme for two dierent mesh sizes. The cell Peclet number decreases as the number of cells increases.

EFFECT OF MESH REFINEMENT

13

Eect of Mesh Renement

In the preceding section, the numerical solutions obtained with the upwind scheme are free of oscillations. Furthermore, the upwind solutions for Pex = 5 and Pex = 1.7 have smaller measured truncation errors than the central difference solutions. This apparent accuracy advantage is not true in all circumstances, however. Mathematical analysis shows that the truncation errors for the upwind difference scheme and central dierence scheme are O (x) and O x2 , respectively. (See, Ferziger and Peri [2, 4.4] for a straightforward analysis.) Thus, c as the mesh is rened, we expect the error in the central dierence solution to decrease much more rapidly than the error in the upwind solution. Table 1 shows the results of mesh renement for the upwind and central dierence solutions to Equation (1). The same data is plotted in Figure 9. The table and plot are constructed with the refineConvect1D function in Listing 5. For large x (large P ex ) the upwind dierence scheme has a smaller error than the central dierence scheme. As x and P ex are reduced, the error in the central dierence solution is reduced much more rapidly than the error in the upwind solution. The columns in Table 1 labelled eu ratio and ec ratio conrm the theoretical prediction of truncation errors. For a given value of m (given row in the table), the value in the eu ratio column is the ratio of ||eu || for the preceding row (previous m) to the value of ||eu || for the current m. For example, for m = 512 0.0320 = 1.89. eu ratio = 0.0169 As x 0 the values of m in subsequent rows dier by a factor of two. The upwind scheme has a theoretical truncation error that is O (x). By halving the control volume width (by doubling m) one expects the truncation error, which is 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 at large m is more representative of the truncation error because the truncation error estimate holds as x 0. The central dierence scheme has a theoretical truncation error that is O x2 . Halving the control volume widths reduces the truncation error by a factor 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 dierence scheme on the coarsest mesh, the improvement in accuracy as x is reduced shows that the central dierence scheme is superior.

6.1

Non-uniform Meshes

Uniform renement of a uniform mesh is often not the best use of computational resources (memory and processing time). For the one-dimensional problem discussed in this article, the computational cost of mesh renement is of no practical concern. However, for industrial applications of CFD, non-uniform meshes are often essential in order to obtain acceptably accurate results with modest computing resources. The basic idea of non-uniform mesh renement is to use smaller control volume widths (smaller node spacing) in regions where the gradient of the de-

EFFECT OF MESH REFINEMENT

14

Table 1: Variation of error with mesh spacing for upwind and central dierence schemes. Numerical solutions obtained at P eL = 50. m 2 is the number of internal control volumes (cells) in the model. Upwind m 8 16 32 64 128 256 512 1023 x 0.166667 0.071429 0.033333 0.016129 0.007937 0.003937 0.001961 0.000978 P ex 8.33 3.57 1.67 0.81 0.40 0.20 0.10 0.05 ||eu || 0.1780 0.1913 0.1225 0.0962 0.0569 0.0320 0.0169 0.0087 0.93 1.56 1.27 1.69 1.78 1.89 1.94 eu ratio Central Dierence ||ec || 3.4154 0.9534 0.2679 0.0714 0.0184 0.0047 0.0012 0.0003 3.58 3.56 3.75 3.87 3.93 3.97 3.98 ec ratio

pendent variable is large. For the solution to the boundary value problem shown graphically in Figure 8, the steepest gradients are near the x = L boundary. We expect that locally rening the mesh near x = L should improve the accuracy of the solution. Figure 10 shows one common method of creating a non-uniform mesh. The width of adjacent control volumes diers by a constant factor, i.e. xi+1 =r xi (28)

where r is a xed constant. To create such a mesh, one species the total length L of the region to be subdivided, the stretching ratio r, and the number of control volumes n. The widths of the control volumes must add up to L, viz.
n

L=
i=1

xi = x1 + rx1 + r2 x1 + . . . + rn1 x1 = x1 1 + r + r2 + . . . + rn1

1 rn 1r where the second equality is an identity. Thus, L = x1 S or S = 1 + r + r2 + . . . + rn1 = x1 = L . S

Dene

(29)

(30)

To create a mesh with a geometric progression of sizes 1. Specify L, r, and n. 2. Compute x1 from Equation (30).

SUMMARY

15

10

Central Upwind 10
0

Max error

10

10

10

10

10

10

10 x

10

10

Figure 9: Reduction in error as mesh is rened for upwind and central dierence schemes. The data is the same as the m, ||eu || , and ||ec || columns in Table 1. 3. Compute the remaining cell widths with a loop. These calculations are automated in the fvMesh function in Listing 6. The demoStretchMesh function uses the fvMesh function to create and plot a onedimensional with r > 1 and another with r < 1. The central1D, upwind1D, and compConvect1D functions allow specication of the mesh stretch ratio r. For example, the following Matlab session compares the performance of the central dierence and upwind dierence scheme on a mesh with 38 control volumes that decrease in size by a factor of 0.95.
>> compConvect1D(40,5,0.1,0.95) PeL = 50.000 Pex_ave, max(Pex), min(Pex) = 1.316, Max error = 2.222e-002 for CDS scheme Max error = 6.476e-002 for UDS scheme 2.915, 0.437

The solutions are plotted in Figure 11. Note that the average cell Pex is less than 2, but the maximum Pex is greater than two. This shows that the oscillations in the central dierence solution depend on the gradient of the solution as well as the local Pex .

Summary

The results of computations presented in this paper support the following conclusions. Numerical solutions to Equation (1) obtained with the central dierence scheme will oscillate if P ex > 2.

REFERENCES

16

Numerical solutions to Equation (1) obtained with the upwind dierence scheme never oscillate for any value of P ex . The stability provided by the upwind dierence scheme is obtained with a loss of accuracy. The upwind dierence scheme has a truncation error that is O (x). The truncation error of the central dierence scheme is O x2 . The mesh renement exercise veries that the central dierence scheme is indeed more accurate than the upwind scheme for suciently ne meshes. This is especially apparent from the plot of errors in Figure 9. The existence of oscillatory solutions from the central dierence scheme is a worry. However, since one goal of most numerical modeling is (or should be) to obtain meshindependent solutions, the more rapidly convergent central dierence scheme is preferred. The reader should be aware that there are many more schemes for interpolation in the convection-diusion equation. A recent paper by Wang and Hutter [6] compares no fewer than twelve methods. In addition to accuracy, one must be concerned about computational cost for multidimensional problems and numerical stability. Ferziger and Peri caution against relying too heavily on performance comc parisons obtained from solving Equation (1). They state [2, 3.11] Indeed, use of this problem as a test cast has probably produced more poor choices of method than any other in the eld. Despite these diculties, we shall consider this problem as some of the issues it raises are worthy of attention. The toy codes presented in this paper provide sample implementations of the central dierence and upwind dierence schemes for the one-dimensional advection-diusion equation. Numerical results demonstrate the oscillations produced by the central dierence scheme on coarse meshes, as well as the superior reduction in truncation error obtained by the central dierence scheme as the mesh is rened.

References
[1] Michael B. Abbot and D.R. Basco. Computational Fluid Dynamics: An Introduction for Engineers. Longman, Essex, UK, 1989. [2] Joel H. Ferziger and Milovan Peri. Computational Methods for Fluid Dyc namics. Springer-Verlag, Berlin, third edition, 2001. [3] Joe D. Homan. Numerical Methods for Engineers and Scientists. McGrawHill, New York, 1992. [4] S.V. Patankar. Numerical Heat Transfer and Fluid Flow. Hemisphere, Washington D.C., 1980. [5] H.K. Versteeg and W. Malalasekera. An Introduction to Computational Fluid Dynamics: The Finite Volume Method. Longman, Essex, UK, 1995.

REFERENCES

17

[6] Yongqi Wang and Kolumban Hutter. Comparisons of numerical methods with 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 nite-volume approximation to one-dimensional, convection-diusion equation. m-le central1D Description Evaluate control-volume, nite-dierence coecients using the central dierence scheme for the convection terms. Compare central dierence and upwind dierence solutions to the model problem. Solve the model problem with central dierence or upwind dierence schemes. Compute and print the truncation error. Create visual representation of stretched meshes. Create variables that dene a one-dimensional nitevolume mesh. Uniform and stretched meshes can be created. Solve the model problem with central dierence or upwind dierence schemes on a sequence of ner meshes. Compute and print the truncation error. Evaluate control-volume, nite-dierence coecients using the upwind dierence scheme for the convection terms.

compConvect1D demoConvect1D

demoStretchMesh fvMesh1D

refineConvect1D

upwind1D

REFERENCES

18

L x1 x2 x3 xn

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

2 Central Upwind Exact 1.5

1 0.5 PeL = 50.0, Pex = 1.3

||ec|| = 0.022,

||eu|| = 0.065

0.5 0

0.2

0.4 x

0.6

0.8

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

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. % xw = vector of positions of west faces of cells. % 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. % Otherwise, src can be a row or column vector with m elements. The 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 = vector of locations of cell centers if nargin<8, src = 0; end % Default: no source term

% --- Compute CVFD coefficients m = length(x); ae = zeros(m,1); aw = ae; for i=2:m-1 be = 0.5*dx(i)/delxw(i+1); ae(i) = (gam/delxw(i+1) - u*be)/dx(i); bw = 0.5*dx(i)/delxw(i); aw(i) = (gam/delxw(i) + u*bw)/dx(i); end ap = ae + aw; % ap is a vector with same shape as ae and aw % --- Create right hand side if prod(size(src)) == 1 b = src*ones(m,1); elseif prod(size(src)) == m b = src(:); else error(sprintf(size(src) = end vector % src is a scalar ==> replicate for all cells % % src is properly sized make sure its a column vector

%d %d is incompatible with mesh definition,m));

% --- Apply boundary conditions ap(1) = 1; ae(1) = 0; b(1) = phib(1); aw(m) = 0; ap(m) = 1; b(m) = phib(2);

% %

prescribed phi at west boundary prescribed phi at east boundary

Listing 1: The central1d function computes the nite volume coecients for one-dimensional, convection diusion equation using central dierencing for the convection term.

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) % demoConvect1D(scheme,m,u,) % demoConvect1D(scheme,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 = (optional) total number of nodes; Default: m = 12 % Internal cell width = 1/(m-2). Domain length is 1 % u = (optional, scalar) uniform velocity. Default: u = 5 % gam = (optional, scalar) diffusion coefficient; Default: gam = 0.1 % r = mesh stretching ratio. If r=1, mesh is uniform % If r>1 control volume widths increase with x. If r<1, control % volume widths decrease with x. % % Output: Plot exact and numerical solutions, print max error in numerical solution if if if if if nargin<1, nargin<2, nargin<3, nargin<4, nargin<5, scheme=CDS; m = 12; u = 5; gam = 0.1; r = 1; end end end end end

% --- Set constants and default input values xlen = 1; % domain length phib = [1 0]; % boundary values % --- Create the mesh. fvMesh1D works for uniform or stretched meshes [x,xw,dx,delxw] = fvMesh1D(m-2,xlen,r); % --- Get CVFD coefficients and solve the system if strcmp(upper(scheme),UDS) [aw,ap,ae,b] = upwind1D(u,gam,x,xw,dx,delxw,phib); elseif strcmp(upper(scheme),CDS) [aw,ap,ae,b] = central1D(u,gam,x,xw,dx,delxw,phib); else error(sprintf(scheme = %s is not supported,scheme)); end phi = tridiagSolve(ap,-ae,-aw,b); % solve the system of equations % --- Evaluate exact solution and maximum error in the numerical solution PeL = u*xlen/gam; pe = phib(1) + (phib(2)-phib(1))*(exp(u*x/gam) - 1)/(exp(PeL)-1); maxerr = norm(phi-pe,inf); Pex = u*dx(2:end-1)/gam; Pexave = u*xlen/(m-2)/gam; fprintf(PeL = %5.3f Pex_ave, max(Pex), min(Pex) = %5.3f, %5.3f, %5.3f\n,... PeL,Pexave,max(Pex),min(Pex)); fprintf(\tMax error = %6.4f for %s scheme\n,maxerr,scheme); % --- plot results xe = linspace(0,xlen); % many x values makes smooth curve for exact solution pe = phib(1) + (phib(2)-phib(1))*(exp(u*xe/gam) - 1)/(exp(u*xlen/gam)-1); plot(x,phi,o--,xe,pe,-); legend(sprintf(%s solution,scheme),exact,2); xlabel(x); ylabel(\phi,Rotation,0); axis([0 xlen min(phib) 1.5*max(phib)]) text(0.1,0.4,sprintf(PeL = %-4.1f, Pex_{ave} = %-4.1f,PeL,Pexave),Fontsize,14); text(0.1,0.25,sprintf(%s scheme, Max error = %-5.3f,scheme,maxerr),Fontsize,14);

Listing 2: The demoConvect1D function obtains solutions to the one-dimensional convection-diusion equation using either upwind or central dierencing for the convection terms.

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. % xw = vector of positions of west faces of cells. % 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. % Otherwise, src can be a row or column vector with m elements. The 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 = vector of locations of cell centers if nargin<8, src = 0; end % Default: no source term

% --- Compute CVFD coefficients % NOTE: For this 1D problem, u is constant, so bw and be are evaluated % once. In general, bw and be vary with position and must be % updated inside the loop that assigns ae(i) and aw(i) m = length(x); ae = zeros(m,1); aw = ae; if u>0 bw = 1; be = 0; else bw = 0; be = 1; end for i=2:m-1 ae(i) = (gam/delxw(i+1) - u*be)/dx(i); aw(i) = (gam/delxw(i) + u*bw)/dx(i); end ap = ae + aw; % ap is a vector with same shape as ae and aw % --- Create right hand side if prod(size(src)) == 1 b = src*ones(m,1); elseif prod(size(src)) == m b = src(:); else error(sprintf(size(src) = end vector % src is a scalar ==> replicate for all cells % % src is properly sized make sure its a column vector

%d %d is incompatible with mesh definition,m));

% --- Apply boundary conditions ap(1) = 1; ae(1) = 0; b(1) = phib(1); aw(m) = 0; ap(m) = 1; b(m) = phib(2);

% %

prescribed phi at west boundary prescribed phi at east boundary

Listing 3: The upwind1d function computes the nite volume coecients for one-dimensional, convection diusion equation using upwind dierencing for the convection term.

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 = mesh stretching ratio. If r=1, mesh is uniform % % Output: Plot of central difference and upwind difference solutions. Print % out of truncation errors if if if if nargin<1, nargin<2, nargin<3, nargin<4, m = u = gam r = 12; 5; = 0.1; 1; end end end end % mesh size

% --- Constants xlen = 1; phib = [1 0];

% %

Length of the domain boundary values

% --- Get CVFD coefficients and solve the system [x,xw,dx,delxw] = fvMesh1D(m-2,xlen,r); [aw,ap,ae,b] = central1D(u,gam,x,xw,dx,delxw,phib); phic = tridiagSolve(ap,-ae,-aw,b); [aw,ap,ae,b] = upwind1D(u,gam,x,xw,dx,delxw,phib); phiu = tridiagSolve(ap,-ae,-aw,b);

% % % % %

Create the mesh Central difference scheme solve the system of equations Upwind difference scheme solve the system of equations

% --- Compare with exact solution PeL = u*xlen/gam; pe = phib(1) + (phib(2)-phib(1))*(exp(u*x/gam) - 1)/(exp(PeL)-1); % Exact solution errc = norm(phic-pe,inf); erru = norm(phiu-pe,inf); % Maximum errors Pex = u*dx(2:end-1)/gam; % Local Pe where dx>0 Pexave = u*xlen/(m-2)/gam; % Pe based on "average" dx fprintf(PeL = %5.3f Pex_ave, max(Pex), min(Pex) = %5.3f, %5.3f, %5.3f\n,... PeL,Pexave,max(Pex),min(Pex)); fprintf(\tMax error = %11.3e for CDS scheme\n,errc); fprintf(\tMax error = %11.3e for UDS scheme\n,erru); % --- Plot results xe = linspace(0,xlen); % For smooth curve use 100 points independent of mesh size pe = phib(1) + (phib(2)-phib(1))*(exp(u*xe/gam) - 1)/(exp(PeL)-1); plot(x,phic,o--,x,phiu,*--,xe,pe,k-); xlabel(x); ylabel(\phi,Rotation,0); legend(Central,Upwind,Exact,2); text(0.1,0.5,sprintf(Pe_L = %-3.1f, Pe_x = %-3.1f,PeL,Pexave),Fontsize,14); text(0.1,0.1,sprintf(||e_c|| = %5.3f, ||e_u|| = %5.3f,errc,erru),Fontsize,14); axis([0 xlen -0.5 2]);

Listing 4: The compConvect1D function obtains solutions to one-dimensional, convection diusion equation using both central dierencing and upwind dierencing.

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) % refineConvect1D(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 = (optional, scalar) uniform velocity. Default: 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<1, mm = [8 16 32 64 128 256 512 1024]; if nargin<2, u = 5; end if nargin<3, gam = 0.1; end % --- Constants xlen = 1; phib = [1 0]; PeL = u*xlen/gam; end % Sequence of meshes

% %

Length of the domain boundary values

% --- Loop over mesh sizes for i = 1:length(mm) [x,xw,dx,delxw] = fvMesh1D(mm(i)-2,xlen); % Uniform mesh [aw,ap,ae,b] = upwind1D(u,gam,x,xw,dx,delxw,phib); % Get UDS coefficients phiu = tridiagSolve(ap,-ae,-aw,b); % and solve [aw,ap,ae,b] = central1D(u,gam,x,xw,dx,delxw,phib); % Get CDS coefficients phic = tridiagSolve(ap,-ae,-aw,b); % and solve pe = phib(1) + (phib(2)-phib(1))*(exp(u*x/gam)-1)/(exp(PeL)-1); % Exact solution erru(i) = norm(phiu-pe,inf); errc(i) = norm(phic-pe,inf); % Maximum errors end % --- Plot error versus mesh dimension, and print same data in a table Deltax = xlen./(mm-2); % CV sizes loglog(Deltax,errc,o--,Deltax,erru,*--); xlabel(\Delta x); ylabel(Max error); legend(Central,Upwind,2); fprintf(\n\nSolution for Pe_L = %f\n,PeL) fprintf(\n -- Upwind --- Cent. Diff. --\n); fprintf( Max Error Max Error\n); fprintf( m Delta x Pe_x error ratio error ratio\n); for i=1:length(errc) fprintf(%5d %9.6f %6.2f %8.5f,mm(i),Deltax(i),Deltax(i)*u/gam,erru(i)); if i>1 fprintf( %7.2f %8.5f %7.2f\n,erru(i-1)/erru(i),errc(i),errc(i-1)/errc(i)); else fprintf( %8.5f\n,errc(i)); end end

Listing 5: The refineConvect1D function demonstrates the reduction of truncation error with mesh size for numerical solutions to the one-dimensional, convection diusion equation. Numerical solutions are obtained with central dierencing and upwind dierencing.

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,dx,delxw] = fvMesh1D(...) % % Input: nx = Number of cells (internal CVs, not nodes). Default: nx=10 % xlen = overall length of the mesh. Default: xlen = 1 % r = growth ratio: dx(i+1) = r*dx(i). Default: r = 1, i.e mesh % is uniform. If r>1 control volume widths increase with x. % If r<1, control volume widths decrease with x. % % Output: x = vector of node locations, including nodes on the boundaries % xw = vector of interface locations on east of each node % xw(1) = xw(2) = 0, xw(nx+2) = xlen % dx = vector of CV widths: dx(i) = xw(i+1)-xw(i) % delxw = vector of x(i) - x(i-1); dx(1)=0 if nargin<1, if nargin<2, if nargin<3, nx = 10; xlen = 1; r = 1; end end end

% --- Create a mesh where dx(i+1) = r*dx(i) if abs(r-1)<10*eps; s = nx; % Mesh is uniform, limit as s->1 is n; else s = (1-r^nx)/(1-r); % Normal formula for stretched mesh end dx = xlen/s; % Width of smallest control volume xw = zeros(nx+2,1); for i=3:nx+2 xw(i) = xw(i-1) + dx; dx = dx*r; end

avoid 1/0

% --- Now that control volumes are defined, locate nodes in the centroids x = zeros(size(xw)); for i=2:nx+1 x(i) = (xw(i)+xw(i+1))/2; end x(nx+2) = xlen; % --- Compute vectors of control volume widths if nargout>2, dx = [ diff(xw); 0]; end % if nargout>3, delxw = [0; diff(x)]; end %

CV widths: dx(i) = xw(i+1) - xw(i) node spacing: delxw(i) = x(i) - x(i-1)

Listing 6: Utility program to create a one-dimensional nite-volume mesh with m 2 control volumes and m nodes.

You might also like