subroutine calc_hamil_contribs_diag(nvecs, krylov_array, ndets, h_matrix, h_diag)
use bit_rep_data, only: IlutBits
use FciMCData, only: Hii
use core_space_util, only: cs_replicas
use global_det_data, only: det_diagH
use kp_fciqmc_data_mod, only: tSemiStochasticKPHamil
use Parallel_neci, only: iProcIndex
use SystemData, only: nel
integer, intent(in) :: nvecs
integer(n_int), intent(in) :: krylov_array(0:, :)
integer, intent(in) :: ndets
real(dp), intent(inout) :: h_matrix(:, :)
real(dp), intent(in), optional :: h_diag(:)
integer :: idet, i, j, min_idet
integer :: nI_spawn(nel)
integer(n_int) :: int_sign(lenof_all_signs)
real(dp) :: real_sign(lenof_all_signs)
real(dp) :: h_diag_elem
! In semi-stochastic calculations the diagonal elements of the core space
! are taken care of in the core Hamiltonian calculation, so skip them here.
! Core determinants are always kept at the top of the list, so they're simple
! to skip.
if (tSemiStochasticKPHamil) then
min_idet = cs_replicas(1)%determ_sizes(iProcIndex) + 1
else
min_idet = 1
end if
do idet = min_idet, ndets
int_sign = krylov_array(IlutBits%ind_pop:IlutBits%ind_pop + lenof_all_signs - 1, idet)
real_sign = transfer(int_sign, real_sign)
if (present(h_diag)) then
h_diag_elem = h_diag(idet) + Hii
else
h_diag_elem = det_diagH(idet) + Hii
end if
! Finally, add in the contribution to the projected Hamiltonian for each pair of Krylov vectors.
do i = 1, nvecs
do j = i, nvecs
h_matrix(i, j) = h_matrix(i, j) + &
h_diag_elem * (real_sign(kp_ind_1(i)) * real_sign(kp_ind_2(j)) + &
real_sign(kp_ind_2(i)) * real_sign(kp_ind_1(j))) / 2.0_dp
end do
end do
end do
end subroutine calc_hamil_contribs_diag