FORTRAN-77. Part 2
Document Sample


• Program full of "errors"
• program errors
• double precision x
• integer i
• real CalLF(10)
•
• i = 1
• x = 1
•
• CalLF(i) = x
•
• 20 continue
• do 10 i = 1.10
• x = x + 1
• 10 continue
• call dp(x)
•
• goto 20
•
•
• 1 = call f(x)
•
• end
•
• subroutine dp(x)
• double precision x
•
• write(*,*)'dp. x=',x
• return
• end
•
• subroutine f(x)
• double precision x
•
• write(*,*)'I am subroutine f'
• write(*,*)'x=',x
• return
• end
• Changing constant
• program add test
• implicit none
• integer x,y
• integer one
• parameter (one = 1)
•
• x = 1
• write(*,*)'x=',x
• call add(x)
• write(*,*)'new x=',x
•
• call add(one)
•
• end
•
•
• subroutine add(x)
• implicit none
• integer x
•
• x = x + 1
• end
• Arrays in memory
• program arrays in memory
• implicit none
• integer n,m,p
• parameter (n=2, m=2)
• double precision a(n,m)
•
• p = n*m
• a(1,1) = 1
• a(1,2) = 2
• a(2,1) = 3
• a(2,2) = 4
• call arrprt(a,p)
• end
•
• subroutine arrprt(x,p)
• implicit none
• integer p
• double precision x(p)
• integer i
•
• do i=1,p
• write(*,*)i,x(i)
• end do
• return
• end
• Machine epsilon.
The smallest floating-point number eps such that:
1 + eps > 1
Write a program which calculates machine epsilon for single (real)
and double precision.
1. eps = 1
2. eps = eps/2
3. if 1 + eps > 1, repeat 2
Make two functions:
real function eps()
double precision function deps()
1. File I/O
o Opening and closing a file
open(list-of-specifiers)
[UNIT=] u number (1 - 99, but usually can be much more)
IOSTAT= ios Input/Output status, integer variable. Upon return, ios is
zero if the statement was successful and returns a non-zero
value otherwise
ERR= err is a label which the program will jump to if there is an error
FILE= fname is a character string denoting the file name
STATUS= sta is a character string that has to be either NEW, OLD or
SCRATCH. It shows the prior status of the file. A scratch file
is a file that is created when opened and deleted when closed
(or the program ends).
ACCESS= acc must be either SEQUENTIAL or DIRECT. The default is
SEQUENTIAL.
FORM= frm must be either FORMATTED or UNFORMATTED. The default is
FORMATTED.
RECL= rl rl specifies the length of each record in a direct-access
file.
close([UNIT=]u[,IOSTAT=ios,ERR=err,STATUS=sta])
In this case sta is a character string which can be KEEP
(the default) or DELETE
o Read and write
o read ([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err,
END=s)list-of-variables
o write([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err,
END=s)list-of-variables
The ERR=err specifier defines which statement label the
program jumps to if an error occurs
The END=s specifier defines which statement label the
program jumps to if it reaches end-of-file
Note: Some unit numbers are reserved: 5 is standard input, 6
is standard output.
2. Format statements
o Syntax
o label format(format-code)
o Example:
900 format (I4,F8.3)
o Common format codes
o
A
text string
D
double precision numbers, exponent notation
E
real numbers, exponent notation
F
real numbers, fixed point format
I
integer
X
horizontal skip (space)
/
vertical skip (newline)
o
o Example:
o
o program io_example
o implicit none
o double precision t,x,y
o integer i,j,k
o
o t=1.0d0
o i=1
o write(*,100)t,i,j,y
o 100 format(1x,E15.6,2I3,' x=',f15.9)
o read(*,*)i,j,x
o open(9,file='data.dat',status='old')
o read(*,101)i,j,k
o write(*,*)i,j,k
o close(9)
o 101 format(i5,i2,i2)
o end
o
o
o
o
3. Unformatted input/output
o Form='unformatted'
o read/write - without format
Example:
c-------------------------------------
program rw
implicit none
double precision x,y,z
double precision u,v,t
integer ierr
x = 1.0d0
y = 2.0d0
z = 3.0d0
u = 4.0d0
v = 5.0d0
t = 6.0d0
open(9,file='temp.dat',form='unformatted',status='unknown')
write(9)x,y,z
write(9)u,v,t
close(9)
x = 0.0
y = 0.0
z = 0.0
u = 0.0
v = 0.0
t = 0.0
open(9,file='temp.dat',form='unformatted',status='old')
read(9)x,y
read(9)u,v,t
close(9)
write(*,*)'x,y,z:',x,y,z
write(*,*)'u,v,t:',u,v,t
x = 0.0
y = 0.0
z = 0.0
u = 0.0
v = 0.0
t = 0.0
open(9,file='temp.dat',form='unformatted',status='old')
read(9,err=10,iostat=ierr)x,y,z,u
read(9)v,t
close(9)
stop
10 continue
write(*,*)'!!!! Reading error !!!'
write(*,1)ierr
write(*,*)'x,y,z:',x,y,z
write(*,*)'u,v,t:',u,v,t
close(9,status='delete')
1 format(1x,'IOSTAT=',i3)
end
c-------------------------------------
4. Format strings in read/write statements
Instead of specifying the format code in a separate format statement,
one can give the format code in the read/write statement directly. For
example, the statement
write (*,'(A, F8.3)') 'The answer is x = ', x
c is equivalent to
write (*,990) 'The answer is x = ', x
990 format (A, F8.3)
5. Implicit loops and repeat counts
6. program implicit_loop
7. implicit none
8. integer n,i,j
9. parameter (n=10)
10. double precision x(n),pi
11.
12. pi=4.0d0*datan(1.0d0)
13. do i=1,n
14. x(i) = sin((i-1)/(n-1.0)*pi)
15. end do
16. write(*,'(f6.3)')(x(j),j=1,n)
17. end
18. Exercise
Write your Fortran program which reads an input file and calculates
expression (x2+y2)1/2 in three different ways:
1. Using a following equation: z1=(x2+y2)1/2
2. Using an equation with sqrt: z2 = sqrt(x2+y2))
3. Unwing a function which you need to write: z3=pythag(x,y).
Algorithm for pythag function:
1. If abs(x) > abs(y) => abs(x)*sqrt(1+(abs(y)/abs(x))2)
2. If abs(y) = 0 => 0
3. Else abs(y)*sqrt((1+(abs(x)/abs(y))2)
Input file:
1.00 2.00
2.00 1.00
0.00 1.00
1.00 0.00
1.0e-260,2.0e-260
1.0e160, 2.0e160
Make the output file in teh following format:
o Column 1: x
o Column 2: y
o Column 3: z1
o Column 4: z2
o Column 5: z3
o Column 6: err1=abs((z1-z3)/z3)
o Column 7: err2=abs((z2-z3)/z3)
Related docs
Get documents about "