C P - PH 354: Omputational Hysics
C P - PH 354: Omputational Hysics
C P - PH 354: Omputational Hysics
10 cm 6 cm
+1 V −1 V
0V
2 cm 6 cm 2 cm
For simplicity let us model the system in two dimensions. Using any of the
methods we have studied, write a program to calculate the electrostatic poten-
tial in the box on a grid of 100 × 100 points, where the walls of the box are at
voltage zero and the two plates (which are of negligible thickness) are at volt-
ages ±1 V as shown. Have your program calculate the value of the potential at
each grid point to a precision of 10−6 volts and then make a density plot of the
result.
Hint: The capacitor plates are part of the boundary condition in this case:
they behave the same way as the walls of the box, with potentials that are fixed
at a certain value and cannot change.
1
varies with the seasons. Suppose the mean daily temperature at a particular
point on the surface varies as:
2πt
T0 (t) = A + B sin ,
τ
where τ = 365 days, A = 10◦ C and B = 12◦ C. At a depth of 20 m below the
surface almost all annual temperature variation is ironed out and the temper-
ature is, to a good approximation, a constant 11◦ C (which is higher than the
mean surface temperature of 10◦ C—temperature increases with depth, due to
heating from the hot core of the planet). The thermal diffusivity of the Earth’s
crust varies somewhat from place to place, but for our purposes we will treat it
as constant with value D = 0.1 m2 day−1 .
Write a program to calculate the temperature profile of the crust as a func-
tion of depth up to 20 m and time up to 10 years. Start with temperature every-
where equal to 10◦ C, except at the surface and the deepest point, choose values
for the number of grid points and the time-step h, then run your program for
the first nine simulated years, to allow it to settle down into whatever pat-
tern it reaches. Then for the tenth and final year plot four temperature profiles
taken at 3-month intervals on a single graph to illustrate how the temperature
changes as a function of depth and time.
0 L
The string vibrates as a result of being struck, except at the ends, x = 0 and
x = L, where it is held fixed. The equations for transverse displacement and
velocity are
∂ξ ∂u ∂2 ξ
= u, = v2 2 .
∂t ∂t ∂x
2
a) Write a program that uses the FTCS method to solve the complete set of
simultaneous first-order equations, Eq. (9.28), for the case v = 100 ms−1 ,
with the initial condition that ξ ( x ) = 0 (transverse displacement) every-
where but the transverse velocity u( x ) is nonzero, with profile
x( L − x) ( x − d )2
u( x ) = C exp − ,
L2 2σ2
d2 x
= − g,
dt2
where g is the acceleration due to gravity.
3
Exercise 5: The Schrödinger equation and the Crank–Nicolson method
One of the most important PDEs, at least for physicists, is the Schrödinger
equation. This exercise uses the Crank–Nicolson method to solve the full time-
dependent Schrödinger equation and hence develop a picture of how a wave-
function evolves over time. The following exercise, Exercise 6, solves the same
problem again, but using the spectral method.
We will look at the Schrödinger equation in one dimension. The techniques
for calculating solutions in two or three dimensions are basically the same as
for one dimension, but the calculations take much longer on the computer, so
in the interests of speed we’ll stick with one dimension. In one dimension the
Schrödinger equation for a particle of mass M with no potential energy reads
h̄2 ∂2 ψ ∂ψ
− = ih̄ .
2M ∂x2 ∂t
For simplicity, let’s put our particle in a box with impenetrable walls, so that
we only have to solve the equation in a finite-sized space. The box forces the
wavefunction ψ to be zero at the walls, which we’ll put at x = 0 and x = L.
Replacing the second derivative in the Schrödinger equation with a finite
difference and applying Euler’s method, we get the FTCS equation
ih̄
ψ( x, t + h) = ψ( x, t) + h ψ ( x + a, t ) + ψ ( x − a, t ) − 2ψ ( x, t ) ,
2ma2
where a is the spacing of the spatial grid points and h is the size of the time-step.
(Be careful not to confuse the time-step h with Planck’s constant h̄.) Performing
a similar step in reverse, we get the implicit equation
ih̄
ψ( x, t + h) − h ψ ( x + a, t + h ) + ψ ( x − a, t + h ) − 2ψ ( x, t + h ) = ψ( x, t).
2ma2
And taking the average of these two, we get the Crank–Nicolson equation for
the Schrödinger equation:
ih̄
ψ( x, t + h) − h ψ ( x + a, t + h ) + ψ ( x − a, t + h ) − 2ψ ( x, t + h )
4ma2
ih̄
= ψ( x, t) + h ψ ( x + a, t ) + ψ ( x − a, t ) − 2ψ ( x, t ) .
4ma2
This gives us a set of simultaneous equations, one for each grid point.
4
The boundary conditions on our problem tell us that ψ = 0 at x = 0 and
x = L for all t. In between these points we have grid points at a, 2a, 3a, and so
forth. Let us arrange the values of ψ at these interior points into a vector
ψ( a, t)
ψ(2a, t)
ψ(t) = .
ψ(3a, t)
..
.
Then the Crank–Nicolson equations can be written in the form
Aψ(t + h) = Bψ(t),
with
ih̄ ih̄ ih̄ ih̄
a1 = 1 + h , a2 = − h , b1 = 1 − h , b2 = h .
2ma2 4ma2 2ma2 4ma2
(Note the different signs and the factors of 2 and 4 in the denominators.)
The equation Aψ(t + h) = Bψ(t) has precisely the form Ax = v of simul-
taneous equations, where A is a tridiagonal matrix. Use the fast tridiagonal
version of Gaussian elimination to solve for ψ( x, t + h) for a given ψ( x, t).
Consider an electron (mass M = 9.109 × 10−31 kg) in a box of length L =
10−8 m. Suppose that at time t = 0 the wavefunction of the electron has the
form
( x − x0 )2 iκx
ψ( x, 0) = exp − e ,
2σ2
where
L
x0 = , σ = 1 × 10−10 m, κ = 5 × 1010 m−1 ,
2
and ψ = 0 on the walls at x = 0 and x = L. (This expression for ψ( x, 0) is not
normalized—there should really be an overall multiplying coefficient to make
sure that the probability density for the electron integrates to unity. It’s safe
5
to drop the constant, however, because the Schrödinger equation is linear, so
the constant cancels out on both sides of the equation and plays no part in the
solution.)
vi = b1 ψi + b2 (ψi+1 + ψi−1 ).
You will also have to choose a value for the time-step h. A reasonable
choice is h = 10−18 s.
Second you will have to solve the linear system Ax = v for x, which
gives you the new value of ψ. You could do this using a standard lin-
ear equation solver like the function solve in numpy.linalg, but since the
matrix A is tridiagonal a better approach would be to use the fast solver
for banded matrices. Note that although the wavefunction of a particle
in principle has a complex value, in this case the wavefunction is always
real—all the coefficients in the equations above are real numbers so if, as
here, the wavefunction starts off real, then it remains real. Thus you do
not need to use a complex array to represent the vector ψ. A real one will
do the job.
Third, once you have the code in place to perform a single step of the cal-
culation, extend your program to perform repeated steps and hence solve
for ψ at a sequence of times a separation h apart. Note that the matrix A
is independent of time, so it doesn’t change from one step to another. You
can set up the matrix just once and then keep on reusing it for every step.
b) Extend your program to make an animation of the solution by displaying
the real part of the wavefunction at each time-step.
c) Run your animation for a while and describe what you see. Write a few
sentences explaining in physics terms what is going on in the system.
6
Exercise 6: The Schrödinger equation and the spectral method
This exercise uses the spectral method to solve the time-dependent Schödinger
equation
h̄2 ∂2 ψ ∂ψ
− = ih̄
2M ∂x2 ∂t
for the same system as in Exercise 5, a single particle in one dimension in a box
of length L with impenetrable walls. The wavefunction in such a box necessar-
ily goes to zero on the walls and hence one possible (unnormalized) solution of
the equation is
πkx
ψk ( x, t) = sin eiEt/h̄ ,
L
where the energy E can be found by substituting into the Schrödinger equation,
giving
π 2 h̄2 k2
E= .
2ML2
As with the vibrating string of Section 9.3.4, we can write a full solution as
a linear combination of such individual solutions, which on the grid points
xn = nL/N takes the value
1 N −1
2 2
πkn π h̄k
ψ( xn , t) = ∑
N k =1
bk sin
N
exp i
2ML2
t ,
where the bk are some set of (possibly complex) coefficients that specify the
exact shape of the wavefunction and the leading factor of 1/N is optional but
convenient.
Since the Schrödinger equation (unlike the wave equation) is first order in
time, we need only a single initial condition on the value of ψ( x, t) to specify
the coefficients bk , although, since the coefficients are in general complex, we
will need to calculate both real and imaginary parts of each coefficient.
Again, we consider an electron (mass M = 9.109 × 10−31 kg) in a box of
length L = 10−8 m. At time t = 0 the wavefunction of the electron has the form
( x − x0 )2 iκx
ψ( x, 0) = exp − e ,
2σ2
where
L
x0 = , σ = 1 × 10−10 m, κ = 5 × 1010 m−1 ,
2
and ψ = 0 on the walls at x = 0 and x = L.
7
a) Write a program to calculate the values of the coefficients bk , which for
convenience can be broken down into their real and imaginary parts as
bk = αk + iηk . Divide the box into N = 1000 slices and create two arrays
containing the real and imaginary parts of ψ( xn , 0) at each grid point. Per-
form discrete sine transforms on each array separately and hence calculate
the values of the αk and ηk for all k = 1 . . . N − 1.
To perform the discrete sine transforms, you can use the fast transform
function dst from the package dcst, which you can find in the on-line
resources in the file named dcst.py. The function takes an array of N
real numbers and returns the discrete sine transform as another array of N
numbers.
(Note that the first element of the input array should in principle always
be zero for a sine transform, but if it is not the dst function will simply pre-
tend that it is. Similarly the first element of the returned array is always
zero, since the k = 0 coefficient of a sine transform is always zero. So in
effect, the sine transform really only takes N − 1 real numbers and trans-
forms them into another N − 1 real numbers. In some implementations of
the discrete sine transform, therefore, though not the one in the package
dsct used here, the first element of each array is simply omitted, since it’s
always zero anyway, and the arrays are only N − 1 elements long.)
b) Putting bk = αk + iηk in the solution above and taking the real part we get
1 N −1
2 2 2 2
π h̄k π h̄k πkn
N k∑
Re ψ( xn , t) = αk cos 2
t − ηk sin 2
t sin
=1
2ML 2ML N
for the real part of the wavefunction. This is an inverse sine transform
with coefficients equal to the quantities in the square brackets. Extend
your program to calculate the real part of the wavefunction ψ( x, t) at an
arbitrary time t using this formula and the inverse discrete sine transform
function idst, also from the package dcst. Test your program by making
a graph of the wavefunction at time t = 10−16 s.
c) Extend your program further to make an animation of the wavefunction
over time, similar to that described in part (b) of Exercise 5 above. A suit-
able time interval for each frame of the animation is about 10−18 s.
d) Run your animation for a while and describe what you see. Write a few
sentences explaining in physics terms what is going on in the system.
8
Exercise 7: Almost tridiagonal system
Write a program to solve a tridiagonal system of equations Ax = d, where A is
a tridiagonal matrix,
b1 c1
a2 b2 c2
a3 b3 c3
A= ... .
a n − 2 bn − 2 c n − 2
a n − 1 bn − 1 c n − 1
an bn
∂f ∂2 f
= D 2, (1)
∂t ∂x
given by
where
b1 c1 ... a1
a2 b2 c2
a3 b3 c3
A= ... ;
a n − 2 bn − 2 c n − 2
a n − 1 bn − 1 c n − 1
cn ... an bn
f n+1 and f n are vectors at times tn+1 and tn respectively; bi = 1 + 2D∆t/∆x2 ,
ai = − D∆t/∆x2 , and ci = − D∆t/∆x2 . Notice that we have used periodic
boundary conditions; this results in appearance of a1 and cn in corners apart
from the tridiagonal structure. Thomas method can be modified to solve Eq. 3
9
using the Sherman-Morrison formula. Sherman-Morrison formula states that
the solution of the matrix equation
( A + uvT ) x = d, (4)
where u and v are column vectors (v T is the transpose of v), is given by solving
Ay = d, Aq = u,
and computing x = y − q(v T y)/(1 + v T q). To solve Eq. 2 you will need to
choose column vectors u and v appropriately and apply the tridiagonal method
twice. Hence the solution is O (n), where n is the number of grid points.
Write a code to solve the diffusion equation (Eq. 1) using periodic boundary
conditions with the method discussed above and the tridiagonal code that you
wrote. Use D = 1 and a domain going from 0 to 1. The initial condition is
f = 1 for 0.4 < x < 0.6; outside this f vanishes. Find the solution ( f ) at
time t = 0.025; choose the timestep ∆t = 4∆x2 /D. Use 256 grid points such
that ∆x = 1/256. See how the solution at t = 1 converges with increasing
resolution (try n = 128, 256, 512, 1024). Plot L1 Richardson error as a function
of ∆x on log-log scale. What is is order of convergence, as deduced from the
slope of this plot? L1 Richardson error is defined as
1 n n
E1 = ∑ | f i − f i2n |,
n i =1
where f in is the numerical solution obtained at grid point i using n grid points
and f i2n is the numerical solution obtained at the same point but using double
the number of grid points.
10
where sqr[ a, b] is a square wave for a ≤ x ≤ b; i.e., it is zero everywhere except
when x lies between a and b, where it is 1. Plot f ( x, t) at t = 0, 1, 2, 3. Compare
the results with the following methods (use ∆t = 0.5∆x/v; use 128 grid points):
upwind, Lax, and Lax-Wendroff. Which method is the best and why? Try
combining Lax-Wendroff with artificial viscosity (discussed later) and see if
the oscillations go away. Perform convergence analysis for all these methods at
t = 1 (use 32, 64, 128, 256, 512 grid points) and plot L1 error (you can either use
the analytic results or use Richardson error; i.e., treating the higher resolution
result as the true solution) as a function of resolution (∆x). What’s the order of
convergence?
The Burger’s equation, a model for supersonic flows, is given by
∂u ∂u
+ u = 0, (7)
∂t ∂x
where u is the fluid velocity. Its better to numerically evolve the conservative
form,
∂ u2
∂u
+ = 0. (8)
∂t ∂x 2
Use periodic boundary conditions, 0 ≤ x ≤ 1, and with the initial condition
given by
u( x, t = 0) = sin(2πx ). (9)
Show results at t = 0, 0.1, 0.25, 0.5 using Lax, upwind, Lax-Wendroff methods;
use ∆t = 0.5∆x and resolution of 128 grid-points. You will see that the Lax-
Wendroff method becomes unstable once a shock forms. Lax-Wendroff method
can be stabilized by using artificial viscosity which is implemented as follows.
Instead of solving Eq. 8, we solve
∂ u2
∂u ∂q
+ =− , (10)
∂t ∂x 2 ∂x
where q is the (artificial) viscous flux given by
2
∂u ∂u
q = l2 when < 0,
∂x ∂x
0, otherwise, (11)
where l is a length scale over which a shock is smoothened. While both the
transport and diffusion terms in Eq. 10 can be applied in a single step with both
11
terms treated explicitly (forward in time), remember that the diffusion term has
a restrictive stability limit on ∆t. Thus it is better to use operator splitting and
applying the transport and viscous diffusion steps independently. Since the
main timestep (∆t = 0.5∆x) can be much shorter than the viscous timestep
(∆tvis = 0.5∆x2 /(l 2 max[∂u/∂x ]) ≈ 0.25∆x/(qcon max[u]), where qcon=l/∆x),
we have to subcycle the artificial viscosity step; i.e., apply artificial viscosity
for multiple (nsub) times with a smaller timestep (∆tsub ) such that ∆tsub ≤ ∆tvis
and nsub × ∆tsub = 0.5∆x (the main timestep). What value of qcon is able to
make the scheme stable? Plot the results with this method at different times.
Which scheme seems to work best?
Iterate Eq. 14 and make contourplots of the potential (Φ) at 10th , 100th and
1000th iteration on a 40 × 40 grid. Plot L1 error (using consecutive iterations)
as a function of number of iterations. From these contourplots can you see
12
from that the large k (small scale) modes converge quickly but the large scale
modes require many iterations to converge. This can be seen by realizing that
Jacobi method is equivalent to solving the diffusion equation explicitly with a
stable timestep; thus diffusion at large scales takes many timesteps to capture
as compared to small scales.
Try to solve the same problem with Gauss-Seidel relaxation (its only very
slightly different from Jacobi in that the Φ values are updated right away); it
corresponds to the matrix decomposition (see class-slides and NR for details)
13