SimiLie
Loading...
Searching...
No Matches
magnetic_form_response.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 <array>
8#include <type_traits>
9
10#include <similie/exterior/continuous_hodge_star/continuous_hodge_star.hpp>
11#include <similie/exterior/continuous_hodge_star/levi_civita_continuous_hodge_star.hpp>
12#include <similie/exterior/form.hpp>
13
15
29template <class X, class Y, class Z>
31{
32public:
33 // NVCC requires types used by extended device lambdas to be accessible.
35 {
36 };
38 {
39 };
41 {
42 };
43
44private:
45 std::array<double, 9> m_star {};
46 double m_volume = 1.0;
47
48 [[nodiscard]] KOKKOS_FUNCTION sil::exterior::Form<std::array<double, 3>> dual(
49 sil::exterior::Form<std::array<double, 3>> const& induction) const
50 {
52 for (std::size_t row = 0; row < 3; ++row)
53 for (std::size_t column = 0; column < 3; ++column)
54 result[row] += m_star[3 * row + column] * induction[column];
55 return result;
56 }
57
58 [[nodiscard]] KOKKOS_FUNCTION double norm_squared(
59 sil::exterior::Form<std::array<double, 3>> const& induction,
60 sil::exterior::Form<std::array<double, 3>> const& dual_induction) const
61 {
62 // B wedge star B = |B|_g^2 vol_g in signed complementary order.
63 double result = 0.0;
64 for (std::size_t component = 0; component < 3; ++component)
65 result += induction[component] * dual_induction[component];
66 return result / m_volume;
67 }
68
69 template <class MemorySpace>
70 [[nodiscard]] KOKKOS_FUNCTION static MagneticFormResponse euclidean_impl()
71 {
72 using MetricIndex = sil::tensor::TensorIdentityIndex<
76 ddc::ChunkSpan<
77 double,
78 ddc::DiscreteDomain<MetricIndex>,
79 Kokkos::layout_right,
81 return MagneticFormResponse(metric, ddc::DiscreteElement<>());
82 }
83
84public:
85 [[nodiscard]] KOKKOS_FUNCTION static MagneticFormResponse euclidean()
86 {
87 KOKKOS_IF_ON_HOST((return euclidean_impl<Kokkos::HostSpace>();))
88 KOKKOS_IF_ON_DEVICE(
89 (return euclidean_impl<typename Kokkos::DefaultExecutionSpace::memory_space>();))
90 }
91
92 [[nodiscard]] KOKKOS_FUNCTION sil::exterior::Form<std::array<double, 3>> field(
93 sil::exterior::Form<std::array<double, 3>> const& induction,
94 double reluctivity) const
95 {
96 sil::exterior::Form<std::array<double, 3>> result = dual(induction);
97 for (double& component : result)
98 component *= reluctivity;
99 return result;
100 }
101
102 template <class Law, class Element>
104 Law const& law,
105 typename Law::State const& state,
106 Element const& element) const
107 {
108 static_assert(Law::N == 3);
109 sil::exterior::Form<std::array<double, 3>> const dual_induction = dual(state.value);
110 double const norm = Kokkos::sqrt(norm_squared(state.value, dual_induction));
111 typename Law::State radial = state;
112 radial.value = {norm, 0.0, 0.0};
113 double const reluctivity
114 = norm == 0.0 ? law.jacobian(radial, element)[0] : law(radial, element)[0] / norm;
115 return field(state.value, reluctivity);
116 }
117
118 template <class Metric, class Element>
119 KOKKOS_FUNCTION MagneticFormResponse(Metric const& metric, Element const& element)
120 {
121 using Source = ddc::
122 TypeSeq<sil::tensor::Contravariant<First>, sil::tensor::Contravariant<Second>>;
123 using Target = ddc::TypeSeq<sil::tensor::Covariant<Third>>;
124 using Top = ddc::TypeSeq<
128 using MetricIndex
129 = ddc::type_seq_element_t<0, ddc::to_type_seq_t<typename Metric::indices_domain_t>>;
130 using Star = std::conditional_t<
134 using Volume = std::conditional_t<
138 auto const star = Star::value(metric, element);
141 std::array<std::size_t, 3> const columns {
142 SourceIndex::access_id_to_mem_id(accessor.template access_element<Y, Z>().uid()),
143 SourceIndex::access_id_to_mem_id(accessor.template access_element<X, Z>().uid()),
144 SourceIndex::access_id_to_mem_id(accessor.template access_element<X, Y>().uid())};
145 for (std::size_t row = 0; row < 3; ++row)
146 for (std::size_t column = 0; column < 3; ++column)
147 m_star[3 * row + column]
148 = (column == 1 ? -1.0 : 1.0) * star.weights[3 * row + columns[column]];
149 m_volume = Volume::
150 value(metric,
151 element,
152 ddc::DiscreteElement<
156 KOKKOS_ASSERT(m_volume > 0.0);
157 }
158
159 template <class Equations, class Element>
160 [[nodiscard]] KOKKOS_FUNCTION sil::exterior::Form<std::array<double, 3>> field(
161 Equations const& equations,
162 typename Equations::State const& state,
163 Element const& element) const
164 {
165 sil::exterior::Form<std::array<double, 3>> result = dual(state.moments);
166 double const norm = Kokkos::sqrt(norm_squared(state.moments, result));
167 typename Equations::State radial = state;
168 radial.moments = {norm, 0.0, 0.0};
169 double const reluctivity
170 = norm == 0.0 ? equations.template jacobian<
171 std::integral_constant<std::size_t, 0>,
172 std::integral_constant<std::size_t, 0>>(radial, element)
173 : equations.template dpotential_dt<
174 std::integral_constant<std::size_t, 0>>(radial, element)
175 / norm;
176 for (double& component : result)
177 component *= reluctivity;
178 return result;
179 }
180
181 template <class Equations, class Element>
182 [[nodiscard]] KOKKOS_FUNCTION std::array<double, 9> jacobian(
183 Equations const& equations,
184 typename Equations::State const& state,
185 Element const& element) const
186 {
187 sil::exterior::Form<std::array<double, 3>> const dual_induction = dual(state.moments);
188 double const q = norm_squared(state.moments, dual_induction);
189 double const norm = Kokkos::sqrt(q);
190 typename Equations::State radial = state;
191 radial.moments = {norm, 0.0, 0.0};
192 double const derivative = equations.template jacobian<
193 std::integral_constant<std::size_t, 0>,
194 std::integral_constant<std::size_t, 0>>(radial, element);
195 double const reluctivity
196 = norm == 0.0 ? derivative
197 : equations.template dpotential_dt<
198 std::integral_constant<std::size_t, 0>>(radial, element)
199 / norm;
200 double const radial_derivative = q == 0.0 ? 0.0 : (derivative - reluctivity) / q;
201 std::array<double, 9> result {};
202 for (std::size_t row = 0; row < 3; ++row)
203 for (std::size_t column = 0; column < 3; ++column)
204 result[3 * row + column] = reluctivity * m_star[3 * row + column]
205 + radial_derivative / m_volume * dual_induction[row]
206 * dual_induction[column];
207 return result;
208 }
209};
210
211} // namespace similie::physics::magnetostatics
Differential-form coefficients, pointwise or sampled over a spatial domain.
Definition form.hpp:32
Continuous Hodge star for an identity metric and the positive coordinate orientation.
Isotropic magnetic response H = nu(|B|_g) star_g B in three dimensions.
KOKKOS_FUNCTION std::array< double, 9 > jacobian(Equations const &equations, typename Equations::State const &state, Element const &element) const
KOKKOS_FUNCTION sil::exterior::Form< std::array< double, 3 > > field(sil::exterior::Form< std::array< double, 3 > > const &induction, double reluctivity) const
static KOKKOS_FUNCTION MagneticFormResponse euclidean()
KOKKOS_FUNCTION sil::exterior::Form< std::array< double, 3 > > field(Equations const &equations, typename Equations::State const &state, Element const &element) const
KOKKOS_FUNCTION sil::exterior::Form< std::array< double, 3 > > constitutive_field(Law const &law, typename Law::State const &state, Element const &element) const
KOKKOS_FUNCTION MagneticFormResponse(Metric const &metric, Element const &element)
detail::Lower< T >::type lower_t
Definition character.hpp:79