Professional Documents
Culture Documents
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 is refined, the dependency of
the truncation error on mesh size for both schemes is verified. The exis-
tence and cause of oscillatory solutions of the central difference scheme on
coarse meshes 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. This paper shows how the finite volume method is applied to a simple
model of convective transport: the one-dimensional convection-diffusion equa-
tion.
There are two primary goals of this paper. The first is to expose the basic
finite volume method in one space dimension. Readers interested in additional
details, including application to the Navier-Stokes equations, should consult
the classic text by Patankar [5]. Ferziger and Perić [2] give a more up-to-date
discussion of finite volume methods, but without the low level details presented
in this paper. Versteeg and Malalasekera [6] discuss the convection models
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 [8] gives a mathematically rigorous treatment of
the finite volume method, including a discussion of different approximations to
the convective terms. Moukalled et al. [4] describe and provide implementation
details for several convection schemes. They also provide Matlab codes for
solving multidimensional flow equations.
∗ Mechanical and Materials Engineering Department, Portland State University, Portland,
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
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.
φ − φ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.
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.
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
δxw δxe
W P E
∆y
xi-1 xi xi+1
y
∆x
x xw xe
∆x2 ∆xm−1
...
i=1 2 3 m −2 m−1 m
...
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
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
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)
The system is tridiagonal, and is easily solved with a direct method called the
tridiagonal matrix algorithm.
aP,1 φ1 − aE,1 φ2 = b1
−aW,m φm−1 + aP,m φm = bm
φ1 = φ0
φm = φL .
Thus, modifying the coefficients and source terms of the equations for the bound-
ary nodes is the mechanism for enforcing the boundary conditions. When the
modified system 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
where φc,i is the value of φ at cell i obtained with the central difference scheme,
i.e., by solving Equation (24). The largest error in the domain is
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.
http://web.cecs.pdx.edu/~gerry/class/ME448/code/
4 THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL 9
1.5
CDS solution
exact
0.5
PeL = 50.0, Pex = 5.0
0
0 0.2 0.4 0.6 0.8 1
x
1.5
CDS solution
exact
0.5
PeL = 50.0, Pex = 1.7
0
0 0.2 0.4 0.6 0.8 1
x
(
1 if uw ≥ 0
βw = (27)
0 if uw < 0
5 UPWIND DIFFERENCING: A CURE WITH A COST 11
ϕP ϕE ϕP ϕE
xW xw xP xe xE xW xw xP xe xE
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
−0.5
0 0.2 0.4 0.6 0.8 1
x
2
Central
Upwind
Exact
1.5
−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
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.
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.
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 interpo-
lation in the convection-diffusion equation. A paper by Wang and Hutter [7]
compares no fewer than twelve methods. In addition to accuracy, one must be
concerned about computational cost for multidimensional problems and numer-
ical 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] F. Moukalled, L. Mangani, and M. Darwish. The Finite Volume Method in
Computational Fluid Dynamics. Springer, 2016.
[5] S.V. Patankar. Numerical Heat Transfer and Fluid Flow. Hemisphere, Wash-
ington D.C., 1980.
[6] H.K. Versteeg and W. Malalasekera. An Introduction to Computational Fluid
Dynamics: The Finite Volume Method. Longman, Essex, UK, 1995.
[7] 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.
[8] Pieter Wesseling. Principles of Computational Fluid Dynamics. Springer,
Heidelberg, 2001.
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
2
Central
Upwind
Exact
1.5
1
φ
−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
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<1, control
% volume widths decrease with x.
%
% Output: Plot exact and numerical solutions, print max error in numerical solution
% --- Create the mesh. fvMesh1D works for uniform or stretched meshes
[x,xw,dx,delxw] = fvMesh1D(m-2,xlen,r);
% --- Evaluate exact solution and maximum error in the numerical solution
PeL = u*xlen/gam;
pe = phib(1) + (phib(2)-phib(1))*(exp(u*x/gam) - 1)/(exp(PeL)-1);
maxerr = norm(phi-pe,inf);
Pex = u*dx(2:end-1)/gam; Pexave = u*xlen/(m-2)/gam;
fprintf(’PeL = %5.3f Pex_ave, max(Pex), min(Pex) = %5.3f, %5.3f, %5.3f\n’,...
PeL,Pexave,max(Pex),min(Pex));
fprintf(’\tMax error = %6.4f for %s scheme\n’,maxerr,scheme);
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
% --- Constants
xlen = 1; % Length of the domain
phib = [1 0]; % boundary values
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.
% --- Constants
xlen = 1; % Length of the domain
phib = [1 0]; % boundary values
PeL = u*xlen/gam;
erru = zeros(size(mm)); errc = erru; % Preallocate for efficiency
% --- Plot error versus mesh dimension, and print same data in a table
Deltax = xlen./(mm-2); % CV sizes
loglog(Deltax,errc,’o--’,Deltax,erru,’*--’);
xlabel(’\Delta x’); ylabel(’Max error’);
legend(’Central’,’Upwind’,’Location’,’northwest’);
% --- Now that control volumes are defined, locate nodes in the centroids
x = zeros(size(xw));
for i=2:nx+1
x(i) = (xw(i)+xw(i+1))/2;
end
x(nx+2) = xlen;