DDC 0.6.0
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 using discrete_dimension_type = UniformPointSampling;
41
42public:
43 template <class DDim, class MemorySpace>
44 class Impl
45 {
46 template <class ODDim, class OMemorySpace>
47 friend class Impl;
48
49 private:
50 Coordinate<CDim> m_origin;
51
52 Real m_step;
53
54 DiscreteElement<DDim> m_reference;
55
56 public:
57 using discrete_dimension_type = UniformPointSampling;
58
59 using discrete_domain_type = DiscreteDomain<DDim>;
60
61 using discrete_element_type = DiscreteElement<DDim>;
62
63 using discrete_vector_type = DiscreteVector<DDim>;
64
65 Impl() noexcept
66 : m_origin(0)
67 , m_step(1)
68 , m_reference(create_reference_discrete_element<DDim>())
69 {
70 }
71
72 Impl(Impl const&) = delete;
73
74 template <class OriginMemorySpace>
75 explicit Impl(Impl<DDim, OriginMemorySpace> const& impl)
76 : m_origin(impl.m_origin)
77 , m_step(impl.m_step)
78 , m_reference(impl.m_reference)
79 {
80 }
81
82 Impl(Impl&&) = default;
83
84 /** @brief Construct a `Impl` from a point and a spacing step.
85 *
86 * @param origin the real coordinate of mesh coordinate 0
87 * @param step the real distance between two points of mesh distance 1
88 */
89 Impl(Coordinate<CDim> origin, Real step)
90 : m_origin(origin)
91 , m_step(step)
92 , m_reference(create_reference_discrete_element<DDim>())
93 {
94 assert(step > 0);
95 }
96
97 ~Impl() = default;
98
99 Impl& operator=(Impl const& x) = delete;
100
101 Impl& operator=(Impl&& x) = default;
102
103 /// @brief Lower bound index of the mesh
104 KOKKOS_FUNCTION Coordinate<CDim> origin() const noexcept
105 {
106 return m_origin;
107 }
108
109 /// @brief Lower bound index of the mesh
110 KOKKOS_FUNCTION discrete_element_type front() const noexcept
111 {
112 return m_reference;
113 }
114
115 /// @brief Spacing step of the mesh
116 KOKKOS_FUNCTION Real step() const
117 {
118 return m_step;
119 }
120
121 /// @brief Convert a mesh index into a position in `CDim`
122 KOKKOS_FUNCTION Coordinate<CDim> coordinate(
123 discrete_element_type const& icoord) const noexcept
124 {
125 return m_origin + Coordinate<CDim>((icoord - front()) * m_step);
126 }
127 };
128
129 /** Construct a Impl<Kokkos::HostSpace> and associated discrete_domain_type from a segment
130 * \f$[a, b] \subset [a, +\infty[\f$ and a number of points `n`.
131 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
132 * One should expect usual floating point rounding errors.
133 *
134 * @param a coordinate of the first point of the domain
135 * @param b coordinate of the last point of the domain
136 * @param n number of points to map on the segment \f$[a, b]\f$ including a & b
137 */
138 template <class DDim>
139 static std::tuple<typename DDim::template Impl<DDim, Kokkos::HostSpace>, DiscreteDomain<DDim>>
140 init(Coordinate<CDim> a, Coordinate<CDim> b, DiscreteVector<DDim> n)
141 {
142 assert(a < b);
143 assert(n > 1);
144 typename DDim::template Impl<DDim, Kokkos::HostSpace>
145 disc(a, Coordinate<CDim>((b - a) / (n - 1)));
146 DiscreteDomain<DDim> domain(disc.front(), n);
147 return std::make_tuple(std::move(disc), std::move(domain));
148 }
149
150 /** Construct a uniform `DiscreteDomain` from a segment \f$[a, b] \subset [a, +\infty[\f$ and a
151 * number of points `n`.
152 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
153 * One should expect usual floating point rounding errors.
154 *
155 * @param a coordinate of the first point of the domain
156 * @param b coordinate of the last point of the domain
157 * @param n the number of points to map the segment \f$[a, b]\f$ including a & b
158 * @param n_ghosts_before number of additional "ghost" points before the segment
159 * @param n_ghosts_after number of additional "ghost" points after the segment
160 */
161 template <class DDim>
162 static std::tuple<
163 typename DDim::template Impl<DDim, Kokkos::HostSpace>,
164 DiscreteDomain<DDim>,
165 DiscreteDomain<DDim>,
166 DiscreteDomain<DDim>,
167 DiscreteDomain<DDim>>
169 Coordinate<CDim> a,
170 Coordinate<CDim> b,
171 DiscreteVector<DDim> n,
172 DiscreteVector<DDim> n_ghosts_before,
173 DiscreteVector<DDim> n_ghosts_after)
174 {
175 assert(a < b);
176 assert(n > 1);
177 Real const discretization_step = (b - a) / (n - 1);
178 typename DDim::template Impl<DDim, Kokkos::HostSpace>
179 disc(a - n_ghosts_before.value() * discretization_step, discretization_step);
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 /** Construct a uniform `DiscreteDomain` from a segment \f$[a, b] \subset [a, +\infty[\f$ and a
193 * number of points `n`.
194 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
195 * One should expect usual floating point rounding errors.
196 *
197 * @param a coordinate of the first point of the domain
198 * @param b coordinate of the last point of the domain
199 * @param n the number of points to map the segment \f$[a, b]\f$ including a & b
200 * @param n_ghosts number of additional "ghost" points before and after the segment
201 */
202 template <class DDim>
203 static std::tuple<
204 typename DDim::template Impl<DDim, Kokkos::HostSpace>,
205 DiscreteDomain<DDim>,
206 DiscreteDomain<DDim>,
207 DiscreteDomain<DDim>,
208 DiscreteDomain<DDim>>
210 Coordinate<CDim> a,
211 Coordinate<CDim> b,
212 DiscreteVector<DDim> n,
213 DiscreteVector<DDim> n_ghosts)
214 {
215 return init_ghosted(a, b, n, n_ghosts, n_ghosts);
216 }
217};
218
219template <class DDim>
221 : public std::is_base_of<detail::UniformPointSamplingBase, DDim>::type
222{
223};
224
225template <class DDim>
227
228template <
229 class DDimImpl,
230 std::enable_if_t<
231 is_uniform_point_sampling_v<typename DDimImpl::discrete_dimension_type>,
232 int>
233 = 0>
234std::ostream& operator<<(std::ostream& out, DDimImpl const& mesh)
235{
236 return out << "UniformPointSampling( origin=" << mesh.origin() << ", step=" << mesh.step()
237 << " )";
238}
239
240template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
241KOKKOS_FUNCTION constexpr Coordinate<typename DDim::continuous_dimension_type> coordinate(
242 DiscreteElement<DDim> const& c)
243{
244 return discrete_space<DDim>().coordinate(c);
245}
246
247/// @brief Lower bound index of the mesh
248template <class DDim>
249KOKKOS_FUNCTION std::enable_if_t<
250 is_uniform_point_sampling_v<DDim>,
251 Coordinate<typename DDim::continuous_dimension_type>>
252origin() noexcept
253{
254 return discrete_space<DDim>().origin();
255}
256
257/// @brief Lower bound index of the mesh
258template <class DDim>
259KOKKOS_FUNCTION std::enable_if_t<is_uniform_point_sampling_v<DDim>, DiscreteElement<DDim>>
260front() noexcept
261{
262 return discrete_space<DDim>().front();
263}
264
265/// @brief Spacing step of the mesh
266template <class DDim>
267KOKKOS_FUNCTION std::enable_if_t<is_uniform_point_sampling_v<DDim>, Real> step() noexcept
268{
269 return discrete_space<DDim>().step();
270}
271
272template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
273KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_left(
274 DiscreteElement<DDim>)
275{
276 return Coordinate<typename DDim::continuous_dimension_type>(step<DDim>());
277}
278
279template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
280KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_right(
281 DiscreteElement<DDim>)
282{
283 return Coordinate<typename DDim::continuous_dimension_type>(step<DDim>());
284}
285
286template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
287KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmin(
288 DiscreteDomain<DDim> const& d)
289{
290 return coordinate(d.front());
291}
292
293template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
294KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmax(
295 DiscreteDomain<DDim> const& d)
296{
297 return coordinate(d.back());
298}
299
300template <class DDim, std::enable_if_t<is_uniform_point_sampling_v<DDim>, int> = 0>
301KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rlength(
302 DiscreteDomain<DDim> const& d)
303{
304 return rmax(d) - rmin(d);
305}
306
307} // 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.