CH- 481 Chemistry and Computers

16 downloads 185434 Views 131KB Size Report
Files. • Chemical Applications of computers ... courses available in the Institute from time to time ... stop end c use all small case letters for convenience ...
CH- 481 Chemistry and Computers B. L. Tembe Rm 365, Chemistry Dept [email protected] We will use the IITB moodle site for out of class interaction Course material can be found in www.chem.iitb.ac.in/Faculty/bltembe /courses/ch481

Course Overview • Computers are tools like spectrometers or calorimeters, which are tools for chemists • Can compute reasonable information on molecular and macroscopic (bulk) systems • Old statement “chemistry is an experimental science” • Computational chemistry is now an accepted branch of chemistry

Today’s lecture • • • • • • • •

About Computers to the “unexposed” Hardware and software Algorithms and Programs Programming languages Operatining systems Login/Passwords Files Chemical Applications of computers

Your Working Area on the computer • • • • • •

Login/password Directories Files- program files, data files, video files executable files, booting files, music files For creating and saving you need an editor We will use vi or ‘pico’ in ‘linux’. Pico is easier for beginners. Type ‘pico filename’ to create/edit a file named filename

Operating systems • • • •

Windows (Proprietary Software) Linux (Public Domain/Free) Installation of the software Upgradation

• You may register and take the linux short term courses available in the Institute from time to time

Programming Languages • • • • • •

C, C++,.. Java Visual Basic, Fortran Pascal SQL

Fortran: Formula Translation • • • • • • •

y=x½ y = sqrt (x) y = x cubed y = x ** x or x* x * x cos (x) tan (x) alog (x), alog10 (x) y = (x**3)**4 y = asin (x)

5 important rules in fortran • Start program statement from 7th column and do not use tabs • Line numbers in columns 1 to 5 • Continuation character in column 6 • c in first column implies a comment (not part of the program’s executable statement • variables starting with i,j,k,l,m,n are integers

algorithms • Unambigous sequence of steps/statements for carrying out any task • A good algorithm a prerequsite for a program • Eg: direction to go to your house in your home-town • a) go to CSTM station • b) take a train to Darjeeling • c) reach 33, Tensing St, Dargeeling 773347

A sample program c c c

10

c

my first program (this is a comment card/line) program myprogm (this is an optional line) input an integer read(*,*) n sum integers from 1 to n msum = 0 do 10 i = 1,n ( this is a do or for loop, for repeated operations) msum = msum + i continue Write (*,*) n, msum stop end use all small case letters for convenience

Summary • • • • • • • •

Introduction to computers Algorithms and programs Operating systems A sample program NOT DONE Compiling a programme: f77 prog.f Executing a program ./a.out Two lab batches: 2 to 4 pm, 4 to 6 pm

Review of Last Lecture • • • • • • •

Introduction to computers Algorithms and programs Operating systems A sample program What was not done DONE: Compiling a programme: f77 prog.f Executing a program ./a.out

Types of Files • • • • • • • • •

Fortran Files: prog.f, qchem.f, stats.f …. C program files: prog.c, nmr.c,…. Data files: kinetics.dat, mydata1,… Executable files: ./a.out prog.exe How to compile programs f77 prog.f This creates a file ./a.out gcc prog.c ,, ,, To run a program, just type ./a.out

Algorithms, Flow Charts and Programs • For our purpose, these are three representations of the problem that we need to solve, the last one, being the most useful • Unambigous sequence of steps/statements for carrying out any task • A flow chart outlines or presents the algorithm in such a way that the flow of control of execution (recall traffic signals and police men) is clearly visible.

Example of a flow chart • Conversion of energy into different units Input the value of energy in kcal/mol, = x In how many other units do you want energy to be calculated? Ej = x * 4.18/(1000* 6.02252* 10**(23) ) Eev= x/23.06

Eau = Eev/27.2

none

A program for the precious task • C read the value of energy in kcal/mol • read (*,*) x • eev = x / 23.06 • eau = eev/27.06 • ej = x * 4.18 / (1000 * 6.02252 * 10 **23) • write (*,*) ‘energy in kcal/mol, ev, au, j) • write (*,*) x, eev, eau, ej • stop • end

Elementary functions in Fortran

• • • • • •

y=x+z/c+a*b p = sin (theta) + alog (conc) q = exp (- energy / (boltz *temp)) r = d ** f t = sqrt (23.0) a=a+b z = acos (y)

Summary of second lecture • Reviewed the first lecture • Types of files, compilation and execution • Algorithms, flowcharts and program • • • • •

Next Lecture Algorithm for the sine function Calculation of the sine function Main ingredients of programs Convergence and iterations

An Algorithm for calculation of the sine function Sin (x) = x – x3/3! + x5/5! – x7/7! + x9/9! How to construct an algorithm for the series? Observe the trends in the terms… 1) as n (no of terms) increases, terms become small; i.e, the series may converge • 2) Each successive term can be obtained from the previous term by multiplying by x2 and dividing by (n+1) x (n + 2), ie, we can iterate • • • •

Algorithm for sin (x) Get the value x in radians Initialize the value of sum (of terms) Calculate successive terms Add successive terms to the sum If the value of the sum does not change by more than say, 10 ** (-10) by adding a new term, stop the addition of successive terms • Print the value of the sum and the no. of terms • • • • •

Programme to calculate sin (x) • C program sinex • write(*,*) ‘input the value of theta in deg’ • x = theta * 3.14159265 / 180.0 • sum = 0.0 • term = x • denom = 1.0 • nterm = 1 • 10 sum = sum + term • denom = denom + 2.0 • term = - term * x * x / (denom * (denom-1)) • nterm = nterm + 1 • if ( (abs (term) .gt. 10**(-10) ) go to 10 • write(*,*) ‘nterm, sum, fortran sine fn • ftsnfu = sin (x) • write(*,*) nterm, sum,ftsnfn • stop • end

Steps in the algorithm/flowchart sum = x, denom = 1, term = x, nterms = 1

denom = denom + 2 term = - term * x * x /(denom * (denom -1) ) sum = sum + term nterms = nterms + 1

Is term < 10 ** (-10)

Write sum, nterms

stop

Elementary “Principles” or ingredients of Computer Programming • • • • • • •

Input Output Declaration statements Execution statements Iterative statements or loops Control of transfer (“if” or “while” statements) Functions, subprograms or Procedures End of lines, functions or subroutines

Calculation of the value of sin (x) at 101 equispaced points • c calculate the value of sin (x) at 101 equispaced points between 0 and 2pi (including the end points). • twopi = 2.0 * 3.14159265 • do 10 i =1, 101 • x = real (i-1) * 0.01 * twopi • y = sin (x) • write (*, *) ‘x and sin (x) =’, x, y c the above statement can be improved) • 10 continue • end

What you are expected to learn in this course • • • • • • •

Run elementary programs for calculating Fibonacci series, the sine function, energy conversion factors, arrranging numbers is ascending order, solve a quadratic eq., elementary interpolation and integration, matrix multiplication, reading and writing to files, and

• Using scilab to diagonalize matrices, • plot elementary wavefunctions, and • solve differential equations.

do 10 i =1, 100, 1 • the last part ‘1’ indicates that i is incremented by 1 every time. • If we want to increment the variable i in steps of 2, we use • do 10 i =1, 100, 2 • executable statements • 10 continue • There are other alternatives to the do statement such as • sum = 0.0 do i=1, 100,2 sum = sum +(real (i))**3 end do • Computer treats real numbers and integers very differently

Observe the graph

We need to write a program which gives the value of f (x) as per the above formula. This is given below. • read (*, *) x • if (x.lt.0.0) then • funct = 0.0 • else if (0.0 .le. x .and. x .lt. 5.0) then • funct = 5.0 • else if (5.0.le. x .and. x .lt. 10.0) then • funct = 10.0 • else • funct = x • endif • write (*,*) ‘ x, funct= ', x, funct • end • c the above program illustrates the use of the if statement

Solution of a quadratic equation • The general form of the quadratic equation is • a x * x + bx + c The roots of this equation are • [ - b + (b * b – 4.0 * a * c) **(-1/2) ] / ( 2*a) • [ - b - (b * b – 4.0 * a * c) **(-1/2) ] / ( 2*a)

program quadratic • c program quadratic • write (*, *) 'input the values of a, b and c:' • read (*, *) a, b, c • if ((a .eq. 0.0) .and. (b .ne. 0.0)) then • x = -c/b • twoa=2.0*a • write (*, *) 'the solution of linear equation x=', x • go to 100 • else if ((a .eq. 0.0) .and. (b .eq. 0.0)) then • write (*, *) 'both coefficients a and b are zero' • go to 100 • endif

ww = b * b - 4.0 * a * c if (ww .lt. 0.0) then go to 50 else rtofww = sqrt (ww) root1 =( -b + rtofww) / twoa root2 =( -b - rtofww) / twoa write (*, *) 'real roots 1 and 2 are', root1, root2 go to 100 endif 50 continue c the roots are complex b**2 - 4 * a * c is -ve ww = 4.0 * a * c - b * b rtofww = sqrt (ww) realpt = -b / twoa ximpt = rtofww / twoa write (*, *) 'complex roots' write (*, *) 'root1', 'real part=', realpt, 'imaginary part=', ximpt ximpt2 = -ximpt write (*, *) 'root2', 'real part=', realpt, 'imaginary part=', ximpt2 100 continue end

Summary: The main ingredients of a program • 1) an instruction to carry out a mathematical operation (such as evaluating a formula for a given value of a variable), • 2) repeating a calculation until a condition is satisfied, • 3) allocation of storage space for calculated quantities such as matrices • 4) reading inputs from files and writing the output to files as well as the computer screen, • 5) terminating the program either on completion or giving messages if something has gone wrong with the execution of the program.