IntroFortran_schedule_and_exercises
IntroFortran_schedule_and_exercises
Jussi Enkovaara
Thursday Friday
General instructions
Use the local workstations for the exercises.
All source codes are in a github repository https://github.com/csc-
training/fortran-introduction and they can be downloaded as
All exercises are under their own subdirectory which contains both skeleton
codes and model solution (under solution folder)
For every given exercise you are supposed to edit or correct the skeleton
source code. Look for TODO-tags in the source and provide a fix.
GNU compiler is used by default and the compiler command is gfortran.
Example command:
gfortran -o hello hello.F90
Session: Getting started with Fortran
2. Control structures
a) Define a two dimensional m-by-n array of real numbers with an additional
boundary of one column/row to each direction, i.e., let the array bounds range
from 0 to m+1 and from 0 to n+1, respectively. Let m=n=16. By using loops,
initialize the array such that elements with indices i+j < 16 have a value of 5.0.
Initialize the rest of the array to a value of 1.0. A skeleton code is provided in the
file control-structures/do_loop.F90.
b) Fibonacci numbers are a sequence of integers defined by the recurrence relation
Fn=Fn-1 + Fn-2 with the initial values F0=0, F1=1. Print out Fibonacci numbers Fn <
100 using a do while loop.
Session: Procedures and modules
As an input, use the array with the same initial values as in Exercise 2 (or start
from the skeleton loops-arrays/loops_a.F90). Evaluate the Laplacian
only at the inner 16x16 points, as the outer points are used as boundary
condition. As a grid spacing, use Δx=Δy=0.01.
b) Instead of using a double do-loop, use array syntax to compute the values of
the Laplacian. Dynamic arrays and intrinsic functions
6. File I/O
Implement a function that reads the values from a file bottle.dat. The file contains a
header line: “# nx ny”, followed by lines representing the rows of the field. A
skeleton code is provided in io/io.F90.
Once you have completed the implementation, you can build the full program (io)
with the provided Makefile by executing
$ make
The program writes out the data as png-image.
Session: Derived types
7. Derived types
Define a derived type for a temperature field. Do the definition within a module (let’s
call it laplacian_mod for the purpose of a subsequent exercise). The type has the
following elements:
Number of grid points nx (=number of rows) and ny (=number of columns)
The grid spacings dx and dy in the x- and in the y-directions
An allocatable two-dimensional array containing the data points of the field.
Define the real-valued variables into double precision, using the ISO_FORTRAN_ENV
intrinsic module. An example is in derived-types/solution/fieldtype.F90.
The skeleton codes readily contain suitable values for time step Δt and for the
diffusion constant α. Run the code with the default initialization.
b) Another task is to implement a reading in of the initial field from a file (cf.
Exercise 6). Test the implementation with the provided bottle.dat.
Appendix: Heat equation solver
The heat equation is a partial differential equation that describes the variation of
temperature in a given region over time
where u(x, y, z, t) represents temperature variation over space at a given time, and α
is a thermal diffusivity constant.
We limit ourselves to two dimensions (plane) and discretize the equation onto a grid.
Then the Laplacian can be expressed as finite differences as
Where ∆x and ∆y are the grid spacing of the temperature grid u. We can study the
development of the temperature grid with explicit time evolution over time steps ∆t:
There is a solver for the 2D equation implemented with Fortran (including some C for
printing out the images). You can compile the program by adjusting the Makefile as
needed and typing “make”.
The solver carries out the time development of the 2D heat equation over the
number of time steps provided by the user. The default geometry is a flat rectangle
(with grid size provided by the user), but other shapes may be used via input files - a
bottle is give as an example. Examples on how to run the binary:
./heat (no arguments - the program will run with the default arguments:
256x256 grid and 500 time steps)
./heat bottle.dat (one argument - start from a temperature grid provided in
the file bottle.dat for the default number of time steps)
./heat bottle.dat 1000 (two arguments - will run the program starting from a
temperature grid provided in the file bottle.dat for 1000 time steps)
./heat 1024 2048 1000 (three arguments - will run the program in a
1024x2048 grid for 1000 time steps)
The program will produce a .png image of the temperature field after every 100
iterations. You can change that from the parameter image_interval. You can visualize
the images using the command animate: animate heat_*.png.