Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
util.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2019 Y. H. Tang, S. Kudo, X. Bian, Z. Li, G. E. Karniadakis *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 *****************************************************************************/
39 
49 #ifndef UTIL_H_
50 #define UTIL_H_
51 
52 // C headers
53 #include <cassert>
54 #include <cctype>
55 #include <climits>
56 #include <cmath>
57 #include <csignal>
58 #include <cstring>
59 #include <cstdio>
60 #include <cstdint>
61 #include <chrono>
62 
63 // C++ headers
64 #include <fstream>
65 #include <iostream>
66 #include <limits>
67 #include <memory>
68 #include <string>
69 #include <functional>
70 #include <stdexcept>
71 #include <atomic>
72 
73 // STL headers
74 #include <algorithm>
75 #include <deque>
76 #include <list>
77 #include <map>
78 #include <set>
79 #include <unordered_map>
80 #include <vector>
81 #include <complex>
82 #include <numeric>
83 #include <array>
84 
85 #ifdef __OMP
86 #include <omp.h>
87 #endif
88 
89 #include "../storage/stream.h"
90 #include "../geometry/point.h"
91 
92 namespace mui {
93 
94 typedef unsigned int uint;
95 typedef unsigned long long ull;
96 typedef unsigned long long llong;
97 
98 const static double PI = 3.1415926535897932385;
99 
100 static bool _quiet = false;
101 
102 inline void set_quiet(bool q) {
103  _quiet = q;
104 }
105 
106 template<typename REAL> inline REAL clamp( REAL x, REAL l, REAL r )
107 {
108  return (x < l) ? l : ( (x > r) ? r : x );
109 }
110 
111 template<typename REAL> inline REAL sgn( REAL x )
112 {
113  return (x>0)-(x<0);
114 }
115 
116 template<uint N> inline double powr( const double x ) {
117  return powr<2>( powr<N/2>( x ) ) * powr<N%2>(x);
118 }
119 
120 template<> inline double powr<2>( const double x ) {
121  return x * x;
122 }
123 
124 template<> inline double powr<1>( const double x ) {
125  return x;
126 }
127 
128 template<class T> inline bool almost_equal(T x, T y) {
129  return (x == y) ||
130  (std::fabs(x-y) < std::numeric_limits<T>::epsilon() * std::fabs(x+y)) ||
131  (std::fabs(x-y) < std::numeric_limits<T>::min());
132 }
133 
134 template<typename T> inline T frexp10(T arg, int &exp) {
135  if(almost_equal(arg, static_cast<T>(0))) exp = 0;
136  else exp = 1 + static_cast<int>(std::floor(std::log10(std::fabs(arg))));
137  return arg * std::pow(10, -(exp));
138 }
139 
140 template<typename T> inline T frexp10(T arg, long &exp) {
141  if(almost_equal(arg, static_cast<T>(0))) exp = 0;
142  else exp = 1 + static_cast<long>(std::floor(std::log10(std::fabs(arg))));
143  return arg * std::pow(10, -(exp));
144 }
145 
146 template<class T> inline T threshold(T x) {
147  return std::numeric_limits<T>::epsilon() * std::fabs(x);
148 }
149 
150 #ifdef __GNUC__
151 template<> inline double powr<0>( __attribute__((unused)) const double x ) {
152 #else
153 template<> inline double powr<0>( const double x ) {
154 #endif
155  return 1.0;
156 }
157 
158 template<typename T, uint D>
159 ostream& operator<<( ostream& stream, const point<T,D>& p )
160 {
161  for( uint i=0; i<D; ++i ) stream << p[i];
162  return stream;
163 }
164 
165 template<typename T, uint D>
167 {
168  for( uint i=0; i<D; ++i ) stream >> p[i];
169  return stream;
170 }
171 
172 template<typename T1, typename T2, typename T3>
173 struct triple {
174  T1 i;
175  T2 j;
176  T3 k;
177  triple() {}
178  triple( T1 _i, T2 _j, T3 _k ) : i(_i), j(_j), k(_k) {}
179  triple( const triple &another ) : i(another.i), j(another.j), k(another.k) {}
180 
181  inline triple& operator = ( const triple &another ) {
182  i = another.i;
183  j = another.j;
184  k = another.k;
185  return *this;
186  }
187 
188  inline bool operator != ( const triple &another ) const {
189  return i != another.i || j != another.j || k != another.k;
190  }
191  inline bool operator == ( const triple &another ) const {
192  return !(operator ==(another) );
193  }
194  inline bool operator < ( const triple &another ) const {
195  return i < another.i || ( i == another.i && ( j < another.j || ( j == another.j && k < another.k ) ) );
196  }
197 };
198 
199 }
200 
201 #endif /* UTIL_H_ */
Definition: stream.h:61
Definition: stream.h:67
T
Definition: dim.h:363
u u u u u u min
Definition: dim.h:289
Definition: comm.h:54
double powr< 0 >(const double x)
Definition: util.h:153
bool almost_equal(T x, T y)
Definition: util.h:128
T frexp10(T arg, int &exp)
Definition: util.h:134
T threshold(T x)
Definition: util.h:146
istream & operator>>(istream &stream, smalluint &sml)
Definition: comm_tcp.h:103
double powr< 1 >(const double x)
Definition: util.h:124
ostream & operator<<(ostream &stream, const smalluint &sml)
Definition: comm_tcp.h:127
double powr< 2 >(const double x)
Definition: util.h:120
double powr(const double x)
Definition: util.h:116
unsigned long long llong
Definition: util.h:96
unsigned long long ull
Definition: util.h:95
REAL clamp(REAL x, REAL l, REAL r)
Definition: util.h:106
REAL sgn(REAL x)
Definition: util.h:111
unsigned int uint
Definition: util.h:94
void set_quiet(bool q)
Definition: util.h:102
Definition: point.h:100
Definition: util.h:173
bool operator==(const triple &another) const
Definition: util.h:191
bool operator!=(const triple &another) const
Definition: util.h:188
T2 j
Definition: util.h:175
T3 k
Definition: util.h:176
triple(const triple &another)
Definition: util.h:179
triple(T1 _i, T2 _j, T3 _k)
Definition: util.h:178
triple()
Definition: util.h:177
T1 i
Definition: util.h:174
bool operator<(const triple &another) const
Definition: util.h:194
triple & operator=(const triple &another)
Definition: util.h:181