HermesCommon  2.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 
24 #include <stdexcept>
25 #include <cstdarg>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <assert.h>
30 #include <math.h>
31 #include <time.h>
32 #include <float.h>
33 #include <errno.h>
34 #include <cmath>
35 
36 #include <pthread.h>
37 #include <cstdlib>
38 #include <ctime>
39 #include <cstring>
40 #include <map>
41 #include <cstdio>
42 #include <stdarg.h>
43 #include <sstream>
44 
45 #include <algorithm>
46 #include <vector>
47 #include <map>
48 #include <set>
49 #include <queue>
50 #include <sstream>
51 #include <fstream>
52 #include <cstring>
53 #include <iostream>
54 #include <omp.h>
55 #include <signal.h>
56 
57 #include "config.h"
58 
59 typedef int int2[2];
60 typedef int int3[3];
61 typedef int int4[4];
62 typedef int int5[5];
63 
64 typedef double double2[2];
65 typedef double double3[3];
66 typedef double double4[4];
67 typedef double double2x2[2][2];
68 typedef double double3x2[3][2];
69 typedef double double3x3[3][3];
70 
71 template<typename Scalar>
72 class Scalar2
73 {
74  Scalar val[2];
75 
76 public:
77  Scalar2(Scalar v1, Scalar v2)
78  {
79  val[0] = v1; val[1] = v2;
80  }
81 
82  Scalar& operator[] (int idx)
83  {
84  assert(idx >= 0 && idx < 2);
85  return val[idx];
86  }
87 };
88 
89 template<typename Scalar>
90 class Scalar3
91 {
92  Scalar val[3];
93 
94 public:
95  Scalar3(Scalar v1, Scalar v2, Scalar v3)
96  {
97  val[0] = v1; val[1] = v2, val[2] = v3;
98  }
99 
100  Scalar& operator[] (int idx)
101  {
102  assert(idx >= 0 && idx < 3);
103  return val[idx];
104  }
105 };
106 
108 #ifdef JU_WIN
109 typedef __int8 int8_t;
110 typedef __int16 int16_t;
111 typedef __int32 int32_t;
112 typedef __int64 int64_t;
113 
114 typedef unsigned __int8 uint8_t;
115 typedef unsigned __int16 uint16_t;
116 typedef unsigned __int32 uint32_t;
117 typedef unsigned __int64 uint64_t;
118 #else
119 #include <inttypes.h>
120 #endif
121 
122 // Pi.
123 #ifndef M_PI
124 #define M_PI 3.14159265358979323846
125 #endif
126 
127 namespace Hermes
128 {
129  inline double sqr(int x) { return x*x; }
130  inline double sqr(double x) { return x*x; }
131  inline double sqrt(double x) { return std::sqrt(x); }
132  inline double sqr(std::complex<double> x) { return std::norm(x); }
133  inline double magn(double x) { return fabs(x); }
134  inline double conj(double a) { return a; }
135  inline double cos(double x) { return std::cos(x); }
136  inline double sin(double x) { return std::sin(x); }
137  inline double atan(double x) { return std::atan(x); }
138  inline double atan2(double x, double y) { return std::atan2(x, y); }
139  inline double abs(double x) { return std::abs(x); }
140  inline double pow(double x, double y) { return std::pow(x, y); }
141  inline double log(double x) { return std::log(x); }
142 
143  /* log file */
144  #undef HERMES_LOG_FILE
145  #ifdef HERMES_REPORT_NO_FILE
146  # define HERMES_LOG_FILE NULL
147  #else
148  # ifdef HERMES_REPORT_FILE
149  # define HERMES_LOG_FILE HERMES_REPORT_FILE
150  # else
151  # ifndef HERMES_TEST
152  # define HERMES_LOG_FILE "hermes.log" // default filename for a library
153  # else
154  # define HERMES_LOG_FILE "test.log" // default filename for a library test
155  # endif
156  # endif
157  # endif
158 
159  /* event codes */
160  #define HERMES_EC_ERROR 'E'
161  #define HERMES_EC_WARNING 'W'
162  #define HERMES_EC_INFO 'I'
163 
164 
165  #define HERMES_LOG_FILE_DELIM_SIZE 80
166  #define BUF_SZ 2048
167 
168  /* function name */
171  #ifdef _WIN32 //Win32
172  # ifdef __MINGW32__ //MinGW
173  # define __CURRENT_FUNCTION __func__
174  # else //MSVC and other compilers
175  # define __CURRENT_FUNCTION __FUNCTION__
176  # endif
177  #else //Linux and Mac
178  # define __CURRENT_FUNCTION __PRETTY_FUNCTION__
179  #endif
180 
181  // Represents "any" part of the boundary when deciding where (on which elements) to assemble the form at hand.
182  const std::string HERMES_ANY = "-1234";
183  // For internal use.
184  const int HERMES_ANY_INT = -1234;
185 
186  namespace Helpers
187  {
188  inline void fprint_num(FILE*f, double x, char* number_format)
189  {
190  fprintf(f, number_format, x);
191  }
192 
193  inline void fprint_num(FILE*f, std::complex<double> x, char* number_format)
194  {
195  char* number_formatComplex = new char[20];
196  sprintf(number_formatComplex, "(%s, %s)", number_format, number_format);
197  fprintf(f, number_formatComplex, x.real(), x.imag());
198  }
199  }
200 
201  namespace BLAS
202  {
203 #ifdef WITH_BLAS
204  // BLAS-related functions
205 
206  // Complex part.
207 #ifdef __cplusplus
208  extern "C"
209  {
210 #endif
211  extern int zscal_(int *, std::complex<double> *, std::complex<double> *, int *);
212  extern int zaxpy_(int *, std::complex<double> *, std::complex<double> *, int *, std::complex<double> *, int *);
213  extern int zcopy_(int *, std::complex<double> *, int *, std::complex<double> *, int *);
214 
215  extern int dscal_(int *, double *, double *, int *);
216  extern int daxpy_(int *, double *, double *, int *, double *, int *);
217  extern int dcopy_(int *, double *, int *, double *, int *);
218 
219 #ifdef __cplusplus
220  }
221 #endif
222 
224  inline void blas_scal(int n, std::complex<double> alpha, std::complex<double> *x, int incx) { zscal_(&n, &alpha, x, &incx); }
226  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); }
228  inline void blas_copy(int n, std::complex<double> *x, int incx, std::complex<double> *y, int incy) { zcopy_(&n, x, &incx, y, &incx); }
229 
231  inline void blas_scal(int n, double alpha, double *x, int incx) { dscal_(&n, &alpha, x, &incx); }
233  inline void blas_axpy(int n, double alpha, double *x, int incx, double *y, int incy) { daxpy_(&n, &alpha, x, &incx, y, &incy); }
235  inline void blas_copy(int n, double *x, int incx, double *y, int incy) { dcopy_(&n, x, &incx, y, &incx); }
236 
237 #endif
238  }
239 }
240 #endif
241