Input and Output 2. Modules: Extra Programs - Part 4 AUTUMN 2009
Input and Output 2. Modules: Extra Programs - Part 4 AUTUMN 2009
Modules
AUTUMN 2009
Alternatively:
WRITE( *,'( 1X, A3, 3( 2X, A7 ) )' ) 'Deg', 'Sin', 'Cos', 'Tan' WRITE( *,'( 1X, I3,3( 2X, F7.4 ) )' ) DEG, SIN( RAD ), COS( RAD ),TAN( RAD )
or:
100 FORMAT( 1X, A3, 3( 2X, A7 ) ) 110 FORMAT( 1X, I3, 3( 2X, F7.4 ) ) WRITE( *, 100 ) 'Deg', 'Sin', 'Cos', 'Tan' WRITE( *, 110 ) DEG, SIN( RAD ), COS( RAD ), TAN( RAD )
Writing to file:
OPEN( 33, FILE = 'trig.out' ) ! open file for output WRITE( 33, FMTHEAD ) 'Deg', 'Sin', 'Cos', 'Tan' WRITE( 33, FMTDATA ) DEG, SIN( RAD ), COS( RAD ), TAN( RAD ) CLOSE( 33 ) ! close file (tidiness is a virtue!)
Fortran
Week 4 - 1
David Apsley
! ! ! ! !
! Format specifiers CHARACTER (LEN=*), PARAMETER :: FMT1 = '( 1X, A4 , 2X, A10 )' CHARACTER (LEN=*), PARAMETER :: FMT2 = '( 1X, F4.2, 2X, 1PE10.3 )' DELTAX = ( XMAX - XMIN ) / NSTEP WRITE( *, FMT1 ) 'X', 'EXP' DO I = 0, NSTEP X = XMIN + I * DELTAX WRITE( *, FMT2 ) X, EXP( X ) END DO END PROGRAM EXPTABLE ! calculate step size ! write headers
Fortran
Week 4 - 2
David Apsley
Example 1.3 Illustrating reading and writing files and data analysis. The program adds scores from a round of golf. It reads an input file scorecard.txt of the form
4 6 Hole: David Judith Miriam Elizabeth 1 5 3 4 4 2 5 6 3 4 3 11 5 9 5 4 3 5 2 3 5 3 1 4 2 6 7 4 6 4
where: the first line is the number of players (here, 4) the second line is the number of holes (here, 6) the third line is the set of hole numbers the remaining lines are the players names and their scores for each hole. The number of players and the number of holes is arbitrary. Output consists of a table of players and total scores and is written to file total.txt.
PROGRAM GOLF IMPLICIT NONE INTEGER NPLAYERS ! number of files INTEGER NHOLES ! number of holes INTEGER SCORE ! total score INTEGER I ! loop counter INTEGER, ALLOCATABLE :: SHOTS(:) ! score for each hole CHARACTER (LEN=15) PLAYER ! player name CHARACTER (LEN=*), PARAMETER :: FMT = '(A, 3X, I3)' ! output format ! Open files OPEN( 21, FILE='scorecard.txt' ) OPEN( 22, FILE='total.txt' ) ! Read numbers of players and holes READ( 21, * ) NPLAYERS READ( 21, * ) NHOLES READ( 21, * ) ! skip a line - nothing needed ! Allocate memory to record the scores of one player ALLOCATE( SHOTS(NHOLES) ) ! Loop round, reading player and scores, writing player and total DO I = 1, NPLAYERS READ( 21, * ) PLAYER, SHOTS SCORE = SUM( SHOTS ) WRITE( 22, FMT ) PLAYER, SCORE END DO ! Close files and tidy up CLOSE( 21 ) CLOSE( 22 ) DEALLOCATE( SHOTS ) END PROGRAM GOLF
Fortran
Week 4 - 3
David Apsley
Example 1.4 Illustrating formatted READ, non-advancing i/o and the IOSTAT specifier. The program converts a passage of text to upper case. Input file: input.txt (any favourite piece of literature will do!) Output file: output.txt
PROGRAM UPPERCASE IMPLICIT NONE INTEGER :: IO = 0 INTEGER :: INCREMENT = ICHAR( 'A' ) - ICHAR( 'a' ) CHARACTER CH OPEN( 10, FILE = 'input.txt' ) OPEN( 20, FILE = 'output.txt' ) ! Open files
DO WHILE ( IO /= -1 ) READ( 10, '( A1 )', IOSTAT = IO, ADVANCE = 'NO' ) CH IF ( IO == 0 ) THEN IF ( CH >= 'a' .AND. CH <= 'z' ) THEN CH = CHAR( ICHAR( CH ) + INCREMENT ) END IF WRITE (20, '( A1 )', ADVANCE = 'NO' ) CH ELSE IF ( IO == -2 ) THEN WRITE (20, *) END IF END DO CLOSE( 10 ) CLOSE( 20 ) END PROGRAM UPPERCASE ! Close files
Fortran
Week 4 - 4
David Apsley
2. Modules Example 2.1 Illustrating modules used to share related parameters and variables. Compilation and linking commands: ftn95 conversion.f95 ftn95 distance.f95 slink distance.obj conversion.obj conversion.f95
MODULE CONVERSION ! Length conversion factors IMPLICIT NONE REAL, PARAMETER :: MILES_TO_METRES REAL, PARAMETER :: YARDS_TO_METRES REAL, PARAMETER :: FEET_TO_METRES REAL, PARAMETER :: INCHES_TO_METRES END MODULE CONVERSION
= = = =
distance.f95
PROGRAM DISTANCE USE CONVERSION IMPLICIT NONE REAL METRES REAL AMOUNT CHARACTER UNITS ! make conversion factors available
PRINT *, 'Input amount and units (i-inches, f-feet, y-yard, m-mile)' READ *, AMOUNT, UNITS SELECT CASE (UNITS) CASE ( 'i' , 'I' CASE ( 'f' , 'F' CASE ( 'y' , 'Y' CASE ( 'm' , 'M' END SELECT
); ); ); );
= = = =
* * * *
Fortran
Week 4 - 5
David Apsley
Example 2.2 Illustrating modules as CONTAINers of useful routines. Compilation and linking commands: ftn95 physics.f95 ftn95 gas.f95 slink gas.obj physics.obj physics.f95
MODULE PHYSICS ! Physical constants and some useful subprograms IMPLICIT NONE REAL, PARAMETER :: SPEED_OF_LIGHT = REAL, PARAMETER :: PLANCKS_CONSTANT = REAL, PARAMETER :: GRAVITATIONAL_CONSTANT = REAL, PARAMETER :: ELECTRON_MASS = REAL, PARAMETER :: ELECTRON_CHARGE = REAL, PARAMETER :: STEFAN_BOLTZMANN_CONSTANT = REAL, PARAMETER :: IDEAL_GAS_CONSTANT = REAL, PARAMETER :: AVOGADRO_NUMBER = CONTAINS REAL FUNCTION PRESSURE( n, T, V ) ! Computes pressure by ideal gas law (pV=nRT) REAL n, T, V ! moles, temperature, volume PRESSURE = n * IDEAL_GAS_CONSTANT * T / V END FUNCTION PRESSURE REAL FUNCTION RADIATION( T, AREA, EMISSIVITY ) ! Computes radiative heat flux REAL T ! thermodynamic temperature REAL AREA ! area of surface REAL EMISSIVITY RADIATION = EMISSIVITY * STEFAN_BOLTZMANN_CONSTANT * T ** 4 * AREA END FUNCTION RADIATION END MODULE PHYSICS
! ! ! ! ! ! ! !
gas.f95
PROGRAM IDEAL_GAS ! Program to test module <physics> USE PHYSICS IMPLICIT NONE REAL RMM REAL MASS REAL TEMPERATURE REAL VOLUME REAL MOLES ! access module
! ! ! ! !
relative molecular mass mass (kg) temperature (K) volume (m3) moles of gas
PRINT *, 'Input relative molecular mass' READ *, RMM PRINT *, 'Input mass(kg), temperature(K), volume(m3)' READ *, MASS, TEMPERATURE, VOLUME MOLES = 1000.0 * MASS / RMM ! calculate moles of gas
PRINT *, 'Pressure = ', PRESSURE( MOLES, TEMPERATURE, VOLUME ), 'Pa' END PROGRAM IDEAL_GAS
Fortran
Week 4 - 6
David Apsley