Fortran 90 and beyond programming language

Learn from examples

1. Program to output an integer, a string, and a floating point number, initially just one method. exf1.f90 source code program exf1 ! exf1.f90 simple output of integer, string, floating point ! ! makes rest of line a comment implicit none integer :: i = 7 ! declare variable i with initial value 7 character :: ac = 'a' ! a single character character (len=13) :: msg = "sample string" real :: x = 37.95 ! 32 bit float double precision :: y = 127.34e10; ! 64 bit double precision print *, "exf1.f90" print *, "i=", i print *, "ac=", ac print *, "msg=", msg print *, "x=", x print *, "y=", y ! or you can specify a format, must have a statement number write(6,90) i 90 format('i=',i6) write(6,92) x 92 format('x=',f12.2) write(6,93) y 93 format('y=',e12.2) end program exf1 Output of execution: exf1.f90 i= 7 ac=a msg=sample string x= 37.9500008 y= 1273400000512.0000 i= 7 x= 37.95 y= 0.13E+13 2. commands to compile and execute the source code at a minimum, Windows, Linux, MacOSX. Windows for exf1.f90 exf1.exe linux: gfortran -o exf1 exf1.f90 exf1 MacOSX gcc -o exf1 exf1.f90 exf1 3. You must be able to declare variables and arrays of various types. exf3.f90 source code program exf3 ! exf3.f90 example file more complete structure, list, arrays, matrix integer, parameter :: n = 4 ! constant character (len=5) :: msg = "short" ! char for string character*4, dimension(3) :: as = (/"abcd", "wxyz", "more"/) integer, dimension(n) :: vec = (/1, 2, 4, 8/) ! initialize an array, any type integer, dimension(4,3) :: mat = & reshape( (/1,2,3,4,5,6,7,8,9,10,11,12/), shape(mat)) ! 4 columns by 3 rows integer, dimension(100) :: big ! allocate space for 100 integers double precision, dimension(3) :: av = (/1.5, 2.2, 9.1/) double precision, dimension(5) :: small ! space for 5 doubles double precision, dimension(4,3) :: matrix ! matrix 4 rows of 3 doubles double precision, dimension(0:3,0:2) :: cmat ! different subscript ranges matrix(4,3) = 1.0 ! subscripts start at 1 1,2,3,4 is 4 items normal cmat(3,2) = 2.0 ! subscripts 0,1,2,3 0,1,2 per dimension print *, "exf3.f90 running" print *, "msg=", msg print *, "as(3)=", as(3) print *, "vec(1)=", vec(1) print *, "vec(n)=", vec(n) print *, "mat(4,3) is column major, row minor" print *, "mat(1,1)=", mat(1,1) print *, "mat(1,2)=", mat(1,2) print *, "mat(1,3)=", mat(1,3) print *, "mat(4,1)=", mat(4,1) print *, "mat(4,2)=", mat(4,2) print *, "mat(4,3)=", mat(4,3) print *, "av(3)=", av(3) cmat(0,0) = 1.0 cmat(3,2) = 2.0 print *, "cmac(0,0)=", cmat(0,0) print *, "cmac(3,2)=", cmat(3,2) end program exf3 Output of execution exf3_90.out exf3.f90 running msg=short as(3)=more vec(1)= 1 vec(n)= 8 mat(4,3) is column major, row minor mat(1,1)= 1 mat(1,2)= 5 mat(1,3)= 9 mat(4,1)= 4 mat(4,2)= 8 mat(4,3)= 12 av(3)= 9.1000003814697266 cmac(0,0)= 1.0000000000000000 cmac(3,2)= 2.0000000000000000 3A. Fortran with a long history has many numerical declarations exf3a.f90 source code program exf3a ! all about numeric declarations implicit none integer, parameter :: float = selected_real_kind(6,30) integer, parameter :: double = selected_real_kind(15,300) real (kind=float) :: x = 0.5_float real*4 :: xx = 0.5 ! float real (kind=double) :: y = 0.5_double real*8 :: yy = 0.5 ! double double precision :: yyy = 0.5 integer (kind=selected_int_kind(2)) :: shortshort = 127 integer (kind=selected_int_kind(4)) :: short = 32767 integer (kind=selected_int_kind(8)) :: long = (2**30-1)+2**30 ! won't take 2**31-1 integer :: k = (2**30-1)+2**30 ! won't take 2**31-1 integer*8 :: kk = 2**30 ! won't take 2**60 kk = kk + kk kk = kk + kk kk = kk + kk kk = kk + kk kk = 4*kk ! 2**38 but not at compile time print *, "exf3a.f90 all about numeric declarations" print *, "real information" print *, "kind(x)=", kind(x), " kind(y)=", kind(y) print *, "kind(xx)=", kind(xx), " kind(yy)=", kind(yy) print *, "kind(yyy)=", kind(yyy) print *, "digits x,y", digits(x), digits(y) print *, "digits xx,yy", digits(xx), digits(yy) print *, "digits yyy", digits(yyy) print *, "epsilon x,y", epsilon(x), epsilon(y) print *, "epsilon xx,yy", epsilon(xx), epsilon(yy) print *, "epsilon yyy", epsilon(yyy) print *, "huge x,y", huge(x), huge(y) print *, "huge xx,yy", huge(xx), huge(yy) print *, "huge yyy", huge(yyy) print *, "maxexponent x,y", maxexponent(x), maxexponent(y) print *, "minexponent x,y", minexponent(x), minexponent(y) print *, "precision x,y", precision(x), precision(y) print *, "radix x,y", radix(x), radix(y) print *, "range x,y", range(x), range(y) print *, "tiny x,y", tiny(x), tiny(y) print *, "sin(x),sin(y)", sin(x), sin(y) print *, "integer information" print *, "kind(shortshort)=", kind(shortshort), shortshort print *, "kind(short)=", kind(short), short print *, "kind(long)=", kind(long), long print *, "kind(k)=", kind(k), k print *, "kind(kk)=", kind(kk), kk end program exf3a Output of execution: exf3a.f90 all about numeric declarations real information kind(x)= 4 kind(y)= 8 kind(xx)= 4 kind(yy)= 8 kind(yyy)= 8 digits x,y 24 53 digits xx,yy 24 53 digits yyy 53 epsilon x,y 1.19209290E-07 2.2204460492503131E-016 epsilon xx,yy 1.19209290E-07 2.2204460492503131E-016 epsilon yyy 2.2204460492503131E-016 huge x,y 3.40282347E+38 1.7976931348623157E+308 huge xx,yy 3.40282347E+38 1.7976931348623157E+308 huge yyy 1.7976931348623157E+308 maxexponent x,y 128 1024 minexponent x,y -125 -1021 precision x,y 6 15 radix x,y 2 2 range x,y 37 307 tiny x,y 1.17549435E-38 2.2250738585072014E-308 sin(x),sin(y) 0.479425550 0.47942553860420301 integer information kind(shortshort)= 1 127 kind(short)= 2 32767 kind(long)= 4 2147483647 kind(k)= 4 2147483647 kind(kk)= 8 68719476736 4. You need to be able to have loops, iteration statement(s) exf4.f90 source code program exf4 ! exf4.f90 loops, iteration statements print *, "exf4.f90 loops, iteration" do i = 1,4 print *, "i=", i end do do i = 2,10,2 ! even values print *, "i=", i end do do j = 5,2,-1 ! reverse print *, "j=", j end do end program exf4 Output of execution: exf4.f90 loops, iteration i= 1 i= 2 i= 3 i= 4 i= 2 i= 4 i= 6 i= 8 i= 10 j= 5 j= 4 j= 3 j= 2 5. You need if then else conditional statements exf5.f90 source code program exf5 ! exf5.f90 if, then, else conditional statements double precision :: x = 2.0 integer :: i = 3 print *, "exf5.f90 if, then, else" if( x < 3.0 ) then ! < > <= >= == != compare operations print *, "compare < > <= >= == != x=", x end if if( x > 3.0 .or. i == 3 .and. i > 2 ) then print *, "logic .or. , .and. , .not. i=", i end if if( x > 3.0 ) then print *, "x > 3.0" else if( x > 2.5 ) then print *, "x > 2.5" else print *, "x <= 3.0" end if end program exf5 Output of execution: exf5.f90 if, then, else compare < > <= >= == != x= 2.0000000000000000 logic .or. , .and. , .not. i= 3 x <= 3.0 6. You need to be able to create functions, procedures, subroutines. exf6.f90 source code ! exf6.f90 define and use functions and subroutines module functions ! inside here, usually compiled as a separate file public :: fun contains function func(t) result (r) real, intent(in) :: t ! declare parameter types real :: r ! declare result type r = t + 1.0 ! last value stored in result is returned end function func subroutine subr(A) real, dimension(:), intent (in out) :: A ! declare parameter type integer :: n n = size(A) do i = 1,n A(i) = A(i) + 1.0 end do end subroutine subr end module functions program exf6 use functions ! makes available func and subr real, dimension(1:5) :: a = (/ 1.0, 2.0, 3.0, 4.0, 5.0 /) real :: x = 1.0 real :: y print *, "exf6.f90 define and use functions and subroutines" y = func(x) print *, "y=funct(x)=", y call subr(a) print *, "call subr(a) a=", a end program exf6 Output of execution exf6.f90 define and use functions and subroutines y=funct(x)= 2.00000000 call subr(a) a= 2.00000000 3.00000000 4.00000000 5.00000000 6.00000000 7. You need to be able to read and write files in various formats. exf7.f90 source code program exf7 ! exf7.f90 read and write files implicit none ! must define all integers integer :: iostat, stat, dat, i integer :: n = 4 character(len=10) :: aformat = "(a20/)" ! format a for characters, f7.3 or e15 for real, i for integer, 1x for space print *, "exf7.f90 read and write files" write(6,fmt="(a20)") "unit 6 is std output" ! read(5,fmt="(i5)", number) ! from std input write(6, aformat) "unit 5 is std input " print *, "writing myfile.dat, then read" ! status="new", status="replace", status="write, options open(unit=11, file="myfile.dat", & form="formatted", action="readwrite", iostat=iostat) print *, "iostat=", iostat if(iostat /= 0) then print *, "can not open file: myfile.dat for writing" stop end if write(unit=11, fmt="(i1)") n print *, "writing ", n do i=1,n write(unit=11, fmt="(i2)") n*10 print *, "writing ", i*10 end do close(unit=11, status="keep") print *, "reading myfile.dat, that was just written" open(unit=11, file="myfile.dat", action="read", iostat=iostat) if(iostat /= 0) then print *, "can not open file: myfile.dat for reading" stop end if do read(unit=11, fmt="(i1)", iostat=iostat) n if(iostat < 0) exit ! end of file if(iostat > 0) cycle ! bad data print *, "n=", n do i=1,n read(unit=11, fmt="(i2)", iostat=iostat) dat if(iostat /= 0) print *, "may have bad data" if(iostat == 0) print *, "dat=", dat end do end do close(unit=11, status="keep") print *, "exf7.f90 finished" end program exf7 Output of execution: exf7.f90 read and write files unit 6 is std output unit 5 is std input writing myfile.dat, then read iostat= 0 writing 4 writing 10 writing 20 writing 30 writing 40 reading myfile.dat, that was just written n= 4 dat= 40 dat= 40 dat= 40 dat= 40 exf7.f90 finished 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, modules, operating system commands, header files, etc. compiled gfortran -o exf8 functions.f90 exf8.f90 # functions.f90 first functions.f90 source code of module ! functions.f90 define and use functions and subroutines in module ! used by exf6.f90 module functions ! in a separate file functions.f90 public :: fun, subr ! can have some private contains function func(t) result (r) real, intent(in) :: t ! declare parameter types real :: r ! declare result type r = t + 1.0 ! last value stored in result is returned end function func subroutine subr(A) real, dimension(:), intent (in out) :: A ! declare parameter type integer :: n n = size(A) do i = 1,n A(i) = A(i) + 1.0 end do end subroutine subr end module functions exf8.f90 source code program exf8 ! exf8.f90 use file with module with functions and subroutines use functions ! makes available func and subr in module functions.f90 real, dimension(1:4) :: a = (/ 1.0, 2.0, 3.0, 4.0 /) real :: x = 1.0 real :: y print *, "exf8.f90 define and use functions and subroutines" y = func(x) print *, "y=funct(x)=", y call subr(a) print *, "call subr(a) a=", a end program exf8 Output of execution: exf8.f90 define and use functions and subroutines y=funct(x)= 2.00000000 call subr(a) a= 2.00000000 3.00000000 4.00000000 5.00000000 Now, you are ready for a more complete language summary

Last updated 1/14/2018