GEOSX
HistoryDataSpec.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2019 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2019 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2019 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All right reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_DATAREPOSITORY_HISTORYDATASPEC_HPP_
20 #define GEOS_DATAREPOSITORY_HISTORYDATASPEC_HPP_
21 
22 #include "codingUtilities/traits.hpp"
23 #include "common/DataTypes.hpp"
24 #include "LvArray/src/Array.hpp"
25 
26 namespace geos
27 {
29 template< typename T >
30 constexpr bool can_history_io = std::is_same< std::remove_reference_t< std::remove_const_t< T > >, char >::value ||
31  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, signed char >::value ||
32  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, float >::value ||
33  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, double >::value ||
34  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, int >::value ||
35  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, long >::value ||
36  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, long long >::value;
37 
43 {
44 public:
49  m_name( "null" ),
50  m_rank( 0 ),
51  m_dims( ),
52  m_type( std::type_index( typeid( nullptr ) ) )
53  {}
54 
62  HistoryMetadata( const string & name, localIndex rank, localIndex * dims, std::type_index type ):
63  m_name( name ),
64  m_rank( rank ),
65  m_dims( dims, dims+rank ),
66  m_type( type )
67  {}
74  HistoryMetadata( const string & name, localIndex count, std::type_index type ):
75  m_name( name ),
76  m_rank( 1 ),
77  m_dims( &count, &count+1 ),
78  m_type( type )
79  {}
85  void setName( const string & name )
86  {
87  m_name = name;
88  }
93  const string & getName( ) const
94  {
95  return m_name;
96  }
102  void setType( std::type_index type )
103  {
104  m_type = type;
105  }
110  std::type_index getType( ) const
111  {
112  return m_type;
113  }
118  localIndex size( ) const
119  {
120  localIndex localSize = 1;
121  for( localIndex dim : m_dims )
122  {
123  localSize *= dim;
124  }
125  return localSize;
126  }
132  {
133  return m_rank;
134  }
139  std::vector< localIndex > const & getDims( ) const
140  {
141  return m_dims;
142  }
149  {
150  return m_dims[dim];
151  }
152 private:
153  string m_name;
154  localIndex m_rank;
155  std::vector< localIndex > m_dims;
156  std::type_index m_type;
157 };
158 
163 template< typename T >
164 constexpr bool can_history_io_container = ( traits::is_array_type< T > || traits::is_sorted_array_type< T > );
165 
176 template< typename T >
177 inline
178 typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
179 getHistoryMetadata( string const & name, ArrayView< T const, 1, 0 > const & arr, localIndex const numComps, localIndex sizeOverride = -1 )
180 {
181  GEOS_UNUSED_VAR( numComps );
182  localIndex size = sizeOverride < 0 ? arr.size( ) : sizeOverride;
183  return HistoryMetadata( name, size, std::type_index( typeid( T )));
184 }
185 
196 template< typename T >
197 inline
198 typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
199 getHistoryMetadata( string const & name, SortedArrayView< T const > const & arr, localIndex const numComps, localIndex sizeOverride = -1 )
200 {
201  GEOS_UNUSED_VAR( numComps );
202  localIndex size = sizeOverride < 0 ? arr.size( ) : sizeOverride;
203  return HistoryMetadata( name, size, std::type_index( typeid(T)));
204 }
205 
216 template< typename ARRAY_T >
217 inline
218 typename std::enable_if< ( traits::is_array_type< ARRAY_T >) && (ARRAY_T::NDIM > 1) && can_history_io< typename ARRAY_T::value_type >, HistoryMetadata >::type
219 getHistoryMetadata( string const & name, ARRAY_T const & arr, localIndex const numComps, localIndex sizeOverride = -1 )
220 {
221  // Array dim > 1 so this should be valid (i.e., no division by zero)
222  localIndex const numIndices = sizeOverride >= 0 ? sizeOverride : arr.size( ) / numComps;
223  localIndex sizes[2] = { numIndices, numComps };
224  return HistoryMetadata( name, 2, &sizes[0], std::type_index( typeid(typename ARRAY_T::value_type)));
225 }
226 
237 template< typename T >
238 inline typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
239 getHistoryMetadata( string const & name, const T & type, localIndex const numComps, localIndex sizeOverride = -1 )
240 {
241  GEOS_UNUSED_VAR( type, numComps );
242  localIndex size = sizeOverride < 0 ? 0 : sizeOverride;
243  return HistoryMetadata( name, size, std::type_index( typeid(T)));
244 }
245 
255 template< typename T >
256 inline typename std::enable_if< can_history_io_container< T > && !can_history_io< typename T::value_type >, HistoryMetadata >::type
257 getHistoryMetadata( string const & name, const T & type, localIndex const numComps, localIndex sizeOverride )
258 {
259  GEOS_ERROR( "Trying to use time history output on an unsupported type." );
260  GEOS_UNUSED_VAR( name, type, numComps, sizeOverride );
261  return HistoryMetadata( );
262 }
263 
273 template< typename T >
274 inline typename std::enable_if< !can_history_io_container< T > && !can_history_io< T >, HistoryMetadata >::type
275 getHistoryMetadata( string const & name, const T & type, localIndex const numComps, localIndex sizeOverride )
276 {
277  GEOS_ERROR( "Trying to use time history output on an unsupported type." );
278  GEOS_UNUSED_VAR( name, type, numComps, sizeOverride );
279  return HistoryMetadata( );
280 }
281 
282 }
283 
284 #endif
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:83
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:122
A minimal class to specify information about time history information being collected and output.
HistoryMetadata(const string &name, localIndex rank, localIndex *dims, std::type_index type)
Constructor for multi-dimensional array types.
void setType(std::type_index type)
Set the type. Typically used for metadata collectors where local metadata is used to produce and outp...
HistoryMetadata(const string &name, localIndex count, std::type_index type)
Constructor for one-dimensional array types.
localIndex size(localIndex dim) const
Get the size of the specified dimension.
void setName(const string &name)
Set the name. Typically used for metadata collectors to avoid writing data with the same name to the ...
localIndex getRank() const
Get the rank of the array data to be collected.
std::type_index getType() const
Get the type of the collected data.
const string & getName() const
Get the name.
HistoryMetadata()
Default constructor.
std::vector< localIndex > const & getDims() const
Get a pointer to the extent of each dimension.
localIndex size() const
Get the total data count for the data being collected.
constexpr bool can_history_io
A constexpr bool to determine wether a type is compatible with the history collected and IO operation...
constexpr bool can_history_io_container
Whether the type is a supported container for history collection and io operations.
LvArray::SortedArrayView< T, localIndex, LvArray::ChaiBuffer > SortedArrayView
A sorted array view of local indices.
Definition: DataTypes.hpp:311
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
std::enable_if< can_history_io< T >, HistoryMetadata >::type getHistoryMetadata(string const &name, ArrayView< T const, 1, 0 > const &arr, localIndex const numComps, localIndex sizeOverride=-1)
Produce a HistoryMetadata object for a supported one-dimensional array type.
LvArray::ArrayView< T, NDIM, USD, localIndex, LvArray::ChaiBuffer > ArrayView
Multidimensional array view type. See LvArray:ArrayView for details.
Definition: DataTypes.hpp:188