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


Fortran 90 Lessons for Computational Chemistry
Chapter 6 - INPUT/OUTPUT (I)


6.1 Objectivos

The main aims of this lesson are the following:

  1. present how to make use of the standard bash redirection for reading and writing data in Fortran.

  1. present the FORMAT statement, as well as its differents descriptors and its use with the commands PRINT and WRITE.

  1. get a basic knowledge about file handling in Fortran with the commands OPEN, CLOSE, and WRITE.


6.2 Main Items.

The combination of fortran descriptors and different kinds of loop in a code can be found in the example excode_6_7.f90, Section 6.3.7. This program reads a data file (a template of this fila can be found under the program, and can be saved removing the trailing ! symbols). When the program opens the datafile with OPEN it uses the STATUS = 'OLD' and ACTION='READ' options. It reads the file, skipping some files making use of a REPEAT UNTIL loop, until it arrives to a line that provides the number of data pairs in the file[7]. Knowing the number of data pairs the appropriate matrices are allocated and the points are read and saved into vectors data_X and data_Y, and computes the maximum (minimum) value of data_X (data_Y) making use of the intrinsic functions MAXVAL and MINVAL (see Objectives, Section 8.1).


6.3 Example Codes


6.3.1 excode_6_1.f90

     PROGRAM ex_6_1
       !
       IMPLICIT NONE
       !
       ! Variables
       INTEGER :: i, big=10
       !
       DO i=1,20
          PRINT 100, i, big
          big=big*10
       END DO
       !
       ! Format Statements
     100 FORMAT(1X, '10 to the ',I3,2X,'=',2X,I12)
       !
     END PROGRAM ex_6_1

6.3.2 excode_6_2.f90

     PROGRAM ex_6_2
       !
       IMPLICIT NONE
       !
       INTEGER, PARAMETER :: Long=SELECTED_INT_KIND(16) ! 64 bits integer
       INTEGER :: i
       INTEGER (KIND=Long) :: big=10 
       !
       DO i=1,18
          !
          PRINT 100, i, big
     100  FORMAT(1X, '10 to the ', I3, 2X, '=', 2X, I16)
          !
          big=big*10
          !
       END DO
       !
     END PROGRAM ex_6_2

6.3.3 excode_6_3.f90

     PROGRAM ex_6_3
       ! Program to produce numeric overflow and underflow
       IMPLICIT NONE
       INTEGER :: I
       REAL     :: small = 1.0
       REAL     :: big   = 1.0
     !
       DO i=1,45
          PRINT 100, I, small, big
     100  FORMAT(' ',I3,' ',F9.4,' ',F9.4)
          !
          small = small/10.0
          big = big*10.0
          !
       END DO
     END PROGRAM ex_6_3

6.3.4 excode_6_4.f90

     PROGRAM ex_6_4
       ! Program to produce numeric overflow and underflow
        IMPLICIT NONE
       INTEGER :: I
       REAL     :: small = 1.0
       REAL     :: big   = 1.0
     !
       DO i=1,45
          PRINT 100, I, small, big
     100  FORMAT(' ',I3,' ',E10.4,' ',E10.4)
          !
          small = small/10.0
          big = big*10.0
          !
       END DO
     END PROGRAM ex_6_4

6.3.5 excode_6_5.f90

     PROGRAM ex_6_5
       ! Program to compute the Body Mass Index (Quetelet Index) according to the formula:
       !    BMI = (weight (kg))/(height^2 (m^2)) 
       !
       IMPLICIT NONE
       CHARACTER (LEN=25) :: Name
       INTEGER :: height_cm = 0, weight_kg = 0 ! height in cm and weight in kg
       REAL     :: height_m = 0.0 ! height in m units
       REAL     :: BMI ! Body Mass Index
       !
       PRINT*,  'Full Name:'; READ*, Name
       !
       PRINT*, 'Weight (kg)?:'; READ*, weight_kg
       !
       PRINT*, 'Height (cm)?:'; READ*, height_cm
       !
       height_m = height_cm/100.0
       BMI = weight_kg/(height_m**2)
       !
       PRINT 100, Name, BMI, BMI 
     100  FORMAT(1X,A ' BMI  is ', F10.4,' or ', E10.4)
     !
     END PROGRAM ex_6_5

6.3.6 excode_6_6.f90

     PROGRAM ex_6_6
     !
       IMPLICIT NONE
       INTEGER , PARAMETER :: N=1000000
       INTEGER , DIMENSION(1:N) :: X
       REAL    , DIMENSION(1:N) :: Y
       INTEGER :: I
       REAL :: T
       REAL    , DIMENSION(1:5) :: TP
       CHARACTER*10 :: COMMENT
     !
       OPEN(UNIT=10,FILE='/tmp/ex_6_6.txt')
     !
       CALL CPU_TIME(T)
     !
       TP(1)=T
       COMMENT=' Initial Time : '
       PRINT 100, COMMENT, TP(1)
       !
       DO I=1,N
          X(I)=I
       END DO
       !
       CALL CPU_TIME(T)
       !
       TP(2)=T-TP(1)
       COMMENT = ' Integer vector. Time :  '
       PRINT 100,COMMENT,TP(2)
       !
       Y=REAL(X)
       !
       CALL CPU_TIME(T)
       !
       TP(3)=T-TP(1)-TP(2)
       COMMENT = ' Real vector. Time :  '
       !
       PRINT 100,COMMENT,TP(3)
       !
       DO I=1,N
          WRITE(10,200) X(I)
     200  FORMAT(1X,I10)
       END DO
       !
       CALL CPU_TIME(T)
       TP(4)=T-TP(1)-TP(2)-TP(3)
       !
       COMMENT = ' Write Integer vector. Time :  '
       PRINT 100,COMMENT,TP(4)
       !
       DO I=1,N
          WRITE(10,300) Y(I)
     300  FORMAT(1X,f10.0)
       END DO
       !
       CALL CPU_TIME(T)
       TP(5)=T-TP(1)-TP(2)-TP(3)-TP(4)
       !
       COMMENT = ' Write Real vector. Time :  '
       PRINT 100,COMMENT,TP(5)
       !
     100 FORMAT(1X,A,2X,F7.3)
     END PROGRAM ex_6_6

6.3.7 excode_6_7.f90

     PROGRAM ex_6_7
       !
       IMPLICIT NONE
       !
       REAL , DIMENSION(:), ALLOCATABLE :: X_vec, Y_vec ! Data Vectors
       INTEGER :: Index, Ierr, Numpoints = 0
       REAL :: Max_x, Min_y
       CHARACTER(LEN=64) :: Filename
       !
       ! READ FILENAME
       READ(5,*) Filename
       ! OPEN FILE (READONLY)
       OPEN( UNIT=10, FILE=Filename, STATUS='OLD', ACTION='READ' )
       !
       DO
          READ(UNIT=10, FMT=100, ERR=10) Numpoints
          IF (Numpoints /= 0) EXIT
     10   READ (UNIT=10, FMT=*) ! JUMP ONE LINE
          CYCLE
       ENDDO
       !
       PRINT*, 'NUMPOINTS = ', Numpoints
       !
       ! ALLOCATE X, Y VECTORS
       ALLOCATE(X_vec(1:NUMPOINTS), STAT = IERR)
       IF (Ierr /= 0) STOP 'X_vec MEM ALLOCATION FAILED'
       ALLOCATE(Y_vec(1:NUMPOINTS), STAT = IERR)
       IF (Ierr /= 0) STOP 'Y_vec MEM ALLOCATION FAILED'
       !
       DO I = 1, Numpoints
          !
          READ(UNIT=10, FMT=110) X_vec(I), Y_vec(I)
          !
       ENDDO
       !
       Max_x = MAXVAL(X_vec)
       Min_y = MINVAL(Y_vec)
       !
       PRINT*, "MAXIMUM X VALUE = ", Max_x
       PRINT*, "MINIMUM Y VALUE = ", Min_y
       ! DEALLOCATE AND CLOSE FILE
       DEALLOCATE(X_vec, STAT = IERR)
       IF (Ierr /= 0) STOP 'X_vec MEM DEALLOCATION FAILED'
       DEALLOCATE(Y_vec, STAT = IERR)
       IF (Ierr /= 0) STOP 'Y_vec MEM DEALLOCATION FAILED'
       !
       CLOSE(10)
       ! FORMAT STATEMENTS
     100 FORMAT(19X,I3)
     110 FORMAT(F6.3,1X,F6.3)
       !
     END PROGRAM ex_6_7
     !# Remark 1
     !# Remark 2
     !Useless line 1
     !Useless line 2
     !Number of points = 4
     !+1.300;-2.443
     !+1.265;-1.453
     !+1.345;-8.437
     !+1.566;+4.455
     !+1.566;+4.455
     !+3.566;+7.755
     !+1.566;+4.457
     !+2.366;+2.454
     !+1.566;+4.405
     !+0.566;+9.450
     !+1.545;+4.465
     !+9.566;+6.455
     !+1.466;+8.405
     !+0.566;+7.055

[ 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