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>
75KOKKOS_FUNCTION Chain<
77 typename AllocationType::array_layout,
78 typename AllocationType::memory_space>
79boundary(AllocationType allocation, SimplexType simplex)
80{
82 typename AllocationType::array_layout,
83 typename AllocationType::memory_space>
84 chain(allocation);
85 detail::generate_half_subchain<SimplexType>(
86 Kokkos::
87 subview(allocation,
88 std::pair<std::size_t, std::size_t>(0, SimplexType::dimension())),
89 simplex.discrete_element(),
90 simplex.discrete_vector(),
91 SimplexType::dimension() % 2);
92 chain += SimplexType::dimension();
93 detail::generate_half_subchain<SimplexType>(
94 Kokkos::
95 subview(allocation,
96 std::pair<std::size_t, std::size_t>(
97 SimplexType::dimension(),
98 2 * SimplexType::dimension())),
99 simplex.discrete_element() + simplex.discrete_vector(),
100 -simplex.discrete_vector());
101 chain += SimplexType::dimension();
102 chain *= (SimplexType::dimension() % 2 ? 1 : -1) * (simplex.negative() ? -1 : 1);
103 return chain;
104}
105
106template <misc::Specialization<Kokkos::View> AllocationType, class SimplexType>
107KOKKOS_FUNCTION Chain<
109 typename AllocationType::array_layout,
110 typename AllocationType::memory_space>
112 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);
123 boundary(
124 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} // namespace exterior
137
138} // 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:79
The top-level namespace of SimiLie.
Definition csr.hpp:14