SimiLie
Loading...
Searching...
No Matches
cochain.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
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 += (i->negative() ? -1 : 1) * i->value();
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 += (i->negative() ? -1 : 1) * i->value();
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 reference operator*() const noexcept
252 {
253 return cosimplex_type(*m_chain, *m_values);
254 }
256 KOKKOS_FUNCTION constexpr pointer operator->() const noexcept
257 {
258 return **this;
259 }
261 KOKKOS_FUNCTION constexpr CochainIterator& operator++()
262 {
263 ++m_chain;
264 ++m_values;
265 return *this;
266 }
268 KOKKOS_FUNCTION constexpr CochainIterator operator++(int)
269 {
270 auto tmp = *this;
271 ++*this;
272 return tmp;
273 }
275 KOKKOS_FUNCTION constexpr CochainIterator& operator--()
276 {
277 --m_chain;
278 --m_values;
279 return *this;
280 }
282 KOKKOS_FUNCTION constexpr CochainIterator operator--(int)
283 {
284 auto tmp = *this;
285 --*this;
286 return tmp;
287 }
289 KOKKOS_FUNCTION constexpr CochainIterator& operator+=(difference_type n)
290 {
291 m_chain += n;
292 m_values += n;
293 return *this;
294 }
296 KOKKOS_FUNCTION constexpr CochainIterator& operator-=(difference_type n)
297 {
298 m_chain -= n;
299 m_values -= n;
300 return *this;
301 }
303 friend KOKKOS_FUNCTION constexpr bool operator==(
304 CochainIterator const& xx,
305 CochainIterator const& yy)
306 {
307 return xx.m_chain == yy.m_chain && xx.m_values == yy.m_values;
308 }
310 friend KOKKOS_FUNCTION constexpr bool operator<(
311 CochainIterator const& xx,
312 CochainIterator const& yy)
313 {
314 return xx.m_chain < yy.m_chain && xx.m_values < yy.m_values;
315 }
317 friend KOKKOS_FUNCTION constexpr bool operator>(
318 CochainIterator const& xx,
319 CochainIterator const& yy)
320 {
321 return xx.m_chain > yy.m_chain && xx.m_values > yy.m_values;
322 }
324 friend KOKKOS_FUNCTION constexpr bool operator<=(
325 CochainIterator const& xx,
326 CochainIterator const& yy)
327 {
328 return !(yy < xx);
329 }
331 friend KOKKOS_FUNCTION constexpr bool operator>=(
332 CochainIterator const& xx,
333 CochainIterator const& yy)
334 {
335 return !(xx < yy);
336 }
338 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
339 {
340 return i += n;
341 }
343 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(difference_type n, CochainIterator i)
344 {
345 return i += n;
346 }
348 friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
349 {
350 return i -= n;
351 }
353 friend KOKKOS_FUNCTION constexpr difference_type operator-(
354 CochainIterator const& xx,
355 CochainIterator const& yy)
356 {
357 return xx.m_chain - yy.m_chain;
358 }
359};
360
361template <misc::Specialization<Cochain> CochainType>
362std::ostream& operator<<(std::ostream& out, CochainType const& cochain)
363{
364 out << "[\n";
365 for (auto i = cochain.begin(); i < cochain.end(); ++i) {
366 if constexpr (!cochain.is_local()) {
367 out << " " << i->simplex() << " : " << i->value() << "\n";
368 } else {
369 out << " -> " << i->discrete_vector() << " : " << i->value() << "\n";
370 }
371 }
372
373 out << "]";
374
375 return out;
376}
377
378} // namespace exterior
379
380} // namespace sil
KOKKOS_FUNCTION constexpr reference operator*() const noexcept
Definition cochain.hpp:250
friend KOKKOS_FUNCTION constexpr bool operator<(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:309
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:295
friend KOKKOS_FUNCTION constexpr bool operator<=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:323
KOKKOS_FUNCTION constexpr CochainIterator & operator++()
Definition cochain.hpp:260
KOKKOS_FUNCTION constexpr CochainIterator & operator--()
Definition cochain.hpp:274
friend KOKKOS_FUNCTION constexpr bool operator>=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:330
KOKKOS_FUNCTION constexpr CochainIterator & operator+=(difference_type n)
Definition cochain.hpp:288
KOKKOS_FUNCTION constexpr pointer operator->() const noexcept
Definition cochain.hpp:255
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:347
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:316
friend KOKKOS_FUNCTION constexpr bool operator==(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:302
friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
Definition cochain.hpp:337
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 > >
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:15