GEOS
FieldIdentifiers.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_MESH_MPICOMMUNICATIONS_FIELDIDENTIFIERS_HPP_
21 #define GEOS_MESH_MPICOMMUNICATIONS_FIELDIDENTIFIERS_HPP_
22 
23 #include "common/DataTypes.hpp"
25 #include "common/logger/Logger.hpp"
26 
27 namespace geos
28 {
33 enum class FieldLocation
34 {
35  Elem,
36  Face,
37  Edge,
38  Node
39 };
40 
41 
46 {
47 public:
48 
55  void addFields( FieldLocation const location, std::vector< string > const & fieldNames )
56  {
57  string key;
58  generateKey( location, key );
59  addFields( fieldNames, key );
60  }
67  template< typename T = std::vector< string > >
68  void addElementFields( std::vector< string > const & fieldNames, T const & regionNames )
69  {
70  for( auto const & regionName : regionNames )
71  {
72  addFields( fieldNames, generateKey( regionName ) );
73  }
74  }
75 
81  std::map< string, array1d< string > > const & getFields() const
82  {
83  return m_fields;
84  }
91  string getRegionName( string const & key ) const
92  {
93  string regionName( key );
94  regionName.erase( 0, std::string( m_locationKeys.elemsKey()).length());
95  return regionName;
96  }
103  void getLocation( string const & key,
104  FieldLocation & location ) const
105  {
106  if( key.find( m_locationKeys.nodesKey() ) != string::npos )
107  {
108  location = FieldLocation::Node;
109  }
110  else if( key.find( m_locationKeys.edgesKey()) != string::npos )
111  {
112  location = FieldLocation::Edge;
113  }
114  else if( key.find( m_locationKeys.facesKey()) != string::npos )
115  {
116  location = FieldLocation::Face;
117  }
118  else if( key.find( m_locationKeys.elemsKey()) != string::npos )
119  {
120  location = FieldLocation::Elem;
121  }
122  else
123  {
124  GEOS_ERROR( GEOS_FMT( "Invalid key, {}, was provided. Location cannot be retrieved.", key ) );
125  }
126  }
127 
128 private:
130  std::map< string, array1d< string > > m_fields;
131 
132  struct keysStruct
133  {
135  static constexpr char const * nodesKey() { return "nodes"; }
137  static constexpr char const * edgesKey() { return "edges"; }
139  static constexpr char const * facesKey() { return "faces"; }
141  static constexpr char const * elemsKey() { return "elems/"; }
142  } m_locationKeys;
143 
150  void generateKey( FieldLocation const location,
151  string & key ) const
152 
153  {
154  switch( location )
155  {
156  case FieldLocation::Node:
157  {
158  key = m_locationKeys.nodesKey();
159  break;
160  }
161  case FieldLocation::Edge:
162  {
163  key = m_locationKeys.edgesKey();
164  break;
165  }
166  case FieldLocation::Face:
167  {
168  key = m_locationKeys.facesKey();
169  break;
170  }
171  case FieldLocation::Elem:
172  {
173  GEOS_ERROR( "An element located field also requires a region name to be specified." );
174  break;
175  }
176  }
177  }
184  string generateKey( string const & regionName ) const
185  {
186  return stringutilities::concat( "", m_locationKeys.elemsKey(), regionName );
187  }
194  void addFields( std::vector< string > const fieldNames, string const key )
195  {
196  for( string const & field : fieldNames )
197  {
198  m_fields[key].emplace_back( field );
199  }
200  }
201 };
202 
203 } /* namespace geos */
204 
205 #endif /* GEOS_MESH_MPICOMMUNICATIONS_FIELDIDENTIFIERS_HPP_ */
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:157
void getLocation(string const &key, FieldLocation &location) const
Get the Location object.
string getRegionName(string const &key) const
Get the Region Name object.
void addFields(FieldLocation const location, std::vector< string > const &fieldNames)
adds fields to the fields map using the location to define a convenient key.
void addElementFields(std::vector< string > const &fieldNames, T const &regionNames)
adds element-based fields to the fields map using the element region names to define keys.
std::map< string, array1d< string > > const & getFields() const
Get the Fields object which is the map containing the fields existing for each location.
std::string string
String type.
Definition: DataTypes.hpp:91
FieldLocation
Enum defining the possible location of a field on the mesh.
@ Node
location is node (like displacements in finite elements)
@ Face
location is face (like flux in mixed finite elements)
@ Elem
location is element (like pressure in finite volumes)
@ Edge
location is edge (like flux between fracture elements)