Average.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: Average.hpp<2>
4//
5// Created: Wed Jul 1 12:25:57 2009
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// Bård Skaflestad <bard.skaflestad@sintef.no>
9//
10// $Date$
11//
12// $Revision$
13//
14//===========================================================================
15
16/*
17 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18 Copyright 2009, 2010 Statoil ASA.
19
20 This file is part of the Open Porous Media project (OPM).
21
22 OPM is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, either version 3 of the License, or
25 (at your option) any later version.
26
27 OPM is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with OPM. If not, see <http://www.gnu.org/licenses/>.
34*/
35
36#ifndef OPM_AVERAGE_HEADER
37#define OPM_AVERAGE_HEADER
38
39
40#include <type_traits>
41#include <cmath>
42
43namespace Opm {
44 namespace utils {
45
46
47
50 template <typename T, typename Tresult>
51 Tresult arithmeticAverage(const T& t1, const T& t2)
52 {
53 // To avoid some user errors, we disallow taking averages of
54 // integral values.
55 static_assert(std::is_integral<T>::value == false, "");
56 Tresult retval(t1);
57 retval += t2;
58 retval *= 0.5;
59 return retval;
60 }
61
62
63
68 template <typename T>
69 T geometricAverage(const T& t1, const T& t2)
70 {
71 // To avoid some user errors, we disallow taking averages of
72 // integral values.
73 static_assert(std::is_integral<T>::value == false, "");
74 if (t1*t2 <= 0)
75 return 0;
76 return std::sqrt(t1*t2);
77 }
78
79
80
83 template <typename T>
84 T harmonicAverage(const T& t1, const T& t2)
85 {
86 // To avoid some user errors, we disallow taking averages of
87 // integral values.
88 static_assert(std::is_integral<T>::value == false, "");
89 if (t1*t2 <= 0)
90 return 0;
91 return (2*t1*t2)/(t1 + t2);
92 }
93
94
95
96 } // namespace utils
97} // namespace Opm
98
99
100
101#endif // OPM_AVERAGE_HEADER
T geometricAverage(const T &t1, const T &t2)
Definition: Average.hpp:69
Tresult arithmeticAverage(const T &t1, const T &t2)
Definition: Average.hpp:51
T harmonicAverage(const T &t1, const T &t2)
Definition: Average.hpp:84
Definition: ImplicitAssembly.hpp:43