# beginning fortran

Post on 14-Jan-2016

38 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

*Black text on white background provided for easy printing. Beginning Fortran. Fortran (77) Advanced 29 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. c234567 - PowerPoint PPT Presentation

TRANSCRIPT

• Beginning FortranFortran (77) Advanced29 October 2009*Black text on white background provided for easy printing

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

• Your Typical Programc234567 PROGRAM MYPROGRAM

STOP END Program OptionsDeclaration of VariablesMAIN CODE

• Program StartOptions/Variable DeclarationMain CodeProgram Endc234567 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

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

• SubroutinesMini-programs that are run inside of a larger program (or subroutine):Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, c

• SubroutinesSubroutine is called, with variables a, b, and c passed to it. Those same variables, at their current values, now exist in subroutine.Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, cSubroutine(a,b,c)INTEGER a, b, c

• SubroutinesSubroutine is called, with variables a, b, and c passed to it. Those same variables, at their current values, now exist in subroutine.Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, cSubroutine(a,b,c)INTEGER a, b, c

• SubroutinesAdditional variables can be declared within the subroutine. These exist only in the subroutine, and will be destroyed when the routine is done.Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, cSubroutine(a,b,c)INTEGER a, b, cINTEGER d, e, f

• SubroutinesLines of code manipulating variables are processed in the subroutine, possibly changing the values of a, b, and c.Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, cSubroutine(a,b,c)INTEGER a, b, cINTEGER d, e, fCodeCodeCode

• SubroutinesAt RETURN, code returns to main program after subroutine was called. Variables a, b, and c now have NEW values depending on subroutine.Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, cSubroutine(a,b,c)INTEGER a, b, cINTEGER d, e, fCodeCodeCodeRETURNEND

• SubroutinesMain program continues on after the subroutine call with new values for a, b, and c.Main ProgramCodeCodeCodeCALL SUBROUTINE(a,b,c)CodeCodeSTOPENDINTEGER a, b, cSubroutine(a,b,c)INTEGER a, b, cINTEGER d, e, fCodeCodeCodeRETURNEND

• SubroutinesWhen 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.

• SubroutinesWhen 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

• Input ArgumentsAny 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)

• Input ArgumentsAny 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).

• Input ArgumentsIn 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

• Input ArgumentsIn 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 AVGNOTE: These variables must be declared as the same type as they are declared in the main program.

• Input ArgumentsNow 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 AVGc AVG = (X1 + X2)/2. RETURN END

• Input ArgumentsAny of the input arguments that were changed in the subroutine will now have new values in the main program.X1 = 3.0X2 = 8.0AVG = -9999.0CALL AVERAGE(X1,X2,AVG)WRITE(*,*) AVGAVG = (X1 + X2)/2.RETURNENDMain ProgramAVERAGE(X1,X2,AVG)

• Input ArgumentsAny of the input arguments that were changed in the subroutine will now have new values in the main program.X1 = 3.0X2 = 8.0AVG = -9999.0CALL AVERAGE(X1,X2,AVG)WRITE(*,*) AVGAVG = (X1 + X2)/2.RETURNENDMain ProgramAVERAGE(X1,X2,AVG)AVG is sent to subroutine with value of -9999.0

• Input ArgumentsAny of the input arguments that were changed in the subroutine will now have new values in the main program.X1 = 3.0X2 = 8.0AVG = -9999.0CALL AVERAGE(X1,X2,AVG)WRITE(*,*) AVGAVG = (X1 + X2)/2.RETURNENDMain ProgramAVERAGE(X1,X2,AVG)Subroutine changes value of AVG to 5.5

• Input ArgumentsAny of the input arguments that were changed in the subroutine will now have new values in the main program.X1 = 3.0X2 = 8.0AVG = -9999.0CALL AVERAGE(X1,X2,AVG)WRITE(*,*) AVGAVG = (X1 + X2)/2.RETURNENDMain ProgramAVERAGE(X1,X2,AVG)At RETURN, we return to the main program, where AVG now has a value of 5.5

• Passing ArraysYou can also pass arrays to a subroutine:Pass the array dimensions as INTEGERs first, then pass the array itselfPROGRAM MAINPARAMETER NX = 360PARAMETER NY = 181REAL TEMPS(NX,NY)CALL ZILCH(NX,NY,TEMPS)STOPENDSUBROUTINE ZILCH(NX,NY,VAR)INTEGER NXINTEGER NYREAL VAR(NX,NY)INTEGER IINTEGER JDO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDOENDDORETURNEND

• Passing ArraysNotice how the same variable can have different names in the main program and the subroutine.PROGRAM MAINPARAMETER NX = 360PARAMETER NY = 181REAL TEMPS(NX,NY)CALL ZILCH(NX,NY,TEMPS)STOPENDSUBROUTINE ZILCH(NX,NY,VAR)INTEGER NXINTEGER NYREAL VAR(NX,NY)INTEGER IINTEGER JDO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDOENDDORETURNEND

• Passing ArraysSUBROUTINE ZILCH(NX,NY,VAR)INTEGER NXINTEGER NYREAL VAR(NX,NY)INTEGER IINTEGER JDO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDOENDDORETURNENDNotice 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

• Rules of SubroutinesSubroutines are basically small programs built inside of a larger programInput 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 valueGiven a, b, and c, do something and then give me a, b, and c back.

• Rules of SubroutinesSince 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 MAININTEGER NXINTEGER NYREAD(*,*) NX, NYCALL MAKEARRAY(NX,NY)STOPENDSUBROUTINE MAKEARRAY(NX,NY)INTEGER NXINTEGER NYREAL VAR(NX,NY)RETURNEND

• Rules of SubroutinesSince 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 MAININTEGER NXINTEGER NYREAD(*,*) NX, NYCALL MAKEARRAY(NX,NY)STOPENDSUBROUTINE MAKEARRAY(NX,NY)INTEGER NXINTEGER NYREAL VAR(NX,NY)RETURNENDNX, NY read fr