prev

next

out of 39

Published on

15-Aug-2015View

14Download

1

Embed Size (px)

Transcript

<ol><li> 1. CH 232 : COMPUTATIONAL CHEMISTRY ILLUSTRATIVE EXAMPLES </li><li> 2. CH 232 : COMPUTATIONAL CHEMISTRY ____________________________________________________________________ 1. Get the bond length of CC and C-H bonds and generate the co-ordinates of all the atoms in acetylene. 2. Write a program to read the coefficients a, b and c of the quadratic equation ax2 + bx + c = 0 and to evaluate its two real roots. 3. Write a program to read Roll Numbers and marks in Quiz1 (10), Mid Sem (30) , Quiz2 (10) and Final Exam(50) of 10 Students and to assign AA if total marks obtained is greater than or equal to 90 BB if total marks obtained is greater than or equal to 80 but less than 90 CC if total marks obtained is greater than or equal to 70 but less than 80 DD if total marks obtained is greater than or equal to 60 but less than 70 FF if total marks obtained is less than 60 Also Compute the class Average of total marks and report the roll number of students obtaining the highest and the lowest score. </li><li> 3. CH 232 : COMPUTATIONAL CHEMISTRY _______________________________________________________________________________ ! THIS IS A PROGRAM TO READ THE BOND LENGTHS IN ACETYLENE ! AND GENERATE THE CO-ORDINATES . ! program coord read(*,*) ccbond, chbond ccmid = 0.0 cleft = ccmid - (ccbond/2.0) cright = ccmid + (ccbond/2.0) hleft = cleft - chbond hright = cright + chbond write(*,*) ' H ',' C ' ,' C ',' H ' write(*,*) hleft, cleft, cright, hright stop end Input as: 1.21 1.00 Output: H C C H -1.605000 -0.605000 0.605000 1.605000 </li><li> 4. ARITHMETIC IF STATEMENT The arithmetic IF allows three way branching depending on the value of an expression. The syntax of arithmetic IF is IF (Expression) N1, N2, N3 The expression within the bracket is evaluated. If is negative, control is passed to the statement having statement number N1 If the expression is zero, control is passed to the statement having statement number N2 If the expression is positive, control is passed to the statement having statement number N3 The statement number must refer to executable statement present within the same unit as arithmetic IF. For example, in case of IF (X+Y) 10, 20, 30 the value of the expression X + Y will determine the label where the branching would take place Value of X+Y Statement to be executed X + Y < 0 Statement number 10 X + Y = 0 Statement number 20 X + Y > 0 Statement number 30 </li><li> 5. ARITHMETIC IF STATEMENT The statement numbers present along with arithmetic IF might not be all different IF (A + B / C) 10, 10, 20 If the expression A + B/C is either zero or negative, control is passed to the statement number 10; otherwise control is passed to the statement number 20. The following are valid usage of arithmetic IF IF (A) 10, 20, 20 IF (A + B) 10, 20, 10 If all the three numbers are identical arithmetic IF becomes equivalent to GO TO: IF (A) 100, 100, 100 and in such a case the conditional branch statement which is independent of condition should be replaced by, GO TO 100. (As this is much more elegant and faster too) The following algebraic expression may be represented by arithmetic IF as: B = A/2, when R < 0 B = 0, when R = 0 B = 2*A when R > 0 IF (R) 11, 12, 13 11 B = 0.5*A GO TO 20 12 B = 0.0 GO TO 20 13 B = 2.0*A 20 </li><li> 6. CH 232 : COMPUTATIONAL CHEMISTRY ___________________________________________________________________________ ! PROGRAM TO GET ROOTS OF A QUADRATIC EQUATION ! Program Quad ! This program can compute the real roots of a quadratic equation ax2 + bx + c. If the ! roots are complex, the program quits after writing a message to this effect. implicit real*8 (a-h, o-z) read(*,*)a,b,c discrm = b*b - 4*a*c if(discrm)100,200,300 100 write(*,*)'roots are complex and cannot be computed by this & program' stop 200 root = (-b)/(2*a) write(*,*)' both roots are equal to: ', root stop 300 root1= (-b + sqrt(discrm))/(2*a) root2= (-b - sqrt(discrm))/(2*a) write(*,*)' The roots of this equation are : ' write(*,*)' root1 = ', root1 write(*,*)' root2 = ', root2 stop end Input as 1 11 30 Output The roots of this equation are : root1 = -5.000000000000000 root2 = -6.000000000000000 </li><li> 7. CH 232 : COMPUTATIONAL CHEMISTRY _______________________________________________________________________________ ! THIS PROGRAM COMPUTES TOTAL MARKS OBTAINED FROM VARIOUS TESTS ! AND PRINTS OUT GRADE, CLASS AVERAGE AND THE HIGHEST AND LOWEST ! MARKS WITH THE CORRESPONDING ROLL NO. ! program grades read(*,*) numstd if (numstd .gt. 10) then write(*,*) 'Number of students exceeds 10' stop endif sum = 0.0 smax = 0.0 smin = 100.0 write(*,*) ' Roll No ' , ' Grade ' do 100 i =1,numstd read(*,*) iroll, quiz1,smid,quiz2, sfinal if ((quiz1 .le. 10) .and. (smid .le. 30) .and. (quiz2 .le. 10) & .and. (sfinal .le. 50)) then total = quiz1+smid+quiz2+sfinal if (total .ge. 90) write(*,*) iroll, ' AA ' if (total .ge. 80 .and. total .lt. 90) write(*,*) iroll , ' BB ' if (total .ge. 70 .and. total .lt. 80) write(*,*) iroll , ' CC ' if (total .ge. 60 .and. total .lt. 70) write(*,*) iroll , ' DD' if (total .lt. 60) write(*,*) iroll, ' FF ' if (total .ge. smax) then smax = total irolhigh=iroll endif </li><li> 8. CH 232 : COMPUTATIONAL CHEMISTRY _______________________________________________________________________________ if (total .le. smin) then smin = total irollow=iroll endif sum = sum +total else Write(*,*) 'Error Please check marks for Roll No ',iroll endif 100 continue avg = sum/numstd write(*,*) write(*,*) 'Class Average is = ', avg write(*,*) write(*,*) 'Highest score is = ', smax, & 'scored by Roll No', irolhigh write(*,*) write(*,*) ' Lowest score is = ', smin, & 'scored by Roll No', irollow stop end Case I Input 10 111 9 28 9 49 112 7 28 4 48 113 8 21 2 41 114 1 36 7 29 115 6 33 9 31 116 7 19 5 40 117 8 21 6 47 118 3 10 7 42 119 9 30 8 49 120 8 30 9 40 </li><li> 9. Output Roll No Grade 111 AA 112 BB 113 CC 116 CC 117 BB 118 DD 119 AA 120 BB Class Average is = 65.20000 Highest score is = 96.00000 scored by Roll No 119 Lowest score is = 62.00000 scored by Roll No 118 Output at terminal Error Please check marks for Roll No 114 Error Please check marks for Roll No 115 Case II Input 11 111 9 28 9 49 112 7 28 4 48 113 8 21 2 41 114 1 36 7 29 115 6 33 9 31 116 7 19 5 40 117 8 21 6 47 118 3 10 7 42 119 9 30 8 49 120 8 30 9 40 </li><li> 10. Output Number of students exceeds 10 Case III Input 10 111 9 28 9 49 112 7 28 4 48 113 8 21 2 41 114 1 26 7 29 115 6 23 9 31 116 7 19 5 40 117 8 21 6 47 118 3 10 7 49 119 9 30 8 49 120 8 30 9 40 Output Roll No Grade 111 AA 112 BB 113 CC 114 DD 115 DD 116 CC 117 BB 118 DD 119 AA 120 BB Class Average is = 79.10000 Highest score is = 96.00000 scored by Roll No 119 Lowest score is = 63.00000 scored by Roll No 114 </li><li> 11. SO MANY INPUTS!!! (NEEDS ALTERNATIVES) ALTERNATIVES USE ARRAYS FOR LARGE NUMBER OF DATA USE DATA FILE TO STORE DATA (INSTEAD OF VDU) </li><li> 12. CH 232 : COMPUTATIONAL CHEMISTRY SUBSCRIPTED VARIABLES AND ARRAYS </li><li> 13. SUBSCRIPTED VARIABLES AND ARRAYS An array consists of a rectangular set of elements, all of the same type and type parameters. Each array has an index that allows us to locate and manipulate the quantities stored in the array. We sometimes also called the subscript. An array is referred to by its name and usually accessed by subscripts. An array may be accessed in four different ways: the array as a whole element by element a portion of the array by means of vector subscripts An array may be of single dimension, or it may have more than one dimension. A single dimensional array is called a vector and a two dimensional array is called a matrix. Difference between single valued variable and subscripted variable The variables so far we have studied are single-valued i.e, they can take only a single value: X = 1.23456 (Variable X has only a single value) For a subscripted variable, we must add one additional piece of information the subscript: X (1) = 1.23456 X (2) = 9.87654 </li><li> 14. SUBSCRIPTED VARIABLES AND ARRAYS The number that appears within the parentheses is the index and indicates the position within the array X that contains the list of data. Thus, the first number in the list is X (1) or 1.23456 The quantity that goes inside the parenthesis can be a constant, a variable, or an expression. Thus, we could have: X (1) = X (J) = X (2*K 1) = The subscript can be a variable that can be controlled by the program (Advantage) EXAMPLE: The array subscript can be controlled by a DO loop. The most common way is to use the loop control variable (LCV) as the subscript of the array. In this example, we will use I as the LCV, and then use it to store value in x1, x2, , x10. The values will be i2. When this program segment is finished, x1 will contain 12, x2 will contain 22 and so forth up to x10 storing 102. DO 10 I = 1, 10 X (I) = I**2 10 CONTINUE The variable I will change each time through the loop. Thus, when I = 1, a value will be assigned to X (1) and so forth. The primary restriction on the subscript is that it must be an integer constant, variable, or expression. (Example in the following page) </li><li> 15. SUBSCRIPTED VARIABLES AND ARRAYS EXAMPLE: (The subscript of an array may be an integer constant, a variable, or an expression) Rule for Subscript Correct Example Incorrect Example Constant X (1) X (1.2) Variable X (J) X (Z) Variable + constant X (J + 1) X (1 + J) Constant variable X (2*J) X (J*2) Constant variable constant X (2*J 1) X (J*2 K) THE DECLARATION STATEMENT Declaration statements go at the beginning of the program before any executable statements and provide important information that allows compiler to reserve enough memory space for the arrays. There are a number of ways in which arrays may be declared ; first lets consider only the declaration of arrays of fixed size. It is usually done with the type declaration statement, which also indicates the size or number of elements in the array. The general form of declaration statement is: type arrayname (Lower limit : Upper limit) Or type, dimension(Lower limit : Upper limit) :: arrayname </li><li> 16. SUBSCRIPTED VARIABLES AND ARRAYS Where, type indicates the type of that array (REAL, INTEGER, LOGICAL, etc.) arrayname is any valid Fortran variable name (X, TIME, etc.) Lower limit indicates the lowest value of the subscript Upper limit indicates the maximum value of the subscript The Lower limit : Upper limit of numbers can be any integers (even negative) as long as the upper limit is larger than the lower limit An important point about declaring array is that we promise the compiler what the maximum size of the array will be. It is not necessary to use all the array space (reserve sufficient space for the data) (However, the reverse situation is not true) While an array represents a group of data as a list, the subscript represents the position within that list. Example in the following page </li><li> 17. SUBSCRIPTED VARIABLES AND ARRAYS EXAMPLE: (The following illustrate some correct and incorrect usage of array declaration statements:) Valid Invalid Comments REAL X(5) (X is a real array with 5 elements) INTEGER A(10), B(3) (A and B are both integer arrays with 10 elements and 3 elements respectively) REAL X(1:10) (OK, but 1 is unnecessary) REAL X(10) (Same result as previous example) REAL X(-5:10), Y(20) Declares X and Y to be real with 16 and 20 elements, respectively INTEGER AMT, M(10) (OK to declare scalar variables and arrays in the same declaration statement.) REAL X(N) (Variable-size arrays not allowed) REAL X(10.0) (Limits must be integer, not real) REAL X(5:-5) (Wrong order of limits) </li><li> 18. Some Basic Definitions The number of elements of an array is called its size. Each array element is a scalar. Whereas we always need to specify the upper bound, the lower bound is optional, and by default has the value 1. The number of dimensions of an array is known as its rank. Scalars are regarded as having rank zero. The number of elements along a dimension of an array is known as the extent in that dimension. For example Real, dimension(-10:5, -20:1, 0:1, -1:0) :: grid Here, the array grid has extents 16, 20, 2 The sequence of extents is known as the shape. in the above example grid has the shape (16, 20, 2, 2). SUBSCRIPTED VARIABLES AND ARRAYS </li><li> 19. SUBSCRIPTED VARIABLES AND ARRAYS MANIPULATING ARRAYS How to manipulate arrays? One dimensional arrays are an efficient way to store and manipulate lists of data. Quite often, these data will be systematically stored, retrieved, and manipulated. EXAMPLE 1: Here is a simple example: This expression represents the sum of all the elements stored in the subscripted variable y and the statement stores the result in a. We add one element at a time, and systematically change the subscript from i = 1 to i = 100. We implement this in Fortran with a DO loop (as given below) REAL Y (100) A = 0.0 DO 10 I = 1, 100 A = A + Y(I) 10CONTINUE The DO 10 loop takes each element of the array and adds it to the running total. For example, when I =1, Y(I) or Y(1) is added, when I=2, Y(2) is added, and so forth. Note: The subscript of the array is also the loop control variable 100 1 i i i a y </li><li> 20. SUBSCRIPTED VARIABLES AND ARRAYS EXAMPLE 2: Create a program that reads in a list of numbers from the terminal, calculate average, and finally prints a list of the individual deviations of each number from the average. The deviation is the difference between the number and the average. (Assume a maximum of 100 numbers will be entered.) REAL X (100), DEV (100) ! Enter the number of data items for the computation Write (*,*) Number of values (less than 100)? Read (*,*) N ! We will read in one data value at a time and store it in X (I) SUM = 0.0 DO 10 I = 1, N READ (*, *) X(I) SUM = SUM + X(I) 10 CONTINUE AVG = SUM /N WRITE (*,*) Average = , AVG ! Once the average has been computed, we can use it to ! calculate the deviations defined by X(I) AVG: DO 20 I = 1, N DEV (I) = X(I) AVG WRITE (*,*) NUMBER = , X(I) WRITE (*, *) DEVIATION = , DEV (I) 20 CONTINUE STOP END </li><li> 21. EXAMPLE 3: The dot product of two vectors _______________________________________________________________________________ ! PROGRAM TO COMPUTE DOT PRODUCT BETWEEN TWO 3-D VECTORS ! program dotprod real avec(3), bvec(3) dotprd = 0.0d0 do 100 i=1,3 read(*,*)avec(i), bvec(i) write(*,*)' Component No. ', i, ' of AVEC and BVEC are :', & avec(i), ' and ', bvec(i) dotprd=dotprd+avec(i)*bvec(i) 100 continue write(*,*) ' Dotproduct of AVEC and BVEC is : ', dotprd if(dotprd.eq.0)write(*,*)' AVEC and BVEC are ORTHOGONAL' stop end Input 1.0 0.0 0.0 1.0 0.0 0.0 Output Component No. 1 of AVEC and BVEC are : 1.000000 and 0.0000000E+00 Component No. 2 of AVEC and BVEC are : 0.0000000E+00 and 0.0000000E+00 Component No. 3 of AVEC and BVEC are : 0.0000000E+00 and 0.0000000E+00 Dotproduct of AVEC and BVEC is : 0.0000000E+00 AVEC and BVEC are ORTHOGONAL </li><li> 22. SUBSCRIPTED VARIABLES AND ARRAYS Another common application of the one-dimensio...</li></ol>

Recommended

View more >