1 / 19

Scientific Programming

Scientific Programming. Class 17. Fortran Fact:.

gabe
Download Presentation

Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming Class 17

  2. Fortran Fact: • John W. Backus, the former IBM employee who led the development of Fortran, the first widely used programming language, that paved the way for modern software, died in March, 2007 at age 82 at his home in Ashland, Oregon, according to a 3/20/07 bulletin in the New YorkTimes. • The Fortran programming language, which was a huge leap forward in easing the creation of computer software, was first released in 1957. • Backus launched his research project at IBM four years earlier, assembling a diverse team of 10, including a chess wizard, a crystallographer and a cryptographer, said the Times. • Fortran came to dominate scientific and mathematical computing.

  3. Assignment: • In class exercise: • Create an array, numbers(i), that contains the elements in datafile. • Homework: • Redo today’s homework using the datafile array, numbers(i), for the necessary mathematics.

  4. PROGRAM arrayuse IMPLICIT NONE REAL,DIMENSION(1000):: numbers REAL:: max, min, avg, rms, hmean INTEGER:: iounit, ierror, i iounit = 10 max = 0. min = 1000. avg = 0. rms = 0. hmean = 0. OPEN(UNIT=iounit,FILE='datafile',IOSTAT=ierror) REWIND(UNIT=iounit,IOSTAT=ierror) DO i = 1, 1000 READ(iounit,*,IOSTAT=ierror)numbers(i) END DO DO i = 1, 1000 IF (numbers(i)>max)THEN max = numbers(i) ELSE IF (numbers(i)<min) THEN min = numbers(i) END IF avg = avg + numbers(i) rms = rms + numbers(i)**2 hmean = hmean + 1./numbers(i) END DO avg = avg/1000. rms = SQRT(rms/1000.) hmean = 1000./hmean WRITE(*,10)min, max, avg, rms, hmean 10 FORMAT(//1x,'Data Set Results:'/,1x,'minimum =',F5.2,2x,'maximum =',F7.2,/1x,'avg value =',F7.2,2x,'rms value =',F7.2,2x,'harmonic mean =',F7.2//) CLOSE(UNIT=iounit) END Assignment (cont.)

  5. Assignment (cont.) • Data Set Results: • minimum = 4.24 maximum = 999.41 • avg value = 507.51 rms value = 585.27 harmonic mean = 168.23

  6. Bonus Exercise: • In the next 5 minutes, print out and hand in the program and output for today’s assignment. • If correct, a 3 point bonus will be awarded.

  7. File Operations • OPEN (UNIT=iounit, FILE=‘filename’,IOSTAT=ierror) • WRITE(iounit,*)a, b, c • Writes a, b, & c to the file named in the OPEN statement using free format • REWIND(UNIT=iounit, IOSTAT=ierror) • Resets access to the file to its beginning • READ(iounit,*,IOSTAT=ierror)a, b, c • Reads the variables a, b, & c from the file named in the OPEN statement • CLOSE (UNIT=iounit) • In between opening & closing a file, you may read from & write to it using its unit number.

  8. File Operations (cont.) • Free Format – READ(iounit,*) • Data files may be comma, space or tab delimited (values separated by commas, spaces or tabs) • Specific Format – READ(iounit,10) • The data files must conform to Format statement 10

  9. Arrays • Large Data Sets • a, b, c, d, ………aa, ab, ac ….etc • x1, x2, x3, …….x1000……..etc • x(1), x(2), x(3), …….x(1000)……..etc • Permits handling large data sets with a single indexed variable name. • Rank 1 Array Declaration • REAL, DIMENSION (isize):: name • INTEGER, DIMENSION (isize):: iname • isize – number of elements (values) • good practice to dimension an array with an integer variable to make expansion of the program simpler • name, iname – name of the array • Once the array is declared, its variables, x(1), x(2), … are manipulated like any variables

  10. Arrays (cont.) • Initializing the elements in an array: • DO i = 1, 10 • array(i) = REAL(i) • END DO • or, • array = (/1.,2.,3.,4.,5.,6.,7.,8.,9.,10./) • or, • array = 0. • or, • REAL, DIMENSION(5):: array = (/1.,2.,3.,4.,5./) • or, • Read the values of the array from a data file • or, • Other options – Textbook Section 6.2.2

  11. Arrays (cont.) • Setting the range in an array: • REAL, DIMENSION(10):: array • Sets the range of the index from 1 to 10 • or, • REAL, DIMENSION(-3:3):: array • Sets the range of the index from -3 to +3 • or, • REAL, DIMENSION(-5:5):: array • Sets the range of the index from -5 to +5 • If an index is “Out of Bounds” run-time errors MAY occur – The “bounds checking” feature will flag this.

  12. Arrays (cont.) • Whole array operations: • Whole array addition (subtraction) is possible if the arrays are conformable (same shape) (same rank & extent) • array1 = array2 +- array3 • (corresponding elements in array2 & array3 are added (or subtracted) to form the elements of array1 • Scalar multiplication is possible for any array • array2 = a*array1 • (each element in array1 is multiplied by a to form array2) • Elemental Intrisic Functions are applied to arrays on an element by element basis • array2 = SIN(array1) • array2 is an array in which each element is the sin of its corresponding element in array1

  13. Arrays (cont.) • Input/Output using arrays: • Write(*,100) x(1),x(2),x(3),x(4),x(5) • 100 FORMAT(1x,5F10.3) • Writes on a single line • or, • Write(*,100) (x(i) i = 1, 5) • 100 FORMAT(1x,5F10.3) • Writes on a single line • or, • DO i = 1, 5 • Write(*,100) x(i) • 100 FORMAT(1x,F10.3) • END DO • Writes in a column

  14. PROGRAM arrayexample IMPLICIT NONE INTEGER:: iounit, ierror, lim, i REAL,DIMENSION(300):: sqt lim = 300 iounit = 10 OPEN(UNIT=iounit,FILE='sqrtdata',IOSTAT=ierror) REWIND(UNIT=iounit,IOSTAT=ierror) DO i = 1, lim sqt(i) = SQRT(REAL(i)) END DO DO i = 1, lim-4, 5 WRITE(iounit,*) (sqt(i+j), j = 0, 4) END DO CLOSE(UNIT=iounit) END Arrays Example

  15. 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000 3.162278 3.316625 3.464102 3.605551 3.741657 3.872983 4.000000 4.123106 4.242640 4.358899 4.472136 4.582576 4.690416 4.795832 4.898980 5.000000 5.099020 5.196152 5.291502 5.385165 5.477226 5.567764 5.656854 5.744563 5.830952 5.916080 6.000000 6.082763 6.164414 6.244998 6.324555 6.403124 6.480741 6.557438 6.633250 6.708204 6.782330 6.855655 6.928203 7.000000 7.071068 7.141428 7.211102 7.280110 7.348469 7.416198 7.483315 7.549834 7.615773 7.681146 7.745967 7.810250 7.874008 7.937254 8.000000 8.062258 8.124039 8.185352 8.246211 8.306623 8.366600 8.426149 8.485281 8.544003 8.602325 8.660254 8.717798 8.774964 8.831760 8.888194 8.944272 9.000000 9.055386 9.110434 9.165152 9.219544 9.273619 9.327379 9.380832 9.433981 9.486833 9.539392 9.591663 9.643651 9.695360 9.746795 9.797959 9.848858 9.899495 9.949874 10.00000 10.04988 10.09950 10.14889 10.19804 10.24695 10.29563 10.34408 10.39230 10.44031 10.48809 10.53565 10.58300 10.63015 10.67708 10.72381 10.77033 10.81665 10.86278 10.90871 10.95445 11.00000 11.04536 11.09054 11.13553 11.18034 11.22497 11.26943 11.31371 11.35782 11.40175 11.44552 11.48913 11.53256 11.57584 11.61895 11.66190 11.70470 11.74734 11.78983 11.83216 11.87434 11.91638 11.95826 12.00000 12.04159 12.08305 12.12436 12.16553 12.20656 12.24745 12.28821 12.32883 12.36932 12.40967 12.44990 12.49000 12.52996 12.56981 12.60952 12.64911 12.68858 12.72792 12.76715 12.80625 12.84523 12.88410 12.92285 12.96148 13.00000 13.03840 13.07670 13.11488 13.15295 13.19091 13.22876 13.26650 13.30413 13.34166 13.37909 13.41641 13.45362 13.49074 13.52775 13.56466 13.60147 13.63818 13.67479 13.71131 13.74773 13.78405 13.82028 13.85641 13.89244 13.92839 13.96424 14.00000 14.03567 14.07125 14.10674 14.14214 14.17745 14.21267 14.24781 14.28286 14.31782 14.35270 14.38750 14.42220 14.45683 14.49138 14.52584 14.56022 14.59452 14.62874 14.66288 14.69694 14.73092 14.76482 14.79865 14.83240 14.86607 14.89966 14.93318 14.96663 15.00000 15.03330 15.06652 15.09967 15.13275 15.16575 15.19868 15.23155 15.26434 15.29706 15.32971 15.36229 15.39480 15.42725 15.45963 15.49193 15.52417 15.55635 15.58846 15.62050 15.65248 15.68439 15.71623 15.74802 15.77973 15.81139 15.84298 15.87451 15.90597 15.93738 15.96872 16.00000 16.03122 16.06238 16.09348 16.12452 16.15549 16.18641 16.21728 16.24808 16.27882 16.30951 16.34014 16.37070 16.40122 16.43168 16.46208 16.49242 16.52271 16.55295 16.58312 16.61325 16.64332 16.67333 16.70329 16.73320 16.76305 16.79286 16.82260 16.85230 16.88194 16.91154 16.94107 16.97056 17.00000 17.02939 17.05872 17.08801 17.11724 17.14643 17.17556 17.20465 17.23369 17.26268 17.29162 17.32051 Arrays Example (cont.)

  16. Assignment • Use arrays to read the data from a file, leastsqu, that I will email to you. • Use arrays and the least squares method to calculate the slope and intercept of the so-called “best fit” linear approximation to the data

  17. Assignment (cont.) • x, y – data pairs • n – number of data pairs • yavg , xavg – arithmetic averages • m = slope = (Sxy) – (Sx)yavg (Sx2) – (Sx)xavg • b = intercept = yavg – m xavg • y = m x + b – Equ of “best fit” line

  18. 0 20.58306176 1 9.408767646 2 2.042807662 3 16.05061815 4 -20.15158156 5 12.26698057 6 19.38197455 7 10.66627588 8 23.47121892 9 18.92630028 10 20.83972015 11 32.20355182 12 12.82645605 13 27.66824756 14 24.86787439 15 -0.738405147 16 25.60037468 17 45.20006815 18 60.40732137 19 8.191229016 20 81.11298121 21 58.07305816 22 24.99737406 23 30.25463647 24 65.15491028 25 55.71859338 26 53.72731964 27 58.90484285 28 61.6511339 29 53.9094004 30 66.93067674 31 58.45589522 32 58.42098487 33 100.127574 34 75.97448559 35 99.13935305 36 85.01418392 37 59.52364965 38 34.73548456 39 102.0246971 40 82.5128374 41 70.12349131 42 113.8253025 43 96.74404138 44 103.115103 45 96.21815041 46 103.7505428 47 77.81383515 48 103.7423564 49 128.5429378 50 106.0382421 51 109.7647162 52 74.82225029 53 108.7015099 54 109.4335621 55 126.259536 56 123.3725051 57 137.9186334 58 101.2204694 59 122.5208274 60 96.34186003 61 91.254481 62 150.7488974 63 116.4834946 64 129.1840219 65 141.8776135 66 139.720548 67 107.6309483 68 148.0818573 69 106.6038684 70 144.7148106 71 130.1608569 72 169.2465566 73 154.6679814 74 188.3699975 75 103.8658347 76 136.9680195 77 162.1059344 78 143.3408819 79 138.3340358 80 147.1074136 81 165.842165 82 120.1300724 83 208.1341964 84 170.6486035 85 186.091841 86 175.8522621 87 162.3104939 88 213.6155927 89 149.2931647 90 175.2313094 91 167.4406208 92 182.3341646 93 144.2301331 94 173.7151598 95 218.6967919 96 173.8959235 97 175.2269367 98 198.3649812 99 219.3534029 100 171.2617184 Datafile

More Related