QMCPACK
Standard.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
8 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 // http://pathintegrals.info //
15 /////////////////////////////////////////////////////////////
16 
17 ////////////////////////////////////////////////////////////////////////////////
18 // //
19 // Standard definitions and functions that are used almost everywhere //
20 // //
21 // Burkhard Militzer Urbana 4-9-99 //
22 // //
23 ////////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef _STANDARD_
26 #define _STANDARD_
27 
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <string>
32 
33 // Define RANGE_CHECKING for testing
34 // #define RANGE_CHECKING
35 
36 ////////////////////////////////////////////////
37 
38 std::string IntToString(const int i);
39 std::string DoubleToString(const double d);
40 int StringToInt(const std::string& s);
41 double StringToDouble(const std::string& s);
42 std::string UpperCase(const std::string& s);
43 std::string LowerCase(const std::string& s);
44 
45 ////////// redefine std::cout to write to a file //////////////
46 // #ifndef NO_COUT
47 // #define std::cout COUT
48 // extern std::ofstream COUT;
49 // #endif // NO_COUT
50 
51 ////////////////////////////////////////////////
52 
53 #include <cmath>
54 
55 #ifndef pi
56 const double pi = 3.14159265358979323846;
57 #endif
58 
59 // Is required for KCC but not on IBM using KCC3.3
60 // laters ones require it too.
61 /* #ifndef __rs6000__ */
62 /* #ifndef __osf__ */
63 #if !defined __GNUC__ || (__GNUC__ == 2 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1))
64 //inline double std::abs(double x) {
65 // return std::abs(x);
66 //}
67 #endif
68 /* #endif */
69 /* #endif */
70 
71 // #define double long double
72 
73 inline double sign(double x) { return (x > 0.0) ? 1.0 : ((x < 0.0) ? -1.0 : 0.0); }
74 
75 inline int sign(int x) { return (x > 0) ? 1 : ((x < 0) ? -1 : 0); }
76 
77 inline double nint(const double x) { return int(x + 0.5 * sign(x)); }
78 
79 inline double sqr(double x) { return (x * x); }
80 
81 inline int sqr(int x) { return (x * x); }
82 
83 ///////////////////////////////////////////////////////////////////////////
84 
85 // Write name fo the variable and its value
86 #define write1(i) \
87  { \
88  std::cout << " " #i "= " << i; \
89  }
90 #define write2(i, j) \
91  { \
92  write1(i); \
93  write1(j); \
94  }
95 #define write3(i, j, k) \
96  { \
97  write2(i, j); \
98  write1(k); \
99  }
100 #define write4(i, j, k, l) \
101  { \
102  write3(i, j, k); \
103  write1(l); \
104  }
105 #define write5(i, j, k, l, m) \
106  { \
107  write4(i, j, k, l); \
108  write1(m); \
109  }
110 #define write6(i, j, k, l, m, n) \
111  { \
112  write5(i, j, k, l, m); \
113  write1(n); \
114  }
115 #define write7(i, j, k, l, m, n, o) \
116  { \
117  write6(i, j, k, l, m, n); \
118  write1(o); \
119  }
120 #define write8(i, j, k, l, m, n, o, p) \
121  { \
122  write7(i, j, k, l, m, n, o); \
123  write1(p); \
124  }
125 #define write9(i, j, k, l, m, n, o, p, q) \
126  { \
127  write8(i, j, k, l, m, n, o, p); \
128  write1(q); \
129  }
130 #define write10(i, j, k, l, m, n, o, p, q, r) \
131  { \
132  write9(i, j, k, l, m, n, o, p, q); \
133  write1(r); \
134  }
135 
136 #define BMWrite(i) \
137  { \
138  write1(i); \
139  std::cout << std::endl; \
140  }
141 #define BMWrite2(i, j) \
142  { \
143  write2(i, j); \
144  std::cout << std::endl; \
145  }
146 #define BMWrite3(i, j, k) \
147  { \
148  write3(i, j, k); \
149  std::cout << std::endl; \
150  }
151 #define BMWrite4(i, j, k, l) \
152  { \
153  write4(i, j, k, l); \
154  std::cout << std::endl; \
155  }
156 #define BMWrite5(i, j, k, l, m) \
157  { \
158  write5(i, j, k, l, m); \
159  std::cout << std::endl; \
160  }
161 #define BMWrite6(i, j, k, l, m, n) \
162  { \
163  write6(i, j, k, l, m, n); \
164  std::cout << std::endl; \
165  }
166 #define BMWrite7(i, j, k, l, m, n, o) \
167  { \
168  write7(i, j, k, l, m, n, o); \
169  std::cout << std::endl; \
170  }
171 #define BMWrite8(i, j, k, l, m, n, o, p) \
172  { \
173  write8(i, j, k, l, m, n, o, p); \
174  std::cout << std::endl; \
175  }
176 #define BMWrite9(i, j, k, l, m, n, o, p, q) \
177  { \
178  write9(i, j, k, l, m, n, o, p, q); \
179  std::cout << std::endl; \
180  }
181 #define BMWrite10(i, j, k, l, m, n, o, p, q, r) \
182  { \
183  write10(i, j, k, l, m, n, o, p, q, r); \
184  std::cout << std::endl; \
185  }
186 
187 void Terminate();
188 
189 inline void WriteError(std::ostringstream& ss)
190 {
191  const std::string errorString = "Error ";
192  // ss << ends;
193  // std::cout is redirect into a file which might not yet be opened
194  if (std::cout)
195  {
196  std::cout.precision(16);
197  std::cout << errorString << ss.str() << std::endl;
198  }
199  std::cerr.precision(16);
200  std::cerr << errorString << ss.str() << std::endl;
201  Terminate();
202 }
203 
204 inline void error(char const* m)
205 {
206  std::ostringstream ss;
207  ss << m;
208  WriteError(ss);
209 }
210 
211 template<class T>
212 inline void error(char const* m, const T& n)
213 {
214  std::ostringstream ss;
215  ss << m << " " << n;
216  WriteError(ss);
217 }
218 
219 template<class T, class U>
220 inline void error(char const* m, const T& t, const U& u)
221 {
222  std::ostringstream ss;
223  ss << m << " " << t << " " << u;
224  WriteError(ss);
225 }
226 
227 template<class T, class U, class V>
228 inline void error(char const* m, const T& t, const U& u, const V& v)
229 {
230  std::ostringstream ss;
231  ss << m << " " << t << " " << u << " " << v;
232  WriteError(ss);
233 }
234 
235 template<class T, class U, class V, class W>
236 inline void error(char const* m, const T& t, const U& u, const V& v, const W& w)
237 {
238  std::ostringstream ss;
239  ss << m << " " << t << " " << u << " " << v << " " << w;
240  WriteError(ss);
241 }
242 
243 template<class T, class U, class V, class W, class X>
244 inline void error(char const* m, const T& t, const U& u, const V& v, const W& w, const X& x)
245 {
246  std::ostringstream ss;
247  ss << m << " " << t << " " << u << " " << v << " " << w << " " << x;
248  WriteError(ss);
249 }
250 
251 template<class T, class U, class V, class W, class X, class Y>
252 inline void error(char const* m, const T& t, const U& u, const V& v, const W& w, const X& x, const Y& y)
253 {
254  std::ostringstream ss;
255  ss << m << " " << t << " " << u << " " << v << " " << w << " " << x << " " << y;
256  WriteError(ss);
257 }
258 
259 template<class T, class U, class V, class W, class X, class Y, class Z>
260 inline void error(char const* m, const T& t, const U& u, const V& v, const W& w, const X& x, const Y& y, const Z& z)
261 {
262  std::ostringstream ss;
263  ss << m << " " << t << " " << u << " " << v << " " << w << " " << x << " " << y << " " << z;
264  WriteError(ss);
265 }
266 
267 inline void WriteWarning(std::ostringstream& ss)
268 {
269  const std::string warningString = "WARNING ";
270  // ss << ends;
271  std::cout << warningString << ss.str() << std::endl;
272  // std::cerr << warningString << ss.str() << std::endl;
273 }
274 
275 inline void warning()
276 {
277  std::ostringstream ss;
278  ss << "...";
279  WriteWarning(ss);
280 }
281 
282 inline void warning(char* m)
283 {
284  std::ostringstream ss;
285  ss << m;
286  WriteWarning(ss);
287 }
288 
289 template<class T>
290 inline void warning(const char* m, const T& t)
291 {
292  std::ostringstream ss;
293  ss << m << " " << t;
294  WriteWarning(ss);
295 }
296 
297 template<class T, class U>
298 inline void warning(const char* m, const T& t, const U& u)
299 {
300  std::ostringstream ss;
301  ss << m << " " << t << " " << u;
302  WriteWarning(ss);
303 }
304 
305 template<class T, class U, class V>
306 inline void warning(const char* m, const T& t, const U& u, const V& v)
307 {
308  std::ostringstream ss;
309  ss << m << " " << t << " " << u << " " << v;
310  WriteWarning(ss);
311 }
312 
313 template<class T, class U, class V, class W>
314 inline void warning(const char* m, const T& t, const U& u, const V& v, const W& w)
315 {
316  std::ostringstream ss;
317  ss << m << " " << t << " " << u << " " << v << " " << w;
318  WriteWarning(ss);
319 }
320 
321 template<class T, class U, class V, class W, class X>
322 inline void warning(const char* m, const T& t, const U& u, const V& v, const W& w, const X& x)
323 {
324  std::ostringstream ss;
325  ss << m << " " << t << " " << u << " " << v << " " << w << " " << x;
326  WriteWarning(ss);
327 }
328 
329 template<class T, class U, class V, class W, class X, class Y>
330 inline void warning(const char* m, const T& t, const U& u, const V& v, const W& w, const X& x, const Y& y)
331 {
332  std::ostringstream ss;
333  ss << m << " " << t << " " << u << " " << v << " " << w << " " << x << " " << y;
334  WriteWarning(ss);
335 }
336 
337 template<class T, class U, class V, class W, class X, class Y, class Z>
338 inline void warning(const char* m, const T& t, const U& u, const V& v, const W& w, const X& x, const Y& y, const Z& z)
339 {
340  std::ostringstream ss;
341  ss << m << " " << t << " " << u << " " << v << " " << w << " " << x << " " << y << " " << z;
342  WriteWarning(ss);
343 }
344 
345 ////////////////////// Functions for array bound checking //////////////////////
346 
347 // Limits is an inline function that checks indices
348 // ok is 0<= n < max
349 inline void Limits(const int n, const int max)
350 {
351 #ifdef RANGE_CHECKING
352  if ((n < 0) || (n >= max))
353  {
354  error("Array Index out of range ", n, max);
355  std::cerr << "Array error: Index out of range: 0<= " << n << " < " << max << "\n";
356  Terminate();
357  }
358 #endif // RANGE_CHECKING
359 }
360 
361 // Limits is an inline function that checks indices
362 // ok is 0<= n <= max
363 inline void LimitsInclusive(const int n, const int max)
364 {
365 #ifdef RANGE_CHECKING
366  if ((n < 0) || (n > max))
367  {
368  // error("Array Error: Index out of range ",n,max);
369  std::cerr << "Array error: Upper limit for index out of range: 0<= " << n << " <= " << max << "\n";
370  Terminate();
371  }
372 #endif // RANGE_CHECKING
373 }
374 
375 inline void EqualLimits(const int max1, const int max2)
376 {
377 #ifdef RANGE_CHECKING
378  if (max1 != max2)
379  {
380  std::cerr << "Array copy error: array sizes not equal:" << max1 << "," << max2 << std::endl;
381  Terminate();
382  }
383 #endif // RANGE_CHECKING
384 }
385 
386 inline void BiggerLimit(const int lower, const int upper)
387 {
388 #ifdef RANGE_CHECKING
389  if (lower >= upper)
390  {
391  std::cerr << "Sub-array limits error: lower limit not lower " << lower << "," << upper << std::endl;
392  Terminate();
393  }
394 #endif // RANGE_CHECKING
395 }
396 
397 
398 #endif // _STANDARD_
void LimitsInclusive(const int n, const int max)
Definition: Standard.h:363
void WriteError(std::ostringstream &ss)
Definition: Standard.h:189
std::string LowerCase(const std::string &s)
void Limits(const int n, const int max)
Definition: Standard.h:349
int StringToInt(const std::string &s)
void warning()
Definition: Standard.h:275
double StringToDouble(const std::string &s)
void Terminate()
const double pi
Definition: Standard.h:56
std::string UpperCase(const std::string &s)
void error(char const *m)
Definition: Standard.h:204
void EqualLimits(const int max1, const int max2)
Definition: Standard.h:375
void BiggerLimit(const int lower, const int upper)
Definition: Standard.h:386
double sign(double x)
Definition: Standard.h:73
std::string DoubleToString(const double d)
double sqr(double x)
Definition: Standard.h:79
double nint(const double x)
Definition: Standard.h:77
void WriteWarning(std::ostringstream &ss)
Definition: Standard.h:267
std::string IntToString(const int i)