subroutine scale_population(walker_list, ndets, target_pop, input_pop, scaling_factor)
! Take an input list of walkers, find the total walker population in
! the list, and then multiply all the walker signs by some factor in
! order for the walker list to have the requested target population.
integer(n_int), intent(inout) :: walker_list(:, :)
integer(int64), intent(in) :: ndets
real(dp), intent(in) :: target_pop
real(dp), intent(out) :: input_pop(lenof_sign_kp)
real(dp), intent(out) :: scaling_factor
integer(int64) :: i
real(dp) :: real_sign(lenof_sign_kp), all_input_pop(lenof_sign_kp)
input_pop = 0.0_dp
! First, find the population of the walkers in walker_list.
do i = 1, ndets
call extract_sign(walker_list(:, i), real_sign)
input_pop = input_pop + abs(real_sign)
end do
call MPISumAll(input_pop, all_input_pop)
! Just use the first particle type to determine the scaing factor.
scaling_factor = target_pop / all_input_pop(1)
! Now multiply the walker signs by scaling_factor.
do i = 1, ndets
call extract_sign(walker_list(:, i), real_sign)
real_sign = real_sign * scaling_factor
call encode_sign(walker_list(:, i), real_sign)
end do
end subroutine scale_population