Gyselalib++
circular_to_cartesian.hpp
1 // SPDX-License-Identifier: MIT
2 #pragma once
3 
4 #include <cassert>
5 #include <cmath>
6 
7 #include <ddc/ddc.hpp>
8 
9 #include <sll/view.hpp>
10 
11 #include "coordinate_converter.hpp"
12 #include "curvilinear2d_to_cartesian.hpp"
13 #include "mapping_tools.hpp"
14 #include "pseudo_cartesian_compatible_mapping.hpp"
15 
40 template <class X, class Y, class R, class Theta>
42  : public CoordinateConverter<ddc::Coordinate<X, Y>, ddc::Coordinate<R, Theta>>
44  , public Curvilinear2DToCartesian<X, Y, R, Theta>
45 {
46 public:
56 
58  using CoordArg = ddc::Coordinate<R, Theta>;
60  using CoordResult = ddc::Coordinate<X, Y>;
61 
62 public:
63  CircularToCartesian() = default;
64 
71  KOKKOS_FUNCTION CircularToCartesian(CircularToCartesian const& other) {}
72 
80 
81  ~CircularToCartesian() = default;
82 
92 
102 
110  KOKKOS_FUNCTION ddc::Coordinate<X, Y> operator()(ddc::Coordinate<R, Theta> const& coord) const
111  {
112  const double r = ddc::get<R>(coord);
113  const double theta = ddc::get<Theta>(coord);
114  const double x = r * Kokkos::cos(theta);
115  const double y = r * Kokkos::sin(theta);
116  return ddc::Coordinate<X, Y>(x, y);
117  }
118 
126  KOKKOS_FUNCTION ddc::Coordinate<R, Theta> operator()(
127  ddc::Coordinate<X, Y> const& coord) const final
128  {
129  const double x = ddc::get<X>(coord);
130  const double y = ddc::get<Y>(coord);
131  const double r = Kokkos::sqrt(x * x + y * y);
132  const double theta = Kokkos::atan2(y, x);
133  return ddc::Coordinate<R, Theta>(r, theta);
134  }
135 
144  KOKKOS_FUNCTION double jacobian(ddc::Coordinate<R, Theta> const& coord) const
145  {
146  double r = ddc::get<R>(coord);
147  return r;
148  }
149 
150 
164  KOKKOS_FUNCTION void jacobian_matrix(ddc::Coordinate<R, Theta> const& coord, Matrix_2x2& matrix)
165  const
166  {
167  const double r = ddc::get<R>(coord);
168  const double theta = ddc::get<Theta>(coord);
169  matrix[0][0] = Kokkos::cos(theta);
170  matrix[0][1] = -r * Kokkos::sin(theta);
171  matrix[1][0] = Kokkos::sin(theta);
172  matrix[1][1] = r * Kokkos::cos(theta);
173  }
174 
186  KOKKOS_FUNCTION double jacobian_11(ddc::Coordinate<R, Theta> const& coord) const
187  {
188  const double theta = ddc::get<Theta>(coord);
189  return Kokkos::cos(theta);
190  }
191 
203  KOKKOS_FUNCTION double jacobian_12(ddc::Coordinate<R, Theta> const& coord) const
204  {
205  const double r = ddc::get<R>(coord);
206  const double theta = ddc::get<Theta>(coord);
207  return -r * Kokkos::sin(theta);
208  }
209 
221  KOKKOS_FUNCTION double jacobian_21(ddc::Coordinate<R, Theta> const& coord) const
222  {
223  const double theta = ddc::get<Theta>(coord);
224  return Kokkos::sin(theta);
225  }
226 
238  KOKKOS_FUNCTION double jacobian_22(ddc::Coordinate<R, Theta> const& coord) const
239  {
240  const double r = ddc::get<R>(coord);
241  const double theta = ddc::get<Theta>(coord);
242  return r * Kokkos::cos(theta);
243  }
244 
245 
265  KOKKOS_FUNCTION void inv_jacobian_matrix(
266  ddc::Coordinate<R, Theta> const& coord,
267  Matrix_2x2& matrix) const
268  {
269  const double r = ddc::get<R>(coord);
270  const double theta = ddc::get<Theta>(coord);
271  assert(fabs(r) >= 1e-15);
272  matrix[0][0] = Kokkos::cos(theta);
273  matrix[0][1] = Kokkos::sin(theta);
274  matrix[1][0] = -1 / r * Kokkos::sin(theta);
275  matrix[1][1] = 1 / r * Kokkos::cos(theta);
276  }
277 
288  KOKKOS_FUNCTION double inv_jacobian_11(ddc::Coordinate<R, Theta> const& coord) const
289  {
290  const double theta = ddc::get<Theta>(coord);
291  return Kokkos::cos(theta);
292  }
293 
304  KOKKOS_FUNCTION double inv_jacobian_12(ddc::Coordinate<R, Theta> const& coord) const
305  {
306  const double theta = ddc::get<Theta>(coord);
307  return Kokkos::sin(theta);
308  }
309 
320  KOKKOS_FUNCTION double inv_jacobian_21(ddc::Coordinate<R, Theta> const& coord) const
321  {
322  const double r = ddc::get<R>(coord);
323  const double theta = ddc::get<Theta>(coord);
324  assert(fabs(r) >= 1e-15);
325  return -1 / r * Kokkos::sin(theta);
326  }
327 
338  KOKKOS_FUNCTION double inv_jacobian_22(ddc::Coordinate<R, Theta> const& coord) const
339  {
340  const double r = ddc::get<R>(coord);
341  const double theta = ddc::get<Theta>(coord);
342  assert(fabs(r) >= 1e-15);
343  return 1 / r * Kokkos::cos(theta);
344  }
345 
346 
347 
369  KOKKOS_FUNCTION void to_pseudo_cartesian_jacobian_center_matrix(Matrix_2x2& matrix) const final
370  {
371  matrix[0][0] = 1.;
372  matrix[0][1] = 0.;
373  matrix[1][0] = 0.;
374  matrix[1][1] = 1.;
375  }
376 
386  KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_11_center() const final
387  {
388  return 1.;
389  }
390 
400  KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_12_center() const final
401  {
402  return 0.;
403  }
404 
414  KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_21_center() const final
415  {
416  return 0.;
417  }
418 
428  KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_22_center() const final
429  {
430  return 1.;
431  }
432 };
433 
434 
435 namespace mapping_detail {
436 template <class X, class Y, class R, class Theta, class ExecSpace>
437 struct MappingAccessibility<ExecSpace, CircularToCartesian<X, Y, R, Theta>> : std::true_type
438 {
439 };
440 } // namespace mapping_detail
A class for describing the circular 2D mapping.
Definition: circular_to_cartesian.hpp:45
KOKKOS_FUNCTION double jacobian_21(ddc::Coordinate< R, Theta > const &coord) const
Compute the (2,1) coefficient of the Jacobian matrix.
Definition: circular_to_cartesian.hpp:221
CircularToCartesian & operator=(CircularToCartesian const &x)=default
Assign a CircularToCartesian from another CircularToCartesian (lvalue).
typename Curvilinear2DToCartesian< X, Y, R, Theta >::cartesian_tag_y cartesian_tag_y
Indicate the second physical coordinate.
Definition: circular_to_cartesian.hpp:50
KOKKOS_FUNCTION double jacobian_22(ddc::Coordinate< R, Theta > const &coord) const
Compute the (2,2) coefficient of the Jacobian matrix.
Definition: circular_to_cartesian.hpp:238
KOKKOS_FUNCTION void jacobian_matrix(ddc::Coordinate< R, Theta > const &coord, Matrix_2x2 &matrix) const
Compute full Jacobian matrix.
Definition: circular_to_cartesian.hpp:164
KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_12_center() const final
Compute the (1,2) coefficient of the pseudo-Cartesian Jacobian matrix at the central point.
Definition: circular_to_cartesian.hpp:400
KOKKOS_FUNCTION double inv_jacobian_11(ddc::Coordinate< R, Theta > const &coord) const
Compute the (1,1) coefficient of the inverse Jacobian matrix.
Definition: circular_to_cartesian.hpp:288
KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_22_center() const final
Compute the (2,2) coefficient of the pseudo-Cartesian Jacobian matrix at the central point.
Definition: circular_to_cartesian.hpp:428
typename Curvilinear2DToCartesian< X, Y, R, Theta >::curvilinear_tag_r curvilinear_tag_r
Indicate the first logical coordinate.
Definition: circular_to_cartesian.hpp:52
ddc::Coordinate< R, Theta > CoordArg
The type of the argument of the function described by this mapping.
Definition: circular_to_cartesian.hpp:58
ddc::Coordinate< X, Y > CoordResult
The type of the result of the function described by this mapping.
Definition: circular_to_cartesian.hpp:60
KOKKOS_FUNCTION CircularToCartesian(CircularToCartesian const &other)
Instantiate a CircularToCartesian from another CircularToCartesian (lvalue).
Definition: circular_to_cartesian.hpp:71
KOKKOS_FUNCTION double inv_jacobian_12(ddc::Coordinate< R, Theta > const &coord) const
Compute the (1,2) coefficient of the inverse Jacobian matrix.
Definition: circular_to_cartesian.hpp:304
KOKKOS_FUNCTION void to_pseudo_cartesian_jacobian_center_matrix(Matrix_2x2 &matrix) const final
Compute the full Jacobian matrix from the mapping to the pseudo-Cartesian mapping at the central poin...
Definition: circular_to_cartesian.hpp:369
KOKKOS_FUNCTION double jacobian_11(ddc::Coordinate< R, Theta > const &coord) const
Compute the (1,1) coefficient of the Jacobian matrix.
Definition: circular_to_cartesian.hpp:186
CircularToCartesian & operator=(CircularToCartesian &&x)=default
Assign a CircularToCartesian from another temporary CircularToCartesian (rvalue).
KOKKOS_FUNCTION double jacobian_12(ddc::Coordinate< R, Theta > const &coord) const
Compute the (1,2) coefficient of the Jacobian matrix.
Definition: circular_to_cartesian.hpp:203
KOKKOS_FUNCTION double inv_jacobian_22(ddc::Coordinate< R, Theta > const &coord) const
Compute the (2,2) coefficient of the inverse Jacobian matrix.
Definition: circular_to_cartesian.hpp:338
CircularToCartesian(CircularToCartesian &&x)=default
Instantiate a Curvilinear2DToCartesian from another temporary CircularToCartesian (rvalue).
typename Curvilinear2DToCartesian< X, Y, R, Theta >::cartesian_tag_x cartesian_tag_x
Indicate the first physical coordinate.
Definition: circular_to_cartesian.hpp:48
KOKKOS_FUNCTION ddc::Coordinate< R, Theta > operator()(ddc::Coordinate< X, Y > const &coord) const final
Convert the coordinate (x,y) to the equivalent coordinate.
Definition: circular_to_cartesian.hpp:126
KOKKOS_FUNCTION void inv_jacobian_matrix(ddc::Coordinate< R, Theta > const &coord, Matrix_2x2 &matrix) const
Compute full inverse Jacobian matrix.
Definition: circular_to_cartesian.hpp:265
KOKKOS_FUNCTION ddc::Coordinate< X, Y > operator()(ddc::Coordinate< R, Theta > const &coord) const
Convert the coordinate to the equivalent (x,y) coordinate.
Definition: circular_to_cartesian.hpp:110
typename Curvilinear2DToCartesian< X, Y, R, Theta >::curvilinear_tag_theta curvilinear_tag_theta
Indicate the second logical coordinate.
Definition: circular_to_cartesian.hpp:55
KOKKOS_FUNCTION double inv_jacobian_21(ddc::Coordinate< R, Theta > const &coord) const
Compute the (2,1) coefficient of the inverse Jacobian matrix.
Definition: circular_to_cartesian.hpp:320
KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_21_center() const final
Compute the (2,1) coefficient of the pseudo-Cartesian Jacobian matrix at the central point.
Definition: circular_to_cartesian.hpp:414
KOKKOS_FUNCTION double jacobian(ddc::Coordinate< R, Theta > const &coord) const
Compute the Jacobian, the determinant of the Jacobian matrix of the mapping.
Definition: circular_to_cartesian.hpp:144
KOKKOS_FUNCTION double to_pseudo_cartesian_jacobian_11_center() const final
Compute the (1,1) coefficient of the pseudo-Cartesian Jacobian matrix at the central point.
Definition: circular_to_cartesian.hpp:386
An operator to convert from the start coordinate type to the end coordinate type.
Definition: coordinate_converter.hpp:13
A class for describing curvilinear 2D mappings from the logical domain to the physical domain.
Definition: curvilinear2d_to_cartesian.hpp:9
An operator to calculate the Jacobian matrix of the mapping from the physical domain to the pseudo-Ca...
Definition: pseudo_cartesian_compatible_mapping.hpp:42
std::array< std::array< double, 2 >, 2 > Matrix_2x2
The type of the pseudo-Cartesian Jacobian matrix and its inverse.
Definition: pseudo_cartesian_compatible_mapping.hpp:45
Define non periodic real R dimension.
Definition: geometry.hpp:31
Define periodic real Theta dimension.
Definition: geometry.hpp:42
Define non periodic real X dimension.
Definition: geometry.hpp:278
Define non periodic real Y dimension.
Definition: geometry.hpp:289