Statistiques
| Révision :

root / src / ZmatBuild.f90 @ 3

Historique | Voir | Annoter | Télécharger (31,62 ko)

1
      SUBROUTINE ZmatBuild(na,atome,ind_zmat,val_zmat,x,y,z,izm, &
2
           Liaisons, LIes_CP,LIes_CF,ListAt,DejaFait,debug)
3

    
4
!
5
!     This second version enables the user to freeze some atoms
6
!     the frozen atoms indices are listed in the frozen array.
7
!
8
!     The idea is surely too stupid to work all the time... but here it is
9
!     we first construct the zmat using only the frozen atoms, and then
10
!     we add the other atoms on top of the first ones...
11
!
12
! 12.06.06
13
! Small modification: there was an inconsistency: izm was increased
14
! after the atom was added to ind_zmat for the three first atoms,
15
! but before for all others. Now, it starts at zero and it
16
! is increased before the atom is added to ind_zmat
17

    
18
      use Path_module, only : Nat, NMaxL, max_Z, KINT, KREAL,Pi
19
      use Io_module
20

    
21
      IMPLICIT NONE
22

    
23

    
24
      INTEGER(KINT) :: Izm,I,IAf,IAt,IAt0,J,Jat
25
      INTEGER(KINT) :: n1, NMaxCF, NMinCP, VCF
26
      INTEGER(KINT) :: IAta,IAtd,IAtDh,IAti,IAtv,ICat
27
      INTEGER(KINT) :: ICP0, IdAt0, IdAtl
28
      INTEGER(KINT) :: Idx, IIat, IIatDh, IndAFaire, IndFin
29
      INTEGER(KINT) :: IdxAt0
30
      INTEGER(KINT) :: ITmp, IZm0, IZm1, IZm2, IZm3, IZm4, IzmTmp
31
      integer(KINT) :: na, atome(NAt), ind_zmat(NAt, 5)
32
      real(KREAL) ::  x(NAt), y(NAt), z(NAt)
33
      real(KREAL) ::  val_zmat(NAt,3)
34
!     ListAt contains the indices of frozen atoms
35
      LOGICAL ListAT(NAt), FFirst
36
      INTEGER(KINT) :: Natreat
37

    
38
      real(KREAL) ::  dist
39
      INTEGER(KINT) :: LIAISONS(NAt, 0:NMaxL)
40
      INTEGER(KINT) :: CAFaire(NAt)
41
      INTEGER(KINT) :: LieS_CP(NAt,0:NMaxL),LieS_CF(NAt,0:NMaxL)
42
      INTEGER(KINT) :: AtCP0(NAt),NbAt0,NbAt0r
43
      Integer(KINT) :: NbCycle
44
      real(KREAL) ::  vx1,vy1,vz1,norm1
45
      real(KREAL) ::  vx2,vy2,vz2,norm2
46
      real(KREAL) ::  val
47
      Logical Debug, Done
48
      Logical DejaFait(NAt)
49

    
50

    
51
      INTERFACE 
52
         FUNCTION angle(v1x,v1y,v1z,norm1,v2x,v2y,v2z,norm2)
53
           use Path_module, only :  Pi,KINT, KREAL
54
           real(KREAL) ::  v1x,v1y,v1z,norm1
55
           real(KREAL) ::  v2x,v2y,v2z,norm2
56
           real(KREAL) ::  angle
57
         END FUNCTION ANGLE
58
      END INTERFACE
59

    
60

    
61

    
62

    
63

    
64
!     As we may have to treat only partial molecules, it may happen
65
!     that there are no central atoms... so we first check this by looking
66
!     for the least number of CP atoms
67
      FFirst=.TRUE.
68
      NMinCP=na
69
      NMaxCF=-1
70
      NAtreat=0
71
      Izm0=Izm+1
72

    
73
      DO I=1,na
74
         if (DEBUG) WRITE(*,*) "DBG ZmatBuild i,listat",i,listAt(i)
75
         IF (ListAt(I).AND.(.NOT.(DejaFait(I)))) THEN
76
            IF (Lies_CP(I,0).lt.NMinCP)      NMinCP=Lies_CP(I,0)
77
            IF (Lies_CF(I,0).gt.NMaxCF)      NMaxCF=Lies_CF(I,0)
78
            NATreat=NATreat+1
79
         END IF
80
      END DO
81
      if (debug) WRITE(*,*) 'NMinCP=',NMinCP
82
      if (debug) WRITE(*,*) 'NMaxCF=',NMaxCF
83
      if (debug) WRITE(*,*) 'NATreat=',NAtreat
84

    
85

    
86
      IF (Debug) THEN
87
         WRITE(*,*) '------------------ Zmat Build -------------------'
88
         WRITE(*,*) 'LIAISONS'
89
         DO I=1,na
90
            WRITE(*,1003) i,(LIAISONS(I,J),J=0,NMaxL)
91
         END DO
92

    
93
         WRITE(*,*) 'LIes_CF'
94
         DO I=1,na
95
            WRITE(*,1003) i,(LIeS_CF(I,J),J=0,NMaxL)
96
         END DO
97

    
98
         WRITE(*,*) 'LIes_CP'
99
         DO I=1,na
100
            WRITE(*,1003) i,(LIeS_CP(I,J),J=0,NMaxL)
101
         END DO
102
         WRITE(*,*) '-- Zmat Build : only _To treat_ atoms------------'
103
         WRITE(*,*) 'LIAISONS'
104
         DO I=1,na
105
            IF (ListAt(I).AND.(.NOT.DejaFait(I))) &
106
                 WRITE(*,1003) i,(LIAISONS(I,J),J=0,NMaxL)
107
         END DO
108

    
109
         WRITE(*,*) 'LIes_CF'
110
         DO I=1,na
111
            IF (ListAt(I).AND.(.NOT.DejaFait(I))) &
112
                WRITE(*,1003) i,(LIeS_CF(I,J),J=0,NMaxL)
113
         END DO
114

    
115
         WRITE(*,*) 'LIes_CP'
116
         DO I=1,na
117
            IF (ListAt(I).AND.(.NOT.DejaFait(I))) &
118
                WRITE(*,1003) i,(LIeS_CP(I,J),J=0,NMaxL)
119
         END DO
120

    
121
      END IF
122

    
123

    
124
      if (NAtreat.EQ.0) THEN
125
         WRITE(*,*) "Foutage de gueule : NAtTreat=0"
126
         RETURN
127
      END IF
128

    
129
      if (debug) THEN
130
         WRITE(*,*) 'DBG ZmatBuil, izm=',izm
131
         DO I=1,na
132
            WRITE(*,'(1X,I5,1X,L4,1X,L4,12(1X,I4))') i,ListAt(I) &
133
                 ,DejaFait(I), &
134
                 (Liaisons(I,J),J=0,Liaisons(I,0))
135
         END DO
136
      END IF
137

    
138
!!!   atraiter NMaxCF=0 :que des atomes separes...
139
!     mais faut-il reellement faire un cas a part ?
140

    
141
!     On compte le nb d'atomes sans atomes CP (centripetes)
142
      NbAt0=0
143
      DO I=1,na
144
         IF (ListAt(I).AND.(.NOT.DejaFait(I)).AND. &
145
              (Lies_CP(I,0).eq.NMinCP)) THEN
146
            NbAt0=NbAt0+1
147
            AtCP0(NbAt0)=I
148
            IF (DEBUG) WRITE(*,*) "Center atom",NbAt0,I,Atome(I)
149
         END IF
150
      END DO
151
      AtCP0(NbAt0+1)=0
152
      NbAt0r=NbAt0
153

    
154
      IF (Debug)  WRITE(*,*) 'DBG ZmatBuld - NCP0,AtCP0 ',NbAt0, &
155
           (AtCP0(i),i=1,NbAt0)
156

    
157
!     On va les traiter tous en construisant les molecules en partant d'eux
158
!     Le premier atome sans CP est different des autres car il fixe
159
!     l'origine
160

    
161
!First atom
162
      IF (Izm==0) THEN
163
         FFirst=.FALSE.
164
         if (debug) WRITE(*,*) 'DBG ZmatBuil - Treating Izm=1'
165
!     IZm=1
166
!     Boucle pour trouver celui qui a le plus d'atomes CF
167
         IdAt0=0
168
         VCF=-1
169
         DO I=1,NbAt0
170
!     WRITE(*,*) 'DBG ZmatB',I,AtCP0(I)
171
            if (AtCP0(I).NE.0) THEN
172
               ICat=AtCP0(I)
173
               if (Lies_CF(ICAt,0).gt.VCF) THEN
174
                  IdAt0=ICat
175
                  IdxAt0=I
176
                  VCF=Lies_CF(ICAt,0)
177
               END IF
178
            END IF
179
         END DO
180

    
181
!     IF (debug) WRITE(*,*) 'DBG ZmatBuil - VCF, IdxAt0,IdAt0',
182
!     &        VCF, IdxAt0,IdAt0
183
!     all atoms with NMinCP CP links do not have CF links... not a good choice to start
184
!     building the molecule(s) around them... we increase NMinCP
185
         AtCP0(IdxAt0)=0
186
         NbAt0r=NbAt0r-1
187

    
188
!     WRITE(IOOUT,*) 'Atome 1', IdAt0, Nom(Atome(IdAt0))
189
         Izm1=IdAt0
190
         Izm=Izm+1
191

    
192
         ind_zmat(izm,1)=Izm1
193
         ind_zmat(izm,2)=0
194
         ind_zmat(izm,3)=0
195
         ind_zmat(izm,4)=0
196
         ind_zmat(izm,5)=0
197
         val_zmat(izm,1)=0.0
198
         val_zmat(izm,2)=0.0
199
         val_zmat(izm,3)=0.0
200
         DejaFait(Izm1)=.True.
201

    
202
      END IF                    ! end of izm==1 test
203

    
204
!     On construit la premiere couche qui est speciale car elle fixe les
205
!     axes.
206
!     Plusieurs cas sont possibles suivant le nb d'atomes CF
207
      IdAt0=ind_zmat(1,1)
208
      Izm1=IdAt0
209

    
210
!Second atom
211
      IF ((izm==1).AND.(NAtreat.ge.2))  THEN
212
         Done=.FALSE.
213
         if (debug) WRITE(*,*) 'DBG ZmatBuil - Treating Izm=2'
214
!     Two cases: 1) We are constructing the first fragment: FIzm1=.T.
215
!     2) We are constructing the second fragment: FIzm1=.F.
216
!     in this case, we select one CP0 atom to start with
217
         IF (FFirst) THEN
218
            FFirst=.FALSE.
219
!     This is the first atom to be dealt with
220
!     We look for a CP0
221
            If (NbAt0r.ge.1) THEN
222
               IdAt0=0
223
               VCF=-1
224
               WRITE(*,*) 'DBG  ZBuild Izm=2:AtCP0',(AtCP0(I),I=1,NbAt0)
225
               DO I=1,NbAt0
226
                  if (AtCP0(I).NE.0) THEN
227
                     ICat=AtCP0(I)
228
                     if (Lies_CF(I,0).gt.VCF) THEN
229
                        Izm2=ICat
230
                        IdxAt0=I
231
                        VCF=Lies_CF(ICAt,0)
232
                     END IF
233
                  END IF
234
               END DO
235
               WRITE(*,*) 'DBG ZBuild Izm=2:izm2,IdxAt0,VCF,AtCP0(1)', &
236
                    izm2,IdxAt0,VCF,AtCP0(1)
237
               NbAt0r=NbAt0r-1
238
               Done=.TRUE.
239
            END IF
240

    
241
!     If we do not have a CP0 available the other tests are identical
242
!     for cases 1 and 2...
243
         END IF
244
         IF (.NOT.DONE) THEN
245
            IF (Lies_CF(IdAt0,0).ge.1) THEN
246
               IZm2=Lies_CF(IdAt0,1)
247
               WRITE(*,*) 'DBG ZBuild Lies_CF(IdAt0,0).ge.1', &
248
                    Lies_CF(IdAt0,0),izm2
249
            ELSE
250
!     First atom is not linked to anything... we look for the second CP0 atom
251
!     if we have one..
252
               If (NbAt0r.ge.1) THEN
253
                  IdAt0=0
254
                  VCF=-1
255
                  IF (DEBUG) WRITE(*,*) 'DBG  ZBuild Izm=2:AtCP0', &
256
                       (AtCP0(I),I=1,NbAt0)
257
                  DO I=1,NbAt0
258
                     if (AtCP0(I).NE.0) THEN
259
                        ICat=AtCP0(I)
260
                        if (Lies_CF(I,0).gt.VCF) THEN
261
                           Izm2=ICat
262
                           IdxAt0=I
263
                           VCF=Lies_CF(ICAt,0)
264
                        END IF
265
                     END IF
266
                  END DO
267
                  IF (DEBUG) WRITE(*,*) 'DBG ZBuild Izm=2:izm2,IdxAt0,VCF,AtCP0(1)', &
268
                       izm2,IdxAt0,VCF,AtCP0(1)
269
                  NbAt0r=NbAt0r-1
270
               ELSE
271
!     we do not have another CP0 atom... but we know that there
272
!     is at least two atoms... we look for the closest atom to Izm1
273
                  Izm2=0
274
                  Dist=1.D99
275
                  DO I=1,Na
276
                     IF (ListAt(I).AND.(.NOT.Dejafait(I))) THEN
277
                        CALL vecteur(IZm1,I,x,y,z,vx1,vy1,vz1,norm1)
278
                        if (Norm1.lt.Dist) IZm2=I
279
                     END IF
280
                  END DO
281
               END IF
282
            END IF
283
         END IF
284
         CALL vecteur(IZm1,IZm2,x,y,z,vx1,vy1,vz1,norm1)
285

    
286
         Izm=Izm+1
287

    
288
         ind_zmat(izm,1)=izm2
289
         ind_zmat(izm,2)=izm1
290
         ind_zmat(izm,3)=0
291
         ind_zmat(izm,4)=0
292
         ind_zmat(izm,5)=0
293
         val_zmat(izm,1)=norm1
294
         val_zmat(izm,2)=0.0
295
         val_zmat(izm,3)=0.0
296
         DejaFait(Izm2)=.TRUE.
297

    
298
      END IF                    ! end of izm==2 test
299

    
300
      Izm1=ind_zmat(1,1)
301
      Izm2=ind_zmat(2,1)
302

    
303
! Third atom
304

    
305
      IF ((Izm==2).AND.(NAtreat.ge.3)) THEN
306
         Done=.FALSE.
307
         if (debug) WRITE(*,*) 'DBG ZmatBuil - Treating Izm=3',FFirst
308
!     Two cases: 1) We are constructing the first fragment: FIzm1=.T.
309
!     2) We are constructing the second fragment: FIzm1=.F.
310
!     in this case, we select one CP0 atom to start with
311
         IF (FFirst) THEN
312
            FFirst=.FALSE.
313
!     This is the first atom to be dealt with
314
!     We look for a CP0
315
            If (NbAt0r.ge.1) THEN
316
               IdAt0=0
317
               VCF=-1
318
               DO I=1,NbAt0
319
                  if (AtCP0(I).NE.0) THEN
320
                     ICat=AtCP0(I)
321
                     if (Lies_CF(I,0).gt.VCF) THEN
322
                        Izm3=ICat
323
                        IdxAt0=I
324
                        VCF=Lies_CF(ICAt,0)
325
                     END IF
326
                  END IF
327
               END DO
328
               NbAt0r=NbAt0r-1
329
               Done=.TRUE.
330
            END IF
331
!     If we do not have a CP0 available the other tests are identical
332
!     for cases 1 and 2...
333
         END IF
334
         IF (.NOT.Done) THEN
335
            WRITE(*,*) 'DBG ZmatBuild: Done false for izm=3'
336
            IF (Lies_CF(izm1,0).ge.2) THEN
337
!     easiest case: the first atom is linked to at least 2 atoms.
338
               WRITE(*,*) 'DBG ZmtB-izm3:Lies_CF(izm1,0)>=2 '
339
               I=1
340
               DO WHILE ((.NOT.ListAt(Lies_CF(izm1,I))) &
341
                    .OR.(DejaFait(Lies_CF(izm1,I))))
342
                  I=I+1
343
               END DO
344
               izm3=Lies_CF(Izm1,I)
345
               Done=.TRUE.
346
            ELSEIF (Lies_CF(Izm2,0).ge.1) THEN
347
!     a bit more complex: first atom is linked to one atom (Izm2)
348
!     but luckily 2nd atom is linked to at least one atom
349
               I=1
350
               DO WHILE ((.NOT.ListAt(Lies_CF(izm2,I))) &
351
                    .OR.(DejaFait(Lies_CF(izm2,I))).AND.(I.le.Lies_CF(Izm2,0)))
352
                  I=I+1
353
               END DO
354
               IF (I.LE.Lies_CF(Izm2,0)) THEN
355
                  Izm3=Lies_CF(izm2,I)
356
               WRITE(*,*) 'DBG ZmtB-izm3:Lies_CF(izm2,0)>=1 and ok  '
357
!     We exchange Izm1 and Izm2 because the logical order is Izm3 linked
358
!     to Izm2  and not to Izm1 as is the default later
359
                  IzmTmp=Izm2
360
                  Izm2=Izm1
361
                  Izm1=IzmTmp
362
                  Done=.TRUE.
363
               END IF
364
            END IF
365
         IF (.NOT.Done) THEN
366
!     None of the first two atoms has links to a third atom...
367
!     we take it from the CP0 if possible...
368
               If (NbAt0r.ge.1) THEN
369
                  WRITE(*,*) 'DBG ZmatB izm=3',NbAt0r
370
                  IdAt0=0
371
                  VCF=-1
372
                  DO I=1,NbAt0
373
                     if (AtCP0(I).NE.0) THEN
374
                        ICat=AtCP0(I)
375
                        if (Lies_CF(I,0).gt.VCF) THEN
376
                           Izm3=ICat
377
                           IdxAt0=I
378
                           VCF=Lies_CF(ICAt,0)
379
                        END IF
380
                     END IF
381
                  END DO
382
                  NbAt0r=NbAt0r-1
383
                  WRITE(*,*) 'DBG ZmatB izm=3',NbAt0r,Izm3
384
               ELSE
385
! Or the  atom closest to 1
386
                  Izm3=0
387
                  Dist=1.D99
388
                  DO I=1,Na
389
                     IF (ListAt(I).AND.(.NOT.Dejafait(I))) THEN
390
                        CALL vecteur(IZm1,I,x,y,z,vx1,vy1,vz1,norm1)
391
                        if (Norm1.lt.Dist) IZm3=I
392
                     END IF
393
                  END DO
394
               END IF
395
            END IF
396
         END IF
397

    
398
         CALL vecteur(IZm1,IZm2,x,y,z,vx1,vy1,vz1,norm1)
399
         CALL vecteur(izm1,izm3,x,y,z,vx2,vy2,vz2,norm2)
400
         val=angle(vx1,vy1,vz1,norm1,vx2,vy2,vz2,norm2)
401

    
402
!     write(*,11) n1,n2,norm1,n3,val
403

    
404
         Izm=Izm+1
405

    
406
         ind_zmat(izm,1)=izm3
407
         ind_zmat(izm,2)=izm1
408
         ind_zmat(izm,3)=izm2
409
         ind_zmat(izm,4)=0
410
         ind_zmat(izm,5)=0
411
         val_zmat(izm,1)=norm2
412
         val_zmat(izm,2)=val
413
         val_zmat(izm,3)=0.0
414
         DejaFait(Izm3)=.TRUE.
415

    
416
      END IF                    ! end of test izm=3
417

    
418
!     We add all atoms CF atoms linked to the already present atoms in the zmat to
419
!     the "to do" list:
420
!     Les atomes CF lies a IdAt0 sont mis en attente pour
421
!     etre traites
422

    
423
!     For 'historical' reasons, the first atom links have to be dealt with
424
!     in a special way... now !
425

    
426
      if (Izm.ge.NaTreat) Return
427

    
428

    
429
      WRITE(*,*) 'DBG ZmatBuild Izm0',Izm0
430

    
431
      if (debug) THEN
432
         WRITE(*,*) 'ind_zmat'
433
         DO I=1,na
434
            WRITE(*,'(1X,5I5)') (ind_zmat(i,j),j=1,5)
435
         END DO
436
      END IF
437

    
438
      IF (FFIrst) GOTO 9753
439

    
440
         I=Ind_zmat(Izm0,1)
441
         DO IdAtl=1,Lies_CF(I,0)
442
            Izm4= Lies_CF(I,IdAtl)
443
!      WRITE(IOOUT,*) 'Atome ',Izm+1,Izm4,Nom(Atome(Izm4)),Izm1,
444
!     $Izm2,Izm3
445
            IF (ListAt(Izm4).AND.(.NOT.DejaFait(Izm4))) THEN
446
               Izm=Izm+1
447
               Call AddLigne(Izm,Izm4,Izm1,Izm2,Izm3,ind_zmat,val_zmat, &
448
                 x,y,z)
449
               DejaFait(Izm4)=.TRUE.
450
            END IF
451
         END DO
452
!      END IF
453

    
454
      if (debug) THEN
455
         WRITE(*,*) 'ind_zmat'
456
         DO I=1,na
457
            WRITE(*,'(1X,5I5)') (ind_zmat(i,j),j=1,5)
458
         END DO
459
       END IF
460

    
461
      if (Izm.ge.NaTreat) Return
462

    
463
      IndFin=1
464
      DO I=Izm0,Izm
465
         Iat=ind_zmat(I,1)
466
!         Do iaf=1,Lies_CF(Iat,0)
467
!!     if (ListAt(Lies_CF(IAt,iaf)).AND.
468
!!     &              (.NOT.DejaFait(Lies_CF(IAt,iaf))))  THEN
469
!            if (ListAt(Lies_CF(IAt,iaf))) THEN
470
!               CAfaire(IndFin)=Lies_CF(IAt,Iaf)
471
               CaFaire(IndFin)=Iat
472
               IndFin=IndFin+1
473
!            END IF
474
!         END DO
475
      END DO
476
      CAfaire(IndFin)=0
477

    
478
      if (debug) WRITE(IOOUT,*) "CaFaire:",CaFaire
479

    
480
      if (debug) WRITE(*,*) 'DBG ZmatBuil - Treating Izm=4'
481

    
482
!     on a cree la premiere couche autour du premier centre
483
!     reste a finir les autres couches
484
      IndAFaire=1
485
      if (debug) WRITE(IOOUT,*) "CaFaire:",CaFaire
486
      Do WHILE (Cafaire(IndAFaire).ne.0)
487
         IAt0=Cafaire(IndAfaire)
488
         if (debug) WRITE(*,*) "IndAFaire,IAt0,Lies_CF(IAt0,0)", &
489
              IndAFaire,IAt0,Lies_CF(IAt0,0)
490
         if (Lies_CF(IAt0,0).ge.1) THEN
491
!     IAt0 est l'atome pour lequel on construit la couche suivante
492
!     le premier atome lie est particulier car il definit l'orientation
493
!     de ce fragment
494
            IAti=Lies_CF(IAt0,1)
495
      WRITE(IOOUT,*) 'IAti:',IAti
496
            IAtd=IAt0
497
      WRITE(IOOUT,*) 'IAtd:',IAtd
498
            IAtv=Lies_CP(IAt0,1)
499
      WRITE(IOOUT,*) 'IAtv:',IAtv
500
            IIAtdh=1
501
            IAtdh=Lies_CF(IAtv,IIatdh)
502
            DO WHILE (Iatdh.eq.IAt0)
503
               IIatdh=IIatdh+1
504
               IAtdh=Lies_CF(IAtv,IIatdh)
505
            END DO
506
            IF (IAtdh.eq.0) IAtdh=Lies_CP(IAtv,1)
507
      WRITE(IOOUT,*) 'IAtdh:',IAtdh
508
            IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
509
               Izm=Izm+1
510
               if (debug) WRITE(*,*) 'izm,IAti,IAtd,IAtv,IAtdh', &
511
                    izm,IAti,IAtd,IAtv,IAtdh
512
               Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh, &
513
                    ind_zmat,val_zmat ,x,y,z)
514
               DejaFait(IAti)=.TRUE.
515
            END IF
516

    
517
            CAfaire(IndFin)=Lies_CF(IAt0,1)
518
            IndFin=IndFin+1
519
!     Le traitement des autres est plus facile
520
            IAtdh=Lies_CF(IAt0,1)
521
            DO IAta=2,Lies_CF(IAt0,0)
522
               IAtI=Lies_CF(IAt0,IAta)
523
               CAfaire(IndFin)=IAtI
524
               IndFin=IndFin+1
525

    
526
               IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
527
                  Izm=Izm+1
528
                  Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh,ind_zmat &
529
                       ,val_zmat,x,y,z)
530
                  DejaFait(IAti)=.TRUE.
531
               END IF
532
            END DO
533
            CAfaire(IndFin)=0
534
         END IF
535
         IndAFaire=IndAFaire+1
536
         if (debug) WRITE(IOOUT,*) "IndAfaire,CaFaire:",IndAfaire, &
537
            CaFaire
538
      END DO
539

    
540
      IndFin=1
541
      DO I=1,Izm0
542
         Iat=ind_zmat(I,1)
543
!         Do iaf=1,Lies_CF(Iat,0)
544
         if (ListAt(IAt).AND. &
545
                    (.NOT.DejaFait(IAt)))  THEN
546
!            if (ListAt(Lies_CF(IAt,iaf))) THEN
547
!               CAfaire(IndFin)=Lies_CF(IAt,Iaf)
548
               CaFaire(IndFin)=Iat
549
               IndFin=IndFin+1
550
            END IF
551
!         END DO
552
      END DO
553
      CAfaire(IndFin)=0
554

    
555
      if (debug) WRITE(IOOUT,*) "CaFaire:",CaFaire
556

    
557
      if (debug) WRITE(*,*) 'DBG ZmatBuil - Treating Izm=4'
558

    
559
!     on a cree la premiere couche autour du premier centre
560
!     reste a finir les autres couches
561
      IndAFaire=1
562
      if (debug) WRITE(IOOUT,*) "CaFaire:",CaFaire
563
      Do WHILE (Cafaire(IndAFaire).ne.0)
564
         IAt0=Cafaire(IndAfaire)
565
         if (debug) WRITE(*,*) "IndAFaire,IAt0,Lies_CF(IAt0,0)", &
566
              IndAFaire,IAt0,Lies_CF(IAt0,0)
567
         if (Lies_CF(IAt0,0).ge.1) THEN
568
!     IAt0 est l'atome pour lequel on construit la couche suivante
569
!     le premier atome lie est particulier car il definit l'orientation
570
!     de ce fragment
571
            IAti=Lies_CF(IAt0,1)
572
      WRITE(IOOUT,*) 'IAti:',IAti
573
            IAtd=IAt0
574
      WRITE(IOOUT,*) 'IAtd:',IAtd
575
            IAtv=Lies_CP(IAt0,1)
576
      WRITE(IOOUT,*) 'IAtv:',IAtv
577
            IIAtdh=1
578
            IAtdh=Lies_CF(IAtv,IIatdh)
579
            DO WHILE (Iatdh.eq.IAt0)
580
               IIatdh=IIatdh+1
581
               IAtdh=Lies_CF(IAtv,IIatdh)
582
            END DO
583
            IF (IAtdh.eq.0) IAtdh=Lies_CP(IAtv,1)
584
      WRITE(IOOUT,*) 'IAtdh:',IAtdh
585
            IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
586
               Izm=Izm+1
587
               if (debug) WRITE(*,*) 'izm,IAti,IAtd,IAtv,IAtdh', &
588
                    izm,IAti,IAtd,IAtv,IAtdh
589
               Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh, &
590
                    ind_zmat,val_zmat ,x,y,z)
591
               DejaFait(IAti)=.TRUE.
592
            END IF
593

    
594
            CAfaire(IndFin)=Lies_CF(IAt0,1)
595
            IndFin=IndFin+1
596
!     Le traitement des autres est plus facile
597
            IAtdh=Lies_CF(IAt0,1)
598
            DO IAta=2,Lies_CF(IAt0,0)
599
               IAtI=Lies_CF(IAt0,IAta)
600
               CAfaire(IndFin)=IAtI
601
               IndFin=IndFin+1
602

    
603
               IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
604
                  Izm=Izm+1
605
                  Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh,ind_zmat &
606
                       ,val_zmat,x,y,z)
607
                  DejaFait(IAti)=.TRUE.
608
               END IF
609
            END DO
610
            CAfaire(IndFin)=0
611
         END IF
612
         IndAFaire=IndAFaire+1
613
         if (debug) WRITE(IOOUT,*) "IndAfaire,CaFaire:",IndAfaire, &
614
            CaFaire
615
      END DO
616

    
617

    
618
!     On a fini de creer la molecule autour du premier atome 'centre'
619
!     Pour les autres c'est presque id... sauf que les axes sont deja fixes
620
!     On pourrait imaginer de mettre des atomes fictifs pour redefinir des axes
621
!     locaux... dans une autre version
622
!     V2.0
623
 9753 FFirst=.FALSE.
624
      if (debug) THEN
625
         WRITE(*,*) 'NbAt0r',NbAt0r,AtCP0
626
      END IF
627
      DO I=1,NbAt0r
628
!     Boucle pour trouver celui qui a le plus d'atomes CF
629
         IdAt0=0
630
         VCF=-1
631
         DO ICP0=1,NbAt0
632
            if (AtCP0(ICP0).NE.0) THEN
633
               ICat=AtCP0(ICP0)
634
               if (Lies_CF(ICAt,0).gt.VCF) THEN
635
                  IdAt0=ICat
636
                  IdxAt0=ICP0
637
                  VCF=Lies_CF(ICAt,0)
638
               END IF
639
            END IF
640
         END DO
641
         AtCP0(IdxAt0)=0
642

    
643
         IF (debug) WRITE(*,*) 'Adding fragment centered on ',IdAt0, &
644
              LIAISONS(IdAt0,0)
645
         IF (debug) WRITE(*,*) 'Center linked to ',Lies_CF(IdAt0,0), &
646
              ' atoms'
647
!     if (LIAISONS(IdAt0,0).EQ.0) goto 12345
648

    
649
         if (debug) THEN
650
            WRITE(IOOUT,*) 'LIAISONS tralala',nbcycle
651
            DO IAt=1,na
652
               WRITE(IOOUT,1003) iat,(LIAISONS(Iat,Jat),Jat=0,NMaxL)
653
            END DO
654
         END IF
655
 1003    FORMAT(1X,I5,' - ',12(I5))
656
!     Boucle pour voir quel est l'atome du fragment precedent qui est le plus
657
!     proche de celui ci
658
!     Rq: si ce sont des cycles lies, il pourrait etre malin de rechercher
659
!     a quoi il etait lie.
660
         norm2=1.e6
661
         Idx=0
662
         DO ICAt=1,Izm
663
            CALL vecteur(Idat0,ind_zmat(icat,1),x,y,z,vx1,vy1,vz1 &
664
                 ,norm1)
665
            if (norm2.ge.norm1) THEN
666
               norm2=norm1
667
               Idx=Icat
668
            END IF
669
         END DO
670
         IF (debug) WRITE(*,*) 'Link between fragments ',IdAt0, &
671
              ind_zmat(Idx,1), ' -- Idx=',Idx
672

    
673
! Despite the fact that this atom has officialy no CP atom
674
! we add one into its list; the one just found
675
         Lies_CP(IdAt0,0)=Lies_CP(IdAt0,0)+1
676
         Lies_CP(Idat0,Lies_CP(IdAt0,0))= ind_zmat(Idx,1)
677
         Lies_CP(Idat0,Lies_CP(IdAt0,0)+1)=0
678

    
679

    
680
!     on a l'indice... on va rajouter cet atome a la liste !
681
         izm=izm+1
682
         n1=ind_zmat(Idx,1)
683
!     Petit probleme si Idx<=2
684
         IF (Idx.EQ.1) THEN
685
!     Pb non negligeable ...
686
            IF (izm.ge.2) THEN
687
               IAtv=ind_zmat(2,1)
688
               IAtdh=ind_zmat(3,1)
689
            ELSE
690
               WRITE(*,*) '2 frag, le 1er a moins de 2 atomes..'
691
               WRITE(*,*) "J'ai merde... "
692
               STOP
693
            END IF
694
         ELSEIF (Idx.EQ.2) THEN
695
            IAtv=1
696
            IF (izm.ge.2) THEN
697
               IAtdh=ind_zmat(3,1)
698
            ELSE
699
               WRITE(*,*) '2 frag, le 1er a moins de 2 atomes...'
700
               WRITE(*,*) "J'ai merde... "
701
               STOP
702
            END IF
703
         ELSE
704
            IAtv=ind_zmat(Idx,2)
705
            IAtdh=ind_zmat(Idx,3)
706
         END IF
707

    
708
!     WRITE(*,*) "Outchilou IAtv, IAtdh", IAtv, IAtdh,n1
709
         CALL vecteur(n1,IdAt0,x,y,z,vx1,vy1,vz1,norm1)
710
         CALL vecteur(n1,IAtv,x,y,z,vx2,vy2,vz2,norm2)
711
         val=angle(vx1,vy1,vz1,norm1, &
712
              vx2,vy2,vz2,norm2)
713
         if (debug) WRITE(*,*) 'Angle val:1',IdAt0,n1,IAtv,val
714
         if ((abs(val).LE.10.).OR.(abs(pi-val).le.10.)) THEN
715
            IAtv=IAtdh
716
!     Ceci pose pb si Idx<=3... a traiter plus tard
717
            IF (Idx.ge.3) THEN
718
               IAtdh=ind_zmat(Idx,4)
719
            ELSE
720
               if (izm.ge.4) THEN
721
                  IAtdh=4
722
               ELSE
723
                  WRITE(*,*) 'Pb Angle Val+idx=3+izm<=4'
724
                  STOP
725
               END IF
726
            END IF
727
         END IF
728
         Call AddLigne(Izm,IdAt0,ind_zmat(Idx,1),IAtv,IAtdh, &
729
              ind_zmat,val_zmat,x,y,z)
730
         DejaFait(IdAt0)=.TRUE.
731

    
732
         IndFin=1
733
         IAtd=IdAt0
734
         n1=IAtdh
735
         IAtdh=IAtv
736
         IAtv=ind_zmat(Idx,1)
737
!     On ajoute les atomes lies a cet atome dans la liste a faire
738
         Do iaf=1,Lies_CF(IdAt0,0)
739
            IatI=Lies_CF(IdAt0,Iaf)
740
!     We check that this atom is not the one that is the closest to
741
!     the center atom
742
            if (IAtv.Ne.IAtI) THEN
743
               IF (debug) WRITE(*,*) 'Adding atom',IAtI, &
744
                    'to CAFaire in pos',iaf
745
               CAfaire(IndFin)=IAtI
746
               IndFin=IndFin+1
747
!     On les ajoute aussi dans la zmat
748
               If (ListAt(IAtI).AND.(.NOT.DejaFait(IatI))) THEN
749
                  izm=izm+1
750
       WRITE(*,*) "Outchili IAtv, IAtdh,IAtI", IAtv, IAtdh,IAtI
751
                  CALL vecteur(IAtI,IAtD,x,y,z,vx1,vy1,vz1,norm1)
752
                  CALL vecteur(IAtI,IAtv,x,y,z,vx2,vy2,vz2,norm2)
753
                  val=angle(vx1,vy1,vz1,norm1, vx2,vy2,vz2,norm2)
754
                  if (debug) &
755
                       WRITE(*,*) 'Angle val:2',IAtI,IAtD,IAtv,val
756
                  if ((abs(val).LE.10.).OR. &
757
                       (abs(180.-abs(val)).le.10.)) THEN
758
                     Call AddLigne(Izm,IAtI,IAtv,IAtdh,n1, &
759
                          ind_zmat,val_zmat,x,y,z)
760
                     DejaFait(Iati)=.TRUE.
761
                  ELSE
762
                     Call AddLigne(Izm,IAtI,IAtD,IAtv,IAtdh, &
763
                          ind_zmat,val_zmat,x,y,z)
764
                     DejaFait(Iati)=.TRUE.
765
                  END IF
766
               END IF
767
            END IF
768
         END DO
769
         CAfaire(IndFin)=0
770

    
771
!     On traite le reste de ce fragment !!
772
         IndAFaire=1
773
         WRITE(IOOUT,*) CaFaire
774
         Do WHILE (Cafaire(IndAFaire).ne.0)
775
            IAt0=Cafaire(IndAfaire)
776
            if (Lies_CF(IAt0,0).ge.1) THEN
777
!     IAt0 est l'atome pour lequel on construit la couche suivante
778
!     le premier atome lie est particulier car il definit l'orientation
779
!     de ce fragment
780
               Itmp=1
781
               IAti=Lies_CF(IAt0,Itmp)
782
               DO WHILE (DejaFait(IAti).AND. &
783
                    (Itmp.Le.Lies_CF(IAt0,0)))
784
                  Itmp=Itmp+1
785
                  IAti=Lies_CF(IAt0,ITmp)
786
               END DO
787
               If (ListAt(IAtI).AND.(.NOT.DejaFait(IatI))) THEN
788
                  IF (debug) WRITE(IOOUT,*) 'IAti:',IAti
789
                  IAtd=IAt0
790
                  IF (debug)     WRITE(IOOUT,*) 'IAtd:',IAtd
791
                  IAtv=Lies_CP(IAt0,1)
792
                  IF (debug)     WRITE(IOOUT,*) 'IAtv:',IAtv
793
                  IIAtdh=1
794
                  IAtdh=Lies_CF(IAtv,IIatdh)
795
                  DO WHILE (Iatdh.eq.IAt0)
796
                     IIatdh=IIatdh+1
797
                     IAtdh=Lies_CF(IAtv,IIatdh)
798
                  END DO
799
                  IF (IAtdh.eq.0) IAtdh=Lies_CP(IAtv,1)
800
                  IF (debug) WRITE(IOOUT,*) 'IAtdh:',IAtdh
801
                  Izm=Izm+1
802
                  Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh, &
803
                       ind_zmat,val_zmat,x,y,z)
804
                     DejaFait(Iati)=.TRUE.
805
!     Le traitement des autres est plus facile
806
                  IAtdh=Lies_CF(IAt0,ITmp)
807
                  DO IAta=ITmp+1,Lies_CF(IAt0,0)
808
                     IAtI=Lies_CF(IAt0,IAta)
809
                     If (ListAt(IAtI).AND.(.NOT.DejaFait(IatI))) &
810
                          THEN
811
                        Izm=Izm+1
812
                        Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh, &
813
                             ind_zmat,val_zmat,x,y,z)
814
                        DejaFait(Iati)=.TRUE.
815
                     END IF
816
                  END DO
817
               END IF
818

    
819
!     On ajoute les atomes lies a cet atome dans la liste a faire
820
               Do iaf=1,Lies_CF(IAt0,0)
821
                  CAfaire(IndFin)=Lies_CF(IAt0,Iaf)
822
                  IndFin=IndFin+1
823
               END DO
824
               CAfaire(IndFin)=0
825
            END IF
826
            IndAFaire=IndAFaire+1
827
         END DO
828
!12345    CONTINUE
829
      END DO
830

    
831
      WRITE(*,*) 'TOTOTOTOTOTOTOTOT'
832

    
833
      if (debug) THEN
834
         WRITE(*,*) 'ind_zmat'
835
         DO I=1,na
836
            WRITE(*,'(1X,5I5)') (ind_zmat(i,j),j=1,5)
837
         END DO
838
      END IF
839

    
840
      IndFin=1
841
      DO I=1,Izm0
842
         Iat=ind_zmat(I,1)
843
         Do iaf=1,Lies_CF(Iat,0)
844
            IIat=Lies_CF(Iat,iaf)
845
            if (ListAt(iIAt).AND. &
846
                 (.NOT.DejaFait(iIAt)))  THEN
847
               IAti=IIat
848
               WRITE(IOOUT,*) 'IAti:',IAti
849
               IAtd=IAt0
850
               WRITE(IOOUT,*) 'IAtd:',IAtd
851
               IAtv=Lies_CP(IAt0,1)
852
               WRITE(IOOUT,*) 'IAtv:',IAtv
853
               IIAtdh=1
854
               IAtdh=Lies_CF(IAtv,IIatdh)
855
               DO WHILE (Iatdh.eq.IAt0)
856
                  IIatdh=IIatdh+1
857
                  IAtdh=Lies_CF(IAtv,IIatdh)
858
               END DO
859
               IF (IAtdh.eq.0) IAtdh=Lies_CP(IAtv,1)
860
               WRITE(IOOUT,*) 'IAtdh:',IAtdh
861
               IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
862
                  Izm=Izm+1
863
                  if (debug) WRITE(*,*) 'izm,IAti,IAtd,IAtv,IAtdh', &
864
                       izm,IAti,IAtd,IAtv,IAtdh
865
                  Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh, &
866
                       ind_zmat,val_zmat ,x,y,z)
867
                  DejaFait(IAti)=.TRUE.
868
                  CaFaire(IndFin)=iIat
869
                  IndFin=IndFin+1
870
               END IF
871
            END IF
872
         END DO
873
      END DO
874
      CAfaire(IndFin)=0
875

    
876
      if (debug) WRITE(IOOUT,*) "CaFaire:",CaFaire
877

    
878
      if (debug) WRITE(*,*) 'DBG ZmatBuil - Toto'
879
      if (debug) THEN
880
         WRITE(*,*) 'ind_zmat'
881
         DO I=1,na
882
            WRITE(*,'(1X,5I5)') (ind_zmat(i,j),j=1,5)
883
         END DO
884
      END IF
885

    
886
!     on a cree la premiere couche autour du premier centre
887
!     reste a finir les autres couches
888
      IndAFaire=1
889
      if (debug) WRITE(IOOUT,*) "CaFaire:",CaFaire
890
      Do WHILE (Cafaire(IndAFaire).ne.0)
891
         IAt0=Cafaire(IndAfaire)
892
         if (debug) WRITE(*,*) "IndAFaire,IAt0,Lies_CF(IAt0,0)", &
893
              IndAFaire,IAt0,Lies_CF(IAt0,0)
894
         if (Lies_CF(IAt0,0).ge.1) THEN
895
!     IAt0 est l'atome pour lequel on construit la couche suivante
896
!     le premier atome lie est particulier car il definit l'orientation
897
!     de ce fragment
898
            IAti=Lies_CF(IAt0,1)
899
      WRITE(IOOUT,*) 'IAti:',IAti
900
            IAtd=IAt0
901
      WRITE(IOOUT,*) 'IAtd:',IAtd
902
            IAtv=Lies_CP(IAt0,1)
903
      WRITE(IOOUT,*) 'IAtv:',IAtv
904
            IIAtdh=1
905
            IAtdh=Lies_CF(IAtv,IIatdh)
906
            DO WHILE (Iatdh.eq.IAt0)
907
               IIatdh=IIatdh+1
908
               IAtdh=Lies_CF(IAtv,IIatdh)
909
            END DO
910
            IF (IAtdh.eq.0) IAtdh=Lies_CP(IAtv,1)
911
      WRITE(IOOUT,*) 'IAtdh:',IAtdh
912
            IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
913
               Izm=Izm+1
914
               if (debug) WRITE(*,*) 'izm,IAti,IAtd,IAtv,IAtdh', &
915
                    izm,IAti,IAtd,IAtv,IAtdh
916
               Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh, &
917
                    ind_zmat,val_zmat ,x,y,z)
918
               DejaFait(IAti)=.TRUE.
919
            END IF
920

    
921
            CAfaire(IndFin)=Lies_CF(IAt0,1)
922
            IndFin=IndFin+1
923
!     Le traitement des autres est plus facile
924
            IAtdh=Lies_CF(IAt0,1)
925
            DO IAta=2,Lies_CF(IAt0,0)
926
               IAtI=Lies_CF(IAt0,IAta)
927
               CAfaire(IndFin)=IAtI
928
               IndFin=IndFin+1
929

    
930
               IF (ListAt(IAti).AND.(.NOT.DejaFait(IAti))) THEN
931
                  IF (debug) &
932
         WRITE(*,*) 'DBG ZmatBuil Toto adding',Iati,Izm
933
                  Izm=Izm+1
934
                  Call AddLigne(Izm,IAti,IAtd,IAtv,IAtdh,ind_zmat &
935
                       ,val_zmat,x,y,z)
936
                  DejaFait(IAti)=.TRUE.
937
               END IF
938
            END DO
939
            CAfaire(IndFin)=0
940
         END IF
941
         IndAFaire=IndAFaire+1
942
         if (debug) WRITE(IOOUT,*) "Toto IndAfaire,CaFaire:",IndAfaire, &
943
            CaFaire
944
      END DO
945

    
946

    
947
      if (debug) THEN
948
         WRITE(*,*) 'ind_zmat'
949
         DO I=1,na
950
            WRITE(*,'(1X,5I5)') (ind_zmat(i,j),j=1,5)
951
         END DO
952
      END IF
953

    
954
      END