Top Banner
Beginning Fortran Fortran (77) Basics 22 October 2009 text on white background provided for easy printing
46

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

Dec 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing.

Beginning Fortran

Fortran (77) Basics22 October 2009

*Black text on white background provided for easy printing

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

Example Code

• Write a program to read in five values of temperature in Fahrenheit and convert to degrees Celsius OR Kelvin OR both.

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

Your Typical Programc234567 PROGRAM MYPROGRAM

STOP END

Program OptionsProgram Options

Declaration of VariablesDeclaration of Variables

MAIN CODEMAIN CODE

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

Your Typical Programc234567 PROGRAM MYPROGRAM

STOP END

Program OptionsProgram Options

Declaration of VariablesDeclaration of Variables

MAIN CODEMAIN CODE

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

Program Declaration

• You declare what kind of Fortran file you are writing on the first line.

• Syntax: <TYPE> <NAME>

c234567

PROGRAM CONVERTF

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

Program Declaration

• You declare what kind of Fortran file you are writing on the first line.

• Syntax: <TYPE> <NAME>

c234567

PROGRAM CONVERTFSpecifies the file as a program Program name – something short but descriptive

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

Your Typical Programc234567 PROGRAM CONVERTF

STOP END

Program OptionsProgram Options

Declaration of VariablesDeclaration of Variables

MAIN CODEMAIN CODE

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

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

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

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>

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

Options and Variables

c234567

IMPLICIT NONE

PARAMETER NT = 5

REAL F(NT)

REAL K(NT)

REAL C(NT)

LOGICAL DOC

LOGICAL DOK

INTEGER I

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

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)

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

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

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

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

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

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

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

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)?

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

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)?

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

Options and Variables

c234567

IMPLICIT NONE

PARAMETER NT = 5

REAL F(NT)

REAL K(NT)

REAL C(NT)

LOGICAL DOC

LOGICAL DOK

INTEGER IInteger that counts from 1 to 5 for loop over one-dimensional arrays

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

Your Typical Programc234567 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 CODEMAIN CODE

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

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

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

FF

From User

FiFi CiCi

KiKiTo User

To User

To User

DOC

DOK

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

FF

From User

FiFi CiCi

KiKiTo User

To User

To User

Read in 5 values of F into array

DOC

DOK

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

FF

From User

FiFi CiCi

KiKiTo User

To User

To User

For each of the five temperatures (for-loop):

DOC

DOK

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

FF

From User

FiFi CiCi

KiKiTo User

To User

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

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

FF

From User

FiFi CiCi

KiKiTo User

To User

To User

Output F to user (this should be done just to make sure that the input was read correctly).

DOC

DOK

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

FF

From User

FiFi CiCi

KiKiTo User

To User

To User

If DOC = TRUE, then output C as well.

DOC

DOK

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

FF

From User

FiFi CiCi

KiKiTo User

To User

To User

If DOK = TRUE, then compute K from C and output to user as well.

DOC

DOK

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

Main Code

FF

From User

c234567 DO I = 1, NT READ(*,*) F(I) ENDDO

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

Main Code

FF

From User

c234567 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 terminalFormat (*) = no particular format

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

Main Code

FF

From User

c234567 DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK

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

Main Code

FF

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.

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

Main Code

c234567 DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO

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

Main Code

c234567 DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO

For each temperature:

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

Main Code

c234567 DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO

For each temperature:Compute Celsius temp.

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

Main Code

c234567 IF (DOK .EQV. .TRUE.) THEN DO I = 1, NT K(I) = C(I) + 273.15 ENDDO ENDIF

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

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:

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

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)

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

Main Codec234567 IF ((DOC .EQV. .TRUE.) .AND. (DOK .EQV. .FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIFc IF ((DOC .EQV. .FALSE.) .AND. (DOK .EQV. .TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIFc 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

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

Main Codec234567 IF ((DOC .EQV. .TRUE.) .AND. (DOK .EQV. .FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIFc IF ((DOC .EQV. .FALSE.) .AND. (DOK .EQV. .TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIFc 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

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

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 ENDIFc IF ((DOC .EQV. .FALSE.) .AND. (DOK .EQV. .TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIFc 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

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

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 ENDIFc IF ((DOC .EQV. .FALSE.) .AND. (DOK .EQV. .TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIFc 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

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

Compilation

• Compilation is performed in the terminal:

Syntax:<compiler> -o <exec. filename> <source filename> <options>

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

Compilation

• Compilation is performed in the terminal:

Syntax:<compiler> -o <exec. filename> <source filename> <options>

Depends on system: f77, g77, pgf77, etc.

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

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

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

Compilation

• Compilation is performed in the terminal:

Syntax:<compiler> -o <exec. filename> <source filename> <options>

Use this *.f file to compile the executable

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

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 codeMbounds – if you attempt to reference an array index out of bounds, will notify youMbyteswapio – some formats require a byte-swap

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

Compilation

• Compilation is performed in the terminal:

Syntax:<compiler> -o <exec. filename> <source filename> <options>

pgf77 –o CONVERTF.exe CONVERTF.f