fortranref.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Fortran Tutorial:
  2. https://fortran-lang.org/en/learn/os_setup/
  3. Fortran 90/95 reference:
  4. https://icl.utk.edu/~mgates3/docs/fortran.html#Loops
  5. Multiple Precision Computation: http://dmsmith.lmu.build/
  6. Fortran Package Manager:
  7. https://fpm.fortran-lang.org/index.html
  8. Fortran Package Manager and OpenMP:
  9. https://www.openmp.org/blog/fortran-package-manager-and-openmp/
  10. Using MPI with Fortran:
  11. https://curc.readthedocs.io/en/latest/programming/MPI-Fortran.html
  12. Fortran terminal I/O, the ISO standard way:
  13. https://www.scivision.dev/fortran-terminal-io
  14. print vs write Fortran statements:
  15. https://www.scivision.dev/print-vs-write-fortran/
  16. ! built-in data types
  17. * integer - for data that represent whole numbers, positive or negative
  18. * real - for floating-point data (not a whole number)
  19. * complex - pair consisting of a real part and an imaginary part
  20. * character - for text data
  21. * logical - for data that represent boolean (true/false) values
  22. ! variable declaration
  23. integer :: n
  24. real :: pi
  25. complex :: freq
  26. character :: char
  27. logical :: option
  28. ! variable assignment
  29. n = 100
  30. pi = 3.1415927
  31. freq = (1.0, -0.5)
  32. char = 'A'
  33. option = .true.
  34. ! built-in numeric type kinds in iso_fortran_env
  35. Type kind Type Size(bytes) C-equivalent
  36. ---------------------------------------------------------------------------------------
  37. int8 integer 1 None
  38. int16 integer 2 short
  39. int32 integer 4 int
  40. int64 integer 8 long
  41. real32 real, complex 4 float
  42. real64 real, complex 8 double
  43. real128 real, complex 16 long double
  44. ! operators
  45. Operator Description Examples
  46. ---------------------------------------------------------------------------------------
  47. ** exponent a**b
  48. * multiply a*b
  49. / divide a/b
  50. + add a + b
  51. - subtract a - b
  52. // string concatenation "foo" // "bar"
  53. < .lt. less than a < b
  54. <= .le. less than or equals a <= b
  55. > .gt. greater than a > b
  56. >= .ge. greater than or equals a >= b
  57. == .eq. equals a == b
  58. /= .ne. not equals a /= b
  59. .not. logical not (unary) .not. a
  60. .and. logical and a .and. b
  61. .or. logical or a .or. b
  62. .eqv. logical = equals a .eqv. b
  63. .neqv. xor, logical /= not equals a .neqv. b
  64. ! pi value
  65. pi = acos(-1.0) /* better/faster than pi = 4*atan(1.0) */
  66. ! function that returns a sum of two integers
  67. function sum(a, b)
  68. integer, intent(in) :: a, b
  69. integer :: sum
  70. sum = a + b
  71. end function sum
  72. ! specifying the data type of the function result in the function statement
  73. integer function sum(a, b)
  74. integer, intent(in) :: a, b
  75. sum = a + b
  76. end function sum
  77. ! specifying the function result as different from the function name
  78. integer function sum(a, b) result(res)
  79. integer, intent(in) :: a, b
  80. res = a + b
  81. end function sum
  82. ! using mpi with fortran [curc.readthedocs.io/en/latest/programming/MPI-Fortran.html]
  83. $ mpif90 program.f90 -o program /* gnu fortran compiler */
  84. $ mpiexec -n 4 ./program
  85. ! initializing an array
  86. integer, allocatable :: a(:)
  87. integer :: i
  88. a = [(i, i = 1, 100)] /* elements will range from 1 to 100 */
  89. ! initializing a real array with sines from 0 to 2pi, with 1000 steps
  90. real, allocatable :: a(:)
  91. integer :: i
  92. real, parameter :: pi = 3.14159256
  93. a = [(sin(2*pi*i/1000.), i = 0, 1000)] /* an array with sines from 0 to 2pi*/
  94. ! initializing an array of a thousand zeros
  95. integer, allocatable :: a(:)
  96. integer :: i
  97. a = [(0, i = 1, 1000)]
  98. ! ternary operator
  99. if-else statement:
  100. if (a > b) then
  101. x = a
  102. else
  103. x = b
  104. endif
  105. ternary statement:
  106. x = merge(a, b, a > b) /* variable = merge(value if true, value if false, condition) */
  107. ! disassemble fortran executables
  108. $ gfortran program.f90 -o program
  109. $ objdump --disassemle program > program.s
  110. ! descriptor symbols
  111. The common descriptor symbols are
  112. * 'd' - number of digits to the right of decimal place of a real
  113. * 'm' - minimum number of 'digits' to be displayed
  114. * 'n' - number of spaces to skip
  115. * 'r' - Repeat Count no. of times a descriptor is to be repeated
  116. * 'w' - Field Width number of characters wide to use for date
  117. Integer: rIw.m
  118. print "(3I6)", i, j, k
  119. which would print each of the 'Integers' i, j and k in fields of width 6 characters
  120. Real: rFw.d
  121. print "(F12.3)", pi
  122. which would print the constant 'pi' in a field of width 12 characters with 3 decimal
  123. places
  124. Real: rEw.d
  125. print "(E10.3)", 123456.0
  126. which gives '0.123E+06'
  127. Real: rESw.d
  128. print "(ES10.3)", 123456.0
  129. which gives '1.235E+05'
  130. Character: rAw
  131. print "(A10)", str
  132. which would print out the character string 'str' in a field width of 10 characters
  133. Space: nX ('n' is the number of desired spaces)
  134. print "(5X, A10)", str
  135. which would print out 5 blank spaces then the character string 'str' in a field width
  136. of 10 characters
  137. Newline: /
  138. print "(/,5X, A10)", str
  139. which would print out a blank line then 5 blank spaces then the character string 'str'
  140. in a field width of 10 characters
  141. Note: descriptor statements are separated with commas in the format string