Statistiques
| Révision :

root / src / CalcHess.f90 @ 2

Historique | Voir | Annoter | Télécharger (4,49 ko)

1 1 pfleura2
      subroutine CalcHess (nfree,geom, hess,Igeom,Iopt)
2 1 pfleura2
3 1 pfleura2
! This routine calculate the Hessian (or its inverse) numerically using Egrad.
4 1 pfleura2
! It should be general and should work with any combination of Coord and Prog
5 1 pfleura2
! however, it has not been tested with Prog different from 'test'
6 1 pfleura2
7 1 pfleura2
! It comes mostly from the hess.f subroutine of the IRC package.
8 1 pfleura2
9 1 pfleura2
10 1 pfleura2
  use Path_module, only : Nat,Coord,Hinv
11 1 pfleura2
12 1 pfleura2
  use Io_module
13 1 pfleura2
!
14 1 pfleura2
      IMPLICIT NONE
15 1 pfleura2
16 1 pfleura2
!!!!!!!!!!! Parameters
17 1 pfleura2
  ! NFree==NCoord: Number of the degrees of freedom
18 1 pfleura2
  ! IGeom: index of the geometry.
19 1 pfleura2
  ! Iopt: step in the optimization process
20 1 pfleura2
  INTEGER(KINT), INTENT (IN) :: NFree, IGeom, IOpt
21 1 pfleura2
! Geometry at which Hessian is calculated (or its inverse)
22 1 pfleura2
   REAL(KREAL), INTENT(IN) :: Geom(Nfree)
23 1 pfleura2
! Hessian in compacted form
24 1 pfleura2
      REAL(KREAL), INTENT(OUT) ::  Hess(Nfree,Nfree)
25 1 pfleura2
!
26 1 pfleura2
! Parameters used to calculate the Hessian
27 1 pfleura2
! numerically with increasing accuray
28 1 pfleura2
      INTEGER(KINT), DIMENSION(6,0:3), PARAMETER :: IECOEF  &
29 1 pfleura2
      = RESHAPE(SOURCE=(/           &
30 1 pfleura2
          -1,1,0,0,0,0,             &
31 1 pfleura2
          -1,1,0,0,0,0,             &
32 1 pfleura2
          1,-8,8,-1,0,0,            &
33 1 pfleura2
          -1,9,-45,45,-9,1          &
34 1 pfleura2
       /), SHAPE=(/6,4/))
35 1 pfleura2
36 1 pfleura2
      INTEGER(KINT), DIMENSION(6,0:3), PARAMETER :: ISCOEF   &
37 1 pfleura2
      = RESHAPE(SOURCE=(/    &
38 1 pfleura2
         0,1,0,0,0,0,         &
39 1 pfleura2
          -1,1,0,0,0,0,       &
40 1 pfleura2
          -2,-1,1,2,0,0,      &
41 1 pfleura2
           -3,-2,-1,1,2,3     &
42 1 pfleura2
         /),SHAPE=(/6,4/))
43 1 pfleura2
      INTEGER(KINT), DIMENSION(0:3), PARAMETER :: NbDiv = (/1,2,12,60/)
44 1 pfleura2
      INTEGER(KINT), DIMENSION(0:3), PARAMETER :: NbCoef = (/2,2,4,6/)
45 1 pfleura2
46 1 pfleura2
47 1 pfleura2
      INTEGER(KINT), PARAMETER :: HessAcc=2
48 1 pfleura2
      REAL(KREAL), PARAMETER :: StepHess=0.01d0
49 1 pfleura2
! ======================================================================
50 1 pfleura2
      logical           :: debug
51 1 pfleura2
52 1 pfleura2
      REAL(KREAL) :: Ener
53 2 pfleura2
      INTEGER(KINT) :: I, J, IAT, IConst
54 1 pfleura2
55 2 pfleura2
       REAL(KREAL), ALLOCATABLE :: Temp2(:, :)
56 1 pfleura2
57 1 pfleura2
58 1 pfleura2
       REAL(KREAL), ALLOCATABLE :: Value(:), ValueOld(:), GradTMP(:)
59 1 pfleura2
       REAL(KREAL), ALLOCATABLE :: RTmp1(:,:), RTmp3(:,:)  ! Nat,3
60 1 pfleura2
       REAL(KREAL), ALLOCATABLE :: RTmp2(:)  ! NFree
61 1 pfleura2
! ======================================================================
62 1 pfleura2
63 1 pfleura2
  INTERFACE
64 1 pfleura2
     function valid(string) result (isValid)
65 1 pfleura2
       CHARACTER(*), intent(in) :: string
66 1 pfleura2
       logical                  :: isValid
67 1 pfleura2
     END function VALID
68 1 pfleura2
  END INTERFACE
69 1 pfleura2
70 1 pfleura2
! ======================================================================
71 1 pfleura2
72 1 pfleura2
      hess=0.d0
73 1 pfleura2
74 1 pfleura2
      debug=valid('CALCHESS').OR.valid('hess')
75 1 pfleura2
76 1 pfleura2
      if (debug) THEN
77 1 pfleura2
         WRITE(*,*) 'Entering CalcHess'
78 1 pfleura2
         WRITE(*,*) 'StepHess=',StepHess
79 1 pfleura2
         WRITE(*,*) "HessAcc=",HessAcc
80 1 pfleura2
      END IF
81 1 pfleura2
82 1 pfleura2
      allocate(Value(Nfree), ValueOld(Nfree), GradTmp(Nfree))
83 1 pfleura2
      ALLOCATE(RTmp1(Nat,3),RTmp3(Nat,3),RTmp2(NFree))
84 1 pfleura2
      allocate(temp2(Nfree,Nfree))
85 1 pfleura2
86 1 pfleura2
      RTmp1=0.d0
87 1 pfleura2
      RTmp3=0.d0
88 1 pfleura2
      RTmp2=Geom
89 1 pfleura2
90 1 pfleura2
!     Now, we calculate the second derivatives for the active variables
91 1 pfleura2
      Value=Geom
92 1 pfleura2
      ValueOld=Geom
93 1 pfleura2
94 1 pfleura2
      Do IConst=1,Nfree
95 1 pfleura2
            DO I=1,NbCoef(HessAcc)
96 1 pfleura2
               Value(IConst)=valueOld(IConst)+StepHess*ISCOEF(I,HessAcc)
97 1 pfleura2
!     We 'propagate this new value in the Zmatrix'
98 1 pfleura2
               call egrad(ener,value,gradtmp,NFree,IGeom,Iopt,RTmp1,RTmp2,RTmp3,.FALSE.)
99 1 pfleura2
               Hess(1:Nfree,IConst)= Hess(1:Nfree,IConst)+   &
100 1 pfleura2
                    gradtmp(1:Nfree)*IECOEF(I,HessAcc)
101 1 pfleura2
            end do
102 1 pfleura2
         END DO
103 1 pfleura2
        deallocate(Value, ValueOld, GradTmp)
104 1 pfleura2
105 1 pfleura2
        HESS=Hess/(NbDiv(HessAcc)*StepHess)
106 1 pfleura2
107 1 pfleura2
         if (debug) then
108 1 pfleura2
            WRITE(*,*) 'Non symmetrized hessian'
109 1 pfleura2
            DO iat=1,Nfree
110 1 pfleura2
               WRITE(*,'(15(1X,F12.5))') Hess(iat,1:Nfree)
111 1 pfleura2
            END DO
112 1 pfleura2
         END if
113 1 pfleura2
114 1 pfleura2
         DO i=1,Nfree
115 1 pfleura2
           Temp2(i,i)=Hess(i,i)
116 1 pfleura2
           DO j=i+1,Nfree
117 1 pfleura2
             Temp2(i,j)=0.5*(Hess(i,j)+Hess(j,i))
118 1 pfleura2
             Temp2(j,i)=Temp2(i,j)
119 1 pfleura2
           END DO
120 1 pfleura2
         END DO
121 1 pfleura2
122 1 pfleura2
         Hess=Hess-Temp2
123 1 pfleura2
124 1 pfleura2
         if (debug) then
125 1 pfleura2
            WRITE(*,*) 'Symmetrized hessian'
126 1 pfleura2
            DO iat=1,Nfree
127 1 pfleura2
               WRITE(*,'(15(1X,F12.5))') Temp2(iat,1:Nfree)
128 1 pfleura2
            END DO
129 1 pfleura2
130 1 pfleura2
            WRITE(*,*) 'Difference between non-syma and Symmetrized hessian'
131 1 pfleura2
            DO iat=1,Nfree
132 1 pfleura2
               WRITE(*,'(15(1X,F12.5))') Hess(iat,1:Nfree)
133 1 pfleura2
            END DO
134 1 pfleura2
         end if
135 1 pfleura2
136 1 pfleura2
! We calculate inverse of the Hessian only if HInv=T
137 1 pfleura2
138 1 pfleura2
         Hess=Temp2
139 1 pfleura2
      if (Hinv) then
140 1 pfleura2
        call Geninv(Nfree,Hess,Temp2,NFree)
141 1 pfleura2
        Hess=Temp2
142 1 pfleura2
      end if
143 1 pfleura2
144 1 pfleura2
      deallocate(RTmp1,RTmp2,RTmp3,Temp2)
145 1 pfleura2
146 1 pfleura2
! ======================================================================
147 1 pfleura2
      end subroutine CalcHess