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)
216 -> Cochain<
217 ChainType,
218 typename TensorType::value_type,
219 ddc::detail::mdspan_to_kokkos_layout_t<typename TensorType::layout_type>>;
220
221template <typename CochainType>
223{
224public:
225 using iterator_category = std::random_access_iterator_tag;
226 using value_type = typename CochainType::cosimplex_type;
227 using difference_type = std::ptrdiff_t;
230
231 using cosimplex_type = typename CochainType::cosimplex_type;
232
233 using chain_iterator = typename CochainType::chain_type::iterator_type;
235 = Kokkos::Experimental::Impl::RandomAccessIterator<typename CochainType::values_type>;
236
237private:
238 chain_iterator m_chain;
239 values_iterator m_values;
240
241public:
242 KOKKOS_DEFAULTED_FUNCTION CochainIterator() = default;
244 KOKKOS_FUNCTION constexpr explicit CochainIterator(
245 chain_iterator chain_it,
246 values_iterator values_it)
247 : m_chain(chain_it)
248 , m_values(values_it)
249 {
250 }
252 KOKKOS_FUNCTION constexpr value_type operator*() const noexcept
253 {
254 if constexpr (!CochainType::chain_type::is_local()) {
255 return cosimplex_type(*m_chain, *m_values);
256 } else {
257 return cosimplex_type(
258 Simplex(std::integral_constant<std::size_t, CochainType::dimension()> {},
260 *m_chain),
261 *m_values);
262 }
263 }
265 KOKKOS_FUNCTION constexpr CochainIterator& operator++()
266 {
267 ++m_chain;
268 ++m_values;
269 return *this;
270 }
272 KOKKOS_FUNCTION constexpr CochainIterator operator++(int)
273 {
274 auto tmp = *this;
275 ++*this;
276 return tmp;
277 }
279 KOKKOS_FUNCTION constexpr CochainIterator& operator--()
280 {
281 --m_chain;
282 --m_values;
283 return *this;
284 }
286 KOKKOS_FUNCTION constexpr CochainIterator operator--(int)
287 {
288 auto tmp = *this;
289 --*this;
290 return tmp;
291 }
293 KOKKOS_FUNCTION constexpr CochainIterator& operator+=(difference_type n)
294 {
295 m_chain += n;
296 m_values += n;
297 return *this;
298 }
300 KOKKOS_FUNCTION constexpr CochainIterator& operator-=(difference_type n)
301 {
302 m_chain -= n;
303 m_values -= n;
304 return *this;
305 }
306
307 /*
308 KOKKOS_FUNCTION constexpr value_type operator[](difference_type n) const
309 {
310 return m_value + n;
311 }
312 */
314 friend KOKKOS_FUNCTION constexpr bool operator==(
315 CochainIterator const& xx,
316 CochainIterator const& yy)
317 {
318 return xx.m_chain == yy.m_chain && xx.m_values == yy.m_values;
319 }
321 friend KOKKOS_FUNCTION constexpr bool operator<(
322 CochainIterator const& xx,
323 CochainIterator const& yy)
324 {
325 return xx.m_chain < yy.m_chain && xx.m_values < yy.m_values;
326 }
328 friend KOKKOS_FUNCTION constexpr bool operator>(
329 CochainIterator const& xx,
330 CochainIterator const& yy)
331 {
332 return xx.m_chain > yy.m_chain && xx.m_values > yy.m_values;
333 }
335 friend KOKKOS_FUNCTION constexpr bool operator<=(
336 CochainIterator const& xx,
337 CochainIterator const& yy)
338 {
339 return !(yy < xx);
340 }
342 friend KOKKOS_FUNCTION constexpr bool operator>=(
343 CochainIterator const& xx,
344 CochainIterator const& yy)
345 {
346 return !(xx < yy);
347 }
349 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
350 {
351 return i += n;
352 }
354 friend KOKKOS_FUNCTION constexpr CochainIterator operator+(difference_type n, CochainIterator i)
355 {
356 return i += n;
357 }
359 friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
360 {
361 return i -= n;
362 }
364 friend KOKKOS_FUNCTION constexpr difference_type operator-(
365 CochainIterator const& xx,
366 CochainIterator const& yy)
367 {
368 return xx.m_chain - yy.m_chain;
369 }
370};
371
372template <misc::Specialization<Cochain> CochainType>
373std::ostream& operator<<(std::ostream& out, CochainType const& cochain)
374{
375 out << "[\n";
376 for (auto i = cochain.begin(); i < cochain.end(); ++i) {
377 if constexpr (!cochain.is_local()) {
378 out << " " << (*i).simplex() << " : " << (*i).value() << "\n";
379 } else {
380 out << " -> " << (*i).discrete_vector() << " : " << (*i).value() << "\n";
381 }
382 }
383
384 out << "]";
385
386 return out;
387}
388
389} // namespace exterior
390
391} // namespace sil
friend KOKKOS_FUNCTION constexpr bool operator<(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:320
typename CochainType::cosimplex_type cosimplex_type
Definition cochain.hpp:231
typename CochainType::cosimplex_type value_type
Definition cochain.hpp:226
KOKKOS_FUNCTION constexpr CochainIterator & operator-=(difference_type n)
Definition cochain.hpp:299
friend KOKKOS_FUNCTION constexpr bool operator<=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:334
KOKKOS_FUNCTION constexpr CochainIterator & operator++()
Definition cochain.hpp:264
KOKKOS_FUNCTION constexpr CochainIterator & operator--()
Definition cochain.hpp:278
friend KOKKOS_FUNCTION constexpr bool operator>=(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:341
KOKKOS_FUNCTION constexpr CochainIterator & operator+=(difference_type n)
Definition cochain.hpp:292
std::random_access_iterator_tag iterator_category
Definition cochain.hpp:225
KOKKOS_DEFAULTED_FUNCTION CochainIterator()=default
typename CochainType::chain_type::iterator_type chain_iterator
Definition cochain.hpp:233
friend KOKKOS_FUNCTION constexpr CochainIterator operator-(CochainIterator i, difference_type n)
Definition cochain.hpp:358
Kokkos::Experimental::Impl::RandomAccessIterator< typename CochainType::values_type > values_iterator
Definition cochain.hpp:234
friend KOKKOS_FUNCTION constexpr bool operator>(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:327
friend KOKKOS_FUNCTION constexpr bool operator==(CochainIterator const &xx, CochainIterator const &yy)
Definition cochain.hpp:313
friend KOKKOS_FUNCTION constexpr CochainIterator operator+(CochainIterator i, difference_type n)
Definition cochain.hpp:348
KOKKOS_FUNCTION constexpr value_type operator*() const noexcept
Definition cochain.hpp:251
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