
! Copyright (C) 2019 J. K. Dewhurst and S. Sharma.
! This file is distributed under the terms of the GNU General Public License.
! See the file COPYING for license details.

subroutine occupyuv
use modmain
use modbog
use modmpi
implicit none
integer ik,ist
real(8) chg,w,vn
real(8) e0,e1,e
! determine the total charge and fermionic anomalous correlation entropy
chg=0.d0
face=0.d0
do ik=1,nkpt
  w=wkpt(ik)
  do ist=1,nstsv
    vn=vnorm(ist,ik)
    chg=chg+w*vn
    if ((vn.gt.0.d0).and.(vn.lt.1.d0)) then
      face=face+w*(vn*log(vn)+(1.d0-vn)*log(1.d0-vn))
    end if
  end do
end do
chg=occmax*chg
face=-occmax*face
! adjust the Fermi energy
efermi=efermi+tauefm*(chgval-chg)
if (mp_mpi) then
  if (abs(chg-chgval).gt.epschg) then
    write(*,*)
    write(*,'("Warning(occupyuv): incorrect charge : ",2G18.10)') chg,chgval
  end if
end if
! estimate the indirect band gap
e0=-1.d8
e1=1.d8
ikgap(1)=1
ikgap(2)=1
do ist=1,nstsv
  do ik=1,nkpt
    e=evaluv(ist,ik)
    if (e.lt.0.d0) then
      if (e.gt.e0) then
        e0=e
        ikgap(1)=ik
      end if
    else
      if (e.lt.e1) then
        e1=e
        ikgap(2)=ik
      end if
    end if
  end do
end do
bandgap(1)=e1-e0
end subroutine

