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 Total, S.A
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 GEOSX_HISTORY_DATA_SPEC_HPP_
20 #define GEOSX_HISTORY_DATA_SPEC_HPP_
21 
22 #include "codingUtilities/traits.hpp"
23 #include "common/DataTypes.hpp"
24 #include "LvArray/src/Array.hpp"
25 
26 namespace geosx
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  std::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 
175 template< typename T >
176 inline
177 typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
178 getHistoryMetadata( string const & name, ArrayView< T const, 1, 0 > const & arr, localIndex sizeOverride = -1 )
179 {
180  localIndex size = sizeOverride < 0 ? arr.size( ) : sizeOverride;
181  return HistoryMetadata( name, size, std::type_index( typeid( T )));
182 }
183 
193 template< typename T >
194 inline
195 typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
196 getHistoryMetadata( string const & name, SortedArrayView< T const > const & arr, localIndex sizeOverride = -1 )
197 {
198  localIndex size = sizeOverride < 0 ? arr.size( ) : sizeOverride;
199  return HistoryMetadata( name, size, std::type_index( typeid(T)));
200 }
201 
211 template< typename ARRAY_T >
212 inline
213 typename std::enable_if< ( traits::is_array_type< ARRAY_T >) && (ARRAY_T::NDIM > 1) && can_history_io< typename ARRAY_T::value_type >, HistoryMetadata >::type
214 getHistoryMetadata( string const & name, ARRAY_T const & arr, localIndex sizeOverride = -1 )
215 {
216  localIndex perIndexSize = arr[ 0 ].size( );
217  localIndex numIndices = ( sizeOverride >= 0 ? sizeOverride : arr.size( ) / perIndexSize );
218  localIndex sizes[2] = { numIndices, perIndexSize };
219  return HistoryMetadata( name, 2, &sizes[0], std::type_index( typeid(typename ARRAY_T::value_type)));
220 }
221 
231 template< typename T >
232 inline typename std::enable_if< can_history_io< T >, HistoryMetadata >::type
233 getHistoryMetadata( string const & name, const T & type, localIndex sizeOverride = -1 )
234 {
235  GEOSX_UNUSED_VAR( type );
236  localIndex size = sizeOverride < 0 ? 0 : sizeOverride;
237  return HistoryMetadata( name, size, std::type_index( typeid(T)));
238 }
239 
248 template< typename T >
249 inline typename std::enable_if< can_history_io_container< T > && !can_history_io< typename T::value_type >, HistoryMetadata >::type
250 getHistoryMetadata( string const & name, const T & type, localIndex sizeOverride )
251 {
252  GEOSX_ERROR( "Trying to use time history output on an unsupported type." );
253  GEOSX_UNUSED_VAR( name );
254  GEOSX_UNUSED_VAR( type );
255  GEOSX_UNUSED_VAR( sizeOverride );
256  return HistoryMetadata( );
257 }
258 
267 template< typename T >
268 inline typename std::enable_if< !can_history_io_container< T > && !can_history_io< T >, HistoryMetadata >::type
269 getHistoryMetadata( string const & name, const T & type, localIndex sizeOverride )
270 {
271  GEOSX_ERROR( "Trying to use time history output on an unsupported type." );
272  GEOSX_UNUSED_VAR( name );
273  GEOSX_UNUSED_VAR( type );
274  GEOSX_UNUSED_VAR( sizeOverride );
275  return HistoryMetadata( );
276 }
277 
278 }
279 
280 #endif
INDEX_TYPE size() const noexcept
Definition: ArrayView.hpp:361
void setName(const string &name)
Set the name. Typically used for metadata collectors to avoid writing data with the same name to the ...
constexpr bool can_history_io_container
Whether the type is a supported container for history collection and io operations.
This class serves to provide a "view" of a multidimensional array.
Definition: ArrayView.hpp:67
HistoryMetadata(const string &name, localIndex count, std::type_index type)
Constructor for one-dimensional array types.
HistoryMetadata(const string &name, localIndex rank, localIndex *dims, std::type_index type)
Constructor for multi-dimensional array types.
constexpr bool can_history_io
A constexpr bool to determine wether a type is compatible with the history collected and IO operation...
localIndex getRank() const
Get the rank of the array data to be collected.
Contains the implementation of LvArray::Array.
constexpr INDEX_TYPE size() const
void setType(std::type_index type)
Set the type. Typically used for metadata collectors where local metadata is used to produce and outp...
localIndex size(localIndex dim) const
Get the size of the specified dimension.
std::vector< localIndex > const & getDims() const
Get a pointer to the extent of each dimension.
#define GEOSX_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:110
#define GEOSX_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:78
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
localIndex size() const
Get the total data count for the data being collected.
std::type_index getType() const
Get the type of the collected data.
std::string string
String type.
Definition: DataTypes.hpp:131
std::enable_if< can_history_io< T >, HistoryMetadata >::type getHistoryMetadata(string const &name, ArrayView< T const, 1, 0 > const &arr, localIndex sizeOverride=-1)
Produce a HistoryMetadata object for a supported one-dimensional array type.
const string & getName() const
Get the name.
HistoryMetadata()
Default constructor.
A minimal class to specify information about time history information being collected and output...
This class provides a view into a SortedArray.