JavaScript is not enabled in your browser
Objexx Engineering

Fortran to C++ Conversion

Fortran to C++ conversions are performed using a sophisticated system Objexx has developed and extended over a number of years. This conversion process preserves the structure and syntax of the Fortran to retain the value of the existing documentation and developer knowledge. The converted C++ is clear and maintainable.

Conversion Benefits and Tradeoffs

Conversion to C++ can be the right choice for a project in a number of situations. C++ is a more powerful language with better object-oriented design support and a much larger pool of developers to draw from. C++ 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 C++ application, and Objexx uses this approach for some projects. But the burden of maintaining the C++—Fortran interface and the additional complexity of a hybrid application are often not warranted.

Performance

The performance of the converted C++ code may initially be slower than the Fortran but can be brought up to parity with a modest tuning effort. The ObjexxFCL arrays have a built-in fast lookup method that can go a long way towards bringing the speed back to that of the Fortran, or in some cases exceeding it. Objexx can do further performance enhancement of converted code, typically achieving speeds well beyond that of the Fortran.

When Performance considerations are important Objexx can do a small conversion+tuning study on sample code to demonstrate the performance of the C++ vs. that of the Fortran.

C++ Evolution

Conversion to C++ 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 C++ 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:

  • Reference (not pointer!) argument passing for clear code that corresponds to the Fortran.
  • Variable names and comments are preserved.
  • Fortran-like array template classes provide column-major arrays that preserve the element indexing of the Fortran and Fortran's array passing "tricks".
  • Fortran-like string and substring classes are provided by the ObjexxFCL that work smoothly with C++ strings.
  • DATA statements become constructor initializers.
  • PARAMETER, SAVE, VOLATILE, INTENT and other Fortran declaration attribute semantics are preserved.
  • COMMON blocks are converted to namespaces.
  • EQUIVALENCEs and COMMONs that can introduce aliasing are detected and converted as appropriate for the intent of the usage.

The Objexx Fortran-C++ Library (ObjexxFCL) provides array, string, i/o, and intrinsic function support for the converted C++. The ObjexxFCL is provided in C++ source form with an unrestrictive, royalty-free license. ObjexxFCL is in use in major codes and has been heavily tested and vetted for correctness. ObjexxFCL arrays provide high-performance tuning capabilities and some unique dynamic sizing capabilities that can be exploited to great effect in meeting post-conversion modernization and refactoring goals.

Conversion Sample 1

This sample shows the conversion of a simple Fortran 77 routine to C++. 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 C++.

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 so a Fortran modernization pass is performed before the C++ 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>C++ comments to flag items of concern.
    • Build/test to validate the modernized Fortran.
  3. Convert to C++.
    • Run conversion system to generate the C++.
    • Review and clean/refine C++ if/as needed.
    • Convert test cases to C++.
    • Test/validate the C++ version.

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

Conversion Projects

Some recent Fortran-to-C++ conversion projects are described below.

Hydrodynamic Wave Modeling

Objexx converted four coastal hydrodynamic wave and tidal modeling applications from Fortran to C++ for a research project at the National Kaohsiung University of Science and Technology Department of Marine Environmental Engineering. The code is a mix of legacy and modern Fortran. Array bound violations, use-before-initialization, argument mismatch, and other bugs were found and fixed as part of the conversion process.

SCADA Control System

Objexx converted a smart grid SCADA operations control system from legacy Fortran 77 to C++ for Survalent Technology Corporation.

EnergyPlus

Objexx converted the U.S. Dept. of Energy EnergyPlus application from Fortran 90/2003 to C++ under contract to Autodesk. EnergyPlus is open source and the code is in the EnergyPlus GitHub repository. The C++ EnergyPlus was quickly validated and tuned to exceed the performance of the Fortran version and continues under active development.

Rosetta

Objexx converted the Rosetta biomolecular modeling system from Fortran 77 to C++ for an academic consortium. Rosetta continues to be a vibrant research project with a wide developer base and successfully using object-oriented C++ to expand its scope and capabilities.