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