1#ifndef TATOOINE_RECTILINEAR_GRID_VERTEX_PROPERTY_SAMPLER_H
2#define TATOOINE_RECTILINEAR_GRID_VERTEX_PROPERTY_SAMPLER_H
17template <
typename Derived,
typename Real, std::
size_t N,
typename Tensor>
20template <
typename GridVertexProperty,
21 template <
typename>
typename... InterpolationKernels>
24template <
typename TopSampler,
typename Real,
typename ValueType,
25 template <
typename>
typename... InterpolationKernels>
28template <
typename DerivedSampler,
typename Real,
typename ValueType,
29 template <
typename>
typename... InterpolationKernels>
32template <
typename DerivedSampler,
typename Real,
typename ValueType,
33 template <
typename>
typename InterpolationKernel0,
34 template <
typename>
typename InterpolationKernel1,
35 template <
typename>
typename... TailInterpolationKernels>
37 DerivedSampler, Real, ValueType, InterpolationKernel0, InterpolationKernel1,
38 TailInterpolationKernels...> {
42 TailInterpolationKernels...>;
45template <
typename DerivedSampler,
typename Real,
typename ValueType,
46 template <
typename>
typename InterpolationKernel>
48 InterpolationKernel> {
52template <
typename DerivedSampler,
typename Real,
typename ValueType,
53 template <
typename>
typename... InterpolationKernels>
55 DerivedSampler, Real, ValueType, InterpolationKernels...>
::value_type;
59template <
typename DerivedSampler,
typename Real,
typename ValueType,
60 template <
typename>
typename HeadInterpolationKernel,
61 template <
typename>
typename... TailInterpolationKernels>
63 template <
typename,
typename,
typename,
template <
typename>
typename,
64 template <
typename>
typename...>
71 HeadInterpolationKernel,
72 TailInterpolationKernels...>;
74 this_type, Real, ValueType, HeadInterpolationKernel,
75 TailInterpolationKernels...>;
79 return DerivedSampler::current_dimension_index();
82 return sizeof...(TailInterpolationKernels) + 1;
85 return tatooine::tensor_num_components<value_type>;
90 return static_cast<DerivedSampler&
>(*this);
95 -> DerivedSampler const& {
96 return static_cast<DerivedSampler const&
>(*this);
109 "Number of indices does not match number of dimensions.");
115 "Number of indices does not match number of dimensions.");
119 template <std::
size_t DimensionIndex>
127 auto at(std::size_t
const i)
const ->
decltype(
auto) {
135 auto operator[](std::size_t i)
const ->
decltype(
auto) {
return at(i); }
139 std::size_t
const stencil_size)
const {
140 auto d =
grid().finite_differences_coefficients(
146 template <
typename It>
148 It sample_end)
const {
150 auto sample_it = sample_begin;
151 auto coeff_it =
begin(coeffs);
152 for (; sample_it != sample_end; ++sample_it, ++coeff_it) {
153 if (*coeff_it != 0) {
154 df_dx += *coeff_it * *sample_it;
161 auto const&... cit_tail)
const
163 auto const [
cell_index, interpolation_factor] = cit_head;
165 return HeadInterpolationKernel{
168 return HeadInterpolationKernel{
171 interpolation_factor);
176 auto const&... cit_tail)
const
178 auto const [left_global_index, interpolation_factor] = cit_head;
179 auto const right_global_index = left_global_index + 1;
181 auto const stencil_size =
min(dim.size(), std::size_t(5));
182 auto const half_stencil_size = stencil_size / 2;
184 auto left_global_begin = left_global_index < half_stencil_size
186 : left_global_index - half_stencil_size;
187 if (
auto potential_left_global_end = left_global_begin + stencil_size;
188 potential_left_global_end > dim.size()) {
189 left_global_begin -= potential_left_global_end - dim.size();
191 auto const left_global_end = left_global_begin + stencil_size;
193 auto right_global_end =
194 dim.size() - right_global_index <= half_stencil_size
196 : right_global_index + half_stencil_size + 1;
197 if (right_global_end < stencil_size) {
198 right_global_end = stencil_size ;
200 auto const right_global_begin = right_global_end - stencil_size;
202 auto const range_global_begin =
min(left_global_begin, right_global_begin);
203 auto const range_global_end =
max(left_global_end, right_global_end);
205 auto const left_local_index = left_global_index - range_global_begin;
206 auto const right_local_index = left_local_index + 1;
209 auto samples = std::vector<value_type>{};
210 samples.reserve(stencil_size + 1);
212 for (
auto i = range_global_begin; i < range_global_end; ++i) {
214 samples.push_back(
at(i));
216 samples.push_back(
at(i).interpolate_cell(cit_tail...));
224 begin(samples) + stencil_size);
229 auto const dright_dx =
232 auto const dy = dim[right_global_index] - dim[left_global_index];
233 return HeadInterpolationKernel<value_type>{
234 samples[left_local_index], samples[right_local_index], dleft_dx * dy,
235 dright_dx * dy}(interpolation_factor);
240 auto const&... cell_indices_interpolation_factors)
const ->
value_type {
241 auto constexpr num_derivatives_needed =
242 HeadInterpolationKernel<value_type>::num_derivatives;
243 if constexpr (num_derivatives_needed == 0) {
245 cell_indices_interpolation_factors...);
246 }
else if constexpr (num_derivatives_needed == 1) {
248 cell_indices_interpolation_factors...);
252 template <std::size_t... Is>
253 auto constexpr sample(std::index_sequence<Is...> ,
255 requires(
sizeof...(Is) ==
sizeof...(xs)) {
260template <
typename GridVertexProperty,
261 template <
typename>
typename... InterpolationKernels>
264 vertex_property_sampler<GridVertexProperty, InterpolationKernels...>,
265 typename GridVertexProperty::real_type,
266 typename GridVertexProperty::value_type, InterpolationKernels...>,
268 vertex_property_sampler<GridVertexProperty, InterpolationKernels...>,
269 typename GridVertexProperty::real_type,
270 sizeof...(InterpolationKernels),
271 typename GridVertexProperty::value_type> {
272 static_assert(
sizeof...(InterpolationKernels) ==
273 GridVertexProperty::num_dimensions());
277 using real_type =
typename GridVertexProperty::real_type;
281 InterpolationKernels...>;
289 return sizeof...(InterpolationKernels);
292 static_assert(is_floating_point<tatooine::value_type<value_type>>);
308 requires(
sizeof...(is) == GridVertexProperty::grid_type::num_dimensions()) {
314 "Number of indices does not match number of dimensions.");
315 return grid().position_at(is...);
318 template <std::
size_t DimensionIndex>
320 return grid().template cell_index<DimensionIndex>(x);
326 if (!
grid().is_inside(x)) {
330 [&](
auto const... xs) {
341template <
typename TopSampler,
typename Real,
typename ValueType,
342 template <
typename>
typename... InterpolationKernels>
345 vertex_property_sampler_view<TopSampler, Real, ValueType,
346 InterpolationKernels...>,
347 Real, ValueType, InterpolationKernels...> {
350 return TopSampler::data_is_changeable();
353 InterpolationKernels...>;
358 InterpolationKernels...>;
361 return TopSampler::num_dimensions() - 1;
365 return TopSampler::current_dimension_index() + 1;
372 std::size_t
const fixed_index)
385 "Number of indices is not equal to number of dimensions.");
389 template <std::
size_t DimensionIndex>
395template <
typename GridVertexProperty,
396 template <
typename>
typename... InterpolationKernels>
399 return GridVertexProperty::num_dimensions();
409 InterpolationKernels...>
const&
413 template <
typename Tensor,
typename TensorReal>
416 auto constexpr eps = 1e-9;
420 if constexpr (is_arithmetic<value_type>) {
447 vec<
typename GridVertexProperty::grid_type::real_type,
sizeof...(xs)>{
452template <
typename Gr
idVertexProperty>
470 auto const [ix, u] =
m_sampler.template cell_index<0>(x);
471 auto const [iy, v] =
m_sampler.template cell_index<1>(y);
477 auto const k = d - c - b + a;
478 auto const dx = k * v + b - a;
479 auto const dy = k * u + c - a;
484 return mat{{dx(0), dy(0)}, {dx(1), dy(1)}};
488 template <
typename Tensor,
typename TensorReal>
496template <
typename Gr
idVertexProperty>
516 auto const [ix, u] =
m_sampler.template cell_index<0>(x);
517 auto const [iy, v] =
m_sampler.template cell_index<1>(y);
518 auto const [iz, w] =
m_sampler.template cell_index<2>(z);
528 auto const k = h - g - f + e - d + c + b - a;
529 auto const dx = (k * v + f - e - b + a) * w + (d - c - b + a) * v + b - a;
530 auto const dy = (k * u + g - e - c + a) * w + (d - c - b + a) * u + c - a;
531 auto const dz = (k * u + g - e - c + a) * v + (f - e - b + a) * u + e - a;
534 return vec{dx, dy, dz};
537 {dx(0), dy(0), dz(0)}, {dx(1), dy(1), dz(1)}, {dx(2), dy(2), dz(2)}};
541 template <
typename Tensor,
typename TensorReal>
545 [
this](
auto const... xs) {
return sample(xs...); });
549template <
typename GridVertexProperty,
550 template <
typename>
typename... InterpolationKernels>
Definition: concepts.h:33
Definition: dimension.h:9
Definition: concepts.h:94
Definition: concepts.h:21
Definition: tensor_concepts.h:23
Definition: cell_container.h:15
auto end(vertex_container< Dimensions... > const &c)
Definition: vertex_container.h:142
auto begin(vertex_container< Dimensions... > const &c)
Definition: vertex_container.h:137
typename base_vertex_property_sampler_at< DerivedSampler, Real, ValueType, InterpolationKernels... >::value_type base_vertex_property_sampler_at_t
Definition: vertex_property_sampler.h:55
auto diff(typed_vertex_property_interface< Grid, ValueType, HasNonConstReference > const &prop, std::size_t const stencil_size=default_diff_stencil_size)
Definition: vertex_property.h:822
typename value_type_impl< T >::type value_type
Definition: type_traits.h:280
constexpr auto max(A &&a, B &&b)
Definition: math.h:20
constexpr auto min(A &&a, B &&b)
Definition: math.h:15
constexpr decltype(auto) invoke_unpacked(F &&f)
All arguments are bound -> just call f.
Definition: invoke_unpacked.h:15
Definition: base_tensor.h:23
std::decay_t< ValueType > & value_type
Definition: vertex_property_sampler.h:49
Definition: vertex_property_sampler.h:30
Definition: vertex_property_sampler.h:62
auto constexpr interpolate_cell(auto const &... cell_indices_interpolation_factors) const -> value_type
Decides if first derivative is needed or not.
Definition: vertex_property_sampler.h:239
static auto constexpr num_dimensions() -> std::size_t
Definition: vertex_property_sampler.h:81
ValueType value_type
Definition: vertex_property_sampler.h:77
auto interpolate_cell_without_derivative(auto const &cit_head, auto const &... cit_tail) const -> value_type
Definition: vertex_property_sampler.h:160
auto at(std::size_t const i) const -> decltype(auto)
Definition: vertex_property_sampler.h:127
auto operator[](std::size_t i) const -> decltype(auto)
Definition: vertex_property_sampler.h:135
friend struct base_vertex_property_sampler
Definition: vertex_property_sampler.h:65
auto constexpr as_derived_sampler() const -> DerivedSampler const &
returns casted as_derived data
Definition: vertex_property_sampler.h:94
base_vertex_property_sampler_at_t< this_type, Real, ValueType, HeadInterpolationKernel, TailInterpolationKernels... > indexing_type
Definition: vertex_property_sampler.h:75
auto position_at(integral auto const ... is) const
Definition: vertex_property_sampler.h:113
Real real_type
Definition: vertex_property_sampler.h:76
auto constexpr sample(std::index_sequence< Is... >, arithmetic auto const ... xs) const -> value_type requires(sizeof...(Is)==sizeof...(xs))
Definition: vertex_property_sampler.h:253
base_vertex_property_sampler< DerivedSampler, Real, ValueType, HeadInterpolationKernel, TailInterpolationKernels... > this_type
Definition: vertex_property_sampler.h:72
auto finite_differences_coefficients(std::size_t const vertex_index, std::size_t const stencil_size) const
Definition: vertex_property_sampler.h:138
auto data_at(integral auto const ... is) const -> value_type const &
Definition: vertex_property_sampler.h:107
static auto constexpr current_dimension_index()
Definition: vertex_property_sampler.h:78
static auto constexpr num_components()
Definition: vertex_property_sampler.h:84
auto interpolate_cell_with_one_derivative(auto const &cit_head, auto const &... cit_tail) const -> value_type
Definition: vertex_property_sampler.h:175
auto differentiate(floating_point_range auto const &coeffs, It sample_begin, It sample_end) const
Calcuates derivative from samples and differential coefficients.
Definition: vertex_property_sampler.h:147
auto grid() const -> auto const &
Definition: vertex_property_sampler.h:103
auto property() const -> auto const &
Definition: vertex_property_sampler.h:99
auto constexpr as_derived_sampler() -> DerivedSampler &
returns casted as_derived data
Definition: vertex_property_sampler.h:89
auto cell_index(arithmetic auto const x) const -> decltype(auto)
Definition: vertex_property_sampler.h:120
differentiated_sampler(vertex_property_sampler< GridVertexProperty, interpolation::linear, interpolation::linear, interpolation::linear > const &vertex_property_sampler)
Definition: vertex_property_sampler.h:507
auto constexpr sample(arithmetic auto x, arithmetic auto y, arithmetic auto z) const
Definition: vertex_property_sampler.h:514
auto constexpr sample(base_tensor< Tensor, TensorReal, num_dimensions()> const &x) const
Definition: vertex_property_sampler.h:542
static auto constexpr num_dimensions() -> std::size_t
Definition: vertex_property_sampler.h:499
vertex_property_sampler< GridVertexProperty, interpolation::linear, interpolation::linear, interpolation::linear > const & m_sampler
Definition: vertex_property_sampler.h:504
vertex_property_sampler< GridVertexProperty, interpolation::linear, interpolation::linear > const & m_sampler
Definition: vertex_property_sampler.h:459
auto constexpr sample(base_tensor< Tensor, TensorReal, num_dimensions()> const &x) const
Definition: vertex_property_sampler.h:489
auto constexpr sample(arithmetic auto x, arithmetic auto y) const
Definition: vertex_property_sampler.h:469
differentiated_sampler(vertex_property_sampler< GridVertexProperty, interpolation::linear, interpolation::linear > const &vertex_property_sampler)
Definition: vertex_property_sampler.h:462
static auto constexpr num_dimensions() -> std::size_t
Definition: vertex_property_sampler.h:455
Definition: vertex_property_sampler.h:397
differentiated_sampler(vertex_property_sampler< GridVertexProperty, InterpolationKernels... > const &vertex_property_sampler)
Definition: vertex_property_sampler.h:407
vertex_property_sampler< GridVertexProperty, InterpolationKernels... > const & m_sampler
Definition: vertex_property_sampler.h:404
auto constexpr sample(base_tensor< Tensor, TensorReal, num_dimensions()> const &x) const
Definition: vertex_property_sampler.h:414
static auto constexpr num_dimensions() -> std::size_t
Definition: vertex_property_sampler.h:398
auto constexpr sample(arithmetic auto const ... xs) const
Definition: vertex_property_sampler.h:445
Definition: vertex_property_sampler.h:18
Definition: vertex_property_sampler.h:347
auto constexpr property() const -> auto const &
Definition: vertex_property_sampler.h:375
std::size_t m_fixed_index
Definition: vertex_property_sampler.h:369
static auto constexpr num_dimensions() -> std::size_t
Definition: vertex_property_sampler.h:360
static auto constexpr data_is_changeable()
Definition: vertex_property_sampler.h:349
TopSampler const & m_top_sampler
Definition: vertex_property_sampler.h:368
auto constexpr data_at(integral auto const ... is) const -> value_type const &
Definition: vertex_property_sampler.h:383
auto grid() const -> auto const &
Definition: vertex_property_sampler.h:379
auto constexpr cell_index(arithmetic auto const x) const -> decltype(auto)
Definition: vertex_property_sampler.h:390
ValueType value_type
Definition: vertex_property_sampler.h:355
static auto constexpr current_dimension_index()
Definition: vertex_property_sampler.h:364
Real real_type
Definition: vertex_property_sampler.h:354
vertex_property_sampler_view< TopSampler, Real, ValueType, InterpolationKernels... > this_type
Definition: vertex_property_sampler.h:353
vertex_property_sampler_view(TopSampler const &top_sampler, std::size_t const fixed_index)
Definition: vertex_property_sampler.h:371
Definition: vertex_property_sampler.h:271
vertex_property_sampler< property_type, InterpolationKernels... > this_type
Definition: vertex_property_sampler.h:276
vertex_property_sampler(vertex_property_sampler &&other) noexcept=default
auto position_at(integral auto const ... is) const
Definition: vertex_property_sampler.h:312
auto data_at(integral auto const ... is) const -> value_type const &requires(sizeof...(is)==GridVertexProperty::grid_type::num_dimensions())
Definition: vertex_property_sampler.h:307
auto property() const -> auto const &
Definition: vertex_property_sampler.h:303
typename GridVertexProperty::value_type value_type
Definition: vertex_property_sampler.h:278
vertex_property_sampler(property_type const &prop)
Definition: vertex_property_sampler.h:298
auto cell_index(arithmetic auto const x) const -> decltype(auto)
Definition: vertex_property_sampler.h:319
auto evaluate(typename field_parent_type::pos_type const &x, typename field_parent_type::real_type const) const -> value_type
Definition: vertex_property_sampler.h:323
static constexpr std::size_t current_dimension_index()
Definition: vertex_property_sampler.h:286
static auto constexpr num_dimensions() -> std::size_t
Definition: vertex_property_sampler.h:288
vertex_property_sampler(vertex_property_sampler const &other)=default
auto grid() const -> auto const &
Definition: vertex_property_sampler.h:305
GridVertexProperty property_type
Definition: vertex_property_sampler.h:274
property_type const & m_property
Definition: vertex_property_sampler.h:295
typename GridVertexProperty::real_type real_type
Definition: vertex_property_sampler.h:277
Real real_type
Definition: field.h:17
Definition: interpolation.h:16
static auto constexpr ood_tensor()
Definition: field.h:21
Definition: invoke_unpacked.h:11