GEOSX
DataTypes.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
22 #ifndef GEOS_COMMON_DATATYPES_HPP
23 #define GEOS_COMMON_DATATYPES_HPP
24 
25 // Source includes
26 #include "common/GeosxConfig.hpp"
27 #include "GeosxMacros.hpp"
28 #include "BufferAllocator.hpp"
29 #include "DataLayouts.hpp"
30 #include "Tensor.hpp"
31 #include "Logger.hpp"
32 #include "LvArray/src/Macros.hpp"
33 #include "LvArray/src/Array.hpp"
34 #include "LvArray/src/ArrayOfArrays.hpp"
35 #include "LvArray/src/ArrayOfSets.hpp"
36 #include "LvArray/src/SparsityPattern.hpp"
37 #include "LvArray/src/CRSMatrix.hpp"
38 #include "LvArray/src/SortedArray.hpp"
39 #include "LvArray/src/StackBuffer.hpp"
40 #include "LvArray/src/ChaiBuffer.hpp"
41 
42 #include "Path.hpp"
43 
44 // TPL includes
45 #include <camp/camp.hpp>
46 
47 // System includes
48 #ifdef GEOSX_USE_MPI
49  #include <mpi.h>
50 #endif
51 
52 #include <cassert>
53 //#include <cmath>
54 #include <cstdint>
55 #include <iostream>
56 #include <memory>
57 #include <typeindex>
58 #include <typeinfo>
59 #include <string>
60 #include <map>
61 #include <unordered_map>
62 #include <vector>
63 #include <set>
64 #include <string_view>
65 
69 namespace geos
70 {
71 
79 template< typename NEW_TYPE, typename EXISTING_TYPE >
80 NEW_TYPE dynamicCast( EXISTING_TYPE * const val )
81 {
82  static_assert( std::is_pointer< NEW_TYPE >::value, "NEW_TYPE must be a pointer." );
83  return dynamic_cast< NEW_TYPE >( val );
84 }
85 
93 template< typename NEW_TYPE, typename EXISTING_TYPE >
94 NEW_TYPE dynamicCast( EXISTING_TYPE & val )
95 {
96  static_assert( std::is_reference< NEW_TYPE >::value, "NEW_TYPE must be a reference." );
97 
98  using POINTER_TO_NEW_TYPE = std::remove_reference_t< NEW_TYPE > *;
99  POINTER_TO_NEW_TYPE ptr = dynamicCast< POINTER_TO_NEW_TYPE >( &val );
100  GEOS_ERROR_IF( ptr == nullptr, "Cast from " << LvArray::system::demangleType( val ) << " to " <<
101  LvArray::system::demangleType< NEW_TYPE >() << " failed." );
102 
103  return *ptr;
104 }
105 
107 #ifdef GEOSX_USE_MPI
108 extern MPI_Comm MPI_COMM_GEOSX;
109 #else
110 extern int MPI_COMM_GEOSX;
111 #endif
112 
117 
119 using size_t = std::size_t;
120 
122 using integer = std::int32_t;
123 
126 
129 
131 using string = std::string;
132 
135 
137 using real32 = float;
139 using real64 = double;
140 
142 
147 
149 using buffer_unit_type = signed char;
150 
151 #ifdef GEOSX_USE_CHAI
153 using buffer_type = std::vector< buffer_unit_type, BufferAllocator< buffer_unit_type > >;
154 #else
156 using buffer_type = std::vector< buffer_unit_type >;
157 #endif
158 
160 
165 
167 using Timestamp = unsigned long long int;
168 
170 
171 //START_SPHINX_INCLUDE_00
172 
177 
179 template< typename T,
180  int NDIM,
181  typename PERMUTATION=camp::make_idx_seq_t< NDIM > >
182 using Array = LvArray::Array< T, NDIM, PERMUTATION, localIndex, LvArray::ChaiBuffer >;
183 
185 template< typename T,
186  int NDIM,
187  int USD = NDIM - 1 >
188 using ArrayView = LvArray::ArrayView< T, NDIM, USD, localIndex, LvArray::ChaiBuffer >;
189 
191 template< typename T, int NDIM, int USD = NDIM - 1 >
192 using ArraySlice = LvArray::ArraySlice< T, NDIM, USD, localIndex >;
193 
195 template< typename T, int NDIM, int MAXSIZE, typename PERMUTATION=camp::make_idx_seq_t< NDIM > >
196 using StackArray = LvArray::StackArray< T, NDIM, PERMUTATION, localIndex, MAXSIZE >;
197 
199 
204 
209 
212 
213 
215 template< typename T >
217 
219 template< typename T >
221 
223 template< typename T, int USD = 0 >
225 
227 template< typename T, int MAXSIZE >
229 
231 template< typename T, typename PERMUTATION=camp::make_idx_seq_t< 2 > >
233 
235 template< typename T, int USD = 1 >
237 
239 template< typename T, int USD = 1 >
241 
243 template< typename T, int MAXSIZE >
245 
247 template< typename T, typename PERMUTATION=camp::make_idx_seq_t< 3 > >
249 
251 template< typename T, int USD=2 >
253 
255 template< typename T, int USD=2 >
257 
259 template< typename T, int MAXSIZE >
261 
263 template< typename T, typename PERMUTATION=camp::make_idx_seq_t< 4 > >
265 
267 template< typename T, int USD=3 >
269 
271 template< typename T, int USD=3 >
273 
275 template< typename T, int MAXSIZE >
277 
279 template< typename T, typename PERMUTATION=camp::make_idx_seq_t< 5 > >
281 
283 template< typename T, int USD=4 >
285 
287 template< typename T, int USD=4 >
289 
291 template< typename T, int MAXSIZE >
293 
295 
300 
302 template< typename T >
303 using set = std::set< T >;
304 
306 template< typename T >
307 using SortedArray = LvArray::SortedArray< T, localIndex, LvArray::ChaiBuffer >;
308 
310 template< typename T >
311 using SortedArrayView = LvArray::SortedArrayView< T, localIndex, LvArray::ChaiBuffer >;
312 
314 
319 
321 template< typename T, typename INDEX_TYPE=localIndex >
322 using ArrayOfArrays = LvArray::ArrayOfArrays< T, INDEX_TYPE, LvArray::ChaiBuffer >;
323 
325 template< typename T, typename INDEX_TYPE=localIndex, bool CONST_SIZES=std::is_const< T >::value >
326 using ArrayOfArraysView = LvArray::ArrayOfArraysView< T, INDEX_TYPE const, CONST_SIZES, LvArray::ChaiBuffer >;
327 
329 template< typename T, typename INDEX_TYPE=localIndex >
330 using ArrayOfSets = LvArray::ArrayOfSets< T, INDEX_TYPE, LvArray::ChaiBuffer >;
331 
333 template< typename T, typename INDEX_TYPE=localIndex >
334 using ArrayOfSetsView = LvArray::ArrayOfSetsView< T, INDEX_TYPE const, LvArray::ChaiBuffer >;
335 
337 template< typename COL_INDEX, typename INDEX_TYPE=localIndex >
338 using SparsityPattern = LvArray::SparsityPattern< COL_INDEX, INDEX_TYPE, LvArray::ChaiBuffer >;
339 
341 template< typename COL_INDEX, typename INDEX_TYPE=localIndex >
342 using SparsityPatternView = LvArray::SparsityPatternView< COL_INDEX, INDEX_TYPE const, LvArray::ChaiBuffer >;
343 
345 template< typename T, typename COL_INDEX=globalIndex >
346 using CRSMatrix = LvArray::CRSMatrix< T, COL_INDEX, localIndex, LvArray::ChaiBuffer >;
347 
349 template< typename T, typename COL_INDEX=globalIndex >
350 using CRSMatrixView = LvArray::CRSMatrixView< T, COL_INDEX, localIndex const, LvArray::ChaiBuffer >;
351 
353 
354 //END_SPHINX_INCLUDE_00
355 
360 
367 template< typename TKEY, typename TVAL, typename SORTED >
368 class mapBase
369 {};
370 
372 template< typename TKEY, typename TVAL >
373 class mapBase< TKEY, TVAL, std::integral_constant< bool, true > > : public std::map< TKEY, TVAL >
374 {
375  using std::map< TKEY, TVAL >::map; // enable list initialization
376 };
377 
378 template< typename TKEY, typename TVAL >
379 class mapBase< TKEY, TVAL, std::integral_constant< bool, false > > : public std::unordered_map< TKEY, TVAL >
380 {
381  using std::unordered_map< TKEY, TVAL >::unordered_map; // enable list initialization
382 };
384 
394 template< typename K, typename V, typename SORTED >
395 inline
396 std::ostream & operator<< ( std::ostream & stream, mapBase< K, V, SORTED > const & map )
397 {
398  stream << "{\n";
399  for( auto const & pair : map )
400  {
401  stream << pair.first << " : " << pair.second << "\n";
402  }
403  stream << "}";
404  return stream;
405 }
406 
408 template< typename TKEY, typename TVAL >
410 
412 template< typename TKEY, typename TVAL >
414 
416 
421 
424 
427 
430 
433 
436 
439 
442 
443 
446 
449 
452 
455 
458 
459 
462 
465 
468 
471 
474 
475 
478 
481 
484 
487 
490 
492 
494 constexpr static auto GLOBALINDEX_MAX = std::numeric_limits< globalIndex >::max();
495 
497 constexpr static auto LOCALINDEX_MAX = std::numeric_limits< localIndex >::max();
498 
500 constexpr static localIndex unmappedLocalIndexValue = -1;
501 
502 
507 
512 struct Regex
513 {
517  string m_regexStr;
525  Regex() {}
530  Regex( string_view regexStr, string_view formatDescription );
531 };
532 
544 template< typename T, typename ENABLE = void >
545 struct TypeRegex
546 {
551  static Regex get() { return {}; }
552 };
553 
558 class rtTypes
559 {
560 public:
561 
565  using RegexMapType = std::map< string, Regex >;
566 
571  struct CustomTypes
572  {
574  static constexpr string_view mapPair = "mapPair";
575  static constexpr string_view plotLevel = "geos_dataRepository_PlotLevel";
576  static constexpr string_view groupName = "groupName";
577  static constexpr string_view groupNameRef = "groupNameRef";
578  static constexpr string_view groupNameRefArray = "groupNameRef_array";
580  };
581 
587  static string getTypeName( std::type_index const key );
588 
593  template< typename T >
594  static Regex const & getTypeRegex()
595  { return getTypeRegex< T >( getTypeName( typeid( T ) ) ); }
596 
603  template< typename T >
604  static Regex const & getTypeRegex( string_view typeName )
605  {
606  RegexMapType & map = getTypeRegexMap();
607  auto const it = map.find( string( typeName ) );
608  if( it != map.end() )
609  {
610  return it->second;
611  }
612  else
613  {
614  return map.emplace( typeName, TypeRegex< T >::get() ).first->second;
615  }
616  }
617 
623 
624 private:
625 
629  static RegexMapType & getTypeRegexMap()
630  {
632  return m;
633  }
634 
638  rtTypes() {}
639 
640 };
641 
649 template< typename T >
650 struct TypeName
651 {
655  static string full()
656  {
657  return ::LvArray::system::demangle( typeid( T ).name() );
658  }
659 
663  static string brief()
664  {
665  string const full_name = full();
666  string::size_type const pos = full_name.find_last_of( "::" );
667  return ( pos == string::npos ) ? full_name : full_name.substr( pos );
668  }
669 };
670 
671 }
672 
673 
674 
675 #endif /* GEOS_COMMON_DATATYPES_HPP */
#define GEOSX_GLOBALINDEX_TYPE
The type that globalIndex will be aliased to.
#define GEOSX_LOCALINDEX_TYPE
The type that localIndex will be aliased to.
#define GEOS_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
Definition: Logger.hpp:107
Base template for ordered and unordered maps.
Definition: DataTypes.hpp:369
Static class to manage the type selection of types at runtime and obtain the regexes of these types....
Definition: DataTypes.hpp:559
std::map< string, Regex > RegexMapType
the regex map type to store and find the regexes by the associated rtTypeName.
Definition: DataTypes.hpp:565
static RegexMapType createBasicTypesRegexMap()
Construct the regexMap for all basic types (TypeRegex< T > extented types are not mentionned)
static string getTypeName(std::type_index const key)
Convert a std::type_index to a string.
static Regex const & getTypeRegex()
Definition: DataTypes.hpp:594
static Regex const & getTypeRegex(string_view typeName)
Definition: DataTypes.hpp:604
array2d< real32 > real32_array2d
A 2-dimensional array of geos::real32 types.
Definition: DataTypes.hpp:448
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:220
LvArray::Array< T, NDIM, PERMUTATION, localIndex, LvArray::ChaiBuffer > Array
Multidimensional array type. See LvArray:Array for details.
Definition: DataTypes.hpp:182
StackArray< T, 2, MAXSIZE > stackArray2d
Alias for 2D stack array.
Definition: DataTypes.hpp:244
float real32
32-bit floating point type.
Definition: DataTypes.hpp:137
array1d< localIndex > localIndex_array
A 1-dimensional array of geos::localIndex types.
Definition: DataTypes.hpp:438
unsigned long long int Timestamp
Timestamp type (used to perform actions such a sparsity pattern computation after mesh modifications)
Definition: DataTypes.hpp:167
array2d< globalIndex > globalIndex_array2d
A 2-dimensional array of geos::globalIndex types.
Definition: DataTypes.hpp:457
Array< T, 2, PERMUTATION > array2d
Alias for 2D array.
Definition: DataTypes.hpp:232
StackArray< T, 4, MAXSIZE > stackArray4d
Alias for 4D stack array.
Definition: DataTypes.hpp:276
Array< T, 5, PERMUTATION > array5d
Alias for 5D array.
Definition: DataTypes.hpp:280
int MPI_COMM_GEOSX
Global MPI communicator used by GEOSX.
LvArray::CRSMatrixView< T, COL_INDEX, localIndex const, LvArray::ChaiBuffer > CRSMatrixView
Alias for CRS Matrix View.
Definition: DataTypes.hpp:350
array1d< globalIndex > globalIndex_array
A 1-dimensional array of geos::globalIndex types.
Definition: DataTypes.hpp:441
Array< T, 3, PERMUTATION > array3d
Alias for 3D array.
Definition: DataTypes.hpp:248
array1d< string > string_array
A 1-dimensional array of geos::string types.
Definition: DataTypes.hpp:432
std::string string
String type.
Definition: DataTypes.hpp:131
NEW_TYPE dynamicCast(EXISTING_TYPE *const val)
Perform a type cast of base to derived pointer.
Definition: DataTypes.hpp:80
constexpr static auto GLOBALINDEX_MAX
A variable for the maximum value of a geos::globalIndex.
Definition: DataTypes.hpp:494
LvArray::ArrayOfSetsView< T, INDEX_TYPE const, LvArray::ChaiBuffer > ArrayOfSetsView
View of array of variable-sized sets. See LvArray::ArrayOfSetsView for details.
Definition: DataTypes.hpp:334
array1d< integer > integer_array
A 1-dimensional array of geos::integer types.
Definition: DataTypes.hpp:423
constexpr static auto LOCALINDEX_MAX
A variable for the maximum value of a geos::localIndex.
Definition: DataTypes.hpp:497
array4d< integer > integer_array4d
A 4-dimensional array of geos::integer types.
Definition: DataTypes.hpp:477
mapBase< TKEY, TVAL, std::integral_constant< bool, false > > unordered_map
Unordered map type.
Definition: DataTypes.hpp:413
LvArray::StackArray< T, NDIM, PERMUTATION, localIndex, MAXSIZE > StackArray
Multidimensional stack-based array type. See LvArray:StackArray for details.
Definition: DataTypes.hpp:196
constexpr static localIndex unmappedLocalIndexValue
A global variable for the value of a object that has not been assigned a geos::globalIndex.
Definition: DataTypes.hpp:500
LvArray::SparsityPatternView< COL_INDEX, INDEX_TYPE const, LvArray::ChaiBuffer > SparsityPatternView
Alias for Sparsity pattern View.
Definition: DataTypes.hpp:342
array4d< localIndex > localIndex_array4d
A 4-dimensional array of geos::localIndex types.
Definition: DataTypes.hpp:486
LvArray::SparsityPattern< COL_INDEX, INDEX_TYPE, LvArray::ChaiBuffer > SparsityPattern
Alias for Sparsity pattern class.
Definition: DataTypes.hpp:338
array4d< real64 > real64_array4d
A 4-dimensional array of geos::real64 types.
Definition: DataTypes.hpp:483
StackArray< T, 1, MAXSIZE > stackArray1d
Alias for 1D stack array.
Definition: DataTypes.hpp:228
LvArray::ArrayOfArraysView< T, INDEX_TYPE const, CONST_SIZES, LvArray::ChaiBuffer > ArrayOfArraysView
View of array of variable-sized arrays. See LvArray::ArrayOfArraysView for details.
Definition: DataTypes.hpp:326
array4d< globalIndex > globalIndex_array4d
A 4-dimensional array of geos::globalIndex types.
Definition: DataTypes.hpp:489
ArraySlice< T, 2, USD > arraySlice2d
Alias for 2D array slice.
Definition: DataTypes.hpp:240
ArraySlice< T, 3, USD > arraySlice3d
Alias for 3D array slice.
Definition: DataTypes.hpp:256
array1d< Path > path_array
A 1-dimensional array of geos::Path types.
Definition: DataTypes.hpp:435
std::set< T > set
A set of local indices.
Definition: DataTypes.hpp:303
ArrayView< T, 5, USD > arrayView5d
Alias for 5D array view.
Definition: DataTypes.hpp:284
double real64
64-bit floating point type.
Definition: DataTypes.hpp:139
array3d< globalIndex > globalIndex_array3d
A 3-dimensional array of geos::globalIndex types.
Definition: DataTypes.hpp:473
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:224
array3d< real32 > real32_array3d
A 3-dimensional array of geos::real32 types.
Definition: DataTypes.hpp:464
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
array3d< localIndex > localIndex_array3d
A 3-dimensional array of geos::localIndex types.
Definition: DataTypes.hpp:470
std::vector< buffer_unit_type > buffer_type
Type of storage for communication buffers.
Definition: DataTypes.hpp:156
LvArray::CRSMatrix< T, COL_INDEX, localIndex, LvArray::ChaiBuffer > CRSMatrix
Alias for CRS Matrix class.
Definition: DataTypes.hpp:346
array3d< integer > integer_array3d
A 3-dimensional array of geos::integer types.
Definition: DataTypes.hpp:461
array2d< integer > integer_array2d
A 2-dimensional array of geos::integer types.
Definition: DataTypes.hpp:445
array1d< real64 > real64_array
A 1-dimensional array of geos::real64 types.
Definition: DataTypes.hpp:429
ArraySlice< T, 4, USD > arraySlice4d
Alias for 4D array slice.
Definition: DataTypes.hpp:272
array2d< real64 > real64_array2d
A 2-dimensional array of geos::real64 types.
Definition: DataTypes.hpp:451
ArraySlice< T, 5, 4 > arraySlice5d
Alias for 5D array slice.
Definition: DataTypes.hpp:288
GEOSX_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
std::size_t size_t
Unsigned size type.
Definition: DataTypes.hpp:119
mapBase< TKEY, TVAL, std::integral_constant< bool, true > > map
Ordered map type.
Definition: DataTypes.hpp:409
std::ostream & operator<<(std::ostream &stream, mapBase< K, V, SORTED > const &map)
Stream output operator for map types.
Definition: DataTypes.hpp:396
LvArray::SortedArray< T, localIndex, LvArray::ChaiBuffer > SortedArray
A sorted array of local indices.
Definition: DataTypes.hpp:307
LvArray::ArrayOfSets< T, INDEX_TYPE, LvArray::ChaiBuffer > ArrayOfSets
Array of variable-sized sets. See LvArray::ArrayOfSets for details.
Definition: DataTypes.hpp:330
Array< T, 4, PERMUTATION > array4d
Alias for 4D array.
Definition: DataTypes.hpp:264
array1d< real32 > real32_array
A 1-dimensional array of geos::real32 types.
Definition: DataTypes.hpp:426
LvArray::SortedArrayView< T, localIndex, LvArray::ChaiBuffer > SortedArrayView
A sorted array view of local indices.
Definition: DataTypes.hpp:311
void printTypeSummary()
Print a short summary of a few select type aliases.
ArrayView< T, 4, USD > arrayView4d
Alias for 4D array view.
Definition: DataTypes.hpp:268
LvArray::ArraySlice< T, NDIM, USD, localIndex > ArraySlice
Multidimensional array slice type. See LvArray:ArraySlice for details.
Definition: DataTypes.hpp:192
StackArray< T, 3, MAXSIZE > stackArray3d
Alias for 3D stack array.
Definition: DataTypes.hpp:260
array4d< real32 > real32_array4d
A 4-dimensional array of geos::real32 types.
Definition: DataTypes.hpp:480
array3d< real64 > real64_array3d
A 3-dimensional array of geos::real64 types.
Definition: DataTypes.hpp:467
signed char buffer_unit_type
Type stored in communication buffers.
Definition: DataTypes.hpp:149
ArrayView< T, 2, USD > arrayView2d
Alias for 2D array view.
Definition: DataTypes.hpp:236
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
StackArray< T, 5, MAXSIZE > stackArray5d
Alias for 5D stack array.
Definition: DataTypes.hpp:292
array2d< localIndex > localIndex_array2d
A 2-dimensional array of geos::localIndex types.
Definition: DataTypes.hpp:454
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:216
LvArray::ArrayOfArrays< T, INDEX_TYPE, LvArray::ChaiBuffer > ArrayOfArrays
Array of variable-sized arrays. See LvArray::ArrayOfArrays for details.
Definition: DataTypes.hpp:322
std::string_view string_view
String type.
Definition: DataTypes.hpp:134
LvArray::ArrayView< T, NDIM, USD, localIndex, LvArray::ChaiBuffer > ArrayView
Multidimensional array view type. See LvArray:ArrayView for details.
Definition: DataTypes.hpp:188
ArrayView< T, 3, USD > arrayView3d
Alias for 3D array view.
Definition: DataTypes.hpp:252
The regular expression data for validating inputs. Use rtTypes to get the regex of a type,...
Definition: DataTypes.hpp:513
Regex()
Default constructor.
Definition: DataTypes.hpp:525
Regex(string_view regexStr, string_view formatDescription)
string m_regexStr
the regular expression string.
Definition: DataTypes.hpp:517
string m_formatDescription
the description of the expected format of the regular expression.
Definition: DataTypes.hpp:521
Utility class for querying type names at runtime.
Definition: DataTypes.hpp:651
static string brief()
Definition: DataTypes.hpp:663
static string full()
Definition: DataTypes.hpp:655
Extension point for custom types to provide a validation regexp to schema. Do not use directly to obt...
Definition: DataTypes.hpp:546
static Regex get()
Get the type's regex (default implementation returns nothing).
Definition: DataTypes.hpp:551
Custom types are useful to customize the regexes of an existing type. The type name can be one of the...
Definition: DataTypes.hpp:572