Computational Physics Recipes
What is a computational physics "recipe"?
One of the most difficult aspects of learning to code — for physics, or any other end — is staring at a blank code editor with only a general, high-level of what you want to accomplish, and no clear idea of what code to write to accomplish it. This is the "blank page problem."
Ever been there? Yeah, me too.
If you've solved similar problems before, then you can probably start by recreating what you did and modifying it. But what if you haven't?
If you can find an example of someone else's solution to a similar problem, you may be able to copy and modify it… Although depending on the complexity of the problem and the transparency or opaqueness of the example, understanding it well enough to know what to modify (and how), without breaking anything, may be be hair-pullingly infuriating.
My solution to this is to offer you a cookbook of "recipes" — starting points that show you how to accomplish various common kinds of coding and computational physics tasks. They are meant to be clear, especially about which bits are essential and which are meant for you to fit to your specific needs. They also include examples, and sometimes variations upon the basic recipe for situations that are similar but differ in some important way.
Some recipes are procedural, describing a sequence of steps for you to follow. Some are syntactic, showing specific python code to accomplish a task. Some are structural, recommending approaches to organizing code in pursuit of your goal.
Often, recipes include key conceptual and/or informational knowledge about python, numerical methods, and the practice of computational physics. Understanding the ideas and knowing the facts is an essential part of using the recipes correctly, flexibly, and successfully.
The recipes in this compendium are organized in the order of the course assignments that accompany them, grouped into "units" or "chapters" or whatever you want to call them:
Recipes by PHY 351 course unit
-
I use the term core python to mean the standard parts of the python ecosystem: the base python language and the "standard library" of modules that is typically installed along with it. For purposes of this course I'm also including the tool stack we'll use to work with python: GitHub and git, JupyterLab and its pieces, and a little of the Unix command-line.
-
World B: Numerical Methods 1 — Randomness, Roots & Extrema
Now that we've covered the essentials of python, let's take a break from "learning to code" in order to "learn why to code" — that is, how to use those skillz to do some physics-y stuff.
-
Here we introduce recipes focused on aspects of python central to numerically-intensive work. These include a deep understanding of how computers handle numbers, working with arrays and matrices of values, creating plots and other data visualizations, and reading to or writing from data files. Along the way, we'll introduce numpy and matplotlib, add-ons to the python language that support numerically-focused work.
-
World D: Numerical Methods 2: Numerical Calculus
Doing math involves more than solving algebraic equations! It often requires taking derivatives, integrating, and solving differential equations. Computers can do that too! The key involves forgetting most of what you learned in calculus courses, and going back to the initial definitions of "derivative" and "integral"… and then applying a whole lot of numerical cleverness.
-
Yeah, "solving" a physics problem and getting a bunch of numbers out – or even a pretty plot — is nice, but there's no substitute for seeing the behavior of your modeled system evolve in realtime. VPython ("visual python") is an add-on toolkit for doing exactly that, and it makes constructing and animating 3D simulations embarrassingly easy.