PAPRECA hybrid off-lattice kMC/MD simulator  2.0.0 (17 September 2024)
utilities.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------------------
2 PAPRECA hybrid off-lattice kinetic Monte Carlo/Molecular dynamics simulator.
3 Copyright (C) 2024 Stavros Ntioudis, James P. Ewen, Daniele Dini, and C. Heath Turner
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 ----------------------------------------------------------------------------------------*/
21 
22 #ifndef UTILITIES_H
23 #define UTILITIES_H
24 
25 //system Headers
26 #include <iostream>
27 #include <unordered_set>
28 #include <unordered_map>
29 #include <array>
30 #include <vector>
31 #include <string>
32 #include <algorithm>
33 #include <cctype>
34 #include <mpi.h>
35 
36 //LAMMPS headers
37 #include "pointers.h"
38 
39 //kMC headers
40 #include "papreca_error.h"
41 
42 namespace PAPRECA{
43 
44  //Functions with template are included ALONG WITH THEIR IMPLEMENTATION in the .h file to avoid undefined reference compiler errors. See: https://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor
45 
46  //Array/Vector functions
47  double doubleArrSum( double *arr , const int &size );
48  void copyDoubleArray3D( double *copy , const double *source , const int start = 0 , const int end = 3 );
49 
50  template< typename T >
51  T getSumOfVecElements( const std::vector< T > &vec ){
52 
57 
58  T sum = T( ); //Initialize sum to zero for numeric types
59 
60  for( const T &element : vec ){
61  sum += element;
62 
63  }
64 
65  return sum;
66 
67  }
68 
69  template < typename T , size_t N >
70  void pushArrayToVector( const T *arr , std::vector<T> &vec ) {
71 
76 
77  for( size_t i = 0; i < N; ++i ) {
78  vec.push_back( arr[i] );
79  }
80 
81  }
82 
83  template <typename T>
84  std::vector< T > getSubVectorFromVector( const std::vector< T > &vec , size_t start , size_t end ) {
85 
91 
92  if( start > vec.size() || end > vec.size() || start > end ) {
93  allAbortWithMessage( MPI_COMM_WORLD , "Invalid range used in getSubVectorFromVector in utilities.h." );
94  return { };
95  }
96 
97  std::vector< T > sub_vector( vec.begin( ) + start , vec.begin( ) + end );
98  return sub_vector;
99  }
100 
101  //Elements in unordered set
102  template < typename T , typename Hash >
103  bool elementIsInUnorderedSet( const std::unordered_set< T , Hash > &set , const T &element ){
104 
110 
111  return set.count( element ) > 0;
112 
113  }
114 
115  //Elements in unordered map
116  template< typename KeyType , typename ValueType , typename Hash >
117  bool mappingExists( const std::unordered_map< KeyType , ValueType , Hash > &map, const KeyType &key ) {
118 
124 
125  return map.count( key ) > 0;
126  }
127 
128 
129  //Elements in vector.
130  template < typename T >
131  bool elementIsInVector( const std::vector<T> &vec , const T &element) {
132 
138 
139  return std::find( vec.begin( ), vec.end( ), element ) != vec.end();
140 
141  }
142 
143 
144  //String management
145  const std::string getConcatenatedStringFromStringsVector( const std::vector< std::string > &strings , size_t start , size_t end );
146  const std::string getConcatenatedStringWithSpacesFromStringsVector( const std::vector< std::string > &strings , size_t start , size_t end );
147  const int getStringPosInStringVec( const std::string &string2check , const std::vector< std::string > &strings );
148 
149  bool stringIsNumber( const std::string &string );
150  bool stringIsIntNumber( const std::string &string );
151  const bool stringIsBool( const std::string &string );
152  const unsigned long int string2UnsignedLongInt( std::string &string );
153  const int string2Int( std::string &string );
154  const double string2Double( std::string &string );
155  const bool string2Bool( std::string &string );
156  const int boolString2Int( std::string &string );
157 
158  struct PairHash{
159 
163 
164  inline std::size_t operator()(const std::pair<int,int> & v) const {
165  return v.first*31+v.second;
166  }
167 
168  };
169 
170 
171  //Note 1: It is faster to search for a pair in a set and then map it (if it exists) than searching directly on a map. This is, of course, assuming that no RAM bottleneck exists (of course, storing both a map and an unordered set occupies more memory).
172  //Note 2: The unordered map gives you a pointer to a (parent) PredefinedReaction object. Use it as it is for bond breaking events, however, cast it to a (child) PredefinedBondForm object if necessary.
173  //typedefs for identifying predefined events easily.
174 
175  typedef std::pair< int , int > INT_PAIR;
176  typedef std::unordered_set< INT_PAIR , PairHash > PAIR_SET;
177 
178  typedef std::unordered_map< INT_PAIR , double , PairHash > INTPAIR2DOUBLE_MAP;
179  typedef std::unordered_map< INT_PAIR , int , PairHash > INTPAIR2INT_MAP;
180 
181  typedef std::unordered_map< int , int > INT2INT_MAP;
182  typedef std::unordered_map< int , INT2INT_MAP > INT2INTSMAP_MAP;
183  typedef std::unordered_set< int > INT_SET;
184 
185  typedef std::unordered_set< LAMMPS_NS::tagint > TAGINT_SET;
186 
187  //Typedef for modern c++ containers of 3xdouble arrays
188  typedef std::array< double , 3 > ARRAY3D;
189 
190  //Typedef for proc event selection
191  typedef std::pair< double , int > DOUBLE2INTPAIR;
192  typedef std::vector< DOUBLE2INTPAIR > DOUBLE2INTPAIR_VEC;
193 
194 }//End of namespace PAPRECA
195 
196 
197 
198 
199 
200 
201 
202 #endif
Definition: bond.cpp:26
bool mappingExists(const std::unordered_map< KeyType, ValueType, Hash > &map, const KeyType &key)
Definition: utilities.h:117
const double string2Double(std::string &string)
Definition: utilities.cpp:246
std::unordered_map< int, INT2INT_MAP > INT2INTSMAP_MAP
Definition: utilities.h:182
const bool stringIsBool(const std::string &string)
Definition: utilities.cpp:190
const unsigned long int string2UnsignedLongInt(std::string &string)
Definition: utilities.cpp:200
std::unordered_set< int > INT_SET
Definition: utilities.h:183
double doubleArrSum(double *arr, const int &size)
Definition: utilities.cpp:27
std::unordered_set< INT_PAIR, PairHash > PAIR_SET
Definition: utilities.h:176
const int getStringPosInStringVec(const std::string &string2check, const std::vector< std::string > &strings)
Definition: utilities.cpp:141
bool stringIsIntNumber(const std::string &string)
Definition: utilities.cpp:175
std::unordered_map< INT_PAIR, int, PairHash > INTPAIR2INT_MAP
Definition: utilities.h:179
const int boolString2Int(std::string &string)
Definition: utilities.cpp:289
void pushArrayToVector(const T *arr, std::vector< T > &vec)
Definition: utilities.h:70
std::vector< DOUBLE2INTPAIR > DOUBLE2INTPAIR_VEC
Definition: utilities.h:192
std::unordered_set< LAMMPS_NS::tagint > TAGINT_SET
Definition: utilities.h:185
const bool string2Bool(std::string &string)
Definition: utilities.cpp:269
bool elementIsInVector(const std::vector< T > &vec, const T &element)
Definition: utilities.h:131
const std::string getConcatenatedStringWithSpacesFromStringsVector(const std::vector< std::string > &strings, size_t start=-1, size_t end=-1)
Definition: utilities.cpp:101
void copyDoubleArray3D(double *copy, const double *source, const int start, const int end)
Definition: utilities.cpp:46
std::unordered_map< int, int > INT2INT_MAP
Definition: utilities.h:181
std::pair< double, int > DOUBLE2INTPAIR
Definition: utilities.h:191
bool elementIsInUnorderedSet(const std::unordered_set< T, Hash > &set, const T &element)
Definition: utilities.h:103
const std::string getConcatenatedStringFromStringsVector(const std::vector< std::string > &strings, size_t start=-1, size_t end=-1)
Definition: utilities.cpp:64
std::pair< int, int > INT_PAIR
Definition: utilities.h:175
std::vector< T > getSubVectorFromVector(const std::vector< T > &vec, size_t start, size_t end)
Definition: utilities.h:84
std::unordered_map< INT_PAIR, double, PairHash > INTPAIR2DOUBLE_MAP
Definition: utilities.h:178
const int string2Int(std::string &string)
Definition: utilities.cpp:223
bool stringIsNumber(const std::string &string)
Definition: utilities.cpp:161
void allAbortWithMessage(MPI_Comm communicator, const std::string &message)
Definition: papreca_error.cpp:62
T getSumOfVecElements(const std::vector< T > &vec)
Definition: utilities.h:51
std::array< double, 3 > ARRAY3D
Definition: utilities.h:188
Functions that enable the communication of error/warning messages to the terminal and coordinate the ...
Utility hash function (struct) for custom typedefs.
Definition: utilities.h:158
std::size_t operator()(const std::pair< int, int > &v) const
Definition: utilities.h:164