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$
  • 2
    $\begingroup$ "Modern Fortran" $\endgroup$ – meawoppl Jul 31 '15 at 22:01
19
$\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$
  • 8
    $\begingroup$ That's right. Or use Cython instead of ctypes as described on the fortran90.org page. $\endgroup$ – Ondřej Čertík May 22 '12 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$ – astrojuanlu May 30 '12 at 11:22
6
$\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$
  • 1
    $\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 Jul 30 '15 at 14:51
  • $\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 Oct 29 '16 at 4:22
5
$\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$
  • $\begingroup$ So rather than use Fortran use Cython for the slower bits of code? $\endgroup$ – James Tocknell May 26 '12 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 Jun 5 '12 at 17:35
3
$\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$
2
$\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$
1
$\begingroup$

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

$\endgroup$
  • 3
    $\begingroup$ The last commit to this project's GitHub repo was late 2010. $\endgroup$ – Geoff Oxberry Apr 18 '13 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 Apr 18 '13 at 12:20
1
$\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$
0
$\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, privacy policy and cookie policy

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