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