Introduction to Modern Fortran

Description

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 enough to start writing well-structured and portable Fortran. Fortran is a rather “large” language, so it is not possible to cover all its features in a three day course. Further elements of Fortran are discussed in the “Intermediate Modern Fortran” course.

The course requires a Fortran compiler. We will provide accounts on ARCHER2 for you to use; this guarantees that everyone is working in the same environment with the same software. That said, if you wish to work on a local machine or laptop which has a Fortran compiler [1], you of course may do so. Use of a text editor will be required will be required.

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


General Information

When: 19 to 21 May 2025. Add to your Google Calendar.

Requirements: Participants must have a working laptop or desktop computer with a Mac, Linux, or Windows operating system (not a tablet, Chromebook, etc.) that they have administrative privileges on. They should have access to a terminal (Mac and Linux users should have a terminal installed by default; Windows users should get either MobaXterm or PuTTY. They are also required to abide by the ARCHER2 Training Code of Conduct.

Contact: Please email w.lucas@epcc.ed.ac.uk for more information.


Prerequisites

You should have used remote HPC facilities before. In particular, you should be happy with connecting using SSH, know what a batch scheduling system is and be familiar with using the Linux command line. You should also be happy editing plain text files in a remote terminal (or, alternatively, editing them on your local system and copying them to the remote HPC system using scp).


Schedule

Setup Download files required for the lesson
Day 1 10:00 1. Connecting to ARCHER2 and transferring data What can I expect from this course?
How will the course work and how will I get help?
How can I give feedback to improve the course?
How can I access ARCHER2 interactively?
10:40 2. Hello World What is the structure of a Fortran program?
How do I print output from the program to the terminal?
11:00 3. Break Break
11:30 4. Variables What are some basic types of numeric variables in Fortran?
How are variables declared and defined?
How do I create constants?
12:00 5. Logical and conditionals, character variables What other types of variable does Fortran provide?
How can I direct a program’s flow down different paths?
Is it possible to store characters in a variable?
12:20 6. Lunch Break
13:20 7. Loops and loop control How can I make the program do work repeatedly?
How can I have the program only do as much work as is required?
13:40 8. Array declarations How do I create arrays of different sizes and numbers of dimensions?
Can I create the arrays at runtime if I don’t yet know how big they need to be?
14:00 9. Array expressions and assignments How do I assign values to Fortran arrays?
Can I work at once with subsets of an array?
How can I reduce or extract information from an array?
14:30 10. Break Break
15:00 11. Mini exercise: a choice of two What are some real problems I can try out?
16:00 Finish
Day 2 10:00 12. Modules and compilation of modules How can I collect parameters and sub-programs of my own in a module?
Can I keep some parts of the module internal to it and hidden?
Am I able to use scoping to introduce temporary variables in a larger program unit?
10:20 13. Functions and subroutines How can I factor code out into different program procedures?
How do I control the flow of information into and out of procedures?
10:40 14. More on array dummy arguments What do I need to take into consideration when passing an array as an argument?
Can I pass arrays which haven’t yet been allocated?
How do I implement optional dummy arguments?
11:00 15. Break Break
11:30 16. More on characters and strings How are character variables compared in Fortran?
How can I create and use flexible strings?
11:50 17. Formats and edit descriptors How do I read input from the keyboard to my program?
How can I control the format of input and output of various types?
12:10 18. Lunch Break
13:10 19. Operations on external files How do I open and close files for input and output?
How do I handle any errors that might occur during I/O?
13:50 20. Using namelists What is a namelist?
How can I use a namelist to read and write grouped data?
14:30 21. Break Break
15:00 22. Exercises What are some problems I can work on?
16:00 Finish
Day 3 10:00 23. Structures: derived types How can I group together variables to make more complex structures?
How are these derived types defined and then initialised?
10:25 24. Pointers and targets How do I use a pointer to reference another variable?
Can I alias variable names?
Can I change the sizes of allocatable arrays?
10:45 25. Procedures again: interfaces What is an interface and why might I need to write one?
Can I use a function or a subroutine as an argument to another procedure?
How can I write a procedure that will accept arguments of different types?
Can I overload arithmetic and relational operators to work with my derived types?
Can I write a function that will accept both scalar and array arguments?
11:25 26. Break Break
11:55 27. Miscellaneous How can I retrieve any command line arguments I want to give my program?
How can I examine environment variables?
Can I have my Fortran code execute OS commands on the terminal?
Can I get the time and date?
12:15 28. Lunch Break
13:15 29. Other things you may see
13:30 30. Exercises: conjugate gradient and matrices How can I really push what I’ve learnt so far?
14:30 31. Break Break
15:00 32. Exercises
16:00 Finish

The actual schedule may vary slightly depending on the topics and exercises chosen by the instructor.