subroutine mrinivec(infos,ea,eb,bvec_mo,xm,nvec)
use precision, only: dp
use io_constants, only: iw
use types, only: information
implicit none
type(information), intent(in) :: infos
real(kind=dp), intent(in), dimension(:) :: ea, eb
real(kind=dp), intent(out), dimension(:,:) :: bvec_mo
real(kind=dp), intent(out), dimension(:) :: xm
integer, intent(in) :: nvec
logical :: debug_mode
real(kind=dp) :: xmj
integer :: nocca, nbf, i, ij, j, k, xvec_dim, lr1, lr2, mrst
integer :: itmp(nvec)
real(kind=dp) :: xtmp(nvec)
debug_mode = infos%tddft%debug_mode
nbf = infos%basis%nbf
nocca = infos%mol_prop%nelec_A
xvec_dim = ubound(xm, 1)
lr1 = nocca-1
lr2 = nocca
! For Singlet or Triplet
mrst = infos%tddft%mult
! Set xm(xvec_dim)
ij = 0
do j = lr1, nbf
do i = 1, lr2
if (i==lr1 .and. j==lr1) then
ij = ij+1
xm(ij) = (eb(lr1)-ea(lr1)+eb(lr2)-ea(lr2))*0.5_dp
cycle
end if
ij = ij+1
xm(ij) = eb(j)-ea(i)
if (i==nocca .and. j==nocca) then
xm(ij) = huge(1.0d0)
else if (mrst==3 .and. i==nocca .and. j==nocca-1) then
xm(ij) = huge(1.0d0)
else if (mrst==3 .and. i==nocca-1 .and. j==nocca) then
xm(ij) = huge(1.0d0)
end if
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, xvec_dim
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
! Ordering xm(xvec_dim): xm(small) <= xm(large)
! Get smaller diagonal values
do j = 1, xvec_dim-1
do i = j+1, xvec_dim
if (xm(j)<=xm(i)) cycle
xmj = xm(j)
xm(j) = xm(i)
xm(i) = xmj
end do
end do
if (debug_mode) then
write(iw,'("print xm(xvec_dim) ordering")')
do i = 1, xvec_dim
write(iw,'(a,i5,f20.10,i5)') 'i,xm(ij)=', i, xm(i)
end do
end if
! 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
! set xm(xvec_dim) again
ij = 0
do j = lr1, nbf
do i = 1, lr2
if (i==lr1 .and. j==lr1) then
ij = ij+1
xm(ij) = (eb(lr1)-ea(lr1)+eb(lr2)-ea(lr2))*0.5_dp
cycle
endif
ij = ij+1
xm(ij) = eb(j)-ea(i)
if (i==nocca .and. j==nocca) then
xm(ij) = 9d99
else if (mrst==3 .and. i==nocca .and. j==nocca-1) then
xm(ij) = 9d99
else if (mrst==3 .and. i==nocca-1 .and. j==nocca) then
xm(ij) = 9d99
end if
end do
end do
if (debug_mode) then
write(iw,'("print xm(xvec_dim) ordering")')
do i = 1, xvec_dim
write(iw,'(a,i5,f20.10,i5)') 'i,xm(ij)=', i, xm(i)
end do
end if
return
end subroutine mrinivec