v0.6.9
Common.hpp
Go to the documentation of this file.
1 /** \file Common.hpp
2  * \brief Basic structures and data
3  *
4  * MoFEM is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or (at your
7  * option) any later version.
8  *
9  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12  * License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>
16 */
17 
18 #ifndef __COMMON_HPP__
19 #define __COMMON_HPP__
20 
21 namespace MoFEM {
22 
23 /**
24  * \brief Exception to catch
25  */
26 struct MoFEMException : public std::exception {
27  const int errorCode;
28  char errorMessage[255];
29  MoFEMException(const MoFEMErrorCodes error_code) : errorCode(error_code) {
30  strcpy(errorMessage, "Huston we have a problem, something is wrong");
31  }
32  MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
33  : errorCode(error_code) {
34  strcpy(errorMessage, error_message);
35  }
36  const char *what() const throw() { return errorMessage; }
37 protected:
38  MoFEMException(const int error_code) : errorCode(error_code) {
39  strcpy(errorMessage, "Huston we have a problem, something is wrong");
40  }
41 };
42 
44  const int lINE;
45  MoFEMExceptionRepeat(const int error_code, const int line)
46  : MoFEMException(error_code), lINE(line) {
47  strcpy(errorMessage, " ");
48  }
49 };
50 
52  MoFEMExceptionInitial(const int error_code, const char error_message[],
53  const int line)
54  : MoFEMExceptionRepeat(error_code, line) {
55  strcpy(errorMessage, error_message);
56  }
57 };
58 
59 typedef moab::ErrorCode MoABErrorCode; ///< MoAB error code
60 typedef PetscErrorCode MoFEMErrorCode; ///< MoFEM/PETSc error code
61 
62 template <typename TYPE> struct MoFEMErrorCodeGeneric {
63  MoFEMErrorCodeGeneric(const TYPE) {}
64 };
65 
66 template <> struct MoFEMErrorCodeGeneric<PetscErrorCode> {
67  PetscErrorCode iERR;
68  MoFEMErrorCodeGeneric(const PetscErrorCode ierr) : iERR(ierr) {}
69  inline operator PetscErrorCode() const { return iERR; }
70 };
71 
72 template <> struct MoFEMErrorCodeGeneric<moab::ErrorCode> {
73  moab::ErrorCode rVAL;
74  MoFEMErrorCodeGeneric(const moab::ErrorCode rval) : rVAL(rval) {}
75  inline operator moab::ErrorCode() const { return rVAL; }
76 };
77 
82 
83 /**
84  * \brief Error check for inline function check.
85  *
86  * This class is not used directly, it is called in CHKERR. In case of the error
87  * pass line number and that is catch at the end of the function. Information is
88  * enriched by function name and file name. Then error is pushed to PETSc error
89  * stack.
90  *
91  * \note This class has no variables and line number is set at compilation.
92  * Adding variables to this function will reduce efficiency of the code. Do
93  * not do that.
94  *
95  */
96 template <int LINE> struct ErrorCheckerCode {
97 
98  /**
99  * @brief Operator for handling PetscErrorCode and MoFEMErrorCode
100  *
101  */
102  inline void operator<<(const MoFEMErrorCode err) {
103  if (PetscUnlikely(err)) {
104  throw MoFEMExceptionRepeat(err, LINE);
105  }
106  return;
107  }
108 
109  /**
110  * @brief Operator for handling moab::ErrorCode
111  *
112  */
113  inline void operator<<(const moab::ErrorCode err) {
114  if (PetscLikely(MB_SUCCESS != err)) {
115  std::string error_str = (unsigned)err <= (unsigned)MB_FAILURE
116  ? moab::ErrorCodeStr[err]
117  : "INVALID ERROR CODE";
118  std::string str("MOAB error (" + boost::lexical_cast<std::string>(err) +
119  ") " + error_str);
120  throw MoFEMExceptionInitial(MOFEM_MOAB_ERROR, str.c_str(), LINE);
121  }
122  return;
123  }
124 };
125 
126 typedef int DofIdx; ///< Index of DOF
127 typedef int FEIdx; ///< Index of the element
128 typedef int EntIdx; ///< Index of DOF on the entity
129 typedef int EntPart; ///< Partition owning entity
130 typedef double FieldData; ///< Field data type
131 typedef int ApproximationOrder; ///< Approximation on the entity
132 typedef int FieldCoefficientsNumber; ///< Number of field coefficients
134 
135 // typedef checked_uint128_t UId;
136 typedef uint128_t UId; ///< Unique Id
137 typedef int ShortId; ///< Unique Id in the field
138 
139 #define UID_DOF_MAK 0x1FF
140 
141 typedef std::bitset<BITREFEDGES_SIZE> BitRefEdges;
142 
143 /**
144  * \brief Bit structure attached to each entity identifying to what mesh entity
145  * is attached.
146  */
147 typedef std::bitset<BITREFLEVEL_SIZE> BitRefLevel;
148 
149 typedef std::bitset<BITFIELDID_SIZE> BitFieldId;
150 typedef std::bitset<BITFEID_SIZE> BitFEId;
151 typedef std::bitset<BITPROBLEMID_SIZE> BitProblemId;
152 typedef std::bitset<BITINTERFACEUID_SIZE> BitIntefaceId;
153 
154 // AUX STRUCTURES
155 
156 /* This small utility that cascades two key extractors will be
157 * used throughout the boost example
158 * http://www.boost.org/doc/libs/1_53_0/libs/multi_index/example/complex_structs.cpp
159 */
160 template <class KeyExtractor1, class KeyExtractor2> struct KeyFromKey {
161 public:
162  typedef typename KeyExtractor1::result_type result_type;
163 
164  KeyFromKey(const KeyExtractor1 &key1_ = KeyExtractor1(),
165  const KeyExtractor2 &key2_ = KeyExtractor2())
166  : key1(key1_), key2(key2_) {}
167 
168  template <typename Arg> result_type operator()(Arg &arg) const {
169  return key1(key2(arg));
170  }
171 
172 private:
173  KeyExtractor1 key1;
174  KeyExtractor2 key2;
175 };
176 
177 template <typename id_type> struct LtBit {
178  inline bool operator()(const id_type &valueA, const id_type &valueB) const {
179  return valueA.to_ulong() < valueB.to_ulong();
180  }
181 };
182 
183 template <typename id_type> struct EqBit {
184  inline bool operator()(const id_type &valueA, const id_type &valueB) const {
185  return valueA.to_ulong() == valueB.to_ulong();
186  }
187 };
188 
189 template <typename id_type> struct HashBit {
190  inline unsigned int operator()(const id_type &value) const {
191  return value.to_ulong();
192  }
193 };
194 
195 /**
196 * \typedef CubitBCType
197 * bc & material meshsets
198 *
199 */
200 typedef std::bitset<32> CubitBCType;
201 
202 // array with std allocators (i.e. concept of capacity is useful here)
203 // typedef ublas::unbounded_array<int,std::allocator<int> > IntAllocator;
204 // typedef ublas::unbounded_array<double,std::allocator<double> >
205 // DoubleAllocator;
206 typedef std::vector<int, std::allocator<int> > IntAllocator;
207 typedef std::vector<double, std::allocator<double> > DoubleAllocator;
208 DEPRECATED typedef IntAllocator IntAllacator; ///< \deprecated Do not use spelling mistake
209 DEPRECATED typedef DoubleAllocator DoubleAllacator; ///< \deprecated Do not use spelling mistake
210 typedef ublas::vector<int, IntAllocator> VectorInt;
211 typedef ublas::vector<double, DoubleAllocator> VectorDouble;
212 typedef ublas::matrix<double, ublas::row_major, DoubleAllocator> MatrixDouble;
213 
214 // bounded vector & matrices
215 typedef ublas::vector<int, ublas::bounded_array<int, 3> > VectorInt3;
216 typedef ublas::vector<int, ublas::bounded_array<int, 9> > VectorInt9;
217 typedef ublas::matrix<double, ublas::row_major, ublas::bounded_array<double, 9> >
219 typedef ublas::vector<double, ublas::bounded_array<double, 3> > VectorDouble3;
220 typedef ublas::vector<double, ublas::bounded_array<double, 9> > VectorDouble9;
221 
222 // shallow adaptor classes
223 typedef ublas::vector<double, ublas::shallow_array_adaptor<double> >
225 typedef ublas::matrix<double, ublas::row_major,
226  ublas::shallow_array_adaptor<double> >
228 typedef ublas::vector<int, ublas::shallow_array_adaptor<int> > VectorIntAdaptor;
229 
230 typedef std::vector<boost::shared_ptr<MatrixDouble> > ShapeFunctionBasesVector;
231 
232 template <class X> inline std::string toString(X x) {
233  std::ostringstream buffer;
234  buffer << x;
235  return buffer.str();
236 }
237 }
238 
239 #endif //__COMMON_HPP__
240 
241 /***************************************************************************//**
242  * \defgroup mofem MoFEM
243  ******************************************************************************/
int ShortId
Unique Id in the field.
Definition: Common.hpp:137
int FieldCoefficientsNumber
Number of field coefficients.
Definition: Common.hpp:132
ublas::vector< double, ublas::bounded_array< double, 3 > > VectorDouble3
Definition: Common.hpp:219
ublas::vector< double, ublas::bounded_array< double, 9 > > VectorDouble9
Definition: Common.hpp:220
ublas::vector< double, ublas::shallow_array_adaptor< double > > VectorAdaptor
Definition: Common.hpp:224
moab::ErrorCode MoABErrorCode
MoAB error code.
Definition: Common.hpp:59
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Common.hpp:60
ublas::vector< int, ublas::shallow_array_adaptor< int > > VectorIntAdaptor
Definition: Common.hpp:228
std::bitset< BITPROBLEMID_SIZE > BitProblemId
Definition: Common.hpp:151
double FieldData
Field data type.
Definition: Common.hpp:130
Error check for inline function check.
Definition: Common.hpp:96
int EntPart
Partition owning entity.
Definition: Common.hpp:129
const EntityHandle no_handle
Definition: Common.hpp:133
std::bitset< BITREFEDGES_SIZE > BitRefEdges
Definition: Common.hpp:141
std::bitset< BITFEID_SIZE > BitFEId
Definition: Common.hpp:150
KeyFromKey(const KeyExtractor1 &key1_=KeyExtractor1(), const KeyExtractor2 &key2_=KeyExtractor2())
Definition: Common.hpp:164
Exception to catch.
Definition: Common.hpp:26
std::bitset< BITFIELDID_SIZE > BitFieldId
Definition: Common.hpp:149
ublas::matrix< double, ublas::row_major, ublas::shallow_array_adaptor< double > > MatrixAdaptor
Definition: Common.hpp:227
int EntIdx
Index of DOF on the entity.
Definition: Common.hpp:128
uint128_t UId
Unique Id.
Definition: Common.hpp:136
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
KeyExtractor1 key1
Definition: Common.hpp:173
std::vector< double, std::allocator< double > > DoubleAllocator
Definition: Common.hpp:207
MoFEMErrorCodeGeneric(const PetscErrorCode ierr)
Definition: Common.hpp:68
bool operator()(const id_type &valueA, const id_type &valueB) const
Definition: Common.hpp:184
char errorMessage[255]
Definition: Common.hpp:28
std::bitset< 32 > CubitBCType
Definition: Common.hpp:200
KeyExtractor2 key2
Definition: Common.hpp:174
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition: Common.hpp:147
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Common.hpp:80
ublas::vector< int, ublas::bounded_array< int, 9 > > VectorInt9
Definition: Common.hpp:216
unsigned int operator()(const id_type &value) const
Definition: Common.hpp:190
int FEIdx
Index of the element.
Definition: Common.hpp:127
ublas::vector< int, IntAllocator > VectorInt
Definition: Common.hpp:210
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
Definition: Common.hpp:78
ublas::matrix< double, ublas::row_major, ublas::bounded_array< double, 9 > > MatrixDouble3by3
Definition: Common.hpp:218
ublas::matrix< double, ublas::row_major, DoubleAllocator > MatrixDouble
Definition: Common.hpp:212
MoFEMErrorCodeGeneric(const TYPE)
Definition: Common.hpp:63
ublas::vector< double, DoubleAllocator > VectorDouble
Definition: Common.hpp:211
MoFEMExceptionInitial(const int error_code, const char error_message[], const int line)
Definition: Common.hpp:52
const int errorCode
Definition: Common.hpp:27
std::string toString(X x)
Definition: Common.hpp:232
bool operator()(const id_type &valueA, const id_type &valueB) const
Definition: Common.hpp:178
const char * what() const
Definition: Common.hpp:36
MoFEMException(const int error_code)
Definition: Common.hpp:38
ublas::vector< int, ublas::bounded_array< int, 3 > > VectorInt3
Definition: Common.hpp:215
int ApproximationOrder
Approximation on the entity.
Definition: Common.hpp:131
MoFEMExceptionRepeat(const int error_code, const int line)
Definition: Common.hpp:45
DEPRECATED typedef DoubleAllocator DoubleAllacator
Definition: Common.hpp:209
#define DEPRECATED
Definition: definitions.h:29
std::vector< int, std::allocator< int > > IntAllocator
Definition: Common.hpp:206
result_type operator()(Arg &arg) const
Definition: Common.hpp:168
std::bitset< BITINTERFACEUID_SIZE > BitIntefaceId
Definition: Common.hpp:152
MoFEMErrorCodes
Error handling.
Definition: definitions.h:109
int DofIdx
Index of DOF.
Definition: Common.hpp:126
KeyExtractor1::result_type result_type
Definition: Common.hpp:162
std::vector< boost::shared_ptr< MatrixDouble > > ShapeFunctionBasesVector
Definition: Common.hpp:230
MoFEMException(const MoFEMErrorCodes error_code)
Definition: Common.hpp:29
MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
Definition: Common.hpp:32
void operator<<(const MoFEMErrorCode err)
Operator for handling PetscErrorCode and MoFEMErrorCode.
Definition: Common.hpp:102
DEPRECATED typedef IntAllocator IntAllacator
Definition: Common.hpp:208
void operator<<(const moab::ErrorCode err)
Operator for handling moab::ErrorCode.
Definition: Common.hpp:113
MoFEMErrorCodeGeneric(const moab::ErrorCode rval)
Definition: Common.hpp:74