subroutine write_most_pop_core_at_end(target_space_size)
! Write the most populated states in CurrentDets to a DETFILE file,
! using the routine generate_space_most_populated, which is the same
! routine used by the pops-core semi-stochastic input option. So this
! routine basically generates a pops-core space, but can be used at the
! end of a calculation, rather than at the start.
integer, intent(in) :: target_space_size
integer :: i, j, k, ierr, iunit
integer :: space_size
logical :: texist
character(*), parameter :: t_r = "write_most_pop_core_at_end"
write(stdout, '(/,"Finding most populated states...")'); call neci_flush(stdout)
space_size = 0
! Calling this routine will find the most populated states in
! CurrentDets and copy them across to SpawnedParts, to the first
! space_size slots in it (overwriting anything which was there before,
! which presumably won't be needed now).
call generate_space_most_populated(target_space_size, .false., 0, &
SpawnedParts(0:niftot, 1 : ), space_size, core_run)
write(stdout, '("Writing the most populated states to DETFILE...")'); call neci_flush(stdout)
iunit = get_free_unit()
! Let each process write its states to the file. Each process waits for
! the process before it to finish before starting.
do i = 0, nProcessors - 1
if (iProcIndex == i) then
if (i == 0) then
open(iunit, file='DETFILE', status='replace')
else
inquire (file='DETFILE', exist=texist)
if (.not. texist) call stop_all(t_r, '"DETFILE" file cannot be found')
open(iunit, file='DETFILE', status='old', position='append')
end if
do j = 1, space_size
do k = 0, nifd
write(iunit, '(i24)', advance='no') SpawnedParts(k, j)
end do
write(iunit, *)
end do
close(iunit)
end if
call MPIBarrier(ierr)
end do
end subroutine write_most_pop_core_at_end