root / src / Hinvup_BFGS.f90 @ 2
Historique | Voir | Annoter | Télécharger (4,47 ko)
| 1 | 1 | pfleura2 | ! This program reads an old hessian, old forces, new |
|---|---|---|---|
| 2 | 1 | pfleura2 | ! forces and updates the hessian matrix inverse |
| 3 | 1 | pfleura2 | ! using the BFGS scheme. |
| 4 | 1 | pfleura2 | ! |
| 5 | 1 | pfleura2 | ! This routines has been modified so that its input is now: |
| 6 | 1 | pfleura2 | SUBROUTINE Hinvup_BFGS(Nfree,ds,dgrad,Hess) |
| 7 | 1 | pfleura2 | |
| 8 | 1 | pfleura2 | IMPLICIT NONE |
| 9 | 1 | pfleura2 | |
| 10 | 1 | pfleura2 | INTEGER, PARAMETER :: KINT=KIND(1) |
| 11 | 1 | pfleura2 | INTEGER, PARAMETER :: KREAL=KIND(1.D0) |
| 12 | 1 | pfleura2 | |
| 13 | 1 | pfleura2 | INTEGER(KINT), INTENT(IN) :: NFREE |
| 14 | 1 | pfleura2 | REAL(KREAL), INTENT(INOUT) :: Hess(Nfree,Nfree) |
| 15 | 1 | pfleura2 | REAL(KREAL), INTENT(IN) :: ds(Nfree) |
| 16 | 1 | pfleura2 | REAL(KREAL), INTENT(IN) :: dGrad(Nfree) |
| 17 | 1 | pfleura2 | |
| 18 | 1 | pfleura2 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 19 | 1 | pfleura2 | ! |
| 20 | 1 | pfleura2 | ! This subroutine updates the inverse of the Hessian |
| 21 | 1 | pfleura2 | ! using a BFGS formula. |
| 22 | 1 | pfleura2 | ! |
| 23 | 1 | pfleura2 | ! input: |
| 24 | 1 | pfleura2 | ! - nfree: number of coordinates |
| 25 | 1 | pfleura2 | ! - ds: GeomOld-Geom (expressed in the optimization coord.) |
| 26 | 1 | pfleura2 | ! - dgrad: GradOld-Grad (expressed in the optimization coord.) |
| 27 | 1 | pfleura2 | ! |
| 28 | 1 | pfleura2 | ! input/output: |
| 29 | 1 | pfleura2 | ! - Hess: old Hessian in input, replaced by updated Hessian on output |
| 30 | 1 | pfleura2 | ! |
| 31 | 1 | pfleura2 | ! Notations for the formula: |
| 32 | 1 | pfleura2 | ! s=Geom-GeomOld |
| 33 | 1 | pfleura2 | ! y=Grad-GradOld |
| 34 | 1 | pfleura2 | ! W= (H)-1 ie the inverse matrix that we are updating |
| 35 | 1 | pfleura2 | ! W+=W - [s(yT)W+Wy(sT)]/(y,s)+ {1+[(y,Wy)/(y,s)]}(s(sT))/(y,s)
|
| 36 | 1 | pfleura2 | ! |
| 37 | 1 | pfleura2 | !!!!!!!!!!!!!!!! |
| 38 | 1 | pfleura2 | ! |
| 39 | 1 | pfleura2 | ! Due to the dual properties, this routine is also called to compute |
| 40 | 1 | pfleura2 | ! the DFP update for the Hessian |
| 41 | 1 | pfleura2 | ! |
| 42 | 1 | pfleura2 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 43 | 1 | pfleura2 | |
| 44 | 1 | pfleura2 | |
| 45 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: HessOld(:,:) , Wy(:) |
| 46 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: Hup(:,:),ssT(:,:) |
| 47 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: HupInt(:) |
| 48 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: Num(:,:), Num2(:,:) |
| 49 | 2 | pfleura2 | REAL(KREAL) :: Den, NormDS |
| 50 | 1 | pfleura2 | INTEGER(KINT) :: i,j,k, Idx |
| 51 | 1 | pfleura2 | |
| 52 | 1 | pfleura2 | LOGICAL debug,valid |
| 53 | 1 | pfleura2 | EXTERNAL valid |
| 54 | 1 | pfleura2 | |
| 55 | 1 | pfleura2 | debug=valid("Hinvup_BFGS")
|
| 56 | 1 | pfleura2 | |
| 57 | 1 | pfleura2 | if (debug) WRITE(*,*) "=========================== Entering Hinvup_BFGS ====================" |
| 58 | 1 | pfleura2 | |
| 59 | 1 | pfleura2 | ALLOCATE(HessOld(nfree,nfree), Hup(nfree,nfree), & |
| 60 | 1 | pfleura2 | HupInt(nfree), & |
| 61 | 1 | pfleura2 | Num(nfree,nfree), Num2(nfree,nfree),Wy(Nfree),ssT(Nfree,nFree) ) |
| 62 | 1 | pfleura2 | |
| 63 | 1 | pfleura2 | |
| 64 | 1 | pfleura2 | |
| 65 | 1 | pfleura2 | HessOld=Hess |
| 66 | 1 | pfleura2 | |
| 67 | 1 | pfleura2 | if (debug) THEN |
| 68 | 1 | pfleura2 | WRITE(*,*) 'DBG Hinvup_BFGS HessOld' |
| 69 | 1 | pfleura2 | Idx=min(12,Nfree) |
| 70 | 1 | pfleura2 | DO I=1,NFree |
| 71 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') HessOld(I,1:Idx) |
| 72 | 1 | pfleura2 | END DO |
| 73 | 1 | pfleura2 | WRITE(*,*) 'DBG Hinvup_BFGS ds' |
| 74 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') ds(1:Idx) |
| 75 | 1 | pfleura2 | WRITE(*,*) 'DBG Hinvup_BFGS dGrad' |
| 76 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') dGrad(1:Idx) |
| 77 | 1 | pfleura2 | END IF |
| 78 | 1 | pfleura2 | |
| 79 | 1 | pfleura2 | |
| 80 | 1 | pfleura2 | |
| 81 | 1 | pfleura2 | NormDS=sqrt(DOT_PRODUCT(DS,DS)) |
| 82 | 1 | pfleura2 | if (NormDS>=1e-6) THEN |
| 83 | 1 | pfleura2 | IF (debug) WRITE(*,*) 'NormDS=',NormDS |
| 84 | 1 | pfleura2 | IF (debug) WRITE(*,*) 'DS=',DS |
| 85 | 1 | pfleura2 | IF (debug) WRITE(*,*) 'DGrad=',DGrad |
| 86 | 1 | pfleura2 | ! We calculate the denominator (DGrad,DS) |
| 87 | 1 | pfleura2 | den=DOT_PRODUCT(DGrad,DS) |
| 88 | 1 | pfleura2 | |
| 89 | 1 | pfleura2 | if (debug) WRITE(*,*) "(y,s)=(yT)s",den |
| 90 | 1 | pfleura2 | |
| 91 | 1 | pfleura2 | ! We calculate Wy |
| 92 | 1 | pfleura2 | Wy=0 |
| 93 | 1 | pfleura2 | DO I=1,nfree |
| 94 | 1 | pfleura2 | DO J=1,NFree |
| 95 | 1 | pfleura2 | Wy(I)=Wy(I)+HessOld(i,j)*DGrad(j) |
| 96 | 1 | pfleura2 | END DO |
| 97 | 1 | pfleura2 | END DO |
| 98 | 1 | pfleura2 | |
| 99 | 1 | pfleura2 | if (debug) WRITE(*,*) "Wy=",Wy |
| 100 | 1 | pfleura2 | |
| 101 | 1 | pfleura2 | ! We calculate Num=Wy(sT) |
| 102 | 1 | pfleura2 | Num=0. |
| 103 | 1 | pfleura2 | DO I=1,Nfree |
| 104 | 1 | pfleura2 | DO J=1,Nfree |
| 105 | 1 | pfleura2 | Num(I,J)=Wy(i)*Ds(j) |
| 106 | 1 | pfleura2 | END DO |
| 107 | 1 | pfleura2 | END DO |
| 108 | 1 | pfleura2 | |
| 109 | 1 | pfleura2 | if (debug) THEN |
| 110 | 1 | pfleura2 | WRITE(*,*) "Wy(sT)" |
| 111 | 1 | pfleura2 | DO I=1,NFree |
| 112 | 1 | pfleura2 | WRITE(*,*) Num(I,:) |
| 113 | 1 | pfleura2 | END DO |
| 114 | 1 | pfleura2 | END if |
| 115 | 1 | pfleura2 | |
| 116 | 1 | pfleura2 | ! We calculate Num2=s(yT) |
| 117 | 1 | pfleura2 | Num2=0 |
| 118 | 1 | pfleura2 | Do I=1,NFree |
| 119 | 1 | pfleura2 | DO J=1,NFree |
| 120 | 1 | pfleura2 | Num2(I,J)=Ds(i)*DGrad(j) |
| 121 | 1 | pfleura2 | END DO |
| 122 | 1 | pfleura2 | END DO |
| 123 | 1 | pfleura2 | |
| 124 | 1 | pfleura2 | if (debug) THEN |
| 125 | 1 | pfleura2 | WRITE(*,*) "s(yT)" |
| 126 | 1 | pfleura2 | DO I=1,NFree |
| 127 | 1 | pfleura2 | WRITE(*,*) Num2(I,:) |
| 128 | 1 | pfleura2 | END DO |
| 129 | 1 | pfleura2 | END if |
| 130 | 1 | pfleura2 | |
| 131 | 1 | pfleura2 | |
| 132 | 1 | pfleura2 | ! we add Num2*W to Num |
| 133 | 1 | pfleura2 | Do I=1,NFree |
| 134 | 1 | pfleura2 | DO J=1,NFree |
| 135 | 1 | pfleura2 | DO K=1,NFree |
| 136 | 1 | pfleura2 | Num(I,J)=Num(I,J)+Num2(I,K)*HessOld(K,J) |
| 137 | 1 | pfleura2 | END DO |
| 138 | 1 | pfleura2 | END DO |
| 139 | 1 | pfleura2 | END DO |
| 140 | 1 | pfleura2 | |
| 141 | 1 | pfleura2 | if (debug) THEN |
| 142 | 1 | pfleura2 | WRITE(*,*) "Num=s(yT)W+Wy(sT)" |
| 143 | 1 | pfleura2 | DO I=1,NFree |
| 144 | 1 | pfleura2 | WRITE(*,*) Num(I,:) |
| 145 | 1 | pfleura2 | END DO |
| 146 | 1 | pfleura2 | END if |
| 147 | 1 | pfleura2 | |
| 148 | 1 | pfleura2 | |
| 149 | 1 | pfleura2 | ! we calculate ssT |
| 150 | 1 | pfleura2 | ssT=0. |
| 151 | 1 | pfleura2 | DO I=1,Nfree |
| 152 | 1 | pfleura2 | DO J=1,NFree |
| 153 | 1 | pfleura2 | ssT(I,J)=Ds(I)*Ds(J) |
| 154 | 1 | pfleura2 | END DO |
| 155 | 1 | pfleura2 | END DO |
| 156 | 1 | pfleura2 | |
| 157 | 1 | pfleura2 | if (debug) THEN |
| 158 | 1 | pfleura2 | WRITE(*,*) "s(sT)" |
| 159 | 1 | pfleura2 | DO I=1,NFree |
| 160 | 1 | pfleura2 | WRITE(*,*) ssT(I,:) |
| 161 | 1 | pfleura2 | END DO |
| 162 | 1 | pfleura2 | END if |
| 163 | 1 | pfleura2 | |
| 164 | 1 | pfleura2 | |
| 165 | 1 | pfleura2 | !We add everything |
| 166 | 1 | pfleura2 | Hup=(1+DOT_PRODUCT(DGRAD,Wy)/Den)*ssT/Den-Num/Den |
| 167 | 1 | pfleura2 | Hess=HessOld+Hup |
| 168 | 1 | pfleura2 | |
| 169 | 1 | pfleura2 | if (debug) THEN |
| 170 | 1 | pfleura2 | WRITE(*,*) 'DBG Hinvup_BFGS Hup' |
| 171 | 1 | pfleura2 | Idx=min(12,Nfree) |
| 172 | 1 | pfleura2 | DO I=1,NFree |
| 173 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') Hup(I,1:Idx) |
| 174 | 1 | pfleura2 | END DO |
| 175 | 1 | pfleura2 | WRITE(*,*) 'DBG Hinvup_BFGS Hess new' |
| 176 | 1 | pfleura2 | Idx=min(12,Nfree) |
| 177 | 1 | pfleura2 | DO I=1,NFree |
| 178 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') Hess(I,1:Idx) |
| 179 | 1 | pfleura2 | END DO |
| 180 | 1 | pfleura2 | END IF |
| 181 | 1 | pfleura2 | |
| 182 | 1 | pfleura2 | ELSE |
| 183 | 1 | pfleura2 | Hess=HessOld |
| 184 | 1 | pfleura2 | END IF |
| 185 | 1 | pfleura2 | ! WRITE(*,*) 'New hess' |
| 186 | 1 | pfleura2 | ! DO I=1,nfree |
| 187 | 1 | pfleura2 | ! WRITE(*,fmt) Hess(i,:) |
| 188 | 1 | pfleura2 | ! END DO |
| 189 | 1 | pfleura2 | |
| 190 | 1 | pfleura2 | DEALLOCATE(HessOld, Wy,ssT, Hup,HupInt, Num, Num2 ) |
| 191 | 1 | pfleura2 | |
| 192 | 1 | pfleura2 | if (debug) WRITE(*,*) "=========================== Hinvup_BFGS Over ====================" |
| 193 | 1 | pfleura2 | |
| 194 | 1 | pfleura2 | END SUBROUTINE Hinvup_BFGS |