subroutine inivec(eiga,eigb,bvec_mo,xm, &
nocca,noccb,nvec)
use precision, only: dp
implicit none
real(kind=dp), intent(in), dimension(:) :: eiga, eigb
real(kind=dp), intent(out), dimension(:,:) :: bvec_mo
real(kind=dp), intent(out), dimension(:) :: xm
integer, intent(in) :: nocca, noccb
integer, intent(in) :: nvec
integer :: i, ij, j, k, nbf, mxvec
integer :: itmp(nvec)
real(kind=dp) :: xtmp(nvec)
nbf = ubound(eiga, 1)
mxvec = ubound(bvec_mo, 2)
! -- Set xm(xvec_dim)
do j = noccb+1, nbf
do i = 1, nocca
ij = (j-noccb-1)*nocca+i
xm(ij) = eigb(j)-eiga(i)
end do
end do
! Find indices of the first `nvec` smallest values in the `xm` array
itmp = 0 ! indices
xtmp = huge(1.0d0) ! values
do i = 1, ubound(xm,1)
do j = 1, nvec
if (xtmp(j) > xm(i)) exit
end do
if (j <= nvec) then
! new small value found, insert it into temporary arrays
xtmp(j+1:nvec) = xtmp(j:nvec-1)
itmp(j+1:nvec) = itmp(j:nvec-1)
xtmp(j) = xm(i)
itmp(j) = i
end if
end do
! -- Get initial vectors: bvec(xvec_dim,nvec)
bvec_mo = 0.0_dp
do k = 1, nvec
bvec_mo(itmp(k),k) = 1.0_dp
end do
end subroutine inivec