COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
Functions.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #ifndef cf3_Math_Functions_hpp
8 #define cf3_Math_Functions_hpp
9 
11 
12 #include "common/CF.hpp"
13 #include "math/LibMath.hpp"
14 
16 
17 namespace cf3 {
18 
19  namespace math {
20 
22 
27 namespace Functions
28 {
34  Real signum (const Real& value);
35 
40  Real sign(const Real& value);
41 
47  Real change_sign(const Real& value, const Real& newSignValue);
48 
54  Real heavyside(const Real& value);
55 
58  template <class T1, class T2>
59  Real get_distance(const T1& n1, const T2& n2);
60 
64  Uint factorial(const Uint& n);
65 
74  template <class T1, class T2, class T3, class T4>
75  Real mixed_product (const T1& v1,
76  const T2& v2,
77  const T3& v3,
78  T4& temp);
79 
85  template <class T1, class T2, class T3>
86  void cross_product (const T1& v1,
87  const T2& v2,
88  T3& result);
89 
96  template <class T1, class T2>
97  Real inner_product (const T1& v1, const T2& v2);
98 
104  template <class T1, class T2, class T3>
105  void tensor_product(const T1& v1, const T2& v2, T3& m);
106 
107 
109 
110  inline Real signum (const Real& value)
111  {
112  if ( value < 0.0 ) return -1.0;
113  if ( value == 0.0 ) return 0.0;
114  return 1.0;
115  }
116 
117 
118  inline Real sign(const Real& value)
119  {
120  if (value < 0.0) return -1.0;
121  else return 1.0;
122  }
123 
124 
125  inline Real change_sign(const Real& value, const Real& newSignValue)
126  {
127  return newSignValue >= 0 ? (value >=0 ? value : -value) : (value >= 0 ? -value : value);
128  }
129 
130 
131  inline Real heavyside(const Real& value)
132  {
133  if (value < 0.0) return 0.0;
134  if (value == 0.0) return 0.5;
135  return 1.0;
136  }
137 
138 
139  template <class T1, class T2>
140  inline Real get_distance(const T1& n1, const T2& n2)
141  {
142  cf3_assert(n1.size() == n2.size());
143 
144  Real dist = 0.;
145  const Uint size = n1.size();
146  for (Uint i = 0; i < size; ++i)
147  {
148  const Real diff = n1[i] - n2[i];
149  dist += diff*diff;
150  }
151  return std::sqrt(dist);
152  }
153 
154 
155  inline Uint factorial(const Uint& n)
156  {
157  if (n<2)
158  return 1;
159  else
160  return (n*factorial(n-1));
161  }
162 
163 
164  template <class T1, class T2, class T3, class T4>
165  inline Real mixed_product (const T1& v1,
166  const T2& v2,
167  const T3& v3,
168  T4& temp)
169  {
170  // sanity checks
171  cf3_assert(v1.size() == 3);
172  cf3_assert(v2.size() == 3);
173  cf3_assert(v3.size() == 3);
174  cf3_assert(temp.size() == 3);
175 
176  cross_product(v1, v2, temp);
177  return inner_product(v3, temp);
178  }
179 
180  template <class T1, class T2, class T3>
181  inline void cross_product (const T1& v1,
182  const T2& v2,
183  T3& result)
184  {
185  // sanity checks
186  cf3_assert(v1.size() == 3);
187  cf3_assert(v2.size() == 3);
188  cf3_assert(result.size() == 3);
189 
190  result[0] = v1[1]*v2[2] - v1[2]*v2[1];
191  result[1] = -v1[0]*v2[2] + v1[2]*v2[0];
192  result[2] = v1[0]*v2[1] - v1[1]*v2[0];
193  }
194 
195 
196  template <class T1, class T2>
197  inline Real inner_product (const T1& v1, const T2& v2)
198  {
199  cf3_assert(v1.size() == v2.size());
200 
201  const Uint size = v1.size();
202  Real result = 0.0;
203  for (Uint i = 0; i < size; ++i)
204  result += v1[i]*v2[i];
205  return result;
206  }
207 
208 
209  template <class T1, class T2, class T3>
210  inline void tensor_product(const T1& v1, const T2& v2, T3& m)
211  {
212  cf3_assert(m.getNbRows() == v1.size());
213  cf3_assert(m.getNbColumns() == v2.size());
214 
215  const Uint v1size = v1.size();
216  const Uint v2size = v2.size();
217  for (Uint i = 0; i < v1size; ++i) {
218  for (Uint j = 0; j < v2size; ++j) {
219  m(i,j) = v1[i]*v2[j];
220  }
221  }
222  }
223 
224 
225 
226 } // end namespace Functions
227 
229 
230  } // math
231 
232 } // cf3
233 
235 
236 #endif // cf3_Math_Functions_hpp
Real get_distance(const T1 &n1, const T2 &n2)
Definition: Functions.hpp:140
void cross_product(const T1 &v1, const T2 &v2, T3 &result)
Definition: Functions.hpp:181
#define cf3_assert(a)
Definition: Assertions.hpp:93
Real sign(const Real &value)
Definition: Functions.hpp:118
bool diff(const mesh::Mesh &a, const mesh::Mesh &b, const Uint max_ulps)
Calculates the difference between two meshes.
Definition: MeshDiff.cpp:128
Real change_sign(const Real &value, const Real &newSignValue)
Definition: Functions.hpp:125
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Uint factorial(const Uint &n)
Definition: Functions.hpp:155
Real mixed_product(const T1 &v1, const T2 &v2, const T3 &v3, T4 &temp)
Definition: Functions.hpp:165
Real signum(const Real &value)
Definition: Functions.hpp:110
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Real heavyside(const Real &value)
Definition: Functions.hpp:131
coolfluid3 header, included almost everywhere
Real inner_product(const T1 &v1, const T2 &v2)
Definition: Functions.hpp:197
void tensor_product(const T1 &v1, const T2 &v2, T3 &m)
Definition: Functions.hpp:210
Send comments to:
COOLFluiD Web Admin