DDC 0.14.0
Loading...
Searching...
No Matches
non_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 <algorithm>
8#include <cassert>
9#include <cstddef>
10#include <initializer_list>
11#include <iosfwd>
12#include <iterator>
13#include <stdexcept>
14#include <tuple>
15#include <type_traits>
16#include <utility>
17#include <vector>
18
19#include <Kokkos_Core.hpp>
20
21#include "coordinate.hpp"
26
27namespace ddc {
28
29namespace detail {
30
31struct NonUniformPointSamplingBase
32{
33};
34
35void print_non_uniform_point_samplig(std::ostream& os, std::size_t size);
36
37} // namespace detail
38
39/// `NonUniformPointSampling` models a non-uniform discretization of the `CDim` segment \f$[a, b]\f$.
40template <class CDim>
41class NonUniformPointSampling : detail::NonUniformPointSamplingBase
42{
43public:
44 using continuous_dimension_type = CDim;
45
46 using discrete_dimension_type = NonUniformPointSampling;
47
48public:
49 template <class DDim, class MemorySpace>
50 class Impl
51 {
52 template <class ODDim, class OMemorySpace>
53 friend class Impl;
54
55 Kokkos::View<Coordinate<CDim>*, MemorySpace> m_points;
56
57 DiscreteElement<DDim> m_reference;
58
59 public:
60 using discrete_dimension_type = NonUniformPointSampling;
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() = default;
69
70 /// @brief Construct a `NonUniformPointSampling` using a brace-list, i.e. `NonUniformPointSampling mesh({0., 1.})`
71 Impl(std::initializer_list<Coordinate<CDim>> const points)
72 : Impl(points.begin(), points.end())
73 {
74 }
75
76 /// @brief Construct a `NonUniformPointSampling` using a C++20 "common range".
77 template <class InputRange>
78 explicit Impl(InputRange const& points) : Impl(points.begin(), points.end())
79 {
80 }
81
82 /// @brief Construct a `NonUniformPointSampling` using a pair of iterators.
83 template <class InputIt>
84 Impl(InputIt const points_begin, InputIt const points_end)
85 : m_reference(create_reference_discrete_element<DDim>())
86 {
87 using view_type = Kokkos::View<Coordinate<CDim>*, MemorySpace>;
88 if (!std::is_sorted(points_begin, points_end)) {
89 throw std::runtime_error("Input points must be sorted");
90 }
91 // Make a contiguous copy of [points_begin, points_end[
92 std::vector<Coordinate<CDim>> host_points(points_begin, points_end);
93 m_points = view_type("NonUniformPointSampling::points", host_points.size());
94 Kokkos::deep_copy(m_points, view_type(host_points.data(), host_points.size()));
95 }
96
97 template <class OriginMemorySpace>
98 explicit Impl(Impl<DDim, OriginMemorySpace> const& impl)
99 : m_points(Kokkos::create_mirror_view_and_copy(MemorySpace(), impl.m_points))
100 , m_reference(impl.m_reference)
101 {
102 }
103
104 Impl(Impl const& x) = delete;
105
106 Impl(Impl&& x) = default;
107
108 ~Impl() = default;
109
110 Impl& operator=(Impl const& x) = delete;
111
112 Impl& operator=(Impl&& x) = default;
113
114 KOKKOS_FUNCTION std::size_t size() const
115 {
116 return m_points.size();
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 Convert a mesh index into a position in `CDim`
126 KOKKOS_FUNCTION Coordinate<CDim> coordinate(
127 discrete_element_type const& icoord) const noexcept
128 {
129 return m_points((icoord - front()).value());
130 }
131 };
132
133 /** Construct an Impl<Kokkos::HostSpace> and associated discrete_domain_type from a range
134 * containing the points coordinates along the `DDim` dimension.
135 *
136 * @param non_uniform_points a range (std::vector, std::array, ...) containing the coordinates of the points of the domain.
137 */
138 template <class DDim, class InputRange>
139 static std::tuple<typename DDim::template Impl<DDim, Kokkos::HostSpace>, DiscreteDomain<DDim>>
140 init(InputRange const& non_uniform_points)
141 {
142 assert(!non_uniform_points.empty());
143 DiscreteVector<DDim> const n(non_uniform_points.size());
144 typename DDim::template Impl<DDim, Kokkos::HostSpace> disc(non_uniform_points);
145 DiscreteDomain<DDim> domain(disc.front(), n);
146 return std::make_tuple(std::move(disc), std::move(domain));
147 }
148
149 /** Construct 4 non-uniform `DiscreteDomain` and an Impl<Kokkos::HostSpace> from 3 ranges containing the points coordinates along the `DDim` dimension.
150 *
151 * @param domain_r a range containing the coordinates of the points of the main domain along the DDim position
152 * @param pre_ghost_r a range containing the positions of the ghost points before the main domain the DDim position
153 * @param post_ghost_r a range containing the positions of the ghost points after the main domain the DDim position
154 */
155 template <class DDim, class InputRange>
156 static std::tuple<
157 typename DDim::template Impl<DDim, Kokkos::HostSpace>,
158 DiscreteDomain<DDim>,
159 DiscreteDomain<DDim>,
160 DiscreteDomain<DDim>,
161 DiscreteDomain<DDim>>
163 InputRange const& domain_r,
164 InputRange const& pre_ghost_r,
165 InputRange const& post_ghost_r)
166 {
167 assert(!domain_r.empty());
168
169 DiscreteVector<DDim> const n(domain_r.size());
170 DiscreteVector<DDim> const n_ghosts_before(pre_ghost_r.size());
171 DiscreteVector<DDim> const n_ghosts_after(post_ghost_r.size());
172
173 std::vector<typename InputRange::value_type> full_domain;
174
175 std::copy(pre_ghost_r.begin(), pre_ghost_r.end(), std::back_inserter(full_domain));
176 std::copy(domain_r.begin(), domain_r.end(), std::back_inserter(full_domain));
177 std::copy(post_ghost_r.begin(), post_ghost_r.end(), std::back_inserter(full_domain));
178
179 typename DDim::template Impl<DDim, Kokkos::HostSpace> disc(full_domain);
180
181 DiscreteDomain<DDim> ghosted_domain(disc.front(), n + n_ghosts_before + n_ghosts_after);
182 DiscreteDomain<DDim> pre_ghost = ghosted_domain.take_first(n_ghosts_before);
183 DiscreteDomain<DDim> main_domain = ghosted_domain.remove(n_ghosts_before, n_ghosts_after);
184 DiscreteDomain<DDim> post_ghost = ghosted_domain.take_last(n_ghosts_after);
185 return std::make_tuple(
186 std::move(disc),
187 std::move(main_domain),
188 std::move(ghosted_domain),
189 std::move(pre_ghost),
190 std::move(post_ghost));
191 }
192};
193
194template <class DDim>
196 : public std::is_base_of<detail::NonUniformPointSamplingBase, DDim>::type
197{
198};
199
200template <class DDim>
202
203namespace concepts {
204
205template <class DDim>
206concept non_uniform_point_sampling = is_non_uniform_point_sampling_v<DDim>;
207
208}
209
210template <class DDimImpl>
211std::ostream& operator<<(std::ostream& os, DDimImpl const& mesh)
212 requires(concepts::non_uniform_point_sampling<typename DDimImpl::discrete_dimension_type>)
213{
214 detail::print_non_uniform_point_samplig(os, mesh.size());
215 return os;
216}
217
218template <concepts::non_uniform_point_sampling DDim>
219KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> coordinate(
220 DiscreteElement<DDim> const& c)
221{
222 return discrete_space<DDim>().coordinate(c);
223}
224
225template <concepts::non_uniform_point_sampling DDim>
226KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_left(
227 DiscreteElement<DDim> i)
228{
229 return coordinate(i) - coordinate(i - 1);
230}
231
232template <concepts::non_uniform_point_sampling DDim>
233KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> distance_at_right(
234 DiscreteElement<DDim> i)
235{
236 return coordinate(i + 1) - coordinate(i);
237}
238
239template <concepts::non_uniform_point_sampling DDim>
240KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmin(
241 DiscreteDomain<DDim> const& d)
242{
243 return coordinate(d.front());
244}
245
246template <concepts::non_uniform_point_sampling DDim>
247KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rmax(
248 DiscreteDomain<DDim> const& d)
249{
250 return coordinate(d.back());
251}
252
253template <concepts::non_uniform_point_sampling DDim>
254KOKKOS_FUNCTION Coordinate<typename DDim::continuous_dimension_type> rlength(
255 DiscreteDomain<DDim> const& d)
256{
257 return rmax(d) - rmin(d);
258}
259
260} // 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...
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 > 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()
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)