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.

Advertisements

Tags: , ,

About Ramon Crehuet

I'm a Computational Biochemist working at the IQAC institute of the Spanish National Research Council (CSIC)

2 responses to “Python for Fortran programmers I: why?”

  1. Elisabeth Ortega (@draentropia) says :

    Nice post Dr. Creuet! But I have something to add here. I’ve recently adapted a Fortran algorithm to Python in order to add a new feature to MMTK (Molecular Modelling Tool Kit).

    Fortran programmers will have their oun code written. Translate the algorithms to python can not be the short way to proceed. There is a package called F2PY (http://www.f2py.com/) which can convert Fortran modules to python libraries. With this tool we can have the speed of Fortran programming with the “user friendlyness” of Python language.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: