Black text on white background provided for easy
*Black text on white background provided for easy printing Beginning Fortran (77) Basics 22 October 2009
Example Code • Write a program to read in five values of temperature in Fahrenheit and convert to degrees Celsius OR Kelvin OR both.
Your Typical Program c 234567 PROGRAM MYPROGRAM Program Options Declaration of Variables MAIN CODE STOP END
Your Typical Program c 234567 PROGRAM MYPROGRAM Program Options Declaration of Variables MAIN CODE STOP END
Program Declaration • You declare what kind of Fortran file you are writing on the first line. • Syntax: <TYPE> <NAME> c 234567 PROGRAM CONVERTF
Program Declaration • You declare what kind of Fortran file you are writing on the first line. • Syntax: <TYPE> <NAME> c 234567 PROGRAM CONVERTF Specifies the file as a program Program name – something short but descriptive
Your Typical Program c 234567 PROGRAM CONVERTF Program Options Declaration of Variables MAIN CODE STOP END
Options and Variables • There are numerous options – you can Google them if you are interested • In general, there are two kinds: – You can “include” variables from another *. h file by putting include ‘<filename>. h’in the options section. – You can switch on other options about how the code is run (Google it) – We are going to use implicit none
Options and Variables • All variables we are going to use must be accounted for in the declaration section (no implicit variables allowed) – implicit none • What do we need? – Temperature in Fahrenheit, Celsius, Kelvin – Logicals (do we want Celsius, Kelvin, both? ) – Some integer to loop through all 5 values – Syntax: <TYPE> <NAME>
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) Specify a special parameter – an unchangeable value that can be used (unlike a REAL K(NT) immediately variable, which can change value) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) Array of 5 REALs for Fahrenheit temps REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) Array of 5 REALs for Kelvin temps REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) Array of 5 REALs for Celsius temps LOGICAL DOC LOGICAL DOK INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) Do we want to convert to Celsius LOGICAL DOC Logical: (TRUE) or not (FALSE)? LOGICAL DOK INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC Do we want to convert to Kelvin LOGICAL DOK Logical: (TRUE) or not (FALSE)? INTEGER I
Options and Variables c 234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK Integer that counts from 1 to 5 for loop INTEGER I over one-dimensional arrays
Your Typical Program c 234567 PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I MAIN CODE STOP END
Main Code • We need to do several things: – Read in 5 values of temperature – Determine if we need to convert to Celsius, Kelvin, or both – Output values
Ki From User To User DOK F Fi Ci DOC To User
Read in 5 values of F into array Ki From User To User DOK F Fi Ci DOC To User
For each of the five temperatures (for-loop): Ki From User To User DOK F Fi Ci DOC To User
Compute C (we are going to do this no matter what, because we know that the output has to either be C or K or both, and we need C in order to calculate K anyway). Ki From User To User DOK F Fi Ci DOC To User
Output F to user (this should be done just to make sure that the input was read correctly). Ki From User To User DOK F Fi Ci DOC To User
If DOC = TRUE, then output C as well. Ki From User To User DOK F Fi Ci DOC To User
If DOK = TRUE, then compute K from C and output to user as well. Ki From User To User DOK F Fi Ci DOC To User
From User F Main Code c 234567 DO I = 1, NT READ(*, *) F(I) ENDDO
From User F Main Code c 234567 DO I = 1, NT READ(*, *) F(I) ENDDO READ is a Fortran command that is used for input. Syntax: READ(<location>, <formatting>) Location (*) = read in from the terminal Format (*) = no particular format
From User F Main Code c 234567 DO I = 1, NT READ(*, *) F(I) ENDDO WRITE(*, *) ‘Convert to C? ’ READ(*, *) DOC WRITE(*, *) ‘Convert to K? ’ READ(*, *) DOK
From User F Main Code c 234567 DO I = 1, NT READ(*, *) F(I) ENDDO WRITE(*, *) ‘Convert to C? ’ READ(*, *) DOC WRITE(*, *) ‘Convert to K? ’ READ(*, *) DOK Write to screen with no particular formatting.
Main Code c 234567 DO I = 1, NT C(I) = (5. /9. )*(F(I)-32. ) ENDDO
Main Code c 234567 DO I = 1, NT C(I) = (5. /9. )*(F(I)-32. ) ENDDO For each temperature:
Main Code c 234567 DO I = 1, NT C(I) = (5. /9. )*(F(I)-32. ) ENDDO For each temperature: Compute Celsius temp.
Main Code c 234567 IF (DOK. EQV. . TRUE. ) THEN DO I = 1, NT K(I) = C(I) + 273. 15 ENDDO ENDIF
Main Code c 234567 IF (DOK. EQV. . TRUE. ) THEN DO I = 1, NT K(I) = C(I) + 273. 15 ENDDO ENDIF Logical trap: If we want to calculate Kelvin:
Main Code c 234567 IF (DOK. EQV. . TRUE. ) THEN DO I = 1, NT K(I) = C(I) + 273. 15 ENDDO ENDIF Logical trap: If we want to calculate Kelvin: Loop through temperatures and calculate Kelvin temps. (If DOK =. FALSE. , this entire loop is avoided)
Main Code c 234567 IF ((DOC. EQV. DO I = 1, NT WRITE(*, *) ENDDO ENDIF c IF ((DOC. EQV. DO I = 1, NT WRITE(*, *) ENDDO ENDIF . TRUE. ). AND. (DOK. EQV. . FALSE. )) THEN F(I), ‘F = ‘, C(I), ‘C’ . FALSE. ). AND. (DOK. EQV. . TRUE. )) THEN F(I), ‘F = ‘, K(I), ‘K’ . TRUE. ). AND. (DOK. EQV. . TRUE. )) THEN F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’
Main Code c 234567 IF ((DOC. EQV. DO I = 1, NT WRITE(*, *) ENDDO ENDIF c IF ((DOC. EQV. DO I = 1, NT WRITE(*, *) ENDDO ENDIF . TRUE. ). AND. (DOK. EQV. . FALSE. )) THEN F(I), ‘F = ‘, C(I), ‘C’ . FALSE. ). AND. (DOK. EQV. . TRUE. )) THEN F(I), ‘F = ‘, K(I), ‘K’ . TRUE. ). AND. (DOK. EQV. . TRUE. )) THEN F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’
c 234567 PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I DO I = 1, NT READ(*, *) F(I) ENDDO WRITE(*, *) ‘Convert to C? ’ READ(*, *) DOC WRITE(*, *) ‘Convert to K? ’ READ(*, *) DOK DO I = 1, NT C(I) = (5. /9. )*(F(I)-32. ) ENDDO IF (DOK. EQV. . TRUE. ) THEN DO I = 1, NT K(I) = C(I) + 273. 15 ENDDO ENDIF IF ((DOC. EQV. . TRUE. ). AND. (DOK. EQV. . FALSE. )) THEN DO I = 1, NT WRITE(*, *) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC. EQV. . FALSE. ). AND. (DOK. EQV. . TRUE. )) THEN DO I = 1, NT WRITE(*, *) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC. EQV. . TRUE. ). AND. (DOK. EQV. . TRUE. )) THEN DO I = 1, NT WRITE(*, *) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF STOP END
c 234567 PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I DO I = 1, NT READ(*, *) F(I) ENDDO WRITE(*, *) ‘Convert to C? ’ READ(*, *) DOC WRITE(*, *) ‘Convert to K? ’ READ(*, *) DOK DO I = 1, NT C(I) = (5. /9. )*(F(I)-32. ) ENDDO IF (DOK. EQV. . TRUE. ) THEN DO I = 1, NT K(I) = C(I) + 273. 15 ENDDO ENDIF IF ((DOC. EQV. . TRUE. ). AND. (DOK. EQV. . FALSE. )) THEN DO I = 1, NT WRITE(*, *) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC. EQV. . FALSE. ). AND. (DOK. EQV. . TRUE. )) THEN DO I = 1, NT WRITE(*, *) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC. EQV. . TRUE. ). AND. (DOK. EQV. . TRUE. )) THEN DO I = 1, NT WRITE(*, *) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF STOP END Program Start Options/Variable Declaration Main Code Program End
Compilation • Compilation is performed in the terminal: Syntax: <compiler> -o <exec. filename> <source filename> <options>
Compilation • Compilation is performed in the terminal: Syntax: <compiler> -o <exec. filename> <source filename> <options> Depends on system: f 77, g 77, pgf 77, etc.
Compilation • Compilation is performed in the terminal: Syntax: <compiler> -o <exec. filename> <source filename> <options> We wish to create an object that is an executable file with the following name
Compilation • Compilation is performed in the terminal: Syntax: <compiler> -o <exec. filename> <source filename> <options> Use this *. f file to compile the executable
Compilation • Compilation is performed in the terminal: Syntax: <compiler> -o <exec. filename> <source filename> <options> Also depends on compiler. Some frequent options: Mextend – allows you to go over column 70 in the code Mbounds – if you attempt to reference an array index out of bounds, will notify you Mbyteswapio – some formats require a byte-swap
Compilation • Compilation is performed in the terminal: Syntax: <compiler> -o <exec. filename> <source filename> <options> pgf 77 –o CONVERTF. exe CONVERTF. f
- Slides: 46