Book: Computational Physics
The materials on this page are taken from the book |

The Python programming language is an excellent choice for learning, teaching, or doing computational physics. It is a well-designed, modern programming language that is simultaneously easy to learn and very powerful. It includes a range of features tailored for scientific computing, including features for handling vectors, inverting and diagonalizing matrices, performing Fourier transforms, making graphs, and creating 3D graphics.

This page contains a selection of resources I've developed for teachers and students interested in computational physics and Python.

Here are several complete book chapters on Python computational physics. You're welcome to download these chapters, print them out, use them in class, or just read them for yourself. Comments or questions are welcome.

**Chapter 2: Python programming for physicists**– This chapter gives an introduction to the Python language at a level suitable for readers with no previous programming experience. It introduces the basic elements of programming with variables and arrays, assignments, arithmetic and functions, inputs, outputs, conditionals, and loops, all in the Python language. The ideas are illustrated with examples drawn from various branches of physics, including classical mechanics, special relativity, and quantum physics.**Chapter 3: Graphics and visualization**– This chapter gives an introduction to some of Python's features for making scientific graphics, including graphs, density plots, and 3D visualizations of physical systems.**Chapter 4: Accuracy and speed**– Very important for good scientific programming is an understanding of the limitations of the computer. Computers are neither infinitely fast nor infinitely accurate. This chapter explains how we estimate the accuracy of our calculations and how long they will take, and some of the pitfalls that can be encountered if we don't take care with such things.**Chapter 5: Integrals and derivatives**– Having mastered the fundamentals of Python programming, we move on to the main business of computational physics. This chapter introduces methods for performing integrals and derivatives on the computer, including basic techniques like the trapezoidal rule and Simpson's rule, and more advanced techniques like adaptive methods, Romberg integration, and Gaussian quadrature. Example applications include the heat capacity of solids, thermal radiation, electrostatics calculations, and image processing.

Subsequent chapters cover a range of further topics in computational physics, including the solution of linear and nonlinear systems of equations, the solution of ordinary and partial differential equations, Fourier transforms, stochastic processes, and Monte Carlo methods. For a full table of contents, see here.

Here are three useful appendices that go with the chapters above:

**Appendix A: Installing Python**– How to install Python on your computer, plus some additional packages that you'll need to do the examples and exercises in the chapters above**Appendix B: Differences between Python versions**– A summary of the differences between versions 2 and 3 of Python**Appendix C: Gaussian quadrature**– A derivation of the integration points for Gaussian quadrature, based on the mathematics of Legendre polynomials. Also contains a discussion of the Stieltjes polynomials and their use in Gauss-Kronrod quadrature.

The following files contain copies of the example programs from the chapters above. All programs are in Python version 3, but they will work fine in version 2 also (technically version 2.6 or later) if you add this line to the beginning of the program:

from __future__ import print_function,divisionThe file names below correspond to the names printed in the margins of the chapter pages next to each program.

**Chapter 2:**

`evenodd.py`

– Check two integers to ensure one is even and the other odd`fibonacci.py`

– Print out the Fibonacci numbers up to 1000`polar.py`

– Convert from polar to Cartesian coordinates`rydberg.py`

– Print out the wavelengths of hydrogen lines

**Chapter 3:**

`circular.py`

– Make a density plot from the data in a file`hrdiagram.py`

– Calculate and display a Hertzsprung–Russell diagram for a catalog of nearby stars`lattice.py`

– Create a 3D visualization of a simple cubic lattice`revolve.py`

– Create an animation of a moving sphere`ripples.py`

– Calculate and display the interference pattern generated by two circular sets of waves

**Chapter 4:**

`qsho.py`

– Calculate the internal energy of a quantum simple harmonic oscillator at temperature*T*

**Chapter 5:**

`gaussint.py`

– Evaluate an integral using Gaussian quadrature`intinf.py`

– Evaluate an integral over an infinite domain`trapezoidal.py`

– Evaluate an integral using the trapezoidal rule

Here are some data sets that accompany the examples and exercises in the chapters above:

`altitude.txt`

– Altitude above sea level, or depth below it, of points on the Earth's surface, measured on a grid`circular.txt`

– Data file for the density plots in Figure 2.3`stars.txt`

– Catalog of temperatures and magnitudes for 7860 nearby stars`stm.txt`

– STM measurements of the (111) surface of silicon`sunspots.txt`

– Data on sunspots since 1749`velocities.txt`

– Velocity of a moving particle as a function of time

Here are a few other pieces of Python code that are useful for some of the exercises.

`banded.py`

– Solve a tridiagonal or banded system of linear equations using Gaussian elimination`colormaps.py`

– Definitions of some useful colormaps for density plots`dcst.py`

– Perform forward and inverse fast cosine and sine transforms`gaussxw.py`

– Calculate positions and weights of integration points for Gaussian quadrature

Last modified: July 15, 2013

Special thanks to Gus Evrard, Brad Orr, Len Sander, and Bruce Sherwood for Python info and comments.

Sources for data sets:

- Altitude data: NOAA 2-minute Gridded Global Relief data set. For details see C. Amante and B. W. Eakins, ETOPO1 1 Arc-Minute Global Relief Model: Procedures, Data Sources and Analysis. NOAA Technical Memorandum NESDIS NGDC-24, March 2009.
- Stars: HYG Database, a composite of data from Hipparcos and the Yale Bright Star and Gliese catalogs, compiled by David Nash.
- Sunspot numbers: NOAA National Geophysical Data Center, based on a data set maintained and published by Royal Observatory of Belgium's Solar Influences Data Analysis Center.