fortran notes
Version
- Fortran77
- F90/F95
Syntax
- not case sensitive
- 6 spaces; max 72 width
c
for comment&
or*
at 6th space for continue-
program and subroutine
program my_prog end subroutine my_sub end
Declare
REAL
INTEGER
COMPLEX
DOUBLE PRECISION: real*8
DOUBLE COMPLEX
CHARACTER
IMPLICIT NONE : add to the beginning of each sub(program)
w = dble(x) * dble(y)
CONSTANTS
1.0
2.0E6
.TRUE.
.FALSE.
INPUT/OUTPUT
READ(*,*) NAME
WRITE(*,*) 'THE HELLO'
WRITE(*,*) 'NAME IS ', NAME
CONDITION
if (condition) print *, ' '
if (x .lt. 0) then
statements
else if (logical) then
statements2
else
statements3
end if
LOOP
Do i=1, 10, 2
blabla
end do
Do while
statements
end do
use exit
to exit loop
MATH
**: exponential
DOT_PRODUCT(vecA, vecB)
MATMUL(matA, matB)
COMPARE
.GT.
.GE.
.EQ.
.LE.
.LT.
.NE.
If (A .LT. 0.0) A = (-1)*A
If () THEN
JJJ
ELSE
JJJ
END IF
.AND.
.OR.
.NOT.
FILE
OPEN (UNIT=10,FILE='MY_DATA.DAT') # other than 5,6
READ(10, *) A
write(10, *) B
close(10)
PROGRAM
program name
declaration
statements
stop
end
Compile
gfortran(f77) test.f -o test.out
./test.out
FUNCTION
- function has a type
- return a value with same name
- called by name and parameters
- can use
return
like ago to
statement, and there can be more than onereturn
end
at the end- call by value
-
can be after the
write
statementreal function r(m,t) integer m real t r = 0.1*t * (m**2 + 14*m + 46) if (r .LT. 0) r = 0.0 return end type function name (list-of-variables) declarations statements return end
-
can also return array value reference
function polynomialMult(npts,x,y) integer npts double precision x(npts), results(npts + 1), y(npts,npts) ! Change the next line to whatever you want double precision, dimension(npts) :: polynomialMult polynomialMult = x(1:npts) + 1 end function
- Passing the sizes of arrays into subroutines is very FORTRAN77 and almost always unnecessary now.
SUBROUTINE
- can return more than 1 value (array)
- no type
- should not be declared in the calling program unit
- called by
call
- call by reference
- must be a stand alone statement
Subroutine has RETURN
subroutine my_sub(a, b)
return
end
call mysub
Progress Bar
Revised from http://thelazycatholic.wordpress.com/2010/08/19/progress-bar-in-fortran/
subroutine progress(j, n)
implicit none
integer(kind=4) :: j,k,n
character(len=18) :: bar="\r???% | |"
! updates the fraction of calculation done
write(unit=bar(2:4),fmt="(i3)") 100*j/n
do k = 1, j*10/n
bar(7+k:7+k)="*"
enddo
! print the progress bar.
write(*,'(a)',advance='no') bar
return
end
then call from main function:
call progress(iter, niter)
Matrix
A(1, :) for 1st row
stop
stop 0 : to stop and return message
call exit(1)
Troublesome
- need to declare sub function in function or subroutine
Reference
Published
15 January 2013
Modified
4 January 2014