Fortran 90/95 and Computational jancely/NM/Texty/Fortran/f90_comp_phys.pdf · 1966: The first ever…

Download Fortran 90/95 and Computational jancely/NM/Texty/Fortran/f90_comp_phys.pdf · 1966: The first ever…

Post on 30-Jul-2018

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Fortran 90/95 and ComputationalPhysics</p><p>Jens Hjrleifur Brarson</p><p>jensba@raunvis.hi.is</p><p>University of Iceland</p><p>Fortran 90/95 and Computational Physics p.1/22</p></li><li><p>Overview</p><p>What is Fortran?</p><p>Why Fortran?</p><p>Some Important Things</p><p>Summary</p><p>Fortran 90/95 and Computational Physics p.2/22</p></li><li><p>What is Fortran 90?</p><p>Fortran 90/95 and Computational Physics p.3/22</p></li><li><p>The Origin</p><p>A team lead by John Backus developedFortran, FORmula TRANslation System, in1954, one of the earliest high-level langua-ges.</p><p>1966: The first ever standard for a programming language:Fortran 66</p><p>New standard 1978: Fortran 77</p><p>The need to modernise the language Fortran 90/95</p><p>Fortran 90/95 and Computational Physics p.4/22</p></li><li><p>The Origin</p><p>A team lead by John Backus developedFortran, FORmula TRANslation System, in1954, one of the earliest high-level langua-ges.</p><p>1966: The first ever standard for a programming language:Fortran 66</p><p>New standard 1978: Fortran 77</p><p>The need to modernise the language Fortran 90/95</p><p>Fortran 90/95 and Computational Physics p.4/22</p></li><li><p>Fortran 90</p><p>http://csep1.phy.ornl.gov/pl/pl.html</p><p>Fortran 90/95 and Computational Physics p.5/22</p><p>http://csep1.phy.ornl.gov/pl/pl.html</p></li><li><p>Why Fortran 90?</p><p>Fortran 90/95 and Computational Physics p.6/22</p></li><li><p>How does F90 compare?</p><p>http://csep1.phy.ornl.gov/pl/pl.html</p><p>One of the ultimate goals of F90 is that the code must beefficient</p><p>Fortran 90/95 and Computational Physics p.7/22</p><p>http://csep1.phy.ornl.gov/pl/pl.html</p></li><li><p>Numerical Libraries</p><p>Fortran has been widely used by scientist and engineers formany years and therfore many algorithms to use innumerical calculations already exist.</p><p>These have been collected in number of numerical libraries,some open (e.g. SLATEC http://www.netlib.org/slatec/ andNumerical Recipes http://www.nr.com/)and some that cost (e.g. NAG http://www.nag.co.uk).</p><p>Fortran 90/95 and Computational Physics p.8/22</p><p>http://www.netlib.org/slatec/http://www.nr.com/http://www.nag.co.uk</p></li><li><p>Some F90 Features</p><p>Fortran 90/95 and Computational Physics p.9/22</p></li><li><p>The Constructs</p><p>F90 has many familiar constructs:</p><p>IF (expr) ...</p><p>IF (expr) THEN...END IF</p><p>DO i = 1, n...END DO</p><p>Other forms of the DO construct</p><p>CASE</p><p>Fortran 90/95 and Computational Physics p.10/22</p></li><li><p>The Constructs</p><p>F90 has many familiar constructs:</p><p>IF (expr) ...</p><p>IF (expr) THEN...END IF</p><p>DO i = 1, n...END DO</p><p>Other forms of the DO construct</p><p>CASE</p><p>Fortran 90/95 and Computational Physics p.10/22</p></li><li><p>The Constructs</p><p>F90 has many familiar constructs:</p><p>IF (expr) ...</p><p>IF (expr) THEN...END IF</p><p>DO i = 1, n...END DO</p><p>Other forms of the DO construct</p><p>CASE</p><p>Fortran 90/95 and Computational Physics p.10/22</p></li><li><p>The Constructs</p><p>F90 has many familiar constructs:</p><p>IF (expr) ...</p><p>IF (expr) THEN...END IF</p><p>DO i = 1, n...END DO</p><p>Other forms of the DO construct</p><p>CASE</p><p>Fortran 90/95 and Computational Physics p.10/22</p></li><li><p>The Constructs</p><p>F90 has many familiar constructs:</p><p>IF (expr) ...</p><p>IF (expr) THEN...END IF</p><p>DO i = 1, n...END DO</p><p>Other forms of the DO construct</p><p>CASE</p><p>Fortran 90/95 and Computational Physics p.10/22</p></li><li><p>Numeric Kind Parameterisation</p><p>Program test_kind</p><p>Implicit none</p><p>Real :: a</p><p>! selected_real_kind([p][,r]) p = precision, r = range</p><p>Integer, parameter :: long = selected_real_kind(9,99)</p><p>Real(long) :: b</p><p>a = 1.7; b = 1.7_long</p><p>Print *, a,kind(a), precision(a), range(a)</p><p>Print *, b,kind(b), precision(b), range(b)</p><p>b = 1.7; print *, b</p><p>b = 1.7D0; print *,b</p><p>End Program test_kind</p><p>Fortran 90/95 and Computational Physics p.11/22</p></li><li><p>IMPLICIT NONE</p><p>Strong typing: all typed entities must have their typesspecified explicitly</p><p>By default an entity in Fortran that has not been assigned atype is implicitly typed, e.g. entities that begin with i,j, ... areof type integer dangerous source of errors</p><p>(Legend has it that error of this type caused the crash of the American Space Shuttle)</p><p>The statement IMPLICIT NONE turns on strong typing and</p><p>its use is strongly recommended</p><p>Fortran 90/95 and Computational Physics p.12/22</p></li><li><p>Modules - Simple Example</p><p>MODULE constants</p><p>IMPLICIT NONE</p><p>INTEGER, PARAMETER :: long = SELECTED_REAL_KIND(15,307)</p><p>REAL(long), PARAMETER :: pi = 3.14159265358979324D0</p><p>END MODULE constants</p><p>PROGRAM module_example</p><p>USE constants</p><p>IMPLICIT NONE</p><p>REAL(long) :: a</p><p>a = 2D0*pi</p><p>print*, a</p><p>END PROGRAM module_example</p><p>Fortran 90/95 and Computational Physics p.13/22</p></li><li><p>Modules - Another Example</p><p>MODULE circle</p><p>USE constants</p><p>IMPLICIT NONE</p><p>CONTAINS</p><p>FUNCTION area(r)</p><p>REAL(long), INTENT(IN) :: r</p><p>REAL(long) :: area</p><p>area = 2D0*pi*r</p><p>END FUNCTION area</p><p>FUNCTION circumference(r)</p><p>REAL(long), INTENT(IN) :: r</p><p>REAL(long) :: circumference</p><p>circumference = pi*r**2</p><p>END FUNCTION circumference</p><p>END MODULE circle</p><p>Fortran 90/95 and Computational Physics p.14/22</p></li><li><p>Modules - Another Example - cont.</p><p>PROGRAM module_example2</p><p>USE constants</p><p>USE circle</p><p>IMPLICIT NONE</p><p>REAL(long) :: r, A, C</p><p>r = 2</p><p>A = area(r)</p><p>C = circumference(r)</p><p>print*, A, C</p><p>END PROGRAM module_example2</p><p>Fortran 90/95 and Computational Physics p.15/22</p></li><li><p>Array Features</p><p>PROGRAM array</p><p>USE constants</p><p>IMPLICIT NONE</p><p>REAL(long), DIMENSION(10,10) :: a</p><p>REAL(long), DIMENSION(5,5) :: b,c</p><p>REAL(long) :: d</p><p>a = 1D0; b = 2D0</p><p>c = MATMUL(a(1:5,6:10),b)</p><p>c = c + b</p><p>d = SUM(c)</p><p>print*, d</p><p>END PROGRAM array</p><p>Fortran 90/95 and Computational Physics p.16/22</p></li><li><p>External Subroutines</p><p>SUBROUTINE area_rectangle(l,b,A)</p><p>USE constants</p><p>IMPLICIT NONE</p><p>REAL(long), DIMENSION(:,:), INTENT(IN) :: l,b</p><p>REAL(long), DIMENSION(size(l,1), size(l,2)) :: A</p><p>A = l*b</p><p>END SUBROUTINE area_rectangle</p><p>Fortran 90/95 and Computational Physics p.17/22</p></li><li><p>External Subroutines - cont.</p><p>PROGRAM subr_example</p><p>USE constants</p><p>IMPLICIT NONE</p><p>INTERFACE</p><p>SUBROUTINE area_rectangle(l,b,A)</p><p>USE constants</p><p>IMPLICIT NONE</p><p>REAL(long), DIMENSION(:,:), INTENT(IN) :: l,b</p><p>REAL(long), DIMENSION(size(l,1), size(l,2)), INTENT(OUT) :: A</p><p>END SUBROUTINE area_rectangle</p><p>END INTERFACE</p><p>REAL(long), DIMENSION(2,2) :: l,b,A</p><p>l = 1D0; b = 2D0</p><p>CALL area_rectangle(l,b,A); print*, A</p><p>END PROGRAM subr_example</p><p>Fortran 90/95 and Computational Physics p.18/22</p></li><li><p>External Subroutines - cont.</p><p>External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfaced</p><p>External subroutines can be made explicitly interfacedby the use of an interface block</p><p>Grouping related procedures and parameters intomodules is good programming</p><p>We imagine subprogram libraries being written as sets of externalsubprograms together with modules holding interface blocks forthem. Metcalf &amp; Reid</p><p>Fortran 90/95 and Computational Physics p.19/22</p></li><li><p>External Subroutines - cont.</p><p>External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfaced</p><p>External subroutines can be made explicitly interfacedby the use of an interface block</p><p>Grouping related procedures and parameters intomodules is good programming</p><p>We imagine subprogram libraries being written as sets of externalsubprograms together with modules holding interface blocks forthem. Metcalf &amp; Reid</p><p>Fortran 90/95 and Computational Physics p.19/22</p></li><li><p>External Subroutines - cont.</p><p>External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfaced</p><p>External subroutines can be made explicitly interfacedby the use of an interface block</p><p>Grouping related procedures and parameters intomodules is good programming</p><p>We imagine subprogram libraries being written as sets of externalsubprograms together with modules holding interface blocks forthem. Metcalf &amp; Reid</p><p>Fortran 90/95 and Computational Physics p.19/22</p></li><li><p>External Subroutines - cont.</p><p>External subroutines are implicitly interfaced whilemodule subroutines are explicitly interfaced</p><p>External subroutines can be made explicitly interfacedby the use of an interface block</p><p>Grouping related procedures and parameters intomodules is good programming</p><p>We imagine subprogram libraries being written as sets of externalsubprograms together with modules holding interface blocks forthem. Metcalf &amp; Reid</p><p>Fortran 90/95 and Computational Physics p.19/22</p></li><li><p>Summary</p><p>Fortran 90/95 and Computational Physics p.20/22</p></li><li><p>Summary</p><p>Fortran has from the beginning been designed fornumerical calculations</p><p>The Fortran 90 standard modernised the language</p><p>Array features make F90 especially attracting fornumerical work</p><p>Fortran is fast</p><p>Fortran 90/95 and Computational Physics p.21/22</p></li><li><p>Resources</p><p>CSEP. Fortran 90 and Computational Science.Technical report, Oak Ridge National Laboratory, 1994http://csep1.phy.ornl.gov/CSEP/PL/PL.html</p><p>The Liverpool Fortran 90 courses homepagehttp://www.liv.ac.uk/HPC/F90page.html</p><p>Michael Metcalf and John Reid. Fortran 90/95 explained,second edition. Oxford, 1999</p><p>Chivers and Sleightholme. Introducing Fortran 95. Springer,2000</p><p>Brainerd, Goldberg and Adams. Programmers Guide toFortran 90, third edition. Springer, 1996</p><p>dbforums.lang.fortran http://dbforums.com/f132/</p><p>Fortran 90/95 and Computational Physics p.22/22</p><p>http://csep1.phy.ornl.gov/CSEP/PL/PL.htmlhttp://www.liv.ac.uk/HPC/F90page.htmlhttp://dbforums.com/f132/</p><p>OverviewWhat is Fortran 90?The OriginFortran 90Why Fortran 90?How does F90 compare?Numerical LibrariesSome F90 FeaturesThe ConstructsNumeric {ed Kind} Parameterisationgreen IMPLICIT NONEModules - Simple ExampleModules - Another ExampleModules - Another Example - cont.Array FeaturesExternal SubroutinesExternal Subroutines - cont.External Subroutines - cont.SummarySummaryResources</p></li></ul>