Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fortran Tutorial Fortran spaces for code qqqqqqCODE

Similar presentations


Presentation on theme: "Fortran Tutorial Fortran spaces for code qqqqqqCODE"— Presentation transcript:

1 Fortran Tutorial Fortran 77 7-72 spaces for code qqqqqqCODE
program progname ! ! Comments are preceded by ! “!” or “C” implicit none C C Data initialization to C 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 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.

2 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

3 Fortran Tutorial ! ! Conditional statements if (i == 3) statement
if (x >= 0.0E0 ) then statement endif if ( a > x .and. b < y ) then if ( c == 0.0E0 ) then else if ( c > 0.0E0 ) then else if (some_flag) then ... if (.not.another_flag) then ... if (name == ‘PRESS’ ) then ... F F 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

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

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

6 Fortran Tutorial Fortran 77 Continuation symbol at space #5 !
! 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.45E ! Equals 3 i = -2.76E ! 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

7 Fortran Tutorial End of program Begin the subroutine
! ! 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

8 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 !

9 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 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 = data set alpha: iter = 1, force = –2.9 data set alpha: iter = 2, force = –4.3 data set alpha: iter = 3, force = data set alpha: iter = 4, force = data set alpha: iter = 5, force = **** –0.2

10 Fortran Tutorial Status clause (possible values)
! ! 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’

11 Fortran Tutorial Run the program
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 –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”)


Download ppt "Fortran Tutorial Fortran spaces for code qqqqqqCODE"

Similar presentations


Ads by Google