12#include <similie/exterior/coboundary/coboundary.hpp>
13#include <similie/exterior/cochain.hpp>
14#include <similie/exterior/concepts.hpp>
15#include <similie/exterior/continuous_hodge_star/continuous_hodge_star.hpp>
16#include <similie/exterior/cosimplex.hpp>
17#include <similie/exterior/stencil.hpp>
18#include <similie/misc/domain_contains.hpp>
19#include <similie/misc/macros.hpp>
20#include <similie/misc/specialization.hpp>
21#include <similie/misc/type_seq_ext.hpp>
22#include <similie/tensor/tensor_impl.hpp>
23#include <similie/tensor/tensor_indices/character.hpp>
25#include <Kokkos_StdAlgorithms.hpp>
35struct CodifferentialType;
41 class LayoutStridedPolicy1,
42 class LayoutStridedPolicy2,
44struct CodifferentialType<
47 LayoutStridedPolicy2>>
49 using type = Cosimplex<
Simplex<K - 1, Tag...>, ElementType>;
54template <misc::Specialization<Cochain> CochainType>
59template <
class TagToRemoveFromCochain,
class CochainTag>
60struct CodifferentialIndex;
62template <tensor::TensorNatIndex TagToRemoveFromCochain, tensor::TensorNatIndex CochainTag>
63 requires(CochainTag::rank() == 1 && std::is_same_v<TagToRemoveFromCochain, CochainTag>)
64struct CodifferentialIndex<TagToRemoveFromCochain, CochainTag>
69template <tensor::TensorNatIndex TagToRemoveFromCochain, tensor::TensorNatIndex Tag>
70struct CodifferentialIndex<
71 TagToRemoveFromCochain,
72 tensor::TensorAntisymmetricIndex<TagToRemoveFromCochain, Tag>>
78 requires(
sizeof...(Tag) > 1)
79struct CodifferentialIndex<
80 TagToRemoveFromCochain,
88template <
class TagToRemoveFromCochain,
class CochainTag>
90 typename detail::CodifferentialIndex<TagToRemoveFromCochain, CochainTag>::type;
98struct CodifferentialTensorType
101 typename TensorType::element_type,
102 ddc::replace_dim_of_t<
103 typename TensorType::discrete_domain_type,
106 typename TensorType::layout_type,
107 typename TensorType::memory_space>>;
115 CochainType TensorType>
117 CodifferentialTensorType<TagToRemoveFromCochain, CochainTag, TensorType>::type;
120template <
class... Args>
131template <tensor::TensorNatIndex TagToRemoveFromCochain, tensor::TensorIndex CochainTag>
143 DualEvaluator
const& evaluate_dual,
144 InverseHodge
const& inverse_hodge,
146 LowerChain
const& lower_chain,
150 TagToRemoveFromCochain::size() - CochainTag::rank(),
151 TagToRemoveFromCochain>;
156 ddc::type_seq_merge_t<ddc::TypeSeq<TagToRemoveFromCochain>, DualIndices>>;
157 auto dual = detail::make_stencil<typename Output::memory_space, TargetIndex>(
158 ddc::DiscreteElement<>());
166 if constexpr ((TagToRemoveFromCochain::size() * (CochainTag::rank() + 1) + 1) % 2 == 1)
173 template <
class Geometry, CochainType Field,
class Element,
class Rules>
174 requires requires(Geometry
const& geometry) {
178 KOKKOS_FORCEINLINE_FUNCTION
static auto value(
179 Geometry
const& geometry,
184 using Memory =
typename Field::memory_space;
185 using Domain =
typename Field::non_indices_domain_t;
186 using DualIndex =
typename Geometry::DualIndex;
187 return detail::operator_stencil<
188 TagToRemoveFromCochain,
191 1>(input.non_indices_domain(), element, [&](
auto evaluate) {
192 auto evaluate_dual = [&](
auto sample,
auto component) {
193 auto primal = detail::make_stencil<Memory, CochainTag>(ddc::DiscreteElement<>());
194 auto dual = detail::make_stencil<Memory, DualIndex>(ddc::DiscreteElement<>());
195 ddc::device_for_each(primal.domain(), [&](
auto source) {
196 primal.mem(source) = evaluate(sample, source);
204 return dual.mem(component);
206 detail::StencilField<DualIndex, Domain, Memory,
decltype(evaluate_dual)>
const
207 dual(input.non_indices_domain(), evaluate_dual);
214 auto output = detail::make_stencil<
217 ddc::DiscreteElement<>());
220 [&](
auto sample,
auto component) {
221 return boundary(dual, sample).mem(component);
223 [&](
auto result,
auto derivative,
auto sample) {
242 template <
class DualField,
class InverseHodge,
class Element>
243 requires(CochainTag::rank() == 1)
246 InverseHodge
const& inverse_hodge,
252 TagToRemoveFromCochain::size() - 1,
253 TagToRemoveFromCochain>>;
256 ddc::type_seq_merge_t<
257 ddc::TypeSeq<TagToRemoveFromCochain>,
259 TagToRemoveFromCochain::size() - 1,
260 TagToRemoveFromCochain>>>;
261 using ChainDomain =
typename detail::NonSpectatorDimension<
262 TagToRemoveFromCochain,
263 typename DualField::non_indices_domain_t>::type;
265 TagToRemoveFromCochain::size(),
267 typename DualField::memory_space>(
typename ChainDomain::discrete_element_type {});
269 TagToRemoveFromCochain::size() - 1,
271 typename DualField::memory_space>(
typename ChainDomain::discrete_element_type {});
274 Element origin = elem;
275 for (std::size_t axis = 0; axis < TagToRemoveFromCochain::size(); ++axis) {
276 ddc::detail::array(origin)[axis] = 2;
284 ddc::DiscreteElement<TopIndex>(0))));
285 auto unit = detail::make_stencil<typename DualField::memory_space, TopIndex>(
286 ddc::DiscreteElement<>());
287 unit.mem(ddc::DiscreteElement<TopIndex>(0)) = 1.0;
288 double const scale = -inverse_hodge.inverse_zero_form(
291 auto result = detail::make_stencil<typename DualField::memory_space, DualIndex>(Element {});
292 for (std::size_t i = 0; i < result.storage().size(); ++i)
293 result.storage()[i] = scale * incidence.storage()[i];
297 template <
class DualField,
class DualRules,
class InverseHodge,
class Element>
298 requires requires {
typename DualField::non_indices_domain_t; }
300 DualField
const& dual_field,
301 DualRules
const& dual_rules,
302 InverseHodge
const& inverse_hodge,
305 auto const rules = [&] {
311 using ChainDomain =
typename detail::NonSpectatorDimension<
312 TagToRemoveFromCochain,
313 typename DualField::non_indices_domain_t>::type;
315 TagToRemoveFromCochain::size() - CochainTag::rank() + 1,
317 typename DualField::memory_space>(
typename ChainDomain::discrete_element_type {});
319 TagToRemoveFromCochain::size() - CochainTag::rank(),
321 typename DualField::memory_space>(
typename ChainDomain::discrete_element_type {});
322 auto output = detail::make_stencil<
323 typename DualField::memory_space,
325 ddc::DiscreteElement<>());
328 [&](
auto sample,
auto component) {
329 return rules(dual_field, sample).mem(component);
346 template <CellComplex,
class,
class,
class,
class,
class>
class Hodge = DiscreteHodgeStar>
347struct CodifferentialGeometry;
351 tensor::TensorIndex MetricIndex,
352 tensor::TensorNatIndex TagToRemoveFromCochain,
353 tensor::TensorIndex CochainTag,
354 CochainType TensorType,
355 misc::Specialization<tensor::Tensor> MetricType,
356 misc::Specialization<tensor::Tensor> PositionType>
359 TagToRemoveFromCochain,
367 TagToRemoveFromCochain,
373 template <
class Exec>
374 static auto geometry(Exec
const& exec, MetricType
const& metric, PositionType
const& position)
376 return detail::CodifferentialGeometry<
377 TagToRemoveFromCochain,
381 PositionType>(exec, metric, position);
384 template <
class Geometry,
class Element,
class Rules>
386 Geometry
const& geometry,
387 TensorType
const& input,
392 value(geometry, input, element, rules);
395 template <
class ChainType,
class LowerChainType,
class NaturalElem>
397 TensorType
const& tensor,
398 MetricType
const& metric,
399 PositionType
const& position,
400 ChainType
const& chain,
401 LowerChainType
const& lower_chain,
402 typename TensorType::non_indices_domain_t::discrete_element_type elem,
403 NaturalElem natural_elem)
405 auto stencil = detail::make_stencil<typename TensorType::memory_space, CochainTag>(
406 detail::backward_stencil_front(elem, tensor.non_indices_domain()));
407 ddc::device_for_each(stencil.domain(), [&](
auto stencil_elem) {
409 = detail::make_stencil<typename TensorType::memory_space, CochainTag>(
410 stencil.non_indices_domain().front());
411 basis_stencil.mem(stencil_elem) = 1.0;
413 [[maybe_unused]] tensor::TensorAccessor<
414 codifferential_index_t<TagToRemoveFromCochain, CochainTag>>
415 codifferential_accessor;
418 codifferential_index_t<TagToRemoveFromCochain, CochainTag>::mem_size()>
419 codifferential_alloc {};
423 Kokkos::layout_right,
424 typename TensorType::memory_space>
426 codifferential_alloc.data(),
427 codifferential_accessor.domain());
429 sil::tensor::Tensor(codifferential_span)};
432 codifferential_tensor,
439 stencil.mem(stencil_elem) = codifferential_tensor(natural_elem);
446 TensorType
const& tensor,
447 MetricType
const& metric,
448 PositionType
const& position,
450 auto const& lower_chain,
451 typename TensorType::non_indices_domain_t::discrete_element_type elem)
453 using source_hodge_input_indices
456 TagToRemoveFromCochain::size() - CochainTag::rank(),
457 TagToRemoveFromCochain>;
458 using target_hodge_input_indices = ddc::
459 type_seq_merge_t<ddc::TypeSeq<TagToRemoveFromCochain>, source_hodge_output_indices>;
462 source_hodge_output_indices>;
463 auto dual_evaluator = [&](
auto sampled_elem,
auto dual_elem) {
470 std::array<double, dual_tensor_index::mem_size()> dual_tensor_alloc {};
473 ddc::DiscreteDomain<dual_tensor_index>,
474 Kokkos::layout_right,
475 typename TensorType::memory_space>
476 dual_tensor_span(dual_tensor_alloc.data(), dual_tensor_accessor.
domain());
481 source_hodge_input_indices,
482 source_hodge_output_indices,
485 typename TensorType::non_indices_domain_t::discrete_element_type> {}
486 .operator()(dual_tensor, tensor[sampled_elem], metric, position, sampled_elem);
487 return dual_tensor.mem(dual_elem);
491 codifferential_tensor,
493 [&](
auto output,
auto dual,
auto sample) {
497 ddc::type_seq_remove_t<
499 ddc::TypeSeq<TagToRemoveFromCochain>>,
502 typename TensorType::non_indices_domain_t::discrete_element_type> {}
503 .operator()(output, dual, metric, position, sample);
519 template <CellComplex,
class,
class,
class,
class,
class>
class Hodge>
520struct CodifferentialGeometry
524 = codifferential_hodge_output_indices_t<Index::size() - Cochain::rank(), Index>;
525 using InverseSourceIndices = ddc::type_seq_merge_t<ddc::TypeSeq<Index>, DualIndices>;
526 using InverseTargetIndices
527 = ddc::type_seq_remove_t<tensor::lower_t<SourceIndices>, ddc::TypeSeq<Index>>;
529 using Element =
typename Tensor::non_indices_domain_t::discrete_element_type;
531 typename NonSpectatorDimension<Index, typename Tensor::non_indices_domain_t>::type;
535 Hodge<CellComplex::Primal, SourceIndices, DualIndices, Metric, Position, Element>>
538 Hodge<CellComplex::Primal,
540 InverseTargetIndices,
545 StencilChain<DualIndex::rank() + 1, ChainDomain,
typename Tensor::memory_space> chain;
546 StencilChain<DualIndex::rank(), ChainDomain,
typename Tensor::memory_space> lower;
547 template <
class Exec>
548 CodifferentialGeometry(Exec
const& exec, Metric
const& metric_, Position
const& position_)
550 , position(position_)
551 , forward(exec, metric, position)
552 , inverse(exec, metric, position)
568 tensor::TensorIndex MetricIndex,
569 tensor::TensorNatIndex TagToRemoveFromCochain,
570 tensor::TensorIndex CochainTag,
572 CochainType TensorType,
573 misc::Specialization<tensor::Tensor> MetricType,
574 misc::Specialization<tensor::Tensor> PositionType,
575 class Rules = tensor::ZeroExtrapolationRule>
579 TagToRemoveFromCochain,
584 typename Codifferential<
586 TagToRemoveFromCochain,
590 PositionType>::staged_storage_type::template rebind<ExecSpace>>
592 ExecSpace
const& exec_space,
593 TensorType
const& tensor,
594 MetricType
const& metric,
595 PositionType
const& position,
596 Rules
const& rules = {})
598 return StagedOperator<
601 TagToRemoveFromCochain,
606 typename Codifferential<
608 TagToRemoveFromCochain,
612 PositionType>::staged_storage_type::
613 template rebind<ExecSpace>>(exec_space, tensor, metric, position, rules);
617 tensor::TensorIndex MetricIndex,
618 tensor::TensorNatIndex TagToRemoveFromCochain,
619 tensor::TensorIndex CochainTag,
620 CochainType DualTensorType,
621 CochainType TensorType,
622 misc::Specialization<tensor::Tensor> HodgeStarType,
623 misc::Specialization<tensor::Tensor> DualHodgeStarType,
625 class DualExtrapolationRule = tensor::ZeroExtrapolationRule>
627 misc::NotSpecialization<DualExtrapolationRule, tensor::Tensor>
628 && !ExteriorFieldType<DualExtrapolationRule>)
630 ExecSpace
const& exec_space,
632 codifferential_tensor,
633 TensorType
const& tensor,
634 HodgeStarType
const& hodge_star,
635 DualHodgeStarType
const& dual_hodge_star,
636 DualTensorType
const& dual_tensor_buffer,
637 DualExtrapolationRule
const& dual_extrapolation = {})
639 auto const dual_extrapolation_rules = [&]() {
641 return dual_extrapolation;
646 using source_hodge_output_indices = codifferential_hodge_output_indices_t<
647 TagToRemoveFromCochain::size() - CochainTag::rank(),
648 TagToRemoveFromCochain>;
649 using dual_tensor_index = misc::
650 convert_type_seq_to_t<tensor::TensorAntisymmetricIndex, source_hodge_output_indices>;
651 using non_spectator_dimensions =
typename detail::NonSpectatorDimension<
652 TagToRemoveFromCochain,
653 typename TensorType::non_indices_domain_t>::type;
655 = tangent_basis<dual_tensor_index::rank() + 1, non_spectator_dimensions>(exec_space);
657 = tangent_basis<dual_tensor_index::rank(), non_spectator_dimensions>(exec_space);
659 SIMILIE_DEBUG_LOG(
"similie_apply_first_hodge_star_for_codifferential");
660 ddc::parallel_for_each(
661 "similie_apply_first_hodge_star_for_codifferential",
663 dual_tensor_buffer.non_indices_domain(),
664 KOKKOS_LAMBDA(
typename TensorType::non_indices_domain_t::discrete_element_type elem) {
668 SIMILIE_DEBUG_LOG(
"similie_apply_second_hodge_star_for_codifferential");
669 ddc::parallel_for_each(
670 "similie_apply_second_hodge_star_for_codifferential",
672 codifferential_tensor.non_indices_domain(),
673 KOKKOS_LAMBDA(
typename TensorType::non_indices_domain_t::discrete_element_type elem) {
674 Codifferential<TagToRemoveFromCochain, CochainTag>::operator()(
675 codifferential_tensor[elem],
676 [&](
auto sampled_elem,
auto dual_elem) {
677 return dual_extrapolation_rules(dual_tensor_buffer, sampled_elem)
680 [&](
auto output,
auto dual,
auto sample) {
688 return codifferential_tensor;
692 tensor::TensorIndex MetricIndex,
693 tensor::TensorNatIndex TagToRemoveFromCochain,
694 tensor::TensorIndex CochainTag,
695 CochainType TensorType,
696 misc::Specialization<tensor::Tensor> MetricType,
697 misc::Specialization<tensor::Tensor> PositionType,
699 class DualExtrapolationRule = tensor::ZeroExtrapolationRule>
701 misc::NotSpecialization<DualExtrapolationRule, tensor::Tensor>
702 && !ExteriorFieldType<DualExtrapolationRule>)
704 ExecSpace
const& exec_space,
706 codifferential_tensor,
707 TensorType
const& tensor,
708 MetricType
const& metric,
709 PositionType
const& position,
710 DualExtrapolationRule
const& dual_extrapolation = {})
713 return make_staged_codifferential<MetricIndex, TagToRemoveFromCochain, CochainTag>(
718 dual_extrapolation)(codifferential_tensor, tensor);
KOKKOS_FUNCTION chain_type const & chain() const noexcept
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
Simplex(ddc::DiscreteElement< Tag... >, ddc::DiscreteVector< T... >) -> Simplex< sizeof...(T), Tag... >
StagedOperator< Codifferential< MetricIndex, TagToRemoveFromCochain, CochainTag, TensorType, MetricType, PositionType >, typename Codifferential< MetricIndex, TagToRemoveFromCochain, CochainTag, TensorType, MetricType, PositionType >::staged_storage_type::template rebind< ExecSpace > > make_staged_codifferential(ExecSpace const &exec_space, TensorType const &tensor, MetricType const &metric, PositionType const &position, Rules const &rules={})
Cache Codifferential::value() at every input node.
typename detail:: CodifferentialTensorType< TagToRemoveFromCochain, CochainTag, TensorType >::type codifferential_tensor_t
Chain(Head, Tail...) -> Chain< typename Head::value_type, typename Head::array_layout, typename Head::memory_space >
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)
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={})
typename detail::CodifferentialIndex< TagToRemoveFromCochain, CochainTag >::type codifferential_index_t
KOKKOS_FUNCTION constexpr auto tangent_basis(Elem elem)
typename detail::CodifferentialHodgeOutputIndexSeq< EndId, T >::type codifferential_hodge_output_indices_t
typename detail::CodifferentialType< CochainType >::type codifferential_t
KOKKOS_FUNCTION bool domain_contains(ddc::DiscreteDomain< DDim... > const &dom, ddc::DiscreteElement< ODDim... > elem)
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::Lower< T >::type lower_t
bool constexpr is_covariant_v
The top-level namespace of SimiLie.
static KOKKOS_FUNCTION void operator()(CochainType auto codifferential_tensor, TensorType const &tensor, MetricType const &metric, PositionType const &position, auto const &chain, auto const &lower_chain, typename TensorType::non_indices_domain_t::discrete_element_type elem)
static auto geometry(Exec const &exec, MetricType const &metric, PositionType const &position)
detail::LinearSpatialOperatorStorage< Codifferential, TagToRemoveFromCochain, CochainTag, codifferential_index_t< TagToRemoveFromCochain, CochainTag >, 1, TensorType > staged_storage_type
static KOKKOS_FUNCTION auto value(TensorType const &tensor, MetricType const &metric, PositionType const &position, ChainType const &chain, LowerChainType const &lower_chain, typename TensorType::non_indices_domain_t::discrete_element_type elem, NaturalElem natural_elem)
static KOKKOS_FUNCTION auto value(Geometry const &geometry, TensorType const &input, Element element, Rules const &rules)
static KOKKOS_FUNCTION auto value(DualField const &dual_field, DualRules const &dual_rules, InverseHodge const &inverse_hodge, Element elem)
static KOKKOS_FORCEINLINE_FUNCTION auto value(Geometry const &geometry, Field const &input, Element element, Rules const &rules)
Complete cochain stencil, with geometry and closure supplied separately.
static KOKKOS_FUNCTION void operator()(Output output, DualEvaluator const &evaluate_dual, InverseHodge const &inverse_hodge, Chain const &chain, LowerChain const &lower_chain, Element elem)
static KOKKOS_FUNCTION auto value(DualField const &, InverseHodge const &inverse_hodge, Element elem)
Scalar codifferential coefficients on the incident dual faces.
Compose reconstruction, continuous Hodge star and reduction policies.