source: src/base/helper.hpp@ ac6d04

Last change on this file since ac6d04 was ac6d04, checked in by Julian Iseringhausen <isering@…>, 14 years ago

Merge recent changes of the vmg library into ScaFaCos.

Includes a fix for the communication problems on Jugene.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1666 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/**
2 * @file helper.hpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Tue Apr 5 21:03:47 2011
5 *
6 * @brief Provides various helper functions.
7 *
8 */
9
10#ifndef HELPER_HPP_
11#define HELPER_HPP_
12
13#include <cassert>
14#include <cstdio>
15#include <limits>
16#include <sstream>
17#include <string>
18
19namespace VMG
20{
21
22class Grid;
23class Vector;
24
25namespace Helper
26{
27 char* GetCharArray(const std::string& str);
28 std::string ReplaceWhitespaces(const char* buffer, const char* replace);
29
30 template <class T>
31 std::string ToString(const T& val)
32 {
33 std::stringstream str;
34 str << std::scientific << val;
35 return str.str();
36 }
37
38 template <class T>
39 T ToVal(const char* val_str)
40 {
41 T val;
42 std::stringstream str;
43 str << val_str;
44 str >> val;
45
46 return val;
47 }
48
49 template <class T>
50 T ToValWithDefault(const char* val_str, const T& def)
51 {
52 T val;
53 std::stringstream str(val_str);
54 str >> val;
55
56 if (str.fail() || str.bad() || !str.eof()) {
57#ifdef DEBUG_VERBOSE
58 std::printf("VMG::Helper::ToValWithDefault: Using default value.\n");
59#endif
60 val = def;
61 }
62
63 return val;
64 }
65
66 /**
67 * Checks a number for validity, i.e. it is neither nan nor inf.
68 */
69 inline bool CheckNumber(const vmg_float& number)
70 {
71 bool valid = true;
72
73 if (std::numeric_limits<vmg_float>::has_quiet_NaN) {
74 valid &= number != std::numeric_limits<vmg_float>::quiet_NaN();
75 assert(number != std::numeric_limits<vmg_float>::quiet_NaN());
76 }
77
78 if (std::numeric_limits<vmg_float>::has_signaling_NaN) {
79 valid &= number != std::numeric_limits<vmg_float>::signaling_NaN();
80 assert(number != std::numeric_limits<vmg_float>::signaling_NaN());
81 }
82
83 if (std::numeric_limits<vmg_float>::has_infinity) {
84 valid &= number != std::numeric_limits<vmg_float>::infinity();
85 valid &= number != -1 * std::numeric_limits<vmg_float>::infinity();
86 assert(number != std::numeric_limits<vmg_float>::infinity());
87 assert(number != -1 * std::numeric_limits<vmg_float>::infinity());
88 }
89
90 return valid;
91 }
92
93 inline int intpow(int base, unsigned int power)
94 {
95 int result = 1;
96 while (power != 0) {
97 if (power & 1)
98 result *= base;
99 base *= base;
100 power >>= 1;
101 }
102 return result;
103 }
104
105 inline vmg_float pow(vmg_float base, unsigned int power)
106 {
107 vmg_float result = 1.0;
108 while (power != 0) {
109 if (power & 1)
110 result *= base;
111 base *= base;
112 power >>= 1;
113 }
114 return result;
115 }
116
117 inline unsigned int fact(unsigned int number)
118 {
119 unsigned int result = 1;
120 for (unsigned int i=2; i<=number; ++i)
121 result *= i;
122 return result;
123 }
124
125 inline vmg_float pow_2(vmg_float val)
126 {
127 return val*val;
128 }
129
130 inline vmg_float pow_3(vmg_float val)
131 {
132 return val*val*val;
133 }
134
135
136 /**
137 * Tests two arbitrary objects for equality and prints
138 * a warning if they differ.
139 */
140 template <class T>
141 bool IsEq(const T& val1, const T& val2, const char name[])
142 {
143 bool rval = (val1 == val2);
144
145#ifdef DEBUG_OUTPUT
146 if (!rval)
147 printf("WARNING: Values are not equal (%s)\n", name);
148#endif /* DEBUG_OUTPUT */
149
150 assert(rval);
151
152 return rval;
153 }
154
155 vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
156
157}
158
159}
160
161#endif /* HELPER_HPP_ */
Note: See TracBrowser for help on using the repository browser.