Monday, 4 June 2012

Import FORTRAN subroutine into Python

          We always want that our FORTRAN subroutines to work in python. Since i could not find any of that anywhere on net i thought i would give it a try . 

           So before starting lets check out things we have we should have a gcc compiler , gfortran   and f2py installed in our ubuntu linux systems. I would suggest you to check the system by typing f2py in the terminal. Usually it comes installed with the python. Also ubuntu system have python installed by default these days . 

         The next thing you can do is to write a subroutine in FORTRAN and this is advisable as we are going to convert it into FORTRAN object . 
         So Lets write a  subroutine in FORTRAN. It is as follows: 
Loading ....

        Then let us build it in c and then import this c module into FORTRAN using this shell command 
Loading ....

Here the the command builds (see -c flag, execute f2py without arguments to see the explanation of command line options) an extension module circle1.so (see -m flag) to the current directory. Now, in Python the FORTRAN subroutine circle1 is accessible via circle1.circle1

If incase an error like this comes :
Loading ....

Then just install python 2.7-dev

sudo apt-get install python2.7-dev

Build again and then no error should come.
 
In the command lets call the python interpreter to check whether we can import FORTRAN subroutine circle1 : 
Loading ....






No comments:

Post a Comment