170 likes | 265 Views
Scientific Programming. Class 14. Today’s Assignment:.
E N D
Scientific Programming Class 14
Today’s Assignment: • The most common model for a reciprocating engine is the slider-crank mechanism shown. Dimensions of a representitive crank are shown. If the crank OB has a constant clockwise1500 RPM rotational speed, the piston will reach its maximum velocity between 0 < q < 180o. Write a Fortran Program that will write out the maximum velocity and the crank angle, q, at which it occurs. • VA = OB SIN(q) (1 + (OB/AB) COS(q) / (1 - (OB/AB)2 SIN2(q))1/2) w
Program: • PROGRAM slidercrank • IMPLICIT NONE • REAL:: ob,ab,angle,angleinc,radian,radianinc,omega,v,va,vb • ob = 5. • ab = 14. • omega = 1500.*2.*(4.*ATAN(1.))/60. • angle = 0. • angleinc = 1. • radianinc = angleinc*(4.*ATAN(1.))/180. • WRITE(*,3) 3 FORMAT(12x,'angle',14x,'max speed',/,12x,'(deg)',14x,'(in/sec)') • DO • radian = angle*(4.*ATAN(1.))/180. • v = ob*SIN(radian)*(1 + (ob/ab)*COS(radian)/(SQRT(1 - ((ob/ab)**2)*((SIN(radian))**2))))*omega • va = ob*SIN(radian-radianinc)*(1 + (ob/ab)*COS(radian-radianinc)/(SQRT(1 - ((ob/ab)**2)*((SIN(radian-radianinc))**2))))*omega • vb = ob*SIN(radian+radianinc)*(1 + (ob/ab)*COS(radian+radianinc)/(SQRT(1 - ((ob/ab)**2)*((SIN(radian+radianinc))**2))))*omega • IF (v>va.AND.v>vb)THEN • WRITE(*,5)angle, v 5 FORMAT(10X,F6.0,12X,F10.2) • ELSE • END IF • IF (angle>=180.) EXIT • angle = angle + angleinc • END DO • END
Program Results: • angle max speed • (deg) (in/sec) • 72. 834.61 • The tolerance on the angle is +- 1o.
Bonus Exercises: • One to three bonus points will be awarded for each correct answer to the question posed on each of the following slides. • Each exercise will be left on the screen for 5 minutes.
Bonus Exercise 1: • Describe each of the following Combinational Logical Operators • L1.AND.L2 • L1.OR.L2 • L1.EQV.L2 • L1.NEQV.L2 • NOT.L1
Bonus Exercise 2: • Describe the operation of the following IF Block. • IF (logical expression 1) THEN • Statement 1 • Statement 2 • ….. • ELSE IF (logical expression 2) THEN • Statement 3 • Statement 4 • ….. • ELSE • Statement 5 • Statement 6 • ….. • END IF • Next line of code
Bonus Exercise 3 : • Describe the operation of the following • The While Loop • The DO WHILE Loop • The Iterative Loop • CYCLE Statement • EXIT Statement
New Stuff – Selected Topics • Formatted Read Statements • Creating, Writing To & Reading From Files
Formatted Read Statements • Read(*,*) a, b, c • or, • Read(*,10) a, b, c • 10 Format(5X,3F10.2) • Input Buffer Line • * - Default Input Device – Keyboard • * - Default Output Device - Monitor
Creating A File On The Hard Drive • OPEN (list of clauses) • CLOSE (list of clauses) • Clauses: • UNIT= integer expression • FILE=‘character expression’ • IOSTAT=integer expression • ….. many, many more
Creating A File On The Hard Drive • OPEN (UNIT=iounit, FILE=‘filename’,IOSTAT=ierror) • CLOSE (UNIT=iounit) • In between opening & closing a file, you may read from & write to it using its unit number. • WRITE(iounit,5) • REWIND(UNIT=iounit, IOSTAT=ierror) • READ(iounit,5,IOSTAT=ierror) • 5 FORMAT
Creating A File On The Hard Drive • PROGRAM dummy • IMPLICIT NONE • REAL:: v, vs, a, r, g, mach, n • INTEGER:: iounit, ierror • iounit = 10 • OPEN(UNIT=iounit, FILE='radius‘, IOSTAT=ierror) • WRITE(*,5) • WRITE(iounit,5) • 5 FORMAT(//8X,'Aircraft Turning Radius') • WRITE(*,6) • WRITE(iounit,6) • 6 FORMAT(8X,'Mach No = .85 : V = 289.3 (m/s) : Vs = 340.3 (m/s)'/) • WRITE(*,8) • WRITE(iounit,8) • 8 FORMAT(8X,'Multiple of g',5X,'Acceleration (m/s2)',3X,'Turning Radius (m)') • g = 9.81 • mach = 0.85 • vs = 340.3 • v = mach*vs • n = 2. • DO • a = n*g • r = v**2/a • WRITE(*,10)n,a,r • WRITE(iounit,10)n,a,r • 10 FORMAT(10X,F6.2,9X,F12.3,9x,F12.3) • n = n + .5 • IF (n==8.5) EXIT • END DO • CLOSE(UNIT=iounit) • END
Contents of the file, “radius”, on the hard drive • Aircraft Turning Radius • Mach No = .85 : V = 289.3 (m/s) : Vs = 340.3 (m/s) • Multiple of g Acceleration (m/s2) Turning Radius (m) • 2.00 19.620 4264.447 • 2.50 24.525 3411.558 • 3.00 29.430 2842.965 • 3.50 34.335 2436.827 • 4.00 39.240 2132.224 • 4.50 44.145 1895.310 • 5.00 49.050 1705.779 • 5.50 53.955 1550.708 • 6.00 58.860 1421.482 • 6.50 63.765 1312.138 • 7.00 68.670 1218.414 • 7.50 73.575 1137.186 • 8.00 78.480 1066.112
Reading From A File On The Hard Drive • PROGRAM readwrite • IMPLICIT NONE • REAL:: x, y, z • INTEGER:: iounit, ierror • iounit = 9 • OPEN(UNIT=iounit,FILE='readfile',IOSTAT=ierror) • x = 2. • y = 4. • z = 9. • WRITE(iounit,10)x,y,z • REWIND(UNIT=iounit, IOSTAT=ierror) • 10 FORMAT(1X,3F5.1) • READ(iounit,*,IOSTAT=ierror)a,b,c • WRITE(*,*)a,b,c • CLOSE(UNIT=iounit) • END • 2.000000 4.000000 9.000000
Assignment: • Read and understand the material in this slide set • Search out information (internet or printed references) and examples on: • how to create files • how to write to them • how to read them