SimiLie
Loading...
Searching...
No Matches
exterior/laplacian/laplacian.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 <cstddef>
8#include <limits>
9#include <optional>
10#include <stdexcept>
11#include <type_traits>
12#include <utility>
13
14#include <similie/exterior/codifferential/codifferential.hpp>
15#include <similie/exterior/stencil.hpp>
16
17namespace sil::exterior {
18
19template <class T>
23
24
25namespace detail {
26
27// Stencil probes sum delta d and d delta after evaluating each contribution.
28// Allow for roundoff in those compositions, relative to their pre-cancellation
29// scale. A small coefficient without cancellation is therefore retained.
30KOKKOS_FORCEINLINE_FUNCTION double laplacian_contribution_sum(double first, double second)
31{
32 double const sum = first + second;
33 double const scale = Kokkos::max(Kokkos::abs(first), Kokkos::abs(second));
34 return Kokkos::isfinite(sum)
35 && Kokkos::abs(sum)
36 <= 128.0 * std::numeric_limits<double>::epsilon() * scale
37 ? 0.0
38 : sum;
39}
40
41template <std::size_t Rank, class Domain, class Memory>
42using LaplacianChains = StencilChains<Rank, Domain, Memory>;
43
44template <class Source, class Target, class Domain, class Memory>
45struct LaplacianHodge
46{
47 using source_index = Source;
48 Domain domain;
49 Kokkos::View<double***, Kokkos::LayoutRight, Memory> coefficients;
50 template <class Element>
51 KOKKOS_FORCEINLINE_FUNCTION double operator()(
52 Element element,
53 std::size_t target,
54 std::size_t source) const
55 {
56 std::size_t node = 0;
57 for (std::size_t axis = 0; axis < Domain::rank(); ++axis)
58 node = node * ddc::detail::array(domain.extents())[axis]
59 + ddc::detail::array(element)[axis] - ddc::detail::array(domain.front())[axis];
60 return coefficients(node, target, source);
61 }
62};
63
64// Fold tensor contraction conventions into canonical component matrices once.
65template <class Source, class Target, class Exec, class Hodge, class Metric, class Position>
66LaplacianHodge<Source, Target, typename Metric::non_indices_domain_t, typename Metric::memory_space>
67cache_laplacian_hodge(
68 Exec const& exec,
69 Hodge const& hodge,
70 Metric const& metric,
71 Position const& position)
72{
73 using Domain = typename Metric::non_indices_domain_t;
74 using Memory = typename Metric::memory_space;
75 Domain const domain = metric.non_indices_domain();
76 Kokkos::View<double***, Kokkos::LayoutRight, Memory> coefficients(
77 "laplacian_hodge_components",
78 domain.size(),
79 Target::mem_size(),
80 Source::mem_size());
81 ddc::parallel_for_each(
82 "similie_cache_laplacian_hodge",
83 exec,
84 domain,
85 KOKKOS_LAMBDA(typename Domain::discrete_element_type element) {
86 auto unit = make_stencil<Memory, Source>(ddc::DiscreteElement<>());
87 auto output = make_stencil<Memory, Target>(ddc::DiscreteElement<>());
88 std::size_t const node = [&] {
89 std::size_t result = 0;
90 for (std::size_t axis = 0; axis < Domain::rank(); ++axis)
91 result = result * ddc::detail::array(domain.extents())[axis]
92 + ddc::detail::array(element)[axis]
93 - ddc::detail::array(domain.front())[axis];
94 return result;
95 }();
96 for (std::size_t source = 0; source < Source::mem_size(); ++source) {
97 unit.mem(ddc::DiscreteElement<Source>(source)) = 1.0;
98 hodge(::sil::exterior::Cochain(tensor::Tensor(output.span_view())),
100 metric,
101 position,
102 element);
103 for (std::size_t target = 0; target < Target::mem_size(); ++target)
104 coefficients(node, target, source)
105 = output.mem(ddc::DiscreteElement<Target>(target));
106 unit.mem(ddc::DiscreteElement<Source>(source)) = 0.0;
107 }
108 });
109 // The temporary Hodge policy may own coefficients read by this kernel.
110 exec.fence();
111 return {domain, coefficients};
112}
113
114template <class Hodge, class Exec, class Metric, class Position>
115Hodge make_laplacian_hodge(Exec const& exec, Metric const& metric, Position const& position)
116{
117 if constexpr (std::is_constructible_v<Hodge, Exec const&, Metric, Position>)
118 return Hodge(exec, metric, position);
119 else
120 return Hodge {};
121}
122
123template <
124 class Index,
125 class Cochain,
126 class Metric,
127 class Position,
128 template <CellComplex, class, class, class, class, class> class Hodge>
129struct LaplacianHodgePair
130{
132 using TargetIndices
133 = codifferential_hodge_output_indices_t<Index::size() - Cochain::rank(), Index>;
134 using InverseSourceIndices = ddc::type_seq_merge_t<ddc::TypeSeq<Index>, TargetIndices>;
135 using InverseTargetIndices
136 = ddc::type_seq_remove_t<tensor::lower_t<SourceIndices>, ddc::TypeSeq<Index>>;
138 using Intermediate
140 using Element = typename Metric::non_indices_domain_t::discrete_element_type;
141 using Forward
142 = Hodge<CellComplex::Primal, SourceIndices, TargetIndices, Metric, Position, Element>;
143 using Inverse
144 = Hodge<CellComplex::Primal,
146 InverseTargetIndices,
147 Metric,
148 Position,
149 Element>;
150 static_assert(DiscreteHodgeStarConcept<
151 Forward,
153 double,
154 ddc::DiscreteDomain<Dual>,
155 Kokkos::layout_right,
156 typename Metric::memory_space>>,
158 double,
159 ddc::DiscreteDomain<Cochain>,
160 Kokkos::layout_right,
161 typename Metric::memory_space>>,
162 Metric,
163 Position,
164 Element,
167 discrete_element_type>);
168 static_assert(DiscreteHodgeStarConcept<
169 Inverse,
171 double,
172 ddc::DiscreteDomain<codifferential_index_t<Index, Cochain>>,
173 Kokkos::layout_right,
174 typename Metric::memory_space>>,
176 double,
177 ddc::DiscreteDomain<Intermediate>,
178 Kokkos::layout_right,
179 typename Metric::memory_space>>,
180 Metric,
181 Position,
182 Element,
185 InverseTargetIndices>>::natural_domain_t::discrete_element_type>);
186 Forward forward;
187 Inverse inverse;
188 template <class Exec>
189 LaplacianHodgePair(Exec const& exec, Metric const& metric, Position const& position)
190 : forward(make_laplacian_hodge<Forward>(exec, metric, position))
191 , inverse(make_laplacian_hodge<Inverse>(exec, metric, position))
192 {
193 }
194 template <class Exec>
195 auto canonical(Exec const& exec, Metric const& metric, Position const& position) const
196 {
197 return std::
198 pair {cache_laplacian_hodge<Cochain, Dual>(exec, forward, metric, position),
199 cache_laplacian_hodge<
200 Intermediate,
202 Index,
203 Cochain>>(exec, inverse, metric, position)};
204 }
205};
206
207template <class A, class B, class C, class D, class Chains>
208struct LaplacianGeometry
209{
210 A derivative_hodge;
211 B inverse_derivative_hodge;
212 C hodge;
213 D inverse_hodge;
214 Chains chains;
215};
216
217template <class Index, class Domain, class Memory, class Evaluate>
218using LaplacianField = StencilField<Index, Domain, Memory, Evaluate>;
219
220template <class Index, class Field, class Rules, class Element, class Chains>
221KOKKOS_FORCEINLINE_FUNCTION auto laplacian_derivative(
222 Field const& field,
223 Rules const& rules,
224 Element element,
225 Chains const& chains)
226{
227 using Source = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename Field::indices_domain_t>>;
229 auto output = make_stencil<typename Field::memory_space, Target>(ddc::DiscreteElement<>());
230 auto const chain = chains.template get<Source::rank() + 1>();
231 auto const lower = chains.template get<Source::rank()>();
232 auto const boundary = [&] {
233 if constexpr (misc::Specialization<Rules, tensor::ExtrapolationRules>)
234 return rules;
235 else
236 return tensor::make_extrapolation_rules(field, rules);
237 }();
238 apply_incidence<false, Index, Source>(
239 ::sil::exterior::Cochain(tensor::Tensor(output.span_view())),
240 [&](auto sample, auto component) { return boundary(field, sample).mem(component); },
241 chain,
242 lower,
243 element);
244 return output;
245}
246
247template <
248 class Index,
249 class Field,
250 class Hodge,
251 class Inverse,
252 class Rules,
253 class Element,
254 class Chains>
255KOKKOS_FORCEINLINE_FUNCTION auto laplacian_codifferential(
256 Field const& field,
257 Hodge const& hodge,
258 Inverse const& inverse,
259 Rules const& rules,
260 Element element,
261 Chains const& chains)
262{
263 using Source = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename Field::indices_domain_t>>;
264 using Dual = misc::convert_type_seq_to_t<
265 tensor::TensorAntisymmetricIndex,
266 codifferential_hodge_output_indices_t<Index::size() - Source::rank(), Index>>;
268 auto evaluate = [&](auto sample, auto component) {
269 double value = 0.0;
270 for (std::size_t source = 0; source < Source::mem_size(); ++source)
271 value += hodge(sample, component.template uid<Dual>(), source)
272 * field.mem(sample, ddc::DiscreteElement<Source>(source));
273 return value;
274 };
275 LaplacianField<
276 Dual,
277 typename Field::non_indices_domain_t,
278 typename Field::memory_space,
279 decltype(evaluate)> const dual(field.non_indices_domain(), evaluate);
280 auto const boundary = [&] {
281 if constexpr (misc::Specialization<Rules, tensor::ExtrapolationRules>)
282 return rules;
283 else
284 return tensor::make_extrapolation_rules(dual, rules);
285 }();
286 auto output = make_stencil<typename Field::memory_space, Target>(ddc::DiscreteElement<>());
287 auto const chain = chains.template get<Dual::rank() + 1>();
288 auto const lower = chains.template get<Dual::rank()>();
289 using Intermediate = typename Inverse::source_index;
290 auto derivative
291 = make_stencil<typename Field::memory_space, Intermediate>(ddc::DiscreteElement<>());
292 apply_incidence<true, Index, Dual>(
293 ::sil::exterior::Cochain(tensor::Tensor(derivative.span_view())),
294 [&](auto sample, auto component) { return boundary(dual, sample).mem(component); },
295 chain,
296 lower,
297 element);
298 ddc::device_for_each(output.domain(), [&](auto target) {
299 double value = 0.0;
300 for (std::size_t source = 0; source < Intermediate::mem_size(); ++source)
301 value += inverse(element, target.template uid<Target>(), source)
302 * derivative.mem(ddc::DiscreteElement<Intermediate>(source));
303 if constexpr ((Index::size() * (Source::rank() + 1) + 1) % 2 == 1)
304 value = -value;
305 output.mem(target) = value;
306 });
307 return output;
308}
309
310template <class Domain, class Element>
311KOKKOS_FORCEINLINE_FUNCTION std::size_t laplacian_node_id(Domain const& domain, Element element)
312{
313 std::size_t id = 0;
314 for (std::size_t axis = 0; axis < Domain::rank(); ++axis)
315 id = id * ddc::detail::array(domain.extents())[axis] + ddc::detail::array(element)[axis]
316 - ddc::detail::array(domain.front())[axis];
317 return id;
318}
319
320template <class Domain>
321KOKKOS_FORCEINLINE_FUNCTION typename Domain::discrete_element_type laplacian_element(
322 Domain const& domain,
323 std::size_t id)
324{
325 typename Domain::discrete_element_type element = domain.front();
326 for (std::size_t axis = Domain::rank(); axis-- > 0;) {
327 ddc::detail::array(element)[axis] += id % ddc::detail::array(domain.extents())[axis];
328 id /= ddc::detail::array(domain.extents())[axis];
329 }
330 return element;
331}
332
333template <class Tensor>
334struct LaplacianInputEvaluator
335{
336 Tensor input;
337 template <class Output, class Element>
338 KOKKOS_FUNCTION void operator()(Output const& output, Element element) const
339 {
340 ddc::device_for_each(output.domain(), [&](auto component) {
341 output.mem(component) = input.mem(element, component);
342 });
343 }
344};
345
346template <class Index, class Cochain, class Tensor, class Boundary, class Chain, class Lower>
347struct LaplacianDerivativeEvaluator
348{
349 Tensor input;
350 Boundary boundary;
351 Chain chain;
352 Lower lower;
353 template <class Output, class Element>
354 KOKKOS_FUNCTION void operator()(Output const& output, Element element) const
355 {
356 Coboundary<Index, Cochain>::operator()(
357 output,
358 [&](auto sample, auto component) { return boundary(input, sample).mem(component); },
359 chain,
360 lower,
361 element);
362 }
363};
364
365template <
366 class Index,
367 class Cochain,
368 class Tensor,
369 class Metric,
370 class Position,
371 class Exec,
372 template <CellComplex, class, class, class, class, class> class Hodge>
373struct LaplacianCodifferential
374{
375 using Pair = LaplacianHodgePair<Index, Cochain, Metric, Position, Hodge>;
376 using DualIndex = typename Pair::Dual;
377 using Memory = typename Tensor::memory_space;
378 using Domain = misc::
379 cartesian_prod_t<typename Tensor::non_indices_domain_t, ddc::DiscreteDomain<DualIndex>>;
380 Pair stars;
381 ddc::Chunk<double, Domain, ddc::KokkosAllocator<double, Memory>> buffer;
382
383 LaplacianCodifferential(
384 Exec const& exec,
385 Tensor const& input,
386 Metric const& metric,
387 Position const& position)
388 : stars(exec, metric, position)
389 , buffer(Domain(input.non_indices_domain(), tensor::TensorAccessor<DualIndex>::domain()),
390 ddc::KokkosAllocator<double, Memory>())
391 {
392 }
393
394 template <class Output, class Evaluate, class Rules>
395 void apply(
396 Exec const& exec,
397 Output output,
398 Evaluate const& evaluate,
399 Metric const& metric,
400 Position const& position,
401 Rules const& rules)
402 {
403 ::sil::exterior::Cochain const dual {tensor::Tensor(buffer.span_view())};
404 auto const& forward = stars.forward;
405 auto const& inverse = stars.inverse;
406 auto const boundary = [&] {
407 if constexpr (misc::Specialization<Rules, tensor::ExtrapolationRules>)
408 return rules;
409 else
410 return tensor::make_extrapolation_rules(dual, rules);
411 }();
412 using ChainDomain =
413 typename NonSpectatorDimension<Index, typename Tensor::non_indices_domain_t>::type;
415 auto const lower = tangent_basis<DualIndex::rank(), ChainDomain>(exec);
416 ddc::parallel_for_each(
417 "similie_laplacian_first_hodge",
418 exec,
419 dual.non_indices_domain(),
420 KOKKOS_LAMBDA(
421 typename Tensor::non_indices_domain_t::discrete_element_type element) {
422 auto point = make_stencil<Memory, Cochain>(ddc::DiscreteElement<>());
423 evaluate(::sil::exterior::Cochain(tensor::Tensor(point.span_view())), element);
424 forward(dual[element],
425 ::sil::exterior::Cochain(tensor::Tensor(point.span_view())),
426 metric,
427 position,
428 element);
429 });
430 ddc::parallel_for_each(
431 "similie_laplacian_second_hodge",
432 exec,
433 output.non_indices_domain(),
434 KOKKOS_LAMBDA(
435 typename Tensor::non_indices_domain_t::discrete_element_type element) {
436 auto derivative = make_stencil<Memory, typename Pair::Intermediate>(
437 ddc::DiscreteElement<>());
438 TransposedCoboundary<Index, DualIndex>::operator()(
439 ::sil::exterior::Cochain(tensor::Tensor(derivative.span_view())),
440 [&](auto sample, auto component) {
441 return boundary(dual, sample).mem(component);
442 },
443 chain,
444 lower,
445 element);
446 inverse(output[element],
447 ::sil::exterior::Cochain(tensor::Tensor(derivative.span_view())),
448 metric,
449 position,
450 element);
451 if constexpr ((Index::size() * (Cochain::rank() + 1) + 1) % 2 == 1)
452 output[element] *= -1;
453 });
454 }
455};
456
457// Disabled endpoint terms do not instantiate nonexistent cochain degrees.
458struct EmptyLaplacianTerm
459{
460 template <class... Args>
461 EmptyLaplacianTerm(Args const&...)
462 {
463 }
464};
465
466template <
467 bool Enabled,
468 class Index,
469 class Cochain,
470 class Tensor,
471 class Metric,
472 class Position,
473 class Exec,
474 template <CellComplex, class, class, class, class, class> class Hodge>
475struct LaplacianUpper : EmptyLaplacianTerm
476{
477 using EmptyLaplacianTerm::EmptyLaplacianTerm;
478};
479
480template <
481 class Index,
482 class Cochain,
483 class Tensor,
484 class Metric,
485 class Position,
486 class Exec,
487 template <CellComplex, class, class, class, class, class> class Hodge>
488struct LaplacianUpper<true, Index, Cochain, Tensor, Metric, Position, Exec, Hodge>
489{
490 using Added = tensor::Covariant<
491 IndexForCodifferentialOfCoboundaryInLaplacian<tensor::uncharacterize_t<Index>>>;
492 LaplacianCodifferential<
493 Added,
495 Tensor,
496 Metric,
497 Position,
498 Exec,
499 Hodge>
500 operation;
501 LaplacianUpper(
502 Exec const& exec,
503 Tensor const& input,
504 Metric const& metric,
505 Position const& position)
506 : operation(exec, input, metric, position)
507 {
508 }
509};
510
511template <
512 bool Enabled,
513 class Index,
514 class Cochain,
515 class Tensor,
516 class Metric,
517 class Position,
518 class Exec,
519 template <CellComplex, class, class, class, class, class> class Hodge>
520struct LaplacianLower : EmptyLaplacianTerm
521{
522 using EmptyLaplacianTerm::EmptyLaplacianTerm;
523};
524
525template <
526 class Index,
527 class Cochain,
528 class Tensor,
529 class Metric,
530 class Position,
531 class Exec,
532 template <CellComplex, class, class, class, class, class> class Hodge>
533struct LaplacianLower<true, Index, Cochain, Tensor, Metric, Position, Exec, Hodge>
534{
535 using Memory = typename Tensor::memory_space;
536 using LowerDomain = misc::cartesian_prod_t<
537 typename Tensor::non_indices_domain_t,
538 ddc::DiscreteDomain<codifferential_index_t<Index, Cochain>>>;
539 LaplacianCodifferential<Index, Cochain, Tensor, Metric, Position, Exec, Hodge> operation;
540 ddc::Chunk<double, LowerDomain, ddc::KokkosAllocator<double, Memory>> lower;
541 std::optional<ddc::Chunk<
542 double,
543 typename Tensor::discrete_domain_type,
544 ddc::KokkosAllocator<double, Memory>>>
545 contribution;
546 LaplacianLower(
547 Exec const& exec,
548 Tensor const& output,
549 Tensor const& input,
550 Metric const& metric,
551 Position const& position)
552 : operation(exec, input, metric, position)
553 , lower(LowerDomain(
554 input.non_indices_domain(),
555 tensor::TensorAccessor<codifferential_index_t<Index, Cochain>>::domain()),
556 ddc::KokkosAllocator<double, Memory>())
557 {
558 if constexpr (Cochain::rank() < Index::size())
559 contribution.emplace(output.domain(), ddc::KokkosAllocator<double, Memory>());
560 }
561};
562
563} // namespace detail
564} // namespace sil::exterior
565
566
567namespace sil::exterior {
568
578template <
579 tensor::TensorIndex MetricIndex,
580 tensor::TensorNatIndex Index,
581 tensor::TensorIndex Cochain,
582 CochainType Tensor,
583 misc::Specialization<tensor::Tensor> Metric,
584 misc::Specialization<tensor::Tensor> Position,
585 class Exec,
586 template <CellComplex, class, class, class, class, class> class Hodge = DiscreteHodgeStar>
588{
589 Exec m_exec;
590 Metric m_metric;
591 Position m_position;
592 detail::LaplacianUpper<
593 (Cochain::rank() < Index::size()),
594 Index,
595 Cochain,
596 Tensor,
597 Metric,
598 Position,
599 Exec,
600 Hodge>
601 m_upper;
602 detail::LaplacianLower<
603 (Cochain::rank() > 0),
604 Index,
605 Cochain,
606 Tensor,
607 Metric,
608 Position,
609 Exec,
610 Hodge>
611 m_lower;
612
613public:
614 using staged_storage_type = detail::
615 LinearSpatialOperatorStorage<Laplacian, Index, Cochain, Cochain, 2, Tensor, Exec, true>;
616
618 Exec const& exec,
619 Tensor const& output,
620 Tensor const& input,
621 Metric const& metric,
622 Position const& position)
623 : m_exec(exec)
624 , m_metric(metric)
625 , m_position(position)
626 , m_upper(exec, input, metric, position)
627 , m_lower(exec, output, input, metric, position)
628 {
629 }
630
631 template <
635 Tensor const& output,
636 Tensor const& input,
637 Primal const& primal = {},
638 Dual const& dual = {})
639 {
640 if constexpr (Cochain::rank() < Index::size()) {
641 using Added = tensor::Covariant<
642 IndexForCodifferentialOfCoboundaryInLaplacian<tensor::uncharacterize_t<Index>>>;
643 using ChainDomain = typename detail::
644 NonSpectatorDimension<Index, typename Tensor::non_indices_domain_t>::type;
645 auto const chain = tangent_basis<Cochain::rank() + 1, ChainDomain>(m_exec);
646 auto const lower = tangent_basis<Cochain::rank(), ChainDomain>(m_exec);
647 auto const boundary = [&] {
649 return primal;
650 else
651 return tensor::make_extrapolation_rules(input, primal);
652 }();
653 m_upper.operation
654 .apply(m_exec,
655 output,
656 detail::LaplacianDerivativeEvaluator<
657 Added,
658 Cochain,
659 Tensor,
660 decltype(boundary),
661 decltype(chain),
662 decltype(lower)> {input, boundary, chain, lower},
663 m_metric,
664 m_position,
665 dual);
666 }
667 if constexpr (Cochain::rank() > 0) {
668 ::sil::exterior::Cochain lower {tensor::Tensor(m_lower.lower)};
669 m_lower.operation
670 .apply(m_exec,
671 lower,
672 detail::LaplacianInputEvaluator<Tensor> {input},
673 m_metric,
674 m_position,
675 dual);
676 if constexpr (Cochain::rank() < Index::size()) {
677 Tensor contribution(tensor::Tensor(m_lower.contribution->span_view()));
678 deriv<Index,
679 codifferential_index_t<Index, Cochain>>(m_exec, contribution, lower, primal);
680 ddc::parallel_for_each(
681 "similie_add_laplacian_contributions",
682 m_exec,
683 output.domain(),
684 KOKKOS_LAMBDA(typename Tensor::discrete_element_type element) {
685 output.mem(element) += contribution.mem(element);
686 });
687 } else
688 deriv<Index, codifferential_index_t<Index, Cochain>>(m_exec, output, lower, primal);
689 }
690 return output;
691 }
692
693private:
694 friend staged_storage_type;
695
696 // Canonical component matrices are needed only while assembling a stencil.
697 static auto geometry(Exec const& exec, Metric const& metric, Position const& position)
698 {
699 using Domain = typename detail::
700 NonSpectatorDimension<Index, typename Tensor::non_indices_domain_t>::type;
701 detail::LaplacianChains<0, Domain, typename Tensor::memory_space> const chains {};
702 auto derivative = [&] {
703 if constexpr (Cochain::rank() < Index::size()) {
704 using Added = tensor::Covariant<IndexForCodifferentialOfCoboundaryInLaplacian<
705 tensor::uncharacterize_t<Index>>>;
706 return detail::LaplacianHodgePair<
707 Added,
709 Metric,
710 Position,
711 Hodge>(exec, metric, position)
712 .canonical(exec, metric, position);
713 } else
714 return std::pair {0, 0};
715 }();
716 auto codifferential = [&] {
717 if constexpr (Cochain::rank() > 0)
718 return detail::LaplacianHodgePair<
719 Index,
720 Cochain,
721 Metric,
722 Position,
723 Hodge>(exec, metric, position)
724 .canonical(exec, metric, position);
725 else
726 return std::pair {0, 0};
727 }();
728 return detail::LaplacianGeometry {
729 derivative.first,
730 derivative.second,
731 codifferential.first,
732 codifferential.second,
733 chains};
734 }
735
736public:
737 template <class Geometry, class Evaluate, class Element, class Primal, class Dual>
738 KOKKOS_FORCEINLINE_FUNCTION static auto operator()(
739 Geometry const& staged,
740 Tensor const& input,
741 Evaluate const& evaluate,
742 Element element,
743 Primal const& primal,
744 Dual const& dual)
745 {
746 using Memory = typename Tensor::memory_space;
747 using Domain = typename Tensor::non_indices_domain_t;
748 detail::LaplacianField<Cochain, Domain, Memory, Evaluate> const
749 field(input.non_indices_domain(), evaluate);
750 auto output = detail::make_stencil<Memory, Cochain>(ddc::DiscreteElement<>());
751 if constexpr (Cochain::rank() < Index::size()) {
752 using Added = tensor::Covariant<
755 auto derivative = [&](auto sample, auto component) {
756 return detail::laplacian_derivative<Added>(field, primal, sample, staged.chains)
757 .mem(component);
758 };
759 detail::LaplacianField<Derived, Domain, Memory, decltype(derivative)> const
760 derivatives(input.non_indices_domain(), derivative);
761 auto result = detail::laplacian_codifferential<Added>(
762 derivatives,
763 staged.derivative_hodge,
764 staged.inverse_derivative_hodge,
765 dual,
766 element,
767 staged.chains);
768 ddc::device_for_each(output.domain(), [&](auto component) {
769 output.mem(component) += result.mem(component);
770 });
771 }
772 if constexpr (Cochain::rank() > 0) {
774 auto codifferential = [&](auto sample, auto component) {
775 return detail::laplacian_codifferential<Index>(
776 field,
777 staged.hodge,
778 staged.inverse_hodge,
779 dual,
780 sample,
781 staged.chains)
782 .mem(component);
783 };
784 detail::LaplacianField<Lower, Domain, Memory, decltype(codifferential)> const
785 codifferentials(input.non_indices_domain(), codifferential);
786 auto result = detail::laplacian_derivative<
787 Index>(codifferentials, primal, element, staged.chains);
788 ddc::device_for_each(output.domain(), [&](auto component) {
789 output.mem(component) = detail::
790 laplacian_contribution_sum(output.mem(component), result.mem(component));
791 });
792 }
793 return output;
794 }
795
801 template <class Geometry, class Element, class Primal, class Dual>
802 KOKKOS_FORCEINLINE_FUNCTION static auto value(
803 Geometry const& geometry,
804 Tensor const& input,
805 Element element,
806 Primal const& primal,
807 Dual const& dual)
808 {
809 return detail::operator_stencil<
810 Index,
811 Cochain,
812 Cochain,
813 2>(input.non_indices_domain(), element, [&](auto evaluate) {
814 return operator()(geometry, input, evaluate, element, primal, dual);
815 });
816 }
817
818 template <class Geometry, class Element, class Component, class Primal, class Dual>
819 KOKKOS_FORCEINLINE_FUNCTION static double value(
820 Geometry const& staged,
821 Tensor const& input,
822 Element element,
823 Component target,
824 std::size_t column,
825 Primal const& primal,
826 Dual const& dual)
827 {
828 auto result = operator()(
829 staged,
830 input,
831 [&](auto sample, auto component) {
832 return detail::laplacian_node_id(input.non_indices_domain(), sample)
833 * Cochain::mem_size()
834 + component.template uid<Cochain>()
835 == column
836 ? 1.0
837 : 0.0;
838 },
839 element,
840 primal,
841 dual);
842 return result.mem(target);
843 }
844};
845
846template <
847 tensor::TensorIndex MetricIndex,
850 template <CellComplex, class, class, class, class, class> class Hodge = DiscreteHodgeStar,
851 class Exec,
852 CochainType Tensor,
853 class Metric,
854 class Position>
856 Exec const& exec,
857 Tensor const& output,
858 Tensor const& input,
859 Metric const& metric,
860 Position const& position)
861{
862 return {exec, output, input, metric, position};
863}
864
865namespace detail {
866
867template <
868 tensor::TensorIndex MetricIndex,
869 tensor::TensorNatIndex LaplacianDummyIndex,
870 tensor::TensorIndex CochainTag,
871 CochainType DualTensorBufferType,
872 CochainType TensorType,
874 misc::Specialization<tensor::Tensor> DualHodgeStarType,
875 class ExecSpace,
876 class PrimalExtrapolationRule = tensor::ClampExtrapolationRule,
877 class DualExtrapolationRule = tensor::ZeroExtrapolationRule>
878TensorType codifferential_of_coboundary(
879 ExecSpace const& exec_space,
880 TensorType out_tensor,
881 TensorType const& tensor,
882 HodgeStarType const& hodge_star,
883 DualHodgeStarType const& dual_hodge_star,
884 DualTensorBufferType const& dual_tensor_buffer,
885 PrimalExtrapolationRule const& primal_extrapolation = {},
886 DualExtrapolationRule const& dual_extrapolation = {})
887{
888 auto const primal_extrapolation_rules = [&]() {
889 if constexpr (misc::Specialization<PrimalExtrapolationRule, tensor::ExtrapolationRules>)
890 return primal_extrapolation;
891 else
892 return tensor::make_extrapolation_rules(tensor, primal_extrapolation);
893 }();
894 auto const dual_extrapolation_rules = [&]() {
895 if constexpr (misc::Specialization<DualExtrapolationRule, tensor::ExtrapolationRules>)
896 return dual_extrapolation;
897 else
898 return tensor::make_extrapolation_rules(dual_tensor_buffer, dual_extrapolation);
899 }();
900 using coboundary_output_index = coboundary_index_t<LaplacianDummyIndex, CochainTag>;
901 using codifferential_hodge_output_indices = codifferential_hodge_output_indices_t<
902 LaplacianDummyIndex::size() - coboundary_output_index::rank(),
903 LaplacianDummyIndex>;
904 using coboundary_dual_tensor_index = misc::convert_type_seq_to_t<
905 tensor::TensorAntisymmetricIndex,
906 codifferential_hodge_output_indices>;
907 using dual_codifferential_hodge_input_indices = ddc::type_seq_merge_t<
908 ddc::TypeSeq<LaplacianDummyIndex>,
909 codifferential_hodge_output_indices>;
910 using dual_codifferential_index = misc::convert_type_seq_to_t<
911 tensor::TensorAntisymmetricIndex,
912 dual_codifferential_hodge_input_indices>;
913 auto const chain = tangent_basis<
914 CochainTag::rank() + 1,
915 typename detail::NonSpectatorDimension<
916 LaplacianDummyIndex,
917 typename TensorType::non_indices_domain_t>::type>(exec_space);
918 auto const lower_chain = tangent_basis<
919 CochainTag::rank(),
920 typename detail::NonSpectatorDimension<
921 LaplacianDummyIndex,
922 typename TensorType::non_indices_domain_t>::type>(exec_space);
923 auto dual_chain = tangent_basis<
924 coboundary_dual_tensor_index::rank() + 1,
925 typename detail::NonSpectatorDimension<
926 LaplacianDummyIndex,
927 typename TensorType::non_indices_domain_t>::type>(exec_space);
928 auto dual_lower_chain = tangent_basis<
929 coboundary_dual_tensor_index::rank(),
930 typename detail::NonSpectatorDimension<
931 LaplacianDummyIndex,
932 typename TensorType::non_indices_domain_t>::type>(exec_space);
933
934 SIMILIE_DEBUG_LOG("similie_deriv_and_apply_first_hodge_star_for_codifferential_of_coboundary");
935 ddc::parallel_for_each(
936 "similie_deriv_and_apply_first_hodge_star_for_codifferential_of_coboundary",
937 exec_space,
938 dual_tensor_buffer.non_indices_domain(),
939 KOKKOS_LAMBDA(typename TensorType::non_indices_domain_t::discrete_element_type elem) {
940 [[maybe_unused]] tensor::TensorAccessor<coboundary_output_index>
941 derivative_accessor;
942 std::array<double, coboundary_output_index::mem_size()> derivative_alloc {};
943 ddc::ChunkSpan<
944 double,
945 ddc::DiscreteDomain<coboundary_output_index>,
946 Kokkos::layout_right,
947 typename TensorType::memory_space>
948 derivative_span(derivative_alloc.data(), derivative_accessor.domain());
949 ::sil::exterior::Cochain derivative_tensor {sil::tensor::Tensor(derivative_span)};
950
951 Coboundary<LaplacianDummyIndex, CochainTag>::operator()(
952 derivative_tensor,
953 [&](auto sampled_elem, auto cochain_elem) {
954 return primal_extrapolation_rules(tensor, sampled_elem)
955 .mem(cochain_elem);
956 },
957 chain,
958 lower_chain,
959 elem);
960
962 tensor_prod(dual_tensor_buffer[elem], derivative_tensor, hodge_star[elem]);
963 });
964
965 SIMILIE_DEBUG_LOG("similie_deriv_and_apply_second_hodge_star_for_codifferential_of_coboundary");
966 ddc::parallel_for_each(
967 "similie_deriv_and_apply_second_hodge_star_for_codifferential_of_coboundary",
968 exec_space,
969 out_tensor.non_indices_domain(),
970 KOKKOS_LAMBDA(typename TensorType::non_indices_domain_t::discrete_element_type elem) {
971 [[maybe_unused]] tensor::TensorAccessor<dual_codifferential_index>
972 dual_codifferential_accessor;
973 std::array<double, dual_codifferential_index::mem_size()>
974 dual_codifferential_alloc {};
975 ddc::ChunkSpan<
976 double,
977 ddc::DiscreteDomain<dual_codifferential_index>,
978 Kokkos::layout_right,
979 typename TensorType::memory_space>
980 dual_codifferential_span(
981 dual_codifferential_alloc.data(),
982 dual_codifferential_accessor.domain());
983 ::sil::exterior::Cochain dual_codifferential {
984 sil::tensor::Tensor(dual_codifferential_span)};
985
986 TransposedCoboundary<LaplacianDummyIndex, coboundary_dual_tensor_index>::operator()(
987 dual_codifferential,
988 [&](auto sampled_elem, auto dual_elem) {
989 return dual_extrapolation_rules(dual_tensor_buffer, sampled_elem)
990 .mem(dual_elem);
991 },
992 dual_chain,
993 dual_lower_chain,
994 elem);
995
997 tensor_prod(out_tensor[elem], dual_codifferential, dual_hodge_star[elem]);
998 if constexpr (
999 (LaplacianDummyIndex::size() * (coboundary_output_index::rank() + 1) + 1)
1000 % 2
1001 == 1) {
1002 out_tensor[elem] *= -1;
1003 }
1004 });
1005
1006 return out_tensor;
1007}
1008
1009template <class LaplacianDummyIndex, class CochainTag>
1010concept ZeroRankLaplacianCochain = CochainTag::rank() == 0;
1011
1012template <class LaplacianDummyIndex, class CochainTag>
1013concept IntermediateRankLaplacianCochain
1014 = CochainTag::rank() > 0 && CochainTag::rank() < LaplacianDummyIndex::size();
1015
1016template <class LaplacianDummyIndex, class CochainTag>
1017concept TopRankLaplacianCochain = CochainTag::rank() == LaplacianDummyIndex::size();
1018
1019} // namespace detail
1020
1021template <
1022 tensor::TensorIndex MetricIndex,
1023 tensor::TensorNatIndex LaplacianDummyIndex,
1024 tensor::TensorIndex CochainTag,
1025 CochainType TensorType,
1026 misc::Specialization<tensor::Tensor> DerivativeHodgeStarType,
1027 misc::Specialization<tensor::Tensor> DualDerivativeHodgeStarType,
1028 CochainType DerivativeDualTensorBufferType,
1029 class ExecSpace,
1030 class PrimalExtrapolationRule = tensor::ClampExtrapolationRule,
1031 class DualExtrapolationRule = tensor::ZeroExtrapolationRule>
1032 requires(detail::ZeroRankLaplacianCochain<LaplacianDummyIndex, CochainTag>)
1033TensorType laplacian(
1034 ExecSpace const& exec_space,
1035 TensorType const& laplacian_tensor,
1036 TensorType const& tensor,
1037 DerivativeHodgeStarType const& hodge_star,
1038 DualDerivativeHodgeStarType const& dual_hodge_star,
1039 DerivativeDualTensorBufferType const& dual_tensor_buffer,
1040 PrimalExtrapolationRule const& primal_extrapolation = {},
1041 DualExtrapolationRule const& dual_extrapolation = {})
1042{
1043 using codifferential_of_coboundary_index
1044 = tensor::Covariant<IndexForCodifferentialOfCoboundaryInLaplacian<
1045 tensor::uncharacterize_t<LaplacianDummyIndex>>>;
1046 return detail::codifferential_of_coboundary<
1047 MetricIndex,
1048 codifferential_of_coboundary_index,
1049 CochainTag>(
1050 exec_space,
1051 laplacian_tensor,
1052 tensor,
1053 hodge_star,
1054 dual_hodge_star,
1055 dual_tensor_buffer,
1056 primal_extrapolation,
1057 dual_extrapolation);
1058}
1059
1060template <
1061 tensor::TensorIndex MetricIndex,
1062 tensor::TensorNatIndex LaplacianDummyIndex,
1063 tensor::TensorIndex CochainTag,
1064 CochainType TensorType,
1065 misc::Specialization<tensor::Tensor> DerivativeHodgeStarType,
1066 misc::Specialization<tensor::Tensor> DualDerivativeHodgeStarType,
1067 CochainType DerivativeDualTensorBufferType,
1068 misc::Specialization<tensor::Tensor> HodgeStarType,
1069 misc::Specialization<tensor::Tensor> DualHodgeStarType,
1070 CochainType DualTensorBufferType,
1071 CochainType CodifferentialTensorBufferType,
1072 CochainType CoboundaryOfCodifferentialBufferType,
1073 class ExecSpace,
1074 class PrimalExtrapolationRule = tensor::ClampExtrapolationRule,
1075 class DualExtrapolationRule = tensor::ZeroExtrapolationRule>
1076 requires(detail::IntermediateRankLaplacianCochain<LaplacianDummyIndex, CochainTag>)
1077TensorType laplacian(
1078 ExecSpace const& exec_space,
1079 TensorType const& laplacian_tensor,
1080 TensorType const& tensor,
1081 DerivativeHodgeStarType const& derivative_hodge_star,
1082 DualDerivativeHodgeStarType const& dual_derivative_hodge_star,
1083 DerivativeDualTensorBufferType const& derivative_dual_tensor_buffer,
1084 HodgeStarType const& hodge_star,
1085 DualHodgeStarType const& dual_hodge_star,
1086 DualTensorBufferType const& dual_tensor_buffer,
1087 CodifferentialTensorBufferType const& codifferential_tensor_buffer,
1088 CoboundaryOfCodifferentialBufferType const& coboundary_of_codifferential_buffer,
1089 PrimalExtrapolationRule const& primal_extrapolation = {},
1090 DualExtrapolationRule const& dual_extrapolation = {})
1091{
1092 using codifferential_of_coboundary_index
1093 = tensor::Covariant<IndexForCodifferentialOfCoboundaryInLaplacian<
1094 tensor::uncharacterize_t<LaplacianDummyIndex>>>;
1095 auto exec_spaces = Kokkos::Experimental::partition_space(exec_space, 1, 1);
1096
1097 detail::codifferential_of_coboundary<
1098 MetricIndex,
1099 codifferential_of_coboundary_index,
1100 CochainTag>(
1101 exec_spaces[0],
1102 laplacian_tensor,
1103 tensor,
1104 derivative_hodge_star,
1105 dual_derivative_hodge_star,
1106 derivative_dual_tensor_buffer,
1107 primal_extrapolation,
1108 dual_extrapolation);
1109
1111 exec_spaces[1],
1112 codifferential_tensor_buffer,
1113 tensor,
1114 hodge_star,
1115 dual_hodge_star,
1116 dual_tensor_buffer,
1117 dual_extrapolation);
1118 sil::exterior::
1119 deriv<LaplacianDummyIndex, codifferential_index_t<LaplacianDummyIndex, CochainTag>>(
1120 exec_spaces[1],
1121 coboundary_of_codifferential_buffer,
1122 codifferential_tensor_buffer,
1123 primal_extrapolation);
1124
1125 exec_spaces[0].fence();
1126 exec_spaces[1].fence();
1127
1128 SIMILIE_DEBUG_LOG("similie_add_coboundary_of_codifferential_contribution_to_laplacian");
1129 ddc::parallel_for_each(
1130 "similie_add_coboundary_of_codifferential_contribution_to_laplacian",
1131 exec_space,
1132 laplacian_tensor.domain(),
1133 KOKKOS_LAMBDA(typename TensorType::discrete_element_type elem) {
1134 laplacian_tensor.mem(elem) += coboundary_of_codifferential_buffer.mem(elem);
1135 });
1136
1137 return laplacian_tensor;
1138}
1139
1140template <
1141 tensor::TensorIndex MetricIndex,
1142 tensor::TensorNatIndex LaplacianDummyIndex,
1143 tensor::TensorIndex CochainTag,
1144 CochainType TensorType,
1145 misc::Specialization<tensor::Tensor> HodgeStarType,
1146 misc::Specialization<tensor::Tensor> DualHodgeStarType,
1147 CochainType DualTensorBufferType,
1148 CochainType CodifferentialTensorBufferType,
1149 class ExecSpace,
1150 class PrimalExtrapolationRule = tensor::ClampExtrapolationRule,
1151 class DualExtrapolationRule = tensor::ZeroExtrapolationRule>
1152 requires(detail::TopRankLaplacianCochain<LaplacianDummyIndex, CochainTag>)
1153TensorType laplacian(
1154 ExecSpace const& exec_space,
1155 TensorType const& laplacian_tensor,
1156 TensorType const& tensor,
1157 HodgeStarType const& hodge_star,
1158 DualHodgeStarType const& dual_hodge_star,
1159 DualTensorBufferType const& dual_tensor_buffer,
1160 CodifferentialTensorBufferType const& codifferential_tensor_buffer,
1161 PrimalExtrapolationRule const& primal_extrapolation = {},
1162 DualExtrapolationRule const& dual_extrapolation = {})
1163{
1165 exec_space,
1166 codifferential_tensor_buffer,
1167 tensor,
1168 hodge_star,
1169 dual_hodge_star,
1170 dual_tensor_buffer,
1171 dual_extrapolation);
1172 return sil::exterior::
1173 deriv<LaplacianDummyIndex, codifferential_index_t<LaplacianDummyIndex, CochainTag>>(
1174 exec_space,
1175 laplacian_tensor,
1176 codifferential_tensor_buffer,
1177 primal_extrapolation);
1178}
1179
1180
1181} // namespace sil::exterior
Cochain class.
Definition cochain.hpp:32
Hodge Laplacian assembled from independently selected Hodge-star policies.
detail:: LinearSpatialOperatorStorage< Laplacian, Index, Cochain, Cochain, 2, Tensor, Exec, true > staged_storage_type
static KOKKOS_FORCEINLINE_FUNCTION auto operator()(Geometry const &staged, Tensor const &input, Evaluate const &evaluate, Element element, Primal const &primal, Dual const &dual)
Laplacian(Exec const &exec, Tensor const &output, Tensor const &input, Metric const &metric, Position const &position)
Tensor operator()(Tensor const &output, Tensor const &input, Primal const &primal={}, Dual const &dual={})
static KOKKOS_FORCEINLINE_FUNCTION double value(Geometry const &staged, Tensor const &input, Element element, Component target, std::size_t column, Primal const &primal, Dual const &dual)
static KOKKOS_FORCEINLINE_FUNCTION auto value(Geometry const &geometry, Tensor const &input, Element element, Primal const &primal, Dual const &dual)
Complete fixed stencil at one node, including component couplings.
TensorType laplacian(ExecSpace const &exec_space, TensorType const &laplacian_tensor, TensorType const &tensor, DerivativeHodgeStarType const &hodge_star, DualDerivativeHodgeStarType const &dual_hodge_star, DerivativeDualTensorBufferType const &dual_tensor_buffer, PrimalExtrapolationRule const &primal_extrapolation={}, DualExtrapolationRule const &dual_extrapolation={})
Chain(Head, Tail...) -> Chain< typename Head::value_type, typename Head::array_layout, typename Head::memory_space >
Laplacian< MetricIndex, Index, Cochain, Tensor, Metric, Position, Exec, Hodge > make_laplacian(Exec const &exec, Tensor const &output, Tensor const &input, Metric const &metric, Position const &position)
Cochain(Tensor const &) -> Cochain< Tensor >
KOKKOS_FUNCTION Chain< boundary_t< SimplexType >, typename AllocationType::array_layout, typename AllocationType::memory_space > boundary(AllocationType allocation, SimplexType const &simplex)
Definition boundary.hpp:170
codifferential_tensor_t< TagToRemoveFromCochain, CochainTag, TensorType > codifferential(ExecSpace const &exec_space, codifferential_tensor_t< TagToRemoveFromCochain, CochainTag, TensorType > const &codifferential_tensor, TensorType const &tensor, HodgeStarType const &hodge_star, DualHodgeStarType const &dual_hodge_star, DualTensorType const &dual_tensor_buffer, DualExtrapolationRule const &dual_extrapolation={})
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
typename detail::CodifferentialIndex< TagToRemoveFromCochain, CochainTag >::type codifferential_index_t
coboundary_tensor_t< TagToAddToCochain, CochainTag, TensorType > deriv(ExecSpace const &exec_space, coboundary_tensor_t< TagToAddToCochain, CochainTag, TensorType > const &coboundary_tensor, TensorType const &tensor, ExtrapolationRules const &extrapolation={})
KOKKOS_FUNCTION constexpr auto tangent_basis(Elem elem)
typename detail::CodifferentialHodgeOutputIndexSeq< EndId, T >::type codifferential_hodge_output_indices_t
typename detail::CoboundaryIndex< TagToAddToCochain, CochainTag >::type coboundary_index_t
convert_type_seq_to_t< ddc::DiscreteDomain, typename detail::CartesianProd< Domains... >::type > cartesian_prod_t
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
KOKKOS_FUNCTION auto make_extrapolation_rules(ExtrapolationRule rule)
Broadcast a rule to both sides of all N non-index dimensions.
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::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t
Spatial closure evaluated independently of the stored field values.