Statistiques
| Révision :

root / src / Step_GEDIIS_All.f90 @ 2

Historique | Voir | Annoter | Télécharger (21,64 ko)

1 1 pfleura2
     ! Geom = input parameter vector (Geometry), Grad = input gradient vector, HEAT is Energy(Geom)
2 1 pfleura2
      SUBROUTINE Step_GEDIIS_All(NGeomF,IGeom,Step,Geom,Grad,HEAT,Hess,NCoord,allocation_flag,Tangent)
3 1 pfleura2
      !SUBROUTINE Step_GEDIIS(Geom_new,Geom,Grad,HEAT,Hess,NCoord,FRST)
4 1 pfleura2
    use Io_module
5 1 pfleura2
    use Path_module, only : Nom, Atome, OrderInv, indzmat, Pi, Nat, Vfree
6 1 pfleura2
      IMPLICIT NONE
7 1 pfleura2
8 1 pfleura2
      INTEGER(KINT) :: NGeomF,IGeom
9 1 pfleura2
      INTEGER(KINT), INTENT(IN) :: NCoord
10 2 pfleura2
      REAL(KREAL) :: Geom(NCoord), Grad(NCoord), Hess(NCoord*NCoord), Step(NCoord)
11 1 pfleura2
    REAL(KREAL) :: HEAT ! HEAT= Energy
12 1 pfleura2
    LOGICAL :: allocation_flag
13 1 pfleura2
    REAL(KREAL), INTENT(INOUT) :: Tangent(Ncoord)
14 1 pfleura2
15 1 pfleura2
      ! MRESET = maximum number of iterations.
16 1 pfleura2
      INTEGER(KINT), PARAMETER :: MRESET=15, M2=(MRESET+1)*(MRESET+1) !M2 = 256
17 1 pfleura2
      REAL(KREAL), ALLOCATABLE, SAVE :: GeomSet(:,:), GradSet(:,:) ! NGeomF,MRESET*NCoord
18 1 pfleura2
      REAL(KREAL), ALLOCATABLE, SAVE :: GSAVE(:,:) !NGeomF,NCoord
19 1 pfleura2
    REAL(KREAL), ALLOCATABLE, SAVE :: ESET(:,:)
20 1 pfleura2
    REAL(KREAL) :: ESET_tmp(MRESET), B(M2), BS(M2), BST(M2), B_tmp(M2) ! M2=256
21 1 pfleura2
      LOGICAL :: DEBUG, PRINT, ci_lt_zero
22 1 pfleura2
      INTEGER(KINT), ALLOCATABLE, SAVE :: MSET(:) ! mth Iteration
23 1 pfleura2
    LOGICAL, ALLOCATABLE, SAVE :: FRST(:)
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, Isch, NFree, Idx
28 2 pfleura2
      REAL(KREAL) :: XMax, XNorm, DET, THRES, tmp, ER_star, ER_star_tmp, Norm
29 1 pfleura2
    REAL(KREAL), PARAMETER :: eps=1e-12
30 1 pfleura2
      REAL(KREAL), PARAMETER :: crit=1e-8
31 1 pfleura2
    REAL(KREAL), ALLOCATABLE :: Tanf(:) ! NCoord
32 1 pfleura2
    REAL(KREAL), ALLOCATABLE :: HFree(:) ! NFree*NFree
33 1 pfleura2
    REAL(KREAL), ALLOCATABLE :: Htmp(:,:) ! NCoord,NFree
34 1 pfleura2
    REAL(KREAL), ALLOCATABLE :: Grad_free(:), Step_free(:) ! NFree
35 1 pfleura2
    REAL(KREAL), ALLOCATABLE :: Geom_free(:), Geom_new_free(:) ! NFree
36 1 pfleura2
    REAL(KREAL), ALLOCATABLE, SAVE :: GeomSet_free(:,:), GradSet_free(:,:)
37 1 pfleura2
38 1 pfleura2
      DEBUG=.TRUE.
39 1 pfleura2
      PRINT=.FALSE.
40 1 pfleura2
41 1 pfleura2
      IF (PRINT)  WRITE(*,'(/,''      BEGIN Step_GEDIIS_ALL   '')')
42 1 pfleura2
43 1 pfleura2
      ! Initialization
44 1 pfleura2
      IF (allocation_flag) THEN
45 1 pfleura2
      ! allocation_flag will be set to False in SPACE_GEDIIS, so no need to modify it here
46 1 pfleura2
         IF (ALLOCATED(GeomSet)) THEN
47 1 pfleura2
            IF (PRINT)  WRITE(*,'(/,''    In allocation_flag, GEDIIS_ALL  Dealloc  '')')
48 1 pfleura2
            DEALLOCATE(GeomSet,GradSet,GSave,GeomSet_free,GradSet_free)
49 1 pfleura2
            RETURN
50 1 pfleura2
         ELSE
51 1 pfleura2
            IF (PRINT)  WRITE(*,'(/,''     In allocation_flag,  GEDIIS_ALL  Alloc  '')')
52 1 pfleura2
            ALLOCATE(GeomSet(NGeomF,MRESET*NCoord),GradSet(NGeomF,MRESET*NCoord),GSAVE(NGeomF,NCoord))
53 1 pfleura2
      ALLOCATE(GeomSet_free(NGeomF,MRESET*NCoord),GradSet_free(NGeomF,MRESET*NCoord))
54 1 pfleura2
      ALLOCATE(MSET(NGeomF),FRST(NGeomF),ESET(NGeomF,MRESET))
55 1 pfleura2
      DO I=1,NGeomF
56 1 pfleura2
         FRST(I) = .TRUE.
57 1 pfleura2
         GeomSet(I,:) = 0.d0
58 1 pfleura2
         GradSet(I,:) = 0.d0
59 1 pfleura2
         GSAVE(I,:)=0.d0
60 1 pfleura2
         GeomSet_free(I,:) = 0.d0
61 1 pfleura2
         GradSet_free(I,:) = 0.d0
62 1 pfleura2
      END DO
63 1 pfleura2
      MSET(:)=0
64 1 pfleura2
         END IF
65 1 pfleura2
     allocation_flag = .FALSE.
66 1 pfleura2
      END IF ! IF (allocation_flag) THEN
67 1 pfleura2
68 1 pfleura2
    ! ADDED FROM HERE:
69 1 pfleura2
    Call FreeMv(NCoord,Vfree) ! VFree(Ncoord,Ncoord), as of now, an Identity matrix.
70 1 pfleura2
      ! we orthogonalize Vfree to the tangent vector of this geom only if Tangent/=0.d0
71 1 pfleura2
      Norm=sqrt(dot_product(Tangent,Tangent))
72 1 pfleura2
      IF (Norm.GT.eps) THEN
73 1 pfleura2
         ALLOCATE(Tanf(NCoord))
74 1 pfleura2
75 1 pfleura2
         ! We normalize Tangent
76 1 pfleura2
         Tangent=Tangent/Norm
77 1 pfleura2
78 1 pfleura2
         ! We convert Tangent into Vfree only displacements. This is useless for now (2007.Apr.23)
79 1 pfleura2
     ! as Vfree=Id matrix but it will be usefull as soon as we introduce constraints.
80 1 pfleura2
         DO I=1,NCoord
81 1 pfleura2
            Tanf(I)=dot_product(reshape(Vfree(:,I),(/NCoord/)),Tangent)
82 1 pfleura2
         END DO
83 1 pfleura2
         Tangent=0.d0
84 1 pfleura2
         DO I=1,NCoord
85 1 pfleura2
            Tangent=Tangent+Tanf(I)*Vfree(:,I)
86 1 pfleura2
         END DO
87 1 pfleura2
         ! first we subtract Tangent from vfree
88 1 pfleura2
         DO I=1,NCoord
89 1 pfleura2
            Norm=dot_product(reshape(vfree(:,I),(/NCoord/)),Tangent)
90 1 pfleura2
            Vfree(:,I)=Vfree(:,I)-Norm*Tangent
91 1 pfleura2
     END DO
92 1 pfleura2
93 1 pfleura2
         Idx=0.
94 1 pfleura2
         ! Schmidt orthogonalization of the Vfree vectors
95 1 pfleura2
         DO I=1,NCoord
96 1 pfleura2
            ! We subtract the first vectors, we do it twice as the Schmidt procedure is not numerically stable.
97 1 pfleura2
            DO Isch=1,2
98 1 pfleura2
               DO J=1,Idx
99 1 pfleura2
                  Norm=dot_product(reshape(Vfree(:,I),(/NCoord/)),reshape(Vfree(:,J),(/NCoord/)))
100 1 pfleura2
                  Vfree(:,I)=Vfree(:,I)-Norm*Vfree(:,J)
101 1 pfleura2
               END DO
102 1 pfleura2
            END DO
103 1 pfleura2
            Norm=dot_product(reshape(Vfree(:,I),(/NCoord/)),reshape(Vfree(:,I),(/NCoord/)))
104 1 pfleura2
            IF (Norm.GE.crit) THEN
105 1 pfleura2
               Idx=Idx+1
106 1 pfleura2
               Vfree(:,Idx)=Vfree(:,I)/sqrt(Norm)
107 1 pfleura2
            END IF
108 1 pfleura2
         END DO
109 1 pfleura2
110 1 pfleura2
         IF (Idx/= NCoord-1) THEN
111 1 pfleura2
            WRITE(*,*) "Pb in orthogonalizing Vfree to tangent for geom",IGeom
112 1 pfleura2
            WRITE(IOOut,*) "Pb in orthogonalizing Vfree to tangent for geom",IGeom
113 1 pfleura2
            STOP
114 1 pfleura2
         END IF
115 1 pfleura2
116 1 pfleura2
         DEALLOCATE(Tanf)
117 1 pfleura2
         NFree=Idx
118 1 pfleura2
      ELSE ! Tangent =0, matches IF (Norm.GT.eps) THEN
119 1 pfleura2
         if (debug) WRITE(*,*) "Tangent=0, using full displacement"
120 1 pfleura2
         NFree=NCoord
121 1 pfleura2
      END IF !IF (Norm.GT.eps) THEN
122 1 pfleura2
123 1 pfleura2
      if (debug) WRITE(*,*) 'DBG Step_GEDIIS_All, IGeom, NFree=', IGeom, NFree
124 1 pfleura2
125 1 pfleura2
      ! We now calculate the new step
126 1 pfleura2
      ! we project the hessian onto the free vectors
127 1 pfleura2
      ALLOCATE(HFree(NFree*NFree),Htmp(NCoord,NFree),Grad_free(NFree))
128 1 pfleura2
    ALLOCATE(Geom_free(NFree),Step_free(NFree),Geom_new_free(NFree))
129 1 pfleura2
      DO J=1,NFree
130 1 pfleura2
        DO I=1,NCoord
131 1 pfleura2
           Htmp(I,J)=0.d0
132 1 pfleura2
           DO K=1,NCoord
133 1 pfleura2
              Htmp(I,J)=Htmp(I,J)+Hess(((I-1)*NCoord)+K)*Vfree(K,J)
134 1 pfleura2
           END DO
135 1 pfleura2
        END DO
136 1 pfleura2
      END DO
137 1 pfleura2
      DO J=1,NFree
138 1 pfleura2
        DO I=1,NFree
139 1 pfleura2
       HFree(I+((J-1)*NFree))=0.d0
140 1 pfleura2
           DO K=1,NCoord
141 1 pfleura2
        HFree(I+((J-1)*NFree))=HFree(I+((J-1)*NFree))+Vfree(K,I)*Htmp(K,J)
142 1 pfleura2
           END DO
143 1 pfleura2
        END DO
144 1 pfleura2
      END DO
145 1 pfleura2
146 1 pfleura2
      DO I=1,NFree
147 1 pfleura2
         Grad_free(I)=dot_product(reshape(Vfree(:,I),(/NCoord/)),Grad)
148 1 pfleura2
     Geom_free(I)=dot_product(reshape(Vfree(:,I),(/NCoord/)),Geom)
149 1 pfleura2
      END DO
150 1 pfleura2
      !ADDED ENDS HERE.***********************************************
151 1 pfleura2
152 1 pfleura2
      ! SPACE_GEDIIS SIMPLY LOADS THE CURRENT VALUES OF Geom AND Grad INTO THE ARRAYS GeomSet
153 1 pfleura2
      ! AND GradSet, MSET is set to zero and then 1 in SPACE_GEDIIS_All at first iteration.
154 1 pfleura2
      CALL SPACE_GEDIIS_All(NGeomF,IGeom,MRESET,MSET,Geom,Grad,HEAT,NCoord,GeomSet,GradSet,ESET,FRST)
155 1 pfleura2
156 1 pfleura2
      IF (PRINT)  WRITE(*,'(/,''       GEDIIS after SPACE_GEDIIS_ALL   '')')
157 1 pfleura2
158 1 pfleura2
    DO J=1,MSet(IGeom)
159 1 pfleura2
         DO K=1,NFree
160 1 pfleura2
            GradSet_free(IGeom,((J-1)*NFree)+K)=dot_product(reshape(Vfree(:,K),(/NCoord/)),&
161 1 pfleura2
               GradSet(IGeom,((J-1)*NCoord)+1:((J-1)*NCoord)+NCoord))
162 1 pfleura2
        GeomSet_free(IGeom,((J-1)*NFree)+K)=dot_product(reshape(Vfree(:,K),(/NCoord/)),&
163 1 pfleura2
                GeomSet(IGeom,((J-1)*NCoord)+1:((J-1)*NCoord)+NCoord))
164 1 pfleura2
         END DO
165 1 pfleura2
    END DO
166 1 pfleura2
167 1 pfleura2
      ! INITIALIZE SOME VARIABLES AND CONSTANTS:
168 1 pfleura2
      NGEDIIS = MSET(IGeom) !MSET=mth iteration
169 1 pfleura2
      MPLUS = MSET(IGeom) + 1
170 1 pfleura2
      MM = MPLUS * MPLUS
171 1 pfleura2
172 1 pfleura2
      ! CONSTRUCT THE GEDIIS MATRIX:
173 1 pfleura2
      ! B_ij calculations from <B_ij=(g_i-g_j)(R_i-R_j)>
174 1 pfleura2
      JJ=0
175 1 pfleura2
      INV=-NFree
176 1 pfleura2
      DO I=1,MSET(IGeom)
177 1 pfleura2
         INV=INV+NFree
178 1 pfleura2
         JNV=-NFree
179 1 pfleura2
         DO J=1,MSET(IGeom)
180 1 pfleura2
            JNV=JNV+NFree
181 1 pfleura2
            JJ = JJ + 1
182 1 pfleura2
            B(JJ)=0.D0
183 1 pfleura2
      DO K=1, NFree
184 1 pfleura2
         B(JJ) = B(JJ) + (((GradSet_free(IGeom,INV+K)-GradSet_free(IGeom,JNV+K))* &
185 1 pfleura2
                 (GeomSet_free(IGeom,INV+K)-GeomSet_free(IGeom,JNV+K)))/2.D0)
186 1 pfleura2
      END DO
187 1 pfleura2
         END DO
188 1 pfleura2
      END DO
189 1 pfleura2
190 1 pfleura2
     ! The following shifting is required to correct indices of B_ij elements in the GEDIIS matrix.
191 1 pfleura2
   ! The correction is needed because the last coloumn of the matrix contains all 1 and one zero.
192 1 pfleura2
      DO I=MSET(IGeom)-1,1,-1
193 1 pfleura2
         DO J=MSET(IGeom),1,-1
194 1 pfleura2
            B(I*MSET(IGeom)+J+I) = B(I*MSET(IGeom)+J)
195 1 pfleura2
         END DO
196 1 pfleura2
    END DO
197 1 pfleura2
198 1 pfleura2
      ! For the last row and last column of GEDIIS matrix:
199 1 pfleura2
      DO I=1,MPLUS
200 1 pfleura2
         B(MPLUS*I) = 1.D0
201 1 pfleura2
         B(MPLUS*MSET(IGeom)+I) = 1.D0
202 1 pfleura2
      END DO
203 1 pfleura2
      B(MM) = 0.D0
204 1 pfleura2
205 1 pfleura2
    DO I=1, MPLUS
206 1 pfleura2
       !WRITE(*,'(10(1X,F20.4))') B((I-1)*MPLUS+1:I*(MPLUS))
207 1 pfleura2
    END DO
208 1 pfleura2
209 1 pfleura2
      ! ELIMINATE ERROR VECTORS WITH THE LARGEST NORM:
210 1 pfleura2
   80 CONTINUE
211 1 pfleura2
      DO I=1,MM !MM = (MSET(IGeom)+1) * (MSET(IGeom)+1)
212 1 pfleura2
         BS(I) = B(I) !just a copy of the original B (GEDIIS) matrix
213 1 pfleura2
      END DO
214 1 pfleura2
215 1 pfleura2
      IF (NGEDIIS .NE. MSET(IGeom)) THEN
216 1 pfleura2
        DO II=1,MSET(IGeom)-NGEDIIS
217 1 pfleura2
           XMAX = -1.D10
218 1 pfleura2
           ITERA = 0
219 1 pfleura2
           DO I=1,MSET(IGeom)
220 1 pfleura2
              XNORM = 0.D0
221 1 pfleura2
              INV = (I-1) * MPLUS
222 1 pfleura2
              DO J=1,MSET(IGeom)
223 1 pfleura2
                 XNORM = XNORM + ABS(B(INV + J))
224 1 pfleura2
              END DO
225 1 pfleura2
              IF (XMAX.LT.XNORM .AND. XNORM.NE.1.0D0) THEN
226 1 pfleura2
                 XMAX = XNORM
227 1 pfleura2
                 ITERA = I
228 1 pfleura2
                 IONE = INV + I
229 1 pfleura2
              ENDIF
230 1 pfleura2
           END DO
231 1 pfleura2
232 1 pfleura2
           DO I=1,MPLUS
233 1 pfleura2
              INV = (I-1) * MPLUS
234 1 pfleura2
              DO J=1,MPLUS
235 1 pfleura2
                 JNV = (J-1) * MPLUS
236 1 pfleura2
                 IF (J.EQ.ITERA) B(INV + J) = 0.D0
237 1 pfleura2
                 B(JNV + I) = B(INV + J)
238 1 pfleura2
              END DO
239 1 pfleura2
       END DO
240 1 pfleura2
           B(IONE) = 1.0D0
241 1 pfleura2
        END DO
242 1 pfleura2
    END IF ! matches IF (NGEDIIS .NE. MSET(IGeom)) THEN
243 1 pfleura2
244 1 pfleura2
      ! SCALE GEDIIS MATRIX BEFORE INVERSION:
245 1 pfleura2
      DO I=1,MPLUS
246 1 pfleura2
         II = MPLUS * (I-1) + I ! B(II)=diagonal elements of B matrix
247 1 pfleura2
         GSAVE(IGeom,I) = 1.D0 / DSQRT(1.D-20+DABS(B(II)))
248 1 pfleura2
     !Print *, 'GSAVE(',IGeom,',',I,')=', GSAVE(IGeom,I)
249 1 pfleura2
      END DO
250 1 pfleura2
      GSAVE(IGeom,MPLUS) = 1.D0
251 1 pfleura2
      DO I=1,MPLUS
252 1 pfleura2
         DO J=1,MPLUS
253 1 pfleura2
            IJ = MPLUS * (I-1) + J
254 1 pfleura2
            B(IJ) = B(IJ) * GSAVE(IGeom,I) * GSAVE(IGeom,J)
255 1 pfleura2
         END DO
256 1 pfleura2
      END DO
257 1 pfleura2
258 1 pfleura2
     ! INVERT THE GEDIIS MATRIX B:
259 1 pfleura2
    DO I=1, MPLUS
260 1 pfleura2
       !WRITE(*,'(10(1X,F20.4))') B((I-1)*MPLUS+1:I*(MPLUS))
261 1 pfleura2
    END DO
262 1 pfleura2
263 1 pfleura2
      CALL MINV(B,MPLUS,DET) ! matrix inversion.
264 1 pfleura2
265 1 pfleura2
    DO I=1, MPLUS
266 1 pfleura2
       !WRITE(*,'(10(1X,F20.16))') B((I-1)*MPLUS+1:I*(MPLUS))
267 1 pfleura2
    END DO
268 1 pfleura2
269 1 pfleura2
      DO I=1,MPLUS
270 1 pfleura2
         DO J=1,MPLUS
271 1 pfleura2
            IJ = MPLUS * (I-1) + J
272 1 pfleura2
            B(IJ) = B(IJ) * GSAVE(IGeom,I) * GSAVE(IGeom,J)
273 1 pfleura2
         END DO
274 1 pfleura2
      END DO
275 1 pfleura2
276 1 pfleura2
      ! COMPUTE THE NEW INTERPOLATED PARAMETER VECTOR (Geometry):
277 1 pfleura2
    ci=0.d0
278 1 pfleura2
    ci_tmp=0.d0
279 1 pfleura2
280 1 pfleura2
    ci_lt_zero= .FALSE.
281 1 pfleura2
    DO I=1, MSET(IGeom)
282 1 pfleura2
     DO J=1, MSET(IGeom) ! B matrix is read column-wise
283 1 pfleura2
        ci(I)=ci(I)+B((J-1)*(MPLUS)+I)*ESET(IGeom,J) !ESET is energy set.
284 1 pfleura2
     END DO
285 1 pfleura2
     ci(I)=ci(I)+B((MPLUS-1)*(MPLUS)+I)
286 1 pfleura2
     !Print *, 'NO ci < 0 yet, c(',I,')=', ci(I)
287 1 pfleura2
     IF((ci(I) .LT. 0.0D0) .OR. (ci(I) .GT. 1.0D0)) THEN
288 1 pfleura2
       ci_lt_zero=.TRUE.
289 1 pfleura2
       EXIT
290 1 pfleura2
     END IF
291 1 pfleura2
      END DO !matches DO I=1, MSET(IGeom)
292 1 pfleura2
293 1 pfleura2
    IF (ci_lt_zero) Then
294 1 pfleura2
       cis_zero = 0
295 1 pfleura2
         ER_star = 0.D0
296 1 pfleura2
     ER_star_tmp = 1e32
297 1 pfleura2
298 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.
299 1 pfleura2
         JJ=0
300 1 pfleura2
         INV=-NFree
301 1 pfleura2
         DO IX=1,MSET(IGeom)
302 1 pfleura2
            INV=INV+NFree
303 1 pfleura2
            JNV=-NFree
304 1 pfleura2
            DO JX=1,MSET(IGeom)
305 1 pfleura2
               JNV=JNV+NFree
306 1 pfleura2
               JJ = JJ + 1
307 1 pfleura2
               BST(JJ)=0.D0
308 1 pfleura2
           DO KX=1, NFree
309 1 pfleura2
              BST(JJ) = BST(JJ) + (((GradSet_free(IGeom,INV+KX)-GradSet_free(IGeom,JNV+KX))* &
310 1 pfleura2
                    (GeomSet_free(IGeom,INV+KX)-GeomSet_free(IGeom,JNV+KX)))/2.D0)
311 1 pfleura2
           END DO
312 1 pfleura2
            END DO
313 1 pfleura2
       END DO
314 1 pfleura2
315 1 pfleura2
     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
316 1 pfleura2
         !Print *, 'Entering into DO I=1, (2**MSET(IGeom))-2  loop, MSET(IGeom)=', MSET(IGeom), ', I=', I
317 1 pfleura2
         ci(:)=1.D0
318 1 pfleura2
         ! find out which cis are zero in I:
319 1 pfleura2
       DO IX=1, MSET(IGeom)
320 1 pfleura2
          JJ=IAND(I, 2**(IX-1))
321 1 pfleura2
        IF(JJ .EQ. 0) Then
322 1 pfleura2
          ci(IX)=0.D0
323 1 pfleura2
          END IF
324 1 pfleura2
       END DO
325 1 pfleura2
326 1 pfleura2
       ci_lt_zero = .FALSE.
327 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.
328 1 pfleura2
       DO IX=1, MSET(IGeom)*MSET(IGeom)
329 1 pfleura2
                B(IX) = BST(IX) !just a copy of the original B (GEDIIS) matrix
330 1 pfleura2
             END DO
331 1 pfleura2
332 1 pfleura2
             ! Removal of KXth row and KXth column in order to accomodate cI to be zero:
333 1 pfleura2
       current_size_B_mat=MSET(IGeom)
334 1 pfleura2
       cis_zero = 0
335 1 pfleura2
       ! The bits of I (index of the upper loop 'DO I=1, (2**MSET(IGeom))-2'), gives which cis are zero.
336 1 pfleura2
       DO KX=1, MSET(IGeom) ! searching for each bit of I (index of the upper loop 'DO I=1, (2**MSET(IGeom))-2')
337 1 pfleura2
          IF (ci(KX) .EQ. 0.D0) Then !remove KXth row and KXth column
338 1 pfleura2
           cis_zero = cis_zero + 1
339 1 pfleura2
340 1 pfleura2
             ! First row removal: (B matrix is read column-wise)
341 1 pfleura2
             JJ=0
342 1 pfleura2
                   DO IX=1,current_size_B_mat ! columns reading
343 1 pfleura2
                      DO JX=1,current_size_B_mat ! rows reading
344 1 pfleura2
                 IF (JX .NE. KX) Then
345 1 pfleura2
                     JJ = JJ + 1
346 1 pfleura2
                     B_tmp(JJ) = B((IX-1)*current_size_B_mat+JX)
347 1 pfleura2
                 END IF
348 1 pfleura2
              END DO
349 1 pfleura2
             END DO
350 1 pfleura2
351 1 pfleura2
             DO IX=1,current_size_B_mat*(current_size_B_mat-1)
352 1 pfleura2
                B(IX) = B_tmp(IX)
353 1 pfleura2
             END DO
354 1 pfleura2
355 1 pfleura2
             ! Now column removal:
356 1 pfleura2
             JJ=0
357 1 pfleura2
                   DO IX=1,KX-1 ! columns reading
358 1 pfleura2
                      DO JX=1,current_size_B_mat-1 ! rows reading
359 1 pfleura2
                 JJ = JJ + 1
360 1 pfleura2
                 B_tmp(JJ) = B(JJ)
361 1 pfleura2
              END DO
362 1 pfleura2
             END DO
363 1 pfleura2
364 1 pfleura2
                   DO IX=KX+1,current_size_B_mat
365 1 pfleura2
                      DO JX=1,current_size_B_mat-1
366 1 pfleura2
                 JJ = JJ + 1
367 1 pfleura2
                    B_tmp(JJ) = B(JJ+current_size_B_mat-1)
368 1 pfleura2
              END DO
369 1 pfleura2
             END DO
370 1 pfleura2
371 1 pfleura2
             DO IX=1,(current_size_B_mat-1)*(current_size_B_mat-1)
372 1 pfleura2
                B(IX) = B_tmp(IX)
373 1 pfleura2
             END DO
374 1 pfleura2
           current_size_B_mat = current_size_B_mat - 1
375 1 pfleura2
        END IF ! matches IF (ci(KX) .EQ. 0.D0) Then !remove
376 1 pfleura2
           END DO ! matches DO KX=1, MSET(IGeom)
377 1 pfleura2
378 1 pfleura2
       ! The following shifting is required to correct indices of B_ij elements in the GEDIIS matrix.
379 1 pfleura2
       ! The correction is needed because the last coloumn and row of the matrix contains all 1 and one zero.
380 1 pfleura2
       DO IX=MSET(IGeom)-cis_zero-1,1,-1
381 1 pfleura2
        DO JX=MSET(IGeom)-cis_zero,1,-1
382 1 pfleura2
           B(IX*(MSET(IGeom)-cis_zero)+JX+IX) = B(IX*(MSET(IGeom)-cis_zero)+JX)
383 1 pfleura2
        END DO
384 1 pfleura2
       END DO
385 1 pfleura2
386 1 pfleura2
       ! for last row and last column of GEDIIS matrix
387 1 pfleura2
       DO IX=1,MPLUS-cis_zero
388 1 pfleura2
        B((MPLUS-cis_zero)*IX) = 1.D0
389 1 pfleura2
        B((MPLUS-cis_zero)*(MSET(IGeom)-cis_zero)+IX) = 1.D0
390 1 pfleura2
       END DO
391 1 pfleura2
       B((MPLUS-cis_zero) * (MPLUS-cis_zero)) = 0.D0
392 1 pfleura2
393 1 pfleura2
           DO IX=1, MPLUS
394 1 pfleura2
              !WRITE(*,'(10(1X,F20.4))') B((IX-1)*MPLUS+1:IX*(MPLUS))
395 1 pfleura2
           END DO
396 1 pfleura2
397 1 pfleura2
       ! ELIMINATE ERROR VECTORS WITH THE LARGEST NORM:
398 1 pfleura2
             IF (NGEDIIS .NE. MSET(IGeom)) THEN
399 1 pfleura2
          JX = min(MSET(IGeom)-NGEDIIS,MSET(IGeom)-cis_zero-1)
400 1 pfleura2
                DO II=1,JX
401 1 pfleura2
                   XMAX = -1.D10
402 1 pfleura2
                   ITERA = 0
403 1 pfleura2
                   DO IX=1,MSET(IGeom)-cis_zero
404 1 pfleura2
                      XNORM = 0.D0
405 1 pfleura2
                      INV = (IX-1) * (MPLUS-cis_zero)
406 1 pfleura2
                      DO J=1,MSET(IGeom)-cis_zero
407 1 pfleura2
                         XNORM = XNORM + ABS(B(INV + J))
408 1 pfleura2
                      END DO
409 1 pfleura2
                      IF (XMAX.LT.XNORM .AND. XNORM.NE.1.0D0) THEN
410 1 pfleura2
                         XMAX = XNORM
411 1 pfleura2
                         ITERA = IX
412 1 pfleura2
                         IONE = INV + IX
413 1 pfleura2
                      ENDIF
414 1 pfleura2
           END DO
415 1 pfleura2
416 1 pfleura2
                   DO IX=1,MPLUS-cis_zero
417 1 pfleura2
                      INV = (IX-1) * (MPLUS-cis_zero)
418 1 pfleura2
                      DO J=1,MPLUS-cis_zero
419 1 pfleura2
                         JNV = (J-1) * (MPLUS-cis_zero)
420 1 pfleura2
                         IF (J.EQ.ITERA) B(INV + J) = 0.D0
421 1 pfleura2
                         B(JNV + IX) = B(INV + J)
422 1 pfleura2
            END DO
423 1 pfleura2
               END DO
424 1 pfleura2
                   B(IONE) = 1.0D0
425 1 pfleura2
          END DO
426 1 pfleura2
           END IF ! matches IF (NGEDIIS .NE. MSET(IGeom)) THEN
427 1 pfleura2
428 1 pfleura2
       ! SCALE GEDIIS MATRIX BEFORE INVERSION:
429 1 pfleura2
       DO IX=1,MPLUS-cis_zero
430 1 pfleura2
        II = (MPLUS-cis_zero) * (IX-1) + IX ! B(II)=diagonal elements of B matrix
431 1 pfleura2
        GSAVE(IGeom,IX) = 1.D0 / DSQRT(1.D-20+DABS(B(II)))
432 1 pfleura2
       END DO
433 1 pfleura2
       GSAVE(IGeom,MPLUS-cis_zero) = 1.D0
434 1 pfleura2
       DO IX=1,MPLUS-cis_zero
435 1 pfleura2
        DO JX=1,MPLUS-cis_zero
436 1 pfleura2
           IJ = (MPLUS-cis_zero) * (IX-1) + JX
437 1 pfleura2
           B(IJ) = B(IJ) * GSAVE(IGeom,IX) * GSAVE(IGeom,JX)
438 1 pfleura2
        END DO
439 1 pfleura2
       END DO
440 1 pfleura2
441 1 pfleura2
       ! INVERT THE GEDIIS MATRIX B:
442 1 pfleura2
       CALL MINV(B,MPLUS-cis_zero,DET) ! matrix inversion.
443 1 pfleura2
444 1 pfleura2
       DO IX=1,MPLUS-cis_zero
445 1 pfleura2
        DO JX=1,MPLUS-cis_zero
446 1 pfleura2
           IJ = (MPLUS-cis_zero) * (IX-1) + JX
447 1 pfleura2
           B(IJ) = B(IJ) * GSAVE(IGeom,IX) * GSAVE(IGeom,JX)
448 1 pfleura2
        END DO
449 1 pfleura2
       END DO
450 1 pfleura2
451 1 pfleura2
           DO IX=1, MPLUS
452 1 pfleura2
              !WRITE(*,'(10(1X,F20.4))') B((IX-1)*MPLUS+1:IX*(MPLUS))
453 1 pfleura2
           END DO
454 1 pfleura2
455 1 pfleura2
             ! ESET is rearranged to handle zero cis and stored in ESET_tmp:
456 1 pfleura2
       JJ=0
457 1 pfleura2
       DO IX=1, MSET(IGeom)
458 1 pfleura2
        IF (ci(IX) .NE. 0) Then
459 1 pfleura2
           JJ=JJ+1
460 1 pfleura2
           ESET_tmp(JJ) = ESET(IGeom,IX)
461 1 pfleura2
        END IF
462 1 pfleura2
       END DO
463 1 pfleura2
464 1 pfleura2
       ! DETERMINATION OF nonzero cis:
465 1 pfleura2
       MyPointer=1
466 1 pfleura2
            DO IX=1, MSET(IGeom)-cis_zero
467 1 pfleura2
          tmp = 0.D0
468 1 pfleura2
            DO J=1, MSET(IGeom)-cis_zero ! B matrix is read column-wise
469 1 pfleura2
           tmp=tmp+B((J-1)*(MPLUS-cis_zero)+IX)*ESET_tmp(J)
470 1 pfleura2
        END DO
471 1 pfleura2
            tmp=tmp+B((MPLUS-cis_zero-1)*(MPLUS-cis_zero)+IX)
472 1 pfleura2
            IF((tmp .LT. 0.0D0) .OR. (tmp .GT. 1.0D0)) THEN
473 1 pfleura2
               ci_lt_zero=.TRUE.
474 1 pfleura2
               EXIT
475 1 pfleura2
        ELSE
476 1 pfleura2
           DO JX=MyPointer,MSET(IGeom)
477 1 pfleura2
              IF (ci(JX) .NE. 0) Then
478 1 pfleura2
                 ci(JX) = tmp
479 1 pfleura2
               MyPointer=JX+1
480 1 pfleura2
               EXIT
481 1 pfleura2
            END IF
482 1 pfleura2
           END DO
483 1 pfleura2
            END IF
484 1 pfleura2
             END DO !matches DO I=1, MSET(IGeom)-cis_zero
485 1 pfleura2
           !Print *, 'Local set of cis, first 10:, MSET(IGeom)=', MSET(IGeom), ', I of (2**MSET(IGeom))-2=', I
486 1 pfleura2
       !WRITE(*,'(10(1X,F20.4))') ci(1:MSET(IGeom))
487 1 pfleura2
           !Print *, 'Local set of cis ends:****************************************'
488 1 pfleura2
489 1 pfleura2
             ! new set of cis determined based on the lower energy (ER_star):
490 1 pfleura2
       IF (.NOT. ci_lt_zero) Then
491 1 pfleura2
                Call Energy_GEDIIS(MRESET,MSET(IGeom),ci,GeomSet_free(IGeom,:),GradSet_free(IGeom,:),ESET(IGeom,:),NFree,ER_star)
492 1 pfleura2
        IF (ER_star .LT. ER_star_tmp) Then
493 1 pfleura2
           ci_tmp=ci
494 1 pfleura2
           ER_star_tmp = ER_star
495 1 pfleura2
        END IF
496 1 pfleura2
             END IF ! matches IF (.NOT. ci_lt_zero) Then
497 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
498 1 pfleura2
      ci = ci_tmp
499 1 pfleura2
    END IF! matches IF (ci_lt_zero) Then
500 1 pfleura2
501 1 pfleura2
    Print *, 'Final set of cis, first 10:***********************************'
502 1 pfleura2
    WRITE(*,'(10(1X,F20.4))') ci(1:MSET(IGeom))
503 1 pfleura2
    Print *, 'Final set of cis ends:****************************************'
504 1 pfleura2
    Geom_new_free(:) = 0.D0
505 1 pfleura2
    DO I=1, MSET(IGeom)
506 1 pfleura2
         Geom_new_free(:) = Geom_new_free(:) + (ci(I)*GeomSet_free(IGeom,(I-1)*NFree+1:I*NFree)) !MPLUS=MSET(IGeom)+1
507 1 pfleura2
     ! R_(N+1)=R*+DeltaR:
508 1 pfleura2
     DO J=1, NFree
509 1 pfleura2
      tmp=0.D0
510 1 pfleura2
      DO K=1,NFree
511 1 pfleura2
         ! this can be commented:
512 1 pfleura2
         !tmp=tmp+HFree((J-1)*NFree+K)*GradSet_free(IGeom,(I-1)*NFree+K) ! If Hinv=.False., then we need to invert Hess
513 1 pfleura2
      END DO
514 1 pfleura2
      Geom_new_free(J) = Geom_new_free(J) - (ci(I)*tmp)
515 1 pfleura2
     END DO
516 1 pfleura2
    END DO
517 1 pfleura2
518 1 pfleura2
    Step_free(:) =  Geom_new_free(:) - Geom_free(:)
519 1 pfleura2
520 1 pfleura2
      XNORM = SQRT(DOT_PRODUCT(Step_free,Step_free))
521 1 pfleura2
      IF (PRINT) THEN
522 1 pfleura2
         WRITE (6,'(/10X,''DEVIATION IN X '',F10.4,8X,''DETERMINANT '',G9.3)') XNORM, DET
523 1 pfleura2
         !WRITE(*,'(10X,''GEDIIS COEFFICIENTS'')')
524 1 pfleura2
         !WRITE(*,'(10X,5F12.5)') (B(MPLUS*MSET(IGeom)+I),I=1,MSET(IGeom))
525 1 pfleura2
      ENDIF
526 1 pfleura2
527 1 pfleura2
      ! THE FOLLOWING TOLERENCES FOR XNORM AND DET ARE SOMEWHAT ARBITRARY!
528 1 pfleura2
      THRES = MAX(10.D0**(-NFree), 1.D-25)
529 1 pfleura2
      IF (XNORM.GT.2.D0 .OR. DABS(DET) .LT. THRES) THEN
530 1 pfleura2
         IF (PRINT)THEN
531 1 pfleura2
            WRITE(*,*) "THE GEDIIS MATRIX IS ILL CONDITIONED"
532 1 pfleura2
            WRITE(*,*) " - PROBABLY, VECTORS ARE LINEARLY DEPENDENT - "
533 1 pfleura2
            WRITE(*,*) "THE GEDIIS STEP WILL BE REPEATED WITH A SMALLER SPACE"
534 1 pfleura2
         END IF
535 1 pfleura2
         DO K=1,MM
536 1 pfleura2
      B(K) = BS(K) ! why this is reverted? Because "IF (NGEDIIS .GT. 0) GO TO 80", see below
537 1 pfleura2
         END DO
538 1 pfleura2
         NGEDIIS = NGEDIIS - 1
539 1 pfleura2
         IF (NGEDIIS .GT. 0) GO TO 80
540 1 pfleura2
         IF (PRINT) WRITE(*,'(10X,''NEWTON-RAPHSON STEP TAKEN'')')
541 1 pfleura2
         Geom_new_free(:) = Geom_free(:) ! Geom_new is set to original Geom, thus Step = Geom(:) - Geom_new(:)=zero, the whole
542 1 pfleura2
                           ! new update to Geom_new is discarded, since XNORM.GT.2.D0 .OR. DABS(DET) .LT. THRES
543 1 pfleura2
      END IF ! matches IF (XNORM.GT.2.D0 .OR. DABS(DET).LT. THRES) THEN
544 1 pfleura2
545 1 pfleura2
    !******************************************************************************************************************
546 1 pfleura2
    Geom_new_free(:) = 0.D0
547 1 pfleura2
    DO I=1, MSET(IGeom)
548 1 pfleura2
         Geom_new_free(:) = Geom_new_free(:) + (ci(I)*GeomSet_free(IGeom,(I-1)*NFree+1:I*NFree)) !MPLUS=MSET(IGeom)+1
549 1 pfleura2
     ! R_(N+1)=R*+DeltaR:
550 1 pfleura2
     DO J=1, NFree
551 1 pfleura2
      tmp=0.D0
552 1 pfleura2
      DO K=1,NFree
553 1 pfleura2
         tmp=tmp+HFree((J-1)*NFree+K)*GradSet_free(IGeom,(I-1)*NFree+K) ! If Hinv=.False., then we need to invert Hess
554 1 pfleura2
      END DO
555 1 pfleura2
      Geom_new_free(J) = Geom_new_free(J) - (ci(I)*tmp)
556 1 pfleura2
     END DO
557 1 pfleura2
    END DO
558 1 pfleura2
559 1 pfleura2
    Step_free(:) =  Geom_new_free(:) - Geom_free(:)
560 1 pfleura2
   !******************************************************************************************************************
561 1 pfleura2
    Step = 0.d0
562 1 pfleura2
      DO I=1,NFree
563 1 pfleura2
     Step = Step + Step_free(I)*Vfree(:,I)
564 1 pfleura2
      END DO
565 1 pfleura2
566 1 pfleura2
      DEALLOCATE(Hfree,Htmp,Grad_free,Step_free,Geom_free,Geom_new_free)
567 1 pfleura2
568 1 pfleura2
      IF (PRINT)  WRITE(*,'(/,''       END Step_GEDIIS_ALL   '',/)')
569 1 pfleura2
570 1 pfleura2
      END SUBROUTINE Step_GEDIIS_All