GEOSX
FunctionBase.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 Total, S.A
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 
19 #ifndef GEOSX_MANAGERS_FUNCTIONS_FUNCTIONBASE_HPP_
20 #define GEOSX_MANAGERS_FUNCTIONS_FUNCTIONBASE_HPP_
21 
22 #include "dataRepository/Group.hpp"
23 #include "rajaInterface/GEOS_RAJA_Interface.hpp"
24 
25 namespace geosx
26 {
27 
28 namespace dataRepository
29 {
30 namespace keys
31 {
36 string const inputVarNames( "inputVarNames" );
37 }
38 }
39 
46 {
47 public:
49  FunctionBase( const std::string & name,
50  dataRepository::Group * const parent );
51 
55  virtual ~FunctionBase() override;
56 
61  static string CatalogName() { return "FunctionBase"; }
62 
66  virtual void InitializeFunction(){}
67 
75  integer isFunctionOfTime() const;
76 
84  virtual void Evaluate( dataRepository::Group const * const group,
85  real64 const time,
87  real64_array & result ) const = 0;
88 
94  virtual real64 Evaluate( real64 const * const input ) const = 0;
95 
98 
104  {
105  static CatalogInterface::CatalogType catalog;
106  return catalog;
107  }
108 
116  real64_array EvaluateStats( dataRepository::Group const * const group,
117  real64 const time,
118  SortedArray< localIndex > const & set ) const;
119 
124  void setInputVarNames( string_array inputVarNames ) { m_inputVarNames = inputVarNames; }
125 
126 
127 protected:
130 
139  template< typename LEAF >
140  void EvaluateT( dataRepository::Group const * const group,
141  real64 const time,
143  real64_array & result ) const;
144 
145  virtual void PostProcessInput() override { InitializeFunction(); }
146 
147 };
148 
149 template< typename LEAF >
151  real64 const time,
153  real64_array & result ) const
154 {
155  real64 const * input_ptrs[4];
156  localIndex varSize[4] = {0, 0, 0, 0};
157  int timeVar[4] = {1, 1, 1, 1};
158 
159  arrayView1d< string const > const & inputVarNames = this->getReference< string_array >( dataRepository::keys::inputVarNames );
160  localIndex const numVars = LvArray::integerConversion< localIndex >( inputVarNames.size());
161  localIndex groupSize = group->size();
162  localIndex totalVarSize = 0;
163  for( auto varIndex=0; varIndex<numVars; ++varIndex )
164  {
165  string const & varName = inputVarNames[varIndex];
166 
167  if( varName=="time" )
168  {
169  input_ptrs[varIndex] = &time;
170  varSize[varIndex] = 1;
171  timeVar[varIndex] = 0;
172  ++totalVarSize;
173  }
174  else if( groupSize > 0 )
175  {
176  // Should we throw a warning if the group is zero-length?
177  dataRepository::WrapperBase const * wrapper = group->getWrapperBase( varName );
178  input_ptrs[ varIndex ] = reinterpret_cast< double const * >( wrapper->voidPointer() );
179 
180  localIndex wrapperSize = LvArray::integerConversion< localIndex >( wrapper->size());
181  varSize[varIndex] = wrapperSize / groupSize;
182  totalVarSize += varSize[varIndex];
183  }
184  }
185 
186  // Make sure the inputs do not exceed the maximum length
187  GEOSX_ERROR_IF( totalVarSize > 4, "Function input size is: " << totalVarSize );
188 
189  // Make sure the result / set size match
190  GEOSX_ERROR_IF( result.size() != set.size(), "To apply a function to a set, the size of the result and set must match" );
191 
192 
193  forAll< serialPolicy >( set.size(), [&, set]( localIndex const i )
194  {
195  localIndex const index = set[ i ];
196  double input[4];
197  int c = 0;
198  for( int a=0; a<numVars; ++a )
199  {
200  for( int b=0; b<varSize[a]; ++b )
201  {
202  input[c] = input_ptrs[a][(index*varSize[a]+b)*timeVar[a]];
203  ++c;
204  }
205  }
206 
207  // Note: we expect that result is the same size as the set
208  result[i] = static_cast< LEAF const * >(this)->Evaluate( input );
209  } );
210 }
211 } /* namespace geosx */
212 
213 #endif /* GEOSX_MANAGERS_FUNCTIONS_FUNCTIONBASE_HPP_ */
virtual void const * voidPointer() const =0
INDEX_TYPE size() const noexcept
Definition: ArrayView.hpp:361
virtual void InitializeFunction()
Function initialization.
This class serves to provide a "view" of a multidimensional array.
Definition: ArrayView.hpp:67
double real64
64-bit floating point type.
Definition: DataTypes.hpp:136
Base class for all wrappers containing common operations.
Definition: WrapperBase.hpp:48
void setInputVarNames(string_array inputVarNames)
Set the input variable names.
string_array m_inputVarNames
names for the input variables
virtual void PostProcessInput() override
static CatalogInterface::CatalogType & GetCatalog()
return the catalog entry for the function
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
This class provides the base class/interface for the catalog value objects.
virtual localIndex size() const =0
Calls T::size()
void EvaluateT(dataRepository::Group const *const group, real64 const time, SortedArrayView< localIndex const > const &set, real64_array &result) const
Method to apply an function with an arbitrary type of output.
string const inputVarNames("inputVarNames")
The key for inputVarNames.
localIndex size() const
Get the "size" of the group, which determines the number of elements in resizable wrappers...
Definition: Group.hpp:1301
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
static string CatalogName()
Static Factory Catalog Functions.
std::string string
String type.
Definition: DataTypes.hpp:131
#define GEOSX_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
Definition: Logger.hpp:103
WrapperBase const * getWrapperBase(indexType const index) const
Retrieve a WrapperBase stored in this group.
Definition: Group.hpp:1058
This class provides a fixed dimensional resizeable array interface in addition to an interface simila...
Definition: Array.hpp:55
std::unordered_map< std::string, std::unique_ptr< CatalogInterface< BASETYPE, ARGS... > > > CatalogType
This is the type that will be used for the catalog. The catalog is actually instantiated in the BASET...