Loading...
Searching...
No Matches
grid_types.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "../kokkos/kokkos_wrapper.hpp"
5#include "../types.hpp"
6#include "dense/mat.hpp"
7
8#include <string>
9
10namespace terra::grid {
11
12using Layout = Kokkos::LayoutRight;
13
14template < typename ScalarType >
15using Grid0DDataScalar = Kokkos::View< ScalarType, Layout >;
16
17template < typename ScalarType >
18using Grid1DDataScalar = Kokkos::View< ScalarType*, Layout >;
19
20template < typename ScalarType >
21using Grid2DDataScalar = Kokkos::View< ScalarType**, Layout >;
22
23template < typename ScalarType >
24using Grid3DDataScalar = Kokkos::View< ScalarType***, Layout >;
25
26template < typename ScalarType >
27using Grid4DDataScalar = Kokkos::View< ScalarType****, Layout >;
28
29template < typename ScalarType >
30using Grid5DDataScalar = Kokkos::View< ScalarType*****, Layout >;
31
32template < typename ScalarType, int VecDim >
33using Grid0DDataVec = Kokkos::View< ScalarType[VecDim], Layout >;
34
35template < typename ScalarType, int VecDim >
36using Grid1DDataVec = Kokkos::View< ScalarType* [VecDim], Layout >;
37
38template < typename ScalarType, int VecDim >
39using Grid2DDataVec = Kokkos::View< ScalarType** [VecDim], Layout >;
40
41template < typename ScalarType, int VecDim >
42using Grid3DDataVec = Kokkos::View< ScalarType*** [VecDim], Layout >;
43
44/// @brief SoA (Structure-of-Arrays) 4D vector grid data.
45///
46/// Stores VecDim separate Grid4DDataScalar views for GPU memory coalescing.
47/// Provides the same `operator()(sd, x, y, r, d)` interface as the former
48/// AoS Kokkos::View<ScalarType****[VecDim]>.
49template < typename ScalarType, int VecDim >
51{
52 using value_type = ScalarType;
54 static constexpr int vec_dim = VecDim;
55 static constexpr int rank = 5;
56
58
59 Grid4DDataVec() = default;
60
61 Grid4DDataVec( const std::string& label, int s0, int s1, int s2, int s3 )
62 {
63 for ( int d = 0; d < VecDim; ++d )
65 label + "_d" + std::to_string( d ), s0, s1, s2, s3 );
66 }
67
68 KOKKOS_INLINE_FUNCTION
69 ScalarType& operator()( int i0, int i1, int i2, int i3, int d ) const
70 {
71 return comp_[d]( i0, i1, i2, i3 );
72 }
73
74 KOKKOS_INLINE_FUNCTION
75 auto extent( int i ) const
76 {
77 if ( i < 4 )
78 return comp_[0].extent( i );
79 return static_cast< decltype( comp_[0].extent( 0 ) ) >( VecDim );
80 }
81
82 /// @brief Get the label (derived from first component by stripping "_d0" suffix).
83 std::string label() const
84 {
85 std::string l = comp_[0].label();
86 auto pos = l.rfind( "_d0" );
87 return ( pos != std::string::npos ) ? l.substr( 0, pos ) : l;
88 }
89
90 /// @brief Host mirror type for I/O.
92 {
93 using value_type = ScalarType;
95
96 ScalarType& operator()( int i0, int i1, int i2, int i3, int d )
97 {
98 return comp_[d]( i0, i1, i2, i3 );
99 }
100
101 const ScalarType& operator()( int i0, int i1, int i2, int i3, int d ) const
102 {
103 return comp_[d]( i0, i1, i2, i3 );
104 }
105
106 auto extent( int i ) const
107 {
108 if ( i < 4 )
109 return comp_[0].extent( i );
110 return static_cast< decltype( comp_[0].extent( 0 ) ) >( VecDim );
111 }
112 };
113};
114
115/// @brief Create a host mirror for Grid4DDataVec.
116template < typename ScalarType, int VecDim >
117typename Grid4DDataVec< ScalarType, VecDim >::HostMirror
118 create_mirror( Kokkos::HostSpace space, const Grid4DDataVec< ScalarType, VecDim >& src )
119{
121 for ( int d = 0; d < VecDim; ++d )
122 result.comp_[d] = Kokkos::create_mirror( space, src.comp_[d] );
123 return result;
124}
125
126/// @brief Create a host mirror for Grid4DDataScalar (delegates to Kokkos).
127template < typename ScalarType >
128typename Grid4DDataScalar< ScalarType >::HostMirror
129 create_mirror( Kokkos::HostSpace space, const Grid4DDataScalar< ScalarType >& src )
130{
131 return Kokkos::create_mirror( space, src );
132}
133
134/// @brief Deep copy from device Grid4DDataVec to host mirror.
135template < typename ScalarType, int VecDim >
139{
140 for ( int d = 0; d < VecDim; ++d )
141 Kokkos::deep_copy( dst.comp_[d], src.comp_[d] );
142}
143
144/// @brief Deep copy from host mirror to device Grid4DDataVec.
145template < typename ScalarType, int VecDim >
149{
150 for ( int d = 0; d < VecDim; ++d )
151 Kokkos::deep_copy( dst.comp_[d], src.comp_[d] );
152}
153
154/// @brief Deep copy for Grid4DDataScalar host mirror to device (delegates to Kokkos).
155template < typename ScalarType >
159{
160 Kokkos::deep_copy( dst, src );
161}
162
163/// @brief Deep copy for Grid4DDataScalar device to host mirror (delegates to Kokkos).
164template < typename ScalarType >
168{
169 Kokkos::deep_copy( dst, src );
170}
171
172template < typename ScalarType, int Rows, int Cols, int NumMatrices >
173using Grid4DDataMatrices = Kokkos::View< dense::Mat< ScalarType, Rows, Cols >****[NumMatrices], Layout >;
174
175template < typename GridDataType >
176constexpr int grid_data_vec_dim()
177{
178 if constexpr (
179 std::is_same_v< GridDataType, Grid0DDataScalar< typename GridDataType::value_type > > ||
180 std::is_same_v< GridDataType, Grid1DDataScalar< typename GridDataType::value_type > > ||
181 std::is_same_v< GridDataType, Grid2DDataScalar< typename GridDataType::value_type > > ||
182 std::is_same_v< GridDataType, Grid3DDataScalar< typename GridDataType::value_type > > ||
183 std::is_same_v< GridDataType, Grid4DDataScalar< typename GridDataType::value_type > > )
184 {
185 return 1;
186 }
187
188 else if constexpr (
189 std::is_same_v< GridDataType, Grid0DDataVec< typename GridDataType::value_type, 1 > > ||
190 std::is_same_v< GridDataType, Grid1DDataVec< typename GridDataType::value_type, 1 > > ||
191 std::is_same_v< GridDataType, Grid2DDataVec< typename GridDataType::value_type, 1 > > ||
192 std::is_same_v< GridDataType, Grid3DDataVec< typename GridDataType::value_type, 1 > > ||
193 std::is_same_v< GridDataType, Grid4DDataVec< typename GridDataType::value_type, 1 > > )
194 {
195 return 1;
196 }
197
198 else if constexpr (
199 std::is_same_v< GridDataType, Grid0DDataVec< typename GridDataType::value_type, 2 > > ||
200 std::is_same_v< GridDataType, Grid1DDataVec< typename GridDataType::value_type, 2 > > ||
201 std::is_same_v< GridDataType, Grid2DDataVec< typename GridDataType::value_type, 2 > > ||
202 std::is_same_v< GridDataType, Grid3DDataVec< typename GridDataType::value_type, 2 > > ||
203 std::is_same_v< GridDataType, Grid4DDataVec< typename GridDataType::value_type, 2 > > )
204 {
205 return 2;
206 }
207
208 else if constexpr (
209 std::is_same_v< GridDataType, Grid0DDataVec< typename GridDataType::value_type, 3 > > ||
210 std::is_same_v< GridDataType, Grid1DDataVec< typename GridDataType::value_type, 3 > > ||
211 std::is_same_v< GridDataType, Grid2DDataVec< typename GridDataType::value_type, 3 > > ||
212 std::is_same_v< GridDataType, Grid3DDataVec< typename GridDataType::value_type, 3 > > ||
213 std::is_same_v< GridDataType, Grid4DDataVec< typename GridDataType::value_type, 3 > > )
214 {
215 return 3;
216 }
217
218 return -1;
219}
220
221/// @brief Enum for encoding the boundary type tuples (in \ref BoundaryVertex, \ref BoundaryEdge, \ref BoundaryFace).
222enum class BoundaryPosition : int
223{
224 /// start (`== 0`)
225 P0 = 0,
226
227 /// end (`== size - 1`)
228 P1 = 1,
229
230 /// variable
231 PV = 2
232};
233
235{
236 return ( static_cast< int >( x ) << 4 ) | ( static_cast< int >( y ) << 2 ) | ( static_cast< int >( r ) << 0 );
237}
238
239/// @brief Enum for identification of the 8 boundary vertices of a subdomain.
240///
241/// @code
242/// V_011
243/// => x = 0,
244/// y = size - 1,
245/// r = size - 1
246/// @endcode
259
260/// @brief Enum for identification of the 12 boundary edges of a subdomain.
261///
262/// @code
263/// E_1Y0
264/// => x = size - 1,
265/// y = variable,
266/// r = 0
267/// @endcode
268enum class BoundaryEdge : int
269{
270 // edge along x, y=0, r=0, (:,BoundaryPosition::START,BoundaryPosition::START) in slice notation
275
276 // (0, :,BoundaryPosition::START) in slice notation
281
286};
287
288/// @brief Enum for identification of the 6 boundary faces of a subdomain.
289///
290/// @code
291/// F_X1R
292/// => x = variable,
293/// y = size - 1,
294/// r = variable
295/// @endcode
308
309/// @brief Enum for the iteration direction at a boundary.
310enum class BoundaryDirection : int
311{
312 FORWARD = 0,
314};
315
316template < typename BoundaryType >
317constexpr BoundaryPosition boundary_position_from_boundary_type_x( const BoundaryType& boundary_type )
318{
319 static_assert(
320 std::is_same_v< BoundaryType, BoundaryVertex > || std::is_same_v< BoundaryType, BoundaryEdge > ||
321 std::is_same_v< BoundaryType, BoundaryFace > );
322
323 return static_cast< BoundaryPosition >( ( static_cast< int >( boundary_type ) & 0b110000 ) >> 4 );
324}
325
326template < typename BoundaryType >
327constexpr BoundaryPosition boundary_position_from_boundary_type_y( const BoundaryType& boundary_type )
328{
329 static_assert(
330 std::is_same_v< BoundaryType, BoundaryVertex > || std::is_same_v< BoundaryType, BoundaryEdge > ||
331 std::is_same_v< BoundaryType, BoundaryFace > );
332
333 return static_cast< BoundaryPosition >( ( static_cast< int >( boundary_type ) & 0b001100 ) >> 2 );
334}
335
336template < typename BoundaryType >
337constexpr BoundaryPosition boundary_position_from_boundary_type_r( const BoundaryType& boundary_type )
338{
339 static_assert(
340 std::is_same_v< BoundaryType, BoundaryVertex > || std::is_same_v< BoundaryType, BoundaryEdge > ||
341 std::is_same_v< BoundaryType, BoundaryFace > );
342
343 return static_cast< BoundaryPosition >( ( static_cast< int >( boundary_type ) & 0b000011 ) >> 0 );
344}
345
346constexpr bool is_edge_boundary_radial( const BoundaryEdge id )
347{
348 return id == BoundaryEdge::E_00R || id == BoundaryEdge::E_10R || id == BoundaryEdge::E_01R ||
350}
351
353{
354 return id == BoundaryFace::F_XY0 || id == BoundaryFace::F_XY1;
355}
356
365
375
392
401
402// String conversion functions
403inline std::string to_string( BoundaryVertex v )
404{
405 switch ( v )
406 {
408 return "V_000";
410 return "V_100";
412 return "V_010";
414 return "V_110";
416 return "V_001";
418 return "V_101";
420 return "V_011";
422 return "V_111";
423 default:
424 return "<unknown LocalBoundaryVertex>";
425 }
426}
427
428inline std::string to_string( BoundaryEdge e )
429{
430 switch ( e )
431 {
433 return "E_X00";
435 return "E_X10";
437 return "E_X01";
439 return "E_X11";
441 return "E_0Y0";
443 return "E_1Y0";
445 return "E_0Y1";
447 return "E_1Y1";
449 return "E_00R";
451 return "E_10R";
453 return "E_01R";
455 return "E_11R";
456 default:
457 return "<unknown LocalBoundaryEdge>";
458 }
459}
460
461inline std::string to_string( BoundaryFace f )
462{
463 switch ( f )
464 {
466 return "F_XY0";
468 return "F_XY1";
470 return "F_X0R";
472 return "F_X1R";
474 return "F_0YR";
476 return "F_1YR";
477 default:
478 return "<unknown LocalBoundaryFace>";
479 }
480}
481
482inline std::ostream& operator<<( std::ostream& os, BoundaryVertex v )
483{
484 return os << to_string( v );
485}
486
487inline std::ostream& operator<<( std::ostream& os, BoundaryEdge e )
488{
489 return os << to_string( e );
490}
491
492inline std::ostream& operator<<( std::ostream& os, BoundaryFace f )
493{
494 return os << to_string( f );
495}
496
497} // namespace terra::grid
Definition bit_masks.hpp:8
Kokkos::View< ScalarType, Layout > Grid0DDataScalar
Definition grid_types.hpp:15
Kokkos::View< ScalarType *****, Layout > Grid5DDataScalar
Definition grid_types.hpp:30
Grid4DDataVec< ScalarType, VecDim >::HostMirror create_mirror(Kokkos::HostSpace space, const Grid4DDataVec< ScalarType, VecDim > &src)
Create a host mirror for Grid4DDataVec.
Definition grid_types.hpp:118
constexpr bool is_edge_boundary_radial(const BoundaryEdge id)
Definition grid_types.hpp:346
Kokkos::View< dense::Mat< ScalarType, Rows, Cols > ****[NumMatrices], Layout > Grid4DDataMatrices
Definition grid_types.hpp:173
Kokkos::View< ScalarType ***[VecDim], Layout > Grid3DDataVec
Definition grid_types.hpp:42
BoundaryPosition
Enum for encoding the boundary type tuples (in BoundaryVertex, BoundaryEdge, BoundaryFace).
Definition grid_types.hpp:223
Kokkos::View< ScalarType ***, Layout > Grid3DDataScalar
Definition grid_types.hpp:24
BoundaryVertex
Enum for identification of the 8 boundary vertices of a subdomain.
Definition grid_types.hpp:248
constexpr std::array all_boundary_faces
Definition grid_types.hpp:393
BoundaryDirection
Enum for the iteration direction at a boundary.
Definition grid_types.hpp:311
constexpr std::array all_boundary_vertices
Definition grid_types.hpp:366
std::ostream & operator<<(std::ostream &os, BoundaryVertex v)
Definition grid_types.hpp:482
constexpr std::array all_boundary_edges
Definition grid_types.hpp:376
Kokkos::LayoutRight Layout
Definition grid_types.hpp:12
Kokkos::View< ScalarType **[VecDim], Layout > Grid2DDataVec
Definition grid_types.hpp:39
constexpr int grid_data_vec_dim()
Definition grid_types.hpp:176
Kokkos::View< ScalarType *[VecDim], Layout > Grid1DDataVec
Definition grid_types.hpp:36
constexpr int boundary_position_encoding(const BoundaryPosition x, const BoundaryPosition y, const BoundaryPosition r)
Definition grid_types.hpp:234
constexpr BoundaryPosition boundary_position_from_boundary_type_y(const BoundaryType &boundary_type)
Definition grid_types.hpp:327
constexpr bool is_face_boundary_normal_to_radial_direction(const BoundaryFace id)
Definition grid_types.hpp:352
std::string to_string(BoundaryVertex v)
Definition grid_types.hpp:403
constexpr BoundaryPosition boundary_position_from_boundary_type_r(const BoundaryType &boundary_type)
Definition grid_types.hpp:337
Kokkos::View< ScalarType[VecDim], Layout > Grid0DDataVec
Definition grid_types.hpp:33
constexpr BoundaryVertex other_side_r(BoundaryVertex boundary_vertex)
Definition grid_types.hpp:357
BoundaryFace
Enum for identification of the 6 boundary faces of a subdomain.
Definition grid_types.hpp:297
Kokkos::View< ScalarType ****, Layout > Grid4DDataScalar
Definition grid_types.hpp:27
constexpr BoundaryPosition boundary_position_from_boundary_type_x(const BoundaryType &boundary_type)
Definition grid_types.hpp:317
void deep_copy(typename Grid4DDataVec< ScalarType, VecDim >::HostMirror &dst, const Grid4DDataVec< ScalarType, VecDim > &src)
Deep copy from device Grid4DDataVec to host mirror.
Definition grid_types.hpp:136
Kokkos::View< ScalarType *, Layout > Grid1DDataScalar
Definition grid_types.hpp:18
Kokkos::View< ScalarType **, Layout > Grid2DDataScalar
Definition grid_types.hpp:21
BoundaryEdge
Enum for identification of the 12 boundary edges of a subdomain.
Definition grid_types.hpp:269
Host mirror type for I/O.
Definition grid_types.hpp:92
Grid4DDataScalar< ScalarType >::HostMirror comp_[VecDim]
Definition grid_types.hpp:94
const ScalarType & operator()(int i0, int i1, int i2, int i3, int d) const
Definition grid_types.hpp:101
ScalarType & operator()(int i0, int i1, int i2, int i3, int d)
Definition grid_types.hpp:96
ScalarType value_type
Definition grid_types.hpp:93
auto extent(int i) const
Definition grid_types.hpp:106
SoA (Structure-of-Arrays) 4D vector grid data.
Definition grid_types.hpp:51
Grid4DDataScalar< ScalarType > comp_[VecDim]
Definition grid_types.hpp:57
auto extent(int i) const
Definition grid_types.hpp:75
typename Grid4DDataScalar< ScalarType >::memory_space memory_space
Definition grid_types.hpp:53
static constexpr int rank
Definition grid_types.hpp:55
static constexpr int vec_dim
Definition grid_types.hpp:54
ScalarType value_type
Definition grid_types.hpp:52
ScalarType & operator()(int i0, int i1, int i2, int i3, int d) const
Definition grid_types.hpp:69
Grid4DDataVec(const std::string &label, int s0, int s1, int s2, int s3)
Definition grid_types.hpp:61
std::string label() const
Get the label (derived from first component by stripping "_d0" suffix).
Definition grid_types.hpp:83