HermesCommon 1.0
common.h
Go to the documentation of this file.
00001 // This file is part of HermesCommon.
00002 //
00003 // Hermes2D is free software: you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation, either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // Hermes2D is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with Hermes2D.  If not, see <http://www.gnu.org/licenses/>.
00015 
00019 #ifndef __HERMES_COMMON_COMMON_H
00020 #define __HERMES_COMMON_COMMON_H
00021 
00022 #include <complex>
00023 
00024 #include <stdexcept>
00025 #include <cstdarg>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <stddef.h>
00029 #include <assert.h>
00030 #include <math.h>
00031 #include <time.h>
00032 #include <float.h>
00033 #include <errno.h>
00034 #include <cmath>
00035 
00036 #include <pthread.h>
00037 #include <cstdlib>
00038 #include <ctime>
00039 #include <cstring>
00040 #include <map>
00041 #include <cstdio>
00042 #include <stdarg.h>
00043 #include <sstream>
00044 
00045 #include <algorithm>
00046 #include <vector>
00047 #include <map>
00048 #include <set>
00049 #include <queue>
00050 #include <sstream>
00051 #include <fstream>
00052 #include <cstring>
00053 #include <iostream>
00054 #include <omp.h>
00055 #include <signal.h>
00056 
00057 #include "config.h"
00058 
00059 typedef int int2[2];
00060 typedef int int3[3];
00061 typedef int int4[4];
00062 typedef int int5[5];
00063 
00064 typedef double double2[2];
00065 typedef double double3[3];
00066 typedef double double4[4];
00067 typedef double double2x2[2][2];
00068 typedef double double3x2[3][2];
00069 typedef double double3x3[3][3];
00070 
00071 template<typename Scalar>
00072 class Scalar2
00073 {
00074   Scalar val[2];
00075 
00076 public:
00077   Scalar2(Scalar v1, Scalar v2)
00078   {
00079     val[0] = v1; val[1] = v2;
00080   }
00081 
00082   Scalar& operator[] (int idx)
00083   {
00084     assert(idx >= 0 && idx < 2);
00085     return val[idx];
00086   }
00087 };
00088 
00089 template<typename Scalar>
00090 class Scalar3
00091 {
00092   Scalar val[3];
00093 
00094 public:
00095   Scalar3(Scalar v1, Scalar v2, Scalar v3)
00096   {
00097     val[0] = v1; val[1] = v2, val[2] = v3;
00098   }
00099 
00100   Scalar& operator[] (int idx)
00101   {
00102     assert(idx >= 0 && idx < 3);
00103     return val[idx];
00104   }
00105 };
00106 
00108 #ifdef JU_WIN
00109 typedef __int8           int8_t;
00110 typedef __int16          int16_t;
00111 typedef __int32          int32_t;
00112 typedef __int64          int64_t;
00113 
00114 typedef unsigned __int8  uint8_t;
00115 typedef unsigned __int16 uint16_t;
00116 typedef unsigned __int32 uint32_t;
00117 typedef unsigned __int64 uint64_t;
00118 #else
00119 #include <inttypes.h>
00120 #endif
00121 
00122 // Pi.
00123 #ifndef M_PI
00124 #define M_PI           3.14159265358979323846
00125 #endif
00126 
00127 namespace Hermes
00128 {
00129   inline double sqr(int x) { return x*x; }
00130   inline double sqr(double x) { return x*x; }
00131   inline double sqrt(double x) { return std::sqrt(x); }
00132   inline double sqr(std::complex<double> x)   { return std::norm(x); }
00133   inline double magn(double x) { return fabs(x); }
00134   inline double conj(double a) {  return a; }
00135   inline double cos(double x) { return std::cos(x); }
00136   inline double sin(double x) { return std::sin(x); }
00137   inline double atan(double x) { return std::atan(x); }
00138   inline double atan2(double x, double y) { return std::atan2(x, y); }
00139   inline double abs(double x) { return std::abs(x); }
00140   inline double pow(double x, double y) { return std::pow(x, y); }
00141   inline double log(double x) { return std::log(x); }
00142 
00143   /* log file */
00144   #undef HERMES_LOG_FILE
00145   #ifdef HERMES_REPORT_NO_FILE
00146   #  define HERMES_LOG_FILE NULL
00147   #else
00148   # ifdef HERMES_REPORT_FILE
00149   #  define HERMES_LOG_FILE HERMES_REPORT_FILE
00150   # else
00151   #  ifndef HERMES_TEST
00152   #    define HERMES_LOG_FILE "hermes.log" // default filename for a library
00153   #  else
00154   #    define HERMES_LOG_FILE "test.log" // default filename for a library test
00155   #  endif
00156   # endif
00157   # endif
00158 
00159   /* event codes */
00160   #define HERMES_EC_ERROR 'E' ///< An event code: warnings. \internal
00161   #define HERMES_EC_WARNING 'W' ///< An event code: warnings. \internal
00162   #define HERMES_EC_INFO 'I' ///< An event code: info about results. \internal
00163 
00165   #define HERMES_LOG_FILE_DELIM_SIZE 80
00166   #define BUF_SZ 2048
00167 
00168   /* function name */
00171   #ifdef _WIN32 //Win32
00172   # ifdef __MINGW32__ //MinGW
00173   #   define __CURRENT_FUNCTION __func__
00174   # else //MSVC and other compilers
00175   #   define __CURRENT_FUNCTION __FUNCTION__
00176   # endif
00177   #else //Linux and Mac
00178   # define __CURRENT_FUNCTION __PRETTY_FUNCTION__
00179   #endif
00180 
00181   // Represents "any" part of the boundary when deciding where (on which elements) to assemble the form at hand.
00182   const std::string HERMES_ANY = "-1234";
00183   // For internal use.
00184   const int HERMES_ANY_INT = -1234;
00185   
00186   namespace Helpers
00187   {
00188     inline void fprint_num(FILE*f, double x, char* number_format)
00189     {
00190       fprintf(f, number_format, x);
00191     }
00192 
00193     inline void fprint_num(FILE*f, std::complex<double> x, char* number_format)
00194     {
00195           char* number_formatComplex = new char[20];
00196           sprintf(number_formatComplex, "(%s, %s)", number_format, number_format); 
00197       fprintf(f, number_formatComplex, x.real(), x.imag());
00198     }
00199   }
00200 
00201   namespace BLAS
00202   {
00203 #ifdef WITH_BLAS
00204     // BLAS-related functions
00205 
00206     // Complex part.
00207 #ifdef __cplusplus
00208     extern "C"
00209     {
00210 #endif
00211       extern int zscal_(int *, std::complex<double> *, std::complex<double> *, int *);
00212       extern int zaxpy_(int *, std::complex<double> *, std::complex<double> *, int *, std::complex<double> *, int *);
00213       extern int zcopy_(int *, std::complex<double> *, int *, std::complex<double> *, int *);
00214 
00215       extern int dscal_(int *, double *, double *, int *);
00216       extern int daxpy_(int *, double *, double *, int *, double *, int *);
00217       extern int dcopy_(int *,           double *, int *, double *, int *);
00218 
00219 #ifdef __cplusplus
00220     }
00221 #endif
00222 
00224     inline void blas_scal(int n, std::complex<double> alpha, std::complex<double> *x, int incx) { zscal_(&n, &alpha, x, &incx); }
00226     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); }
00228     inline void blas_copy(int n, std::complex<double> *x, int incx, std::complex<double> *y, int incy) { zcopy_(&n, x, &incx, y, &incx); }
00229 
00231     inline void blas_scal(int n, double alpha, double *x, int incx) { dscal_(&n, &alpha, x, &incx); }
00233     inline void blas_axpy(int n, double alpha, double *x, int incx, double *y, int incy) { daxpy_(&n, &alpha, x, &incx, y, &incy); }
00235     inline void blas_copy(int n, double *x, int incx, double *y, int incy) { dcopy_(&n, x, &incx, y, &incx); }
00236 
00237 #endif
00238   }
00239 }
00240 #endif
00241