Introduction To Fortron Programming
Introduction To Fortron Programming
Introduction To Fortron Programming
1 Introduction 1
2 Fortran 95/2003 3
2.1 Program structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.1 Naming of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.2 Variable declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.3 Arrays and matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.4 Dynamic variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.5 Assignment of variables . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Operatorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Conditional statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.5 Repetitive statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6 Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.7 Subroutines and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.8 Input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.8.1 Reading and Writing from files . . . . . . . . . . . . . . . . . . . . 26
2.9 String manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.10 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3 Photran 33
3.1 Starting Photran . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2 Creating a Photran Makefile project . . . . . . . . . . . . . . . . . . . . . . 34
3.2.1 Adding a new source file . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3 Building the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4 Running the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Litteraturförteckning 39
A Exercises 41
A.1 Fortran . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
i
CONTENTS CONTENTS
ii
Chapter 1
Introduction
This book is an introduction in programming with Fortran 95/2003 i in science and tech-
nology. The book also covers methods for integrating Fortran code with other program-
ming languages both dynamic (Python) and compiled languages (C++). An introduction
in using modern development enrvironments such as Eclipse/Photran, for debugging and
development is also given.
1
Introduction
2
Chapter 2
Fortran 95/2003
Fortran was the first high-level language and was developed in the fifties. The languages
has since the developed through a number of standards Fortran IV (1966), Fortran 77,
Fortran 90, Fortran 95 and the latest Fortran 2003. The advantages with standardised
languages is that the code can be run on different computer architectures without modifi-
cation. In every new standard the language has been extended with more modern language
elements. To be compatible with previous standards older language elements are not re-
moved. However, language elements that are considered bad or outdated can be removed
after 2 standard revisions. As an example Fortran 90 is fully backwards compatible with
Fortran 77, but in Fortran 95 some older language constructs where removed.
The following sections gives a short introduction to the Fortran 90 language and some
of the extensions in Fortran 95. The description is centered on the most important lan-
guage features. A more thorough description of the language can be found in the book
Fortran 95/2003 Explained [1]
[ program program-name]
[specification statements]
[executable statements]
[contains]
[subroutines]
end [ program [program-name]]
From the syntax it can be seen that the only identifier that must be included in main
program definition is end.
The syntax for a subroutine and functions are defined in the same way, but the program
identifier is replaced with subroutine or function. A proper way of organizing subroutines
is to place these in separat files or place the in modules (covered in upcoming sections).
Subroutines can also be placed in the main program contains-section, which is the preferred
3
2.1 Program structure Fortran 95/2003
method if all subroutines are placed in the same source file. The code below shows a simple
example of a main program with a subroutine in Fortran.
program s a m p l e 1
i n t e g e r , parameter : : ap=s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
r e a l ( kind=ap ) : : x , y
r e a l ( kind=ap ) : : k ( 2 0 , 2 0 )
x = 6 .0 ap
y = 0.25 ap
write (∗ ,∗) x
write (∗ ,∗) y
w r i t e ( ∗ , ∗ ) ap
c a l l myproc ( k )
contains
s u b r o u t i n e myproc ( k )
i n t e g e r , parameter : : ap=s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
r e a l ( kind=ap ) : : k ( 2 0 , 2 0 )
k =0.0 a p
k ( 1 , 1 ) = 42.0 ap
return
end s u b r o u t i n e myproc
end program s a m p l e 1
!−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
The program source code can contain upper and lower case letters, numbers and special
characters. However, it should be noted that Fortran does not differentiate between upper
and lower case letters. The program source is written starting from the first position with
one statement on each line. If a row is terminated with the charachter &, this indicates
that the statement is continued on the the next line. All text placed after the character
! is a comment and wont affect the function of the program. Even if the comments don’t
have any function in the program they are important for source code readability. This
is especially important for future modification of the program. In addition to the source
code form described above there is also the possibility of writing code in fixed form, as in
Fortran 77 and earlier versions. In previous version of the Fortran standard this was the
only source code form available.
4
Fortran 95/2003 2.2 Variables
2.2 Variables
2.2.1 Naming of variables
Variables in Fortran 95 consists of 1 to 31 alphanumeric characters (letters except å ä
and ö, underscore and numbers). The first character of a variable name must be a letter.
Allowable variable names can be:
a
a thing
x1
mass
q123
time of flight
Variable names can consist of both upper case and lower case letters. It should be
noted that a and A references the same variable. Invalid variables names can be:
- integer, Integers
- real, Floating point numbers
- complex, Complex numbers
- logical, Boolean values
- character, Strings and characters
type defines the variable type and can be integer, real, complex, logical, character, or
type( type-name ). attribute defines additional special attributes or how the variable is to
be used. The following examples shows some typical Fortran variable declarations.
5
2.2 Variables Fortran 95/2003
c h a r a c t e r : : ch ! Character
c h a r a c t e r , dimension ( 6 0 ) : : chv ! Array of characters
c h a r a c t e r ( l e n =80) : : l i n e ! Character string
c h a r a c t e r ( l e n =80) : : l i n e s ( 6 0 ) ! Array of strings
The precision and size of the variable type can be specified by adding a parenthesis
directly after the type declaration. The variables A and B in the following example are
declared as floating point scalars with different precisions. The number in the parenthesis
denotes for many architectures, how many bytes a floating point variable is represented
with.
real (8) : : A
real (4) : : B
integer (4) : : I
To be able to choose the correct precision for a floating point variable, Fortran has
a built in function selected real kind that returns the value to be used in the declaration
with a given precision. This is illustrated in the following example.
i n t e g e r , parameter : : ap = s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
r e a l ( kind=ap ) : : X , Y
In this example the floating point variable should have at least 15 significant decimals
and could represent numbers from 10−300 to 10300 . For several common architectures se-
lected real kind will return the value 8. The advantage of using the above approach is
that the precision of the floating point values can be specified in a architectural indepen-
dent way. The precision constant can also be used when specifying numbers in variable
assignments as the following example illustrate.
X = 6 .0 ap
The importance of specifying the precision for assigning scalar values to variables is
illustrated in the following example.
program c o n s t a n t s
i m p l i c i t none
6
Fortran 95/2003 2.2 Variables
i n t e g e r , parameter : : ap = s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
r e a l ( ap ) : : p i 1 , p i 2
pi1 = 3.141592653589793
p i 2 = 3.141592653589793 ap
stop
end program c o n s t a n t s
pi1 = 3.14159274101257
pi2 = 3.14159265358979
The scalar number assigned to the variable pi1 is chosen by the compiler to be repre-
sented by the least number of bytes floating point precision, in this case real(4), which is
shown in the output from the above program.
Variable declarations in Fortran always precedes the executable statements in the main
program or in a subroutine. Declarations can also be placed directly after the module
identifier in modules. Variable does not have to be declared in Fortran. The default
is that variables starting I, J,..., N are defined as integer and variables starting with A,
B,... ,H or O, P,... , Z are defined as real. This kind of implicit variable declaration is
not recommended as it can lead to programming errors when variables are misspelled.
To avoid implicit variable declarations the following declaration can be placed first in a
program or module:
i m p l i c i t none
This statement forces the compiler to make sure that all variables are declared. If a
variable is not declared the compilation is stopped with an error message. This is default
for many other strongly typed languages such as, C, C++ and Java.
i n t e g e r , parameter : : ap = s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
r e a l ( ap ) , dimension ( 2 0 , 2 0 ) : : K ! M a t r i x 20 x20 e l e m e n t s
7
2.2 Variables Fortran 95/2003
r e a l ( ap ) : : f e ( 6 ) ! A r r a y w i t h 6 e l e m e n t s
The default starting index in arrays is 1. It is however possible to define custom indices
in the declaration, as the following example shows.
r e a l ( ap ) : : i d x ( −3:3)
This declares an array, idx with the indices [-3, -2, -1, 0, 1, 2, 3], which contains 7
elements.
r e a l , dimension ( : , : ) , a l l o c a t a b l e : : K
r e a l , dimension ( : ) , a l l o c a t a b l e : : f
Variables with the allocatable attribute can’t be used until memory is allocated. Mem-
ory allocation is done using the allocate method. To allocate the variables, K,f, in the
previous examples the following code is used.
a l l o c a t e (K( 2 0 , 2 0 ) )
allocate ( f (20) )
When the allocated memory is no longer needed it can be deallocated using the com-
mand, deallocate, as the following code illustrates.
d e a l l o c a t e (K)
deallocate ( f )
An important issue when using dynamically allocatable variable is to make sure the
application does not ”leak”. ”Leaking” is term used by applications that allocate memory
during the execution and never deallocate used memory. If unchecked the application
will use more and more resources and will eventually make the operating system start
swapping and perhaps become also become unstable. A rule of thumb is that an allocate
statement should always have corresponding deallocate. An example of using dynamically
allocated arrays is shown in section XXX.
8
Fortran 95/2003 2.2 Variables
variable = expr
where variable denotes the variable to be assigned and expr the expression to be as-
signed. The following example assign the a variable the value 5.0 with the precision
defined in the constant ap.
a = 5 .0 ap
Assignment of boolean variables are done in the same way using the keywords, .false.
and .true. indicating a true or false value. A boolean expression can also be used int the
assignment. In the following example the variable, flag, is assigned the value .false..
f l a g =. f a l s e .
...
f i r s t n a m e = ’ Jan ’
l a s t n a m e = ”J o h a n s s o n ”
company name1 = ”McDonald ’ s ”
company name2 = ’ McDonald ’ ’ s ’
The first variable, first name, is assigned the text ”Jan”, remaining characters in the
string will be padded with spaces. A string is assigned using citation marks, ” or apos-
trophes, ’. This can be of help when apostrophes or citation marks is used in strings as
shown in the assignemnt of the variables, company name1 och company name2.
Arrays are assigned values either by explicit indices or the entire array in a single
statement. The following code assigned the variable, K, the value 5.0 at position row 5
and column 6.
K( 5 , 6 ) = 5 . 0
K = 5.0
the entire array, K, would have been assigned the value 5.0. This is an efficient way of
assigning entire arrays initial values.
9
2.3 Operatorer Fortran 95/2003
Explicit values can be assigned to arrays in a single statement using the following
assignment.
r e a l ( ap ) : : v ( 5 ) ! A r r a y w i t h 5 e l e m e n t s
v = (/ 1 . 0 , 2 . 0 , 3 . 0 , 4 . 0 , 5.0 /)
v (1) = 1.0
v (2) = 2.0
v (3) = 3.0
v (4) = 4.0
v (5) = 5.0
The number of elements in the list must be the same as the number of elements in the
array variable.
Assignments to specific parts of arrays can be achieved using index-notation. The
following example illustrates this concept.
program i n d e x n o t a t i o n
i m p l i c i t none
r e a l : : A( 4 , 4 )
r e a l : : B( 4 )
r e a l : : C(4)
stop
end program i n d e x n o t a t i o n
K( 5 , : ) = (/ 1 . 0 , 2 . 0 , 3 . 0 , 4 . 0 , 5.0 /)
v = (/ 5 . 0 , 4 . 0 , 3 . 0 , 2 . 0 , 1.0 /)
2.3 Operatorer
The following arithmetic operators are defined in Fortran:
10
Fortran 95/2003 2.4 Conditional statements
** power to
* multiplication
/ division
+ addition
- subtraction
Parenthesis are used to specify the order of different operators. If no parenthesis are
given in an expression operators are evaluated in the following order:
1. Operations with **
2. Operations with * or /
3. Operations with + or –
Relational operators:
Logical operators:
.and. and
.or. or
.not. not
if (scalar-logical-expr) then
block
end if
11
2.4 Conditional statements Fortran 95/2003
if (scalar-logical-expr) then
block1
else
block2
end if
if (scalar-logical-expr1) then
block1
else if (scalar-logical-expr2) then
block2
else
block3
end if
In this form the scalar-logical-expr1 is evaluated first. If this expression is true block1
is executed, otherwise if scalar-logical-expr2 evaluates as true block2 is executed. If no
other expressions are evaluated to true, block3 is executed. An if-statement can contain
several else if-blocks. The use of if-statements is illustrated in the following example:
program l o g i c
i m p l i c i t none
integer : : x
logical : : flag
w r i t e ( ∗ , ∗ ) ’ E n t e r an i n t e g e r v a l u e . ’
read ( ∗ , ∗ ) x
f l a g = . FALSE .
i f ( x >1000) then
x = 1000
f l a g = . TRUE .
end i f
i f ( x <0) then
x = 0
f l a g = . TRUE .
end i f
12
Fortran 95/2003 2.4 Conditional statements
i f ( f l a g ) then
write (∗ , ’ (a , I4 ) ’ ) ’ Corrected value = ’ , x
else
write (∗ , ’ ( a , I4 ) ’ ) ’ Value = ’ , x
end i f
stop
end program l o g i c
In this statement the expression, expr is evaluated and the case-block with the corre-
sponding selector is executed. To handle the case when no case-block corresponds to the
expr, a case-block with the default keyword can be added. The syntax then becomes:
select case(expr)
case selector
block
case default
block
end select
s e l e c t case ( d i s p l a y m o d e )
case ( d i s p l a c e m e n t s )
...
case ( g e o m e t r y )
...
end s e l e c t
To handle the case when display mode does not correspone to any of the alternatives
the above code is modified to the following code.
s e l e c t case ( d i s p l a y m o d e ) case ( d i s p l a c e m e n t s )
...
case ( g e o m e t r y )
...
case d e f a u l t
...
end s e l e c t
13
2.5 Repetitive statements Fortran 95/2003
program c a s e s a m p l e
integer : : value
s e l e c t case ( v a l u e )
case ( : 0 )
write (∗ ,∗) ’ G r e a t e r t h a n one . ’
case ( 1 )
write (∗ ,∗) ’ Number one ! ’
case ( 2 : 9 )
write (∗ ,∗) ’ Between 2 and 9 . ’
case ( 1 0 )
write (∗ ,∗) ’ Number 1 0 ! ’
case ( 1 1 : 4 1 )
write (∗ ,∗) ’ L e s s t h a n 42 b u t g r e a t e r t h a n 1 0 . ’
case ( 4 2 )
write (∗ ,∗) ’ Meaning o f l i f e o r p e r h a p s 6 ∗ 7 . ’
case ( 4 3 : )
write (∗ ,∗) ’ G r e a t e r than 42. ’
case d e f a u l t
write (∗ ,∗) ’ T h i s s h o u l d n e v e r happen ! ’
end s e l e c t
stop
end program c a s e s a m p l e
variable is the control-variable of the loop. expr1 is the starting value, expr2 is the
end value and expr3 is the step interval. If the step interval is not given it is assumed to
be 1. There are two ways of controlling the execution flow in a do-statement. The exit
command terminates the loop and program execution is continued after the do-statement.
The cycle command terminates the execution of the current block and continues execution
with the next value of the control variable. The example below illustrates the use of a
do-statement.
14
Fortran 95/2003 2.5 Repetitive statements
program l o o p s a m p l e
i m p l i c i t none
integer : : i
do i =1 ,20
i f ( i >10) then
w r i t e ( ∗ , ∗ ) ’ T e r m i n a t e s do−s t a t e m e n t . ’
exit
e l s e i f ( i <5) then
write (∗ ,∗) ’ Cycling to next value . ’
cycle
end i f
write (∗ ,∗) i
end do
stop
end program l o o p s a m p l e
C y c l i n g to next value .
C y c l i n g to next value .
C y c l i n g to next value .
C y c l i n g to next value .
5
6
7
8
9
10
Terminates do−s t a t e m e n t .
do while (scalar-logical-expr)
block
end do
The following code shows a simple do while-statement printing the function f (x) =
sin(x).
x = 0.0
do w h i l e x <1.05
f = sin (x)
x = x + 0.1
write (∗ ,∗) x , f
end do
15
2.5 Repetitive statements Fortran 95/2003
Fortran 95 has added a number of new loop-statements. The forall-statement has been
added to optimise nested loops for execution on multiprocessor machines. The syntax is:
The following example shows how a do-statement can be replaced with a forall-statement.
do i =1,n
do j =1,m
A( i , j )=i+j
end do
end do
! Is equivalent with
f o r a l l ( i =1:n , j =1:m)
A( i , j )=i+j
end f o r a l l
where (logical-array-expr)
array-assignments
end where
and
where (logical-array-expr)
array-assignments
else where
array-assignments
end where
where (A>1)
B = 0
e l s e where
B = A
end where
In this example two arrays with the same size are used in the where-statement. In this
case the values in the B array are assigned 0 when an element in the A array is larger
than 1 otherwise the element in B is assigned the same value as in the A array.
16
Fortran 95/2003 2.6 Built-in functions
Function Description
acos(x) Returns arccos(x)
asin (x) Returns arcsin(x)
atan(x) Returns arctan(x)
atan2(y,x) Returns arctan( xy ) from −π till −π
cos(x) Returns cos(x)
cosh(x) Returns cosh(x)
exp(x) Returns ex
log(x) Returns ln(x)
log10(x) Returns lg(x)
sin (x) Returns sin(x)
sinh (x) Returns sinh(x)
√
sqrt (x) Returns x
tan(x) Returns tan(x)
tanh(x) Returns tanh(x)
Function Description
abs(a) Returns absolute value of a
aint (a) Truncates a floating point value
int (a) Converts a floating point value to an integer
nint (a) Rounds a floating point value to the nearest integer
real(a) Converts an integer to a floating point value
max(a1,a2[,a3 ,...]) Returns the maximum value of two or more values
min(a1,a2 [, a3 ,...]) Returns the minimum value of two or more values
Function Description
dot product(u, v) Returns the scalar product of u · v
17
2.6 Built-in functions Fortran 95/2003
Most built-in functions and operators in Fortran support arrays. The following exam-
ple shows how functions and operators support operations on arrays.
r e a l , dimension ( 2 0 , 2 0 ) : : A , B , C
The following example shows how a stiffness matrix for a bar element easily can be
created using these functions and operators. The Matrix Ke is defined as follows
The GT is returned by using the Fortran function transpose and the matrix multipli-
cations are performed with matmul. The matrices Kel and G are defined as
" #
EA 1 −1
Kel = (2.2)
L −1 1
and
Function Description
all (mask) Returns true of all elements in the logical
array mask are true. For example all (A
>0) returns true if all elements in A are
greater than 0.
18
Fortran 95/2003 2.6 Built-in functions
" #
nx ny nz 0 0 0
G= (2.3)
0 0 0 nx ny nz
Length and directional cosines are defined as
q
L= (x2 − x1 )2 + (y2 − y1 )2 + (z2 − z1 )2 (2.4)
x 2 − x1 y2 − y1 z2 − z1
nx = , ny = , nz = (2.5)
L L L
In the example the input parameters are assigned the following values:
x1 = 0, y1 = 0, z1 = 0 (2.6)
x2 = 1, y2 = 1, z2 = 1 (2.7)
E = 1, A = 1 (2.8)
program f u n c t i o n s a m p l e
i m p l i c i t none
i n t e g e r , parameter : : ap = s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
integer : : i , j
r e a l ( ap ) :: x1 , x2 , y1 , y2 , z1 , z2
r e a l ( ap ) :: nx , ny , nz
r e a l ( ap ) :: L, E, A
r e a l ( ap ) :: Kel (2 ,2)
r e a l ( ap ) :: Ke ( 6 , 6 )
r e a l ( ap ) :: G( 2 , 6 )
E = 1 .0 ap
A = 1 .0 ap
x1 = 0 . 0 a p
x2 = 1 . 0 a p
y1 = 0 . 0 a p
y2 = 1 . 0 a p
z1 = 0 . 0 a p
z2 = 1 . 0 a p
19
2.7 Subroutines and functions Fortran 95/2003
K e l ( 1 , : ) = ( / 1 . 0 a p , −1.0 a p /)
K e l ( 2 , : ) = ( / −1.0 ap , 1 .0 ap /)
K e l = K e l ∗ ( E∗A/L )
G ( 1 , : ) = ( / nx , ny , nz , 0 . 0 ap , 0 . 0 ap , 0 . 0 a p / )
G ( 2 , : ) = ( / 0 . 0 ap , 0 . 0 ap , 0 . 0 ap , nx , ny , nz / )
! Print matrix
do i =1 ,6
w r i t e ( ∗ , ’ ( 6 G10 . 3 ) ’ ) ( Ke ( i , j ) , j =1 ,6)
end do
stop
end program f u n c t i o n s a m p l e
For a more thorough description of matrix handling in Fortran 90/95, see Metcalf and
Reid [1]
subroutine subroutine-name[([dummy-argument-list])]
[argument-declaration]
...
return
end subroutine [subroutine-name]
All variables in Fortran program are passed to subroutines as references to the actual
variables. Modifying a parameter in a subroutine will modify the values of variables in the
20
Fortran 95/2003 2.7 Subroutines and functions
calling subroutine or program. To be able to use the variables in the argument list they
must be declared in the subroutine. This is done right after the subroutine declaration.
When a subroutine is finished control is returned to the calling routine or program using
the return-command. Several return statements can exist in subroutine to return control
to the calling routine or program. This is illustrated in the following example.
s u b r o u t i n e myproc ( a , B , C)
i m p l i c i t none
integer : : a
r e a l , dimension ( a , ∗ ) : : B
r e a l , dimension ( a ) : : C
.
.
.
return
end s u b r o u t i n e
A subroutine is called using the call statement. The above subroutine is called with
the following code.
c a l l myproc ( a , B , C)
It should be noted that the names used for variables are local to each respective
subroutine. Names of variables passed as arguments does not need to have the same name
in the calling and called subroutines. It is the order of the arguments that determines
how the variables are referenced from the calling subroutine.
In the previous example illustrates how to make the subroutines independent of prob-
lem size. The dimensions of the arrays are passed using the a parameter instead of using
constant values. The last index of an array does not have to specified, indicated with a
*, as it is not needed to determine the address to array element.
Functions are subroutines with a return value, and can be used in different kinds of
expressions. The syntax is
The following code shows a simple function definition returning the value of sin(x)
real function f ( x )
real : : x
f=s i n ( x )
return
end f u n c t i o n f
21
2.7 Subroutines and functions Fortran 95/2003
The return value defined by assigning the name of the function a value. As seen in
the previous example. The function is called by giving the name of the function and the
associated function arguments.
a = f (y)
The following example illustrates how to use subroutines to assign an element matrix
for a three-dimensional bar element. The example also shows how dynamic memory
allocation can be used to allocate matrices. See also the example in section XX
program s u b r o u t i n e s a m p l e
i n t e g e r , parameter : : ap = &
s e l e c t e d r e a l k i n d (15 ,300)
r e a l ( ap ) : : ex ( 2 ) , ey ( 2 ) , e z ( 2 ) , ep ( 2 )
r e a l ( ap ) , a l l o c a t a b l e : : Ke ( : , : )
ep ( 1 ) = 1.0 ap
ep ( 2 ) = 1.0 ap
ex ( 1 ) = 0.0 ap
ex ( 2 ) = 1.0 ap
ey ( 1 ) = 0.0 ap
ey ( 2 ) = 1.0 ap
ez (1) = 0.0 ap
ez (2) = 1.0 ap
a l l o c a t e ( Ke ( 6 , 6 ) )
c a l l b a r 3 e ( ex , ey , ez , ep , Ke )
c a l l w r i t e M a t r i x ( Ke )
d e a l l o c a t e ( Ke )
stop
end program s u b r o u t i n e s a m p l e
s u b r o u t i n e b a r 3 e ( ex , ey , ez , ep , Ke )
i m p l i c i t none
i n t e g e r , parameter : : ap = &
s e l e c t e d r e a l k i n d (15 ,300)
r e a l ( ap ) : : ex ( 2 ) , ey ( 2 ) , e z ( 2 ) , ep ( 2 )
r e a l ( ap ) : : Ke ( 6 , 6 )
22
Fortran 95/2003 2.7 Subroutines and functions
r e a l ( ap ) : : G ( 2 , 6 )
K e l ( 1 , : ) = ( / 1 . 0 a p , −1.0 a p /)
K e l ( 2 , : ) = ( / −1.0 ap , 1 .0 ap /)
K e l = K e l ∗ ( ep ( 1 ) ∗ ep ( 2 ) /L )
return
end s u b r o u t i n e b a r 3 e
s u b r o u t i n e w r i t e M a t r i x (A)
i n t e g e r , parameter : : ap = &
s e l e c t e d r e a l k i n d (15 ,300)
r e a l ( ap ) : : A ( 6 , 6 )
! Print matrix
do i =1 ,6
w r i t e ( ∗ , ’ ( 6 G10 . 4 ) ’ ) (A( i , j ) , j =1 ,6)
end do
return
end s u b r o u t i n e w r i t e M a t r i x
23
2.8 Input and output Fortran 95/2003
u is the device that is used for reading or writing. If a star (*) is used as a device,
standard output and standard input are used (screen, keyboard or pipes).
fmt is a string describing how variables should be read or written. This is often
important when writing results to text files, to make it more easily readable. If a star (*)
is used a so called free format is used, no special formatting is used. The format string
consists of one or more format specifiers, which have the general form:
where repeat-count is the number of variables that this format applies to. format-
descriptor defines the type of format specifier. w defined the width of the output field
and m is the number of significant numbers or decimals in the output. The following
example outputs some numbers using different format specifiers and table 2.5 show the
most commonly used format specifiers.
program f o r m a t t i n g
i m p l i c i t none
i n t e g e r , parameter : : ap = &
s e l e c t e d r e a l k i n d (15 ,300)
w r i t e ( ∗ , ’ ( A15 ) ’ ) ’ 123456789012345 ’
w r i t e ( ∗ , ’ ( G15 . 4 ) ’ ) 5.675789 ap
w r i t e ( ∗ , ’ ( G15 . 4 ) ’ ) 0.0675789 ap
w r i t e ( ∗ , ’ ( E15 . 4 ) ’ ) 0.675779 ap
w r i t e ( ∗ , ’ ( F15 . 4 ) ’ ) 0.675779 ap
write (∗ ,∗) 0.675779 ap
write (∗ , ’ ( I15 ) ’ ) 156
w r i t e ( ∗ , ∗ ) 156
stop
end program f o r m a t t i n g
24
Fortran 95/2003 2.8 Input and output
Kod Beskrivning
E Scientific notation. Values are converted to the format ”-d.dddE+ddd”.
F Decimal notation. Values are converted to the format ”-d ddd.ddd...”.
G Generic notation. Values are converted to the format -ddd.ddd or -d.dddE+ddd
I Integers.
A Strings
TRn Move n positions right
Tn Continue at position n
123456789012345
5.676
0 . 6 7 5 8 E−01
0 . 6 7 5 8 E+00
0.6758
0.675779000000000
156
156
During output a invisible cursor is moved from left to right. The format specifiers
TRn and Tn are used to move this cursor. TRn moves the cursor n positions to the right
from the previous position. Tn places the cursor at position n. Figure ?? shows how this
can be used in a write-statement.
write(*,’(Tn,Ex.x,Tn,Ex.x,TRn,Ex.x)’) a, b, c
Tn a b TRn c
0.1231414 0.3414 0.3456414
Tn
The output routines in Fortran was originally intended to be used on row printers
where the first character was a control character. The effect of this is that the default
behavior of these routines is that output always starts at the second position. On modern
computers this is not an issue, and the first character can be used for printing. To print
from the first character, the format specifier T1 can be used to position the cursor at the
first position. The following code writes ”Hej hopp!” starting from the first position.
w r i t e ( ∗ , ’ ( T1 , A) ’ ) ’ Hej hopp ! ’
25
2.8 Input and output Fortran 95/2003
program s a m p l e 2
implicit none
real (8) , allocatable : : i n f i e l d ( : , : )
real (8) , a l l o c a t a b l e : : rowsum ( : )
integer : : rows , i , j
i n t e g e r , parameter : : i n f i l e = 15
i n t e g e r , parameter : : o u t f i l e = 16
! Allocate matrices
rows=5
a l l o c a t e ( i n f i e l d ( 3 , rows ) )
a l l o c a t e ( rowsum ( rows ) )
open ( u n i t= i n f i l e , f i l e = ’ i n d a t a . d a t ’ ,&
a c c e s s= ’ s e q u e n t i a l ’ ,&
a c t i o n= ’ r e a d ’ )
26
Fortran 95/2003 2.9 String manipulation
do i =1, rows
read ( i n f i l e , ∗ ) ( i n f i e l d ( j , i ) , j =1 ,3)
rowsum ( i )=&
i n f i e l d ( 1 , i )+ i n f i e l d ( 2 , i )+ i n f i e l d ( 3 , i )
w r i t e ( o u t f i l e , ∗ ) rowsum ( i )
end do
! Close files
close ( i n f i l e )
close ( o u t f i l e )
deallocate ( i n f i e l d )
d e a l l o c a t e ( rowsum )
stop
end program s a m p l e 2
In this example, 2 files are opened, indata.dat and utdata.dat with open-statements.
Using the read-statement five rows with 3 numbers on each row are read from the file
indata.dat. The sum of each row is calculated and is written using write-statements to
the file utdata.dat. Finally the files are closed using the close-statements.
c1 = ’ Hej ’
c2 = ’ hopp ! ’
c = c1 // c2 ! = ’ Hej hopp ! ’
Fortran does not have dynamic strings, so the size of the resulting string must be large
enough for the concatenated string.
Substrings can be extracted using a syntax similar to the syntax used when indexing
arrays.
c3 = c ( 5 : 8 ) ! C o n t a i n s t h e s t r i n g ’ hopp ’
A common task in many codes is the conversion of numbers to and from strings.
Fortran does not have any explicit functions these type of conversions, instead the the
read and write statements can be used together with strings to accomplish the same thing.
By replacing the file unit number with a character string variable, the string can be read
from and written to using read and write statements.
27
2.10 Modules Fortran 95/2003
To convert a floating point value to a string the following code can be used.
program s t r i n g s 2
i m p l i c i t none
integer : : i
character (20) : : c
c = ’5 ’
read ( c , ’ ( I 5 ) ’ ) i
write (∗ ,∗) i
i = 42
write ( c , ’ ( I5 ) ’ ) i
write (∗ ,∗) c
stop
end program s t r i n g s 2
5
42
2.10 Modules
When programs become larger, they often need to be split into more manageable parts. In
other languages this is often achieved using include files or packages. In Fortran 77, no such
functionality exists. Source files can be grouped in files, but no standard way of including
28
Fortran 95/2003 2.10 Modules
module module-name
[specification-stmts]
[ contains
module-subprograms]
end module [module-name]]
The block specification-stmts defines the variables that are available for programs or
subroutines using the module. In the block, module-subprograms, subroutines in the mod-
ule are declared. A module can contain only variables or only subroutines or both. One
use of this, is to declare variables common to several modules i a separate module. Mod-
ules are also a good way to divide a program into logical and coherent parts. Variables
and functions in a module can be made private to a module, hiding them for routines
using the module. The keywords public and private can be used to control the access to
a variable or a function. In the following code the variable, a, is hidden from subroutines
or programs using this module. The variable, b, is however visible. When nothing is
specified in the variable declaration, the variable is assumed to be public.
module mymodule
integer , private : : a
integer : : b
...
The ability to hide variables in modules enables the developer to hide the implemen-
tation details of a module, reducing the risk of accidental modification variables and use
of subroutines used in the implementation.
To access the routines and variables in a module the use statement is used. This makes
all the public variables and subroutines available in programs and other modules. In the
following example illustrate how the subroutines use in the previous examples are placed
in a module, truss, and used from a main program.
module t r u s s
i n t e g e r , p u b l i c , parameter : : &
ap = s e l e c t e d r e a l k i n d ( 1 5 , 3 0 0 )
29
2.10 Modules Fortran 95/2003
integer , private : : m y p r i v a t e v a r i a b l e
contains
s u b r o u t i n e b a r 3 e ( ex , ey , ez , ep , Ke )
i m p l i c i t none
r e a l ( ap ) : : ex ( 2 ) , ey ( 2 ) , e z ( 2 ) , ep ( 2 )
r e a l ( ap ) : : Ke ( 6 , 6 )
nxx = ( ex ( 2 )−ex ( 1 ) ) /L
nyx = ( ey ( 2 )−ey ( 1 ) ) /L
nzx = ( e z ( 2 )−e z ( 1 ) ) /L
K e l ( 1 , : ) = ( / 1 . 0 a p , −1.0 a p /)
K e l ( 2 , : ) = ( / −1.0 ap , 1 .0 ap /)
K e l = K e l ∗ ( ep ( 1 ) ∗ ep ( 2 ) /L )
return
end s u b r o u t i n e b a r 3 e
s u b r o u t i n e w r i t e M a t r i x (A)
30
Fortran 95/2003 2.10 Modules
r e a l ( ap ) : : A ( 6 , 6 )
do i =1 ,6
w r i t e ( ∗ , ’ ( 6 G10 . 4 ) ’ ) (A( i , j ) , j =1 ,6)
end do
return
end s u b r o u t i n e w r i t e M a t r i x
end module t r u e s s
program m o d u l e s a m p l e
use t r u s s
i m p l i c i t none
r e a l ( ap ) : : ex ( 2 ) , ey ( 2 ) , e z ( 2 ) , ep ( 2 )
r e a l ( ap ) , a l l o c a t a b l e : : Ke ( : , : )
ep ( 1 ) = 1.0 ap
ep ( 2 ) = 1.0 ap
ex ( 1 ) = 0.0 ap
ex ( 2 ) = 1.0 ap
ey ( 1 ) = 0.0 ap
ey ( 2 ) = 1.0 ap
ez (1) = 0.0 ap
ez (2) = 1.0 ap
a l l o c a t e ( Ke ( 6 , 6 ) )
c a l l b a r 3 e ( ex , ey , ez , ep , Ke )
c a l l w r i t e M a t r i x ( Ke )
d e a l l o c a t e ( Ke )
stop
end program m o d u l e s a m p l e
Please note that the declaration of ap in the truss module is used to define the precision
of the variables in the main program.
31
2.10 Modules Fortran 95/2003
32
Chapter 3
Photran
Photran is a integrated development environment, IDE, for Fortran based on the Eclipse-
project. The user interface resembles the one found in commercial alternatives such as
Microsoft Visual Studio or Absoft Fortran. This chapter gives a short introduction on
how to get started with this development enviroment
When Photran is started for the first time, a welcome screen is shown. This screen
will not be used in this chapter. Click on
33
3.2 Creating a Photran Makefile project Photran
34
Photran 3.2 Creating a Photran Makefile project
In the next page, figure 3.4, enter the name of the project and select the ”Makefile
project” in the ”Project type” list.
To be able to build a project a toolchain must be selected. A toolchain is set of tools and
compilers that is used to build a project. Linux user can choose ”Linux GCC” or ”GCC
Toolchain”. On Windows the toolchains ”GCC Toolchain”, ”MinGW GCC” or ”Cygwin
GCC” can be selected depending on the tools installed. If the Fortran Python Software
Pack is installed ”MinGW GCC” must be chosen for Windows. Windows users should
uncheck the box ”Show project types and toolchains only if they are supported on the
platform.”. This will show all available toolchains even if Photran can’t detect them. Click
Next to go to the last configuration page. In this step an error parser must be configured
in the advanced settings. Click on Advanced Settings.... This brings up the advanced
configuration dialog. Select Fortran Build/Settings. In the Binary Parsers parsers for
different executable formats can be selected, see figure 3.5.
On Windows the ”PE Windows Parser” should be selected. On Linux the ”Elf Parser”
should be selected. In the Error Parsers, see figure 3.6, parsers for compiler error messages
can be selected. The closest match for the gfortran compiler is the ”Fortran Error Parser
for G95 Fortran” selection.
35
3.3 Building the project Photran
Click OK to save the settings and close the advanced settings dialog. The project
is now ready to be created. Click Finish to create the project. Before the project is
saved Photran shows a dialog with the option of switching to the Fortran Perspective, see
figure 3.7.
A new source file is added to the project by selecting File/New/Other and selecting
”Source File” from the Fortran Folder. Click Next. In the next page the name of the
source file is entered. Click Finish to create the file and add it to the project.
36
Photran 3.4 Running the project
37
3.4 Running the project Photran
38
Bibliography
[1] Michael Metcalf and John Reid, Fortran 90/95 Explained, Oxford University Press
Inc, New York, 1996
[2] Niels Ottosen & Hans Petersson, Introduction to the Finite Element Method, Prentice
Hall International (UK) Ltd, 1992
[3] Fortran 90, ISO/IEC 1539 : 1991, International Organisation for Standardization,
http://www.iso.ch/
[4] CALFEM – A finite element toolbox to MATLAB version 3.3, Division of Structural
Mechanics, 1999
39
BIBLIOGRAPHY BIBLIOGRAPHY
40
Appendix A
Exercises
A.1 Fortran
1-1 Which of the following names can be used as Fortran variable names?
a) number of stars
b) fortran is a nice language to use
c) 2001 a space odyssey
d) more$ money
1-3 Declare a floating point variable a that can represent values between
10−150 and 10150 with 14 significatn numbers.
41
A.1 Fortran Exercises
program p r e c i s i o n
i m p l i c i t none
i n t e g e r , parameter : : ap = &
s e l e c t e d r e a l k i n d (15 ,300)
r e a l ( ap ) : : a , b
a = 1.234567890123456
b = 1.234567890123456 ap
i f ( a==b ) then
write (∗ ,∗) ’ Values are equal . ’
else
write (∗ ,∗) ’ Values are d i f f e r e n t . ’
endif
stop
end program p r e c i s i o n
1-5 Declare a [3×3] floating point array ,Ke, and an 3 element integer array,
f
If the value of the variable,i, is greater than 100 print ’i is greater than
100!’
If the value of the logical variable, extra filling, is true print ’Extra filling
is ordered.’, otherwise print ’No extra filling.’.
42
Exercises A.1 Fortran
1-11 Write a program declaring a floating point matrix, I, with the dimensions
[10 × 10] and initialises it with the identity matrix.
1-12 Give the following expressions in Fortran:
a) √12
ex sin2 x
b) √
c) a2 + b2
d) |x − y|
1-13 Give the following matrix and vector expressions in Fortran. Also give
appropriate array declarations:
a) AB
b) AT A
c) ABC
d) a · b
1-17 Implement a function returning the value of the the following expression:
ex sin2 x
43
A.1 Fortran Exercises
111111111122222222223
123456789012345678901234567890
x f (x)
−1.000 −0.841
−0.900 −0.783
−0.800 −0.717
−0.700 −0.644
−0.600 −0.565
−0.500 −0.479
−0.400 −0.389
−0.300 −0.296
−0.200 −0.199
−0.100 −0.100
0.000 0.000
0.100 0.100
0.200 0.199
0.300 0.296
0.400 0.389
0.500 0.479
0.600 0.565
0.700 0.644
0.800 0.717
0.900 0.783
1.000 0.841
1-19 Write a program calculating the total length of a piecewise linear curve.
The curve is defined in a textfile line.dat.
The program must not contain any limitations regarding the number of
points in the number of points in the curve read from the file.
44
Exercises A.1 Fortran
1-20 Declare 3 strings, c1, c2 and c3 containing the words ’Fortran’, ’is’ och
’fun’. Merge these into a new string, c4, making a complete sentence.
1-21 Write a function converting a string into a floating point value. Write
a program illustrating the use of the function.
45
A.1 Fortran Exercises
46