Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing."— Presentation transcript:

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

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

3 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE

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

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.

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

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

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

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

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

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

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

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. Don’t worry; I’ll wait.”

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. Don’t worry; I’ll wait.” CALL (arg1,arg2,arg3,…) “input arguments”

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)

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

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

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.

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

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)

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

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

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

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

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

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

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.”

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

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

30 Rules of Subroutines This is something we couldn’t do before – CONVERTF was not particularly useful because we couldn’t 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.

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 END MAIN CODE: CALL SUB(NX) STOP END SUB(NX) VARIABLE DECLARATION: INTEGER NX VARIABLE DECLARATION: INTEGER NX MAIN CODE: RETURN END MAIN CODE: RETURN END

32 Using Subroutines You can write subroutines in two different ways: – You can write the subroutine directly within the parent program – You can write the subroutine in a separate *.f file and compile it along with the parent program

33 Using Subroutines … STOP END SUBROUTINE MYSUBROUTINE(X1,X2,X3,…) IMPLICIT NONE REAL X1 REAL X2 …

34 Compiling With Subroutines Compile external subroutines separately using the following syntax: pgf77 –c.f Then compile the parent program with the subroutine *.o files attached: pgf77 … -o.f

35 Proper Coding Technique There are some simple rules to follow to make sure that your code is readable and is robust – you can do a lot with it without having to write new source code.

36 Write in CAPITAL LETTERS This isn’t required, but writing code in capital letters and comments in lower-case makes it easier to distinguish the two from each other.

37 Use Subroutines Often If you find yourself having to code the same thing in over and over in your program, it is better to block that code off into a subroutine and call the subroutine every time you need to perform that function.

38 Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3

39 Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 Main Program essentially just reads in necessary information to begin program (array sizes, etc). Passes all of that information to the Driver Subroutine.

40 Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 Most of the main functions of the program are completed in the Driver Subroutine, where variables are declared based on information passed in from the Main Program.

41 Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 Individual functions that need to be completed (some multiple times) are written in different subroutines, which are called from the Driver Subroutine.

42 Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 You will find that most, if not all, professional code is written in this format.

43 Make Compilation Clear Programs can have complicated compilations, especially when you start attaching multiple subroutines with different options. If the compilation can be completed on a single line, put a comment in the code somewhere near the top containing the compilation line. If it’s more complex than that, make a script that compiles the program for you.

44 Use Source Directories Rather than keeping your source code, your subroutines, and your compiled programs all in the same directory, create a ‘source’ subdirectory where you keep your source code. This will prevent you from accidentally deleting, replacing, or otherwise ruining the code you’ve worked on.

45 Don’t Re-Invent The Wheel If you write your subroutines in separate files, you’ll find yourself using them in multiple programs – there’s no need to rewrite it. Use professional subroutine packages where possible – e.g. spherepack.


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

Similar presentations


Ads by Google