chulalongkorn university 1. what is...

17
1 1 Chulalongkorn Chulalongkorn University University Computational Chemistry Unit Cell Computational Chemistry Unit Cell Computational Chemistry Unit Cell Computational Chemistry Unit Cell Chulalongkorn Chulalongkorn University University PS Topics (3 rd week ) -โปรแกรมภาษาฟอรแทรน เบื้องตน - เขียนโปรแกรมสําหรับการอานอินพุทและเขียนเอาทพุทจาก ขอมูล PDB -เขียนโปรแกรมแปลงขอมูลทางโครงสรางจากรูปแบบอื่นๆ ใหอยู ในรูป PDB format -ขียนโปรแกรมวิเคราะหทางโครงสรางโมเลกุลจากระบบพิกัดของ อะตอม - ปฏิบัติการ 3 2 Chulalongkorn Chulalongkorn University University Computational Chemistry Unit Cell Computational Chemistry Unit Cell Computational Chemistry Unit Cell Computational Chemistry Unit Cell Chulalongkorn Chulalongkorn University University PS 1. What is Fortran? - a general purpose programming language, mainly intended for scientific and engineering applications. - an acronym for FORmula TRANslation. - started in the 1950's at IBM. - a Fortran version is denoted by the last two digits of the year. Fortran 66, Fortran 77 (the most common), Fortran 90 (95). - portable across machine platforms

Upload: others

Post on 15-Apr-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

11

1

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

Topics (3rd week )-โปรแกรมภาษาฟอรแทรน เบื้องตน- เขียนโปรแกรมสําหรับการอานอินพุทและเขียนเอาทพุทจากขอมูล PDB-เขียนโปรแกรมแปลงขอมูลทางโครงสรางจากรูปแบบอื่นๆ ใหอยูในรูป PDB format-ขียนโปรแกรมวิเคราะหทางโครงสรางโมเลกุลจากระบบพิกัดของอะตอม- ปฏิบัติการ 3

2

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

1. What is Fortran?- a general purpose programming language, mainly intended for scientific and engineering applications. - an acronym for FORmula TRANslation. - started in the 1950's at IBM. - a Fortran version is denoted by the last two digits of the year. Fortran 66, Fortran 77 (the most common), Fortran 90 (95).- portable across machine platforms

Page 2: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

22

3

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

Compiling, linking and running a Fortran program

4

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

2.1 Declaration (& Title) statement2.2 Content or body of the program2.3 Stop and end statement2.4 Subroutine or module

2. โครงสรางโคดของโปรแกรม (code structure of program)

Page 3: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

33

5

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

ตัวอยาง โปรแกรม area.f- program circle

real r, areac This program reads a real number r and printsc the area of a circle with radius r.

write (*,*) 'Give radius r:'read (*,*) rarea = 3.14159*r*rwrite (*,*) 'Area = ', areastopend

title & declaration

programming & processing

end statement

6

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

3. Program layout (fixed format of Fortran 77)ภาษาฟอรแทรน 77 มีกฎขอบังคบัที่เขมงวดเกี่ยวกับฟอรแมตหรือตําแหนงคอลัมน ดังนี้

Column position rulesCol. 1 : Blank, or a "c" or "*" for commentsCol. 1-5 : Statement label (optional)Col. 6 : Continuation of previous line (optional)Col. 7-72 : StatementsCol. 73-80: Sequence number (optional, rarely used today)

Page 4: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

44

7

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

4. Constants and variables4.1 The CHARACTER type4.2 The INTEGER type4.3 The REAL type4.4 Variables

4.4.1 Explicit typing: INTEGER WIDTHREAL NUM, K

4.4.2 Implicit (or default) typing: 4.4.3 Assigning a value

4.4.3.1 The READ statement: read (*,*) v1,v2,v34.4.3.2 The assignment statement: x=10

8

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

Program readwritepdb

c =======================

c declaration

implicit real(a-h,o-z), integer(i-n)

parameter (maxmol=10,maxnr=1000,maxnatm=50000,maxchain=50)

integer numat(maxnatm,maxchain),numres(maxnatm,maxchain),

+ natom(maxnatm)

character*6 keywrd

character*80 filnam,line

character*4 atmnam(maxnatm,maxchain),atn

character*3 resnam(maxnatm,maxchain),ren

character*1 chain

double precision xatm(maxnatm,maxchain),yatm(maxnatm,maxchain),

+ zatm(maxnatm,maxchain)

c ===============

ตัวอยาง โปรแกรม readpdb.f

Page 5: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

55

9

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

c ===============c Enter input file

write(*,'(/,4x,"Enter pdb input file")')read(*,'(a)') filnamprint *, 'input file name : ',filnam

c =================

10

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

3. Arithmetic, relational & logical expressions

Operator ความหมาย + , - , * , /, ** บวก ลบ คูณ หาร เลขยกกําลัง.lt. นอยกวา.le. นอยกวาหรือเทากับ.eq. เทากับ.ne. ไมเทากับ.ge. มากกวาหรือเทากับ.gt. มากกวา.or. หรือ.and. และ

Page 6: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

66

11

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

4. The logical IF statement

Example: IF (A.LT.B) SUM = SUM + A

The block IF structure: if ( ) then

else

endif

12

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

5. Iteration statement5.1 The GOTO statement

GOTO label5.2 The DO-loop

5.2.1 DO label, var = e1, e2, [,e3] label continue

Example: DO 10, I = 1,5 DO 10, I = 0,100,5 DO 10, I = 100,0,-5 DO 10, I = 0,100,-5 DO 10, J = I,4*N**2-1,K

10 continueDO I = 1,10

Page 7: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

77

13

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

5.2.2 DO var = e1, e2, [,e3] enddo

Example: DO I = 1,100…enddo

14

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

c ==========================c read pdb file

do i = 1,10natom(i) = 0

enddo51 read(10,'(a6)')keywrd

if(keywrd .ne. 'ATOM ') thengoto 51

elseendifbackspace (10)

ตัวอยาง iteration statementพิจารณาโปรแกรม readpdb.f

Page 8: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

88

15

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

idmax = 155 read(10,50,end=555)keywrd,nat,atn,ren,chain,nres,

+ xcrd,ycrd,zcrd50 format(A6,I5,2X,A4,A3,1X,A1,I4,4X,3F8.3)

if(keywrd .eq. 'ATOM ') thencall chain2id(idch,chain)natom(idch) = natom(idch)+1numat(natom(idch),idch) = natatmnam(natom(idch),idch) = atnresnam(natom(idch),idch) = rennumres(natom(idch),idch) = nresxatm(natom(idch),idch) = xcrdyatm(natom(idch),idch) = ycrdzatm(natom(idch),idch) = zcrdidmax= max(idmax,idch)

elseendifgoto 55

555 itotchn = idmaxwrite(*,'(/,"Total chain = ",i2)') itotchn

16

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

do i = 1,itotchncall id2chain(i,chain)write(*,'(/, " Chain ",a)') chainwrite(*,'(/," Total number of atom = ",i6)')natom(i)

enddo

Page 9: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

99

17

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

Examples:

WRONG

GOTO 10. . . DO 20, I = 1,5. . .

10 . . .. . .

20 CONTINUE

is wrong, but

Examples:

RIGHT

DO 20, I = 1,5. . .

10 . . . . . . IF (...) GOTO 10

.IF (...) GOTO 30. . .

20 CONTINUE. . .

30 . . .

ขอควรระวังในการใช do-statement

√×

1. Increment must not be zero.2. การใชคําสั่ง GOTO รวมกับ DO

18

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

Valid Invalid

DO 20 ... DO 20 ... ... ...

DO 10 ... DO 10 ... ... ...

10 CONTINUE 20 CONTINUE ... ...

20 CONTINUE 10 CONTINUE

3. การซอน do-statement

√ ×

Page 10: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1010

19

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

PROGRAM TABLES

DO 20, I = 2,12

PRINT *,I,' TIMES TABLE'

DO 10, J = 1,12

PRINT *,I,' TIMES',J,' IS',I*J

10 CONTINUE

20 CONTINUE

END

ตัวอยางโปรแกรมเขียนสูตรคูณแม 2 ถึง 12

20

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

6. Arrays6.1 Array declarations:

INTEGER AGE(100),NUM(25),DEGDIMENSION AGE(100),NUM(25)

integer numat(maxnatm,maxchain),numres(maxnatm,maxchain),

+ natom(maxnatm)

character*4 atmnam(maxnatm,maxchain),atn

character*3 resnam(maxnatm,maxchain),ren

double precision xatm(maxnatm,maxchain),yatm(maxnatm,maxchain),

+ zatm(maxnatm,maxchain)

ตัวอยาง โปรแกรม array.f

Page 11: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1111

21

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

DATA A,B,N/1.0,2.0,17/

DATA N1,N2,N3,N4/4*0/

DATA A(1),A(2),A(3),A(4)/4*0.0/,A(20)/-1.0/

6.2 The DATA statement (useful for initializing arrays)DATA variable_list/constant_list/

22

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

PROGRAM ARRAYS

DOUBLE PRECISION A(5)

N = 5

PRINT *, "ENTER VALUES:"

READ *, (A(I),I=1,N)

PRINT *, (A(I), I =1,N),(A(I)*5,I=1,N)

END

6.3 Input and output of arrays

ตัวอยาง

Page 12: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1212

23

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

PROGRAM MULTIPLE_ARRAYS

DOUBLE PRECISION A(5,12)

N = 5

M = 12

PRINT *, "ENTER VALUES:"

READ *, X

DO I = 1,N

DO J = 1, M

A(I,J) = X*I*J

PRINT *,X," x ",I, " x ", J, " = ",A(I,J)

ENDDO

ENDDO

END

6.4 Multi-dimensional arrays:INTEGER MARK(5,100), REAL A(10,20), CHARACTER*4 C(1,5) ตัวอยาง

24

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

7. Input/output statementsคําสั่ง ความหมาย

OPEN - open a file before the program can use it.CLOSE - close a file. A file that has not been closed can

usually not be read.READ - inputWRITE - outputPRINT - previously output to line printer, now a synonym to

WRITE. It works on a standard unit.INQUIRE - inquires about file status.REWIND - rewinds a file to the beginning.BACKSPACE - rewinds a file one record.ENDFILE - marks end of file.FORMAT - Fortran speciality (see below).

Page 13: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1313

25

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

c ======================================================

c open input & output pdb file

open(unit=10,file=filnam,status='old')

open(unit=7,file='pdbout')

7.1 The OPEN & CLOSE statementOPEN(openlist) OPEN(8, FILE='MYFILE.DAT')CLOSE(unit)CLOSE(8)

ตัวอยาง

26

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

READ(UNIT=5, FMT=100) X,Y,Z

READ(FMT=100, UNIT=5) X,Y,Z

READ(5, FMT=100) X,Y,Z

READ(5, 100) X,Y,Z

READ(*, 100) X,Y,Z

WRITE(*,*) X,Y,Z

WRITE(6,100),X,Y,Z

7.2 The READ & WRITE statementsREAD (cilist) input_listWRITE(cilist) output_list

ตัวอยางcilist is a list of input-output specifiers : unit, fmt, err, end

Page 14: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1414

27

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

FORMAT LETTERS:A - text string: A4 (4 positions or 4 characters)

D - double precision numbers, exponent notation

E - real numbers, exponent notation

F - real numbers, fixed point format: F8.3 (8 positions, out of which 3 are number used for the fractional part)

I – integer: I5 (5 positions reserved)

X - horizontal skip (space): 7X (skip for 7 positions)

/ - vertical skip (newline)

7.3 The FORMAT statementslabel FORMAT (specification_list)

28

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

55 read(10,50,end=555)keywrd,nat,atn,ren,chain,nres,+ xcrd,ycrd,zcrd

50 format(A6,I5,2X,A4,A3,1X,A1,I4,4X,3F8.3)

7.3 The FORMAT statementslabel FORMAT (specification_list)

ตัวอยาง

Page 15: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1515

29

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

Name Type Definition

ABS(IR) Generic Absolute value: |IR| ACOS(R) REAL arccos(R)ASIN(R) REAL arcsin(R) ATAN(R) REAL arctan(R) COS(R) REAL cos(R) DIM(IR1,IR2) Generic Positive difference: MAX(IR1-IR2,0)EXP(R) REAL eRINT(R) INTEGER INTEGER portion of R LOG(R) REAL Natural logarithm: logeRLOG10(R) REAL Common logarithm: log10R MAX(IR1,IR2,...) Generic Largest of IR1,IR2,... MIN(IR1,IR2,...) Generic Smallest of IR1,IR2,... SIN(R) REAL sin(R) SQRT(R) REAL R TAN(R) REAL tan(R)

8 Functions and subroutines8.1 Intrinsic functions

30

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

SUBROUTINE name[(argument_list)]SUBROUTINE name

8.2 Subroutines : a subprogram

A subroutine is referenced by a CALL statement, which has the form: CALL name[(argument_list)]

main programcall chain2id(idch,chain)ENDC======================================================

SUBROUTINE CHAIN2ID(IDCH,CHAIN)INTEGER IDCHCHARACTER*1 CHAIN

IF(CHAIN.EQ.'A' .or. chain.eq.' ') THENIDCH = 1

ELSE IF(CHAIN.EQ.'B') THENIDCH = 2

ELSE IF(CHAIN.EQ.'C') THENIDCH = 3

ELSE IF(CHAIN.EQ.'D') THENIDCH = 4

ENDIFRETURNEND

Page 16: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1616

31

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

r1

r2

r3

v2

v11

2 3

4

การคํานวณมุมทอรชัน

v1. v2 = |v1| |v2| cos θ

มุมทอรชัน (θ)

v1 = r1 x r2

v2 = -r2 x r3

32

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS

ตัวอยาง โปรแกรม Torsion_AngleProgram Torsion_Angle

implicit real(a-h,o-z), integer(i-n)

write(*,'(/,4x,"Enter xyz cooridnates of the four atoms")')

read(*,*) x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4 call

torsion(x1,x2,x3,x4,y1,y2,y3,y4,z1,z2,z3,z4,phi)write(*,'("Torsion angle = ",f6.1)') phi

print *, 'Done'99 stop

endC======================================================

SUBROUTINE torsion(x1,x2,x3,x4,y1,y2,y3,y4,z1,z2,z3,z4,tor)

subroutine caldist(X1,X2,Y1,Y2,Z1,Z2,R)

Page 17: Chulalongkorn University 1. What is Fortran?pioneer.netserv.chula.ac.th/~spornthe/2302664/lecture_3.pdf · 1. What is Fortran? - a general purpose programming language, mainly intended

1717

33

ChulalongkornChulalongkorn UniversityUniversityC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lChulalongkornChulalongkorn UniversityUniversity

PS

ปฏิบัติการ

1. เขียนและแกไขโปรแกรมตามที่กําหนดให

Assignment1. แบบฝกหัด 2. แกไขโปรแกรม

34

ChulalongkornChulalongkorn UniversityUniversity

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

Com

puta

tiona

l Che

mis

try

Uni

t Cel

lC

ompu

tatio

nal C

hem

istr

y U

nit C

ell

ChulalongkornChulalongkorn UniversityUniversity

PS