Introduction#

Overview of Course#

This is a course on Computational Physics. That is, learning how to use computers to do physics. I am still working on the course materials, but here is my Vision of where we are going. The idea is to put the physics first, but along the way learn about computation and the interplay of computation and physics.

Physics Topics

  • Mechanics

    • Periodic Motion

    • Projectiles

    • Chaos

  • Statistical Mechanics

    • Hard Spheres

    • Magnetic Models

  • Electricity and Magnetism

    • Fields

  • Quantum Mechanics

    • Stationary States

    • Dynamics

Algorithms/Numerical Techniques

  • Quadratures (Numerical Integration)

  • Integrating Ordinary Differential Equations

  • Monte Carlo techniques

  • Numerical Linear Algebra

  • Finite Differences

  • Finite Elements

Computer Science Topics

  • Thinking Computationally

    • Converting models into algorithms

  • Computational Workflows

    • Writing/Running Code

    • Debugging

    • Validating

    • Benchmarking

    • Refactoring

  • Visualization

  • Machine Learning

What is the role of computers in physics?#

It is currently impossible to do physics without computers. They play an essential role in all parts of the discipline.

  • Converting models into numbers

  • Gaining understanding through parametric dependencies

  • Modeling with Algorithms

  • Numerical Experiments

  • Data Analysis

We hope to learn about how to do all of these.

A key point is that computation is not about finding a number. It is a process which gives us understanding.

What computer language should one use for learning computational physics?#

Glib answer 1: It doesn’t matter

Glib answer 2: Whatever language you know

Glib answer 3: Whatever language you think you will use in the future

Options:#

C++

  • Very flexible – have full control of memory, and how things are passed.

    • Any programming paradigm can be used in C++

  • Very mature – great collection of packages for scientific computing

    • Great software development tools to aid in debugging/profiling, etc…

  • Runs Fast – very little unneccesssary overhead, and can be optimized

  • Portable – free C++ compilers are everywhere.

    • Compiled code is a program which can be run independently

  • Not a great teaching language

    • Syntax can be complex

    • Need to learn a lot before you can start programming

    • Workflow (write, compile cycle) can be frustrating

Fortran

  • Legacy language

    • At its roots is a language designed for punch-cards

    • Much of the syntax is rooted in programming strategies which are out of favor

    • Has been updated: in modern Fortran you can use any programming paradigm

  • Runs Fast – has reputation for making some of the fastest code

  • Portable – free Fortran compilers are everywhere.

    • Compiled code is a program which can be run independently

  • Not a great teaching language

    • Syntax is clunky (but has a simplicity to it)

    • Workflow (write, compile cycle) can be frustrating

Mathematica

  • High level language

    • Excellent built-in algorithms for integrating, solving differential equations…

    • Syntax for working with low-level details can be ideosyncratic

  • Best system for dealing with symbolic arithmetic

  • High level routines are very fast

    • Low level programs can be fast, but learning how to create such code is like learning a new language

  • Easy to pick up

  • Seamlessly combines symbolic and numerical expressions

  • Proprietary – Mathematica code can only be run on Mathematica

  • Largely Interpreted

    • Leads to efficient programming paradigm (Notebook Paradigm)

Matlab

  • Specialize language

    • Only used for scientific programming

  • Easy to pick up

  • Excellent (often proprietary) ecosystem of modeling tools (finite elements..)

  • Proprietary – Matlab code can only be run on Matlab

  • Widely used in academia

  • Built-in IDE

Python

  • Modern

    • syntax is smart – drives you towards creating good objects

  • Excellent open-source package ecosystem

    • You can find a freely available high-level package to do almost anything

  • Familiar

    • Most students have already been introduced to Python

  • Can be fast if the right strategies are used (such as vectorizing)

  • Largely Interpreted

    • Can use a number of efficient workflows

  • Easy to learn

  • Designed for general-purpose computing

  • Very widely used

    • Very high probability you will use it in future

Julia

  • Modern

    • syntax is smart – drives you towards creating good objects

  • Excellent package infrastructure

    • You can find a freely available high-level package to do almost anything

  • Unfamiliar

    • Most students have not encountered it before (good and bad – level playing field)

  • Easy to write fast code

  • Just in Time compiled

    • Nominally benefits of both compiled code and interpreted code

Some basic principles#

Premature optimization is the root of all evil – Donald Knuth

  • don’t waste your time trying to get performance out of a subroutine that is not the rate-limiting step in your calculation

Good strategy for scientific programing:

  1. Make a precise goal

  2. Write out on paper the algorithm, data structures

    • give options: what is simplest, what will be most efficient

    • figure out strategy which will take the least programming time to get a working code

  3. Code up simple algorithm

    • test to make sure it is working

  4. Is it fast/accurate enough for your purposes?

    • if yes, you are done

    • if no, refactor and repeat

Step 2 is most important. The purpose of this course is to give you exposure to the strategies which are available to you – so that you can figure out this step. We will also work on the craft of programming, and some strategies for making better code (more efficient, easier to work with…)

The most challenging part of this workflow is validating and debugging: How to convince yourself that the code is doing what you want? How to fix it if it is not? Modern computing paradigms are all about making this validation and debugging as simple and robust as possible.

Don’t be afraid to start over, and rewrite your program from scratch (even using a different language). Scientific programming should be iterative. Having a working code which is slow will make debugging the fast code easier.

Throughout this, keep ease-of-use in mind. A keystone idea in modern scientific programming is that you are not trying to adjust your thinking so that you can talk to the computer – rather you are trying to make the computer understand your language, so that you can effortlessly talk to the computer.

Packages#

Modern scientific programming makes significant use of packages. It is rare for anyone to write their own linear algebra routines, or graph plotting routines.

  • don’t be afraid to use high-level packages

  • don’t be afraid to use low-level programming

Challenges with packages:

  • which one to use?

  • need to learn some special syntax

  • what happens when you want to do something which is just beyond the reach of the package? Can you extend their code? Do you need to start from scratch?

  • can you trust it?

Your scientific programming possibilities will be limited by over-reliance on black boxes that you do not understand. Conversely, a refusal to use high-level routines would mean that you will perpetually be wasting your time reinventing the wheel (and quite possibly reinventing an inferior wheel). Finding this balance is a challenge.

Another important thing to consider is that someone needs to write the packages. Maybe that is you!