mantaflow  0.10
A framework for fluid simulation
general.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * MantaFlow fluid solver framework
5  * Copyright 2011 Tobias Pfaff, Nils Thuerey
6  *
7  * This program is free software, distributed under the terms of the
8  * GNU General Public License (GPL)
9  * http://www.gnu.org/licenses
10  *
11  * Globally used macros and functions
12  *
13  ******************************************************************************/
14 
15 #ifndef _GENERAL_H
16 #define _GENERAL_H
17 
18 #include <iostream>
19 #include <sstream>
20 #include <cmath>
21 #include <algorithm>
22 
23 namespace Manta {
24 
25 // ui data exchange
26 #ifdef GUI
27  // defined in qtmain.cpp
28  extern void updateQtGui(bool full, int frame, float time, const std::string& curPlugin);
29 #else
30  // dummy function if GUI is not enabled
31  inline void updateQtGui(bool full, int frame, float time, const std::string& curPlugin) {}
32 #endif
33 
34 
35 // activate debug mode if _DEBUG is defined (eg for windows)
36 #ifndef DEBUG
37 #ifdef _DEBUG
38 #define DEBUG 1
39 #endif // _DEBUG
40 #endif // DEBUG
41 
42 // Standard exception
43 class Error : public std::exception
44 {
45 public:
46  Error(const std::string& s) : mS(s) {
47 # ifdef DEBUG
48  // print error
49  std::cerr << "Aborting: "<< s <<" \n";
50  // then force immedieate crash in debug mode
51  *(volatile int*)(0) = 1;
52 # endif
53  }
54  virtual ~Error() throw() {}
55  virtual const char* what() const throw() { return mS.c_str(); }
56 private:
57  std::string mS;
58 };
59 
60 // mark unused parameter variables
61 #define unusedParameter(x) ((void)x)
62 
63 // Debug output functions and macros
64 extern int gDebugLevel;
65 
66 #define MSGSTREAM std::ostringstream msg; msg.precision(7); msg.width(9);
67 #define debMsg(mStr, level) if (_chklevel(level)) { MSGSTREAM; msg << mStr; std::cout << msg.str() << std::endl; }
68 inline bool _chklevel(int level=0) { return gDebugLevel >= level; }
69 
70 // error and assertation macros
71 #ifdef DEBUG
72 # define DEBUG_ONLY(a) a
73 #else
74 # define DEBUG_ONLY(a)
75 #endif
76 #define throwError(msg) { std::ostringstream __s; __s << msg << std::endl << "Error raised in " << __FILE__ << ":" << __LINE__; throw Manta::Error(__s.str()); }
77 #define errMsg(msg) throwError(msg);
78 #define assertMsg(cond,msg) if(!(cond)) throwError(msg)
79 #define assertDeb(cond,msg) DEBUG_ONLY( assertMsg(cond,msg) )
80 
81 // for compatibility with blender, blender only defines WITH_MANTA, make sure we have "BLENDER"
82 #ifndef BLENDER
83 #ifdef WITH_MANTA
84 #define BLENDER 1
85 #endif
86 #endif
87 
88 // common type for indexing large grids
89 typedef long long IndexInt;
90 
91 // template tricks
92 template<typename T>
94  typedef T type;
95 };
96 
97 template<typename T>
98 struct remove_pointers<T*> {
99  typedef T type;
100 };
101 
102 template<typename T>
103 struct remove_pointers<T&> {
104  typedef T type;
105 };
106 
107 // Commonly used enums and types
109 struct MuTime {
110  MuTime() { get(); }
111  MuTime operator-(const MuTime& a) { MuTime b; b.time = time - a.time; return b; };
112  MuTime operator+(const MuTime& a) { MuTime b; b.time = time + a.time; return b; };
113  MuTime operator/(unsigned long a) { MuTime b; b.time = time / a; return b; };
114  MuTime& operator+=(const MuTime& a) { time += a.time; return *this; }
115  MuTime& operator-=(const MuTime& a) { time -= a.time; return *this; }
116  MuTime& operator/=(unsigned long a) { time /= a; return *this; }
117  std::string toString();
118 
119  void clear() { time = 0; }
120  void get();
121  MuTime update();
122 
123  unsigned long time;
124 };
125 std::ostream& operator<< (std::ostream& os, const MuTime& t);
126 
128 std::string buildInfoString();
129 
130 // Some commonly used math helpers
131 template<class T> inline T square(T a) {
132  return a*a;
133 }
134 template<class T> inline T cubed(T a) {
135  return a*a*a;
136 }
137 
138 template<class T> inline T clamp(const T& val, const T& vmin, const T& vmax) {
139  if (val < vmin) return vmin;
140  if (val > vmax) return vmax;
141  return val;
142 }
143 
144 template<class T> inline T nmod(const T& a, const T& b);
145 template<> inline int nmod(const int& a, const int& b) { int c=a%b; return (c<0) ? (c+b) : c; }
146 template<> inline float nmod(const float& a, const float& b) { float c=std::fmod(a,b); return (c<0) ? (c+b) : c; }
147 template<> inline double nmod(const double& a, const double& b) { double c=std::fmod(a,b); return (c<0) ? (c+b) : c; }
148 
149 template<class T> inline T safeDivide(const T& a, const T& b);
150 template<> inline int safeDivide<int>(const int &a, const int& b) { return (b) ? (a/b) : a; }
151 template<> inline float safeDivide<float>(const float &a, const float& b) { return (b) ? (a/b) : a; }
152 template<> inline double safeDivide<double>(const double &a, const double& b) { return (b) ? (a/b) : a; }
153 
154 inline bool c_isnan(float c) {
155  volatile float d=c;
156  return d != d;
157 }
158 
159 } // namespace
160 
161 #endif
162 
163 
Definition: commonkernels.h:22
Definition: general.h:43
std::string buildInfoString()
print info about this mantaflow build, used eg by printBuildInfo in fluidsolver.cpp ...
Definition: general.cpp:102
Definition: general.h:93
Timing class for preformance measuring.
Definition: general.h:109