Date post: | 06-Jul-2018 |
Category: |
Documents |
Upload: | mbeygi1357 |
View: | 216 times |
Download: | 0 times |
of 46
8/18/2019 Fortran 02
1/46
Beginning Fortran
Fortran (77) Basics
22 October 2009
*Black text on white background provided or eas! printing
8/18/2019 Fortran 02
2/46
"xa#ple $ode
• %rite a progra# to read in &vevalues o te#perature in Fahrenheitand convert to degrees $elsius O'elvin O' both
8/18/2019 Fortran 02
3/46
our +!pical ,rogra#
c234567
PROGRAM MYPROGRAM
STOP
END
,rogra#Options
-eclaration o .ariables
/1 $O-"
8/18/2019 Fortran 02
4/46
our +!pical ,rogra#
c234567
PROGRAM MYPROGRAM
STOP
END
,rogra#Options
-eclaration o .ariables
/1 $O-"
8/18/2019 Fortran 02
5/46
,rogra# -eclaration
• ou declare what kind o Fortran &le!ou are writing on the &rst line
•3!ntax4 5+,"6 5/"6
c234567
PROGRAM CONVERTF
8/18/2019 Fortran 02
6/46
,rogra# -eclaration
• ou declare what kind o Fortran &le!ou are writing on the &rst line
•3!ntax4 5+,"6 5/"6
c234567
PROGRAM CONVERTFSpecifies the file as a program Program name – something short but descriptive
8/18/2019 Fortran 02
7/46
our +!pical ,rogra#
c234567
PROGRAM CONVERTF
STOP
END
,rogra#Options
-eclaration o .ariables
/1 $O-"
8/18/2019 Fortran 02
8/46
Options and .ariables
• +here are nu#erous options !oucan 8oogle the# i !ou are interested
• 1n general there are two kinds4
– ou can :include; variables ro# another*h &le b! putting include‘.h’in the options section
– ou can switch on other options abouthow the code is run (8oogle it)
– %e are going to use implicit none
8/18/2019 Fortran 02
9/46
Options and .ariables
• ll variables we are going to use #ustbe accounted or in the declarationsection (no i#plicit variables allowed)
implicit none
• %hat do we need<
– +e#perature in Fahrenheit $elsius elvin
– =ogicals (do we want $elsius elvin both values
– 3!ntax4 5+,"6 5/"6
8/18/2019 Fortran 02
10/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I
8/18/2019 Fortran 02
11/46
Options and .ariables
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)
8/18/2019 Fortran 02
12/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I
Array of !"A#s for $ahrenheit temps
8/18/2019 Fortran 02
13/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I
Array of !"A#s for %elvin temps
8/18/2019 Fortran 02
14/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I
Array of !"A#s for &elsius temps
8/18/2019 Fortran 02
15/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I
#ogical' o we want to convert to &elsius
(!*") or not ($A#S")+
8/18/2019 Fortran 02
16/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I #ogical' o we want to convert to %elvin(!*") or not ($A#S")+
8/18/2019 Fortran 02
17/46
Options and .ariables
c234567
IMPLICIT NONE
PARAMETER NT = 5
REAL F(NT)
REAL K(NT)
REAL C(NT)
LOGICAL DOC
LOGICAL DOK
INTEGER I
nteger that counts from - to for loopover one.dimensional arrays
8/18/2019 Fortran 02
18/46
our +!pical ,rogra#
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
/1 $O-"
8/18/2019 Fortran 02
19/46
/ain $ode
• %e need to do several things4
– 'ead in > values o te#perature
– -eter#ine i we need to convert to$elsius elvin or both
– Output values
8/18/2019 Fortran 02
20/46
8/18/2019 Fortran 02
21/46
F
$rom *ser
Fi Ci
K i
o *ser
o *ser
o *ser
!ead in values of $
into array
/&
/%
8/18/2019 Fortran 02
22/46
F
$rom *ser
Fi Ci
K i
o *ser
o *ser
o *ser
$or each of the five
temperatures (for.loop)'
/&
/%
8/18/2019 Fortran 02
23/46
F
$rom *ser
Fi Ci
K i
o *ser
o *ser
o *ser
&ompute & (we are going to do this no matter
what, because we know that the output has to
either be & or % or both, and we need & in
order to calculate % anyway)0
/&
/%
8/18/2019 Fortran 02
24/46
F
$rom *ser
Fi Ci
K i
o *ser
o *ser
o *ser
/utput $ to user (this
should be done 1ust to
make sure that the input
was read correctly)0
/&
/%
8/18/2019 Fortran 02
25/46
F
$rom *ser
Fi Ci
K i
o *ser
o *ser
o *ser
f /& 2 !*", then
output & as well0
/&
/%
8/18/2019 Fortran 02
26/46
F
$rom *ser
Fi Ci
K i
o *ser
o *ser
o *ser
f /% 2 !*", then
compute % from & and
output to user as well0
/&
/%
8/18/2019 Fortran 02
27/46
/ain $ode
F
$rom *ser
c234567
DO I = 1, NT
READ(*,*) F(I)
ENDDO
8/18/2019 Fortran 02
28/46
/ain $ode
F
$rom *ser
c234567
DO I = 1, NT
READ(*,*) F(I)
ENDDO
!"A is a $ortran command that is used for input0
Synta3' !"A(4location5,4formatting5)#ocation (6) 2 read in from the terminal
$ormat (6) 2 no particular format
8/18/2019 Fortran 02
29/46
/ain $ode
F
$rom *ser
c234567
DO I = 1, NT
READ(*,*) F(I)
ENDDO WRITE(*,*) ‘Convert to C?’
READ(*,*) DOC
WRITE(*,*) ‘Convert to K?’
READ(*,*) DOK
8/18/2019 Fortran 02
30/46
/ain $ode
F
$rom *ser
c234567
DO I = 1, NT
READ(*,*) F(I)
ENDDO WRITE(*,*) ‘Convert to C?’
READ(*,*) DOC
WRITE(*,*) ‘Convert to K?’
READ(*,*) DOK
7rite to screen with no particular formatting0
8/18/2019 Fortran 02
31/46
/ain $ode
c234567
DO I = 1, NT
C(I) = (5./9.)*(F(I)-32.)
ENDDO
8/18/2019 Fortran 02
32/46
/ain $ode
c234567
DO I = 1, NT
C(I) = (5./9.)*(F(I)-32.)
ENDDO
$or each temperature'
8/18/2019 Fortran 02
33/46
/ain $ode
c234567
DO I = 1, NT
C(I) = (5./9.)*(F(I)-32.)
ENDDO
$or each temperature'
&ompute &elsius temp0
8/18/2019 Fortran 02
34/46
/ain $ode
c234567
IF (DOK .EQV. .TRUE.) THEN
DO I = 1, NT
K(I) = C(I) + 273.15
ENDDO
ENDIF
8/18/2019 Fortran 02
35/46
/ain $ode
c234567
IF (DOK .EQV. .TRUE.) THEN
DO I = 1, NT
K(I) = C(I) + 273.15
ENDDO
ENDIF
#ogical trap' f we want to calculate %elvin'
8/18/2019 Fortran 02
36/46
/ain $ode
c234567
IF (DOK .EQV. .TRUE.) THEN
DO I = 1, NT
K(I) = C(I) + 273.15
ENDDO
ENDIF
#ogical trap' f we want to calculate %elvin'
#oop through temperatures and calculate %elvin temps0(f /% 2 0$A#S"0, this entire loop is avoided)
8/18/2019 Fortran 02
37/46
/ain $odec234567
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
8/18/2019 Fortran 02
38/46
/ain $odec234567
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
c234567
PROGRAM CONVERTF
8/18/2019 Fortran 02
39/46
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
P St tc234567
PROGRAM CONVERTF
8/18/2019 Fortran 02
40/46
Program Start
Options/Variable Declaration
Main Code
Program End
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
8/18/2019 Fortran 02
41/46
$o#pilation
• $o#pilation is peror#ed in theter#inal4
3!ntax45co#piler6 ?o 5exec &lena#e6 5source
&lena#e6 5options6
8/18/2019 Fortran 02
42/46
$o#pilation
• $o#pilation is peror#ed in theter#inal4
3!ntax45co#piler6 ?o 5exec &lena#e6 5source
&lena#e6 5options6epends on system' f88, g88, pgf88, etc0
8/18/2019 Fortran 02
43/46
$o#pilation
• $o#pilation is peror#ed in theter#inal4
3!ntax45co#piler6 ?o 5exec &lena#e6 5source
&lena#e6 5options67e wish to create an ob1ect that is an e3ecutable file with the following name
8/18/2019 Fortran 02
44/46
$o#pilation
• $o#pilation is peror#ed in theter#inal4
3!ntax45co#piler6 ?o 5exec &lena#e6 5source
&lena#e6 5options6*se this 60f file to compile the e3ecutable
8/18/2019 Fortran 02
45/46
$o#pilation
• $o#pilation is peror#ed in theter#inal4
3!ntax45co#piler6 ?o 5exec &lena#e6 5source
&lena#e6 5options6 Also depends on compiler0 Some fre9uent options'
:e3tend – allows you to go over column 8; in the code:bounds – if you attempt to reference an array inde3 out of bounds, will notify you
:byteswapio – some formats re9uire a byte.swap
8/18/2019 Fortran 02
46/46
$o#pilation
• $o#pilation is peror#ed in theter#inal4
3!ntax45co#piler6 ?o 5exec &lena#e6 5source
&lena#e6 5options6
pgf77 –o CONVERTF.exe CONVERTF.f