Skip to content

Working with Fortran in 2020: Areas of application

Fortran – the proven programming language for technical applications

by DI Dr. Christoph Hofer

The name Fortran is composed of “FORmel TRANslator” and is one of the oldest programming languages. For many software developers, it is the archetype of an old, ponderous, limited and difficult-to-understand programming language with which one would best not have anything to do. For the old versions of Fortran, this prejudice may indeed be true. However, Fortran has changed a lot in its long history, so that in its “modern” variant (such as Fortran 2003) the language has a much worse reputation than it deserves. The typical use case for Fortran as a programming language is computationally intensive numerical simulations, such as weather forecasts, flow simulations, stability calculations, and many more.

Table of contents

  • From old to new
  • Compilers: The agony of choice
  • Why is Fortran still used today?
  • Fortran/C/Python integration
  • Author
Programming

From old to new

Fortran is considered to be the first higher programming language ever realised and was developed by IBM in the years 1954 – 1957 (FORTRAN I). The scope of the language was still very limited, for example, there were only integers and reals (floating point numbers) as data types and no functions yet. In the following years, new improved and more extensive Fortran versions were developed (FORTRAN II, FORTRAN III, FORTAN 66). Fortran received its next major update in 1977 (FORTRAN 77). Due to new features in the language, this version became very popular and thus quickly became “the” Fortran. Even today, when people talk about Fortran code, they mainly mean FORTRAN 77 code, which also explains the many prejudices against the language. Since then, there have been several more updates to the language, bringing it closer to modern programming concepts and standards.

Major milestones in the development were the updates to Fortran 90 and Fortran 2003, which, in addition to the name change (FORTRAN → Fortran), added common concepts such as free source file formats, modules, operator overloading, derived data types, pointers and object-oriented programming to the programming language. In addition, Fortran 95 and Fortran 2008 were each minor updates of the language. The latest version of the Fortran standard is Fortran 2018, although no compiler manufacturer supports all features yet.

Info

Higher Programming Language

Microprocessors are programmed in a so-called machine language, i.e. a binary code that is interpreted by the microprocessor as a sequence of instructions. Since these languages are very dependent on the hardware used and direct programming in a machine language is very time-consuming, the development of higher programming languages and compilers was a great step forward. Higher programming languages use mathematical expressions or expressions based on a natural language (usually English) that are translated into machine language by a compiler (and linker). Higher programming languages are independent of the hardware, the adaptation to the concrete hardware is done by the compiler.

Operator Overloading

is a programming technique with which the meaning of operators (such as +, -, *, …) depends on the respective types used. For example, 1 + 2 returns the number 3, but “Helllo ” + “World” returns the string “Hello World”.

Derived Data Types

allow the user to define data types from existing types. This offers the possibility of defining logically related data in one type and reusing it in different parts of the programme.

Pointer

is a data type that stores the memory address of a variable instead of the variable itself. Pointers refer to a memory address, so to speak, and the extraction of the value behind it is called dereferencing. In contrast to pointers in C/C++, Fortran pointers have even more information and also allow to refer to unrelated memory areas (in the case of arrays).

Object-oriented Programming

is a programming style in which data is not only collected in Derived Data Types but encapsulated together with logic and functionality in so-called objects. Each object has defined interfaces through which it can interact with other objects, e.g. often not all data and functions of an object are visible to other objects. The aim is to avoid code duplications in order to reduce the potential for errors and the maintenance effort.

Compilers: The agony of choice

In addition to the scope of the programming language itself, a simple and pleasant workflow during development is also important for programmers, i.e. in which development environment can one test and debug the written code well with which compiler. The limited number of available development environments reinforces the impression that Fortran is no longer one of the most common languages today.

In order to programme modern Fortran (Fortran 2003/2008), one has several compilers to choose from, both open source and commercial, among them

With regard to development environments, there are IDEs specially developed for Fortran, such as the NAG Fortran Builder, or integrations for existing IDEs, such as the Intel Integration for Visual Studio, or extensions for editors, such as Visual Studio Code. In addition to the supplied debuggers (gdb, idb,..), external commercial debuggers such as Total View (https://totalview.io/) are also available for Fortran.

Laptop

Why is Fortran still used today?

Legacy Code

Many Fortran systems that are still in use today were developed in the 1980s. At that time, Fortran was the language for scientific computing and numerical simulations. Such programmes contain a lot of knowledge and experience, often from several generations of engineers and scientists. Transferring them to other “modern” programming languages is often far too time-consuming or expensive, and the added value for companies would hardly be given. Reengineering the programme code in a modern Fortran standard and actively developing it further is often the better alternative.

Fortran is designed for technical applications

As already mentioned at the beginning of the article, numerical simulations are the typical use case for Fortran as a programming language. These areas are characterised by the fact that on the one hand efficient programme execution is important, but on the other hand the calculations are described by mathematical formulae. Fortran gives the programmer the possibility to represent these formulas compactly in the programme code in a readable way. Furthermore, the language is designed for efficient calculations, which means that high-performance code is the rule rather than the exception.

Fortran is a simple language

Compared to C++ or scripting languages like Python, Fortran has a rather limited feature set, which is why for many of today’s business applications the development would be very tedious, but for technical/scientific applications it is very suitable. The smaller feature set also allows less experienced programmers to develop good software. Due to the many possibilities of C++20 and Python, less experienced programmers are often overwhelmed by the possibilities. Used incorrectly, complex languages often create more confusion than they contribute to solving the problem. In this sense, Fortran was designed for scientists and engineers, not for computer scientists and software developers.

Old and new

Fortran/C/Python integration

With Fortran 2003, a new possibility for easier interaction between Fortran and C code was created with the module iso_c_binding. This module includes:

  • Types of primitive data types (integers, reals, …) that correspond to the corresponding C data types (int, double, …).
  • Functions for working with C pointers, like converting C to Fortran pointers or reading the memory address of a Fortran variable.
  • Constants for non-displayable C characters, such as the newline character (\n) or the horizontal tab (\t).

Not all data types from Fortran can be transferred to the C code by means of the iso_c_binding and vice versa. For example, there is no equivalent of a C union in Fortran, or in C there is no direct equivalent of the Fortran ALLOCATABLE attribute for arrays, which allows dynamic allocation of memory that is automatically freed if the array goes out-of-scope. Furthermore, C/C++ has no support for array slices, i.e. a structured subset of an array, so copies must be created when these are passed.

In some projects of RISC Software GmbH, the requirement was to write Python interfaces for Fortran code. The programme SWIG (http://www.swig.org/), which generates code for Python/C interfaces, has proven itself for this purpose. To generate a Python/Fortran interface, the Fortran/C interface is written first and then the Python/C interface is generated using SWIG. SWIG is very flexible in its application and even allows numpy arrays as arguments of the Python functions, which leads to efficient and readable Python. By integrating it into the CMake build system, this approach works platform-independently and offers a simple and efficient workflow in development.

Whiteboard

Contact









    Author

    DI Dr. Christoph Hofer

    Professional Software Engineer Unit Industrial Software Applications