DDC 0.16.0
Loading...
Searching...
No Matches
periodic_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 <cstddef>
9#include <iosfwd>
10#include <tuple>
11#include <type_traits>
12#include <utility>
13
14#include <Kokkos_Core.hpp>
15
16#include "coordinate.hpp"
22#include "real_type.hpp"
23
24namespace ddc {
25
26namespace detail {
27
28struct PeriodicSamplingBase
29{
30};
31
32void print_periodic_sampling(std::ostream& os, CoordinateElement origin, Real step);
33
34} // namespace detail
35
36/** PeriodicSampling models a periodic discretization of the provided continuous dimension
37 */
38template <class CDim>
40 : detail::PeriodicSamplingBase
42{
43public:
44 using continuous_dimension_type = CDim;
45
46 using discrete_dimension_type = PeriodicSampling;
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 std::size_t m_n_period;
61
62 DiscreteElement<DDim> m_reference;
63
64 public:
65 using discrete_dimension_type = PeriodicSampling;
66
67 using discrete_domain_type = DiscreteDomain<DDim>;
68
69 using discrete_element_type = DiscreteElement<DDim>;
70
71 using discrete_vector_type = DiscreteVector<DDim>;
72
73 Impl() noexcept
74 : m_origin(0)
75 , m_step(1)
76 , m_n_period(2)
77 , m_reference(create_reference_discrete_element<DDim>())
78 {
79 }
80
81 Impl(Impl const&) = delete;
82
83 template <class OriginMemorySpace>
84 explicit Impl(Impl<DDim, OriginMemorySpace> const& impl)
85 : m_origin(impl.m_origin)
86 , m_step(impl.m_step)
87 , m_n_period(impl.m_n_period)
88 , m_reference(impl.m_reference)
89 {
90 }
91
92 Impl(Impl&&) = default;
93
94 /** @brief Construct a `Impl` from a point and a spacing step.
95 *
96 * @param origin the real coordinate of mesh coordinate 0
97 * @param step the real distance between two points of mesh distance 1
98 * @param n_period the number of steps in a period
99 */
100 Impl(Coordinate<CDim> origin, Real step, std::size_t n_period)
101 : m_origin(origin)
102 , m_step(step)
103 , m_n_period(n_period)
104 , m_reference(create_reference_discrete_element<DDim>())
105 {
106 assert(step > 0);
107 assert(n_period > 0);
108 }
109
110 ~Impl() = default;
111
112 Impl& operator=(Impl const& x) = delete;
113
114 Impl& operator=(Impl&& x) = default;
115
116 /// @brief Lower bound index of the mesh
117 KOKKOS_FUNCTION Coordinate<CDim> origin() const noexcept
118 {
119 return m_origin;
120 }
121
122 /// @brief Lower bound index of the mesh
123 KOKKOS_FUNCTION discrete_element_type front() const noexcept
124 {
125 return m_reference;
126 }
127
128 /// @brief Spacing step of the mesh
129 KOKKOS_FUNCTION Real step() const
130 {
131 return m_step;
132 }
133
134 /// @brief Number of steps in a period
135 KOKKOS_FUNCTION std::size_t n_period() const
136 {
137 return m_n_period;
138 }
139
140 /// @brief Convert a mesh index into a position in `CDim`
141 KOKKOS_FUNCTION Coordinate<CDim> coordinate(
142 discrete_element_type const& icoord) const noexcept
143 {
144 return m_origin
145 + Coordinate<CDim>(
146 static_cast<int>(((icoord - front()) + m_n_period / 2) % m_n_period)
147 - static_cast<int>(m_n_period / 2))
148 * m_step;
149 }
150 };
151
152 /** Construct a Impl<Kokkos::HostSpace> and associated discrete_domain_type from a segment
153 * \f$[a, b] \subset [a, +\infty[\f$ and a number of points `n`.
154 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
155 * One should expect usual floating point rounding errors.
156 *
157 * @param a coordinate of the first point of the domain
158 * @param b coordinate of the last point of the domain
159 * @param n number of points to map on the segment \f$[a, b]\f$ including a & b
160 * @param n_period the number of steps in a period
161 */
162 template <concepts::discrete_dimension DDim>
163 static std::tuple<typename DDim::template Impl<DDim, Kokkos::HostSpace>, DiscreteDomain<DDim>>
164 init(Coordinate<CDim> a,
165 Coordinate<CDim> b,
166 DiscreteVector<DDim> n,
167 DiscreteVector<DDim> n_period)
168 {
169 assert(a < b);
170 assert(n > 1);
171 assert(n_period > 1);
172 typename DDim::template Impl<DDim, Kokkos::HostSpace>
173 disc(a, Coordinate<CDim>((b - a) / (n - 1)), n_period);
174 DiscreteDomain<DDim> domain(disc.front(), n);
175 return std::make_tuple(std::move(disc), std::move(domain));
176 }
177
178 /** Construct a periodic `DiscreteDomain` from a segment \f$[a, b] \subset [a, +\infty[\f$ and a
179 * number of points `n`.
180 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
181 * One should expect usual floating point rounding errors.
182 *
183 * @param a coordinate of the first point of the domain
184 * @param b coordinate of the last point of the domain
185 * @param n the number of points to map the segment \f$[a, b]\f$ including a & b
186 * @param n_period the number of steps in a period
187 * @param n_ghosts_before number of additional "ghost" points before the segment
188 * @param n_ghosts_after number of additional "ghost" points after the segment
189 */
190 template <concepts::discrete_dimension DDim>
191 std::tuple<
192 Impl<DDim, Kokkos::HostSpace>,
193 DiscreteDomain<DDim>,
194 DiscreteDomain<DDim>,
195 DiscreteDomain<DDim>,
196 DiscreteDomain<DDim>>
198 Coordinate<CDim> a,
199 Coordinate<CDim> b,
200 DiscreteVector<DDim> n,
201 DiscreteVector<DDim> n_period,
202 DiscreteVector<DDim> n_ghosts_before,
203 DiscreteVector<DDim> n_ghosts_after)
204 {
205 assert(a < b);
206 assert(n > 1);
207 assert(n_period > 1);
208 Real const discretization_step = (b - a) / (n - 1);
209 Impl<DDim, Kokkos::HostSpace>
210 disc(a - n_ghosts_before.value() * discretization_step,
211 discretization_step,
212 n_period);
213 DiscreteDomain<DDim> ghosted_domain(disc.front(), n + n_ghosts_before + n_ghosts_after);
214 DiscreteDomain<DDim> pre_ghost = ghosted_domain.take_first(n_ghosts_before);
215 DiscreteDomain<DDim> main_domain = ghosted_domain.remove(n_ghosts_before, n_ghosts_after);
216 DiscreteDomain<DDim> post_ghost = ghosted_domain.take_last(n_ghosts_after);
217 return std::make_tuple(
218 std::move(disc),
219 std::move(main_domain),
220 std::move(ghosted_domain),
221 std::move(pre_ghost),
222 std::move(post_ghost));
223 }
224
225 /** Construct a periodic `DiscreteDomain` from a segment \f$[a, b] \subset [a, +\infty[\f$ and a
226 * number of points `n`.
227 * Note that there is no guarantee that either the boundaries a or b will be exactly represented in the sampling.
228 * One should expect usual floating point rounding errors.
229 *
230 * @param a coordinate of the first point of the domain
231 * @param b coordinate of the last point of the domain
232 * @param n the number of points to map the segment \f$[a, b]\f$ including a & b
233 * @param n_period the number of steps in a period
234 * @param n_ghosts number of additional "ghost" points before and after the segment
235 */
236 template <concepts::discrete_dimension DDim>
237 std::tuple<
238 Impl<DDim, Kokkos::HostSpace>,
239 DiscreteDomain<DDim>,
240 DiscreteDomain<DDim>,
241 DiscreteDomain<DDim>,
242 DiscreteDomain<DDim>>
244 Coordinate<CDim> a,
245 Coordinate<CDim> b,
246 DiscreteVector<DDim> n,
247 DiscreteVector<DDim> n_period,
248 DiscreteVector<DDim> n_ghosts)
249 {
250 return init_ghosted(a, b, n, n_period, n_ghosts, n_ghosts);
251 }
252};
253
254template <class T>
255struct is_periodic_sampling : public std::is_base_of<detail::PeriodicSamplingBase, T>::type
256{
257};
258
259template <class T>
261
262namespace concepts {
263
264template <class T>
265concept periodic_sampling = discrete_dimension<T> && is_periodic_sampling_v<T>;
266
267}
268
269template <class DDimImpl>
270std::ostream& operator<<(std::ostream& os, DDimImpl const& mesh)
271 requires(concepts::periodic_sampling<typename DDimImpl::discrete_dimension_type>)
272{
273 detail::print_periodic_sampling(os, mesh.origin(), mesh.step());
274 return os;
275}
276
277/// @brief Lower bound index of the mesh
278template <concepts::periodic_sampling DDim>
279KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> origin() noexcept
280{
281 return discrete_space<DDim>().origin();
282}
283
284/// @brief Lower bound index of the mesh
285template <concepts::periodic_sampling DDim>
286KOKKOS_FUNCTION DiscreteElement<DDim> front() noexcept
287{
288 return discrete_space<DDim>().front();
289}
290
291/// @brief Spacing step of the mesh
292template <concepts::periodic_sampling DDim>
293KOKKOS_FUNCTION Real step() noexcept
294{
295 return discrete_space<DDim>().step();
296}
297
298template <concepts::periodic_sampling DDim>
299KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> coordinate(
300 DiscreteElement<DDim> const& c)
301{
302 return discrete_space<DDim>().coordinate(c);
303}
304
305template <concepts::periodic_sampling DDim>
306KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_left(
307 DiscreteElement<DDim>)
308{
309 return Coordinate<typename DDim::continuous_dimension_type>(step<DDim>());
310}
311
312template <concepts::periodic_sampling DDim>
313KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_right(
314 DiscreteElement<DDim>)
315{
316 return Coordinate<typename DDim::continuous_dimension_type>(step<DDim>());
317}
318
319template <concepts::periodic_sampling DDim>
320KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmin(
321 DiscreteDomain<DDim> const& d)
322{
323 return coordinate(d.front());
324}
325
326template <concepts::periodic_sampling DDim>
327KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmax(
328 DiscreteDomain<DDim> const& d)
329{
330 return coordinate(d.back());
331}
332
333template <concepts::periodic_sampling DDim>
334KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rlength(
335 DiscreteDomain<DDim> const& d)
336{
337 return rmax(d) - rmin(d);
338}
339
340} // 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...
Impl & operator=(Impl &&x)=default
KOKKOS_FUNCTION Coordinate< CDim > origin() const noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION discrete_element_type front() const noexcept
Lower bound index 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(Impl const &)=delete
KOKKOS_FUNCTION std::size_t n_period() const
Number of steps in a period.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Impl(Coordinate< CDim > origin, Real step, std::size_t n_period)
Construct a Impl from a point and a spacing step.
KOKKOS_FUNCTION Real step() const
Spacing step of the mesh.
Impl & operator=(Impl const &x)=delete
PeriodicSampling models a periodic 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, DiscreteVector< DDim > n_period)
Construct a Impl<Kokkos::HostSpace> and associated discrete_domain_type from a segment and a number ...
std::tuple< 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_period, DiscreteVector< DDim > n_ghosts_before, DiscreteVector< DDim > n_ghosts_after)
Construct a periodic DiscreteDomain from a segment and a number of points n.
std::tuple< 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_period, DiscreteVector< DDim > n_ghosts)
Construct a periodic DiscreteDomain from a segment and a number of points n.
ScopeGuard & operator=(ScopeGuard const &x)=delete
~ScopeGuard() noexcept
ScopeGuard(int argc, char **&argv)
ScopeGuard(ScopeGuard &&x) noexcept=delete
ScopeGuard & operator=(ScopeGuard &&x) noexcept=delete
ScopeGuard(ScopeGuard const &x)=delete
The top-level namespace of DDC.
constexpr bool is_non_uniform_point_sampling_v
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rlength(DiscreteDomain< DDim > const &d)
bool is_discrete_space_initialized() noexcept
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > coordinate(DiscreteElement< DDim > const &c)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_left(DiscreteElement< DDim > i)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > origin() noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmin(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_right(DiscreteElement< DDim > i)
void init_discrete_space(Args &&... args)
Initialize (emplace) a global singleton discrete space.
detail::ddim_impl_t< DDim, Kokkos::HostSpace > const & host_discrete_space()
constexpr bool is_periodic_sampling_v
KOKKOS_FUNCTION Real step() noexcept
Spacing step of the mesh.
KOKKOS_FUNCTION DiscreteElement< DDim > front() noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION detail::ddim_impl_t< DDim, MemorySpace > const & discrete_space()
Arg0 init_discrete_space(std::tuple< DDimImpl, Arg0 > &&a)
Move construct a global singleton discrete space and pass through the other argument.
std::tuple< Arg0, Arg1, Args... > init_discrete_space(std::tuple< DDimImpl, Arg0, Arg1, Args... > &&a)
Move construct a global singleton discrete space and pass through remaining arguments.
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmax(DiscreteDomain< DDim > const &d)