The Control-Volume Finite-Difference Approximation To The Diffusion Equation
The Control-Volume Finite-Difference Approximation To The Diffusion Equation
The Control-Volume Finite-Difference Approximation To The Diffusion Equation
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
∂ ∂φ ∂ ∂φ
Γ + Γ +S =0 (1)
∂x ∂x ∂y ∂y
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
xu(1) = 0
x(i) xu(nx+1)
xu(i)
yv(ny+1)
ny
nx ny
y(j)
y j=0 yv(1) = 0
i=0 1 2 3 nx
xw xe
∆x
δyn yn
W P E
∆y
δys
ys
δxw δxe
S
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 Z xe Z yn
∂ ∂φ φN − φP φP − φS
Γ dy dx ≈ Γn − Γs ∆x (7)
xw ys ∂y ∂y δy n δy s
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
Z xe Z yn
S dy dx ≈ SP ∆x ∆y (8)
xw ys
where Sp is the source term evaluated at node P . Adding together equations 6–8 and rearranging
yields
−aS φS − aW φW + aP φP − aE φE − aN φN = b (9)
where
Γe
aE = (10)
∆x δxe
Γw
aW = (11)
∆x δxw
Γn
aN = (12)
∆y δy n
Γs
aS = (13)
∆y δy s
aP = aE + aW + aN + aS (14)
b = SP (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 = nx ny 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 = ΓE = Γe (16)
∂x xe − ∂x xe + ∂x xe
4 NON-UNIFORM Γ 5
material 1 material 2
P E
δxe- δxe+
δxe
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 − φP φe − φP
Γe = ΓP (17)
δxe δxe−
φE − φP φE − φe
Γe = ΓE (18)
δxe δxe+
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
δxe− Γe
φe − φP = (φE − φP ) (19)
ΓP δxe
δxe+ Γe
φE − φe = (φE − φP ) (20)
ΓE δxe
Adding equations 19 and 20 gives
Γe δxe− δxe+
φE − φP = (φE − φP ) + .
δxe ΓP ΓE
Thus,
ΓE ΓP
Γe = (21)
βΓE + (1 − β)ΓP
5 IMPLEMENTATION 6
where
δxe− xe − xP
β≡ = (22)
δxe xE − xP
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.
Function Description
fvAmatrix Store the finite volume coefficients aE , aW , aN , aS , and aP
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
aP is also computed.
fvcoef Compute the neighboring finite volume coefficients (aE ,
aW , aN , and aS ) 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 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.
Figure 4 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.
Figure 4: Relationship between generic routines (core code) and the main program used to solve a
particular problem.
5 IMPLEMENTATION 8
Ly3, ny3
∆y
Ly2, ny2
Ly1, ny1
y
∆x Lx1, nx1 Lx2, nx2
x
Figure 5: 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.
A block-uniform mesh is shown in the right hand side of Figure 5. 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
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) 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.
Table 2: 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 Boundary
type Condition Post-processing in fvpost
1 Specified T Compute q 00 from discrete approximation to Fourier’s law.
Tb − Ti
q 00 = k
xb − xi
where Ti and Tb are interior and boundary temperatures,
respectively.
2 Specified q 00 Compute Tb from discrete approximation to Fourier’s law.
xb − xi
Tb = Ti + q 00
k
where Ti and Tb 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
Tb − Ti
−k = h(Tb − Tamb )
xb − xi
which can be solved for Tb to give
where δxe = xb − xi
4 Symmetry q 00 = 0. Set boundary Tb equal to adjacent interior Ti .
Table 4: 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= aS a W ap aE aN
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 ≤ Lx 0 ≤ y ≤ Ly
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
" 2 #
2
π 2π π 2π
f= + sin sin (26)
Lx Ly Lx Ly
xu(1) = 0
x(i) xu(nx+1)
xu(i)
yv(ny+1)
ny
nx ny
y(j)
y j=0 yv(1) = 0
i=0 1 2 3 nx
Model Problem 2
Model problem 2 has φ = 0 on all boundaries, Γ = 1, and a uniform source term, i.e. f (x, y) = con-
stant.
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 α 6= 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) = 0 φ(Lx , y) = 10
φ(x, 0) = 0 φ(x, Ly ) = 10
6 MODEL PROBLEMS 14
Ly y
Ly x
y
Lx
x
Lx
Figure 8: Two possible calculation domains (shaded regions) for fully-developed flow in a rectangular
duct.
The code for solving Equation (1) can be used to solve Equation (28) by making the following
definitions
dp
φ = w, Γ = µ (= constant), S=− .
dz
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.
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 = Lx and y = Ly
After the solution to the w field is obtained, the engineering quantity of interest is the product
of the Darcy friction factor f , and the Reynolds number Re, where
Dh dp ρw̄Dh
f= 1 2 − Re =
2 ρw̄ dz µ
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(Lx + Ly ). For the quarter duct, P = Lx + Ly .
Combining the expressions for f and Re yields
2Dh2
dp
f Re = −
w̄µ 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
Since e is defined wherever φex and φfd are defined it is also a (discrete) field variable. If ē is some
suitably defined average nodal error then we also expect that
ē ∼ O(h2 ) (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
L2 norm of the error at each node pP
kek2 e2i
= (31)
N N
Substitute the average error, ē for ei
pP √
kek2 ē2 N ē2 ē
∼ = =√
N N N N
For a uniform grid on a square domain N = n2x , where nx is the number of control volumes in the
x-direction (cf. Figure 1). The grid spacing is h = Lx /(nx + 1). Thus
2 3
O(h2 )
kek2 O((Lx /(nx + 1)) ) 1
∼ = ∼O (32)
N nx nx nx
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 5. The data in the first and last column of the table are
consistent with Equation (32).
REFERENCES 16
kek2 kek2
nx = ny N reduction in from
N N
the next coarser grid
5 25 1.191 × 10−2 —
1.191
10 100 1.420 × 10−3 = 8.39
0.1420
1.420
20 400 1.755 × 10−4 = 8.09
0.1755
1.755
40 1600 2.187 × 10−5 = 8.02
0.2187
2.187
80 6400 2.714 × 10−6 = 8.06
0.2714
Table 5: 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.
REFERENCES 17
function mainFullyDevRec(nx,ny,Lx,Ly)
% mainFullyDevRec Fully-developed laminar flow in a rectangular duct.
%
% Synopsis: mainFullyDevRec
% mainFullyDevRec(nx)
% mainFullyDevRec(nx,ny)
% mainFullyDevRec(nx,ny,Lx)
% mainFullyDevRec(nx,ny,Lx,Ly)
%
% Input: nx = (optional) number of control volumes in the x direction. Default: nx = 4
% ny = (optional) number of control volumes in the y direction. Default: ny = 4
% Lx = (optional) length of the duct in the x direction. Default: Lx = 1
% Ly = (optional) length of the domain in the x direction. Default: Lx = 1
% --- Define the mesh, material properties, source terms, and boundary conditions
[x,xu,y,yv,src,con,ebc,wbc,nbc,sbc] = setupFullyDevRect(nx,Lx,ny,Ly,mu,-dpdz);
% --- Solve for temperature field at internal nodes; tic/toc measures execution time
tic; w = A\b; etSolve = toc;
% --- Create xx and yy grid matrices, then make mesh, surface, and contour plots
xp = [0; x]; yp = flipud([0; y]); [xx,yy] = meshgrid(xp,yp);
W = fvpost(x,xu,y,yv,con,src,ebc,wbc,nbc,sbc,w,0); % matrix representation of w field
figure; meshc(xx,yy,W); shading(’interp’)
figure; contour(xx,yy,W); colorbar(’vert’); axis(’equal’);
figure; pcolor(xx,yy,W); colorbar(’vert’); shading(’interp’); axis(’equal’);
etPost = toc;
fprintf(’\nElapsed times:\n’);
fprintf(’\tSetup %8.3f\n\tSolve %8.3f\n\tPost-process%8.3f\n’,...
etSetup,etSolve,etPost);
Figure 9: The mainFullyDevRect function uses the finite-volume method to solve fully-developed,
laminar flow in a rectangle.
REFERENCES 18
Figure 10: The setupFullyDevRect function defines the mesh and boundary condition data struc-
tures for the finite-volume model of fully-developed, laminar flow in a rectangle.