You are on page 1of 22

Programming in FORTRAN95

Lecture I
Thomas Kalscheuer

14. January 2008

Uppsala universitet Page 1


1 – Introduction

Introduction
If builders built buildings the way programmers wrote programs,
then the first woodpecker that came along would destroy civilization.
- Weinberg’s second law -

• FORTRAN95 is the most powerful and best structured high level


programming language for scientific computing available.

• FORTRAN95 is in most cases by a factor of 3-4 faster than


MATLAB, for instance.

• It is very well integrated into parallel programming platforms.

• Free compilers became available recently.

Uppsala universitet Page 2


1 – Introduction
If you miss one issue of any magazine, it will be the issue
that contains the article, story or installment you were most anxious to read.
- Johnson’s third law -
All of your friends either missed it, lost it or threw it out.
- Corollary to Johnson’s third law -

References
[metc04] M. Metcalf, J. Reid and M. Cohen: fortran95/2003 explained,
Oxford University Press, Oxford, 2004
[hahn94] B.D. Hahn: Fortran 90 for Scientists and Engineers, Arnold,
London, 1994
[intel] Intel: Intel Fortran Language Reference,
http://developer.intel.com, 2005-

Uppsala universitet Page 3


1 – Introduction
Free Compilers
• Intel Fortran Compiler:
Free for academic purposes, very stable, parallelization on
shared memory platforms with OpenMP included, excellent
documentation both for FORTRAN and the compiler in
electronic form. Works out of the box under Linux, requires
Microsoft C++ Builder for Windoze.
Link: http://www.intel.com/cd/software/products/
asmo-na/eng/compilers/flin/index.htm
• g95:
Comes with GPL. Not really integrated in gcc (some problems
due to origin of source code). Many hardware/OS platforms
supported. Appears to work relatively stably, but still a few bugs
burried here and there.
Link: http://www.g95.org

Uppsala universitet Page 4


1 – Introduction

• gfortran:
As part of gcc it comes with GPL. Due to gcc backend, almost all
current hardware/OS platforms supported. Forked from g95.
Unfortunately, still in a relatively premature status.
Link: http://gcc.gnu.org/fortran/

Uppsala universitet Page 5


1 – Introduction

Links
• Michael Metcalf’s Fortran 90 CNL Articles:
http://wwwasdoc.web.cern.ch/wwwasdoc/f90.html

• Michael Metcalf’s Fortran 90/95/HPF Information File:


http://www.fortran.com/fortran/metcalf.htm

• Fortran Resources and Fortran 77/90/95 Compilers for


Windows and Linux:
http://www.personal.psu.edu/faculty/h/d/
hdk/fortran.html

Uppsala universitet Page 6


1 – Introduction

• Michel Olagnon’s Fortran 90 List:


http://www.ifremer.fr/ditigo/molagnon/
fortran90/engfaq.html

• Introduction to FORTRAN 90, QUB:


http://www.pcc.qub.ac.uk/tec/courses/
f90/stu-notes/F90_notesMIF_1.html

Uppsala universitet Page 7


1 – Introduction

Proposed software installation for Windoze users


• Download and install the Windoze version of g95 from
http://www.g95.org to C:\ProgramFiles\g95. Let
installation software supplement the system variable PATH with
;C:\ProgramFiles\g95\bin

• Download the web installer Install Now! of the editor


XEmacs from http://www.xemacs.org and run it.

Uppsala universitet Page 8


2 – Elementary FORTRAN95 - Part 1
Elementary FORTRAN95 - Part 1
A first program and its compilation
A journey of a thousand miles must begin with a single step.
- Lao-zi -

Our first program prints a simple greeting message to the screen:


! written by Thomas Kalscheuer, 22/10/2005
! compile with: g95 -o hello hello.f90
!
! prints "Hello, world!" message to screen
program hello

print *, "Hello, world!"

end program hello

Uppsala universitet Page 9


2 – Elementary FORTRAN95 - Part 1
Program Layout
It is the nobility of their style which will make our
writers of 1840 unreadable forty years from now.
- Stendahl -

General structure of a simple program (bracketed parts are optional):


[program program_name]
[declaration statements]
[executable statements]
end [program [program_name]]
• Statements:
Declaration statements define variables like complex, real or
integer (whole) numbers, strings of characters and logical (e.g.
real :: A).
Executable statements perform certain actions on previously

Uppsala universitet Page 10


2 – Elementary FORTRAN95 - Part 1

defined variables (e.g. assignment statements A = 100 or


print *, "Hello, world!").

• Blanks:
Blanks generally ignored by compiler, i.e. use them to make your
code more legible. Be aware: blanks not allowed within tokens
like keywords (e.g. end, program, print, etc.), variable names,
labels, constants, operators and separators.
Blanks are also used for indentation of program lines.

• Comments:
Any characters appearing after exclamation marks (!) are
comments and ignored by the compiler. Use them to explain
your code!

Uppsala universitet Page 11


2 – Elementary FORTRAN95 - Part 1

• Continuation lines:
If a line becomes too long it can be continued on the next line by
using an ampersand (&) as the last non-blank character:
print *, "This is a very long line and it does", &
" not just print: Hello, world!"

• Case:
FORTRAN is not case sensitive, e.g. end is the same as END

Uppsala universitet Page 12


2 – Elementary FORTRAN95 - Part 1
Data types, variables and constants
Variables are defined according to their data type. There are five
intrinsic (built-in) data types in FORTRAN95:
• Numeric:
1. integer numbers are signed whole numbers, e.g. -1237, 88
Type declaration with:
integer :: i, j
2. real numbers in either floating or exponential form, e.g.
-0.345, 1.45E09
Type declaration with:
real :: amplitude, phase
3. complex numbers consisting of a real and imaginary part in
either floating or exponential form, e.g. (4.5, -8.9E-05), (0.002,
2500.)

Uppsala universitet Page 13


2 – Elementary FORTRAN95 - Part 1
Type declaration with:
complex :: Z_xy, Z_yx

• Non-numeric:
1. character defines strings, e.g. ’John Smith’, "Course duration"
Type declaration with:
character :: name
2. logical has one of two values, i.e. either .true. or .false.
Type declaration with:
logical :: test_finished

Definition of constants:
through the parameter attribute, e.g.
real, parameter :: pi = 3.1415926

Uppsala universitet Page 14


2 – Elementary FORTRAN95 - Part 1

Implicit typing:
is a relic of the old days. If implicit typing is switched on (which is
the default!), variables that are not explicitly declared and start with
i-n or I-N are assumed to be integers and all others are assumed to be
real. Typing errors in variable names may lead to unexpected
program behaviour.
You should always switch off implicit typing at the start of your
programs with implicit none. As a consequence you have to
declare all variables explicitly.

Uppsala universitet Page 15


2 – Elementary FORTRAN95 - Part 1
Numeric expressions and assignments
One is equal to two for
sufficiently large values of one.
- anonymous -

operator action
** exponentiation
*, / multiplication, division
+, - addition, subtraction

Table 1: Intrinsic numeric operators

Numeric expressions:
• integer: truncated towards zero
10/3 evaluates to 3

Uppsala universitet Page 16


2 – Elementary FORTRAN95 - Part 1
-8/3 evaluates to -2
10-3 evaluates to 7
• mixed mode: results in real number
10/3.0 evaluates to 3.333
4./5 evaluates to 0.8
Numeric assignments:
• Typical form:
var = expr
• If expr is not of the same type as var, it is converted to that type
before assignment.
• Example:
integer :: k
k = 3.0/2 ! evaluates to 1

Uppsala universitet Page 17


2 – Elementary FORTRAN95 - Part 1
• Performance consideration:
Whenever possible, avoid integer and real mixed type
expressions!

Intrinsic numeric functions:

• sqrt, sin, tanh, etc.

• arguments of trigonometric functions are radians

• Example:
real :: alpha ! angle in degrees
real, parameter :: pi = 3.1415926

alpha = 90.0
print *, "cos of alpha is ", cos(alpha*pi/180.0)

Uppsala universitet Page 18


2 – Elementary FORTRAN95 - Part 1
Basic input and output
Junk in, junk out!
- Important programming paradigm -

Input: – supply data while program is running through keyboard


– basic form:
real :: a
integer :: l
read *, a, l
– different input variables my be on the same line (record),
separated by white space, commas, or on different lines
(records)
– data for read may run over onto subsequent records
Output: – print out data to screen
– basic form:

Uppsala universitet Page 19


2 – Elementary FORTRAN95 - Part 1

real :: a
a = 0.05
print *, "value of a:", a

Uppsala universitet Page 20


2 – Elementary FORTRAN95 - Part 1
A second example program
Our second program reads the radius of sphere from the keyboard,
computes the surface area of the sphere and prints it on the screen:
! written by Thomas Kalscheuer, 22/10/2005
! compile with: g95 -o sphere_surface sphere_surface.f90
!
! computes surface area of sphere
program sphere_surface

implicit none

! constant pi
real, parameter :: pi = 3.141592687
! radius and surface area of sphere
real :: radius, surface_area

Uppsala universitet Page 21


2 – Elementary FORTRAN95 - Part 1

print *, "Enter radius of sphere:"


read *, radius

! actual computation of surface area


surface_area = 4.0*pi*radius**2.0

print *, "Surface area of sphere:" , surface_area

end program sphere_surface

Uppsala universitet Page 22

You might also like