GEOS
HistoryDataSpec.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_DATAREPOSITORY_HISTORYDATASPEC_HPP_
21 #define GEOS_DATAREPOSITORY_HISTORYDATASPEC_HPP_
22 
23 #include "codingUtilities/traits.hpp"
24 #include "common/DataTypes.hpp"
25 #include "common/logger/Logger.hpp"
26 #include "LvArray/src/Array.hpp"
27 
28 namespace geos
29 {
31 template< typename T >
32 constexpr bool can_history_io = std::is_same< std::remove_reference_t< std::remove_const_t< T > >, char >::value ||
33  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, signed char >::value ||
34  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, float >::value ||
35  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, double >::value ||
36  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, int >::value ||
37  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, long >::value ||
38  std::is_same< std::remove_reference_t< std::remove_const_t< T > >, long long >::value;
39 
45 {
46 public:
51  m_name( "null" ),
52  m_rank( 0 ),
53  m_dims( ),
54  m_type( std::type_index( typeid( nullptr ) ) )
55  {}
56 
64  HistoryMetadata( const string & name, localIndex rank, localIndex * dims, std::type_index type ):
65  m_name( name ),
66  m_rank( rank ),
67  m_dims( dims, dims+rank ),
68  m_type( type )
69  {}
76  HistoryMetadata( const string & name, localIndex count, std::type_index type ):
77  m_name( name ),
78  m_rank( 1 ),
79  m_dims( &count, &count+1 ),
80  m_type( type )
81  {}
87  void setName( const string & name )
88  {
89  m_name = name;
90  }
95  const string & getName( ) const
96  {
97  return m_name;
98  }
104  void setType( std::type_index type )
105  {
106  m_type = type;
107  }
112  std::type_index getType( ) const
113  {
114  return m_type;
115  }
120  localIndex size( ) const
121  {
122  localIndex localSize = 1;
123  for( localIndex dim : m_dims )
124  {
125  localSize *= dim;
126  }
127  return localSize;
128  }
134  {
135  return m_rank;
136  }
141  std::vector< localIndex > const & getDims( ) const
142  {
143  return m_dims;
144  }
151  {
152  return m_dims[dim];
153  }
154 private:
155  string m_name;
156  localIndex m_rank;
157  std::vector< localIndex > m_dims;
158  std::type_index m_type;
159 };
160 
165 template< typename T >
166 constexpr bool can_history_io_container = ( traits::is_array_type< T > || traits::is_sorted_array_type< T > );
167 
178 template< typename T >
179 inline
180 typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
181 getHistoryMetadata( string const & name, ArrayView< T const, 1, 0 > const & arr, localIndex const numComps, localIndex sizeOverride = -1 )
182 {
183  GEOS_UNUSED_VAR( numComps );
184  localIndex size = sizeOverride < 0 ? arr.size( ) : sizeOverride;
185  return HistoryMetadata( name, size, std::type_index( typeid( T )));
186 }
187 
198 template< typename T >
199 inline
200 typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
201 getHistoryMetadata( string const & name, SortedArrayView< T const > const & arr, localIndex const numComps, localIndex sizeOverride = -1 )
202 {
203  GEOS_UNUSED_VAR( numComps );
204  localIndex size = sizeOverride < 0 ? arr.size( ) : sizeOverride;
205  return HistoryMetadata( name, size, std::type_index( typeid(T)));
206 }
207 
218 template< typename ARRAY_T >
219 inline
220 typename std::enable_if< ( traits::is_array_type< ARRAY_T >) && (ARRAY_T::NDIM > 1) && can_history_io< typename ARRAY_T::value_type >, HistoryMetadata >::type
221 getHistoryMetadata( string const & name, ARRAY_T const & arr, localIndex const numComps, localIndex sizeOverride = -1 )
222 {
223  // Array dim > 1 so this should be valid (i.e., no division by zero)
224  localIndex const numIndices = sizeOverride >= 0 ? sizeOverride : arr.size( ) / numComps;
225  localIndex sizes[2] = { numIndices, numComps };
226  return HistoryMetadata( name, 2, &sizes[0], std::type_index( typeid(typename ARRAY_T::value_type)));
227 }
228 
239 template< typename T >
240 inline typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
241 getHistoryMetadata( string const & name, const T & type, localIndex const numComps, localIndex sizeOverride = -1 )
242 {
243  GEOS_UNUSED_VAR( type, numComps );
244  localIndex size = sizeOverride < 0 ? 0 : sizeOverride;
245  return HistoryMetadata( name, size, std::type_index( typeid(T)));
246 }
247 
257 template< typename T >
258 inline typename std::enable_if< can_history_io_container< T > && !can_history_io< typename T::value_type >, HistoryMetadata >::type
259 getHistoryMetadata( string const & name, const T & type, localIndex const numComps, localIndex sizeOverride )
260 {
261  GEOS_ERROR( "Trying to use time history output on an unsupported type." );
262  GEOS_UNUSED_VAR( name, type, numComps, sizeOverride );
263  return HistoryMetadata( );
264 }
265 
275 template< typename T >
276 inline typename std::enable_if< !can_history_io_container< T > && !can_history_io< T >, HistoryMetadata >::type
277 getHistoryMetadata( string const & name, const T & type, localIndex const numComps, localIndex sizeOverride )
278 {
279  GEOS_ERROR( "Trying to use time history output on an unsupported type." );
280  GEOS_UNUSED_VAR( name, type, numComps, sizeOverride );
281  return HistoryMetadata( );
282 }
283 
284 }
285 
286 #endif
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:84
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:157
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.
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:85
LvArray::SortedArrayView< T, localIndex, LvArray::ChaiBuffer > SortedArrayView
A sorted array view of local indices.
Definition: DataTypes.hpp:271
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:148