Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased interpolate speed #2859

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/Fields/interpolate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@ vec[low] <= val && vec[high] >= val

using a binary search. The input array `vec` has to be monotonically increasing.

Code credit: https://computersciencehub.io/julia/code-for-binary-search-algorithm-julia
Code credit: https://gist.github.com/cuongld2/8e4fed9ba44ea2b4598f90e7d5b6c612/155f9cb595314c8db3a266c3316889443b068017
"""
@inline function index_binary_search(vec, val, array_size)
if issorted(vec)
low = 0
high = array_size - 1

while low + 1 < high
mid = middle_point(low, high)
if vec[mid + 1] == val
return (mid + 1, mid + 1)
elseif vec[mid + 1] < val
low = mid
else
high = mid
end
low = 0
high = array_size - 1

while low + 1 < high
mid = middle_point(low, high)
if @inbounds vec[mid + 1] == val
return (mid + 1, mid + 1)
elseif @inbounds vec[mid + 1] < val
low = mid
else
high = mid
end

return (low + 1, high + 1)
else
throw(error("Vector not sorted, unable to search value"))
end

return (low + 1, high + 1)
end

@inline function fractional_index(array_size::Int, val::FT, vec) where {FT}
Expand Down