source: src/base/helper.hpp@ f57182

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

Open boundary conditions.

Conflicts:

lib/vmg/src/Makefile.am
lib/vmg/src/base/factory.cpp
lib/vmg/test/unit_test/library/dirichlet_fas_lr_mpi.cpp

  • Property mode set to 100644
File size: 4.0 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()) val = def;
75
76 return val;
77 }
78
79 /**
80 * Checks a number for validity, i.e. it is neither nan nor inf.
81 */
82 template <class T>
83 inline bool CheckNumber(const T& number)
84 {
85 bool valid = true;
86
87 // Check for nan
88 valid &= number == number;
89 assert(number == number);
90
91 // Check for infinity
92 if (!std::numeric_limits<T>::is_integer && std::numeric_limits<T>::has_denorm == std::denorm_present) {
93 valid &= number >= -1 * std::numeric_limits<T>::max();
94 valid &= number <= std::numeric_limits<T>::max();
95 assert(number >= -1 * std::numeric_limits<T>::max());
96 assert(number <= std::numeric_limits<T>::max());
97 }else {
98 valid &= number >= std::numeric_limits<T>::min();
99 valid &= number <= std::numeric_limits<T>::max();
100 assert(number >= std::numeric_limits<T>::min());
101 assert(number <= std::numeric_limits<T>::max());
102 }
103
104 return valid;
105 }
106
107 inline int intpow(int base, unsigned int power)
108 {
109 int result = 1;
110 while (power != 0) {
111 if (power & 1)
112 result *= base;
113 base *= base;
114 power >>= 1;
115 }
116 return result;
117 }
118
119 inline vmg_float pow(vmg_float base, unsigned int power)
120 {
121 vmg_float result = 1.0;
122 while (power != 0) {
123 if (power & 1)
124 result *= base;
125 base *= base;
126 power >>= 1;
127 }
128 return result;
129 }
130
131 inline unsigned int fact(unsigned int number)
132 {
133 unsigned int result = 1;
134 for (unsigned int i=2; i<=number; ++i)
135 result *= i;
136 return result;
137 }
138
139 inline vmg_float pow_2(vmg_float val)
140 {
141 return val*val;
142 }
143
144 inline vmg_float pow_3(vmg_float val)
145 {
146 return val*val*val;
147 }
148
149 inline int log_2(int val)
150 {
151 assert(val > 0);
152 int log2 = 0;
153 int x = 1;
154
155 while (x < val) {
156 x <<= 1;
157 ++log2;
158 }
159
160 return log2;
161 }
162
163
164 /**
165 * Tests two arbitrary objects for equality and prints
166 * a warning if they differ.
167 */
168 template <class T>
169 bool IsEq(const T& val1, const T& val2, const char name[])
170 {
171 bool rval = (val1 == val2);
172
173#ifdef DEBUG
174 if (!rval)
175 printf("Equality test failed (%s)\n", name);
176 assert(rval);
177#endif /* DEBUG */
178
179 return rval;
180 }
181
182 bool AssertVectorsEqual(const Vector& pos_1, const Vector& pos_2);
183
184 vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
185
186}
187
188}
189
190#endif /* HELPER_HPP_ */
Note: See TracBrowser for help on using the repository browser.