Cee 6550 Ftcs Cn Diffusion

12
4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 115 4.3 Finite Difference Solutions of the Diffusion Eqn. We have developed a wide variety of analytical solutions for the diffusion equation with uniform diffusivities (eq. (4.2)), including 1D, 2D, and 3D solutions for instantaneous point sources, distributed sources, and continuous sources in an unbounded domain. We have also introduced the method of images, by which we may extend these solutions to problems with perfectly absorbing and no-flux boundaries. However, where diffusivities are not uniform, where boundaries are not perfectly absorbing or no-flux, and where boundaries have very complicated shapes, our analytical methods meet their limits, and we must turn to numerical modeling. The simplest numerical modeling technique is the finite difference method. Let us illustrate the finite difference method with the following 1D diffusion problem: Consdier 1D diffusion on the domain -L x L for time t 0. Provided that diffusion is Fickian, the governing equation is: ∂C ∂t = ∂x D x ∂C ∂x where the diffusivity D x = D x (x, t) is an arbitrary but known function of space and time 2 . Since we know the function D x (x, t), we also know its spatial derivative ∂D x /∂x, so we may expand the governing equation as follows: ∂C ∂t = ∂D x ∂x ∂C ∂x + D x 2 C ∂x 2 (4.40) Let the initial condition be C (x, t = 0) = C 0 (x) where C 0 (x) is an arbitrary function of x. Let us define a general Dirichlet boundary condition at x = -L and a no-flux boundary condition at x = L: C (x = -L, t)= C B (t) ∂C ∂x x=L =0 2 In the case of non-Fickian diffusion, D x may also be a function of the concentration distribution C(x), in which case this problem becomes non-linear. We will discuss linear solutions only, but provided that the nonlinearity is weak, the finite difference method may be easily extended to nonlinear problems if we linearize them – in the example where D x depends on concentration, we would linearize the problem by using the values of C(x) at the current time step to calculate D x , then using this current value of D x to calculate C(x) at the next time step. Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Transcript of Cee 6550 Ftcs Cn Diffusion

Page 1: Cee 6550 Ftcs Cn Diffusion

4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 115

4.3 Finite Difference Solutions of the Diffusion Eqn.

We have developed a wide variety of analytical solutions for the diffusion equation withuniform diffusivities (eq. (4.2)), including 1D, 2D, and 3D solutions for instantaneouspoint sources, distributed sources, and continuous sources in an unbounded domain. Wehave also introduced the method of images, by which we may extend these solutions toproblems with perfectly absorbing and no-flux boundaries. However, where diffusivitiesare not uniform, where boundaries are not perfectly absorbing or no-flux, and whereboundaries have very complicated shapes, our analytical methods meet their limits, andwe must turn to numerical modeling. The simplest numerical modeling technique is thefinite difference method.

Let us illustrate the finite difference method with the following 1D diffusion problem:Consdier 1D diffusion on the domain −L ≤ x ≤ L for time t ≥ 0. Provided thatdiffusion is Fickian, the governing equation is:

∂C

∂t=

∂x

(Dx

∂C

∂x

)where the diffusivity Dx = Dx(x, t) is an arbitrary but known function of space andtime2. Since we know the function Dx(x, t), we also know its spatial derivative ∂Dx/∂x,so we may expand the governing equation as follows:

∂C

∂t=∂Dx

∂x

∂C

∂x+Dx

∂2C

∂x2(4.40)

Let the initial condition beC(x, t = 0) = C0(x)

where C0(x) is an arbitrary function of x. Let us define a general Dirichlet boundarycondition at x = −L and a no-flux boundary condition at x = L:

C(x = −L, t) = CB(t)(∂C

∂x

)x=L

= 0

2In the case of non-Fickian diffusion, Dx may also be a function of the concentration distributionC(x), in which case this problem becomes non-linear. We will discuss linear solutions only, but providedthat the nonlinearity is weak, the finite difference method may be easily extended to nonlinear problemsif we linearize them – in the example where Dx depends on concentration, we would linearize the problemby using the values of C(x) at the current time step to calculate Dx, then using this current value of Dx

to calculate C(x) at the next time step.

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 2: Cee 6550 Ftcs Cn Diffusion

116 CHAPTER 4. THE DIFFUSION EQUATION

The finite difference approach is to discretize x and t as illustrated below:

and to solve for Cni where Cn

i ≡ C(xi, tn). In order to solve for Cni , we need to develop a

finite difference formula approximating eq. (4.40). We do so using Taylor series expansionin both space and time to approximate the spatial and temporal derivatives.

4.3.1 Approximating the Derivatives

Let’s deal with the spatial derivatives first: Note that if we know Cni , we may approximate

Cni+1 and Cn

i−1 using Taylor series as follows:

Cni+1 = Cn

i +

(∂C

∂x

)ni

∆x+

(∂2C

∂x2

)ni

∆x2

2+

(∂3C

∂x3

)ni

∆x3

6+O(∆x4) (4.41)

Cni−1 = Cn

i −(∂C

∂x

)ni

∆x+

(∂2C

∂x2

)ni

∆x2

2−(∂3C

∂x3

)ni

∆x3

6+O(∆x4) (4.42)

where O(∆x4) denotes terms that are order 4 and higher in ∆x.

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 3: Cee 6550 Ftcs Cn Diffusion

4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 117

There are three ways we can approximate (∂C/∂x)ni using Cni , Cn

i+1, and Cni−1. Rear-

ranging eq. (4.41), we find the forward difference approximation:(∂C

∂x

)ni

=Cni+1 − Cn

i

∆x+O(∆x) (4.43)

Rearranging eq. (4.42), we find the backward difference approximation:(∂C

∂x

)ni

=Cni − Cn

i−1

∆x+O(∆x) (4.44)

And subtracting eq. (4.42) from eq. (4.41), we find the central difference approxima-tion: (

∂C

∂x

)ni

=Cni+1 − Cn

i−1

2∆x+O(∆x2) (4.45)

Note that the truncation error is O(∆x) for the forward and backward differenceapproximations, so we say that these approximations are first order accurate. Sincethe truncation error is O(∆x2) for the central difference approximation, we say that it issecond order accurate. Since ∆x is small, second order accuracy is better than firstorder accuracy.

The time derivative, (∂C/∂t)ni , may also be approximated using forward, backward, orcentral differencing, resulting in accuracy that isO(∆t), O(∆t), andO(∆t2), respectively.

There is only one way to approximate (∂2C/∂x2)ni using Cni , Cn

i+1, and Cni−1. Adding

eq. (4.41) to eq. (4.42) and rearranging a little, we find the following finite differenceapproximation for (∂2C/∂x2)ni(

∂2C

∂x2

)ni

=Cni+1 − 2Cn

i + Cni−1

∆x2+O(∆x2) (4.46)

4.3.2 The FTCS Scheme

As an example, let us illustrate how to solve for Cni using the FTCS scheme. FTCS

stands for Forward difference in Time and Central difference in Space. Plugging inthe forward difference approximation for ∂C/∂t and the central difference approximationfor ∂C/∂x along with the finite difference approximation for ∂2C/∂x2, the governingequation (eq. (4.40)) becomes

Cn+1i − Cn

i

∆t=

(∂Dx

∂x

)ni

Cni+1 − Cn

i−1

2∆x+ (Dx)

ni

Cni+1 − 2Cn

i + Cni−1

∆x2+O(∆t,∆x2)

We see that the FTCS scheme is first order accurate in time and second order accu-rate in space. Rearranging, we may solve for concentration at time n + 1 in terms of

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 4: Cee 6550 Ftcs Cn Diffusion

118 CHAPTER 4. THE DIFFUSION EQUATION

concentrations at the previous time, n, as follows

Cn+1i =

[∆t

2∆x

∂Dx

∂x+Dx∆t

∆x2

]Cni+1 +

[1− 2

Dx∆t

∆x2

]Cni +

[∆t

2∆x

∂Dx

∂x+Dx∆t

∆x2

]Cni−1

where to make things look neater, we have let Dx = (Dx)ni and ∂Dx/∂x = (∂Dx/∂x)ni .

If the diffusivity varies slowly (specifically, if ∂Dx/∂x is small compared to 2Dx/∆x),which we can ensure by picking a small ∆x, then the FTCS scheme simplifies to

Cn+1i =

Dx∆t

∆x2Cni+1 +

[1− 2

Dx∆t

∆x2

]Cni +

Dx∆t

∆x2Cni−1 (4.47)

Note that

s ≡ Dx∆t

∆x2(4.48)

is a dimensionless number called the diffusion number. In terms of s, we find thefollowing finite difference equation for the FTCS scheme:

Cn+1i = s Cn

i+1 + (1− 2s)Cni + s Cn

i−1 (4.49)

We may sketch the following computational stencil to illustrate that for the FTCSscheme, the concentration at location i at time n+1 depends on the three concentrationsat locations i− 1, i, and i+ 1 at time n

We march this computational stencil forward in time to solve for Cni over the whole grid,

starting with the initial conditions and imposing the boundary conditions along the way.

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 5: Cee 6550 Ftcs Cn Diffusion

4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 119

4.3.3 Imposing Initial and Boundary Conditions

Initial Conditions

The initial conditions, C(x, t = 0) = C0(x) give us the concentrations Cn=1i at time

n = 1 for all locations i = 1, ..., I. If we apply our stencil (eq. (4.49)) at time n = 1 andlocations i = 2, ..., I − 1, we can calculate the values of Cn=2

i at time n = 2 and locationsi = 2, ..., I − 1 as illustrated below:

Notice that we cannot calculate the boundary values of Cn=2i located at i = 1 and i = I

using our computational stencil, and this is a good thing because otherwise the problemwould be over-determined. Before we can calculate concentrations at time n = 3, weneed to impost the boundary conditions to evaluate Cn=2

i=1 and Cn=2i=I .

Dirichlet Boundary Condition

Imposing the Dirichlet boundary condition, C(x = −L, t) = CB(t), is fairly straight-forward. We simply use this boundary condition to specify the value of Cn

i=1 at timesn = 2, 3, ... as illustrated below:

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 6: Cee 6550 Ftcs Cn Diffusion

120 CHAPTER 4. THE DIFFUSION EQUATION

Neumann Boundary Condition

Imposing the no-flux boundary condition(∂C

∂x

)x=L

= 0

is a bit more tricky because this boundary condition does not explicitly tell us the valueof Cn

i=I . We may use the forward difference scheme to approximate Cni=I ≈ Cn

i=I−1,but the forward difference scheme is only first order accurate in space! To maintainthe second order spatial accuracy of the FTCS scheme, we must create fictitious gridpoints at i = I + 1 and apply the central differencing scheme to approximate Cn

i=I+1 ≈Cni=I−1 with second order accuracy. After calculating Cn

i=I+1 using the central differenceapproximation, we may use the three known values, Cn

i=I−1, Cni=I , and Cn

i=I+1 and ourstencil (eq. (4.49)) to calculate Cn+1

i=I as illustrated below:

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 7: Cee 6550 Ftcs Cn Diffusion

4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 121

4.3.4 Extending FTCS to 2D and 3D

We may easily extend the FTCS scheme to 2D and 3D diffusion problems by writingTaylor series expansions in the y and z-directions. As an example, the FTCS stencil forthe 2D diffusion equation

∂C

∂t=

∂x

(Dx

∂C

∂x

)+

∂y

(Dy

∂C

∂y

)with slowly varying diffusivities (i.e., ∂Dx/∂x << 2Dx/∆x and ∂Dy/∂y << 2Dy/∆y) is

Cn+1i,j = sx C

ni+1,j + sx C

ni−1,j + sy C

ni,j+1 + sy C

ni,j−1 + (1− 2sx − 2sy)C

ni,j (4.50)

where the diffusion numbers in x and y have been defined as follows

sx ≡Dx∆t

∆x2and sy ≡

Dy∆t

∆y2

This stencil is illustrated below:

This stencil, represented by eq. (4.50), is known as the five-point stencil because fivepoints at time n are used to calculate values at time n + 1. Note that we may createa nine-point stencil by incorporating the values of concentration on the diagonals,such as Cn

i+1,j+1 into our finite difference approximations of the second derivatives. Wewill not cover the nine-point stencil in this course, but you can look it up in many booksabout finite difference methods. In 3D we can develop a seven-point or a 27-point stencil,depending on whether the diagonals are included.

In general (in 1D, 2D, or 3D), higher order approximations of the derivatives may beused to get better accuracy (third order and higher), incorporating even more points intothe computational stencil, but second order accuracy is usually enough.

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 8: Cee 6550 Ftcs Cn Diffusion

122 CHAPTER 4. THE DIFFUSION EQUATION

4.3.5 Implicit Schemes

The FTCS scheme is called an explicit scheme because the values of C at time n+1 arecalculated explicitly from the values of C at time n. In contrast, an implicit schemeuses the values of C at time n+ 1 (and possibly also the values of C at time n) to solvefor the values of C at time n+ 1. The result is a matrix equation for Cn+1

i at each timestep. A general implicit scheme for solving the 1D diffusion equation has the form:

Cn+1i − Cn

i

∆t= θ

(∂Dx

∂x

∂C

∂x+Dx

∂2C

∂x2

)n+1

i

+ (1− θ)(∂Dx

∂x

∂C

∂x+Dx

∂2C

∂x2

)ni

(4.51)

where the differential equations in parenthesis are discretized (e.g., as demonstrated inSection 4.3.2 for the FTCS scheme), and θ is a constant between zero and one thatdetermines the amount of weight given to the values of C at times n and n + 1. Foran explicit scheme such as the FTCS scheme, θ = 0 so that no weight is given to theterms at time n+ 1; for a fully implicit scheme, θ = 1 so that no weight is given to theterms at time n. The most popular scheme is probably the Crank-Nicolson schemefor which θ = 0.5 and central differencing is used in space. The Crank-Nicolson (C-N)scheme is said to be “centered in time” because equal weight is given to the values of C attimes n and n+ 1 in calculating the time derivative3. Because of this, C-N is accurate toO(∆t2,∆x2). This results in less dispersive error compared to the FTCS scheme (moreon this soon).

The finite difference equation for the Crank-Nicolson scheme, assuming that ∂Dx/∂x issmall compared to 2Dx/∆x, works out to

−s2Cn+1i−1 + (1 + s) Cn+1

i − s

2Cn+1i+1 =

s

2Cni−1 + (1− s) Cn

i +s

2Cni+1 (4.52)

where

s =Dx∆t

∆x2

Note that even if Dx varies in space and time, it is evaluated at location i and time n forthe purpose of calculating s.

3C-N could be called the CTCS scheme as it is centered in both time and space, but nobody calls itthat.

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 9: Cee 6550 Ftcs Cn Diffusion

4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 123

Equation (4.52) represents the following a tridiagonal matrix equation:

− s2

(1 + s) − s2

0 0 . . . 0

0 − s2

(1 + s) − s2

0 . . . 0

0 0 − s2

(1 + s) − s2

. . . 0

.

.

.

0 . . . . − s2

(1 + s) − s2

0

0 . . . . 0 − s2

(1 + s) − s2

Cn+11

Cn+12

Cn+13

Cn+14

.

.

.

Cn+1I−1

Cn+1I

Cn+1I+1

=

s2Cn

1 + (1− s) Cn2 + s

2Cn

3

s2Cn

2 + (1− s) Cn3 + s

2Cn

4

s2Cn

3 + (1− s) Cn4 + s

2Cn

5

.

.

.

s2CnI−2 + (1− s) Cn

I−1 + s2CnI

s2CnI−1 + (1− s) Cn

I + s2CnI+1

At each time step, we solve this matrix equation for the unknown values of Cn+1

i fromthe known values of Cn

i . Since the matrix is tridiagonal, we may use the highly efficientThomas algorithm, also known as the TDMA (the tridiagonal matrix algorithm). TheThomas algorithm solves a tridiagonal matrix inO(I) steps whereas Gaussian eliminationrequires O(I3) steps. The Thomas algorithm is sufficiently popular that you may find itthrough a quick internet search.

The boundary conditions have been left out of this matrix so that you can fill them in.If we apply a general Dirichlet type boundary condition at i = 1:

C(x = −L, t) = CB(t)⇒ Cn+11 = CB(tn+1)

and a no-flux boundary condition at i = I, discretized using central differencing and a

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 10: Cee 6550 Ftcs Cn Diffusion

124 CHAPTER 4. THE DIFFUSION EQUATION

ficticious grid point at i = I + 1 for second order accuracy:(∂C

∂x

)x=L

= 0⇒ Cn+1I+1 = Cn+1

I−1

how would we impose the boundary conditions within the matrix equation?

4.3.6 Von Neumann Stabiltity Analysis

In a finite difference solver, initial conditions will have some small numerical error E:

If the error is amplified as time progresses, the solution will become unstable, and thenumerical solution will blow up. This is what a solution that is blowing up typicallylooks like:

How can we predict from the discretized equations whether errors will be amplified ordamped out? There are several approaches, but here we will only discuss one. VonNeumann’s idea was to represent the initial error as a Fourier series and see what happensto the individual Fourier modes as time progresses. Since the diffusion equation is linear,the modes grow independently.

The error at time n can be written

Eni =

∑κ

Enκ e jκi∆x

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 11: Cee 6550 Ftcs Cn Diffusion

4.3. FINITE DIFFERENCE SOLUTIONS OF THE DIFFUSION EQN. 125

where we have adopted the electrical engineering convention that j ≡√−1 so we can

use i as the index of the x-axis as we did in the previous sections. κ is wave number, Enκ

is the Fourier coefficient for wave number κ, and Enκe jκi∆x is a single Fourier mode

with wave number κ.

Let’s plug a single Fourier mode into the discretized FTCS equation

Cn+1i = s Cn

i+1 + (1− 2s)Cni + s Cn

i−1

and see if the amplitude grows or shrinks in time – this will tell us if the solution is stableor unstable. The solution is stable if ∣∣∣∣∣En+1

κ

Enκ

∣∣∣∣∣ ≤ 1

Plugging Cni = En

κ e jκi∆x into the discrete FTCS equation, we find

En+1κ e jκi∆x = sEn

κ e jκ(i−1)∆x + (1− 2s)Enκ e jκi∆x + sEn

κ e jκ(i+1)∆x

Dividing by Enκ e jκi∆x ⇒

En+1κ

Enκ

= se −jκ∆x + (1− 2s) + se +jκ∆x

Recall that e jθ = cos θ + j sin θ, that cos(−θ) = cos θ, and that sin(−θ) = − sin θ ⇒

En+1κ

Enκ

= 2s cosκ∆x+ (1− 2s)

Stability requires∣∣∣En+1

κ /Enκ

∣∣∣ ≤ 1 for all wave numbers κ, thus stability of the FTCS

scheme requires|2s cosκ∆x+ (1− 2s)| ≤ 1

for all values of κ∆x. The worst case scenario is κ∆x = −π which requires

|1− 4s| ≤ 1⇒

−1 ≤ 1− 4s ≤ 1⇒

−2 ≤ −4s ≤ 0⇒

2 ≥ 4s ≥ 0⇒

1/2 ≥ s ≥ 0

So we see that for the FTCS formulation of the diffusion equation to be stable, we musthave

0 ≤ s ≤ 1

2

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)

Page 12: Cee 6550 Ftcs Cn Diffusion

126 CHAPTER 4. THE DIFFUSION EQUATION

Since s = Dx∆t/∆x2, this means that the time step ∆t must be in the range

0 ≤ ∆t ≤ ∆x2

2Dx

for the FTCS scheme to be stable for all wave numbers. From a physical perspective, thestability requirement for the FTCS scheme is that the computational time step must besmaller than the time it takes the scalar to diffuse between two adjacent computationalgrid points. This seems reasonable: if we want to keep track of the concentration at eachgrid point at each time step, we can’t have the scalar skipping grid points during thetime step.

Note that since ∆t ∼ ∆x2, if we double the spatial resolution, we have to quadruple thetime resolution of our numerical solver!

Cornell University CEE 6550 (Alexandra King, Seth Schweitzer)