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


Fortran 90 Lessons for Computational Chemistry
Chapter 7 - Input/Output (II)


7.1 Objectives

The main aims of this session consist of:

  1. presenting the use of FORMAT in reading operations.

  1. considering basic techniques about the reading of files in Fortran.

  1. presenting possible alternatives to the standard I/O: here documents and the NAMELIST type input.

  1. presenting internal files.

This chapter is very much linked with the previous one, having an emphasis in reading data instead of writing them. We present interesting options for providing input data to a program. Formatted input is seldom used with the keyboard, though it is very important when reading data stored in a file.


7.2 Main items.


7.3 Example Codes


7.3.1 Programa ejemplo_7_1.f90

     PROGRAM EJEMPLO_7_1
       IMPLICIT NONE
       !Definicion de variables
       INTEGER , PARAMETER :: NROW=5
       INTEGER , PARAMETER :: NCOL=6
       REAL , DIMENSION(1:NROW,1:NCOL)   :: RESULT_EXAMS = 0.0
       REAL , DIMENSION(1:NROW)          :: MEDIA_ESTUD = 0.0
       REAL , DIMENSION(1:NCOL)          :: MEDIA_ASIGN = 0.0
       INTEGER :: R,C
       !
       ! Abrir fichero para lectura
       OPEN(UNIT=20,FILE='notas.dat',STATUS='OLD') 
       !
       DO R=1,NROW
          READ(UNIT=20,FMT=100) RESULT_EXAMS(R,1:NCOL),MEDIA_ESTUD(R) ! Lectura de notas y luego de promedio
          100 FORMAT(6(2X,F4.1),2X,F5.2) ! Se leen 6 numeros seguidos y luego un septimo 
       ENDDO
       READ (20,*) ! Saltamos una linea con esta orden
       READ (20,110) MEDIA_ASIGN(1:NCOL) ! 
     110 FORMAT(6(2X,F4.1))
     !
     ! IMPRESION DE LAS NOTAS EN LA SALIDA ESTANDAR
       DO R=1,NROW
          PRINT 200, RESULT_EXAMS(R,1:NCOL), MEDIA_ESTUD(R)
     200  FORMAT(1X,6(1X,F5.1),' = ',F6.2)
       END DO
       PRINT *,'  ====  ====  ====  ====  ====  ==== '
       PRINT 210, MEDIA_ASIGN(1:NCOL)
     210 FORMAT(1X,6(1X,F5.1))
     END PROGRAM EJEMPLO_7_1

7.3.2 excode_7_2.f90

     PROGRAM ex_7_2
     ! Second degree equation solver
     ! y = A*x**2 + B*x + C
       IMPLICIT NONE
       ! Variables
       REAL :: A = 0.0
       REAL :: B = 0.0
       REAL :: C = 0.0
       REAL, DIMENSION(2) :: SOL
       REAL :: TEMP
       INTEGER :: I
       !
       ! Input: A, B, C
       READ*, A 
       READ*, B
       READ*, C 
       !
       ! Calculations
       TEMP = SQRT(B*B-4.0*A*C)
       !
       SOL(1) = (-B+TEMP)/(2.0*A)
       SOL(2) = (-B-TEMP)/(2.0*A)
       !
       !
       ! 
       DO I=1, 2
          PRINT 200, I, SOL(I)
     200  FORMAT(1X,'SOLUTION ', I2,' = ',F18.6)
       END DO
     !
     END PROGRAM EX_7_2

7.3.3 Script ej_here_file

     # Compile..
     gfortran -o second_order  excode_7_2.f90
     # And Run...
     ./second_order <<eof
     2.0     # A
     1.0     # B
     -4.0    # C
     eof

7.3.4 excode_7_3.f90

     PROGRAM ex_7_3
       ! Solving second order algebraic equation
       ! y = A*x**2 + B*x + C
       IMPLICIT NONE
       ! Variables
       REAL :: A = 0.0
       REAL :: B = 0.0
       REAL :: C = 0.0
       REAL, DIMENSION(2) :: SOL
       REAL :: TEMP
       INTEGER :: I
       !
       !     NAMELIST DEFINITION
       NAMELIST/INP0/ A, B, C
       !     NAMELIST FILE
       OPEN(UNIT=10,FILE='sec_order.inp',STATUS='OLD')
       !     Inpot of A, B, C
       READ(10,INP0)
       !
       ! Calculations
       TEMP = SQRT(B*B-4.0*A*C)
       !
       SOL(1) = (-B+TEMP)/(2.0*A)
       SOL(2) = (-B-TEMP)/(2.0*A)
       !
       !
       ! OUTOPUT 
       DO I=1, 2
          PRINT 200, I, SOL(I)
     200  FORMAT(1X,'SOLUTION ', I2,' = ',F18.6)
       END DO
     !
     END PROGRAM EX_7_3

7.3.5 namelist input file

     #
     #       INPUT FILE FOR excode_7_3.f90
     #
      &INP0 A=2.0, B=1.0, C=-4.0 /

7.3.6 excode_7_4.f90

     PROGRAM ex_7_4
       !
       ! Internal file example
       !
       IMPLICIT NONE
       ! Variables
       REAL :: x_var
       INTEGER :: unit_n, index_X
       CHARACTER(LEN=65) :: filename
       CHARACTER(LEN=56) :: pref
       !
       PRINT*, "Introduce file name preffix: "
       READ(*,*) pref
       !
       DO unit_n = 10, 20
          !
          WRITE(filename, '(A, "_", i2,".dat")') TRIM(pref), unit_n
          OPEN(UNIT = unit_n, FILE = filename, STATUS = "UNKNOWN", ACTION = "WRITE")
          !
          DO index_X = 0, 100
             x_var = REAL(index_X)*0.01
             WRITE(unit_n, '(1X,2ES14.6)') x_var, SIN(REAL(unit_n)*x_var)
          ENDDO
          !
          CLOSE(UNIT = unit_n)
          !
       ENDDO
     !
     END PROGRAM ex_7_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