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