Fortran
Fortran
Doug Sondak
SCV
sondak@bu.edu
Information Services & Technology 08/14/2024
Outline
Boston University Slideshow Title Goes Here
Goals
Introduction
Fortran History
Basic syntax
Makefiles
Additional syntax
2
Information Services & Technology 08/14/2024
Goals
Boston University Slideshow Title Goes Here
3
Information Services & Technology 08/14/2024
Introduction
Boston University Slideshow Title Goes Here
4
Information Services & Technology 08/14/2024
Introduction (cont’d)
Boston University Slideshow Title Goes Here
5
Information Services & Technology 08/14/2024
Fortran History
Boston University Slideshow Title Goes Here
6
Information Services & Technology 08/14/2024
Fortran History
Boston University Slideshow Title Goes Here
Fortran 66 (1966)
Fortran 77 (1978)
Fortran 90 (1991)
“fairly” modern (structures, etc.)
Current “workhorse” Fortran
Fortran 95 (minor tweaks to Fortran 90)
Fortran 2003
Gradually being implemented by compiler companies
Object-oriented support
Interoperability with C is in the standard (yay!)
7
Information Services & Technology 08/14/2024
8
Information Services & Technology 08/14/2024
9
Information Services & Technology 08/14/2024
Fortran Syntax
Program is contained in a text file
Boston University Slideshow Title Goes Here
10
Information Services & Technology 08/14/2024
11
Information Services & Technology 08/14/2024
12
Information Services & Technology 08/14/2024
13
Information Services & Technology 08/14/2024
14
Information Services & Technology 08/14/2024
Comment character is !
Anything to the right of a comment character on a given line will be
ignored by the compiler
Use comments liberally to document your source code
print*
“list-directed” output
Simple way to produce output on the screen
Follow by comma, then stuff to print
print*, ’This is my character string.’
15
Information Services & Technology 08/14/2024
Not case-sensitive
Ampersand at end of line tells compiler that statement
is continued on next source line
Spaces don’t matter except within literal character
strings
I use them liberally to make code easy to read, e.g., before and after
equals signs
Note that source lines do not end with semicolons (as
in C or Matlab)
16
Information Services & Technology 08/14/2024
Exercise 1
Boston University Slideshow Title Goes Here
17
Information Services & Technology 08/14/2024
Compilation
Boston University Slideshow Title Goes Here
18
Information Services & Technology 08/14/2024
Compilation (cont’d)
Boston University Slideshow Title Goes Here
19
Information Services & Technology 08/14/2024
Compilation (3)
Boston University Slideshow Title Goes Here
20
Information Services & Technology 08/14/2024
Compilation (4)
Boston University Slideshow Title Goes Here
In a Unix window:
pgf90 –o hello hello.f90
Emacs users may find this convenient:
CTL-x 2 will split the window horizontally
CTL-x o toggles between windows
“o” stands for “other”
M-x will prompt for a command at the bottom of the window
Type “shell” (no quotes) for the command
Half of emacs window will now be a Unix shell, so you can do
your compilation there
In a normal Unix tcshell you can retrieve previous Unix
commands with the up arrow; here it’s CTL up arrow
21
Information Services & Technology 08/14/2024
Compilation (5)
Boston University Slideshow Title Goes Here
22
Information Services & Technology 08/14/2024
Arithmetic
Boston University Slideshow Title Goes Here
+, -, *, /
** indicates power
2.41.5 2.4**1.5
Built-in math functions such as sin, acos, exp, etc.
argument in parentheses
sin(0.6)
Exponential notation indicated by letter “e”
4.2 10 3 4.2e3
23
Information Services & Technology 08/14/2024
24
Information Services & Technology 08/14/2024
Exercise 2
Boston University Slideshow Title Goes Here
F = (9/5)C + 32
solution
25
Information Services & Technology 08/14/2024
Arrays
Boston University Slideshow Title Goes Here
26
Information Services & Technology 08/14/2024
Arrays (cont’d)
Boston University Slideshow Title Goes Here
Dynamic allocation
Useful when size is not known at compile time, e.g., input value
Need to specify no. dimensions in declaration
Need to specify that it’s an allocatable array
real, dimension(:,:,:), allocatable :: x, y
allocate function performs allocation
allocate( x(ni,nj,nk), y(ldim,mdim,ndim) )
When you’re done with the variables, deallocate
deallocate(x, y)
not necessary at very end of code; Fortran will clean them up for
you
27
Information Services & Technology 08/14/2024
Parameters
Boston University Slideshow Title Goes Here
28
Information Services & Technology 08/14/2024
Exercise 3
Write program to prompt for 2 floating-point vectors of
Boston University Slideshow Title Goes Here
29
Information Services & Technology 08/14/2024
Control
Boston University Slideshow Title Goes Here
30
Information Services & Technology 08/14/2024
Exercise 4
Boston University Slideshow Title Goes Here
31
Information Services & Technology 08/14/2024
If-Then-Else
Boston University Slideshow Title Goes Here
32
Information Services & Technology 08/14/2024
If-Then-Else (cont’d)
Boston University Slideshow Title Goes Here
33
Information Services & Technology 08/14/2024
Exercise 5
Boston University Slideshow Title Goes Here
34
Information Services & Technology 08/14/2024
Array Syntax
Boston University Slideshow Title Goes Here
35
Information Services & Technology 08/14/2024
36
Information Services & Technology 08/14/2024
Exercise 6
Boston University Slideshow Title Goes Here
37
Information Services & Technology 08/14/2024
Subprograms
Boston
Calculations may be grouped into subroutines and
University Slideshow Title Goes Here
functions
perform specific tasks such as:
read or write data
initialize data
solve a system of equations
Function returns a single object (number, array, etc.),
and usually does not alter the arguments
Altering arguments in a function, called “side effects,” is sometimes
considered bad programming practice
Subroutine transfers calculated values (if any) through
arguments
38
Information Services & Technology 08/14/2024
Functions
Boston
Definition starts with a return type
University Slideshow Title Goes Here
39
Information Services & Technology 08/14/2024
Subroutines
Boston University Slideshow Title Goes Here
40
Information Services & Technology 08/14/2024
Exercise 7
Boston University Slideshow Title Goes Here
41
Information Services & Technology 08/14/2024
42
Information Services & Technology 08/14/2024
Exercise 8
Boston University Slideshow Title Goes Here
43
Information Services & Technology 08/14/2024
Makefiles
Boston University Slideshow Title Goes Here
44
Information Services & Technology 08/14/2024
Makefiles (cont’d)
Boston University Slideshow Title Goes Here
45
Information Services & Technology 08/14/2024
Makefiles (3)
example makefile:
Boston University Slideshow Title Goes Here
### compiler
F90 = pgf90
COMMONFLAGS = -O3
COMPFLAGS = -c $(COMMONFLAGS)
LINKFLAGS = $(COMMONFLAGS)
### objects
OBJ = mymain.o sub1.o sub2.o fun1.o
46
Information Services & Technology 08/14/2024
Makefiles (4)
Boston University Slideshow Title Goes Here
variables
Some character strings appear repeatedly in makefiles
It’s convenient to give them names so if they are changed, you only
have to do it in one place
To define variable:
name = string
No quotes are required for the string
String may contain spaces
“name” is any name you want
Variable names are usually all capitals
To continue line, use \ character
47
Information Services & Technology 08/14/2024
Makefiles (5)
Boston University Slideshow Title Goes Here
Variables (cont’d)
To use variable, either of these work:
$(name)
${name}
Example:
Define compiler
F90 = pgf90
To use elsewhere in makefile:
$(F90)
48
Information Services & Technology 08/14/2024
Makefiles (6)
Boston University Slideshow Title Goes Here
49
Information Services & Technology 08/14/2024
Makefiles (7)
Boston University Slideshow Title Goes Here
50
Information Services & Technology 08/14/2024
Makefiles (8)
Boston University Slideshow Title Goes Here
51
Information Services & Technology 08/14/2024
Makefiles (9)
Boston University Slideshow Title Goes Here
52
Information Services & Technology 08/14/2024
Makefiles (10)
Boston University Slideshow Title Goes Here
53
Information Services & Technology 08/14/2024
Makefiles (11)
Boston University Slideshow Title Goes Here
### suffix rule
.SUFFIXES:
.SUFFIXES: .f90 .o
.f90.o:
$(F90) $(COMPFLAGS) $*.f90
### compiler
F90 = pgf90
COMMONFLAGS = -O3
COMPFLAGS = -c $(COMMONFLAGS)
LINKFLAGS = $(COMMONFLAGS)
### objects
OBJ = mymain.o sub1.o sub2.o fun1.o
Makefiles (12)
Boston
When youSlideshow
University type “make,” it will look for a file called “makefile” or
Title Goes Here
“Makefile”
It then searches for the first target in the file
In our example (and the usual case) the object files are
prerequisites
It checks the suffix rule to see how to create an object file
In our case, it sees that .o files depend on .f90 files
It checks the time stamps on the associated .o and .f90 files to
see if the .f90 is newer
If the .f90 file is newer it performs the suffix rule
In our case, compiles the routine
55
Information Services & Technology 08/14/2024
Makefiles (13)
Boston University Slideshow Title Goes Here
Once all the prerequisites are updated as required, it
performs the recipe
In our case it links the object files and creates our
executable
Many makefiles have an additional target, “clean,” that
removes .o and other files
clean:
rm –f *.o
When there are multiple targets, specify desired target
as argument to make command
make clean
56
Information Services & Technology 08/14/2024
Exercise 9
Boston University Slideshow Title Goes Here
57
Information Services & Technology 08/14/2024
Kind
Boston University Slideshow Title Goes Here
58
Information Services & Technology 08/14/2024
Kind (cont’d)
Boston University Slideshow Title Goes Here
59
Information Services & Technology 08/14/2024
Exercise 10
Boston University Slideshow Title Goes Here
60
Information Services & Technology 08/14/2024
Modules
Boston University Slideshow Title Goes Here
61
Information Services & Technology 08/14/2024
Modules (cont’d)
Boston University Slideshow Title Goes Here
module module-name
implicit none
… variable declarations …
contains
… subprogram definitions …
end module module-name
62
Information Services & Technology 08/14/2024
Modules (3)
Boston University Slideshow Title Goes Here
63
Information Services & Technology 08/14/2024
Modules (4)
Boston University Slideshow Title Goes Here
64
Information Services & Technology 08/14/2024
Modules (5)
Boston University Slideshow Title Goes Here
65
Information Services & Technology 08/14/2024
Exercise 11
Boston University Slideshow Title Goes Here
66
Information Services & Technology 08/14/2024
Derived Types
Boston University Slideshow Title Goes Here
Analogous to structures in C
Can package a number of variables under one name
type grid
integer :: nvals
real, dimension(100,100) :: x, y, jacobian
end type grid
67
Information Services & Technology 08/14/2024
To declare a variable
type(grid) :: airfoil
Components are accessed using %
airfoil%nvals = 20
airfoil%x = 0.0 !... array notation, initialize entire array
Handy way to transfer lots of data to a subprogram
call calc_jacobian(airfoil)
68
Information Services & Technology 08/14/2024
Exercise 12
Boston University Slideshow Title Goes Here
69
Information Services & Technology 08/14/2024
i/o
Boston University Slideshow Title Goes Here
70
Information Services & Technology 08/14/2024
i/o (cont’d)
Boston University Slideshow Title Goes Here
im
For integers
m is total number of places in field
i3 125
am
For character strings
m is number of characters
a5 hello
Left-justifies
If m isn’t specified, writes number of characters in variable
declaration
71
Information Services & Technology 08/14/2024
i/o (3)
Boston University Slideshow Title Goes Here
fm.n
For floating-point (real) numbers
m is total number of characters in field
n is number of decimal places
f5.3 1.234
f5.2 -1.23
If m is larger than required, right-justifies
em.n
Exponential notation
e9.2 -0.23e-01
Always zero left of decimal
72
Information Services & Technology 08/14/2024
i/o (4)
Boston University Slideshow Title Goes Here
esm.n
scientific notation
es9.2 -2.30e-02
In format statement, put formats within ‘()’
Example write statement
write(6, ‘(a, f6.2, i5, es15.3)’) ‘answers are ’, x, j, y
73
Information Services & Technology 08/14/2024
i/o (5)
Boston University Slideshow Title Goes Here
74
Information Services & Technology 08/14/2024
i/o (6)
Boston University Slideshow Title Goes Here
75
Information Services & Technology 08/14/2024
Exercise 13
Boston University Slideshow Title Goes Here
76
Information Services & Technology 08/14/2024
Unformatted i/o
Boston University Slideshow Title Goes Here
Binary data take less disk space than ascii (formatted)
data
Data can be written to file in binary representation
Not directly human-readable
open(199, file=‘unf.d’, form=‘unformatted’)
write(199) x(1:100000), j1, j2
read(199) x(1:100000), j1, j2
Note that there is no format specification
Fortran unformatted slightly different format than C
binary
Fortran unformatted contains record delimiters
77
Information Services & Technology 08/14/2024
Exercise 14
Boston University Slideshow Title Goes Here
78
Information Services & Technology 08/14/2024
References
Boston University Slideshow Title Goes Here
79