| 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 <cmath>
|
|---|
| 33 | #include <cstdio>
|
|---|
| 34 | #include <limits>
|
|---|
| 35 | #include <sstream>
|
|---|
| 36 | #include <string>
|
|---|
| 37 |
|
|---|
| 38 | namespace VMG
|
|---|
| 39 | {
|
|---|
| 40 |
|
|---|
| 41 | class Grid;
|
|---|
| 42 | class Vector;
|
|---|
| 43 |
|
|---|
| 44 | namespace Helper
|
|---|
| 45 | {
|
|---|
| 46 | char* GetCharArray(const std::string& str);
|
|---|
| 47 | std::string ReplaceWhitespaces(const char* buffer, const char* replace);
|
|---|
| 48 |
|
|---|
| 49 | template <class T>
|
|---|
| 50 | std::string ToString(const T& val)
|
|---|
| 51 | {
|
|---|
| 52 | std::stringstream str;
|
|---|
| 53 | str << std::scientific << val;
|
|---|
| 54 | return str.str();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | template <class T>
|
|---|
| 58 | T ToVal(const char* val_str)
|
|---|
| 59 | {
|
|---|
| 60 | T val;
|
|---|
| 61 | std::stringstream str;
|
|---|
| 62 | str << val_str;
|
|---|
| 63 | str >> val;
|
|---|
| 64 |
|
|---|
| 65 | return val;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | template <class T>
|
|---|
| 69 | T ToValWithDefault(const char* val_str, const T& def)
|
|---|
| 70 | {
|
|---|
| 71 | T val;
|
|---|
| 72 | std::stringstream str(val_str);
|
|---|
| 73 | str >> val;
|
|---|
| 74 |
|
|---|
| 75 | if (str.fail() || str.bad() || !str.eof()) val = def;
|
|---|
| 76 |
|
|---|
| 77 | return val;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Checks a number for validity, i.e. it is neither nan nor inf.
|
|---|
| 82 | */
|
|---|
| 83 | template <class T>
|
|---|
| 84 | inline bool CheckNumber(const T& number)
|
|---|
| 85 | {
|
|---|
| 86 | bool valid = true;
|
|---|
| 87 |
|
|---|
| 88 | // Check for nan
|
|---|
| 89 | valid &= number == number;
|
|---|
| 90 | assert(number == number);
|
|---|
| 91 |
|
|---|
| 92 | // Check for infinity
|
|---|
| 93 | if (!std::numeric_limits<T>::is_integer && std::numeric_limits<T>::has_denorm == std::denorm_present) {
|
|---|
| 94 | valid &= number >= -1 * std::numeric_limits<T>::max();
|
|---|
| 95 | valid &= number <= std::numeric_limits<T>::max();
|
|---|
| 96 | assert(number >= -1 * std::numeric_limits<T>::max());
|
|---|
| 97 | assert(number <= std::numeric_limits<T>::max());
|
|---|
| 98 | }else {
|
|---|
| 99 | valid &= number >= std::numeric_limits<T>::min();
|
|---|
| 100 | valid &= number <= std::numeric_limits<T>::max();
|
|---|
| 101 | assert(number >= std::numeric_limits<T>::min());
|
|---|
| 102 | assert(number <= std::numeric_limits<T>::max());
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return valid;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | inline int intpow(int base, unsigned int power)
|
|---|
| 109 | {
|
|---|
| 110 | int result = 1;
|
|---|
| 111 | while (power != 0) {
|
|---|
| 112 | if (power & 1)
|
|---|
| 113 | result *= base;
|
|---|
| 114 | base *= base;
|
|---|
| 115 | power >>= 1;
|
|---|
| 116 | }
|
|---|
| 117 | return result;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | inline vmg_float pow(vmg_float base, unsigned int power)
|
|---|
| 121 | {
|
|---|
| 122 | vmg_float result = 1.0;
|
|---|
| 123 | while (power != 0) {
|
|---|
| 124 | if (power & 1)
|
|---|
| 125 | result *= base;
|
|---|
| 126 | base *= base;
|
|---|
| 127 | power >>= 1;
|
|---|
| 128 | }
|
|---|
| 129 | return result;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | inline unsigned int fact(unsigned int number)
|
|---|
| 133 | {
|
|---|
| 134 | unsigned int result = 1;
|
|---|
| 135 | for (unsigned int i=2; i<=number; ++i)
|
|---|
| 136 | result *= i;
|
|---|
| 137 | return result;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | inline vmg_float pow_2(vmg_float val)
|
|---|
| 141 | {
|
|---|
| 142 | return val*val;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | inline vmg_float pow_3(vmg_float val)
|
|---|
| 146 | {
|
|---|
| 147 | return val*val*val;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | inline int log_2(int val)
|
|---|
| 151 | {
|
|---|
| 152 | assert(val > 0);
|
|---|
| 153 | int log2 = 0;
|
|---|
| 154 | int x = 1;
|
|---|
| 155 |
|
|---|
| 156 | while (x < val) {
|
|---|
| 157 | x <<= 1;
|
|---|
| 158 | ++log2;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | return log2;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | inline int RoundUpToNextMultiple(int number, int multiple)
|
|---|
| 165 | {
|
|---|
| 166 | return static_cast<int>(ceil((static_cast<vmg_float>(number)-0.5) / multiple)) * multiple;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | inline int RoundDownToNextMultiple(int number, int multiple)
|
|---|
| 170 | {
|
|---|
| 171 | return static_cast<int>(floor((static_cast<vmg_float>(number)+0.5) / multiple)) * multiple;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Tests two arbitrary objects for equality and prints
|
|---|
| 177 | * a warning if they differ.
|
|---|
| 178 | */
|
|---|
| 179 | template <class T>
|
|---|
| 180 | bool IsEq(const T& val1, const T& val2, const char name[])
|
|---|
| 181 | {
|
|---|
| 182 | bool rval = (val1 == val2);
|
|---|
| 183 |
|
|---|
| 184 | #ifdef DEBUG
|
|---|
| 185 | if (!rval)
|
|---|
| 186 | printf("Equality test failed (%s)\n", name);
|
|---|
| 187 | assert(rval);
|
|---|
| 188 | #endif /* DEBUG */
|
|---|
| 189 |
|
|---|
| 190 | return rval;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | bool AssertVectorsEqual(const Vector& pos_1, const Vector& pos_2, const vmg_float& tol);
|
|---|
| 194 |
|
|---|
| 195 | vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
|
|---|
| 196 |
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | #endif /* HELPER_HPP_ */
|
|---|