SimiLie
Loading...
Searching...
No Matches
integration.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// AI-GENERATED
4
5#pragma once
6
7#include <similie/exterior/chain.hpp>
8#include <similie/exterior/cochain.hpp>
9#include <similie/exterior/cochain_indices.hpp>
10#include <similie/exterior/form.hpp>
11#include <similie/exterior/local_chain.hpp>
12#include <similie/exterior/reduction_and_reconstruction/reduction_and_reconstruction.hpp>
13
14namespace sil::exterior {
15
16namespace detail {
17
18template <tensor::TensorIndex Index, class SimplexType>
19KOKKOS_FUNCTION auto integration_element(SimplexType const& simplex)
20{
21 static_assert(Index::rank() == SimplexType::dimension());
22 std::array<std::size_t, Index::rank()> directions {};
23 std::size_t count = 0;
24 auto const vector = ddc::detail::array(simplex.discrete_vector());
25 for (std::size_t axis = 0; axis < vector.size(); ++axis) {
26 if (vector[axis] != 0) {
27 assert(count < directions.size());
28 directions[count++] = axis;
29 }
30 }
31 assert(count == directions.size());
32 if constexpr (Index::rank() == 0) {
33 return typename tensor::natural_domain_t<Index>::discrete_element_type(0);
34 } else {
35 return natural_elem_from_flat_ids<
36 typename tensor::natural_domain_t<Index>::discrete_element_type>(directions);
37 }
38}
39
40} // namespace detail
41
50template <tensor::TensorIndex Index, CochainType CochainTensor, class SimplexType>
51KOKKOS_FUNCTION double integrate(CochainTensor const& cochain, SimplexType const& simplex)
52{
53 return (simplex.negative() ? -1.0 : 1.0)
54 * cochain.get(cochain.access_element(
55 simplex.discrete_element(),
56 detail::integration_element<Index>(simplex)));
57}
58
66template <tensor::TensorIndex Index, class ExecSpace, CochainType CochainTensor, class ChainType>
67double integrate(ExecSpace const& exec, CochainTensor const& cochain, ChainType const& chain)
68{
69 static_assert(
70 Kokkos::SpaceAccessibility<ExecSpace, typename ChainType::memory_space>::accessible);
71 static_assert(Kokkos::SpaceAccessibility<ExecSpace, typename CochainTensor::memory_space>::
72 accessible);
73 double result = 0.0;
74 Kokkos::parallel_reduce(
75 "integrate_cochain",
76 Kokkos::RangePolicy<ExecSpace, Kokkos::IndexType<std::size_t>>(exec, 0, chain.size()),
77 KOKKOS_LAMBDA(std::size_t i, double& sum) {
78 sum += integrate<Index>(cochain, chain[i]);
79 },
80 result);
81 return result;
82}
83
91template <
93 FormType Form,
94 class Position,
95 class SimplexType>
96KOKKOS_FUNCTION double integrate(
97 Form const& form,
98 Position const& position,
99 SimplexType const& simplex)
100{
101 assert(misc::domain_contains(form.non_indices_domain(), simplex.discrete_element()));
102 assert(misc::domain_contains(position.non_indices_domain(), simplex.discrete_element()));
104 position.non_indices_domain(),
105 simplex.discrete_element() + simplex.discrete_vector()));
107 using NaturalElement = typename tensor::tensor_accessor_for_domain_t<
108 reduction_domain_t<Indices>>::natural_domain_t::discrete_element_type;
109 if constexpr (ddc::type_seq_size_v<Indices> == 0) {
110 return (simplex.negative() ? -1.0 : 1.0)
111 * form.get(form.access_element(
112 simplex.discrete_element(),
113 detail::integration_element<typename Operator::target_index_type>(simplex)));
114 } else {
115 // Reduce only this cell's tangent component. Other forward directions may
116 // lie outside the domain, for example on an upper boundary face.
117 auto const target = detail::flat_natural_elem_ids(
118 detail::integration_element<typename Operator::target_index_type>(simplex));
120 double result = 0.0;
121 ddc::device_for_each(source.natural_domain(), [&](auto source_element) {
122 auto const coefficient = detail::merge_reduction_natural_elems<
123 NaturalElement>(detail::flat_natural_elem_ids(source_element), target);
124 using FormElement = typename Form::accessor_t::natural_domain_t::discrete_element_type;
125 FormElement const form_element = detail::natural_elem_from_flat_ids<FormElement>(
126 detail::flat_natural_elem_ids(source_element));
127 result += Operator::value(position, simplex.discrete_element(), coefficient)
128 * form.get(form.access_element(simplex.discrete_element(), form_element));
129 });
130 return (simplex.negative() ? -1.0 : 1.0) * result;
131 }
132}
133
137template <
139 class ExecSpace,
140 FormType Form,
141 class Position,
142 class ChainType>
144 ExecSpace const& exec,
145 Form const& form,
146 Position const& position,
147 ChainType const& chain)
148{
149 static_assert(
150 Kokkos::SpaceAccessibility<ExecSpace, typename ChainType::memory_space>::accessible);
151 static_assert(Kokkos::SpaceAccessibility<ExecSpace, typename Form::memory_space>::accessible);
152 static_assert(
153 Kokkos::SpaceAccessibility<ExecSpace, typename Position::memory_space>::accessible);
154 double result = 0.0;
155 Kokkos::parallel_reduce(
156 "integrate_form",
157 Kokkos::RangePolicy<ExecSpace, Kokkos::IndexType<std::size_t>>(exec, 0, chain.size()),
158 KOKKOS_LAMBDA(std::size_t i, double& sum) {
159 sum += integrate<Indices>(form, position, chain[i]);
160 },
161 result);
162 return result;
163}
164
165} // namespace sil::exterior
Differential-form coefficients, pointwise or sampled over a spatial domain.
Definition form.hpp:32
static KOKKOS_FUNCTION constexpr natural_domain_t natural_domain()
ddc::detail::convert_type_seq_to_discrete_domain_t< ddc::type_seq_merge_t< ddc::TypeSeq< misc::convert_type_seq_to_t< tensor::TensorFullIndex, tensor::primes< tensor::upper_t< Indices > > > >, ddc::TypeSeq< reduction_index_t< Indices > > > > reduction_domain_t
KOKKOS_FUNCTION double integrate(CochainTensor const &cochain, SimplexType const &simplex)
Evaluate an already reduced k-cochain on one oriented k-cell.
Form(Coefficients const &) -> Form< Coefficients >
KOKKOS_FUNCTION bool domain_contains(ddc::DiscreteDomain< DDim... > const &dom, ddc::DiscreteElement< ODDim... > elem)
detail::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t