Sunteți pe pagina 1din 7

School of Mechanical Aerospace and Civil Engineering

Finite element and finite volume schemes are both based on dividing the flow domain into a
(large) number of small cells, or volumes. These can be of any shape (triangles,
quadrilaterals, etc. in 2-D; tetrahedra, prisms, cubes, etc. in 3-D). They are thus more
suitable for application to complex flow geometries.

Finite Difference Schemes


T. J. Craft
George Begg Building, C41
Finite element approaches are the standard methods used in solid mechanics. These can
be adapted to fluid problems, but finite volume schemes tend to be the more popular choice
in CFD (and are currently used in most, if not all, of the major commercial packages).
Contents:
Introduction to CFD
Numerical solution of equations
Finite difference methods
Finite volume methods
Solving sets of linear equations
Unsteady problems
Turbulence and other physical modelling
Body-fitted coordinate systems

We will examine finite volume schemes later. However, to begin with we consider how finite
difference schemes can be devised.

Reading:
J. Ferziger, M. Peric, Computational Methods for Fluid
Dynamics
H.K. Versteeg, W. Malalasekara, An Introduction to Computational Fluid Dynamics: The Finite Volume Method
S.V. Patankar, Numerical Heat Transfer and Fluid Flow
Notes: http://cfd.mace.manchester.ac.uk/tmcfd
- People - T. Craft - Online Teaching Material

Despite not being generally used in industrial codes, finite difference schemes are useful for
introducing the ideas of accuracy, truncation error, stability and boundedness in a
well-defined and fairly transparent way.

- p. 3

Introduction

Approximating Derivatives

In this lecture we turn attention to the process of discretizing the governing partial differential
equations of a fluid flow.
Discretization is the process of approximating the differential equations by a system of
algebraic ones linking the (discrete) nodal values of velocity, pressure, etc.

In developing finite difference schemes a simple equation to consider, which is related to the
more complex Navier-Stokes system, is the 1-D convection-diffusion problem:

(1)
=
x
x
x

There are three broad methods employed for discretizing the governing partial differential
equations of a fluid flow:

is the variable being transported by the flow (ie. the quantity we need to solve for), U is the
convective velocity and the diffusivity. If, for example, represents the fluid temperature,

this equation describes the 1-D problem of temperature changes due to convection by the
fluid and diffusion by molecular action.

Finite Difference (FD)


Finite Element (FE)

Finite Volume (FV)


Although there are obvious similarities in the resulting set of discretized algebraic equations,
the methods do employ different approaches to obtaining these and, as a result, there can
be differences in both the accuracy and ease of application of the various methods.
Finite difference schemes can generally be applied to
regular-shaped domains using body-fitted grids (curved grid
lines, following domain boundaries).

We store the unknown variable at a number of fixed


nodal locations (x1 , x2 , x3 , etc), and in order to transform
equation (1) into a set of algebraic equations relating the
values of at x1 , x2 , etc. we need to approximate the
various derivatives in terms of these nodal values of .

x
x1

Since many problems involve rather more complex


expressions than simply derivatives of itself, we
consider some arbitrary function f (x), and suppose we
can evaluate it at the uniformly spaced grid points x1 , x2 ,
x3 , etc. as shown.

However, large grid distortions need to be avoided, and the


schemes cannot easily be applied to very complex flow
geometry shapes.

x3

x4

f(x)

x
x1

- p. 2

x2

x2

x3

x4
- p. 4

Taylor Series Approximation

Alternatively, we could use equation (2) to evaulate f at a point xi x:

Taylor series expansions can often be used to develop and/or analyse the accuracy of
numerical approximations for derivatives.

f (xi x) = f (xi ) xf (xi ) +

f (xi + x) = f (xi ) + xf (xi ) +

2!

f (xi ) +

(x)3
3!

f (xi ) + O(x4 )

(2)

where f denotes the derivative df /dx, f the


second derivative d2 f /dx2 etc.

f (xi1 ) = f (xi ) xf (xi ) +

(8)

f (xi ) f (xi1 )
+ O(x)
x

(9)

xi

This approximation,

Rearranging gives

(x)2
(x)3
f (xi )
f (xi ) + O(x4 )
2!
3!

Rearranging leads to the approximation


f (xi ) =

f (xi ) =

(7)

Taking x as the grid spacing now leads to

We can write a Taylor series expansion to evaluate f at positions x close to xi :


(x)2

(x)2
(x)3
f (xi )
f (xi ) + O(x4 )
2!
3!

fi (fi fi1 )/x


f (xi + x) f (xi )
(x)2
x

f (xi )
f (xi ) + O(x3 )
x
2!
3!

(10)

is referred to as a backward differencing scheme.


x

f (xi + x) f (xi )
+ O(x)
x

(3)

xi-1

It is again a first order scheme.

xi

where O(x) means the leading term in the remainder is proportional to x.


- p. 5

If we take x as the uniform grid spacing, so x = xi+1 xi , then we get the approximation
f (xi ) =

f (xi+1 ) f (xi )
+ O(x)
x

(4)

which we will write in shorthand form as


fi =

fi+1 fi
+ O(x)
x

(5)

where fi represents f evaluated at xi , etc.

We could combine the two Taylor series approximations above:


(x)2
(x)3
f (xi ) +
f (xi ) + O(x4 )
2!
3!
(x)2
(x)3
f (xi )
f (xi ) + O(x4 )
f (xi1 ) = f (xi ) xf (xi ) +
2!
3!
f (xi+1 ) = f (xi ) + xf (xi ) +

(11)
(12)

Subtracting one from the other results in


f (xi+1 ) f (xi1 ) = 2xf (xi ) + 2

(x)3
f (xi ) + O(x5 )
3!

(13)

Rearranging gives the approximation

The approximation suggested by equation (5),


fi (fi+1 fi )/x

- p. 7

f (xi ) =

(6)

is known as a forward difference.

x
xi

f (xi+1 ) f (xi1 )
+ O(x2 )
2x

(14)

The approximation

x i+1

fi (fi+1 fi1 )/(2x)

This is a first order scheme, since the error is proportional to x, so decreases linearly with
grid spacing.

(15)

is referred to as a centred difference.


This is a second order approximation, since the
error reduces as (x)2 : if the grid spacing is
halved, the error goes down by a factor of 4.

- p. 6

x
xi-1

xi

x i+1
- p. 8

Order of Approximation

Polynomial Approximation

At this point it is worth considering exactly what is meant by the order of accuracy of a
discretization approximation.

Note that the above approximations for the derivatives could also have been derived by
fitting a polynomial to the function f through xi and surrounding points and then
differentiating this polynomial to get its gradient.

As we refine the grid, for any useful scheme, errors associated with the discretization
approximation can be expected to reduce.

The forward and backward difference schemes arise


from fitting a first order polynomial (a straight line)
through the points (xi , xi+1 ) and (xi1 , xi )
respectively.

We reach a grid independent solution when any further grid refinement produces no
significant difference in the computed solution. At this stage the discretization errors are
small enough that they can be neglected.
We have defined the order of a scheme in terms of the leading order error term in the Taylor
series expansion.
This should, therefore, determine at what rate the error reduces as the grid is refined.
For a first order scheme, if the grid spacing is halved the error is halved; for a second order
scheme the error would reduce by a factor of 4.

The centred difference scheme can be obtained by


fitting a quadratic curve through the points xi1 , xi
and xi+1 . The resulting polynomial approximation
for f can be writen as
f (x) =

x
xi-1

xi

x i+1

(x xi )(x xi+1 )
(x xi1 )(x xi+1 )
(x xi )(x xi1 )
fi1 +
fi +
fi+1
(xi1 xi )(xi1 xi+1 )
(xi xi1 )(xi xi+1 )
(xi+1 xi )(xi+1 xi1 )
(18)

Note that this does not mean that a second order solution on one particular grid will always
be more accurate than a first order one (although this will usually be the case for a
sufficiently fine grid).

On a uniform grid with xi+1 xi = xi xi1 = x and xi+1 xi1 = 2x this becomes

It does, however, tell us that as we refine the grid the error in the higher order scheme goes
down more rapidly, so we should expect the higher order scheme to reach a grid
independent solution on a coarser grid than would be required for a lower order scheme.

f (x) =

(x xi )(x xi+1 )
(x xi1 )(x xi+1 )
(x xi )(x xi1 )
fi1
fi +
fi+1
2(x)2
(x)2
2(x)2

- p. 9

It is also worth noting that the behaviour as described above for a particular scheme can
only be expected to be seen on a reasonably fine grid.

fi fi1
(x)2
x
+
f (xi )
f (xi ) + O(x3 )
x
2!
3!

f (x) =

If the first term in the truncation is to be the leading error term, then x has to be small
enough so that
(x)2
x
|f (xi )| <<
|f (xi )|
3!
2!

or

(x xi + x xi+1 )
(x xi1 + x xi+1 )
(x xi + x xi1 )
fi1
fi +
fi+1 (20)
2(x)2
(x)2
2(x)2

and thus

(16)


f (xi )

x << 3
f (xi )

- p. 11

Differentiating with respect to x then gives

The reason for this can be seen from the Taylor series expansion. For example, in the first
order backward difference scheme we have
fi =

(19)

f (xi ) =

x
0
x
(fi+1 fi1 )
fi1
fi +
fi+1 =
2(x)2
(x)2
2(x)2
2x

(21)

The above procedure does not immediately show us what order the truncation error is.
(17)

However, the order of the scheme can be determined by expressing fi+1 and fi1 as Taylor
series expansions about xi as before, and substituting these into the right hand side
expression of equation (21).

For example, in a turbulent near-wall boundary layer we find that the turbulent kinetic energy
dissipation rate, , behaves like A/y + where A is some constant and y + the
non-dimensional distance to the wall. Then, differentiating with respect to y + ,
A/(y + )2

2A/(y + )3

6A/(y + )4

so

| / | y + /3

The above result therefore suggests that, if we are considering the flow region y + 30
(which is broadly where this behaviour of is found), then the grid spacing should be such
that y + is significantly less than 30 (typically y + . 5), in order to see errors from this first
order scheme scale linearly with grid spacing.
- p. 10

- p. 12

Approximating Higher Order Derivatives

Non-Uniform Grids
In most CFD applications non-uniform grids are employed, allowing the grid to be more
refined in regions where strong gradients are expected.

Taylor series expansions or polynomial fits can also be used to derive approximations to
higher order derivatives. For example, to approximate the second derivative d2 f /dx2 we
could, as before, write:
(x)2

(x)3

f (xi ) +
f (xi ) + O(x4 )
2!
3!
(x)2
(x)3
f (xi )
f (xi ) + O(x4 )
f (xi1 ) = f (xi ) xf (xi ) +
2!
3!
f (xi+1 ) = f (xi ) + xf (xi ) +

(22)

In such a case, the simple forward/backward differences


are clearly still first order.

(23)

Analysis of the central difference scheme, however, is a


little different.
xi-1

xi
xi

x
xi+1

Using Taylor series expansions we can now write:

Adding the two equations and rearranging now results in


f (xi ) =

x i-1

f (xi+1 ) 2f (xi ) + f (xi1 )


+ O((x)2 )
(x)2

(24)

From this we can see that the central difference approximation for the second derivative

(xi )2
(xi )3
(29)
f (xi ) +
f (xi ) + O((xi )4 )
2!
3!
2
3
(xi1 )
(xi1 )
f (xi1 ) = f (xi ) xi1 f (xi ) +
f (xi )
f (xi ) + O((xi1 )4 )
2!
3!
f (xi+1 ) = f (xi ) + xi f (xi ) +

(30)

fi

fi+1 2fi + fi1

(x)2

(25)

Subtracting gives
f (xi+1 ) f (xi1 ) = (xi + xi1 )f (xi ) + (x2i x2i1 )f (xi ) + O(x3 )

is again second order accurate when applied on a uniform grid.


Note that when approximating higher order derivatives the computational stencil (the nodal
points used) tends to become larger, because we need more points to give information
about curvature etc.

Hence we obtain
f (xi ) =

f (xi+1 ) f (xi1 )
f (xi )
(xi xi1 ) + O(x2 )
+
xi + xi+1
2!

- p. 13

(26)

If is constant, then it can simply be taken outside the derivative, and the form above
(equation (25)) employed to approximate d2 f /dx2 .

df
dx

x1/2

fi fi1
x

(32)
- p. 15

Note that formally the central difference fi = (fi+1 fi1 )/(xi + xi1 ) is now only first
order accurate.

In many fluids related problems the second derivative that requires approximation is a
diffusion term of the form

x
x

However, in practice, one should avoid using very rapidly expanding grids, so that typically
xi = rxi1 where the grid expansion ratio r is not too far from unity. In this case the
factor (xi xi1 ) = xi (1 r) in the leading order error can still be essentially second
order.
Note that the approximation derived from the polynomial fit of equation (18) still gives a
formally second order approximation.

If is not constant, then the term is often


approximated by using central differences to
estimate df /dx at the mid-points xi+1/2 and
xi1/2 , followed by a central difference between
these values to estimate the second derivative
term:

(31)

In this case we get


f (x) =

x
xi-1

df
dx

x+1/2

x i-1/2

xi

fi+1 fi
x

i+1/2 (fi+1 fi ) i1/2 (fi fi1 )


f

x
x
(x)2

xi+1/2

x i+1

(x xi1 )(x xi+1 )


(x xi )(x xi1 )
(x xi )(x xi+1 )
fi +
fi1
fi+1
(xi1 )(xi + xi1 )
xi xi1
(xi )(xi + xi1 )
(33)

Differentiating leads to
(27)

f (xi ) =

(x xi + x xi+1 )
(x xi1 + x xi+1 )
(x xi + x xi1 )
fi1 +
fi+1
fi +
(xi1 )(xi + xi1 )
xi xi1
(xi )(xi + xi1 )
(34)

(28)

Hence we obtain the approximation

Note that interpolation may be required to estimate values of at the midpoints xi+1/2 and
xi1/2 .
- p. 14

fi

xi
xi xi1
xi1
fi1 +
fi+1
fi +
(xi1 )(xi + xi1 )
xi xi1
(xi )(xi + xi1 )

(35)
- p. 16

If we express fi+1 and fi1 as Taylor series expansions about xi the above approximation
implies

Notice that the Peclet number gives a measure of the ratio of convective effects to diffusive
ones.

xi
(xi1 )2
(xi1 )3
fi xi1 fi +
fi
fi + O(x4 )
(xi1 )(xi + xi1 )
2!
2!

As can be seen, as the Peclet number increases (diffusive effects become weaker), the
region over which most of the variation in occurs becomes thinner, and the gradients of
across it become correspondingly larger.

fi

xi xi1
fi
xi xi1

If we discretize the convection-diffusion equation using a uniform grid and central differences
for both convective and diffusive terms, we get

i+1 i1
i+1 2i + i1
U
(38)
=
2x
x2

xi1
(xi )2
(xi )3
+
fi + xi fi +
fi +
fi + O(x4 )
(xi )(xi + xi1 )
2!
2!
= fi +

xi xi1
fi + O(x3 )
3!

or, on rearranging:

Thus showing that the approximation of equation (35) is indeed second order.

i1

2x
x2

2
i + i+1
x2

2x
x2

=0

(39)

At the boundaries 1 and n are known from the boundary


conditions (1 = 0 and n = 1). Hence the discretized
x1
x2
equations at nodes 2 and n 1 can be simplified to

U
2
=0

2
3
x2
2x
x2

U
2
U

n2
+

n1
2x
x2
x2
2x
x2

xn1 x n

(40)

(41)

- p. 17

The Convection-Diffusion Problem

The discretized form of equations can thus be represented in the tri-diagonal matrix form:

The 1-D convection-diffusion equation for a transported variable is sufficient to demonstrate


some of the advantages and weaknesses that can be encountered with the numerical
approximations outlined above.
It can be written as

U
=
x
x

- p. 19

(36)

is the variable being transported by the flow, which could be temperature, chemical
species concentration, etc. U is the fluid velocity which, for simplicity, we take as constant.
The fluid density, , and diffusivity of the variable , , are also taken as constants.

b1 c1
0
B
Ba2 b2 c2
B
B0
a3 b3
B
B
B
B.
..
B ..
.
B
B
B
B. . . . . . .
0
B
B
@. . . . . . . . . . . .

0
c3

..

an3
0

....................

where ai = 2x

The term on the left hand side of the equation represents the transport effect of being
convected by the fluid. The right hand side represents the molecular diffusion of .

,
(x)2

bi =

10

1 0
2
CB
C B
. . . . . . . . . . . . . . . . . . . .C B 3 C B
CB
C B
B
B
C
0
. . . . . . . . . . . . C B .. C
C B
CB . C B
CB
C B
.. C B .. C B
..
B
C=B
.
. C
CB . C B
CB
C B
.
CB . C B
B
C B
bn3 cn3
0 C
CB . C B
CB
C B
an2 bn2 cn2 A @n2 A @

............................

an1

2
, ci
(x)2

bn1

U
2x

n1

0
0

..
.
..
.
..
.
0

(x)2

U
2x

C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
A

(42)

.
(x)2

This system of equations can be solved by a variety of methods, some of which will be
examined later.

As an example, we consider the above equation,


applied on the interval 0 x L, with boundary
conditions = 0 at x = 0 and = 1 at x = L.
The exact solution to this problem is given by
(x) =

exp(xP e/L) 1
exp(P e) 1

(37)

where P e = U L/ is the Peclet number.


- p. 18

- p. 20

In this case, rearranging equation (39) to solve for i gives

U x
i1
U x
i+1
1
+
1+
i =
2
2
2
2

i+1
P ex
i1
P ex
=
1
+
1+
2
2
2
2

At P e = 1.5 both the 3 node and 5 node central difference solutions give a good
approximation to the exact result.
At P e = 3 the coarser grid solution (with P ex = 1.5) is becoming less accurate.
(43)

where P ex is the cell Peclet number, P ex = U x/.

At P e = 10 both solutions with P ex = 2.5 and 5 give negative values of at some nodes.

For a given bulk Peclet number, U L/, as we divide the domain into a larger number of
intervals, the cell Peclet number U x/ decreases.
If we perform two sets of simulations on uniform grids,
one with 3 computational nodes, and one with 5, then
the resulting cell Peclet numbers, P ex , at different bulk
Peclet numbers, P e, are as given in the table.

3 nodes
5 nodes

x
L/2
L/4

At P e = 5 the fine grid solution (with P ex = 1.25) is still satisfactory, but with P ex = 2.5 the
estimated value of at x/L = 0.5 is now negative.

x=0

x=L

x1
x1

x2
x2

x3

P e = 0.5

P e = 1.5

Pe = 3

Pe = 5

P e = 10

0.25
0.125

0.75
0.375

1.5
0.75

2.5
1.25

5
2.5

This is an example of an unbounded scheme. If represented a species concentration, for


example, or some other physically positive quantity, then the under/overshoots would imply a
solution returning physically unrealizable results.

x3
x4

x5

It can be shown that P ex < 2 is a sufficient condition to ensure the centered difference
scheme does not produce under/overshoots in this problem.
However, this is not a necessary condition. If a non-uniform grid is used large cells with
P ex > 2 can be placed towards the left hand side of the domain (where d/dx is small)
without having an adverse effect on the solution.
Generally, the centered convection scheme will produce under/overshoots when the cell
Peclet number is large and the solution has steep gradients.
(What exactly is large, or steep, can be problem-dependent).

The values of at the nodes can be found by solving equation (43) at each node, and the
results are shown in the following graphs.
- p. 21

- p. 23

Upwinding
A similar set of solutions can be generated using the first order upwind scheme (a backward
difference) for approximating the convective terms. The discretized equation is then

i+1 2i + i1
i i1
(44)
=
U
x
x2
Rearranging leads to

U
U

2
+ i
+ i+1
=0

+
i1
2
2
2
x
x
x
x
x

(45)

and introducing the cell Peclet number, P ex , results in


i =

i1 (1 + P ex ) + i+1
2 + P ex

(46)

The results of solving these equations on the same grids as those used earlier for the central
difference scheme are also shown in the graphs.
As might be expected, once the grid is reasonably fine (P ex is not too large) the first order
upwind scheme is not as accurate as the second order central difference scheme.
However, the upwind slution is always bounded. Whereas the central difference gives
under/overshoots when the cell Peclet number is large, the upwind scheme does not.
Instead, it appears to underestimate the gradients in .
- p. 22

- p. 24

Numerical Diffusion

Suppose, for example, we have the condition d/dx = 0 at x = 0.

As seen in the convection/diffusion example, the first order upwind scheme always returns a
bounded solution. However, it is not particularly accurate.
The Taylor series expansions examined earlier show that
x
x2
i i1
= i
i +
+ O(x3 )
x
2!
3! i
i1 2i + i+1
2x2
=
i + O(x4 )
i +
x2
4!

(47)
(48)

Hence the differential equation actually solved when using the first order upwind scheme is
U

x d2
d
x2 d3

+
2
dx
2! dx
3! dx3

d2
2x2 d4
+
2
dx
4! dx4

+ O(x4 )

(49)

d
=
dx

so

1 = 2

(51)

A quadratic fit through the boundary and two neighbouring points on a uniform grid would
give

31 + 42 3
d
so
1 = (4/3)2 (1/3)3

(52)
dx 1
2x
Note the above expressions both use one-sided differences. Central differences can
sometimes be applied by adding imaginary nodes beyond the domain boundary (see
problem sheet example).
These expressions can then be used to modify the discretized equation at the
near-boundary nodes.

If we retain only leading order error terms, this can be written as


U

A first order one-sided difference would give

2 1
d

dx 1
x

P ex d2
U x d2
= 1+
+
2
2!
dx
2!
dx2

(50)

The error introduced by the upwind scheme is thus seen to be equivalent to increasing the
diffusivity. The error is thus referred to as diffusive, and generally acts to "smear" the
solution. This tends to exert a stabilizing influence on the numerical solution, although as
can be seen it has an adverse effect on accuracy.
- p. 25

- p. 27

Boundary Conditions

Extension to Multiple Dimensions


Although the finite difference methods outlined have been presented in terms of 1-D
problems, the extension to 2 or 3 dimensions is fairly straightforward.

As in the example examined earlier, the form of the difference equation to be solved often
has to be modified at the edges of the domain to account for boundary conditions.
Dirichlet boundary conditions (where the value of
the variable is fixed) can usually be implemented
by simply setting the appropriate value at the
boundary node.

(i,j+1)

If, for example, we discretize the 2-D Poisson equation


x1

x2

2
2
+
= f (x, y)
x2
y 2

xn1 x n

Terms involving this boundary node in the discretized equations at neighbouring nodes can
then be moved to the source term on the right hand side, as was done in the example
earlier. The values of to be solved for are then 2 , 3 ,. . . ,n1 .
Note that if high order schemes with computational stencils covering more than three points
are used, it may be necessary to modify these at the near-boundary nodes (to avoid
referencing non-existent nodes).
Neumann boundary conditions (where the gradient of at the boundary is prescribed) are
typically implemented by using a discretized form of the boundary condition.

(i1,j) (i,j)

(53)

on a uniform grid as shown, using central differences for


the derivatives, we obtain

(i,j1)

i,j1 2i,j + i.j+1


i1,j 2i,j + i+1.j
+
= fi,j
(x)2
(y)2

(55)

where the subscripts P , E , W , N , S denote the nodal


positions and the coefficients and source term are:
ae = aw = 1/(x)2

an = as = 1/(y)2

(54)

Note that we now have a five point stencil, with a difference


equation of the form
ap P = ae E + aw W + an N + as S + Su

- p. 26

(i+1,j)

ap = ae + aw + an + as

S
Su = fi,j

(56)
- p. 28

S-ar putea să vă placă și