Lorena A. Barba group

CFD Python: 12 steps to Navier-Stokes

Cavity flow solution at Reynolds number of 200 with a 41x41 mesh.

Cavity flow solution at Reynolds number of 200 with a 41x41 mesh.

We announce the public release of online educational materials for self-learners of CFD using IPython Notebooks: the CFD Python Class!

Update! (Jan.2014) CFD Python has a new home on GitHub

Some background

This post describes the first practical module of Prof. Barba's Computational Fluid Dynamics class, as taught between 2010 and 2013 at Boston University. The module is called "12 steps to Navier-Stokes equations" (yes, it's a tongue-in-check allusion of the recovery programs for behavioral problems). It was inspired by the ideas of Dr. Rio Yokota, who was a post-doc in Barba's lab, and has been refined by Prof. Barba and her students over several semesters teaching the course.

The course is for beginners. It assumes only basic programming skills —the concepts of iterations, function calls and so on— and builds immediate hands-on experience via this module and several others that come after. In the future, we hope to publish materials for the other modules also (e.g., convection schemes with Burgers equation, Euler equations and shock-tube problem, and others).

We use Python for this class, and those engineering students that are dependent on Matlab just have to bite the bullet and learn Python. (The reasons for this are many and would be subject for another blog post!)

Prof. Barba has a long track record of sharing educational materials freely online. Recordings of her lectures for the CFD course were made available first on iTunesU in 2010. Then, in 2012, she decided to use the flipped classroom model and re-posted the videos on YouTube, after editing for length and with some additions.

The new CFD Python class notebooks are her latest free online materials!

Instructions

The simplest way to enjoy these materials is to view each lesson online (follow the links below), as rendered by the IPython Notebook Viewer. In that case, you could follow along and write your own version of the programs to run with your local Python installation.

But you can also download the whole collection of Notebooks from the CFD Python GitHub repository. If you have a full distribution of the Python "ecosystem," then navigate to the directory where you've downloaded the collection, and launch the IPython Notebook with the following command on the terminal:
ipython notebook

 

Description of the practical module

This module has been proved in the classroom for four consecutive years. It has brought several dozen students to develop their own 2D Navier-Stokes finite-difference solver from scratch in just over a month (with two class meetings per week). The module consists of the following steps (links are to the individual IPython Notebooks):

Steps 1–4 are in one dimension:

(i) linear convection with a step-function initial condition (IC) and appropriate boundary conditions (BC);

with the same IC/BCs:

(ii) nonlinear convection, and

(iii) diffusion only;

with a saw-tooth IC and periodic BCs

(iv) Burgers' equation.

Steps 5–10 are in two dimensions:

(v) linear convection with square function IC and appropriate BCs;

(vi)  nonlinear convection, with the same IC/BCs

(vii) diffusion only, with the same IC/BCs;

(viii) Burgers' equation;

(ix)  Laplace equation, with zero IC and both Neumann and Dirichlet BCs;

(x) Poisson equation in 2D.

Steps 11–12 solve the Navier-Stokes equation in 2D:

(xi) cavity flow;

(xii) channel flow.

Students are instructed to follow these steps one by one, without skipping any! The most important step is #1, in fact. Everything builds from there.

We know that self-learners around the world have already started following this "12-step program" using the videos on iTunesU and YouTube since 2010. In fact, at least two students have blogged about their experience: DaFeda in Norway (June 2011) and  Sidafa Conde at UMass-Dartmouth (January 2012).

And clearly the contributions of Suraj Shankar to the Matlab Central file exchange were also result of his following the class online.

Prof. Barba often receives emails of appreciation from students in far-away places who are learning with her free online materials. And in 2012, she opened her CFD Piazza class to off-campus students, resulting in about 180 students "auditing" and participating in the online class discussions —this effort resulted in Prof. Barba being featured in the Piazza Blog as "Innovator of the Week" in April 2012.

We hope that the CFD Python series will help a new cohort of students and self-learners gain basic CFD skills. Let us know what you think!

Acknowledgements

  • William Lane

    Looks great, Gil and Prof. Barba! I might have to give iPython notebooks a try. One thing I noticed, is that the animations didn't work for me. It could just be a browser/operating system bug (Chrome/Ubuntu)... Additionally, it might be helpful to people to point out the odd-even decoupling that occurs in Step 11 with the pressure field. I know that was a great learning point for me.

    • http://lorenabarba.com/ Lorena A. Barba

      Thanks for leaving a comment, Will. I was debating whether to include a discussion of the issue of collocated versus staggered grid. Maybe it could be subject of a separate notebook (rather than adding it to Step 11). We'll think about it !

  • Jungyeoul Brad Maeng

    Great effort. From a previous student who took this course, it is a great way to get introduced to the joy of CFD. I am now doing fundamental research in CFD due to this eye opening course.

    • http://lorenabarba.com/ Lorena A. Barba

      I'm so glad to hear from you, Brad. I knew you had gone to graduate school but I did not know you were working in CFD! This is of course gratifying to hear. Best of luck in your research and thanks for leaving a comment!

  • Francisco Botero

    Great material. I'd like to implement it in my class. However, I'd like to have a Spanish version. Are you interested? How can I contact you?

    My best regards,

    Francisco

    • http://lorenabarba.com/ Lorena A. Barba

      Hi Francisco -- you've just contacted me :-)
      If you want to translate these, go ahead! You can send me email if you want to discuss further. I'm glad you find this material useful.

  • Kevin Kennedy

    These are fantastic! I've been trying to learn python but it's tough to find interesting projects to learn it with. Have you given any thought to having a lesson or two on 3D CFD? Every class I've ever taken does one and two, but never three. I'd love some help making that jump.

  • omkar raut

    This is great....i am learning CFD....this material will really help me...thanx..and i want basics of CFD so how should i contact you??

  • Pingback: This Week in CFD | Another Fine Mesh

  • http://ondrejcertik.com/ Ondřej Čertík

    Lorena, this is fantastic! Thanks a lot for sharing. If you have any problems with SymPy, please don't hesitate to ask us (for example on the sympy mailinglist). I'll be very happy to help make it do what you need.

    • http://lorenabarba.com/ Lorena A. Barba

      I'll keep that in mind, and thanks! We are developing more educational notebooks but haven't yet used more SymPy. I have a project in mind where SymPy will feature more prominently, though … I just need the time!

  • Jonathan Nickerson

    Professor Barba,

    I am a graduate student currently trying to teach myself CFD. I have spent this week working through your CFD Python course. I must say, this course has been a blessing. It has made learning CFD fun and straightforward. Thank you very much for being awesome!

    Are you interested in active feedback regarding the course? I ran into a few snags and, provided you are interested, I would like to bring them to your attention. If this is the case, then can I perhaps contact you via email?

    Thanks again

    • http://lorenabarba.com/ Lorena A. Barba

      Hi Jonathan,
      Thanks for leaving a comment and I'm glad this has been helpful to you! Please do send any feedback you have, and I'll be happy to introduce modifications to the lessons to improve them with your feedback.

  • Binhjamin

    professor Baraba!

    This is terrific! thank you for making learning fun!

  • Hassan Imani

    These are very good for me and I think for every body. I had many book and pdf lecture about CFD, but i couldn't understand them very well. At the moment, i have CFD course in my university and i am very good in that because I saw your course.
    At the end i have a question, could you please help me about Boundary Element Method, because I want to use to this method in my thesis.

    I am looking for your help, please.

    Thank you so much

    • http://lorenabarba.com/ Lorena A. Barba

      Hello Hassani — Thanks for your comment!

      I don't have materials on BEM, even though we actually use BEM in our research activities. However, as a basic foundation, you might find value in the AeroPython series, which builds up the skills to write a panel method:
      https://github.com/barbagroup/AeroPython
      When I add a blog post about that, I'd be glad to get a comment about how (if) you used them.

  • Abhay

    Dear prof, can you please separate course on finite volume method only. That would help us lot.

    thank you,
    Abhay

  • Abhay

    Dear Prof, Can you please take a course on CFD finite volume method separately. This will help us a lot.
    thank you,
    Abhay

    • http://lorenabarba.com/ Lorena A. Barba

      It's certainly among my aspirations to provide more CFD materials online, and FVM is one of the important topics. But given my many teaching and research obligations, it will have to wait until I teach another on-campus CFD course.

      But there are some new materials coming soon in the form of IPython Notebooks, so keep checking back!

  • Guest

    These are fantastic!

  • Miloud Hadj

    Thank you so much

    These are fantastic!

  • cacheme

    Hi Francisco, in case you are still interested we have already translated the module. Of course, you can use it in your class and let us know how was it! :)

    https://bitbucket.org/franktoffel/cfd-python-class-es

    • http://lorenabarba.com/ Lorena A. Barba

      Thank you so much for the excellent work!

      • cacheme

        It was a pleasure! We would like to speak more languages ;)

  • Pingback: SC13 Birds-of-a-Feather Session Python for High Performance Computing

  • Eshan

    Thank you prof Barba for this wonderful work... much much helpful it has been. I have been following all the videos uploaded on youtube: starting from Lecture Zero to Lecture 28, which stups abruptly and I can not find any further lectures, which i guess would be on FEM and FVM. I tried mailing you about the same but couldnt get u. Please help me on how to find furtherlectures after Lecture No.28(CFD ME702)

    • http://lorenabarba.com/ Lorena A. Barba

      Thanks for your comment and I'm glad that the videos on iTunes U and YouTube have been helpful to you.

      Although I did cover some basics of FVM in that course, I never got to making the videos for that topic. Until I teach CFD again (on campus), I might not find a reason to develop more videos. But when I do, I'll remember to include FVM, for sure.

  • http://lorenabarba.com/ Lorena A. Barba

    I hope you are making progress with learning Python and CFD. And thanks for leaving a comment!

    CFD in 3D gets very computationally intensive and almost inevitably requires parallel computing. By the time you are ready to work in 3D, you are an expert. There are many steps to get there, and I have a long list of IPython-Notebook-powered lessons planned to help in that growth!

  • Arid Piloti

    Thanks for this learning material.

  • http://fr.linkedin.com/in/abdaddi/ Abdallah

    Thank you so much Prof. Barba for these powerful lessons. We gradually learn how to code and solve numerically some of the classical equations in physics. It was a good initiation for us in CFD using the programming language Python. Formerly I find it too scary to write programs in computers but now I feel more confident to go ahead and discover the beauty of the computational world. I would like to ask you if you have other lessons using different schemes such as the spectral method. Thank you again for making these lessons for us!!

    • http://lorenabarba.com/ Lorena A. Barba

      Thanks, Abdallah.

      There will be more lessons using convection schemes for Burgers and Euler equations, but I've no plans to write some on spectral methods. Perhaps when I teach a numerical methods class (if I do!) … in the meantime, you might enjoy AeroPython:
      https://github.com/barbagroup/AeroPython

      I'll write a blog post soon about how I am using the AeroPython notebooks in an Aerodynamics class at GW.

  • Luke Mondy

    Hi Lorena, I'm a PhD student in Australia, doing long-term geodynamic modelling, and these tutorials are fantastic. Very clear and enjoyable.
    Thanks very much!

    • http://lorenabarba.com/ Lorena A. Barba

      Thanks for taking the time to leave a comment, Luke!
      I'm very glad the materials are helpful to you. There are more notebooks coming, but it's been hard to find the time to finally finish and release them. I'll announce on Twitter when I do.

  • Rajib Anwar

    Great tutorials.. It was very helpful. I was wondering if it is possible to provide some tutorials on immersed boundary method?

    • http://lorenabarba.com/ Lorena A. Barba

      Hi Rajib,
      Thanks for your comment. I'm glad you find these helpful. Next time I teach this class, I will add new materials. For the time being, you might be interested to look at our research code called "cuIBM" and the technical report on figshare documenting its verification & validation. Search our Publications.

  • Oscar Aguilar Gutiérrez

    Hello Prof. Barba! I find your website really fascinating and helpful especially for following the iTunes U course. I am just starting too learn CFD and I have a question about the codes of the early steps (1 - 3) I can't find where are the boundary conditions that according to the videos should be u(0,t) = 0 and u(2,t) = 0 in the code, so I would like to know in which part of the code are the BCs? Thanks in advance!

    • http://lorenabarba.com/ Lorena A. Barba

      Ah, to be honest, we are just ignoring the BCs in the code for steps 1 to 3. This means that the code is only valid while the wave does not hit the right boundary, but we just went for a simple code here. It would probably be a good idea to add at least an explanation about this.
      Thanks for the comment!

  • Luis Felipe Gutierrez M

    Congratulations, it's a very good material for learning and teaching cfd, and python is a wonderful selection to introduce concepts without getting lost in details related to programming.

  • Bran Liang

    This is one of the most awesome lessons in the world!!Exactly what i want!!!And you published it online for free!!!Really appreciate... And i will try to let more people around me to know your work^_^

    • http://lorenabarba.com/ Lorena A. Barba

      Well, you really sound happy to find *study materials* online :-)

  • Dan Sandiford

    Hi Lorena,

    In Step 6 the 2D non-linear convection equation becomes a pair of coupled equations. The linear version, on the other hand, is written with only one independent variable. I don't quite follow the maths here, could you explain how you get there, is there a derivation somewhere I could look at?

    Wonderful tutorial, thanks very much.

    • http://lorenabarba.com/ Lorena A. Barba

      Hello, Dan.

      I'm glad you find these useful.

      Did you see the YouTube lesson that goes over the derivation? This might help ...

      http://youtu.be/tUg_dE3NXoY

  • Robson Nazareth

    Hi Prof. Lorena, I am really enjoying your lessons and this is being very helpful to me. I have a question about the discretized pressure-Poisson eqn: Why the discretized pressure-Poisson eqn in the step 11 is differente than that found at the end of the youtube video lesson 11? In the step 11 it looks like a descretized version of the the continuous pressure-Poisson eqn plus the term rho*d/dt(du/dx + dv/dy), which should be equal to zero to satisfy continuity in the continuous case.

    • http://lorenabarba.com/ Lorena A. Barba

      Hi Robson,
      Thanks for your comment and I'm glad the lessors are helpful to you.

      Ah, you touch on a subtle issue that—among others—makes the Poisson equation the source of lots of arguments.

      First, we have to be careful when we are dealing with the *continuum* and *discretized* equations.

      In the video lesson 11, we write out the 2D momentum equation component-wise, apply the divergence operator, write out all the terms, and cancel out the terms with Del(u) because they are zero by continuity. For the *continuum equations*, the momentum and continuity equations imply the following pressure-Poisson equation (see screenshot attached)…

      This derivation is the easiest to understand and follow, and that's why we do it in the lesson. But we have to realize that in the *discrete equations* we cannot be sure that the divergence of the velocity is zero, because of possible numerical errors. So we keep the divergence terms —at least some of them— and we don't' eliminate the time derivative term.

      We work with a semi-discretized form of the momentum equation (in vector form) towards the end of video lesson 11, and we kept all the terms with divergence, viewing the pressure-Poisson equation as a correction. But there are many versions of this!

    • http://lorenabarba.com/ Lorena A. Barba

      Hi Robson,
      Thanks for your comment and I'm glad the lessors are helpful to you.

      Ah, you touch on a subtle issue that—among others—makes the Poisson equation the source of lots of arguments.

      First, we have to be careful when we are dealing with the *continuum* and *discretized* equations.

      In the video lesson 11, we write out the 2D momentum equation component-wise, apply the divergence operator, write out all the terms, and cancel out the terms with Del(u) because they are zero by continuity. For the *continuum equations*, the momentum and continuity equations imply the following pressure-Poisson equation (see screenshot attached)…

      This derivation is the easiest to understand and follow, and that's why we do it in the lesson. But we have to realize that in the *discrete equations* we cannot be sure that the divergence of the velocity is zero, because of possible numerical errors. So we keep the divergence terms —at least some of them— and we don't' eliminate the time derivative term.

      We work with a semi-discretized form of the momentum equation (in vector form) towards the end of video lesson 11, and we kept all the terms with divergence, viewing the pressure-Poisson equation as a correction. But there are many versions of this!

      (trying the comment again, to attach the image)

      • http://lorenabarba.com/ Lorena A. Barba

        The image now, second try ...

        • Robson Nazareth

          Hi Prof. Lorena,
          Thank you for reply.

          I saw that from the semi-discretized form of the momentum equation (1) to the semi-discretized form of the pressure-Poisson equation (2), it is disconsidered the high order derivatives terms (3) (attached). If this is correct, which is the assumption to disconsider this terms?

        • Guest user

          I'm also curious why the terms mentioned by the guest author are dropped. I understand how I could rearrange the equation to get all of the dropped terms as products of divergence, but since we're attempting to ensure that divergence is zero, this elimination does not make sense to me.

          You mention in your videos that the viscosity term is small, and can often be ignored, which eliminates some of the terms, but not all of them. Even with the elimination of the viscosity terms, I still see the terms in the attached image.

          Thanks for taking the time to post these lessons, I've found them very helpful.

          Edit: Apparently, no image. Here's a link: http://i.imgur.com/bCkTPgZ.png

  • Jared

    Hi prof. Lorena,
    thank you so much for your work. I really enjoyed myself studying this application of numerical analysis, and I had the occasion to brush up long forgotten skills.
    Could I ask you which are the implications behind using an implicit scheme for the time derivation? I'd really like to implement a backward Euler, a Crank-Nicolson, or even a Runge-Kutta! Could you recommend me some articles that deals with this problem, any book or online resource?

    Thank you.

    • http://lorenabarba.com/ Lorena A. Barba

      Hi Jared,
      Coming soon, we'll have several other lessons on numerical methods, as I'll be teaching that class in the Fall'14 semester. Everything will be open on the web, and you'll be able to follow our course. Keep an eye out for announcements!

      • Jared

        Thank you! I can't wait!

  • http://lorenabarba.com/ Lorena A. Barba

    You can make comments on our new home on Github, using either an Issue or a pull request. Enjoy!

  • Rocko Brown

    I am having trouble getting plots to show inline. Could someone tell me where I could find help with this?

    • Rocko Brown

      Got it - love these lessons! Thank you!

      • http://lorenabarba.com/ Lorena A. Barba

        Glad you solved it, and glad you're liking the lessons. There'll be more coming this Fall semester, when I teach an numerical methods course, so stay tuned!

  • Alberto Lozoya Rodríguez

    Your course is amazing and I'm learning a lot. But I have some doubts.
    In the lesson 11, when you obtain the graphic, I have noticed that you delete
    even rows. Could you explain why? I think it must be the decoupling between
    velocity and pressure. Is really like this?These rows are deleted later, but
    what is the implication of keeping on the integration domain?

    • Gil Forsyth

      Hi Alberto,

      Are you referring to this line?

      plt.quiver(X[::2,::2],Y[::2,::2],u[::2,::2],v[::2,::2])

      The quiver function is drawing the arrows that show the velocity throughout the cavity. Every other arrow is skipped because otherwise there are so many arrows on the plot that it's hard to see what's happening with the pressure contours below.

  • Joe Splink

    Hi Prof. Barba,

    I made it to the 12th step, i.e. completed step 11 in MATLAB, and am reporting in as directed. Truly a wonderul series of lessons. Innovative in important ways - a course in CFD with actual computations ! Making MATLAB/Python integral to the course is the only way to go IMO. The 'flipped classroom' has got to be a huge improvement over past practices.

    A comment: I was quite sure this thing was not going to work as I when I modified some of the early projects, like setting c=-1 in step 1, the thing blew up, so since the fluid goes every which I way in step 11 I figured something would blow up. But, it worked like a charm. What happened?

    Also, my goal is step 13, the backward facing step. That was my CFD goal from the start, and I'm feeling pretty close. The sim for step 11 seems very robust. I'm thinking I can modify the geometry on step 11, give it a dp/dx = 0 and u some appropriate profile at the inlet (do I have to specify dp/dy?), and dp/dx = du/dx = 0 (again I'd like to let dp/dy float) at outlet, and it should work. Suggestions? The backward facing step would be a very impressive demonstration. I don't see step 12 as contributing so I'm thinking I'll skip that step.

    Thanks for your great and innovative course.