One Dimensional Convection: Interpolation Models for CFD
←
→
Page content transcription
If your browser does not render page correctly, please read the page content below
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 accuracy of 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 and finite-difference approximations of first order derivatives. This paper shows how the 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 finite 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 finite volume meth- 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 effect of non-uniform meshes. 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 volume method, including a discussion of different approximations to the convective terms. ∗ Mechanical and Materials Engineering Department, Portland State University, Portland, OR, 97201, gerry@pdx.edu
2 THE CONVECTION-DIFFUSION EQUATION 2 1 50 0.9 5 0.8 0.7 1 0.6 0 φ 0.5 −1 0.4 0.3 −5 0.2 0.1 −50 0 0 0.2 0.4 0.6 0.8 1 x Figure 1: Exact solution to Equation (1) subject to the boundary conditions φ(0) = 1 and φ(1) = 0. Parameter labels on the curves are values of P 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 in transport equations. The one-dimensional convection-diffusion equation is a compact, though somewhat non-physical, model of transport of heat, mass and other passive scalars. Applying the finite volume method to this equation allows different 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 analysis only involves one model equation. 2 The Convection-Diffusion Equation The one-dimensional convection-diffusion equation is d d dφ (uφ) − Γ −S =0 (1) dx dx dx The dependent variable φ is a scalar that is transported by the velocity u, which is constant. The diffusion coefficient is Γ, and S is a volumetric source term. For S = 0 and the boundary conditions φ(0) = φ0 φ(L) = φL , (2) the exact solution to Equation (1) is φ − φ0 exp(ux/Γ) − 1 = (3) φL − φ0 exp(PeL ) − 1 where uL PeL = (4) Γ
3 THE FINITE VOLUME MESH 3 is the Peclet number, the dimensionless parameter that describes the relative strength 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 from right 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 φ − φ0 x = (5) φL − φ0 L When u = 0 and S = 0, Equation (1) becomes the one-dimensional Laplace equation, which describes heat conduction through a slab with uniform con- ductivity. Equation (5) is the solution to the one-dimensional heat conduction problem with fixed end temperatures. 3 The Finite Volume Mesh In the finite difference method, the mesh is defined by the location of nodes in space (and possibly time). In the finite 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 . In the finite volume method, the numerical approximation is obtained by integrating the governing equation over the control volume. The nodal volumes are used to compute 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 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 labeled E for east, and the node at xi−1 is labeled 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 first. 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 finite 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, are on the boundaries, and do not have cell volumes2 . These nodes are used to implement boundary conditions. 1 There are other ways to define the locations of nodes relative to the boundaries of the control volume. Locating the node at the centroid is a popular scheme. 2 An alternative view is that the control volumes for i = 1 and i = m have zero width, and hence zero volume.
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 control volume shown in Figure 2. Z xe Z xe Z xe d(uφ) d dφ dx − Γ dx − S dx = 0 (6) xw dx xw dx dx xw In the following sections, each term in this equation is evaluated and simplified 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 Diffusion Term The second term in Equation (6) expresses the balance of transport by diffusion into the control volume3 . The integral can be evaluated exactly. Z xe d dφ dφ dφ Γ dx = Γ − Γ (7) xw dx dx dx e dx w The two diffusive fluxes are replaced by finite-difference approximations dφ φE − φP Γ ≈ Γe = De (φE − φP ) dx e δxe dφ φP − φW Γ ≈ Γw = Dw (φP − φW ) dx w δxw where Γe Γw De = Dw = (8) δxe δxw and δxe = xE − xP δxw = xP − xW . (9) 3 For example, if φ = T (temperature) and Γ = k (thermal conductivity), the diffusion term is the net conduction of heat into the control volume. δxw δxe W P E ∆y xi-1 xi xi+1 y ∆x x xw xe Figure 2: One-dimensional control volume.
4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 5 ∆x2 ∆xm−1 ... i=1 2 3 m −2 m−1 m ... δxw,2 δxe,2 δxw,m−1 δxe,m−1 Figure 3: A one-dimensional mesh used to solve Equation (1). The mesh is depicted as uniform, but the finite volume method is not restricted to uniform meshes. 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 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 the terms just defined, Equation (7) becomes Z xe ∂ ∂φ Γ dx ≈ De (φE − φP ) − Dw (φP − φW ) (10) xw ∂x ∂x 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, Z xe S dx ≈ SP ∆xp . (11) xw The distribution of SP is supplied as an input to the model. The finite 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 exactly. Z xe d(uφ) dx = (uφ)e − (uφ)w (12) xw dx To evaluate the right hand side of the preceding expression, the values of φe and φw need to be estimated. In the finite 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 (13)
4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 6 ϕW ϕP ϕE xW xw xP xe xE Figure 4: Linear interpolation to obtain interface values φw and φe for the central difference approximation. where xe − xP βe = (14) xE − xP Equations (13) and (14) constitute the central difference scheme for approxi- mating the derivatives4 . Using linear interpolation to estimate φw in terms of φW and φP gives φw = βw φW + (1 − βw )φP (15) where xP − xw βw = . (16) xP − xW 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 rear- ranging gives Z xe d(uφ) dx = ue βe (φE − φP ) − uw βw (φW − φP ) + ue φP − uw φP (17) xw dx The last two terms in the preceding equation cancel because u is a uniform parameter, i.e. ue = uw . Therefore, Equation (17) simplifies to5 Z xe d(uφ) dx = ue βe (φE − φP ) − uw βw (φW − φP ) (18) xw dx 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) 4 The 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 ) is d(uφ) (uφ)E − (uφ)P ≈ dx e xE − xP 5 Repeating this derivation for two- or three-dimensional convection models requires invoca- tion 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 fixed parameter and u = ue = uw .
4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 7 where 1 aE = (De − ue βe ) (20) ∆xP 1 aW = (Dw + uw βw ) (21) ∆xP 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 φ1 b1 −aW,2 aP,2 −aE,2 φ2 b2 .. .. .. .. .. . . . . . = (24) −aW,i aP,i −aE,i φ i bi .. .. .. . . . .. .. . . −aW,m aP,m φm bm The system is tridiagonal, and is easily solved with a direct method called the tridiagonal 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 through m − 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 row in Equation (24). Those equations are aP,1 φ1 − aE,1 φ2 = b1 −aW,m φm−1 + 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 coefficients and source terms of the equations for the bound- ary nodes allows the values those nodes to be specified. When the modified sys- tem of equations is solved, the boundary nodes are fixed, and the values of the interior nodes are consistent with the specified 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.
4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 8 4.6 Matlab Implementation Numerical solutions to Equations (1) and (2) are obtained with the Matlab functions central1D and demoConvect1D. The source codes for central1d and demoConvect1D are given in Listing 1 and Listing 2 at the end of this article. The central1D function defines a one-dimensional mesh and evaluates the finite volume coefficients 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 finite volume coefficients. demoConvect1D then uses the tridiagSolve function from the NMM toolbox. The finite 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 difference scheme is ec,i = φc,i − φ(xi ) where φc,i is the value of φ at cell i obtained with the central difference scheme, i.e.,, by the solution to Equation (24). 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. u∆x P ex = (25) Γ which describes the local strength of the convection and diffusion 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 refined, ∆x → 0 and Pex → 0, while PeL is independent of the mesh. 4.8 Performance of the Central Difference 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 finer 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 difference solution to Equation (1) will not oscillate as long as P ex < 2.
4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 9 1.5 CDS solution exact 1 φ 0.5 PeL = 50.0, Pex = 5.0 CDS scheme, Max error = 1.583 0 0 0.2 0.4 0.6 0.8 1 x Figure 5: Central difference solutions to Equation (1) for P eL = 50, P ex = 5. 1.5 CDS solution exact 1 φ 0.5 PeL = 50.0, Pex = 1.7 CDS scheme, Max error = 0.268 0 0 0.2 0.4 0.6 0.8 1 x Figure 6: Central difference solutions to Equation (1) for P eL = 50, P ex = 1.7.
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 the existence 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, the interpolation coefficient βe is ∆xP /2 βe = δxe and Equation (20) can be rearranged as 1 1 Γe ∆xP /2 aE = (De − ue βe ) = − ue ∆xP ∆xP δxe δxe Γe P ex = 1− ∆xP δxe 2 Since Γe /(∆xP δxe ) > 0 always, the magnitude of P ex determines the sign of aE . Specifically, when P ex < 2, aE > 0, and when P ex ≥ 2, aE ≤ 0. When aE < 0, the eigenvalues of the coefficient matrix in Equation (24) become complex, and the numerical solution oscillates. See, e.g. Hoffman [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 sufficiently fine 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 fine meshes took too much time. Modern computers have enough memory and floating point performance that selecting a sufficiently fine mesh to mini- mize (or eliminate) oscillations is usually not a problem. Nonetheless, a more robust formulation is still desirable for many practical problems, especially for exploratory calculations on coarse meshes. 5 Upwind Differencing: A Cure with a Cost The existence of oscillatory solutions for P ex > 2 is a nagging problem for the central difference scheme. The oscillations can be completely eliminated with a surprisingly simple modification. Unfortunately, this simple modification results in a severe loss of accuracy in the computed result. In § 4.9, it was asserted that the existence of a negative coefficients causes the oscillation in the solution. A quick fix 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 : ( 0 if ue ≥ 0 βe = (26) 1 if ue < 0 ( 1 if uw ≥ 0 βw = (27) 0 if uw < 0
5 UPWIND DIFFERENCING: A CURE WITH A COST 11 uw > 0 ue > 0 uw < 0 ue < 0 ϕW ϕW ϕP ϕE ϕP ϕE xW xw xP xe xE xW xw xP xe xE 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 not important. The sign of u 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 the linear profiles for the central difference scheme in Figure 4. In the left half of Figure 7 the velocities are positive. When ue > 0, Equa- tion (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 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 of Equations (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 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 difference scheme is free from oscil- lations. In addition, the upwind difference scheme produces a smaller truncation error than the central difference scheme for both of these meshes.
5 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 −0.5 0 0.2 0.4 0.6 0.8 1 x 2 Central Upwind Exact 1.5 1 φ 0.5 PeL = 50.0, Pex = 1.7 ||ec|| = 0.268, ||eu|| = 0.122 0 −0.5 0 0.2 0.4 0.6 0.8 1 x Figure 8: Numerical solutions to Equation (1) with the upwind difference scheme and the central difference scheme for two different mesh sizes. The cell Peclet number decreases as the number of cells increases.
6 EFFECT OF MESH REFINEMENT 13 6 Effect of Mesh Refinement 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 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 Perić [2, § 4.4] for a straightforward analysis.) Thus, as the mesh is refined, we expect the error in the central difference solution to decrease much more rapidly than the error in the upwind solution. Table 1 shows the results of mesh refinement for the upwind and central difference 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 difference scheme has a smaller error than the 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 the upwind solution. The columns in Table 1 labeled “eu ratio” and “ec ratio” confirm the theo- retical 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 eu ratio = = 1.89. 0.0169 As ∆x → 0 the values of m in subsequent rows differ 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 difference 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 difference scheme on the coarsest mesh, the improvement in accuracy as ∆x is reduced shows that the 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 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 refinement is to use smaller control volume widths (smaller node spacing) in regions where the gradient of the de-
6 EFFECT OF MESH REFINEMENT 14 Table 1: Variation of error with mesh spacing for upwind and central difference schemes. Numerical solutions obtained at P eL = 50. m − 2 is the number of internal 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 1 10 Central Upwind 0 10 −1 10 Max error −2 10 −3 10 −4 10 −5 10 −4 −3 −2 −1 0 10 10 10 10 10 ∆x Figure 9: Reduction in error as mesh is refined for upwind and central difference schemes. The plot data is from the ∆x, ||eu ||∞ , and ||ec ||∞ columns in Table 1.
6 EFFECT OF MESH REFINEMENT 15 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 for u > 0 (or PeL > 0). We expect that locally refining 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 differs by a constant factor, i.e. ∆xi+1 =r (28) ∆xi where r is a fixed constant. To create such a mesh, one specifies 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 X L= ∆xi = ∆x1 + r∆x1 + r2 ∆x1 + . . . + rn−1 ∆x1 i=1 = ∆x1 1 + r + r2 + . . . + rn−1 Define 1 − rn S = 1 + r + r2 + . . . + rn−1 = (29) 1−r where the second equality is an identity. Thus, L = ∆x1 S or L ∆x1 = . (30) S 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. The demoStretchMesh function uses the fvMesh function to create and plot a one- dimensional with r > 1 and another with r < 1. The central1D, upwind1D, and compConvect1D functions allow specification of the mesh stretch ratio r. For example, the following Matlab session com- pares the performance of the central difference and upwind difference 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, 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 average cell Pex is less than 2, but the maximum Pex is greater than two. This shows that the oscillations in the central difference solution depend on the gradient of the solution as well as the local Pex .
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 difference scheme on a uniform mesh will oscillate if P ex > 2. • Numerical solutions to Equation (1) obtained with the upwind difference scheme never oscillate for any value of P ex . • The stability provided by the upwind difference scheme is obtained with a loss of accuracy. The upwind difference scheme has a truncation error that 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 the computed solution if the cells with smaller size (closer mesh spacing) is concentrated in regions with steep gradients in the solutions The mesh refinement exercise verifies that the central difference scheme is indeed more accurate than the upwind scheme for sufficiently fine meshes. This is especially apparent from the plot of errors in Figure 9. The existence of oscillatory 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 is preferred. The reader should be aware that there are many more schemes for inter- polation in the convection-diffusion 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 com- 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 field. Despite these difficulties, 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 difference and upwind difference schemes for the one-dimensional advection-diffusion equation. Numerical results demonstrate the oscillations produced by the central difference scheme on coarse meshes, as well as the superior reduction in truncation error obtained by the central difference scheme as the mesh is refined. References [1] Michael B. Abbot and D.R. Basco. Computational Fluid Dynamics: An Introduction for Engineers. Longman, Essex, UK, 1989.
REFERENCES 17 [2] Joel H. Ferziger and Milovan Perić. 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 Fluid Dynamics: The Finite Volume Method. Longman, Essex, UK, 1995. [6] Yongqi Wang and Kolumban Hutter. Comparisons of numerical methods with respect to convectively dominanted problems. International Journal for Numerical Methods in Fluids, 37:721–745, 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 coefficients using the central difference scheme for the convection terms. compConvect1D Compare central difference and upwind difference so- lutions to the model problem. demoConvect1D Solve the model problem with central difference or upwind difference schemes. Compute and print the truncation error. demoStretchMesh Create visual representation of stretched meshes. fvMesh1D Create variables that define a one-dimensional finite- volume mesh. Uniform and stretched meshes can be created. refineConvect1D Solve the model problem with central difference or upwind difference schemes on a sequence of finer meshes. Compute and print the truncation error. upwind1D Evaluate control-volume, finite-difference coefficients using the upwind difference scheme for the convec- tion terms.
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 −0.5 0 0.2 0.4 0.6 0.8 1 x 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 replicate for all cells b = src*ones(m,1); elseif numel(src) == m % src is properly sized b = src(:); % make sure it’s a column vector else error(’size(src) = %d %d is incompatible with mesh definition’,m); end % --- Apply boundary conditions ap(1) = 1; ae(1) = 0; b(1) = phib(1); % prescribed phi at west boundary aw(m) = 0; ap(m) = 1; b(m) = phib(2); % prescribed phi at east boundary Listing 1: The central1d function computes the finite volume coefficients for one-dimensional, convection diffusion equation using central differencing 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
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 nargin0 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 vector if 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 it’s a column vector else error(’size(src) = %d %d is incompatible with mesh definition’,m); end % --- Apply boundary conditions ap(1) = 1; ae(1) = 0; b(1) = phib(1); % prescribed phi at west boundary aw(m) = 0; ap(m) = 1; b(m) = phib(2); % prescribed phi at east boundary Listing 3: The upwind1d function computes the finite volume coefficients for one-dimensional, convection diffusion equation using upwind differencing 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 nargin
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
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
You can also read