Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
implicit none
integer, intent (in) :: i
logical, intent (in) :: control
integer, save :: j = 0
integer :: k
j = j + i
if ( control ) k = 0
k = k + i
write (*, *) 'i, j, k=', i, j, k
end subroutine asub
end module subs
program test_saves
use subs
implicit none
call asub ( 3, .TRUE. )
call asub ( 4, .FALSE. )
end program test_saves
```
Local variable k of the subroutine is intentionally misused -- in this program it is initialized in the first call since control is TRUE, but on the second call control is FALSE, so k is not redefined. But without the save attribute k is undefined, so the using its value is illegal.
```fortran
i, j, k= 3 3 3
i, j, k= 4 7 7
```
Compiling the program with ifort and aggressive optimization options, k lost its value:
```fortran
i, j, k= 3 3 3
i, j, k= 4 7 4
```
-->
---
# `internal` subprogams
```fortran
program main
implicit none
integer N
real X(20)
...
write(*,*), 'Processing x...', process()
...
contains
logical function process()
! in this function N and X can be accessed directly (scope of main)
! Please not that this method is not recommended:
! it would be better to pass X as an argument of process
implicit none
if (sum(x) > 5.) then
process = .FALSE.
else
process = .TRUE.
endif
end function process
end program
```
<!-- _footer: "" -->
---
# `external` subprogams
- `external` subprogams are defined in a separate program unit
- to use them in another program unit, refer with the `EXTERNAL` statement
- compiled separately and linked
**!!! DO NOT USE THEM**: modules are much easier and more robust :exclamation:
They are only needed when subprogams are written with different programming language or when using external libraries (such as BLAS)
> It's **highly** recommended to construct `INTERFACE` blocks for any external subprogams used
---
# `interface` statement
```fortran
SUBROUTINE nag_rand(table)
INTERFACE
SUBROUTINE g05faf(a,b,n,x)
REAL, INTENT(IN) :: a, b
INTEGER, INTENT(IN) :: n
REAL, INTENT(OUT) :: x(n)
END SUBROUTINE g05faf
END INTERFACE
!
REAL, DIMENSION(:), INTENT(OUT) :: table
!
call g05faf(-1.0,-1.0, SIZE(table), table)
END SUBROUTINE nag_rand
```
<!-- _footer: "" -->
---
# Fortran Compiler and libraries
Examples:
```bash
module load netCDF-Fortran/4.5.3-gompi-2021b
gfortran -ffree-line-length-none \
-o OceanGrideChange.exe 07_OceanGrideChange.f90 \
-I${EBROOTNETCDFMINFORTRAN}/include -L${EBROOTNETCDFMINFORTRAN}/lib -lnetcdff
```
```bash
module load netCDF-Fortran/4.5.3-iimpi-2021b
ifort -O3 \
-o OceanGrideChange.exe 07_OceanGrideChange.f90 \
-I${EBROOTNETCDFMINFORTRAN}/include -L${EBROOTNETCDFMINFORTRAN}/lib -lnetcdff
```
> Fortran 90 source code [09_OceanGrideChange.f90](https://forge.uclouvain.be/barriat/learning-fortran/-/blob/master/src/09_OceanGrideChange.f90) with the input file [09_input.nc](https://forge.uclouvain.be/barriat/learning-fortran/-/blob/master/src/09_input.nc)
---
# Conclusions
- Fortran in all its standard versions and vendor-specific dialects is a rich but confusing language
- Fortran is a modern language that continues to evolve
- Fortran is still ideally suited for numerical computations in engineering and science
- most new language features have been added since F95
- "High Performance Fortran" includes capabilities designed for parallel processing