Fortran to Python Conversion
Objexx Engineering provides Fortran to Python conversions using our new Fortran parsing engine that is also used for our Fortran to C++ conversions. The conversion is designed to maximally preserve the original code's structure and syntax to preserve the value of the code and existing documentation and to allow a wide range of developers to maintain the code.
Conversion Benefits and Tradeoffs
Conversion to Python can be the right choice for a project in a number of situations. Python has a number of benefits including much higher developer efficiency, better support for a rapid/agile process, and a much larger pool of developers to draw from. Python also has a vast range of libraries and excellent GUI, visualization, and plotting packages to choose from.
Conversion vs. Wrapping
It is possible to get many of these benefits by wrapping a Fortran computational core in a modern Python application, and Objexx uses this approach for some projects But the burden of maintaining the Python—Fortran interface and the additional complexity of a hybrid application are often not warranted.
Performance impacts of a conversion to Python are important to understand. Despite the use of NumPy arrays the as-converted Python is likely to be significantly slower than the Fortran. For performance-critical applications, as many Fortran codes are, profiling and performance optimization should follow the conversion. Performance improvements can be obtained in a number of ways, such as using numerical expression packages such as numexpr and Theano or packages that compile to fast native binary code such as Cython and NumbaPro. More sophisticated multithreading and multiprocessing parallelization solutions can also be applied when justified.
When Performance considerations are important Objexx can do a small conversion+tuning study on sample code to get a solid assessment of the performance ceiling of the Python vs. that of the Fortran. In our experience, the benefits of a Python code base usually far outweigh the modest performance penalty.
Some of the technical aspects of our conversion include:
- Variable names and comments are preserved
- Array indexing is preserved via the provided Array classes that wrap NumPy arrays for efficiency
- DATA, SAVE, and PARAMETER attributes are handled cleanly with native Python constructs/idioms
- COMMON blocks become module globals
This conversion of a simple Fortran 77 routine to Python (shown at right) gives some sense of our approach. The philosophy of this method is to maintain the syntax and semantics of the Fortran as much as possible so that development is not significantly slowed by the transition to Python.