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/tensor_indices/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(
29 misc::Specialization<ChainType, Chain> || misc::Specialization<ChainType, LocalChain>
30 || misc::Specialization<ChainType, tensor::Tensor>)
32{
33public:
34 using memory_space = typename ChainType::memory_space;
35
36 using simplex_type = typename ChainType::simplex_type;
37 using chain_type = ChainType;
38 using element_type = ElementType;
39 using discrete_element_type = ChainType::discrete_element_type;
40 using discrete_vector_type = ChainType::discrete_vector_type;
41 using values_type = Kokkos::View<ElementType*, LayoutStridedPolicy, memory_space>;
43
44private:
45 // chain_type const& m_chain;
46 chain_type m_chain;
47 values_type m_values;
48
49public:
50 KOKKOS_DEFAULTED_FUNCTION constexpr Cochain() = default;
51
52 KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain const&) = default;
53
54 KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain&&) = default;
55
56 template <class... T>
57 requires(sizeof...(T) >= 1 && (std::is_convertible_v<T, double> && ...))
58 KOKKOS_FUNCTION constexpr explicit Cochain(
59 chain_type chain,
60 values_type allocation,
61 T... value) noexcept
62 : m_chain(std::move(chain))
63 , m_values(std::move(allocation))
64 {
65 int i = 0;
66 ((m_values(i++) = value), ...);
67 assert(sizeof...(T) == chain.size()
68 && "cochain constructor must get as much values as the chain contains simplices");
69 }
70
71 KOKKOS_FUNCTION constexpr explicit Cochain(
72 chain_type const& chain,
73 values_type const& values) noexcept
74 : m_chain(std::move(chain))
75 , m_values(std::move(values))
76 {
77 assert(values.size() == chain.size()
78 && "cochain constructor must get as much values as the chain contains simplices");
79 }
80
81 template <tensor::TensorIndex Index>
84 KOKKOS_FUNCTION constexpr explicit Cochain(
85 chain_type const& chain,
88 ddc::DiscreteDomain<Index>,
89 ddc::detail::kokkos_to_mdspan_layout_t<LayoutStridedPolicy>,
90 memory_space> const& tensor) noexcept
91 : m_chain(std::move(chain))
92 , m_values(tensor.allocation_kokkos_view())
93 {
94 assert(m_values.size() == chain.size()
95 && "cochain constructor must get as much values as the chain contains simplices");
96 }
97
98 KOKKOS_DEFAULTED_FUNCTION ~Cochain() = default;
99
100 KOKKOS_DEFAULTED_FUNCTION Cochain& operator=(Cochain const& other) = default;
101
102 KOKKOS_DEFAULTED_FUNCTION Cochain& operator=(Cochain&& other) = default;
103
104 static constexpr bool is_local() noexcept
105 {
106 return chain_type::is_local();
107 }
108
109 static constexpr std::size_t dimension() noexcept
110 {
111 return chain_type::dimension();
112 }
113
114 KOKKOS_FUNCTION std::size_t size() noexcept
115 {
116 return m_chain.size();
117 }
118
119 KOKKOS_FUNCTION std::size_t size() const noexcept
120 {
121 return m_chain.size();
122 }
123
124 KOKKOS_FUNCTION std::size_t allocation_size() noexcept
125 {
126 return m_chain.allocation_size();
127 }
128
129 KOKKOS_FUNCTION std::size_t allocation_size() const noexcept
130 {
131 return m_chain.allocation_size();
132 }
133
134 KOKKOS_FUNCTION chain_type const& chain() const noexcept
135 {
136 return m_chain;
137 }
138
139 KOKKOS_FUNCTION chain_type const& values() const noexcept
140 {
141 return m_values;
142 }
143
145
146 KOKKOS_FUNCTION auto begin()
147 {
148 return iterator(m_chain.begin(), Kokkos::Experimental::begin(m_values));
149 }
150
151 KOKKOS_FUNCTION auto begin() const
152 {
153 return iterator(m_chain.begin(), Kokkos::Experimental::begin(m_values));
154 }
155
156 KOKKOS_FUNCTION auto end()
157 {
158 return iterator(m_chain.end(), Kokkos::Experimental::end(m_values));
159 }
160
161 KOKKOS_FUNCTION auto end() const
162 {
163 return iterator(m_chain.end(), Kokkos::Experimental::end(m_values));
164 }
165
166 KOKKOS_FUNCTION auto cbegin() const
167 {
168 return iterator(m_chain.begin(), Kokkos::Experimental::begin(m_values));
169 }
170
171 KOKKOS_FUNCTION auto cend() const
172 {
173 return iterator(m_chain.end(), Kokkos::Experimental::end(m_values));
174 }
175
176 KOKKOS_FUNCTION Cosimplex<simplex_type, element_type>& operator[](std::size_t i) noexcept
177 {
178 assert(i < size());
179 return Cosimplex<simplex_type, element_type>(m_chain(i), m_values(i));
180 }
181
183 std::size_t i) const noexcept
184 {
185 assert(i < size());
186 return Cosimplex<simplex_type, element_type>(m_chain(i), m_values(i));
187 }
188
189 KOKKOS_FUNCTION element_type integrate() noexcept
190 {
191 element_type out = 0;
192 for (auto i = begin(); i < end(); ++i) {
194 out += (i->negative() ? -1 : 1) * i->value();
196 out += (i->negative() ? -1 : 1) * i->value();
197 }
198 }
199 return out;
200 }
201
202 KOKKOS_FUNCTION element_type const integrate() const noexcept
203 {
204 element_type out = 0;
205 for (auto i = begin(); i < end(); ++i) {
207 out += (i->negative() ? -1 : 1) * i->value();
209 out += (i->negative() ? -1 : 1) * i->value();
210 }
211 }
212 return out;
213 }
214};
215
216// A structured cochain field retains its integral semantics when sliced.
217template <class T, class Domain, class Layout, class Memory, class ElementType, class ChainLayout>
218class Cochain<tensor::Tensor<T, Domain, Layout, Memory>, ElementType, ChainLayout>
219 : public tensor::Tensor<T, Domain, Layout, Memory>
220{
222
223public:
225 KOKKOS_DEFAULTED_FUNCTION Cochain() = default;
226 KOKKOS_FUNCTION explicit Cochain(Tensor const& tensor) : Tensor(tensor) {}
227 KOKKOS_FUNCTION Tensor const& coefficients() const
228 {
229 return *this;
230 }
231 KOKKOS_FUNCTION Tensor& coefficients()
232 {
233 return *this;
234 }
235
236 KOKKOS_FUNCTION Cochain& operator+=(Cochain const& other)
237 {
238 coefficients() += other.coefficients();
239 return *this;
240 }
241 KOKKOS_FUNCTION Cochain& operator*=(T scalar)
242 {
243 coefficients() *= scalar;
244 return *this;
245 }
246
247 template <class Element>
248 KOKKOS_FUNCTION auto operator[](Element const& element) const
249 {
250 return ::sil::exterior::Cochain(Tensor::operator[](element));
251 }
252};
253
254template <misc::Specialization<tensor::Tensor> Tensor>
255Cochain(Tensor const&) -> Cochain<Tensor>;
256
257template <class T>
259
260template <class ChainType, misc::Specialization<tensor::Tensor> TensorType>
261Cochain(ChainType, TensorType) -> Cochain<
262 ChainType,
263 typename TensorType::value_type,
264 ddc::detail::mdspan_to_kokkos_layout_t<typename TensorType::layout_type>>;
265
266template <typename CochainType>
268{
269public:
270 using iterator_category = std::random_access_iterator_tag;
271 using value_type = typename CochainType::cosimplex_type;
272 using difference_type = std::ptrdiff_t;
275
276 using cosimplex_type = typename CochainType::cosimplex_type;
277
278 using chain_iterator = typename CochainType::chain_type::iterator_type;
280 = Kokkos::Experimental::Impl::RandomAccessIterator<typename CochainType::values_type>;
281
282private:
283 chain_iterator m_chain;
284 values_iterator m_values;
285
286public:
287 KOKKOS_DEFAULTED_FUNCTION CochainIterator() = default;
289 KOKKOS_FUNCTION constexpr explicit CochainIterator(
290 chain_iterator chain_it,
291 values_iterator values_it)
292 : m_chain(chain_it)
293 , m_values(values_it)
294 {
295 }
297 KOKKOS_FUNCTION constexpr reference operator*() const noexcept
298 {
299 return cosimplex_type(*m_chain, *m_values);
300 }
302 KOKKOS_FUNCTION constexpr pointer operator->() const noexcept
303 {
304 return **this;
305 }
307 KOKKOS_FUNCTION constexpr CochainIterator& operator++()
308 {
309 ++m_chain;
310 ++m_values;
311 return *this;
312 }
314 KOKKOS_FUNCTION constexpr CochainIterator operator++(int)
315 {
316 auto tmp = *this;
317 ++*this;
318 return tmp;
319 }
321 KOKKOS_FUNCTION constexpr CochainIterator& operator--()
322 {
323 --m_chain;
324 --m_values;
325 return *this;
326 }
328 KOKKOS_FUNCTION constexpr CochainIterator operator--(int)
329 {
330 auto tmp = *this;
331 --*this;
332 return tmp;
333 }
335 KOKKOS_FUNCTION constexpr CochainIterator& operator+=(difference_type n)
336 {
337 m_chain += n;
338 m_values += n;
339 return *this;
340 }
342 KOKKOS_FUNCTION constexpr CochainIterator& operator-=(difference_type n)
343 {
344 m_chain -= n;
345 m_values -= n;
346 return *this;
347 }
349 friend KOKKOS_FUNCTION constexpr bool operator==(
350 CochainIterator const& xx,
351 CochainIterator const& yy)
352 {
353 return xx.m_chain == yy.m_chain && xx.m_values == yy.m_values;
354 }
356 friend KOKKOS_FUNCTION constexpr bool operator<(
357 CochainIterator const& xx,
358 CochainIterator const& yy)
359 {
360 return xx.m_chain < yy.m_chain && xx.m_values < yy.m_values;
361 }
363 friend KOKKOS_FUNCTION constexpr bool operator>(
364 CochainIterator const& xx,
365 CochainIterator const& yy)
366 {
367 return xx.m_chain > yy.m_chain && xx.m_values > yy.m_values;
368 }
370 friend KOKKOS_FUNCTION constexpr bool operator<=(
371 CochainIterator const& xx,
372 CochainIterator const& yy)
373 {
374 return !(yy < xx);
375 }
377 friend KOKKOS_FUNCTION constexpr bool operator>=(
378 CochainIterator const& xx,
379 CochainIterator const& yy)
380 {
381 return !(xx < yy);
382 }
384 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
385 {
386 return i += n;
387 }
389 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(difference_type n, CochainIterator i)
390 {
391 return i += n;
392 }
394 friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
395 {
396 return i -= n;
397 }
399 friend KOKKOS_FUNCTION constexpr difference_type operator-(
400 CochainIterator const& xx,
401 CochainIterator const& yy)
402 {
403 return xx.m_chain - yy.m_chain;
404 }
405};
406
407template <misc::Specialization<Cochain> CochainType>
408std::ostream& operator<<(std::ostream& out, CochainType const& cochain)
409{
410 out << "[\n";
411 for (auto i = cochain.begin(); i < cochain.end(); ++i) {
412 if constexpr (!cochain.is_local()) {
413 out << " " << i->simplex() << " : " << i->value() << "\n";
414 } else {
415 out << " -> " << i->discrete_vector() << " : " << i->value() << "\n";
416 }
417 }
418
419 out << "]";
420
421 return out;
422}
423
424} // namespace exterior
425
426} // namespace sil
KOKKOS_FUNCTION constexpr reference operator*() const noexcept
Definition cochain.hpp:296
friend KOKKOS_FUNCTION constexpr bool operator<(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:355
typename CochainType::cosimplex_type cosimplex_type
Definition cochain.hpp:276
typename CochainType::cosimplex_type value_type
Definition cochain.hpp:271
KOKKOS_FUNCTION constexpr CochainIterator & operator-=(difference_type n)
Definition cochain.hpp:341
friend KOKKOS_FUNCTION constexpr bool operator<=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:369
KOKKOS_FUNCTION constexpr CochainIterator & operator++()
Definition cochain.hpp:306
KOKKOS_FUNCTION constexpr CochainIterator & operator--()
Definition cochain.hpp:320
friend KOKKOS_FUNCTION constexpr bool operator>=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:376
KOKKOS_FUNCTION constexpr CochainIterator & operator+=(difference_type n)
Definition cochain.hpp:334
KOKKOS_FUNCTION constexpr pointer operator->() const noexcept
Definition cochain.hpp:301
std::random_access_iterator_tag iterator_category
Definition cochain.hpp:270
KOKKOS_DEFAULTED_FUNCTION CochainIterator()=default
typename CochainType::chain_type::iterator_type chain_iterator
Definition cochain.hpp:278
friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
Definition cochain.hpp:393
Kokkos::Experimental::Impl::RandomAccessIterator< typename CochainType::values_type > values_iterator
Definition cochain.hpp:279
friend KOKKOS_FUNCTION constexpr bool operator>(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:362
friend KOKKOS_FUNCTION constexpr bool operator==(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:348
friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
Definition cochain.hpp:383
Cochain class.
Definition cochain.hpp:32
KOKKOS_DEFAULTED_FUNCTION constexpr Cochain()=default
Kokkos::View< ElementType *, LayoutStridedPolicy, memory_space > values_type
Definition cochain.hpp:41
KOKKOS_FUNCTION std::size_t allocation_size() const noexcept
Definition cochain.hpp:129
KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain const &)=default
KOKKOS_FUNCTION auto end()
Definition cochain.hpp:156
KOKKOS_FUNCTION auto begin()
Definition cochain.hpp:146
KOKKOS_DEFAULTED_FUNCTION Cochain & operator=(Cochain &&other)=default
KOKKOS_FUNCTION chain_type const & chain() const noexcept
Definition cochain.hpp:134
KOKKOS_DEFAULTED_FUNCTION Cochain & operator=(Cochain const &other)=default
KOKKOS_FUNCTION auto end() const
Definition cochain.hpp:161
ChainType::discrete_element_type discrete_element_type
Definition cochain.hpp:39
KOKKOS_FUNCTION Cosimplex< simplex_type, element_type > & operator[](std::size_t i) noexcept
Definition cochain.hpp:176
KOKKOS_FUNCTION chain_type const & values() const noexcept
Definition cochain.hpp:139
KOKKOS_DEFAULTED_FUNCTION constexpr Cochain(Cochain &&)=default
typename ChainType::simplex_type simplex_type
Definition cochain.hpp:36
KOKKOS_FUNCTION std::size_t size() noexcept
Definition cochain.hpp:114
static constexpr bool is_local() noexcept
Definition cochain.hpp:104
KOKKOS_FUNCTION element_type integrate() noexcept
Definition cochain.hpp:189
static constexpr std::size_t dimension() noexcept
Definition cochain.hpp:109
KOKKOS_FUNCTION std::size_t size() const noexcept
Definition cochain.hpp:119
ChainType::discrete_vector_type discrete_vector_type
Definition cochain.hpp:40
KOKKOS_FUNCTION element_type const integrate() const noexcept
Definition cochain.hpp:202
KOKKOS_FUNCTION Cosimplex< simplex_type, element_type > const & operator[](std::size_t i) const noexcept
Definition cochain.hpp:182
KOKKOS_FUNCTION auto cend() const
Definition cochain.hpp:171
KOKKOS_FUNCTION auto begin() const
Definition cochain.hpp:151
KOKKOS_DEFAULTED_FUNCTION ~Cochain()=default
KOKKOS_FUNCTION std::size_t allocation_size() noexcept
Definition cochain.hpp:124
KOKKOS_FUNCTION constexpr Cochain(chain_type const &chain, values_type const &values) noexcept
Definition cochain.hpp:71
KOKKOS_FUNCTION auto cbegin() const
Definition cochain.hpp:166
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 > const &tensor) noexcept
Definition cochain.hpp:84
typename ChainType::memory_space memory_space
Definition cochain.hpp:34
ElementType element_type
Definition cochain.hpp:38
Cosimplex class.
Definition cosimplex.hpp:20
std::ostream & operator<<(std::ostream &out, ChainType const &chain)
Definition chain.hpp:304
Cochain(Tensor const &) -> Cochain< Tensor >
The top-level namespace of SimiLie.
Definition csr.hpp:15