fortran-python

Fortran-Python

Modernizing interoperability between scientific computing’s two most important languages

Context

Most production codes in computational chemistry, physics, and climate science are written in Fortran. Most analysis, visualization, and workflow tooling is written in Python. Bridging the two without losing performance or type safety is a persistent problem.

I spent two years (2021-2023) as a core contributor to f2py within NumPy, including a Google Summer of Code mentorship on the LFortran project. The work covered three interconnected problems: derived type interoperability, build system modernization, and the Fortran language ecosystem itself (Kedward et al. 2022).

Derived types across the language boundary

Fortran derived types had no first-class representation in f2py. My work explored three strategies, each documented in a series of blog posts:

  1. Direct bind(c) shadowing: For C-compatible types, a C struct mirrors the Fortran type exactly. No copies, no indirection.
  2. Opaque pointer wrapping: For arbitrary types, the Fortran side exposes handles (c_ptr values) and accessor functions. Python stores these in PyCapsule objects.
  3. Python class generation: A full PyTypeObject for each derived type, with type-bound procedures becoming Python methods.

Build systems and packaging

Distributing compiled Fortran extensions through pip and conda-forge requires working build systems. I contributed to the meson-based build for NumPy and developed packaging patterns for mixed Fortran-C-Python projects (Goswami 2022).

Code

  • f2py (NumPy) – Core contributor (2021-2023), GSoC mentor
  • wailord – Creator; quantum chemistry workflow automation

References

Goswami, Rohit. 2022. “Wailord: Parsers and Reproducibility for Quantum Chemistry.” Proceedings of the 21st Python in Science Conference, July, 193–97. https://doi.org/10.25080/majora-212e5952-021.
Kedward, Laurence, Balint Aradi, Ondrej Certik, Milan Curcic, Sebastian Ehlert, Philipp Engel, Rohit Goswami, et al. 2022. “The State of Fortran.” Computing in Science & Engineering, April. https://doi.org/10.1109/MCSE.2022.3159862.

← All research threads