Statistiques
| Révision :

root / src / Step_GEDIIS_All.f90

Historique | Voir | Annoter | Télécharger (23,08 ko)

1
     ! Geom = input parameter vector (Geometry), Grad = input gradient vector, HEAT is Energy(Geom)
2
      SUBROUTINE Step_GEDIIS_All(NGeomF,IGeom,Step,Geom,Grad,HEAT,Hess,NCoord,allocation_flag,Tangent)
3

    
4
!----------------------------------------------------------------------
5
! This routine was adapted from the public domain mopac6 diis.f 
6
!  source file (c) 2009, Stewart Computational Chemistry.
7
!  <http://www.openmopac.net/Downloads/Downloads.html>
8
!
9
!----------------------------------------------------------------------
10
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
11
!  Centre National de la Recherche Scientifique,
12
!  Université Claude Bernard Lyon 1. All rights reserved.
13
!
14
!  This work is registered with the Agency for the Protection of Programs 
15
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
16
!
17
!  Authors: P. Fleurat-Lessard, P. Dayal
18
!  Contact: optnpath@gmail.com
19
!
20
! This file is part of "Opt'n Path".
21
!
22
!  "Opt'n Path" is free software: you can redistribute it and/or modify
23
!  it under the terms of the GNU Affero General Public License as
24
!  published by the Free Software Foundation, either version 3 of the License,
25
!  or (at your option) any later version.
26
!
27
!  "Opt'n Path" is distributed in the hope that it will be useful,
28
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
29
!
30
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
!  GNU Affero General Public License for more details.
32
!
33
!  You should have received a copy of the GNU Affero General Public License
34
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
35
!
36
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
37
! for commercial licensing opportunities.
38
!----------------------------------------------------------------------
39

    
40
    use Io_module
41
    use Path_module, only :  Vfree
42
      IMPLICIT NONE
43

    
44
      INTEGER(KINT) :: NGeomF,IGeom
45
      INTEGER(KINT), INTENT(IN) :: NCoord
46
      REAL(KREAL) :: Geom(NCoord), Grad(NCoord), Hess(NCoord*NCoord), Step(NCoord)
47
    REAL(KREAL) :: HEAT ! HEAT= Energy
48
    LOGICAL :: allocation_flag
49
    REAL(KREAL), INTENT(INOUT) :: Tangent(Ncoord)
50
    
51
      ! MRESET = maximum number of iterations.
52
      INTEGER(KINT), PARAMETER :: MRESET=15, M2=(MRESET+1)*(MRESET+1) !M2 = 256
53
      REAL(KREAL), ALLOCATABLE, SAVE :: GeomSet(:,:), GradSet(:,:) ! NGeomF,MRESET*NCoord
54
      REAL(KREAL), ALLOCATABLE, SAVE :: GSAVE(:,:) !NGeomF,NCoord
55
    REAL(KREAL), ALLOCATABLE, SAVE :: ESET(:,:)
56
    REAL(KREAL) :: ESET_tmp(MRESET), B(M2), BS(M2), BST(M2), B_tmp(M2) ! M2=256
57
      LOGICAL :: DEBUG, PRINT, ci_lt_zero
58
      INTEGER(KINT), ALLOCATABLE, SAVE :: MSET(:) ! mth Iteration
59
    LOGICAL, ALLOCATABLE, SAVE :: FRST(:)
60
    REAL(KREAL) :: ci(MRESET), ci_tmp(MRESET) ! MRESET = maximum number of iterations.
61
      INTEGER(KINT) :: NGEDIIS, MPLUS, INV, ITERA, MM, cis_zero
62
      INTEGER(KINT) :: I, J, K, JJ, JNV, II, IONE, IJ, IX, JX, KX
63
    INTEGER(KINT) :: current_size_B_mat, MyPointer, Isch, NFree, Idx
64
      REAL(KREAL) :: XMax, XNorm, DET, THRES, tmp, ER_star, ER_star_tmp, Norm
65
    REAL(KREAL), PARAMETER :: eps=1e-12
66
      REAL(KREAL), PARAMETER :: crit=1e-8
67
    REAL(KREAL), ALLOCATABLE :: Tanf(:) ! NCoord
68
    REAL(KREAL), ALLOCATABLE :: HFree(:) ! NFree*NFree
69
    REAL(KREAL), ALLOCATABLE :: Htmp(:,:) ! NCoord,NFree
70
    REAL(KREAL), ALLOCATABLE :: Grad_free(:), Step_free(:) ! NFree
71
    REAL(KREAL), ALLOCATABLE :: Geom_free(:), Geom_new_free(:) ! NFree
72
    REAL(KREAL), ALLOCATABLE, SAVE :: GeomSet_free(:,:), GradSet_free(:,:)
73

    
74
      DEBUG=.TRUE.
75
      PRINT=.FALSE.
76
    
77
      IF (PRINT)  WRITE(*,'(/,''      BEGIN Step_GEDIIS_ALL   '')')
78
    
79
      ! Initialization
80
      IF (allocation_flag) THEN
81
      ! allocation_flag will be set to False in SPACE_GEDIIS, so no need to modify it here
82
         IF (ALLOCATED(GeomSet)) THEN
83
            IF (PRINT)  WRITE(*,'(/,''    In allocation_flag, GEDIIS_ALL  Dealloc  '')')
84
            DEALLOCATE(GeomSet,GradSet,GSave,GeomSet_free,GradSet_free)
85
            RETURN
86
         ELSE
87
            IF (PRINT)  WRITE(*,'(/,''     In allocation_flag,  GEDIIS_ALL  Alloc  '')')
88
            ALLOCATE(GeomSet(NGeomF,MRESET*NCoord),GradSet(NGeomF,MRESET*NCoord),GSAVE(NGeomF,NCoord))
89
      ALLOCATE(GeomSet_free(NGeomF,MRESET*NCoord),GradSet_free(NGeomF,MRESET*NCoord))
90
      ALLOCATE(MSET(NGeomF),FRST(NGeomF),ESET(NGeomF,MRESET))
91
      DO I=1,NGeomF
92
         FRST(I) = .TRUE.
93
         GeomSet(I,:) = 0.d0
94
         GradSet(I,:) = 0.d0
95
         GSAVE(I,:)=0.d0
96
         GeomSet_free(I,:) = 0.d0
97
         GradSet_free(I,:) = 0.d0
98
      END DO
99
      MSET(:)=0      
100
         END IF
101
     allocation_flag = .FALSE.
102
      END IF ! IF (allocation_flag) THEN
103
    
104
    ! ADDED FROM HERE:
105
    Call FreeMv(NCoord,Vfree) ! VFree(Ncoord,Ncoord), as of now, an Identity matrix.
106
      ! we orthogonalize Vfree to the tangent vector of this geom only if Tangent/=0.d0
107
      Norm=sqrt(dot_product(Tangent,Tangent))
108
      IF (Norm.GT.eps) THEN
109
         ALLOCATE(Tanf(NCoord))
110

    
111
         ! We normalize Tangent
112
         Tangent=Tangent/Norm
113

    
114
         ! We convert Tangent into Vfree only displacements. This is useless for now (2007.Apr.23)
115
     ! as Vfree=Id matrix but it will be usefull as soon as we introduce constraints.
116
         DO I=1,NCoord
117
            Tanf(I)=dot_product(reshape(Vfree(:,I),(/NCoord/)),Tangent)
118
         END DO
119
         Tangent=0.d0
120
         DO I=1,NCoord
121
            Tangent=Tangent+Tanf(I)*Vfree(:,I)
122
         END DO
123
         ! first we subtract Tangent from vfree
124
         DO I=1,NCoord
125
            Norm=dot_product(reshape(vfree(:,I),(/NCoord/)),Tangent)
126
            Vfree(:,I)=Vfree(:,I)-Norm*Tangent
127
     END DO
128

    
129
         Idx=0
130
         ! Schmidt orthogonalization of the Vfree vectors
131
         DO I=1,NCoord
132
            ! We subtract the first vectors, we do it twice as the Schmidt procedure is not numerically stable.
133
            DO Isch=1,2
134
               DO J=1,Idx
135
                  Norm=dot_product(reshape(Vfree(:,I),(/NCoord/)),reshape(Vfree(:,J),(/NCoord/)))
136
                  Vfree(:,I)=Vfree(:,I)-Norm*Vfree(:,J)
137
               END DO
138
            END DO
139
            Norm=dot_product(reshape(Vfree(:,I),(/NCoord/)),reshape(Vfree(:,I),(/NCoord/)))
140
            IF (Norm.GE.crit) THEN
141
               Idx=Idx+1
142
               Vfree(:,Idx)=Vfree(:,I)/sqrt(Norm)
143
            END IF
144
         END DO
145
       
146
         IF (Idx/= NCoord-1) THEN
147
            WRITE(*,*) "Pb in orthogonalizing Vfree to tangent for geom",IGeom
148
            WRITE(IOOut,*) "Pb in orthogonalizing Vfree to tangent for geom",IGeom
149
            STOP
150
         END IF
151
     
152
         DEALLOCATE(Tanf)
153
         NFree=Idx
154
      ELSE ! Tangent =0, matches IF (Norm.GT.eps) THEN
155
         if (debug) WRITE(*,*) "Tangent=0, using full displacement"
156
         NFree=NCoord
157
      END IF !IF (Norm.GT.eps) THEN
158
  
159
      if (debug) WRITE(*,*) 'DBG Step_GEDIIS_All, IGeom, NFree=', IGeom, NFree
160

    
161
      ! We now calculate the new step
162
      ! we project the hessian onto the free vectors
163
      ALLOCATE(HFree(NFree*NFree),Htmp(NCoord,NFree),Grad_free(NFree))
164
    ALLOCATE(Geom_free(NFree),Step_free(NFree),Geom_new_free(NFree))
165
      DO J=1,NFree
166
        DO I=1,NCoord
167
           Htmp(I,J)=0.d0
168
           DO K=1,NCoord
169
              Htmp(I,J)=Htmp(I,J)+Hess(((I-1)*NCoord)+K)*Vfree(K,J)
170
           END DO
171
        END DO
172
      END DO
173
      DO J=1,NFree
174
        DO I=1,NFree
175
       HFree(I+((J-1)*NFree))=0.d0
176
           DO K=1,NCoord
177
        HFree(I+((J-1)*NFree))=HFree(I+((J-1)*NFree))+Vfree(K,I)*Htmp(K,J)
178
           END DO
179
        END DO
180
      END DO
181

    
182
      DO I=1,NFree
183
         Grad_free(I)=dot_product(reshape(Vfree(:,I),(/NCoord/)),Grad)
184
     Geom_free(I)=dot_product(reshape(Vfree(:,I),(/NCoord/)),Geom)
185
      END DO
186
      !ADDED ENDS HERE.***********************************************
187
    
188
      ! SPACE_GEDIIS SIMPLY LOADS THE CURRENT VALUES OF Geom AND Grad INTO THE ARRAYS GeomSet
189
      ! AND GradSet, MSET is set to zero and then 1 in SPACE_GEDIIS_All at first iteration.
190
      CALL SPACE_GEDIIS_All(NGeomF,IGeom,MRESET,MSET,Geom,Grad,HEAT,NCoord,GeomSet,GradSet,ESET,FRST)
191

    
192
      IF (PRINT)  WRITE(*,'(/,''       GEDIIS after SPACE_GEDIIS_ALL   '')')
193
    
194
    DO J=1,MSet(IGeom)
195
         DO K=1,NFree
196
            GradSet_free(IGeom,((J-1)*NFree)+K)=dot_product(reshape(Vfree(:,K),(/NCoord/)),&
197
               GradSet(IGeom,((J-1)*NCoord)+1:((J-1)*NCoord)+NCoord))
198
        GeomSet_free(IGeom,((J-1)*NFree)+K)=dot_product(reshape(Vfree(:,K),(/NCoord/)),&
199
                GeomSet(IGeom,((J-1)*NCoord)+1:((J-1)*NCoord)+NCoord))
200
         END DO
201
    END DO
202

    
203
      ! INITIALIZE SOME VARIABLES AND CONSTANTS:
204
      NGEDIIS = MSET(IGeom) !MSET=mth iteration
205
      MPLUS = MSET(IGeom) + 1
206
      MM = MPLUS * MPLUS
207

    
208
      ! CONSTRUCT THE GEDIIS MATRIX:
209
      ! B_ij calculations from <B_ij=(g_i-g_j)(R_i-R_j)>
210
      JJ=0
211
      INV=-NFree
212
      DO I=1,MSET(IGeom)
213
         INV=INV+NFree
214
         JNV=-NFree
215
         DO J=1,MSET(IGeom)
216
            JNV=JNV+NFree
217
            JJ = JJ + 1
218
            B(JJ)=0.D0
219
      DO K=1, NFree
220
         B(JJ) = B(JJ) + (((GradSet_free(IGeom,INV+K)-GradSet_free(IGeom,JNV+K))* &
221
                 (GeomSet_free(IGeom,INV+K)-GeomSet_free(IGeom,JNV+K)))/2.D0)
222
      END DO
223
         END DO
224
      END DO
225

    
226
     ! The following shifting is required to correct indices of B_ij elements in the GEDIIS matrix.
227
   ! The correction is needed because the last coloumn of the matrix contains all 1 and one zero.
228
      DO I=MSET(IGeom)-1,1,-1
229
         DO J=MSET(IGeom),1,-1
230
            B(I*MSET(IGeom)+J+I) = B(I*MSET(IGeom)+J) 
231
         END DO
232
    END DO
233
   
234
      ! For the last row and last column of GEDIIS matrix:
235
      DO I=1,MPLUS
236
         B(MPLUS*I) = 1.D0
237
         B(MPLUS*MSET(IGeom)+I) = 1.D0
238
      END DO
239
      B(MM) = 0.D0
240
    
241
    DO I=1, MPLUS
242
       !WRITE(*,'(10(1X,F20.4))') B((I-1)*MPLUS+1:I*(MPLUS))
243
    END DO
244
        
245
      ! ELIMINATE ERROR VECTORS WITH THE LARGEST NORM:
246
   80 CONTINUE
247
      DO I=1,MM !MM = (MSET(IGeom)+1) * (MSET(IGeom)+1)
248
         BS(I) = B(I) !just a copy of the original B (GEDIIS) matrix
249
      END DO
250
    
251
      IF (NGEDIIS .NE. MSET(IGeom)) THEN
252
        DO II=1,MSET(IGeom)-NGEDIIS
253
           XMAX = -1.D10
254
           ITERA = 0
255
           DO I=1,MSET(IGeom)
256
              XNORM = 0.D0
257
              INV = (I-1) * MPLUS
258
              DO J=1,MSET(IGeom)
259
                 XNORM = XNORM + ABS(B(INV + J))
260
              END DO
261
              IF (XMAX.LT.XNORM .AND. XNORM.NE.1.0D0) THEN
262
                 XMAX = XNORM
263
                 ITERA = I
264
                 IONE = INV + I
265
              ENDIF
266
           END DO
267
     
268
           DO I=1,MPLUS
269
              INV = (I-1) * MPLUS
270
              DO J=1,MPLUS
271
                 JNV = (J-1) * MPLUS
272
                 IF (J.EQ.ITERA) B(INV + J) = 0.D0
273
                 B(JNV + I) = B(INV + J)
274
              END DO
275
       END DO
276
           B(IONE) = 1.0D0
277
        END DO
278
    END IF ! matches IF (NGEDIIS .NE. MSET(IGeom)) THEN
279
    
280
      ! SCALE GEDIIS MATRIX BEFORE INVERSION:
281
      DO I=1,MPLUS
282
         II = MPLUS * (I-1) + I ! B(II)=diagonal elements of B matrix
283
         GSAVE(IGeom,I) = 1.D0 / DSQRT(1.D-20+DABS(B(II)))
284
     !Print *, 'GSAVE(',IGeom,',',I,')=', GSAVE(IGeom,I) 
285
      END DO
286
      GSAVE(IGeom,MPLUS) = 1.D0
287
      DO I=1,MPLUS
288
         DO J=1,MPLUS
289
            IJ = MPLUS * (I-1) + J
290
            B(IJ) = B(IJ) * GSAVE(IGeom,I) * GSAVE(IGeom,J)
291
         END DO
292
      END DO
293
  
294
     ! INVERT THE GEDIIS MATRIX B:
295
    DO I=1, MPLUS
296
       !WRITE(*,'(10(1X,F20.4))') B((I-1)*MPLUS+1:I*(MPLUS))
297
    END DO
298
        
299
      CALL MINV(B,MPLUS,DET) ! matrix inversion.
300
    
301
    DO I=1, MPLUS
302
       !WRITE(*,'(10(1X,F20.16))') B((I-1)*MPLUS+1:I*(MPLUS))
303
    END DO
304
    
305
      DO I=1,MPLUS
306
         DO J=1,MPLUS
307
            IJ = MPLUS * (I-1) + J
308
            B(IJ) = B(IJ) * GSAVE(IGeom,I) * GSAVE(IGeom,J)
309
         END DO
310
      END DO
311
    
312
      ! COMPUTE THE NEW INTERPOLATED PARAMETER VECTOR (Geometry):    
313
    ci=0.d0
314
    ci_tmp=0.d0
315
  
316
    ci_lt_zero= .FALSE.
317
    DO I=1, MSET(IGeom)
318
     DO J=1, MSET(IGeom) ! B matrix is read column-wise
319
        ci(I)=ci(I)+B((J-1)*(MPLUS)+I)*ESET(IGeom,J) !ESET is energy set.                            
320
     END DO
321
     ci(I)=ci(I)+B((MPLUS-1)*(MPLUS)+I)
322
     !Print *, 'NO ci < 0 yet, c(',I,')=', ci(I)
323
     IF((ci(I) .LT. 0.0D0) .OR. (ci(I) .GT. 1.0D0)) THEN
324
       ci_lt_zero=.TRUE.
325
       EXIT
326
     END IF
327
      END DO !matches DO I=1, MSET(IGeom)
328
    
329
    IF (ci_lt_zero) Then
330
       cis_zero = 0
331
         ER_star = 0.D0
332
     ER_star_tmp = 1e32
333
     
334
     ! B_ij calculations from <B_ij=(g_i-g_j)(R_i-R_j)>, Full B matrix created first and then rows and columns are removed. 
335
         JJ=0
336
         INV=-NFree
337
         DO IX=1,MSET(IGeom)
338
            INV=INV+NFree
339
            JNV=-NFree
340
            DO JX=1,MSET(IGeom)
341
               JNV=JNV+NFree
342
               JJ = JJ + 1
343
               BST(JJ)=0.D0
344
           DO KX=1, NFree
345
              BST(JJ) = BST(JJ) + (((GradSet_free(IGeom,INV+KX)-GradSet_free(IGeom,JNV+KX))* &
346
                    (GeomSet_free(IGeom,INV+KX)-GeomSet_free(IGeom,JNV+KX)))/2.D0)
347
           END DO
348
            END DO
349
       END DO  
350
    
351
     DO I=1, (2**MSET(IGeom))-2 ! all (2**MSET(IGeom))-2 combinations of cis, except the one where all cis are .GT. 0 and .LT. 1
352
         !Print *, 'Entering into DO I=1, (2**MSET(IGeom))-2  loop, MSET(IGeom)=', MSET(IGeom), ', I=', I
353
         ci(:)=1.D0
354
         ! find out which cis are zero in I:
355
       DO IX=1, MSET(IGeom)
356
          JJ=IAND(I, 2**(IX-1))
357
        IF(JJ .EQ. 0) Then
358
          ci(IX)=0.D0
359
          END IF
360
       END DO
361
       
362
       ci_lt_zero = .FALSE.
363
       ! B_ij calculations from <B_ij=(g_i-g_j)(R_i-R_j)>, Full B matrix created first and then rows and columns are removed. 
364
       DO IX=1, MSET(IGeom)*MSET(IGeom)
365
                B(IX) = BST(IX) !just a copy of the original B (GEDIIS) matrix
366
             END DO
367
       
368
             ! Removal of KXth row and KXth column in order to accomodate cI to be zero:
369
       current_size_B_mat=MSET(IGeom)
370
       cis_zero = 0
371
       ! The bits of I (index of the upper loop 'DO I=1, (2**MSET(IGeom))-2'), gives which cis are zero.
372
       DO KX=1, MSET(IGeom) ! searching for each bit of I (index of the upper loop 'DO I=1, (2**MSET(IGeom))-2')
373
          IF (ci(KX) .EQ. 0.D0) Then !remove KXth row and KXth column
374
           cis_zero = cis_zero + 1
375
           
376
             ! First row removal: (B matrix is read column-wise)
377
             JJ=0
378
                   DO IX=1,current_size_B_mat ! columns reading
379
                      DO JX=1,current_size_B_mat ! rows reading
380
                 IF (JX .NE. KX) Then
381
                     JJ = JJ + 1
382
                     B_tmp(JJ) = B((IX-1)*current_size_B_mat+JX)
383
                 END IF
384
              END DO   
385
             END DO
386
       
387
             DO IX=1,current_size_B_mat*(current_size_B_mat-1)
388
                B(IX) = B_tmp(IX)
389
             END DO
390
       
391
             ! Now column removal:
392
             JJ=0   
393
                   DO IX=1,KX-1 ! columns reading
394
                      DO JX=1,current_size_B_mat-1 ! rows reading
395
                 JJ = JJ + 1
396
                 B_tmp(JJ) = B(JJ)
397
              END DO
398
             END DO        
399
        
400
                   DO IX=KX+1,current_size_B_mat
401
                      DO JX=1,current_size_B_mat-1
402
                 JJ = JJ + 1
403
                    B_tmp(JJ) = B(JJ+current_size_B_mat-1)
404
              END DO
405
             END DO
406
              
407
             DO IX=1,(current_size_B_mat-1)*(current_size_B_mat-1)
408
                B(IX) = B_tmp(IX)
409
             END DO
410
           current_size_B_mat = current_size_B_mat - 1
411
        END IF ! matches IF (ci(KX) .EQ. 0.D0) Then !remove
412
           END DO ! matches DO KX=1, MSET(IGeom)
413

    
414
       ! The following shifting is required to correct indices of B_ij elements in the GEDIIS matrix.
415
       ! The correction is needed because the last coloumn and row of the matrix contains all 1 and one zero.
416
       DO IX=MSET(IGeom)-cis_zero-1,1,-1
417
        DO JX=MSET(IGeom)-cis_zero,1,-1
418
           B(IX*(MSET(IGeom)-cis_zero)+JX+IX) = B(IX*(MSET(IGeom)-cis_zero)+JX) 
419
        END DO
420
       END DO
421
       
422
       ! for last row and last column of GEDIIS matrix 
423
       DO IX=1,MPLUS-cis_zero
424
        B((MPLUS-cis_zero)*IX) = 1.D0
425
        B((MPLUS-cis_zero)*(MSET(IGeom)-cis_zero)+IX) = 1.D0
426
       END DO
427
       B((MPLUS-cis_zero) * (MPLUS-cis_zero)) = 0.D0
428
         
429
           DO IX=1, MPLUS
430
              !WRITE(*,'(10(1X,F20.4))') B((IX-1)*MPLUS+1:IX*(MPLUS))
431
           END DO
432
              
433
       ! ELIMINATE ERROR VECTORS WITH THE LARGEST NORM:
434
             IF (NGEDIIS .NE. MSET(IGeom)) THEN
435
          JX = min(MSET(IGeom)-NGEDIIS,MSET(IGeom)-cis_zero-1)
436
                DO II=1,JX
437
                   XMAX = -1.D10
438
                   ITERA = 0
439
                   DO IX=1,MSET(IGeom)-cis_zero
440
                      XNORM = 0.D0
441
                      INV = (IX-1) * (MPLUS-cis_zero)
442
                      DO J=1,MSET(IGeom)-cis_zero
443
                         XNORM = XNORM + ABS(B(INV + J))
444
                      END DO
445
                      IF (XMAX.LT.XNORM .AND. XNORM.NE.1.0D0) THEN
446
                         XMAX = XNORM
447
                         ITERA = IX
448
                         IONE = INV + IX
449
                      ENDIF
450
           END DO
451
     
452
                   DO IX=1,MPLUS-cis_zero
453
                      INV = (IX-1) * (MPLUS-cis_zero)
454
                      DO J=1,MPLUS-cis_zero
455
                         JNV = (J-1) * (MPLUS-cis_zero)
456
                         IF (J.EQ.ITERA) B(INV + J) = 0.D0
457
                         B(JNV + IX) = B(INV + J)
458
            END DO
459
               END DO
460
                   B(IONE) = 1.0D0
461
          END DO
462
           END IF ! matches IF (NGEDIIS .NE. MSET(IGeom)) THEN
463

    
464
       ! SCALE GEDIIS MATRIX BEFORE INVERSION:
465
       DO IX=1,MPLUS-cis_zero
466
        II = (MPLUS-cis_zero) * (IX-1) + IX ! B(II)=diagonal elements of B matrix
467
        GSAVE(IGeom,IX) = 1.D0 / DSQRT(1.D-20+DABS(B(II)))
468
       END DO
469
       GSAVE(IGeom,MPLUS-cis_zero) = 1.D0
470
       DO IX=1,MPLUS-cis_zero
471
        DO JX=1,MPLUS-cis_zero
472
           IJ = (MPLUS-cis_zero) * (IX-1) + JX
473
           B(IJ) = B(IJ) * GSAVE(IGeom,IX) * GSAVE(IGeom,JX)
474
        END DO
475
       END DO
476

    
477
       ! INVERT THE GEDIIS MATRIX B:
478
       CALL MINV(B,MPLUS-cis_zero,DET) ! matrix inversion.
479
    
480
       DO IX=1,MPLUS-cis_zero
481
        DO JX=1,MPLUS-cis_zero
482
           IJ = (MPLUS-cis_zero) * (IX-1) + JX
483
           B(IJ) = B(IJ) * GSAVE(IGeom,IX) * GSAVE(IGeom,JX)
484
        END DO
485
       END DO
486
       
487
           DO IX=1, MPLUS
488
              !WRITE(*,'(10(1X,F20.4))') B((IX-1)*MPLUS+1:IX*(MPLUS))
489
           END DO
490
              
491
             ! ESET is rearranged to handle zero cis and stored in ESET_tmp:
492
       JJ=0
493
       DO IX=1, MSET(IGeom)
494
        IF (ci(IX) .NE. 0) Then
495
           JJ=JJ+1
496
           ESET_tmp(JJ) = ESET(IGeom,IX)
497
        END IF
498
       END DO
499
       
500
       ! DETERMINATION OF nonzero cis:
501
       MyPointer=1
502
            DO IX=1, MSET(IGeom)-cis_zero
503
          tmp = 0.D0
504
            DO J=1, MSET(IGeom)-cis_zero ! B matrix is read column-wise
505
           tmp=tmp+B((J-1)*(MPLUS-cis_zero)+IX)*ESET_tmp(J)
506
        END DO
507
            tmp=tmp+B((MPLUS-cis_zero-1)*(MPLUS-cis_zero)+IX)
508
            IF((tmp .LT. 0.0D0) .OR. (tmp .GT. 1.0D0)) THEN
509
               ci_lt_zero=.TRUE.
510
               EXIT
511
        ELSE
512
           DO JX=MyPointer,MSET(IGeom)
513
              IF (ci(JX) .NE. 0) Then
514
                 ci(JX) = tmp
515
               MyPointer=JX+1
516
               EXIT
517
            END IF
518
           END DO
519
            END IF
520
             END DO !matches DO I=1, MSET(IGeom)-cis_zero
521
           !Print *, 'Local set of cis, first 10:, MSET(IGeom)=', MSET(IGeom), ', I of (2**MSET(IGeom))-2=', I
522
       !WRITE(*,'(10(1X,F20.4))') ci(1:MSET(IGeom))
523
           !Print *, 'Local set of cis ends:****************************************'
524

    
525
             ! new set of cis determined based on the lower energy (ER_star):
526
       IF (.NOT. ci_lt_zero) Then
527
                Call Energy_GEDIIS(MRESET,MSET(IGeom),ci,GeomSet_free(IGeom,:),GradSet_free(IGeom,:),ESET(IGeom,:),NFree,ER_star)
528
        IF (ER_star .LT. ER_star_tmp) Then
529
           ci_tmp=ci
530
           ER_star_tmp = ER_star
531
        END IF
532
             END IF ! matches IF (.NOT. ci_lt_zero) Then
533
          END DO !matches DO I=1, (2**K)-2 ! all (2**K)-2 combinations of cis, except the one where all cis are .GT. 0 and .LT. 1 
534
      ci = ci_tmp
535
    END IF! matches IF (ci_lt_zero) Then
536
    
537
    Print *, 'Final set of cis, first 10:***********************************'
538
    WRITE(*,'(10(1X,F20.4))') ci(1:MSET(IGeom))
539
    Print *, 'Final set of cis ends:****************************************'
540
    Geom_new_free(:) = 0.D0
541
    DO I=1, MSET(IGeom)    
542
         Geom_new_free(:) = Geom_new_free(:) + (ci(I)*GeomSet_free(IGeom,(I-1)*NFree+1:I*NFree)) !MPLUS=MSET(IGeom)+1
543
     ! R_(N+1)=R*+DeltaR:
544
     DO J=1, NFree
545
      tmp=0.D0
546
      DO K=1,NFree
547
         ! this can be commented:
548
         !tmp=tmp+HFree((J-1)*NFree+K)*GradSet_free(IGeom,(I-1)*NFree+K) ! If Hinv=.False., then we need to invert Hess
549
      END DO
550
      Geom_new_free(J) = Geom_new_free(J) - (ci(I)*tmp)
551
     END DO
552
    END DO
553
    
554
    Step_free(:) =  Geom_new_free(:) - Geom_free(:)
555
    
556
      XNORM = SQRT(DOT_PRODUCT(Step_free,Step_free))
557
      IF (PRINT) THEN
558
         WRITE (6,'(/10X,''DEVIATION IN X '',F10.4,8X,''DETERMINANT '',G9.3)') XNORM, DET
559
         !WRITE(*,'(10X,''GEDIIS COEFFICIENTS'')')
560
         !WRITE(*,'(10X,5F12.5)') (B(MPLUS*MSET(IGeom)+I),I=1,MSET(IGeom))
561
      ENDIF
562

    
563
      ! THE FOLLOWING TOLERENCES FOR XNORM AND DET ARE SOMEWHAT ARBITRARY!
564
      THRES = MAX(10.D0**(-NFree), 1.D-25)
565
      IF (XNORM.GT.2.D0 .OR. DABS(DET) .LT. THRES) THEN
566
         IF (PRINT)THEN
567
            WRITE(*,*) "THE GEDIIS MATRIX IS ILL CONDITIONED"     
568
            WRITE(*,*) " - PROBABLY, VECTORS ARE LINEARLY DEPENDENT - "
569
            WRITE(*,*) "THE GEDIIS STEP WILL BE REPEATED WITH A SMALLER SPACE"
570
         END IF
571
         DO K=1,MM
572
      B(K) = BS(K) ! why this is reverted? Because "IF (NGEDIIS .GT. 0) GO TO 80", see below
573
         END DO
574
         NGEDIIS = NGEDIIS - 1
575
         IF (NGEDIIS .GT. 0) GO TO 80
576
         IF (PRINT) WRITE(*,'(10X,''NEWTON-RAPHSON STEP TAKEN'')')
577
         Geom_new_free(:) = Geom_free(:) ! Geom_new is set to original Geom, thus Step = Geom(:) - Geom_new(:)=zero, the whole 
578
                           ! new update to Geom_new is discarded, since XNORM.GT.2.D0 .OR. DABS(DET) .LT. THRES
579
      END IF ! matches IF (XNORM.GT.2.D0 .OR. DABS(DET).LT. THRES) THEN
580
    
581
    !******************************************************************************************************************
582
    Geom_new_free(:) = 0.D0
583
    DO I=1, MSET(IGeom)    
584
         Geom_new_free(:) = Geom_new_free(:) + (ci(I)*GeomSet_free(IGeom,(I-1)*NFree+1:I*NFree)) !MPLUS=MSET(IGeom)+1
585
     ! R_(N+1)=R*+DeltaR:
586
     DO J=1, NFree
587
      tmp=0.D0
588
      DO K=1,NFree
589
         tmp=tmp+HFree((J-1)*NFree+K)*GradSet_free(IGeom,(I-1)*NFree+K) ! If Hinv=.False., then we need to invert Hess
590
      END DO
591
      Geom_new_free(J) = Geom_new_free(J) - (ci(I)*tmp)
592
     END DO
593
    END DO
594
    
595
    Step_free(:) =  Geom_new_free(:) - Geom_free(:)
596
   !******************************************************************************************************************
597
    Step = 0.d0
598
      DO I=1,NFree
599
     Step = Step + Step_free(I)*Vfree(:,I)
600
      END DO    
601
    
602
      DEALLOCATE(Hfree,Htmp,Grad_free,Step_free,Geom_free,Geom_new_free)
603
    
604
      IF (PRINT)  WRITE(*,'(/,''       END Step_GEDIIS_ALL   '',/)')
605

    
606
      END SUBROUTINE Step_GEDIIS_All
607