Statistics
| Revision:

root / src / Egrad.f90 @ 10

History | View | Annotate | Download (17.3 kB)

1
subroutine Egrad(E,Geom,Grad,NCoord,IGeom,IOpt,GeomCart,FOptGeom,GeomOld,GeomCart_old)
2

    
3
  ! This routines calculates the energy E and the gradient Grad of 
4
  ! a molecule with Geometry Geom (may be in internal coordinates),
5
  ! using for now, either Gaussian or Ext, more general later.
6

    
7
  use Path_module, only : Nat,AtName,Coord,dzdc,indzmat,Atome,massat, &
8
       prog,NCart,XyzGeomF, XyzGeomI, renum, &
9
       GeomOld_all,BTransInvF,BTransInv_local,UMatF,UMat_local &
10
       , BprimT,ScanCoord, Coordinate,NPrim,BMat_BakerT,BBT,BBT_inv &
11
      ,Order, OrderInv, XPrimitiveF,FPBC,XGeomRefPBC,YGeomRefPBC,ZGeomRefPBC
12

    
13
  ! IntCoordF(NGeomF,NCoord),GeomOld_all(NGeomF,NCoord)
14
  ! allocated in Path.f90
15

    
16
  use Io_module
17
  IMPLICIT NONE
18

    
19
  ! Energy (calculated if F300K=.F., else estimated)
20
  REAL(KREAL), INTENT (OUT) :: E
21
  ! NCoord: Number of the degrees of freedom
22
  ! IGeom: index of the geometry.
23
  INTEGER(KINT), INTENT (IN) :: NCoord, IGeom, IOpt
24
  ! Geometry at which gradient is calculated (cf Factual also):
25
  REAL(KREAL), INTENT (INOUT) :: Geom(NCoord)
26
  ! Gradient calculated at Geom geometry:
27
  REAL(KREAL), INTENT (OUT) :: Grad(NCoord)
28
  ! Cartesian geometry corresponding to (Internal Geometry) Geom:
29
  REAL(KREAL), INTENT (OUT) :: GeomCart(Nat,3)
30
!!! Optional, just for geometry optimization with Baker coordinates
31
  REAL(KREAL), INTENT (IN), OPTIONAL :: GeomCart_old(Nat,3)
32
  REAL(KREAL), INTENT (INOUT), OPTIONAL :: GeomOld(NCoord)
33
! FOptGeom is a flag indicating if we are doing a geom optimization
34
! it can be omitted so that we use a local flag: Flag_Opt_Geom
35
  LOGICAL,INTENT (IN), OPTIONAL :: FOptGeom
36
! if FOptGeom is given Flag_Opt_Geom=FOptGeom, else Flag_Opt_Geom=F
37
  LOGICAL  :: Flag_Opt_Geom
38

    
39
  ! ======================================================================
40

    
41
  LOGICAL :: debug
42
  REAL(KREAL), ALLOCATABLE :: GradTmp(:)
43
  REAL(KREAL), ALLOCATABLE :: GradCart(:)
44
  REAL(KREAL), ALLOCATABLE :: x(:), y(:), z(:)
45
  REAL(KREAL) ::  Pi
46
  REAL(KREAL) :: MRot(3,3), Rmsd
47

    
48
  INTEGER(KINT) :: iat, i, j, IBeg,Idx
49

    
50
  REAL(KREAL), ALLOCATABLE :: XPrimRef(:), XPrim(:) ! NPrim
51

    
52
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
53
!
54
!!!!!!!! PFL Debug
55
!
56
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
57
  REAL(KREAL), ALLOCATABLE :: GeomTmp(:,:) !3,Nat
58
  LOGICAL ::  FALOBBT,FALOBPrimt,FAloBBTInv
59
  LOGICAL :: DebugPFL
60

    
61
  ! ======================================================================
62
  INTERFACE
63
     function valid(string) result (isValid)
64
       CHARACTER(*), intent(in) :: string
65
       logical                  :: isValid
66
     END function VALID
67
  END INTERFACE
68
  ! ======================================================================
69

    
70
  Pi=dacos(-1.0d0)
71

    
72
  if (present(FOptGeom)) THEN
73
     Flag_Opt_Geom=FOptGeom
74
  ELSE
75
     Flag_Opt_Geom=.FALSE.
76
  END IF
77

    
78
  debug=valid('EGRAD')
79
  debugPFL=valid('bakerPFL')
80
  if (debug) Call Header("Entering Egrad")
81

    
82
  if (debug) Print *, 'EGrad.f90, L73, IOpt=', IOpt, ' Flag_Opt_Geom=',Flag_Opt_Geom
83

    
84
  Grad=0.
85
  E=0.
86

    
87

    
88
  ALLOCATE(GradCart(3*Nat))
89
  ALLOCATE(x(Nat),y(Nat),z(Nat))
90
  ALLOCATE(XPrimRef(NPrim),XPrim(NPrim))
91

    
92
  SELECT CASE (COORD)
93
  CASE ('ZMAT')
94
     ! In order to avoid problem with numbering and linear angles/diehedral, we convert the
95
     ! zmat into cartesian coordinates
96
     Call Int2Cart(nat,indzmat,Geom,GeomCart)
97
! In case of PBC calculations, we have to re-orient the geometry onto the user initial geometry
98
     If (FPBC) THEN
99
! we align this geometry on the initial one
100
        if (debug) THEN
101
           WRITe(*,*) "We are orientating..."
102
           WRITE(*,*) "Geom before orientation"
103
           WRITE(*,*) Nat
104
           WRITE(*,*) ""
105
           DO I=1,Nat
106
              If (renum) Iat=Order(I)
107
              WRITE(*,'(1X,A10,3(1X,F15.8),A)') Trim(AtName(Iat)),GeomCart(I,1),GeomCart(I,2),GeomCart(I,3)
108
           END DO
109
        END IF
110

    
111
        IF (Renum) THEN
112
           DO I=1,Nat
113
              Iat=Order(I)
114
              X(I)=GeomCart(Iat,1)
115
              Y(I)=GeomCart(Iat,2)
116
              Z(I)=GeomCart(Iat,3)
117
           END DO
118
        ELSE
119
           X(1:Nat)=GeomCart(1:Nat,1)
120
           Y(1:Nat)=GeomCart(1:Nat,2)
121
           Z(1:Nat)=GeomCart(1:Nat,3)
122
        END IF
123

    
124
           WRITE(*,*) "Geom before orientation after reorderring"
125
           WRITE(*,*) Nat
126
           WRITE(*,*) ""
127
           DO I=1,Nat
128
!              Iat=I
129
!              If (Renum) Iat=Order(I)
130
              WRITE(*,'(1X,A10,3(1X,F15.8),A)') Trim(AtName(I)),X(I),Y(I),Z(I)
131
           END DO
132
           WRITE(*,*) "Ref Geom"
133
           WRITE(*,*) Nat
134
           WRITE(*,*) ""
135
           DO I=1,Nat
136
              WRITE(*,'(1X,A10,3(1X,F15.8),A)') Trim(AtName(I)),XGeomRefPBC(I),YGeomRefPBC(I),ZGeomRefPBC(I)
137
           END DO
138
        END IF
139

    
140
        Call CalcRmsd(Nat,XGeomRefPBC,YGeomRefPBC,ZGeomRefPBC, &
141
             X,Y,Z, MRot, Rmsd,.TRUE.,.TRUE.) 
142

    
143
        If (DEBUG) THEN
144
           WRITE(*,*) "Geom AFTER orientation"
145
           WRITE(*,*) Nat
146
           WRITE(*,*) ""
147
           DO I=1,Nat
148
              WRITE(*,'(1X,A10,3(1X,F15.8),A)') Trim(AtName(I)),X(I),Y(I),Z(I)
149
           END DO
150
        END IF
151
        
152
        If (Renum)  THEN
153
           Do I=1,Nat
154
              Iat=orderInv(I)
155
              GeomCart(Iat,1)=X(I)
156
              GeomCart(Iat,2)=Y(I)
157
              GeomCart(Iat,3)=Z(I)
158
           END DO
159
        END IF
160
  CASE ('BAKER')
161
     XPrimRef=XPrimitiveF(IGeom,:)
162
     IF (Flag_Opt_Geom) THEN
163
        IF (IOpt .EQ. 1) THEN
164
           GeomCart(:,1) = XyzGeomI(IGeom,1,:)
165
           GeomCart(:,2) = XyzGeomI(IGeom,2,:)
166
           GeomCart(:,3) = XyzGeomI(IGeom,3,:)
167
        ELSE
168
           if (debug) WRITE(*,*) 'Before Call ConvertBakerInternal_cart; L125, Egrad.f90'
169
           if (.NOT.present(GeomOld)) THEN
170
              WRITE(*,*) "ERROR: GeomOld not passed as an argument"
171
              STOP
172
           END IF
173
           WRITE(*,*) 'GeomOld='
174
           WRITE(*,'(12(1X,F6.3))') GeomOld
175
           WRITE(*,*) 'Geom='
176
           WRITE(*,'(12(1X,F6.3))') Geom
177
           if (.NOT.present(GeomCart_Old)) THEN
178
              WRITE(*,*) "ERROR: GeomCart_Old not passed as an argument"
179
              STOP
180
           END IF
181

    
182
           WRITE(*,*) 'GeomCart_old='
183
           WRITE(*,'(20(1X,F6.3))') GeomCart_old(1:Nat,1:3)
184
           Call ConvertBakerInternal_cart(GeomOld,Geom,GeomCart_old(:,1), &
185
                GeomCart_old(:,2),GeomCart_old(:,3),x,y,z,XPrim,XPrimRef)
186
           GeomCart(:,1) = x(:)
187
           GeomCart(:,2) = y(:)
188
           GeomCart(:,3) = z(:)
189
        END IF
190
     ELSE
191
        IF (IOpt .EQ. 0) THEN  
192
           GeomCart(:,1) = XyzGeomF(IGeom,1,:)
193
           GeomCart(:,2) = XyzGeomF(IGeom,2,:)
194
           GeomCart(:,3) = XyzGeomF(IGeom,3,:)
195
        ELSE
196
           ! IntCoordF(NGeomF,NCoord), XyzGeomF(NGeomF,3,Nat)
197
           ! Geom has to be converted into x,y,z
198
           ! GeomOld_all and XyzGeomF are old geometry in internal and cartesian coords resp.
199
           DO I=1,NCoord ! these variables are used in ConvertBakerInternal_cart()
200
              BTransInv_local(I,:) = BTransInvF(IGeom,I,:)
201
              UMat_local(:,I) = UMatF(IGeom,:,I)
202
           END DO
203
           if (debug) Print *, 'I am before Call ConvertBakerInternal_cart; L160, Egrad.f90'
204
           WRITE(*,*) 'GeomOld='
205
           WRITE(*,'(12(1X,F6.3))') GeomOld_all(IGeom,:)
206
           WRITE(*,*) 'Geom='
207
           WRITE(*,'(12(1X,F6.3))') Geom
208
           WRITE(*,*) 'DBG Egrad L165 GeomCart_old='
209
           DO I=1,Nat
210
              WRITE(*,'(3(1X,F12.8))') XyzGeomF(IGeom,:,I)
211
           END DO
212

    
213
           Call ConvertBakerInternal_cart(GeomOld_all(IGeom,:),Geom,XyzGeomF(IGeom,1,:), &
214
          XyzGeomF(IGeom,2,:),XyzGeomF(IGeom,3,:),x,y,z,XPrim,XPrimRef)
215
           if (debug) Print *, 'I am after Call ConvertBakerInternal_cart; L111, Egrad.f90'
216
           DO I=1,NCoord ! these variables are used in ConvertBakerInternal_cart()
217
              BTransInvF(IGeom,I,:) = BTransInv_local(I,:)
218
           END DO
219
           ! GeomCart is actually XyzCoord and has INTENT(OUT) in this subroutine.
220
           GeomCart(:,1) = x(:)
221
           GeomCart(:,2) = y(:)
222
           GeomCart(:,3) = z(:)
223
        END IF ! matches IF (IOpt .EQ. 0) THEN
224
     END IF
225
  CASE ('MIXED')
226
!  write(*,*) "Coucou 4-mixed"
227
     Call Mixed2Cart(Nat,indzmat,geom,GeomCart)
228
  CASE ('CART')
229

    
230
     GeomCart=reshape(Geom,(/Nat,3/))
231
     if (debug) THEN
232
        WRITE(*,*) "Coord=CART... in egrad"
233
        DO Iat=1,Nat
234
           WRITE(IOTMP,'(1X,A10,3(1X,F15.8),A)') Trim(AtName(Iat)),Geom(3*Iat-2:3*Iat)
235
        END DO
236
     END IF
237
  CASE ('HYBRID')
238
!  write(*,*) "Coucou 4-hybrid"
239
        GeomCart=reshape(Geom,(/Nat,3/))
240
  CASE DEFAULT
241
     WRITE (*,*) "Coord=",TRIM(COORD)," not recognized. Stop"
242
     STOP
243
  END SELECT
244
  !WRITE(*,*) Nat
245
  !WRITE(*,*) 'GeomCart in Egrad_baker'
246
  !DO I=1,Nat
247
  !  WRITE(*,'(1X,A,3(1X,F12.8))') AtName(I),GeomCart(I,1:3)
248
  !END DO
249

    
250

    
251

    
252
  SELECT CASE (Prog)
253
  CASE ('GAUSSIAN')
254
! we call the Gaussian routine.
255
! it will return the energy and the gradient in cartesian coordinates.
256
! Gradient calculated at GeomCart geometry and stored in GradCart, which has INTENT(OUT)
257
     Call egrad_gaussian(E,GeomCart,GradCart)
258
  CASE ('MOPAC')
259
! we call the Mopac routine.
260
! it will return the energy and the gradient in cartesian coordinates.
261
! Gradient calculated at GeomCart geometry and stored in GradCart, which has INTENT(OUT)
262
     Call egrad_mopac(E,GeomCart,GradCart)
263
  CASE ('VASP')
264
     Call egrad_vasp(E,Geomcart,GradCart)
265
  CASE ('SIESTA')
266
     Call egrad_siesta(E,Geomcart,GradCart)
267
  CASE ('TURBOMOLE')
268
     Call egrad_turbomole(E,Geomcart,GradCart)
269
  CASE ('EXT')
270
     Call egrad_ext(E,Geomcart,GradCart)
271
  CASE ('TEST')
272
     Call egrad_test(Nat,E,Geomcart,GradCart)
273
  CASE ('TEST2D')
274
     Call egrad_test_2D(Nat,E,Geomcart,GradCart)
275
  CASE ('CHAMFRE')
276
     Call egrad_chamfre(Nat,E,Geomcart,GradCart)
277
  CASE ('LEPS')
278
     Call egrad_LEPS(Nat,E,Geomcart,GradCart)
279
  END SELECT
280
  if (debug) THEN
281
     WRITE(*,*) 'DBG EGRAD, GradCart read'
282
     DO I=1,Nat
283
        Iat=I
284
        if (renum) Iat=orderInv(I)
285
        WRITE(*,'(1X,A10,3(1X,F15.8),A)') Trim(AtName(Iat)),GradCart(3*I-2:3*I)
286
     END DO
287
  END IF
288

    
289
!  If (PROG=="VASP") STOP
290

    
291

    
292
  ! We have the gradient in Cartesian coordinates... which is ok for COORD=Cart, Hybrid
293
  !  but we have to convert it into Zmat if COORD=Zmat
294
  SELECT CASE (COORD)
295
     CASE ("ZMAT")
296
        ALLOCATE(GradTmp(3*Nat))
297
        if (debug) WRITE(*,*) "DBG EGRAD Calling CalcBMat_int"
298
        CALL CalcBMat_int (nat, reshape(GeomCart,(/3,Nat/),ORDER=(/2,1/)), indzmat, dzdc, massat,atome)
299

    
300
        if (debug) WRITE(*,*) "DBG EGRAD Calling ConvGrad_Cart2Int"
301
        CALL ConvGrad_Cart2Int(Nat,dzdc,IndZmat,GradCart,GradTmp)
302

    
303
        if (debug) WRITE(*,*) "DBG EGRAD Storing Grad"
304
     Grad(1)=GradTmp(4)
305
     Grad(2)=GradTmp(7)
306
! We might have troubles whith angles that are not in the [0:pi] range because,
307
! when converted to catesian coord, they do appear in the [0:Pi] range to gaussian...
308
! so that the derivative is not good, and a multiplicative factor should be added...
309
!
310
! This in fact should be taken care of in B mat calculation... 
311
!
312
     IF ((Geom(3).LE.0).OR.(Geom(3).GE.Pi)) THEN
313
        Grad(3)=-1.0d0*GradTmp(8)
314
     ELSE
315
        Grad(3)=GradTmp(8)
316
     END IF
317
     Idx=4
318
     DO I=4,Nat
319
        Grad(Idx)=GradTmp(3*i-2)
320
        Grad(Idx+1)=GradTmp(3*i-1)
321
        Grad(Idx+2)=GradTmp(3*i)
322
        Idx=Idx+3
323
     END DO
324
     DEALLOCATE(GradTmp)
325
   CASE ('BAKER')
326
     ! We have the gradient in Cartesian coordinates... which is ok for COORD=Cart, Hybrid
327
     ! but we have to convert it into internal coordinates if COORD=Baker	 
328
!!!!!!!!!!!!!!!!!!!!
329
     !
330
     !   PFL Debug
331
     !
332
!!!!!!!!!!!!!!!!!!!!
333
     if (debugPFL) THEN
334
        WRITE(*,*) "DBG PFL Egrad_Baker... computing dzdc"
335
     if (.not.ALLOCATED(indzmat)) THEN
336
        ALLOCATE(indzmat(Nat,5))
337
     IndZmat=0
338
     Indzmat(1,1)=1
339
     IndZmat(2,1)=2
340
     IndZmat(2,2)=1
341
     IndZmat(3,1)=3
342
     IndZmat(3,2)=2
343
     IndZmat(3,3)=1
344
     IndZmat(4,1)=4
345
     IndZmat(4,2)=3
346
     IndZmat(4,3)=2
347
     IndZmat(4,4)=1
348
     END IF
349
     IF (.NOT.ALLOCATED(DzDc)) THEN
350
        ALLOCATE(DzDc(3,nat,3,Nat))
351
     END IF
352
     if (.NOT.Allocated(GeomTmp)) Allocate(GeomTmp(3,Nat))                                            
353
     DO I=1,Nat
354
     GeomTmp(1,I)=GeomCart(OrderInv(I),1)
355
     GeomTmp(2,I)=GeomCart(OrderInv(i),2)
356
     GeomTmp(3,I)=GeomCart(OrderInv(i),3)
357
     END DO
358

    
359
     DzDc=0.d0
360
     CALL CalcBMat_int (nat, GeomTmp, indzmat, dzdc, massat,atome)     
361

    
362
     END IF ! if (debugPFL)
363
!!!!!!!!!!!!!!!!!!!!!!!!!
364
     ! Debugging PFL
365
!!!!!!!!!!!!!!!!!!!!!!!!
366

    
367
     WRITE(*,*) "BTransInv_local Trans that is used originally"
368
     DO J=1,3*Nat
369
        WRITE(*,'(50(1X,F12.8))') BTransInv_local(:,J)
370
     END DO
371

    
372
     WRITE(*,*) "Calculating actual values using GeomCart"
373
     if (.NOT.Allocated(GeomTmp)) Allocate(GeomTmp(3,Nat))
374
     GeomTmp(1,:)=GeomCart(1:Nat,1)
375
     GeomTmp(2,:)=GeomCart(1:Nat,2)
376
     GeomTmp(3,:)=GeomCart(1:Nat,3)
377

    
378
     ! Computing B^prim:
379
     FAloBBT=.NOT.ALLOCATED(BBT)
380
     FAloBBTInv=.NOT.ALLOCATED(BBT_inv)
381
     FAloBPrimT=.NOT.ALLOCATED(BprimT)
382
     if (FAloBPrimT) ALLOCATE(Bprimt(3*Nat,NPrim))
383
     if (FAloBBT) ALLOCATE(BBt(NCoord,NCoord))
384
     if (FAloBBTInv) ALLOCATE(BBt_inv(NCoord,NCoord))
385
     BprimT=0.d0
386
     ScanCoord=>Coordinate
387
     I=0
388
     DO WHILE (Associated(ScanCoord%next))
389
        I=I+1
390
        SELECT CASE (ScanCoord%Type)
391
        CASE ('BOND') 
392
           CALL CONSTRAINTS_BONDLENGTH_DER(Nat,ScanCoord%at1,ScanCoord%AT2,GeomTmp,BprimT(1,I))
393
        CASE ('ANGLE')
394
           CALL CONSTRAINTS_BONDANGLE_DER(Nat,ScanCoord%At1,ScanCoord%AT2,ScanCoord%At3,GeomTmp,BprimT(1,I))
395
        CASE ('DIHEDRAL')
396
           CALL CONSTRAINTS_TORSION_DER2(Nat,ScanCoord%At1,ScanCoord%AT2,ScanCoord%At3,ScanCoord%At4,GeomTmp,BprimT(1,I))
397
        END SELECT
398
        ScanCoord => ScanCoord%next
399
     END DO
400

    
401
     if (debug) THEN
402
        WRITE(*,*) "Bprim "
403
        DO J=1,3*Nat
404
           WRITE(*,'(50(1X,F12.6))') BprimT(J,:)
405
        END DO
406
     END IF
407

    
408
     BMat_BakerT = 0.d0
409
     DO I=1,NCoord
410
        DO J=1,NPrim
411
           !BprimT is transpose of B^prim.
412
           !B = UMat^T B^prim, B^T = (B^prim)^T UMat
413
           BMat_BakerT(:,I)=BMat_BakerT(:,I)+BprimT(:,J)*UMat_local(J,I)
414
        END DO
415
     END DO
416

    
417
     BBT = 0.d0
418
     DO I=1,NCoord
419
        DO J=1,3*Nat   ! BBT(:,I) forms BB^T
420
           BBT(:,I) = BBT(:,I) + BMat_BakerT(J,:)*BMat_BakerT(J,I)
421
        END DO
422
     END DO
423

    
424
     BBT_inv = 0.d0
425

    
426
     Call GenInv(NCoord,BBT,BBT_inv,NCoord)
427

    
428
!     ALLOCATE(V(NCoord,NCoord))
429
!     tol = 1e-10
430
!     ! BBT is destroyed by GINVSE
431
!     Call GINVSE(BBT,NCoord,NCoord,BBT_inv,NCoord,NCoord,NCoord,tol,V,NCoord,FAIL,IOOUT)
432
!     DEALLOCATE(V)
433
!     IF(Fail) Then
434
!        WRITE (*,*) "Failed in generalized inverse, L220, ConvertBaker...f90"
435
!        STOP
436
!     END IF
437

    
438
     !Print *, 'BBT_inv='
439
     DO J=1,NCoord
440
        ! WRITE(*,'(50(1X,F10.2))') BBT_inv(:,J)
441
        ! Print *, BBT_inv(:,J)
442
     END DO
443
     !Print *, 'End of BBT_inv'
444

    
445
     ! Calculation of (B^T)^-1 = (BB^T)^-1B:
446
     BTransInv_local = 0.d0
447
     DO I=1,3*Nat
448
        DO J=1,NCoord
449
           BTransInv_local(:,I)=BTransInv_local(:,I)+BBT_inv(:,J)*BMat_BakerT(I,J)
450
        END DO
451
     END DO
452

    
453
     Print *, 'BMat_BakerT='
454
     DO J=1,3*Nat
455
        WRITE(*,'(50(1X,F12.8))') BMat_BakerT(:,J)
456
     END DO
457

    
458
     WRITE(*,*) "BTransInv_local Trans corresponding to GeomCart"
459
     DO J=1,3*Nat
460
        WRITE(*,'(50(1X,F12.8))') BTransInv_local(:,J)
461
     END DO
462

    
463
     if (FAloBPrimT) DEALLOCATE(Bprimt)
464
     if (FAloBBT) DEALLOCATE(BBt)
465
     if (FAloBBTInv) DEALLOCATE(BBt_inv)
466

    
467
     Grad=0.d0
468
     DO I=1, 3*Nat
469
        ! Here Grad is gradient in Baker coordinates. GradCart(3*Nat)
470
        Grad(:) = Grad(:) + BTransInv_local(:,I)*GradCart(I) !BTransInv_local = BTransInv in Opt_Geom.f90
471
     END DO
472
     !WRITE(*,*) 'In Egrad.f90, L263, Gradient:' 
473
     !WRITE(*,'(12(1X,F8.4))') Grad(1:NCoord)
474

    
475

    
476
     CASE ('MIXED')
477
        ALLOCATE(GradTmp(3*Nat))
478
        if (debug) WRITE(*,*) "DBG EGRAD Calling CalcBMat_mixed"
479
        CALL CalcBMat_mixed (nat, reshape(GeomCart,(/3,Nat/),ORDER=(/2,1/)), indzmat, dzdc, massat,atome)
480

    
481
        if (Debug) THEN
482
           WRITE(*,*) "DzDc"
483
           DO I=1,Nat
484
              DO J=1,3
485
                 WRITE(*,'(50(1X,F12.6))') dzdc(J,I,:,:)
486
              END DO
487
           END DO
488
        END IF
489
       
490

    
491
        CALL ConvGrad_Cart2Int(Nat,dzdc,IndZmat,GradCart,GradTmp)
492
        DO I=1,Nat
493
           WRITE(*,'(1X,3(1X,F10.5))') GradTmp(3*I-2:3*I)
494
        END DO
495
        SELECT CASE (NCART)
496
           CASE (1)
497
              Grad(1:3)=GradTmp(1:3)
498
              Grad(4)=GradTmp(4)
499
              Grad(5)=GradTmp(7)
500
              Grad(6)=GradTmp(8)
501
              Idx=7
502
              IBeg=4
503
           CASE(2)
504
              Grad(1:3)=GradTmp(1:3)
505
              Grad(4:6)=GradTmp(4:6)
506
              Grad(7)=GradTmp(7)
507
              Grad(8)=GradTmp(8)
508
              Idx=9
509
               IBeg=4
510
            CASE DEFAULT
511
               Idx=1
512
               IBeg=1
513
            END SELECT
514
     DO I=IBeg,Nat
515
        Grad(Idx)=GradTmp(3*i-2)
516
        Grad(Idx+1)=GradTmp(3*i-1)
517
        Grad(Idx+2)=GradTmp(3*i)
518
        Idx=Idx+3
519
     END DO
520
     DEALLOCATE(GradTmp)
521
  CASE ("CART","HYBRID")
522
     Grad=GradCart
523
  CASE DEFAULT
524
     WRITE(*,*) "Coord=",AdjustL(Trim(COORD))," not yet implemented in Egradients. STOP"
525
     STOP
526
  END SELECT
527

    
528
  DEALLOCATE(GradCart)
529
  DEALLOCATE(x,y,z)
530

    
531
  IF (Debug) WRITE(*,*) 'DBG EGRAD grad',grad(1:NCoord)
532
  if (debug) Call Header("Egrad Over")
533

    
534
  !999 CONTINUE
535
  !if (.NOT.Ftmp) WRITE(*,*) 'We should not be here !!!!'
536
  !STOP
537
  ! ======================================================================
538
end subroutine Egrad
539