Introduction to Modern Fortran

September 9, 2024 ยท View on GitHub





Introduction to Modern Fortran

CC BY-NC-SA 4.0

This course is aimed at users and developers who know how to program, but have little or no experience in Fortran, and those who may wish to have a refresher in Fortran.

Fortran (a contraction of Formula Translation) was the first programming language to have a standard (in 1954), but has changed significantly over the years. More recent standards (the latest being Fortran 2023) come under the umbrella term "Modern Fortran". Fortran retains very great significance in many areas of scientific and numerical computing, particularly for applications such as quantum chemistry, plasmas, and in numerical weather prediction and climate models.

This course provides an introduction to the basics of writing Fortran. It will cover basic syntax, variables, expressions and assignments, flow of control, and introductions to i/o and user-defined types. Common Fortran idioms are introduced and contrasted with those available in C-like languages; the course will try to focus on real usage rather than formal descriptions.

At the end of the course you should be able to understand many Fortran programs and be confident to start to write well-structured and portable Fortran. Fortran is a rather "large" language, so it is not possible to cover all its features in a two day course. Further elements of Fortran are discussed in the "Intermediate Modern Fortran" course.

Prerequisites: attendees must be familiar with the basic concepts of programming: variables, logic, flow of control, loops, functions and so on. No knowledge of Fortran is assumed. Previous programming experience might typically be in the context C/C++ or python. If you know no programming, we suggest this course on Fortran is not the place to start.

The course requires a Fortran compiler, for which a local machine or laptop may be appropriate [1]. If you do not have access to a Fortran compiler, course training accounts on ARCHER2 will be available which provide access to various compilers. Use of a text editor will be required (some may prefer an IDE, but we do not intend to consider or support IDEs).

[1] This may typically be gfortran, freely available as part of Gnu Compiler Collection (GCC). See e.g., https://gcc.gnu.org/wiki/GFortranBinaries

Installation

For details of how to log into an ARCHER2 account, see https://docs.archer2.ac.uk/quick-start/quickstart-users/

Check out the git repository to your laptop or ARCHER2 account.

$ git clone https://github.com/ARCHER2-HPC/archer2-fortran-intro.git
$ cd archer2-fortran-intro

The default Fortran compiler on ARCHER2 is the Cray Fortran compiler invoked using ftn. For example,

$ cd section1.01
$ ftn example1.f90

should generate an executable with the default name a.out.

Each section of the course is associated with a different directory, each of which contains a number of example programs and exercise templates. Answers to exercises generally re-appear as templates to later exercises. Miscellaneous solutions also appear in the solutions directory.

Not all the examples compile. Some have deliberate errors which will be discussed as part of the course.

Timetable

The timetable may shift slightly in terms of content, but we will stick to the advertised start and finish times, and the break times.

Day one

TimeContentSection
09:30Logistics: login, compiler set-up, local detailsNone
10:00Background: Fortran standardsFXX
10:20"Hello World"
program, print and write, usesection1.01
10:40Variables: numeric; expressions and assignments, kind
real, integer, complex, parametersection1.02
11:00Break
11:30Variables: logical, character, conditionals
if .. end if and select casesection1.03
11:50Loops and flow of control
do .. end do, exit and cyclesection2.01
12:30Exercises
13:00Lunch
14:00Array declarations: rank, bounds, size and shape
dimension, allocatable, allocate(), reshape()section2.02
14:20Array expressions: sections, conformance, masks
minval() etc.; where .. end where; any() all()section2.03
14:40Mini-exercise: solve a tri-diagonal system
section2.04
15:00Break
15:30Modules and compilation of modules; scope
module .. contains .. end module; public, privatesection3.01
15:50Functions and subroutines; dummy argument intent
function result() subroutine intent ...section3.02
16:10More on array dummy arguments; assumed shape, ...
lbound(), ubound()section3.03
16:30Finish

Day two

TimeContentSection
09:30More on characters and strings; deferred length
len() len_trim(), trim()section4.01
09:50Format strings and edit descriptors
read and writesection4.02
10:10I/O and files; recovering from errors, or not
open close inquire stopsection4.03
10:30Exercises
11:00Break
11:30Data structures
type .. end typesection5.01
11:50Pointer attribute and targets
null(), associated(), associate .. end associatesection5.02
12:10Functions: interface blocks, limited polymorphism ...section6.01
interface, module procedure
12:30Miscellaneous intrinsic functions
get_command_argument(), date_and_time() ...section6.02
13:00Lunch
14:00Mini-exercise on conjugate gradient ...
.. and larger matricessection6.03
15:00Break
15:30Exercises / solutions
16:00Other things you may see; testing; resourcessection7.01
16:30Finish

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

CC BY-NC-SA 4.0