Introduction to FORTRAN Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University
Reference 彭國倫, Fortran 95 程式設計, 碁峰, 2001.
Outline History of FORTRAN Basics Declaration Program Flow Control Arrays Functions Pointers Modules
History of FORTRAN FORmula TRANslator First FORTRAN compiler John Backus, IBM, 1957 1st high level programming language Most popular in 1960’s FORTRAN 66 1st ANSI standard FORTRAN FORTRAN 77, 1978 FORTRAN 90, 1992 Pointers and OO FORTRAN 95, 1997 Support for parallel computation
Fixed Format (*.f, *.for) C FIXED FORMATDEMO program main write(*,*) ‘Hello’ write(*,*) 1’Hello’ 100 write(*,*) ‘Hello’ 10 Stop end
Free Format (*.f90) ! Free Format program main write(*,*) “Hello” ! This is also a comment write(*,*) & “Hello” wri& &te(*,*) “Hello” end
Basic Data Types and Arithmetic Operators INTEGER REAL COMPLEX CHARACTER LOGICAL Arithmetic operators +, -, *, /, **, ()
Demo Program program example integer a real b real*8 c complex d character(len=10) s logical t a = 2 + 2*4 – 3 b = 2.5 + 3.0/2.0 c = 0.1 write(*,*) a, “ “, b, “ “, c d = (1.0, 2.0) ! d = 1.0 + 2.0 i write(*,*) d s = “Hello” write(*,*) s t = .true. write(*,*) t stop end
Read Data and Formatted Output program readWriteDemo integer a, b, c real d complex e read(*,*) a, b, c write(*,100) a + b + c 100 format(I4) d = 12.3 write(*, “(1X, F5.2)”) d e = (1, 2) write(*, 200) e 200 format(1X, F4.1, F4.1) end
implicit and parameter Statements program demo implicit none integer :: i = 123 + 321 real pi write(*,”(‘123 + 321 =‘, I4)”) i parameter (pi = 3.141592653) write(*,”(F4.2)”) sin(pi/6) end
DATA Statement program demo implicit none integer a real b complex c character* (20) str data a, b, c, str /1, 2.0, (1, 2), ‘FORTRAN 77’/ write(*,*) a, b, c, str stop end
type Statement (FORTRAN 90) program typeDemo implicit none type::person character(len=30) :: name integer :: age end type person type(person) :: a; write(*,*) “Name:” read(*,*) a%name write(*,*) “Age:” read(*,*) a%age write(*,100) a%name, a%age 100 format(/,”Name: “, A10/,”Age:”,I3) stop end
if Statement and Logical Operations (FORTRAN 77) PROGRAM Demo IMPLICIT NONE REAL speed LOGICAL hasBarrier WRITE(*,*) “speed:” READ(*,*) speed hasBarrier = .false. IF ( speed .GT. 100 .OR. hasBarrier ) THEN WRITE(*,*) “Slow down” ELSE WRITE(*,*) “Keep going” END IF STOP END
if Statement and Logical Operations (FORTRAN 90) program Demo implicit none integer rain, windSpeed write(*,*) “Rain:” read(*,*) rain write(*,*) “Wind:” read(*,*) windSpeed if( rain >= 500 .or. windSpeed >= 10 ) then write(*,*) “Take one day off” else write(*,*) “Go to office” endif stop end
Multiple if-else if Statement program demo implicit none integer score character grade write(*,*) “Score:” read(*,*) score if ( score > 100 ) then grade = ‘?’ else if ( score >= 90 ) then grade = ‘A’ else if ( score >= 80 ) then grade = ‘B’ else end if write(*,”(‘Grade:’, A1)”) grade stop end
Nested if Statement program demo implicit none integer score character grade write(*,*) “Score:” read(*,*) score if ( score > 100 ) then grade = ‘?’ else if ( score >= 90 ) then grade = ‘A’ grade = ‘B’ end if write(*,”(‘Grade:’, A1)”) grade stop end
select case Statement program demo implicit none integer score character grade write(*,*) “Score:” read(*,*) score select case(score) case(90:100) grade = ‘A’ case default grade = ‘B’ end select write(*,”(‘Grade:’,A1)”) grade stop end
goto Statement program NOT_RECOMMENDED implicit none real height real weight write(*,*) “height:” read(*,*) height write(*,*) “weight:” read(*,*) weight if( weight > height – 100 ) goto 200 100 write(*,*) “Under control” goto 300 200 write(*,*) “Too fat!” 300 stop end
Arithmetic goto program NOT_RECOMMENDED implicit none real a, b real c data a, b /2.0, 1.0/ c = a - b if ( c ) 10, 20, 30 10 write(*,*) ‘a < b’ goto 40 20 write(*,*) ‘a = b’ 30 write(*,*) ‘a > b’ 40 Stop end
do Statement (.f90) program demo implicit none integer counter integer parameter :: lines = 10 do counter=1, lines, 1 write(*,*) “Happy New Year”, counter end do stop end
do Statement (*.for) PROGRAM demo IMPLICIT NONE INTEGER limit PARAMETER (limit=10) INTEGER counter INTEGER ans DATA ans /0/ DO 100, counter = 2, limit, 2 ans = ans + counter 100 CONTINUE WRITE(*,*) ans STOP END
do while Statement (*.f90) program demo implicit none integer, parameter :: limit = 10 integer counter integer :: ans = 0 counter = 2 do while ( counter <= limit ) ans = ans + counter counter = counter + 2 end do write(*,*) ans stop end
cycle Statement (*.f90) program elevator implicit none integer :: dest = 9 integer floor do floor = 1, dest if ( floor == 4 ) cycle write(*,*) floor end do stop end
exit Statement (*.f90) program WeightGuess implicit none real, parameter :: weight = 45.0 real, parameter :: error = 0.0001 real :: guess do while ( .true. ) write(*,*) “Weight:” read(*,*) guess if ( abs(guess – weight) < error ) exit end do write(*,*) “Correct!” stop end
One-Dimensional Array program Demo implicit none integer, parameter :: students = 5 integer student( students ) integer i do i = 1, students write(*,”(‘Number ‘, I2)”) i read(*,*) student(i) end do do while ( .true. ) write(*,*) “Query” read(*,*) i if( i <= 0 .or. i>students ) exit write(*,*) student(i) stop end
Two-Dimensional Array (1/2) program MatrixAddition implicit none integer, parameter :: row = 2 integer, parameter :: col = 2 integer matrixA(row, col) integer matrixB(row, col) integer matrixC(row, col) integer r integer c write(*,*) “Matrix A” do r = 1, row do c = 1, col write(*,”(‘A(‘,I1,’,’,I1,’)=‘)”) r, c read(*,*) matrixA(r,c) end do
Two-Dimensional Array (1/2) write(*,*) “Matrix B” do r = 1, row do c = 1, col write(*,”(‘B(‘,I1,’,’,I1,’)=‘)”) r, c read(*,*) matrixB(r,c) end do write(*,*) “Matrix A + B = “ matrixC(r,c) = MatrixB(r,c) + MatrixA(r,c) write(*,”(‘C(‘,I1,’,’,I1,’)=‘,I3)”) r, c, & matrixC(r,c) stop end
Declaration of Arrays integer a(5) integer a(0:5) integer a(-3:3) integer b(2:3, -1:3)
Setting Array Data integer a(5) data a /1, 2, 3, 4, 5/ integer i data (a(i), i=2,4) /2, 3, 4/ integer a(2,2) integer I, j data ( (a(I,j), i=1,2), j=1,2) /1, 2, 3, 4/
Column Major Storage A(1,1) A(3,1) A(2,2) A(3,2) A(1,3) A(2,3) A(3,3)
Adjustable Array (*.f90) program Demo implicit none integer students integer, allocatable :: a( integer I write(*,*) “How many students:” read(*,*) students allocate( a(students) ) do I = 1, students write(*, “(‘Number ‘, I3)”) I read(*,*) a(i) end do deallocate( a ) stop end
Subroutines program example implicit none call message() stop end subroutine message() write(*,*) “Hello.” return
Pass by Reference program example implicit none integer :: a = 1 integer :: b = 2 write(*,*) a, b call add(a) call add(b) stop end subroutine add(num) integer :: num num = num + 1 return
Functions program example implicit none real :: a = 1 real :: b = 2 real, external :: add write(*,*) add(a,b) stop end function add(a, b) real :: a, b real :: add add = a + b return
common Statement program example implicit none integer :: a, b common a, b a = 1 b = 2 call showCommon() stop end subroutine showCommon() integer :: num1, num2 common num1, num2 write(*,*) num1, num2 return
Labeled common Blocks program example implicit none integer :: a, b common /group1/ a common /group2/ b a = 1 b = 2 call showGroup1() call showGroup2() stop end subroutine showGroup1() integer :: num common /group1/ num1 write(*,*) num1 return
Block Data program example implicit none integer :: a, b common a, b integer :: c, d common /group1/ c, d write(*,*) a, b, c, d stop end block data integer a, b data a, b /1, 2/ integer c, d common c, d data c, d /3, 4/ end block data
Recursive Functions (*.f90) program example implicit none integer n integer, external :: fact write(*,*) ‘N =‘ read(*,*) n write(*,”(I2,’! = ‘,I8)” ) n, fact(n) stop end recursive integer function fact(n) result(ans) integer, intent(in) :: n if( n <= 1 ) then ans = 1 return end if ans = n * fact(n-1)
Pointers I program example implicit none integer, target :: a = 1 integer, pointer :: p p=>a write(*,*) p a = 2 p = 3 write(*,*) a stop end
Pointers II program example implicit none integer, target :: a = 1 integer, pointer :: p allocate(p) p = 1 write(*,*) p deallocate(p) p=>a stop end
Pointers III program example implicit none integer, pointer : a(:) allocate( a(5) ) a = (/ 1, 2, 3, 4, 5 /) write(*,*) a deallocate( a ) stop end
Modules (1/4) module constant implicit none real, parameter :: pi = 3.14159 real, parameter :: g = 9.81 end module module typedef type player real :: angle real :: speed real :: distance end type
Modules (2/4) module shoot use constant use typedef implicit none contains real function AngleToRad( angle ) real angle AngleToRad = angle * pi/180.0 return end subroutine GetDistance( person ) type(player) :: person real rad, Vx, time
Modules (3/4) rad = AngleToRad( person%angle ) Vx = person%speed * cos( rad ) time = 2.0 * person%speed * sin( rad )/g person % distance = Vx * time return end subroutine end module
Modules (4/4) program example use shoot implicit none integer, parameter :: players = 2 type(player) :: people(players) = (/ & player(30.0, 25.0, 0.0 ), & player(35.0, 21.0, 0.0 ) /) integer :: i do i = 1, players call GetDistance( people(i) ) write(*,”(‘Player ‘,I1,’ =‘,F8.2)”) i, & people(i)%distance end do stop end