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