Download - Fortran Workshop
-
8/10/2019 Fortran Workshop
1/145
Fortran 90+IntensiveWorkshopDr Stephen So
Griffith Schoo of !ngineering
mailto:[email protected]:[email protected] -
8/10/2019 Fortran Workshop
2/145
http://maxwell.me.gu.edu.au/sso/fortran
"rief #utine Fortran versus $%&'%"
Fortran progra* deveop*ent and snta,
Intrinsic data tpes and operators
%rras and arra *anipuation Fie reading and -riting
Suprogra*s / functions and suroutines
$odues
Derived data tpes Function and operating overoading
gfortran co*pier opti*isation
aing '%%1 and FF&%1
aing Fortran 90 suprogra*s fro* $%&'%"
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
3/145
http://maxwell.me.gu.edu.au/sso/fortran
Introduction !ssentia ee*ents of the Fortran 90+
(90923004) progra**ing anguage -i e
covered rior progra**ing e,perience (e.g. $%&'%")
is assu*ed
Working in the Windo-s environ*ent
See http5*a,-e.*e.gu.edu.aussofortran
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
4/145
http://maxwell.me.gu.edu.au/sso/fortran
What do I need in order to-rite Fortran progra*s6 Fortran co*pier converts our source coded into an e,ecutae
proe*
&e,t editor or integrated deveop*entenviron*ent (ID!) used to -rite our source code into a te,t fie
So*e nu*erica iraries ('%%17 "'%S7FF&%17 81SI&!7 etc.) contains suprogra*s -ritten (and tested)
others
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
5/145
http://maxwell.me.gu.edu.au/sso/fortran
Fortran co*piers&here are *an co**ercia Fortran co*piers on
the *arket (cost a ot of :::)
Inte ;isua Fortran GI ;isua Fortran
-
8/10/2019 Fortran Workshop
6/145
http://maxwell.me.gu.edu.au/sso/fortran
ID!s o**ercia ID!s $icrosoft ;isua Studio (used Inte ;F7 GI
;F7 'ahe7 o*pa= ;F) usto* ID!s (used %soft and oder version
of 'ahe Fortran 92)
Free and open>source ID!s
ode"ocks !cipse (hotran pugin)
-
8/10/2019 Fortran Workshop
7/145http://maxwell.me.gu.edu.au/sso/fortran
-
8/10/2019 Fortran Workshop
8/145http://maxwell.me.gu.edu.au/sso/fortran
Fortran versus $%&'%" I"$ $athe*atica For*ua &ransation Sste*
(Fortran II7 III7 I;7 ??7 7 907 927 30047 300A)
%dvantages5 ;er fast (co*pied versus interpreted)
!fficient -ith *e*or (can deaocate arras)
$ore contro over data representation and
for*atting ;er popuar in scientific and engineering
co**unit
'ots of egac Fortran code avaiae ("'%S7
'%%17 etc.)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
9/145http://maxwell.me.gu.edu.au/sso/fortran
Fortran versus $%&'%" Disadvantages5&edious and difficut to earn
$ore verose
;er i*ited *atri, *anipuation in routines or tooo,es
in visuaisation toos
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
10/145http://maxwell.me.gu.edu.au/sso/fortran
Fortran versus strengths are in sste*s progra**ing
FortranBs strengths are in nu*er crunchingand nu*erica data processing
Fortran co*piers are *ore sophisticatedthan co*piers for nu*erica opti*isation
$odern Fortran co*piers have support forparae processing and G processing(D%) as -e
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
11/145http://maxwell.me.gu.edu.au/sso/fortran
o*piing and inking Fortran 90 source code is tped into a te,t
fie ( C.f907 C.92)
&o run the Fortran progra*5 Step 5 o*piesource code E oect fie
(C.o) Step 35 'inkin other oect fies (other
suprogra*s) and stu E e,ecutae fie(C.e,e)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
12/145http://maxwell.me.gu.edu.au/sso/fortran
o*piing and inking
Compilationstage
Linkingstage
main.f90
program.exe
Windows stub
Static library
func2.o
func1.o
main.o
func2.f90
func1.f90
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
13/145http://maxwell.me.gu.edu.au/sso/fortran
sing gfortran (fro*co**and>ine) In a co**and ine7 to co*pie and ink a
singe Fortran source fie (heo.f90) gfortran heo.f90 >o heo.e,e
&o perfor* co*piing on (no inking) gfortran heo.f90 >c
&o ink *utipe oect fies gfortran heo.o func.o func3.o >o heo.e,e
&o incude static iraries gfortran heo.o iapack.a ias.a >o heo.e,e
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
14/145http://maxwell.me.gu.edu.au/sso/fortran
Fortran progra* structure
programprogram_name
specification statements
executable statements
[contains]
[internal subprograms]
end programprogram_name
-
8/10/2019 Fortran Workshop
15/145http://maxwell.me.gu.edu.au/sso/fortran
Si*pe Fortran progra*
programhello
implicit none
! This is a comment
print*, 'Hello, world'
end programhello
Save this into a te,t fie caed heo.f90
o*pie in co**and ine gfortran heo.f90 >o heo.e,e
his statement should always beincluded
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
16/145http://maxwell.me.gu.edu.au/sso/fortran
o**ents and continuation %n ine of code after J is treated as a
co**ent
% state*ent can e roken into *utipeines appending a K (in $%&'%" it is ...)print*, 'Length of the two sides are', &
side_1, 'and', side_2, &
'metres'$utipe state*ents can e put onto a singe
ine separating the* -ith se*i>coons (L)
side_1 = 3; side_2 = 4; h!ot = 1"
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
17/145http://maxwell.me.gu.edu.au/sso/fortran
rinting to the screenWe can see the print state*ent ao-s us to
dispa te,t on the screen
Snta,5 printfmt, list
fmtspecifies ho- the te,t is to e for*atted(*ore aout this ater)
If fmtis an asterisk (C)7 for*atting isauto*atic (ist>directed I#)
list can consist of strings (te,t) or variaes
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
18/145http://maxwell.me.gu.edu.au/sso/fortran
8eading fro* keoard&o read data fro* the keoard7 -e use the
readstate*ent
Snta,5readfmt7 list
If fmtis an asterisk (C)7 the for*at of inputdata is auto*atic
list consists of variaes to store the data
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
19/145http://maxwell.me.gu.edu.au/sso/fortran
;ariaes 'ike $%&'%"7 variaes are *e*or
ocations used to store data
nike $%&'%"5 -e *ust decare every variablethat -e use
in the progra*(unike $%&'%") ;ariae na*es are not case>sensitive (e.g. pi7
pI7 i7 I are the sa*e na*e) Snta, for decaring variaes data_type[, specifiers##] variable_name
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
20/145http://maxwell.me.gu.edu.au/sso/fortran
!,a*pes of variaedecarations integer## $o%nter, i, real## mass real, parameter## !i = 3141( logical## flag = .false. complex## im!edan$e
character)2" ## name
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
21/145
http://maxwell.me.gu.edu.au/sso/fortran
Intrinsic nu*eric tpes Integer (M tes 43 its) % -hoe nu*er that has no fractiona part or
deci*a point e.g. 47 >MM7 30 'argest5 3MMA4?M
Real (M tes 43 its) % nu*er that can have a fractiona part or deci*a
point e.g. 4.07 3.4M7 >9A.37 4.4eA I!!! 2M singe precision5 ? deci*a digits
S*aest7 'argest5 .2M9M42!>4A7 4.M03A34M!+4A
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
22/145
http://maxwell.me.gu.edu.au/sso/fortran
Intrinsic nu*eric tpesDouble precision(A tes ?M its) % rea nu*er -ith t-ice the precision e.g. 4.4dA
I!!! 2M doue precision5 2 deci*a digits
S*aest5 3.33204A2A20304A!>40A 'argest5 .9?944MA?342!+40A
Complex
o*pe, nu*er consisting of a rea part andi*aginar part
e.g. 4.3 / M.9A i E (4.37 M.9A)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
23/145
http://maxwell.me.gu.edu.au/sso/fortran
Intrinsic non>nu*eric tpesCharacter %SII character or te,t e.g. BaB7 BtB7 B:B character(N) to decare string of Nchars an use either N or B to dei*it strings for concatenation e.g. BacBBdefB
len() function returns the nu*er ofcharacters
Logical !ither .true.or .false.
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
24/145
http://maxwell.me.gu.edu.au/sso/fortran
1ind para*eter&he range of an integer (*a, 3MMA4?M)
*a not e enough
&he precision of doue precision (2) *anot e enough
Fortran 90 ao-s us to specif the kind ofinteger and the kind of rea
&he kind nu*er is usually(ut not a-as)the nu*er of tesinteger(kindE A) or integer(A) / A te integer
real(A) / A te rea (doue precision)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
25/145
http://maxwell.me.gu.edu.au/sso/fortran
1inds for different co*piers o*pier specific (refer to kinds.f90 and
kindfind.f90)
gfortran supports5
integer()7 integer(3)7 integer(M)7 integer(A)7integer(?) (?M it on)
rea(M)7 rea(A) (doue precision)7
rea(0) / precision of A (e,tended precision)
'ahe Fortran 92 and Inte ;isua Fortransupports5 integer()7 integer(3)7 integer(M)7 integer(A)
rea(M)7 rea(A) (doue precision)7
rea(?) / precision of 44 (=uad precision)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
26/145
http://maxwell.me.gu.edu.au/sso/fortran
Intrinsic kind functions kind(a) -i return the kind of variae a
se the foo-ing functions for portaiit
(since the kind nu*er *a not e e=ua tothe nu*er of tes)5selected_int_kind(p) returns the kind of
integer -ithpsignificant digits
selected_real_kind(p7 r) returns the kindnu*er of a rea -ith p digit precision anddeci*a e,ponent range of >rand +r
&hese functions return > if not supported the co*pier7 generating a co*pie error
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
27/145
http://maxwell.me.gu.edu.au/sso/fortran
Specifing the kind of iteraconstant In order to specif the kind for a itera
constant7 -e use an underscore foo-ed
the kind nu*er 34O3 / integer(3) itera constant
34.4OA / rea(A) itera constant
$ore convenient to use para*etersinteger7 parameter55 sp E selected_real_kind(?)
integer7 parameter55 dp E selected_real_kind(2)
&hen to specif a doue precision itera7 -etpe5 4.M?Odp
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
28/145
http://maxwell.me.gu.edu.au/sso/fortran
1ind nu*er e,a*peprogram+ind_eam!le
implicit none
integer, parameter## d! = selected_real_kind)1-
integer, parameter## e! = selected_real_kind)1.
real## a
double precision## /, $ real)e! ## d
a = 1234-(0."1234-(0."1234-(0."1234-(0.0
/ = 1234-(0."1234-(0."1234-(0."1234-(0.0
$ = 1234-(0."1234-(0."1234-(0."1234-(0.0_d!
d = 1234-(0."1234-(0."1234-(0."1234-(0.0_e!
print*, 'real)4 ', a
print*, 'real)4 ', /
print*, 'real). ', $
print*, 'real)1" ', d
end program+ind_eam!le
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
29/145
http://maxwell.me.gu.edu.au/sso/fortran
Intrinsic functions forco*pe, variaes real(P) / returns the rea part of P
aimag(P) / returns the i*aginar part of P
conjg(P) / return co*pe, conugate of P
abs(P) / returns *agnitude of P
P E cmplx(a7 ) / for* co*pe, nu*er
fro* t-o rea variaes a and
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
30/145
http://maxwell.me.gu.edu.au/sso/fortran
Initiaising variaes&here are three -as to initiaise variaes
-ith starting vaues5
During the decaration5real## mass1 = -"3, mass2 = 1"""
In a datastate*ent (version )5
real## mass1, mass2,
datamass1, mass2 -"3, 1""" In a datastate*ent (version 3)5
real## mass1, mass2,
datamass1 -"3, mass2 1"""
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
31/145
http://maxwell.me.gu.edu.au/sso/fortran
8epeating initiaising data If a ist of variaes need to e initiaised -ith
the sa*e vaue7 -e can repeat the*
Instead of5
integer## a, /, $, d
dataa, /, $, d ", ", ", "
We can use5integer## a, /, $, d
dataa, /, $, d 4 * "Works for initiaising arras too
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
32/145
http://maxwell.me.gu.edu.au/sso/fortran
%rith*etic operators%ddition +
Sutraction >
$utipication C
Division
!,ponentiation CC
e.g. 43E 4 CC 3 For integer po-ers7 it is faster to *utip
*anua
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
33/145
http://maxwell.me.gu.edu.au/sso/fortran
8eationa operators Greater than .gt.or
Greater than or e=ua .ge.or !
'ess than .lt.or "
'ess than or e=ua .le. or "!
!=ua .e#.or !!
-
8/10/2019 Fortran Workshop
34/145
http://maxwell.me.gu.edu.au/sso/fortran
'ogica operators e=uivaence .ne#v.
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
35/145
http://maxwell.me.gu.edu.au/sso/fortran
Si*pe e,a*pe progra*program age_!rogram
implicit none
integer## ear, age, !resent_ear = 2"11
character)1" ## m_name ! string of 10 chars
print*, 'hat is o%r name'
read*, m_name
print*, 'hat ear were o% /orn'
read*, ear
age = !resent_ear 5 ear
print*, 'Hello', m_name, 'o% are of age', age
end program age_!rogram
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
36/145
http://maxwell.me.gu.edu.au/sso/fortran
Data tpe conversions
-
8/10/2019 Fortran Workshop
37/145
http://maxwell.me.gu.edu.au/sso/fortran
!,picit data tpe conversionWe can e,picit force data tpe conversions7
ust to e certain
int(,) E converts to integer real(,) E convert to rea
dble(,)7 dfloat(,) E convert to doueprecision
cmplx(,) E converts reainteger to co*pe,(no i*aginar part)
cmplx(,7 ) E converts reainteger toco*pe, (rea , and i*aginar )
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
38/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*peinteger## a = 3, $ = 0
real## / = 43, d
complex## e
$ = a 6 /
d = real)a 6 /
e = cmplx)/, $ ! not e = (b, cprint*, $, d, e
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
39/145
http://maxwell.me.gu.edu.au/sso/fortran
Do oop Snta,5
do[7ar = start, end, ste!]
statements
end do
var*ust e an integer
!,a*pe5doi = 1, 1", 2
total = total 6 i
end do
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
40/145
http://maxwell.me.gu.edu.au/sso/fortran
Do oop (no counter) Infinite do (no var) can e stopped
-ith exit
do
if)condition then
exit end if
end do
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
41/145
http://maxwell.me.gu.edu.au/sso/fortran
I*pied Do oop an e used for5 Initiaising arras
8eading a ist of vaues (usefu for 3D arras) rinting a ist of vaues
Snta,5 )statement, var= start, stop, step
print*, )i, i = ", 2", 2 read*, )ta/le)i, i = 1, 1" real## 7al%es)2"
7al%es = ) )"2 * i, i = 1, 2"
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
42/145
http://maxwell.me.gu.edu.au/sso/fortran
8eading -ith i*pied do o*pare this code5
doi = 1, 1"
read *, 7al%e)i
end doWith this code5
read*, )7al%e)i, i = 1, 1" Is there a difference6
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
43/145
http://maxwell.me.gu.edu.au/sso/fortran
Whie oop Introduced in Fortran 90 (so*e Fortran
co*piers supported it)
Snta,5do while)expression
statements
end do
!,a*pe5do while)total .lt.1""
total = total 6 1
end do
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
44/145
http://maxwell.me.gu.edu.au/sso/fortran
If then ese... !,a*pe5
if)dis$rim .gt." then
print*, '8here are two roots'
else if)dis$rim .eq." then
print*, '8here is a single root'
else
print*, '8he roots are $om!le'
end if
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
45/145
http://maxwell.me.gu.edu.au/sso/fortran
Seect state*entselect case)o!tion
case)1
print*, '9ardening'
case)2#1"
print*, ':lothes'
case)11, 12, 1-, 2"#3"
print*, 'esta%rant'
case default
print*, ':ar!ar+'
end select
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
46/145
http://maxwell.me.gu.edu.au/sso/fortran
%rras %rras are variaes that consist of *utipe
ee*ents of the sa*e tpe
Fortran supports up to di*ensions " defaut7 arra indices start at
!,a*pes5integer## ta/le1)2", 7e$tor)-, -
complex, dimension)3, 3 ## matri1, matri2
ta/le1)1 = 3
7e$tor)2, 3 =
-
8/10/2019 Fortran Workshop
47/145
http://maxwell.me.gu.edu.au/sso/fortran
usto* arra indices and
initiaisationWe can redefine the inde, range
integer## n%m/er)" # 1", $ost)
-
8/10/2019 Fortran Workshop
48/145
http://maxwell.me.gu.edu.au/sso/fortran
%rra initiaisation using a
data state*entWe can aso initiaise arras using a data
state*ent (fro* Fortran )
real## readings)-, $o%nt)3, 7oltage).datareadings 12, ",
-
8/10/2019 Fortran Workshop
49/145
http://maxwell.me.gu.edu.au/sso/fortran
Ro- *utidi*ensiona arras
are stored nike 7 Fortran stores *utidi*ensiona
arras using column-order
indices to the eft change fastest I*agine a 3D arra5 integer55 a(37 4)
In Fortran7 this arra is stored in this order5
Ha(7)7 a(37)7 a(73)7 a(373)7 a(74)7 a(374)
a!1"1#
a!2"1#
a!1"2#
a!2"2#
a!1"$#
a!2"$#
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
50/145
http://maxwell.me.gu.edu.au/sso/fortran
%rra sicing 'ike $%&'%"7 -e can seect su>sections or
sices of an arra
Snta,5 array_name)start#end#stride
integer## a)1", /)-, total
dataa 1, 2, 3, 4, -, (, 0, ., , 1"
/ = a)3#0 a)3,a)4,a)-,a)(,a)0
/ = a)#- a)1,a)2,a)3,a)4,a)-
/ = a)(# a)(,a)0,a).,a),a)1"/ = a)1#1"#2 a)1,a)3,a)-,a)0,a)
/ = a)) 2, -, 0, ., a)2,a)-,a)0,a).,a)
total = sum)a)2#0 ! um is an intrinsic function
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
51/145
http://maxwell.me.gu.edu.au/sso/fortran
%rra operators
-
8/10/2019 Fortran Workshop
52/145
http://maxwell.me.gu.edu.au/sso/fortran
Where...ese-here...end
-here %o-s us to perfor* an operation on on
certain ee*ents of an arra (ased on a
*asking condition)e.g. where)a > "" a = 1" a
&his perfor*s a reciproca on on theee*ents of arra athat are positive
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
53/145
http://maxwell.me.gu.edu.au/sso/fortran
Where...ese-here...end
-here$ore genera for*5where)logical expression
array operations
elsewhere
array operations
end where
For e,a*pe5
where)a > "" a = log)a ! log of positive elements
elsewhere
a = "" ! set negative elements to "ero
end where
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
54/145
http://maxwell.me.gu.edu.au/sso/fortran
Intrinsic vector*atri,
functions&ranspose a E transpose()
Dot product c E dot_product(a7 )
$atri, *utipication (*atrices *ust confor*) c E matmul(a7 )
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
55/145
http://maxwell.me.gu.edu.au/sso/fortran
$atri, e,a*pe Given t-o *atrices % and "
We -ant to -rite a Fortran progra* thatco*putes and prints the foo-ing5
E %"%&
A=
[1 2
3 4
] B=
[5 6
7 8
]
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
56/145
-
8/10/2019 Fortran Workshop
57/145
http://maxwell.me.gu.edu.au/sso/fortran
In=uiring and reshaping
arras s E shape(a) returns the shape of arra a
n E si%e(a) returns the tota nu*er of
ee*ents in arra a n E si%e(a7 i) returns the tota nu*er of
ee*ents aong di*ension iof arra a
c E reshape(7 s) function to change shape
of arra bto shape of a(stored as s) e.g. $ = reshape)/, )3, 4
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
58/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe arra progra*programresha!e1
implicit none integer## a)4, /)2, 2, $)2, 2, i, , d)3, -
dataa 1, 2, 3, 4, / 1, 2, 3, 4
print*, '?ha!e of a=', shape)a
print*, '?ha!e of /=', shape)/
print*, 'si@e of dim 2=', size)d, 2
$ = reshape)a, )2, 2
print*, 'Aatri /'
doi = 1, 2
print*, )/)i, , = 1, 2
end do print*, 'Aatri $'
doi = 1, 2
print*, )$)i, , = 1, 2
end do
end programresha!e1
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
59/145
http://maxwell.me.gu.edu.au/sso/fortran
#utput
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
60/145
http://maxwell.me.gu.edu.au/sso/fortran
%ocatae arras ti*e
se aocatae arras if their siPe is not kno-nreal, allocatable## arra)#
In the code7 to aocate this arra to nee*ents
allocate)arra)n
When not needed7 -e can free the *e*ordeallocate)arra
Qou can aocate *utidi*ensiona arras
real, allocatable## arra)#, #, #
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
61/145
http://maxwell.me.gu.edu.au/sso/fortran
For*atting screen outputWe have used printC7 var7 var37 var4
&he asterisk C *eans use ist>directed output
(co*pier auto*atica for*ats thevariaes)
For *ore fine for*atting contros7 there aret-o *ethods5
Incuding a for*at specifier string sing the formatstate*ent
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
62/145
http://maxwell.me.gu.edu.au/sso/fortran
For*atting screen output For*at specifier string5
print')1, i-, -, f1".', inde, densit
For*at state*ent5
print1", inde, densit1" format)1, i-, -, f1".
&hese e,a*pes oth use the foo-ingfor*atting for each ine5 #ne space (,)7 then
Integer nu*er -ith -idth of 2 paces (i2)7 then
Five spaces (2,)7 then
8ea nu*er -ith -idth of 0 paces and A deci*apaces (f0.A)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
63/145
http://maxwell.me.gu.edu.au/sso/fortran
For*atting specifiers Nx/ < ank spaces
iW/ integer nu*er -ith W paces
fW.D rea nu*er -ith W tota paces(incudes deci*a point) and D deci*apaces
eW.DeE/ rea nu*er in e,ponentia for*at
-ith W paces7 D deci*a paces7 !e,ponentia paces (e.g. 0.,,,ePP)
enW.D engineering notation (e47 e?7 e97...)
esW.D scientific notation
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
64/145
http://maxwell.me.gu.edu.au/sso/fortran
$ore for*at specifiers aW/ character -ith W paces
tN/ *ove to asoute screen position ne- ine (end of record)
N() > repeat the for*at inside () Nti*es
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
65/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*peprogramad7format
implicit none
integer## iteration = 23, i
real## estimate = 231234-(0..00(, error = 04324
real, parameter## !i = 3141-2(-4
print')B8he 7al%e of !i isB, 1, f0-', !i
print- ! print column headings
doi = 1, 1"
print1", iteration, estimate, error
end do
- format)'Cteration', 4, 'Dstimate', 4, 'Drror'
1" format)i4, 4, f1"-, 4, f41
end programad7format
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
66/145
http://maxwell.me.gu.edu.au/sso/fortran
#utput
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
67/145
http://maxwell.me.gu.edu.au/sso/fortran
#pening te,t fies for
reading-riting&o open a te,t fie for reading-riting5
open)%nit = num, file = filename,
stat%s =mode, [options]
!ach fie opened has associated -ith it a unitnu*er (unit E is optiona)
&he status sets the *ode of the fie5old/ the fie aread e,ists and is not to e repaced
ne&/ create a ne- fie (the fie *ust not e,ist)replace/ over-rite e,isting fie (if it e,ists) or create a
ne- one (if it does not e,ist)
scratch/ -riting to te*porar fie that is auto*aticadeeted -hen cosed
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
68/145
http://maxwell.me.gu.edu.au/sso/fortran
Fie opening Hoptions positionE BappendB (appends data to BodB
e,isting fie)
actionE read-riteread-rite read / cannot perfor* -rites on this fie
-rite / cannot perfor* reads on this fie
read-rite / can oth -rite and read
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
69/145
http://maxwell.me.gu.edu.au/sso/fortran
Fie opening e,a*pes&o open an e,isting fie Bdata.datBopen)%nit = 1", file = 'datadat', stat%s = 'old'
&o open an e,isting fie Bdata.datB (appends-ritten data)open)1", file = 'datadat', stat%s = 'old',!osition = 'a!!end'
&o -rite data to a ne- fie Boutput.t,tB that doesnot e,ist
open)2, file = 'o%t!%ttt', stat%s = 'new'
8epace an e,isting fie Boutput.datB or create it ifit does not e,istopen)-, file 'o%t!%tdat', stat%s = 're!la$e'
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
70/145
http://maxwell.me.gu.edu.au/sso/fortran
osing fies that are openWhen a fie is not used an*ore7 it is
reco**ended that it e cosed
When -riting7 cosing a fie -i ensure datain the *e*or uffer is -ritten to disk
Snta,5
close)num
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
71/145
http://maxwell.me.gu.edu.au/sso/fortran
Writing to a fie Snta,5
&rite(num7 fmt7 Hoptions)7 var17 var27 ...
numis the unit nu*er of the fie (C is theter*ina e.g.&rite(C7 C) is e=uivaent to printC7)
fmtis the for*at specifier string (can use C)
Hoptions incude rec7 err7 end7 advance
If using for*at specifier string and -ant touse *ini*a spacing7 -e can use a B0 -idthBfor i7 a7 f (ut not e7 en7 es)
e.g. i07 a07 f0.0
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
72/145
http://maxwell.me.gu.edu.au/sso/fortran
Writing e,a*pe integer## iter, i
real## est, error
open)-, file = 'o%tdat', stat%s = 'new'
write)-, 1" ! write column headings
1" format)'Cter', 4, 'Dstimate', 4, 'Drror'
doi = 1, 1""
write)-, 1- iter, est, error end do
1- format)i4, 4, f04, 4, f-2
close)-
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
73/145
http://maxwell.me.gu.edu.au/sso/fortran
#pening a Bne-B fie that
aread e,ists Fortran -i give a runti*e error if opening a
Bne-B fie that aread e,ists
&his prevents the progra* fro* over-ritinge,istingfies (so it is a safet precaution)
For e,a*pe5
Et line 0 of file sim!leritef")%nit = -, file = ''
Fortran r%ntime error# File'sim!ledat' alread eists
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
74/145
http://maxwell.me.gu.edu.au/sso/fortran
#pening an BodB fie that
does not e,istEt line 0 of file sim!leritef" )%nit= -, file = ''
Fortran r%ntime error# File 'sim!ledat'does not eist
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
75/145
http://maxwell.me.gu.edu.au/sso/fortran
8eading fro* a fie Snta,5
read(num7 fmt7 Hoptions)7 var17 var27 ...
fmtE C (ist>directed input) is good for *ost
cases&he for*at specifiers ao- ver fine contro
-hen nu*ers are not separated spaces ordeci*a points
For e,a*pe7 if the data fie has a rea7 an integer
and a rea -ith no spaces5
34.M2?3M44.M?
We can use the foo-ing for*at specifier5
format)f04, i2, f(4
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
76/145
http://maxwell.me.gu.edu.au/sso/fortran
8eading rea nu*ers %nother e,a*pe5
4M29 -ith f.2 -oud transfer as 4.M29
f.M -oud transfer as 4.M29 When reading rea nu*ers -ith a deci*a point
and a space after fractiona part7 the D part offW.D is overridden and W determines precision
For e,a*pe (note bis a ank space) b9.439b-ith fA.4 -oud transfer as 9.439 (the .4 isoverridden)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
77/145
http://maxwell.me.gu.edu.au/sso/fortran
8eading rea nu*ers For e,a*pe7 given the foo-ing nu*er
3.A34M243M4
If -e read using f0.7 -e transfer3.A34M2 (. is overridden to fi up to 0paces)
If -e read using f4.7 -e transfer 3. (. is
overridden ut sti get truncation ecauseon 4 paces)
&his on appies to reading7 not -riting
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
78/145
http://maxwell.me.gu.edu.au/sso/fortran
8eading unti end of fie If -e do not kno- ho- *an vaues to read7
-e need to detect the end of fie read)unit, fmt, end=num 7ar1, 7ar2
When -e reach end of fie7 progra* u*ps tonum
do
read)-, *, end= 2", 7al%e
end do2" print*, Gea$hed end of file
hecking iostat (see ne,t side) is preferredover using end
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
79/145
http://maxwell.me.gu.edu.au/sso/fortran
!rror handing&o hande e,ceptions in read-rite7 -e
pass an integer as iostatinteger## ios
read)unit, fmt, iostat= ios, 7ar1, 7ar2
write)unit, fmt, iostat= ios, 7ar1, 7ar2
If the integer iosis5
negative / end of record or end of fiepositive / error detected0 / no proe*s
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
80/145
http://maxwell.me.gu.edu.au/sso/fortran
!rror handing e,a*pe
integer## ios
real## a
do
read)-, *, iostat= ios, a
if)ios .gt." then
print*, 'ead error'
stop ! abort program
else if )ios .lt."then
print*, 'Dnd of file rea$hed'
exit! exit the do loop
end if
end do
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
81/145
http://maxwell.me.gu.edu.au/sso/fortran
"ackspace and re-ind backspaceunit_num after each read-rite7 the fie pointer advances
to the ne,t record or ine ackspace *oves the pointer ack to the
previous record or ine after advancing
re&indunit_num
repositions pointer ack to the eginning ofthe fie
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
82/145
http://maxwell.me.gu.edu.au/sso/fortran
"ackspace e,a*pe 'et us sa -e have an open fie (2) that contains5
34
M
read(27 C)7 vaue E
read(27 C)7 vaue E 3 read(27 C)7 vaue E 4
ackspace 2
read(27 C)7 vaue E 4 (not 3)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
83/145
http://maxwell.me.gu.edu.au/sso/fortran
Suprogra*s It is good practice to spit a arge progra* into
s*aer suprogra*s
ro*otes readaiit and re>usaiit We pass data as argu*ents to suprogra*s
(du** variaes)
" defaut7 argu*ents arepassed by referenceinFortran (uness e,picit specified)
Fortran supports t-o tpes of suprogra*s Functions
Suroutines
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
84/145
http://maxwell.me.gu.edu.au/sso/fortran
Functions Functions accept data as argu*ents and
direct return one vaue
e.g. , E sin(theta)7 P E c*p,(re7 i*)We need to e,picit state the data tpe of
the argu*ents and return vaue
" defaut7 the function na*e is the return
variae#r -e can use the result cause
&here are severa -as to decare the returnvaue of a function
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
85/145
http://maxwell.me.gu.edu.au/sso/fortran
8eturn tpe of a function$ethod 5
functionadd),
implicit none real, intent(in)## ,
real## add
add = 6
end functionadd
&o ca this function7 c E add(a7 )
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
86/145
-
8/10/2019 Fortran Workshop
87/145
http://maxwell.me.gu.edu.au/sso/fortran
8eturn tpe of a function$ethod 45
functionadd), result)@
implicit none real, intent(in)## ,
real## @
@ = 6
end functionadd
&o ca this function7 c E add(a7 )
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
88/145
http://maxwell.me.gu.edu.au/sso/fortran
8eturn tpe of a function$ethod M5
realfunctionadd), result)@
implicit none real, intent(in)## ,
@ = 6
end functionadd
&o ca this function7 c E add(a7 )
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
89/145
http://maxwell.me.gu.edu.au/sso/fortran
Intent of argu*ents&hough optiona7 it is reco**ended to state the
intent of each argu*ent to a function orsuroutine
%o-s co*pier to pick up ca errors
&here are three tpes of intent5 intent(in)
intent(out)
intent(inout)
%rgu*ents -ith intent(out) and intent(inout)refect an changes ack to caing progra*(pass reference)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
90/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pereal functionestimate), error
implicit none
real, intent)in ##
real, intent)out ## error
estimate = #some code$
error = #approx estimation error$
end functionestimate&his function returns the esti*ate direct
ut aso the esti*ate error via theargu*ents
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
91/145
http://maxwell.me.gu.edu.au/sso/fortran
Suroutines Suroutines do not return a vaue direct
&he can indirect return vaues via the
argu*ents (intent of outor inout)&hough optiona7 a argu*ents shoud have
their intent specified
se the callstate*ent to ca a suroutine
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
92/145
http://maxwell.me.gu.edu.au/sso/fortran
Suroutine e,a*pesubroutineadd), , res%lt
implicit none
real, intent)in ## ,
real, intent)out ## res%lt
res%lt = 6
endsubroutineadd
&o ca this suroutine5
calladd)a, /, $
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
93/145
http://maxwell.me.gu.edu.au/sso/fortran
%ternate returningWhen e,ecutation reaches the end
state*ent of a function or suroutine7 -e
return ack to the caing progra* If -e -ant to return ack via an aternate
*eans7 -e can use the returnstate*ent
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
94/145
http://maxwell.me.gu.edu.au/sso/fortran
%ssu*ed siPe arras and
strings
-
8/10/2019 Fortran Workshop
95/145
http://maxwell.me.gu.edu.au/sso/fortran
Interna suprogra*sWe can define suprogra*s to e kno-n and
used on the *ain progra* (often caed
utiities)&hese suprogra*s are internaland cannot
e caed other *ain progra*s
sed functionssuroutines that are on
usefu for the current *ain progra*
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
96/145
http://maxwell.me.gu.edu.au/sso/fortran
Snta, for interna
suprogra*sprogramprogram_name
implicit none
[executable statements]
contains
[function or subroutine]
end program!rogram_name
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
97/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pes of interna
suroutines First e,a*pe re=uires the siPe of the arra
to e passed to suroutine
Second e,a*pe uses the shape() functionso -e on need to pass the arra
Suroutine in second e,a*pe is *oregeneric
program internal
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
98/145
http://maxwell.me.gu.edu.au/sso/fortran
programinternal
implicit none
integer## a)3, 3
dataa 1, 2, 3, 4, -, (, 0, ., call!rintAatri)a, 3, 3
contains ! internal subroutine
subroutine!rintAatri)a, n%mows, n%m:ols
implicit none
integer, intent)in ## n%mows, n%m:ols, &
a)n%mows, n%m:ols
integer## i,
doi = 1, n%mows
print*, )a)i, , = 1, n%m:ols
end do
end subroutine!rintAatri
end programinternal
program internal
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
99/145
http://maxwell.me.gu.edu.au/sso/fortran
programinternal
implicit none
integer## a)3, 3
dataa 1, 2, 3, 4, -, (, 0, ., call!rintAatri)a
contains ! internal subroutine
subroutine!rintAatri)a
implicit none integer, intent)in ## a)#, #
integer## i, , s)2
s = shape)a
doi = 1, s)1 print*, )a)i, , = 1, s)2
end do
end subroutine!rintAatri
end programinternal
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
100/145
http://maxwell.me.gu.edu.au/sso/fortran
sing e,terna suprogra*sWe can ca functions or suroutines that are
e,terna to the progra*
&his ao-s us to uid up a BirarB of usefuprocedures for future use
We can specif -hich procedures aree,terna (*andator for functions)
&-o *ethods -e can use5 sing externalstate*entattriute
Decaring an e,picit interface (highreco**ended)
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
101/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe using e,terna
functionprogrameam!le1
implicit none
real## = 3, = 4
real, external## $al$adi%s
print*, 'adi%s = ', $al$adi%s),
end programeam!le1
real function$al$adi%s),
implicit none
real, intent)in ## ,
$al$adi%s = sqrt) ** 2 6 ** 2
end function$al$adi%s
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
102/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe using e,terna
suroutineprogram eam!le2
implicit none
real## = 3, = 4, r
external$al$adi%s
call$al$adi%s), , r
print*, 'adi%s = ', r
end programeam!le2
subroutine$al$adi%s), , r
implicit none real, intent)in ## ,
real, intent)out ## r
r = sqrt) ** 2 6 ** 2
end subroutine$al$adi%s
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
103/145
http://maxwell.me.gu.edu.au/sso/fortran
!,terna function (using
e,picit interface)programeam!le1
implicit none
real## = 3, = 4
interface
real function$al$adi%s),
real, intent)in ## ,
end function$al$adi%s
end interface
print*, 'adi%s = ', $al$adi%s),
end programeam!le1
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
104/145
http://maxwell.me.gu.edu.au/sso/fortran
!,terna suroutine (using
e,picit interface)programeam!le2
implicit none
real## = 3, = 4, r
interface subroutine$al$adi%s), , r
real, intent)in ## ,
real, intent)out ## r
end subroutine$al$adi%s
end interface
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
105/145
http://maxwell.me.gu.edu.au/sso/fortran
Wh use interfaces6 %o-s the co*pier to check -hether ou
have the correct argu*ents to the e,ternafunctionsuroutine ca
If ou use external7 the co*pier cannotcheck the argu*ents (*a cause runti*eerrors)
&r re*oving one of the argu*ents to
cac8adius and see ho- the co*pier reacts external*a e used if the
functionsuroutine is not direct caed (see
-
8/10/2019 Fortran Workshop
106/145
http://maxwell.me.gu.edu.au/sso/fortran
%nother e,a*pe of e,terna
dangerprogrammain
implicit none
real, external## add
print*, '8he res%lt is', add)3, 4end programmain
real functionadd)a, / result)$
implicit none
real, intent)in ## a, /
$ = a 6 /
end function add
OUTPUT:
8he res%lt is ."".2-D
-
8/10/2019 Fortran Workshop
107/145
http://maxwell.me.gu.edu.au/sso/fortran
BSaveB variaes ;ariaes decared in suprogra*s are
destroed after returning (i.e. voatie)
We can decare save variaes that re*ain(and retain their vaue) after the suprogra*has finished
integer, save## i
Initiaising a variaein a suprogra* *akesit automaticallya BsaveB variae
integer## i = "
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
108/145
http://maxwell.me.gu.edu.au/sso/fortran
!e*enta functions Functions -ith scaar du** argu*ents can
on e passed scaars7 e.g.
realfunctionadd)a, / real## a, /
add = a 6 /
&he aove function on adds t-o scaars7 a
and If -e define it as elemental7 the function
can e used for adding confor*ing arras
elemental real functionadd)a, /
Fu e,a*pe of an ee*enta
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
109/145
http://maxwell.me.gu.edu.au/sso/fortran
Fu e,a*pe of an ee*enta
functionprogramelem implicit none
real, dimension)3 ## a, /
dataa 1, 2, 3, / -, 3, 1 print*, add)a, /
contains
elemental real functionadd)a, /
real, intent)in ## a, /
add = a 6 /
end functionadd
end program elem
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
110/145
http://maxwell.me.gu.edu.au/sso/fortran
$oduesWriting interface ocks is tedious -hen
caing *an different e,terna suprogra*s
Fortran 90 provides a -a of groupingreated suprogra*s (as -e as tpedefinitions and variaes) into a singe iraror module
o*piing a *odue creates a C.*od fie thatcontains the interfaces
" using a *odue7 -e do not need to -ritean interface ock for each suprogra*
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
111/145
http://maxwell.me.gu.edu.au/sso/fortran
Snta, for *oduesmodulemodule_name
[variable declarations]
[type definitions]
contains
[functions and subroutines]
end modulemodule_name
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
112/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe of a *oduemodulegriffith_st%dent
typest%dent
integer## n%m/er
character)3" ## name
real## g!a end typest%dent
integer ## n%m?t%dents, netI%m
contains
subroutineadd_st%dent)name, g!a
end subroutineadd_st%dent
end modulegriffith_st%dent
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
113/145
http://maxwell.me.gu.edu.au/sso/fortran
sing a *odueWhen co*piing a *odue7 a .*od fie (e.g.
griffithOstudent.*od) -i e created
&o use a *odue7 -e use the usestate*ent e.g.
programst%dentJrogram
usegriffith_st%dent
implicit none
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
114/145
http://maxwell.me.gu.edu.au/sso/fortran
sing *odues When using a *odue7 a tpe definitions and
variaes defined there -i e avaiae in the*ain progra*
It is possie to BhideB variaes or suprogra*s(as private)
% suroutines and functions can e caed-ithout the need for an e,picit interface ock
(the co*pier can sti check our argu*entsJJ)&herefore7 it is reco**ended to put our o-n
suroutinesfunctions into *odues
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
115/145
http://maxwell.me.gu.edu.au/sso/fortran
8ecursive functions So*e *athe*atica agorith*s are recursive
nature
For e,a*pe7 the factoria52J E 2 , MJ
MJ E M , 4J
4J E 4 , 3J
etc. In Fortran7 on recursive functions can ca
the*seves
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
116/145
http://maxwell.me.gu.edu.au/sso/fortran
8ecursive functions nike nor*a functions7 the function na*e
cannot e used as the return variae (-h6)
We add the resultstate*ent For e,a*pe5recursive functionfa$torial) result)res
real, intent)in ##
real## res
end functionfa$torial
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
117/145
http://maxwell.me.gu.edu.au/sso/fortran
8ecursive functions %nother *ethod of decaring the tpe of the
resut variae5
real recursive functionfactoria(,) result(res) or
recursive real functionfactoria(,) result(res)
8e*e*er to incude a stopping condition
-hen using recursive functions se sparing as the are so-
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
118/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe of recursive functionreal recursive functionfa$torial) result)fa$t
real, intent)in ##
if) .gt.1 then fa$t = * fa$torial) < 1
else
fa$t = 1 ! stopping condition
end if
end functionfa$torial
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
119/145
http://maxwell.me.gu.edu.au/sso/fortran
Derived data tpes %rras can on store ee*ents of the sa*e
data tpe
Derived data tpes can group different datavariaes (kno-n as structures in )
!,a*pe5
typest%dent
integer## n%m/er character)2" ## name
real## g!a
end typest%dent
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
120/145
http://maxwell.me.gu.edu.au/sso/fortran
Derived data tpes&o decare a variae of the derived data
tpe5
type)st%dent ## /o/We use the operator to access the
*e*ers
/o/Kname = 'o/'
/o/Kn%m/er = 1234-/o/Kg!a = -.
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
121/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe of reading into
derived data tpestype)st%dent ## adam, e7e
adam = st%dent)1234-, 'Edam_?andler', -(
open)1", file= 't!eett', status= 're!la$e'
write)1", * adam ! write using listed %&'
close)1"
open)12, file= 't!eett', status= 'old'
read)12, * e7e ! read using listed %&'close)12
print*, adam
print*, e7e
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
122/145
http://maxwell.me.gu.edu.au/sso/fortran
Function overoading B#veroadingB *eans caing different
suprogra*s using the sa*e generic na*e
Which suprogra* is caed depends on the tpeof the argu*ents
For e,a*pe7 -e *ight have t-o versions of thefunction na*e func(,) sfunc(,) if , is a rea
dfunc(,) if , is a doue precision
#veroading ao-s Fortran to auto*aticachoose to ca sfunc() or dfunc() depending onthe tpe of ,
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
123/145
$ d d
-
8/10/2019 Fortran Workshop
124/145
http://maxwell.me.gu.edu.au/sso/fortran
$odue procedure
overoading Functions defined in an *odue aread have
an e,picit interface
So -e donBt need to define another e,picitinterface
&o overoad *odue functions sfunc anddfunc5
interfacef%n$ module proceduresf%n$, df%n$
end interfacef%n$
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
125/145
http://maxwell.me.gu.edu.au/sso/fortran
#perator overoading Suppose -e -rite a function that processes
t-o derived tpes (e.g. add)
8ather than caing the add() function7Fortran 90+ ao-s us to BoveroadB aconfor*ing arith*etic operator
For e,a*pe7 -e can BoveroadB the +
operator to ca the add function$akes progra* *ore readae
! f t
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
126/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe of operator
overoading 'et us define a poar coordinate derived tpe
in a *odue5
type!olar real## mag
real## angle
end type!olar
In our *odue7 -e define an poar%ddfunction that perfor*s an addition inrectanguar co>ordinates
! f t
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
127/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe of operator
overoadingfunction!olarEdd)a, / result)$
type)!olar, intent)in ## a, /
type)!olar ## $
We can overoad the + operator via theinterface
interfaceoperator)6
module procedure!olarEdd
end interface
-
8/10/2019 Fortran Workshop
128/145
http://maxwell.me.gu.edu.au/sso/fortran
!,a*pe of operator
overoading See the fu poaro*p source code
f t ti i ti
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
129/145
http://maxwell.me.gu.edu.au/sso/fortran
gfortran opti*isationWe can add the foo-ing s-itches for *ore
etter opti*isation >#3 or >#4 (eve 3 opti*isation is defaut) >funro>oops
>*tune E arch(opti*ises for a particuarprocessor) native ( used to co*pie) 7 pentiu*47
pentiu*M7 pentiu*>*7 core37 etc.
>*fp*athEsse >***,7 >*sse7 >*sse37 >*sse4 ....
http5gcc.gnu.orgoninedocsgcc>4.M.?gcc#pti*iPe>#ptions.ht*
http5gcc.gnu.orgoninedocsgcc>M.4.4gcci4A?>and>,A?O003d?M>#ptions.ht*
< t B th d
http://maxwell.me.gu.edu.au/sso/fortranhttp://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.htmlhttp://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/i386-and-x86_002d64-Options.htmlhttp://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/i386-and-x86_002d64-Options.htmlhttp://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.htmlhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
130/145
http://maxwell.me.gu.edu.au/sso/fortran
-
8/10/2019 Fortran Workshop
131/145
http://maxwell.me.gu.edu.au/sso/fortran
Statistics *odue e,a*pe
(statistics.f90)&his progra* shoud read a data fie (reg.dat)
&his fie contains so*e header info and then 3
cou*ns of data (, and ) Functions and suroutines are -ritten to find
*ean7 variance7 and ine of est fit
&hese suprogra*s are grouped into a singe
*odue (statistics)
-
8/10/2019 Fortran Workshop
132/145
http://maxwell.me.gu.edu.au/sso/fortran
Static iraries 8ather than co*piing suprogra*s each
ti*e7 -e can store as oect fies (C.o) and
ink into the *ain progra* ater (saves ti*e) 'inking ots of oect fies can e a hasse
(esp. if ou donBt kno- -hich ones ou arecaing)
% static irar (C.a or C.i) is an archive forstoring *an reated oect fies into one fie
We si*p ink the static irar -ith our*ain progra*
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
133/145
http://maxwell.me.gu.edu.au/sso/fortran
"'%S and '%%1 iraries "asic 'inear %gera Suprogra*s ("'%S) and
'inear %gera ackage ('%%1)
"'%S contains routines for vector and *atri,*utipication (proa redundant due to
Fortran 90 capaiities) '%%1 contains routines for inear agera
(!;D7 S;D7 '7 T87 hoesk7 Schur7 etc.)
Generic "'%S and '%%1 source code can e
do-noaded fro* http5---.neti.orgapack o**ercia Fortran packages often co*e -ith
opti*ised '%%1 iraries (Inte $1'7 %$D%$'7 etc.)
http://maxwell.me.gu.edu.au/sso/fortranhttp://www.netlib.org/lapack/http://www.netlib.org/lapack/http://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
134/145
http://maxwell.me.gu.edu.au/sso/fortran
'%%1 iraries for -indo-s Static '%%1 iraries are provided -ith
co**ercia co*piers
We can uid a '%%1 static irar usingour o-n co*pier
Ro-ever7 the irar usua on inks tocode co*pied using sa*e co*pier
ie. a '%%1 irar *ade using gfortrancannot e inked into a progra* co*piedusing Inte ;isua Fortran
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
135/145
http://maxwell.me.gu.edu.au/sso/fortran
'%%1 na*ing convention#f the for* UQQVVV
U7 indicates the data tpe as foo-s5
S 8!%' D D#"'! 8!ISI#< #$'!U V #$'!UC? or D#"'! #$'!U
QQ7 indicate the tpe of *atri, (or of the*ost significant *atri,)
VVV7 indicate the co*putation perfor*ed
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
136/145
http://maxwell.me.gu.edu.au/sso/fortran
-
8/10/2019 Fortran Workshop
137/145
http://maxwell.me.gu.edu.au/sso/fortran
aing '%%1 fro* Fortran
90
-
8/10/2019 Fortran Workshop
138/145
http://maxwell.me.gu.edu.au/sso/fortran
sing '%%1 to cacuate
inverse of a *atri, '%%1 re=uires t-o suroutine cas (in
order)
sgetrf() > cacuates the ' deco*position of % sgetri() > uses ' deco*p of % to find its
inverse
8ead the docu*entation for each suroutine sgetrf.pdf or sgetrf.t,t
sgetri.pdf or sgetri.t,t
Write an interface ock for oth suroutines
i '%%1 t t
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
139/145
http://maxwell.me.gu.edu.au/sso/fortran
sing '%%1 to cacuate
eigenvectors and eigenvauesWe -i consider the '%%1 routinessyevd
for finding eigenvectors and eigenvauesfro* a s**etric *atri,
Docu*entation specifies the necessar siPeof each *atri, (-ork7 i-ork)
If -orking -ith doue precision *atrices7use dsyevd
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
140/145
http://maxwell.me.gu.edu.au/sso/fortran
sefu inks for using '%%1 http5---.neti.orgapackug(users
guide containing descriptions of a '%%1routines)
aing Fortran 90
http://maxwell.me.gu.edu.au/sso/fortranhttp://www.netlib.org/lapack/lug/http://www.netlib.org/lapack/lug/http://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
141/145
http://maxwell.me.gu.edu.au/sso/fortran
aing Fortran 90
suprogra*s fro* $%&'%" o*ines the speed of Fortran -ith $%&'%"
&here are t-o -as of doing this5
$!U fies 'oading of dna*ic iraries (D''s)
$!U fies are heavi docu*ented in $%&'%"
We -i ook at the second *ethod
Dna*ic iraries are si*iar to staticiraries7 e,cept that the are dna*icainked at runti*e
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
142/145
http://maxwell.me.gu.edu.au/sso/fortran
reating a dna*ic irar In Windo-s7 the e,tension is C.d (in 'inu,7 it
is C.so)
-
8/10/2019 Fortran Workshop
143/145
http://maxwell.me.gu.edu.au/sso/fortran
reating a header fie$%&'%" re=uires an acco*paning header
fie
onsists of the function na*e foo-ed anunderscore. e.g. add > addO
!,a*pe5
realfunctionadd)a, /
real, intent)in ## a, / add = a 6 /
end functionadd
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
144/145
http://maxwell.me.gu.edu.au/sso/fortran
header fie&he header fie (add.h) -oud consist of externfloatadd_)float*a, float*/;
&he corresponding data tpes in integer E int rea E foat
doue precision E doue
character E char If using arra argu*ents7 the siPe of the
arras *ust e passed
'oading and caing the D''
http://maxwell.me.gu.edu.au/sso/fortranhttp://maxwell.me.gu.edu.au/sso/fortran -
8/10/2019 Fortran Workshop
145/145
'oading and caing the D''
in $%&'%"We use the oadirar function in $%&'%"
loadli/rar)'li/add', 'addh'
#nce this has een oaded7 -e ca thefunction
a = $allli/)'li/add', 'add_', 2, 3