I have started using python as the programming language for doing all my assignments in CFD. I have a very little experience in programming. I am from mechanical engineering background and am pursuing higher education in Aerospace engineering.

sometimes the computing aspect of CFD becomes more tedious than manipulating the equations or doing the math.

What are general guidelines that make run our program faster? What are the tricks to do things parallely? How to write the codes that run faster?

Where do I get resources (easy to understand for a layperson like me) that answer above questions?

  • $\begingroup$ possible duplicate of What are some good strategies for improving the serial performance of my code? $\endgroup$
    – Dan
    Jun 4 '12 at 8:09
  • $\begingroup$ @ Dan, I don't think so. I am asking for 'any' possible resource that will help understand a newcomer programming tactics. I don't have any specific requirements or conditions imposed. More specifically I am asking for the resources that will help making codes more elegant. $\endgroup$
    – Subodh
    Jun 4 '12 at 8:15
  • $\begingroup$ Are you fixed on python or would you consider C++? In this case I would suggest two things: learn C++, find an open source library (in my case OpenFOAM), don't develop things from scratch, but learn, dig through an advanced piece of code, learn about its aspect, change and experiment, all driven with a specific purpose: in your case, e.g. aerodynamical simulations. $\endgroup$
    – tmaric
    Jun 4 '12 at 10:30
  • $\begingroup$ @tomislav-maric, thank you very much. I am not a strict 'pythonian'. In fact, being a newcomer to the field, I think I have plenty of options available in front of me. I am learning OpenFOAM too. So I agree with your views that one should start working on a project and learn through that (or in short get hands dirty), which is precisely what I should do!Thanks. $\endgroup$
    – Subodh
    Jun 4 '12 at 10:48
  • $\begingroup$ @smj btw, I come from a mechanical engineering bacground as well and I'm now a graduate student in computational science (mech. engineering did NOT prepare me for this)... If you are working with OF, search for the C++ book list on stack overflow, and start learning. You need 3 things: C++, OpenFOAM and knowledge from computational science. OpenFOAM and computational science, you can learn in a dendritic way: find an assignment and get it done, learning along what you need. Having something finished will motivate you. As for C++: start with the C++ Primer and learn it. Good luck! :) $\endgroup$
    – tmaric
    Jun 4 '12 at 10:54

I will try to answer your question considering that you are asking for Python specifically. I will describe my own method of tackling a simulation problem. Strategies for faster simulations are given in this description.

First, I prototype new simulations in Python. Of course, I try to make use of NumPy and SciPy as much as I can. Whereas NumPy provides a suitable array data type for numerical simulations, SciPy offers a wide numerical routines working with NumPy arrays.

Once the prototypes work more or less, I try to learn which parts of the program or script are the bottleneck. There are typical candidates for that:

  • Loops in Python are slow. Very slow.
  • As Python uses duck typing, calling functions can be slow.

I use a simple profiling strategy to learn where all the run time is spent. Using the IPython shell (which I cannot recommend enough), I run my script with

%timeit script.py

This "magic command" will do the profiling (using timeit) for you and present you a list with times once your script has terminated. Use this list to find out where your code is too slow.

Once you nailed down the parts that need to be speeded up, you may consider using compiled languages. I will point to two solutions.

First, there is the Cython language. Cython is a programming language very similar to Python (in fact, Python code is often valid Python code, too); however, the Cython compiler converts the Cython files to C code, which then may be compiled into a module usable from Python. Cython understands NumPy arrays. There are two ways in which using Cython can help you: first, you may introduce data types. This will speed up function calls. Also, if you iterate over arrays, your loop will run faster (in fact, if you type both the dummy variable and the array, you get a plain C loop!). Second, in my experiments even the untyped scripts run a bit faster due the fact that they're compiled instead of interpreted.

The other compiled language that will be useful for you is Fortran. There are different ways to use Fortran with Python (f2py, fortwrap, Cython). As to me personally f2py seems to be the easiest way, I'll quickly describe what it does. f2py can compile Fortran code to Python modules. It will allow you to use NumPy arrays as input and output variables from Python space. In Fortran space, these will be ordinary Fortran arrays. You can operate on those at full Fortran speed.

Personally, I tend to use Cython where the number of function calls is the bottleneck. For loop-heavy stuff, I prefer f2py (maybe because I have a strong Fortran background).

On additional note on Fortran: modern Fortran reads and writes very similar to NumPy - the syntax is very close. This makes it easy to convert NumPy code to Fortran code.

Note that both Cython and f2py support paralleism in some way. For Cython, you'll find help here, whereas for Fortran, there are the standard techniques such as OpenMP or MPI. Furthermore, there are Python wrappers for MPI, too. Personally, I use mpi4py on the Python level as well as OpenMP in Fortran.

Let me recommend a bit of literature: the book Python Scripting For Computational Science by H.-P. Langtangen is a great resource on Python in general as well as on strategies to make Python a bit faster. Unfortunately, AFAIR, it does not mention anything on Cython. As I second resource you may look at these slides. These give examples for everything I mentioned in this post (see also the code and sources here). There are many other good set of slides on the internet.

If you have more specific questions, we are all happy to help!


For CFD+Python there is a solution: http://pythonflu.wikidot.com/ These are Python-bindings on top of OpenFOAM (which was already mentioned in the comments to the questions). These bindings allow programming on "solver level" (there are examples where original OpenFOAM-solvers are replicated in Python and they are not slower than the originals - the slow loops mentioned in another answer are not a problem here as the "inner loops" happen in the C++-code of OpenFOAM).

The advantage of these bindings is also that all the parallelization in OpenFOAM happens beneath the solver-level, so you don't have to bother with it (as well as the other stuff that the OpenFOAM-core takes care of: input/output, linear solver, operator discretization)

So if you just want to write a new solver and not add new features to the OF-core (boundary conditions, linear solver, etc) then PythonFlu might be sufficient for you and you can avoid C++ (which has a much higher learning cure than Python)

PS: originally wanted to add this as a comment to the discussion of the original question, but my reputation doesn't allow me this

  • $\begingroup$ Hi Bernhard! Welcome to scicomp! :) $\endgroup$
    – tmaric
    Jun 5 '12 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.