SimiLie
Loading...
Searching...
No Matches
cochain.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/are_all_same.hpp>
9#include <similie/misc/specialization.hpp>
10#include <similie/tensor/antisymmetric_tensor.hpp>
11
12#include "chain.hpp"
13#include "cosimplex.hpp"
14#include "local_chain.hpp"
15
16namespace sil {
17
18namespace exterior {
19
20template <typename CochainType>
21class CochainIterator;
22
24template <
25 class ChainType,
26 class ElementType = double,
27 class LayoutStridedPolicy = Kokkos::LayoutRight>
28 requires(misc::Specialization<ChainType, Chain> || misc::Specialization<ChainType, LocalChain>)
30{
31public:
32 using memory_space = typename ChainType::memory_space;
33
34 using simplex_type = typename ChainType::simplex_type;
35 using chain_type = ChainType;
36 using element_type = ElementType;
37 using discrete_element_type = ChainType::discrete_element_type;
38 using discrete_vector_type = ChainType::discrete_vector_type;
39 using values_type = Kokkos::View<ElementType*, LayoutStridedPolicy, memory_space>;
41
42private:
43 // chain_type const& m_chain;
44 chain_type m_chain;
45 values_type m_values;
46
47public:
48 KOKKOS_DEFAULTED_FUNCTION constexpr Cochain() = default;
49
50 KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain const&) = default;
51
52 KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain&&) = default;
53
54 template <class... T>
55 requires(sizeof...(T) >= 1 && (std::is_convertible_v<T, double> && ...))
56 KOKKOS_FUNCTION constexpr explicit Cochain(
57 chain_type chain,
58 values_type allocation,
59 T... value) noexcept
60 : m_chain(std::move(chain))
61 , m_values(std::move(allocation))
62 {
63 int i = 0;
64 ((m_values(i++) = value), ...);
65 assert(sizeof...(T) == chain.size()
66 && "cochain constructor must get as much values as the chain contains simplices");
67 }
68
69 KOKKOS_FUNCTION constexpr explicit Cochain(
70 chain_type const& chain,
71 values_type const& values) noexcept
72 : m_chain(std::move(chain))
73 , m_values(std::move(values))
74 {
75 assert(values.size() == chain.size()
76 && "cochain constructor must get as much values as the chain contains simplices");
77 }
78
79 template <tensor::TensorIndex Index>
82 KOKKOS_FUNCTION constexpr explicit Cochain(
83 chain_type const& chain,
86 ddc::DiscreteDomain<Index>,
87 ddc::detail::kokkos_to_mdspan_layout_t<LayoutStridedPolicy>,
88 memory_space> tensor) noexcept
89 : m_chain(std::move(chain))
90 , m_values(tensor.allocation_kokkos_view())
91 {
92 assert(m_values.size() == chain.size()
93 && "cochain constructor must get as much values as the chain contains simplices");
94 }
95
96 KOKKOS_DEFAULTED_FUNCTION ~Cochain() = default;
97
98 KOKKOS_DEFAULTED_FUNCTION Cochain& operator=(Cochain const& other) = default;
99
100 KOKKOS_DEFAULTED_FUNCTION Cochain& operator=(Cochain&& other) = default;
101
102 static KOKKOS_FUNCTION constexpr bool is_local() noexcept
103 {
104 return chain_type::is_local();
105 }
106
107 static KOKKOS_FUNCTION constexpr std::size_t dimension() noexcept
108 {
109 return chain_type::dimension();
110 }
111
112 KOKKOS_FUNCTION std::size_t size() noexcept
113 {
114 return m_chain.size();
115 }
116
117 KOKKOS_FUNCTION std::size_t size() const noexcept
118 {
119 return m_chain.size();
120 }
121
122 KOKKOS_FUNCTION std::size_t allocation_size() noexcept
123 {
124 return m_chain.allocation_size();
125 }
126
127 KOKKOS_FUNCTION std::size_t allocation_size() const noexcept
128 {
129 return m_chain.allocation_size();
130 }
131
132 KOKKOS_FUNCTION chain_type const& chain() const noexcept
133 {
134 return m_chain;
135 }
136
137 KOKKOS_FUNCTION chain_type const& values() const noexcept
138 {
139 return m_values;
140 }
141
143
144 KOKKOS_FUNCTION auto begin()
145 {
146 return iterator(m_chain.begin(), Kokkos::Experimental::begin(m_values));
147 }
148
149 KOKKOS_FUNCTION auto begin() const
150 {
151 return iterator(m_chain.begin(), Kokkos::Experimental::begin(m_values));
152 }
153
154 KOKKOS_FUNCTION auto end()
155 {
156 return iterator(m_chain.end(), Kokkos::Experimental::end(m_values));
157 }
158
159 KOKKOS_FUNCTION auto end() const
160 {
161 return iterator(m_chain.end(), Kokkos::Experimental::end(m_values));
162 }
163
164 KOKKOS_FUNCTION auto cbegin() const
165 {
166 return iterator(m_chain.begin(), Kokkos::Experimental::begin(m_values));
167 }
168
169 KOKKOS_FUNCTION auto cend() const
170 {
171 return iterator(m_chain.end(), Kokkos::Experimental::end(m_values));
172 }
173
174 KOKKOS_FUNCTION Cosimplex<simplex_type, element_type>& operator[](std::size_t i) noexcept
175 {
176 assert(i < size());
177 return Cosimplex<simplex_type, element_type>(m_chain(i), m_values(i));
178 }
179
181 std::size_t i) const noexcept
182 {
183 assert(i < size());
184 return Cosimplex<simplex_type, element_type>(m_chain(i), m_values(i));
185 }
186
187 KOKKOS_FUNCTION element_type integrate() noexcept
188 {
189 element_type out = 0;
190 for (auto i = begin(); i < end(); ++i) {
192 out += ((*i).negative() ? -1 : 1) * (*i).value();
194 out += (m_chain.negative() ? -1 : 1) * (*i).value(); // always false
195 }
196 }
197 return out;
198 }
199
200 KOKKOS_FUNCTION element_type const integrate() const noexcept
201 {
202 element_type out = 0;
203 for (auto i = begin(); i < end(); ++i) {
205 out += ((*i).negative() ? -1 : 1) * (*i).value();
207 out += (m_chain.negative() ? -1 : 1) * (*i).value(); // always false
208 }
209 }
210 return out;
211 }
212};
213
214template <class ChainType, misc::Specialization<tensor::Tensor> TensorType>
215Cochain(ChainType, TensorType) -> Cochain<
216 ChainType,
217 typename TensorType::value_type,
218 ddc::detail::mdspan_to_kokkos_layout_t<typename TensorType::layout_type>>;
219
220template <typename CochainType>
222{
223public:
224 using iterator_category = std::random_access_iterator_tag;
225 using value_type = typename CochainType::cosimplex_type;
226 using difference_type = std::ptrdiff_t;
229
230 using cosimplex_type = typename CochainType::cosimplex_type;
231
232 using chain_iterator = typename CochainType::chain_type::iterator_type;
234 = Kokkos::Experimental::Impl::RandomAccessIterator<typename CochainType::values_type>;
235
236private:
237 chain_iterator m_chain;
238 values_iterator m_values;
239
240public:
241 KOKKOS_DEFAULTED_FUNCTION CochainIterator() = default;
243 KOKKOS_FUNCTION constexpr explicit CochainIterator(
244 chain_iterator chain_it,
245 values_iterator values_it)
246 : m_chain(chain_it)
247 , m_values(values_it)
248 {
249 }
251 KOKKOS_FUNCTION constexpr value_type operator*() const noexcept
252 {
253 if constexpr (!CochainType::chain_type::is_local()) {
254 return cosimplex_type(*m_chain, *m_values);
255 } else {
256 return cosimplex_type(
257 Simplex(std::integral_constant<std::size_t, CochainType::dimension()> {},
259 *m_chain),
260 *m_values);
261 }
262 }
264 KOKKOS_FUNCTION constexpr CochainIterator& operator++()
265 {
266 ++m_chain;
267 ++m_values;
268 return *this;
269 }
271 KOKKOS_FUNCTION constexpr CochainIterator operator++(int)
272 {
273 auto tmp = *this;
274 ++*this;
275 return tmp;
276 }
278 KOKKOS_FUNCTION constexpr CochainIterator& operator--()
279 {
280 --m_chain;
281 --m_values;
282 return *this;
283 }
285 KOKKOS_FUNCTION constexpr CochainIterator operator--(int)
286 {
287 auto tmp = *this;
288 --*this;
289 return tmp;
290 }
292 KOKKOS_FUNCTION constexpr CochainIterator& operator+=(difference_type n)
293 {
294 m_chain += n;
295 m_values += n;
296 return *this;
297 }
299 KOKKOS_FUNCTION constexpr CochainIterator& operator-=(difference_type n)
300 {
301 m_chain -= n;
302 m_values -= n;
303 return *this;
304 }
305
306 /*
307 KOKKOS_FUNCTION constexpr value_type operator[](difference_type n) const
308 {
309 return m_value + n;
310 }
311 */
313 friend KOKKOS_FUNCTION constexpr bool operator==(
314 CochainIterator const& xx,
315 CochainIterator const& yy)
316 {
317 return xx.m_chain == yy.m_chain && xx.m_values == yy.m_values;
318 }
320 friend KOKKOS_FUNCTION constexpr bool operator<(
321 CochainIterator const& xx,
322 CochainIterator const& yy)
323 {
324 return xx.m_chain < yy.m_chain && xx.m_values < yy.m_values;
325 }
327 friend KOKKOS_FUNCTION constexpr bool operator>(
328 CochainIterator const& xx,
329 CochainIterator const& yy)
330 {
331 return xx.m_chain > yy.m_chain && xx.m_values > yy.m_values;
332 }
334 friend KOKKOS_FUNCTION constexpr bool operator<=(
335 CochainIterator const& xx,
336 CochainIterator const& yy)
337 {
338 return !(yy < xx);
339 }
341 friend KOKKOS_FUNCTION constexpr bool operator>=(
342 CochainIterator const& xx,
343 CochainIterator const& yy)
344 {
345 return !(xx < yy);
346 }
348 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
349 {
350 return i += n;
351 }
353 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(difference_type n, CochainIterator i)
354 {
355 return i += n;
356 }
358 friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
359 {
360 return i -= n;
361 }
363 friend KOKKOS_FUNCTION constexpr difference_type operator-(
364 CochainIterator const& xx,
365 CochainIterator const& yy)
366 {
367 return xx.m_chain - yy.m_chain;
368 }
369};
370
371template <misc::Specialization<Cochain> CochainType>
372std::ostream& operator<<(std::ostream& out, CochainType const& cochain)
373{
374 out << "[\n";
375 for (auto i = cochain.begin(); i < cochain.end(); ++i) {
376 if constexpr (!cochain.is_local()) {
377 out << " " << (*i).simplex() << " : " << (*i).value() << "\n";
378 } else {
379 out << " -> " << (*i).discrete_vector() << " : " << (*i).value() << "\n";
380 }
381 }
382
383 out << "]";
384
385 return out;
386}
387
388} // namespace exterior
389
390} // namespace sil
friend KOKKOS_FUNCTION constexpr bool operator<(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:319
typename CochainType::cosimplex_type cosimplex_type
Definition cochain.hpp:230
typename CochainType::cosimplex_type value_type
Definition cochain.hpp:225
KOKKOS_FUNCTION constexpr CochainIterator & operator-=(difference_type n)
Definition cochain.hpp:298
friend KOKKOS_FUNCTION constexpr bool operator<=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:333
KOKKOS_FUNCTION constexpr CochainIterator & operator++()
Definition cochain.hpp:263
KOKKOS_FUNCTION constexpr CochainIterator & operator--()
Definition cochain.hpp:277
friend KOKKOS_FUNCTION constexpr bool operator>=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:340
KOKKOS_FUNCTION constexpr CochainIterator & operator+=(difference_type n)
Definition cochain.hpp:291
std::random_access_iterator_tag iterator_category
Definition cochain.hpp:224
KOKKOS_DEFAULTED_FUNCTION CochainIterator()=default
typename CochainType::chain_type::iterator_type chain_iterator
Definition cochain.hpp:232
friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
Definition cochain.hpp:357
Kokkos::Experimental::Impl::RandomAccessIterator< typename CochainType::values_type > values_iterator
Definition cochain.hpp:233
friend KOKKOS_FUNCTION constexpr bool operator>(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:326
friend KOKKOS_FUNCTION constexpr bool operator==(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:312
friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
Definition cochain.hpp:347
KOKKOS_FUNCTION constexpr value_type operator*() const noexcept
Definition cochain.hpp:250
Cochain class.
Definition cochain.hpp:30
KOKKOS_DEFAULTED_FUNCTION constexpr Cochain()=default
Kokkos::View< ElementType *, LayoutStridedPolicy, memory_space > values_type
Definition cochain.hpp:39
static KOKKOS_FUNCTION constexpr bool is_local() noexcept
Definition cochain.hpp:102
KOKKOS_FUNCTION std::size_t allocation_size() const noexcept
Definition cochain.hpp:127
KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain const &)=default
KOKKOS_FUNCTION auto end()
Definition cochain.hpp:154
KOKKOS_FUNCTION auto begin()
Definition cochain.hpp:144
static KOKKOS_FUNCTION constexpr std::size_t dimension() noexcept
Definition cochain.hpp:107
KOKKOS_DEFAULTED_FUNCTION Cochain & operator=(Cochain &&other)=default
KOKKOS_FUNCTION chain_type const & chain() const noexcept
Definition cochain.hpp:132
KOKKOS_DEFAULTED_FUNCTION Cochain & operator=(Cochain const &other)=default
KOKKOS_FUNCTION auto end() const
Definition cochain.hpp:159
ChainType::discrete_element_type discrete_element_type
Definition cochain.hpp:37
KOKKOS_FUNCTION Cosimplex< simplex_type, element_type > & operator[](std::size_t i) noexcept
Definition cochain.hpp:174
KOKKOS_FUNCTION chain_type const & values() const noexcept
Definition cochain.hpp:137
KOKKOS_FUNCTION constexpr Cochain(chain_type const &chain, tensor::Tensor< element_type, ddc::DiscreteDomain< Index >, ddc::detail::kokkos_to_mdspan_layout_t< LayoutStridedPolicy >, memory_space > tensor) noexcept
Definition cochain.hpp:82
KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain &&)=default
typename ChainType::simplex_type simplex_type
Definition cochain.hpp:34
KOKKOS_FUNCTION std::size_t size() noexcept
Definition cochain.hpp:112
KOKKOS_FUNCTION element_type integrate() noexcept
Definition cochain.hpp:187
KOKKOS_FUNCTION std::size_t size() const noexcept
Definition cochain.hpp:117
ChainType::discrete_vector_type discrete_vector_type
Definition cochain.hpp:38
KOKKOS_FUNCTION element_type const integrate() const noexcept
Definition cochain.hpp:200
KOKKOS_FUNCTION Cosimplex< simplex_type, element_type > const & operator[](std::size_t i) const noexcept
Definition cochain.hpp:180
KOKKOS_FUNCTION auto cend() const
Definition cochain.hpp:169
KOKKOS_FUNCTION auto begin() const
Definition cochain.hpp:149
KOKKOS_DEFAULTED_FUNCTION ~Cochain()=default
KOKKOS_FUNCTION std::size_t allocation_size() noexcept
Definition cochain.hpp:122
KOKKOS_FUNCTION constexpr Cochain(chain_type const &chain, values_type const &values) noexcept
Definition cochain.hpp:69
KOKKOS_FUNCTION auto cbegin() const
Definition cochain.hpp:164
typename ChainType::memory_space memory_space
Definition cochain.hpp:32
ElementType element_type
Definition cochain.hpp:36
Cosimplex class.
Definition cosimplex.hpp:20
std::ostream & operator<<(std::ostream &out, ChainType const &chain)
Definition chain.hpp:304
Cochain(ChainType, TensorType) -> Cochain< ChainType, typename TensorType::value_type, ddc::detail::mdspan_to_kokkos_layout_t< typename TensorType::layout_type > >
Simplex(ddc::DiscreteElement< Tag... >, ddc::DiscreteVector< T... >) -> Simplex< sizeof...(T), Tag... >
constexpr T filled_struct(ElementType const n=0)
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
The top-level namespace of SimiLie.
Definition csr.hpp:14