subroutine generate_sing_doub_guga(ilut_list, space_size, only_keep_conn)
! routine to generate the singles and doubles core space from the
! HF (or current reference determinant) used in the semi-stochastic
! code when GUGA is in use
integer(n_int), intent(inout) :: ilut_list(0:, :)
integer, intent(inout) :: space_size
logical, intent(in) :: only_keep_conn
integer(n_int) :: ilutG(0:nifguga)
integer(n_int), allocatable :: excitations(:, :)
integer :: nexcit, i
integer(n_int) :: temp_ilut(0:niftot)
HElement_t(dp) :: temp_hel
integer :: temp_nI(nel)
! essentially use the acthamiltonian on the HFdet and since i
! only keep non-zero matrix elements anyway as output, the
! only_keep_conn is kind of implicitly .true. always..
call add_state_to_space(ilutHF, ilut_list, space_size)
! to the exact guga excitation to the HF det
call convert_ilut_toGUGA(ilutHF, ilutG)
call actHamiltonian(ilutG, CSF_Info_t(ilutG), excitations, nexcit)
do i = 1, nexcit
! check if matrix element is zero if we only want to keep the
! connected determinants(not sure if thats implicitly done in the
! actHamiltonian routine(i think it only keeps non-zero excitations
call convert_ilut_toNECI(excitations(:, i), temp_ilut, temp_hel)
if (only_keep_conn .and. near_zero(temp_hel)) cycle
call decode_bit_det(temp_nI, temp_ilut)
call add_state_to_space(temp_ilut, ilut_list, space_size, temp_nI)
end do
end subroutine generate_sing_doub_guga