Fortran Tutorial Fortran 77 program progname 7 72

  • Slides: 11
Download presentation
Fortran Tutorial Fortran 77 program progname ! ! 7 -72 spaces for code Comments

Fortran Tutorial Fortran 77 program progname ! ! 7 -72 spaces for code Comments are preceded by “!” or “C” qqqqqq. CODE implicit none C C Must leave six spaces for “statement label”. Data initialization to follow… integer : : i, j, k, n, m real : : x, y, z ! integer, dimension(3) : : larray real, dimension(3) : : rvec, forces ! real, dimension(0: 3, 8) : : tensor Single precision (kind=4) Double precision (kind=8) ! character(len=6) : : name ! Comment. ! logical : : some_flag, another_flag ! complex : : phase, factor ! real, parameter : : four = 4. 0 E 0 ! ! =================== Beyond this point expressions are allowed. Comments can be added at the end of a line.

Fortran Tutorial ! ! ! Data initiation continued. . . x = 1. 50

Fortran Tutorial ! ! ! Data initiation continued. . . x = 1. 50 E 0 y = 10. 0 E 0 do i=1, 3 rvec(1) = 0. 100 E 0 rvec(2) = 0. 000 E 0 rvec(3) = 0. 750 E 0 enddo ! forces(i) = 0. 0 E 0 ! forces(1: 3) = 0. 00 E 0 ! phase = (2. 0 E 0, 1. 25 E 0) ! tensor(0, : ) = 0. 0 E 0 tensor(1: 3, : ) = 3. 0 E 0 ! name = ‘PRESS’ some_flag =. true. another_flag =. false. ! do i=1, 3 do j=1, 3 tensor(i, j) = 3. 0 E 0 enddo

Fortran Tutorial ! ! ! Conditional statements if (i == 3) statement if (i

Fortran Tutorial ! ! ! Conditional statements if (i == 3) statement if (i > 4 ) statement if (i <= 2 ) statement ! if (x >= 0. 0 E 0 ) then statement endif ! if ( a > x. and. b < y ) then if ( c == 0. 0 E 0 ) then statement else if ( c > 0. 0 E 0 ) then statement else statement endif ! if (some_flag) then. . . if (. not. another_flag) then. . . ! if (name == ‘PRESS’ ) then. . . ! F 90 F 77 Operation == . eq. Equal to /= . ne. Not equal to > . gt. Greater than >= . ge. Greater or equal to < . lt. Less than <= . le. Less than or equal to

Fortran Tutorial ! ! ! Loop statements do i=1, 10 statement do i=1, 3

Fortran Tutorial ! ! ! Loop statements do i=1, 10 statement do i=1, 3 statement enddo if (condition) goto 10 statement ! do i=1, n-1 do j=i+1, n statement enddo ! do i=1, 10 statement if (force(i) > 10. 0 E 0) exit statement enddo ! do statement if (condition) break enddo ! enddo 10 continue

Fortran Tutorial ! ! ! Loop statements (continued) do i=1, n, 3 statement enddo

Fortran Tutorial ! ! ! Loop statements (continued) do i=1, n, 3 statement enddo ! ! ! While loop. . . i = 0 do while (i<n) statement i = i + 1 enddo ! ! Increment by 3

Fortran Tutorial ! ! ! Fortran 77 Expressions x = exp( sqrt( x**2 +

Fortran Tutorial ! ! ! Fortran 77 Expressions x = exp( sqrt( x**2 + y**2 + x = a * b / c ! & x = a**3 * b + c/d z**2) ) ! x = exp( sqrt( x**2 + y**2 + z**2) ) ! ! ! & Complex variables. . . phase = (1. 0 E 0, 1. 0 E 0) ! Equals 1+i phase = cmplx(a, b) ! Equals a+ib c = conjg(phase) ! Equals a- ib ! ! ! a = real(phase) b = aimag(phase) ! ! ! Floating points & integers. . . i = 3. 45 E 0 i = -2. 76 E 0 ! ! Equals 3 ! Equals – 3 Good practice. . . i = int( real(j)*force(k)/2. 0 E 0 ) ! Continuation symbol at space #5

Fortran Tutorial ! ! ! Subroutines. . . call myroutine(a, b, 3, x, y,

Fortran Tutorial ! ! ! Subroutines. . . call myroutine(a, b, 3, x, y, z, flag) ! stop end ! -----------------subroutine myroutine(a, b, k, x, y, z, root) ! implicit none real : : a, b, x, y, z integer : : k logical : : root ! integer : : i ! a = (x**2+y**2+z**2) do i=1, k a = a + b**I enddo ! if (root) a = sqrt(a) ! return end subroutine !------------------- End of program Begin the subroutine Return control back to calling program or routine End the routine

Fortran Tutorial ! ! ! User functions. . . integer : : n, x,

Fortran Tutorial ! ! ! User functions. . . integer : : n, x, factorial . . x = factorial(n). stop end ! -----------------function factorial(n) ! implicit none integer : : factorial, n ! integer : : i ! if (i<0) return(0) if (i=0) return(1) ! factorial = 1 do i=1, n factorial = factorial * i enddo ! return end function !-------------------

Fortran Tutorial ! ! ! Output. . . write(*, *) ‘This is a tutorial.

Fortran Tutorial ! ! ! Output. . . write(*, *) ‘This is a tutorial. ’ write(*, *) ‘The answer is ‘, i floating point n spaces long, d spaces an character string n spaces long. en. d exponential n spaces long, d spaces esn. d scientific n spaces long, d spaces after the decimal point. (n>=d+7) write(*, 100) i format(‘This is a tutorial. ’, /, ‘The answer is ‘, i 3) & ! 200 fn. d after the decimal point. (n>=d+7) Fixed format -> 100 integer n spaces long. after the decimal place. Free format -> ! ! ! in write(*, 200) (force(i), I=1, 3) format(/, ’Particle force = ‘, 3 f 4. 1) ln logical n spaces long. x space. t tab. ! 300 ! name = ‘alpha’ do i=1, n write(*, 300) name, i, x, y, z enddo format(‘ data set ‘, a 5, ’: iter = ‘, & i 3, ’, force = ‘, 3(x, f 4. 1)) Particle force = 1. 0 0. 4 1. 8 data data set set set alpha: alpha: iter iter = = = 1, 2, 3, 4, 5, force force = = = 10. 1 1. 2 – 2. 9 1. 4 0. 1 – 4. 3 2. 3 0. 3 2. 5 -2. 1 1. 2 2. 8 -3. 9 **** – 0. 2

Fortran Tutorial ! ! ! External files. . . open(unit=6, file=‘ myfile’, & form=‘formatted’,

Fortran Tutorial ! ! ! External files. . . open(unit=6, file=‘ myfile’, & form=‘formatted’, status=‘old’) ! ! ! File with I/O unit=6 is now open. Status clause (possible values) ‘old’, ’new’, ’unknown’, ‘scratch’, ’replace’ Unformatted read---> read(6, *) a, b, c ! These are read(6, *) x, y, z ! on separate read(6, *) (force(i), i=1, 3) ! lines. ! ! ! form clause (possible values) Formatted read---> 100 ‘formatted’, read(6, 100) i, j, name format(2 x, i 3, 3 x, a 6) ‘unformatted’ ! close(unit=6) ! Close file. ! ! -------------------! Write to files. . . ! open(unit=7, file=‘output’, & form=‘formatted’, status=‘new’) ! write(7, 200) name, x, y, z 200 format(2 x, a 6, ’ data -> ‘, 3(2 x, f 5. 2)) ! close(unit=7) !

Fortran Tutorial COMPILING. . . -> f 90 –o executable program. f Linking many

Fortran Tutorial COMPILING. . . -> f 90 –o executable program. f Linking many files -> f 90 –c code 1. f -> f 90 –c code 2. f -> f 90 –o executable code 1. o code 2. o program. f Example: -> -> f 90 –c code 1. f f 90 –c code 2. f f 90 –o prog. x code 1. o code 2. o program. f prog. x Run the program To run in “background” -> prog. x & -> Code will run in background allow you use other commands. Check the status of a run -> ps –a Kill a run -> kill – 9 6445 Include process ID (from”ps –a”)