You are on page 1of 24

One Dimensional Convection:

Interpolation Models for CFD


Gerald Recktenwald
January 28, 2014

Abstract
A finite-volume model of the one-dimensional convection-diffusion equation is described and implemented in Matlab. This well-known problem
has an exact solution, which is used to compare the behavior and accuracy
of the central difference and upwind difference schemes. Matlab codes
for both schemes are developed and numerical solutions are presented on
sequences of finer meshes. As the mesh size is reduced, the dependency
of the truncation error on mesh size for both schemes is verified. The existence and cause of oscillatory solutions for the central difference scheme
are explained. The central difference scheme is shown to be superior the
upwind method contrary to older versions of conventional wisdom so
long as the mesh is suitably refined. The rich literature of alternative
convective modeling schemes is not addressed here.

Introduction

Finite volume methods are widely used in computational fluid dynamics (CFD)
codes. The elementary finite volume method uses a cell-centered mesh and
finite-difference approximations of first order derivatives. This paper shows how
the finite volume method is applied to a simple model of convective transport:
the one-dimensional convection-diffusion equation.
There are two primary goals of this paper. The first is to expose the finite
volume method. Readers interested in additional details, including application
to the Navier-Stokes equations, should consult the classic text by Patankar [4].
Ferziger and Peric [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 [5] provide a detailed discussion of the topics described in this
paper, although their presentation does not deal with the effect of non-uniform
meshes. Abbott and Basco [1] provide a basic analysis of convection modeling
for the transient version of the one-dimensional convection-diffusion equation.
Wesseling [7] gives a mathematically rigorous treatment of the finite volume
method, including a discussion of different approximations to the convective
terms.
Mechanical and Materials Engineering Department, Portland State University, Portland,
OR, 97201, gerry@pdx.edu

THE CONVECTION-DIFFUSION EQUATION


1

2
50

0.9

0.8
0.7
1

0.6

0
1

0.5
0.4
0.3

0.2
0.1
0
0

50

0.2

0.4

0.6

0.8

Figure 1: Exact solution to Equation (1) subject to the boundary conditions


(0) = 1 and (1) = 0. Parameter labels on the curves are values of P eL .
The second goal of this paper is to introduce and compare the central difference scheme and the upwind scheme for modeling the convective term in
transport equations. The one-dimensional convection-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 convection modeling schemes. Only the two schemes are considered and the analysis
only involves one model equation.

The Convection-Diffusion Equation

The one-dimensional convection-diffusion equation is




d
d
d
(u)

S =0
dx
dx
dx

(1)

The dependent variable is a scalar that is transported by the velocity u, which


is constant. The diffusion coefficient is , and S is a volumetric source term.
For S = 0 and the boundary conditions
(0) = 0

(L) = L ,

(2)

the exact solution to Equation (1) is


0
exp(ux/) 1
=
L 0
exp(PeL ) 1
where
PeL =

uL

(3)

(4)

THE FINITE VOLUME MESH

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 conditions (0) = 1 and (1) = 0. For large PeL , the distribution is nearly uniform
except for a thin layer near the x = L boundary. Negative u (velocity from
right to left) causes the profile to be shifted to the left instead of the right.
As PeL 0 the effect of convection disappears and the solution to Equation (1), subject to the boundary conditions in (2), is
x
0
=
L 0
L

(5)

When u = 0 and S = 0, Equation (1) becomes the one-dimensional Laplace


equation, which describes heat conduction through a slab with uniform conductivity. Equation (5) is the solution to the one-dimensional heat conduction
problem with fixed end temperatures.

The Finite Volume Mesh

In the finite difference method, the mesh is defined by the location of nodes in
space (and possibly time). In the finite volume method, the spatial domain of
the physical problem is subdivided into non-overlapping cells or control volumes.
A single node is located at the geometric centroid of the control volume1 . In the
finite volume method, the numerical approximation is obtained by integrating
the governing equation over the control volume. The nodal volumes are used to
compute the flux of dependent variable from one control volume into the next.
Figure 2 shows a typical control volume in a Cartesian coordinate system.
Since we are only concerned with one-dimensional diffusion problems, the nodes
above and below the control volume are not shown.
Figure 2 also introduces compass point notation. The node at xi is referred
to as point P. Relative to P, the node at xi+1 is labeled E for east, and the node
at xi1 is labeled W for west. The cell face between P and E is at xe , and the
cell face between W and P is at xw . The convention is that upper case letters
(P, E, W) refer to the location of the nodes, and the lower case letters (e, w)
refer to the cell faces.
The use of the dual notation may seem cumbersome at first. However,
compass point notation is very convenient for the derivation of the discrete
approximation to Equation (1). The use of nodal indices such as i, i + 1, and
i 1 is still important, especially when implementing the finite volume method
in a computer code.
Figure 3 depicts a group of control volumes along the x axis. The left and
right edges are the boundaries of the domain. Two nodes, i = 1 and i = m,
are on the boundaries, and do not have cell volumes2 . These nodes are used to
implement boundary conditions.
1 There are other ways to define the locations of nodes relative to the boundaries of the
control volume. Locating the node at the centroid is a popular scheme.
2 An alternative view is that the control volumes for i = 1 and i = m have zero width, and
hence zero volume.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

The Central-Difference Finite Volume Model

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


Z xe
Z xe
Z xe
d
d
d(u)
dx

dx
S dx = 0
(6)
dx
dx
xw dx
xw
xw
In the following sections, each term in this equation is evaluated and simplified
separately. The parts are then reassembled into a discrete equation relating
at node P to the values at nodes E and W.

4.1

The Diffusion Term

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



 

Z xe
d
d
d
d

(7)

dx =
dx
dx e
dx w
xw dx
The two diffusive fluxes are replaced by finite-difference approximations


d
E P

e
= De (E P )
dx e
xe


d
dx


w
w

where
De =

P W
= Dw (P W )
xw

e
xe

w
xw

(8)

xw = xP xW .

(9)

Dw =

and
xe = xE xP

3 For example, if = T (temperature) and = k (thermal conductivity), the diffusion term


is the net conduction of heat into the control volume.

xw

xe

xi-1

xi

xi+1

y
x

xw

xe

Figure 2: One-dimensional control volume.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL


xm1

x2
...
i=1

m 2

m1

...
xw,2

xe,2

xw,m1

xe,m1

Figure 3: A one-dimensional mesh used to solve Equation (1). The mesh is


depicted as uniform, but the finite volume method is not restricted to uniform
meshes.
Remember that P , E , and W are the values of at the nodes P, E, and W
in Figure 2. These are the discrete unknowns that are obtained by solution of
the finite volume model equations.
In this paper only the case of uniform is considered, so e = w = .
Nonuniform is easily handled by the finite volume method [4]. Using the
terms just defined, Equation (7) becomes


Z xe

dx De (E P ) Dw (P W )
(10)
x
xw x

4.2

The Source Term

The discrete contribution of the source term is obtained by assuming that S has
the uniform value of SP throughout the control volume. Thus,
Z xe
S dx SP xp .
(11)
xw

The distribution of SP is supplied as an input to the model. The finite volume


method also allows source terms to depend on . (See, e.g., [4]).

4.3

The Convection Term

The convective term in Equation (6) can be integrated once exactly.


Z xe
d(u)
dx = (u)e (u)w
dx
xw

(12)

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)

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

W
P

xW

xw

xP

E
xe

xE

Figure 4: Linear interpolation to obtain interface values w and e for the


central difference approximation.
where

xe xP
(14)
xE xP
Equations (13) and (14) constitute the central difference scheme for approximating the derivatives4 .
Using linear interpolation to estimate w in terms of W and P gives
e =

w = w W + (1 w )P

(15)

where

xP xw
.
(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 rearranging gives
Z xe
d(u)
dx = ue e (E P ) uw w (W P ) + ue P uw P
(17)
dx
xw
w =

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)
dx
xw

4.4

The Discrete Equation

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

(19)

4 The title comes from the finite-difference approach to modeling Equation (1). The first
order central difference approximation to the convective term at the interface (x = xe ) is

d(u)
(u)E (u)P

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

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

where
aE =

1
(De ue e )
xP

(20)

aW =

1
(Dw + uw w )
xP

(21)

aP = aE + aW
b = SP

(22)
(23)

Equation (19) applies to each internal node in the computational domain.


The system of equations for m nodes (including boundary nodes) can be
written in matrix notation as


1
aP,1
aE,1
b1
aW,2 aP,2 aE,2
2 b2

.. ..
..
..
..

. .
.
.
.

=
(24)

i bi
aW,i aP,i aE,i


. .

..
..
..

.
.
. .. ..
aW,m aP,m
m
bm
The system is tridiagonal, and is easily solved with a direct method called the
tridiagonal matrix algorithm.

4.5

Boundary Conditions

The boundary conditions in Equation (2) are enforced by modifying the coefficients in Equation (24). For nodes 2 through m 1, Equations (20) through
(23) define the coefficients in the matrix and the right hand side vector. Implementation of boundary conditions only concerns the first row and the last row
in Equation (24). Those equations are
aP,1 1 aE,1 2 = b1
aW,m m1 + aP,m m = bm
Setting aP,1 = 1, aE,1 = 0, b1 = 0 , and aW,m = 0, aP,m = 1, bm = L . gives
the trivial equations
1 = 0
m = L .
Thus, modifying the coefficients and source terms of the equations for the boundary nodes allows the values those nodes to be specified. 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.

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

4.6

Matlab Implementation

Numerical solutions to Equations (1) and (2) are obtained with the Matlab
functions central1D and demoConvect1D. The source codes for central1d and
demoConvect1D are given in Listing 1 and Listing 2 at the end of this article.
The central1D function defines a one-dimensional mesh and evaluates the
finite volume coefficients in Equation (20) through Equation (23) for given values
of u, , and S. The demoConvect1D function calls central1D or upwind1D
(described later) to obtain the finite volume coefficients. demoConvect1D then
uses the tridiagSolve function from the NMM toolbox. The finite volume
solution is then compared with the exact solution.

4.7

Measuring the Truncation Error

Since the exact solution is given by Equation (3), the truncation error of the
numerical solution can be computed. Designate the exact solution at cell i as
(xi ). The error at cell i obtained with the central difference scheme is
ec,i = c,i (xi )
where c,i is the value of at cell i obtained with the central difference scheme,
i.e.,, by the solution to Equation (24). The largest error in the domain is
max |ec,i | = ||ec ||
i

The width of the internal control volumes in the domain is x. Replacing


L with x in Equation (4) gives the mesh Peclet number.
P ex =

ux

(25)

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

4.8

Performance of the Central Difference Scheme

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

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

THE CENTRAL-DIFFERENCE FINITE VOLUME MODEL

1.5
CDS solution
exact

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

0
0

0.2

0.4

0.6

0.8

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

1.5
CDS solution
exact

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

0
0

0.2

0.4

0.6

0.8

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

UPWIND DIFFERENCING: A CURE WITH A COST

4.9

10

Consequences of Negative Coefficients

The oscillatory numerical solution shown in Figure 5 can be explained by the


existence of negative aE or aW in Equation (19). Consider the possible values
taken by aE , which is defined by Equation (20). For a uniform mesh, the
interpolation coefficient e is
e =

xP /2
xe

and Equation (20) can be rearranged as


aE =

1
1
(De ue e ) =
xP
xP

e
xP /2
ue
xe
xe


e
P ex
=
1
xP xe
2

Since e /(xP xe ) > 0 always, the magnitude of P ex determines the sign


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

Upwind Differencing: A Cure with a Cost

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

1 if uw 0
0 if uw < 0

(27)

UPWIND DIFFERENCING: A CURE WITH A COST


uw > 0

ue > 0

P
xW

xw

xP

uw < 0

xE

ue < 0

E
xe

11

xW

xw

xP

E
xe

xE

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

5.1

Matlab Implementation

The coefficients of the upwind scheme are evaluated in the upwind1D function,
in Listing 3. The substantial difference between upwind1D and central1d is
that the coefficients are computed with Equations (26) and (27) instead of
Equations (14) and (16).
The upwind difference solutions corresponding to Figure 5 and Figure 6 are
obtained with the following command line inputs
>> demoConvect1D(UDS)
>> demoConvect1D(UDS,32)

The output from the preceding commands is not shown here.


The compConvect1D function in Listing 4 evaluates both the central difference and upwind difference solutions, and plots these solutions along with the
exact solution. The following command line inputs create the plots in Figure 8.
>> compConvect1D
>> compConvect1D(32)

For both Pex = 5 and Pex = 1.7 the upwind difference scheme is free from oscillations. In addition, the upwind difference scheme produces a smaller truncation
error than the central difference scheme for both of these meshes.

UPWIND DIFFERENCING: A CURE WITH A COST

12

2
Central
Upwind
Exact
1.5

0.5

0.5
0

PeL = 50.0, Pex = 5.0

||ec|| = 1.583,

0.2

||eu|| = 0.204

0.4

0.6

0.8

0.6

0.8

x
2
Central
Upwind
Exact
1.5

0.5

0.5
0

PeL = 50.0, Pex = 1.7

||ec|| = 0.268,

0.2

||eu|| = 0.122

0.4
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.

EFFECT OF MESH REFINEMENT

13

Effect of Mesh Refinement

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

6.1

Non-uniform Meshes

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

EFFECT OF MESH REFINEMENT

14

Table 1: Variation of error with mesh spacing for upwind and central difference
schemes. Numerical solutions obtained at P eL = 50. m 2 is the number of
internal control volumes (cells) in the model.
Upwind

Central Difference

P ex

||eu ||

0.166667

8.33

0.1780

16

0.071429

3.57

0.1913

0.93

0.9534

3.58

32

0.033333

1.67

0.1225

1.56

0.2679

3.56

64

0.016129

0.81

0.0962

1.27

0.0714

3.75

128

0.007937

0.40

0.0569

1.69

0.0184

3.87

256

0.003937

0.20

0.0320

1.78

0.0047

3.93

512

0.001961

0.10

0.0169

1.89

0.0012

3.97

1024

0.000978

0.05

0.0087

1.94

0.0003

3.98

2048

0.000489

0.02

0.0044

1.97

0.00007

3.99

eu ratio

||ec ||

ec ratio

3.4154

10

Central
Upwind
0

10

Max error

10

10

10

10

10

10

10

10
x

10

10

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.

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
xi

(28)

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.
L=

n
X

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

i=1

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

Define

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

x1 =

L
.
S

(29)

(30)

To create a mesh with a geometric progression of sizes


1. Specify L, r, and n.
2. Compute x1 from Equation (30).
3. Compute the remaining cell widths with a loop.
These calculations are performed by the fvMesh function in Listing 6. The
demoStretchMesh function uses the fvMesh function to create and plot a onedimensional with r > 1 and another with r < 1.
The central1D, upwind1D, and compConvect1D functions allow specification
of the mesh stretch ratio r. For example, the following Matlab session compares the performance of the central difference and upwind difference scheme
on a mesh with 38 control volumes that decrease in size by a factor of 0.95.
>> compConvect1D(40,5,0.1,0.95)
PeL = 50.000 Pex_ave, max(Pex), min(Pex) = 1.316,
Max error = 2.222e-002 for CDS scheme
Max error = 6.476e-002 for UDS scheme

2.915,

0.437

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

SUMMARY

16

Summary

The results of computations presented in this paper support the following conclusions.
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 meshindependent solutions, the more rapidly convergent central difference scheme is
preferred.
The reader should be aware that there are many more schemes for interpolation in the convection-diffusion equation. A recent paper by Wang and
Hutter [6] compares no fewer than twelve methods. In addition to accuracy, one
must be concerned about computational cost for multidimensional problems and
numerical stability.
Ferziger and Peric caution against relying too heavily on performance comparisons 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 Peric. Computational Methods for Fluid Dynamics. Springer-Verlag, Berlin, third edition, 2001.
[3] Joe D. Hoffman. Numerical Methods for Engineers and Scientists. McGrawHill, New York, 1992.
[4] S.V. Patankar. Numerical Heat Transfer and Fluid Flow. Hemisphere, Washington D.C., 1980.
[5] H.K. Versteeg and W. Malalasekera. An Introduction to Computational Fluid
Dynamics: The Finite Volume Method. Longman, Essex, UK, 1995.
[6] Yongqi Wang and Kolumban Hutter. Comparisons of numerical methods
with respect to convectively dominanted problems. International Journal
for Numerical Methods in Fluids, 37:721745, 2001.
[7] Pieter Wesseling. Principles of Computational Fluid Dynamics. Springer,
Heidelberg, 2001.

Appendix: Code Listings


Table 2: Matlab functions used to implement and test the finite-volume approximation 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 solutions 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 finitevolume 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 convection terms.

REFERENCES

18

L
x1

x2

x3

xn

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


widths.

2
Central
Upwind
Exact
1.5

0.5

0.5
0

PeL = 50.0, Pex = 1.3

||ec|| = 0.022,

0.2

||eu|| = 0.065

0.4

0.6

0.8

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

REFERENCES

19

function [aw,ap,ae,b] = central1D(u,gam,x,xw,dx,delxw,phib,src)


% central1D Central difference coefficients for 1D advection-diffusion equation
%
% Synopsis: [aw,ap,ae,b] = central1D(u,gam,x,xw,phib)
%
[aw,ap,ae,b] = central1D(u,gam,x,xw,phib,src)
%
% Input: u
= (scalar) uniform velocity.
%
gam = (scalar) uniform diffusion coefficient
%
x
= vector of positions of cell centers. There are m-2 interior cells.
%
xw = vector of positions of west faces of cells.
%
phib = two-element vector containing boundary values. phib(1) = phi at x=0;
%
phib(2) = phi at x=xlen
%
src = (optional) source term. If no value is given, src=0 is assumed. If
%
src is a scalar (constant), it is replicated as a uniform source term.
%
Otherwise, src can be a row or column vector with m elements. The i=1
%
and i=m elements are ignored, as these correspond to boundary nodes.
%
% Output: aw,ap,ae = coefficients of 3 point central difference scheme
%
b = right hand side vector
%
x = vector of locations of cell centers
if nargin<8, src = 0;

end

Default: no source term

% --- Compute CVFD coefficients


m = length(x);
ae = zeros(m,1); aw = ae;
for i=2:m-1
be = 0.5*dx(i)/delxw(i+1);
ae(i) = (gam/delxw(i+1) - u*be)/dx(i);
bw = 0.5*dx(i)/delxw(i);
aw(i) = (gam/delxw(i)
+ u*bw)/dx(i);
end
ap = ae + aw;
% ap is a vector with same shape as ae and aw
% --- Create right hand
if numel(src) == 1
b = src*ones(m,1);
elseif numel(src) == m
b = src(:);
else
error(size(src) = %d
end

side vector
% src is a scalar ==> replicate for all cells
%
%

src is properly sized


make sure its a column vector

%d is incompatible with mesh definition,m);

% --- Apply boundary conditions


ap(1) = 1;
ae(1) = 0;
b(1) = phib(1);
aw(m) = 0;
ap(m) = 1;
b(m) = phib(2);

%
%

prescribed phi at west boundary


prescribed phi at east boundary

Listing 1: The central1d function computes the 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
if
if
if
if
if

nargin<1,
nargin<2,
nargin<3,
nargin<4,
nargin<5,

scheme=CDS;
m = 12;
u = 5;
gam = 0.1;
r = 1;

end
end
end
end
end

% --- Set constants and default input values


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

Listing 2: The demoConvect1D function obtains solutions to the one-dimensional


convection-diffusion equation using either upwind or central differencing for the
convection terms.

REFERENCES

21

function [aw,ap,ae,b] = upwind1D(u,gam,x,xw,dx,delxw,phib,src)


% upwind1D Upwind difference coefficients for 1D advection-diffusion equation
%
% Synopsis: [aw,ap,ae,b] = upwind1D(u,gam,x,xw,phib)
%
[aw,ap,ae,b] = upwind1D(u,gam,x,xw,phib,src)
%
% Input: u
= (scalar) uniform velocity.
%
gam = (scalar) uniform diffusion coefficient
%
x
= vector of positions of cell centers. There are m-2 interior cells.
%
xw = vector of positions of west faces of cells.
%
phib = two-element vector containing boundary values. phib(1) = phi at x=0;
%
phib(2) = phi at x=xlen
%
src = (optional) source term. If no value is given, src=0 is assumed. If
%
src is a scalar (constant), it is replicated as a uniform source term.
%
Otherwise, src can be a row or column vector with m elements. The i=1
%
and i=m elements are ignored, as these correspond to boundary nodes.
%
% Output: aw,ap,ae = coefficients of 3 point upwind difference scheme
%
b = right hand side vector
%
x = vector of locations of cell centers
if nargin<8, src = 0;

end

Default: no source term

% --- Compute CVFD coefficients


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

side vector
% src is a scalar ==> replicate for all cells
%
%

src is properly sized


make sure its a column vector

%d is incompatible with mesh definition,m);

% --- Apply boundary conditions


ap(1) = 1;
ae(1) = 0;
b(1) = phib(1);
aw(m) = 0;
ap(m) = 1;
b(m) = phib(2);

%
%

prescribed phi at west boundary


prescribed phi at east boundary

Listing 3: The upwind1d function computes the finite volume coefficients for
one-dimensional, convection diffusion equation using upwind differencing for
the convection term.

REFERENCES

22

function compConvect1D(m,u,gam,r)
% compConvect1D Compare CDS and UDS schemes for 1D advection-diffusion equation.
%
Plot a comparision with exact solution, and print truncation errors.
%
% Synopsis: compConvect1D
%
compConvect1D(m)
%
compConvect1D(m,u)
%
compConvect1D(m,u,gam)
%
compConvect1D(m,u,gam,r)
%
% Input: m
= total number of nodes;
Number of interior cells is m-2
%
u
= (scalar) uniform velocity.
%
gam = (scalar) uniform diffusion coefficient
%
r
= mesh stretching ratio. If r=1, mesh is uniform
%
% Output: Plot of central difference and upwind difference solutions. Print
%
out of truncation errors
if
if
if
if

nargin<1,
nargin<2,
nargin<3,
nargin<4,

m =
u =
gam
r =

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

12;
5;
= 0.1;
1;

%
%

end
end
end
end

mesh size

Length of the domain


boundary values

% --- Get CVFD coefficients and solve the system


[x,xw,dx,delxw] = fvMesh1D(m-2,xlen,r);
[aw,ap,ae,b] = central1D(u,gam,x,xw,dx,delxw,phib);
phic = tridiagSolve(ap,-ae,-aw,b);
[aw,ap,ae,b] = upwind1D(u,gam,x,xw,dx,delxw,phib);
phiu = tridiagSolve(ap,-ae,-aw,b);

%
%
%
%
%

Create the mesh


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

% --- Compare with exact solution


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

Listing 4: The compConvect1D function obtains solutions to one-dimensional,


convection diffusion equation using both central differencing and upwind differencing.

REFERENCES

23

function refineConvect1D(mm,u,gam)
% refineConvect1D Mesh refinement of solutions to 1D advection-diffusion equation.
%
Obtain CDS and UDS solutions at different mesh sizes.
%
% Synopsis: refineConvect1D
%
refineConvect1D(mm)
%
refineConvect1D(mm,u)
%
refineConvect1D(mm,u,gam)
%
% Input: mm = vector of m values. m is the number of nodes (including boundary
%
nodes) in the domain. Default: mm = [8 16 32 64 128 256 512].
%
CDS and UDS solutions are obtained for each m in mm.
%
u
= (optional, scalar) uniform velocity. Default: u = 5
%
gam = (optional, scalar) diffusion coefficient; Default: gam = 0.1
%
% Output: Table of truncation errors versus mesh size. Plot of same data.
if nargin<1, mm = [8 16 32 64 128 256 512 1024 2048];
if nargin<2, u = 5;
end
if nargin<3, gam = 0.1;
end

end

Sequence of meshes

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

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

REFERENCES

24

function [x,xw,dx,delxw] = fvMesh1D(nx,xlen,r)


% fvMesh1D Create one-dimensional finite-volume mesh. Uniform meshes and
%
stretched non-uniform meshes are supported.
%
% Synopsis: [x,xw] = fvMesh1D
%
[x,xw] = fvMesh1D(nx)
%
[x,xw] = fvMesh1D(nx,xlen)
%
[x,xw] = fvMesh1D(nx,xlen,r)
%
[x,xw,dx] = fvMesh1D(...)
%
[x,xw,dx,delxw] = fvMesh1D(...)
%
% Input: nx
= Number of cells (internal CVs, not nodes). Default: nx=10
%
xlen = overall length of the mesh. Default: xlen = 1
%
r = growth ratio: dx(i+1) = r*dx(i). Default: r = 1, i.e mesh
%
is uniform. If r>1 control volume widths increase with x.
%
If r<1, control volume widths decrease with x.
%
% Output: x = vector of node locations, including nodes on the boundaries
%
xw = vector of interface locations on east of each node
%
xw(1) = xw(2) = 0, xw(nx+2) = xlen
%
dx = vector of CV widths: dx(i) = xw(i+1)-xw(i)
%
delxw = vector of x(i) - x(i-1);
dx(1)=0
if nargin<1,
if nargin<2,
if nargin<3,

nx = 10;
xlen = 1;
r = 1;

end
end
end

% --- Create a mesh where dx(i+1) = r*dx(i)


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

avoid 1/0

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

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

Listing 6: Utility program to create a one-dimensional finite-volume mesh with


m 2 control volumes and m nodes.

You might also like