DDC 0.0.0

a discrete domain computation library

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