Tag Archive | tuto

Python for Fortran programmers I: why?


Being trained as a scientist (as I am), you have a certain probability of having been taught Fortran. Are there reasons to try to learn another language? And why Python?

I use computers all the time. But most of the computationally intensive tasks that run in my cluster are performed by highly optimized codes that I have not written: Gromacs, Orca, Gaussian, etc. What I usually need to do is to play with the data these codes generate. Here is where I frequently need to code something. But playing with the data rarely involves only number crunching. Okay, sometimes I may need to do an interpolation, or a least square fitting, but much more often I need to graph the results, or parse software outputs to get these data. So, should I use Fortran?

For the number crunching, a naïve answer would be ‘yes’. I disagree. If what you want to do is a common task, such as the ones mentioned (or a diagonalization, or a minimization, or…) you can probably find packages or libraries where that is already implemented. So why reinvent the wheel? You can find that in Fortran, but you can find that in Python too. And the installation, the documentation and the ease of use will be better in Python. Do you want an example? I need the eigenvalues of a matrix stored in a text file ‘a.dat’. And you only have python installed. Nothing else.

$ sudo apt-get install python-numpy
$ python
>>> import numpy 
>>> a=numpy.loadtxt('a.dat') 
>>> numpy.linalg.eigvals(a) 
array([ 16.13476875,  -8.29479695,  -0.3399718 ])

Now, beat that with Fortran… Of course, sometimes you’ll have to code your computationally intensive task –we are scientists, we are supposed to be doing new things–, and Fortran is an efficient language for that.

But most of the time I am not solving linear equations. Most of the time I am struggling to get the energies of a set of outputs and correlate that with distances that are in a set of trajectory files. Or to modify tens of input files in a systematic way. Or to plot these results automatically, without manual copy&paste to a spreadsheet. And for that, Fortran is almost useless.

As John D. Cook puts it: “I’d rather do math in a general-purpose language than try to do general-purpose programming in a math language”

I am not saying you should abandon Fortran. I am just advocating for another tool that can ease many of your common tasks.