find Function

private pure function find(x, xvec) result(i)

Binary search to find index i such that xvec(i) <= x < xvec(i+1) Assumes xvec is sorted in ascending order

Note

Replaces the simple loop in the original code.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x
real(kind=wp), intent(in), dimension(:) :: xvec

Return Value integer


Called by

proc~~find~~CalledByGraph proc~find find proc~interpolation_index interpolation_index proc~interpolation_index->proc~find proc~mean_molecular_weight_ratio_lower mean_molecular_weight_ratio_lower proc~mean_molecular_weight_ratio_lower->proc~find proc~pressure_lower pressure_lower proc~pressure_lower->proc~find proc~tm Tm proc~tm->proc~find proc~coesa_atmosphere COESA_atmosphere proc~coesa_atmosphere->proc~pressure_lower proc~mean_molecular_weight_lower mean_molecular_weight_lower proc~coesa_atmosphere->proc~mean_molecular_weight_lower proc~mean_molecular_weight_upper mean_molecular_weight_upper proc~coesa_atmosphere->proc~mean_molecular_weight_upper proc~pressure_upper pressure_upper proc~coesa_atmosphere->proc~pressure_upper proc~temperature_lower temperature_lower proc~coesa_atmosphere->proc~temperature_lower proc~speed_of_sound_86km speed_of_sound_86km proc~coesa_atmosphere->proc~speed_of_sound_86km proc~coesa_density COESA_density proc~coesa_density->proc~pressure_lower proc~coesa_density->proc~mean_molecular_weight_lower proc~coesa_density->proc~mean_molecular_weight_upper proc~coesa_density->proc~pressure_upper proc~coesa_density->proc~temperature_lower proc~mean_molecular_weight_lower->proc~mean_molecular_weight_ratio_lower proc~mean_molecular_weight_upper->proc~interpolation_index proc~pressure_upper->proc~interpolation_index proc~temperature_lower->proc~tm proc~speed_of_sound_86km->proc~mean_molecular_weight_lower proc~speed_of_sound_86km->proc~temperature_lower

Source Code

pure function find(x, xvec) result(i)
    !! Binary search to find index i such that xvec(i) <= x < xvec(i+1)
    !! Assumes xvec is sorted in ascending order
    !!
    !!@note Replaces the simple loop in the original code.
    real(wp),intent(in) :: x
    real(wp),dimension(:),intent(in) :: xvec
    integer :: i
    integer :: left, right, mid

    left = 1
    right = size(xvec)

    ! Handle edge cases
    if (x <= xvec(1)) then
        i = 1
        return
    end if
    if (x > xvec(right)) then
        i = right
        return
    end if

    ! Binary search
    do while (right - left > 1)
        mid = (left + right) / 2
        if (x < xvec(mid)) then
            right = mid
        else
            left = mid
        end if
    end do

    i = left
end function find