120 likes | 289 Views
Fortran Tutorial. Fortran 77. program progname ! ! Comments are preceded by ! “!” or “C” ! implicit none C C Data initialization to C follow… C integer :: i,j,k,n,m real :: x,y,z ! integer, dimension(3) :: larray
E N D
Fortran Tutorial Fortran 77 program progname ! ! Comments are preceded by ! “!” or “C” ! implicit none C C Data initialization to C follow… C 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 ! character(len=6) :: name ! Comment. ! logical :: some_flag,another_flag ! complex :: phase,factor ! real, parameter :: four = 4.0E0 ! ! ===================================== ! Beyond this point expressions are ! allowed. 7-72 spaces for code qqqqqqCODE Must leave six spaces for “statement label”. Single precision (kind=4) Double precision (kind=8) Comments can be added at the end of a line.
Fortran Tutorial ! ! Data initiation continued... ! x = 1.50E0 y = 10.0E0 ! rvec(1) = 0.100E0 rvec(2) = 0.000E0 rvec(3) = 0.750E0 ! forces(1:3) = 0.00E0 ! phase = (2.0E0,1.25E0) ! tensor(0,:) = 0.0E0 tensor(1:3,:) = 3.0E0 ! name = ‘PRESS’ some_flag = .true. another_flag = .false. ! do i=1,3 forces(i) = 0.0E0 enddo do i=1,3 do j=1,3 tensor(i,j) = 3.0E0 enddo enddo
Fortran Tutorial ! ! Conditional statements ! if (i == 3) statement if (i > 4 ) statement if (i <= 2 ) statement ! if (x >= 0.0E0 ) then statement statement statement endif ! if ( a > x .and. b < y ) then if ( c == 0.0E0 ) then statement statement else if ( c > 0.0E0 ) then statement statement else statement statement endif endif ! if (some_flag) then ... if (.not.another_flag) then ... ! if (name == ‘PRESS’ ) then ... ! F90 F77 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,3 statement statement enddo ! do i=1,n-1 do j=i+1,n statement statement statement enddo enddo ! do i=1,10 statement if (force(i) > 10.0E0) exit statement enddo ! do statement statement if (condition) break enddo ! do i=1,10 statement if (condition) goto 10 statement enddo 10 continue
Fortran Tutorial ! ! Loop statements (continued) ! do i=1,n,3 ! Increment by 3 statement statement enddo ! ! While loop... ! i = 0 do while (i<n) statement statement i = i + 1 enddo !
Fortran Tutorial ! ! Expressions ! x = a * b / c ! x = a**3 * b + c/d ! x = exp( sqrt( x**2 + y**2 + & z**2) ) ! ! Complex variables... ! phase = (1.0E0,1.0E0) ! 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.45E0 ! Equals 3 i = -2.76E0 ! Equals –3 ! ! Good practice ... ! i = int( real(j)*force(k)/2.0E0 ) ! Fortran 77 x = exp( sqrt( x**2 + y**2 + & z**2) ) Continuation symbol at space #5
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,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 in integer n spaces long. fn.d floating point n spaces long, d spaces after the decimal place. an character string n spaces long. en.d exponential n spaces long, d spaces after the decimal point. (n>=d+7) esn.d scientific n spaces long, d spaces after the decimal point. (n>=d+7) ln logical n spaces long. x space. t tab. ! ! Output... ! ! Free format -> ! write(*,*) ‘This is a tutorial.’ write(*,*) ‘The answer is ‘,i ! ! Fixed format -> ! write(*,100) i 100 format(‘This is a tutorial.’,/, & ‘The answer is ‘,i3) ! write(*,200) (force(i),I=1,3) 200 format(/,’Particle force = ‘,3f4.1) ! name = ‘alpha’ do i=1,n write(*,300) name,i,x,y,z enddo 300 format(‘ data set ‘,a5,’: iter = ‘, & i3,’, force = ‘,3(x,f4.1)) ! Particle force = 1.0 0.4 1.8 data set alpha: iter = 1, force = 10.1 1.2 –2.9 data set alpha: iter = 2, force = 1.4 0.1 –4.3 data set alpha: iter = 3, force = 2.3 0.3 2.5 data set alpha: iter = 4, force = -2.1 1.2 2.8 data set alpha: iter = 5, force = -3.9 **** –0.2
Fortran Tutorial ! ! External files... ! open(unit=6,file=‘myfile’, & form=‘formatted’,status=‘old’) ! ! File with I/O unit=6 is now open. ! ! Unformatted read---> ! read(6,*) a,b,c ! These are read(6,*) x,y,z ! on separate read(6,*) (force(i),i=1,3) ! lines. ! ! Formatted read---> ! read(6,100) i,j,name 100 format(2x,i3,x,i3,3x,a6) ! 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(2x,a6,’ data -> ‘,3(2x,f5.2)) ! close(unit=7) ! Status clause (possible values) ‘old’,’new’,’unknown’, ‘scratch’,’replace’ form clause (possible values) ‘formatted’, ‘unformatted’
Fortran Tutorial COMPILING... -> f90 –o executable program.f Linking many files -> f90 –c code1.f -> f90 –c code2.f -> f90 –o executable code1.o code2.o program.f Example: -> f90 –c code1.f -> f90 –c code2.f -> f90 –o prog.x code1.o code2.o program.f -> prog.x To run in “background” -> prog.x & -> Check the status of a run -> ps –a Kill a run -> kill –9 6445 Run the program Code will run in background and allow you use other commands. Include process ID (from”ps –a”)