source: src/base/helper.hpp@ d13e27

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

vmg: Work on output verbosity.

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

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[fcf7f6]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
[48b662]19/**
20 * @file helper.hpp
21 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
22 * @date Tue Apr 5 21:03:47 2011
[dfed1c]23 *
[48b662]24 * @brief Provides various helper functions.
[dfed1c]25 *
[48b662]26 */
27
28#ifndef HELPER_HPP_
29#define HELPER_HPP_
30
31#include <cassert>
32#include <cstdio>
33#include <limits>
[dfed1c]34#include <sstream>
35#include <string>
[48b662]36
37namespace VMG
38{
39
[dfed1c]40class Grid;
41class Vector;
42
[48b662]43namespace Helper
44{
[dfed1c]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
[d13e27]74 if (str.fail() || str.bad() || !str.eof()) val = def;
[dfed1c]75
76 return val;
77 }
[48b662]78
79 /**
80 * Checks a number for validity, i.e. it is neither nan nor inf.
81 */
82 inline bool CheckNumber(const vmg_float& number)
83 {
84 bool valid = true;
85
86 if (std::numeric_limits<vmg_float>::has_quiet_NaN) {
87 valid &= number != std::numeric_limits<vmg_float>::quiet_NaN();
88 assert(number != std::numeric_limits<vmg_float>::quiet_NaN());
89 }
90
91 if (std::numeric_limits<vmg_float>::has_signaling_NaN) {
92 valid &= number != std::numeric_limits<vmg_float>::signaling_NaN();
93 assert(number != std::numeric_limits<vmg_float>::signaling_NaN());
94 }
95
96 if (std::numeric_limits<vmg_float>::has_infinity) {
97 valid &= number != std::numeric_limits<vmg_float>::infinity();
98 valid &= number != -1 * std::numeric_limits<vmg_float>::infinity();
99 assert(number != std::numeric_limits<vmg_float>::infinity());
100 assert(number != -1 * std::numeric_limits<vmg_float>::infinity());
101 }
102
103 return valid;
104 }
105
[dfed1c]106 inline int intpow(int base, unsigned int power)
[48b662]107 {
[dfed1c]108 int result = 1;
[48b662]109 while (power != 0) {
110 if (power & 1)
111 result *= base;
112 base *= base;
113 power >>= 1;
114 }
115 return result;
116 }
117
[dfed1c]118 inline vmg_float pow(vmg_float base, unsigned int power)
119 {
120 vmg_float result = 1.0;
121 while (power != 0) {
122 if (power & 1)
123 result *= base;
124 base *= base;
125 power >>= 1;
126 }
127 return result;
128 }
129
130 inline unsigned int fact(unsigned int number)
131 {
132 unsigned int result = 1;
133 for (unsigned int i=2; i<=number; ++i)
134 result *= i;
135 return result;
136 }
137
[ac6d04]138 inline vmg_float pow_2(vmg_float val)
139 {
140 return val*val;
141 }
142
[dfed1c]143 inline vmg_float pow_3(vmg_float val)
144 {
145 return val*val*val;
146 }
147
[71b148]148 inline int log_2(int val)
149 {
150 assert(val > 0);
151 int log2 = 0;
152 int x = 1;
153
154 while (x < val) {
155 x <<= 1;
156 ++log2;
157 }
158
159 return log2;
160 }
161
[dfed1c]162
[48b662]163 /**
164 * Tests two arbitrary objects for equality and prints
165 * a warning if they differ.
166 */
167 template <class T>
168 bool IsEq(const T& val1, const T& val2, const char name[])
169 {
170 bool rval = (val1 == val2);
171
[d13e27]172#ifdef DEBUG
[48b662]173 if (!rval)
[d13e27]174 printf("Equality test failed (%s)\n", name);
[48b662]175 assert(rval);
[d13e27]176#endif /* DEBUG */
[48b662]177
178 return rval;
179 }
180
[dfed1c]181 vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
[48b662]182
183}
184
185}
186
187#endif /* HELPER_HPP_ */
Note: See TracBrowser for help on using the repository browser.