opm-common
cmp.hpp
1 /*
2  Copyright 2016 Statoil ASA.
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 3 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 
20 #ifndef COMMON_UTIL_NUMERIC_CMP
21 #define COMMON_UTIL_NUMERIC_CMP
22 
23 #include <algorithm>
24 #include <cmath>
25 #include <cstddef>
26 #include <cstring>
27 #include <type_traits>
28 #include <vector>
29 
30 namespace Opm {
31 
63 
64  namespace cmp {
65 
66  const double default_abs_epsilon = 1e-8;
67  const double default_rel_epsilon = 1e-5;
68 
69  template<typename T>
70  bool scalar_equal(T value1, T value2, T abs_eps , T rel_eps) {
71  static_assert(std::is_floating_point<T>::value, "Function scalar_equal() A can only be instantiated with floating point types");
72 
73  bool equal = true;
74  T diff = std::fabs(value1 - value2);
75  if (diff > abs_eps) {
76  T scale = std::max(std::fabs(value1), std::fabs(value2));
77 
78  if (diff > scale * rel_eps) {
79  equal = false;
80  }
81  }
82  return equal;
83  }
84 
85 
86  template<typename T>
87  bool scalar_equal(T value1, T value2) {
88  return scalar_equal<T>( value1 , value2 , default_abs_epsilon , default_rel_epsilon );
89  }
90 
91  template<typename T>
92  bool vector_equal(const std::vector<T>& v1, const std::vector<T>& v2, T abs_eps, T rel_eps) {
93  if (v1.size() != v2.size()) {
94  return false;
95  }
96 
97  for (size_t i = 0; i < v1.size(); i++) {
98  if (!scalar_equal<T>( v1[i], v2[i], abs_eps, rel_eps ))
99  return false;
100  }
101 
102  return true;
103  }
104 
105  template<typename T>
106  bool vector_equal(const std::vector<T>& v1, const std::vector<T>& v2) {
107  return vector_equal<T>(v1, v2, default_abs_epsilon, default_rel_epsilon);
108  }
109 
110 
111  template<typename T>
112  bool array_equal(const T* p1, const T* p2, size_t num_elements, T abs_eps, T rel_eps) {
113  if (std::memcmp(p1 , p2 , num_elements * sizeof * p1) == 0)
114  return true;
115  else {
116  size_t index;
117  for (index = 0; index < num_elements; index++) {
118  if (!scalar_equal<T>( p1[index] , p2[index] , abs_eps , rel_eps)) {
119  return false;
120  }
121  }
122  }
123  return true;
124  }
125 
126 
127  template<typename T>
128  bool array_equal(const T* p1, const T* p2, size_t num_elements) {
129  return array_equal<T>(p1, p2, num_elements , default_abs_epsilon, default_rel_epsilon);
130  }
131  }
132 }
133 
134 #endif
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30