DDC 0.4.1
Loading...
Searching...
No Matches
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 <cassert>
8#include <ostream>
9#include <tuple>
10#include <type_traits>
11#include <utility>
12
13#include <Kokkos_Core.hpp>
14
15#include "ddc/coordinate.hpp"
16#include "ddc/discrete_domain.hpp"
17#include "ddc/discrete_element.hpp"
18#include "ddc/discrete_space.hpp"
19#include "ddc/discrete_vector.hpp"
20#include "ddc/real_type.hpp"
21
22namespace ddc {
23
24namespace detail {
25
26struct UniformPointSamplingBase
27{
28};
29
30} // namespace detail
31
32/** UniformPointSampling models a uniform discretization of the provided continuous dimension
33 */
34template <class CDim>
35class UniformPointSampling : detail::UniformPointSamplingBase
36{
37public:
38 using continuous_dimension_type = CDim;
39
40#if defined(DDC_BUILD_DEPRECATED_CODE)
41 using continuous_element_type
42 [[deprecated("Use ddc::Coordinate<continuous_dimension_type> instead.")]]
43 = Coordinate<CDim>;
44#endif
45
46 using discrete_dimension_type = UniformPointSampling;
47
48public:
49 template <class DDim, class MemorySpace>
50 class Impl
51 {
52 template <class ODDim, class OMemorySpace>
53 friend class Impl;
54
55 private:
56 Coordinate<CDim> m_origin;
57
58 Real m_step;
59
60 public:
61 using discrete_dimension_type = UniformPointSampling;
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() noexcept : m_origin(0), m_step(1) {}
70
71 Impl(Impl const&) = delete;
72
73 template <class OriginMemorySpace>
74 explicit Impl(Impl<DDim, OriginMemorySpace> const& impl)
75 : m_origin(impl.m_origin)
76 , m_step(impl.m_step)
77 {
78 }
79
80 Impl(Impl&&) = default;
81
82 /** @brief Construct a `Impl` from a point and a spacing step.
83 *
84 * @param origin the real coordinate of mesh coordinate 0
85 * @param step the real distance between two points of mesh distance 1
86 */
87 Impl(Coordinate<CDim> origin, Real step) : m_origin(origin), m_step(step)
88 {
89 assert(step > 0);
90 }
91
92 ~Impl() = default;
93
94 Impl& operator=(Impl const& x) = delete;
95
96 Impl& operator=(Impl&& x) = default;
97
98 /// @brief Lower bound index of the mesh
99 KOKKOS_FUNCTION Coordinate<CDim> origin() const noexcept
100 {
101 return m_origin;
102 }
103
104 /// @brief Lower bound index of the mesh
105 KOKKOS_FUNCTION discrete_element_type front() const noexcept
106 {
107 return discrete_element_type(0);
108 }
109
110 /// @brief Spacing step of the mesh
111 KOKKOS_FUNCTION Real step() const
112 {
113 return m_step;
114 }
115
116 /// @brief Convert a mesh index into a position in `CDim`
117 KOKKOS_FUNCTION Coordinate<CDim> coordinate(
118 discrete_element_type const& icoord) const noexcept
119 {
120 return m_origin + Coordinate<CDim>(icoord.uid() * m_step);
121 }
122 };
123
124 /** Construct a Impl<Kokkos::HostSpace> and associated discrete_domain_type from a segment
125 * \f$[a, b] \subset [a, +\infty[\f$ and a number of points `n`.
126 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
127 * One should expect usual floating point rounding errors.
128 *
129 * @param a coordinate of the first point of the domain
130 * @param b coordinate of the last point of the domain
131 * @param n number of points to map on the segment \f$[a, b]\f$ including a & b
132 */
133 template <class DDim>
134 static std::tuple<typename DDim::template Impl<DDim, Kokkos::HostSpace>, DiscreteDomain<DDim>>
135 init(Coordinate<CDim> a, Coordinate<CDim> b, DiscreteVector<DDim> n)
136 {
137 assert(a < b);
138 assert(n > 1);
139 typename DDim::template Impl<DDim, Kokkos::HostSpace>
140 disc(a, Coordinate<CDim>((b - a) / (n - 1)));
141 DiscreteDomain<DDim> domain(disc.front(), n);
142 return std::make_tuple(std::move(disc), std::move(domain));
143 }
144
145 /** Construct a uniform `DiscreteDomain` from a segment \f$[a, b] \subset [a, +\infty[\f$ and a
146 * number of points `n`.
147 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
148 * One should expect usual floating point rounding errors.
149 *
150 * @param a coordinate of the first point of the domain
151 * @param b coordinate of the last point of the domain
152 * @param n the number of points to map the segment \f$[a, b]\f$ including a & b
153 * @param n_ghosts_before number of additional "ghost" points before the segment
154 * @param n_ghosts_after number of additional "ghost" points after the segment
155 */
156 template <class DDim>
157 static std::tuple<
158 typename DDim::template Impl<DDim, Kokkos::HostSpace>,
159 DiscreteDomain<DDim>,
160 DiscreteDomain<DDim>,
161 DiscreteDomain<DDim>,
162 DiscreteDomain<DDim>>
164 Coordinate<CDim> a,
165 Coordinate<CDim> b,
166 DiscreteVector<DDim> n,
167 DiscreteVector<DDim> n_ghosts_before,
168 DiscreteVector<DDim> n_ghosts_after)
169 {
170 assert(a < b);
171 assert(n > 1);
172 Real const discretization_step = (b - a) / (n - 1);
173 typename DDim::template Impl<DDim, Kokkos::HostSpace>
174 disc(a - n_ghosts_before.value() * discretization_step, discretization_step);
175 DiscreteDomain<DDim> ghosted_domain(disc.front(), n + n_ghosts_before + n_ghosts_after);
176 DiscreteDomain<DDim> pre_ghost = ghosted_domain.take_first(n_ghosts_before);
177 DiscreteDomain<DDim> main_domain = ghosted_domain.remove(n_ghosts_before, n_ghosts_after);
178 DiscreteDomain<DDim> post_ghost = ghosted_domain.take_last(n_ghosts_after);
179 return std::make_tuple(
180 std::move(disc),
181 std::move(main_domain),
182 std::move(ghosted_domain),
183 std::move(pre_ghost),
184 std::move(post_ghost));
185 }
186
187 /** Construct a uniform `DiscreteDomain` from a segment \f$[a, b] \subset [a, +\infty[\f$ and a
188 * number of points `n`.
189 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
190 * One should expect usual floating point rounding errors.
191 *
192 * @param a coordinate of the first point of the domain
193 * @param b coordinate of the last point of the domain
194 * @param n the number of points to map the segment \f$[a, b]\f$ including a & b
195 * @param n_ghosts number of additional "ghost" points before and after the segment
196 */
197 template <class DDim>
198 static std::tuple<
199 typename DDim::template Impl<DDim, Kokkos::HostSpace>,
200 DiscreteDomain<DDim>,
201 DiscreteDomain<DDim>,
202 DiscreteDomain<DDim>,
203 DiscreteDomain<DDim>>
205 Coordinate<CDim> a,
206 Coordinate<CDim> b,
207 DiscreteVector<DDim> n,
208 DiscreteVector<DDim> n_ghosts)
209 {
210 return init_ghosted(a, b, n, n_ghosts, n_ghosts);
211 }
212};
213
214template <class DDim>
216 : public std::is_base_of<detail::UniformPointSamplingBase, DDim>::type
217{
218};
219
220template <class DDim>
222
223template <
224 class DDimImpl,
225 std::enable_if_t<
226 is_uniform_point_sampling_v<typename DDimImpl::discrete_dimension_type>,
227 int>
228 = 0>
229std::ostream& operator<<(std::ostream& out, DDimImpl const& mesh)
230{
231 return out << "UniformPointSampling( origin=" << mesh.origin() << ", step=" << mesh.step()
232 << " )";
233}
234
235template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
236KOKKOS_FUNCTION constexpr Coordinate<typename DDim::continuous_dimension_type> coordinate(
237 DiscreteElement<DDim> const& c)
238{
239 return discrete_space<DDim>().coordinate(c);
240}
241
242/// @brief Lower bound index of the mesh
243template <class DDim>
244KOKKOS_FUNCTION std::enable_if_t<
245 is_uniform_point_sampling_v<DDim>,
246 Coordinate<typename DDim::continuous_dimension_type>>
247origin() noexcept
248{
249 return discrete_space<DDim>().origin();
250}
251
252/// @brief Lower bound index of the mesh
253template <class DDim>
254KOKKOS_FUNCTION std::enable_if_t<is_uniform_point_sampling_v<DDim>, DiscreteElement<DDim>>
255front() noexcept
256{
257 return discrete_space<DDim>().front();
258}
259
260/// @brief Spacing step of the mesh
261template <class DDim>
262KOKKOS_FUNCTION std::enable_if_t<is_uniform_point_sampling_v<DDim>, Real> step() noexcept
263{
264 return discrete_space<DDim>().step();
265}
266
267template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
268KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_left(
269 DiscreteElement<DDim>)
270{
271 return Coordinate<typename DDim::continuous_dimension_type>(step<DDim>());
272}
273
274template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
275KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_right(
276 DiscreteElement<DDim>)
277{
278 return Coordinate<typename DDim::continuous_dimension_type>(step<DDim>());
279}
280
281template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
282KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmin(
283 DiscreteDomain<DDim> const& d)
284{
285 return coordinate(d.front());
286}
287
288template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
289KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmax(
290 DiscreteDomain<DDim> const& d)
291{
292 return coordinate(d.back());
293}
294
295template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
296KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rlength(
297 DiscreteDomain<DDim> const& d)
298{
299 return rmax(d) - rmin(d);
300}
301
302} // namespace ddc
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
Impl(Coordinate< CDim > origin, Real step)
Construct a Impl from a point and a spacing step.
Impl & operator=(Impl const &x)=delete
KOKKOS_FUNCTION Coordinate< CDim > origin() const noexcept
Lower bound index of the mesh.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
KOKKOS_FUNCTION discrete_element_type front() const noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION Real step() const
Spacing step of the mesh.
KOKKOS_FUNCTION Coordinate< CDim > coordinate(discrete_element_type const &icoord) const noexcept
Convert a mesh index into a position in CDim
Impl & operator=(Impl &&x)=default
Impl(Impl const &)=delete
UniformPointSampling models a uniform discretization of the provided continuous dimension.
static std::tuple< typename DDim::template Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim > > init(Coordinate< CDim > a, Coordinate< CDim > b, DiscreteVector< DDim > n)
Construct a Impl<Kokkos::HostSpace> and associated discrete_domain_type from a segment and a number ...
static std::tuple< typename DDim::template Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim > > init_ghosted(Coordinate< CDim > a, Coordinate< CDim > b, DiscreteVector< DDim > n, DiscreteVector< DDim > n_ghosts)
Construct a uniform DiscreteDomain from a segment and a number of points n.
static std::tuple< typename DDim::template Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim > > init_ghosted(Coordinate< CDim > a, Coordinate< CDim > b, DiscreteVector< DDim > n, DiscreteVector< DDim > n_ghosts_before, DiscreteVector< DDim > n_ghosts_after)
Construct a uniform DiscreteDomain from a segment and a number of points n.
The top-level namespace of DDC.
constexpr bool is_uniform_point_sampling_v
KOKKOS_FUNCTION std::enable_if_t< is_uniform_point_sampling_v< DDim >, DiscreteElement< DDim > > front() noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION std::enable_if_t< is_uniform_point_sampling_v< DDim >, Coordinate< typename DDim::continuous_dimension_type > > origin() noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION constexpr Coordinate< typename DDim::continuous_dimension_type > coordinate(DiscreteElement< DDim > const &c)
KOKKOS_FUNCTION std::enable_if_t< is_uniform_point_sampling_v< DDim >, Real > step() noexcept
Spacing step of the mesh.