COMPUTATION PHYSICS

20
9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١ COMPUTATION PHYSICS PHYS 6312 DR. Maher O. El-Ghossain Associate Professor 9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢ SYLLABUS § TEXT BOOKS: Computational Physics, Fortran Version, Steven E. Koonin. § Coverage : 1- Introduction To Computing 3 h 2- Introduction to Fortran Programming 6 h And Introduction to MatLaB 3-Basic Mathematical Operation Program 6 h 4- Special Function and Gaussain Quadrature 6 h

Transcript of COMPUTATION PHYSICS

Page 1: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١

COMPUTATION PHYSICS

PHYS 6312DR. Maher O. El-Ghossain

Associate Professor

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢

SYLLABUS

§ TEXT BOOKS: Computational Physics, Fortran Version, Steven E. Koonin.

§ Coverage :

1- Introduction To Computing 3 h2- Introduction to Fortran Programming 6 h

And Introduction to MatLaB3-Basic Mathematical Operation Program 6 h4- Special Function and Gaussain Quadrature 6 h

Page 2: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣

5-Ordinary Differential Equation 6 h

6- Matrix Operation 6 h

7- Boundary value and Eigen Value Problem 6 h

8- Monte Carlo Tchniuques

§GRADE: M.EX 20%, HW 10% , Project 10%,Activities 10%, F . Ex 50%.

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٤

CHAPTER 1INTRODUCTION TO COMPUTING

• Computer: Input – Processing-Output – Storage• Computer System: Computer – All the Machines

connected to the computer, execute program.• General Processing Unit ( CPU): The heart of

any computer system convert input to output.• Data and Programs: Data any information read

by a computer, programs are instruction tells the computer how to process information which written in programming language.

• Computer Storage: Primary Storage called Memory, Secondary Storage ( separate Device).

Page 3: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٥

§The CPU and its Memory: Consists of Arithmetic logic Unit (ALU) and control unit.

• Hardware and Software:Machines and Programs, users are people using the computer, programmers are people write programs.

• Microcomputer: Small computer system called personal computer PCS.

• Desktop Unit: Desktop computers like IBM PC, Apple, Macintosh.

• Labtop Units: Light weight computer (1 18 Ib)• Super Computers: High speed, accuracy, high

quality computer•

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٦

•BINARY System: 0 or 1 , on or off, B Byte : Single Character of Data ,

KB, MB, GB,TB

• RAM : Random Access Memory, which work when the computer on , data lost when computer off

• ROM : Read – Only Memory, non erasable programs.

• The Operating System : Collective of Program Manage the computer system activities, PC –Dos , Unix, Vax/Vms , Windows system software.

Page 4: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٧

•Numbering: Decimal 0,1,…..,9. Base 10

• 7216 = 7 x 103 + 2 x 102 + 1 x 101 +6x100

• Binary : 0 or 1 . Base 2• (11100)2 = 1x24+ 1x23+ 1x22+0x21+0x20

= ( 28 )10• (1011001)2 = ( 89) 10• = 1x20 + 0x21 + 0x22 + 1x23

+1x24+0x25 +1x26

Decimal To Binary:89/2 44/2 22/2 11/2 5/2 2/2 2/11 0 0 1 1 0 1

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٨

•Hexadecimal : Base 16

• 0, 1, 2,3,4,5,6,7,8,9,A,B,C,D,E,F.• A= 10, B=11, C= 12, ,D=13, E=14,F=15.• Hexadecimal number: 4F6A• =4x163 + 15 x 162 + 6 x 161 + 10x160

• = 20330

• Some Binary Number:

• 0 0000 1 0001 2 0010 • 3 0011 4 0100 5 0101• 6 0110 7 0111 8 1000• 9 1001 10 1010

Page 5: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٩

Computers

• Labtop Unit •Desktop Unit

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٠

Scientific programming languages What is the best high-level language to use for scientific programming? This, unfortunately, is a highly contentious question. Over the years, literally hundreds of high-level languages have been developed. However, few have stood the test of

time. Many languages) e.g. ،Algol, Pascal, Haskell) can be dismissed as ephemeral computer science

fads. Others) e.g. ،Cobol, Lisp, Ada) are too specialized to adapt for scientific use. Let us examine the remaining options

Page 6: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١١

• :

–FORTRAN was the first high-level programming language to be developed: in fact, it predates the languages listed below by decades. Before the advent of FORTRAN, all programming was done in assembler code! Moreover, FORTRAN was specifically designed for scientific computing.

Indeed, in the early days of computers all computing was scientific in nature--i.e. ،physicists and mathematicians were

the original computer scientists! FORTRAN's main advantages are that it is very straightforward, and it interfaces well with most commonly available, pre-written subroutine libraries (since these libraries generally consist ofcompiled FORTRAN code). FORTRAN's main disadvantages are all associated with its relative antiquity. For instance. FORTRAN's control statements are fairly rudimentary, whereas its input/output facilities are positively paleolithic .

FORTRAN 77

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٢

–This language is a major extension to FORTRAN 77 which does away with many of the latter language's objectionable features. In addition, many ``modern'' features, such as dynamic memory allocation, are included in the language for the first time. The major disadvantage of this language is the absence of an inexpensive compiler. There seems little prospect of this situation changing in the near future.

FORTRAN 90:

Page 7: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٣

C•:

–This language was originally developed by computer scientists towrite operating systems. Indeed, all UNIX operating systems are written in C. C is, consequently, an extremely flexible and powerful language. Amongst its major advantages are its good control statements and excellent input/output facilities. C's main disadvantage is that, since it was not specifically written to be a

scientific language, some important scientific features) e.g. ،complex arithmetic) are missing. Although C is a high-level language, it incorporates many comparatively low-level features, such as pointers (this is hardly surprisingly, since C was originally designed to write operating systems). The low-level features of C--in particular, the rather primitive implementation of arrays--sometimes make scientific programming more complicated than need be the case, and undoubtedly facilitate programming errors.On the other hand, these features allow scientific programmers to

write extremely efficient code. Since efficiency is generally the most important concern in scientific computing, the low-level features of C are, on balance, advantageous .

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٤

C++

–This language is a major extension of C whose main aim is to facilitate object-orientated programming. Object-orientation is a completely different approach to programming than the more traditional procedural approach: it is particularly well suited to large projects involving many people who are each writing different segments of the same code. However, object-orientation represents a large, and somewhat unnecessary, overhead for the type of straightforward, single person programming tasks considered in this course. Note, however, that C++ incorporates some non-object-orientated extensions to C which are extremely useful .

Page 8: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٥

Other Programs

1. MTHCAD2. MAPLE 3. MATHEMATICA4. MATLAB5. TRUE BASIC

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٦

•Of the above languages, we can immediately rule out C++, because object-orientation is an unnecessary complication (at least, for our purposes), and FORTRAN 90, because of the absence of an inexpensive compiler. The remaining options are FORTRAN 77 and C. I have chosen to use C (augmented by some of the useful, non-object-orientated features of C++) in this course, simply because I find the archaic features of FORTRAN 77 too embarrassing to teach students in the 21st century .

Page 9: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٧

CHAPTER 2Introduction To Fortran Programming• FORTRAN: Formula Translator• Planning Program through flow chart• Input, output, Decision, Calculation,

start,stop, Process.

I/o D

Ca

s

c

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٨

•Coding a Fortran Program:

• Column• 1 Comments and special option

* and C indicates the line is not translated by FORTRAN.

• 1 – 5 statement reference # (space )• 6 Continuation Character• 7 - 72 Fortran Statement and Comments

Page 10: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ١٩

FLOW CHARTstart

test

operation

operation

stop

operation

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٠

Five FORTRAN Statements:to write a FORTRAN program

• INPUT

• OUTPUT

• ARITHMATIC STATEMENT

• STOP

• END

Page 11: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢١

Variable and Constants:

• Variable Real or Integer• Constant Real or Integer• Real are A, B, C, D, X, Y, Z

( With Fraction)• Integer I,J, K, L ,M, N

(No Fraction)• Input Statement:• READ *, A, B• OUTPUT STATEMENT:• PRINT *, A, B

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٢

•Arithmetic Statements:

• + Addition A + B• - Subtraction A – B• / Division A/ B• * Multiplication A * B• ** Exponentiation A**B• Y = A * cos (k*r)• Z = C *EXP(-x)

Page 12: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٣

EXAMPLE 1

• CalculateFor different x = 5, x =6?

Program 1:Read * x1,x2Y1= 1/sqrt(pi)* EXP(-x1**2)/2Y2=1/sqrt(pi)*EXP(-X2**2)/2PRINT *, X1, X2, Y1,Y2STOPEND

2/2

/1 xey −= π

WriteY1,y2

CalculateY1,Y2

start

stop

ReadX1,x2

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٤

FUNCTION

• SQRT (x)• / x / ABS(x)• ex EXP (x)• Sin x sin (x)• Cos x cos (x)• Log10 x Alog10 (x )• Loge x Alog (x)

x

Page 13: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٥

FORMATED INPUT AND OUTPUT

READ (m,n) Variable listN FORMAT (format list)

READ (5,20) M,N20 FORMAT (I3, I5)

READ (5,20) A20 FORMAT (F12.3)

WRITE(6,27) ID, Amount27 FORMAT(I8, 3x, F10.2)

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٦

EXAMPLE 2

• Calculate the perimeter P and area A of triangle a,b,c ?

• P = a + b + c• Area =• S= P/2 = (a + b +c )/2• a = 38.6 , b = 42.4 , c = 56.1• Write FORTRAN PROGRAM

))()(( csbsass −−−

Page 14: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٧

PROGRAM 2

• READ (5, 11) A, B, C• 11 FORMAT (3F 10.1)• P = A + B + C• S = P/2.0• AREA = SQRT(S*(S-A)*(S-B)*(S-C))• WRITE (6,12) A,B,C,P,AREA• 12 FORMAT (3(F8.1,4x),F10.1,4x,F12.2)• STOP• END

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٨

EXAMPLE 3

• Solve the two linear equations a1x+b1y=c1,• A2x+b2y=c2 ,find x and y for given a,b,c• For the condition d = a1b2-a2b1 not equal 0?

• X=(b2c1- b1c2 ) / ( a1 b2 –a2b1)

• Y=(a1 c2 – a2 c1) / ( a1 b2 – a2 b1)

Page 15: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٢٩

PROGRAM3

• READ (5,11) A1,B1,C1,A2,B2,C2• 11 FORMAT (6F10.3)• D=A1*B2 – A2*B1• X=(B2*C2 – A2*C2)/D• Y=(A1*C2 – A2*C1)/D• Write (6,12) A1,B1,C1,A2,B2,C2,X,Y• 12 Format(8F10.4)• Stop• END

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٠

UNCONDITIONAL TRANSFER

• GO TO N• N STATEMENT NUMBER• I = 1• 99 PRINT *, I• I=I+1• GO TO 99• STOP• END

Page 16: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣١

EXAMPLE 4

• Compute the average score for each student?

• N student No.• S1, S2, S3 SCORES• Read N, S1,S2, ..• Calculate SUM and Average• Print N , Average

WriteN,a

CalculateSumAver

start

stop

Readn

s1,s2

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٢

PROGRAM 4

• C COMPUTE THE AVERAGE SCORE FOR • C EACH STUDENT• 100 READ *, N, S1,S2,S3,S4• SUM = S1+S2+S3+S4• AVER= SUM/4• GO TO 100• STOP• END

Page 17: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٣

UNCONDITIONAL TRANSFER

• LOGICAL IF STATEMENT• IF(A.LE.B) GO TO 50• IF(L.GE.75) N=N+1• IF(J.NE.K)WRITE(6,30) X,Y• .LT. LESS THAN , .EQ. EQUAL• .GT. GREATER THAN• IF(X-2.0)10,20,30 • < 0 go to 10, = 0 go to 20, >0 go to 30

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٤

•ONE DIMENSIONAL ARRAY X(I), Y(I ), Z(I)

• DIMENSION A(100), B(50), C(30)

• Multi Dimensional Array: • DIMENSION A(30,30),B(30,30),C(30,30)

• DO LOOPS: DO 50 I=1,N

• ……………….50 CONTINUE

Page 18: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٥

•FUNCTION AND SUBROUTINES

• FUNCTION BIG(A,B,C)• FUNCTION NAME(P1,P2,P3,P4,…)

• SUBROUTINE NAME(P1,P2,P3,..)• SUBROUTINE LARGE( A,N,K,H)

• CALL LARGE (A,N,K,H)

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٦

EXAMPLE 5

• Calculate the Factorial of a number N ?• N ! = 1.2.3……(N-1).N

If N=0

WriteN,ifac

Do 2 J=1,N

, ifac=Ifac*J

start

stop

Read NIfac=1

no

yes

Page 19: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٧

PROGRAM 5

• C FUNCTION CALCULATE THE C FACTORIAL OF A NUMBER N

• C FUNCTION IFAC(N)• READ (*,*) N• IF(N.EQ.0) GO TO 30 ( or return)• DO 10 J= 1,k• IFAC=IFAC*J• 10 CONTINUE• C RETURN• 30 STOP• END

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٨

EXAMPLE 6

• Write Subroutine to find the largest ,sum and average number from a group of numbers or scores?

H<A(j)

start

Read N

Sum=0H=A(1)

Do j=1,nSum=sum+A(j)

H=A(j)

stop

Page 20: COMPUTATION PHYSICS

9/21/2013 1:39:33 PM Dr Maher O. El-Ghossain ٣٩

PROGRAM 6• SUBROUTINE LARGE(A,N,H,AV)• DIMENSION A(100)• SUM=0.0• HIGH=A(1)• DO 100 J =1,N

SUM=SUM+A(J)• AV=SUM/N• IF (HIGH.LT.A(J)) HIGH=A(J)100 CONTINUE• RETURN• END