Current best method for wrapping Modern Fortran code with Python
Asked Answered
G

1

15

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. I have 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. Which one should I use?

This question has now been cross-posted to (and answered at) Computational Science stack exchange site as Which package should I use to wrap Modern Fortran Code with Python?

Gallium answered 19/5, 2012 at 13:48 Comment(7)
maybe calling fortran libraries through ctypes would be an option?Horse
f2py (as included in scipy) seems to claim to support Fortan95. Have you tried it?Hemihedral
You might also try asking on scicomp.stackexchange since there are a lot of people over there that work with Python and Fortran.Hemihedral
You might make progress by using modern Fortran's 'interoperability with C' features and making your compiled Fortran present a C-like interface to the outside world.Playground
f2py2e (the one in numpy/scipy) has limited Fortran 95 support (it was designed with idea of wrapping old Fortran 77 code), hence fwrap and G3 f2py were written. However, both seem partly inactive, and the documentation, especially for G3 f2py, is unclear.Gallium
@JamesTocknell, it has been 8 years now, and I am curious of how this turned out. What path did you pick, and how did that turn out?Outsell
@Outsell I ended up not using Fortran for that particular system. The Computational Science link suggested better options (e.g. export Fortran code as C and wrap via standard tools such as Cython, just write Cython), so I'd suggest looking there for more ideas.Gallium
H
9

I have used SWIG to wrap some C/C++ code before but they don't support Fortran. They link to the following, though

At least FortWrap states that they support Fortran95, but you might have to use FortWrap to get C/C++ wrappers and SWIG for the Python wrappers. f90wrap will automatically generate the Python wrappers based on the f90 sources (and supports derived types).

Hemihedral answered 19/5, 2012 at 16:57 Comment(2)
I mentioned F2PY, and I'd much rather avoid SWIG. Also pyfortran is old code which probably won't work with numpy (it's designed for Numeric), and FortWrap is designed for C++, again lacking integration with numpy.Gallium
I just discovered f90wrap thanks to this answer. You saved my day!Sphericity

© 2022 - 2024 — McMap. All rights reserved.