Computational Physics 1 Home Page Title Page
Module PH3707 Contents
JJ II 10 Credits J I
Page1 of 85
Go Back by Roger Stewart Full Screen Version date: Monday, 7 July 2008 Close
Quit
opttoa Physics Computational opttoa Physics Computational
opttoa Physics Computational opttoa Physics Computational
Home Page
opttoa Physics Computational Physics Computational Title Page
Contents
opttoa Physics Computational Physics Computational
opttoa Physics– Computational JJ II Computational Physics–
opttoa Physics– Computational Computational Physics–
opttoa Physics– Computational Computational Physics–
opttoa Physics– Computational Computational Physics– J I
opttoa Physics Computational Physics– Computational Computational Physics– Computational Physics
opttoa Physics– Computational Computational Physics– Page2 of 85
opttoa Physics– Computational Computational Physics–
opttoa Physics– Computational Computational Physics– opttoa Physics– Computational Computational Physics– Go Back
Computational Physics Computational Physics Full Screen
Close Computational Physics Computational Physics
Quit Computational Physics Computational Physics ComputationalComputational Physics Physics Copyright c 2003 Dennis Dunn.
Home Page
Title Page
Contents
JJ II
J I
Page3 of 85
Go Back
Full Screen
Close
Quit Contents Home Page
Title Page
Contents Contents 4 JJ II INTRODUCTION TO THE MODULE5 J I 1 ANALYSIS OF WAVEFORMS 21 Page4 of 85 1.1 Objectives 22 Go Back 1.2 Fourier Analysis 23 1.2.1 Why bother? 24 Full Screen
1.2.2 Aperiodic functions 25 Close 1.3 The Numerical Methods 26 Quit 1.4 Diffusion Equation 29 1.4.1 Exercises 30
2 EIGENVALUES AND EIGENVECTORS OF MATRICES 33 2.1 Objectives 34 2.2 Eigenvalues and eigenvectors of real symmetric or hermitian matrices 35 2.3 A Matrix Eigenvalue Package 39 2.4 Schrodinger¨ Equation 42 2.4.1 Harmonic Oscillator 43 Home Page 2.4.2 Spherically Symmetric 3D Systems 44
Title Page 3 RANDOM PROCESSES 48
3.1 Objectives 49 Contents 3.2 Introduction 50 JJ II 3.3 Random Number Generators 51 J I 3.3.1 The basic algorithm 51 3.4 Intrinsic Subroutine 52 Page5 of 85
3.4.1 Different number ranges 53 Go Back 3.4.2 Testing your random generator 54 Full Screen 3.5 Monte Carlo Integration 56 Close 3.5.1 Hit and Miss Method 56
3.5.2 Sampling Method 56 Quit 3.6 Nuclear Decay Chains 58 3.6.1 Analytic approach 58 3.6.2 Computer simulation 60 3.7 Exercises 61 4 MONTE CARLO SIMULATION 65 4.1 OBJECTIVES 66 4.2 EQUILIBRIUM AND FLUCTUATIONS 67 4.3 MONTE CARLO SIMULATIONS – THE PRINCIPLES 70 Home Page 4.4 The Metropolis Monte Carlo Algorithm 71
4.5 The Ising Model 72 Title Page
4.6 The Ising Model and the Monte Carlo Algorithm 73 Contents 4.7 Rationale 74 JJ II 4.8 MONTE CARLO SIMULATIONS – IN ACTION 75 4.9 Order Parameter - Magnetisation 76 J I
4.10 Temperature Scan (Annealing and Quenching) 77 Page6 of 85 4.11 MATHEMATICAL APPENDIX 78 Go Back 4.11.1 Fluctuations 78 4.11.2 Metropolis Monte Carlo Algorithm and Principle of Detailed Balance 79 Full Screen
4.11.3 Susceptibility and Fluctuations 80 Close 4.12 EXERCISES 82 Quit Index 85 INTRODUCTION TO THE MODULE Home Page
Title Page
Contents Version date: Monday, 7 July 2008 JJ II
J I
Page7 of 85
Go Back
Full Screen
Close
Quit Introduction
In this module you will be taught techniques employed in computational science and, in particular, computational physics using the FORTRAN95 language. The unit consists of four ‘computer ex- periments’, each of which must be completed within a specified time. Each ‘computer experiment’ is described in a separate chapter of this manual and contains a series of exercises for you to com- Home Page plete. You should work alone and should keep a detailed record of the work in a logbook that must be submitted for assessment at the end of each experiment. Title Page For three of the four projects, there will be a supervised laboratory session each week and further Contents unsupervised sessions.
There will be no supervised sessions for the fourth project: These will be exercises in independent JJ II learning. The Salford Fortran95 compiler will be used in this course, and this may be started by double- J I clicking on the Plato icon under the “Programming - Salford Fortran 95” program group. A “FTN95 Page8 of 85 Help” facility is supplied with this software and can be found within the same program group. This help facility includes details of the standard FORTRAN95 commands as well as the compiler- Go Back specific graphics features. All of the programs needed during this course may be downloaded from the Part 3 - PH3707 Computational Physics page on the department’s web-server: Full Screen (www.rdg.ac.uk/physicsnet). Close
Quit Web Site Information
In addition to all the chapters and programs required for this course, there are links to other useful sites including a description of programming style; a description of computational science in gen- eral and FORTRAN programming in particular; a tutorial for FORTRAN90; and a description of object-oriented programming. Home Page
Title Page
Contents
JJ II
J I
Page9 of 85
Go Back
Full Screen
Close
Quit References
Programming in Fortran 90/95 By J S Morgan and J L Schonfelder
Published by N.A. Software 2002. 316 pages. Home Page
This can be ordered online from Title Page www.fortran.com $15 Contents Fortran 95 Handbook By Jeanne Adams, Walt Brainerd, Jeanne Martin, Brian Smith, and Jerry Wagener JJ II
Published by MIT Press, 1997. 710 pages. J I
$55.00 Page 10 of 85 Fortran 95/2003 Explained Go Back By Michael Metcalf and John Reid Full Screen Oxford University Press ISBN 0-19-8526293-8 $35.00 Close
Fortran 90 for Scientists and Engineers Quit By Brian Hahn Published by Arnold £19.99 Fortran 90/95 for Scientists and Engineers By Stephen J. Chapman McGraw-Hill, 1998 ISBN 0-07-011938-4 $68.00 Numerical Recipes in Fortran90 By William Press, Saul Teukolsky, William Vetterling, and Brian Flannery Published by Cambridge University Press, 1996. 550 pages. $49.00 Home Page A more complete list of reference texts is held at Title Page
http://www.fortran.com/fortran/Books/books.html Contents where books can be ordered directly. JJ II
J I
Page 11 of 85
Go Back
Full Screen
Close
Quit Logbooks
You must keep an accurate and complete record of the work in a logbook. The logbook is what is assessed. In the logbook you should answer all the questions asked in the text, include copies of the programs with explanations of how they work, and record details of the program inputs and of the output created by the programs. On completion of each chapter you should write a brief summary Home Page of what has been achieved in the project. Title Page I am often asked what should go in the logbook. It is difficult to give a precise answer to this since each computer experiment is different but as a guide: it should have Contents
JJ II • a complete record of your computer experiment; • sufficient detail for someone else to understand what you were doing and why; and J I • for someone else to be able to repeat the computer experiment. Page 12 of 85
In particular should also include: Go Back
Full Screen • program listings; • a description of any changes you made to the programs; Close If you have made a succession of changes, you should not reproduce the complete program each time but simply specify what changes you have made and why you have made them. Quit • the data entered and results obtained from your programs (or, if there are a very large number of results, a summary of these results); • a comment on each set of results; You will lose marks if you simply record masses of computer output without commenting on its significance. • descriptions of any new techniques you have learned. It worked!
A statement in your logbook of the form ”I tried the program and it worked” will not be looked on favorably.
• What inputs did you provide to the program? Home Page • What output did you obtain? • What evidence do you have that this output is correct? Title Page
Contents Program Testing JJ II It is always safe to assume that a program you have written is wrong in some way. If you have made some error with the programming language then the compiler will tell you about this: it may not J I always tell you precisely what is wrong! However it may be the case that what you have told the computer to do is not really what you Page 13 of 85 intended. Computers have no intelligence: You need to be very precise in your instructions. Go Back Every program should be tested. You do this by giving the program input data such that you know, or can can easily calculate, the result. If possible your method of calculation should be via a Full Screen different method than that which the computer is using: the method you have told the computer to use may be incorrect. Close Only when the program has passed your tests should you start to use it to give you new results. Quit Module Assessment
The module comprises 4 computational projects. A record of each project must be kept in a logbook and the logbook submitted for assessment by the specified deadline. The final assessment will based on the best 3 project marks. Each project will be marked out of 20. A detailed marking scheme is given, in this manual, for each Home Page project. Title Page Guidelines on the assessment are given below. Contents
JJ II
J I
Page 14 of 85
Go Back
Full Screen
Close
Quit Late Submissions
If a project is submitted up to one calendar week after the original deadline 2 marks will be de- ducted. I am prepared to mark any project, which is more than one week late, providing it is submitted by the last day of the of the Spring Term. However for such a project 4 marks will be deducted. Home Page
Title Page
Contents
JJ II
J I
Page 15 of 85
Go Back
Full Screen
Close
Quit Extensions & Extenuating Circumstances
If you have a valid reason for not being able to complete a project by the specified deadline then you should
• inform the lecturer as soon as possible; and Home Page • complete an Extension of Deadlines Form. The form can be obtained from the School Office (Physics 210). Title Page
If you believe that there has been some non-academic problem that you encountered during the Contents module (medical or family problems for example) you should complete an Extenuating Circum- stances Form, again obtainable from the School Office, so that the Director of Teaching & Learning JJ II and the Examiners can consider this. J I
Page 16 of 85
Go Back
Full Screen
Close
Quit Feedback
In addition to comments written in your logbook by the assessor during marking, feedback on the projects will be provided by a class discussion and, when appropriate, by individual discussion with the lecturer. There will be no feedback, apart from the mark, on late submissions. Home Page
Title Page
Contents
JJ II
J I
Page 17 of 85
Go Back
Full Screen
Close
Quit Assessment Guidelines
This module is assessed solely by continuous assessment. Each project (which corresponds to one chapter of the manual) is assessed as follows. The depth of understanding and level of achievement will be assessed taking into account the fol- lowing three categories: Home Page
1. Completion of the project (0 – 17 marks) Title Page Completeness of the record Contents Description and justification of all actions Following the documented instructions, answering questions and performing derivations etc. JJ II 2. Summary (0 – 3 marks) J I • Review of objectives • Summary of achievements Page 18 of 85 • Retrospective comments on the effectiveness of the exer- cises Go Back
3. Bonus for extra work (0 - 2 marks) Full Screen • Any exceptional computational work beyond the require- ments stated Close • An exceptional depth of analysis • An outstanding physical insight Quit
I should point out that bonus marks are only rarely awarded and, in any case, the total mark of a project cannot exceed 20. Unfinished work will be marked pro rata, unless there are extenuated circumstances. If you are unable to attend the laboratory session you should inform the lecturer Dr R J Stewart by telephone 0118 378 8536 or by email [email protected]
Home Page
Title Page
Contents
JJ II
J I
Page 19 of 85
Go Back
Full Screen
Close
Quit Plagiarism
In any learning process you should make use of whatever resources are available. In this course, I hope, the lecturer and postgraduate assistant should be valuable resources. Your fellow students may also be useful resources and I encourage you to discuss the projects with them. However at the end of these discussions you should then write your own program (or program Home Page modification). It is completely unacceptable to copy someone else’s program (or results). This is a form of cheating and will be dealt with as such. Title Page
I should point out that such copying (even if slightly disguised) is very easy to detect. Contents
JJ II
J I
Page 20 of 85
Go Back
Full Screen
Close
Quit Time Management
There is ample time for you to complete each of these projects providing you manage your time sensibly. You should aim to spend at least six hours per week on each project: that is a total of about 18 hours per project. Home Page
Each project is divided into a number of ’exercises’ and each of these exercises is allocated a mark. Title Page This mark is approximately proportional to the time that I expect you to spend on the exercise. You should therefore be able to allocate your time accordingly: It is not sensible to spend half of the Contents available time on an exercise which is worth only a quarter of the total marks. Each of the projects below is given a deadline. You should not take this as a target. You should set JJ II yourself a target well before the deadline. J I
Page 21 of 85
Go Back
Full Screen
Close
Quit Projects
• Random Processes [Deadline: noon, Wednesday Week 3 Autumn Term] • Analysis of Waveforms ; [Deadline: noon, Wednesday Week 6 Autumn Term] • Eigenvalues & Eigenvectors [Deadline: noon, Wednesday Week 9 Autumn Term] • Monte Carlo Simulation [Deadline: noon, Wednesday Week 2 Spring Term] Home Page The ’Monte Carlo Simulation’ project will be unsupervised is an exercise in independent learning. Title Page Nevertheless the lecturer and demonstrator will be available for consultation.
Contents
JJ II
J I
Page 22 of 85
Go Back
Full Screen
Close
Quit Chapter 1 Home Page
Title Page ANALYSIS OF WAVEFORMS Contents
JJ II Version Date: Wednesday, 5 September, 2007 at 13:05 J I
Page 23 of 85
Go Back
Full Screen
Close
Quit 1.1. Objectives
In this chapter the main elements of Fourier Analysis are reviewed and the methods are applied to some basic wave forms. On completion of this chapter students will have utilized simple numerical techniques for performing Fourier Analysis; studied the convergence of Fourier series and how this is effected by discontinuities in the function; and investigated the best choice of Fourier coefficients Home Page in finite series. Fourier techniques will be applied to the solution of the diffusion equation. Title Page
Contents
JJ II
J I
Page 24 of 85
Go Back
Full Screen
Close
Quit 1.2. Fourier Analysis
Fourier’s theorem states that any well-behaved (or physical) periodic wave form f(x) with period L may be expressed as the series
∞ Home Page X f (x) = Fr exp (ikrx) (1.1) r=−∞ Title Page where the wave-vector kr is given by Contents 2πr kr = (1.2) L JJ II
The complex Fourier coefficients Fr are given by J I
1 Z Page 25 of 85 = f (x) exp (−ik x) dx (1.3) Fr L r Go Back Here the integrals are over any complete period (e.g. x = 0 to x = L or x = −L to x = L ). 2 2 Full Screen r The rth component of the sum in (1.1) corresponds to a harmonic wave with spatial frequency /L L Close and hence a wavelength /r.
Normally, in Physics, f(x) is a real function. In this case the Fourier coefficients have the following Quit symmetry property:
∗ Fr = F−r (1.4) An important exception to this is the case of quantum mechanics where the wavefunctions are normally complex. The above symmetry does not apply to this case. In numerical work we can only deal with series with a finite number of terms. Suppose the finite series, f [M] (x), is used to approximate the function, f (x).
M [M] X f (x) = Fr exp (ikrx) (1.5) r=−M Home Page
The mean-square-error involved in making this approximation is by definition Title Page
L Contents 1 Z h i2 E[M] = f(x) − f [M](x) dx. (1.6) L 0 JJ II and this can be written (after quite a bit of manipulation!) as J I
Page 26 of 85 +∞ M [M] X 2 X 2 E = |Fr| − |Fr| (1.7) Go Back r=−∞ r=−M Full Screen This latter form shows that the mean square error decreases monotonically as a function of M (ie [M+1] [M] [M] E 6 E ). f (x) converges to f(x) as more terms are added to the series. It can also Close be shown that the mean-square-error E[M] (for any fixed M) is minimized by using the Fourier coefficients as calculated through equation (1.3). Quit
1.2.1. Why bother?
It is not entirely clear from the above equations what has been gained by expressing the function f(x) as a series (1.1) or (1.5). In Physics we often need to evaluate the derivative (or second derivative etc) of a function: If the function has been expressed as a Fourier series then this is a trivial operation. For example the Jth derivative of f(x), using (1.1), is
J ∞ d f (x) X J = (ikr) [Fr exp (ikrx)] (1.8) dxJ Home Page r=−∞
We can similarly write expressions for integrals of f(x). Title Page
Contents 1.2.2. Aperiodic functions JJ II The instances, in Physics, of genuinely periodic functions are exceedingly rare. However there are still many applications of the above theory. J I
Suppose a function f(x) either exists only in a finite range 0 ≤ x ≤ L or is known only in this finite Page 27 of 85 range. We can construct a periodic function simply by making a periodic repetition of the finite- range function with a repeat period L and apply the theory to this (artificial) periodic function. It is Go Back in this form that Fourier Analysis is normally applied in Physics. Full Screen
Close
Quit 1.3. The Numerical Methods
We now consider how to calculate the coefficients Fr. In general we can approximate an integral by means of the trapezoidal rule. The essence of this is shown in the figure (1.1). Home Page
Title Page
Contents
JJ II
J I
Page 28 of 85
Go Back
Full Screen
Close
Quit
Figure 1.1. Trapezoidal Integration
The integrand is divided into N equal intervals of size L/N and the integrand is approximated by a sequence of straight-line segments. The function f(x) is evaluated at the positions xs = sL/N. Note that N needs to be quite large to ensure accuracy. In the program you will use N has been taken to be 1000. The result of this procedure applied to (1.3) and is
" N−1 # 1 f(0) + f(L) X sL 2π rs = + f exp −i (1.9) Home Page Fr N 2 N N s=1 Title Page We can simplify this by defining an array Contents
f = f sL ; s = 1, .., N − 1 s N JJ II (1.10) f(0)+f(L) f0 = 2 J I
Page 29 of 85 Using this array gives the result for the coefficients as
Go Back N−1 1 X 2π rs = f exp −i (1.11) Full Screen Fr N s N s=0 Close This approximate procedure for the integrals predicts coefficients for r < N/2. It fails to correctly predict coefficients for r ≥ N/2. That is it fails to predict the Fourier components with spatial Quit frequency greater than N/2L and wavelengths less than 2L/N. In fact, if the function f(x) has no spatial frequency greater than N/2L, the Sampling Theorem tells us that the expression (1.11) is exact. There is a technical problem in evaluating the exponentials in (1.11) or (1.5). In Fortran we can only evaluate exp (iα) if α is not too large: In practice less than about 70. (You might think this is a deficiency of Fortran but you need to be aware that no other language has complex exponentials built in). We can surmount this difficulty by evaluating the exponentials in the form:
2π rs 2π MOD (rs, N) exp −i = exp −i (1.12) N N Home Page That is we have replaced rs by MOD (rs, N). This is a Fortran function which gives the remainder when rs is divided by N. This procedure works because (rs − MOD (rs, N)) /N is an integer Title Page and because Contents exp (−i2πm) = 1 (1.13) JJ II for any integer m. J I The argument of the exponential on the right-hand side of (1.12) is then a small quantity (in fact, less in magnitude than 2π). Page 30 of 85
If we only need to evaluate the original function f at the discrete points xs = sL/N then the Go Back formula (1.5) simplifies to Full Screen M X 2π rs f = exp i (1.14) Close s Fr N r=−M Quit which is very similar to the expression for the Fourier coefficient (1.11) and the exponential is calculated in the same way. 1.4. Diffusion Equation
I now look at the use of Fourier Analysis in solving differential equations. In thermal equilibrium (and in the absence of external forces) gases and liquids have uniform den- sities. If a gas or liquid is prepared with a high density in a localized region then this excess density Home Page will quickly spread out until uniformity is restored: This process is called diffusion. If f(x, t) denotes the deviation of density from equilibrium then the evolution of this quantity with time is Title Page determined by the diffusion equation:
Contents ∂f (x, t) ∂2f (x, t) = D (1.15) ∂t ∂x2 JJ II
D is the diffusion constant. J I
Now suppose that I use equation (1.5) for f(x, t) but where the coefficients are functions of time: Page 31 of 85
M Go Back X f (x, t) = Fr (t) exp (ikrx) (1.16) r=−M Full Screen
Inserting this expression in the diffusion equation gives the following result for the coefficients: Close