1 / 63

Fortran

Fortran. General: Mathematical computations (engineering, computational biology) FORmula TRANslation  FORTRAN Started in 1950's at IBM Fortran 66 Fortran 77  this class Fortran 90 (95) Why: dominant programming language used in engineering applications

edric
Download Presentation

Fortran

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fortran • General: • Mathematical computations (engineering, computational biology) • FORmula TRANslation  FORTRAN • Started in 1950's at IBM • Fortran 66 • Fortran 77  this class • Fortran 90 (95) • Why: • dominant programming language used in engineering applications • ( > 50 % engineering applications)

  2. Layout 1234567890123456789012345678901234567890123456789012 ... 34567890 Column 1 marked C for comment line 73-80 Sequence number 1-5 statement number 6 continuation 7-72 main text

  3. Fortran 77 is not a free-format language, • strict set of rules for source code • column position rules: • Col. 1 : Blank, or a "c" or "*" for comments • Col. 2-5 : Statement label (optional) • Col. 6 : Continuation of previous line (optional) • Col. 7-72 : Statements • Col. 73-80: Sequence number (optional, rarely used today)

  4. No difference between upper or lower cases No reserved keyword Integer Real Complex (1.0,2.2), (1.0e0,0.22e1) Logical .true. , .false. Character ‘a’, ‘123’ Double precision: 3.14159265d0 Variable names

  5. Variable names generally consist of 1-6 characters • chosen from the letters a-z and the digits 0-9 • The first character must be a letter • Case insensitive (upper case, lower case)

  6. integer sum, id, SSN integer*2 short real temp, height double precision temp, height, long complex curr,volt logical done, error character*20 name, names(20) Extra caution: I-N rule

  7. Any names start with I,J,K,L,M,N (i,j,k,l,m,n) To overrule IMPLICIT NONE To add additional implicit variable type IMPLICIT COMPLEX C,X Default integer variable names

  8. A general Fortran program: • a main program (or driver) and • several subprograms (or procedures or subroutines). • program name • declarations • statements • stop • end • subprograms …..

  9. --------------------------------------------------------- program circle real r, area c This program reads a real number r and prints c the area of a circle with radius r. write (*,*) 'Give radius r:' read (*,*) r area = 3.14159*r*r write (*,*) 'Area = ', area stop end

  10. Constant: • parameter (name = constant, ... , name = constant) • parameter (pi=3.14159,c=3.e8) • The "variable" defined in the parameter statement is not a variable but rather a constant whose value can never change • A "variable" can appear in at most one parameter statement • The parameter statement(s) must come before the first executable statement

  11. DATA x,y,z/1.0,2.2,3.e0/ placed before executable statements data

  12. Arithmetic Expressions operand operator operand ** {exponentiation} *,/ {multiplication, division} +,- {addition, subtraction} caution for division integer/integer  perform integer division 5/3  1 5.0/3.0  1.66667 5.0/3  1.66667

  13. Logical expressions Logical expressions can only have the value .TRUE. or .FALSE.. A logical expression can be formed by comparing arithmetic expressions using the following relational operators: meaning .LT. < .LE. <= .GT. > .GE. >= .EQ. = .NE. !=

  14. Assignment The assignment has the form variable_name = expression Evaluate the right hand side and assign the resulting value to the variable on the left. For example, area = pi * r**2

  15. Type conversion different data types occur in the same expression  type conversion implicitly: real x x = x + 1 explicitly: int (variable) int(2.3)  2 real … real(2) 2.0 dble … ichar … char …

  16. key-board input / screen output read (*,*) list-of-variables write(*,*) list-of-variables integer m, n real x, y read(*,*) m, n ! -1 100 read(*,*) x, y ! -1.0 1e+2 write(*,*) m,n write(*,*) x,y

  17. Format statement write(*, label) list-of-variables label format format-code Eg. format(1x,i3,1x,f5.2) A - text string D - double precision numbers E - real numbers, exponent notation F - real numbers, fixed point format I - integer X - horizontal skip (space) / - vertical skip (newline)

  18. x = 0.025 write(*,100) 'x=', x 100 format (A,F) write(*,110) 'x=', x 110 format (A,F5.3) write(*,120) 'x=', x 120 format (A,E) write(*,130) 'x=', x 130 format (A,E8.1) write (*,999) x 999 format ('The answer is x = ', F8.3) Output: x= 0.0250000 x=0.025 x= 0.2500000E-01 x= 0.3E-01 The answer is x= 0.250

  19. FILE I/O • open statement (no need to specific read/write at open) • open(unit=15,file=‘input1.dat’,status=‘old’) • open(unit=16,file=‘output.dat’,status=‘new’) • open(11,file=‘newinput.dat’) • open(12,file=‘newoutput.dat’)

  20. Write statement: • write(unit number,label) variable list • open(11,file=‘output.dat’,status=‘new’) • write(11,130) 'x=', x • 130 format (A,E8.1) • output.dat • x= 0.3E-01

  21. Read statement: • read (unit number, label) variable list • open(10,file=‘input.dat’,status=‘old’) • read(10,*) x,y • input.dat • 10.0 20.0

  22. Source code format • Data type • Simple I/O • Control statement • if • block if • if-else • if-else if • do • go to

  23. logical expression executable statement • if (logical expression) executable statement • logical expression = .true. executes statement • logical expression = .false. do not execute statement false true

  24. if (x .LT. 0) x = -x ! Find the absolute value of x if (weight.gt.20) count=count+1 program checknumber write(*,*) ‘input number’ read(*,*) n If (n.lt.0) write(*,*) ‘negative number’ If(n.eq.0) write(*,*) ‘value equal to 0’ If(n.gt.0) write(*,*) ‘positive output’ stop end

  25. if (logical expression) then statements endif if ( grade.gt.1.67) then sumh=sumh+hour sumg=sumg+grade course=course+1 endif gpa=sumg/sumh statements

  26. If (logical expression ) then statement 1 statement 2 … statement m … else statement a statement b statement c … endif statement group a statement group b

  27. logical expression false true statement group B statement group A

  28. numpos=0 numneg=0 assign x value … If (x.gt.0) then y=sqrt(x)*sin(x) sum=sum+x numpos=numpos+1 else y=log(abs(x)) sum=sum+abs(x) numneg=numneg+1 endif

  29. if (logical expression) then statements else if (logical expression) then statements else if (logical expression) then statements : : else statements endif block 1 block 2 block 3

  30. T block 1 condition 1 T block 2 condition 2 T condition 3 block 3 T block n condition n else block F

  31. Multiprocessor parallel program computer node selection C multiway selection if(myrank.eq.0) then write(*,*) ‘start job on node 0’ … else if(myrank.eq.1) then write(*,*) ‘start job on node 1’ … else if(myrank.eq.2) then write(*,*) ‘start job on node 2’ … else if(myrank.eq.n-2) then write(*,*) ‘start job on node n-2’ else write(*,*) ‘start job on node n-1’ endif

  32. Nested if if (x .GT. 0) then if (x .GE. y) then write(*,*) 'x is positive and x >= y' else write(*,*) 'x is positive but x < y' endif elseif (x .LT. 0) then write(*,*) 'x is negative' else write(*,*) 'x is zero' endif

  33. do label var = expr1, expr2, expr3 statements label continue var: integer exp1: initial value of var exp2: terminating bound exp3: increment step do var = expr1, expr2, expr3 statements enddo popular exp3: default is 1

  34. integer i, n, sum sum = 0 do 10 i = 1, n !do i=1,n sum = sum + i write(*,*) 'i =', i write(*,*) 'sum =', sum 10 continue !enddo integer i do 20 i = 10, 1, -2 write(*,*) 'i =', i 20 continue

  35. Nested loop do index1=1,nmax do index2=1,mmax statements enddo !enddo for index2 enddo !enddo for index1 Typically used in the multiple dimension array manipulation

  36. while (logical expr) do statements enddo i = 1 sum=0 while (i.lt.100) do sum = sum + i i = i+2 enddo

  37. go to n unconditional branch Better to use in do/while loop … do i=1,200 sum=sum+i if(sum.gt.100) goto 20 enddo 20 write(*,*) sum …

  38. n CONTINUE Used to serve as the end of a DO loop n is the statement Example do 9 I=1,100,2 do 8 j=1,4 … 8 continue ... 9 continue CONTINUE

  39. Control statement • if • block if • if-else • if-else if • do • go to • Arrays • Subprograms • Function • Subroutine

  40. One dimensional Array real a1,a2,a3,…a20 real a(20) default, index starts from 1 to 20 a(1) a(2) a(17) a(18) a(19) a(20)

  41. real var(lowerbound:upperbound) • lowerbound < upperbound • bounds must be integer numbers real b(0:19), weird(-162:237) Number of element 20 400 real a(-5:10) a(-5),a(-4),…a(0),a(1),…a(10) a(-5) a(-4) a(7) a(8) a(9) a(10)

  42. 100 real temp(-100:100) open(1,file=‘temp.dat’,status=‘old’) do i=-100,100 read(1,*) temp(i) enddo stop end temp.dat 20 22 . . . 29 Read (1,*)(temp(j), j=-100,100) -100 Temp.dat 20 22 … 29

  43. Two dimensional array --> no pointer real image(200,300),coord(-200:20,-40:200) Fortran stores array by column! data statement data list of variable names /list of values/ data a,b,c/2.3,2.1,1.5/ real a(2,3) data a/1.0,2.0,3.0,4.0,5.0,6.0/ 1 3 5 2 4 6

  44. 3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2 5.5 6.70 real y(4,6) read(*,*) y write(*,*) y stop end Input line: 3.4 10.9 7.1 32.7 23.1 … 2.1 8.8 21.6 6.7 Output : 3.4 10.9 7.1 32.7 23.1 3.1 43.2 75.2 4.0 1.7 12.1 43.2 11.2 1.5 23.0 3.2 3.4 12.1 12.1 5.5 2.1 8.8 21.6 6.7

  45. real y(4,6) do i=1,4 read(*,*) ( y(i,j) , j=1,6) enddo do i=1,4 write(*,*) (y(i,j),j=1,6) enddo stop end inputline: 3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2 5.5 6.70 Output: same as inputline

  46. real y(4,6) read(*,*) (( y(i,j) , j=1,6),i=1,4) write(*,2) (( y(i,j) , j=1,6),i=1,4) format(1x,6f7.1) stop end 2 Intputline: 3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2 5.5 6.70 3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2 5.5 6.70 Outputline:

  47. three dimensional array and multiple dimensional array real a(10,20,30), ab(10,20,30,40) A(j,k,m) j varies most rapidly, k next, m most slowly A(1,1,1), A(2,1,1),…,A(10,1,1) A(1,2,1), A(2,2,1),... real temp(10,10,10) temp(5,5,5)= 21.4 temp(1,2,3)= 20.0 1 2 3 4 5 6 7 8 9 10

  48. Subprogram: • Function • One return value • Subroutine • May have multiple return values

  49. type function name (list-of-variables) declarations statements name=… return end • Functions have a type. This type must also be declared in the calling program. • The return value should be stored in a variable with the same name as the function. • Functions are terminated by the return statement instead of stop • type – integer, complex, real, …

  50. C This is the function function sum(w) real w(3,4) sum=0 do j=1,3 do i=1,4 sum=sum+w(i,j) enddo enddo return end C This is the main program dimension w(3,4) read(*,*) w write(*,*) ‘sum is ‘, sum(w) stop end Inputline: 3.2 1.2 5.4 3.2 9.0 6.5 2.1 12.2 32.1 45.3 1.2 1.2 Output: sum is 122.50

More Related