DDC 0.0.0

a discrete domain computation library

uniform_point_sampling.hpp
1 // SPDX-License-Identifier: MIT
2 
3 #pragma once
4 
5 #include <cassert>
6 #include <type_traits>
7 
8 #include <Kokkos_Core.hpp>
9 
10 #include "ddc/coordinate.hpp"
11 #include "ddc/detail/macros.hpp"
12 #include "ddc/discrete_domain.hpp"
13 #include "ddc/discrete_element.hpp"
14 #include "ddc/discrete_space.hpp"
15 #include "ddc/discrete_vector.hpp"
16 
17 namespace ddc {
18 
21 template <class CDim>
23 {
24 public:
26 
28 
29 
31 
33 
35 
37 
38 public:
39  template <class MemorySpace>
40  class Impl
41  {
42  template <class OMemorySpace>
43  friend class Impl;
44 
45  private:
46  continuous_element_type m_origin {0.};
47 
48  double m_step {1.};
49 
50  public:
52 
53  Impl() = default;
54 
55  Impl(Impl const&) = delete;
56 
57  template <class OriginMemorySpace>
58  explicit Impl(Impl<OriginMemorySpace> const& impl)
59  {
60  m_origin = impl.m_origin;
61  m_step = impl.m_step;
62  }
63 
64  Impl(Impl&&) = default;
65 
71  constexpr Impl(continuous_element_type origin, double step) : m_origin(origin), m_step(step)
72  {
73  assert(step > 0);
74  }
75 
84  [[deprecated(
85  "Use the version accepting a vector for n "
86  "instead.")]] constexpr Impl(continuous_element_type a, continuous_element_type b, std::size_t n)
87  : m_origin(a)
88  , m_step((b - a) / (n - 1))
89  {
90  assert(a < b);
91  assert(n > 1);
92  }
93 
101  : m_origin(a)
102  , m_step((b - a) / (n - 1))
103  {
104  assert(a < b);
105  assert(n > 1);
106  }
107 
108  ~Impl() = default;
109 
111  constexpr continuous_element_type origin() const noexcept
112  {
113  return m_origin;
114  }
115 
117  constexpr discrete_element_type front() const noexcept
118  {
119  return discrete_element_type {0};
120  }
121 
123  constexpr double step() const
124  {
125  return m_step;
126  }
127 
130  discrete_element_type const& icoord) const noexcept
131  {
132  return m_origin + continuous_element_type(icoord.uid()) * m_step;
133  }
134  };
135 
143  static std::tuple<Impl<Kokkos::HostSpace>, discrete_domain_type> init(
147  {
148  assert(a < b);
149  assert(n > 1);
150  Impl<Kokkos::HostSpace> disc(a, continuous_element_type {(b - a) / (n - 1)});
151  discrete_domain_type domain {disc.front(), n};
152  return std::make_tuple(std::move(disc), std::move(domain));
153  }
154 
164  static std::tuple<
165  Impl<Kokkos::HostSpace>,
174  discrete_vector_type n_ghosts_before,
175  discrete_vector_type n_ghosts_after)
176  {
178  assert(a < b);
179  assert(n > 1);
180  double discretization_step {(b - a) / (n - 1)};
182  disc(a - n_ghosts_before.value() * discretization_step, discretization_step);
183  discrete_domain_type ghosted_domain
184  = discrete_domain_type(disc.front(), n + n_ghosts_before + n_ghosts_after);
185  discrete_domain_type pre_ghost
186  = discrete_domain_type(ghosted_domain.front(), n_ghosts_before);
187  discrete_domain_type main_domain
188  = discrete_domain_type(ghosted_domain.front() + n_ghosts_before, n);
189  discrete_domain_type post_ghost
190  = discrete_domain_type(main_domain.back() + 1, n_ghosts_after);
191  return std::make_tuple(
192  std::move(disc),
193  std::move(main_domain),
194  std::move(ghosted_domain),
195  std::move(pre_ghost),
196  std::move(post_ghost));
197  }
198 
207  static std::tuple<
208  Impl<Kokkos::HostSpace>,
217  discrete_vector_type n_ghosts)
218  {
219  return init_ghosted(a, b, n, n_ghosts, n_ghosts);
220  }
221 };
222 
223 template <class>
224 struct is_uniform_sampling : public std::false_type
225 {
226 };
227 
228 template <class CDim>
229 struct is_uniform_sampling<UniformPointSampling<CDim>> : public std::true_type
230 {
231 };
232 
233 template <class DDim>
235 
236 
237 template <
238  class DDimImpl,
239  std::enable_if_t<
240  is_uniform_sampling_v<typename DDimImpl::discrete_dimension_type>,
241  int> = 0>
242 std::ostream& operator<<(std::ostream& out, DDimImpl const& mesh)
243 {
244  return out << "UniformPointSampling( origin=" << mesh.origin() << ", step=" << mesh.step()
245  << " )";
246 }
247 
249 template <class DDim>
250 DDC_INLINE_FUNCTION std::
251  enable_if_t<is_uniform_sampling_v<DDim>, typename DDim::continuous_element_type>
252  origin() noexcept
253 {
254  return discrete_space<DDim>().origin();
255 }
256 
258 template <class DDim>
259 DDC_INLINE_FUNCTION std::
260  enable_if_t<is_uniform_sampling_v<DDim>, typename DDim::discrete_element_type>
261  front() noexcept
262 {
263  return discrete_space<DDim>().front();
264 }
265 
267 template <class DDim>
268 DDC_INLINE_FUNCTION std::enable_if_t<is_uniform_sampling_v<DDim>, double> step() noexcept
269 {
270  return discrete_space<DDim>().step();
271 }
272 
273 template <class CDim>
274 DDC_INLINE_FUNCTION constexpr Coordinate<CDim> coordinate(
276 {
277  return discrete_space<UniformPointSampling<CDim>>().coordinate(c);
278 }
279 
280 template <class CDim>
282 {
284 }
285 
286 template <class CDim>
288 {
290 }
291 
292 template <class CDim>
294 {
295  return coordinate(d.front());
296 }
297 
298 template <class CDim>
300 {
301  return coordinate(d.back());
302 }
303 
304 template <class CDim>
306 {
307  return rmax(d) - rmin(d);
308 }
309 
310 template <class T>
311 struct is_uniform_domain : std::false_type
312 {
313 };
314 
315 template <class... DDims>
317  : std::conditional_t<(is_uniform_sampling_v<DDims> && ...), std::true_type, std::false_type>
318 {
319 };
320 
321 template <class T>
323 
324 } // namespace ddc
Definition: discrete_domain.hpp:24
constexpr discrete_element_type back() const noexcept
Definition: discrete_domain.hpp:132
constexpr discrete_element_type front() const noexcept
Definition: discrete_domain.hpp:127
A DiscreteElement identifies an element of the discrete dimension.
Definition: discrete_element.hpp:124
A DiscreteVector is a vector in the discrete dimension.
Definition: discrete_vector.hpp:208
constexpr std::enable_if_t< N==1, DiscreteVectorElement const & > value() const noexcept
Definition: discrete_vector.hpp:315
Definition: uniform_point_sampling.hpp:41
constexpr continuous_element_type origin() const noexcept
Lower bound index of the mesh.
Definition: uniform_point_sampling.hpp:111
constexpr 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:129
constexpr discrete_element_type front() const noexcept
Lower bound index of the mesh.
Definition: uniform_point_sampling.hpp:117
constexpr double step() const
Spacing step of the mesh.
Definition: uniform_point_sampling.hpp:123
constexpr Impl(continuous_element_type a, continuous_element_type b, discrete_vector_type n)
Construct a Impl from a segment and a number of points n.
Definition: uniform_point_sampling.hpp:100
constexpr Impl(continuous_element_type origin, double step)
Construct a Impl from a point and a spacing step.
Definition: uniform_point_sampling.hpp:71
Impl(Impl< OriginMemorySpace > const &impl)
Definition: uniform_point_sampling.hpp:58
constexpr Impl(continuous_element_type a, continuous_element_type b, std::size_t n)
Construct a Impl from a segment and a number of points n.
Definition: uniform_point_sampling.hpp:86
Impl(Impl const &)=delete
UniformPointSampling models a uniform discretization of the provided continuous dimension.
Definition: uniform_point_sampling.hpp:23
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:170
Coordinate< CDim > continuous_element_type
Definition: uniform_point_sampling.hpp:27
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:213
DiscreteDomain< UniformPointSampling > discrete_domain_type
Definition: uniform_point_sampling.hpp:34
CDim continuous_dimension_type
Definition: uniform_point_sampling.hpp:25
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:143
Definition: aligned_allocator.hpp:9
ddc::Coordinate< QueryDDims... > rmin(DiscreteDomain< DDims... > const &domain) noexcept
Definition: discrete_domain.hpp:270
constexpr bool is_uniform_sampling_v
Definition: uniform_point_sampling.hpp:234
ddc::Coordinate< QueryDDims... > coordinate(DiscreteDomain< DDims... > const &domain, DiscreteElement< QueryDDims... > const &icoord) noexcept
Definition: discrete_domain.hpp:261
std::ostream & operator<<(std::ostream &out, DiscreteElement<> const &)
Definition: discrete_element.hpp:300
constexpr bool is_uniform_domain_v
Definition: uniform_point_sampling.hpp:322
DDC_INLINE_FUNCTION Coordinate< CDim > distance_at_left(DiscreteElement< NonUniformPointSampling< CDim >> i)
Definition: non_uniform_point_sampling.hpp:144
DDC_INLINE_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:252
constexpr DiscreteElement< QueryDDims... > front(DiscreteDomain< DDims... > const &domain) noexcept
Definition: discrete_domain.hpp:249
DDC_INLINE_FUNCTION Coordinate< CDim > distance_at_right(DiscreteElement< NonUniformPointSampling< CDim >> i)
Definition: non_uniform_point_sampling.hpp:151
ddc_detail::TaggedVector< CoordinateElement, CDims... > Coordinate
A Coordinate represents a coordinate in the continuous space.
Definition: coordinate.hpp:19
DDC_INLINE_FUNCTION Coordinate< CDim > rlength(DiscreteDomain< NonUniformPointSampling< CDim >> const &d)
Definition: non_uniform_point_sampling.hpp:170
ddc::Coordinate< QueryDDims... > rmax(DiscreteDomain< DDims... > const &domain) noexcept
Definition: discrete_domain.hpp:276
DDC_INLINE_FUNCTION std::enable_if_t< is_uniform_sampling_v< DDim >, double > step() noexcept
Spacing step of the mesh.
Definition: uniform_point_sampling.hpp:268
Definition: uniform_point_sampling.hpp:312
Definition: uniform_point_sampling.hpp:225