HermesCommon  3.0
common.h
Go to the documentation of this file.
1 // This file is part of HermesCommon.
2 //
3 // Hermes2D is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 
19 #ifndef __HERMES_COMMON_COMMON_H
20 #define __HERMES_COMMON_COMMON_H
21 
22 #include <complex>
23 #include <vector>
24 
25 #include <stdexcept>
26 #include <cstdarg>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stddef.h>
30 #include <assert.h>
31 #include <math.h>
32 #include <initializer_list>
33 #include <time.h>
34 #include <float.h>
35 #include <climits>
36 #include <limits.h>
37 #include <errno.h>
38 #include <cmath>
39 
40 #include <cstdlib>
41 #include <ctime>
42 #include <cstring>
43 #include <map>
44 #include <cstdio>
45 #include <stdarg.h>
46 #include <sstream>
47 #ifdef _WINDOWS
48 #include <memory>
49 #else
50 #include <tr1/memory>
51 #endif
52 #include <algorithm>
53 #include <limits>
54 #include <map>
55 #include <set>
56 #include <queue>
57 #include <sstream>
58 #include <fstream>
59 #include <cstring>
60 #include <iostream>
61 #include <signal.h>
62 #include <utility>
63 
64 #include "config.h"
65 
66 #ifdef WITH_TC_MALLOC
67 #include "tcmalloc.h"
68 #endif
69 
70 #ifdef WITH_BSON
71 #define MONGO_HAVE_STDINT
72 #include "bson.h"
73 #endif
74 
75 #ifdef WITH_MATIO
76 #include "matio.h"
77 #endif
78 
79 #ifdef WITH_OPENMP
80 #include <omp.h>
81 #else
82 inline int omp_get_max_threads() { return 1; }
83 inline int omp_get_num_threads() { return 1; }
84 inline int omp_get_thread_num() { return 0; }
85 #endif
86 
87 #ifdef WITH_PJLIB
88 #include "pjlib.h"
89 #endif
90 
92 #ifdef JU_WIN
93 typedef __int8 int8_t;
94 typedef __int16 int16_t;
95 typedef __int32 int32_t;
96 typedef __int64 int64_t;
97 
98 typedef unsigned __int8 uint8_t;
99 typedef unsigned __int16 uint16_t;
100 typedef unsigned __int32 uint32_t;
101 typedef unsigned __int64 uint64_t;
102 #else
103 #include <inttypes.h>
104 #endif
105 
106 #ifdef _WINDOWS
107 #define finite _finite
108 #endif
109 
110 typedef int int2[2];
111 typedef int int3[3];
112 typedef int int4[4];
113 typedef int int5[5];
114 
115 typedef double double2[2];
116 typedef double double3[3];
117 typedef double double4[4];
118 typedef double double2x2[2][2];
119 typedef double double2x3[2][3];
120 typedef double double2x4[2][4];
121 typedef double double3x2[3][2];
122 typedef double double3x3[3][3];
123 typedef double double3x4[3][4];
124 
125 typedef float float2[2];
126 typedef float float3[3];
127 typedef float float4[4];
128 typedef float float2x2[2][2];
129 typedef float float2x3[2][3];
130 typedef float float2x4[2][4];
131 typedef float float3x2[3][2];
132 typedef float float3x3[3][3];
133 typedef float float3x4[3][4];
134 
135 template<typename Scalar>
136 class Scalar2
137 {
138  Scalar val[2];
139 
140 public:
141  Scalar2(Scalar v1, Scalar v2)
142  {
143  val[0] = v1; val[1] = v2;
144  }
145 
146  Scalar& operator[] (int idx)
147  {
148  assert(idx >= 0 && idx < 2);
149  return val[idx];
150  }
151 };
152 
153 template<typename Scalar>
154 class Scalar3
155 {
156  Scalar val[3];
157 
158 public:
159  Scalar3(Scalar v1, Scalar v2, Scalar v3)
160  {
161  val[0] = v1; val[1] = v2, val[2] = v3;
162  }
163 
164  Scalar& operator[] (int idx)
165  {
166  assert(idx >= 0 && idx < 3);
167  return val[idx];
168  }
169 };
170 
171 // Pi.
172 #ifndef M_PI
173 #define M_PI 3.14159265358979323846
174 #endif
175 
176 namespace Hermes
177 {
178  const double HermesEpsilon = std::numeric_limits<double>::epsilon() * 10.;
179  const double HermesSqrtEpsilon = std::sqrt(std::numeric_limits<double>::epsilon());
180 
181  inline int sqr(int x) { return x*x; }
182  inline double sqr(double x) { return x*x; }
183  inline double sqrt(double x) { return std::sqrt(x); }
184  inline double sqr(std::complex<double> x) { return std::norm(x); }
185  inline double magn(double x) { return fabs(x); }
186  inline double conj(double a) { return a; }
187  inline double cos(double x) { return std::cos(x); }
188  inline double sin(double x) { return std::sin(x); }
189  inline double atan(double x) { return std::atan(x); }
190  inline double atan2(double x, double y) { return std::atan2(x, y); }
191  inline double abs(double x) { return std::abs(x); }
192  inline double pow(double x, double y) { return std::pow(x, y); }
193  inline double log(double x) { return std::log(x); }
194 
195  /* event codes */
196 #define HERMES_EC_ERROR 'E'
197 #define HERMES_EC_WARNING 'W'
198 #define HERMES_EC_INFO 'I'
199 
200 #define HERMES_LOG_FILE_DELIM_SIZE 80
202 #define BUF_SZ 2048
203 
204  /* function name */
207 #ifdef _WINDOWS
208 # ifdef __MINGW32__ //MinGW
209 # define __CURRENT_FUNCTION __func__
210 # else //MSVC and other compilers
211 # define __CURRENT_FUNCTION __FUNCTION__
212 # endif
213 #else //Linux and Mac
214 # define __CURRENT_FUNCTION __PRETTY_FUNCTION__
215 #endif
216 
217  // Represents "any" part of the boundary when deciding where (on which elements) to assemble the form at hand.
218  const std::string HERMES_ANY = "-1234";
219  // For internal use.
220  const int HERMES_ANY_INT = -1234;
221 
222  namespace Exceptions
223  {
224  class NullException;
225  class LengthException;
226  }
227 
228  namespace Helpers
229  {
231  template<typename T>
232  inline void check_for_null(T instance)
233  {
234  if (!instance)
236  }
237 
239  template<typename T>
240  inline void check_length(const std::vector<T>& instance, const unsigned int length)
241  {
242  if (instance.size() != length)
243  throw Hermes::Exceptions::LengthException(instance.size(), length);
244  }
245 
247  template<typename T, typename U>
248  inline void check_length(const std::vector<T>& instanceT, const std::vector<U>& instanceU)
249  {
250  if (instanceT.size() != instanceU.size())
252  }
253 
255  inline void fprint_num(FILE*f, double x, char* number_format)
256  {
257  fprintf(f, number_format, x);
258  }
259 
260  inline void fprint_num(FILE*f, std::complex<double> x, char* number_format)
261  {
262  std::stringstream ss;
263  ss << "(" << number_format << ", " << number_format << ")";
264  fprintf(f, ss.str().c_str(), x.real(), x.imag());
265  }
266 
267  template<typename Scalar>
268  inline void fprint_coordinate_num(FILE*f, int i, int j, Scalar x, char* number_format)
269  {
270  fprintf(f, "%d %d ", i, j);
271  fprint_num(f, x, number_format);
272  }
273 
275  template<typename Scalar>
276  struct TypeIsReal
277  {
278  static const bool value = false;
279  };
280 
281  template<typename Scalar>
283  {
284  static const bool value = false;
285  };
286 
287  template<>
288  struct TypeIsReal < double >
289  {
290  static const bool value = true;
291  };
292 
293  template<>
294  struct TypeIsComplex < std::complex<double> >
295  {
296  static const bool value = true;
297  };
298  }
299 
300  namespace BLAS
301  {
302 #ifdef WITH_BLAS
303  // BLAS-related functions
304 
305  // Complex part.
306 #ifdef __cplusplus
307  extern "C"
308  {
309 #endif
310  extern int zscal_(int *, std::complex<double> *, std::complex<double> *, int *);
311  extern int zaxpy_(int *, std::complex<double> *, std::complex<double> *, int *, std::complex<double> *, int *);
312  extern int zcopy_(int *, std::complex<double> *, int *, std::complex<double> *, int *);
313 
314  extern int dscal_(int *, double *, double *, int *);
315  extern int daxpy_(int *, double *, double *, int *, double *, int *);
316  extern int dcopy_(int *, double *, int *, double *, int *);
317 
318 #ifdef __cplusplus
319  }
320 #endif
321 
323  inline void blas_scal(int n, std::complex<double> alpha, std::complex<double> *x, int incx) { zscal_(&n, &alpha, x, &incx); }
325  inline void blas_axpy(int n, std::complex<double> alpha, std::complex<double> *x, int incx, std::complex<double> *y, int incy) { zaxpy_(&n, &alpha, x, &incx, y, &incy); }
327  inline void blas_copy(int n, std::complex<double> *x, int incx, std::complex<double> *y, int incy) { zcopy_(&n, x, &incx, y, &incx); }
328 
330  inline void blas_scal(int n, double alpha, double *x, int incx) { dscal_(&n, &alpha, x, &incx); }
332  inline void blas_axpy(int n, double alpha, double *x, int incx, double *y, int incy) { daxpy_(&n, &alpha, x, &incx, y, &incy); }
334  inline void blas_copy(int n, double *x, int incx, double *y, int incy) { dcopy_(&n, x, &incx, y, &incx); }
335 
336 #endif
337  }
338 }
339 #endif
340 
General namespace for the Hermes library.
Parameter length parameter exception. Internal. Exception occurs when some parameter has wrong length...
Definition: exceptions.h:126
Definition: compat.h:85
Type deductors.
Definition: common.h:276
Null parameter exception. Internal. Exception occurs when some parameter is Null or empty and it shou...
Definition: exceptions.h:101
int int2[2]
Int types handling.
Definition: common.h:110