Statistiques
| Révision :

root / src / Hinvup_BFGS.f90 @ 3

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

1
! This program reads an old hessian, old forces, new
2
! forces and updates the hessian matrix inverse
3
! using the BFGS scheme.
4
! 
5
! This routines has been modified so that its input is now:
6
SUBROUTINE Hinvup_BFGS(Nfree,ds,dgrad,Hess)
7

    
8
  IMPLICIT NONE
9

    
10
  INTEGER, PARAMETER :: KINT=KIND(1)
11
  INTEGER, PARAMETER :: KREAL=KIND(1.D0)
12

    
13
  INTEGER(KINT), INTENT(IN) :: NFREE
14
  REAL(KREAL), INTENT(INOUT) :: Hess(Nfree,Nfree)
15
  REAL(KREAL), INTENT(IN) :: ds(Nfree)
16
  REAL(KREAL), INTENT(IN) :: dGrad(Nfree)
17
  
18
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19
!
20
! This subroutine updates the inverse of the Hessian
21
! using a BFGS formula.
22
!
23
! input:
24
!  - nfree: number of coordinates
25
!  - ds: GeomOld-Geom (expressed in the optimization coord.)
26
!  - dgrad: GradOld-Grad  (expressed in the optimization coord.)
27
!
28
!  input/output:
29
!  - Hess: old Hessian in input, replaced by updated Hessian on output
30
!
31
!  Notations for the formula:
32
! s=Geom-GeomOld  
33
! y=Grad-GradOld
34
! W= (H)-1 ie the inverse matrix that we are updating
35
! W+=W - [s(yT)W+Wy(sT)]/(y,s)+ {1+[(y,Wy)/(y,s)]}(s(sT))/(y,s)
36
! 
37
!!!!!!!!!!!!!!!!
38
!
39
! Due to the dual properties, this routine is also called to compute
40
! the DFP update for the Hessian
41
!
42
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43

    
44

    
45
  REAL(KREAL), ALLOCATABLE  :: HessOld(:,:) , Wy(:)
46
  REAL(KREAL), ALLOCATABLE  :: Hup(:,:),ssT(:,:)
47
  REAL(KREAL), ALLOCATABLE  :: HupInt(:)
48
  REAL(KREAL), ALLOCATABLE  :: Num(:,:), Num2(:,:)
49
  REAL(KREAL) :: Den, NormDS
50
  INTEGER(KINT) :: i,j,k, Idx
51

    
52
  LOGICAL debug,valid
53
  EXTERNAL valid
54

    
55
  debug=valid("Hinvup_BFGS")
56

    
57
  if (debug) WRITE(*,*) "=========================== Entering Hinvup_BFGS ===================="
58

    
59
  ALLOCATE(HessOld(nfree,nfree), Hup(nfree,nfree), &
60
       HupInt(nfree),  &
61
       Num(nfree,nfree), Num2(nfree,nfree),Wy(Nfree),ssT(Nfree,nFree) )
62

    
63

    
64

    
65
  HessOld=Hess
66

    
67
  if (debug) THEN
68
     WRITE(*,*) 'DBG Hinvup_BFGS HessOld'
69
     Idx=min(12,Nfree)
70
     DO I=1,NFree
71
        WRITE(*,'(12(1X,F8.4))') HessOld(I,1:Idx)
72
     END DO
73
     WRITE(*,*) 'DBG Hinvup_BFGS ds'
74
     WRITE(*,'(12(1X,F8.4))') ds(1:Idx)
75
     WRITE(*,*) 'DBG Hinvup_BFGS dGrad'
76
     WRITE(*,'(12(1X,F8.4))') dGrad(1:Idx)
77
  END IF
78

    
79

    
80

    
81
  NormDS=sqrt(DOT_PRODUCT(DS,DS))
82
  if (NormDS>=1e-6) THEN
83
     IF (debug) WRITE(*,*) 'NormDS=',NormDS
84
     IF (debug) WRITE(*,*) 'DS=',DS
85
     IF (debug) WRITE(*,*) 'DGrad=',DGrad
86
     ! We calculate the denominator (DGrad,DS)
87
     den=DOT_PRODUCT(DGrad,DS)
88

    
89
     if (debug) WRITE(*,*) "(y,s)=(yT)s",den
90

    
91
     ! We calculate Wy
92
     Wy=0
93
     DO I=1,nfree
94
        DO J=1,NFree
95
           Wy(I)=Wy(I)+HessOld(i,j)*DGrad(j)
96
        END DO
97
     END DO
98

    
99
     if (debug) WRITE(*,*) "Wy=",Wy
100

    
101
     ! We calculate Num=Wy(sT)
102
     Num=0.
103
     DO I=1,Nfree
104
        DO J=1,Nfree
105
           Num(I,J)=Wy(i)*Ds(j)
106
        END DO
107
     END DO
108

    
109
     if (debug) THEN
110
        WRITE(*,*) "Wy(sT)"
111
        DO I=1,NFree
112
           WRITE(*,*) Num(I,:)
113
        END DO
114
     END if
115

    
116
     ! We calculate Num2=s(yT)
117
     Num2=0
118
     Do I=1,NFree
119
        DO J=1,NFree
120
           Num2(I,J)=Ds(i)*DGrad(j)
121
        END DO
122
     END DO
123

    
124
     if (debug) THEN
125
        WRITE(*,*) "s(yT)"
126
        DO I=1,NFree
127
           WRITE(*,*) Num2(I,:)
128
        END DO
129
     END if
130

    
131

    
132
     ! we add Num2*W to Num
133
     Do I=1,NFree
134
        DO J=1,NFree
135
           DO K=1,NFree
136
              Num(I,J)=Num(I,J)+Num2(I,K)*HessOld(K,J)
137
           END DO
138
        END DO
139
     END DO
140

    
141
     if (debug) THEN
142
        WRITE(*,*) "Num=s(yT)W+Wy(sT)"
143
        DO I=1,NFree
144
           WRITE(*,*) Num(I,:)
145
        END DO
146
     END if
147

    
148

    
149
     ! we calculate ssT
150
     ssT=0.
151
     DO I=1,Nfree
152
        DO J=1,NFree
153
           ssT(I,J)=Ds(I)*Ds(J)
154
        END DO
155
     END DO
156

    
157
     if (debug) THEN
158
        WRITE(*,*) "s(sT)"
159
        DO I=1,NFree
160
           WRITE(*,*) ssT(I,:)
161
        END DO
162
     END if
163

    
164

    
165
     !We add everything
166
     Hup=(1+DOT_PRODUCT(DGRAD,Wy)/Den)*ssT/Den-Num/Den
167
     Hess=HessOld+Hup
168

    
169
     if (debug) THEN
170
        WRITE(*,*) 'DBG Hinvup_BFGS Hup'
171
        Idx=min(12,Nfree)
172
        DO I=1,NFree
173
           WRITE(*,'(12(1X,F8.4))') Hup(I,1:Idx)
174
        END DO
175
        WRITE(*,*) 'DBG Hinvup_BFGS Hess new'
176
        Idx=min(12,Nfree)
177
        DO I=1,NFree
178
           WRITE(*,'(12(1X,F8.4))') Hess(I,1:Idx)
179
        END DO
180
     END IF
181

    
182
  ELSE
183
     Hess=HessOld
184
  END IF
185
  !     WRITE(*,*) 'New hess'
186
  !     DO I=1,nfree
187
  !        WRITE(*,fmt) Hess(i,:)
188
  !     END DO
189

    
190
  DEALLOCATE(HessOld, Wy,ssT, Hup,HupInt, Num, Num2 )
191

    
192
  if (debug) WRITE(*,*) "=========================== Hinvup_BFGS Over ===================="
193

    
194
END SUBROUTINE Hinvup_BFGS