Skip to content
Extraits de code Groupes Projets
Valider d8c3b25f rédigé par Pierre-Yves Barriat's avatar Pierre-Yves Barriat
Parcourir les fichiers

Upgrade examples code

parent fa7e16e5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de avec 72 ajouts et 2317 suppressions
...@@ -84,7 +84,9 @@ no dynamic memory allocation, old & obsolete constructs, “spaghetti” code, e ...@@ -84,7 +84,9 @@ no dynamic memory allocation, old & obsolete constructs, “spaghetti” code, e
FORTRAN is a compiled language (like C) so the source code (what you write) must be converted into machine code before it can be executed (e.g. Make command) FORTRAN is a compiled language (like C) so the source code (what you write) must be converted into machine code before it can be executed (e.g. Make command)
![h:400](assets/build_fortran.png) ![h:350](assets/build_fortran.png)
> Fortran 77 source code [hello_world.f](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/00_hello_world.f)
--- ---
...@@ -160,25 +162,6 @@ Arrays of any type must be declared: ...@@ -160,25 +162,6 @@ Arrays of any type must be declared:
--- ---
# Data Type Declarations
FORTRAN >90 allows user defined types
```fortran
TYPE my_variable
character(30) :: name
integer :: id
real(8) :: value
integer, dimension(3,3) :: dimIndex
END TYPE variable
type(my_variable) var
var%name = "salinity"
var%id = 1
```
---
# Implicit vs Explicit Declarations # Implicit vs Explicit Declarations
By default, an implicit type is assumed depending on the first letter of the variable name: By default, an implicit type is assumed depending on the first letter of the variable name:
...@@ -259,9 +242,10 @@ Numeric expressions are up-cast to the highest data type in the expression accor ...@@ -259,9 +242,10 @@ Numeric expressions are up-cast to the highest data type in the expression accor
and smaller byte size *(low)* to larger byte size *(high)* and smaller byte size *(low)* to larger byte size *(high)*
## Example: ## Examples:
> fortran 77 source code [arith.f](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/01_arith.f) > Fortran 77 source code [arith.f](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/01_arith.f)
> Fortran 77 source code [sphere.f](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/02_sphere.f)
--- ---
...@@ -507,6 +491,8 @@ Today, most I/O is to and from a file: it requires more extensive I/O capabiliti ...@@ -507,6 +491,8 @@ Today, most I/O is to and from a file: it requires more extensive I/O capabiliti
- data reading & writing with `READ` & `WRITE` - data reading & writing with `READ` & `WRITE`
- can use **unformatted** `READ` & `WRITE` if no human readable data are involved (much faster access, smaller files) - can use **unformatted** `READ` & `WRITE` if no human readable data are involved (much faster access, smaller files)
> Fortran 77 source code [plot.f](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/03_plot.f)
--- ---
# `OPEN` & `CLOSE` example # `OPEN` & `CLOSE` example
...@@ -617,6 +603,10 @@ It is possible to pre-define the structure of input and output data using `NAMEL ...@@ -617,6 +603,10 @@ It is possible to pre-define the structure of input and output data using `NAMEL
> This is not part of standard F77 but it is included in >F90 > This is not part of standard F77 but it is included in >F90
---
# `NAMELIST` - cont'd
On input, the `NAMELIST` data must be structured as follows: On input, the `NAMELIST` data must be structured as follows:
```fortran ```fortran
...@@ -628,7 +618,8 @@ On input, the `NAMELIST` data must be structured as follows: ...@@ -628,7 +618,8 @@ On input, the `NAMELIST` data must be structured as follows:
/ /
``` ```
<!-- _footer: "" --> > Fortran 90 source code [namelist.f90](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/04_namelist.f90)
> Namelist file [namelist.def](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/04_namelist.def)
--- ---
...@@ -736,8 +727,6 @@ AV = WEIGHT*AVG3(A1,F2,B2) ...@@ -736,8 +727,6 @@ AV = WEIGHT*AVG3(A1,F2,B2)
Subroutine is invoked using the `CALL` statement Subroutine is invoked using the `CALL` statement
`SUBROUTINE` example:
```fortran ```fortran
SUBROUTINE AVG3S(A,B,C,AVERAGE) SUBROUTINE AVG3S(A,B,C,AVERAGE)
AVERAGE=(A+B+C)/3 AVERAGE=(A+B+C)/3
...@@ -752,7 +741,9 @@ CALL AVG3S(A1,F2,B2,AVR) ...@@ -752,7 +741,9 @@ CALL AVG3S(A1,F2,B2,AVR)
RESULT = WEIGHT*AVR RESULT = WEIGHT*AVR
``` ```
> any returned values must be returned through argument list Any returned values must be returned through argument list
> Fortran 90 source code [newton.f90](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/05_newton.f90)
--- ---
...@@ -899,6 +890,8 @@ The `COMMON` statement allows variables to have a more extensive scope than othe ...@@ -899,6 +890,8 @@ The `COMMON` statement allows variables to have a more extensive scope than othe
With > F90, it's better to use the `MODULE` subprogram instead of the `COMMON` statement With > F90, it's better to use the `MODULE` subprogram instead of the `COMMON` statement
> Fortran 77 source code [common.f](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/06_common.f) - Fortran 90 source code [module.f90](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/06_module.f90)
--- ---
# Modular programming (>F90) # Modular programming (>F90)
...@@ -915,6 +908,25 @@ The principle is that making significant parts of the code independent, replacea ...@@ -915,6 +908,25 @@ The principle is that making significant parts of the code independent, replacea
--- ---
# Data Type Declarations
FORTRAN >90 allows user derived types
```fortran
TYPE my_variable
character(30) :: name
integer :: id
real(8) :: value
integer, dimension(3,3) :: dimIndex
END TYPE variable
type(my_variable) var
var%name = "salinity"
var%id = 1
```
---
# Subprograms type # Subprograms type
`MODULE` are subprograms that allow modular coding and data encapsulation `MODULE` are subprograms that allow modular coding and data encapsulation
...@@ -1026,6 +1038,28 @@ END SUBROUTINE nag_rand ...@@ -1026,6 +1038,28 @@ END SUBROUTINE nag_rand
--- ---
# 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 [OceanGrideChange.f90](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/07_OceanGrideChange.f90) with the input file [input.nc](https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/src/master/src/07_input.nc)
---
# Conclusions # Conclusions
- Fortran in all its standard versions and vendor-specific dialects is a rich but confusing language - Fortran in all its standard versions and vendor-specific dialects is a rich but confusing language
......
Ce diff est replié.
Ce diff est replié.
Aucun aperçu pour ce type de fichier
assets/build_fortran.png

98,4 ko

assets/f77_format.png

70,3 ko

assets/fortran_logo.png

30,3 ko

! Ce programme affiche "Bonjour, le monde!" C Display "Hello world!"
C
program hello_world program hello_world
implicit none ! important implicit none
print *, "Bonjour, le monde!" print *, "Hello world!"
end program hello_world end program hello_world
...@@ -6,14 +6,15 @@ ...@@ -6,14 +6,15 @@
WRITE(*,*) 'Enter the value for the radius of a sphere.' WRITE(*,*) 'Enter the value for the radius of a sphere.'
READ(*,*) radius READ(*,*) radius
ccccc PI value ccccc PI & radius values
pi = radius =
pi =
ccccc PI value ccccc PI value
WRITE(*,*) 'The value of pi is ', pi WRITE(*,*) 'The value of pi is ', pi
ccccc Air & volume ccccc Air & volume
area = area =
volume = volume =
ccccc Air & volume ccccc Air & volume
WRITE(*,*) 'For a radius ', radius WRITE(*,*) 'For a radius ', radius
......
Fichier déplacé
Fichier déplacé
program newton
implicit none
c Use a Newton iteration to solve a polynomial equation
c
c x - current approximation to the solution
c f - polynomial function
c df - derivative of f with respect to x
c xo - previous guess for solution
c eps - convergence criterion
c dx - change in solution approximation
c it - number of iterations
c itmax - maximum number of iterations
real
integer
c Now start executable fortran statements
x=
do while ()
x=
end do
end
c ******************************************************************************************
subroutine derivate(x,f,df)
c Evaluate the function f(x)=x**3+x-10
c also return the derivative of the function
implicit none
real
return
end
...@@ -2,7 +2,7 @@ program newton ...@@ -2,7 +2,7 @@ program newton
implicit none implicit none
! Use a Newton iteration to solve the equation ! Use a Newton iteration to solve the equation --> x**3+x-10
! !
! x - current approximation to the solution ! x - current approximation to the solution
! f(x) - polynomial function ! f(x) - polynomial function
......
...@@ -5,7 +5,7 @@ program plot ...@@ -5,7 +5,7 @@ program plot
real(4) :: fx, x real(4) :: fx, x
integer :: i integer :: i
open (112,file='04_gnuxy') open (112,file='05_gnuxy.gpl')
write(112,*) 'set grid' write(112,*) 'set grid'
write(112,*) 'set xzeroaxis' write(112,*) 'set xzeroaxis'
...@@ -34,7 +34,7 @@ program plot ...@@ -34,7 +34,7 @@ program plot
! Generate x-y pairs for the graph ! Generate x-y pairs for the graph
open (112,file='04_dataxy_1') open (112,file='05_dataxy.dat')
do i=-40,40 do i=-40,40
x = .1*i x = .1*i
fx = x**3+x-10.0 fx = x**3+x-10.0
...@@ -44,7 +44,7 @@ program plot ...@@ -44,7 +44,7 @@ program plot
print *, ' Hit the Return (Enter) key to continue' print *, ' Hit the Return (Enter) key to continue'
call system ('gnuplot 04_gnuxy') call system ('gnuplot 05_gnuxy.gpl')
stop stop
end end
Fichier déplacé
Fichier déplacé
Fichier déplacé
Fichier déplacé
ifort -o OceanGrideChange.exe 8_OceanGrideChange.f90 -I${EBROOTNETCDFMINFORTRAN}/include -L${EBROOTNETCDFMINFORTRAN}/lib -lnetcdff
gfortran -ffree-line-length-none -o OceanGrideChange.exe 8_OceanGrideChange.f90 -I${EBROOTNETCDFMINFORTRAN}/include -L${EBROOTNETCDFMINFORTRAN}/lib -lnetcdff
Fichier déplacé
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter