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  Copyright (C) 2015 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
30 #ifndef OPM_MATERIAL_MATH_TOOLBOX_HPP
31 #define OPM_MATERIAL_MATH_TOOLBOX_HPP
32 
33 #include <cmath>
34 #include <algorithm>
35 #include <type_traits>
36 
37 namespace Opm {
38 template <class Evaluation, bool isScalar = std::is_floating_point<Evaluation>::value >
39 struct MathToolbox;
40 
41 template <class LhsEval, class RhsEval,
42  bool lhsIsScalar = std::is_floating_point<LhsEval>::value,
43  bool rhsIsScalar = std::is_floating_point<RhsEval>::value>
45 
46 template <class LhsEval, class RhsEval>
47 struct ToLhsEvalHelper<LhsEval, RhsEval, true, true> {
48  static LhsEval exec(const RhsEval& eval)
49  { return eval; }
50 };
51 
52 template <class LhsEval, class RhsEval>
53 struct ToLhsEvalHelper<LhsEval, RhsEval, true, false> {
54  static LhsEval exec(const RhsEval& eval)
55  { return eval.value; }
56 };
57 
58 template <class LhsEval, class RhsEval>
59 struct ToLhsEvalHelper<LhsEval, RhsEval, false, false> {
60  static LhsEval exec(const RhsEval& eval)
61  { return eval; }
62 };
63 
64 /*
65  * \brief A traits class which provides basic mathematical functions for arbitrary scalar
66  * floating point values.
67  *
68  * The reason why this is done in such a complicated way is to enable other approaches,
69  * in particular automatic differentiation based ones.
70  */
71 template <class ScalarT>
72 struct MathToolbox<ScalarT, true>
73 {
74 public:
78  typedef ScalarT Scalar;
79 
87  typedef ScalarT Evaluation;
88 
95  static Scalar value(const Evaluation& eval)
96  { return eval; }
97 
105  static Scalar createConstant(Scalar value)
106  { return value; }
107 
115  static Scalar createVariable(Scalar value, int /* varIdx */)
116  { return value; }
117 
129  template <class LhsEval>
130  static LhsEval toLhs(const Evaluation& eval)
132 
143  static Scalar passThroughOrCreateConstant(Scalar value)
144  { return value; }
145 
147  // arithmetic functions
149 
151  static Scalar max(Scalar arg1, Scalar arg2)
152  { return std::max(arg1, arg2); }
153 
155  static Scalar min(Scalar arg1, Scalar arg2)
156  { return std::min(arg1, arg2); }
157 
159  static Scalar abs(Scalar arg)
160  { return std::abs(arg); }
161 
163  static Scalar tan(Scalar arg)
164  { return std::tan(arg); }
165 
167  static Scalar atan(Scalar arg)
168  { return std::atan(arg); }
169 
171  static Scalar atan2(Scalar arg1, Scalar arg2)
172  { return std::atan2(arg1, arg2); }
173 
175  static Scalar sin(Scalar arg)
176  { return std::sin(arg); }
177 
179  static Scalar asin(Scalar arg)
180  { return std::asin(arg); }
181 
183  static Scalar cos(Scalar arg)
184  { return std::cos(arg); }
185 
187  static Scalar acos(Scalar arg)
188  { return std::acos(arg); }
189 
191  static Scalar sqrt(Scalar arg)
192  { return std::sqrt(arg); }
193 
195  static Scalar exp(Scalar arg)
196  { return std::exp(arg); }
197 
199  static Scalar log(Scalar arg)
200  { return std::log(arg); }
201 
203  static Scalar pow(Scalar base, Scalar exp)
204  { return std::pow(base, exp); }
205 };
206 
207 } // namespace Opm
208 
209 #endif
ScalarT Scalar
The type used to represent scalar values.
Definition: MathToolbox.hpp:78
static LhsEval toLhs(const Evaluation &eval)
Given a function evaluation, constrain it to its value (if necessary).
Definition: MathToolbox.hpp:130
static Scalar value(const Evaluation &eval)
Return the value of the function at a given evaluation point.
Definition: MathToolbox.hpp:95
static LhsEval exec(const RhsEval &eval)
Definition: MathToolbox.hpp:54
Definition: MathToolbox.hpp:39
Definition: Air_Mesitylene.hpp:31
static Scalar passThroughOrCreateConstant(Scalar value)
Pass a value through if it is an evaluation, or create a constant evaluation if it is a scalar...
Definition: MathToolbox.hpp:143
Evaluation< Scalar, VarSetTag, numVars > max(const Evaluation< Scalar, VarSetTag, numVars > &x1, const Evaluation< Scalar, VarSetTag, numVars > &x2)
Definition: Math.hpp:114
Evaluation< Scalar, VarSetTag, numVars > sqrt(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:278
Evaluation< Scalar, VarSetTag, numVars > cos(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:248
Evaluation< Scalar, VarSetTag, numVars > atan2(const Evaluation< Scalar, VarSetTag, numVars > &x, const Evaluation< Scalar, VarSetTag, numVars > &y)
Definition: Math.hpp:198
static Scalar exp(Scalar arg)
The natural exponentiation of a value.
Definition: MathToolbox.hpp:195
ScalarT Evaluation
The type used to represent function evaluations.
Definition: MathToolbox.hpp:87
static Scalar sin(Scalar arg)
The sine of a value.
Definition: MathToolbox.hpp:175
static Scalar atan(Scalar arg)
The arcus tangens of a value.
Definition: MathToolbox.hpp:167
static Scalar cos(Scalar arg)
The cosine of a value.
Definition: MathToolbox.hpp:183
static LhsEval exec(const RhsEval &eval)
Definition: MathToolbox.hpp:48
Evaluation< Scalar, VarSetTag, numVars > atan(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:183
static Scalar asin(Scalar arg)
The arcus sine of a value.
Definition: MathToolbox.hpp:179
static Scalar createVariable(Scalar value, int)
Given a scalar value, return an evaluation of a linear function.
Definition: MathToolbox.hpp:115
Evaluation< Scalar, VarSetTag, numVars > log(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:369
Evaluation< Scalar, VarSetTag, numVars > exp(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:295
Evaluation< Scalar, VarSetTag, numVars > abs(const Evaluation< Scalar, VarSetTag, numVars > &)
Definition: Math.hpp:41
static Scalar min(Scalar arg1, Scalar arg2)
The minimum of two arguments.
Definition: MathToolbox.hpp:155
Evaluation< Scalar, VarSetTag, numVars > min(const Evaluation< Scalar, VarSetTag, numVars > &x1, const Evaluation< Scalar, VarSetTag, numVars > &x2)
Definition: Math.hpp:61
static Scalar acos(Scalar arg)
The arcus cosine of a value.
Definition: MathToolbox.hpp:187
static Scalar tan(Scalar arg)
The tangens of a value.
Definition: MathToolbox.hpp:163
static Scalar abs(Scalar arg)
The absolute value.
Definition: MathToolbox.hpp:159
Evaluation< Scalar, VarSetTag, numVars > asin(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:233
static Scalar createConstant(Scalar value)
Given a scalar value, return an evaluation of a constant function.
Definition: MathToolbox.hpp:105
Definition: MathToolbox.hpp:44
static Scalar pow(Scalar base, Scalar exp)
Exponentiation to an arbitrary base.
Definition: MathToolbox.hpp:203
Evaluation< Scalar, VarSetTag, numVars > sin(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:218
Evaluation< Scalar, VarSetTag, numVars > pow(const Evaluation< Scalar, VarSetTag, numVars > &base, Scalar exp)
Definition: Math.hpp:312
Evaluation< Scalar, VarSetTag, numVars > tan(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:167
static Scalar sqrt(Scalar arg)
The square root of a value.
Definition: MathToolbox.hpp:191
static Scalar max(Scalar arg1, Scalar arg2)
The maximum of two arguments.
Definition: MathToolbox.hpp:151
Evaluation< Scalar, VarSetTag, numVars > acos(const Evaluation< Scalar, VarSetTag, numVars > &x)
Definition: Math.hpp:263
static LhsEval exec(const RhsEval &eval)
Definition: MathToolbox.hpp:60
static Scalar log(Scalar arg)
The natural logarithm of a value.
Definition: MathToolbox.hpp:199
static Scalar atan2(Scalar arg1, Scalar arg2)
The arcus tangens of a value.
Definition: MathToolbox.hpp:171