Computational Physics Course Description
Computational Physics Course Description
Course Description
The use of computers in physics, as well as most other branches of science and engineering, has increased
many times along with the rapid development of faster and cheaper hardware. This course aims to give the
student a thorough grounding in the main computational techniques used in modern physics. It is particu-
larly important in this course that the students should learn by doing. The course is therefore designed such
that a significant fraction of the students’ time is spent actually programming specific physical problems
rather than learning abstract techniques.
The course will cover problems in 5 broad sections:
• Ordinary differential equations, such as those of classical mechanics.
• Partial differential equations, such as Maxwell’s equations and the Diffusion and Schrödinger equa-
tions.
• Matrix methods, such as systems of equations and eigenvalue problems applied to Poisson’s equation
and electronic structure calculations.
• Monte Carlo and other simulation methods, such as the Metropolis algorithm and molecular dynam-
ics.
• Computer Algebra; an introduction using Mathematica to the uses and abuses of algebraic computing
in physics.
This is not a short course in computing science, nor in programming. It focuses specifically on methods
for solving physics problems. Students will be expected to be familiar with basic programming: successful
completion of the 1st year computing Lab. is sufficient. There is no requirement that the practical work be
done using Microsoft C++ on the departmental computers, but anyone wishing to use some other program-
ming language or computer should consult the lecturer beforehand. This is to make sure there is both help
available from demonstrators and that it will be possible to assess the work satisfactorily.
For most weeks of the term there will be 2 timetabled sessions in the computing Lab., room 319, on
Tuesday afternoons, with a lecture at 9.00am on Thursdays in the lecture theatre, LT3. From 2.00pm to
5.00pm on Tuesdays there will be demonstrators available in the Lab. During this period the Lab. facilities
will be reserved for those taking part in this course.
On the 10th October and again on the 17th there will be a Lecture at 2.00pm and the Lab session will
start at 3.00pm.
Each of the 5 sections of the course will be accompanied by a short problem sheet including practical
exercises and longer projects.
Students will be expected to do the first project and 2 of the others.
During this session we will be experimenting with electronic submission of programs. This will en-
able the assessors to check and run your program if necessary. The program(s) and any output files,
which would appear as appendices in the report should still be included in your report but, in addition,
should be sent as plain text e-mail attachment(s)1 to Computational-Physics, A. MacKinnon
1 Using Outlook click Insert then File
1
or ph.compphys@ic.ac.uk by the stated deadline. Do not send it to my personal e-mail address. The
first project is to be handed in to Martin Morris in the Computing Lab by 5.00pm on the 23rd October.
These will be assessed and returned by the demonstrators who should provide the students with feedback
which will be of use in the longer projects later on. This project will count for ≈ 10% of the final result.
During the Lab. sessions staff and demonstrators will be available in room 319, but the students are
expected to do the practical work and the problems outside these times as well. I am aware that there is an
Instrumentation lecture scheduled for 3.00pm on Tuesdays. Nevertheless the Lab. will be reserved for this
course for the whole afternoon and demonstrators will be available during this period.
The report on the 2nd project, which should be chosen from the 2 offered on Partial Differential Equa-
tions, should be submitted by 5.00pm on November 20th.
The 3rd project may be chosen from those offered on Matrix and Monte Carlo methods or on Computer
Algebra and the report should be submitted by 5.00pm on Monday December 18th. This is after the last
day of term, so you might like to set your own deadline earlier than this.
This timetable has been designed to make sure that students are working at a steady rate during the
term.
On the Thursday afternoon of the first week of the 2nd term, 11th January 2001, there will be a short
written test under exam conditions. This will count for ≈ 30% of the final result. The 2nd and 3rd projects
will also count for ≈ 30%.
Thus the distribution of marks for the various parts of the course will be approximately
Project 1 10
Project 2 30
Project 3 30
Test 30
There will be no examination in the summer.
Further information is available on the World Wide Web under
http://www.sst.ph.ic.ac.uk/angus/Lectures/compphys/.
Computational Physics
Timetable of Lectures and Projects
Test
There will be a test under examination conditions on Thursday January 11th 2001 from 4.00pm - 5.00pm.
For those with surnames beginning with A–K the test will be in lecture theatre 2 and for those with sur-
names beginning with L–Z in lecture theatre 3.
3
Computational Physics
Ordinary Differential Equations
d2 y dy
m +η + κy = 0 (1.2)
dt2 dt
can be rewritten in terms of y and velocity v = dy/dt in the form of a pair of 1st order ODEs
dv η κ
+ v + y= 0 (1.3a)
dt m m
dy
−v =0 (1.3b)
dt
Similarly any nth order differential equation can be reduced to n 1st order equations.
Such systems of ODEs can be written in a very concise notation by defining a vector, y say, whose
elements are the unknowns, such as y and v in (1.3). Any ODE in n unknowns can then be written in the
general form
dy
+ f(y, t) = 0 (1.4)
dt
where y and f are n–component vectors.
4
Ordinary Differential Equations 5
Remember that there is no significance in the use of the letter t in the above equations. The variable is
not necessarily time but could just as easily be space, as in (1.6), or some other physical quantity.
Formally we can write the solution of (1.4) as
Z t
y(t) = y(t0 ) − f (y(t0 ), t0 ) dt0 (1.5)
t0
by integrating both sides over the interval t0 → t. Although (1.5) is formally correct, in practice it is
usually impossible to evaluate the integral on the right–hand–side as it presupposes the solution y(t). We
will have to employ an approximation.
All differential equations require boundary conditions. Here we will consider cases in which all the
boundary conditions are defined at a particular value of t (e.g. t = 0). For higher order equations the
boundary conditions may be defined at different values of t. The modes of a violin string at frequency ω
obey the equation
d2 y ω2
= − y (1.6)
dx2 c2
with boundary conditions such that y = 0 at both ends of the string. We shall consider such problems in
chapter 2.10.3.
to obtain
y(tn+1 ) = y(tn ) − δt f(y(tn ), tn ).
or, in a more concise notation,
We can integrate over any larger interval by subdividing the range into sections of width δt and repeating
(1.8) for each part.
Equivalently we can consider that we have approximated the derivative with a forward difference
dy y − yn
≈ n+1 . (1.9)
dt n δt
δt2 d2 y
dy
yn+1 = yn + δt + + ... (1.11)
dt n 2 dt2 n
δt2 d2 y
dy
yn + δt + + . . . ≈ yn − δtf (yn , tn ) (1.12a)
dt n 2 dt2 n
dy
= yn + δt , (1.12b)
dt n
where we have used (1.4) to obtain the final form. Hence, we see that the term in δt in the expansion has
been correctly reproduced by the approximation, but that the higher order terms are wrong. We therefore
describe the Euler method as 1st order accurate.
An approximation to a quantity is nth order accurate if the term in δtn in the Taylor expansion of the
quantity is correctly reproduced. The order of accuracy of a method is the order of accuracy with which
the unknown is approximated.
Note that the term accuracy has a slightly different meaning in this context from that which you might
use to describe the results of an experiment. Sometimes the term order of accuracy is used to avoid any
ambiguity.
The leading order deviation is called the truncation error. Thus in (1.12) the truncation error is the term
in δt2 .
1.2.2 Stability
The Euler method is 1st order accurate. However there is another important consideration in analysing the
method: stability. Let us suppose that at some time the actual numerical solution deviates from the true
solution of the difference equation (1.8) (N.B. not the original differential equation (1.4)) by some small
amount δy, due, for example, to the finite accuracy of the computer. Then adding this into (1.8) gives
∂f
yn+1 + δyn+1 = yn + δyn − δt f (yn , tn ) + δy n , (1.13)
∂y n
where the term in brackets, [], is the Taylor expansion of f (y, t) with respect to y. Subtracting (1.8) we
obtain a linear equation for δy
∂f
δyn+1 = 1 − δt δyn , (1.14)
∂y n
which it is convenient to write in the form
If g has a magnitude greater than one then δyn will tend to grow with increasing n and may eventually
dominate over the required solution. Hence the Euler method is stable only if |g| ≤ 1 or
∂f
−1 ≤ 1 − δt ≤ +1. (1.16)
∂y
As δt is positive by definition the 2nd inequality implies that the derivative must also be positive. The 1st
inequality leads to a restriction on δt, namely
∂f
δt ≤ 2/ . (1.17)
∂y
Ordinary Differential Equations 7
When the derivative is complex more care is required in the calculation of |g|. In this case it is easier to
look for solutions of the condition |g|2 ≤ 1. For the oscillation equation (1.1c) the condition becomes
1 + δt2 ω 2 ≤ 1 (1.18)
which is impossible to fulfil for real δt and ω. Comparing these result with our 3 types of differential
equations (1.1) we find the following stability conditions
δt2 d2 y δt3 d3 y
dy
yn + δt + + + ...
dt n 2 dt2 n 6 dt3 n
δt2 d2 y δt3 d3 y
dy
= yn − δt + − + . . . − 2δtfn (1.24a)
dt n 2 dt2 n 6 dt3 n
δt2 d2 y δt3 d3 y
dy
= yn + δt + − + ... (1.24b)
dt n 2 dt2 n 6 dt3 n
from which all terms up to δt2 cancel so that the method is clearly 2nd order accurate. Note in passing
that using (1.23) 2 consecutive values of yn are required in order to calculate the next one: yn and yn−1
are required to calculate yn+1 . Hence 2 boundary conditions are required, even though (1.23) was derived
from a 1st order differential equation. This so–called leap–frog method is more accurate than the Euler
method, but is it stable? Repeating the same analysis (section 1.2.2) as for the Euler method we again
obtain a linear equation for δyn
∂f
δyn+1 = δyn−1 − 2δt δyn . (1.25)
∂y n
We analyse this equation by writing δyn = gδyn−1 and δyn+1 = g 2 δyn−1 to obtain
2 ∂f
g = 1 − 2δt g (1.26)
∂y n
This intrinsic method is 2nd order accurate as that is the accuracy of the trapezoidal rule for integration.
What about the stability? Applying the same methodology as before we find that the crucial quantity, g, is
the expression in square brackets, [], in (1.34) which is always < 1 for the decay equation and has modulus
unity in the oscillatory case (after substituting α 7→ ±iω). Hence it is stable in both cases. Why is it not
used instead of the other methods? Unfortunately only a small group of equations, such as our examples,
can be rearranged in this way. For non–linear equations it may be impossible, and even for linear equations
when y is a vector, there may be a formal solution which is not useful in practice. It is always possible
to solve the resulting non–linear equation iteratively, using (e.g. ) Newton–Raphson iteration, but this is
usually not worthwhile in practice.
In fact the intrinsic method is also stable for the growth equation when it is analysed as discussed earlier
(section 1.2.3), so that the method is in fact stable for all 3 classes of equations.
1.7 Summary
In these notes we have introduced some methods for solving ordinary differential equations. However, by
far the most important lesson to be learned is that to be useful a method must be both accurate and stable.
The latter condition is often the most difficult to fulfil and careful analysis of the equations to be solved
may be necessary before choosing an appropriate method to use for finding a numerical solution.
The stability conditions derived above tend to have the form δt ≤ ω −1 which may be interpreted as δt
should be less than the characteristic period of oscillation. This conforms with common sense. In fact we
can write down a more general common sense condition: δt should be small compared with the smallest
time scale present in the problem.
Finally, many realistic problems don’t fall into the neat categories of (1.1). The simplest example is
a damped harmonic oscillator. Often it is difficult to find an exact analytical solution for the stability
condition. It pays in such cases to consider some extreme conditions, such as (e.g. ) very weak damping
or very strong damping, work out the conditions for these cases and simply choose the most stringent
condition. In non–linear problems the cases when the unknown, y, is very large or very small may provide
tractable solutions.
1.8 Problems
1. Write down definitions of the terms order of accuracy, truncation error, conditional stability as
applied to the numerical solution of ordinary differential equations.
2. Write down 1st order accurate finite difference approximations for
df d2 f d3 f ∂ ∂f
a) b) c) d)
dx dx2 dx3 ∂t ∂x
Hint: the result has to be something like ayn+1 + byn + cyn−1 . Expand the ys around yn and choose
the coefficients to eliminate contributions from unwanted terms in the expansion.
3. Derive expressions for the truncation error of the following difference approximations.
df fn+1 − fn df fn+1 − fn−1
a) = b) =
dx n δx dx n 2δx
d2 f fn+1 − 2fn + fn−1 d3 f fn+2 − 2fn+1 + 2fn−1 − fn−2
c) = d) =
dx2 n δx2 dx3 n 2δx3
1
fn+2 + 23 fn+1 − 23 fn−1 + 1
df − 12 12 fn−2
e) =
dx n δx
Ordinary Differential Equations 11
d4 θ
+ τ θ = 0.
dx4
Show how to re–express this as a system of first order differential equations.
dy γ
+ =0
dt y
by Euler’s method and show under what conditions the method is stable.
Write and test a short program (it should only require a few lines) to test the method. Vary δt, γ and
y(t = 0) to check the validity of the stability condition you have derived.
6. Using equation 1.22 show that the Euler method is stable for a vector equation provided all the
eigenvalues of G have modulus less than or equal to unity.
7. Show that equation 1.30 gives the correct stability condition for both the Runge–Kutta and Predictor–
Corrector methods. Why do you think this is a good method for damped oscillatory equations? (The
last part doesn’t have to be mathematically rigorous).
dv
m = qv × B − γv (1.35)
dt
where m and q are the mass and charge of the particle respectively, B is the magnetic field and γ represents
some sort of friction.
Units
Note firstly that there are only 2 independent constants in the problem, qB/m and γ/m, and that these
constants have the units of inverse time; in fact the former is the cyclotron frequency and the latter is a
damping rate. In general in any programming problem it pays to think carefully about the units to be used
in the program. There are several reasons for this.
• If inappropriate units are used the program may not work at all. An example of this would be the
use of SI units to study the dynamics of galaxies or to study atomic physics. In the former R3 might
easily arise and be bigger than the largest number representable on the machine, whereas in the latter
h̄4 may be smaller than the smallest number on the machine and be set automatically to zero with
disastrous consequences.
• The problem often has its own natural units and it makes sense to work in these units. This has the
consequence that most of the numbers in your program will be of order unity rather than very large
or very small.
Ordinary Differential Equations 12
In general you should look for the natural units of a problem and write your program appropriately. Note
that these will generally not be SI or cgs.
In the problem we are considering here there are 2 natural time scales, m/qB and m/γ. If we decide
to work in one of these, e.g. the cyclotron period m/qB, we can rewrite (1.35) in the simpler form
dvx γ
= +vy −
vx (1.36a)
dt0 qB
dvy γ
0
= −vx − vy (1.36b)
dt qB
or perhaps
dvx γ
= −vy −
vx (1.37a)
dt0 qB
dvy γ
= +vx −
vy (1.37b)
dt0 qB
depending on the sign of qB/m. Here t = t0 |m/qB| and we have chosen our coordinate system such that
the magnetic field, B, is in the z–direction. Note, in addition, that choosing the units appropriately has
eliminated all but one of the constants from the problem. This cuts down on superfluous arithmetic in the
program.
which can be easily solved using the integrating factor method to give
γ
qB
ṽ = ṽ0 exp −i t− t . (1.39)
m m
Finally we take real and imaginary parts to find the vx and vy components
qB h γ i
vx = +v0 cos t + φ0 exp − t (1.40a)
m m
qB h γ i
vy = −v0 sin t + φ0 exp − t (1.40b)
m m
Choosing an Algorithm
The problem as posed does not fit neatly into the categories defined in (1.1). By considering the accuracy
and stability properties of the various algorithms described in these notes you have to decide which is the
most appropriate algorithm to use for solving the problem. The computer time required by the algorithm as
well as the ease with which it can be programmed may be legitimate considerations. It may not be possible
to solve the the stability equations exactly in the most general case. Nevertheless you should be able to
deduce enough information on which to base a decision.
In your report you should discuss the merits and demerits of the various possible algorithms and explain
the rationale behind your choice. You should also write a program to implement your chosen algorithm
and test it for various values of δt and in all the physically significant regimes.
Ordinary Differential Equations 13
You should now write a program to solve (1.41) using the most appropriate method as found in sec-
tion 1.9.1. Try to investigate the behaviour of the system in various physical regimes. You should also
vary δt to check whether the stability conforms to your expectations. Think about the physical system you
are describing and whether your results are consistent with the behaviour you would expect.
Ex = E0 cos ωt (1.42)
We shall consider each of these cases separately as different methods are required for each.
14
Partial Differential Equations 15
∂2y ∂2y
2
− c2 2 = 0. (2.3)
∂t ∂x
The wave equation can be rewritten in the form
∂ ∂ ∂ ∂
+c −c y=0 (2.4)
∂t ∂x ∂t ∂x
or as a system of 2 equations
∂z ∂z
+c =0 (2.5a)
∂t ∂x
∂y ∂y
−c = z. (2.5b)
∂t ∂x
Note that the first of these equations (2.5a) is independent of y and can be solved on it’s own. The second
equation (2.5b) can then be solved by using the known solution of the first. Note that we could equally
have chosen the equations the other way round, with the signs of the velocity c interchanged.
As the 2 equations (2.5) are so similar we expect the stability properties to be the same. We therefore
concentrate on (2.5a) which is known as the Advective equation and is in fact the conservation of mass
equation of an incompressible fluid (section 2.6.1)
∂u ∂u
+c = 0. (2.6)
∂t ∂x
Note also that the boundary conditions will usually be specified in the form
whereas
3 n
n
∂u 3 ∂ u
unj+1 − unj−1 ≈ 2δx 1
+ 3 δx + ... (2.10)
∂x j ∂x3 j
and substitute these forms into (2.8) to obtain
2 n 3 n
n n
∂u 1 2 ∂ u cδt ∂u 1 3 ∂ u
δt + 2 δt + ... ≈ 2δx + 3 δx + ... (2.11)
∂t j ∂t2 j 2δx ∂x j ∂x3 j
so that, when the original differential equation (2.6) is subtracted, we are left with a truncation error which
is 2nd order in the time but 3rd order in the spatial part.
Partial Differential Equations 16
The stability is a property of the time, t, integration rather than the space, x. We analyse this by
considering a plane wave solution for the x–dependence by substituting unj = v n exp(ikxj ) to obtain
cδt n ikxj+1
v n+1 eikxj = v n eikxj − − eikxj−1
v e (2.12)
2δx
or, after dividing out the common exponential factor,
cδt
v n+1 = 1 − i sin(kδx) v n . (2.13)
δx
Since the wave (2.3) and advection (2.6) equations express a conservation law (section 2.6) the solution
should neither grow nor decay as a function of time. If we substitute v n 7→ v n + δv n and subtract (2.13)
we obtain an equation for δv n
n+1 cδt
δv = 1−i sin(kδx) δv n (2.14)
δx
which is identical to (2.13). Hence the stability condition is simply given by the quantity in square brackets.
We call this the amplification factor and write it as
g = 1 − iα (2.15)
where
cδt
α= sin(kδx). (2.16)
δx
As g is complex the stability condition becomes
2
|g| = 1 + α2 ≤ 1 for all k . (2.17)
The condition must be fulfilled for all wave vectors k; otherwise a component with a particular k will tend
to grow at the expense of the others. This is known as the von Neumann stability condition2 . Unfortunately
it is never fulfilled for the simple method applied to the advection equation: i.e. the method is unstable for
the advection equation.
so that
2
2 cδt
|g| = cos2 (kδx) + sin2 (kδx) (2.20a)
δx
( 2 )
2 cδt
= 1 − sin (kδx) 1 − (2.20b)
δx
2 von Neumann is perhaps better known for his work on the theory of computing, but his name is also associated with various other
∂u ∂2u
−κ 2 =0 (2.22)
∂t ∂x
and discretise it using the Euler method for the time derivative and the simplest centred 2nd order derivative
to obtain
κδt
un+1 = unj + 2 unj+1 − 2unj + unj−1 .
j (2.23)
δx
Applying the von Neumann analysis to this system by considering a single Fourier mode in x space, we
obtain
4κδt 2 kδx
v n+1 = v n 1 − sin (2.24)
δx2 2
so that the condition that the method is stable for all k gives
1 δx2
δt ≤ 2 . (2.25)
κ
Although the method is in fact conditionally stable the condition (2.25) hides an uncomfortable property:
namely, that if we want to improve accuracy and allow for smaller wavelengths by halving δx we must
divide δt by 4. Hence, the number of space steps is doubled and the number of time steps is quadrupled:
the time required is multiplied by 8. Note that this is different from the sorts of conditions we have
encountered up to now, in that it doesn’t depend on any real physical time scale of the problem.
2κδt n
un+1 = un−1 uj+1 − un+1 + un−1 + unj−1 .
j j + j j (2.26)
δx2
which can be solved explicitly for un+1
j at each mesh point
1−α α
un+1 ujn−1 unj+1 + unj−1
j = + (2.27)
1+α 1+α
where
κδt
α=2 . (2.28)
δx2
When the usual von Neumann analysis is applied to this method it is found to be unconditionally stable.
Note however that this does not imply that δx and δt can be made indefinitely large; common sense tells
us that they must be small compared to any real physical time or length scales in the problem. We must
still worry about the accuracy of the method. Another difficulty this method shares with the Leap–Frog
(section 1.3) method is that it requires boundary conditions at 2 times rather than one, even though the
original diffusion equation is only 1st order in time.
Partial Differential Equations 19
2.7 Dispersion
Let us return to the Lax method (section 2.3.2) for hyperbolic equations. The solution of the differential
equation has the form
u(x, t) = u0 ei(ωt−kx) (2.36)
where ω = ck. Let us substitute (2.36) into the Lax algorithm
i(ωtn+1 −kxj ) 1 i(ωtn −kxj ) +ikδx −ikδx
cδt +ikδx −ikδx
ve = 2 ve e +e − e −e . (2.37)
δx
By cancelling the common factors we now obtain
cδt
eiωδt = cos(kδx) − i sin(kδx). (2.38)
δx
From this we can derive a dispersion relation, ω vs k, for the discrete equation and compare the result
with ω = ck for the original differential equation. Since, in general, ω could be complex we write it as
ω = Ω + iγ and compare real and imaginary parts on both sides of the equation to obtain
we compare lattice vibrations with classical elastic waves: the long wavelength sound waves are OK but
the shorter wavelengths deviate.
The second equation (2.40b) describes the damping of the modes. Again for small kδx and γδt, γ = 0,
but (e.g.) short wavelengths, λ = 4δx, are strongly damped. This may be a desirable property as short
wavelength oscillations may be spurious. After all we should have chosen δx to be small compared with any
expected features. Nevertheless with this particular algorithm λ = 2δx is not damped. Other algorithms,
such as Lax–Wendroff, have been designed specifically to damp anything with a kδx > 14 .
2.8 Problems
1. Explain the difference between hyperbolic, parabolic and elliptic partial differential equations, and
give an example of each. What is the important physical distinction between hyperbolic and parabolic
equations, on the one hand, and elliptic equations on the other?
2. Describe the von Neumann procedure for analysing the stability of partial differential equations.
3. Describe the physical principle behind the Courant–Friedrichs–Lewy condition as applied to the
numerical solution of partial differential equations.
4. Are the following equations hyperbolic, elliptic or parabolic?
∂2f ∂2f ∂2f
a) 3 + 2 − =0
∂t2 ∂x2 ∂t∂x
∂f ∂2f ∂2f ∂2f
b) 7 +3 2 − −2 2 =0
∂x ∂t ∂t∂x ∂x
∂2f ∂2f ∂2f ∂f
c) 3 2 + 2 +6 + = ex
∂x ∂t ∂t∂x ∂t
∂2f ∂2f ∂2f ∂f ∂f
d) 2
+ 2
− 2 + +2 =0
∂t ∂x ∂t∂x ∂x ∂t
∂ ∂f ∂f ∂ ∂f ∂f
e) +3 −f + 2 − = sin(x + t3 )
∂t ∂t ∂x ∂x ∂t ∂x
5. The equation
∂f ∂3f
=β 3
∂t ∂x
can be represented by the difference equation
(m) (m) (m)
βδt
fn(m+1) = fn(m) + µ fn+2 − 3fn+1 + 3fn(m) − fn−1 µ= .
δx3
Derive the truncation error of this difference equation.
Write down an alternative difference equation which is 2nd order accurate in both δt and δx.
6. The Dufort–Frankel (2.26) scheme is a method for the solution of the diffusion equation.
Show that the method is unconditionally stable.
Discuss the advantages and disadvantages of this method.
7. The diffusion equation in a medium where the diffusion constant D varies in space (D = D(x)) is
∂2f
∂f ∂ ∂f ∂f ∂D ∂f
= D or =D 2 + .
∂t ∂x ∂x ∂t ∂x ∂x ∂x
Show that the difference equation
(m) (m) (m) (m) (m)
!
(m+1) (m)
− 2fn + fn−1 fn+1 − fn−1
fn − fn f Dn+1 − Dn−1
= Dn n+1 +
δt δx2 2δx 2δx
Lagrangian Fluid 22
R
is not conservative, i.e. f dx is not conserved.
Construct an alternative difference scheme which is conservative.
8. Show that the Lax (2.18) scheme for the solution of the advection equation is equivalent to
2 2
∂f ∂f δx ∂ f
= −u + − 12 u2 δt + higher order terms.
∂t ∂x 2δt ∂x2
Examine the behaviour of wave–like solutions f = exp(i(kx − ωt)) in the Lax scheme and explain
the behaviour in terms of diffusion.
9. Describe what is meant by numerical dispersion.
10. Lax–Wendroff method consists of 2 steps, just like Runge–Kutta or Predictor–Corrector. It is given
by
n+1/2 δt n
uj+1/2 = 21 unj+1 + unj − 12 c uj+1 − unj
(2.41)
δx
δt
n+1/2 n+1/2
un+1
j = unj − c u − uj−1/2 (2.42)
δx j+1/2
Draw a diagram to illustrate the way this algorithm operates on an (x, t) grid.
Show that the algorithm is stable provided the Courant–Friedrichs–Lewy condition is obeyed.
Show that the algorithm tends to dampen waves with wavelength λ = 2δx.
where,
Pn(m) = constant × (ρ(m)
n )
γ
. (2.44)
and,
(m) (m)
ρ(m)
n = ∆m/(xn+ 1 − xn− 1 ) , (2.45)
2 2
(m) (m) (m) (m)
xn+ 1 and un+ 1 are the positions and velocities of the cell boundaries. ρn and Pn are the densities
2 2
and pressures in the cells. ∆m is the mass in each cell.
It is useful for the purpose of programming to redefine things to get rid of the various half integer
indices. Hence we can write the equations as
(m)
un0(m+1) = u0(m)
n + (Pn(m) − Pn+1 )δt/∆m (2.46a)
x0(m+1)
n = x0(m)
n + u0(m+1)
n δt (2.46b)
0(m) 0(m)
ρ(m)
n = ∆m/(xn − xn−1 ) , (2.46c)
which maps more easily onto the arrays in a program.
Lagrangian Fluid 23
u0(0)
n = M c sin(2πn/N + πcδt/L) (2.47a)
0(0)
xn = nL/N + (M L/2π) cos(2πn/N ) , (2.47b)
where M , the amplitude of the sound wave, is the ratio of u to the sound speed (i.e. the Mach number). Be
careful to choose sensible values for the parameters: e.g. the values of xn should rise monotonically with
n, otherwise some cells will have negative fluid densities.
The essential physics of the problem is independent of the absolute values of the equilibrium pressure
and density so you can set L = 1, ∆m = (1/N ) and P = ργ . In addition you can assume that γ =
Cp /Cv = 53 .
Note that the scheme is leap-frog but not of the dangerous variety: alternate steps solve for u and x.
2.9.5 An Improvement?
Consider the operation:
1
yn 7→ 4 (yn+1 + 2yn + yn−1 ) (2.48)
By applying the von Neumann trick you should be able to deduce the effect of such an operation. Now try
applying the transformation (2.48) to to x and u after a few iterations. How does the behaviour of your
simulation change?
2.10.2 Discretisation
The simplest discretisation of (2.49), based on the Euler method, gives the equation
2 2 o
1 δt 1 δt
n
(n+1) (n) (n) (n) (n) (n) (n) (n)
yj = yj − 4 yj+1 − yj−1 + 2 3 yj+2 − 2yj+1 + 2yj−1 − yj−2 . (2.51)
δx δx
You should check that this is indeed a sensible discretisation of (2.49) but that it is unstable. Note that
when analysing the non–linear term in (2.51) you should make the substitution y 7→ y + δy and retain the
linear terms in δy. Thereafter you should treat y as a constant, independent of x and t, and apply the von
Neumann method to δy.
If you find the full stability analysis difficult you might consider the 2 limits of large and small y. In the
former case the 3rd derivative is negligible and (2.49) reduces to a non–linear advection equation, whereas
in the latter the non–linear term is negligible and the equation is similar to the diffusion equation but with
a 3rd derivative. In any case you will require to choose δt so that the equation is stable in both limits.
You are free to choose any method you wish to solve the equation but you will find the Runge–Kutta
or Predictor–Corrector methods most reliable. Hence treating yj as a long vector y and the terms on the
right–hand–side of (2.51) as a vector function f (y) the R–K method can be written concisely as
where δt f (y) is the quantity in square brackets [] in (2.51). Check that this method is stable, at least in the
2 limiting cases. Bear in mind that the Runge–Kutta method is usable for oscillatory equations in spite of
the small instability as long as the term (ωδt)4 is small.
By studying the analytical solution (2.50) you should be able to choose a sensible value for δx in terms
of α and from the stability conditions you can deduce an appropriate δt. Again, by looking at (2.50) you
should be able to decide on a sensible size for the total system.
You should use periodic boundary conditions, so that your solitons can run around your system several
times if necessary. The easiest way to do this is by using “ghost” elements at each end of your arrays.
Suppose your arrays should run from y1 to yN . Then you can add a couple of extra elements to each end:
y−1 , y0 , yN +1 , yN +2 . After each step you can then assign these values as
y−1 = yN −1 , y 0 = yN , yN +1 = y1 , yN +2 = y2 (2.53)
so that the derivative terms in (2.51) can be calculated without having to take any special measures.
Solitons 25
2.10.3 Physics
You should first check your program by studying the single soliton case. Use (2.50) at t = 0 as your initial
condition and watch how the soliton develops. Check that after it has gone round the system once it retains
its shape.
Now you can try 2 solitons of different sizes. The initial conditions should be the simple sum of 2
solitons well separated from one another. Watch what happens when the solitons meet.
In your report you should discuss the behaviour of the solitons as well as the properties of the method
chosen to solve the equations.
Computational Physics
Matrix Algebra
3.1 Introduction
Nearly every scientific problem which is solvable on a computer can be represented by matrices. However
the ease of solution can depend crucially on the types of matrices involved. There are 3 main classes of
problems which we might want to solve:
1. Trivial Algebraic Manipulation such as addition, A + B or multiplication, AB, of matrices.
2. Systems of equations: Ax = b where A and b are known and we have to find x. This also includes
the case of finding the inverse, A−1 . The standard example of such a problem is Poisson’s equation
(section 3.4).
3. Eigenvalue Problems: Ax = αx. This also includes the generalised eigenvalue problem: Ax = αBx.
Here we will consider the time–independent Schrödinger equation.
In most cases there is no point in writing your own routine to solve such problems. There are many
computer libraries, such as Numerical Algorithms Group (n.d.), Lapack Numerical Library (n.d.) (for
linear algebra problems and eigenvalue problems). which contain well tried routines. In addition vendors
of machines with specialised architectures often provide libraries of such routines as part of the basic
software.
• Hermitian Matrices: Many Hamiltonians have this property especially those containing magnetic
fields: Aji = A†ij where at least some elements are complex.
• Real Symmetric Matrices: These are the commonest matrices in physics as most Hamiltonians can
be represented this way: Aji = Aij and all Aij are real. This is a special case of Hermitian matrices.
• Positive Definite Matrices: A special sort of Hermitian matrix in which all the eigenvalues are
positive. The overlap matrices used in tight–binding electronic structure calculations are like this.
Sometimes matrices are non–negative definite and zero eigenvalues are also allowed. An example is
the dynamical matrix describing vibrations of the atoms of a molecule or crystal, where ω 2 ≥ 0.
• Unitary Matrices: The product of the matrix and its Hermitian conjugate is a unit matrix, U† U = I.
A matrix whose columns are the eigenvectors of a Hermitian matrix is unitary; the unitarity is a
consequence of the orthogonality of the eigenvectors. A scattering (S) matrix is unitary; in this case
a consequence of current conservation.
26
Matrix Algebra 27
• Diagonal Matrices: All matrix elements are zero except the diagonal elements, Aij = 0 when
i 6= j. The matrix of eigenvalues of a matrix has this form. Finding the eigenvalues is equivalent to
diagonalisation.
• Tridiagonal Matrices: All matrix elements are zero except the diagonal and first off diagonal el-
ements, Ai,i 6= 0, Ai,i±1 6= 0. Such matrices often occur in 1 dimensional problems and at an
intermediate stage in the process of diagonalisation.
• Upper and Lower Triangular Matrices: In Upper Triangular Matrices all the matrix elements
below the diagonal are zero, Aij = 0 for i > j. A Lower Triangular Matrix is the other way round,
Aij = 0 for i < j. These occur at an intermediate stage in solving systems of equations and inverting
matrices.
• Sparse Matrices: Matrices in which most of the elements are zero according to some pattern. In
general sparsity is only useful if the number of non–zero matrix elements of an N × N matrix is
proportional to N rather than N 2 . In this case it may be possible to write a function which will
multiply a given vector x by the matrix A to give Ax without ever having to store all the elements of
A. Such matrices commonly occur as a result of simple discretisation of partial differential equations
(see chapter 1.9.4), and in simple models to describe many physical phenomena.
• General Matrices: Any matrix which doesn’t fit into any of the above categories, especially non–
square matrices.
Such matrices describe systems involving spin–orbit coupling. All eigenvalues are doubly
degenerate (Kramers degeneracy).
accurate enough.
Matrix Algebra 28
This may seem trivial but it maintains the convention that all the terms on the left contain unknowns and
everything on the right is known. It also allows us to rewrite the (3.3) in matrix form as
δx2 · f1 − V0
−2 1 V1
1 −2 1 V2 δx2 · f2
δx2 · f3
1 −2 1 V3
. .
. .. . .
.. .. ..
..
= 2
(3.5)
1 −2 1 Vn
δx · fn
.. .. .. . .
.. ..
. . .
1 −2 1 VN −1 δx2 · fN −1
1 −2 VN δx2 · fN − VN +1
which is a simple matrix equation of the form
Ax = b (3.6)
in which A is tridiagonal. Such equations can be solved by methods which we shall consider in section 3.5.
For the moment it should suffice to note that the tridiagonal form can be solved particularly efficiently and
that functions for this purpose can be found in most libraries of numerical functions.
There are several points which are worthy of note.
• We could only write the equation in this matrix form because the boundary conditions allowed us to
eliminate a term from the 1st and last lines.
• Periodic boundary conditions, such as V (x + L) = V (x) can be implemented, but they have the
effect of adding a non–zero element to the top right and bottom left of the matrix, A1N and AN 1 , so
that the tridiagonal form is lost.
• It is sometimes more efficient to solve Poisson’s or Laplace’s equation using Fast Fourier Transfor-
mation (FFT). Again there are efficient library routines available (Numerical Algorithms Group n.d.).
This is especially true in machines with vector processors.
The 2 dimensional index pairs {m, n} may be mapped on to one dimension for the purpose of setting up
the matrix. A common choice is so–called dictionary order,
(1, 1), (1, 2), . . . (1, N ), (2, 1), (2, 2), . . . (2, N ), . . . (N, 1), (N, 2), . . . (N, N )
Alternatively Fourier transformation can be used either for all dimensions or to reduce the problem to
tridiagonal form.
AX = B (3.8)
where A is an M × M matrix and X and B are M × N matrices. Matrix inversion is simply the special
case where B is an M × M unit matrix.
A = LU (3.9)
where L and U are lower and upper triangular matrices respectively, followed by a function which performs
the operations
Y = L−1 B (3.10a)
X = U−1 Y (3.10b)
on each column of B.
The procedure is sometimes described as Gaussian Elimination. A common variation on this proce-
dure is partial pivoting. This is a trick to avoid numerical instability in the Gaussian Elimination (or LU
decomposition) by sorting the rows of A to avoid dividing by small numbers.
There are several aspects of this procedure which should be noted:
• The LU decomposition is usually done in place, so that the matrix A is overwritten by the matrices
L and U.
• The matrix B is often overwritten by the solution X.
• A well written LU decomposition routine should be able to spot when A is singular and return a flag
to tell you so.
• Often the LU decomposition routine will also return the determinant of A.
• Conventionally the lower triangular matrix L is defined such that all its diagonal elements are unity.
This is what makes it possible to replace A with both L and U.
• Some libraries provide separate routines for the Gaussian Elimination and the Back–Substitution
steps. Often the Back–Substitution must be run separately for each column of B.
• Routines are provided for a wide range of different types of matrices. The symmetry of the matrix is
not often used however.
The time taken to solve N equations in N unknowns is generally proportional to N 3 for the Gaussian–
Elimination (LU–decomposition) step. The Back–Substitution step goes as N 2 for each column of B. As
before this may be modified by parallelism.
Matrix Algebra 31
Note that only one array is now required to store X, whereas the Jacobi method needed 2.
The time required for each iteration is proportional to N for each column of B, assuming A is sparse.
The number of iterations required depends on the details of the problem, on the quality of the initial guess
for X, and on the accuracy of the required solution.
The basis functions are usually chosen for convenience and as some approximate analytical solution of the
problem. Thus in chemistry it is common to choose the φβ to be known atomic orbitals. In solid state
physics often plane waves are chosen.
Inserting (3.17) into (3.14) gives
X X
aβ −∇2 + V (r) φβ (r) = E
aβ φβ (r). (3.18)
β β
Ax = αx (3.22)
where A is a square matrix x is an eigenvector and α is an eigenvalue. Sometimes the eigenvalue and
eigenvector are called latent root and latent vector respectively. An N × N matrix usually has N distinct
eigenvalue/eigenvector pairs2 . The full solution of the eigenvalue problem can then be written in the form
AUr = Ur α (3.23a)
Ul A = αUl (3.23b)
where α is a diagonal matrix of eigenvalues and Ur (Ul ) are matrices whose columns (rows) are the corre-
sponding eigenvectors. Ul and Ur are the left and right handed eigenvectors respectively, and Ul = U−1
r .
3
• For Hermitian matrices Ul and Ur are unitary and are therefore Hermitian transposes of each other:
U†l = Ur .
• For Real Symmetric matrices Ul and Ur are also real. Real unitary matrices are sometimes called
orthogonal.
Ax = αBx. (3.24)
This can easily be transformed into a simple eigenvalue problem by multiplying both sides by the inverse
of either A or B. This has the disadvantage however that if both matrices are Hermitian B−1 A is not, and
the advantages of the symmetry are lost, together, possibly, with some important physics.
2 For an exception consider the matrix [2 i / i 0].
3 Occasionally, Ur may be singular. In such cases the N vectors do not span an N dimensional space (e.g. 2 are parallel)
Matrix Algebra 34
There is actually a more efficient way of handling the transformation. Using Cholesky factorisation an
LU decomposition of a positive definite matrix can be carried out such that
B = LL† (3.25)
which can be interpreted as a sort of square root of B. Using this we can transform the problem into the
form
−1 h i h i
L A L†
−1
L† x = α L† x (3.26a)
A0 y = αy. (3.26b)
Most libraries contain routines for solving the generalised eigenvalue problem for Hermitian and Real
Symmetric matrices using Cholesky Factorisation followed by a standard routine. Problem 6 contains a
simple and informative example.
Sturm Sequence
The Sturm sequence is a very nice algorithm found in most libraries. It finds all the eigenvalues in a
given range, αmin < α < αmax , and the corresponding eigenvectors. It is also able to find the number of
such eigenvalues very quickly and will return a message if insufficient storage has been allocated for the
eigenvectors. It does require a tridiagonalisation beforehand and is often combined with the Lanczos (see
section 3.6.6) algorithm, to deal with sparse matrices.
3.7 Problems
1. Poisson’s equation ∇2 φ = −ρ/0 is usually differenced in 2 dimensions as
2. The equation in question 1 can be solved by the Gauss–Seidel method. If S11 = 1 and Sij = 0
(1) (2) (0) (n)
otherwise, find φij and φij if φij = 0 for all i and j, where φij is the value of φ after n iterations.
3. Work out the factors, L and U, in the LU decomposition of the matrix,
1 3 5
A = 2 5 3 .
1 1 4
Hence,
• Solve the simultaneous equations,
Ax = b,
for a variety of right hand sides, b.
• Evaluate det(A)
• Find A−1
4. Show that the Jacobi method (3.12) is stable as long as, µ, the eigenvalue of largest modulus of
D−1 (L + U) is less than unity.
5. Find both eigenvalues and the corresponding left and right handed eigenvectors of the matrix
−1
.
1 0
6. The vibrational modes of a certain molecule are described by an equation of motion in the form
mi ω 2 xi = Kij xj ,
where mi and xi are the mass and the displacement respectively of the ith atom and the real sym-
metric matrix K describes the interactions between the atoms.
Show that this problem can be represented in the form of a generalised eigenvalue problem: Ax =
λBx in which the matrix B is positive definite.
By considering the transformation x0i = (mi )1/2 xi , show how to transform this into a simple eigen-
value problem in which the matrix is real symmetric.
7. Write down the types of matrices which occur in the following problems:
(a) A simple discretisation (as in question1) of Poisson’s equation in 1 dimension.
(b) The same but in more than 1D.
(c) A simple discretisation (as above) of Schrödinger’s equation in 3 dimensions.
(d) Schrödinger’s equation for a molecule written in terms of atomic basis functions.
(e) Schrödinger’s equation for a crystal at a general k point in the Brillouin zone.
3.8.1 Analysis
The difference equations for the oscillating wire can be derived by dividing it into N segments (10 or 20
should be sufficient) each of which can be considered rigid. This would be the case if the wire consisted of
a series of rods connected together.
Let xn be the displacement of the bottom of the nth segment. Let θn be the angle it makes with the
vertical. Let Tn be the tension in the nth segment. Assume that the mass of the wire is located at the joints
of the segments, ∆m at each joint. ∆m = ρδz where δz is the length of each segment, and ρ is the mass
per unit length of the wire. The equation of motion for the mass at the bottom of the nth segment is,
d2 xn
∆m = Tn+1 sin(θn+1 ) − Tn sin(θn ) . (3.27)
dt2
Assume small oscillations so that sin(θn ) = (xn − xn−1 )/δz.
d2 xn 1
∆m = [Tn+1 xn+1 − (Tn+1 + Tn )xn + Tn xn−1 ] . (3.28)
dt2 δz
The mass associated with the end of the wire will be only ∆m/2 since there is no contribution from the
(N+1)th segment. Consequently, the equation of motion for this point is,
d2 xN
(M + 21 ∆m) = −TN sin(θN ) = −TN (xN − xN −1 )/δz , (3.29)
dt2
where M is any mass carried by the crane. In addition the displacement of the top of the wire is zero, so
that x0 = 0 in the equation for n = 1.
The modes of oscillation are calculated by seeking solutions of the form xn (t) = yn exp(iωt). Substi-
tuting this into the equations of motion gives,
−∆mω 2 yn = (Tn+1 yn+1 − (Tn + Tn+1 )yn + Tn yn−1 ) /δz (3.30a)
−(M + 12 ∆m)ω 2 yN = TN (yN −1 − yN )/δz . (3.30b)
The specification of the equations is completed by noting that, from the equilibrium conditions,
TN = (M + 12 ∆m)g , Tn = Tn+1 + g∆m , (3.31)
where g is the acceleration due to gravity.
The equations can be organised in the form,
Ay = −Mω 2 y , (3.32)
where y is the column vector of displacements, (y1 , y2 , . . . , yN ), A is a symmetric tridiagonal matrix and
M is a diagonal matrix. The problem becomes one of finding the eigenvalues, −ω 2 , and eigenvectors, y, of
a generalised eigenvalue problem. The eigenvectors show how the wire distorts when oscillating in each
mode and the eigenvalues give the corresponding oscillation frequencies. Low frequency modes are more
important than high frequency modes to the crane manufacturer.
The problem can be solved most easily by using a LaPack routine which finds the eigenvalues and
eigenvectors directly. However, before doing so it is necessary to eliminate the matrix M using the same
method as discussed in problem 6.
You should investigate both the computational aspects, such as the dependence of the results on δz, as
well as the physical ones, such as the dependence of the behaviour on the mass, M . Do your results make
physical sense? You might even compare them with a simple experiment involving a weight on the end of
a string.
and amorphous solids, in which there is no long range order, although there is some correlation between the
positions of nearby atoms. It was also known that it was impossible for a crystal to have five fold rotational
symmetry, since this is incompatible with translational order.
This was how things stood until 1984, when Shechtman et al. (Phys. Rev. Lett. 53 1951 (1984)), were
measuring the X ray diffraction pattern of an alloy of Al and Mn and got a sharp pattern with clear five
fold symmetry. The sharpness of the pattern meant that there had to be long range order, but the five fold
symmetry meant that the solid could not be crystalline. Shechtman called the material a “quasicrystal”.
One possible explanation (although this has still not been conclusively established) is that quasicrystals
are three dimensional analogues of Penrose tilings (Scientific American, January 1977 — Penrose tilings
were known as a mathematical curiosity before quasicrystals were discovered). Penrose found that you
could put together two (or more) different shapes in certain well defined ways so that they “tiled” the plane
perfectly, but with a pattern that never repeated itself. Sure enough, some of these tilings do have five fold
symmetries; and sure enough, there is perfect long range order (although no translational symmetry) so
that the diffraction pattern from a Penrose lattice would be sharp.
C (3.33)
C 7→ A , A 7→ AC , (3.34)
to obtain longer and longer sequences. The first few sequences generated are,
Note the interesting property that each generation is the “sum” of the 2 previous generations: ACAAC =
ACA ⊕ AC
In a one dimensional Fibonacci quasicrystal, the longs and shorts could represent the interatomic dis-
tances; or the strengths of the bonds between the atoms; or which of two different types of atom is at that
position in the chain.
The equations (3.36) are N linear algebraic equations which may be cast as a tridiagonal matrix eigen-
problem. The eigenvalues of this problem are ω 2 and so give the vibrational frequencies, and the eigenvec-
tors give the corresponding normal mode coordinates, xn , n = 1, N .
Note, however, that the presence of the masses mn in (3.36) means that the problem is in the generalised
form Ax = ω 2 Bx. As discussed in the notes and in problem 6 this can be transformed into the normal
−1/2 −1/2
eigenvalue problem by making the substitution xn = mn yn and multiplying the nth equation by mn .
I suggest you solve the eigenproblem by using a NAG or LaPack routine for the eigenvalues only. There
should be no problem in dealing with chains of several hundred atoms or more.
39
Monte Carlo Methods and Simulation 40
must be true for all x, as the probability of finding any y 0 < y must be the same as that for finding any
x0 < x. It follows that
dx
p(y) = p0 (x) . (4.4)
dy
If x is uniformly distributed between 0 and 1, i.e. p0 (x) = 1, then
dx
p(y) = = exp(−y) → y = − ln x. (4.5)
dy
Hence, a Poisson distribution is generated by taking the logarithm of numbers drawn from a uniform
distribution.
which is just a generalisation of the well known results for (e.g. ) hxi or x2 , where we are using the
notation <> to denote averaging. Now consider an integral of the sort which might arise while using
Laplace transforms. Z ∞
exp(−x)f (x)dx. (4.7)
0
This integral can be evaluated by generating a set of N random numbers, {x1 , . . . , xN }, from a Poisson
distribution, p(x) = exp(−x), and calculating the mean of f (x) as
N
1 X
f (xi ). (4.8)
N i=1
The error in this mean is evaluated as usual by considering the corresponding standard error of the
mean
1
2 2
σ2 = f (x) − hf (x)i . (4.9)
N −1
As mentioned earlier, Monte–Carlo integration can be particularly efficient in the case of multi–dimensional
integrals. However this case is particularly susceptible to the flaws in random number generators. It is a
common feature that when a random set of coordinates in a d–dimensional space is generated (i.e. a set
of d random numbers), the resulting distribution contains (hyper)planes on which the probability is either
significantly higher or lower than expected.
yi e−βEi
P
hyi = Pi −βEi (4.10)
ie
Monte Carlo Methods and Simulation 41
where Ei is the energy of the system in state i and β = 1/kB T . Such problems can be solved using the
Metropolis et al. (1953) algorithm.
Let us suppose the system is initially in a particular state i and we change it to another state j. The
detailed balance condition demands that in equilibrium the flow from i to j must be balanced by the flow
from j to i. This can be expressed as
pi Ti→j = pj Tj→i (4.11)
where pi is the probability of finding the system in state i and Ti→j is the probability (or rate) that a system
in state i will make a transition to state j. (4.11) can be rearranged to read
Ti→j pj
= (4.12a)
Tj→i pi
= e−β(Ej −Ei ) . (4.12b)
Generally the right–hand–side of (4.12) is known and we want to generate a set of states which obey the
distribution pi . This can be achieved by choosing the transition rates such that
1 if pj > pi or Ej < Ei
Ti→j = pj −β(Ej −Ei ) . (4.13)
or e if pj < pi or Ej > Ei
pi
In practice if pj < pi a random number, r, is chosen between 0 and 1 and the system is moved to state j
only if r is less than pj /pi or e−β(Ej −Ei ) .
(4.13) is not the only way in which the condition (4.12) can be fulfilled, but it is by far the most
commonly used.
An important feature of the procedure is that it is never necessary to evaluate the partition function, the
denominator in (4.10) but only the relative probabilities of the different states. This is usually much easier
to achieve as it only requires the calculation of the change of energy from one state to another.
Note that, although we have derived the algorithm in the context of thermodynamics, its use is by no
means confined to that case. See for example section 4.4.
where J is a positive energy, S = ± 12 , and i and j are nearest neighbours on a lattice. In this case we change
from one state to another by flipping a single spin and the change in energy is simply
X
∆Ei = −J Sj (4.15)
j
where the sum is only over the nearest neighbours of the flipped spin.
The simulation proceeds by choosing a spin (usually at random) and testing whether the energy would
be increased or decreased by flipping the spin. If it is decreased the rules (4.13) say that the spin should
definitely be flipped. If, on the other hand, the energy is increased, a uniform random number, r, between
0 and 1 is generated and compared with e−β∆E . If it is smaller the spin is flipped, otherwise the spin is
unchanged.
Further information can be found in section 4.7.
The sequence of random changes is often considered as a sort of time axis. In practice we think (e.g.
) about the time required to reach equilibrium. Sometimes, however, the transition rate becomes very low
and the system effectively gets stuck in a non–equilibrium state. This is often the case at low temperatures
when almost every change causes an increase in energy.
such that the quantity in braces ({}) has the form of a probability distribution. This integral can now easily
be evaluated by Monte–Carlo integration. Typically a sequence of r’s is generated using the Metropolis
(section 4.3) algorithm, so that it is not even necessary to normalise the trial wave function, and the quantity
in square brackets [] (4.17) is averaged over the r’s.
This method, variational quantum Monte–Carlo, presupposes we have a good guess for the wave func-
tion and want to evaluate an integral over it. It is only one of several different techniques which are referred
to as quantum Monte Carlo. Others include, Diffusion Monte–Carlo, Green’s function Monte–Carlo and
World Line Monte–Carlo.
In practice it is better to use the scalar force F (r) = ∂Φ/∂r to avoid unnecessary numerical differentiation.
Monte Carlo Methods and Simulation 43
A common feature of such problems is that the time derivative of one variable only involves the other
variable as with r and v in the above equations of motion (4.19). In such circumstances a leap–frog like
method suggests itself as the most appropriate. Hence we write
rni = rn−2
i + 2δtvn−1
i (4.20a)
2δt X rni − rnj
vn+1 = vn−1 F rni − rnj n
i i + . (4.20b)
m
j6=i
ri − rnj
This method has the advantage of simplicity as well as the merit of being properly conservative (sec-
tion 2.6).
The temperature is defined from the kinetic energy of N particles via
* +
3 1 X1 2
2 kB T = N 2 mi vi (4.21)
i
4.6 Problems
1. Show that (4.1) has the property that it generates all the integers from 1 to 10 in an apparently random
order if a = 7 and b = 11, and that the sequence repeats itself thereafter.
2. A certain statistical quantity is distributed according to
Given a function which generates random numbers uniformly distributed between 0 & 1, show how
to transform these into the distribution p(x).
3. Suggest a Monte–Carlo integration procedure for the integral
Z +∞
2 2 4 4
e−a x −b x dx
−∞
The Ising Model 44
4. Describe how you would use the Metropolis algorithm to generate a set of random numbers dis-
tributed according to the integrand of problem 3.
The following are essay type questions which are provided as examples of the sorts of questions which
might arise in an examination of Monte–Carlo and related methods.
5. In an ionic conductor, such as AgI, there are several places available on the lattice for each Ag
ion, and the ions can move relatively easily between these sites, subject to the Coulomb repulsion
between the ions.
Describe how you would use the Metropolis algorithm to simulate such a system.
6. Some quantum mechanics textbooks suggest that the ground state wave function for a Hydrogen
atom is
ψ(r) ∝ e−αr .
Describe how you would use the variational quantum Monte–Carlo procedure to calculate the ground
state energy for a range of values of α and hence how you would provide estimates of the true ground
state energy and the value of α.
7. Describe how you would simulate the melting (or sublimation) of Argon in vacuo using the molecular
dynamics method. Pay particular attention to how you would calculate the specific and latent heats.
where the i and j designate points on a lattice and S takes the values ± 12 . The various different physical
systems differ in the definition and sign of the various Jij ’s.
and this can only take one of five different values given by the number of neighbouring ↑ spins. Hence it is
sensible to store these in a short array before starting the calculation. Note also that there is really only 1
parameter in the model, J/kB T , so that it would make sense to write your program in terms of this single
parameter rather than J and T separately.
The Ising Model 45
The calculation should use periodic boundary conditions, in order to avoid spurious effects due to
boundaries. There are several different ways to achieve this. One of the most efficient is to think of the
system as a single line of spins wrapped round a torus. This way it is possible to avoid a lot of checking for
the boundary. For an N × N system of spins define an array of 2N 2 elements using the shortest sensible
variable type: char in C(++). It is easier to use 1 for spin ↑ and 0 for spin ↓, as this makes the calculation
of the number of neighbouring ↑ spins easier. In order to map between spins in a 2d space Sr and in the 1d
array Sk the following mapping can be used.
where the 2nd N 2 elements of the array are always maintained equal to the 1st N 2 . This way it is never
necessary to check whether one of the neighbours is over the edge. It is important to remember to change
Sk+N 2 whenever Sk is changed.
The calculation proceeds as follows:
1. Initialise the spins, either randomly or aligned.
2. Choose a spin to flip. It is better to choose a spin at random rather than systematically as systematic
choices can lead to spurious temperature gradients across the system.
3. Decide whether to flip the spin by using the Metropolis condition (see notes).
4. If the spin is to be flipped, do so but remember to flip its mirror in the array.
5. Update the energy and magnetisation.
6. Add the contributions to the required averages.
7. Return to step 2 and repeat.
It should be possible to calculate these as you go along, by accumulating the changes rather than by recal-
culating the complete sum after each step. A 10 × 10 lattice should suffice for most purposes and certainly
for testing, but you may require a much bigger lattice close to a transition.
A useful trick is to use the final state at one temperature as the initial state for the next slightly different
temperature. That way the system won’t need so long to reach equilibrium.
It should be possible to calculate the specific heat and the magnetic susceptibility. The specific heat
could be calculated by differentiating the energy with respect to temperature. This is a numerically ques-
tionable procedure however. Much better is to use the relationship
2
1 J
2 2
Cv ∝ E − hEi (4.30)
N kB T
Similarly, in the paramagnetic state, the susceptibility can be calculated using
1 J
2 2
χ∝ S − hSi (4.31)
N kB T
P
where S = i Si and the averages are over different states, i.e. can be calculated by averaging over the
different Metropolis steps. Both these quantities are expected to diverge at the transition, but the divergence
Quantum Monte Carlo Calculation 46
will tend to be rounded off due to the small size of the system. Note however that the fact that (4.30) &
(4.31) have the form of variances, and that these diverge at the transition, indicates that the average energy
and magnetisation will be subject to large fluctuations around the transition.
Finally a warning. A common error made in such calculations is to add a contribution to the averages
only when a spin is flipped. In fact this is wrong as the fact that it isn’t flipped means that the original state
has a higher probability of occupation.
where r1 and r2 are the position vectors of the two electrons, r1 = |r1 |, r2 = |r2 |, and r12 = |r1 − r2 |.
Energies are in units of the Hartree energy (1 Hartree = 2 Rydbergs) and distances are in units of the
Bohr radius. The ground state spatial wavefunction is symmetric under exchange of the two electrons (the
required antisymmetry is taken care of by the spin part of the wavefunction, which we can forget about
otherwise).
The expression for the energy expectation value of a particular trial wavefunction, ΦT (r1 , r2 ), is,
. . . Φ∗T ĤΦT d3 r1 d3 r2
R R
ET = R . (4.33)
. . . Φ∗T ΦT d3 r1 d3 r2
R
In the variational Monte Carlo method, this equation is rewritten in the form,
Z Z
1
ET = . . . ĤΦT f (r1 , r2 ) d3 r1 d3 r2 , (4.34)
ΦT
where
Φ∗T (r1 , r2 )ΦT (r1 , r2 )
f (r1 , r2 ) = R (4.35)
. . . Φ∗T ΦT d3 r1 d3 r2
R
is interpreted as a probability density which is sampled using the Metropolis algorithm. Note that the
Metropolis algorithm only needs to know ratios of the probability density at different points, and so the
normalisation integral, Z Z
... Φ∗T ΦT d3 r1 d3 r2 , (4.36)
ground state energy if these are large enough. Anyway, the better the trial wavefunction, the closer to the
true ground state energy the variational estimate should be.
In this project you are given a possible trial wavefunction,
Use the variational Monte Carlo technique to calculate variational estimates of the true ground state energy.
Before you start programming, you will need the analytic expressions for the local energy. This involves
some nasty algebra, but the answer is,
1 17 r1 · r12 r2 · r12
ĤΦ = − − + . (4.39)
Φ 4 r1 r12 r2 r12
where r1 and r2 are the coordinates of the 2 atoms relative to the nucleus and r12 = r2 − r1 .
The Monte Carlo moves can be made by generating random numbers (use a library routine to do this)
and adding them to the electron coordinates. I suggest that you update all six electron position coordinates
(x1 , y1 , z1 , x2 , y2 , z2 ) each move, and so you will need six random numbers each time. The accepted
lore is that the Metropolis algorithm is most efficient when the step size is chosen to keep the acceptance
probability close to 0.5. However, the method should work in principle no matter what the step size and
you should try a few different step sizes to confirm that this is indeed the case. The starting positions of
the two electrons can be chosen randomly, but remember that the Metropolis algorithm only samples the
probability distribution exactly in the limit as the number of moves tends to infinity. You will therefore
have to throw away the results from the moves near the beginning of the run and only start accumulating
the values of the local energy once things have settled down. You should experiment to find out how many
moves you need to throw away. √
The statistical errors in Monte Carlo calculations decrease like 1/ N , where N is the total number
of moves after the initial equilibration period. The errors therefore improve only slowly as the length of
the run is increased. You will not be able (and should not attempt) to attain great accuracy. However, you
should think hard about the magnitude of the statistical errors involved. Calculating the variance of the
values in the list of energies accumulated during the random walk is easy and you should certainly do it.
5.1 Introduction
Algebraic or Symbolic computing is very different from the sort of numerical computing you have learned
so far. Algebraic computing systems have been available for a long time, almost as long as the older con-
ventional computer languages such as FORTRAN or ALGOL. Systems such as REDUCE or MACSYMA
were available on mainframes in the 1960’s as general purpose packages. There were also some specialist
packages available for various branches of physics and mathematics, such as the program FORM which is
widely used in theoretical particle physics (and is available free). With the development of PCs packages
such as muMATH became available and more recently MAPLE and MATHEMATICA. In these lectures
the examples will be drawn from MATHEMATICA but the discussion will be kept as general as possible.
The newer packages not only perform algebraic manipulations but have powerful built in numerical
analysis and plotting capabilities. Most systems can be used as interpreters, with the commands typed in
one by one with an instant (?) response, but many also include the possibility to define and run your own
macros or procedures, so that they have many of the attributes of a conventional compiler as well.
As many of these packages were designed with different specialised criteria in mind, some packages
are better for particular classes of problems than others.
As well as being given a permanent value variables can be given a temporary value
48
Computer Algebra 49
In[4]:= Clear[x]
In[5]:= y = (1 + x)ˆ2
Out[5]= (1 + x)2
In[6]:= y/. x -> 2
Out[6]= 9
In[7]:= y
Out[7]= (1 + x)2
Note the command Clear[x] which makes sure that x does not have a value.
Basic algebra can be done by using the functions Expand[] and Factor[] as follows
In[8]:= Expand[y]
Out[8]= 1 + 2x + x2
In[9]:= Factor[%]
Out[9]= (1 + x)2
Note the use of % as shorthand for the result of the last operation. We could also have written Fac-
tor[Out[8]] here.
There are several other commands available which do specific manipulations on such expressions, in
particular:
Differentiation and integration are carried out using the D[f,x] and Integrate[f,x] operators.
In[10]:= D[y,x]
Out[10]= 2(1 + x)
In[11]:= Integrate[%,x]
Out[11]= 2x + x2
In[12]:= Factor[%]
Out[12]= x(2 + x)
In[13]:= Integrate[y,x]
x3
Out[13]= x + x2 +
3
or, using special functions,
In[14]:= D[Sin[x],x]
Out[14]= Cos[x]
In[15]:= Integrate[%,x]
Out[15]= Sin[x]
In[16]:= x = Sin[z]
Out[16]= Sin[z]
In[17]:= y
Out[17]= (1 + Sin[z])2
In[18]:= D[y,z]
Out[18]= 2 Cos[z](1 + Sin[z])
In[19]:= Integrate[y,z]
3z Sin[2z]
Out[19]= − 2 Cos[z] −
2 4
Higher derivatives are written as
Computer Algebra 50
∂ ∂ ∂
• D[f, x, y, z,...] multiple derivative ∂x ∂y ∂z . . ..
∂n
• D[f, {x,n}] repeated derivative ∂xn .
where the last command evaluates the 2nd last (%%) result to 40 significant figures. This function can
also be used in other situations such as
In[23]:= N[Pi, 100]
Out[23]= 3.1415926535897932384626433832795028841971693993751
05820974944592307816406286208998628034825342117068
Mathematica has a number of built in constants (all beginning with capital letters) such as
In[24]:= Eˆ(I Pi)
Out[24]= −1
In[29]:= Solve[4x + 8 == 0, x]
Out[29]= {{x −> −2}}
In[30]:= Solve[xˆ2 + 2x - 7 == 0, x]
−2 − 4 Sqrt[2] −2 + 4 Sqrt[2]
Out[30]= {{x −> , x −> }}
2 2
In[31]:= N[%]
Out[31]= {x −> −3.82843, x −> 1.82843}}
In[32]:= Solve[a xˆ2 + b x + c == 0, x]
−b + Sqrt[b2 − 4ac] −b − Sqrt[b2 − 4ac]
Out[32]= {{x −> , x −> }}
2a 2a
where both numerical and symbolic solutions may be obtained. We can also find more complicated prob-
lems, such as
In[33]:= Solve[{xˆ2 + yˆ2 == 1, x + 3 y == 0}, {x, y}]
−3 1 3 1
Out[33]= {{x −> , y −> }, {x −> , y −> −( )}}
Sqrt[10] Sqrt[10] Sqrt[10] Sqrt[10]
In[34]:= x/.%
−3 3
Out[34]= {x −> , x −> }
Sqrt[10] Sqrt[10]
In[35]:= y/.%%
1 1
Out[35]= {y −> , y −> −( )}
Sqrt[10] Sqrt[10]
in which we should note the syntax for solving systems of equations by grouping things in braces, {}, and
for extracting parts of the solution. Note also how the different, distinct solutions are grouped together.
Unfortunately there are equations which Mathematica cannot solve so simply, but which with a little
help it could do so. One very useful command in this context is Eliminate, used as follows
In[36]:= Eliminate[{a x + y == 0, 2 x + (1 - a) y == 1}, y]
Out[36]= −(ax) + a2 x == 1 − 2x
In[37]:= Solve[%, x]
1
Out[37]= {{x −> }}
2 − a + a2
Often Mathematica is unable to solve a problem in a single step but can do so if the user guides it through
the steps. This is particularly the case when an equation may have unphysical solutions which can be
ignored. The user can recognise the physically important solutions and concentrate on those.
In[46]:= equ
Out[46]= −2 a2 a03 x + 2 a2 x − a3 a04 x2 − 2 a a0 a2 x2 + 3 a3 x2
−2 a2 a02 a2 x3 − 2 a a0 a3 x3 + 4 a4 x3 − a a22 x4
−2 a2 a02 a3 x4 − 2 a a0 a4 x4 − 2 a a2 a3 x5 − 2 a2 a02 a4 x5
−a a32 x6 − 2 a a2 a4 x6 − 2 a a3 a4 x7 − a a42 x8
In[47]:= Coefficient[equ, x]
Out[47]= −2 a2 a03 + 2 a2
In[48]:= Solve[%==0, a2]
Out[48]= {{a2 −> a2 a03 }}
In[49]:= a2 = a2/.%[[1]]
Out[49]= a2 a03
Thomas–Fermi 53
In[50]:= equ
Out[50]= −3 a3 a04 x2 + 3 a3 x2 − 2 a4 a05 x3 − 2 a a0 a3 x3
+4 a4 x3 − a5 a06 x4 − 2 a2 a02 a3 x4 − 2 a a0 a4 x4
−2 a3 a03 a3 x5 − 2 a2 a02 a4 x5 − a a32 x6 − 2 a3 a03 a4 x6
−2 a a3 a4 x7 − a a42 x8
In[51]:= Coefficient[equ, xˆ2]
Out[51]= −3 a3 a04 + 3 a3
In[52]:= Solve[%==0, a3]
Out[52]= {{a3 −> a3 a04 }}
In[53]:= a3 = a3/.%[[1]]
Out[53]= a3 a04
In[54]:= equ
Out[54]= −4 a4 a05 x3 + 4 a4 x3 − 3 a5 a06 x4 − 2 a a0 a4 x4
−2 a6 a07 x5 − 2 a2 a02 a4 x5 − a7 a08 x6 − 2 a3 a03 a4 x6
−2 a4 a04 a4 x7 − a a42 x8
In[55]:= Coefficient[equ, xˆ3]
Out[55]= −4 a4 a05 + 4 a4
In[56]:= Solve[%==0, a4]
Out[56]= {{a4 −> a4 a05 }}
In[57]:= a4 = a4/.%[[1]]
Out[57]= a4 a05
In[58]:= y
Out[58]= a0 + a a02 x + a2 a03 x2 + a3 a04 x3 + a4 a05 x4
In[59]:= Factor[%]
Out[59]= a0 (1 + a a0 x + a2 a02 x2 + a3 a03 x3 + a4 a04 x4 )
This looks like the first few terms of the series for
a0
y(x) = (5.2)
1 − a0 ax
which is indeed the solution of the differential equation (5.1. So we see that in this sort of bookkeeping
exercise the use of computer algebra can be of considerable assistance.
d2 V
∝ (EF − V )3/2 , (5.1)
dx2
which can be simplified into the form
d2 φ
= Aφ3/2 . (5.2)
dx2
Thomas–Fermi 54
This is a second order differential equation, so the general solution must contain 2 unknowns. One solution
is known, namely
400
φ= 2 , (5.3)
A (x − x0 )4
which contains the single unknown x0 .
The object of the exercise here is to find a more general solution, or at least something which may be
used as such (i.e. a series expansion).
By considering the limit of small z and the known solution (5.3) we see immediately that s = −2 and that
b0 = 20/A.
You should try to find some more coefficients in the expansion (5.6) using Mathematica. The technique
is similar to that described in the notes. You should bear in mind that we have already found one arbitrary
coefficient, x0 , so that your solution should contain at least one other such arbitrary coefficient. Some of
the coefficients may be zero; you should try to find a few non–trivial ones.
In your report you should describe how you have obtained the solution and any special insight you have
gained into the nature of the solution. Instead of a program as an appendix you might consider including a
printout of a Mathematica Notebook as well as some graphs of your solution.
Bibliography
55