27
$\begingroup$

I know of, and have used f2py2e to wrap some old Fortran 77 code, but my understanding is that it does not work with newer Fortran 95 code. I've researched what I should use, and have come across fwrap and G3 f2py, neither of which seem to give any explanation of their current state, or how to use them (beyond basic usage).

I have also seen that the version of f2py has the option to use the third generation f2py, but it is commented as being non-functional. Given this, I don't know which project I should use for a uni project. Which one should I use for new code?

P.S. This is basically the same question as https://stackoverflow.com/questions/10665717/current-best-method-for-wrapping-modern-fortran-code-with-python, it was suggested that asking here might give better answers.

$\endgroup$
1
  • 2
    $\begingroup$ "Modern Fortran" $\endgroup$
    – meawoppl
    Commented Jul 31, 2015 at 22:01

8 Answers 8

20
$\begingroup$

You can use the Python builtin ctypes module as described on fortran90.org. It is pretty straight forward and doesn't require any external dependencies. Also, the ndpointer arg type helper is very handy.

$\endgroup$
2
  • 9
    $\begingroup$ That's right. Or use Cython instead of ctypes as described on the fortran90.org page. $\endgroup$ Commented May 22, 2012 at 5:28
  • $\begingroup$ @OndřejČertík, great website! There are few valuable resources on modern Fortran on the Internet, but this is definitely one of my favourites along with fortranwiki.org $\endgroup$
    – user782
    Commented May 30, 2012 at 11:22
7
$\begingroup$

I personally use f2py for this. However, the Fortran I write for f2py is not really modern Fortran, but rather free-form F90 with modules. I do not use assumed shapes or custom types. This method still works very well for me.

There is a resource I want to share concerning the use of C as part of the wrapping. The rest of this page is recommend, too.

$\endgroup$
2
  • 3
    $\begingroup$ 2015 update: since quite a while, all my Fortran interfacing happens through Cython and iso_c_binding. This allows to keep the Fortran code itself modern while exposing only what's really needed to Cython/C. $\endgroup$
    – AlexE
    Commented Jul 30, 2015 at 14:51
  • 1
    $\begingroup$ 2016 update: the iso_c_binding option is getting easier now than I can just compile a .pyx file using the cython command, and reducing the overlap between changing either my fortran code or python code and quickly getting everything to work. $\endgroup$
    – cbcoutinho
    Commented Oct 29, 2016 at 4:22
6
$\begingroup$

The documentation of f2py is really not that great. I'm in the process of moving my own projects from f2py to Cython. Even though Cython can be used to wrap C code, I found that it is most useful for creating new high performance code. If your intent is to write new Fortran code, wrap it, and call it in Python, I recommend that you use Cython instead. That way you can write Python code, and if there are performance bottlenecks, you transfer that over to Cython with minor modifications.

$\endgroup$
2
  • $\begingroup$ So rather than use Fortran use Cython for the slower bits of code? $\endgroup$ Commented May 26, 2012 at 3:17
  • 2
    $\begingroup$ Exactly! I've found that it's much easier because your Cython code can interact with Python objects and Numpy arrays naturally (without an extra conversion layer). This is specially helpful if you want to use your own data structures. Making Fortran or C use you Python classes in f2py is not such a straight-forward task. $\endgroup$
    – Leo Uieda
    Commented Jun 5, 2012 at 17:35
4
$\begingroup$

f90wrap is build on top of f2py and automatically generates the Python wrappers for the underlying Fortran code base, including access to derived types. A set of simple examples show how it works.

$\endgroup$
3
$\begingroup$

You could use FortWrap to get you halfway there. This will create a C++ interface to your Fortran code, which could then be wrapped with SWIG to get a Python interface. We do this on a somewhat large base of Fortran code and it works quite well (in fact, FortWrap was developed by a member of our team for this exact purpose).

$\endgroup$
2
$\begingroup$

fwrap specifically targets Fortran90/95 and uses some of the same software stack as f2py.

$\endgroup$
2
  • 3
    $\begingroup$ The last commit to this project's GitHub repo was late 2010. $\endgroup$ Commented Apr 18, 2013 at 5:31
  • 1
    $\begingroup$ While I agree that that's discouraging I've still had decent success with it. Out of the box it's more functional than f2py on F90. It also has a very low entry barrier. I was glad to have found it. $\endgroup$
    – MRocklin
    Commented Apr 18, 2013 at 12:20
2
$\begingroup$

f2py supports modern fortran (assumed-shaped arrays and so), see an example script

Also, for experiments with fortran code I'd recommend IPython magic, which is easy to install and very convenient (it relies on f2py as well).

$\endgroup$
1
$\begingroup$

Here is a list of Fortran wrappers for Python:

https://github.com/mgaitan/fortran_magic/issues/4

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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