Gyselalib++
 
Loading...
Searching...
No Matches
combined_mapping.hpp
1// SPDX-License-Identifier: MIT
2#pragma once
3
4#include "sll/math_tools.hpp"
5
6#include "inv_jacobian_o_point.hpp"
7#include "inverse_jacobian_matrix.hpp"
8#include "mapping_tools.hpp"
9
24template <class Mapping1, class Mapping2>
26{
27 static_assert(is_mapping_v<Mapping1>);
28 static_assert(is_mapping_v<Mapping2>);
29 static_assert(std::is_same_v<typename Mapping2::CoordResult, typename Mapping1::CoordArg>);
30 static_assert(
31 is_analytical_mapping_v<Mapping2>,
32 "The second mapping must be analytical to evaluate the jacobian");
33
34public:
36 using CoordArg = typename Mapping2::CoordArg;
38 using CoordResult = typename Mapping1::CoordResult;
40 using CoordJacobian = typename Mapping2::CoordResult;
41
42private:
43 using InverseMapping2 = inverse_mapping_t<Mapping2>;
44
45 static_assert(has_2d_jacobian_v<Mapping1, CoordJacobian>);
46 static_assert(has_2d_jacobian_v<InverseMapping2, CoordJacobian>);
47
48private:
49 Mapping1 m_mapping_1;
50 Mapping2 m_mapping_2;
51 InverseMapping2 m_inv_mapping_2;
52 // The Jacobian defined on CoordJacobian is the inverse of the inverse mapping
54 double m_epsilon;
55
56public:
68 template <
69 class Map1,
70 std::enable_if_t<
71 (has_singular_o_point_inv_jacobian_v<Map1>)
72 || (has_singular_o_point_inv_jacobian_v<InverseMapping2>),
73 bool> = true>
74 CombinedMapping(Map1 mapping_1, Mapping2 mapping_2, double epsilon)
75 : m_mapping_1(mapping_1)
76 , m_mapping_2(mapping_2)
77 , m_inv_mapping_2(mapping_2.get_inverse_mapping())
78 , m_jacobian_mapping_2(mapping_2.get_inverse_mapping())
79 , m_epsilon(epsilon)
80 {
81 static_assert(std::is_same_v<Mapping1, Map1>);
82 }
83
91 template <
92 class Map1,
93 std::enable_if_t<
94 !((has_singular_o_point_inv_jacobian_v<Map1>)
95 || (has_singular_o_point_inv_jacobian_v<InverseMapping2>)),
96 bool> = true>
97 CombinedMapping(Map1 mapping_1, Mapping2 mapping_2)
98 : m_mapping_1(mapping_1)
99 , m_mapping_2(mapping_2)
100 , m_inv_mapping_2(mapping_2.get_inverse_mapping())
101 , m_jacobian_mapping_2(mapping_2.get_inverse_mapping())
102 , m_epsilon(0.0)
103 {
104 static_assert(std::is_same_v<Mapping1, Map1>);
105 }
106
115 {
116 return m_mapping_1(m_mapping_2(coord));
117 }
118
136 KOKKOS_INLINE_FUNCTION void jacobian_matrix(CoordJacobian const& coord, Matrix_2x2& matrix)
137 const
138 {
139 Matrix_2x2 jacobian_mapping_1;
140 m_mapping_1.jacobian_matrix(coord, jacobian_mapping_1);
141
142 Matrix_2x2 m_jacobian_mapping_2 = m_jacobian_mapping_2(coord);
143
144 matrix = mat_mul(jacobian_mapping_1, m_jacobian_mapping_2);
145 }
146
153 KOKKOS_INLINE_FUNCTION double jacobian_11(CoordJacobian const& coord_rtheta) const
154 {
155 Matrix_2x2 J;
156 jacobian_matrix(coord_rtheta, J);
157 return J[0][0];
158 }
159
166 KOKKOS_INLINE_FUNCTION double jacobian_12(CoordJacobian const& coord_rtheta) const
167 {
168 Matrix_2x2 J;
169 jacobian_matrix(coord_rtheta, J);
170 return J[0][1];
171 }
172
179 KOKKOS_INLINE_FUNCTION double jacobian_21(CoordJacobian const& coord_rtheta) const
180 {
181 Matrix_2x2 J;
182 jacobian_matrix(coord_rtheta, J);
183 return J[1][0];
184 }
185
192 KOKKOS_INLINE_FUNCTION double jacobian_22(CoordJacobian const& coord_rtheta) const
193 {
194 Matrix_2x2 J;
195 jacobian_matrix(coord_rtheta, J);
196 return J[1][1];
197 }
198
205 KOKKOS_INLINE_FUNCTION double jacobian(CoordJacobian const& coord_rtheta) const
206 {
207 Matrix_2x2 J;
208 jacobian_matrix(coord_rtheta, J);
209 return determinant(J);
210 }
211
227 KOKKOS_FUNCTION void inv_jacobian_matrix(CoordJacobian const& coord, Matrix_2x2& matrix) const
228 {
229 if constexpr (
230 (has_singular_o_point_inv_jacobian_v<Mapping1>)
231 || (has_singular_o_point_inv_jacobian_v<InverseMapping2>)) {
232 using R = ddc::type_seq_element_t<0, ddc::to_type_seq_t<CoordJacobian>>;
233 using Theta = ddc::type_seq_element_t<1, ddc::to_type_seq_t<CoordJacobian>>;
234 double r = ddc::get<R>(coord);
235 if (r < m_epsilon) {
237 *this);
238 CoordJacobian coord_eps(m_epsilon, ddc::get<Theta>(coord));
239 Matrix_2x2 J_0 = o_point_val();
240 Matrix_2x2 J_eps;
241 non_singular_inverse_jacobian_matrix(coord_eps, J_eps);
242 matrix[0][0] = (1 - r / m_epsilon) * J_0[0][0] + r / m_epsilon * J_eps[0][0];
243 matrix[0][1] = (1 - r / m_epsilon) * J_0[0][1] + r / m_epsilon * J_eps[0][1];
244 matrix[1][0] = (1 - r / m_epsilon) * J_0[1][0] + r / m_epsilon * J_eps[1][0];
245 matrix[1][1] = (1 - r / m_epsilon) * J_0[1][1] + r / m_epsilon * J_eps[1][1];
246 } else {
247 non_singular_inverse_jacobian_matrix(coord, matrix);
248 }
249 } else {
250 non_singular_inverse_jacobian_matrix(coord, matrix);
251 }
252 }
253
260 KOKKOS_INLINE_FUNCTION double inv_jacobian_11(CoordJacobian const& coord_rtheta) const
261 {
262 Matrix_2x2 J;
263 inv_jacobian_matrix(coord_rtheta, J);
264 return J[0][0];
265 }
266
273 KOKKOS_INLINE_FUNCTION double inv_jacobian_12(CoordJacobian const& coord_rtheta) const
274 {
275 Matrix_2x2 J;
276 inv_jacobian_matrix(coord_rtheta, J);
277 return J[0][1];
278 }
279
286 KOKKOS_INLINE_FUNCTION double inv_jacobian_21(CoordJacobian const& coord_rtheta) const
287 {
288 Matrix_2x2 J;
289 inv_jacobian_matrix(coord_rtheta, J);
290 return J[1][0];
291 }
292
299 KOKKOS_INLINE_FUNCTION double inv_jacobian_22(CoordJacobian const& coord_rtheta) const
300 {
301 Matrix_2x2 J;
302 inv_jacobian_matrix(coord_rtheta, J);
303 return J[1][1];
304 }
305
312 KOKKOS_INLINE_FUNCTION double inv_jacobian(CoordJacobian const& coord_rtheta) const
313 {
314 Matrix_2x2 J;
315 inv_jacobian_matrix(coord_rtheta, J);
316 return determinant(J);
317 }
318
324 template <class Mapping>
325 KOKKOS_INLINE_FUNCTION Mapping const& get() const
326 {
327 static_assert(std::is_same_v<Mapping, Mapping1> || std::is_same_v<Mapping, Mapping2>);
328 if constexpr (std::is_same_v<Mapping, Mapping1>) {
329 return m_mapping_1;
330 } else {
331 return m_mapping_2;
332 }
333 }
334
335private:
352 KOKKOS_INLINE_FUNCTION void non_singular_inverse_jacobian_matrix(
353 CoordJacobian const& coord,
354 Matrix_2x2& matrix) const
355 {
356 InverseJacobianMatrix<Mapping1, CoordJacobian> inv_jacobian_matrix_1(m_mapping_1);
357 Matrix_2x2 inv_jacobian_mapping_1 = inv_jacobian_matrix_1(coord);
358
359 Matrix_2x2 inv_jacobian_mapping_2;
360 m_inv_mapping_2.jacobian_matrix(coord, inv_jacobian_mapping_2);
361
362 matrix = mat_mul(inv_jacobian_mapping_2, inv_jacobian_mapping_1);
363 }
364};
365
366
367namespace mapping_detail {
368template <class Mapping1, class Mapping2, class ExecSpace>
369struct MappingAccessibility<ExecSpace, CombinedMapping<Mapping1, Mapping2>>
370{
371 static constexpr bool value = MappingAccessibility<ExecSpace, Mapping1>::value
372 && MappingAccessibility<ExecSpace, Mapping2>::value;
373};
374} // namespace mapping_detail
A class which describes a mapping which is constructed by combining two mappings.
Definition combined_mapping.hpp:26
KOKKOS_INLINE_FUNCTION double inv_jacobian_12(CoordJacobian const &coord_rtheta) const
Compute the (1,2) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:273
KOKKOS_INLINE_FUNCTION double jacobian_11(CoordJacobian const &coord_rtheta) const
Compute the (1,1) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:153
KOKKOS_INLINE_FUNCTION double inv_jacobian_11(CoordJacobian const &coord_rtheta) const
Compute the (1,1) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:260
CombinedMapping(Map1 mapping_1, Mapping2 mapping_2, double epsilon)
Build a CombinedMapping from the component mappings.
Definition combined_mapping.hpp:74
KOKKOS_INLINE_FUNCTION double jacobian_22(CoordJacobian const &coord_rtheta) const
Compute the (2,2) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:192
KOKKOS_FUNCTION void inv_jacobian_matrix(CoordJacobian const &coord, Matrix_2x2 &matrix) const
Compute the full inverse Jacobian matrix.
Definition combined_mapping.hpp:227
typename Mapping1::CoordResult CoordResult
The type of the result of the function described by this mapping.
Definition combined_mapping.hpp:38
KOKKOS_INLINE_FUNCTION void jacobian_matrix(CoordJacobian const &coord, Matrix_2x2 &matrix) const
Compute full Jacobian matrix.
Definition combined_mapping.hpp:136
KOKKOS_INLINE_FUNCTION double jacobian_12(CoordJacobian const &coord_rtheta) const
Compute the (1,2) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:166
CombinedMapping(Map1 mapping_1, Mapping2 mapping_2)
Build a CombinedMapping from the component mappings.
Definition combined_mapping.hpp:97
typename Mapping2::CoordArg CoordArg
The type of the argument of the function described by this mapping.
Definition combined_mapping.hpp:36
KOKKOS_INLINE_FUNCTION double jacobian_21(CoordJacobian const &coord_rtheta) const
Compute the (2,1) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:179
KOKKOS_INLINE_FUNCTION double inv_jacobian(CoordJacobian const &coord_rtheta) const
Compute the determinant of the Jacobian matrix.
Definition combined_mapping.hpp:312
KOKKOS_INLINE_FUNCTION Mapping const & get() const
Get access to one of the internal mappings.
Definition combined_mapping.hpp:325
typename Mapping2::CoordResult CoordJacobian
The coordinate system on which the Jacobian is described.
Definition combined_mapping.hpp:40
KOKKOS_INLINE_FUNCTION double inv_jacobian_22(CoordJacobian const &coord_rtheta) const
Compute the (2,2) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:299
KOKKOS_INLINE_FUNCTION double inv_jacobian_21(CoordJacobian const &coord_rtheta) const
Compute the (2,1) coefficient of the Jacobian matrix.
Definition combined_mapping.hpp:286
CoordResult operator()(CoordArg coord)
Convert the argument coordinate to the equivalent result coordinate.
Definition combined_mapping.hpp:114
KOKKOS_INLINE_FUNCTION double jacobian(CoordJacobian const &coord_rtheta) const
Compute the determinant of the Jacobian matrix.
Definition combined_mapping.hpp:205
An operator for calculating the inverse of the Jacobian at an O-point.
Definition inv_jacobian_o_point.hpp:27
A class to calculate the inverse of the Jacobian matrix.
Definition inverse_jacobian_matrix.hpp:18
Define non periodic real R dimension.
Definition geometry.hpp:31
Define periodic real Theta dimension.
Definition geometry.hpp:42