Document 12820974

advertisement
ORTRAN 90
Lecturer : Rafel Hekma
att Hameed
University of Babylon
Subject : Fortran 90
College of Engine
ering
ee
Year : Second B.Sc.
Mechanical Engineering Dep.
FORMAT Statement
 Horizontal Spacing
The general form of this descriptor is as follows:
nX
 The next n positions are skipped. The X edit descriptor can be used for
both input and output. For output, the next n positions are skipped and
the content there is unchanged.
Consider the following WRITE statement:
INTEGER
:: a = 12
INTEGER
:: b = 768
REAL
:: c = 3.715
WRITE(*,20
20) a, b, c
20 FORMAT((1X,I3,3X,I5,2X,F5.2)
ϭ
 Tabbing
Edit descriptors Tc, TLc and TRc provide a way of moving to a
specific positions on the current input or output line. More importantly, they
can move backward and forward. The general forms are
Tc, TLc and TRc

Tc moves to position c, TLc move backward c positions, and TRc
moves forward c positions.
INTEGER :: a = 123, b = 789
WRITE(*,"(1X,I5,T10,I5)") a, b
1
2
3
1
2
3
4
5
6
INTEGER
7
8
9
10
11
7
8
9
12
13
14
15
:: a = 123, b = 456, c = 789
WRITE(*,"(T10,I3,TL9,I3,TR5,I3)") a, b, c
1
2
3
4
5
6
4
5
6
7
8
9
1
2
7
8
9
10
11
12
13
14
15
 Special Characters
There are a number of other characters which control the format of data;
most are used in WRITE( ) statements only.

’ ’ to output the character string specified.

/ specifies take a new line.
For example:
Ϯ
INTEGER :: value = 100
INTEGER :: a=101, b=201
...
WRITE(*,’( ’The value is’, 2X, I3, ’ units.’)’) value
WRITE(*,’( ’a =’, 1X, I3, /, ’b = ’, 1X, I3)’) a,b
WRITE(*,’( ’a and b =’, 2(1X, I3) )’) a, b
t h e
v a l
a =
1 0 1
b =
2 0 1
a
a n d
u e
b =
i
s
1 0 1
1 0 0 u n i
t s .
2 0 1
Example
Write a program to calculate the mass of air contained in a room of 6m
10m  4m if the pressure 100 kpa and the temperature is 25 oC? Assume air
to be an ideal gas. Print your results in output file, and use formatting for print
your result.
Note: PV=mRT ,R=0.287 kNm/kg K
PROGRAM CALCULATE_MASS_OF_AIR
REAL, PARAMETER :: R=0.287
REAL:: P,V,T,M
READ(*,*) P,V,T
OPEN(UNIT=4,FILE='MASS.DAT')
! CONVERT TEMPERATURE FROM CELSIUS TO KELVIN
T=T+273.15
M=(P*V)/(R*T)
WRITE(4,20) M
20 FORMAT (2X,"MASS OF AIR=",1X,F7.3)
END
ϯ
Example
Write a program to read the launch angle a, the time since launch t, and
the launch velocity u, and compute the position, the velocity and the angle
with the ground. Use format statement to print your results.
Note:
The horizontal and vertical displacements, x and y, are computed as
follows:
x = u × cos(a) × t
y = u × sin(a) × t – g.t2/2
The horizontal and vertical components of the velocity vector are computed
as:
Vx = u × cos(a) x
Vy = u × sin(a) − g×t
The magnitude of the velocity vector is
V=(V2x+V2y).5
The angle between the ground and the velocity vector is
tan(è ) = Vy / Vx
PROGRAM PROJECTILE
IMPLICIT NONE
REAL, PARAMETER :: g = 9.8
REAL, PARAMETER :: PI = 3.1415926
REAL :: Angle , Time, U ,V,Vx,Vy,X,Y
REAL :: Theta
! direction at time in degree
READ(*,*) Angle, Time, U
Angle = Angle * PI / 180.0
! convert to radian
X = U * COS(Angle) * Time
Y = U * SIN(Angle) * Time – (g*Time**2)/ 2.0
Vx = U * COS(Angle)
Vy = U * SIN(Angle) - g * Time
V = SQRT(Vx*Vx + Vy*Vy)
Theta = ATAN(Vy/Vx) * 180.0 / PI
! convert to degree
WRITE(*,30) X,Y,V, Theta
30 format(1x, 'Horizontal displacement :',2x,F7.3,/,1x, 'Vertical&
&displacement
:
',2x,F7.3,/,1x,
'Resultant
velocity
',2x,F6.3,/,1x,'Direction(in degree) : ',2x,F6.3)
END
ϰ
Download