pick_from_cum_list Subroutine

public subroutine pick_from_cum_list(cum_arr, cum_sum, ind, pgen)

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: cum_arr(:)
real(kind=dp), intent(in) :: cum_sum
integer, intent(out) :: ind
real(kind=dp), intent(out) :: pgen

Contents

Source Code


Source Code

    subroutine pick_from_cum_list(cum_arr, cum_sum, ind, pgen)
        real(dp), intent(in) :: cum_arr(:), cum_sum
        integer, intent(out) :: ind
        real(dp), intent(out) :: pgen
        real(dp) :: r

        if (cum_sum < EPS) then
            ind = -1
            pgen = 0.0_dp
            return
        end if

        r = genrand_real2_dsfmt() * cum_sum

        ind = binary_search_first_ge(cum_arr, r)

        if (ind == 1) then
            pgen = cum_arr(1) / cum_sum
        else
            pgen = (cum_arr(ind) - cum_arr(ind - 1)) / cum_sum
        end if

    end subroutine pick_from_cum_list