SimiLie
Loading...
Searching...
No Matches
boundary.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <array>
7
8#include <ddc/ddc.hpp>
9
10#include <similie/misc/specialization.hpp>
11
12#include "chain.hpp"
13#include "local_chain.hpp"
14
15namespace sil {
16
17namespace exterior {
18
19namespace detail {
20
21template <class SimplexType>
22struct BoundaryType;
23
24template <std::size_t K, class... Tag>
25struct BoundaryType<Simplex<K, Tag...>>
26{
27 using type = Simplex<K - 1, Tag...>;
28};
29
30template <class SimplexType, class LayoutStridedPolicy, class MemorySpace>
31struct BoundaryType<Chain<SimplexType, LayoutStridedPolicy, MemorySpace>>
32{
33 using type = Chain<typename BoundaryType<SimplexType>::type, LayoutStridedPolicy, MemorySpace>;
34};
35
36} // namespace detail
37
38template <class T>
39using boundary_t = typename detail::BoundaryType<T>::type;
40
41namespace detail {
42
43template <class MemorySpace = Kokkos::HostSpace, class SimplexType>
44KOKKOS_FUNCTION constexpr LocalChain<boundary_t<SimplexType>, Kokkos::LayoutRight, MemorySpace>
45generate_local_half_subchain(
46 typename SimplexType::discrete_element_type elem,
47 typename SimplexType::discrete_vector_type vect,
48 bool negative = false)
49{
50 auto array = ddc::detail::array(vect);
51 LocalChain<boundary_t<SimplexType>, Kokkos::LayoutRight, MemorySpace> chain(elem);
52 auto id_dist = -1;
53 for (std::size_t i = 0; i < SimplexType::dimension(); ++i) {
54 auto array_ = array;
55 auto j = array_.begin() + id_dist + 1;
56 auto id = std::find_if(j, array_.end(), [](int k) { return k != 0; });
57 id_dist = std::distance(array_.begin(), id);
58 *id = 0;
59 typename SimplexType::discrete_vector_type vect_;
60 ddc::detail::array(vect_) = array_;
61 chain += boundary_t<SimplexType>(elem, vect_, (negative + i) % 2);
62 j = id + 1;
63 }
64 return chain;
65}
66
67// TODO Kokkosify
68template <class SimplexType, misc::Specialization<Kokkos::View> AllocationType>
69KOKKOS_FUNCTION constexpr Chain<
71 typename AllocationType::array_layout,
72 typename AllocationType::memory_space>
73generate_half_subchain(
74 AllocationType allocation,
75 typename SimplexType::discrete_element_type elem,
76 typename SimplexType::discrete_vector_type vect,
77 bool negative = false)
78{
79 auto array = ddc::detail::array(vect);
81 typename AllocationType::array_layout,
82 typename AllocationType::memory_space>
83 chain(allocation);
84 auto id_dist = -1;
85 for (std::size_t i = 0; i < SimplexType::dimension(); ++i) {
86 auto array_ = array;
87 auto j = array_.begin() + id_dist + 1;
88 auto id = std::find_if(j, array_.end(), [](int k) { return k != 0; });
89 id_dist = std::distance(array_.begin(), id);
90 *id = 0;
91 typename SimplexType::discrete_vector_type vect_;
92 ddc::detail::array(vect_) = array_;
93 chain += boundary_t<SimplexType>(elem, vect_, (negative + i) % 2);
94 j = id + 1;
95 }
96 return chain;
97}
98
99} // namespace detail
100
101template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
103{
104 KOKKOS_FUNCTION static Chain<
106 typename AllocationType::array_layout,
107 typename AllocationType::memory_space>
108 operator()(AllocationType allocation, SimplexType const& simplex)
109 {
111 typename AllocationType::array_layout,
112 typename AllocationType::memory_space>
113 chain(allocation);
114 detail::generate_half_subchain<SimplexType>(
115 Kokkos::
116 subview(allocation,
117 std::pair<std::size_t, std::size_t>(0, SimplexType::dimension())),
118 simplex.discrete_element(),
119 simplex.discrete_vector(),
120 SimplexType::dimension() % 2);
121 chain += SimplexType::dimension();
122 detail::generate_half_subchain<SimplexType>(
123 Kokkos::
124 subview(allocation,
125 std::pair<std::size_t, std::size_t>(
126 SimplexType::dimension(),
127 2 * SimplexType::dimension())),
128 simplex.discrete_element() + simplex.discrete_vector(),
129 -simplex.discrete_vector());
130 chain += SimplexType::dimension();
131 chain *= (SimplexType::dimension() % 2 ? 1 : -1) * (simplex.negative() ? -1 : 1);
132 return chain;
133 }
134
135 KOKKOS_FUNCTION static Chain<
137 typename AllocationType::array_layout,
138 typename AllocationType::memory_space>
140 AllocationType allocation,
141 Chain<SimplexType,
142 typename AllocationType::array_layout,
143 typename AllocationType::memory_space> const& chain)
144 {
146 typename AllocationType::array_layout,
147 typename AllocationType::memory_space>
148 boundary_chain(allocation);
149 for (auto i = chain.begin(); i < chain.end(); ++i) {
150 std::size_t const distance = Kokkos::Experimental::distance(chain.begin(), i);
152 Kokkos::
153 subview(allocation,
154 std::pair<std::size_t, std::size_t>(
155 2 * SimplexType::dimension() * distance,
156 2 * SimplexType::dimension() * (distance + 1))),
157 *i);
158 boundary_chain += 2 * SimplexType::dimension();
159 }
160 boundary_chain.optimize();
161 return boundary_chain;
162 }
163};
164
165template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
166KOKKOS_FUNCTION Chain<
168 typename AllocationType::array_layout,
169 typename AllocationType::memory_space>
170boundary(AllocationType allocation, SimplexType const& simplex)
171{
172 return Boundary<AllocationType, SimplexType>::operator()(allocation, simplex);
173}
174
175template <class MemorySpace = Kokkos::HostSpace, class SimplexType>
176KOKKOS_FUNCTION LocalChain<boundary_t<SimplexType>, Kokkos::LayoutRight, MemorySpace> boundary(
177 SimplexType const& simplex)
178{
179 LocalChain<boundary_t<SimplexType>, Kokkos::LayoutRight, MemorySpace> chain(
180 simplex.discrete_element());
181 chain += detail::generate_local_half_subchain<MemorySpace, SimplexType>(
182 simplex.discrete_element(),
183 simplex.discrete_vector(),
184 SimplexType::dimension() % 2);
185 chain += detail::generate_local_half_subchain<MemorySpace, SimplexType>(
186 simplex.discrete_element() + simplex.discrete_vector(),
187 -simplex.discrete_vector());
188 chain *= (SimplexType::dimension() % 2 ? 1 : -1) * (simplex.negative() ? -1 : 1);
189 return chain;
190}
191
192template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
193KOKKOS_FUNCTION Chain<
195 typename AllocationType::array_layout,
196 typename AllocationType::memory_space>
198 AllocationType allocation,
199 Chain<SimplexType,
200 typename AllocationType::array_layout,
201 typename AllocationType::memory_space> const& chain)
202{
204}
205
206} // namespace exterior
207
208} // namespace sil
Chain class.
Definition chain.hpp:26
KOKKOS_FUNCTION void optimize()
Definition chain.hpp:132
Simplex class.
Definition simplex.hpp:75
Chain(Head, Tail...) -> Chain< typename Head::value_type, typename Head::array_layout, typename Head::memory_space >
typename detail::BoundaryType< T >::type boundary_t
Definition boundary.hpp:39
KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > boundary(AllocationType allocation, SimplexType const &simplex)
Definition boundary.hpp:170
The top-level namespace of SimiLie.
Definition csr.hpp:15
static KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > operator()(AllocationType allocation, Chain< SimplexType, typename AllocationType::array_layout, typename AllocationType::memory_space > const &chain)
Definition boundary.hpp:139
static KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > operator()(AllocationType allocation, SimplexType const &simplex)
Definition boundary.hpp:108