Statistiques
| Révision :

root / src / Step_GEDIIS.f90 @ 3

Historique | Voir | Annoter | Télécharger (15,66 ko)

1 1 pfleura2
     ! Geom = input parameter vector (Geometry), Grad = input gradient vector.
2 1 pfleura2
     ! HEAT is Energy(Geom)
3 1 pfleura2
      SUBROUTINE Step_GEDIIS(Geom_new,Geom,Grad,HEAT,Hess,NCoord,FRST)
4 1 pfleura2
5 1 pfleura2
    use Io_module
6 1 pfleura2
    use Path_module, only : Nom, Atome, OrderInv, indzmat, Pi, Nat
7 1 pfleura2
8 1 pfleura2
      IMPLICIT NONE
9 1 pfleura2
10 1 pfleura2
      INTEGER(KINT) :: NCoord
11 1 pfleura2
      REAL(KREAL) :: Geom_new(NCoord), Grad(NCoord), Hess(NCoord*NCoord)
12 1 pfleura2
    REAL(KREAL), INTENT(IN) :: Geom(NCoord)
13 1 pfleura2
    REAL(KREAL) :: HEAT ! HEAT= Energy
14 1 pfleura2
      LOGICAL :: FRST
15 1 pfleura2
16 1 pfleura2
      ! MRESET = maximum number of iterations.
17 1 pfleura2
      INTEGER(KINT), PARAMETER :: MRESET=15, M2=(MRESET+1)*(MRESET+1) !M2 = 256
18 1 pfleura2
      REAL(KREAL), ALLOCATABLE, SAVE :: GeomSet(:), GradSet(:) ! MRESET*NCoord
19 1 pfleura2
      REAL(KREAL), ALLOCATABLE, SAVE :: DX(:), GSAVE(:) !NCoord
20 1 pfleura2
    REAL(KREAL), SAVE :: ESET(MRESET)
21 1 pfleura2
    REAL(KREAL) :: ESET_tmp(MRESET), B(M2),BS(M2),BST(M2), B_tmp(M2) ! M2=256
22 1 pfleura2
      LOGICAL DEBUG, PRINT, ci_lt_zero
23 1 pfleura2
      INTEGER(KINT), SAVE :: MSET ! mth Iteration
24 1 pfleura2
    REAL(KREAL) :: ci(MRESET), ci_tmp(MRESET) ! MRESET = maximum number of iterations.
25 2 pfleura2
      INTEGER(KINT) :: NGEDIIS, MPLUS, INV, ITERA, MM, cis_zero
26 2 pfleura2
      INTEGER(KINT) :: I, J, K, JJ, JNV, II, IONE, IJ, IX, JX, KX
27 2 pfleura2
    INTEGER(KINT) :: current_size_B_mat, MyPointer
28 2 pfleura2
      REAL(KREAL) :: XMax, XNorm, DET, THRES, tmp, ER_star, ER_star_tmp
29 1 pfleura2
30 1 pfleura2
      DEBUG=.TRUE.
31 1 pfleura2
      PRINT=.FALSE.
32 1 pfleura2
33 1 pfleura2
      IF (PRINT)  WRITE(*,'(/,''      BEGIN GEDIIS   '')')
34 1 pfleura2
35 1 pfleura2
      ! Initialization
36 1 pfleura2
      IF (FRST) THEN
37 1 pfleura2
      ! FRST will be set to False in SPACE_GEDIIS, so no need to modify it here
38 1 pfleura2
         IF (ALLOCATED(GeomSet)) THEN
39 1 pfleura2
            IF (PRINT)  WRITE(*,'(/,''    In FRST, GEDIIS Dealloc  '')')
40 1 pfleura2
            DEALLOCATE(GeomSet,GradSet,DX,GSave)
41 1 pfleura2
            RETURN
42 1 pfleura2
         ELSE
43 1 pfleura2
            IF (PRINT)  WRITE(*,'(/,''     In FRST,  GEDIIS Alloc  '')')
44 1 pfleura2
            ALLOCATE(GeomSet(MRESET*NCoord),GradSet(MRESET*NCoord),DX(NCoord),GSAVE(NCoord))
45 1 pfleura2
         END IF
46 1 pfleura2
      END IF ! IF (FRST) THEN
47 1 pfleura2
48 1 pfleura2
      ! SPACE_GEDIIS SIMPLY LOADS THE CURRENT VALUES OF Geom AND Grad INTO THE ARRAYS GeomSet
49 1 pfleura2
      ! AND GradSet, MSET is set to zero and then 1 in SPACE_GEDIIS at first iteration.
50 1 pfleura2
      CALL SPACE_GEDIIS(MRESET,MSET,Geom,Grad,HEAT,NCoord,GeomSet,GradSet,ESET,FRST)
51 1 pfleura2
52 1 pfleura2
      IF (PRINT)  WRITE(*,'(/,''       GEDIIS after SPACE_GEDIIS  '')')
53 1 pfleura2
54 1 pfleura2
      ! INITIALIZE SOME VARIABLES AND CONSTANTS:
55 1 pfleura2
      NGEDIIS = MSET !MSET=mth iteration
56 1 pfleura2
      MPLUS = MSET + 1
57 1 pfleura2
      MM = MPLUS * MPLUS
58 1 pfleura2
59 1 pfleura2
      ! CONSTRUCT THE GEDIIS MATRIX:
60 1 pfleura2
      ! B_ij calculations from <B_ij=(g_i-g_j)(R_i-R_j)>
61 1 pfleura2
      JJ=0
62 1 pfleura2
      INV=-NCoord
63 1 pfleura2
      DO I=1,MSET
64 1 pfleura2
         INV=INV+NCoord
65 1 pfleura2
         JNV=-NCoord
66 1 pfleura2
         DO J=1,MSET
67 1 pfleura2
            JNV=JNV+NCoord
68 1 pfleura2
            JJ = JJ + 1
69 1 pfleura2
            B(JJ)=0.D0
70 1 pfleura2
      DO K=1, NCoord
71 1 pfleura2
         B(JJ) = B(JJ) + (((GradSet(INV+K)-GradSet(JNV+K))*(GeomSet(INV+K)-GeomSet(JNV+K)))/2.D0)
72 1 pfleura2
      END DO
73 1 pfleura2
         END DO
74 1 pfleura2
      END DO
75 1 pfleura2
76 1 pfleura2
     ! The following shifting is required to correct indices of B_ij elements in the GEDIIS matrix.
77 1 pfleura2
   ! The correction is needed because the last coloumn of the matrix contains all 1 and one zero.
78 1 pfleura2
      DO I=MSET-1,1,-1
79 1 pfleura2
         DO J=MSET,1,-1
80 1 pfleura2
            B(I*MSET+J+I) = B(I*MSET+J)
81 1 pfleura2
         END DO
82 1 pfleura2
    END DO
83 1 pfleura2
84 1 pfleura2
      ! for last row and last column of GEDIIS matrix
85 1 pfleura2
      DO I=1,MPLUS
86 1 pfleura2
         B(MPLUS*I) = 1.D0
87 1 pfleura2
         B(MPLUS*MSET+I) = 1.D0
88 1 pfleura2
      END DO
89 1 pfleura2
      B(MM) = 0.D0
90 1 pfleura2
91 1 pfleura2
    DO I=1, MPLUS
92 1 pfleura2
       !WRITE(*,'(10(1X,F20.4))') B((I-1)*MPLUS+1:I*(MPLUS))
93 1 pfleura2
    END DO
94 1 pfleura2
95 1 pfleura2
      ! ELIMINATE ERROR VECTORS WITH THE LARGEST NORM:
96 1 pfleura2
   80 CONTINUE
97 1 pfleura2
      DO I=1,MM !MM = (MSET+1) * (MSET+1)
98 1 pfleura2
         BS(I) = B(I) !just a copy of the original B (GEDIIS) matrix
99 1 pfleura2
      END DO
100 1 pfleura2
101 1 pfleura2
      IF (NGEDIIS .NE. MSET) THEN
102 1 pfleura2
        DO II=1,MSET-NGEDIIS
103 1 pfleura2
           XMAX = -1.D10
104 1 pfleura2
           ITERA = 0
105 1 pfleura2
           DO I=1,MSET
106 1 pfleura2
              XNORM = 0.D0
107 1 pfleura2
              INV = (I-1) * MPLUS
108 1 pfleura2
              DO J=1,MSET
109 1 pfleura2
                 XNORM = XNORM + ABS(B(INV + J))
110 1 pfleura2
              END DO
111 1 pfleura2
              IF (XMAX.LT.XNORM .AND. XNORM.NE.1.0D0) THEN
112 1 pfleura2
                 XMAX = XNORM
113 1 pfleura2
                 ITERA = I
114 1 pfleura2
                 IONE = INV + I
115 1 pfleura2
              ENDIF
116 1 pfleura2
           END DO
117 1 pfleura2
118 1 pfleura2
           DO I=1,MPLUS
119 1 pfleura2
              INV = (I-1) * MPLUS
120 1 pfleura2
              DO J=1,MPLUS
121 1 pfleura2
                 JNV = (J-1) * MPLUS
122 1 pfleura2
                 IF (J.EQ.ITERA) B(INV + J) = 0.D0
123 1 pfleura2
                 B(JNV + I) = B(INV + J)
124 1 pfleura2
              END DO
125 1 pfleura2
       END DO
126 1 pfleura2
           B(IONE) = 1.0D0
127 1 pfleura2
        END DO
128 1 pfleura2
    END IF ! matches IF (NGEDIIS .NE. MSET) THEN
129 1 pfleura2
130 1 pfleura2
      ! SCALE GEDIIS MATRIX BEFORE INVERSION:
131 1 pfleura2
      DO I=1,MPLUS
132 1 pfleura2
         II = MPLUS * (I-1) + I ! B(II)=diagonal elements of B matrix
133 1 pfleura2
         GSAVE(I) = 1.D0 / DSQRT(1.D-20+DABS(B(II)))
134 1 pfleura2
     !Print *, 'GSAVE(',I,')=', GSAVE(I)
135 1 pfleura2
      END DO
136 1 pfleura2
      GSAVE(MPLUS) = 1.D0
137 1 pfleura2
      DO I=1,MPLUS
138 1 pfleura2
         DO J=1,MPLUS
139 1 pfleura2
            IJ = MPLUS * (I-1) + J
140 1 pfleura2
            B(IJ) = B(IJ) * GSAVE(I) * GSAVE(J)
141 1 pfleura2
         END DO
142 1 pfleura2
      END DO
143 1 pfleura2
144 1 pfleura2
     ! INVERT THE GEDIIS MATRIX B:
145 1 pfleura2
    DO I=1, MPLUS
146 1 pfleura2
       !WRITE(*,'(10(1X,F20.4))') B((I-1)*MPLUS+1:I*(MPLUS))
147 1 pfleura2
    END DO
148 1 pfleura2
149 1 pfleura2
      CALL MINV(B,MPLUS,DET) ! matrix inversion.
150 1 pfleura2
151 1 pfleura2
    DO I=1, MPLUS
152 1 pfleura2
       !WRITE(*,'(10(1X,F20.16))') B((I-1)*MPLUS+1:I*(MPLUS))
153 1 pfleura2
    END DO
154 1 pfleura2
155 1 pfleura2
      DO I=1,MPLUS
156 1 pfleura2
         DO J=1,MPLUS
157 1 pfleura2
            IJ = MPLUS * (I-1) + J
158 1 pfleura2
            B(IJ) = B(IJ) * GSAVE(I) * GSAVE(J)
159 1 pfleura2
         END DO
160 1 pfleura2
      END DO
161 1 pfleura2
162 1 pfleura2
      ! COMPUTE THE NEW INTERPOLATED PARAMETER VECTOR (Geometry):
163 1 pfleura2
    ci=0.d0
164 1 pfleura2
    ci_tmp=0.d0
165 1 pfleura2
166 1 pfleura2
    ci_lt_zero= .FALSE.
167 1 pfleura2
    DO I=1, MSET
168 1 pfleura2
     DO J=1, MSET ! B matrix is read column-wise
169 1 pfleura2
        ci(I)=ci(I)+B((J-1)*(MPLUS)+I)*ESET(J) !ESET is energy set, yet to be fixed.
170 1 pfleura2
     END DO
171 1 pfleura2
     ci(I)=ci(I)+B((MPLUS-1)*(MPLUS)+I)
172 1 pfleura2
     !Print *, 'NO ci < 0 yet, c(',I,')=', ci(I)
173 1 pfleura2
     IF((ci(I) .LT. 0.0D0) .OR. (ci(I) .GT. 1.0D0)) THEN
174 1 pfleura2
       ci_lt_zero=.TRUE.
175 1 pfleura2
       EXIT
176 1 pfleura2
     END IF
177 1 pfleura2
      END DO !matches DO I=1, MSET
178 1 pfleura2
179 1 pfleura2
    IF (ci_lt_zero) Then
180 1 pfleura2
       cis_zero = 0
181 1 pfleura2
         ER_star = 0.D0
182 1 pfleura2
     ER_star_tmp = 1e32
183 1 pfleura2
184 1 pfleura2
     ! 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.
185 1 pfleura2
         JJ=0
186 1 pfleura2
         INV=-NCoord
187 1 pfleura2
         DO IX=1,MSET
188 1 pfleura2
            INV=INV+NCoord
189 1 pfleura2
            JNV=-NCoord
190 1 pfleura2
            DO JX=1,MSET
191 1 pfleura2
               JNV=JNV+NCoord
192 1 pfleura2
               JJ = JJ + 1
193 1 pfleura2
               BST(JJ)=0.D0
194 1 pfleura2
           DO KX=1, NCoord
195 1 pfleura2
              BST(JJ) = BST(JJ) + (((GradSet(INV+KX)-GradSet(JNV+KX))*(GeomSet(INV+KX)-GeomSet(JNV+KX)))/2.D0)
196 1 pfleura2
           END DO
197 1 pfleura2
            END DO
198 1 pfleura2
       END DO
199 1 pfleura2
200 1 pfleura2
     DO I=1, (2**MSET)-2 ! all (2**MSET)-2 combinations of cis, except the one where all cis are .GT. 0 and .LT. 1
201 1 pfleura2
         ci(:)=1.D0
202 1 pfleura2
         ! find out which cis are zero in I:
203 1 pfleura2
       DO IX=1, MSET
204 1 pfleura2
          JJ=IAND(I, 2**(IX-1))
205 1 pfleura2
        IF(JJ .EQ. 0) Then
206 1 pfleura2
          ci(IX)=0.D0
207 1 pfleura2
          END IF
208 1 pfleura2
       END DO
209 1 pfleura2
210 1 pfleura2
       ci_lt_zero = .FALSE.
211 1 pfleura2
       ! 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.
212 1 pfleura2
       DO IX=1, MSET*MSET
213 1 pfleura2
                B(IX) = BST(IX) !just a copy of the original B (GEDIIS) matrix
214 1 pfleura2
             END DO
215 1 pfleura2
216 1 pfleura2
             ! Removal of KXth row and KXth column in order to accomodate cI to be zero:
217 1 pfleura2
       current_size_B_mat=MSET
218 1 pfleura2
       cis_zero = 0
219 1 pfleura2
       ! The bits of I (index of the upper loop 'DO I=1, (2**MSET)-2'), gives which cis are zero.
220 1 pfleura2
       DO KX=1, MSET ! searching for each bit of I (index of the upper loop 'DO I=1, (2**MSET)-2')
221 1 pfleura2
          IF (ci(KX) .EQ. 0.D0) Then !remove KXth row and KXth column
222 1 pfleura2
           cis_zero = cis_zero + 1
223 1 pfleura2
224 1 pfleura2
             ! First row removal: (B matrix is read column-wise)
225 1 pfleura2
             JJ=0
226 1 pfleura2
                   DO IX=1,current_size_B_mat ! columns reading
227 1 pfleura2
                      DO JX=1,current_size_B_mat ! rows reading
228 1 pfleura2
                 IF (JX .NE. KX) Then
229 1 pfleura2
                     JJ = JJ + 1
230 1 pfleura2
                     B_tmp(JJ) = B((IX-1)*current_size_B_mat+JX)
231 1 pfleura2
                 END IF
232 1 pfleura2
              END DO
233 1 pfleura2
             END DO
234 1 pfleura2
235 1 pfleura2
             DO IX=1,current_size_B_mat*(current_size_B_mat-1)
236 1 pfleura2
                B(IX) = B_tmp(IX)
237 1 pfleura2
             END DO
238 1 pfleura2
239 1 pfleura2
             ! Now column removal:
240 1 pfleura2
             JJ=0
241 1 pfleura2
                   DO IX=1,KX-1 ! columns reading
242 1 pfleura2
                      DO JX=1,current_size_B_mat-1 ! rows reading
243 1 pfleura2
                 JJ = JJ + 1
244 1 pfleura2
                 B_tmp(JJ) = B(JJ)
245 1 pfleura2
              END DO
246 1 pfleura2
             END DO
247 1 pfleura2
248 1 pfleura2
                   DO IX=KX+1,current_size_B_mat
249 1 pfleura2
                      DO JX=1,current_size_B_mat-1
250 1 pfleura2
                 JJ = JJ + 1
251 1 pfleura2
                    B_tmp(JJ) = B(JJ+current_size_B_mat-1)
252 1 pfleura2
              END DO
253 1 pfleura2
             END DO
254 1 pfleura2
255 1 pfleura2
             DO IX=1,(current_size_B_mat-1)*(current_size_B_mat-1)
256 1 pfleura2
                B(IX) = B_tmp(IX)
257 1 pfleura2
             END DO
258 1 pfleura2
           current_size_B_mat = current_size_B_mat - 1
259 1 pfleura2
        END IF ! matches IF (ci(KX) .EQ. 0.D0) Then !remove
260 1 pfleura2
           END DO ! matches DO KX=1, MSET
261 1 pfleura2
262 1 pfleura2
       ! The following shifting is required to correct indices of B_ij elements in the GEDIIS matrix.
263 1 pfleura2
       ! The correction is needed because the last coloumn and row of the matrix contains all 1 and one zero.
264 1 pfleura2
       DO IX=MSET-cis_zero-1,1,-1
265 1 pfleura2
        DO JX=MSET-cis_zero,1,-1
266 1 pfleura2
           B(IX*(MSET-cis_zero)+JX+IX) = B(IX*(MSET-cis_zero)+JX)
267 1 pfleura2
        END DO
268 1 pfleura2
       END DO
269 1 pfleura2
270 1 pfleura2
       ! for last row and last column of GEDIIS matrix
271 1 pfleura2
       DO IX=1,MPLUS-cis_zero
272 1 pfleura2
        B((MPLUS-cis_zero)*IX) = 1.D0
273 1 pfleura2
        B((MPLUS-cis_zero)*(MSET-cis_zero)+IX) = 1.D0
274 1 pfleura2
       END DO
275 1 pfleura2
       B((MPLUS-cis_zero) * (MPLUS-cis_zero)) = 0.D0
276 1 pfleura2
277 1 pfleura2
           DO IX=1, MPLUS
278 1 pfleura2
              !WRITE(*,'(10(1X,F20.4))') B((IX-1)*MPLUS+1:IX*(MPLUS))
279 1 pfleura2
           END DO
280 1 pfleura2
281 1 pfleura2
       ! ELIMINATE ERROR VECTORS WITH THE LARGEST NORM:
282 1 pfleura2
             IF (NGEDIIS .NE. MSET) THEN
283 1 pfleura2
          JX = min(MSET-NGEDIIS,MSET-cis_zero-1)
284 1 pfleura2
                DO II=1,JX
285 1 pfleura2
                   XMAX = -1.D10
286 1 pfleura2
                   ITERA = 0
287 1 pfleura2
                   DO IX=1,MSET-cis_zero
288 1 pfleura2
                      XNORM = 0.D0
289 1 pfleura2
                      INV = (IX-1) * (MPLUS-cis_zero)
290 1 pfleura2
                      DO J=1,MSET-cis_zero
291 1 pfleura2
                         XNORM = XNORM + ABS(B(INV + J))
292 1 pfleura2
                      END DO
293 1 pfleura2
                      IF (XMAX.LT.XNORM .AND. XNORM.NE.1.0D0) THEN
294 1 pfleura2
                         XMAX = XNORM
295 1 pfleura2
                         ITERA = IX
296 1 pfleura2
                         IONE = INV + IX
297 1 pfleura2
                      ENDIF
298 1 pfleura2
           END DO
299 1 pfleura2
300 1 pfleura2
                   DO IX=1,MPLUS-cis_zero
301 1 pfleura2
                      INV = (IX-1) * (MPLUS-cis_zero)
302 1 pfleura2
                      DO J=1,MPLUS-cis_zero
303 1 pfleura2
                         JNV = (J-1) * (MPLUS-cis_zero)
304 1 pfleura2
                         IF (J.EQ.ITERA) B(INV + J) = 0.D0
305 1 pfleura2
                         B(JNV + IX) = B(INV + J)
306 1 pfleura2
            END DO
307 1 pfleura2
               END DO
308 1 pfleura2
                   B(IONE) = 1.0D0
309 1 pfleura2
          END DO
310 1 pfleura2
           END IF ! matches IF (NGEDIIS .NE. MSET) THEN
311 1 pfleura2
312 1 pfleura2
       ! SCALE GEDIIS MATRIX BEFORE INVERSION:
313 1 pfleura2
       DO IX=1,MPLUS-cis_zero
314 1 pfleura2
        II = (MPLUS-cis_zero) * (IX-1) + IX ! B(II)=diagonal elements of B matrix
315 1 pfleura2
        GSAVE(IX) = 1.D0 / DSQRT(1.D-20+DABS(B(II)))
316 1 pfleura2
       END DO
317 1 pfleura2
       GSAVE(MPLUS-cis_zero) = 1.D0
318 1 pfleura2
       DO IX=1,MPLUS-cis_zero
319 1 pfleura2
        DO JX=1,MPLUS-cis_zero
320 1 pfleura2
           IJ = (MPLUS-cis_zero) * (IX-1) + JX
321 1 pfleura2
           B(IJ) = B(IJ) * GSAVE(IX) * GSAVE(JX)
322 1 pfleura2
        END DO
323 1 pfleura2
       END DO
324 1 pfleura2
325 1 pfleura2
       ! INVERT THE GEDIIS MATRIX B:
326 1 pfleura2
       CALL MINV(B,MPLUS-cis_zero,DET) ! matrix inversion.
327 1 pfleura2
328 1 pfleura2
       DO IX=1,MPLUS-cis_zero
329 1 pfleura2
        DO JX=1,MPLUS-cis_zero
330 1 pfleura2
           IJ = (MPLUS-cis_zero) * (IX-1) + JX
331 1 pfleura2
           B(IJ) = B(IJ) * GSAVE(IX) * GSAVE(JX)
332 1 pfleura2
        END DO
333 1 pfleura2
       END DO
334 1 pfleura2
335 1 pfleura2
           DO IX=1, MPLUS
336 1 pfleura2
              !WRITE(*,'(10(1X,F20.4))') B((IX-1)*MPLUS+1:IX*(MPLUS))
337 1 pfleura2
           END DO
338 1 pfleura2
339 1 pfleura2
             ! ESET is rearranged to handle zero cis and stored in ESET_tmp:
340 1 pfleura2
       JJ=0
341 1 pfleura2
       DO IX=1, MSET
342 1 pfleura2
        IF (ci(IX) .NE. 0) Then
343 1 pfleura2
           JJ=JJ+1
344 1 pfleura2
           ESET_tmp(JJ) = ESET(IX)
345 1 pfleura2
        END IF
346 1 pfleura2
       END DO
347 1 pfleura2
348 1 pfleura2
       ! DETERMINATION OF nonzero cis:
349 1 pfleura2
       MyPointer=1
350 1 pfleura2
            DO IX=1, MSET-cis_zero
351 1 pfleura2
          tmp = 0.D0
352 1 pfleura2
            DO J=1, MSET-cis_zero ! B matrix is read column-wise
353 1 pfleura2
           tmp=tmp+B((J-1)*(MPLUS-cis_zero)+IX)*ESET_tmp(J)
354 1 pfleura2
        END DO
355 1 pfleura2
            tmp=tmp+B((MPLUS-cis_zero-1)*(MPLUS-cis_zero)+IX)
356 1 pfleura2
            IF((tmp .LT. 0.0D0) .OR. (tmp .GT. 1.0D0)) THEN
357 1 pfleura2
               ci_lt_zero=.TRUE.
358 1 pfleura2
               EXIT
359 1 pfleura2
        ELSE
360 1 pfleura2
           DO JX=MyPointer,MSET
361 1 pfleura2
              IF (ci(JX) .NE. 0) Then
362 1 pfleura2
                 ci(JX) = tmp
363 1 pfleura2
               MyPointer=JX+1
364 1 pfleura2
               EXIT
365 1 pfleura2
            END IF
366 1 pfleura2
           END DO
367 1 pfleura2
            END IF
368 1 pfleura2
             END DO !matches DO I=1, MSET-cis_zero
369 1 pfleura2
           !Print *, 'Local set of cis, first 10:, MSET=', MSET, ', I of (2**MSET)-2=', I
370 1 pfleura2
       !WRITE(*,'(10(1X,F20.4))') ci(1:MSET)
371 1 pfleura2
           !Print *, 'Local set of cis ends:****************************************'
372 1 pfleura2
373 1 pfleura2
             ! new set of cis determined based on the lower energy (ER_star):
374 1 pfleura2
       IF (.NOT. ci_lt_zero) Then
375 1 pfleura2
                Call Energy_GEDIIS(MRESET,MSET,ci,GeomSet,GradSet,ESET,NCoord,ER_star)
376 1 pfleura2
        IF (ER_star .LT. ER_star_tmp) Then
377 1 pfleura2
           ci_tmp=ci
378 1 pfleura2
           ER_star_tmp = ER_star
379 1 pfleura2
        END IF
380 1 pfleura2
             END IF ! matches IF (.NOT. ci_lt_zero) Then
381 1 pfleura2
          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
382 1 pfleura2
      ci = ci_tmp
383 1 pfleura2
    END IF! matches IF (ci_lt_zero) Then
384 1 pfleura2
385 1 pfleura2
    Print *, 'Final set of cis, first 10:***********************************'
386 1 pfleura2
    WRITE(*,'(10(1X,F20.4))') ci(1:MSET)
387 1 pfleura2
    Print *, 'Final set of cis ends:****************************************'
388 1 pfleura2
    Geom_new(:) = 0.D0
389 1 pfleura2
    DO I=1, MSET
390 1 pfleura2
         Geom_new(:) = Geom_new(:) + (ci(I)*GeomSet((I-1)*NCoord+1:I*NCoord)) !MPLUS=MSET+1
391 1 pfleura2
     ! R_(N+1)=R*+DeltaR:
392 1 pfleura2
     DO J=1, NCoord
393 1 pfleura2
      tmp=0.D0
394 1 pfleura2
      DO K=1,NCoord
395 1 pfleura2
         !tmp=tmp+Hess((J-1)*NCoord+K)*GradSet((I-1)*NCoord+K) ! If Hinv=.False., then we need to invert Hess
396 1 pfleura2
      END DO
397 1 pfleura2
      Geom_new(J) = Geom_new(J) - (ci(I)*tmp)
398 1 pfleura2
     END DO
399 1 pfleura2
    END DO
400 1 pfleura2
401 1 pfleura2
    DX(:) = Geom(:) - Geom_new(:)
402 1 pfleura2
403 1 pfleura2
      XNORM = SQRT(DOT_PRODUCT(DX,DX))
404 1 pfleura2
      IF (PRINT) THEN
405 1 pfleura2
         WRITE (6,'(/10X,''DEVIATION IN X '',F10.4,8X,''DETERMINANT '',G9.3)') XNORM, DET
406 1 pfleura2
         !WRITE(*,'(10X,''GEDIIS COEFFICIENTS'')')
407 1 pfleura2
         !WRITE(*,'(10X,5F12.5)') (B(MPLUS*MSET+I),I=1,MSET)
408 1 pfleura2
      ENDIF
409 1 pfleura2
410 1 pfleura2
      ! THE FOLLOWING TOLERENCES FOR XNORM AND DET ARE SOMEWHAT ARBITRARY!
411 1 pfleura2
      THRES = MAX(10.D0**(-NCoord), 1.D-25)
412 1 pfleura2
      IF (XNORM.GT.2.D0 .OR. DABS(DET) .LT. THRES) THEN
413 1 pfleura2
         IF (PRINT)THEN
414 1 pfleura2
            WRITE(*,*) "THE GEDIIS MATRIX IS ILL CONDITIONED"
415 1 pfleura2
            WRITE(*,*) " - PROBABLY, VECTORS ARE LINEARLY DEPENDENT - "
416 1 pfleura2
            WRITE(*,*) "THE GEDIIS STEP WILL BE REPEATED WITH A SMALLER SPACE"
417 1 pfleura2
         END IF
418 1 pfleura2
         DO K=1,MM
419 1 pfleura2
      B(K) = BS(K) ! why this is reverted? Because "IF (NGEDIIS .GT. 0) GO TO 80", see below
420 1 pfleura2
         END DO
421 1 pfleura2
         NGEDIIS = NGEDIIS - 1
422 1 pfleura2
         IF (NGEDIIS .GT. 0) GO TO 80
423 1 pfleura2
         IF (PRINT) WRITE(*,'(10X,''NEWTON-RAPHSON STEP TAKEN'')')
424 1 pfleura2
         Geom_new(:) = Geom(:) ! Geom_new is set to original Geom, thus DX = Geom(:) - Geom_new(:)=zero
425 1 pfleura2
      END IF ! matches IF (XNORM.GT.2.D0 .OR. DABS(DET).LT. THRES) THEN
426 1 pfleura2
427 1 pfleura2
   !*******************************************************************************************************************
428 1 pfleura2
    Geom_new(:) = 0.D0
429 1 pfleura2
    DO I=1, MSET
430 1 pfleura2
         Geom_new(:) = Geom_new(:) + (ci(I)*GeomSet((I-1)*NCoord+1:I*NCoord)) !MPLUS=MSET+1
431 1 pfleura2
     ! R_(N+1)=R*+DeltaR:
432 1 pfleura2
     DO J=1, NCoord
433 1 pfleura2
      tmp=0.D0
434 1 pfleura2
      DO K=1,NCoord
435 1 pfleura2
         tmp=tmp+Hess((J-1)*NCoord+K)*GradSet((I-1)*NCoord+K) ! If Hinv=.False., then we need to invert Hess
436 1 pfleura2
      END DO
437 1 pfleura2
      Geom_new(J) = Geom_new(J) - (ci(I)*tmp)
438 1 pfleura2
     END DO
439 1 pfleura2
    END DO
440 1 pfleura2
   !*******************************************************************************************************************
441 1 pfleura2
442 1 pfleura2
      IF (PRINT)  WRITE(*,'(/,''       END GEDIIS  '',/)')
443 1 pfleura2
444 1 pfleura2
      END SUBROUTINE Step_GEDIIS