220 likes | 373 Views
Scientific Programming. Class 11. Formats – Input/Output (I/O). The first space (column) in a 133 column Fortran Output Line is reserved for a Format Control Character 1 New Page Blank Single Spacing 0 Double Spacing + No spacing (overprint)
E N D
Scientific Programming Class 11
Formats – Input/Output (I/O) • The first space (column) in a 133 column Fortran Output Line is reserved for a Format Control Character • 1 New Page • Blank Single Spacing • 0 Double Spacing • + No spacing (overprint) • The remaining 132 characters contain the output
Format Descriptor Symbols • c column number • d number of decimal places • m min # of digits to display • n number of spaces to skip • r repeat count • w field width
Format Descriptors • I Descriptor - rIw • r repeat count • I integer format • w field width (inc +-) • output - right justified • exceed w & **** results
Format Descriptors • F Descriptor – rFw.d • r repeat count • F real format • w field width (inc +-) • d # of decimal places • output - right justified • output is rounded, or • output is filled in w/ zeros • exceed w & **** results
Format Descriptors • E Descriptor – rEw.d • r repeat count • E exponential format • w field width (inc +-) • d # of decimal places • output - right justified • output is rounded, or • output is filled in w/ zeros • exceed w & **** results
Format Descriptors • ES Descriptor – rESw.d • r repeat count • ES sci exponential format • w field width (inc +-) • d # of decimal places • output - right justified • output is rounded, or • output is filled in w/ zeros • exceed w & **** results
Format Descriptors • X Descriptor – nX • n # of blank spaces to add in the output line • T Descriptor – Tc • c jump to column c in the output line • / Descriptor - /, //, ///,……/////// • Skip 1, 2, 3, or,…..7 lines
HW - Practice Assignment • Write Fortran statements that perform the operations described below. 1. Skip to a new page and print the title 'This is a test!' starting in column 25. 2. Skip a line, then display the values of i, j, and data1 in fields 10 characters wide. Allow two decimal points for the real variable. 3. Beginning in column 12, write out the string' The result is' followed by the value of result expressed to 5 significant digits in correct scientific notation. • Assume that real variables a, b, and c are initialized with -0.0001, 6.02 x 1023, and 3.141593 respectively, and that integer variables i, j, and k are initialized with 32767, 24, and -1010101 respectively. What will be printed out by each of the following sets of statements? 4. WRITE .(*,10) a, b, c • 10 FORMAT (lX, 3F10.4) 5. WRITE (*,20) a, b, c • 20 FORMAT (lX, F10.3, 2X, E10.3, 2X, F10.5) 6. WRITE (*,40) a, b, c • 40 FORMAT (lX,ES10.4, ES11.4,F10.4) 7. WRITE (*,13) i, j, k • 13 FORMAT(1X, I5) • Examine the following Fortran statement. Is it correct or incorrect? If it is incorrect, why? Assume default typing for variable names where they are not otherwise defined. 8. WRITE (*,20) istart, istop, step • 20 FORMAT(2I6, FI0.4)
Practice Assignment • Write Fortran statements that perform the operations described below. 1. Skip to a new page and print the title 'This is a test!' starting in column 25. 2. Skip a line, then display the values of i, j, and data1 in fields 10 characters wide. Allow two decimal points for the real variable. 3. Beginning in column 12, write out the string' The result is' followed by the value of result expressed to 5 significant digits in correct scientific notation. • 1 • WRITE(*,110) • 110 FORMAT(‘1’,T25,’This is a test’) or, • 110 FORMAT(‘1’,24X,’This is a test’) • 2 • WRITE(*,120) i, j, data1 • 120 FORMAT(‘0’,2I10,F10.2) or, • 120 FORMAT(‘b’,/,2I10,F10.2) • 3 • WRITE(*,130) result • 130 FORMAT(T12,’The result is’,ES12.4) or, • 130 FORMAT(12X,’The result is’,ES12.4)
Practice Assignment (cont.) • Assume that real variables a, b, and c are initialized with -0.0001, 6.02 x 1023, and 3.141593 respectively, and that integer variables i, j, and k are initialized with 32767, 24, and -1010101 respectively. What will be printed out by each of the following sets of statements? 4. WRITE (*,10) a, b, c • 10 FORMAT (1X, 3F10.4) 5. WRITE (*,20) a, b, c • 20 FORMAT (1X, F10.3, 2X, E10.3, 2X, F10.5) • 4 • WRITE(*,10) a, b, c • 10 FORMAT(1X,3F10.4) • bbbbb-.0001**********bbbb3.1416 • 5 • WRITE(*,20) a, b, c • 20 FORMAT(1X,F10.3,2X,E10.3,2X,F10.5) • bbbbbbb.000bbb0.602E+24bbbbb3.14159
Practice Assignment (cont.) • a = -.0001 b = 6.02 x 1023 c= 3.141593 • i = 32767 j = 24 k = -1010101 • 6 • WRITE(*,40) a, b, c • 40 FORMAT(1X,ES10.4,ES11.4,F10.4) • b**********b6.0200E+23bbbb3.1416 • 7 • WRITE(*,’(1X,I5)’) i, j, k • b32767 • bbbb24 • b*****
Practice Assignment (cont.) • Examine the following Fortran statement. Is it correct or incorrect? If it is incorrect, why? Assume default typing for variable names where they are not otherwise defined. 8. WRITE (*,20) istart, istop, step • 20 FORMAT(2I6, FI0.4) • 8 OK • WRITE(*,’(2I6,F10.4)’) istart, istop, step
Example • REAL:: a = 1.602E-19, b = 57.2957795, c = -1. • WRITE(*,’(1X, ES14.7, 2(1X,E13.7))‘) a, b, c • bb1.6020000E-19b0.5729578E+02b*************
Example • Prepare A Table of Base 10 Logarithms • PROGRAM logcal • IMPLICIT NONE • REAL:: y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,x • INTEGER:: i • WRITE(*,5) • 5FORMAT(//8X,'Base 10 Logarithm Table'/) • WRITE(*,8) • 8FORMAT(1X,7X,'X.0',4X,'X.1',4X,'X.2',4X,'X.3',4X,'X.4',4X,'X.5',4X,'X.6',4X,'X.7',4X,'X.8',4X,'X.9') • x = 1. • DO i = 1, 10 • y1 = LOG10(x) • y2 = LOG10(x+.1) • y3 = LOG10(x+.2) • y4 = LOG10(x+.3) • y5 = LOG10(x+.4) • y6 = LOG10(x+.5) • y7 = LOG10(x+.6) • y8 = LOG10(x+.7) • y9 = LOG10(x+.8) • y10 = LOG10(x+.9) • WRITE(*,10)x,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10 • 10FORMAT(1X,F4.1,10F7.3) • x = x+1. • END DO • END
Example (cont.) Base 10 Logarithm Table X.0 X.1 X.2 X.3 X.4 X.5 X.6 X.7 X.8 X.9 1.0 0.000 0.041 0.079 0.114 0.146 0.176 0.204 0.230 0.255 0.279 2.0 0.301 0.322 0.342 0.362 0.380 0.398 0.415 0.431 0.447 0.462 3.0 0.477 0.491 0.505 0.519 0.531 0.544 0.556 0.568 0.580 0.591 4.0 0.602 0.613 0.623 0.633 0.643 0.653 0.663 0.672 0.681 0.690 5.0 0.699 0.708 0.716 0.724 0.732 0.740 0.748 0.756 0.763 0.771 6.0 0.778 0.785 0.792 0.799 0.806 0.813 0.820 0.826 0.833 0.839 7.0 0.845 0.851 0.857 0.863 0.869 0.875 0.881 0.886 0.892 0.898 8.0 0.903 0.908 0.914 0.919 0.924 0.929 0.934 0.940 0.944 0.949 9.0 0.954 0.959 0.964 0.968 0.973 0.978 0.982 0.987 0.991 0.996 10.0 1.000 1.004 1.009 1.013 1.017 1.021 1.025 1.029 1.033 1.037
Example • What is the minimum field width, w, required to display the variable, length, in Fw.d format with d = 4 if, -10000 <= length <= +10000 • -10000.0000 to +10000.0000 • w = 11 for -10000 • w = 10 for 10000
Example • Ideal Gas Law: P V = n R T • P = Pressure (kPa) • V = Volume (L) • n = # of moles (mol) = 1.0 mol • 1 mol = 6.02 x 1023 molecules • T = Absolute Temperature (K) = 273 K & 373 K • R = Universal Gas Constant (8.314 L kPa/mol K)
Example (cont.) • Ideal Gas Law • PROGRAM igl • IMPLICIT NONE • REAL:: p,v,n,r,t • INTEGER:: i • WRITE(*,5) • 5 FORMAT(//8X,'Perfect Gas Law Table') • WRITE(*,6) • 6 FORMAT(8X,'n = 1 mol : T = 273 K'/) • WRITE(*,8) • 8 FORMAT(1X,7X,'Pressure (kPa)',7X,'Volume (Liters)') • n = 1. • t = 273. • r = 8.314 • p = 1. • DO i = 1, 10 • v = n * r * t/ p • WRITE(*,10)p, v • 10 FORMAT(8X,F7.3,10X,F12.3) • p = p+100. • END DO • END
Ideal Gas Law Perfect Gas Law Table n = 1 mol : T = 273 K Pressure (kPa) Volume (Liters) 1.000 2269.722 101.000 22.472 201.000 11.292 301.000 7.541 401.000 5.660 501.000 4.530 601.000 3.777 701.000 3.238 801.000 2.834 901.000 2.519 Perfect Gas Law Table n = 1 mol : T = 373 K Pressure (kPa) Volume (Liters) 1.000 3101.122 101.000 30.704 201.000 15.428 301.000 10.303 401.000 7.733 501.000 6.190 601.000 5.160 701.000 4.424 801.000 3.872 901.000 3.442 Example (cont.)
Mid – Term Exam • An in-class mid-term exam will be given on Tuesday, 2/26/08. The exam will not require a PC. • Class will not meet on 2/28/08
Assignment • Write a Fortran program that prints out a simple multiplication table for number combinations from 1.0 to 16.0 w/ increments of 1.0. • The table should have a heading. • Use formatting to make the columns & rows line up.