Finite Difference Schemes
Finite Difference Schemes
Introduction
TPFE MSc CFD-1 In this lecture we consider the process of discretizing the governing partial differential equations of a uid ow. Discretization is the process of approximating the differential equations by a system of algebraic ones linking the (discrete) nodal values of velocity, pressure, etc. There are three broad methods employed for discretizing the governing partial differential equations of a uid ow:
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
Although there are obvious similarities in the resulting set of discretized algebraic equations, the methods employ different approaches to obtaining these. As a result, there can be differences in both the accuracy and ease of application of the various methods.
2010/11 2 / 35
Finite difference schemes can generally be applied to regular-shaped domains using body-tted grids (curved grid lines, following domain boundaries). Large grid distortions need to be avoided, and the schemes cannot easily be applied to very complex ow geometry shapes. Finite element and nite volume schemes are both based on dividing the ow 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 ow geometries.
Finite element approaches are traditionally used in solid mechanics. These can be adapted to uid problems, but nite 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). We will examine nite volume schemes later. However, to begin with we consider how nite difference schemes can be devised. Despite not being generally used in industrial codes, nite difference schemes are useful for introducing the ideas of accuracy, truncation error, stability and boundedness in a well-dened and fairly transparent way.
2010/11
3 / 35
2010/11
4 / 35
Approximating Derivatives
Many problems involve rather more complex expressions than simply derivatives of itself. We therefore consider some arbitrary function f (x ), and suppose we can evaluate it at the uniformly spaced grid points x1 , x2 , x3 , etc. as shown.
f(x)
For illustration, a simple equation to consider, related to the more complex Navier-Stokes system, is the 1-D convection-diffusion problem:
U = x x
(1)
is the variable transported by the ow (ie. the quantity we need to solve for), U is the convective velocity and the diffusivity.
If, for example, represents uid temperature, this equation describes the 1-D problem of temperature changes due to convection by the uid and diffusion by molecular action. We store at a number of xed grid points (x1 , x2 , x3 , etc). To approximate equation (1) by 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 x2 x3 x4
x
x1 x2 x3 x4
2010/11
5 / 35
2010/11
6 / 35
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 fi +1 fi + O (x ) x (4)
Taylor series expansions can often be used to develop and/or analyse the accuracy of numerical approximations for derivatives. Writing a Taylor series expansion to evaluate f at positions x close to xi : f (xi + x ) = f (xi ) + xf (xi ) +
(x )3 (x )2 f (xi ) + f (xi ) + O (x 4) 2! 3!
f
(2)
where f denotes the derivative df /dx , f the second derivative d 2 f /dx 2 etc. Rearranging gives f (xi ) =
The approximation suggested by equation (5), fi (fi +1 fi )/x is known as a forward difference. (6)
xi
x
xi x i+1
(3)
This is a rst order scheme, since the error is proportional to x , so decreases linearly with grid spacing.
2010/11 8 / 35
Alternatively, we could use equation (2) to evaluate f at a point xi x : f (xi x ) = f (xi ) xf (xi ) + (x )2 (x )3 f (xi ) f (xi ) + O (x 4) 2! 3! (x )2 (x )3 f (xi ) f (xi ) + O (x 4) 2! 3! (7)
We could combine the two Taylor series approximations above: (x )3 (x )2 f (xi ) + f (xi ) + O (x 4) 2! 3! 2 3 ( x ) ( x ) f (xi 1 ) = f (xi ) xf (xi ) + f (xi ) f (xi ) + O (x 4) 2! 3! Subtracting one from the other results in f (xi +1 ) = f (xi ) + xf (xi ) + f (xi +1 ) f (xi 1 ) = 2xf (xi ) + 2 (x )3 f (xi ) + O (x 5) 3! (11) (12)
Taking x as the grid spacing now leads to f (xi 1 ) = f (xi ) xf (xi ) + (8)
(13)
(14)
This approximation,
fi (fi fi 1 )/x
(10)
x
xi-1 xi x i+1
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.
2010/11 10 / 35
Order of Approximation
At this point it is worth considering exactly what is meant by the order of accuracy of a discretization approximation. As we rene the grid, for any useful scheme, errors associated with the discretization approximation can be expected to reduce. We reach a grid independent solution when any further grid renement produces no signicant difference in the computed solution. At this stage the discretization errors are small enough that they can be neglected. We have dened 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 rened. For a rst 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.
2010/11 11 / 35
Note this does not mean a second order solution on one particular grid will always be more accurate than a rst order one (although this will usually be the case for a sufciently ne grid). It does, however, imply that as we rene the grid the error in the higher order scheme goes down more rapidly, so we expect the higher order scheme to reach a grid independent solution on a coarser grid than would be required for a lower order scheme. It is worth noting that the behaviour described above for a particular scheme can only be expected on a reasonably ne grid. The reason for this can be seen from the Taylor series expansion. For example, in the rst order backward difference scheme we have x (x )2 f f f (xi ) f (xi ) + O (x 3) (16) fi = i i 1 + x 2! 3! If the rst term in the truncation is to be the leading error term, then x has to be small enough so that x (x )2 |f (xi )| << |f (xi )| 3! 2! or x << 3 f (xi ) f (xi )
2010/11
(17)
12 / 35
Polynomial Approximation
On a uniform grid with xi +1 xi = xi xi 1 = x and xi +1 xi 1 = 2x this becomes (x xi 1)(x xi +1) (x xi )(x xi 1) (x xi )(x xi +1) fi 1 fi + fi +1 2(x )2 (x )2 2(x )2 (19) Differentiating with respect to x then gives f (x ) = f (x ) = (x xi 1 + x xi +1) (x xi + x xi 1 ) (x xi + x xi +1) fi 1 fi + fi +1 2(x )2 (x )2 2(x )2 (20) and thus f (xi ) = x 0 x (f f ) fi 1 fi + fi +1 = i +1 i 1 2x 2(x )2 (x )2 2(x )2 (21)
The above approximations for the derivatives could also have been derived by tting a polynomial to the function f through xi and surrounding points and then differentiating this polynomial to get its gradient.
The forward and backward difference schemes arise from tting a rst order polynomial (a straight line) through the points (xi , xi +1 ) and (xi 1 , xi ) respectively. The centred difference scheme can be obtained by tting a quadratic curve through the points xi 1 , xi and xi +1 . The resulting polynomial approximation for f can be written as
f
x
xi-1 xi x i+1
The above procedure does not immediately show us what order the truncation error is. However, the order of the scheme can be determined by expressing fi +1 and fi 1 as Taylor series expansions about xi as before, and substituting these into the right hand side expression of equation (21).
2010/11 14 / 35
f (x ) =
(x xi 1)(x xi +1)fi (x xi )(x xi 1 )fi +1 (x xi )(x xi +1 )fi 1 + + (xi 1 xi )(xi 1 xi +1) (xi xi 1)(xi xi +1) (xi +1 xi )(xi +1 xi 1 ) (18)
2010/11 13 / 35
Note the computational stencil (the nodal points used) is now larger, because we need more points to give information about curvature etc. In many uids related problems the second derivative that requires approximation is a diffusion term of the form
Taylor series expansions or polynomial ts can also be used to derive approximations to higher order derivatives. For example, to approximate the second derivative d f /dx we could, as before, write: (x )3 (x )2 f (xi ) + f (xi ) + O (x 4) f (xi +1 ) = f (xi ) + xf (xi ) + 2! 3! (x )2 (x )3 f (xi 1 ) = f (xi ) xf (xi ) + f (xi ) f (xi ) + O (x 4) 2! 3! Adding the two equations and rearranging now results in
2 2
f x
f
(26)
(22) (23)
This can be approximated using central differences to estimate df /dx at the mid-points xi +1/2 and xi 1/2 , followed by a central difference between these values to estimate the second derivative term: df dx f f i i 1 x i 1/2 df dx
x
xi-1 x i-1/2 xi xi+1/2 x i+1
(24)
f f i +1 i x i +1/2
(27) (28)
This gives the central difference approximation for the second derivative fi fi +1 2fi + fi 1 (x )2
2010/11
(25)
f x
Note that interpolation may be required to estimate values of at the midpoints xi +1/2 and xi 1/2 .
2010/11 16 / 35
Non-Uniform Grids
In most CFD applications non-uniform grids are employed, allowing the grid to be more rened in regions where strong gradients are expected. In such a case, the simple forward/backward differences are clearly still rst order. Analysis of the central difference scheme, however, is a little different. Using Taylor series expansions we can now write:
f
(32)
Note that formally the central difference fi = (fi +1 fi 1 )/(xi + xi 1) is now only rst order accurate. In practice, one should avoid using rapidly expanding grids, so typically xi = r xi 1 with grid expansion ratio r not too far from unity. The factor (xi xi 1 ) = xi (1 r ) in the leading order error can then still be essentially second order. The approximation derived from the polynomial t of equation (18) still gives a formally second order approximation. In this case we get (x xi )(x xi +1)fi 1 (x xi 1)(x xi +1)fi (x xi )(x xi 1)fi +1 + (xi 1 )(xi + xi 1) x i x i 1 (xi )(xi + xi 1 ) (33) Differentiating leads to f (x ) = (x xi + x xi +1 )fi 1 (x xi 1 + x xi +1 )fi (x xi + x xi 1)fi +1 + + (xi 1 )(xi + xi 1 ) x i x i 1 (xi )(xi + xi 1) (34)
2010/11 18 / 35
xi-1
x i-1
xi xi xi+1
(xi )3 (xi )2 f (xi ) + f (xi ) + O ((xi )4 ) (29) f (xi +1 ) = f (xi ) + xi f (xi ) + 2! 3! 2 ( x ) (xi 1 )3 i 1 f (xi 1 ) = f (xi ) xi 1f (xi ) + f (xi ) f (xi ) + O ((xi 1)4 ) 2! 3! (30) Subtracting gives
3 f (xi +1 ) f (xi 1 ) = (xi + xi 1 )f (xi ) + (xi2 xi2 1 )f (xi ) + O (x ) (31)
Finite Difference Schemes 2010/11 17 / 35
f (xi ) =
Finite Difference Schemes
If we express fi +1 and fi 1 as Taylor series expansions about xi the above approximation implies
The 1-D convection-diffusion equation for a transported variable is sufcient to demonstrate some of the advantages and weaknesses that can be encountered with the numerical approximations outlined above. It can be written as
fi
U = x x
(36)
is the variable being transported by the ow, which could be temperature, chemical species concentration, etc. U is the uid velocity which, for simplicity, we take as constant.
The uid density, , and , the diffusivity of , are also taken as constants. The left hand side of the equation represents the transport effect of being convected by the uid. The right hand side represents the molecular diffusion of .
2010/11 20 / 35
= fi +
xi xi 1 fi + O (x 3) 3!
Thus showing that the approximation of equation (35) is indeed second order.
Finite Difference Schemes 2010/11 19 / 35
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
Discretizing this convection-diffusion equation on a uniform grid using central differences for both convective and diffusive terms, we get
U
or, on rearranging:
i +1 i 1 2x
i +1 2i + i 1 x 2
U 2x x 2
(38)
(x ) =
exp(xPe/L) 1 exp(Pe) 1
(37)
i 1
U 2x x 2
2 i + i +1 x 2
=0
(39)
Notice that the Peclet number gives a measure of the ratio of convective effects to diffusive ones. 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.
1 and n are known from the boundary conditions (1 = 0 and n = 1). Hence the discretized equations at nodes 2 and n 1 can be simplied to
2 2 + 3 x 2
x1
x2
xn1 x n
U 2x x 2
=0
(40) (41)
2010/11 22 / 35
n2
Finite Difference Schemes 2010/11 21 / 35 Finite Difference Schemes
U 2x x 2
2 U + 2 n1 = 2x x 2 x
The discretized form of equations can thus be represented in the tri-diagonal matrix form: 0 b1 c1 0 . . . . . . . . . . . . . . . . . . . . . . . . . 2 0 0 . . . . . . . . . . . . . . . . . . a2 b2 c2 3 . . . c3 0 . . . . . . . . . . . . 0 a3 b3 . . . . . . .. .. .. . . . = . . . . . . . . . . . . . 0 . . . . . . . 0 an3 bn3 cn3 . 0 . . . . . . . . . . . . 0 an2 bn2 cn2 n2 U n1 2 .................. 0 an1 bn1 2x ( x ) (42) U U 2 where ai = 2 x (x )2 , bi = (x )2 , ci = 2x (x )2 . This system of equations can be solved by a variety of methods, some of which will be examined later.
i =
=
i +1 U x U x 1 + i 1 1 + 2 2 2 2 i +1 Pex Pex 1 + i 1 1 + 2 2 2 2
(43)
For a given bulk Peclet number, UL/, as we divide the domain into a larger number of intervals, the cell Peclet number U x / decreases. For uniform grids with 3 gnd 5 grid nodes, the cell Peclet numbers, Pex , at different bulk Peclet numbers, Pe, are given in the table. 3 nodes 5 nodes x L/2 L/4 Pe = 0.5 0.25 0.125 Pe = 1.5 0.75 0.375 Pe = 3 1.5 0.75
x=0
x1 x1 x2 x2 x3 x4
x=L
x3 x5
Pe = 5 2.5 1.25
Pe = 10 5 2.5
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.
2010/11 24 / 35
2010/11
23 / 35
At Pe = 1.5 both the 3 node and 5 node central difference solutions give a good approximation to the exact result. At Pe = 3 the coarser grid solution (with Pex = 1.5) is becoming less accurate. At Pe = 5 the ner grid solution (with Pex = 1.25) is still satisfactory, but with Pex = 2.5 the estimated value of at x /L = 0.5 is now negative. At Pe = 10 both solutions with Pex = 2.5 and 5 give negative values of at some nodes. 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. It can be shown that Pex < 2 is a sufcient condition to ensure the centred difference scheme does not produce under/overshoots in this problem.
2010/11
25 / 35
2010/11
26 / 35
However, this is not a necessary condition. If a non-uniform grid were used, large cells with Pex > 2 could 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 centred convection scheme will produce under and overshoots when the cell Peclet number is large and the solution has steep gradients. (What exactly is large, or steep, can be problem-dependent).
Upwinding
A similar set of solutions can be generated using the rst order upwind scheme (a backward difference) for approximating the convective terms. The discretized equation is then
i i 1 x
i +1 2i + i 1 x 2
(44)
Rearranging leads to
i 1
U x x 2
+ i
U 2 + x x 2
+ i +1
x 2
=0
(45)
i =
i 1 (1 + Pex ) + i +1 2 + Pex
(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.
2010/11 28 / 35
2010/11
27 / 35
As might be expected, once the grid is reasonably ne (Pex not too large) the rst order upwind solution is not as accurate as the second order central difference one. However, the upwind solution is always bounded.
Numerical Diffusion
As seen in the above example, the rst order upwind scheme always returns a bounded solution. However, it is not particularly accurate. The Taylor series expansions examined earlier show that
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 .
i i 1 x x 2 + + O (x 3) = i x 2! i 3! i i 1 2i + i +1 2x 2 = i + + O (x 4) 4! i x 2
(47) (48)
Hence the differential equation actually being solved when using the rst order upwind scheme is
d x d 2 x 2 d 3 + dx 2! dx 2 3! dx 3
d 2 2x 2 d 4 + 4! dx 4 dx 2
+ O (x 4 ) (49)
2010/11
29 / 35
2010/11
30 / 35
Boundary Conditions
(50)
U x d = + dx 2!
d 2 Pex = 1+ 2! dx 2
d 2 dx 2
The error introduced by the upwind scheme is seen to be equivalent to increasing the diffusivity.
As in the example examined earlier, the form of the difference equation to be solved often has to be modied at the edges of the domain to account for boundary conditions. Dirichlet boundary conditions (where the value of the variable is xed) can usually be implemented by simply setting the appropriate value at the boundary node.
The error is thus referred to as diffusive, and generally acts to smear the solution. This tends to exert a stabilizing inuence on the numerical solution, although as seen it has an adverse effect on accuracy.
x1
x2
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 . 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).
2010/11 32 / 35
2010/11
31 / 35
Neumann boundary conditions (the gradient of being prescribed) can be implemented by using a discretized form of the boundary condition. Suppose, for example, we have the condition d /dx = 0 at x = 0.
Although the nite difference methods outlined have been presented in terms of 1-D problems, the extension to 2 or 3 dimensions is fairly straightforward. If, for example, we discretize the 2-D Poisson equation
2 1 x
so
1 = 2
(51)
A quadratic t through the boundary and two neighbouring points on a uniform grid would give d dx
1
31 + 42 3 2x
2 2 + = f (x , y ) x2 y2
on a uniform grid as shown, using central differences for the derivatives, we obtain
(53)
so
1 = (4/3)2 (1/3)3
(52)
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).
i 1,j 2i ,j + i +1.j i ,j 1 2i ,j + i .j +1 + = fi ,j (x )2 (y )2
(54)
These expressions can then be used to modify the discretized equation at the near-boundary nodes.
2010/11 33 / 35 Finite Difference Schemes 2010/11 34 / 35
Note that we now have a ve point stencil, with a difference equation of the form ap P = ae E + aw W + an N + as S + Su (55)
W
N P E
where the subscripts P , E , W , N , S denote the nodal positions and the coefcients and source term are: ae = aw = 1/(x )
2
an = as = 1/(y ) Su = fi ,j
ap = ae + aw + an + as
2010/11
35 / 35