[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ next ]
Fortran 90 Lessons for Computational Chemistry
Fortran Arrays
The main aims of this session are the following
present one dimension arrays as Fortran data structures.
present the different ways of defining an array.
present the DO loop syntax and the implicit DO and their use with matrices.
explore dynamic arrays in Fortran 90
present multidimensional arrays as Fortran data structures.
Basic Definitions:
rank: number of indices necessary to indicate unambiguously an array element.
bounds: max and min values of the indices labelling array elements in each dimension.
extent: number of elements in an array dimension.
size: total number of a matrix.
conformal: two arrays are conformal if both have the same rank and extent.
The following points should be emphasized:
one dimensional array (vector) definition making use of the DO control structure (see excode_3_1.f90, Section 3.3.1 and exercise 2_1)
use of the PARAMETER declaration for the definition of array bounds in static array declaration.
initialize before use. Beware of surprises. The initialization to a common constant value is extremely simple: vec = valor. A possible alternative is the use of array constructors. In the following example, in order to define an integer array with six elements called vec_int three possible and equivalent options are given
do i = 0, 5
vec_int(i) = 2*i
enddo
vec_int = (/(2*i, i = 0, 5)/)
vec_int = (/0,2,4,6,8,10/)
Last two options involve array constructors and can be carried out when the array is declared[4]
use of the ALLOCATABLE declaration and the use of the ALLOCATE function, as it is shown in example excode_3_2.f90, Section 3.3.2. The ALLOCATE option STAT = var allows to chek if the array has been properly defined. See example in program excode_9_3.f90, Section 9.3.3.
implicit DO and multidimensional arrays. See example excode_3_3.f90, Section 3.3.3.
most general form of the DO control structure and possibility of introducing zero or negative array indeces. See example excode_3_4.f90, Section 3.3.4.
combination of bash redirectioning with Fortran
programs. Necessary for exercise 2, it is explained in More on Arrays, Chapter 4.
PROGRAM ex_3_1
!
! VARIABLES DEFINITION
IMPLICIT NONE
REAL :: Total=0.0, Average=0.0
INTEGER, PARAMETER :: Week=7
REAL , DIMENSION(1:semana) :: Lab_Hours
INTEGER :: Day
!
PRINT *,' Labor Time (hours per day during a week):'
DO Day= 1, Week
READ *, Lab_Hours(Day)
ENDDO
!
DO Day = 1, Week
Total = Total + Lab_Hours(Day)
ENDDO
Average = Total / Week
!
PRINT *,' Average Weekly Workload: '
PRINT *, Average, ' hours'
END PROGRAM ex_3_1
PROGRAM ex_3_2
!
! VARIABLE DEFINITION
IMPLICIT NONE
REAL :: Total=0.0, Average=0.0
REAL , DIMENSION(:), ALLOCATABLE :: Lab_Hours
INTEGER :: Day, Number_Days
!
PRINT *,' Number of workdays:'
READ *, Number_Days
!
ALLOCATE(Lab_Hours(1:Number_Days))
!
PRINT *,' Daily hours of work in ', Number_Days, ' days.'
DO Day = 1, Number_Days
READ *, Lab_Hours(Day)
ENDDO
!
DO Day=1, Number_Days
Total = Total + Lab_Hours(Day)
ENDDO
Average = Total / Number_Days
!
PRINT *,' Average daily workhours in ',Number_Days, ' days : '
PRINT *, Average, ' hours'
!
END PROGRAM ex_3_2
PROGRAM ATTEND_CONTROL
IMPLICIT NONE
INTEGER , PARAMETER :: N_students = 3
INTEGER , PARAMETER :: N_courses = 3
INTEGER , PARAMETER :: N_lab = 3
INTEGER :: student, course, lab
CHARACTER*2 , DIMENSION(1:N_lab,1:N_courses,1:N_lab) :: attend = 'NO'
DO student = 1, N_students
DO course = 1,N_courses
READ *,(attend(lab,course,student),lab = 1, N_lab)
ENDDO
ENDDO
PRINT *,' Lab attendance : '
DO student=1, N_students
PRINT *,' Student = ', student
DO course = 1,N_courses
PRINT *,' Course = ', course, ' : ', (attend(lab,course,student),lab=1,N_lab)
ENDDO
ENDDO
END PROGRAM ATTEND_CONTROL
PROGRAM ex_3_4
IMPLICIT NONE
REAL , DIMENSION(-180:180) :: Time=0
INTEGER :: Degree, Strip
REAL :: Value
!
DO Degree=-165,165,15
Value=Degree/15
DO Strip=-7,7
Time(Degree+Strip)=Value
ENDDO
ENDDO
!
DO Strip=0,7
Time(-180 + Strip) = -180/15
Time( 180 - Strip) = 180/15
ENDDO
!
DO Degree=-180,180
PRINT *,Degree,' ',Time(Degree), 12 + Time(Degree)
END DO
END PROGRAM ex_3_4
[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ next ]
Fortran 90 Lessons for Computational Chemistry
mailto:francisco.perez@dfaie.uhu.es