Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing

Download Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing

Post on 26-Dec-2015

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing </li> <li> Slide 2 </li> <li> Example Code Write a program to read in five values of temperature in Fahrenheit and convert to degrees Celsius OR Kelvin OR both. </li> <li> Slide 3 </li> <li> Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE </li> <li> Slide 4 </li> <li> Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE </li> <li> Slide 5 </li> <li> Program Declaration You declare what kind of Fortran file you are writing on the first line. Syntax: c234567 PROGRAM CONVERTF </li> <li> Slide 6 </li> <li> Program Declaration You declare what kind of Fortran file you are writing on the first line. Syntax: c234567 PROGRAM CONVERTF Specifies the file as a program Program name something short but descriptive </li> <li> Slide 7 </li> <li> Your Typical Program c234567 PROGRAM CONVERTF STOP END Program Options Declaration of Variables MAIN CODE </li> <li> Slide 8 </li> <li> 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 .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 </li> <li> Slide 9 </li> <li> 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: </li> <li> Slide 10 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I </li> <li> Slide 11 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Specify a special parameter an unchangeable value that can immediately be used (unlike a variable, which can change value) </li> <li> Slide 12 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Array of 5 REALs for Fahrenheit temps </li> <li> Slide 13 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Array of 5 REALs for Kelvin temps </li> <li> Slide 14 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Array of 5 REALs for Celsius temps </li> <li> Slide 15 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Logical: Do we want to convert to Celsius (TRUE) or not (FALSE)? </li> <li> Slide 16 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Logical: Do we want to convert to Kelvin (TRUE) or not (FALSE)? </li> <li> Slide 17 </li> <li> Options and Variables c234567 IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I Integer that counts from 1 to 5 for loop over one-dimensional arrays </li> <li> Slide 18 </li> <li> Your Typical Program c234567 PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I STOP END MAIN CODE </li> <li> Slide 19 </li> <li> 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 </li> <li> Slide 20 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User DOC DOK </li> <li> Slide 21 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User Read in 5 values of F into array DOC DOK </li> <li> Slide 22 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User For each of the five temperatures (for-loop): DOC DOK </li> <li> Slide 23 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi 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). DOC DOK </li> <li> Slide 24 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User Output F to user (this should be done just to make sure that the input was read correctly). DOC DOK </li> <li> Slide 25 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User If DOC = TRUE, then output C as well. DOC DOK </li> <li> Slide 26 </li> <li> F F From User FiFi FiFi CiCi CiCi KiKi KiKi To User If DOK = TRUE, then compute K from C and output to user as well. DOC DOK </li> <li> Slide 27 </li> <li> Main Code F F From User c234567 DO I = 1, NT READ(*,*) F(I) ENDDO </li> <li> Slide 28 </li> <li> Main Code F F From User c234567 DO I = 1, NT READ(*,*) F(I) ENDDO READ is a Fortran command that is used for input. Syntax: READ(, ) Location (*) = read in from the terminal Format (*) = no particular format </li> <li> Slide 29 </li> <li> Main Code F F From User c234567 DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) Convert to C? READ(*,*) DOC WRITE(*,*) Convert to K? READ(*,*) DOK </li> <li> Slide 30 </li> <li> Main Code F F From User c234567 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. </li> <li> Slide 31 </li> <li> Main Code c234567 DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO </li> <li> Slide 32 </li> <li> Main Code c234567 DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO For each temperature: </li> <li> Slide 33 </li> <li> Main Code c234567 DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO For each temperature: Compute Celsius temp. </li> <li> Slide 34 </li> <li> Main Code c234567 IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) + 273.15 ENDDO ENDIF </li> <li> Slide 35 </li> <li> Main Code c234567 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: </li> <li> Slide 36 </li> <li> Main Code c234567 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) </li> <li> Slide 37 </li> <li> Main Code c234567 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 </li> <li> Slide 38 </li> <li> Main Code c234567 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 </li> <li> Slide 39 </li> <li> c234567 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 </li> <li> Slide 40 </li> <li> Program Start Options/Variable Declaration Main Code Program End c234567 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 </li> <li> Slide 41 </li> <li> Compilation Compilation is performed in the terminal: Syntax: -o </li> <li> Slide 42 </li> <li> Compilation Compilation is performed in the terminal: Syntax: -o Depends on system: f77, g77, pgf77, etc. </li> <li> Slide 43 </li> <li> Compilation Compilation is performed in the terminal: Syntax: -o We wish to create an object that is an executable file with the following name </li> <li> Slide 44 </li> <li> Compilation Compilation is performed in the terminal: Syntax: -o Use this *.f file to compile the executable </li> <li> Slide 45 </li> <li> Compilation Compilation is performed in the terminal: Syntax: -o 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 </li> <li> Slide 46 </li> <li> Compilation Compilation is performed in the terminal: Syntax: -o pgf77 o CONVERTF.exe CONVERTF.f </li> </ul>