beginning fortran fortran (77) advanced 29 october 2009 *black text on white background provided for...

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

Post on 13-Dec-2015

226 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing Slide 2 Example Code Write a program to read in five values of temperature in Fahrenheit and convert to degrees Celsius OR Kelvin OR both. Slide 3 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE Slide 4 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 Slide 5 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 This program is not particularly useful: -What if we want to calculate 3 temperatures? Or 30? -Open source code, change NT, recompile, re-run -We are primarily restricted because of the necessity of declaring the value of NT: it is a parameter that has to be set in the code before anything else is done. Slide 6 Subroutines Mini-programs that are run inside of a larger program (or subroutine): Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Slide 7 Subroutines Subroutine is called, with variables a, b, and c passed to it. Those same variables, at their current values, now exist in subroutine. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c Slide 8 Subroutines Subroutine is called, with variables a, b, and c passed to it. Those same variables, at their current values, now exist in subroutine. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c Slide 9 Subroutines Additional variables can be declared within the subroutine. These exist only in the subroutine, and will be destroyed when the routine is done. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Slide 10 Subroutines Lines of code manipulating variables are processed in the subroutine, possibly changing the values of a, b, and c. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Code Slide 11 Subroutines At RETURN, code returns to main program after subroutine was called. Variables a, b, and c now have NEW values depending on subroutine. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Code RETURN END Slide 12 Subroutines Main program continues on after the subroutine call with new values for a, b, and c. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Code RETURN END Slide 13 Subroutines When calling a subroutine, the main program is basically saying: Given these variables at their current values, do some stuff and then give me my variables back. Dont worry; Ill wait. Slide 14 Subroutines When calling a subroutine, the main program is basically saying: Given these variables at their current values, do some stuff and then give me my variables back. Dont worry; Ill wait. CALL (arg1,arg2,arg3,) input arguments Slide 15 Input Arguments Any variables that the main program wants the subroutine to make use of have to be passed to the subroutine as input arguments. These appear as a list of the names of the variables in parentheses after the name of the subroutine being called. CALL AVERAGE(X1,X2,AVG) Slide 16 Input Arguments Any variables that the main program wants the subroutine to make use of have to be passed to the subroutine as input arguments. These appear as a list of the names of the variables in parentheses after the name of the subroutine being called. CALL AVERAGE(X1,X2,AVG) Subroutine AVERAGE takes in three variables: two numbers (X1 and X2) and a variable that will represent the average of them (AVG). Slide 17 Input Arguments In the subroutine, these variables are re- declared so that they now exist simultaneously in the main program and in the subroutine: C234567 SUBROUTINE AVERAGE(X1,X2,AVG) IMPLICIT NONE REAL X1 REAL X2 REAL AVG Slide 18 Input Arguments In the subroutine, these variables are re- declared so that they now exist simultaneously in the main program and in the subroutine: C234567 SUBROUTINE AVERAGE(X1,X2,AVG) IMPLICIT NONE REAL X1 REAL X2 REAL AVG NOTE: These variables must be declared as the same type as they are declared in the main program. Slide 19 Input Arguments Now we can write code in the subroutine to compute AVG from X1 and X2: C234567 SUBROUTINE AVERAGE(X1,X2,AVG) IMPLICIT NONE REAL X1 REAL X2 REAL AVG c AVG = (X1 + X2)/2. RETURN END Slide 20 Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = -9999.0 CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) Slide 21 Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = -9999.0 CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) AVG is sent to subroutine with value of -9999.0 Slide 22 Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = -9999.0 CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) Subroutine changes value of AVG to 5.5 Slide 23 Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = -9999.0 CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) At RETURN, we return to the main program, where AVG now has a value of 5.5 Slide 24 Passing Arrays You can also pass arrays to a subroutine: Pass the array dimensions as INTEGERs first, then pass the array itself PROGRAM MAIN PARAMETER NX = 360 PARAMETER NY = 181 REAL TEMPS(NX,NY) CALL ZILCH(NX,NY,TEMPS) STOP END SUBROUTINE ZILCH(NX,NY,VAR) INTEGER NX INTEGER NY REAL VAR(NX,NY) INTEGER I INTEGER J DO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDO RETURN END Slide 25 Passing Arrays Notice how the same variable can have different names in the main program and the subroutine. PROGRAM MAIN PARAMETER NX = 360 PARAMETER NY = 181 REAL TEMPS(NX,NY) CALL ZILCH(NX,NY,TEMPS) STOP END SUBROUTINE ZILCH(NX,NY,VAR) INTEGER NX INTEGER NY REAL VAR(NX,NY) INTEGER I INTEGER J DO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDO RETURN END Slide 26 Passing Arrays SUBROUTINE ZILCH(NX,NY,VAR) INTEGER NX INTEGER NY REAL VAR(NX,NY) INTEGER I INTEGER J DO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDO RETURN END Notice something interesting here: -We took in values from the main program for NX and NY, then created a variable VAR based on those dimensions. -We can do this, because input arguments are special Slide 27 Rules of Subroutines Subroutines are basically small programs built inside of a larger program Input arguments are handed down to the subroutine from its parent program the subroutine is constructed with a priori knowledge of these variables and their present value Given a, b, and c, do something and then give me a, b, and c back. Slide 28 Rules of Subroutines Since the subroutine is built with the input arguments already known, we can make an array inside of the subroutine based on variables passed into it. PROGRAM MAIN INTEGER NX INTEGER NY READ(*,*) NX, NY CALL MAKEARRAY(NX,NY) STOP END SUBROUTINE MAKEARRAY(NX,NY) INTEGER NX INTEGER NY REAL VAR(NX,NY) RETURN END Slide 29 Rules of Subroutines Since the subroutine is built with the input arguments already known, we can make an array inside of the subroutine based on variables passed into it. PROGRAM MAIN INTEGER NX INTEGER NY READ(*,*) NX, NY CALL MAKEARRAY(NX,NY) STOP END SUBROUTINE MAKEARRAY(NX,NY) INTEGER NX INTEGER NY REAL VAR(NX,NY) RETURN END NX, NY read from user into MAINNX, NY handed to MAKEARRAY to make VAR Slide 30 Rules of Subroutines This is something we couldnt do before CONVERTF was not particularly useful because we couldnt ask the user for the dimensions of array F and then make an array, we had to use a parameter. However, we can ask for the dimensions of the array and create an array inside of a subroutine with those dimensions, because for the subroutine, that variable is passed in as a priori knowledge. Slide 31 CONVERTF VARIABLE DECLARATION: PARAMETER NX = 5 VARIABLE DECLARATION: PARAMETER NX = 5 MAIN CODE: STOP END MAIN CODE: STOP END CONVERTF2 VARIABLE DECLARATION: INTEGER NX VARIABLE DECLARATION: INTEGER NX MAIN CODE: CALL SUB(NX) STOP EN

Recommended

View more >