SimiLie
Loading...
Searching...
No Matches
continuous_hodge_star.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <array>
7#include <stdexcept>
8
9#include <ddc/ddc.hpp>
10
11#include <similie/exterior/coboundary/coboundary.hpp>
12#include <similie/exterior/cochain_indices.hpp>
13#include <similie/exterior/concepts.hpp>
14#include <similie/exterior/reduction_and_reconstruction/reduction_and_reconstruction.hpp>
15#include <similie/exterior/stencil.hpp>
16#include <similie/exterior/volume.hpp>
17#include <similie/misc/factorial.hpp>
18#include <similie/misc/macros.hpp>
19#include <similie/misc/small_matrix.hpp>
20#include <similie/misc/specialization.hpp>
21#include <similie/misc/type_seq_conversion.hpp>
22#include <similie/tensor/metric.hpp>
23#include <similie/tensor/tensor_indices/antisymmetric_tensor.hpp>
24#include <similie/tensor/tensor_indices/character.hpp>
25#include <similie/tensor/tensor_indices/full_tensor.hpp>
26
27namespace sil {
28
29namespace exterior {
30
31template <misc::Specialization<ddc::TypeSeq> Indices1, misc::Specialization<ddc::TypeSeq> Indices2>
33 = ddc::detail::convert_type_seq_to_discrete_domain_t<ddc::type_seq_merge_t<
34 ddc::TypeSeq<misc::convert_type_seq_to_t<tensor::TensorFullIndex, Indices1>>,
35 std::conditional_t<
36 (ddc::type_seq_size_v<Indices2> == 0),
37 ddc::TypeSeq<>,
38 ddc::TypeSeq<misc::convert_type_seq_to_t<
40 Indices2>>>>>;
41
42template <
45 class MetricType,
46 class BatchElem>
48
49
50template <
51 CellComplex Complex,
52 class Indices1,
53 class Indices2,
54 class MetricType,
55 class PositionType,
56 class BatchElem,
57 template <class, class, class, CellComplex> class ReconstructionPolicy = Reconstruction,
58 template <class, class, class, CellComplex> class ReductionPolicy = Reduction,
59 template <class, class, class, class> class HodgePolicy = ContinuousHodgeStar>
61
62namespace detail {
66template <class MetricType, class PositionType, class BatchElem>
67KOKKOS_FUNCTION auto one_form_pairing(
68 MetricType const& metric,
69 PositionType const& position,
70 BatchElem elem)
71{
72 using PositionIndex = ddc::
73 type_seq_element_t<0, ddc::to_type_seq_t<typename PositionType::indices_domain_t>>;
74 using MetricIndex
75 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename MetricType::indices_domain_t>>;
76 constexpr std::size_t N = PositionIndex::size();
77 std::array<double, N * N> jacobian {}, gram {}, inverse {}, workspace {};
78 for (std::size_t axis = 0; axis < N; ++axis) {
79 std::array<double, N> const edge = detail::edge_vector<PositionIndex>(position, elem, axis);
80 for (std::size_t physical = 0; physical < N; ++physical)
81 jacobian[physical * N + axis] = edge[physical];
82 }
83 for (std::size_t i = 0; i < N; ++i)
84 for (std::size_t j = 0; j < N; ++j)
85 for (std::size_t a = 0; a < N; ++a)
86 for (std::size_t b = 0; b < N; ++b)
87 gram[i * N + j] += jacobian[a * N + i] * jacobian[b * N + j]
88 * metric.get(metric.access_element(
89 elem,
90 ddc::DiscreteElement<
93 auto gram_view
95 auto inverse_view = misc::math::
97 if (!misc::math::
98 invert(inverse_view,
99 gram_view,
101 double,
102 typename MetricType::memory_space>(workspace.data(), N * N)))
103 return inverse;
104 double const orientation = detail::reduction_determinant<N>(jacobian);
105 double const volume = (orientation < 0.0 ? -1.0 : 1.0)
106 * Kokkos::sqrt(Kokkos::abs(detail::reduction_determinant<N>(gram)));
107 for (double& coefficient : inverse)
108 coefficient *= volume;
109 return inverse;
110}
111
112
113
114template <std::size_t N, std::size_t M>
115KOKKOS_FUNCTION bool has_unique_ids(std::array<std::size_t, M> const& ids)
116{
117 if constexpr (M > 0) {
118 for (std::size_t i = 0; i < M; ++i) {
119 if (ids[i] >= N) {
120 return false;
121 }
122 for (std::size_t j = i + 1; j < M; ++j) {
123 if (ids[i] == ids[j]) {
124 return false;
125 }
126 }
127 }
128 }
129 return true;
130}
131
132template <std::size_t N, std::size_t M1, std::size_t M2>
133KOKKOS_FUNCTION int permutation_sign(
134 std::array<std::size_t, M1> const& source_ids,
135 std::array<std::size_t, M2> const& target_ids)
136{
137 std::array<std::size_t, N> permutation {};
138 for (std::size_t i = 0; i < M1; ++i) {
139 permutation[i] = source_ids[i];
140 }
141 if constexpr (M2 > 0) {
142 for (std::size_t i = 0; i < M2; ++i) {
143 permutation[M1 + i] = target_ids[i];
144 }
145 }
146
147 bool odd = false;
148 for (std::size_t i = 0; i < N; ++i) {
149 for (std::size_t j = i + 1; j < N; ++j) {
150 odd = (permutation[i] > permutation[j]) != odd;
151 }
152 }
153 return odd ? -1 : 1;
154}
155
156template <std::size_t N, std::size_t M>
157KOKKOS_FUNCTION std::array<std::size_t, N - M> complement_ids(std::array<std::size_t, M> const& ids)
158{
159 std::array<std::size_t, N - M> complement {};
160 std::size_t complement_id = 0;
161 for (std::size_t i = 0; i < N; ++i) {
162 bool found = false;
163 for (std::size_t id : ids) {
164 found = found || (id == i);
165 }
166 if (!found) {
167 complement[complement_id++] = i;
168 }
169 }
170 return complement;
171}
172
173template <misc::Specialization<ddc::TypeSeq> Indices>
174struct HodgeStarTargetSize
175{
176 static constexpr std::size_t value
177 = misc::convert_type_seq_to_t<tensor::TensorAntisymmetricIndex, Indices>::mem_size();
178};
179
180template <>
181struct HodgeStarTargetSize<ddc::TypeSeq<>>
182{
183 static constexpr std::size_t value = 1;
184};
185
186
187template <
188 misc::Specialization<ddc::TypeSeq> Indices1,
189 misc::Specialization<ddc::TypeSeq> Indices2,
190 misc::Specialization<tensor::Tensor> HodgeStarType,
191 misc::Specialization<tensor::Tensor> MetricType,
192 class BatchElem>
193struct FillContinuousHodgeStarMem
194{
195 HodgeStarType m_hodge_star;
196 MetricType m_metric;
197 BatchElem m_elem;
198
199 template <class MemElem>
200 KOKKOS_FUNCTION void operator()(MemElem mem_elem) const
201 {
202 m_hodge_star.mem(typename HodgeStarType::discrete_element_type(m_elem, mem_elem))
204 value(m_metric,
205 m_elem,
206 m_hodge_star.accessor().canonical_natural_element(mem_elem));
207 }
208};
209
210} // namespace detail
211
212
213} // namespace exterior
214} // namespace sil
215
216
217namespace sil {
218namespace exterior {
219
220template <
221 misc::Specialization<ddc::TypeSeq> Indices1,
222 misc::Specialization<ddc::TypeSeq> Indices2,
223 class MetricType,
224 class BatchElem>
226{
227 static constexpr bool input_is_form = true;
228 static constexpr bool output_is_form = true;
229 using staged_storage_type = detail::LinearLocalOperatorStorage<
233 typename MetricType::non_indices_domain_t,
234 typename MetricType::memory_space,
235 typename MetricType::memory_space::execution_space,
239
240 template <FormType HodgeTensorType, FormType FormTensorType>
241 KOKKOS_FUNCTION static void operator()(
242 HodgeTensorType const& hodge_tensor,
243 FormTensorType const& form_tensor,
244 MetricType const& metric,
245 BatchElem elem)
246 {
248 hodge_star_domain_t<Indices1, Indices2>> hodge_star_accessor;
249 static constexpr std::size_t HODGE_STAR_SIZE
251 * detail::HodgeStarTargetSize<Indices2>::value;
252 std::array<double, HODGE_STAR_SIZE> hodge_star_alloc {};
253 ddc::ChunkSpan<
254 double,
256 Kokkos::layout_right,
257 typename HodgeTensorType::memory_space>
258 hodge_star_span(hodge_star_alloc.data(), hodge_star_accessor.domain());
259 sil::tensor::Tensor hodge_star(hodge_star_span);
260 ddc::device_for_each(hodge_star.domain(), [&](auto it) {
261 hodge_star.mem(it) = value(metric, elem, hodge_star.canonical_natural_element(it));
262 });
264 tensor_prod(hodge_tensor.coefficients(), hodge_star, form_tensor.coefficients());
265 }
266
267 KOKKOS_FUNCTION static detail::LocalOperatorStencil<
270 value(MetricType const& metric, BatchElem element)
271 {
273 using Target = reduction_index_t<Indices2>;
274 detail::LocalOperatorStencil<Source, Target> result;
275 auto unit = detail::make_stencil<typename MetricType::memory_space, Source>(
276 ddc::DiscreteElement<>());
277 auto output = detail::make_stencil<typename MetricType::memory_space, Target>(
278 ddc::DiscreteElement<>());
279 for (std::size_t source = 0; source < Source::mem_size(); ++source) {
280 unit.mem(ddc::DiscreteElement<Source>(source)) = 1.0;
282 Form(tensor::Tensor(output.span_view())),
283 Form(tensor::Tensor(unit.span_view())),
284 metric,
285 element);
286 for (std::size_t target = 0; target < Target::mem_size(); ++target)
287 result.weights[target * Source::mem_size() + source]
288 = output.mem(ddc::DiscreteElement<Target>(target));
289 unit.mem(ddc::DiscreteElement<Source>(source)) = 0.0;
290 }
291 return result;
292 }
293
294 KOKKOS_FUNCTION static double value(MetricType const& metric, BatchElem elem, auto natural_elem)
295 {
296 constexpr std::size_t N = ddc::type_seq_size_v<Indices1> + ddc::type_seq_size_v<Indices2>;
297 constexpr std::size_t K = ddc::type_seq_size_v<Indices1>;
300 std::array const source_ids = ddc::detail::array(SourceElem(natural_elem));
301 std::array const target_ids = ddc::detail::array(TargetElem(natural_elem));
302
303 if (!detail::has_unique_ids<N>(source_ids) || !detail::has_unique_ids<N>(target_ids)) {
304 return 0.;
305 }
306
307 std::array<std::size_t, K> const complement = detail::complement_ids<N>(target_ids);
308 using MetricIndex = ddc::
309 type_seq_element_t<0, ddc::to_type_seq_t<typename MetricType::indices_domain_t>>;
310 using MetricIndex1 = tensor::metric_index_1<MetricIndex>;
311 using MetricIndex2 = tensor::metric_index_2<MetricIndex>;
312 std::array<double, N * N> metric_alloc {};
313 std::array<double, N * N> determinant_metric_alloc {};
314 std::array<double, N * N> inverse_metric_alloc {};
315 std::array<double, N * N> workspace_alloc {};
316 std::array<double, K * K> submatrix_alloc {};
317 for (std::size_t i = 0; i < N; ++i) {
318 for (std::size_t j = 0; j < N; ++j) {
319 metric_alloc[i * N + j] = metric.get(metric.access_element(
320 elem,
321 ddc::DiscreteElement<MetricIndex1, MetricIndex2>(i, j)));
322 }
323 }
324 determinant_metric_alloc = metric_alloc;
325
326 auto determinant_metric_view = misc::math::matrix_view<
327 double,
328 typename MetricType::memory_space>(determinant_metric_alloc.data(), N, N);
329 auto metric_view = misc::math::
331 double const determinant = misc::math::determinant(determinant_metric_view);
332 auto inverse_metric_view = misc::math::matrix_view<
333 double,
334 typename MetricType::memory_space>(inverse_metric_alloc.data(), N, N);
335 auto workspace = misc::math::vector_view<
336 double,
337 typename MetricType::memory_space>(workspace_alloc.data(), N * N);
338 if (!misc::math::invert(inverse_metric_view, metric_view, workspace)) {
339 return 0.;
340 }
341
342 return Kokkos::sqrt(Kokkos::abs(determinant))
343 * static_cast<double>(detail::permutation_sign<N>(complement, target_ids))
345 K>(inverse_metric_view, source_ids, complement, submatrix_alloc)
346 / misc::factorial(K);
347 }
348};
349
350static_assert(ContinuousHodgeStarConcept<
351 ContinuousHodgeStar<
352 ddc::TypeSeq<detail::DirectionConcept>,
353 ddc::TypeSeq<>,
354 detail::FieldConcept<detail::MetricIndexConcept>,
355 ddc::DiscreteElement<detail::GridConcept>>,
356 detail::FormFieldConcept<detail::ScalarConcept>,
357 detail::FormFieldConcept<detail::DirectionConcept>,
358 detail::FieldConcept<detail::MetricIndexConcept>,
359 ddc::DiscreteElement<detail::GridConcept>,
360 ddc::DiscreteElement<detail::DirectionConcept>>);
361
362template <
369 class ExecSpace>
371 ExecSpace const& exec_space,
372 HodgeStarType const& hodge_star,
373 MetricType const& metric,
374 PositionType const& position)
375{
376 static_assert(
377 ddc::type_seq_size_v<ddc::to_type_seq_t<typename PositionType::indices_domain_t>> == 1);
378
379 SIMILIE_DEBUG_LOG("similie_compute_hodge_star");
380 ddc::parallel_for_each(
381 "similie_compute_hodge_star",
382 exec_space,
383 hodge_star.non_indices_domain(),
384 KOKKOS_LAMBDA(
385 typename HodgeStarType::non_indices_domain_t::discrete_element_type elem) {
386 auto const stencil = DiscreteHodgeStar<
387 Complex,
388 Indices1,
389 Indices2,
390 MetricType,
391 PositionType,
392 typename HodgeStarType::non_indices_domain_t::
393 discrete_element_type> {}
394 .value(metric, position, elem);
395 ddc::device_for_each(hodge_star.accessor().domain(), [&](auto component) {
396 using Source = tensor::lower_t<reduction_index_t<Indices1>>;
397 using Target = reduction_index_t<Indices2>;
398 using SourceElement
399 = misc::convert_type_seq_to_t<ddc::DiscreteElement, Indices1>;
400 using TargetElement
401 = misc::convert_type_seq_to_t<ddc::DiscreteElement, Indices2>;
402 auto const natural = hodge_star.accessor().canonical_natural_element(component);
403 hodge_star.mem(elem, component)
404 = detail::local_stencil_coefficient<Source, Target>(
405 stencil,
406 ddc::detail::array(SourceElement(natural)),
407 ddc::detail::array(TargetElement(natural)));
408 });
409 });
410 return hodge_star;
411}
412
413template <
418 class ExecSpace>
420 ExecSpace const& exec_space,
421 HodgeStarType const& hodge_star,
422 MetricType const& metric)
423{
424 SIMILIE_DEBUG_LOG("similie_compute_continuous_hodge_star");
425 ddc::parallel_for_each(
426 "similie_compute_continuous_hodge_star",
427 exec_space,
428 hodge_star.non_indices_domain(),
429 KOKKOS_LAMBDA(
430 typename HodgeStarType::non_indices_domain_t::discrete_element_type elem) {
431 ddc::device_for_each(
432 hodge_star.accessor().domain(),
433 detail::FillContinuousHodgeStarMem<
434 Indices1,
435 Indices2,
436 HodgeStarType,
437 MetricType,
438 typename HodgeStarType::non_indices_domain_t::
439 discrete_element_type> {hodge_star, metric, elem});
440 });
441 return hodge_star;
442}
443
444} // namespace exterior
445
446} // namespace sil
447
448
449namespace sil::exterior {
450namespace detail {
451
452template <std::size_t I, class T>
453struct CodifferentialHodgeOutputIndex : tensor::uncharacterize_t<T>
454{
455};
456
457template <class Ids, class T>
458struct CodifferentialHodgeOutputIndexSeq_;
459
460template <std::size_t... Id, class T>
461struct CodifferentialHodgeOutputIndexSeq_<std::index_sequence<Id...>, T>
462{
463 using type = ddc::TypeSeq<tensor::Covariant<CodifferentialHodgeOutputIndex<Id, T>>...>;
464};
465
466template <std::size_t EndId, class T>
467struct CodifferentialHodgeOutputIndexSeq;
468
469template <std::size_t EndId, class T>
470 requires(EndId == 0)
471struct CodifferentialHodgeOutputIndexSeq<EndId, T>
472{
473 using type = ddc::TypeSeq<>;
474};
475
476template <std::size_t EndId, class T>
477 requires(EndId > 0)
478struct CodifferentialHodgeOutputIndexSeq<EndId, T>
479{
480 using type =
481 typename CodifferentialHodgeOutputIndexSeq_<std::make_index_sequence<EndId>, T>::type;
482};
483
484} // namespace detail
485
486template <std::size_t EndId, class T>
488 typename detail::CodifferentialHodgeOutputIndexSeq<EndId, T>::type;
489
490namespace detail {
491template <std::size_t Axis, class Position, class Rules, class Element>
492KOKKOS_FUNCTION void geometry_front(
493 Position const& position,
494 Rules const& rules,
495 Element elem,
496 Element& front)
497{
498 if (!tensor::detail::is_interface(rules.template boundary_rule<Axis, true>())) {
499 if (ddc::detail::array(elem)[Axis]
500 == ddc::detail::array(position.non_indices_domain().back())[Axis]
501 && ddc::detail::array(elem)[Axis]
502 > ddc::detail::array(position.non_indices_domain().front())[Axis])
503 --ddc::detail::array(front)[Axis];
504 }
505}
506
507template <class Position, class Rules, class Element, std::size_t... Axis>
508KOKKOS_FUNCTION auto local_geometry(
509 Position const& position,
510 Rules const& rules,
511 Element elem,
512 std::index_sequence<Axis...>)
513{
514 using Index
515 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename Position::indices_domain_t>>;
516 Element front = elem;
517 (geometry_front<Axis>(position, rules, elem, front), ...);
518 auto geometry = make_stencil<typename Position::memory_space, Index>(front);
519 ddc::device_for_each(geometry.domain(), [&](auto sample) {
520 geometry.mem(sample)
521 = rules(position, Element(sample)).mem(ddc::DiscreteElement<Index>(sample));
522 });
523 return geometry;
524}
525
526template <class MemorySpace, class... Coordinates>
527KOKKOS_FUNCTION auto euclidean_metric(ddc::TypeSeq<Coordinates...>)
528{
529 using Index = tensor::TensorIdentityIndex<
531 tensor::Covariant<tensor::MetricIndex2<Coordinates...>>>;
533 // Identity tensors have no stored entries.
534 return tensor::Tensor(
535 ddc::ChunkSpan<
536 double,
537 ddc::DiscreteDomain<Index>,
538 Kokkos::layout_right,
539 MemorySpace>(nullptr, accessor.domain()));
540}
541
542
543} // namespace detail
557template <
558 class Direction,
559 class Cochain,
560 class Position,
561 class GeometryRules,
562 class BoundaryFluxRules>
564{
566 using dual_indices = codifferential_hodge_output_indices_t<Direction::size() - 1, Direction>;
569 using non_indices_domain_t = typename Cochain::non_indices_domain_t;
570 using indices_domain_t = ddc::DiscreteDomain<component_type>;
571 using memory_space = typename Position::memory_space;
575 Position position;
576 GeometryRules geometry;
577 BoundaryFluxRules boundary_flux;
579
580 KOKKOS_FUNCTION non_indices_domain_t non_indices_domain() const
581 {
582 return cochain.non_indices_domain();
583 }
585 template <std::size_t Axis, class Element, class Component>
586 KOKKOS_FUNCTION bool upper_flux(
587 Element elem,
588 Component component,
589 std::size_t normal,
590 double& result) const
591 {
592 if (!tensor::detail::is_interface(geometry.template boundary_rule<Axis, true>())) {
593 if (normal == Axis
594 && ddc::detail::array(elem)[Axis]
595 == ddc::detail::array(non_indices_domain().back())[Axis]) {
596 ++ddc::detail::array(elem)[Axis];
597 auto const& rule = boundary_flux.template boundary_rule<Axis, true>();
598 auto const coefficients = rule.value(non_indices_domain(), elem);
599 result = tensor::detail::contract_extrapolation_basis(
600 coefficients,
601 [](auto) { return 0.0; },
602 [&](std::size_t) {
603 return tensor::detail::
604 extrapolation_source_value(rule, *this, elem, component);
605 });
606 return true;
607 }
608 }
609 return false;
610 }
612 template <std::size_t Axis, class Element>
613 KOKKOS_FUNCTION bool physical_cell(Element cell) const
614 {
615 if (!tensor::detail::is_interface(geometry.template boundary_rule<Axis, false>()))
616 if (ddc::detail::array(cell)[Axis]
617 < ddc::detail::array(non_indices_domain().front())[Axis])
618 return false;
619 if (!tensor::detail::is_interface(geometry.template boundary_rule<Axis, true>()))
620 if (ddc::detail::array(cell)[Axis]
621 >= ddc::detail::array(non_indices_domain().back())[Axis])
622 return false;
623 return true;
624 }
626 template <class Element>
627 KOKKOS_FUNCTION auto cell_geometry(Element cell) const
628 {
629 constexpr std::size_t N = Direction::size();
630 constexpr std::size_t edge_count = std::size_t(1) << (N - 1);
631 using Index = ddc::
632 type_seq_element_t<0, ddc::to_type_seq_t<typename Position::indices_domain_t>>;
633 auto positions = detail::make_stencil<typename Position::memory_space, Index>(cell);
634 std::array<double, N * N> jacobian {};
635 for (std::size_t axis = 0; axis < N; ++axis) {
636 for (std::size_t edge_id = 0; edge_id < edge_count; ++edge_id) {
637 Element first = cell;
638 std::size_t tangent = 0;
639 for (std::size_t other = 0; other < N; ++other)
640 if (other != axis)
641 ddc::detail::array(first)[other] += (edge_id >> tangent++) & 1;
642 Element second = first;
643 ++ddc::detail::array(second)[axis];
644 for (std::size_t physical = 0; physical < N; ++physical)
645 jacobian[physical * N + axis]
646 += (geometry(position, second)
647 .mem(ddc::DiscreteElement<Index>(physical))
648 - geometry(position, first)
649 .mem(ddc::DiscreteElement<Index>(physical)))
650 / edge_count;
651 }
652 }
653 ddc::device_for_each(positions.domain(), [&](auto sample) {
654 std::size_t const physical = ddc::DiscreteElement<Index>(sample).uid();
655 double value = 0.0;
656 for (std::size_t axis = 0; axis < N; ++axis)
657 value += jacobian[physical * N + axis]
658 * (ddc::detail::array(Element(sample))[axis]
659 - ddc::detail::array(cell)[axis]);
660 positions.mem(sample) = value;
661 });
662 return positions;
663 }
665 template <class Element, std::size_t... Axis>
666 KOKKOS_FUNCTION double dual_volume(Element elem, std::index_sequence<Axis...>) const
667 {
668 constexpr std::size_t N = Direction::size();
669 auto metric = detail::euclidean_metric<typename Position::memory_space>(
671 std::array<std::size_t, N> const ids {Axis...};
672 double volume = 0.0;
673 for (std::size_t vertex = 0; vertex < (std::size_t(1) << N); ++vertex) {
674 Element cell = elem;
675 for (std::size_t axis = 0; axis < N; ++axis)
676 ddc::detail::array(cell)[axis] -= (vertex >> axis) & 1;
677 if (!(physical_cell<Axis>(cell) && ...))
678 continue;
679 auto positions = cell_geometry(cell);
681 double,
682 typename Position::discrete_domain_type,
683 Kokkos::layout_right,
684 typename Position::memory_space>
685 local_position(positions);
686 volume += SimplexVolume<
688 N,
689 decltype(metric),
690 decltype(local_position),
691 Element>::template operator()<N>(metric, local_position, cell, ids)
692 / (std::size_t(1) << N);
693 }
694 return volume;
695 }
697 template <class Component>
698 KOKKOS_FUNCTION std::size_t normal_direction(Component component) const
699 {
701 auto const ids
702 = detail::flat_natural_elem_ids(accessor.canonical_natural_element(component));
703 for (std::size_t normal = 0; normal < Direction::size(); ++normal) {
704 bool found = false;
705 for (std::size_t id : ids)
706 found = found || id == normal;
707 if (!found)
708 return normal;
709 }
710 return Direction::size();
711 }
713 template <class Element, class Component, std::size_t... Axis>
714 KOKKOS_FUNCTION bool prescribed_flux(
715 Element elem,
716 Component component,
717 double& prescribed,
718 std::index_sequence<Axis...>) const
719 {
720 return (upper_flux<Axis>(elem, component, normal_direction(component), prescribed) || ...);
721 }
723 template <class Element, class Component>
724 KOKKOS_FUNCTION bool prescribed_flux(Element elem, Component component, double& prescribed)
725 const
726 {
727 return prescribed_flux(
728 elem,
729 component,
730 prescribed,
731 std::make_index_sequence<Direction::size()> {});
732 }
733
738 template <class Element, class Component, std::size_t... Axis>
739 KOKKOS_FUNCTION auto stencil(Element elem, Component component, std::index_sequence<Axis...>)
740 const
741 {
742 using Index = ddc::
743 type_seq_element_t<0, ddc::to_type_seq_t<typename Cochain::indices_domain_t>>;
744 constexpr std::size_t N = Direction::size();
745 constexpr std::size_t edge_count = std::size_t(1) << (N - 1);
746 auto result = detail::make_stencil<typename Position::memory_space, Index, 3>(Element {});
747 double prescribed = 0.0;
748 if (prescribed_flux(elem, component, prescribed))
749 return result;
750 std::size_t const normal = normal_direction(component);
752 auto const ids
753 = detail::flat_natural_elem_ids(accessor.canonical_natural_element(component));
754 auto metric = detail::euclidean_metric<typename Position::memory_space>(
756 for (std::size_t incident = 0; incident < edge_count; ++incident) {
757 Element cell = elem;
758 std::size_t bit = 0;
759 for (std::size_t axis = 0; axis < N; ++axis)
760 if (axis != normal)
761 ddc::detail::array(cell)[axis] -= (incident >> bit++) & 1;
762 if (!(physical_cell<Axis>(cell) && ...))
763 continue;
764 auto positions = cell_geometry(cell);
766 double,
767 typename Position::discrete_domain_type,
768 Kokkos::layout_right,
769 typename Position::memory_space>
770 local_position(positions);
771 std::array<double, N * N> const pairing
772 = detail::one_form_pairing(metric, local_position, cell);
773 using PositionIndex = ddc::
774 type_seq_element_t<0, ddc::to_type_seq_t<typename Position::indices_domain_t>>;
775 std::array<double, N * edge_count * N> edges {};
776 std::array<double, N * edge_count> projector_row {};
777 std::array<double, N> weighted_projection {};
778 std::array<double, N * N> gram {}, inverse {}, workspace {};
779 std::size_t selected = 0;
780 for (std::size_t axis = 0; axis < N; ++axis) {
781 for (std::size_t edge_id = 0; edge_id < edge_count; ++edge_id) {
782 Element first = cell;
783 std::size_t tangent = 0;
784 for (std::size_t other = 0; other < N; ++other)
785 if (other != axis)
786 ddc::detail::array(first)[other] += (edge_id >> tangent++) & 1;
787 Element second = first;
788 ++ddc::detail::array(second)[axis];
789 std::size_t const edge = axis * edge_count + edge_id;
790 if (axis == normal && first == elem)
791 selected = edge;
792 for (std::size_t physical = 0; physical < N; ++physical)
793 edges[edge * N + physical]
794 = geometry(position, second)
795 .mem(ddc::DiscreteElement<PositionIndex>(physical))
796 - geometry(position, first)
797 .mem(ddc::DiscreteElement<PositionIndex>(physical));
798 for (std::size_t i = 0; i < N; ++i) {
799 for (std::size_t j = 0; j < N; ++j)
800 gram[i * N + j] += edges[edge * N + i] * edges[edge * N + j];
801 }
802 }
803 }
804 auto inverse_view = misc::math::
806 if (!misc::math::
807 invert(inverse_view,
809 double,
810 typename Position::memory_space>(gram.data(), N, N),
812 double,
813 typename Position::memory_space>(workspace.data(), N * N)))
814 continue;
815
816 // One row of P W P, with P = I - E (E^T E)^-1 E^T.
817 // It annihilates constant Cartesian forms, as in the pointwise Hodge.
818 for (std::size_t edge = 0; edge < N * edge_count; ++edge) {
819 projector_row[edge] = edge == selected ? 1.0 : 0.0;
820 for (std::size_t i = 0; i < N; ++i)
821 for (std::size_t j = 0; j < N; ++j)
822 projector_row[edge] -= edges[selected * N + i] * inverse[i * N + j]
823 * edges[edge * N + j];
824 projector_row[edge] *= pairing[(edge / edge_count) * (N + 1)] / edge_count;
825 for (std::size_t i = 0; i < N; ++i)
826 weighted_projection[i] += edges[edge * N + i] * projector_row[edge];
827 }
828 for (std::size_t axis = 0; axis < N; ++axis)
829 for (std::size_t edge_id = 0; edge_id < edge_count; ++edge_id) {
830 std::size_t const edge = axis * edge_count + edge_id;
831 double weight = pairing[normal * N + axis] / (edge_count * edge_count)
832 + projector_row[edge];
833 for (std::size_t i = 0; i < N; ++i)
834 for (std::size_t j = 0; j < N; ++j)
835 weight -= weighted_projection[i] * inverse[i * N + j]
836 * edges[edge * N + j];
837 weight *= diffusivity
838 * detail::permutation_sign<
839 N>(std::array<std::size_t, 1> {normal}, ids);
840 Element first = cell;
841 std::size_t tangent = 0;
842 for (std::size_t other = 0; other < N; ++other)
843 if (other != axis)
844 ddc::detail::array(first)[other] += (edge_id >> tangent++) & 1;
845 Element local = first;
846 for (std::size_t coordinate = 0; coordinate < N; ++coordinate)
847 ddc::detail::array(local)[coordinate]
848 = ddc::detail::array(first)[coordinate]
849 - ddc::detail::array(elem)[coordinate] + 1;
850 if constexpr (Index::rank() == 0) {
851 // The scalar coboundary's tensor stencil is the oriented
852 // endpoint difference; retain that incidence independently of H.
853 result.mem(local, ddc::DiscreteElement<Index>(0)) -= weight;
854 ++ddc::detail::array(local)[axis];
855 result.mem(local, ddc::DiscreteElement<Index>(0)) += weight;
856 } else
857 result.mem(local, ddc::DiscreteElement<Index>(axis)) += weight;
858 }
859 }
860 return result;
861 }
863 template <class Element, class Component>
864 KOKKOS_FUNCTION auto value(Element elem, Component component) const
865 {
866 return stencil(elem, component, std::make_index_sequence<Direction::size()> {});
867 }
868
870 template <CochainType TopTensor, class Element>
871 KOKKOS_FUNCTION double inverse_zero_form(TopTensor const& top, Element elem) const
872 {
873 using TopIndices = ddc::type_seq_merge_t<ddc::TypeSeq<Direction>, dual_indices>;
875 using Memory = typename Position::memory_space;
876 using ScalarTensor
877 = tensor::Tensor<double, ddc::DiscreteDomain<Scalar>, Kokkos::layout_right, Memory>;
878 auto scalar = detail::make_stencil<Memory, Scalar>(ddc::DiscreteElement<>());
879 auto positions = detail::local_geometry(
880 position,
881 geometry,
882 elem,
883 std::make_index_sequence<Direction::size()> {});
885 double,
886 typename Position::discrete_domain_type,
887 Kokkos::layout_right,
888 Memory>
889 local_position(positions);
890 auto metric = detail::euclidean_metric<Memory>(
892 DiscreteHodgeStar<
895 ddc::TypeSeq<>,
896 decltype(metric),
897 decltype(local_position),
898 Element> {}
899 .operator()(
900 ::sil::exterior::Cochain(ScalarTensor(scalar)),
901 top,
902 metric,
903 local_position,
904 elem);
905 double const local_volume = SimplexVolume<
907 Direction::size(),
908 decltype(metric),
909 decltype(local_position),
910 Element>::template
911 operator()<Direction::size()>(
912 metric,
913 local_position,
914 elem,
915 []<std::size_t... Axis>(std::index_sequence<Axis...>) {
916 return std::array<std::size_t, Direction::size()> {Axis...};
917 }(std::make_index_sequence<Direction::size()> {}));
918 // Each incident cell contributes one vertex share to the dual volume;
919 // physical walls truncate it, while interface rules supply donor cells.
920 return scalar.mem(ddc::DiscreteElement<Scalar>(0)) * local_volume
921 / dual_volume(elem, std::make_index_sequence<Direction::size()> {});
922 }
923
927 template <CochainType Output, CochainType TopTensor, class Element>
928 KOKKOS_FUNCTION void operator()(Output const& output, TopTensor const& top, Element elem) const
929 {
930 output.mem(ddc::DiscreteElement<tensor::Covariant<tensor::ScalarIndex>>(0))
931 = inverse_zero_form(top, elem);
932 }
934 template <class Element, class Component>
935 KOKKOS_FUNCTION double mem(Element elem, Component component) const
936 {
937 double prescribed = 0.0;
938 if (prescribed_flux(elem, component, prescribed))
939 return prescribed;
940 auto const coefficients = value(elem, component);
941 double result = 0.0;
942 ddc::device_for_each(coefficients.domain(), [&](auto entry) {
943 double const weight = coefficients.mem(entry);
944 if (weight != 0.0)
945 result += weight
946 * cochain
947 .mem(tensor::detail::stencil_sample(elem, entry),
948 typename Cochain::indices_domain_t::discrete_element_type(
949 entry));
950 });
951 return result;
952 }
953};
954
958template <class Direction, class Cochain, class Position, class Geometry, class Flux>
960 Cochain cochain,
961 Position const& position,
962 Geometry const& geometry_rule,
963 Flux const& flux_rule,
964 double diffusivity = 1.0)
965{
966#if defined(KOKKOS_ENABLE_CUDA)
967 if constexpr (Kokkos::SpaceAccessibility<Kokkos::Cuda, typename Position::memory_space>::
968 accessible) {
969 // Debug builds cannot always infer the full sampling/Hodge call depth.
970 std::size_t stack_size = 0;
971 if (cudaDeviceGetLimit(&stack_size, cudaLimitStackSize) != cudaSuccess
972 || (stack_size < 32768 && cudaDeviceSetLimit(cudaLimitStackSize, 32768) != cudaSuccess))
973 throw std::runtime_error("cannot reserve the CUDA stack for scalar DEC sampling");
974 }
975#endif
976 auto const geometry = [&] {
978 return geometry_rule;
979 else
980 return tensor::make_extrapolation_rules(position, geometry_rule);
981 }();
982 auto const flux = [&] {
984 return flux_rule;
985 else
986 return tensor::make_extrapolation_rules(cochain, flux_rule);
987 }();
988 return DualHodgeCochain<
989 Direction,
990 Cochain,
991 Position,
992 std::remove_cvref_t<decltype(geometry)>,
993 std::remove_cvref_t<decltype(flux)>> {cochain, position, geometry, flux, diffusivity};
994}
995
996
997} // namespace sil::exterior
998
999
1000#include <similie/exterior/discrete_hodge_star/discrete_hodge_star.hpp>
Cochain class.
Definition cochain.hpp:32
Differential-form coefficients, pointwise or sampled over a spatial domain.
Definition form.hpp:32
static KOKKOS_FUNCTION constexpr natural_domain_t::discrete_element_type canonical_natural_element(ddc::DiscreteElement< MemIndex... > mem_elem)
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
HodgeStarType fill_discrete_hodge_star(ExecSpace const &exec_space, HodgeStarType const &hodge_star, MetricType const &metric, PositionType const &position)
auto make_dual_hodge_cochain(Cochain cochain, Position const &position, Geometry const &geometry_rule, Flux const &flux_rule, double diffusivity=1.0)
Normalize single policies locally, as for the staged differential operators.
Cochain(Tensor const &) -> Cochain< Tensor >
typename detail::ReductionTargetIndex< Indices >::type reduction_index_t
Form(Coefficients const &) -> Form< Coefficients >
ddc::detail::convert_type_seq_to_discrete_domain_t< ddc::type_seq_merge_t< ddc::TypeSeq< misc::convert_type_seq_to_t< tensor::TensorFullIndex, Indices1 > >, std::conditional_t<(ddc::type_seq_size_v< Indices2 >==0), ddc::TypeSeq<>, ddc::TypeSeq< misc::convert_type_seq_to_t< tensor::TensorAntisymmetricIndex, Indices2 > > > > > hodge_star_domain_t
HodgeStarType fill_continuous_hodge_star(ExecSpace const &exec_space, HodgeStarType const &hodge_star, MetricType const &metric)
typename detail::CodifferentialHodgeOutputIndexSeq< EndId, T >::type codifferential_hodge_output_indices_t
KOKKOS_FUNCTION bool invert(InverseView const &inverse, MatrixView const &matrix, WorkspaceView const &workspace)
KOKKOS_FUNCTION unmanaged_vector_view_t< Scalar, MemorySpace > vector_view(Scalar *data, std::size_t n)
KOKKOS_FUNCTION MatrixView::non_const_value_type determinant(MatrixView const &matrix)
KOKKOS_FUNCTION unmanaged_matrix_view_t< Scalar, MemorySpace > matrix_view(Scalar *data, std::size_t rows, std::size_t cols)
KOKKOS_FUNCTION double submatrix_determinant(MatrixView const &matrix, RowIds const &row_ids, ColIds const &col_ids, std::array< double, N *N > &submatrix_alloc)
convert_type_seq_to_t< ddc::DiscreteDomain, typename detail::CartesianProd< Domains... >::type > cartesian_prod_t
KOKKOS_FUNCTION constexpr std::size_t factorial(std::size_t k) noexcept
Definition factorial.hpp:13
typename detail::ConvertTypeSeqTo< T, Seq >::type convert_type_seq_to_t
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
detail::Upper< T >::type upper_t
ddc::type_seq_element_t< 0, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_1
Definition metric.hpp:33
prime< Index, 2 > second
Definition prime.hpp:21
KOKKOS_FUNCTION auto make_extrapolation_rules(ExtrapolationRule rule)
Broadcast a rule to both sides of all N non-index dimensions.
ddc::type_seq_element_t< 1, ddc::to_type_seq_t< typename MetricIndex::subindices_domain_t > > metric_index_2
Definition metric.hpp:41
Tensor< ElementType, ddc::DiscreteDomain< ProdDDim... >, LayoutStridedPolicy, MemorySpace > tensor_prod(Tensor< ElementType, ddc::DiscreteDomain< ProdDDim... >, LayoutStridedPolicy, MemorySpace > const &prod_tensor, Tensor< ElementType, ddc::DiscreteDomain< DDim1... >, LayoutStridedPolicy, MemorySpace > const &tensor1, Tensor< ElementType, ddc::DiscreteDomain< DDim2... >, LayoutStridedPolicy, MemorySpace > const &tensor2)
detail::Uncharacterize< Index >::type uncharacterize_t
detail::Lower< T >::type lower_t
Definition character.hpp:79
detail::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t
The top-level namespace of SimiLie.
Definition csr.hpp:15
static KOKKOS_FUNCTION void operator()(HodgeTensorType const &hodge_tensor, FormTensorType const &form_tensor, MetricType const &metric, BatchElem elem)
detail::LinearLocalOperatorStorage< ContinuousHodgeStar, tensor::lower_t< reduction_index_t< Indices1 > >, reduction_index_t< Indices2 >, typename MetricType::non_indices_domain_t, typename MetricType::memory_space, typename MetricType::memory_space::execution_space, tensor::TensorFullIndex, misc::convert_type_seq_to_t< ddc::DiscreteElement, Indices1 >, misc::convert_type_seq_to_t< ddc::DiscreteElement, Indices2 > > staged_storage_type
static KOKKOS_FUNCTION detail::LocalOperatorStencil< tensor::lower_t< reduction_index_t< Indices1 > >, reduction_index_t< Indices2 > > value(MetricType const &metric, BatchElem element)
static KOKKOS_FUNCTION double value(MetricType const &metric, BatchElem elem, auto natural_elem)
Compose reconstruction, continuous Hodge star and reduction policies.
KOKKOS_FUNCTION detail::LocalOperatorStencil< Source, Target > value(MetricType const &metric, PositionType const &position, BatchElem element) const
Lazy dual cochain obtained by applying the Hodge star to a primal one-cochain.
KOKKOS_FUNCTION auto cell_geometry(Element cell) const
misc::convert_type_seq_to_t< tensor::TensorAntisymmetricIndex, dual_indices > component_type
codifferential_hodge_output_indices_t< Direction::size() - 1, Direction > dual_indices
KOKKOS_FUNCTION double mem(Element elem, Component component) const
KOKKOS_FUNCTION auto value(Element elem, Component component) const
KOKKOS_FUNCTION double inverse_zero_form(TopTensor const &top, Element elem) const
Inverse zero-form Hodge with the physical, truncated dual volume.
KOKKOS_FUNCTION non_indices_domain_t non_indices_domain() const
KOKKOS_FUNCTION double dual_volume(Element elem, std::index_sequence< Axis... >) const
KOKKOS_FUNCTION std::size_t normal_direction(Component component) const
typename Position::memory_space memory_space
KOKKOS_FUNCTION void operator()(Output const &output, TopTensor const &top, Element elem) const
Apply the inverse zero-form Hodge to a pointwise top cochain.
ddc::DiscreteDomain< component_type > indices_domain_t
KOKKOS_FUNCTION auto stencil(Element elem, Component component, std::index_sequence< Axis... >) const
Constitutive coefficients on the neighbouring primal tensor nodes.
KOKKOS_FUNCTION bool upper_flux(Element elem, Component component, std::size_t normal, double &result) const
KOKKOS_FUNCTION bool physical_cell(Element cell) const
KOKKOS_FUNCTION bool prescribed_flux(Element elem, Component component, double &prescribed, std::index_sequence< Axis... >) const
misc::cartesian_prod_t< non_indices_domain_t, ddc::DiscreteDomain< component_type > > discrete_domain_type
tensor::upper_t< ddc::to_type_seq_t< tensor::natural_domain_t< Direction > > > source_indices
typename Cochain::non_indices_domain_t non_indices_domain_t