This page intentionally left blank www.com Introductory Computational Physics Computers are one of the most important tools available to physicists, whether for calculating and displaying results, simulating experiments, or solving complex systems of equations. Introducing students to computational physics, this textbook shows how to use computers to solve mathematical problems in physics and teaches students about choosing different numerical approaches. It also introduces students to many of the programs and packages available. The book relies solely on free software: the operating system chosen is Linux, which comes with an excellent C++ compiler, and the graphical interface is the ROOT package available for free from CERN.
This up-to-date, broad scope textbook is suitable for undergraduates starting on computational physics courses. It includes exercises and many examples of programs. Online resources at www.org/9780521828627 feature additional reference information, solutions, and updates on new techniques, software and hardware used in physics. Andi Klein is a Technical Staff member at Los Alamos National Laboratory, New Mexico.
He gained his Ph. from the University of Basel, Switzerland. He held the position of Professor of Physics at Old Dominion University, Virginia, from 1990 to 2002, where he taught courses in computational physics. Alexander Godunov is Assistant Professor at the Department of Physics, Old Dominion University, Virginia.
He gained his Ph. from Moscow State University, Russia and has held research positions at Tulane University, Louisiana, and visiting positions at research centers in France and Russia.com Introductory Computational Physics Andi Klein and Alexander Godunov Los Alamos National Laboratory and Old Dominion University www.com cambridge university press Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge cb2 2ru, UK Published in the United States of America by Cambridge University Press, New York www.org Information on this title: www.org/9780521828628 © Cambridge University Press 2006 This publication is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format isbn-13 978-0-511-16650-1 eBook (Adobe Reader) isbn-10 0-511-16650-8 eBook (Adobe Reader) isbn-13 978-0-521-82862-8 hardback isbn-10 0-521-82862-7 hardback isbn-13 978-0-521-53562-5 isbn-10 0-521-53562-x Cambridge University Press has no responsibility for the persistence or accuracy of urls for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate.com Contents Preface page ix 1 Introduction 1 1.1 The need for computers in science 1 1.2 What is computational physics? 1 1.3 Linux and C++ 2 2 Basics 5 2.1 Basic computer hardware 5 2.3 How does it work? 9 3 Short introduction to Linux 11 3.1 Getting started and logging in 11 3.3 The filesystem, or where is everything? 12 3.4 Moving around in your system 13 3.5 Listing your directory 14 3.6 Creating your own files 15 3.7 Doing some work 17 3.9 Machine representation and precision 20 3.4 Polynomial interpolation 31 v www.com vi Contents 4.6 Rational function interpolation 34 4.7 Exercises 35 5 Taking derivatives 37 5.1 General discussion of derivatives with computers 37 5.3 Central difference and higher order methods 38 5.4 Higher order derivatives 40 5.5 Exercises 40 6 Numerical integration 41 6.1 Introduction to numerical integration 41 6.2 The simplest integration methods 42 6.3 More advanced integration 44 6.4 Exercises 49 7 Solution of nonlinear equations 51 7.3 Method of secants 52 7.4 Brute force method 53 7.5 Exercises 53 8 Differential equations 55 8.2 A brush up on differential equations 55 8.3 Introduction to the simple and modified Euler methods 57 8.4 The simple Euler method 58 8.5 The modified Euler method 62 8.6 Runge–Kutta method 65 8.7 Adaptive step size Runge–Kutta 70 8.8 The damped oscillator 72 8.1 Linear systems of equations 83 9.3 Standard libraries 86 www.com Contents vii 9.5 Exercises 88 10 Random processes and Monte Carlo simulation 89 10.1 Random processes in science 89 10.2 Random number generators 90 10.3 The random walk 92 10.4 Random numbers for nonuniform distributions 97 10.5 Monte Carlo integration 101 10.6 Exercises 103 References 105 Appendix A The ROOT system 107 A.1 What is ROOT 107 A.2 The ROOT basics 107 A.3 The first steps 108 A.5 Exercises 115 Appendix B Free scientific libraries 117 B.3 Where to obtain ROOT 118 Appendix C FORTRAN and C++ 119 C.1 Calling FORTRAN from C++ 120 Appendix D Program listings 121 D.2 Runge–Kutta program 123 D.3 Random walk in two dimensions 131 D.4 Acceptance and rejection method with sin(x) distribution 134 Index 137 www.com Preface Computers are one of the most important tools in any field of science and especially in physics.
A student in an undergraduate lab will appreciate the help of a computer in calculating a result from a series of measurements. The more advanced researcher will use them for tasks like simulating an experiment, or solving complex systems of equations. Physics is deeply connected to mathematics and requires a lot of calculational skills. If one is only interested in a conceptual understanding of the field, or an estimate of the outcome of an experiment, simple calculus will probably suffice.
We can solve the problem of a cannon ball without air resistance or Coriolis force with very elementary math, but once we include these effects, the solution becomes quite a bit more complicated. Physics, being an experimental science, also requires that the measured results are statistically significant, meaning we have to repeat an experiment several times, necessitating the same calculation over and over again and comparing the results. This then leads to the question of how to present your results. It is much easier to determine the compatibility of data points from a graph, rather than to try to compare say 1000 numbers with each other and determine whether there is a significant deviation.
From this it is clear that the computer should not only “crunch numbers,” but should also be able to display the results graphically. Computers have been used in physics research for many years and there is a plethora of programs and packages on the Web which can be used to solve different problems. In this book we are trying to use as many of these available solutions as possible and not reinvent the wheel. Some of these packages have been written in FORTRAN, and in Appendix C you will find a description of how to call a FORTRAN subroutine from a C++ program.
As we stated above, physics relies heavily on graphical representations. Usually, the scientist would save the results from some calculations into a file, which then can be read and used for display by a graphics package like gnuplot or a spreadsheet program with graphics capability. We have decided to pursue ix www.com x Preface a different path, namely using the ROOT package [1] developed at the high energy physics lab CERN in Switzerland. ROOT, being an object oriented C++ package, not only provides a lot of physics and math C++-classes but also has an excellent graphics environment, which lets you create publication quality graphs and plots.
This package is constantly being developed and new features and classes are being added. There is an excellent user’s guide, which can be found on the ROOT website in different formats. In order to get started quickly we have given a short introduction in Appendix A.com Chapter 1 Introduction 1.1 The need for computers in science Over the last few decades, computers have become part of everyday life. Once the domain of science and business, today almost every home has a per- sonal computer (PC), and children grow up learning expressions like “hard- ware,” “software,” and “IRQ.” However, teaching computational techniques to undergraduates is just starting to become part of the science curriculum.
Computational skills are essential to prepare students both for graduate school and for today’s work environment. Physics is a corner-stone of every technological field. When you have a solid understanding of physics, and the computational know-how to calculate solutions to complex problems, success is sure to follow you in the high-tech environment of the twenty-first century.2 What is computational physics? Computational physics provides a means to solve complex numerical prob- lems. In itself it will not give any insight into a problem (after all, a computer is only as intelligent as its user), but it will enable you to attack problems which otherwise might not be solvable.
Recall your first physics course. A typical introductory physics problem is to calculate the motion of a cannon ball in two dimensions. This problem is always treated without air resistance. One of the difficulties of physics is that the moment one goes away from such an idealized system, the task rapidly becomes rather complicated.
If we want to calculate the solution with real-world elements (e., drag), things become rather difficult. A way out of this mess is to use the methods of computational physics to solve this linear differential equation.com 2 Introduction One important aspect of computational physics is modeling large complex systems. For example, if you are a stock broker, how will you predict stock market performance? Or if you are a meteorologist, how would you try to predict changes in climate? You would solve these problems by employing Monte Carlo techniques. This technique is simply impossible without com- puters and, as just noted, has applications which reach far beyond physics.
Another class of physics problems are phenomena which are represented by nonlinear differential equations, like the chaotic pendulum. Again, computa- tional physics and its numerical methods are a perfect tool to study such sys- tems. If these systems were purely confined to physics, one might argue that this does not deserve an extended treatment in an undergraduate course. How- ever, there is an increasing list of fields which use these equations; for exam- ple, meteorology, epidemiology, neurology and astronomy to name just a few.
An advantage of computational physics is that one can start with a simple problem which is easily solvable analytically. The analytical solution illus- trates the underlying physics and allows one the possibility to compare the computer program with the analytical solution. Once a program has been written which can handle the case with the typical physicist’s approximation, then you add more and more complex real-world factors. With this short introduction, we hope that we have sparked your interest in learning computational physics.
Before we get to the heart of it, however, we want to tell you what computer operating system and language we will be using.3 Linux and C++ Linux You may be accustomed to the Microsoft Windows or Apple MAC operating systems. In science and in companies with large computing needs, however, UNIX is the most widely used operating system platform. Linux is a UNIX- type operating system originally developed by Linus Torwald which runs on PCs. Today hundreds of people around the world continue to work on this system and either provide software updates or write new software.
We use Linux as the operating system of choice for this text book because: • Linux is widely available at no cost; • Linux runs on almost all available computers; • it has long-term stability not achieved by any other PC operating system; • Linux distributions include a lot of free software, i., PASCAL, FOR- TRAN, C, C++.3 Linux and C++ 3 In today’s trend to use networked clusters of workstations for large computa- tional tasks, knowledge of UNIX/Linux will provide you with an additional, highly marketable skill. C++ In science, historically the most widely used programming language was FORTRAN, a fact reflected in all the mathematical and statistical libraries still in use the world over (e., SLATEC, LAPACK, CERNLIB). One disad- vantage of FORTRAN has always been that it was strongly decoupled from the hardware. If you wanted to write a program which would interact directly with one of the peripherals, you would have to write code in assembly lan- guage.