The Control-Volume Finite-DifferenceApproximation to the Diffusion Equation Gerald W. Recktenwald Department of Mechanical Engineering Portland State University
[email protected] January 12, 2010 Abstract A two-dimensional computer code for solution of the diffusion equation (Poisson equation) is described. The code is implemented in Matlab, and is intended for educational use. The partial differential equation is converted to a system of linear equations with the finite-volume method. The system is solved by a direct method, though extending the code to use iterative methods would not be difficult. Use of the code requires the user to edit and run Matlab functions. Utility routines for prescribing a variety of boundary conditions and computing wall fluxes are provided to ease the programming tasks. The code is demonstrated with a handful of sample problems. 1 Introduction The control-volume finite-difference (CVFD) method is widely used in the numerical simulation of fluid dynamics, heat transfer and combustion. Several commercial CFD (computational fluid dy- namics) codes are based on this method. Though there is no one ultimate numerical approximation scheme, the CVFD method has numerous desirable features. It naturally maintains conservation of species when applied to conservation laws. It readily handles material discontinuities and conjugate heat transfer problems. It also has the pedagogical advantage that only simple calculus is required to derive the CVFD approximation to common conservation equations. This article presents a brief derivation of the CVFD approximation to the diffusion equation. The two dimensional diffusion equation in Cartesian coordinates is ∂ ∂x Γ ∂φ ∂x + ∂ ∂y Γ ∂φ ∂y +S = 0 (1) where φ is the scalar field, Γ is the diffusion coefficient, and S is the source term. In this form Γ is assumed to be non-uniform. We seek the solution φ(x, y) to Equation (1) in a region of space Ω, subject to appropriate conditions on the boundary, ∂Ω. The article begins with the nomenclature necessary to define a finite volume grid on a rectangular domain. Following that the general form of the CVFD approximation to the diffusion equation is obtained. Next, the harmonic mean is introduced as the correct procedure for treating discontinuities in Γ. In § 5 important details concerned with writing a CVFD program are discussed. A series of model problems suitable for testing a CVFD code problems are presented in § 6. The article ends with a discussion of truncation error, including a quantitative procedure for verifying the correctness of a CVFD code. 1 2 FINITE VOLUME MESH 2 x y 1 2 nx i = 0 2 j = 0 2 3 3 nx ny xu(1) = 0 x(i) y(j) xu(i) 1 yv(j) yv(ny+1) xu(nx+1) 1 nx+1 nx+2 nx ny yv(1) = 0 Interior node Boundary node Ambiguous corner node Figure 1: The control-volume finite-difference grid covering the calculation domain. 2 Finite Volume Mesh Figure 1 depicts a rectangular domain of length L x in the x-direction and L y in the y-direction. The domain is divided into non-overlapping control volumes by the lines that define the boundaries of the individual control volumes. The pattern created by the lines is called the computational grid or mesh. In Figure 1 the control volumes are square, but this is not required. At the center of each control volume is a node, designated by an open circle. Two sets of grid lines can be identified: the grid lines that define the control volume faces, and the grid lines (not shown) that define the locations of the nodes. In the computer implementation the grid lines that define node locations will be stored in the variables x(i) and y(j). The grid lines that define the control volume interfaces will be stored in xu(i) and yv(j). In section 3 we obtain an approximation to Equation (1) which applies to the interior nodes. Boundary values are applied at the nodes indicated by full circles. Nodes at the very corner of the domain, those marked with open squares, will not appear in the discrete approximation to Equation (1). Nodes in the domain may be identified by their (i, j) grid indices. An additional naming con- vention based on the directions on a map will be used to simplify the algebra in following sections. Figure 2 is a detailed sketch of one of the control volumes in the domain. A typical node (i, j) in Figure 1 is also referred to as node P in Figure 2. The (i + 1, j) and (i − 1, j) neighbors of P are designated E for east and W for west, respectively. The (i, j + 1) and (i, j −1) neighbors of P are referred to as N and S for north and south, respectively. Figure 2 also defines numerous geometric variables. In general the width, ∆x, of a control volume will not be equal to the distances δx e and δx w between P and its east and west neighbors. Regardless of the grid spacing P is always located 3 DISCRETE APPROXIMATION FOR INTERIOR CONTROL VOLUMES 3 δx w δx e S P W E N ∆x ∆y x e x w y n y s δy s δy n Figure 2: Geometric variables for a typical control-volume. in the geometric center of its control volume. Thus x P −x w = x e −x P = ∆x 2 (2) y P −y s = y n −y P = ∆y 2 (3) In these expressions it is crucial to distinguish between upper and lower case letters used as sub- scripts. Lower case subscripts refer to the locations of the control volume faces. Upper case sub- scripts refer to the locations of the nodes. 3 Discrete Approximation for Interior Control Volumes The control volume finite-difference method is used to transform Equation (1) to a system of discrete equations for the nodal values of φ. First, Equation (1) is integrated over the typical control volume depicted in Figure 2. This reduces the equation to one involving only first derivatives in space. Then these first derivatives are replaced with central difference approximations. Integrating the first term in Equation (1) over the finite control volume yields yn ys xe xw ∂ ∂x Γ ∂φ ∂x dxdy = yn ys ¸ Γ ∂φ ∂x e − Γ ∂φ ∂x w dy (4) ≈ ¸ Γ ∂φ ∂x e − Γ ∂φ ∂x w ∆y (5) ≈ ¸ Γ e φ E −φ P δx e −Γ w φ P −φ W δx w ∆y (6) Equation (4) follows from an exact evaluation of the inner integral with respect to x. Equation (5) is obtained by assuming that the flux terms, e.g. (Γ∂φ/∂x) e , do not vary in the y direction over a given control volume face. Equation (6) results from applying a central difference approximation to each partial derivative term. In these expressions φ E , φ W , and φ P are the nodal values of φ, 4 NON-UNIFORM Γ 4 and Γ e and Γ w are the diffusion coefficients evaluated at the interfaces of the control volumes. The procedure for evaluating Γ e and Γ w is described in section 4. Starting with the second term in Equation (1) and applying the steps used to obtain Equation (6) results in xe xw yn ys ∂ ∂y Γ ∂φ ∂y dy dx ≈ ¸ Γ n φ N −φ P δy n −Γ s φ P −φ S δy s ∆x (7) On the left side of Equation (7) the order of integration has been chosen so that the outer ∂/∂y operator is eliminated first. This is allowable if the integrand is smooth, as is the case for solutions to Equation (1). Integrating the source term in Equation (1) over the control volume gives xe xw yn ys S dy dx ≈ S P ∆x∆y (8) where S p is the source term evaluated at node P. Adding together Equations (6)–8 and rearranging yields −a S φ S −a W φ W +a P φ P −a E φ E −a N φ N = b (9) where a E = Γ e ∆xδx e (10) a W = Γ w ∆xδx w (11) a N = Γ n ∆y δy n (12) a S = Γ s ∆y δy s (13) a P = a E +a W +a N +a S (14) b = S P (15) For additional information on the transformation of Equation (1) into Equation (9) (see Patankar [3]). Equations 9 through 15 apply to each of the N = n x n y internal nodes in Figure 1. This defines a linear system of N equations in the N unknown internal values of φ. 4 Non-uniform Γ Calculation of the coefficients in equations 10–13 requires values of Γ at the interfaces of the control volumes. Rather than using an interpolation scheme, the interface values of Γ are defined by requiring continuity of the diffusive flux at the control volume interfaces. Consider the situation depicted in Figure 3 where two control volumes meet along a material discontinuity. This common situation occurs when the diffusion equation is solved in a composite material. Assume that Γ is uniform within each of the control volumes. (When the finite volume grid is defined the control volume faces are aligned with discontinuities. Thus it is not an unreasonable restriction to require that material properties are uniform within a control volume.) Continuity of flux at the interface requires Γ P ∂φ ∂x xe− = Γ E ∂φ ∂x xe+ = Γ e ∂φ ∂x xe (16) 4 NON-UNIFORM Γ 5 δx e material 1 material 2 δx e+ δx e- P E Figure 3: Discontinuous variation in Γ at the interface between two control-volumes. Equation (16) defines Γ e . In other words, the value of Γ e is chosen such that the flux, Γ∂φ/∂x, is continuous. Central difference approximations to the flux continuity conditions in Equation (16) are Γ e φ E −φ P δx e = Γ P φ e −φ P δx e− (17) Γ e φ E −φ P δx e = Γ E φ E −φ e δx e+ (18) where φ e is the value of φ at the interface. Equations 17 and 18 are discrete approximations for the diffusion flux into the left (minus) and right (plus) sides of the control volume interface in Figure 3. Equations 17 and 18 can be rearranged as φ e −φ P = δx e− Γ P Γ e δx e (φ E −φ P ) (19) φ E −φ e = δx e+ Γ E Γ e δx e (φ E −φ P ) (20) Adding equations 19 and 20 gives φ E −φ P = Γ e δx e (φ E −φ P ) ¸ δx e− Γ P + δx e+ Γ E . Cancel the factor of (φ E −φ P ) and solve for Γ e /δx e to get Γ e δx e = ¸ δx e− Γ P + δx e+ Γ E −1 = Γ E Γ P δx e− Γ E +δx e+ Γ P . Thus, Γ e = Γ E Γ P βΓ E + (1 −β)Γ P (21) 5 IMPLEMENTATION 6 where β ≡ δx e− δx e = x e −x P x E −x P (22) Equation (21) gives the interface diffusion coefficient as the harmonic mean of the diffusion coeffi- cients in adjacent control volumes. This gives the proper limiting behavior as either Γ E or Γ P goes to zero. Similar formulas for Γ w , Γ n , and Γ s may be derived, or written down by direct analogy to the formula for Γ e . These interface values for Γ are used in equations 10–13. 5 Implementation A “good” code to solve Equation (1) will have efficient data structures, a robust and efficient solution procedure, it will be flexible enough to accommodate practical problems, and it will be easy to maintain and debug. There are many possible ways to implement a code to achieve the aforementioned goals. In this section, the design of a particular set of Matlab routines for the solution of Equation (1) is described. Table 1: Core Matlab routines used to solve Equation (1). Function Description fvAmatrix Store the finite volume coefficients a E , a W , a N , a S , and a P in the sparse matrix A in Equation (24). fvave Compute the volume-weighted average of a field variable over the domain. fvbc Modify finite volume coefficients and source terms to in- clude the effect of boundary conditions. The final value of a P is also computed. fvcoef Compute the neighboring finite volume coefficients (a E , a W , a N , and a S ) for all interior control volumes. No special treatment is applied to control volumes adjacent to bound- aries. fvpost Update unknown boundary values and heat fluxes after the solution to the interior nodes is obtained. An energy bal- ance is computed and printed. The temperature field is stored in a matrix suitable for use with Matlab contour and surface plotting routines. fvUniformMesh Compute the location of cell centers and cell interfaces for a group of cells with uniform size. fvUniformMesh can be applied to either x or y direction cells, and with care it can be applied to subregions of the grid. fvUniBlockMesh Compute the location of cell centers and cell interfaces for a mesh consisting of blocks that contain uniform meshes. The overall mesh can contain blocks of unequal size and unequal numbers of control volumes. Within each block, the mesh is uniform. 5 IMPLEMENTATION 7 The code described here was designed with three primary objectives. • Make the code modular so that it can be more easily maintained. • Make the code flexible so that it can be applied to a number of different problems described by Equation (1). In particular, allow users of the code to easily select different boundary conditions, material properties, and source terms. • Make the code reasonably simple to understand. If necessary, sacrifice some efficiency or flexibility in order to make the code easier to read. The solution to a particular problem is obtained by defining problem-specific grid and boundary values, and then using a set of generic Matlab functions. The generic Matlab functions are listed in Table 1. Table 2 represents the structure of an analysis code that uses the functions from Table 1. On the left is a block depicting the sequence of steps in the main program. On the right is a block containing the names of the generic functions. Several of the steps in the main program are linked by double-ended arrows to routine names “Core Code” box. These arrows indicate a function call in the main program that sends information to, and receives information from the generic routines. 5.1 Mesh Variables The mesh is specified by four vectors: x, xu, y, and yv. As shown in Figure 1, x(i) and y(j) are the x and y positions of the node for φ i,j . The indexing of the control volume faces is arbitrary. We define xu(i) to be the location of the face to the left of x(i), and define yv(j) to be the location of the face below y(j). This convention comes from related codes that are used to model convective transport, which will not be discussed here. For many problems either a uniform or block-uniform mesh are suitable. We define a uniform mesh to have uniform control volume widths in any one coordinate direction. The widths of the control volumes in different directions need not be uniform. This situation is depicted in the left hand side of Figure 4 where ∆x = ∆y, but ∆x is same for all control volumes. For a uniform mesh, the x, xu, y, and yv vectors are computed in fvUniformMesh. A block-uniform mesh is shown in the right hand side of Figure 4. In a block-uniform mesh, the calculation domain is divided into a number of panels in the x and y direction. The intersections of these panels define rectangular blocks. The width ∆x of control volumes is uniform within an Table 2: Relationship between generic routines (core code) and the main program used to solve a particular problem. Algorithm Tasks Core Routines Define the mesh fvUniformMesh Define boundary conditions Compute finite-volume coefficients for interior cells fvcoef Adjust coefficients for boundary conditions fvbc Solve system of equations Assemble coefficient matrix fvAmatrix Solve Compute boundary values and/or fluxes fvpost Plot results 5 IMPLEMENTATION 8 Uniform Mesh Block-Uniform Mesh Lx 1 , nx 1 Lx 2 , nx 2 Ly 1 , ny 1 Ly 2 , ny 2 Ly 3 , ny 3 x y ∆x ∆y Figure 4: Uniform and block-uniform meshes. Note that the uniform mesh the widths of the control volumes in the x and y directions are not necessarily uniform. The term uniform describes the spacing of the control volumes along individual directions. x-direction panel, and adjacent panels can have different ∆x. Similarly, ∆y is uniform within a y-direction panel, but may vary from panel to panel. The fvUniBlockMesh function is used to define block-uniform meshes. For convenience, we define the auxiliary vectors dx, dy, dxw, dys dx(i) = xu(i + 1) −xu(i) dy(j) = yv(j + 1) −yv(j) dxw(i) = x(i) −x(i −1) dys(j) = y(j) −y(y −1) dx(i) and dy(j) are the x- and y-direction widths of the control volume around the node for φ i,j . These code variables correspond to ∆x and ∆y in Figure 2. dxw and dxs are the distances between adjacent nodes, which correspond to δx and δy in Figure 2. 5.2 Data Structures The fundamental decision is whether the unknowns at the center of the control volume will be stored in one-dimensional arrays (vectors) or multidimensional arrays. Storing the data in vectors has the potential for greater computational efficiency, whereas multidimensional arrays allow the most straightforward programming. The grid in Figure 1 suggests that the discrete variable, φ, could be naturally stored as a two- dimensional array, phi(i,j). If the indices of phi(i,j) run 0 ≤ i ≤ n x + 1 and 0 ≤ j ≤ n y + 1 then both the unknown interior values and the boundary conditions can be stored in the same array. The primary advantage of this data structure is that it is easy to program. Use of multidimensional arrays does not preclude efficient programs. However, vector processors are more efficient when operating on longer vectors than shorter vectors. With φ stored in a two-dimensional array the largest vector length is n x (if the code is written in Fortran) or n y (if the code is written in C). Alternatively one can store the nodal values of φ at the interior grid points in a vector phi[n], n = 1,...,N. Data on the two-dimensional grid of Figure 1 can be mapped to the one-dimensional array with the so-called natural ordering n = i + (j −1)nx Other orderings are possible (see, e.g., [1, 2]). With natural ordering the neighbors in the compass point notation of Figure 2 have these indices 5 IMPLEMENTATION 9 np = i + (j-1)*nx ne = np + 1 nw = np - 1 nn = np + nx ns = np - nx where np is the index of node (i, j), ne is the index of node (i + 1, j), etc. Whether or not the unknown values of φ are stored in a two-dimensional array corresponding to the grid indices or a vector with natural ordering, the set of unknowns can be represented symbolically as a vector. ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ φ 1,1 φ 2,1 . . . φ nx,1 φ 1,2 φ 2,2 . . . φ i,j . . . φ nx,ny ¸ ⇐⇒ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ φ 1 φ 2 . . . φ nx φ nx+1 φ nx+2 . . . φ n . . . φ N ¸ (23) If the interior values of φ are stored in a vector using natural ordering, additional storage for the boundary values must also be provided. One solution to this problem is to define four additional vectors ebcval(j), j = 1,..,ny wbcval(j), j = 1,..,ny nbcval(i), i = 1,..,nx sbcval(i), i = 1,..,nx The values stored in ebcval, etc., are the values of φ of the boundary nodes. This is sufficient for Dirichlet boundary conditions. Additional information needs to be provided if multiple types of boundary conditions (e.g., Neumann, symmetry, convective) are allowed in the code. To allow for additional boundary types, the boundary condition information is stored in an n×5 matrix for each of the four boundaries. The value of n is either nx or ny depending on the boundary: east and west boundaries are adjacent to ny control volumes, and the north and south boundaries are adjacent to nx control volumes. Table 3 documents the four types of boundary conditions that are defined for the finite volume codes listed in Table 1. The names and dimensions of the boundary condition matrices are listed in Table 4. The first letter of the matrix data structure indicates the location of the boundary. Thus, ebc contains data for the east boundary, wbc contains data for the west boundary, etc. The structure of these matrices is the same for each boundary surface. Table 5 describes the contents of each column of ebc. The contents of the other boundary matrices are analogous, except that the row index in nbc and sbc is the index of the boundary node in the x direction, e.g. sbc(3,1) is the boundary condition type for the third cell from the left on the south boundary. 5 IMPLEMENTATION 10 Table 3: Boundary condition types. The boundary type is expressed in terms of a heat conduction problem where φ = T, Γ = k, and S is a heat source term. Boundary type Boundary Condition Post-processing in fvpost 1 Specified T Compute q from discrete approximation to Fourier’s law. q = k T b −T i x b −x i where T i and T b are interior and boundary temperatures, respectively. 2 Specified q Compute T b from discrete approximation to Fourier’s law. T b = T i +q x b −x i k where T i and T b are interior and boundary temperatures, respectively. 3 Convection From specified h and T ∞ , compute boundary temperature and heat flux through the cell face on the boundary. Con- tinuity of heat flux requires −k T b −T i x b −x i = h(T b −T amb ) which can be solved for T b to give T b = hT amb + (k/δx e )T i h + (k/δx e ) where δx e = x b −x i 4 Symmetry q = 0. Set boundary T b equal to adjacent interior T i . Table 4: Matrices for storing boundary condition data. Matrix boundary size ebc east ny ×5 wbc west ny ×5 nbc north nx ×5 sbc south nx ×5 5 IMPLEMENTATION 11 Table 5: Contents of ebc data structure for the east boundary. j is the index of the boundary node in the y-direction. Column of ebc Value ebc(j,1) Integer from 1 to 4 indicating the type of boundary condition ebc(j,2) Value of T on the boundary node (if known) ebc(j,3) Value of heat flux through the cell face on the boundary (if known) ebc(j,4) Value of convection coefficient h if ebc(j, 1) = 3. ebc(j,5) Value of free-stream temperature of fluid flowing over the the bound- ary if ebc(j, 1) = 3. A = a S a p a W a E a N Figure 5: Schematic representation of the A matrix. 5.3 Solving the Linear System Equation (9) applies to each of the interior values of φ. Determining the discrete φ field requires solving the system Ax = b (24) where the unknown vector x corresponds to the interior values of φ (cf. Equation (23)), the coefficient matrix, A, consists of the coefficients of Equation (9), and the right hand side vector b, includes the source term (Equation (15)) and boundary conditions. The matrix A is sparse in the sense that there are many more zero entries than non-zero entries. The structure of the A matrix is depicted 6 MODEL PROBLEMS 12 in Figure 5. The b vector is b = ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ ¸ f 1,1 +a S 1,1 φ 1,0 +a W 1,1 φ 0,1 f 2,1 +a S 2,1 φ 2,0 . . . f nx,1 +a S nx,1 φ nx,0 +a E nx,1 φ nx+1,1 f 1,2 +a W 1,2 φ 0,2 f 2,2 . . . f i,j . . . f nx,ny +a N nx,ny φ nx,ny+1 +a E nx,ny φ nx+1,ny ¸ (25) where the superscripts refer to the compass point notation for the coefficients in equations 10–13 and the subscripts refer to the grid indices in Figure 1. In Equation (25) the φ values on the boundary are referred to in terms of their two-dimensional grid indices. Storing φ as a vector is necessary if the system of equations represented by 9 is to be solved with a direct technique, such as Gauss elimination. For iterative methods the matrix A is never explicitly assembled. Rather the entries in A are stored in arrays corresponding directly to the coefficients in Equation (9). Thus, iterative methods can use the coefficient arrays whether they are stored as vectors or multidimensional arrays. 6 Model Problems Computer codes to solve Equation (1) may be exercised by applying them to a series of model problems. These problems contain features found in more complicated engineering situations. Since the geometry and boundary conditions are relatively simple, the model problems may be solved with simple codes that are not as complex as those capable of solving practical engineering problems. These model problems are also useful for debugging more complex codes and testing different solution strategies. The model problems require solution of Equation (1) on a rectangular domain 0 ≤ x ≤ L x 0 ≤ y ≤ L y The problems are distinguished by their different boundary conditions, and by the variation of the source term and diffusivity in the domain. Model Problem 1 Model problem 1 has φ = 0 on all boundaries, Γ = 1, and the source term given by f = ¸ π L x 2 + 2π L y 2 ¸ sin πx L x sin 2πy L y (26) The exact solution is φ = sin πx L x sin 2πy L y (27) and is shown in Figure 6. 6 MODEL PROBLEMS 13 0 0.5 1 0 0.2 0.4 0.6 0.8 1 0 0.5 1 1.5 2 y x 0 0.5 1 0 0.2 0.4 0.6 0.8 1 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 y x Figure 6: Solution to model problem 1 (left) and model problem 2 (right) on a 64 ×64 mesh. Model Problem 2 Model problem 2 has φ = 0 on all boundaries, Γ = 1, and a uniform source term, i.e. f(x, y) = con- stant. The solution is shown in the right hand side of Figure 6. Model Problem 3 Model problem 3 involves solution to Equation (1) in the domain shown in Figure 7. The source term and diffusion coefficient have different values in two subregions of the domain. Outside of the central region Γ = Γ 1 = 1 S = 0 In the central region of the domain Γ = αΓ 1 S = 1000 where α > 0 is a scalar. When α = 1 there is a discontinuous diffusivity change between the two regions. Model problem 3 becomes more difficult to solve as the magnitude of α increases or decreases. The boundary conditions are φ(0, y) = 20 φ(L x , y) = 0 φ(x, 0) = 10 φ(x, L y ) = 0 Model Problem 4: Fully-Developed Flow in a Rectangular Duct Figure 8 shows two representations of the cross section of a rectangular duct. For simple fully- developed flow the governing equation for the axial velocity w is µ ¸ ∂ 2 w ∂x 2 + ∂ 2 w ∂y 2 − dp dz = 0 (28) The code for solving Equation (1) can be used to solve Equation (28) by making the following definitions φ = w, Γ = µ (= constant), S = − dp dz . 6 MODEL PROBLEMS 14 0.25L x 0.25L y 0.5L y 0.5L x L x L y Γ 1 = 1 S 1 = 0 Γ 2 = αΓ 1 S 2 = 1000 0 0.5 1 0 0.2 0.4 0.6 0.8 1 0 5 10 15 20 25 30 35 y x Figure 7: Geometry of calculation domain (left) and finite-volume solution on a 64 × 64 mesh for model problem 3. For the full duct simulation depicted on the left hand side of Figure 8, the boundary conditions are no slip conditions on all four walls. w(x, 0) = w(x, L y ) = w(0, y) = w(L x , y) = 0. (full duct) For the quarter duct simulation depicted on the right hand side of Figure 8, the boundary conditions are no slip conditions on the solid walls (x = L x and y = L y w(L y , y) = w(x, L x ) = 0 (quarter duct) and symmetry conditions on the other two planes ∂u ∂x x=0 = ∂u ∂y y=0 = 0. After the solution to the w field is obtained, the engineering quantity of interest is the product Full Duct x y L x L y Quarter Duct x y L x L y Figure 8: Two possible calculation domains (shaded regions) for fully-developed flow in a rectangular duct. 7 TRUNCATION ERRORS 15 of the Darcy friction factor f, and the Reynolds number Re, where f = D h 1 2 ρ ¯ w 2 − dp dz Re = ρ ¯ wD h µ D h is the hydraulic diameter D h = 4 ×cross section area wetted perimeter = 4A P and ¯ w is the average velocity in the cross section ¯ w = 1 A A wdA The wetted perimeter depends on whether the full duct or quarter duct is being used in the numerical model. (See Figure 8.) For the full duct, P = 2(L x +L y ). For the quarter duct, P = L x +L y . Combining the expressions for f and Re yields fRe = 2D 2 h ¯ wµ − dp dz 7 Truncation Errors The truncation error for the approximation leading to Equation (9) is O((∆x) 2 ) +O((∆y) 2 ). Con- sider the case where the solution is obtained on a square domain with a uniform grid. Then ∆x = ∆y = h If φ ex is the exact solution and φ fd is the finite- difference solution then at any point in the domain e ≡ φ ex −φ fd ∼ O(h 2 ) (29) Since e is defined wherever φ ex and φ fd are defined it is also a (discrete) field variable. If ¯ e is some suitably defined average nodal error then we also expect that ¯ e ∼ O(h 2 ) (30) With these estimates of truncation error we can use model problem 1, which has an easily computable exact solution, to check the correctness of computer codes that implement the control- volume finite-difference method. After obtaining the numerical solution we compute the normalized L 2 norm of the error at each node e 2 N = ¸ e 2 i N (31) Substitute the average error, ¯ e for e i e 2 N ∼ ¸ ¯ e 2 N = √ N¯ e 2 N = ¯ e √ N For a uniform grid on a square domain N = n 2 x , where n x is the number of control volumes in the x-direction (cf. Figure 1). The grid spacing is h = L x /(n x + 1). Thus e 2 N ∼ O(h 2 ) n x = O((L x /(n x + 1)) 2 ) n x ∼ O 1 n x 3 (32) If the code is working correctly, doubling the number of control volumes in each direction will reduce the normalized truncation error defined in Equation (31) by a factor of eight. The results of testing a correct code are given in Table 6. The data in the first and last column of the table are consistent with Equation (32). REFERENCES 16 n x = n y N e 2 N reduction in e 2 N from the next coarser grid 5 25 1.190 ×10 −2 — 10 100 1.420 ×10 −3 1.190 0.1420 = 8.39 20 400 1.755 ×10 −4 1.420 0.1755 = 8.09 40 1600 2.187 ×10 −5 1.755 0.2187 = 8.02 80 6400 2.714 ×10 −6 2.187 0.2732 = 8.06 160 25600 3.414 ×10 −7 2.732 0.3414 = 8.00 Table 6: Reduction in normalized error for the control-volume finite-difference solution to model problem 1. References [1] Jack J. Dongarra, Iain S. Duff, Danny C. Sorensen, and Henk A. van der Vorst. Solving Linear Systems on Vector and Shared Memory Computers. SIAM, Philadelphia, 1991. [2] Gene Golub and James M. Ortega. Scientific Computing: An Introduction with Parallel Com- puting. Academic Press, Inc., Boston, 1993. [3] S.V. Patankar. Numerical Heat Transfer and Fluid Flow. Hemisphere, Washington D.C., 1980. A AXISYMMETRIC GEOMETRY 17 P δx w δx e S W E N ∆x ∆r x e x w r n r s δr s δr n P S N W E Figure 9: Axisymmetric control-volume. A Axisymmetric Geometry The diffusion equation in axisymmetric coordinates is ∂ ∂x Γ ∂φ ∂x + 1 r ∂ ∂r 1 r Γ ∂φ ∂r +S = 0 (33) Integrating the first term in Equation (33) over the finite control volume yields rn rs xe xw ∂ ∂x Γ ∂φ ∂x dxrdr = rn rs ¸ Γ ∂φ ∂x e − Γ ∂φ ∂x w dy ≈ ¸ Γ ∂φ ∂x e − Γ ∂φ ∂x w r∆r ≈ ¸ Γ e φ E −φ P δx e −Γ w φ P −φ W δx w ∆r Integrating the second term in Equation (33) gives xe xw rn rs 1 r ∂ ∂r rΓ ∂φ ∂r r dr dx ≈ ¸ r n Γ n φ N −φ P δr n −r s Γ s φ P −φ S δr s ∆x Integrating the source term in Equation (33) over the control volume gives xe xw rn rs S r dr dx ≈ S P ∆xr p ∆r (34) where r p is the radial position of node P, and S p is the source term evaluated at node P. j) in Figure 1 is also referred to as node P in Figure 2. respectively. The pattern created by the lines is called the computational grid or mesh. j) grid indices. At the center of each control volume is a node. and the grid lines (not shown) that define the locations of the nodes. Nodes in the domain may be identified by their (i. j − 1) neighbors of P are referred to as N and S for north and south. The (i.2 FINITE VOLUME MESH 2 xu(1) = 0 x(i) xu(i) yv(ny+1) xu(nx+1) ny nx ny y(j) 3 nx+1 1 nx+2 2 yv(j) Interior node Boundary node 2 Ambiguous corner node nx 3 yv(1) = 0 1 y j=0 i=0 1 2 nx x Figure 1: The control-volume finite-difference grid covering the calculation domain. An additional naming convention based on the directions on a map will be used to simplify the algebra in following sections. respectively. those marked with open squares. Figure 2 is a detailed sketch of one of the control volumes in the domain. In section 3 we obtain an approximation to Equation (1) which applies to the interior nodes. Nodes at the very corner of the domain. Regardless of the grid spacing P is always located . j + 1) and (i. 2 Finite Volume Mesh Figure 1 depicts a rectangular domain of length Lx in the x-direction and Ly in the y-direction. Boundary values are applied at the nodes indicated by full circles. ∆x. but this is not required. In the computer implementation the grid lines that define node locations will be stored in the variables x(i) and y(j). The grid lines that define the control volume interfaces will be stored in xu(i) and yv(j). In general the width. j) neighbors of P are designated E for east and W for west. The domain is divided into non-overlapping control volumes by the lines that define the boundaries of the individual control volumes. of a control volume will not be equal to the distances δxe and δxw between P and its east and west neighbors. will not appear in the discrete approximation to Equation (1). The (i + 1. Figure 2 also defines numerous geometric variables. A typical node (i. Two sets of grid lines can be identified: the grid lines that define the control volume faces. designated by an open circle. In Figure 1 the control volumes are square. j) and (i − 1. Lower case subscripts refer to the locations of the control volume faces. (Γ∂φ/∂x)e . . Then these first derivatives are replaced with central difference approximations. Integrating the first term in Equation (1) over the finite control volume yields yn ys xe xw ∂ ∂x Γ ∂φ ∂x yn dx dy = ys Γ Γ ∂φ ∂x − Γ e ∂φ ∂x dy w (4) (5) (6) ≈ ≈ ∂φ ∂φ − Γ ∆y ∂x e ∂x w φE − φP φP − φW Γe − Γw ∆y δxe δxw Equation (4) follows from an exact evaluation of the inner integral with respect to x. Equation (6) results from applying a central difference approximation to each partial derivative term. Thus xP − xw yP − ys ∆x 2 ∆y = yn − yP = 2 = xe − xP = (2) (3) In these expressions it is crucial to distinguish between upper and lower case letters used as subscripts.g. and φP are the nodal values of φ. In these expressions φE . e. do not vary in the y direction over a given control volume face. 3 Discrete Approximation for Interior Control Volumes The control volume finite-difference method is used to transform Equation (1) to a system of discrete equations for the nodal values of φ. φW . Equation (5) is obtained by assuming that the flux terms. Upper case subscripts refer to the locations of the nodes. This reduces the equation to one involving only first derivatives in space. First. Equation (1) is integrated over the typical control volume depicted in Figure 2. in the geometric center of its control volume.3 DISCRETE APPROXIMATION FOR INTERIOR CONTROL VOLUMES 3 xw ∆x N xe δyn yn W P E ∆y δys δxw δxe ys S Figure 2: Geometric variables for a typical control-volume. Thus it is not an unreasonable restriction to require that material properties are uniform within a control volume. Equations 9 through 15 apply to each of the N = nx ny internal nodes in Figure 1. (When the finite volume grid is defined the control volume faces are aligned with discontinuities. Adding together Equations (6)–8 and rearranging yields −aS φS − aW φW + aP φP − aE φE − aN φN = b (9) where aE aW aN aS aP b = = = = Γe ∆x δxe Γw ∆x δxw Γn ∆y δy n Γs ∆y δy s (10) (11) (12) (13) (14) (15) = aE + aW + aN + aS = SP For additional information on the transformation of Equation (1) into Equation (9) (see Patankar [3]). Starting with the second term in Equation (1) and applying the steps used to obtain Equation (6) results in yn xe ∂ φP − φS ∂φ φN − φP − Γs ∆x (7) Γ dy dx ≈ Γn ∂y δy n δy s ys ∂y xw On the left side of Equation (7) the order of integration has been chosen so that the outer ∂/∂y operator is eliminated first. 4 Non-uniform Γ Calculation of the coefficients in equations 10–13 requires values of Γ at the interfaces of the control volumes. the interface values of Γ are defined by requiring continuity of the diffusive flux at the control volume interfaces. Integrating the source term in Equation (1) over the control volume gives xe xw yn S dy dx ≈ SP ∆x ∆y ys (8) where Sp is the source term evaluated at node P . as is the case for solutions to Equation (1). Consider the situation depicted in Figure 3 where two control volumes meet along a material discontinuity.) Continuity of flux at the interface requires ΓP ∂φ ∂x = ΓE xe − ∂φ ∂x = Γe xe + ∂φ ∂x (16) xe . Rather than using an interpolation scheme. This common situation occurs when the diffusion equation is solved in a composite material.4 NON-UNIFORM Γ 4 and Γe and Γw are the diffusion coefficients evaluated at the interfaces of the control volumes. This is allowable if the integrand is smooth. Assume that Γ is uniform within each of the control volumes. This defines a linear system of N equations in the N unknown internal values of φ. The procedure for evaluating Γe and Γw is described in section 4. Γe = ΓE ΓP βΓE + (1 − β)ΓP (21) −1 = ΓE ΓP . δxe− ΓE + δxe+ ΓP . Equations 17 and 18 are discrete approximations for the diffusion flux into the left (minus) and right (plus) sides of the control volume interface in Figure 3. Equations 17 and 18 can be rearranged as φe − φP φE − φe Adding equations 19 and 20 gives φE − φP = Γe δxe− δxe+ (φE − φP ) + .4 NON-UNIFORM Γ 5 material 1 material 2 P E δxeδxe δxe+ Figure 3: Discontinuous variation in Γ at the interface between two control-volumes. the value of Γe is chosen such that the flux. δxe ΓP ΓE = = δxe− Γe (φE − φP ) ΓP δxe δxe+ Γe (φE − φP ) ΓE δxe (19) (20) Cancel the factor of (φE − φP ) and solve for Γe /δxe to get δxe− Γe δxe+ = + δxe ΓP ΓE Thus. In other words. Equation (16) defines Γe . Γ∂φ/∂x. Central difference approximations to the flux continuity conditions in Equation (16) are φE − φP δxe φE − φP Γe δxe Γe = = φe − φP δxe− φE − φe ΓE δxe+ ΓP (17) (18) where φe is the value of φ at the interface. is continuous. aS . Compute the location of cell centers and cell interfaces for a group of cells with uniform size. and it will be easy to maintain and debug. Function fvAmatrix fvave fvbc Description Store the finite volume coefficients aE . aN . Compute the neighboring finite volume coefficients (aE . aN . In this section. and aP in the sparse matrix A in Equation (24). Table 1: Core Matlab routines used to solve Equation (1). These interface values for Γ are used in equations 10–13. aW . The overall mesh can contain blocks of unequal size and unequal numbers of control volumes. Compute the volume-weighted average of a field variable over the domain.5 IMPLEMENTATION 6 where β≡ δxe− xe − xP = δxe xE − xP (22) Equation (21) gives the interface diffusion coefficient as the harmonic mean of the diffusion coefficients in adjacent control volumes. fvUniformMesh can be applied to either x or y direction cells. An energy balance is computed and printed. or written down by direct analogy to the formula for Γe . fvcoef fvpost fvUniformMesh fvUniBlockMesh . a robust and efficient solution procedure. This gives the proper limiting behavior as either ΓE or ΓP goes to zero. No special treatment is applied to control volumes adjacent to boundaries. There are many possible ways to implement a code to achieve the aforementioned goals. Γn . the mesh is uniform. Update unknown boundary values and heat fluxes after the solution to the interior nodes is obtained. Similar formulas for Γw . aW . and Γs may be derived. and aS ) for all interior control volumes. Compute the location of cell centers and cell interfaces for a mesh consisting of blocks that contain uniform meshes. Within each block. and with care it can be applied to subregions of the grid. 5 Implementation A “good” code to solve Equation (1) will have efficient data structures. The final value of aP is also computed. Modify finite volume coefficients and source terms to include the effect of boundary conditions. The temperature field is stored in a matrix suitable for use with Matlab contour and surface plotting routines. it will be flexible enough to accommodate practical problems. the design of a particular set of Matlab routines for the solution of Equation (1) is described. The width ∆x of control volumes is uniform within an Table 2: Relationship between generic routines (core code) and the main program used to solve a particular problem.1 Mesh Variables The mesh is specified by four vectors: x. allow users of the code to easily select different boundary conditions. which will not be discussed here. and receives information from the generic routines. The solution to a particular problem is obtained by defining problem-specific grid and boundary values. x(i) and y(j) are the x and y positions of the node for φi. If necessary. As shown in Figure 1. The generic Matlab functions are listed in Table 1. The intersections of these panels define rectangular blocks. xu. • Make the code flexible so that it can be applied to a number of different problems described by Equation (1). In a block-uniform mesh. and define yv(j) to be the location of the face below y(j). This situation is depicted in the left hand side of Figure 4 where ∆x = ∆y. sacrifice some efficiency or flexibility in order to make the code easier to read. The indexing of the control volume faces is arbitrary. On the left is a block depicting the sequence of steps in the main program. y. Algorithm Tasks Define the mesh Define boundary conditions Compute finite-volume coefficients for interior cells Adjust coefficients for boundary conditions Solve system of equations Assemble coefficient matrix Solve Compute boundary values and/or fluxes Plot results Core Routines fvUniformMesh fvcoef fvbc fvAmatrix fvpost . y.5 IMPLEMENTATION 7 The code described here was designed with three primary objectives. and yv. 5. Table 2 represents the structure of an analysis code that uses the functions from Table 1. the calculation domain is divided into a number of panels in the x and y direction. • Make the code reasonably simple to understand. A block-uniform mesh is shown in the right hand side of Figure 4. and source terms.j . We define xu(i) to be the location of the face to the left of x(i). xu. the x. On the right is a block containing the names of the generic functions. We define a uniform mesh to have uniform control volume widths in any one coordinate direction. This convention comes from related codes that are used to model convective transport. These arrows indicate a function call in the main program that sends information to. In particular. For many problems either a uniform or block-uniform mesh are suitable. Several of the steps in the main program are linked by double-ended arrows to routine names “Core Code” box. For a uniform mesh. and then using a set of generic Matlab functions. • Make the code modular so that it can be more easily maintained. The widths of the control volumes in different directions need not be uniform. and yv vectors are computed in fvUniformMesh. but ∆x is same for all control volumes. material properties. However.g. dys dx(i) = xu(i + 1) − xu(i) dy(j) = yv(j + 1) − yv(j) dxw(i) = x(i) − x(i − 1) dys(j) = y(j) − y(y − 1) dx(i) and dy(j) are the x. For convenience. which correspond to δx and δy in Figure 2. dxw and dxs are the distances between adjacent nodes. The grid in Figure 1 suggests that the discrete variable.. φ. [1.. 2]).j . If the indices of phi(i. With natural ordering the neighbors in the compass point notation of Figure 2 have these indices . The fvUniBlockMesh function is used to define block-uniform meshes. Alternatively one can store the nodal values of φ at the interior grid points in a vector phi[n].j).2 Data Structures The fundamental decision is whether the unknowns at the center of the control volume will be stored in one-dimensional arrays (vectors) or multidimensional arrays. whereas multidimensional arrays allow the most straightforward programming. The term uniform describes the spacing of the control volumes along individual directions. The primary advantage of this data structure is that it is easy to program. dxw. and adjacent panels can have different ∆x. With φ stored in a two-dimensional array the largest vector length is nx (if the code is written in Fortran) or ny (if the code is written in C).. n = 1. Use of multidimensional arrays does not preclude efficient programs. These code variables correspond to ∆x and ∆y in Figure 2.N. 5. x-direction panel. we define the auxiliary vectors dx.and y-direction widths of the control volume around the node for φi.5 IMPLEMENTATION Block-Uniform Mesh Ly3. Similarly. ny1 8 Uniform Mesh Figure 4: Uniform and block-uniform meshes. but may vary from panel to panel.. e.. nx1 Lx2. ∆y is uniform within a y-direction panel. could be naturally stored as a twodimensional array. ny3 ∆y Ly2. Storing the data in vectors has the potential for greater computational efficiency. Data on the two-dimensional grid of Figure 1 can be mapped to the one-dimensional array with the so-called natural ordering n = i + (j − 1)nx Other orderings are possible (see. ny2 y ∆x x Lx1.j) run 0 ≤ i ≤ nx + 1 and 0 ≤ j ≤ ny + 1 then both the unknown interior values and the boundary conditions can be stored in the same array. nx2 Ly1. phi(i. dy. vector processors are more efficient when operating on longer vectors than shorter vectors. Note that the uniform mesh the widths of the control volumes in the x and y directions are not necessarily uniform. 5 IMPLEMENTATION 9 np ne nw nn ns = = = = = i + (j-1)*nx np + 1 np .nx The values stored in ebcval..j . etc. φnx φnx... .. are the values of φ of the boundary nodes. .ny 1.1 φnx+1 φ1. . φn φi.. Whether or not the unknown values of φ are stored in a two-dimensional array corresponding to the grid indices or a vector with natural ordering.g. wbc contains data for the west boundary.2 (23) φ2.1 φ2 φ2. To allow for additional boundary types. . Table 3 documents the four types of boundary conditions that are defined for the finite volume codes listed in Table 1. etc. Thus. The contents of the other boundary matrices are analogous.nx 1.1) is the boundary condition type for the third cell from the left on the south boundary... j j i i = = = = 1.. wbcval(j). . The value of n is either nx or ny depending on the boundary: east and west boundaries are adjacent to ny control volumes. sbc(3. .2 ⇐⇒ φnx+2 . symmetry. . Neumann. the set of unknowns can be represented symbolically as a vector. . The first letter of the matrix data structure indicates the location of the boundary. Table 5 describes the contents of each column of ebc. .ny φN If the interior values of φ are stored in a vector using natural ordering..1 . etc.. and the north and south boundaries are adjacent to nx control volumes. . convective) are allowed in the code. .nx where np is the index of node (i. additional storage for the boundary values must also be provided. except that the row index in nbc and sbc is the index of the boundary node in the x direction. φ1 φ1. .. The structure of these matrices is the same for each boundary surface..ny 1.g. j). the boundary condition information is stored in an n × 5 matrix for each of the four boundaries. j). ne is the index of node (i + 1. The names and dimensions of the boundary condition matrices are listed in Table 4. This is sufficient for Dirichlet boundary conditions. . φnx.1 np + nx np .. nbcval(i). . ebc contains data for the east boundary. One solution to this problem is to define four additional vectors ebcval(j). Additional information needs to be provided if multiple types of boundary conditions (e. e.. sbcval(i). . . Matrix ebc wbc nbc sbc boundary east west north south size ny × 5 ny × 5 nx × 5 nx × 5 . xb − xi Tb = Ti + q k where Ti and Tb are interior and boundary temperatures. Compute Tb from discrete approximation to Fourier’s law. Set boundary Tb equal to adjacent interior Ti . Tb − Ti q =k xb − xi where Ti and Tb are interior and boundary temperatures. From specified h and T∞ . respectively. Continuity of heat flux requires −k Tb − Ti = h(Tb − Tamb ) xb − xi 2 Specified q 3 Convection which can be solved for Tb to give Tb = where δxe = xb − xi 4 Symmetry q = 0.5 IMPLEMENTATION 10 Table 3: Boundary condition types. hTamb + (k/δxe )Ti h + (k/δxe ) Table 4: Matrices for storing boundary condition data. Boundary type 1 Boundary Condition Specified T Post-processing in fvpost Compute q from discrete approximation to Fourier’s law. and S is a heat source term. The boundary type is expressed in terms of a heat conduction problem where φ = T . compute boundary temperature and heat flux through the cell face on the boundary. Γ = k. respectively. Determining the discrete φ field requires solving the system Ax = b (24) where the unknown vector x corresponds to the interior values of φ (cf.4) ebc(j. Value of free-stream temperature of fluid flowing over the the boundary if ebc(j. A= aS a W ap aE aN Figure 5: Schematic representation of the A matrix. consists of the coefficients of Equation (9). 5.1) ebc(j. 1) = 3.3) ebc(j. The structure of the A matrix is depicted . and the right hand side vector b.3 Solving the Linear System Equation (9) applies to each of the interior values of φ.5 IMPLEMENTATION 11 Table 5: Contents of ebc data structure for the east boundary. includes the source term (Equation (15)) and boundary conditions.2) ebc(j. The matrix A is sparse in the sense that there are many more zero entries than non-zero entries. Column of ebc ebc(j. j is the index of the boundary node in the y-direction. A. 1) = 3.5) Value Integer from 1 to 4 indicating the type of boundary condition Value of T on the boundary node (if known) Value of heat flux through the cell face on the boundary (if known) Value of convection coefficient h if ebc(j. Equation (23)). the coefficient matrix. 1 + aS φnx. fi. and by the variation of the source term and diffusivity in the domain.6 MODEL PROBLEMS 12 in Figure 5.ny φnx. Γ = 1. .1 1. the model problems may be solved with simple codes that are not as complex as those capable of solving practical engineering problems. 6 Model Problems Computer codes to solve Equation (1) may be exercised by applying them to a series of model problems. Model Problem 1 Model problem 1 has φ = 0 on all boundaries. such as Gauss elimination. and the source term given by f= The exact solution is φ = sin and is shown in Figure 6. In Equation (25) the φ values on the boundary are referred to in terms of their two-dimensional grid indices. . Since the geometry and boundary conditions are relatively simple.1 f1.0 + aW φ0. iterative methods can use the coefficient arrays whether they are stored as vectors or multidimensional arrays. . . E fnx. The model problems require solution of Equation (1) on a rectangular domain 0 ≤ x ≤ Lx 0 ≤ y ≤ Ly The problems are distinguished by their different boundary conditions.ny (25) where the superscripts refer to the compass point notation for the coefficients in equations 10–13 and the subscripts refer to the grid indices in Figure 1. fnx.ny + aN nx. Storing φ as a vector is necessary if the system of equations represented by 9 is to be solved with a direct technique. The b vector is b= f1.2 + aW φ0. . Rather the entries in A are stored in arrays corresponding directly to the coefficients in Equation (9). For iterative methods the matrix A is never explicitly assembled.1 .ny+1 + anx.ny φnx+1.1 nx.0 + aE φnx+1.2 . These model problems are also useful for debugging more complex codes and testing different solution strategies.1 nx.1 + aS φ1.2 1.1 f2.j . Thus.1 1. π Lx 2 2 + 2π Ly sin πx Lx 2πy Ly sin 2πy Ly (26) πx Lx sin (27) . . These problems contain features found in more complicated engineering situations.1 + aS φ2.0 2.2 f2. 6 MODEL PROBLEMS 13 2 0. The solution is shown in the right hand side of Figure 6. S=− . y) = 20 φ(Lx . Ly ) = 0 Model Problem 4: Fully-Developed Flow in a Rectangular Duct Figure 8 shows two representations of the cross section of a rectangular duct.6 y 0.5 0 x 1 0. i.e.5 0.01 0 1 0. The boundary conditions are φ(0.05 1 0.6 y 0. and a uniform source term. y) = constant. 0) = 10 φ(x. Γ = µ (= constant).02 0. Γ = 1. For simple fullydeveloped flow the governing equation for the axial velocity w is µ dp ∂2w ∂2w + − =0 2 2 ∂x ∂y dz (28) The code for solving Equation (1) can be used to solve Equation (28) by making the following definitions dp φ = w.03 0. f (x.08 0.4 0. Model problem 3 becomes more difficult to solve as the magnitude of α increases or decreases. Model Problem 2 Model problem 2 has φ = 0 on all boundaries.2 0 Figure 6: Solution to model problem 1 (left) and model problem 2 (right) on a 64 × 64 mesh.8 0.5 0. y) = 0 φ(x.06 0.8 0. dz . Model Problem 3 Model problem 3 involves solution to Equation (1) in the domain shown in Figure 7. When α = 1 there is a discontinuous diffusivity change between the two regions.5 0 x 1 0.4 0.04 0. The source term and diffusion coefficient have different values in two subregions of the domain.2 0 0 1 0. Outside of the central region Γ = Γ1 = 1 S = 0 In the central region of the domain Γ = αΓ1 S = 1000 where α > 0 is a scalar.07 1. y) = 0. (full duct) For the quarter duct simulation depicted on the right hand side of Figure 8. 0) = w(x.6 y 0. the boundary conditions are no slip conditions on the solid walls (x = Lx and y = Ly w(Ly .5Lx Lx 0 1 0.6 MODEL PROBLEMS 14 35 30 25 20 Γ2 = αΓ1 Ly 0.5 0 x 1 0. the engineering quantity of interest is the product Full Duct Quarter Duct Ly y x Ly y x Lx Lx Figure 8: Two possible calculation domains (shaded regions) for fully-developed flow in a rectangular duct. Lx ) = 0 and symmetry conditions on the other two planes ∂u ∂x = x=0 (quarter duct) ∂u ∂y = 0.25Lx Figure 7: Geometry of calculation domain (left) and finite-volume solution on a 64 × 64 mesh for model problem 3. Ly ) = w(0.4 0.25Ly 0.5Ly S2 = 1000 Γ1 = 1 S1 = 0 15 10 5 0. the boundary conditions are no slip conditions on all four walls. y) = w(Lx .8 0. y) = w(x. y=0 After the solution to the w field is obtained.2 0 0. . w(x. For the full duct simulation depicted on the left hand side of Figure 8. .7 TRUNCATION ERRORS 15 of the Darcy friction factor f . After obtaining the numerical solution we compute the normalized L2 norm of the error at each node e2 e 2 i = (31) N N Substitute the average error. to check the correctness of computer codes that implement the controlvolume finite-difference method. For the quarter duct. where nx is the number of control volumes in the x x-direction (cf. (See Figure 8. doubling the number of control volumes in each direction will reduce the normalized truncation error defined in Equation (31) by a factor of eight. Combining the expressions for f and Re yields f Re = 2 2Dh wµ ¯ − dp dz 7 Truncation Errors The truncation error for the approximation leading to Equation (9) is O((∆x)2 ) + O((∆y)2 ). If e is some ¯ suitably defined average nodal error then we also expect that e ∼ O(h2 ) ¯ (30) With these estimates of truncation error we can use model problem 1. Figure 1). Thus e 2 O(h2 ) O((Lx /(nx + 1)) ) ∼ = ∼O N nx nx 2 1 nx 3 (32) If the code is working correctly. The data in the first and last column of the table are consistent with Equation (32). which has an easily computable exact solution. where f= Dh is the hydraulic diameter Dh = 4 × cross section area 4A = wetted perimeter P 1 A Dh 1 ¯2 2 ρw − dp dz Re = ρwDh ¯ µ and w is the average velocity in the cross section ¯ w= ¯ wdA A The wetted perimeter depends on whether the full duct or quarter duct is being used in the numerical model. The grid spacing is h = Lx /(nx + 1). P = Lx + Ly .) For the full duct.difference solution then at any point in the domain e ≡ φex − φfd ∼ O(h2 ) (29) Since e is defined wherever φex and φfd are defined it is also a (discrete) field variable. Consider the case where the solution is obtained on a square domain with a uniform grid. The results of testing a correct code are given in Table 6. and the Reynolds number Re. Then ∆x = ∆y = h If φex is the exact solution and φfd is the finite. P = 2(Lx + Ly ). e for ei ¯ √ e2 ¯ e 2 N e2 ¯ e ¯ ∼ = =√ N N N N For a uniform grid on a square domain N = n2 . Patankar.714 × 10−6 3. Duff.C. [2] Gene Golub and James M.732 0. Numerical Heat Transfer and Fluid Flow.187 × 10−5 2..420 0. Dongarra.06 = 8. and Henk A. References [1] Jack J.3414 = 8. Ortega. Scientific Computing: An Introduction with Parallel Computing. Inc.190 × 10−2 1.REFERENCES e 2 N 1. Philadelphia. van der Vorst.2732 2.V.39 = 8. SIAM.02 = 8. Boston. Hemisphere..09 = 8. Iain S.414 × 10−7 e 2 from reduction in N the next coarser grid — 1. Washington D.2187 2. Sorensen. 1980. Danny C.755 0. [3] S. Academic Press.00 16 nx = ny 5 10 20 40 80 160 N 25 100 400 1600 6400 25600 Table 6: Reduction in normalized error for the control-volume finite-difference solution to model problem 1. 1993.420 × 10−3 1.1755 1.755 × 10−4 2. Solving Linear Systems on Vector and Shared Memory Computers. .187 0.1420 1.190 0. 1991. A AXISYMMETRIC GEOMETRY 17 xw N ∆x N xe E P W δrn rn W P E ∆r rs δxw δxe δrs S S Figure 9: Axisymmetric control-volume. . A Axisymmetric Geometry ∂ ∂x ∂φ ∂x 1 ∂ r ∂r 1 ∂φ Γ r ∂r The diffusion equation in axisymmetric coordinates is Γ + +S =0 (33) Integrating the first term in Equation (33) over the finite control volume yields rn rs xe xw ∂ ∂x Γ ∂φ ∂x rn dx rdr = rs Γ Γ ∂φ ∂x − Γ e ∂φ ∂x dy w ≈ ≈ Integrating the second term in Equation (33) gives xe xw rn rs ∂φ ∂φ − Γ r∆r ∂x e ∂x w φ P − φW φE − φP − Γw Γe ∆r δxe δxw 1∂ r ∂r rΓ ∂φ ∂r r dr dx ≈ rn Γn φN − φP φP − φS − rs Γs ∆x δrn δrs Integrating the source term in Equation (33) over the control volume gives xe xw rn S r dr dx ≈ SP ∆x rp ∆r rs (34) where rp is the radial position of node P . and Sp is the source term evaluated at node P .