Statistiques
| Révision :

root / src / ConvertBakerInternal_cart.f90 @ 3

Historique | Voir | Annoter | Télécharger (24,03 ko)

1 1 pfleura2
SUBROUTINE ConvertBakerInternal_cart(IntCoord_k,IntCoord,x_k,y_k,z_k,x,y,z,XPrim,XPrimRef)
2 1 pfleura2
3 1 pfleura2
!================================================================
4 1 pfleura2
! Converts the positions in Baker Coordinates to cartesian coordinates
5 1 pfleura2
!================================================================
6 1 pfleura2
!
7 1 pfleura2
! Input:
8 1 pfleura2
!    Incoord_k(NCoord) REAL: Starting internal coordinates
9 1 pfleura2
!    IntCoord(NCoord) REAL: Coordinate to convert to cartesian coordinates
10 1 pfleura2
!    x_k,y_k,z_k(Nat) REAL: Starting cartesian geometry
11 1 pfleura2
!    XPrimRef(NPrim) REAL: Reference values for Primitives coordinates (mainly for dihedral)
12 1 pfleura2
!
13 1 pfleura2
! Output:
14 1 pfleura2
!    x,y,z(Nat) REAL: Cartesian coordinates corresponding to IntCoord
15 1 pfleura2
!    XPrim(NPrim) REAL: Primitives for the cartesian coordinates. (This is a by-product)
16 1 pfleura2
!
17 1 pfleura2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
18 1 pfleura2
19 1 pfleura2
! BTransInv,UMat should be passed as arguments of this subroutine.
20 1 pfleura2
21 1 pfleura2
22 1 pfleura2
  ! IdxGeom: geometry index.
23 1 pfleura2
  ! Internal coordinates IntCoord(NCoord) is to be converted
24 1 pfleura2
  ! into Cartesian coordinates x(Nat),y(Nat),z(Nat).
25 1 pfleura2
26 1 pfleura2
  use Path_module, only : Nat,AtName,IntCoordI,XyzGeomI,BMat_BakerT,NPrim,&
27 1 pfleura2
       BTransInv,BBT,BBT_inv,BprimT,a0,ScanCoord,Coordinate,&
28 1 pfleura2
       Xprimitive,Pi,NGeomI,IntCoordF,BTransInv_local,Xprimitive_t,&
29 1 pfleura2
       Symmetry_elimination,NCoord,UMat_local
30 1 pfleura2
  ! UMat_local is allocated in Calc_baker.f90
31 1 pfleura2
  ! XyzGeomI(NGeomI,3,Nat),BMat_BakerT(3*Nat,NCoord),a0=0.529177249d0
32 1 pfleura2
33 1 pfleura2
  Use Io_Module
34 1 pfleura2
  use VarTypes
35 1 pfleura2
  IMPLICIT NONE
36 1 pfleura2
37 1 pfleura2
  REAL(KREAL), INTENT(OUT) :: x(Nat),y(Nat),z(Nat)
38 1 pfleura2
  REAL(KREAL), INTENT(IN) :: x_k(Nat),y_k(Nat),z_k(Nat)
39 1 pfleura2
  REAL(KREAL), INTENT(IN) :: IntCoord(NCoord)
40 1 pfleura2
  REAL(KREAL), INTENT(INOUT) ::  IntCoord_k(NCoord)
41 1 pfleura2
  REAL(KREAL), INTENT(IN) :: XPrimRef(NPrim)
42 1 pfleura2
  REAL(KREAL), INTENT(OUT) :: XPrim(NPrim)
43 1 pfleura2
44 1 pfleura2
45 1 pfleura2
  ! IdxGeom: geometry index.
46 2 pfleura2
  Integer(KINT) :: I, J, Counter
47 1 pfleura2
  REAL(KREAL) :: normDeltaX_int
48 1 pfleura2
49 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: DeltaX_int(:)  !DeltaX_int(NPrim,3*Nat-6), 3*Nat-6??
50 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: DeltaX_cart(:) !DeltaX_cart(3*Nat)
51 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: Geom(:,:) !(3,Nat)
52 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: X_cart_k(:,:) ! (3,Nat)
53 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: GMat(:,:) !(NPrim,NPrim)
54 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: EigVec(:,:), EigVal(:) ! EigVec(NPrim,NPrim)
55 1 pfleura2
  REAL(KREAL), ALLOCATABLE :: UMat_tmp(:,:)
56 1 pfleura2
57 2 pfleura2
  REAL(KREAL) :: Angle, angle_d
58 1 pfleura2
  REAL(KREAL), PARAMETER :: Crit=1e-08
59 1 pfleura2
60 1 pfleura2
  EXTERNAL Angle, angle_d
61 1 pfleura2
  LOGICAL :: debug, FAIL
62 1 pfleura2
63 1 pfleura2
  INTEGER(KINT), PARAMETER :: NbItMax=50
64 1 pfleura2
65 1 pfleura2
66 1 pfleura2
  INTERFACE
67 1 pfleura2
     function valid(string) result (isValid)
68 1 pfleura2
       CHARACTER(*), intent(in) :: string
69 1 pfleura2
       logical                  :: isValid
70 1 pfleura2
     END function VALID
71 1 pfleura2
72 1 pfleura2
73 1 pfleura2
74 1 pfleura2
     SUBROUTINE Calc_Xprim(nat,x,y,z,Coordinate,NPrim,XPrimitive,XPrimRef)
75 1 pfleura2
       !
76 1 pfleura2
       ! This subroutine uses the description of a list of Coordinates
77 1 pfleura2
       ! to compute the values of the coordinates for a given geometry.
78 1 pfleura2
       !
79 1 pfleura2
!!!!!!!!!!
80 1 pfleura2
       ! Input:
81 1 pfleura2
       !   Na: INTEGER, Number of atoms
82 1 pfleura2
       !  x,y,z(Na): REAL, cartesian coordinates of the considered geometry
83 1 pfleura2
       ! Coordinate (Pointer(ListCoord)): description of the wanted coordiantes
84 1 pfleura2
       ! NPrim, INTEGER: Number of coordinates to compute
85 1 pfleura2
       !
86 1 pfleura2
       ! Optional: XPrimRef(NPrim) REAL: array that contains coordinates values for
87 1 pfleura2
       ! a former geometry. Useful for Dihedral angles evolution...
88 1 pfleura2
89 1 pfleura2
!!!!!!!!!!!
90 1 pfleura2
       ! Output:
91 1 pfleura2
       ! XPrimimite(NPrim) REAL: array that will contain the values of the coordinates
92 1 pfleura2
       !
93 1 pfleura2
!!!!!!!!!
94 1 pfleura2
95 1 pfleura2
       Use VarTypes
96 1 pfleura2
       Use Io_module
97 1 pfleura2
       Use Path_module, only : pi
98 1 pfleura2
99 1 pfleura2
       IMPLICIT NONE
100 1 pfleura2
101 1 pfleura2
       Type (ListCoord), POINTER :: Coordinate
102 1 pfleura2
       INTEGER(KINT), INTENT(IN) :: Nat,NPrim
103 1 pfleura2
       REAL(KREAL), INTENT(IN) :: x(Nat), y(Nat), z(Nat)
104 1 pfleura2
       REAL(KREAL), INTENT(IN), OPTIONAL :: XPrimRef(NPrim)
105 1 pfleura2
       REAL(KREAL), INTENT(OUT) :: XPrimitive(NPrim)
106 1 pfleura2
107 1 pfleura2
     END SUBROUTINE CALC_XPRIM
108 1 pfleura2
  END INTERFACE
109 1 pfleura2
110 1 pfleura2
111 1 pfleura2
  debug=valid('ConvertBakerInternal_cart')
112 1 pfleura2
  if (debug) WRITE(*,*) '=======Entering ConvertBakerInternal_cart======='
113 1 pfleura2
114 1 pfleura2
  FAIL = .FALSE.
115 1 pfleura2
116 1 pfleura2
!!!!!!
117 1 pfleura2
  ! Allocation phase
118 1 pfleura2
119 1 pfleura2
  ALLOCATE(DeltaX_int(NCoord),DeltaX_cart(3*Nat))
120 1 pfleura2
  ALLOCATE(UMat_tmp(NPrim,NCoord))
121 1 pfleura2
  ALLOCATE(X_cart_k(3,Nat))
122 1 pfleura2
  ALLOCATE(Geom(3,Nat),BprimT(3*Nat,NPrim))
123 1 pfleura2
  ALLOCATE(BBT(NCoord,NCoord))
124 1 pfleura2
  ALLOCATE(BBT_inv(NCoord,NCoord))
125 1 pfleura2
  ALLOCATE(Gmat(NPrim,NPrim))
126 1 pfleura2
  ALLOCATE(EigVal(NPrim),EigVec(NPrim,NPrim))
127 1 pfleura2
128 1 pfleura2
129 1 pfleura2
  if (debug) THEN
130 1 pfleura2
     WRITE(*,*) "Checking purposes...."
131 1 pfleura2
     WRITE(*,*) "Trying ot convert Xint initial (IntCoord)"
132 1 pfleura2
     WRITE(*,'(50(1X,F12.8))') IntCoord(:)
133 1 pfleura2
     WRITE(*,*) "BTransInv_local"
134 1 pfleura2
     DO J=1,3*Nat
135 1 pfleura2
        WRITE(*,'(50(1X,F12.8))') BTransInv_local(:,J)
136 1 pfleura2
     END DO
137 1 pfleura2
     WRITE(*,*) "Xint initial (IntCoord_k)"
138 1 pfleura2
     WRITE(*,'(50(1X,F12.8))') IntCoord_k(:)
139 1 pfleura2
     WRITE(*,*) "Cartesian coordinates"
140 1 pfleura2
     WRITE(*,*) Nat
141 1 pfleura2
     WRITE(*,*) 'GeomCart in ConvertBakerInternal_cart'
142 1 pfleura2
     DO I=1,Nat
143 1 pfleura2
        WRITE(*,'(1X,A,I4,3(1X,F12.4))') AtName(I),I,X_k(I),Y_k(i),Z_k(i)
144 1 pfleura2
     END DO
145 1 pfleura2
     WRITE(*,*) "Calculating actual values using x_k,y_k,z_k"
146 1 pfleura2
147 1 pfleura2
!     WRITE(*,*) "First, with Calc_XPrim"
148 1 pfleura2
!     WRITE(*,*) "Coordinate:",associated(Coordinate)
149 1 pfleura2
     Call Calc_Xprim(nat,x_k,y_k,z_k,Coordinate,NPrim,XPrimitive_t,XPrimRef)
150 1 pfleura2
151 1 pfleura2
!      I=0 ! index for the NPrim (NPrim is the number of ! primitive coordinates).
152 1 pfleura2
!      ScanCoord=>Coordinate
153 1 pfleura2
!      DO WHILE (Associated(ScanCoord%next))
154 1 pfleura2
!         I=I+1
155 1 pfleura2
!         SELECT CASE (ScanCoord%Type)
156 1 pfleura2
!         CASE ('BOND')
157 1 pfleura2
!            Print *, I, ':',  ScanCoord%At1, '-', ScanCoord%At2, '=', Xprimitive_t(I)
158 1 pfleura2
!         CASE ('ANGLE')
159 1 pfleura2
!            Print *, I, ':', ScanCoord%At1, '-', ScanCoord%At2,'-',ScanCoord%At3, '=', Xprimitive_t(I)*180./Pi
160 1 pfleura2
!         CASE ('DIHEDRAL')
161 1 pfleura2
!            Print *, I, ':',  ScanCoord%At1, '-',ScanCoord%At2,'-',ScanCoord%At3,'-',&
162 1 pfleura2
!            ScanCoord%At4,'=',Xprimitive_t(I)*180./Pi
163 1 pfleura2
!         END SELECT
164 1 pfleura2
!         ScanCoord => ScanCoord%next
165 1 pfleura2
!      END DO ! matches DO WHILE (Associated(ScanCoord%next))
166 1 pfleura2
167 1 pfleura2
!      WRITE(*,*) "Second with normal proc"
168 1 pfleura2
!      I=0 ! index for the NPrim (NPrim is the number of ! primitive coordinates).
169 1 pfleura2
!      Xprimitive_t=0.d0
170 1 pfleura2
!      ScanCoord=>Coordinate
171 1 pfleura2
!      DO WHILE (Associated(ScanCoord%next))
172 1 pfleura2
!         I=I+1
173 1 pfleura2
!         SELECT CASE (ScanCoord%Type)
174 1 pfleura2
!         CASE ('BOND')
175 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At1,x_k,y_k,z_k,vx2,vy2,vz2,Norm2)
176 1 pfleura2
!            Xprimitive_t(I) = Norm2
177 1 pfleura2
!            Print *, I, ':',  ScanCoord%At1, '-', ScanCoord%At2, '=', Xprimitive_t(I)
178 1 pfleura2
!         CASE ('ANGLE')
179 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At3,x_k,y_k,z_k,vx1,vy1,vz1,Norm1)
180 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At1,x_k,y_k,z_k,vx2,vy2,vz2,Norm2)
181 1 pfleura2
!            Xprimitive_t(I) = angle(vx1,vy1,vz1,Norm1,vx2,vy2,vz2,Norm2)*Pi/180.
182 1 pfleura2
!            Print *, I, ':', ScanCoord%At1, '-', ScanCoord%At2,'-',ScanCoord%At3, '=', Xprimitive_t(I)*180./Pi
183 1 pfleura2
!         CASE ('DIHEDRAL')
184 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At1,x_k,y_k,z_k,vx1,vy1,vz1,Norm1)
185 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At3,x_k,y_k,z_k,vx2,vy2,vz2,Norm2)
186 1 pfleura2
!            Call vecteur(ScanCoord%At3,ScanCoord%At4,x_k,y_k,z_k,vx3,vy3,vz3,Norm3)
187 2 pfleura2
!            Call produit_vect(vx3,vy3,vz3,vx2,vy2,vz2,vx5,vy5,vz5,norm5)
188 2 pfleura2
!            Call produit_vect(vx1,vy1,vz1,vx2,vy2,vz2,vx4,vy4,vz4,norm4)
189 1 pfleura2
! !!! PFL 28 Feb 2008 Test to be sure that angle does not change by more than Pi
190 1 pfleura2
!            !           Xprimitive_t(I)=angle_d(vx4,vy4,vz4,norm4,vx5,vy5,vz5,norm5,vx2,vy2,vz2,norm2)*Pi/180.
191 1 pfleura2
!            DiheTmp= angle_d(vx4,vy4,vz4,norm4,vx5,vy5,vz5,norm5, &
192 1 pfleura2
!                 vx2,vy2,vz2,norm2)
193 1 pfleura2
!            Xprimitive_t(I) = DiheTmp*Pi/180.
194 1 pfleura2
!            ! We treat large dihedral angles differently as +180=-180 mathematically and physically
195 1 pfleura2
!            ! but this causes lots of troubles when converting baker to cart.
196 1 pfleura2
!            ! So we ensure that large dihedral angles always have the same sign
197 1 pfleura2
!            if (abs(ScanCoord%SignDihedral).NE.1) THEN
198 1 pfleura2
!               ScanCoord%SignDihedral=Int(Sign(1.,DiheTmp))
199 1 pfleura2
!            ELSE
200 1 pfleura2
!               If ((abs(DiheTmp).GE.170.D0).AND.(Sign(1.,DiheTmp)*ScanCoord%SignDihedral<0)) THEN
201 1 pfleura2
!                  Xprimitive_t(I) = DiheTmp*Pi/180.+ ScanCoord%SignDihedral*2.*Pi
202 1 pfleura2
!               END IF
203 1 pfleura2
!            END IF
204 1 pfleura2
205 1 pfleura2
!            IF (abs(Xprimitive_t(I)-XPrimRef(I)).GE.Pi) THEN
206 1 pfleura2
!               if ((Xprimitive_t(I)-XPrimRef(I)).GE.Pi) THEN
207 1 pfleura2
!                  Xprimitive_t(I)=Xprimitive_t(I)-2.d0*Pi
208 1 pfleura2
!               ELSE
209 1 pfleura2
!                  Xprimitive_t(I)=Xprimitive_t(I)+2.d0*Pi
210 1 pfleura2
!               END IF
211 1 pfleura2
!            END IF
212 1 pfleura2
213 1 pfleura2
!            Print *, I, ':',  ScanCoord%At1, '-',ScanCoord%At2,'-',ScanCoord%At3,'-',&
214 1 pfleura2
!            ScanCoord%At4,'=',Xprimitive_t(I)*180./Pi
215 1 pfleura2
!         END SELECT
216 1 pfleura2
!         ScanCoord => ScanCoord%next
217 1 pfleura2
!      END DO ! matches DO WHILE (Associated(ScanCoord%next))
218 1 pfleura2
219 1 pfleura2
     IntCoord_k=0.d0
220 1 pfleura2
     DO I=1, NPrim
221 1 pfleura2
        ! Transpose of UMat is needed below, that is why UMat(I,:).
222 1 pfleura2
        IntCoord_k(:)=IntCoord_k(:)+UMat_local(I,:)*Xprimitive_t(I)
223 1 pfleura2
     END DO
224 1 pfleura2
     WRITE(*,*) "Xint (intCoord_k) cooresponding to x_k,y_k,z_k"
225 1 pfleura2
     WRITE(*,'(50(1X,F12.8))') IntCoord_k(:)
226 1 pfleura2
     WRITE(*,*) "UMat_local"
227 1 pfleura2
     DO I=1, NPrim
228 1 pfleura2
        WRITE(*,'(50(1X,F12.8))') UMat_local(I,:)
229 1 pfleura2
     END DO
230 1 pfleura2
231 1 pfleura2
     !     Geom(1,:)=X_k(1:Nat)/a0
232 1 pfleura2
     !     Geom(2,:)=y_k(1:Nat)/a0
233 1 pfleura2
     !     Geom(3,:)=z_k(1:Nat)/a0
234 1 pfleura2
235 1 pfleura2
     Geom(1,:)=X_k(1:Nat)
236 1 pfleura2
     Geom(2,:)=y_k(1:Nat)
237 1 pfleura2
     Geom(3,:)=z_k(1:Nat)
238 1 pfleura2
239 1 pfleura2
     ! Computing B^prim:
240 1 pfleura2
     BprimT=0.d0
241 1 pfleura2
     ScanCoord=>Coordinate
242 1 pfleura2
     I=0
243 1 pfleura2
     DO WHILE (Associated(ScanCoord%next))
244 1 pfleura2
        I=I+1
245 1 pfleura2
        SELECT CASE (ScanCoord%Type)
246 1 pfleura2
        CASE ('BOND')
247 1 pfleura2
           CALL CONSTRAINTS_BONDLENGTH_DER(Nat,ScanCoord%at1,ScanCoord%AT2,Geom,BprimT(1,I))
248 1 pfleura2
        CASE ('ANGLE')
249 1 pfleura2
           CALL CONSTRAINTS_BONDANGLE_DER(Nat,ScanCoord%At1,ScanCoord%AT2,ScanCoord%At3,Geom,BprimT(1,I))
250 1 pfleura2
        CASE ('DIHEDRAL')
251 1 pfleura2
           CALL CONSTRAINTS_TORSION_DER2(Nat,ScanCoord%At1,ScanCoord%AT2,ScanCoord%At3,ScanCoord%At4,Geom,BprimT(1,I))
252 1 pfleura2
        END SELECT
253 1 pfleura2
        ScanCoord => ScanCoord%next
254 1 pfleura2
     END DO
255 1 pfleura2
256 1 pfleura2
     !     if (debug) THEN
257 1 pfleura2
     !        WRITE(*,*) "PFL DBG, I,NPrim,BPrimT",I,NPrim
258 1 pfleura2
     !        DO I=1,NPrim
259 1 pfleura2
     !           WRITE(*,'(50(1X,F12.6))') BPrimT(:,I)
260 1 pfleura2
     !        END DO
261 1 pfleura2
     !     END IF
262 1 pfleura2
263 1 pfleura2
     BMat_BakerT = 0.d0
264 1 pfleura2
     DO I=1,NCoord
265 1 pfleura2
        DO J=1,NPrim
266 1 pfleura2
           !BprimT is transpose of B^prim.
267 1 pfleura2
           !B = UMat^T B^prim, B^T = (B^prim)^T UMat
268 1 pfleura2
           BMat_BakerT(:,I)=BMat_BakerT(:,I)+BprimT(:,J)*UMat_local(J,I)
269 1 pfleura2
        END DO
270 1 pfleura2
     END DO
271 1 pfleura2
272 1 pfleura2
     BBT = 0.d0
273 1 pfleura2
     DO I=1,NCoord
274 1 pfleura2
        DO J=1,3*Nat   ! BBT(:,I) forms BB^T
275 1 pfleura2
           BBT(:,I) = BBT(:,I) + BMat_BakerT(J,:)*BMat_BakerT(J,I)
276 1 pfleura2
        END DO
277 1 pfleura2
     END DO
278 1 pfleura2
279 1 pfleura2
     BBT_inv = 0.d0
280 1 pfleura2
281 1 pfleura2
     !Print *, 'NCoord=', NCoord
282 1 pfleura2
     !Print *, 'BBT='
283 1 pfleura2
     DO J=1,NCoord
284 1 pfleura2
        ! WRITE(*,'(50(1X,F12.6))') BBT(:,J)
285 1 pfleura2
     END DO
286 1 pfleura2
     !Print *, 'End of BBT'
287 1 pfleura2
     ! GenInv is in Mat_util.f90
288 1 pfleura2
     Call GenInv(NCoord,BBT,BBT_inv,NCoord)
289 1 pfleura2
!     ALLOCATE(V(NCoord,NCoord))
290 1 pfleura2
!     tol = 1e-12
291 1 pfleura2
!     ! BBT is destroyed by GINVSE
292 1 pfleura2
!     Call GINVSE(BBT,NCoord,NCoord,BBT_inv,NCoord,NCoord,NCoord,tol,V,NCoord,FAIL,IOOUT)
293 1 pfleura2
!     DEALLOCATE(V)
294 1 pfleura2
!     IF(Fail) Then
295 1 pfleura2
!        WRITE (*,*) "Failed in generalized inverse, L220, ConvertBaker...f90"
296 1 pfleura2
!        STOP
297 1 pfleura2
!     END IF
298 1 pfleura2
299 1 pfleura2
     !Print *, 'BBT_inv='
300 1 pfleura2
     DO J=1,NCoord
301 1 pfleura2
        ! WRITE(*,'(50(1X,F10.2))') BBT_inv(:,J)
302 1 pfleura2
        ! Print *, BBT_inv(:,J)
303 1 pfleura2
     END DO
304 1 pfleura2
     !Print *, 'End of BBT_inv'
305 1 pfleura2
306 1 pfleura2
     ! Calculation of (B^T)^-1 = (BB^T)^-1B:
307 1 pfleura2
     BTransInv_local = 0.d0
308 1 pfleura2
     DO I=1,3*Nat
309 1 pfleura2
        DO J=1,NCoord
310 1 pfleura2
           BTransInv_local(:,I)=BTransInv_local(:,I)+BBT_inv(:,J)*BMat_BakerT(I,J)
311 1 pfleura2
        END DO
312 1 pfleura2
     END DO
313 1 pfleura2
314 1 pfleura2
     !Print *, 'BMat_BakerT='
315 1 pfleura2
     DO J=1,3*Nat
316 1 pfleura2
        !WRITE(*,'(50(1X,F12.8))') BMat_BakerT(:,J)
317 1 pfleura2
     END DO
318 1 pfleura2
     !Print *, 'End of BMat_BakerT'
319 1 pfleura2
320 1 pfleura2
     WRITE(*,*) "BTransInv_local Trans corresponding to x_k,y_k,z_k"
321 1 pfleura2
     DO J=1,3*Nat
322 1 pfleura2
        WRITE(*,'(50(1X,F12.8))') BTransInv_local(:,J)
323 1 pfleura2
     END DO
324 1 pfleura2
325 1 pfleura2
326 1 pfleura2
  END IF
327 1 pfleura2
328 1 pfleura2
  DeltaX_int(:) = IntCoord(:)-IntCoord_k(:)
329 1 pfleura2
330 1 pfleura2
  X_cart_k(1,:) = x_k(:)
331 1 pfleura2
  X_cart_k(2,:) = y_k(:)
332 1 pfleura2
  X_cart_k(3,:) = z_k(:)
333 1 pfleura2
334 1 pfleura2
  normDeltaX_int=0.d0 ! This is to be implemented.
335 1 pfleura2
  DO I=1, NCoord
336 1 pfleura2
     normDeltaX_int=normDeltaX_int+DeltaX_int(I)*DeltaX_int(I)
337 1 pfleura2
  END DO
338 1 pfleura2
  normDeltaX_int = sqrt(normDeltaX_int)
339 1 pfleura2
  ! I just need initial value of normDeltaX_int to be .GT. 1d-11,
340 1 pfleura2
  ! that is why it is initialized to 1.d0
341 1 pfleura2
  !normDeltaX_int = 1.d0
342 1 pfleura2
343 1 pfleura2
  if (debug) WRITE(*,*) "Entering the loop"
344 1 pfleura2
  Counter = 0
345 1 pfleura2
  DO While ((normDeltaX_int .GT. Crit) .AND. (Counter .LT. NbItMax))
346 1 pfleura2
     !DO While (normDeltaX_int .GT. 1d-6)
347 1 pfleura2
     Counter = Counter + 1
348 1 pfleura2
     DeltaX_cart = 0.d0
349 1 pfleura2
!     if (normDeltaX_int.LE.1e-4) THEN
350 1 pfleura2
!        DeltaX_int=DeltaX_int*1e4
351 1 pfleura2
!     END IF
352 1 pfleura2
     DO I=1,NCoord
353 1 pfleura2
        ! below BTransInv_local(I,:) is used because actually we need Transpose of BTransInv_local.
354 1 pfleura2
        DeltaX_cart(:)=DeltaX_cart(:)+BTransInv_local(I,:)*DeltaX_int(I)
355 1 pfleura2
     END DO
356 1 pfleura2
!     if (normDeltaX_int.LE.1e-4) THEN
357 1 pfleura2
!        DeltaX_int=DeltaX_int*1e-4
358 1 pfleura2
!     DeltaX_cart=DeltaX_cart*1e-4
359 1 pfleura2
!     END IF
360 1 pfleura2
361 1 pfleura2
362 1 pfleura2
           if (debug) THEN
363 1 pfleura2
              WRITE(*,*) "Info for iteration:",counter
364 1 pfleura2
              Print *, 'DeltaX_int='
365 1 pfleura2
              WRITE(*,'(50(1X,F12.8))') DeltaX_int
366 1 pfleura2
              Print *, 'DeltaX_cart,norm',sqrt(dot_product(DeltaX_cart,DeltaX_cart))
367 1 pfleura2
              DO J=1,Nat
368 1 pfleura2
              WRITE(*,'(1X,A4,3(1X,F15.11),3(1X,D25.15))') AtName(J),DeltaX_cart(3*J-2:3*J),DeltaX_cart(3*J-2:3*J)
369 1 pfleura2
              END DO
370 1 pfleura2
              Print *, 'BTransInv_local Trans='
371 1 pfleura2
              DO J=1,3*Nat
372 1 pfleura2
                 WRITE(*,'(50(1X,F12.8))') BTransInv_local(:,J)
373 1 pfleura2
              END DO
374 1 pfleura2
              Print *, 'DeltaX_int='
375 1 pfleura2
              WRITE(*,'(50(1X,F12.8))') DeltaX_int
376 1 pfleura2
           END IF
377 1 pfleura2
378 1 pfleura2
     ! Finite precision error correction step:
379 1 pfleura2
!     DO I=1,3*Nat
380 1 pfleura2
!        IF (DeltaX_cart(I) .NE. 0.d0) Then
381 1 pfleura2
!           IF(abs(DeltaX_cart(I)) .LT. 1d-10) Then
382 1 pfleura2
!              !Print *, 'Correcting DeltaX_cart(',I,')=', DeltaX_cart(I)
383 1 pfleura2
!              DeltaX_cart(I) = 0.d0
384 1 pfleura2
!           END IF
385 1 pfleura2
!        END IF
386 1 pfleura2
!     END DO
387 1 pfleura2
388 1 pfleura2
     ! new cartesian coordinates:
389 1 pfleura2
     DO I=1, Nat
390 1 pfleura2
        X_cart_k(1,I) = X_cart_k(1,I) + DeltaX_cart(3*I-2)
391 1 pfleura2
        X_cart_k(2,I) = X_cart_k(2,I) + DeltaX_cart(3*I-1)
392 1 pfleura2
        X_cart_k(3,I) = X_cart_k(3,I) + DeltaX_cart(3*I)
393 1 pfleura2
     END DO
394 1 pfleura2
395 1 pfleura2
     !     Geom(1,:)=X_cart_k(1,1:Nat)/a0
396 1 pfleura2
     !     Geom(2,:)=X_cart_k(2,1:Nat)/a0
397 1 pfleura2
     !     Geom(3,:)=X_cart_k(3,1:Nat)/a0
398 1 pfleura2
     Geom(1,:)=X_cart_k(1,1:Nat)
399 1 pfleura2
     Geom(2,:)=X_cart_k(2,1:Nat)
400 1 pfleura2
     Geom(3,:)=X_cart_k(3,1:Nat)
401 1 pfleura2
402 1 pfleura2
     if (debug) THEN
403 1 pfleura2
     WRITE(*,*) 'GeomCart used to compute BPrim'
404 1 pfleura2
     DO I=1,Nat
405 1 pfleura2
        WRITE(*,'(1X,A,I4,3(1X,F12.4))') AtName(I),I,Geom(1,I),Geom(2,i),Geom(3,i)
406 1 pfleura2
     END DO
407 1 pfleura2
     END IF
408 1 pfleura2
409 1 pfleura2
     ! Computing B^prim:
410 1 pfleura2
     BprimT=0.d0
411 1 pfleura2
     ScanCoord=>Coordinate
412 1 pfleura2
     I=0
413 1 pfleura2
     DO WHILE (Associated(ScanCoord%next))
414 1 pfleura2
        I=I+1
415 1 pfleura2
        SELECT CASE (ScanCoord%Type)
416 1 pfleura2
        CASE ('BOND')
417 1 pfleura2
           CALL CONSTRAINTS_BONDLENGTH_DER(Nat,ScanCoord%at1,ScanCoord%AT2,Geom,BprimT(1,I))
418 1 pfleura2
        CASE ('ANGLE')
419 1 pfleura2
           CALL CONSTRAINTS_BONDANGLE_DER(Nat,ScanCoord%At1,ScanCoord%AT2,ScanCoord%At3,Geom,BprimT(1,I))
420 1 pfleura2
        CASE ('DIHEDRAL')
421 1 pfleura2
           CALL CONSTRAINTS_TORSION_DER2(Nat,ScanCoord%At1,ScanCoord%AT2,ScanCoord%At3,ScanCoord%At4,Geom,BprimT(1,I))
422 1 pfleura2
        END SELECT
423 1 pfleura2
        ScanCoord => ScanCoord%next
424 1 pfleura2
     END DO
425 1 pfleura2
426 1 pfleura2
     if (debug) THEN
427 1 pfleura2
        WRITE(*,*) "Bprim "
428 1 pfleura2
        DO J=1,3*Nat
429 1 pfleura2
           WRITE(*,'(50(1X,F12.6))') BprimT(J,:)
430 1 pfleura2
        END DO
431 1 pfleura2
     END IF
432 1 pfleura2
433 1 pfleura2
434 1 pfleura2
     BMat_BakerT = 0.d0
435 1 pfleura2
     DO I=1,NCoord
436 1 pfleura2
        DO J=1,NPrim
437 1 pfleura2
           !BprimT is transpose of B^prim.
438 1 pfleura2
           !B = UMat^T B^prim, B^T = (B^prim)^T UMat
439 1 pfleura2
           BMat_BakerT(:,I)=BMat_BakerT(:,I)+BprimT(:,J)*UMat_local(J,I)
440 1 pfleura2
        END DO
441 1 pfleura2
     END DO
442 1 pfleura2
443 1 pfleura2
     ! ADDED FROM HERE:
444 1 pfleura2
     ! We now compute G=B(BT) matrix
445 1 pfleura2
     !IF (IOptt .EQ. 5000) Then
446 1 pfleura2
     !GMat=0.d0
447 1 pfleura2
     !DO I=1,NPrim
448 1 pfleura2
     !  DO J=1,3*Nat
449 1 pfleura2
     !    GMat(:,I)=Gmat(:,I)+BprimT(J,:)*BprimT(J,I) !*1.d0/mass(atome(int(K/3.d0)))
450 1 pfleura2
     !END DO
451 1 pfleura2
     !END DO
452 1 pfleura2
453 1 pfleura2
     ! We diagonalize G
454 1 pfleura2
     !Call Jacobi(GMat,NPrim,EigVal,EigVec,NPrim)
455 1 pfleura2
     !Call Trie(NPrim,EigVal,EigVec,NPrim)
456 1 pfleura2
457 1 pfleura2
     ! We construct the new DzDc(b=baker) matrix
458 1 pfleura2
     ! UMat is nonredundant vector set, i.e. set of eigenvectors of
459 1 pfleura2
     !  BB^T corresponding to eigenvalues > zero.
460 1 pfleura2
461 1 pfleura2
     !UMat=0.d0
462 1 pfleura2
     !UMat_tmp=0.d0
463 1 pfleura2
     !BMat_BakerT = 0.d0
464 1 pfleura2
     !J=0
465 1 pfleura2
     !DO I=1,NPrim
466 1 pfleura2
     !  IF (abs(eigval(I))>=1e-6) THEN
467 1 pfleura2
     !   J=J+1
468 1 pfleura2
     !  DO K=1,NPrim
469 1 pfleura2
     ! BprimT is transpose of B^prim.
470 1 pfleura2
     ! B = UMat^T B^prim, B^T = (B^prim)^T UMat
471 1 pfleura2
     !BMat_BakerT(:,J)=BMat_BakerT(:,J)+BprimT(:,K)*Eigvec(K,I)
472 1 pfleura2
     ! END DO
473 1 pfleura2
     !IF(J .GT. 3*Nat-6) THEN
474 1 pfleura2
     !WRITE(*,*) 'Number of vectors in Eigvec with eigval .GT. &
475 1 pfleura2
     !    1e-6 (=UMat) (=' ,J,') exceeded 3*Nat-6=',3*Nat-6, &
476 1 pfleura2
     !    'Stopping the calculation.'
477 1 pfleura2
     !   STOP
478 1 pfleura2
     !  END IF
479 1 pfleura2
     ! UMat_tmp(:,J) =  Eigvec(:,I)
480 1 pfleura2
     ! END IF
481 1 pfleura2
     !END DO
482 1 pfleura2
483 1 pfleura2
     ! THIS IS TO BE CORRECTED, A special case for debugging C2H3F case:
484 1 pfleura2
     !J=0
485 1 pfleura2
     !DO I=1, 3*Nat-6
486 1 pfleura2
     ! IF ((I .NE. 6) .AND. (I .NE. 7) .AND. (I .NE. 9)) Then
487 1 pfleura2
     !  J=J+1
488 1 pfleura2
     ! Print *, 'J=', J, ' I=', I
489 1 pfleura2
     !UMat(:,J) = UMat_tmp(:,I)
490 1 pfleura2
     !     END IF
491 1 pfleura2
     ! END DO
492 1 pfleura2
     !BMat_BakerT=0.d0
493 1 pfleura2
     !    DO I=1,NCoord
494 1 pfleura2
     !      DO J=1,NPrim
495 1 pfleura2
     ! B = UMat^T B^prim, B^T = (B^prim)^T UMat
496 1 pfleura2
     !       BMat_BakerT(:,I)=BMat_BakerT(:,I)+BprimT(:,J)*UMat(J,I)
497 1 pfleura2
     !    END DO
498 1 pfleura2
     !END DO
499 1 pfleura2
     ! TO BE CORRECTED, ENDS HERE.
500 1 pfleura2
501 1 pfleura2
     !END IF
502 1 pfleura2
     ! END of the new changes.
503 1 pfleura2
504 1 pfleura2
     BBT = 0.d0
505 1 pfleura2
     DO I=1,NCoord
506 1 pfleura2
        DO J=1,3*Nat   ! BBT(:,I) forms BB^T
507 1 pfleura2
           BBT(:,I) = BBT(:,I) + BMat_BakerT(J,:)*BMat_BakerT(J,I)
508 1 pfleura2
        END DO
509 1 pfleura2
     END DO
510 1 pfleura2
511 1 pfleura2
     BBT_inv = 0.d0
512 1 pfleura2
513 1 pfleura2
     !Print *, 'NCoord=', NCoord
514 1 pfleura2
     !Print *, 'BBT='
515 1 pfleura2
     DO J=1,NCoord
516 1 pfleura2
        ! WRITE(*,'(50(1X,F12.6))') BBT(:,J)
517 1 pfleura2
     END DO
518 1 pfleura2
     !Print *, 'End of BBT'
519 1 pfleura2
     ! GenInv is in Mat_util.f90
520 1 pfleura2
     Call GenInv(NCoord,BBT,BBT_inv,NCoord)
521 1 pfleura2
     !     if (debug) THEN
522 1 pfleura2
     !        WRITE(*,*) 'BBT_inv by GenInv'
523 1 pfleura2
     !        DO J=1,NCoord
524 1 pfleura2
     !           WRITE(*,'(50(1X,F12.6))') BBT_inv(:,J)
525 1 pfleura2
     !        END DO
526 1 pfleura2
     !     END IF
527 1 pfleura2
528 1 pfleura2
!    ALLOCATE(V(NCoord,NCoord))
529 1 pfleura2
!    tol = 1e-12
530 1 pfleura2
!    ! BBT is destroyed by GINVSE
531 1 pfleura2
!    Call GINVSE(BBT,NCoord,NCoord,BBT_inv,NCoord,NCoord,NCoord,tol,V,NCoord,FAIL,IOOUT)
532 1 pfleura2
!    DEALLOCATE(V)
533 1 pfleura2
!    IF(Fail) Then
534 1 pfleura2
!       WRITE (*,*) "Failed in generalized inverse, L220, ConvertBaker...f90"
535 1 pfleura2
!       STOP
536 1 pfleura2
!    END IF
537 1 pfleura2
538 1 pfleura2
     !     if (debug) THEN
539 1 pfleura2
     !        WRITE(*,*) 'BBT_inv by Genvse'
540 1 pfleura2
     !        DO J=1,NCoord
541 1 pfleura2
     !           WRITE(*,'(50(1X,F12.6))') BBT_inv(:,J)
542 1 pfleura2
     !        END DO
543 1 pfleura2
     !     END IF
544 1 pfleura2
545 1 pfleura2
     !Print *, 'End of BBT_inv'
546 1 pfleura2
547 1 pfleura2
     ! Calculation of (B^T)^-1 = (BB^T)^-1B:
548 1 pfleura2
     BTransInv_local = 0.d0
549 1 pfleura2
     DO I=1,3*Nat
550 1 pfleura2
        DO J=1,NCoord
551 1 pfleura2
           BTransInv_local(:,I)=BTransInv_local(:,I)+BBT_inv(:,J)*BMat_BakerT(I,J)
552 1 pfleura2
        END DO
553 1 pfleura2
     END DO
554 1 pfleura2
555 1 pfleura2
     !     if (debug) THEN
556 1 pfleura2
     !        Print *, 'BMat_BakerT='
557 1 pfleura2
     !        DO J=1,3*Nat
558 1 pfleura2
     !           WRITE(*,'(50(1X,F12.6))') BMat_BakerT(:,J)
559 1 pfleura2
     !        END DO
560 1 pfleura2
     !        Print *, 'End of BMat_BakerT'
561 1 pfleura2
     !     END IF
562 1 pfleura2
563 1 pfleura2
     !      if (debug) THEN
564 1 pfleura2
     !         Print *, 'BTransInv_local Trans='
565 1 pfleura2
     !         DO J=1,3*Nat
566 1 pfleura2
     !            WRITE(*,'(50(1X,F16.6))') BTransInv_local(:,J)
567 1 pfleura2
     !         END DO
568 1 pfleura2
     !         Print *, 'End of BTransInv_local Trans'
569 1 pfleura2
     !      END IF
570 1 pfleura2
571 1 pfleura2
     ! New cartesian cooordinates:
572 1 pfleura2
     x(:) = X_cart_k(1,:)
573 1 pfleura2
     y(:) = X_cart_k(2,:)
574 1 pfleura2
     z(:) = X_cart_k(3,:)
575 1 pfleura2
576 1 pfleura2
577 1 pfleura2
     Call Calc_Xprim(nat,x,y,z,Coordinate,NPrim,XPrimitive_t,XPrimRef)
578 1 pfleura2
579 1 pfleura2
!      I=0 ! index for the NPrim (NPrim is the number of
580 1 pfleura2
!      ! primitive coordinates).
581 1 pfleura2
!      Xprimitive_t=0.d0
582 1 pfleura2
!      ScanCoord=>Coordinate
583 1 pfleura2
!      DO WHILE (Associated(ScanCoord%next))
584 1 pfleura2
!         I=I+1
585 1 pfleura2
!         SELECT CASE (ScanCoord%Type)
586 1 pfleura2
!         CASE ('BOND')
587 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At1,x,y,z,vx2,vy2,vz2,Norm2)
588 1 pfleura2
!            Xprimitive_t(I) = Norm2
589 1 pfleura2
!            !Print *, I, ':',  ScanCoord%At1, '-', ScanCoord%At2, '=', Xprimitive_t(I)
590 1 pfleura2
!         CASE ('ANGLE')
591 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At3,x,y,z,vx1,vy1,vz1,Norm1)
592 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At1,x,y,z,vx2,vy2,vz2,Norm2)
593 1 pfleura2
!            Xprimitive_t(I) = angle(vx1,vy1,vz1,Norm1,vx2,vy2,vz2,Norm2)*Pi/180.
594 1 pfleura2
!            !Print *, I, ':', ScanCoord%At1, '-', ScanCoord%At2,'-',ScanCoord%At3, '=', Xprimitive_t(I)*180./Pi
595 1 pfleura2
!         CASE ('DIHEDRAL')
596 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At3,x,y,z,vx2,vy2,vz2,Norm2)
597 1 pfleura2
!            Call vecteur(ScanCoord%At2,ScanCoord%At1,x,y,z,vx1,vy1,vz1,Norm1)
598 1 pfleura2
!            Call vecteur(ScanCoord%At3,ScanCoord%At4,x,y,z,vx3,vy3,vz3,Norm3)
599 2 pfleura2
!            Call produit_vect(vx3,vy3,vz3,vx2,vy2,vz2,vx5,vy5,vz5,norm5)
600 2 pfleura2
!            Call produit_vect(vx1,vy1,vz1,vx2,vy2,vz2,vx4,vy4,vz4,norm4)
601 1 pfleura2
! !!! PFL 28 Feb 2008 Test to be sure that angle does not change by more than Pi
602 1 pfleura2
!            !           Xprimitive_t(I)=angle_d(vx4,vy4,vz4,norm4,vx5,vy5,vz5,norm5,vx2,vy2,vz2,norm2)*Pi/180.
603 1 pfleura2
!            DiheTmp=angle_d(vx4,vy4,vz4,norm4,vx5,vy5,vz5,norm5,vx2,vy2,vz2,norm2)
604 1 pfleura2
!            Xprimitive_t(I) = DiheTmp*Pi/180.
605 1 pfleura2
!            ! We treat large dihedral angles differently as +180=-180 mathematically and physically
606 1 pfleura2
!            ! but this causes lots of troubles when converting baker to cart.
607 1 pfleura2
!            ! So we ensure that large dihedral angles always have the same sign
608 1 pfleura2
!            if (abs(ScanCoord%SignDihedral).NE.1) THEN
609 1 pfleura2
!               ScanCoord%SignDihedral=Int(Sign(1.d0,DiheTmp))
610 1 pfleura2
!            ELSE
611 1 pfleura2
!               If ((abs(DiheTmp).GE.170.D0).AND.(Sign(1.,DiheTmp)*ScanCoord%SignDihedral<0)) THEN
612 1 pfleura2
!                  Xprimitive_t(I) = DiheTmp*Pi/180.+ ScanCoord%SignDihedral*2.*Pi
613 1 pfleura2
!               END IF
614 1 pfleura2
!            END IF
615 1 pfleura2
616 1 pfleura2
617 1 pfleura2
618 1 pfleura2
!            !Print *, I, ':',  ScanCoord%At1, '-',ScanCoord%At2,'-',ScanCoord%At3,'-',&
619 1 pfleura2
!            !ScanCoord%At4,'=',Xprimitive_t(I)*180./Pi
620 1 pfleura2
!         END SELECT
621 1 pfleura2
!         ScanCoord => ScanCoord%next
622 1 pfleura2
!      END DO ! matches DO WHILE (Associated(ScanCoord%next))
623 1 pfleura2
624 1 pfleura2
     !     if (debug) THEN
625 1 pfleura2
     !        WRITE(*,*) "Xprimitive_t"
626 1 pfleura2
     !        WRITE(*,'(50(1X,F12.6))') Xprimitive_t
627 1 pfleura2
     !     END IF
628 1 pfleura2
629 1 pfleura2
     IntCoord_k=0.d0
630 1 pfleura2
     DO I=1, NPrim
631 1 pfleura2
        ! Transpose of UMat is needed below, that is why UMat(I,:).
632 1 pfleura2
        IntCoord_k(:)=IntCoord_k(:)+UMat_local(I,:)*Xprimitive_t(I)
633 1 pfleura2
     END DO
634 1 pfleura2
635 1 pfleura2
     if (debug) THEN
636 1 pfleura2
        WRITE(*,*) "New Xint (IntCoord_k)"
637 1 pfleura2
        WRITE(*,'(50(1X,F12.8))') IntCoord_k
638 1 pfleura2
        WRITE(*,*) "Target (IntCoord)"
639 1 pfleura2
        WRITE(*,'(50(1X,F12.8))') IntCoord
640 1 pfleura2
641 1 pfleura2
     END IF
642 1 pfleura2
643 1 pfleura2
644 1 pfleura2
     ! Computing DeltaX_int
645 1 pfleura2
646 1 pfleura2
     DeltaX_int(:) = IntCoord(:)-IntCoord_k(:)
647 1 pfleura2
648 1 pfleura2
649 1 pfleura2
650 1 pfleura2
651 1 pfleura2
     ! norm2 of DeltaX_int is being calculated here:
652 1 pfleura2
     normDeltaX_int=0.d0
653 1 pfleura2
     DO I=1, NCoord
654 1 pfleura2
        normDeltaX_int=normDeltaX_int+DeltaX_int(I)*DeltaX_int(I)
655 1 pfleura2
     END DO
656 1 pfleura2
     normDeltaX_int = sqrt(normDeltaX_int)
657 1 pfleura2
658 1 pfleura2
          if (debug) THEN
659 1 pfleura2
             WRITE(*,*) "New Delta_Xint (deltaX_int)"
660 1 pfleura2
             WRITE(*,'(50(1X,F12.6))') DeltaX_int
661 1 pfleura2
             WRITE(*,*) "Norm:",normDeltaX_int
662 1 pfleura2
          END IF
663 1 pfleura2
664 1 pfleura2
     !IF (Counter .GE. 400) THEN
665 1 pfleura2
     !Print *, 'Counter=', Counter, 'normDeltaX_int=', normDeltaX_int
666 1 pfleura2
     !Print *, 'DeltaX_int='
667 1 pfleura2
     !WRITE(*,'(50(1X,F8.2))') DeltaX_int
668 1 pfleura2
     !END IF
669 1 pfleura2
670 1 pfleura2
     IF (Mod(Counter,1).EQ.0) THEN
671 1 pfleura2
        !WRITE(*,*) Nat
672 1 pfleura2
        !WRITE(*,*) 'GeomCart in ConvertBakerInternal_cart'
673 1 pfleura2
        DO I=1,Nat
674 1 pfleura2
           !  WRITE(*,'(1X,A,I4,3(1X,F12.4))') AtName(I),I,X_Cart_k(1:3,I)
675 1 pfleura2
        END DO
676 1 pfleura2
     END IF
677 1 pfleura2
678 1 pfleura2
     !call two_norm(DeltaX_int,normDeltaX_int,3*Nat)
679 1 pfleura2
  END DO !matches DO While (normDeltaX_int .GT. 1d-11)
680 1 pfleura2
681 1 pfleura2
  IF (debug) THEN
682 1 pfleura2
  WRITE(*,*) 'GeomCart in ConvertBakerInternal_cart'
683 1 pfleura2
  DO I=1,Nat
684 1 pfleura2
     WRITE(*,'(1X,A,I4,3(1X,F12.4))') AtName(I),I,X_Cart_k(1:3,I)
685 1 pfleura2
  END DO
686 1 pfleura2
  END IF
687 1 pfleura2
688 1 pfleura2
  if (debug) THEN
689 1 pfleura2
     WRITE(*,*) "Bmat_bakerT"
690 1 pfleura2
     DO J=1,NCoord
691 1 pfleura2
        WRITE(*,'(50(1X,F12.6))') BMat_BakerT(:,J)
692 1 pfleura2
     END DO
693 1 pfleura2
  END IF
694 1 pfleura2
695 1 pfleura2
696 1 pfleura2
697 1 pfleura2
  !IF (IOptt .GE. 2) THEN
698 1 pfleura2
  !   Print  *, 'Counter=', Counter
699 1 pfleura2
  !END IF
700 1 pfleura2
  IF (Counter .GE. NbItMax) Then
701 1 pfleura2
     Print *, 'Counter GE 500, Stopping, normDeltaX_int=', normDeltaX_int
702 1 pfleura2
     STOP
703 1 pfleura2
  END IF
704 1 pfleura2
705 1 pfleura2
706 1 pfleura2
     x(:) = X_cart_k(1,:)
707 1 pfleura2
     y(:) = X_cart_k(2,:)
708 1 pfleura2
     z(:) = X_cart_k(3,:)
709 1 pfleura2
710 1 pfleura2
711 1 pfleura2
!  call CalcRmsd(Nat,x_k,y_k,z_k,x,y,z,MRot,rmsd,.TRUE.,.TRUE.)
712 1 pfleura2
713 1 pfleura2
714 1 pfleura2
  if (debug) WRITE(*,*) "COnverted in ",counter," iterations"
715 1 pfleura2
716 1 pfleura2
  DEALLOCATE(Geom,DeltaX_int,DeltaX_cart)
717 1 pfleura2
  DEALLOCATE(BprimT,BBT,BBT_inv,UMat_tmp)
718 1 pfleura2
  DEALLOCATE(X_cart_k)
719 1 pfleura2
  DEALLOCATE(GMat,EigVal,EigVec)
720 1 pfleura2
721 1 pfleura2
  XPrim=Xprimitive_t
722 1 pfleura2
723 1 pfleura2
  if (debug) WRITE(*,*) '=====ConvertBakerInternal_cart Over====='
724 1 pfleura2
725 1 pfleura2
END SUBROUTINE ConvertBakerInternal_cart