Summary and Setup
Caution
This is a new lesson built with The Carpentries Workbench. It is currently in pre-alpha development by the British Geological Survey, the National Centre for Atmospheric Science, the National Oceanography Centre, and the Met Office.
The lesson adapts resources from ARCHER2, Imperial College London, Modern Fortran in Science and Technology course, LRZ’s Programming with Fortran course, and more.1
Fortran was created in 1957 by a team at IBM and excels with numerically intensive science code. Far from the punchcards, and fixed-form formatting of Fortran’s early days, modern Fortran is fast, supports object orientated programming and is easy to read.
Fortran is often used to write code for High Performance Computing (HPC). Many of the research codes run on ARCHER22 are Fortran programs.3 Various areas of Science are covered by these Fortran programs. For example quantum chemistry, plasmas, and numerical weather prediction.
More recent standards of Fortran come under the umbrella term “Modern Fortran”. These are the Fortran 95 standard, and more recent standards.4
Prerequisites
Learners should be familiar with the basic concepts of programming: variables, logic, control flow, loops, functions and so on. No knowledge of Fortran is assumed. No previous experience with compiled languages is required. Access to the command line is required. Learners must be comfortable using the command line5.
This lesson has been tested primarily on Linux. Windows users may find it easier to install Windows Subsystem for Linux (WSL). With WSL you will have access to a Linux command line. Windows and macOS users should open an Issue if you find problems with the lesson material.
Discussion #3 on the lesson repository, https://github.com/carpentries-incubator/intro-to-modern-fortran/discussions/3, give details on the materials this course draws from.↩︎
ARCHER2, https://www.archer2.ac.uk/, is one of the UKs national supercomputing facilities.↩︎
ARCHER2 code use article: https://www.archer2.ac.uk/news/2021/05/19/code-use.html↩︎
Fortran standards page on the Fortran Wiki: https://fortranwiki.org/fortran/show/Standards↩︎
Learners should be comfortable with the Carpentries Unix Shell lesson material: https://swcarpentry.github.io/shell-novice/↩︎
Learning Outcomes
This course provides an introduction to writing Modern Fortran. It covers everything from basic syntax and variables, to functions, arrays, and modules. Common Fortran idioms will be introduced and compared to C++.
Within the first hour you will have:
- written a hello world program
- compiled your first program
- done basics maths in a program
At the end of the course you should be able to:
- understand many Fortran programs
- write well-structured Fortran
- write portable Fortran modules
Fortran is a rather “large” language, so it is not possible to cover all its features in this course. We focus on features used to write modern HPC code. Other features you might encounter in legacy code are explained in spoilers:
You will see spoilers like this in the lesson. They contain features more commonly used in legacy code. These spoilers explain why the feature is not used often in modern code.
The follow on lesson Object Orientated Fortran (coming soon) contains further advanced topics.
Data Sets
Download the tar file and extract the contents in your Desktop. This file contains template code and data for some exercises. It also contains solutions for all exercises.
BASH
$ mkdir ~/Desktop/intro-to-modern-fortran
$ cd ~/Desktop/intro-to-modern-fortran
$ tar -xzf ~/Downloads/exercises.tar.gz --strip-components=1
If the tar file download fails, the exercises and data sets are in this lessons GitHub repository:
The tar
file is in the root directory. Alternatively the exercises are in
the top level exercises/
directory.
Compiler Setup
Fortran is a compiled language. You have to translate your Fortran code to machine code before running the program. We do this with a Fortran compiler. You should test your code with multiple compilers, as some are better at debugging certain errors than others.
Installing a Compiler
Your instructor may provide different instructions for installing a suitable compiler or have installed one for you. If you need to install a compiler yourself follow the instructions below. We recommend installing GFortran as your first compiler.
Testing your compiler
Now you have a compiler installed you can compile a simple program.
The exercise and data file you
downloaded contains setup/hello_world.f90
. This is a simple
Fortran program:
Enter the setup directory:
OUTPUT
hello_world.f90
To compile the program run:
You should see a new file in the directory:
OUTPUT
a.out hello_world.f90
The a.out
file is your compiled program. To run the
program:
OUTPUT
Hello world!
You have compiled your first Fortran program! The first episode in this lesson will cover compiling in more detail.
Text Editors and Integrated Development Environments (IDEs)
Fortran files are plain text and can be edited with any plain text editor. We recommend you use an editor that is capable of syntax highlighting. Setup instructions for some editors are available below.
Editor Setup Quick Links:
Emacs
Emacs is a cross-platform text editor available for Linux, macOS, and Windows. Emacs applies syntax highlighting for most Fortran files. Emacs f90-mode mode lets you tab complete Fortran statements. It also adds a drop-down menu providing additional Fortran-related options.
Fortran files with different extensions, such as .X90
,
will not be picked up in this mode. Files can be manually set to
f90-mode using M-x f90-mode
. File
extensions can be added to your Emacs
initialization file so they are recognised as Fortran when opened.
To add this to your initialisation file:
LISP
(setq auto-mode-alist
(append '(("\\.mf90" . f90-mode)
("\\.X90" . f90-mode)
) auto-mode-alist))
VS Code
Visual Studio Code (VS Code) is another powerful cross-platform IDE. To use VS Code with Fortran we recommend two additions:
- The Modern Fortran extension provides syntax highlighting, IntelliSense for Fortran statements, and debugging capabilities. It also adds customization options to improve the coding experience for Fortran developers.
- The Modern Fortran extension depends on
fortls
: a language server that interfaces VS Code.
fortls
must be installed before the Modern Fortran
extension.
Once fortls
is installed, install the Modern
Fortran extension.
Adding a ruler
We recommend adding a ruler. This is a vertical line in the editor after a set number of characters. Rulers help avoid long lines that are hard to read.
To add a ruler at 80 characters:
- Open the Command Palette (Press
Cmd+Shift+P
, orCtrl+Shift+P
) - Type ‘settings’. Then select the option which says “Preferences: Open User Settings (JSON)”
- This will open the
settings.json
file in your editor. - Add the
editor.rulers
property to the JSON file, e.g.
Save the file. You should see a vertical line appear in your editor.