Exemple de comment utiliser seulement certaines variables d'un module en Fortran 90 (voir aussi la discussion sur stackoverflow sur le sujet)
En utilisant: Only
Module fortran (module_test.f90):
module variablesreal, parameter :: pi = 3.1415927real, parameter :: golden_ratio = 1.61803real :: radiusend module variables
Programme principal (test_module.f90):
program code_testuse variables, ONLY : piimplicit nonewrite(6,*) 'pi', pi!write(6,*) 'golden_ratio', golden_ratioend program code_test
En spécifiant ONLY: pi cela signifie que toutes les autres variables du module sont ignorées par le programme principal.
Compiler le code
gfortran module_test.f90 test_module.f90 -o test_module
lancer le code
test_module
donne ici
pi 3.14159274
par contre si on ajoute
write(6,*) 'pi', piwrite(6,*) 'golden_ratio', golden_ratio
on a alors le message d'erreur durant la compilation:
write(6,*) 'golden_ratio', golden_ratio1Error: Symbol 'golden_ratio' at (1) has no IMPLICIT type
En utilisant Dynamic arrays
Une autre approche que j'utilise parfois avec un module comprenant des tableaux de grandes dimensions.
Dans cet exemple on veut uniquement les tableaux A et C
Module:
module variablesimplicit none!----------------------------------------------------------------------------------------!! Dynamic Allocation of Arraysinteger, dimension(:), allocatable :: Ainteger, dimension(:), allocatable :: Binteger, dimension(:), allocatable :: C!----------------------------------------------------------------------------------------!CONTAINSsubroutine get_array(my_list, dim)implicit noneinteger, intent(in) :: dimcharacter*1, dimension(dim), intent(in) :: my_list!----------------------------------------------------------------------------------------!! Matrix Aif ( ANY( my_list=='A' ) ) thenallocate( A(6) )A = 7end if!----------------------------------------------------------------------------------------!! Matrix Bif ( ANY( my_list=='B' ) ) thenallocate( B(3) )B(1) = 3B(2) = 8B(3) = 1end if!----------------------------------------------------------------------------------------!! Matrix Cif ( ANY( my_list=='C' ) ) thenallocate( C(1) )C = 2end if!----------------------------------------------------------------------------------------!end subroutine get_array!----------------------------------------------------------------------------------------!end module variables
Le programme principal
program code_testuse variablesimplicit noneinteger, parameter :: dim = 2character*1, dimension(dim) :: my_listdata my_list /'A','C'/call get_array(my_list, dim)if( ANY( my_list=='A' ) ) write(6,*) 'A', Aif( ANY( my_list=='B' ) ) write(6,*) 'B', Bif( ANY( my_list=='C' ) ) write(6,*) 'C', Cif( ANY( my_list=='A' ) ) deallocate(A)if( ANY( my_list=='B' ) ) deallocate(B)if( ANY( my_list=='C' ) ) deallocate(C)end program code_test
donne
A 7 7 7 7 7 7C 2
Références
| Liens | Site |
|---|---|
| Modules | stanford.edu |
| How do you USE Fortran 90 module data | stackoverflow |
| Fortran:How to check if array contains value? | stackoverflow |
| Compile Programs with Modules | cs.mtu.edu |
