-
-
Notifications
You must be signed in to change notification settings - Fork 6
Generalise _interpolate! to allow mixed dimensions
#33
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
Open
rafaqz
wants to merge
28
commits into
SciML:main
Choose a base branch
from
rafaqz:generalis_interpolate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d37c715
gneralise interpolate to allow mixed dimensions
rafaqz b36c2d9
cleanup
rafaqz ad6bdaa
simpler type, single _interpolat! method
rafaqz 3b6aa64
bugfix
rafaqz 2e8b635
handle empty dimensions with NoInterpolationDimension
rafaqz c4595d2
more fixes for mixed dimensions
rafaqz b384f41
more mixed dims
rafaqz d6d652d
cleanup
rafaqz 87dd1ab
tweaks
rafaqz 107dd45
working besides Constant nans
rafaqz 9061c83
fix ConstantInterpolationDimension nans
rafaqz 0bfe73c
remove constant
rafaqz 42e6d68
fix eval grid checks
rafaqz 60c36a2
simplify and minimise changes
rafaqz cb2864d
rename scale to weight
rafaqz c010f30
cleanup grid_size
rafaqz 5a87b17
explicity multi_point_index
rafaqz 31fe7ea
bugfix and test nointerp itp()
rafaqz 471b1e5
cleanup
rafaqz 09e20e2
run JuliaFormatter
rafaqz 05f2c4c
use JuliaFormatter v2
rafaqz a27ae40
bugfix eval_unstructured
rafaqz 0222ba0
review suggestions
rafaqz 00704e4
use NURBS
rafaqz b37af8c
dim checks
rafaqz 45fbe9b
bugfix constructor changes
rafaqz 3537e53
comments and format
rafaqz 79acff4
fix NoInterpolationDimension docstring
rafaqz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,137 +1,98 @@ | ||
| function _interpolate!( | ||
| out, | ||
| A::NDInterpolation{N_in, N_out, ID}, | ||
| t::Tuple{Vararg{Number, N_in}}, | ||
| idx::NTuple{N_in, <:Integer}, | ||
| derivative_orders::NTuple{N_in, <:Integer}, | ||
| A::NDInterpolation{N,N_in,N_out}, | ||
| ts::Tuple{Vararg{Number}}, | ||
| idx::NTuple{N, <:Integer}, | ||
| derivative_orders::NTuple{N, <:Integer}, | ||
| multi_point_index | ||
| ) where {N_in, N_out, ID <: LinearInterpolationDimension} | ||
| out = make_zero!!(out) | ||
| any(>(1), derivative_orders) && return out | ||
|
|
||
| tᵢ = ntuple(i -> A.interp_dims[i].t[idx[i]], N_in) | ||
| tᵢ₊₁ = ntuple(i -> A.interp_dims[i].t[idx[i] + 1], N_in) | ||
|
|
||
| # Size of the (hyper)rectangle `t` is in | ||
| t_vol = one(eltype(tᵢ)) | ||
| for (t₁, t₂) in zip(tᵢ, tᵢ₊₁) | ||
| t_vol *= t₂ - t₁ | ||
| ) where {N,N_in,N_out} | ||
| (; interp_dims, cache, u) = A | ||
| check_derivative_order(interp_dims, derivative_orders) || return out | ||
| if isnothing(multi_point_index) | ||
| multi_point_index = map(_ -> 1, interp_dims) | ||
| end | ||
| out = make_zero!!(out) | ||
| denom = zero(eltype(ts)) | ||
| # Setup | ||
| space = map(iteration_space, interp_dims) | ||
| preparations = map(prepare, interp_dims, derivative_orders, multi_point_index, ts, idx) | ||
|
|
||
| # Loop over the corners of the (hyper)rectangle `t` is in | ||
| for I in Iterators.product(ntuple(i -> (false, true), N_in)...) | ||
| c = eltype(out)(inv(t_vol)) | ||
| for (t_, right_point, d, t₁, t₂) in zip(t, I, derivative_orders, tᵢ, tᵢ₊₁) | ||
| c *= if right_point | ||
| iszero(d) ? t_ - t₁ : one(t_) | ||
| else | ||
| iszero(d) ? t₂ - t_ : -one(t_) | ||
| end | ||
| end | ||
| J = (ntuple(i -> idx[i] + I[i], N_in)..., ..) | ||
| if iszero(N_out) | ||
| out += c * A.u[J...] | ||
| for I in Iterators.product(space...) | ||
| scaling = map(scale, interp_dims, preparations, I) | ||
| J = map(index, interp_dims, ts, idx, I) | ||
| product = if cache isa EmptyCache | ||
| prod(scaling) | ||
| else | ||
| @. out += c * A.u[J...] | ||
| product = cache.weights[J...] * prod(scaling) | ||
| denom += product | ||
| end | ||
| end | ||
| return out | ||
| end | ||
|
|
||
| function _interpolate!( | ||
| out, | ||
| A::NDInterpolation{N_in, N_out, ID}, | ||
| t::Tuple{Vararg{Number, N_in}}, | ||
| idx::NTuple{N_in, <:Integer}, | ||
| derivative_orders::NTuple{N_in, <:Integer}, | ||
| multi_point_index | ||
| ) where {N_in, N_out, ID <: ConstantInterpolationDimension} | ||
| if any(>(0), derivative_orders) | ||
| return if any(i -> !isempty(searchsorted(A.interp_dims[i].t, t[i])), 1:N_in) | ||
| typed_nan(out) | ||
| if iszero(N_out) | ||
| @assert all(map(j -> j isa Integer, J)) | ||
| out += product * u[J...] | ||
| else | ||
| out | ||
| out .+= product .* view(u, J...) | ||
| end | ||
| end | ||
| idx = ntuple( | ||
| i -> t[i] >= A.interp_dims[i].t[end] ? length(A.interp_dims[i].t) : idx[i], N_in) | ||
| if iszero(N_out) | ||
| out = A.u[idx...] | ||
| else | ||
| out .= A.u[idx...] | ||
| end | ||
| return out | ||
| end | ||
|
|
||
| # BSpline evaluation | ||
| function _interpolate!( | ||
| out, | ||
| A::NDInterpolation{N_in, N_out, ID}, | ||
| t::Tuple{Vararg{Number, N_in}}, | ||
| idx::NTuple{N_in, <:Integer}, | ||
| derivative_orders::NTuple{N_in, <:Integer}, | ||
| multi_point_index | ||
| ) where {N_in, N_out, ID <: BSplineInterpolationDimension} | ||
| (; interp_dims) = A | ||
|
|
||
| out = make_zero!!(out) | ||
| degrees = ntuple(dim_in -> interp_dims[dim_in].degree, N_in) | ||
| basis_function_vals = get_basis_function_values_all( | ||
| A, t, idx, derivative_orders, multi_point_index | ||
| ) | ||
|
|
||
| for I in CartesianIndices(ntuple(dim_in -> 1:(degrees[dim_in] + 1), N_in)) | ||
| B_product = prod(dim_in -> basis_function_vals[dim_in][I[dim_in]], 1:N_in) | ||
| cp_index = ntuple( | ||
| dim_in -> idx[dim_in] + I[dim_in] - degrees[dim_in] - 1, N_in) | ||
| if !(cache isa EmptyCache) | ||
| if iszero(N_out) | ||
| out += B_product * A.u[cp_index...] | ||
| out /= denom | ||
| else | ||
| out .+= B_product * view(A.u, cp_index..., ..) | ||
| out ./= denom | ||
| end | ||
| end | ||
|
|
||
| return out | ||
| end | ||
|
|
||
| # NURBS evaluation | ||
| function _interpolate!( | ||
| out, | ||
| A::NDInterpolation{N_in, N_out, ID, <:NURBSWeights}, | ||
| t::Tuple{Vararg{Number, N_in}}, | ||
| idx::NTuple{N_in, <:Integer}, | ||
| derivative_orders::NTuple{N_in, <:Integer}, | ||
| multi_point_index | ||
| ) where {N_in, N_out, ID <: BSplineInterpolationDimension} | ||
| (; interp_dims, cache) = A | ||
| check_derivative_order(dims::Tuple, derivative_orders::Tuple) = | ||
| all(map(check_derivative_order, dims, derivative_orders)) | ||
| check_derivative_order(::LinearInterpolationDimension, d_o) = d_o <= 1 | ||
| check_derivative_order(::ConstantInterpolationDimension, d_o) = d_0 <= 0 | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| check_derivative_order(::AbstractInterpolationDimension, d_o) = true | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # TODO how to handle this | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # if derivative_order > 0 | ||
| # return if any(i -> !isempty(searchsorted(A.interp_dims[i].t, t[i])) | ||
| # typed_nan(out) | ||
| # else | ||
| # out | ||
| # end | ||
| # end | ||
|
|
||
| out = make_zero!!(out) | ||
| degrees = ntuple(dim_in -> interp_dims[dim_in].degree, N_in) | ||
| basis_function_vals = get_basis_function_values_all( | ||
| A, t, idx, derivative_orders, multi_point_index | ||
| function prepare(d::LinearInterpolationDimension, derivative_order, multi_point_index, t, i) | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| t₁ = d.t[i] | ||
| t₂ = d.t[i + 1] | ||
| t_vol_inv = inv(t₂ - t₁) | ||
| return (; t, t₁, t₂, t_vol_inv, derivative_order) | ||
| end | ||
| prepare(::ConstantInterpolationDimension, derivative_orders, multi_point_index, t, i) = (;) | ||
| prepare(::NoInterpolationDimension, derivative_orders, multi_point_index, t, i) = (;) | ||
| function prepare(d::BSplineInterpolationDimension, derivative_order, multi_point_index, t, i) | ||
| # TODO the dim_in arg isn't really needed, so drop it. Currently just 0 | ||
| basis_function_values = get_basis_function_values( | ||
| d, t, i, derivative_order, multi_point_index | ||
| ) | ||
| return (; basis_function_values) | ||
| end | ||
|
|
||
| denom = zero(eltype(t)) | ||
|
|
||
| for I in CartesianIndices(ntuple(dim_in -> 1:(degrees[dim_in] + 1), N_in)) | ||
| B_product = prod(dim_in -> basis_function_vals[dim_in][I[dim_in]], 1:N_in) | ||
| cp_index = ntuple( | ||
| dim_in -> idx[dim_in] + I[dim_in] - degrees[dim_in] - 1, N_in) | ||
| weight = cache.weights[cp_index...] | ||
| product = weight * B_product | ||
| denom += product | ||
| if iszero(N_out) | ||
| out += product * A.u[cp_index...] | ||
| else | ||
| out .+= product * view(A.u, cp_index..., ..) | ||
| end | ||
| end | ||
| iteration_space(::LinearInterpolationDimension) = (false, true) | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| iteration_space(::ConstantInterpolationDimension) = 1 | ||
| iteration_space(::NoInterpolationDimension) = 1 | ||
| iteration_space(d::BSplineInterpolationDimension) = 1:d.degree + 1 | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if iszero(N_out) | ||
| out /= denom | ||
| function scale(::LinearInterpolationDimension, prep::NamedTuple, right_point::Bool) | ||
| (; t, t₁, t₂, t_vol_inv, derivative_order) = prep | ||
| if right_point | ||
| iszero(derivative_order) ? t - t₁ : one(t) | ||
| else | ||
| out ./= denom | ||
| end | ||
|
|
||
| return out | ||
| iszero(derivative_order) ? t₂ - t : -one(t) | ||
| end * t_vol_inv | ||
| end | ||
| scale(::ConstantInterpolationDimension, prep::NamedTuple, i) = 1 | ||
rafaqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| scale(::NoInterpolationDimension, prep::NamedTuple, i) = 1 | ||
| scale(::BSplineInterpolationDimension, prep::NamedTuple, i) = prep.basis_function_values[i] | ||
|
|
||
| index(::LinearInterpolationDimension, t, idx, i) = idx + i | ||
| index(d::ConstantInterpolationDimension, t, idx, i) = t >= d.t[end] ? length(d.t) : idx[i] | ||
| index(::NoInterpolationDimension, t, idx, i) = Colon() | ||
| index(d::BSplineInterpolationDimension, t, idx, i) = idx + i - d.degree - 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.