Statistics
| Revision:

root / utils / Xyz2Path.f @ 3

History | View | Annotate | Download (28 kB)

1
          program Xyz2Path
2
! This programs reads a XYZ file and converts it into distances,
3
! valence angle and dihedral angles.
4
! It prints them as a function of the irc distance...
5
!-----------------------------------------------
6
! Input: name of the XYZ File
7
! it also needs a file call list which has the following structure:
8
! one line contains the type of the value you want to follow, it can be
9
! b  for a Bond distance
10
! a for an angle
11
! d for a dihedral
12
! this descriptor is followed by the number of the atoms involved !
13
! a typical file can be:
14
! b  1  2
15
! b 2  3
16
! a 1 2 3
17
!----------------------------------------------
18
! Ouput: A files call Scan.dat
19
! wich contains in the first lines the input file (as a reminder)
20
! and then for each step the wanted values
21
!------------------------------------------------
22
! Second version also reads the energy (as to be written after E= on
23
! the comment line)
24
!------------------------------------------------
25
! Third version contains a new command: c for Center of Mass
26
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27
! v 3.1 the c command now creates the center of mass... and allows
28
! people to do whatever they want with it...
29
! Syntax: c NbAt ListAt
30

    
31

    
32
          IMPLICIT NONE
33

    
34
          INCLUDE "Xyz2Path.param"
35

    
36
          character*40 f1
37
          REAL*8   geos(3,maxnat), geos1(3,maxnat)
38
          character*33 fmt
39
          character*3 atoms(maxnat)
40
          character*5 Type
41
          Character*120 line
42
          INTEGER*4 NbPrint
43
          REAL*8   AU2PS,Pi
44
          REAL*8  Mass(MaxNat), Ener, Conv, Ds, s
45
          REAL*8  MassAt(0:86)
46
          INTEGER*4 At1,At2,At3,At4,IOOUT,Iat
47
          INTEGER*4  IArg, I, NNN, Ng, J
48

    
49
          INTEGER*4 Nat,NbDist, NbAngle, NbDie,NbCOM
50
          INTEGER*4 At1B(MaxNat),At2B(MaxNat)
51
          INTEGER*4 At1A(MaxNat),At2A(MaxNat),At3A(MaxNat)
52
          INTEGER*4 At1D(MaxNat),At2D(MaxNat),At3D(MaxNat),At4D(MaxNat)
53
          INTEGER*4 AtCom(0:MaxNat,MaxNat)
54
          REAL*8    VB(MaxNat),VA(MaxNat),VD(MaxNat),VCOM(MaxNat)
55

    
56
          REAL*8 MRot(3,3), Rmsd
57
          LOGICAL FExist,FRot,FAlign,Debug
58

    
59
          INTEGER*4 ConvertNumAt
60
          external ConvertNumAt
61

    
62
          COMMON /Indices/Nat,NbDist,NbAngle,NbDie,NbCom, At1B,At2B,
63
     &         At1A,At2A,At3A,At1D,At2D,At3D,At4D,AtCom
64
          COMMON /Values/VB,VA,VD,VCom
65
          COMMON /Const/AU2ps,Pi
66
 
67
          DATA MassAt/0.0D0,                                             
68
     $           1.0078D0,                      4.0026D0,                
69
     $            7.0160D0, 9.0122D0,11.0093D0,                          
70
     $           12.0000D0,14.0031D0,15.9949D0,18.9984D0,19.9924D0,      
71
     $           22.9898D0,23.9850D0,26.9815D0,                          
72
     $           27.9769D0,30.9738D0,31.9721D0,34.9688D0,39.9624D0,      
73
     $           39.0983D0,40.08D0,                                      
74
     $             44.9559D0, 47.88D0, 50.9415D0, 51.996D0, 54.9380D0,   
75
     $             55.847D0, 58.9332D0, 58.69D0, 63.546D0, 65.39D0,      
76
     $           69.72D0,72.59D0,74.9216D0,78.96D0,79.904D0,83.80D0,     
77
     $           85.4678D0,87.62D0,88.9059D0,91.224D0,92.9064D0,         
78
     $   95.94D0,98D0,101.07D0,102.906D0,106.42D0,107.868D0,112.41D0,    
79
     $   114.82D0,118.71D0,121.75D0,127.60D0,126.905D0,131.29D0,          
80
!     6           'CS','BA',
81
     $           132.905,137.34,                                          
82
!     6       'LA',
83
!               'CE','PR','ND','PM','SM','EU','GD',
84
!               'TB','DY','HO', 'ER','TM','YB','LU',
85
     $      138.91,                                                       
86
     $       140.12, 130.91, 144.24,147.,150.35, 151.96,157.25,           
87
     $       158.924, 162.50, 164.93, 167.26,168.93,173.04,174.97,        
88
!     6                'HF','TA',' W','RE','OS','IR','PT',
89
!                      'AU','HG',
90
!     6                                 'TL','PB','BI','PO','AT','RN'/
91
     $       178.49, 180.95, 183.85, 186.2, 190.2, 192.2, 195.09,         
92
     $       196.97, 200.59,                                              
93
     $       204.37, 207.19,208.98,210.,210.,222. /
94

    
95

    
96
          AU2PS=1./41341.37
97
          NbPrint=100
98
          Pi=dacos(-1.d0)
99
          IOOUT=13
100
          Conv=1.
101
          FRot=.TRUE.
102
          FAlign=.TRUE.
103
          Debug=.FALSE.
104
          NbDist=0
105
          NbAngle=0
106
          NbDie=0
107

    
108
          iarg=command_argument_count()
109
          if (iarg.lt.1) then
110
                 write(*,*) 'XYZ filename:'
111
              read(*,'(a)') f1
112
            else
113
              call getarg(1,f1)
114
          endif
115

    
116
          open(13,file='Scan.dat')
117

    
118
          INQUIRE(File='list',Exist=FExist)
119
          if (.NOT.FExist) THEN
120
             WRITE(*,*) "No file 'list', just printing Energy"
121
          END IF
122

    
123
          open(11,file=f1)
124
          call rtraitem(11,nnn,ener,geos1,atoms)
125
          close(11)
126

    
127
         DO I=1,nnn
128
            Iat=ConvertNumAt(atoms(I))
129
            Mass(I)=MassAt(Iat)
130
!           write(*,*) I,Atoms(I),Mass(I),geos1(:,I)
131
         END DO
132

    
133
         Nat=nnn
134

    
135
         if (FExist) THEN
136
         open(14,file='list')
137
         Type="d"
138
         DO WHILE (Type.ne."E")
139
          CALL READLINE(14,Type,Line)
140
!          WRITE(*,*) Line,Type
141
          if (Type.eq."b") THEN
142
             NbDist=NbDist+1
143
             READ(Line,*) At1,At2
144
             At1B(NbDist)=At1
145
             At2B(NbDist)=At2
146
             WRITE(13,'("# b ",2(A3,I3))') Atoms(At1),At1,Atoms(At2),At2
147
             WRITE(*,'("# b ",2(A3,I3))') Atoms(At1),At1,Atoms(At2),At2
148
          END IF
149
          if (Type.eq."a") THEN
150
             NbAngle=NbAngle+1
151
             READ(Line,*) At1,At2,At3
152
             At1A(NbAngle)=At1
153
             At2A(NbAngle)=At2
154
             At3A(NbAngle)=At3
155
            WRITE(13,'("# a ",3(A3,I3))') Atoms(At1),At1,Atoms(At2),
156
     &            At2, Atoms(At3),At3
157
            WRITE(*,'("# a ",3(A3,I3))') Atoms(At1),At1,Atoms(At2),
158
     &           At2, Atoms(At3),At3
159
          END IF
160
          if (Type.eq."d") THEN
161
             NbDie=NbDie+1
162
             READ(Line,*) At1,At2,At3,At4
163
             At1D(NbDie)=At1
164
             At2D(NbDie)=At2
165
             At3D(NbDie)=At3
166
             At4D(NbDie)=At4
167
            WRITE(13,'("# d ",4(A3,I3))') Atoms(At1),At1,Atoms(At2),At2,  
168
     &          Atoms(At3),At3,Atoms(At4),At4
169
            WRITE(*,'("# d ",4(A3,I3))') Atoms(At1),At1,Atoms(At2),At2,   
170
     &          Atoms(At3),At3,Atoms(At4),At4
171

    
172
          END IF
173
          if (Type.eq."c") THEN
174
            NbCOM=NbCOM+1
175
            READ(Line,*) At1,(AtCom(j,NbCOM),j=1,At1)
176
            AtCom(0,NbCOM)=At1
177
            Atoms(nat+NbCom)="G"
178
            WRITE(13,'("# c ",I4,20(A3,I3))') At1,                  
179
     &          (Atoms(AtCom(i,NbCoM)),AtCom(i,NbCOM),i=1,At1)
180
            WRITE(*,'("# c ",I4,20(A3,I3))') At1,                   
181
     &          (Atoms(AtCom(i,NbCoM)),AtCom(i,NbCOM),i=1,At1)
182
          END IF
183

    
184
         END DO
185
         END IF
186

    
187

    
188

    
189
          fmt='(   (1X,F12.5),1X,F15.6)'
190
          write(fmt(2:4),'(i3)') NbDist+NbAngle+NbDie+1
191
!           write(*,*) nat3
192
!          write(*,*) 'fmt:',fmt
193

    
194
          ng=1
195

    
196
         s=0.
197

    
198
          open(11,file=f1)
199

    
200
10           call rtraitem(11,nnn,ener,geos,atoms)
201
!           WRITE(*,*) nnn
202
           if (nnn.gt.0) then
203

    
204
          call CalcRmsd(nnn, geos1, geos,MRot,rmsd,FRot,FAlign,debug)
205
         ds=0.
206
           DO I=1,nnn
207
             DO J=1,3
208
             ds=ds+mass(I)*(geos(J,I)-geos1(J,I))**2
209
!            write(*,*) I,J,geos(J,I),Geos1(J,I),ds
210
             geos1(J,I)=Geos(J,I)
211
              END DO
212
            END DO
213
           s=s+sqrt(ds)
214

    
215
! We convert coordinates in a0 into Angstroem
216
!              write(*,*) "Analyse !"
217
            if (FExist) THEN
218
            Call Analyse(geos)
219

    
220
             write(IOOUT,fmt) s,(VB(j)/Conv,j=1,NbDist),
221
     &            (VA(j),j=1,NbAngle),
222
     &            (VD(j),j=1,NbDie),ener
223
             write(*,fmt) s,(VB(j)/Conv,j=1,NbDist),
224
     &            (VA(j),j=1,NbAngle),
225
     &            (VD(j),j=1,NbDie),ener
226
             ELSE
227
                        write(IOOUT,fmt) s,ener
228
             write(*,fmt) s,ener
229
            END IF 
230
           ng=ng+1
231
            goto 10
232
           endif
233
           WRITE(*,*) 'Found ',ng-1,' geometries'
234
           close(11)
235
         end
236

    
237
!--------------------------------------------------------------
238
          subroutine rtraitem(ifil,nnn,E,tab,atoms)
239
!           implicit real*8 (a-h,o-z)
240
            IMPLICIT NONE
241
           character*120 line
242
           character*3 Atoms(*)
243
           integer*4 nnn, IFil, Idx, I, J
244
           REAL*8  Tab(3,*), E
245

    
246

    
247
           read(ifil,*,err=99,end=99) nnn
248
           read(ifil,'(A)') line
249
           idx=index(line,'E')
250
!           WRITE(*,*) 'idx,line',idx,line
251
           if (idx/=0) THEN
252
             line=line(idx+2:)
253
              idx=index(line,"=")
254
              if (idx/=0) line=line(idx+1:)
255
              idx=index(line,":")
256
              if (idx/=0) line=line(idx+1:)
257
              read(line,*) E
258
           ELSE
259
             E=0.
260
           END IF
261
           
262
!           write(*,*) 'coucou',line
263
           do i=1,nnn
264
              read(ifil,'(A)',err=99,end=99) Line
265
              do while (line(1:1)==' ')
266
                 line=line(2:)
267
              end do
268
              atoms(i)=line(1:3)
269
!            write(*,*) 'coucou atoms',atoms(i)
270
              do while (line(1:1).ne.' ')
271
                 line=line(2:)
272
              end do
273
!              WRITE(*,*) 'coucou2:',i,line
274
              read(line,*) (tab(j,i),j=1,3)
275
           end do
276
!           WRITE(*,*) 'coucou:',nnn,tab(1,1)
277
           return
278
99           nnn=0
279
!             WRITE(*,*) 'Erreur lecture',ifil,nnn
280
             return
281
          end
282

    
283
!--------------------------------------------------------------
284

    
285
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
286
! READLINE
287
! This subroutine reads a line for a file, and converts
288
! the first field into a character variable, and the rest into 4 integers
289
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
290

    
291
      SUBROUTINE READLINE(IOIN,Type,Line)
292
        
293
       IMPLICIT NONE
294
      CHARACTER Type*5,LINE*120
295
      INTEGER i,IOIN
296
      READ(IOIN,'(A120)',ERR=999,END=999) LINE
297
!      WRITE(*,*) Line
298
      DO WHILE (LINE(1:1).eq.' ')
299
         LINE=LINE(2:)
300
      END DO
301

    
302
      i=1
303
      DO WHILE (LINE(i:i).ne.' ')
304
         i=i+1
305
      END DO
306
      
307
      if (i.ge.6) THEN
308
         WRITE(*,*) 'Pb with READLINE:',LINE
309
         GOTO 999
310
      END IF
311
      Type=LINE(1:i-1)
312
      LINE=LINE(i:120) // " 0 0 0 0"
313

    
314
      RETURN
315
 999  Type="E"
316
      END
317

    
318

    
319

    
320
      SUBROUTINE Analyse(geos)
321

    
322
      IMPLICIT NONE
323
          INCLUDE "Xyz2Path.param"
324

    
325
      REAL*8 geos(3,maxnat)
326
      REAL*8 AU2PS,Pi
327
      INTEGER*4 i,j,k
328
      REAL*8 V1x,V1y,V1z,V2x,V2y,V2z,V3x,V3y,V3z
329
      REAL*8 d1,d2,ca,sa
330
      REAL*8 V4x,v4y,v4z,v5x,v5y,v5z
331
      REAL*8 COG(3)
332

    
333
      INTEGER*4 Nat,NbDist, NbAngle, NbDie,NbCOM
334
      INTEGER*4 At1B(MaxNat),At2B(MaxNat)
335
      INTEGER*4 At1A(MaxNat),At2A(MaxNat),At3A(MaxNat)
336
      INTEGER*4 At1D(MaxNat),At2D(MaxNat),At3D(MaxNat),At4D(MaxNat)
337
      INTEGER*4 AtCom(0:MaxNat,MaxNat)
338
      REAL*8    VB(MaxNat),VA(MaxNat),VD(MaxNat),VCOM(MaxNat)
339

    
340
      COMMON /Indices/Nat,NbDist,NbAngle,NbDie,NbCom,   
341
     &      At1B,At2B,At1A,At2A,At3A,At1D,At2D,At3D,At4D,AtCom
342
      COMMON /Values/VB,VA,VD,VCom
343
      COMMON /Const/AU2ps,Pi
344

    
345
          DO I=1,Nat
346
             WRITE(*,'(1X,I3,3(1X,F15.6))') i,(geos(j,i),j=1,3)
347
          END DO
348

    
349
! First, we create the Centre of Mass atoms
350
            DO i=1,NbCOM
351
              COG(1)=0.
352
              COG(2)=0.
353
              COG(3)=0.
354
              DO j=1,AtCOm(0,i)
355
                 DO k=1,3
356
                    COG(k)=COG(k)+geos(k,AtCom(j,i))
357
                 END DO
358
              END DO
359
              DO k=1,3
360
                 COG(k)=COG(k)/AtCOM(0,i)
361
                 geos(k,Nat+i)=COG(k)
362
              END DO
363
            END DO
364

    
365

    
366
            DO i=1,NbDist
367
               VB(i)=sqrt((geos(1,At1B(i))-geos(1,At2B(i)))**2+  
368
     &      (geos(2,At1B(i))-geos(2,At2B(i)))**2+                     
369
     &      (geos(3,At1B(i))-geos(3,At2B(i)))**2)
370
            END DO
371
            DO i=1,NbAngle
372
               v1x=geos(1,At1A(i))-geos(1,At2A(i))
373
               v1y=geos(2,At1A(i))-geos(2,At2A(i))
374
               v1z=geos(3,At1A(i))-geos(3,At2A(i))
375
               d1=sqrt(v1x**2+v1y**2+v1z**2)
376
               v2x=geos(1,At3A(i))-geos(1,At2A(i))
377
               v2y=geos(2,At3A(i))-geos(2,At2A(i))
378
               v2z=geos(3,At3A(i))-geos(3,At2A(i))
379
               d2=sqrt(v2x**2+v2y**2+v2z**2)
380
               VA(i)=acos((v1x*v2x+v1y*v2y+v1z*v2z)/(d1*d2))*180./Pi
381
            END DO
382
            DO i=1,NbDie
383
!               WRITE(*,*) At1D(i),At2D(i),At3D(i),At4D(i)
384
!              WRITE(*,*) geos(1,At1D(i)),geos(2,At1D(i)),geos(3,At1D(i))
385
!              WRITE(*,*) geos(1,At2D(i)),geos(2,At2D(i)),geos(3,At2D(i))
386
               v1x=geos(1,At1D(i))-geos(1,At2D(i))
387
               v1y=geos(2,At1D(i))-geos(2,At2D(i))
388
               v1z=geos(3,At1D(i))-geos(3,At2D(i))
389
               v2x=geos(1,At3D(i))-geos(1,At2D(i))
390
               v2y=geos(2,At3D(i))-geos(2,At2D(i))
391
               v2z=geos(3,At3D(i))-geos(3,At2D(i))
392
               v3x=geos(1,At4D(i))-geos(1,At3D(i))
393
               v3y=geos(2,At4D(i))-geos(2,At3D(i))
394
               v3z=geos(3,At4D(i))-geos(3,At3D(i))
395
          
396
               v4x=v1y*v2z-v1z*v2y
397
               v4y=v1z*v2x-v1x*v2z
398
               v4z=v1x*v2y-v1y*v2x
399
               d1=sqrt(v4x**2+v4y**2+v4z**2)
400
               v5x=-v2y*v3z+v2z*v3y
401
               v5y=-v2z*v3x+v2x*v3z
402
               v5z=-v2x*v3y+v2y*v3x
403
               d2=sqrt(v5x**2+v5y**2+v5z**2)
404
               ca=(v4x*v5x+v4y*v5y+v4z*v5z)/(d1*d2)
405
               sa=v1x*v5x+v1y*v5y+v1z*v5z
406
               VD(i)=acos(ca)*180./Pi
407
               if (sa<0.) VD(i)=-VD(i)
408
!               WRITE(*,*) "Dihe",v5x,v5y,v5z,v4x,v4y,v4z,d1,d2,
409
!     &(v4x*v5x+v4y*v5y+v4z*v5z)/(d1*d2),pi
410
!!!!!!!!! Another solution, more elegant ?
411
!               norm2=sqrt(v2x**2+v2y**2+v2z**2)
412
!               sa=(v4x*(v5y*v2z-v5z*v2y) 
413
!     *            -v4y*(v5x*v2z-v5z*v2x) 
414
!     *            +v4z*(v5x*v2y-v5y*v2x))
415
!     *       /(d1*norm2*d2)
416
!               angle_d=datan2(sa,ca)*180./Pi
417
!               WRITE(*,*) sa,ca,angle_d,d1,d2,norm2
418
!               WRITE(*,*) VD(i),angle_d
419
            END DO
420

    
421
        END
422

    
423
C================================================================
424
C    Convertit un nom d'atome (2 lettres) en nombre de masse (entier)
425
C cette fonction a ete modifiee pour pouvoir convertir un nom
426
C d'atome avec un numero a la fin...
427
C================================================================
428

    
429
        FUNCTION ConvertNumAt(ATOM)
430

    
431

    
432
        IMPLICIT NONE 
433

    
434
        INTEGER*4 I,Long,ConvertNumAt,IC
435
        character*2 ATOM*2,ATOME*3,L_Atom*2
436
        INTEGER*4  Max_Z
437
        PARAMETER (Max_Z=86)
438
        CHARACTER*2  Nom(0:Max_Z)
439

    
440
       DATA NOM/ ' X',' H',                                    'HE',
441
     $          'LI','BE',            ' B',' C',' N',' O',' F','NE',
442
     $          'NA','MG',            'AL','SI',' P',' S','CL','AR',
443
     $          ' K','CA',
444
     $      'SC','TI',' V','CR','MN','FE','CO','NI','CU','ZN',
445
     $                                'GA','GE','AS','SE','BR','KR',
446
     $          'RB','SR',
447
     $      ' Y','ZR','NB','MO','TC','RU','RH','PD','AG','CD',
448
     $                                'IN','SN','SB','TE',' I','XE',
449
     $          'CS','BA',
450
     $      'LA',
451
     $        'CE','PR','ND','PM','SM','EU','GD','TB','DY','HO',
452
     $        'ER','TM','YB','LU',
453
     $               'HF','TA',' W','RE','OS','IR','PT','AU','HG',
454
     $                                'TL','PB','BI','PO','AT','RN'/
455

    
456

    
457
C Verifie qu'il n'y a que des lettres et des espaces dans ATOM
458
!        WRITE(*,*) 'DBG CNVNUMAT, ATOM:',ATOM
459

    
460
        L_atom=Atom
461
        IF (ATOM(1:1).LT.'A') L_ATOM(1:1)=' '
462
        IC=Ichar(ATOM(1:1))
463
        IF ((ic.le.123).AND.(ic.ge.97)) L_ATOM(1:1)=CHAr(IC-32)
464
        IF (ATOM(2:2).LT.'A') L_ATOM(2:2)=' '
465
        IC=Ichar(ATOM(2:2))
466
        IF ((ic.le.123).AND.(ic.ge.97)) L_ATOM(2:2)=CHAr(IC-32)
467
C Justifie le nom sur la droite (et non sur la gauche comme souvent...)
468
         Long=INDEX(L_ATOM,' ')-1
469
         ATOME=' ' // L_ATOM
470
        IF (Long.EQ.1) L_ATOM=ATOME(1:2)
471
!        WRITE(*,*) 'DBG CNVNUMAT, L_ATOM:',L_ATOM
472
        I=max_Z
473
        DO WHILE ((nom(I).NE.L_ATOM) .AND. (I.GT.0))
474
         I=I-1
475
        END DO
476
        ConvertNumAT=I
477
        END
478
! This subroutine calculates RMSD using quaternions.
479
! It is based on the F90 routine bu E. Coutsias
480
! http://www.math.unm.edu/~vageli/homepage.html
481
! I (PFL) have just translated it, and I have changed the diagonalization
482
! subroutine.
483
! I also made some changes to make it suitable for Cart package.
484
!----------------------------------------------------------------------
485
!----------------------------------------------------------------------
486
! Copyright (C) 2004, 2005 Chaok Seok, Evangelos Coutsias and Ken Dill
487
!      UCSF, Univeristy of New Mexico, Seoul National University
488
! Witten by Chaok Seok and Evangelos Coutsias 2004.
489
                                                                                
490
! This library is free software; you can redistribute it and/or
491
! modify it under the terms of the GNU Lesser General Public
492
! License as published by the Free Software Foundation; either
493
! version 2.1 of the License, or (at your option) any later version.
494
!
495
                                                                                
496
! This library is distributed in the hope that it will be useful,
497
! but WITHOUT ANY WARRANTY; without even the implied warranty of
498
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
499
! Lesser General Public License for more details.
500
!
501
                                                                                
502
! You should have received a copy of the GNU Lesser General Public
503
! License along with this library; if not, write to the Free Software
504
! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
505
!----------------------------------------------------------------------------
506

    
507
      subroutine CalcRmsd(na, geom,geom2,U,rmsd,FRot,FAlign,debug)
508
!-----------------------------------------------------------------------
509
!  This subroutine calculates the least square rmsd of two coordinate
510
!  sets coord1(3,n) and coord2(3,n) using a method based on quaternion.
511
!  It then calculate the  rotation matrix U and the centers of coord, and uses
512
! them to align the two molecules.
513
!-----------------------------------------------------------------------
514

    
515

    
516
      IMPLICIT NONE
517

    
518
      INCLUDE "Xyz2Path.param"
519

    
520
      INTEGER*4 IOIN, IOOUT, IOSCRN, IOPRNT, IOTAMP
521
      COMMON/IODEFS/IOIN,IOOUT,IOSCRN,IOPRNT,IOTAMP
522

    
523
      integer*4 na
524
      real*8  geom(3,MaxNAt), Geom2(3,MaxNAt)
525
      real*8  U(3,3), rmsd
526
      LOGICAL  FRot,FAlign,Debug
527

    
528
      REAL*8 Coord1(3,MaxNAt), Coord2(3,MaxNAt)
529
      real*8  xc1,yc1,zc1, xc2,yc2,zc2
530
      
531

    
532
      integer*4 i, j,  ia
533
      real*8 x_norm, y_norm, lambda
534
      real*8 Rmatrix(3,3)
535
      real*8 S(4,4)
536
      real*8 EigVec(4,4), EigVal(4)
537

    
538

    
539

    
540
  ! calculate the barycenters, centroidal coordinates, and the norms
541
      x_norm = 0.0d0
542
      y_norm = 0.0d0
543
      xc1=0.
544
      yc1=0.
545
      zc1=0.
546
      xc2=0.
547
      yc2=0.
548
      zc2=0.
549
      do ia=1,na
550
         xc1=xc1+geom(1,ia)
551
         xc2=xc2+geom2(1,ia)
552
         yc1=yc1+geom(2,ia)
553
         yc2=yc2+geom2(2,ia)
554
         zc1=zc1+geom(3,ia)
555
         zc2=zc2+geom2(3,ia)
556
!         if (debug) WRITE(*,'(A,I5,4(1X,F10.4))') 'ia...',ia,x(ia),
557
!     &                        x2(ia),xc1,xc2
558
      END DO
559
      xc1=xc1/dble(na)
560
      yc1=yc1/dble(na)
561
      zc1=zc1/dble(na)
562
      xc2=xc2/dble(na)
563
      yc2=yc2/dble(na)
564
      zc2=zc2/dble(na)
565

    
566
      IF (debug) WRITE(*,'(1X,A,3(1X,F10.4))') 'Center1',xc1,yc1,zc1
567
      IF (debug) WRITE(*,'(1X,A,3(1X,F10.4))') 'Center2',xc2,yc2,zc2
568
      do i=1,na
569
         Coord1(1,i)=geom(1,i)-xc1
570
         Coord1(2,i)=geom(2,i)-yc1
571
         Coord1(3,i)=geom(3,i)-zc1
572
         Coord2(1,i)=geom2(1,i)-xc2
573
         Coord2(2,i)=geom2(2,i)-yc2
574
         Coord2(3,i)=geom2(3,i)-zc2
575
         x_norm=x_norm+Coord1(1,i)**2+Coord1(2,i)**2+Coord1(3,i)**2
576
         y_norm=y_norm+Coord2(1,i)**2+Coord2(2,i)**2+Coord2(3,i)**2
577
      end do
578

    
579
      IF (debug) THEN
580
         WRITE(*,*) "R matrix"
581
         DO I=1,3
582
            WRITE(*,*) (RMatrix(I,j),j=1,3)
583
         END DO
584
      END IF
585

    
586
  ! calculate the R matrix
587
      do i = 1, 3
588
         do j = 1, 3
589
            Rmatrix(i,j)=0.
590
            do ia=1,na
591
               Rmatrix(i,j) = Rmatrix(i,j)+Coord1(i,ia)*Coord2(j,ia)
592
            END DO
593
         end do
594
      end do
595

    
596
      IF (debug) THEN
597
         WRITE(*,*) "R matrix"
598
         DO I=1,3
599
            WRITE(*,*) (RMatrix(I,j),j=1,3)
600
         END DO
601
      END IF
602

    
603

    
604
  ! S matrix
605
      S(1, 1) = Rmatrix(1, 1) + Rmatrix(2, 2) + Rmatrix(3, 3)
606
      S(2, 1) = Rmatrix(2, 3) - Rmatrix(3, 2)
607
      S(3, 1) = Rmatrix(3, 1) - Rmatrix(1, 3)
608
      S(4, 1) = Rmatrix(1, 2) - Rmatrix(2, 1)
609

    
610
      S(1, 2) = S(2, 1)
611
      S(2, 2) = Rmatrix(1, 1) - Rmatrix(2, 2) - Rmatrix(3, 3)
612
      S(3, 2) = Rmatrix(1, 2) + Rmatrix(2, 1)
613
      S(4, 2) = Rmatrix(1, 3) + Rmatrix(3, 1)
614

    
615
      S(1, 3) = S(3, 1)
616
      S(2, 3) = S(3, 2)
617
      S(3, 3) =-Rmatrix(1, 1) + Rmatrix(2, 2) - Rmatrix(3, 3)
618
      S(4, 3) = Rmatrix(2, 3) + Rmatrix(3, 2)
619

    
620
      S(1, 4) = S(4, 1)
621
      S(2, 4) = S(4, 2)
622
      S(3, 4) = S(4, 3)
623
      S(4, 4) =-Rmatrix(1, 1) - Rmatrix(2, 2) + Rmatrix(3, 3) 
624

    
625

    
626
! PFL : I use my usual Jacobi diagonalisation
627
  ! Calculate eigenvalues and eigenvectors, and 
628
  ! take the maximum eigenvalue lambda and the corresponding eigenvector q.
629

    
630
      IF (debug) THEN
631
         WRITE(*,*) "S matrix"
632
         DO I=1,4
633
            WRITE(*,*) (S(I,j),j=1,4)
634
         END DO
635
      END IF
636

    
637
      Call Jacobi(S,4,EigVal,EigVec,4)
638

    
639
      Call Trie(4,EigVal,EigVec,4)
640

    
641
      Lambda=EigVal(4)
642

    
643
! RMS Deviation
644
       rmsd=sqrt(max(0.0d0,((x_norm+y_norm)-2.0d0*lambda))/dble(na))
645

    
646
      if (FRot.OR.FAlign) Call rotation_matrix(EigVec(1,4),U)
647
      IF (FAlign) THEN
648
         DO I=1,na
649
            geom2(1,i)=Coord2(1,i)*U(1,1)+Coord2(2,i)*U(2,1)
650
     &           +Coord2(3,i)*U(3,1) +xc1
651
            geom2(2,i)=Coord2(1,i)*U(1,2)+Coord2(2,i)*U(2,2)
652
     &           +Coord2(3,i)*U(3,2) +yc1
653
            geom2(3,i)=Coord2(1,i)*U(1,3)+Coord2(2,i)*U(2,3)
654
     &           +Coord2(3,i)*U(3,3) +zc1
655
         END DO
656
      END IF
657

    
658
      END
659

    
660

    
661
!-----------------------------------------------------------------------
662
      subroutine rotation_matrix(q, U)
663
!-----------------------------------------------------------------------
664
! This subroutine constructs rotation matrix U from quaternion q.
665
!-----------------------------------------------------------------------
666
! This subroutine calculates RMSD using quaternions.
667
! It is based on the F90 routine bu E. Coutsias
668
! http://www.math.unm.edu/~vageli/homepage.html
669
! I (PFL) have just translated it, and I have changed the diagonalization
670
! subroutine.
671
! I also made some changes to make it suitable for Cart package.
672
!----------------------------------------------------------------------
673
!----------------------------------------------------------------------
674
! Copyright (C) 2004, 2005 Chaok Seok, Evangelos Coutsias and Ken Dill
675
!      UCSF, Univeristy of New Mexico, Seoul National University
676
! Witten by Chaok Seok and Evangelos Coutsias 2004.
677
                                                                                
678
! This library is free software; you can redistribute it and/or
679
! modify it under the terms of the GNU Lesser General Public
680
! License as published by the Free Software Foundation; either
681
! version 2.1 of the License, or (at your option) any later version.
682
!
683
                                                                                
684
! This library is distributed in the hope that it will be useful,
685
! but WITHOUT ANY WARRANTY; without even the implied warranty of
686
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
687
! Lesser General Public License for more details.
688
!
689
                                                                                
690
! You should have received a copy of the GNU Lesser General Public
691
! License along with this library; if not, write to the Free Software
692
! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
693
!----------------------------------------------------------------------------
694

    
695
      real*8 q(4)
696
      real*8 U(3,3)
697
      real*8 q0,q1,q2,q3,b0,b1,b2,b3,q00,q01,q02,q03
698
      REAL*8 q11,q12,q13,q22,q23,q33
699

    
700
      q0 = q(1)
701
      q1 = q(2)
702
      q2 = q(3)
703
      q3 = q(4)
704

    
705
      b0 = 2.0d0*q0
706
      b1 = 2.0d0*q1
707
      b2 = 2.0d0*q2
708
      b3 = 2.0d0*q3
709

    
710
      q00 = b0*q0-1.0d0
711
      q01 = b0*q1
712
      q02 = b0*q2
713
      q03 = b0*q3
714

    
715
      q11 = b1*q1
716
      q12 = b1*q2
717
      q13 = b1*q3  
718
      
719
      q22 = b2*q2
720
      q23 = b2*q3
721
      
722
      q33 = b3*q3 
723
      
724
      U(1,1) = q00+q11
725
      U(1,2) = q12-q03
726
      U(1,3) = q13+q02
727
      
728
      U(2,1) = q12+q03
729
      U(2,2) = q00+q22
730
      U(2,3) = q23-q01
731
      
732
      U(3,1) = q13-q02
733
      U(3,2) = q23+q01
734
      U(3,3) = q00+q33
735
      
736
      end 
737
      
738
c============================================================
739
c
740
c ++  diagonalisation par jacobi
741
c     vecteur propre i : V(i,i)
742
c     valeur propre i : D(i)
743
c
744
c============================================================
745
c 
746
      SUBROUTINE JACOBI(A,N,D,V,max_N)
747
      IMPLICIT REAL*8 (A-H,O-Z)
748
      parameter (max_it=500)
749
      DIMENSION A(max_N,max_N),B(max_N),Z(max_N)
750
      DIMENSION V(max_N,max_N),D(max_N)
751

    
752

    
753
      DO 12 IP=1,N
754
        DO 11 IQ=1,N
755
          V(IP,IQ)=0.
756
11      CONTINUE
757
        V(IP,IP)=1.
758
12    CONTINUE
759
      DO 13 IP=1,N
760
        B(IP)=A(IP,IP)
761
        D(IP)=B(IP)
762
        Z(IP)=0.
763
13    CONTINUE
764
      NROT=0
765
      DO 24 I=1,max_it
766
        SM=0.
767
        DO 15 IP=1,N-1
768
          DO 14 IQ=IP+1,N
769
            SM=SM+ABS(A(IP,IQ))
770
14        CONTINUE
771
15      CONTINUE
772
        IF(SM.EQ.0.)RETURN
773
        IF(I.LT.4)THEN
774
          TRESH=0.2*SM/N**2
775
        ELSE
776
          TRESH=0.
777
        ENDIF
778
        DO 22 IP=1,N-1
779
          DO 21 IQ=IP+1,N
780
            G=100.*ABS(A(IP,IQ))
781
            IF((I.GT.4).AND.(ABS(D(IP))+G.EQ.ABS(D(IP)))
782
     *         .AND.(ABS(D(IQ))+G.EQ.ABS(D(IQ))))THEN
783
              A(IP,IQ)=0.
784
            ELSE IF(ABS(A(IP,IQ)).GT.TRESH)THEN
785
              H=D(IQ)-D(IP)
786
              IF(ABS(H)+G.EQ.ABS(H))THEN
787
                T=A(IP,IQ)/H
788
              ELSE
789
                THETA=0.5*H/A(IP,IQ)
790
                T=1./(ABS(THETA)+SQRT(1.+THETA**2))
791
                IF(THETA.LT.0.)T=-T
792
              ENDIF
793
              C=1./SQRT(1+T**2)
794
              S=T*C
795
              TAU=S/(1.+C)
796
              H=T*A(IP,IQ)
797
              Z(IP)=Z(IP)-H
798
              Z(IQ)=Z(IQ)+H
799
              D(IP)=D(IP)-H
800
              D(IQ)=D(IQ)+H
801
              A(IP,IQ)=0.
802
              DO 16 J=1,IP-1
803
                G=A(J,IP)
804
                H=A(J,IQ)
805
                A(J,IP)=G-S*(H+G*TAU)
806
                A(J,IQ)=H+S*(G-H*TAU)
807
16            CONTINUE
808
              DO 17 J=IP+1,IQ-1
809
                G=A(IP,J)
810
                H=A(J,IQ)
811
                A(IP,J)=G-S*(H+G*TAU)
812
                A(J,IQ)=H+S*(G-H*TAU)
813
17            CONTINUE
814
              DO 18 J=IQ+1,N
815
                G=A(IP,J)
816
                H=A(IQ,J)
817
                A(IP,J)=G-S*(H+G*TAU)
818
                A(IQ,J)=H+S*(G-H*TAU)
819
18            CONTINUE
820
              DO 19 J=1,N
821
                G=V(J,IP)
822
                H=V(J,IQ)
823
                V(J,IP)=G-S*(H+G*TAU)
824
                V(J,IQ)=H+S*(G-H*TAU)
825
19            CONTINUE
826
              NROT=NROT+1
827
            ENDIF
828
21        CONTINUE
829
22      CONTINUE
830
        DO 23 IP=1,N
831
          B(IP)=B(IP)+Z(IP)
832
          D(IP)=B(IP)
833
          Z(IP)=0.
834
23      CONTINUE
835
24    CONTINUE
836
      write(6,*) max_it,' iterations should never happen'
837
      STOP
838
      RETURN
839
      END
840
c
841
c============================================================
842
c
843
c ++  trie des vecteur dans l'ordre croissant
844
c
845
c============================================================
846
c
847
        SUBROUTINE trie(nb_niv,ene,psi,max_niv)
848
        integer i,j,k,nb_niv,max_niv
849
        real*8 ene(max_niv),psi(max_niv,max_niv)
850
        real*8 a
851

    
852
        DO i=1,nb_niv
853
          DO j=i+1,nb_niv
854
            IF (ene(i) .GT. ene(j)) THEN
855
c              permutation
856
              a=ene(i)
857
              ene(i)=ene(j)
858
              ene(j)=a
859
              DO k=1,nb_niv
860
                 a=psi(k,i)
861
                 psi(k,i)=psi(k,j)
862
                 psi(k,j)=a
863
              END DO
864
            END IF
865
          END DO
866
        END DO
867

    
868
        END
869