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

  IMPLICIT NONE

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

  INTEGER(KINT), INTENT(IN) :: NFREE
  REAL(KREAL), INTENT(INOUT) :: Hess(Nfree,Nfree)
  REAL(KREAL), INTENT(IN) :: ds(Nfree)
  REAL(KREAL), INTENT(IN) :: dGrad(Nfree)
  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! This subroutine updates the inverse of the Hessian
! using a BFGS formula.
!
! input:
!  - nfree: number of coordinates
!  - ds: GeomOld-Geom (expressed in the optimization coord.)
!  - dgrad: GradOld-Grad  (expressed in the optimization coord.)
!
!  input/output:
!  - Hess: old Hessian in input, replaced by updated Hessian on output
!
!  Notations for the formula:
! s=Geom-GeomOld  
! y=Grad-GradOld
! W= (H)-1 ie the inverse matrix that we are updating
! W+=W - [s(yT)W+Wy(sT)]/(y,s)+ {1+[(y,Wy)/(y,s)]}(s(sT))/(y,s)
! 
!!!!!!!!!!!!!!!!
!
! Due to the dual properties, this routine is also called to compute
! the DFP update for the Hessian
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


  REAL(KREAL), ALLOCATABLE  :: HessOld(:,:) , Wy(:)
  REAL(KREAL), ALLOCATABLE  :: Hup(:,:),ssT(:,:)
  REAL(KREAL), ALLOCATABLE  :: HupInt(:)
  REAL(KREAL), ALLOCATABLE  :: Num(:,:), Num2(:,:)
  REAL(KREAL) :: Den, NormDS
  INTEGER(KINT) :: i,j,k, Idx

  LOGICAL debug,valid
  EXTERNAL valid

  debug=valid("Hinvup_BFGS")

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

  ALLOCATE(HessOld(nfree,nfree), Hup(nfree,nfree), &
       HupInt(nfree),  &
       Num(nfree,nfree), Num2(nfree,nfree),Wy(Nfree),ssT(Nfree,nFree) )



  HessOld=Hess

  if (debug) THEN
     WRITE(*,*) 'DBG Hinvup_BFGS HessOld'
     Idx=min(12,Nfree)
     DO I=1,NFree
        WRITE(*,'(12(1X,F8.4))') HessOld(I,1:Idx)
     END DO
     WRITE(*,*) 'DBG Hinvup_BFGS ds'
     WRITE(*,'(12(1X,F8.4))') ds(1:Idx)
     WRITE(*,*) 'DBG Hinvup_BFGS dGrad'
     WRITE(*,'(12(1X,F8.4))') dGrad(1:Idx)
  END IF



  NormDS=sqrt(DOT_PRODUCT(DS,DS))
  if (NormDS>=1e-6) THEN
     IF (debug) WRITE(*,*) 'NormDS=',NormDS
     IF (debug) WRITE(*,*) 'DS=',DS
     IF (debug) WRITE(*,*) 'DGrad=',DGrad
     ! We calculate the denominator (DGrad,DS)
     den=DOT_PRODUCT(DGrad,DS)

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

     ! We calculate Wy
     Wy=0
     DO I=1,nfree
        DO J=1,NFree
           Wy(I)=Wy(I)+HessOld(i,j)*DGrad(j)
        END DO
     END DO

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

     ! We calculate Num=Wy(sT)
     Num=0.
     DO I=1,Nfree
        DO J=1,Nfree
           Num(I,J)=Wy(i)*Ds(j)
        END DO
     END DO

     if (debug) THEN
        WRITE(*,*) "Wy(sT)"
        DO I=1,NFree
           WRITE(*,*) Num(I,:)
        END DO
     END if

     ! We calculate Num2=s(yT)
     Num2=0
     Do I=1,NFree
        DO J=1,NFree
           Num2(I,J)=Ds(i)*DGrad(j)
        END DO
     END DO

     if (debug) THEN
        WRITE(*,*) "s(yT)"
        DO I=1,NFree
           WRITE(*,*) Num2(I,:)
        END DO
     END if


     ! we add Num2*W to Num
     Do I=1,NFree
        DO J=1,NFree
           DO K=1,NFree
              Num(I,J)=Num(I,J)+Num2(I,K)*HessOld(K,J)
           END DO
        END DO
     END DO

     if (debug) THEN
        WRITE(*,*) "Num=s(yT)W+Wy(sT)"
        DO I=1,NFree
           WRITE(*,*) Num(I,:)
        END DO
     END if


     ! we calculate ssT
     ssT=0.
     DO I=1,Nfree
        DO J=1,NFree
           ssT(I,J)=Ds(I)*Ds(J)
        END DO
     END DO

     if (debug) THEN
        WRITE(*,*) "s(sT)"
        DO I=1,NFree
           WRITE(*,*) ssT(I,:)
        END DO
     END if


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

     if (debug) THEN
        WRITE(*,*) 'DBG Hinvup_BFGS Hup'
        Idx=min(12,Nfree)
        DO I=1,NFree
           WRITE(*,'(12(1X,F8.4))') Hup(I,1:Idx)
        END DO
        WRITE(*,*) 'DBG Hinvup_BFGS Hess new'
        Idx=min(12,Nfree)
        DO I=1,NFree
           WRITE(*,'(12(1X,F8.4))') Hess(I,1:Idx)
        END DO
     END IF

  ELSE
     Hess=HessOld
  END IF
  !     WRITE(*,*) 'New hess'
  !     DO I=1,nfree
  !        WRITE(*,fmt) Hess(i,:)
  !     END DO

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

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

END SUBROUTINE Hinvup_BFGS
