1 Crank-Nicholson Algorithm: Übungen Zur Computational Nanoscience Blatt 5
1 Crank-Nicholson Algorithm: Übungen Zur Computational Nanoscience Blatt 5
2013
Today, you will learn how to implement the Crank-Nicholson algorithm, which is used to solve partial dierential equations (PDEs): u 2u = 2. (1) t x
Crank-Nicholson algorithm
The numerical scheme we have been considering up to now for solving PDEs, is characterized by the fact that the value of the function at the new time can be written explicitly as a function of the values at past times. This is called explicit scheme, and suers from limitations to ensure stability. These limitations, in particular in the case of the diusion equation, can turn out to be quite severe rendering some of these schemes very expensive in CPU time. 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 time but a system of equations has to be solved. Such schemes are called implicit schemes. The simplest and well known implicit scheme, the so-called Crank-Nicholson scheme is obtained by a combination of the forward time dierencing and an averaging over time the second order centered space derivative operator. Then, we can write our partial derivatives with this discretization scheme: +1 un un u j j ; t dt 2u 1 x2 2
+1 n+1 +1 + un un j +1 2uj j 1 dx2
1 2
n n un j +1 2uj + uj 1 dx2
Note that for the partial derivative in x, we now use the weighted average method, averaging at time steps n and n + 1. With the discretization, Eq. (1) reads,
+1 un un j = j
This is easy to express in matrix form, using two matrices, A and B that connects the solution at a time n + 1 with the solution at a time n, Aun+1 = Bun , or: n+1 u1 2+ 1 0 0 ... 0 0 +1 1 un 2 + 1 0 . . . 0 0 2 n +1 0 1 2+ 1 ... 0 0 u3 n+1 0 0 1 2 + ... 0 0 = u4 . . . . . . . .. . . . . . . . . . . . . . . . n+1 0 0 0 0 ... 2 + 1 uJ 1 +1 0 0 0 0 ... 1 2+ un J 2 1 0 0 . . . 0 0 1 2 1 0 . . . 0 0 0 1 2 1 . . . 0 0 0 0 1 2 . . . 0 0 ... ... ... ... .. . 0 0 0 0 . . . 0 0 0 0 . . . un 1 un 2 un 3 un 4 . . . .
... 2 ... 1
1 un J 1 un 2 J
(2)
This is the Crank-Nicholson algorithm. Our main problem is, as before, to invert a tri-diagonal matrix! Let us do our usual reminder, so indexes dont get confusing: The super-index n labels time. The sub-index j labels space. I know u1 j for all j (remember, n = 1 involves t = 0).
n I know un 1 and uJ 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. To have a similar form as last week, we dene dj s, as a column vector representing the product Bum :
n d1 = ( 2)un 1 + u2 ; n n dj = ( 2)un j + uj +1 + uj 1 ;
j = 2, J 1,
+1 ... 2 + 1 un J 1 n ... 1 2+ uJ +1
= d J 1 dJ
d1 d2 d3 d4 . . .
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 of Eq. (2), dene b = 2 + , a = c = 1, and eliminate the o-diagonal terms below the diagonal of matrix A by dening another vector ej , such that c e1 = ; b d1 = so we can now write Eq. (2) as 1 0 0 0 . . . e1 1 0 0 . . . 0 0 0 e2 1 0 . . . 0 0 0 0 e3 1 . . . 0 0 ... ... ... ... .. . 0 0 0 0 . . . 0 0 0 0 . . . eJ 1 1
+1 un 1 n+1 u2 +1 un 3 n+1 u4 . . .
ej = dj =
j = 2, . . . J 1 j = 2, . . . J 1,
d1 ; b
0 0
... 1 ... 0
un+1 J 1 +1 un J
= d J 1 uL
d1 d2 d3 d4 . . .
(3)
d1 =
d1 ; 2+
e1 =
1 , 2+
so we can get from d1 and e1 the value of d2 , from there d3 , and all the way to dJ 1 . Note that the BCs are imposed on the last row of Eq. (3), since we know that un J = uL n.
+1 2. Back substitution: The last row of Eq. (3) has the BCs: un = uL . We start in the previous-last row of Eq. J n+1 (3), which gives a solution for uJ 1 , +1 n+1 un eJ 1 = dJ 1 uL eJ 1 , J 1 = dJ 1 uJ +1 and from there get all the un , going from that row up to the rst one, j +1 +1 un = dj un j j +1 ej .
Note that once this method is implemented, you can use last weeks simplest method too.
Exercises
Exercise 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 immediately detect that the code gives you erroneous results in one of the edges. Your rst task is to plot the results in le 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 rst plot is the numerically calculated results, and the second corresponds to the exact solution. Your ultimate task is to look into the code and detect the bug. Hint: one possible solution would involve changing only one digit. Explain why the program did not work and how you nd 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 aect the nal 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 diusion equation, (1). Find the correspondence between this scheme and the Cayley form, (x, t) = 1+ i H t 2 h
1
i H t (x, 0). 2 h
= 22 + V . What Hint: do the same discretization procedure as in the rst part of this document, with H x changes would be needed in order to solve the Schrdinger Eq. by using our PDE.f90?