SimiLie
Loading...
Searching...
No Matches
vertex_reconstructed_derivative.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 <stdexcept>
8
9#include <similie/exterior/covariant_derivative/covariant_derivative.hpp>
10#include <similie/exterior/quadrature/quadrature.hpp>
11#include <similie/exterior/reduction_and_reconstruction/reduction_and_reconstruction.hpp>
12#include <similie/exterior/volume.hpp>
13
14namespace sil::exterior {
15
26template <std::size_t FibreRank, class... SpatialIndex>
28{
29public:
30 // NVCC requires types used by extended device lambdas to be accessible.
31 template <class Dimension>
32 struct Grid
33 {
34 using continuous_dimension_type = Dimension;
35 };
36
37private:
38 static constexpr std::size_t dimension = sizeof...(SpatialIndex);
39 static constexpr std::size_t vertex_count = std::size_t(1) << dimension;
40 static constexpr std::size_t basis_count = vertex_count * FibreRank;
41 using Element = ddc::DiscreteElement<Grid<SpatialIndex>...>;
42
43 [[nodiscard]] KOKKOS_FUNCTION static Element element(std::size_t vertex)
44 {
45 Element result;
46 for (std::size_t axis = 0; axis < dimension; ++axis)
47 ddc::detail::array(result)[axis] = (vertex >> axis) & 1;
48 return result;
49 }
50
51 struct NodalBasis
52 {
53 std::size_t input;
54
55 [[nodiscard]] KOKKOS_FUNCTION std::array<double, FibreRank> operator()(
56 std::size_t,
57 std::size_t node) const
58 {
59 std::array<double, FibreRank> value {};
60 if (node == input / FibreRank)
61 value[input % FibreRank] = 1.0;
62 return value;
63 }
64 };
65
66 template <class MemorySpace>
67 KOKKOS_FUNCTION void initialize(
68 std::array<std::array<double, dimension>, vertex_count> const& positions)
69 {
70 using PositionIndex = tensor::Contravariant<tensor::TensorNaturalIndex<SpatialIndex...>>;
71 using GradientIndex = tensor::Covariant<tensor::TensorNaturalIndex<SpatialIndex...>>;
72 using MetricIndex = tensor::TensorIdentityIndex<
73 tensor::Covariant<tensor::MetricIndex1<SpatialIndex...>>,
74 tensor::Covariant<tensor::MetricIndex2<SpatialIndex...>>>;
75 using Domain = ddc::DiscreteDomain<Grid<SpatialIndex>...>;
76 Domain const
77 nodes(Element(),
78 ddc::DiscreteVector<Grid<SpatialIndex>...>(
79 (static_cast<void>(sizeof(SpatialIndex)), 2)...));
80 std::array<double, vertex_count * dimension> position_storage {};
81 tensor::Tensor position(
82 ddc::ChunkSpan<
83 double,
84 ddc::DiscreteDomain<Grid<SpatialIndex>..., PositionIndex>,
85 Kokkos::layout_right,
86 MemorySpace>(
87 position_storage.data(),
88 ddc::DiscreteDomain<Grid<SpatialIndex>..., PositionIndex>(
89 nodes,
91 for (std::size_t vertex = 0; vertex < vertex_count; ++vertex)
92 for (std::size_t axis = 0; axis < dimension; ++axis)
93 position.mem(element(vertex), ddc::DiscreteElement<PositionIndex>(axis))
94 = positions[vertex][axis];
95 tensor::Tensor metric(
96 ddc::ChunkSpan<
97 double,
98 ddc::DiscreteDomain<MetricIndex>,
99 Kokkos::layout_right,
100 MemorySpace>(nullptr, tensor::TensorAccessor<MetricIndex>::domain()));
101 for (std::size_t vertex = 0; vertex < vertex_count; ++vertex) {
102 // Geometry and the reconstruction must use the same corner frame.
103 std::array<double, dimension * dimension> jacobian {};
104 std::array<std::size_t, dimension> directions {};
105 for (std::size_t axis = 0; axis < dimension; ++axis) {
106 directions[axis] = axis;
107 for (std::size_t physical = 0; physical < dimension; ++physical)
108 jacobian[physical * dimension + axis]
109 = ((vertex >> axis) & 1 ? -1.0 : 1.0)
110 * (positions[vertex ^ (std::size_t(1) << axis)][physical]
111 - positions[vertex][physical]);
112 }
113 if (!(detail::reduction_determinant<dimension>(jacobian) > 0.0)) {
114 KOKKOS_IF_ON_HOST((
115 throw std::runtime_error(
116 "vertex reconstruction requires an unfolded, nondegenerate cell");))
117 KOKKOS_IF_ON_DEVICE((
118 Kokkos::abort(
119 "vertex reconstruction requires an unfolded, nondegenerate cell");))
120 }
121 measures[vertex] = SimplexVolume<
123 dimension,
124 decltype(metric),
125 decltype(position),
126 Element>::template
127 operator()<dimension>(metric, position, element(vertex), directions);
128 auto const reconstruction
129 = Reconstruction<ddc::TypeSeq<GradientIndex>, decltype(position), Element>::
130 value(position, element(vertex));
131 for (std::size_t input = 0; input < basis_count; ++input) {
132 std::array<std::array<double, FibreRank>, dimension> differential {};
133 for (std::size_t axis = 0; axis < dimension; ++axis)
134 differential[axis] = CovariantDerivative<SpatialIndex...>::
135 template cochain_value<0, FibreRank>(
136 {axis},
137 vertex & ~(std::size_t(1) << axis),
138 NodalBasis {input})
139 .value();
140 for (std::size_t fibre = 0; fibre < FibreRank; ++fibre)
141 for (std::size_t physical = 0; physical < dimension; ++physical)
142 for (std::size_t axis = 0; axis < dimension; ++axis)
143 gradients[vertex][input][fibre * dimension + physical]
144 += reconstruction.weights[physical * dimension + axis]
145 * differential[axis][fibre];
146 }
147 }
148 }
149
150public:
151 static_assert(dimension > 0 && FibreRank > 0);
152 std::array<double, vertex_count> measures {};
153 std::array<std::array<std::array<double, FibreRank * dimension>, basis_count>, vertex_count>
155
156 KOKKOS_FUNCTION explicit VertexReconstructedDerivative(
157 std::array<std::array<double, dimension>, vertex_count> const& positions)
158 {
159 KOKKOS_IF_ON_HOST((initialize<Kokkos::HostSpace>(positions);))
160 KOKKOS_IF_ON_DEVICE(
161 (initialize<typename Kokkos::DefaultExecutionSpace::memory_space>(positions);))
162 }
163
164 [[nodiscard]] KOKKOS_FUNCTION std::array<double, FibreRank * dimension> gradient(
165 std::array<std::array<double, FibreRank>, vertex_count> const& nodal) const
166 {
167 auto const measure = [&](auto const&, std::size_t sample) { return measures[sample]; };
168 double const volume = quadrature(
170 [](auto const&, std::size_t) { return 1.0; },
171 measure);
172 std::array<double, FibreRank * dimension> result {};
173 for (std::size_t component = 0; component < result.size(); ++component)
174 result[component]
175 = quadrature(
177 [&](auto const&, std::size_t sample) {
178 double value = 0.0;
179 for (std::size_t input = 0; input < basis_count; ++input)
180 value += gradients[sample][input][component]
181 * nodal[input / FibreRank][input % FibreRank];
182 return value;
183 },
184 measure)
185 / volume;
186 return result;
187 }
188};
189
190} // namespace sil::exterior
Reconstruct derivatives of nodal, flat-bundle-valued zero-cochains at the vertices of one cubical cel...
KOKKOS_FUNCTION VertexReconstructedDerivative(std::array< std::array< double, dimension >, vertex_count > const &positions)
std::array< std::array< std::array< double, FibreRank *dimension >, basis_count >, vertex_count > gradients
KOKKOS_FUNCTION std::array< double, FibreRank *dimension > gradient(std::array< std::array< double, FibreRank >, vertex_count > const &nodal) const
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
KOKKOS_FUNCTION double quadrature(Rule const &rule, Evaluator const &evaluator, Measure const &measure)
Integrate a scalar evaluator with an independent reference-cell rule.
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
Tensor product trapezoidal rule on the reference cube [0,1]^Dimension.