SimiLie
Loading...
Searching...
No Matches
boundary.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8#include <similie/misc/specialization.hpp>
9
10#include "chain.hpp"
11
12namespace sil {
13
14namespace exterior {
15
16namespace detail {
17
18template <class SimplexType>
19struct BoundaryType;
20
21template <std::size_t K, class... Tag>
22struct BoundaryType<Simplex<K, Tag...>>
23{
24 using type = Simplex<K - 1, Tag...>;
25};
26
27template <class SimplexType, class LayoutStridedPolicy, class MemorySpace>
28struct BoundaryType<Chain<SimplexType, LayoutStridedPolicy, MemorySpace>>
29{
30 using type = Chain<typename BoundaryType<SimplexType>::type, LayoutStridedPolicy, MemorySpace>;
31};
32
33} // namespace detail
34
35template <class T>
36using boundary_t = typename detail::BoundaryType<T>::type;
37
38namespace detail {
39
40// TODO Kokkosify
41template <class SimplexType, misc::Specialization<Kokkos::View> AllocationType>
42KOKKOS_FUNCTION constexpr Chain<
44 typename AllocationType::array_layout,
45 typename AllocationType::memory_space>
46generate_half_subchain(
47 AllocationType allocation,
48 typename SimplexType::discrete_element_type elem,
49 typename SimplexType::discrete_vector_type vect,
50 bool negative = false)
51{
52 auto array = ddc::detail::array(vect);
54 typename AllocationType::array_layout,
55 typename AllocationType::memory_space>
56 chain(allocation);
57 auto id_dist = -1;
58 for (std::size_t i = 0; i < SimplexType::dimension(); ++i) {
59 auto array_ = array;
60 auto j = array_.begin() + id_dist + 1;
61 auto id = std::find_if(j, array_.end(), [](int k) { return k != 0; });
62 id_dist = std::distance(array_.begin(), id);
63 *id = 0;
64 typename SimplexType::discrete_vector_type vect_;
65 ddc::detail::array(vect_) = array_;
66 chain += boundary_t<SimplexType>(elem, vect_, (negative + i) % 2);
67 j = id + 1;
68 }
69 return chain;
70}
71
72} // namespace detail
73
74template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
76{
77 KOKKOS_FUNCTION static Chain<
79 typename AllocationType::array_layout,
80 typename AllocationType::memory_space>
81 run(AllocationType allocation, SimplexType simplex)
82 {
84 typename AllocationType::array_layout,
85 typename AllocationType::memory_space>
86 chain(allocation);
87 detail::generate_half_subchain<SimplexType>(
88 Kokkos::
89 subview(allocation,
90 std::pair<std::size_t, std::size_t>(0, SimplexType::dimension())),
91 simplex.discrete_element(),
92 simplex.discrete_vector(),
93 SimplexType::dimension() % 2);
94 chain += SimplexType::dimension();
95 detail::generate_half_subchain<SimplexType>(
96 Kokkos::
97 subview(allocation,
98 std::pair<std::size_t, std::size_t>(
99 SimplexType::dimension(),
100 2 * SimplexType::dimension())),
101 simplex.discrete_element() + simplex.discrete_vector(),
102 -simplex.discrete_vector());
103 chain += SimplexType::dimension();
104 chain *= (SimplexType::dimension() % 2 ? 1 : -1) * (simplex.negative() ? -1 : 1);
105 return chain;
106 }
107
108 KOKKOS_FUNCTION static Chain<
110 typename AllocationType::array_layout,
111 typename AllocationType::memory_space>
112 run(AllocationType allocation,
113 Chain<SimplexType,
114 typename AllocationType::array_layout,
115 typename AllocationType::memory_space> chain)
116 {
118 typename AllocationType::array_layout,
119 typename AllocationType::memory_space>
120 boundary_chain(allocation);
121 for (auto i = chain.begin(); i < chain.end(); ++i) {
122 std::size_t const distance = Kokkos::Experimental::distance(chain.begin(), i);
124 run(Kokkos::
125 subview(allocation,
126 std::pair<std::size_t, std::size_t>(
127 2 * SimplexType::dimension() * distance,
128 2 * SimplexType::dimension() * (distance + 1))),
129 *i);
130 boundary_chain += 2 * SimplexType::dimension();
131 }
132 boundary_chain.optimize();
133 return boundary_chain;
134 }
135};
136
137template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
138KOKKOS_FUNCTION Chain<
140 typename AllocationType::array_layout,
141 typename AllocationType::memory_space>
142boundary(AllocationType allocation, SimplexType simplex)
143{
144 return Boundary<AllocationType, SimplexType>::run(allocation, simplex);
145}
146
147template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
148KOKKOS_FUNCTION Chain<
150 typename AllocationType::array_layout,
151 typename AllocationType::memory_space>
153 AllocationType allocation,
154 Chain<SimplexType,
155 typename AllocationType::array_layout,
156 typename AllocationType::memory_space> chain)
157{
158 return Boundary<AllocationType, SimplexType>::run(allocation, chain);
159}
160
161} // namespace exterior
162
163} // 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:36
KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > boundary(AllocationType allocation, SimplexType simplex)
Definition boundary.hpp:142
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 > run(AllocationType allocation, Chain< SimplexType, typename AllocationType::array_layout, typename AllocationType::memory_space > chain)
Definition boundary.hpp:112
static KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > run(AllocationType allocation, SimplexType simplex)
Definition boundary.hpp:81