Introduction to Finite Difference Techniques

advertisement
Introduction to Finite Difference Techniques
In this chapter, we briefly review the techniques available for the discretization of
governing equations prior to their solution on digital computer. Discretization is the
process in which an ordinary derivative, or a partial derivative is approximated by an
algebraic formula, which approaches the original derivative as the interval in which the
approximation is made shrinks to zero.
There are three popular approaches to discretizing a given equation. These are
a) finite difference technique, b) finite volume technique, and c) finite element
technique.
These three techniques are roughly of comparable complexity when it
comes to their implementation, and often (but not always) yield identical discretized
formulas.
A Model Problem:
To illustrate these three techniques, and to show how they relate to each other,
let us consider the following ordinary differential equation:
d 2T
c
dx 2
(1)
Let us assume that this ODE is to be solved within the interval x= 0, and x=L, subject to
the boundary conditions
T  T0 at x  0
T  TL at x  L
(2)
For simplicity we assume 'c' to be a constant.
Construction of Grid for the Model Problem:
The analytical solution of the above ODE is likely to be a smooth continuously
differentiable solution. As a first step towards solving this ODE, we assume that we
would seek the numerical solution only at a set of N points within this interval (including
the end points). Thus we give up some of the useful information in the original analytical
solution, for the ease of solving this ODE numerically.
Node 1
Node i-1 Node i
Node i+1
Node N
At first glance it appears that these N points (called nodes) shown in figure 1
may be chosen anywhere within the interval. But we realize that we will be using these
N nodes to obtain approximations to the first and second derivatives of the function
T(x). To ensure that these derivatives are computed as reliably as possible, we space
these N nodes equally, at a distance equal to x.
In some physical problems, resolution of rapidly varying phenomena (e.g.
velocity profile near the wall in a boundary layer, or pressure distribution across a shock
wave) requires that the node points be closely packed. If this be the case, then one
can place nodes in the other regions sparsely, but should ensure that there are
adequate number of points in all regions to obtain meaning information on the solution,
its first derivative, and the second derivative.
This process of placing the nodes in a given region is called grid generation, and
is a highly developed science. Later in this course, we will show some grid generation
techniques, but will not have the time to study such techniques in detail.
Regardless of whether we use a finite difference, finite volume or a finite element
technique, this process of grid generation must be carried out first, and the grid plotted
to ensure that the nodes are equally placed.
Finite Difference Technique:
We look at the first of the three approaches for solving equation (1). Let 'i' be a
typical node in the interior of the discretized region. Let 'i+l' and 'i-l' be the two nodes to
the left and right of the current node. If T(x) is a sufficiently smooth function, then T(x)
may be expanded around about the node 'i' using a Taylor's series. Thus,
2
2
3
3
4
4
 dT  x  d T  x  d T  x  d T 
 2  
 3  
 4   .....
Ti 1  T ( xi  x)  Ti  x
 
2  dx 
6  dx  24  dx 
 dx i
Ti 1
2
2
3
3
4
4
 dT  x  d T  x  d T  x  d T 





  .....
 T ( xi  x)  Ti  x


 
2  dx 2 
6  dx 3  24  dx 4 
 dx i
(3)
We decide to end the series after the terms shown above. This is called truncating the
series. Truncating an infinite series to suit the computation process leads to errors
called truncation errors. This source of errors is different from the round-off errors,
which occur due to the limitations of the hardware and software (example, a calculator
that can display only up to 8 digits). Computers differ from each other in the accuracy to
which arithmetic is performed prior to round off. Usually, 64-bit arithmetic on machines
such as the CRAY produces a smaller round-off error than 32-bit arithmetic, the default
mode on many workstation class of machines.
If the expressions for Ti+1 and Ti-1 shown in equation (3) are added together, and
the resulting expression rearranged, the following expression for T xx results:
d 2T
Ti 1  2Ti  Ti 1 x 2 d 4T


....
dx 2 i
x 2
12 dx 4 i
or,
d 2T
T  2Ti  Ti 1
 i 1
 O x 2
2
2
dx i
x
 
(4)
The 'O' in the above equation reads 'order of'. It simply states that if T is approximated
by the first term on the right hand side of equation (4), the truncation error is of the
order of x2. Then, if x approaches zero, the first term on equation (2.5), called the
finite difference approximation to T xx will approach the exact analytical derivative. The
above approximation is a second order accurate approximation because the truncated
terms are of the order of x2, or smaller.
We could also device other derivatives of T.
For example, subtracting the
expressions for Ti+1 and Ti-1 shown in equation (3), and some rearranging, we get
dT
T T
 i 1 i 1  O x 2
dx i
2x
 
(5)
Additional derivatives of T may be computed if T is evaluated at nodes 'i+2', 'i-2' etc
through a Taylor's series about the node i.
Taylor's series may be viewed as a polynomial fit to the function T(x) using the
nodal values to fix the polynomial coefficients. If we choose 'n' nodes We can fit at most
a (n-1) order polynomial, and obtain at most a derivative of order n-1. Note that the
decision to use nodes i, i-1 and i+l was ours. We could have used the nodes i, i-1 and i2 for example, and obtained the following:
d 2T
T  2Ti 1  Ti  2
 i
 O x 2
2
2
dx i
x
 
(6)
Thus the finite difference series will cheerfully give us a discretizaion want, using
whatever nodes we choose. It is the scientist's responsibility to choose those nodes that
are likely to influence what happens at i, from a physical point of view. For example,
use of
(6) would not represent the physics in this particular case if we think
of
equation (1) as describing the temperature distribution in a rod subjected to a heat
source of strength c. Since heat may be conducted in either direction, the node will be
influenced equally well by the nodes i+l and i-1. Approximations such as equation (6)
which do not acknowledge the influence of the node i+l on i will likely lead to illbehaved algorithms, and will eventually lead to meaningless solutions.
In summary, the finite difference analog to our ODE, of second order accuracy,
is
Ti 1  2Ti  Ti 1
c
x 2
(7)
Spectral Finite Difference Methods:
The choice of Taylor's series to represent the behavior of T(x) in the vicinity is by
no means unique. Other series expansions, such as Fourier series expansions may be
used. For example, if the T distribution is known to be periodic in x (i.e. To = TL) then
one can write T(x) as the following Fourier series.
n
T ( x)  A0 
N
2
 2nx 

L 
 A sin 
n 1
n
n
N
2
 2nx 

L 
 B cos
n 1
n
(8)
Here An and Bn are Fourier coefficients which may be evaluated if T is known at N
node points within the interval O<x<L using classical techniques. For example, a Fast
Fourier Transform may be used if N is a power of 2.
Once such an expansion is found, the second derivative of T may be found by
analytical1y differentiating equation (8) twice.
Approximations such as the one shown above which use Fourier series or Chebyshev
polynomials to fit the behavior of within the interval O<x<L are called spectral
approximations.
They are of higher order accuracy compared to the Taylor series
methods. They converge with fewer terms in the series to the correct solution, so that
fewer nodes need to be used. They are widely used in problems such as numerical
simulation of transition and turbulence.
They, however, behave undesirably in the
vicinity of sharp gradients such as shocks. For this reason, and because of the slightly
increased coding complexity, they have not found widespread use in fixed and rotary
wing aerodynamics.
Finite Volume Techniques:
In finite volume techniques, we assume that the region of interest is divided into
cells, or finite volumes, as shown in figure below. The nodes may from the vertices of
the cell, or the centroid of the cell, and either choice has its advantages. For the sake of
illustration, we will assume that the nodes are located at cell centers. Let 'i' be a typical
cell or "control volume" or "finite volume" as shown in figure below.
Control Volume of width x
Node i-1
Node i
Node i+1
If we integrate the governing equation (2.1) across the width of the cell, we get the
following approximation:
i
1
2
T
xx
dx  cx
1
i
2
(9)
Notice that the limits of the integral are the cell boundaries, i+1/2 and i-1/2, which
may be assumed to be halfway between the nodes.
Performing the integration analytically, we get
Tx i  1  Tx i  1  cx
2
2
(9)
We now go to our collection of finite difference formulas and borrow a finite difference
approximation to Tx. Using (5) as our analog, we get
Ti 1  Ti
x
T T
 i i 1
x
Tx i  1 
2
Tx i  1
2
(10)
Since these formulas are second order accurate, and since the original expression
(2.11) was analytically obtained, we are guaranteed (at least formally) second order
accuracy.
Using equation (10) in (9), we get
Ti 1  2Ti  Ti 1
c
x 2
(11)
This is our finite volume analog of the original ODE. For this simple ODE, it matches
exactly the
finite difference analog shown in equation (7). This may not always be
the case in more complex problems, however.
Finite Element Techniques:
The finite element
techniques in use in CFD are called weighted residual
methods, or Galerkin methods, named after the Russian scientist who perfected such
methods. One may view the finite volume technique as a finite element technique in
which the weighting function is unity.
Let N(x) be a weighting function which equals 1 at node i, and zero at other
nodes.
The function N(x) may be a simple linear function with discontinuous first
derivatives as shown in figure below.
1
N(x)
Node N
Node 1
Node i
We now multiply the governing equation by N(x), and integrate over the entire
region O<x<L. Since N(x) is zero in most of the region, only the interval xi<x<xi+l
contributes to the integration. We thus get,
i 1
i 1
d 2T
 2 N ( x)dx  i1cN ( x)dx
i 1 dx
(12)
The right hand side is easily integrated, since c is a constant. (If it were not, a
numerical integration will have to be done.) The right side is simply c times the area
under the triangle shown in the figure above, or cx.
The left side requires some work. We first write the left side of equation (11) as:
i 1
 dT 
 N ( x)d  dx 
i 1
(13)
From a commonly used technique called integration by parts, if P and Q are two
functions of x, then:
 PdQ  PQ   QdP
(14)
Applying this to the expression in equation (13), we get
i 1
i 1
i 1
dT 
dT dN
 dT  
i 1N ( x)d  dx    N ( x) dx i 1  i1 dx dx dx
(15)
The first term on the right hand side of equation (15) is zero since N(x) is zero at all
nodes except at i (see figure above). The second term may be analytically integrated,
once we realize that the quantity dN/dx is a piecewise constant. It equals +1/x in the
interval between I-1 and i. It equals -1/x in the interval between node I and I+1. The
expression in equation (15), which started out as the left side of equation (12) becomes:
dT  1 
i 1 dx  x dx  
i

i 1

i
i 1
dT  1 
1
1
1
Ti 1  Ti   Ti  Ti 1 
dT 
dT 
  dx  


dx  x 
x i 1
x i
x
i
(16)
Assembling the right sides of equation (12), which equals cx as discussed earlier, and
the left side of equation (12), we get:
Ti 1  2Ti  Ti 1
c
x 2
(17)
Note that this expression is identical to that from the finite difference (FD)
method (equation 7) and from the finite volume method (FV) (equation 11). For our
simple ODE the three methods (FD, FV and FEM) all lead to same result. While this will
not be expected for all PDEs, we can expect these three methods of discretization to
yield algebraic equations of comparable size and complexity.
In summary, given an ODE or a PDE, and a priori knowledge of which nodes
should participate in a given discretization, one can construct a finite difference, a finite
volume or a finite element approximation of the given equation. These approximations
may be of a user prescribed spatial accuracy. In practice, second order accurate
schemes are most frequently used.
In much of our work, we will use the finite difference technique.
Download