[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ next ]

# `Fortran 90` Lessons for Computational Chemistry Chapter 3 - Introduction to `Fortran` Arrays

## 3.1 Objectivos

The main aims of this session are the following

1. present one dimension arrays as `Fortran` data structures.

1. present the different ways of defining an array.

1. present the DO loop syntax and the implicit DO and their use with matrices.

1. explore dynamic arrays in `Fortran 90`

1. present multidimensional arrays as `Fortran` data structures.

## 3.2 Main items.

Basic Definitions:

1. rank: number of indices necessary to indicate unambiguously an array element.

1. bounds: max and min values of the indices labelling array elements in each dimension.

1. extent: number of elements in an array dimension.

1. size: total number of a matrix.

1. conformal: two arrays are conformal if both have the same rank and extent.

The following points should be emphasized:

• 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]

• 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.

## 3.3 Example Codes.

### 3.3.1 excode_3_1.f90

```     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
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
```

### 3.3.2 excode_3_2.f90

```     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:'
!
ALLOCATE(Lab_Hours(1:Number_Days))
!
PRINT *,' Daily hours of work in ', Number_Days, ' days.'
DO Day = 1, Number_Days
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
```

### 3.3.3 excode_3_3.f90

```     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
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
```

### 3.3.4 excode_3_4.f90

```     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

0.0

Curro Pérez-Bernal `mailto:francisco.perez@dfaie.uhu.es`