! This program reads an old hessian, old forces, new forces
! and print the updated hessian and old coordinates.
! Instead of the Murtagh-Sargent update, we use the Bofill update
! which is an average of Murtagh-Sargent and Powell-symmetryc-Broyden
! cf JCP 1999, vol 111 (19), p 8773

! This subroutine slightly differs from previous ones as
! it is written with the Gradient as input and not the forces
! Using the relation F=-Grad, one can easily go from older
! versions to this one (which more closely mimics the JCP article)

SUBROUTINE Hupdate_Bofill(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)


  REAL(KREAL), ALLOCATABLE  :: HessOld(:,:) , HupMS(:,:)
  REAL(KREAL), ALLOCATABLE  :: HupPSB(:,:), Hup(:,:)
  REAL(KREAL), ALLOCATABLE  :: HupInt(:)
  REAL(KREAL), ALLOCATABLE  :: HS(:), Num(:,:), Num2(:,:)
  REAL(KREAL) :: Phi, Num3, Den, Den2,NormDS
  CHARACTER(120) :: fmt,fmt2,Line
  INTEGER(KINT) :: i,j, idx

  LOGICAL debug,valid
  EXTERNAL valid

  debug=valid("Hupdate").OR.Valid("Hupdate_bofill")

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

  ALLOCATE(HessOld(nfree,nfree), HupMS(nfree,nfree),  &
       HupPSB(nfree,nfree), Hup(nfree,nfree), &
       HupInt(nfree), HS(nfree),  &
       Num(nfree,nfree), Num2(nfree,nfree) )

  HessOld=Hess

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

  NormDS=sqrt(DOT_PRODUCT(DS,DS))
  if (NormDS>=1e-6) THEN

     HS=0
     DO I=1,nfree
        DO J=1,nfree
           HS(I)=HS(I)+HessOld(i,j)*Ds(j)
        END DO
     END DO
     HupInt=DGrad-HS

     ! WE calculate the MS update
     Num=0.
     Den=0.
     DO I=1,nfree
        DO J=1,nfree
           Num(I,J)=HUpInt(i)*HupInt(j)
        END DO
        Den=Den+Hupint(i)*Ds(i)
     END DO

     HupMS=Num/Den

     ! Now we calculate the PSB update
     Num=0
     Num2=0
     Num3=0
     Den=0
     DO I=1,nfree
        DO J=1,nfree
           Num(I,J)=HUpInt(i)*Ds(j)+Ds(i)*HupInt(j)
           Num2(I,J)=DS(i)*DS(j)
        END DO
        Den=Den+Ds(i)*Ds(i)
        Num3=Num3+DS(i)*HupInt(i)
     END DO
     HupPSB=Num/Den-Num3*Num2/(Den)**2

     ! We calculate Phi
     Den2=0.
     Do I=1,nfree
        Den2=Den2+HupInt(i)*HupInt(i)
     END DO
     Phi=Num3**2/(Den*Den2)

     Hup=Phi*HupMS+(1-Phi)*HupPSB

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

  DEALLOCATE(HessOld, HupMS, HupPSB, Hup, &
       HupInt, HS, Num, Num2 )

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

END SUBROUTINE Hupdate_Bofill
