sheet5
TRANSCRIPT
Übungen zur Computational Nanoscience
Blatt 5
zum 22.05.2013
Today, you will learn how to implement the Crank-Nicholson algorithm, which is used to solve partial differentialequations (PDEs):
∂u
∂t= α
∂2u
∂x2. (1)
1 Crank-Nicholson algorithmThe numerical scheme we have been considering up to now for solving PDEs, is characterized by the fact that thevalue of the function at the new time can be written explicitly as a function of the values at past times. This iscalled ’explicit’ scheme, and suffers from limitations to ensure stability. These limitations, in particular in the caseof the diffusion equation, can turn out to be quite severe rendering some of these schemes very expensive in CPUtime. It is possible to write schemes which are a priori not subject to such timestep limitations for stability reasons.Unfortunately, in such schemes it is no longer possible to explicitly solve for the value of the function at the new timebut a system of equations has to be solved. Such schemes are called implicit schemes. The simplest and well knownimplicit scheme, the so-called Crank-Nicholson scheme is obtained by a combination of the forward time differencingand an averaging over time the second order centered space derivative operator. Then, we can write our partialderivatives with this discretization scheme:
∂u
∂t→
un+1j − un
j
dt;
∂2u
∂x2→ 1
2
(un+1
j+1 − 2un+1j + un+1
j−1
dx2
)+
12
(un
j+1 − 2unj + un
j−1
dx2
).
Note that for the partial derivative in x, we now use the weighted average method, averaging at time steps n andn + 1. With the discretization, Eq. (1) reads,
un+1j − un
j =αdt
2(dx)2(un+1
j+1 − 2un+1j + un+1
j−1 + unj+1 − 2un
j + unj−1
),
or, calling µ = 2(dx)2/αdt and re-arranging,
−un+1j+1 + un+1
j (2 + µ)− un+1j−1 = un
j−1 + unj (2− µ) + un
j+1.
This is easy to express in matrix form, using two matrices, A and B that connects the solution at a time n + 1 withthe solution at a time n, Aun+1 = Bun, or:
2 + µ −1 0 0 . . . 0 0−1 2 + µ −1 0 . . . 0 00 −1 2 + µ −1 . . . 0 00 0 −1 2 + µ . . . 0 0...
......
.... . .
......
0 0 0 0 . . . 2 + µ −10 0 0 0 . . . −1 2 + µ
un+11
un+12
un+13
un+14...
un+1J−1
un+1J
=
µ− 2 1 0 0 . . . 0 01 µ− 2 1 0 . . . 0 00 1 µ− 2 1 . . . 0 00 0 1 µ− 2 . . . 0 0...
......
.... . .
......
0 0 0 0 . . . µ− 2 10 0 0 0 . . . 1 µ− 2
un1
un2
un3
un4...
unJ−1
unJ
. (2)
This is the Crank-Nicholson algorithm. Our main problem is, as before, to invert a tri-diagonal matrix! Let us do ourusual reminder, so indexes don’t get confusing:
• The super-index n labels time.
• The sub-index j labels space.
• I know u1j for all j (remember, n = 1 involves t = 0).
• I know un1 and un
J for all n (remember, j = 1 involves x = 0 and j = J involves x = L).
With all this in mind, we can employ the Thomas algorithm, our two-step process to solve a tri-diagonal matrix. Tohave a similar form as last week, we define dj ’s, as a column vector representing the product Bum:
d1 = (µ− 2)un1 + un
2 ; dj = (µ− 2)unj + un
j+1 + unj−1; j = 2, J − 1,
so we have again,
2 + µ −1 0 0 . . . 0 0−1 2 + µ −1 0 . . . 0 00 −1 2 + µ −1 . . . 0 00 0 −1 2 + µ . . . 0 0...
......
.... . .
......
0 0 0 0 . . . 2 + µ −10 0 0 0 . . . −1 2 + µ
un+11
un+12
un+13
un+14...
un+1J−1
un+1J
=
d1
d2
d3
d4...
dJ−1
dJ
.
We may proceed then as last week, solving in our usual two step process:
1. Forward sweep: we eliminate the µ’s below the diagonal and normalize the diagonal to 1. For the left side ofEq. (2), define b = 2 + µ, a = c = −1, and eliminate the off-diagonal terms below the diagonal of matrix A bydefining another vector ej , such that
e1 =c
b; ej =
c
b− aej−1,j = 2, . . . J − 1
d′1 =d1
b; d′j =
dj − adj−1
b− ae′j, j = 2, . . . J − 1,
so we can now write Eq. (2) as
1 e1 0 0 . . . 0 00 1 e2 0 . . . 0 00 0 1 e3 . . . 0 00 0 0 1 . . . 0 0...
......
.... . .
......
0 0 0 0 . . . 1 eJ−1
0 0 0 0 . . . 0 1
un+11
un+12
un+13
un+14...
un+1J−1
un+1J
=
d′1d′2d′3d′4...
d′J−1
uL
. (3)
Remember that we know u1j , so for n = 1 (which is t = 0), we have
d1 = (µ− 2)u11 + u1
2; d′1 =d1
2 + µ; e1 =
−12 + µ
,
so we can get from d′1 and e1 the value of d′2, from there d′3, and all the way to d′J−1. Note that the BCs areimposed on the last row of Eq. (3), since we know that un
J = uL∀n.
2. Back substitution: The last row of Eq. (3) has the BCs: un+1J = uL. We start in the previous-last row of Eq.
(3), which gives a solution for un+1J−1,
un+1J−1 = d′J−1 − un+1
J eJ−1 = d′J−1 − uLeJ−1,
and from there get all the un+1j , going from that row up to the first one,
un+1j = d′j − un+1
j+1 ej .
Note that once this method is implemented, you can use last week’s simplest method too.
2 ExercisesExercise 1. This exercise concerns the code ’PDE.f90’. Please, download it, save it in a new folder, compile
it and run it. The code available to you has a bug. The BCs are not properly set, and you will immediatelydetect that the code gives you erroneous results in one of the edges. Your first task is to plot the results in file‘results_u.dat’. You may use gnuplot, for instance:
gnuplot > p ’results_u .dat ’ u 2:3 , ’results_u .dat ’ u 2:4 w lines
The first plot is the numerically calculated results, and the second corresponds to the exact solution. Yourultimate task is to look into the code and detect the bug. Hint: one possible solution would involve changingonly one digit. Explain why the program did not work and how you find the bug.
Exercise 2. Change the value of ‘alpha’ to 5.0. How do results change? what is the physical meaning of ’alpha’?
Exercise 3. Change the value of ‘nt’ to 100. Does this affect the final result? Go back to the original value of ‘nt’and now change ‘nx’ to 100. How does your solution look like?
Exercise 3.As you saw in the lecture, the Crank-Nicholson scheme was originally designed to solve the heat diffusionequation, (1). Find the correspondence between this scheme and the Cayley form,
Ψ(x, t) =(
1 +i
2hH∆t
)−1(1− i
2hH∆t
)Ψ(x, 0).
Hint: do the same discretization procedure as in the first part of this document, with H = − ∂2
∂x2 + V . Whatchanges would be needed in order to solve the Schrödinger Eq. by using our PDE.f90?