DDC 0.4.1
Loading...
Searching...
No Matches
non_uniform_point_sampling.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <algorithm>
8#include <cassert>
9#include <cstddef>
10#include <initializer_list>
11#include <ostream>
12#include <stdexcept>
13#include <tuple>
14#include <type_traits>
15#include <utility>
16#include <vector>
17
18#include <Kokkos_Core.hpp>
19
20#include "ddc/coordinate.hpp"
21#include "ddc/discrete_domain.hpp"
22#include "ddc/discrete_element.hpp"
23#include "ddc/discrete_space.hpp"
24#include "ddc/discrete_vector.hpp"
25
26namespace ddc {
27
28namespace detail {
29
30struct NonUniformPointSamplingBase
31{
32};
33
34} // namespace detail
35
36/// `NonUniformPointSampling` models a non-uniform discretization of the `CDim` segment \f$[a, b]\f$.
37template <class CDim>
38class NonUniformPointSampling : detail::NonUniformPointSamplingBase
39{
40public:
41 using continuous_dimension_type = CDim;
42
43#if defined(DDC_BUILD_DEPRECATED_CODE)
44 using continuous_element_type
45 [[deprecated("Use ddc::Coordinate<continuous_dimension_type> instead.")]]
46 = Coordinate<CDim>;
47#endif
48
49 using discrete_dimension_type = NonUniformPointSampling;
50
51public:
52 template <class DDim, class MemorySpace>
53 class Impl
54 {
55 template <class ODDim, class OMemorySpace>
56 friend class Impl;
57
58 Kokkos::View<Coordinate<CDim>*, MemorySpace> m_points;
59
60 public:
61 using discrete_dimension_type = NonUniformPointSampling;
62
63 using discrete_domain_type = DiscreteDomain<DDim>;
64
65 using discrete_element_type = DiscreteElement<DDim>;
66
67 using discrete_vector_type = DiscreteVector<DDim>;
68
69 Impl() = default;
70
71 /// @brief Construct a `NonUniformPointSampling` using a brace-list, i.e. `NonUniformPointSampling mesh({0., 1.})`
72 Impl(std::initializer_list<Coordinate<CDim>> const points)
73 : Impl(points.begin(), points.end())
74 {
75 }
76
77 /// @brief Construct a `NonUniformPointSampling` using a C++20 "common range".
78 template <class InputRange>
79 explicit Impl(InputRange const& points) : Impl(points.begin(), points.end())
80 {
81 }
82
83 /// @brief Construct a `NonUniformPointSampling` using a pair of iterators.
84 template <class InputIt>
85 Impl(InputIt const points_begin, InputIt const points_end)
86 {
87 using view_type = Kokkos::View<Coordinate<CDim>*, MemorySpace>;
88 if (!std::is_sorted(points_begin, points_end)) {
89 throw std::runtime_error("Input points must be sorted");
90 }
91 // Make a contiguous copy of [points_begin, points_end[
92 std::vector<Coordinate<CDim>> host_points(points_begin, points_end);
93 m_points = view_type("NonUniformPointSampling::points", host_points.size());
94 Kokkos::deep_copy(m_points, view_type(host_points.data(), host_points.size()));
95 }
96
97 template <class OriginMemorySpace>
98 explicit Impl(Impl<DDim, OriginMemorySpace> const& impl)
99 : m_points(Kokkos::create_mirror_view_and_copy(MemorySpace(), impl.m_points))
100 {
101 }
102
103 Impl(Impl const& x) = delete;
104
105 Impl(Impl&& x) = default;
106
107 ~Impl() = default;
108
109 Impl& operator=(Impl const& x) = delete;
110
111 Impl& operator=(Impl&& x) = default;
112
113 KOKKOS_FUNCTION std::size_t size() const
114 {
115 return m_points.size();
116 }
117
118 /// @brief Lower bound index of the mesh
119 KOKKOS_FUNCTION discrete_element_type front() const noexcept
120 {
121 return discrete_element_type(0);
122 }
123
124 /// @brief Convert a mesh index into a position in `CDim`
125 KOKKOS_FUNCTION Coordinate<CDim> coordinate(
126 discrete_element_type const& icoord) const noexcept
127 {
128 return m_points(icoord.uid());
129 }
130 };
131
132 /** Construct an Impl<Kokkos::HostSpace> and associated discrete_domain_type from a range
133 * containing the points coordinates along the `DDim` dimension.
134 *
135 * @param non_uniform_points a range (std::vector, std::array, ...) containing the coordinates of the points of the domain.
136 */
137 template <class DDim, class InputRange>
138 static std::tuple<typename DDim::template Impl<DDim, Kokkos::HostSpace>, DiscreteDomain<DDim>>
139 init(InputRange const& non_uniform_points)
140 {
141 assert(!non_uniform_points.empty());
142 DiscreteVector<DDim> const n(non_uniform_points.size());
143 typename DDim::template Impl<DDim, Kokkos::HostSpace> disc(non_uniform_points);
144 DiscreteDomain<DDim> domain(disc.front(), n);
145 return std::make_tuple(std::move(disc), std::move(domain));
146 }
147
148 /** Construct 4 non-uniform `DiscreteDomain` and an Impl<Kokkos::HostSpace> from 3 ranges containing the points coordinates along the `DDim` dimension.
149 *
150 * @param domain_r a range containing the coordinates of the points of the main domain along the DDim position
151 * @param pre_ghost_r a range containing the positions of the ghost points before the main domain the DDim position
152 * @param post_ghost_r a range containing the positions of the ghost points after the main domain the DDim position
153 */
154 template <class DDim, class InputRange>
155 static std::tuple<
156 typename DDim::template Impl<DDim, Kokkos::HostSpace>,
157 DiscreteDomain<DDim>,
158 DiscreteDomain<DDim>,
159 DiscreteDomain<DDim>,
160 DiscreteDomain<DDim>>
162 InputRange const& domain_r,
163 InputRange const& pre_ghost_r,
164 InputRange const& post_ghost_r)
165 {
166 assert(!domain_r.empty());
167
168 DiscreteVector<DDim> const n(domain_r.size());
169 DiscreteVector<DDim> const n_ghosts_before(pre_ghost_r.size());
170 DiscreteVector<DDim> const n_ghosts_after(post_ghost_r.size());
171
172 std::vector<typename InputRange::value_type> full_domain;
173
174 std::copy(pre_ghost_r.begin(), pre_ghost_r.end(), std::back_inserter(full_domain));
175 std::copy(domain_r.begin(), domain_r.end(), std::back_inserter(full_domain));
176 std::copy(post_ghost_r.begin(), post_ghost_r.end(), std::back_inserter(full_domain));
177
178 typename DDim::template Impl<DDim, Kokkos::HostSpace> disc(full_domain);
179
180 DiscreteDomain<DDim> ghosted_domain(disc.front(), n + n_ghosts_before + n_ghosts_after);
181 DiscreteDomain<DDim> pre_ghost = ghosted_domain.take_first(n_ghosts_before);
182 DiscreteDomain<DDim> main_domain = ghosted_domain.remove(n_ghosts_before, n_ghosts_after);
183 DiscreteDomain<DDim> post_ghost = ghosted_domain.take_last(n_ghosts_after);
184 return std::make_tuple(
185 std::move(disc),
186 std::move(main_domain),
187 std::move(ghosted_domain),
188 std::move(pre_ghost),
189 std::move(post_ghost));
190 }
191};
192
193template <class DDim>
195 : public std::is_base_of<detail::NonUniformPointSamplingBase, DDim>::type
196{
197};
198
199template <class DDim>
201
202template <
203 class DDimImpl,
204 std::enable_if_t<
205 is_non_uniform_point_sampling_v<typename DDimImpl::discrete_dimension_type>,
206 int>
207 = 0>
208std::ostream& operator<<(std::ostream& out, DDimImpl const& mesh)
209{
210 return out << "NonUniformPointSampling(" << mesh.size() << ")";
211}
212
213template <class DDim, std::enable_if_t<is_non_uniform_point_sampling_v<DDim>, int> = 0>
214KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> coordinate(
215 DiscreteElement<DDim> const& c)
216{
217 return discrete_space<DDim>().coordinate(c);
218}
219
220template <class DDim, std::enable_if_t<is_non_uniform_point_sampling_v<DDim>, int> = 0>
222 DiscreteElement<DDim> i)
223{
224 return coordinate(i) - coordinate(i - 1);
225}
226
227template <class DDim, std::enable_if_t<is_non_uniform_point_sampling_v<DDim>, int> = 0>
229 DiscreteElement<DDim> i)
230{
231 return coordinate(i + 1) - coordinate(i);
232}
233
234template <class DDim, std::enable_if_t<is_non_uniform_point_sampling_v<DDim>, int> = 0>
235KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmin(
236 DiscreteDomain<DDim> const& d)
237{
238 return coordinate(d.front());
239}
240
241template <class DDim, std::enable_if_t<is_non_uniform_point_sampling_v<DDim>, int> = 0>
242KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmax(
243 DiscreteDomain<DDim> const& d)
244{
245 return coordinate(d.back());
246}
247
248template <class DDim, std::enable_if_t<is_non_uniform_point_sampling_v<DDim>, int> = 0>
249KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rlength(
250 DiscreteDomain<DDim> const& d)
251{
252 return rmax(d) - rmin(d);
253}
254
255} // namespace ddc
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
KOKKOS_FUNCTION std::size_t size() const
KOKKOS_FUNCTION Coordinate< CDim > coordinate(discrete_element_type const &icoord) const noexcept
Convert a mesh index into a position in CDim
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Impl & operator=(Impl &&x)=default
Impl & operator=(Impl const &x)=delete
Impl(InputIt const points_begin, InputIt const points_end)
Construct a NonUniformPointSampling using a pair of iterators.
KOKKOS_FUNCTION discrete_element_type front() const noexcept
Lower bound index of the mesh.
Impl(InputRange const &points)
Construct a NonUniformPointSampling using a C++20 "common range".
Impl(std::initializer_list< Coordinate< CDim > > const points)
Construct a NonUniformPointSampling using a brace-list, i.e. NonUniformPointSampling mesh({0....
NonUniformPointSampling models a non-uniform discretization of the CDim segment .
static std::tuple< typename DDim::template Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim > > init(InputRange const &non_uniform_points)
Construct an Impl<Kokkos::HostSpace> and associated discrete_domain_type from a range containing the ...
static std::tuple< typename DDim::template Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim > > init_ghosted(InputRange const &domain_r, InputRange const &pre_ghost_r, InputRange const &post_ghost_r)
Construct 4 non-uniform DiscreteDomain and an Impl<Kokkos::HostSpace> from 3 ranges containing the po...
The top-level namespace of DDC.
constexpr bool is_non_uniform_point_sampling_v
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmax(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rlength(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_left(DiscreteElement< DDim > i)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmin(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > coordinate(DiscreteElement< DDim > const &c)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_right(DiscreteElement< DDim > i)