Introduction To Fortron Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Getting started with Fortran 90/95

Jonas Lindemann och Ola Dahlblom

May 12, 2009


2
Contents

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]

2.1 Program structure


Every Fortran-program must have a main program routine. From the main routine, other
subroutines that make up the program are called. The syntax for a main program is:

[ 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 )

write (∗ ,∗) k (1 ,1)

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:

1a ! First character not a letter


a t h i n g ! Contains a space character
\ $ s i g n ! Contains a non-alphanumeric character

2.2.2 Variable declarations


There are 5 built-in data types in Fortran:

- integer, Integers
- real, Floating point numbers
- complex, Complex numbers
- logical, Boolean values
- character, Strings and characters

The syntax for a variable declaration is:

type [[,attribute]... ::] entity-list

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.

integer : : a ! Scalar integer variable


real : : b ! Scalar floating point variable
logical : : flag ! boolean variable

r e a l : : D( 1 0 ) ! Floating point array consisting of 10 elements


r e a l : : K( 2 0 , 2 0 ) ! Floating point array of 20x20 elements

i n t e g e r , dimension ( 1 0 ) : : C ! Integer array of 10 elements

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

Constants are declared by specifying an additional paramtera, parameter. A declared


constant can be used in following variable declarations. An example of use is shown in
the following example.

i n t e g e r , parameter : : A = 5 ! Integer constant


r e a l : : C(A) ! Floating point array where
! the number of elements is
! specified by A

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

write (∗ ,∗) ’ pi1 = ’ , pi1


write (∗ ,∗) ’ pi2 = ’ , pi2

stop

end program c o n s t a n t s

The program gives the following results:

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.

2.2.3 Arrays and matrices


In scientific and technical applications matrices and arrays are important concepts. As
Fortran is a language mainly for technical computing, arrays and matrices play a vital
role in the language.
Declaring arrays and matrices can be done in two ways. In the first method the dimen-
sions are specified using the special attribute, dimension, after the data type declaration.
The second method, the dimensions are specified by adding the dimensions directly after
the variable name. The following code illustrate these methods of declaring arrays.

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.

2.2.4 Dynamic variables


In Fortran 77 and earlier versions of the standard it was not possible to dynamically
allocate memory during program execution. This capability is now available in Fortran 90
and later versions. To declare an array as dynamically allocatable, the attribute allocatable
must be added to the array declaration. The dimensions are also replaced with a colon, :,
indicating the number of dimensions in the declared variable. A typical allocatable array
declaration is shown in the following example.

r e a l , dimension ( : , : ) , a l l o c a t a b l e : : K

In this example the two-dimensional array, K, is defined as allocatable. To indicate


that the array is two-dimensional is done by specifying dimension(:,:) in the variable
attribute. To declare a one-dimensional array the code becomes:

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

2.2.5 Assignment of variables


The syntax for scalar variable assignment is,

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 .

Assignment of strings are illustrated in the following example.

character (40) :: first name


character (40) :: last name
character (20) :: company name1
character (20) :: company name2

...

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

If the assignment had been written as

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 /)

This is equivalent to an assignment using the following statements.

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)

B = A ( 2 , : ) ! Assigns B the values of row 2 in A


C = A ( : , 1 ) ! Assigns C the values of column 1 in A

stop

end program i n d e x n o t a t i o n

Using index-notation rows or columns can be assigned in single statements as shown


in the following code:

! Assign row 5 in matrix K the values 1, 2, 3, 4, 5

K( 5 , : ) = (/ 1 . 0 , 2 . 0 , 3 . 0 , 4 . 0 , 5.0 /)

! Assign the array v the values 5, 4, 3, 2, 1

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 –

The following code illustrates operator precedence.

c = a+b /2 ! is equivalent to a + (b/2)


c = ( a+b ) /2 ! in this case (a + b) is evaluated and then / 2

Relational operators:

< or .lt. less than (less than)


<= or .le. less than or equal to (less than or equal)
> or .gt. greater than (greater than)
>= or .ge. greater than or equal to (greater than or equal)
== or .eq. equal to (equal)
/= or .ne. not equal to (not equal)

Logical operators:

.and. and
.or. or
.not. not

2.4 Conditional statements


The simplest form of if-statements in Fortran have the following syntax

if (scalar-logical-expr) then
block
end if

where scalar-logical-expr is a boolean expression, that has to be evaluated as true,


(.true.), for the statements in, block, to be executed. An extended version of the if-
statement adds a else-block with the following syntax

11
2.4 Conditional statements Fortran 95/2003

if (scalar-logical-expr) then
block1
else
block2
end if

In this form the block1 will be executed if scalar-logical-expr is evaluated as true,


otherwise block2 will be executed. A third form of if-statement contains one or more else
if-statements with the following syntax:

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

! Read an integer from standard input

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

! Correct value to the interval 0-1000

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

! If flag is .TRUE. the input value

12
Fortran 95/2003 2.4 Conditional statements

! has been corrected.

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

Another conditional constructi is the case-statement.

select case (expr)


case selector
block
end select

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

Example of case-statement use is shown in the following example:

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

The following program example illustrate how case-statements can be used.

program c a s e s a m p l e

integer : : value

write (∗ ,∗) ’ Enter a value ’


read ( ∗ , ∗ ) v a l u e

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

2.5 Repetitive statements


The most common repetitive statement in Fortran is the do-statement. The syntax is:

do variable = expr1, expr2 [,expr3]


block
end do

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

The above program gives the following output:

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 .

Another repetitive statement available is the do while-statement. With this statement,


the code block can execute until a certain condition is fulfilled. The syntax is:

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:

forall (index = lower:upper [,index = lower:upper])


[body]
end forall

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

Another statement optimised for multiprocessor architectures is the where-statement.


With this statement conditional operations on an array can be achieved efficiently. The
syntax comes in two versions.

where (logical-array-expr)
array-assignments
end where

and

where (logical-array-expr)
array-assignments
else where
array-assignments
end where

The usage of the where-statement is best illustrated with an example.

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

2.6 Built-in functions


Fortran has a number of built-in functions covering a number of different areas. The
following tables list a selection of these. For a more thorough description of the built-in
function please see, Metcalf and Reid [1].

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)

Table 2.1: Mathematical functions

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

Table 2.2: Miscellaneous conversion functions

Function Description
dot product(u, v) Returns the scalar product of u · v

matmul(A, B) Matrix multiplication. The result must


have the same for as AB

transpose(C) Returns the transpose CT . Elementet


CijT motsvarar Cji

Table 2.3: Vector and matrix functions

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

C = A/B ! Division Cij = Aij /Bij


p
C = s q r t (A) ! Square root Cij = Aij

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

Ke = (GT Kel )G (2.1)

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.

any(mask) Returns true if any of the elements in mask


are true.

count(mask) Returns the number of elements in mask


that are true.

maxval(array) Returns the maximum value of the ele-


ments in the array array.

minval( array ) Returns the minimum value of the ele-


ments in the array array.

product( array ) Returns the product of the elements in the


array array.

sum(array) Returns the sum of elements in the array


array.

Table 2.4: Array functions

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 )

! Initiate scalar values

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

! Calcuate directional cosines

L = s q r t ( ( x2−x1 ) ∗∗2 + ( y2−y1 ) ∗∗2 + ( z2−z1 ) ∗∗2 )


nx = ( x2−x1 ) /L
ny = ( y2−y1 ) /L
nz = ( z2−z1 ) /L

19
2.7 Subroutines and functions Fortran 95/2003

! Calucate local stiffness matrix

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 / )

! Calculate transformed stiffness matrix

Ke = matmul ( matmul ( t r a n s p o s e (G) , K e l ) ,G)

! 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

The program produces the following output

0.1925 0.1925 0.1925 −.1925 −.1925 −.1925


0.1925 0.1925 0.1925 −.1925 −.1925 −.1925
0.1925 0.1925 0.1925 −.1925 −.1925 −.1925
−.1925 −.1925 −.1925 0.1925 0.1925 0.1925
−.1925 −.1925 −.1925 0.1925 0.1925 0.1925
−.1925 −.1925 −.1925 0.1925 0.1925 0.1925

For a more thorough description of matrix handling in Fortran 90/95, see Metcalf and
Reid [1]

2.7 Subroutines and functions


A subroutine in Fortran 90/95 has the following syntax

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

type function function-name([dummy-argument-list])


[argument-declaration]
...
function-name = return-value
...
return
end function function-name

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 )

r e a l ( ap ) : : nxx , nyx , nzx


r e a l ( ap ) : : L , E , A
r e a l ( ap ) : : K e l ( 2 , 2 )

22
Fortran 95/2003 2.7 Subroutines and functions

r e a l ( ap ) : : G ( 2 , 6 )

! Calculate directional cosines

L = s q r t ( ( ex ( 2 )−ex ( 1 ) ) ∗∗2 + ( ey ( 2 )−ey ( 1 ) ) ∗∗2 + &


( e z ( 2 )−e z ( 1 ) ) ∗∗2 )
nxx = ( ex ( 2 )−ex ( 1 ) ) /L
nyx = ( ey ( 2 )−ey ( 1 ) ) /L
nzx = ( e z ( 2 )−e z ( 1 ) ) /L

! Calculate local stiffness matrix

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 )

G ( 1 , : ) = ( / nxx , nyx , nzx , 0 . 0 ap , 0 . 0 ap , 0 . 0 a p / )


G ( 2 , : ) = ( / 0 . 0 ap , 0 . 0 ap , 0 . 0 ap , nxx , nyx , nzx / )

! Calculate transformed stiffness matrix

Ke = matmul ( matmul ( t r a n s p o s e (G) , K e l ) ,G)

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

The program gives the following output.

0.1925 0.1925 0.1925 −.1925 −.1925 −.1925


0.1925 0.1925 0.1925 −.1925 −.1925 −.1925
0.1925 0.1925 0.1925 −.1925 −.1925 −.1925
−.1925 −.1925 −.1925 0.1925 0.1925 0.1925

23
2.8 Input and output Fortran 95/2003

−.1925 −.1925 −.1925 0.1925 0.1925 0.1925


−.1925 −.1925 −.1925 0.1925 0.1925 0.1925

2.8 Input and output


Input and output to and from different devices, such as screen, keyboard and files are
accomplished using the commands read and write. The syntax for these commands are:

read(u, fmt) [list]


write(u, fmt) [list]

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:

[repeat-count] format-descriptor w[.m]

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

The program produces the following output:

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

Table 2.5: Formatting codes in read/write

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

Figure 2.1: Positioning of output in Fortran 90/95

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

A more thorough description of the available format specifiers in Fortran is given in


Metcalf and Reid [1].

2.8.1 Reading and Writing from files


The input and output routines can also be used to write data to and from files. This is
accomplished by associating a file in the file system with a file unit number, and using
this number in the read and write statements to direct the input and output to the correct
files. A file is associated, opened, with a unit number using an open-statement. When
operations on the file is finished it is closed using the close-statement.
The file unit number is an integer usually between 1 and 99. On many systems the file
unit number 5 is the keyboard and unit 6 the screen display. It is therefore recommended
to avoid using these numbers in file operations.
In the open-statement the properties of the opened files are given, such as if the file
already exists, how the file is accessed (reading or writing) and the filename used in the
filesystem.
An example of reading and writing file is given in the following example.

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

! File unit numbers

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 the file ’indata.dat’ for reading

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 ’ )

! Open the file ’utdata.dat’ for writing

open ( u n i t=o u t f i l e , f i l e = ’ u t 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= ’ w r i t e ’ )

! Read input from file

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 )

! Free used memory

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.

2.9 String manipulation


There are several ways of manipulating strings in Fortran. Strings can be concatenated
with the operator, //, as shown in the following example:

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.

character (255) : : mystring


r e a l (8 ) : : myvalue
value = 42.0
w r i t e ( m y s t r i n g , ’ ( G15 . 4 ) ’ ) v a l u e
! m y s t r i n g now c o n t a i n s ’ 5.676 ’

To convert a value contained in string to a floating point value the read-statement is


used.

character (255) : : mystring


r e a l (8 ) : : myvalue
mystring = ’ 42.0 ’
read ( m y s t r i n g , ∗ ) m y v a l u e
! my v a l u e now c o n t a i n s 4 2 . 0

A more complete example is shown in the following listing:

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

The program produces the following output.

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

specific libraries of subroutines or function exists in the language. The C preprocessor


is often used to include code from libraries in Fortran, but is not standardised in the
language itself.
In Fortran 90 the concept of modules was introduced. A Fortran 90 module can contain
both variables, parameters and subroutines. This makes it possible to divide programs
into well defined modules which are more easily maintained. The syntax for a module is
similar to that of how a main program in Fortran is defined.

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

! Public variable declarations

! Variables that are visible for other programs


! and modules

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

! Private variables declarations

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 )

r e a l ( ap ) :: nxx , nyx , nzx


r e a l ( ap ) :: L, E, A
r e a l ( ap ) :: Kel (2 ,2)
r e a l ( ap ) :: G( 2 , 6 )

! Calculate directional cosines

L = s q r t ( ( ex ( 2 )−ex ( 1 ) ) ∗∗2 + ( ey ( 2 )−ey ( 1 ) ) ∗∗2 &


+ ( e z ( 2 )−e z ( 1 ) ) ∗∗2 )

nxx = ( ex ( 2 )−ex ( 1 ) ) /L
nyx = ( ey ( 2 )−ey ( 1 ) ) /L
nzx = ( e z ( 2 )−e z ( 1 ) ) /L

! Calculate local stiffness matrix

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 )

G ( 1 , : ) = ( / nxx , nyx , nzx , &


0 . 0 ap , 0 . 0 ap , 0 . 0 a p / )
G ( 2 , : ) = ( / 0 . 0 ap , 0 . 0 ap , 0 . 0 ap , &
nxx , nyx , nzx / )

! Calculate transformed stiffness matrix

Ke = matmul ( matmul ( t r a n s p o s e (G) , K e l ) ,G)

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 )

! Print matrix to standard output

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

Main program using the truss module.

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

3.1 Starting Photran


Photran is started by choosing ”Programs/Fortran Python Software Pack/Photran IDE”
in the start-menu in Windows. When Photran has been started a dialog is shown asking for
a location of a workspace directory, see figure 3.1. A workspace is a directory containing
Photran configuration and project files. If the checkbox, Use this as the default...,
is checked this question will not appear the next time Photran is started, the selected
workspace will be used by default.

Figure 3.1: Choice of workspace directory

When Photran is started for the first time, a welcome screen is shown. This screen
will not be used in this chapter. Click on

to show Photran’s normal user interface layout, as shown in figure 3.2

33
3.2 Creating a Photran Makefile project Photran

Figure 3.2: Photran default interface layout

3.2 Creating a Photran Makefile project


The Photran IDE is centered around projects. This means that source files and build
files are added to projects which Photran are maintained by Photran. Unfortunately,
Photran does not have as advanced project management features as its parent project
Eclipse. Photran can’t generate makefiles automatically from the source files contained in
the project. To solve this the Fortran Python Software Pack comes with a Fortran Make
File Generator that generates a Makefile from files located in the project directory. The
following example shows how to create and configure a Makefile project in Photran.
First a new project is created in Photran by selecting File/New/Ohter. This brings
up a dialog listing the available project types in Photran, see figure 3.3.

Figure 3.3: Project type

Select Fortran/Fortran Project in the list shown. Click Next.

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.

Figure 3.4: Project name and type

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.

Figure 3.5: Binary parser configuration

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

Figure 3.6: Error parser configuration

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.

Figure 3.7: Switching to Fortran Perspective

A perspective in Photran is a pre-configured layout of the development environment.


Photran comes with a Fortran perspective and a Fortran Debug perspective used when
debugging Fortran applications.

3.2.1 Adding a new source file

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.

3.3 Building the project


When all source files have been added to the project it can be built from the Project/Build
All... menu. This will execute the build process. Output from the process can be seen in
the Console tab in the lower pane of the Photran window, as shown in figure 3.8.
Any errors in the build process are also shown in this tab.

36
Photran 3.4 Running the project

Figure 3.8: Output from the build process

3.4 Running the project


When the project has been built successfully it can be run by selecting Run/Run from
the menu. Output from the program is redirected to the Console in the lower part of the
window, as shown in figure 3.9.

Figure 3.9: Output from running program

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-2 Declare the following variables in Fortran: 2 scalar integers a, and b, 3


floating point scalars c, d and e, 2 character strings infile and outfile and
a logical variable f.

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

1-4 What is printed by the following program?

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

1-6 Declare an integer array, idx, with the following indices


[0, 1, 2, 3, 4, 5, 6, 7].

1-7 Give the following assignments:


Floating point array, A, is assigned the value 5.0 at (2,3).
Integer matrix, C, is assigned the value 0 at row 2.

1-8 Give the following if-statements:

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.’.

1-9 Give a case-statment for the variable, a, printing ’a is 1’ when a is 1, ’a


is between 1 and 20’ for values between 1 and 20 and prints ’a is not
between 1 and 20’ for all other values.

42
Exercises A.1 Fortran

1-10 Write a program consisting of a do-statement 1 to 20 with the control


variable, i. For values, i, between 1 till 5, the value of i is printed,
otherwise ’i>5’ is printed. The loop is to be terminated when i equals
15.

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-14 Show expressions in Fortran calculating maximum, mininmum, sum and


product of the elements of an array.

1-15 Declare an allocatable 2-dimensional floating point array and a 1-


dimensional floating point vector. Also show program-statements how
memory for these variables are allocated and deallocated.

1-16 Create a subroutine, identity, initialising a arbitrary two-dimensionl to


the identity matrix. Write a program illustrating the use of the subrou-
tine.

1-17 Implement a function returning the value of the the following expression:

ex sin2 x

43
A.1 Fortran Exercises

1-18 Write a program listing


f (x) = sin x from −1.0 to 1.0 in intervals of 0.1. The output from the
program should have the following format:

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 file has the following structure:

{number of points n in the file}


{x-coordinate point 1} {y-coordinate point 1}
{x-coordinate point 2} {y-coordinate point 2}
.
.
{x-coordinate point n} {y-coordinate point n}

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.

1-22 Create a module, conversions, containing the function in 1-21 and a


function for converting a string to an integer value. Change the program
in 1-21 to use this module. The module is placed in a separate file,
conversions.f90 and the main program in main.f90.

45
A.1 Fortran Exercises

46

You might also like