CFD Python
Please cite as: Barba, Lorena A., and Forsyth, Gilbert F. (2018). CFD Python: the 12 steps to Navier-Stokes equations. Journal of Open Source Education, 1(9), 21, https://doi.org/10.21105/jose.00021
CFD Python, a.k.a. the 12 steps to Navier-Stokes, is a practical module for learning the foundations of Computational Fluid Dynamics (CFD) by coding solutions to the basic partial differential equations that describe the physics of fluid flow. The module was part of a course taught by Prof. Lorena Barba between 2009 and 2013 in the Mechanical Engineering department at Boston University (Prof. Barba since moved to the George Washington University).
The module assumes only basic programming knowledge (in any language) and some background in partial differential equations and fluid mechanics. The "steps" were inspired by ideas of Dr. Rio Yokota, who was a post-doc in Prof. Barba's lab until 2011, and the lessons were refined by Prof. Barba and her students over several semesters teaching the CFD course. We wrote this set of Jupyter notebooks in 2013 to teach an intensive two-day course in Mendoza, Argentina.
Guiding students through these steps (without skipping any!), they learn many valuable lessons. The incremental nature of the exercises means they get a sense of achievement at the end of each assignment, and they feel they are learning with low effort. As they progress, they naturally practice code re-use and they incrementally learn programming and plotting techniques. As they analyze their results, they learn about numerical diffusion, accuracy and convergence. In about four weeks of a regularly scheduled course, they become moderately proficient programmers and are motivated to start discussing more theoretical matters.
How to use this module
In a regular-session university course, students can complete the CFD Python lessons in 4 to 5 weeks. As an intensive tutorial, the module can be completed in two or three full days, depending on the learner's prior experience. The lessons can also be used for self study. In all cases, learners should follow along the worked examples in each lesson by re-typing the code in a fresh Jupyter notebook, maybe taking original notes as they try things out.
Lessons
Launch an interactive session with this module using the Binder service:
Steps 1–4 are in one spatial dimension. Steps 5–10 are in two dimensions (2D). Steps 11–12 solve the Navier-Stokes equation in 2D. Three "bonus" notebooks cover the CFL condition for numerical stability, array operations with NumPy, and defining functions in Python.
- Quick Python Intro —For Python novices, this lesson introduces the numerical libraries (NumPy and Matplotlib), Python variables, use of whitespace, and slicing arrays.
- Step 1 —Linear convection with a step-function initial condition (IC) and appropriate boundary conditions (BCs).
- Step 2 —With the same IC/BCs, nonlinear convection.
- CFL Condition —Exploring numerical stability and the Courant-Friedrichs-Lewy (CFL) condition.
- Step 3 —With the same IC/BCs, diffusion only.
- Step 4 —Burgers’ equation, with a saw-tooth IC and periodic BCs (with an introduction to Sympy).
- Array Operations with NumPy
- Step 5 —Linear convection in 2D with a square-function IC and appropriate BCs.
- Step 6 —With the same IC/BCs, nonlinear convection in 2D.
- Step 7 —With the same IC/BCs, diffusion in 2D.
- Step 8 —Burgers’ equation in 2D
- Defining Functions in Python
- Step 9 —Laplace equation with zero IC and both Neumann and Dirichlet BCs.
- Step 10 —Poisson equation in 2D.
- Step 11 —Solves the Navier-Stokes equation for 2D cavity flow.
- Step 12 —Solves the Navier-Stokes equation for 2D channel flow.
Dependencies
To use these lessons, you need Python 3, and the standard stack of scientific Python: NumPy, Matplotlib, SciPy, Sympy. And of course, you need Jupyter—an interactive computational environment that runs on a web browser.
This mini-course is built as a set of Jupyter notebooks containing the written materials and worked-out solutions on Python code. To work with the material, we recommend that you start each lesson with a fresh new notebook, and follow along, typing each line of code (don't copy-and-paste!), and exploring by changing parameters and seeing what happens.
Installing via Anaconda
We *highly* recommend that you install the [Anaconda Python Distribution](http://docs.continuum.io/anaconda/install). It will make your life so much easier. You can download and install Anaconda on Windows, OSX and Linux.
After installing, to ensure that your packages are up to date, run the following commands in a terminal:
conda update conda
conda update jupyter numpy sympy scipy matplotlib
If you prefer Miniconda (a mini version of Anaconda that saves you disk space), install all the necessary libraries to follow this course by running the following commands in a terminal:
conda update conda
conda install jupyter
conda install numpy scipy sympy matplotlib
Without Anaconda
If you already have Python installed on your machine, you can install Jupyter using pip:
pip install jupyter
Please also make sure that you have the necessary libraries installed by running
pip install numpy scipy sympy matplotlib
How to contribute to CFD Python
We accept contributions via pull request—in fact, several users have already submitted pull requests making corrections or small improvements. You can also open an issue if you find a bug, or have a suggestion.
Copyright and License
(c) 2017 Lorena A. Barba, Gilbert F. Forsyth. All content is under Creative Commons Attribution CC-BY 4.0, and all code is under BSD-3 clause (previously under MIT, and changed on March 8, 2018).
We are happy if you re-use the content in any way!