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:
- Direct
bind(c)shadowing: For C-compatible types, a C struct mirrors the Fortran type exactly. No copies, no indirection. - Opaque pointer wrapping: For arbitrary types, the Fortran side exposes
handles (
c_ptrvalues) and accessor functions. Python stores these inPyCapsuleobjects. - Python class generation: A full
PyTypeObjectfor 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
Related writing
- Derived types and their interoperability
- ISO_C_BINDING and type-bound procedures
- Fortran OOP and Python
- Types from Fortran to Python via opaque pointers
- Handling legacy Fortran code
- GSoC 2021: LFortran series