source: src/base/helper.hpp@ e271f0

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

vmg: Added license files and headers (GPLv3).

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

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * vmg - a versatile multigrid solver
3 * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
4 *
5 * vmg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * vmg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * @file helper.hpp
21 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
22 * @date Tue Apr 5 21:03:47 2011
23 *
24 * @brief Provides various helper functions.
25 *
26 */
27
28#ifndef HELPER_HPP_
29#define HELPER_HPP_
30
31#include <cassert>
32#include <cstdio>
33#include <limits>
34#include <sstream>
35#include <string>
36
37namespace VMG
38{
39
40class Grid;
41class Vector;
42
43namespace Helper
44{
45 char* GetCharArray(const std::string& str);
46 std::string ReplaceWhitespaces(const char* buffer, const char* replace);
47
48 template <class T>
49 std::string ToString(const T& val)
50 {
51 std::stringstream str;
52 str << std::scientific << val;
53 return str.str();
54 }
55
56 template <class T>
57 T ToVal(const char* val_str)
58 {
59 T val;
60 std::stringstream str;
61 str << val_str;
62 str >> val;
63
64 return val;
65 }
66
67 template <class T>
68 T ToValWithDefault(const char* val_str, const T& def)
69 {
70 T val;
71 std::stringstream str(val_str);
72 str >> val;
73
74 if (str.fail() || str.bad() || !str.eof()) {
75#ifdef DEBUG_VERBOSE
76 std::printf("VMG::Helper::ToValWithDefault: Using default value.\n");
77#endif
78 val = def;
79 }
80
81 return val;
82 }
83
84 /**
85 * Checks a number for validity, i.e. it is neither nan nor inf.
86 */
87 inline bool CheckNumber(const vmg_float& number)
88 {
89 bool valid = true;
90
91 if (std::numeric_limits<vmg_float>::has_quiet_NaN) {
92 valid &= number != std::numeric_limits<vmg_float>::quiet_NaN();
93 assert(number != std::numeric_limits<vmg_float>::quiet_NaN());
94 }
95
96 if (std::numeric_limits<vmg_float>::has_signaling_NaN) {
97 valid &= number != std::numeric_limits<vmg_float>::signaling_NaN();
98 assert(number != std::numeric_limits<vmg_float>::signaling_NaN());
99 }
100
101 if (std::numeric_limits<vmg_float>::has_infinity) {
102 valid &= number != std::numeric_limits<vmg_float>::infinity();
103 valid &= number != -1 * std::numeric_limits<vmg_float>::infinity();
104 assert(number != std::numeric_limits<vmg_float>::infinity());
105 assert(number != -1 * std::numeric_limits<vmg_float>::infinity());
106 }
107
108 return valid;
109 }
110
111 inline int intpow(int base, unsigned int power)
112 {
113 int result = 1;
114 while (power != 0) {
115 if (power & 1)
116 result *= base;
117 base *= base;
118 power >>= 1;
119 }
120 return result;
121 }
122
123 inline vmg_float pow(vmg_float base, unsigned int power)
124 {
125 vmg_float result = 1.0;
126 while (power != 0) {
127 if (power & 1)
128 result *= base;
129 base *= base;
130 power >>= 1;
131 }
132 return result;
133 }
134
135 inline unsigned int fact(unsigned int number)
136 {
137 unsigned int result = 1;
138 for (unsigned int i=2; i<=number; ++i)
139 result *= i;
140 return result;
141 }
142
143 inline vmg_float pow_2(vmg_float val)
144 {
145 return val*val;
146 }
147
148 inline vmg_float pow_3(vmg_float val)
149 {
150 return val*val*val;
151 }
152
153 inline int log_2(int val)
154 {
155 assert(val > 0);
156 int log2 = 0;
157 int x = 1;
158
159 while (x < val) {
160 x <<= 1;
161 ++log2;
162 }
163
164 return log2;
165 }
166
167
168 /**
169 * Tests two arbitrary objects for equality and prints
170 * a warning if they differ.
171 */
172 template <class T>
173 bool IsEq(const T& val1, const T& val2, const char name[])
174 {
175 bool rval = (val1 == val2);
176
177#ifdef DEBUG_OUTPUT
178 if (!rval)
179 printf("WARNING: Values are not equal (%s)\n", name);
180#endif /* DEBUG_OUTPUT */
181
182 assert(rval);
183
184 return rval;
185 }
186
187 vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
188
189}
190
191}
192
193#endif /* HELPER_HPP_ */
Note: See TracBrowser for help on using the repository browser.