13.07.2015 Views

Fortran 90/95 and Computational Physics

Fortran 90/95 and Computational Physics

Fortran 90/95 and Computational Physics

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong><strong>Physics</strong>Jens Hjörleifur Bárðarsonjensba@raunvis.hi.isUniversity of Icel<strong>and</strong><strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.1/22


OverviewWhat is <strong>Fortran</strong>?Why <strong>Fortran</strong>?Some Important ThingsSummary<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.2/22


What is <strong>Fortran</strong> <strong>90</strong>?<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.3/22


The OriginA team lead by John Backus developed<strong>Fortran</strong>, FORmula TRANslation System, in1<strong>95</strong>4, one of the earliest high-level languages.<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.4/22


The OriginA team lead by John Backus developed<strong>Fortran</strong>, FORmula TRANslation System, in1<strong>95</strong>4, one of the earliest high-level languages.1966: The first ever st<strong>and</strong>ard for a programming language:<strong>Fortran</strong> 66New st<strong>and</strong>ard 1978: <strong>Fortran</strong> 77The need to modernise the language → <strong>Fortran</strong> <strong>90</strong>/<strong>95</strong><strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.4/22


<strong>Fortran</strong> <strong>90</strong>http://csep1.phy.ornl.gov/pl/pl.html<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.5/22


Why <strong>Fortran</strong> <strong>90</strong>?<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.6/22


How does F<strong>90</strong> compare?http://csep1.phy.ornl.gov/pl/pl.htmlOne of the ultimate goals of F<strong>90</strong> is that the code must beefficient<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.7/22


Numerical Libraries<strong>Fortran</strong> has been widely used by scientist <strong>and</strong> engineers formany years <strong>and</strong> therfore many algorithms to use innumerical calculations already exist.These have been collected in number of numerical libraries,some open (e.g. SLATEC http://www.netlib.org/slatec/ <strong>and</strong>Numerical Recipes http://www.nr.com/)<strong>and</strong> some that cost (e.g. NAG http://www.nag.co.uk).<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.8/22


Some F<strong>90</strong> Features<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.9/22


The ConstructsF<strong>90</strong> has many familiar constructs:IF (expr) ...<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.10/22


The ConstructsF<strong>90</strong> has many familiar constructs:IF (expr) ...IF (expr) THEN...END IF<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.10/22


The ConstructsF<strong>90</strong> has many familiar constructs:IF (expr) ...IF (expr) THEN...END IFDO i = 1, n...END DO<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.10/22


The ConstructsF<strong>90</strong> has many familiar constructs:IF (expr) ...IF (expr) THEN...END IFDO i = 1, n...END DOOther forms of the DO construct<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.10/22


The ConstructsF<strong>90</strong> has many familiar constructs:IF (expr) ...IF (expr) THEN...END IFDO i = 1, n...END DOOther forms of the DO constructCASE<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.10/22


Numeric Kind ParameterisationProgram test_kindImplicit noneReal :: a! selected_real_kind([p][,r]) p = precision, r = rangeInteger, parameter :: long = selected_real_kind(9,99)Real(long) :: ba = 1.7; b = 1.7_longPrint *, a,kind(a), precision(a), range(a)Print *, b,kind(b), precision(b), range(b)b = 1.7;b = 1.7D0;print *, bprint *,bEnd Program test_kind<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.11/22


IMPLICIT NONEStrong typing: all typed entities must have their typesspecified explicitlyBy default an entity in <strong>Fortran</strong> that has not been assigned atype is implicitly typed, e.g. entities that begin with i,j, ... areof type integer → dangerous source of errors(Legend has it that error of this type caused the crash of the American Space Shuttle)The statement IMPLICIT NONE turns on strong typing <strong>and</strong>its use is strongly recommended<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.12/22


Modules - Simple ExampleMODULE constantsIMPLICIT NONEINTEGER, PARAMETER :: long = SELECTED_REAL_KIND(15,307)REAL(long), PARAMETER :: pi = 3.14159265358979324D0END MODULE constantsPROGRAM module_exampleUSE constantsIMPLICIT NONEREAL(long) :: aa = 2D0*piprint*, aEND PROGRAM module_example<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.13/22


Modules - Another ExampleMODULE circleUSE constantsIMPLICIT NONECONTAINSFUNCTION area(r)REAL(long), INTENT(IN) :: rREAL(long):: areaarea = 2D0*pi*rEND FUNCTION areaFUNCTION circumference(r)REAL(long), INTENT(IN) :: rREAL(long):: circumferencecircumference = pi*r**2END FUNCTION circumferenceEND MODULE circle<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.14/22


Modules - Another Example - cont.PROGRAM module_example2USE constantsUSE circleIMPLICIT NONEREAL(long) :: r, A, Cr = 2A = area(r)C = circumference(r)print*, A, CEND PROGRAM module_example2<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.15/22


Array FeaturesPROGRAM arrayUSE constantsIMPLICIT NONEREAL(long), DIMENSION(10,10) :: aREAL(long), DIMENSION(5,5) :: b,cREAL(long):: da = 1D0; b = 2D0c = MATMUL(a(1:5,6:10),b)c = c + bd = SUM(c)print*, dEND PROGRAM array<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.16/22


External SubroutinesSUBROUTINE area_rectangle(l,b,A)USE constantsIMPLICIT NONEREAL(long), DIMENSION(:,:), INTENT(IN) :: l,bREAL(long), DIMENSION(size(l,1), size(l,2)) :: AA = l*bEND SUBROUTINE area_rectangle<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.17/22


External Subroutines - cont.PROGRAM subr_exampleUSE constantsIMPLICIT NONEINTERFACESUBROUTINE area_rectangle(l,b,A)USE constantsIMPLICIT NONEREAL(long), DIMENSION(:,:), INTENT(IN):: l,bREAL(long), DIMENSION(size(l,1), size(l,2)), INTENT(OUT) :: AEND SUBROUTINE area_rectangleEND INTERFACEREAL(long), DIMENSION(2,2) :: l,b,Al = 1D0; b = 2D0CALL area_rectangle(l,b,A);print*, AEND PROGRAM subr_example<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.18/22


External Subroutines - cont.External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfaced<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.19/22


External Subroutines - cont.External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfacedExternal subroutines can be made explicitly interfacedby the use of an interface block<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.19/22


External Subroutines - cont.External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfacedExternal subroutines can be made explicitly interfacedby the use of an interface blockGrouping related procedures <strong>and</strong> parameters intomodules is good programming<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.19/22


External Subroutines - cont.External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfacedExternal subroutines can be made explicitly interfacedby the use of an interface blockGrouping related procedures <strong>and</strong> parameters intomodules is good programmingWe imagine subprogram libraries being written as sets of externalsubprograms together with modules holding interface blocks forthem. Metcalf & Reid<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.19/22


Summary<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.20/22


Summary<strong>Fortran</strong> has from the beginning been designed fornumerical calculationsThe <strong>Fortran</strong> <strong>90</strong> st<strong>and</strong>ard modernised the languageArray features make F<strong>90</strong> especially attracting fornumerical work<strong>Fortran</strong> is fast<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.21/22


ResourcesCSEP. <strong>Fortran</strong> <strong>90</strong> <strong>and</strong> <strong>Computational</strong> Science.Technical report, Oak Ridge National Laboratory, 1994http://csep1.phy.ornl.gov/CSEP/PL/PL.htmlThe Liverpool <strong>Fortran</strong> <strong>90</strong> courses homepagehttp://www.liv.ac.uk/HPC/F<strong>90</strong>page.htmlMichael Metcalf <strong>and</strong> John Reid. <strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> explained,second edition. Oxford, 1999Chivers <strong>and</strong> Sleightholme. Introducing <strong>Fortran</strong> <strong>95</strong>. Springer,2000Brainerd, Goldberg <strong>and</strong> Adams. Programmer’s Guide to<strong>Fortran</strong> <strong>90</strong>, third edition. Springer, 1996dbforums.lang.fortran http://dbforums.com/f132/<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>and</strong> <strong>Computational</strong> <strong>Physics</strong> – p.22/22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!