Changeset dfed1c for src/base/helper.hpp


Ignore:
Timestamp:
Nov 22, 2011, 9:22:10 PM (14 years ago)
Author:
Julian Iseringhausen <isering@…>
Children:
facba0
Parents:
66f24d
Message:

Major vmg update.

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/base/helper.hpp

    r66f24d rdfed1c  
    33 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
    44 * @date   Tue Apr  5 21:03:47 2011
    5  * 
     5 *
    66 * @brief  Provides various helper functions.
    7  *
    8  *
     7 *
    98 */
    109
     
    1514#include <cstdio>
    1615#include <limits>
     16#include <sstream>
     17#include <string>
    1718
    1819namespace VMG
    1920{
    2021
     22class Grid;
     23class Vector;
     24
    2125namespace Helper
    2226{
     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  }
    2365
    2466  /**
    2567   * Checks a number for validity, i.e. it is neither nan nor inf.
    26    *
    2768   */
    2869  inline bool CheckNumber(const vmg_float& number)
     
    5091  }
    5192
    52   inline int intpow(unsigned int base, unsigned int power)
     93  inline int intpow(int base, unsigned int power)
    5394  {
    54     assert (power >= 0);
    55     unsigned int result = 1;
     95    int result = 1;
    5696    while (power != 0) {
    5797      if (power & 1)
     
    62102    return result;
    63103  }
     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_3(vmg_float val)
     126  {
     127    return val*val*val;
     128  }
     129
    64130
    65131  /**
     
    72138    bool rval = (val1 == val2);
    73139
     140#ifdef DEBUG_OUTPUT
    74141    if (!rval)
    75142      printf("WARNING: Values are not equal (%s)\n", name);
     143#endif /* DEBUG_OUTPUT */
    76144
    77145    assert(rval);
     
    80148  }
    81149
    82   inline void MM(int nn, vmg_float u, vmg_float *tbl)
    83   {
    84     int i, n, m;
    85 
    86     assert(u >= 0.0 && u <= 1.0);
    87 
    88     /* n = 1: */
    89     /* i = 0: */
    90     tbl[0] = 1.0;
    91 
    92     for (n = 2; n <= nn; n++) {
    93       m = n - 1;
    94 
    95       /* i = n - 1: */
    96       tbl[n-1] =  (1 - u) * tbl[n-2] / m;
    97 
    98       for (i = n - 2; i >= 1; i--)
    99         tbl[i] = ((u + i) * tbl[i] + (n - (u + i)) * tbl[i-1]) / m;
    100 
    101       /* i = 0: */
    102       tbl[0] *= u / m;
    103     }
    104   }
    105 
    106   inline void MdM(int nn, vmg_float u,
    107                   vmg_float * restrict tbl, vmg_float * restrict dtbl)
    108   {
    109     int i, n, m;
    110 
    111     assert(nn >= 2);
    112     assert(u >= 0.0 && u <= 1.0);
    113 
    114     /* n = 1: */
    115     /* i = 0: */
    116     tbl[0] = 1.0;
    117 
    118     for (n = 2; n <= nn; n++) {
    119       m = n - 1;
    120 
    121       /* differentiation */
    122       if (n == nn) {
    123         dtbl[n-1] = - tbl[n-2];
    124 
    125         for (i = n - 2; i >= 1; i--)
    126           dtbl[i] = tbl[i] - tbl[i-1];
    127 
    128         dtbl[0] = tbl[0];
    129       }
    130 
    131       /* i = n - 1: */
    132       tbl[n-1] =  (1 - u) * tbl[n-2] / m;
    133 
    134       for (i = n - 2; i >= 1; i--)
    135         tbl[i] = ((u + i) * tbl[i] + (n - (u + i)) * tbl[i-1]) / m;
    136 
    137       /* i = 0: */
    138       tbl[0] *= u / m;
    139 
    140     }
    141   }
     150  vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid);
    142151
    143152}
Note: See TracChangeset for help on using the changeset viewer.