MathToolbox.hpp
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
32 #ifndef OPM_MATERIAL_MATH_TOOLBOX_HPP
33 #define OPM_MATERIAL_MATH_TOOLBOX_HPP
34 
35 #include <cmath>
36 #include <algorithm>
37 #include <type_traits>
38 
39 namespace Opm {
40 /*
41  * \brief A traits class which provides basic mathematical functions for arbitrary scalar
42  * floating point values.
43  *
44  * The reason why this is done in such a complicated way is to enable other approaches,
45  * in particular automatic differentiation based ones.
46  */
47 template <class ScalarT>
49 {
50  static_assert(std::is_floating_point<ScalarT>::value,
51  "This class expects floating point scalars! (specialization missing?)");
52 public:
56  typedef ScalarT Scalar;
57 
65  typedef ScalarT ValueType;
66 
75 
83  { return value; }
84 
92  { return value; }
93 
102  { return value; }
103 
111  static Scalar createVariable(Scalar value, unsigned /*varIdx*/)
112  { return value; }
113 
125  template <class LhsEval>
126  static LhsEval decay(Scalar value)
127  {
128  static_assert(std::is_floating_point<LhsEval>::value,
129  "The left-hand side must be a primitive floating point type!");
130 
131  return value;
132  }
133 
137  static bool isSame(Scalar a, Scalar b, Scalar tolerance)
138  {
139  Scalar valueDiff = a - b;
140  Scalar denom = std::max<Scalar>(1.0, std::abs(a + b));
141 
142  return std::abs(valueDiff) < tolerance || std::abs(valueDiff)/denom < tolerance;
143  }
144 
146  // arithmetic functions
148 
150  static Scalar max(Scalar arg1, Scalar arg2)
151  { return std::max(arg1, arg2); }
152 
154  static Scalar min(Scalar arg1, Scalar arg2)
155  { return std::min(arg1, arg2); }
156 
158  static Scalar abs(Scalar arg)
159  { return std::abs(arg); }
160 
162  static Scalar tan(Scalar arg)
163  { return std::tan(arg); }
164 
166  static Scalar atan(Scalar arg)
167  { return std::atan(arg); }
168 
170  static Scalar atan2(Scalar arg1, Scalar arg2)
171  { return std::atan2(arg1, arg2); }
172 
174  static Scalar sin(Scalar arg)
175  { return std::sin(arg); }
176 
178  static Scalar asin(Scalar arg)
179  { return std::asin(arg); }
180 
182  static Scalar cos(Scalar arg)
183  { return std::cos(arg); }
184 
186  static Scalar acos(Scalar arg)
187  { return std::acos(arg); }
188 
190  static Scalar sqrt(Scalar arg)
191  { return std::sqrt(arg); }
192 
194  static Scalar exp(Scalar arg)
195  { return std::exp(arg); }
196 
198  static Scalar log(Scalar arg)
199  { return std::log(arg); }
200 
202  static Scalar pow(Scalar base, Scalar exp)
203  { return std::pow(base, exp); }
204 
206  static bool isfinite(Scalar arg)
207  { return std::isfinite(arg); }
208 
210  static bool isnan(Scalar arg)
211  { return std::isnan(arg); }
212 };
213 
214 template <class Eval1, class Eval2>
216 {
217  typedef typename std::remove_const< typename std::remove_reference<Eval1>::type >::type T;
218  typedef typename std::remove_const< typename std::remove_reference<Eval2>::type >::type U;
219 
220  //static_assert(std::is_constructible<T, U>::value || std::is_constructible<U, T>::value,
221  // "One of the argument types must be constructible to the other");
222 
223  typedef typename std::conditional<std::is_constructible<T, U>::value,
224  T,
225  U>::type type;
226 };
227 
228 // these are convenience functions for not having to type MathToolbox<Scalar>::foo()
229 template <class Evaluation, class Scalar>
230 Evaluation constant(const Scalar& value)
232 
233 template <class Evaluation, class Scalar>
234 Evaluation variable(const Scalar& value, unsigned idx)
235 { return Opm::MathToolbox<Evaluation>::createVariable(value, idx); }
236 
237 template <class ResultEval, class Evaluation>
238 auto decay(const Evaluation& value)
239  -> decltype(Opm::MathToolbox<Evaluation>::template decay<ResultEval>(value))
240 { return Opm::MathToolbox<Evaluation>::template decay<ResultEval>(value); }
241 
242 template <class Evaluation>
243 auto getValue(const Evaluation& val)
244  -> decltype(Opm::MathToolbox<Evaluation>::value(val))
246 
247 template <class Evaluation>
248 auto scalarValue(const Evaluation& val)
251 
252 template <class Evaluation1, class Evaluation2>
253 typename ReturnEval_<Evaluation1, Evaluation2>::type
254 max(const Evaluation1& arg1, const Evaluation2& arg2)
256 
257 template <class Evaluation1, class Evaluation2>
258 typename ReturnEval_<Evaluation1, Evaluation2>::type
259 min(const Evaluation1& arg1, const Evaluation2& arg2)
261 
262 template <class Evaluation>
263 Evaluation abs(const Evaluation& value)
264 { return Opm::MathToolbox<Evaluation>::abs(value); }
265 
266 template <class Evaluation>
267 Evaluation tan(const Evaluation& value)
268 { return Opm::MathToolbox<Evaluation>::tan(value); }
269 
270 template <class Evaluation>
271 Evaluation atan(const Evaluation& value)
272 { return Opm::MathToolbox<Evaluation>::atan(value); }
273 
274 template <class Evaluation1, class Evaluation2>
275 typename ReturnEval_<Evaluation1, Evaluation2>::type
276 atan2(const Evaluation1& value1, const Evaluation2& value2)
278 
279 template <class Evaluation>
280 Evaluation sin(const Evaluation& value)
281 { return Opm::MathToolbox<Evaluation>::sin(value); }
282 
283 template <class Evaluation>
284 Evaluation asin(const Evaluation& value)
285 { return Opm::MathToolbox<Evaluation>::asin(value); }
286 
287 template <class Evaluation>
288 Evaluation cos(const Evaluation& value)
289 { return Opm::MathToolbox<Evaluation>::cos(value); }
290 
291 template <class Evaluation>
292 Evaluation acos(const Evaluation& value)
293 { return Opm::MathToolbox<Evaluation>::acos(value); }
294 
295 template <class Evaluation>
296 Evaluation sqrt(const Evaluation& value)
297 { return Opm::MathToolbox<Evaluation>::sqrt(value); }
298 
299 template <class Evaluation>
300 Evaluation exp(const Evaluation& value)
301 { return Opm::MathToolbox<Evaluation>::exp(value); }
302 
303 template <class Evaluation>
304 Evaluation log(const Evaluation& value)
305 { return Opm::MathToolbox<Evaluation>::log(value); }
306 
307 template <class Evaluation1, class Evaluation2>
308 typename ReturnEval_<Evaluation1, Evaluation2>::type
309 pow(const Evaluation1& base, const Evaluation2& exp)
311 
312 template <class Evaluation>
313 bool isfinite(const Evaluation& value)
315 
316 template <class Evaluation>
317 bool isnan(const Evaluation& value)
318 { return Opm::MathToolbox<Evaluation>::isnan(value); }
319 
320 } // namespace Opm
321 
322 #endif
323 
Definition: MathToolbox.hpp:215
static Scalar createVariable(Scalar value, unsigned)
Given a scalar value, return an evaluation of a linear function.
Definition: MathToolbox.hpp:111
static Scalar atan2(Scalar arg1, Scalar arg2)
The arcus tangens of a value.
Definition: MathToolbox.hpp:170
static Scalar cos(Scalar arg)
The cosine of a value.
Definition: MathToolbox.hpp:182
static bool isfinite(Scalar arg)
Return true iff the argument&#39;s value and all its derivatives are finite values.
Definition: MathToolbox.hpp:206
static Scalar sin(Scalar arg)
The sine of a value.
Definition: MathToolbox.hpp:174
static Scalar atan(Scalar arg)
The arcus tangens of a value.
Definition: MathToolbox.hpp:166
static Scalar acos(Scalar arg)
The arcus cosine of a value.
Definition: MathToolbox.hpp:186
static Scalar min(Scalar arg1, Scalar arg2)
The minimum of two arguments.
Definition: MathToolbox.hpp:154
Definition: Air_Mesitylene.hpp:33
static Scalar pow(Scalar base, Scalar exp)
Exponentiation to an arbitrary base.
Definition: MathToolbox.hpp:202
ScalarT ValueType
The type used to represent values.
Definition: MathToolbox.hpp:65
static Scalar log(Scalar arg)
The natural logarithm of a value.
Definition: MathToolbox.hpp:198
static bool isnan(Scalar arg)
Return true iff the argument&#39;s value or any of its derivatives are NaN values.
Definition: MathToolbox.hpp:210
static LhsEval decay(Scalar value)
Given a function evaluation, constrain it to its value (if necessary).
Definition: MathToolbox.hpp:126
static Scalar tan(Scalar arg)
The tangens of a value.
Definition: MathToolbox.hpp:162
Definition: MathToolbox.hpp:48
ScalarT Scalar
The type used to represent "primitive" scalar values.
Definition: MathToolbox.hpp:51
Opm::MathToolbox< Scalar > InnerToolbox
The toolbox for the type of value objects.
Definition: MathToolbox.hpp:74
static Scalar asin(Scalar arg)
The arcus sine of a value.
Definition: MathToolbox.hpp:178
static Scalar value(Scalar value)
Return the value of the function at a given evaluation point.
Definition: MathToolbox.hpp:82
static Scalar max(Scalar arg1, Scalar arg2)
The maximum of two arguments.
Definition: MathToolbox.hpp:150
static Scalar abs(Scalar arg)
The absolute value.
Definition: MathToolbox.hpp:158
static bool isSame(Scalar a, Scalar b, Scalar tolerance)
Returns true if two values are identical up to a specified tolerance.
Definition: MathToolbox.hpp:137
static Scalar sqrt(Scalar arg)
The square root of a value.
Definition: MathToolbox.hpp:190
static Scalar createConstant(Scalar value)
Given a scalar value, return an evaluation of a constant function.
Definition: MathToolbox.hpp:101
static Scalar exp(Scalar arg)
The natural exponentiation of a value.
Definition: MathToolbox.hpp:194
static Scalar scalarValue(Scalar value)
Return the primitive scalar value of a value object.
Definition: MathToolbox.hpp:91