subroutine add_core_states_currentdets(run)
! And if the state is already present, simply set its flag.
! Also sort the states afterwards.
use DetBitOps, only: ilut_lt, ilut_gt, DetBitLT
use searching, only: BinSearchParts
use sort_mod, only: sort
integer, intent(in) :: run
integer :: i, comp, MinInd, PartInd, nwalkers
logical :: tSuccess
associate(rep => cs_replicas(run))
MinInd = 1
nwalkers = int(TotWalkers)
do i = 1, rep%determ_sizes(iProcIndex)
if (nwalkers > 0) then
! If there is only one state in CurrentDets to check then BinSearchParts doesn't
! return the desired value for PartInd, so do this separately...
if (MinInd == nwalkers) then
comp = DetBitLT(CurrentDets(:, MinInd), SpawnedParts(0:NIfTot, i), nifd)
if (comp == 0) then
tSuccess = .true.
PartInd = MinInd
else if (comp == 1) then
tSuccess = .false.
PartInd = MinInd
else if (comp == -1) then
tSuccess = .false.
PartInd = MinInd - 1
end if
else
call BinSearchParts(SpawnedParts(:, i), MinInd, nwalkers, PartInd, tSuccess)
end if
else
tSuccess = .false.
PartInd = 0
end if
if (tSuccess) then
call set_flag(CurrentDets(:, PartInd), flag_deterministic(run))
if (tTruncInitiator .and. t_core_inits) then
call set_flag(CurrentDets(:, PartInd), get_initiator_flag_by_run(run))
end if
MinInd = PartInd
else
! Move all states below PartInd down one and insert the new state in the slot.
CurrentDets(:, PartInd + 2:nwalkers + 1) = CurrentDets(:, PartInd + 1:nwalkers)
CurrentDets(:, PartInd + 1) = SpawnedParts(0:NIfTot, i)
nwalkers = nwalkers + 1
MinInd = PartInd + 1
end if
end do
end associate
call sort(CurrentDets(:, 1:nwalkers), ilut_lt, ilut_gt)
TotWalkers = int(nwalkers, int64)
end subroutine add_core_states_currentdets