-
Notifications
You must be signed in to change notification settings - Fork 2
Python and Fortran
A short guide on using f2py to call Fortran subroutines from Python
Fortran to Python Interface Generator with an Application to Aerospace Engineering - uses f2py
Integrating Fortran with PyNE - a Spatial Solver, uses the CMAKE build system to create the f2py interface (specifies CMAKELISTS.TXT files). PyNE is The Nuclear Engineering Toolkit, a suite of tools to aid in computational nuclear science & engineering.
Compiling multiple modules with f2py
Calling Fortran routines from Python - This tutorial gives a quick introduction to the F2PY package.
Using fortran from python - The most convenient way to experiment with python + fortran is fortran-magic for ipython.
Binaries available for gfortran - GCC Wiki
Homebrew Custom GCC and Cross Compilers Docs - Use this to download to Mac OSX
F2PY Users Guide and Reference Manual
Three ways to wrap Fortran or C functions to Python
Using F2PY
Using F2PY bindings in Python
Using Python as glue - SciPy explanation
$ python -m numpy.f2py
The quickest way to wrap the Fortran subroutine FIB to Python is to run
$ python -m numpy.f2py -c fib1.f -m fib1
Flags:
-c : Compiles fortran files and build extension files (fib1.cpython-36m-darwin.so)
-m : Name of the output module (fib1.pyf)
When to use this method: Suitable for wrapping (e.g. third party) Fortran codes for which modifications to their source codes are not desirable nor even possible.
First, create a signature file from fib1.f by running
$ python -m numpy.f2py fib1.f -m fib2 -h fib1.pyf
Flags:
-h : Writes signatures of the fortran routines to file (.pyf files) and exits. You can then edit and use it instead of . If ==stdout then the signatures are printed to stdout.
Second, edit the fib1.pyf file, saved as fib2.pyf. (See docs.)
Then compile
python -m numpy.f2py -c fib2.pyf fib1.f
When to use this method: If editing Fortran codes is acceptable, then the generation of an intermediate signature file can be skipped in most cases. Namely, F2PY specific attributes can be inserted directly to Fortran source codes using the so-called F2PY directive. A F2PY directive defines special comment lines (starting with Cf2py, for example) which are ignored by Fortran compilers but F2PY interprets them as normal lines. (See docs.)
python -m numpy.f2py -c -m fib3 fib3.f
--help-fcompiler : List available Fortran compilers and exit
--f77exec= : Specify the path to the F77 compiler
--f90exec= : Specify the path to the F90 compiler
Notes from Fortran to Python Interface Generator with an Application to Aerospace Engineering - uses f2py
Wrappers created with f2py enable the following operations:
• Call Fortran routines from Python
– Fortran 77/90/95 routines
– Fortran 90/95 module routines
• Access Fortran data from Python
– Fortran 77 common block variables
– Fortran 90/95 module variables
• Call Python functions from Fortran
LFortran - LFortran is a modern open-source (BSD licensed) interactive Fortran compiler built on top of LLVM. It can execute user’s code interactively to allow exploratory work (much like Python, MATLAB or Julia) as well as compile to binaries with the goal to run user’s code on modern architectures such as multi-core CPUs and GPUs.