JavaScript is not enabled in your browser
Objexx Engineering

Fortran to Python Conversion

Objexx Engineering provides Fortran to Python conversions using the same Fortran parsing engine that is used for our Fortran to C++ conversions. The conversion process preserves the structure and syntax of the Fortran to retain the value of the existing documentation and developer knowledge. The converted Python is clear and maintainable.

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

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 replacing array loops with NumPy calls, using numerical expression packages such as numexpr or packages that compile to fast native binary code such as Cython and Numba. Parallelization can also be applied when appropriate.

When Performance considerations are important Objexx can do a small profiling/conversion/tuning study on sample code to get a solid assessment of the likely performance potential of the converted Python. The benefits of a Python code base outweigh the performance considerations for many applications.

Python Evolution

Conversion to Python is often a first step in a code evolution plan. Objexx can perform refactoring and/or provide team mentoring to help clients migrate the converted Python to exploit the capabilities of the language, such as improved modularity via object-oriented design and improved testability.

Technical Notes

Some of the technical aspects of our conversion include:

  • Variable names and comments are preserved.
  • Arrays can be converted to NumPy arrays, with necessary indexing adjustments, or to ObjexxFPL Array classes that wrap NumPy arrays and preserve the original Fortran indexing.
  • Fortran-like strings that are provided by the ObjexxFPL can be optionally used for some or all of the Python conversion.
  • DATA, SAVE, and PARAMETER attributes are handled cleanly with native Python constructs/idioms.
  • COMMON blocks become module globals.
  • EQUIVALENCEs and COMMONs that can introduce aliasing are detected and converted as appropriate for the intent of the usage.

The Objexx Fortran-Python Library (ObjexxFPL) provides array, string, i/o, and intrinsic function support for the converted Python. The ObjexxFPL is provided in Python source form with an unrestrictive, royalty-free license. ObjexxFPL is in use in major codes and has been heavily tested and vetted for correctness.

Conversion Sample 1

This sample shows the conversion of a simple Fortran 77 routine to Python. The philosophy of the approach 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.

  • The NumPy array option was used for the Python generation and the element subscripting is adjusted automatically for zero-based indexing.
  • This and other options are available to customize the conversion to best meet project requirements.

Conversion Sample 2

This sample demonstrates more of the typical conversion steps for legacy Fortran. The original Fortran is "straight-line" code using GOTOs and an archaic arithmetic IF statement for control flow. Such code is hard to follow and Python doesn't provide a GOTO mechanism so a Fortran modernization pass is performed before the Python conversion step.

The steps in this conversion include:

  1. Test the original Fortran.
    • Write test cases to provide safety net.
    • Static testing to identify potential bugs/issues.
    • Build with options to help expose bugs.
    • Run with max debug build to catch bugs via runtime errors.
  2. Modernize the Fortran.
    • Convert to free format Fortran source.
    • Run restructuring tool with appropriate options.
    • Modernizes code and may identify additional problems.
    • Replaces GOTOs with modern Fortran DO loops and IF constructs.
    • Review code and add F>Py comments to flag items of concern.
    • Build/test to validate the modernized Fortran.
  3. Convert to Python.
    • Run conversion system to generate the Python.
    • Review and clean/refine Python if/as needed.
    • Convert test cases to Python.
    • Test/validate the Python version.

Sample matching Fortran and Python test case drivers are shown. When run they produce the same result: 257.879.

Conversion Projects

Some recent Fortran-to-Python conversion projects are described below.

Building HVAC and Sprinkler Design

Objexx converted building HVAC and sprinkler system design applications from Fortran to Python for an Australian company.

  • The applications consisted of 7.4 MB of Fortran 95 source code.
  • A thorough testing/debugging phase was conducted on the Fortran prior to conversion.
  • Objexx provided the client recommendations and support for increasing the performance of the Python versions.