| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2022 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_base_floating_point_copmerator_h |
| 16 |
#define dealii_base_floating_point_copmerator_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/derivative_form.h> |
| 21 |
#include <deal.II/base/table.h> |
| 22 |
#include <deal.II/base/tensor.h> |
| 23 |
#include <deal.II/base/vectorization.h> |
| 24 |
|
| 25 |
#include <bitset> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
DEAL_II_NAMESPACE_OPEN |
| 29 |
|
| 30 |
/** |
| 31 |
* A class that is used to compare floating point arrays (e.g. std::vector, |
| 32 |
* Tensor<1,dim>, etc.). The most common use case of this comparator |
| 33 |
* is for detecting arrays with the same content for the sake of |
| 34 |
* compression. The idea of this class is to consider two arrays as |
| 35 |
* equal if they are the same within a given tolerance. We use this |
| 36 |
* comparator class within a std::map<> of the given arrays. Note that this |
| 37 |
* comparison operator does not satisfy all the mathematical properties one |
| 38 |
* usually wants to have (consider e.g. the numbers a=0, b=0.1, c=0.2 with |
| 39 |
* tolerance 0.15; the operator gives a<c, but neither a<b? nor b<c? is |
| 40 |
* satisfied). This is not a problem in the use cases for this class, but be |
| 41 |
* careful when using it in other contexts. |
| 42 |
*/ |
| 43 |
template <typename Number> |
| 44 |
struct FloatingPointComparator |
| 45 |
{ |
| 46 |
using ScalarNumber = |
| 47 |
typename dealii::internal::VectorizedArrayTrait<Number>::value_type; |
| 48 |
static constexpr std::size_t width = |
| 49 |
dealii::internal::VectorizedArrayTrait<Number>::width(); |
| 50 |
|
| 51 |
/** |
| 52 |
* An enum to decode whether a particular comparison returns less, larger, |
| 53 |
* or equal. This is needed beyond the regular operator() because of |
| 54 |
* efficiency reasons in nested checks as supported by this class. |
| 55 |
*/ |
| 56 |
enum class ComparisonResult |
| 57 |
{ |
| 58 |
less, |
| 59 |
equal, |
| 60 |
greater |
| 61 |
}; |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructor. |
| 65 |
*/ |
| 66 |
FloatingPointComparator( |
| 67 |
const ScalarNumber tolerance, |
| 68 |
const bool use_absolute_tolerance = true, |
| 69 |
const std::bitset<width> &mask = std::bitset<width>().flip()); |
| 70 |
|
| 71 |
FloatingPointComparator(const FloatingPointComparator &rhs) = default; |
| 72 |
|
| 73 |
FloatingPointComparator(FloatingPointComparator &&rhs) noexcept = default; |
| 74 |
|
| 75 |
/** |
| 76 |
* Compare two objects of the same type and return `true` in case the first |
| 77 |
* object is less than the second one. This function calls the compare() |
| 78 |
* functions below, so only types supported by compare() will work with this |
| 79 |
* function. |
| 80 |
*/ |
| 81 |
template <typename T> |
| 82 |
bool |
| 83 |
operator()(const T &object1, const T &object2) const; |
| 84 |
|
| 85 |
/** |
| 86 |
* Compare two vectors of numbers (not necessarily of the same length), |
| 87 |
* where vectors of different lengths are first sorted by their length and |
| 88 |
* then by the entries. |
| 89 |
*/ |
| 90 |
template <typename T> |
| 91 |
ComparisonResult |
| 92 |
compare(const std::vector<T> &v1, const std::vector<T> &v2) const; |
| 93 |
|
| 94 |
/** |
| 95 |
* Compare two arrays. |
| 96 |
*/ |
| 97 |
template <std::size_t dim, typename T> |
| 98 |
ComparisonResult |
| 99 |
compare(const std::array<T, dim> &t1, const std::array<T, dim> &t2) const; |
| 100 |
|
| 101 |
/** |
| 102 |
* Compare two tensors. |
| 103 |
*/ |
| 104 |
template <int rank, int dim, typename T> |
| 105 |
ComparisonResult |
| 106 |
compare(const Tensor<rank, dim, T> &t1, const Tensor<rank, dim, T> &t2) const; |
| 107 |
|
| 108 |
/** |
| 109 |
* Compare two derivative forms. |
| 110 |
*/ |
| 111 |
template <int rank, int dim, int spacedim, typename T> |
| 112 |
ComparisonResult |
| 113 |
compare(const DerivativeForm<rank, dim, spacedim, T> &t1, |
| 114 |
const DerivativeForm<rank, dim, spacedim, T> &t2) const; |
| 115 |
|
| 116 |
/** |
| 117 |
* Compare two tables. |
| 118 |
*/ |
| 119 |
template <typename T> |
| 120 |
ComparisonResult |
| 121 |
compare(const Table<2, T> &t1, const Table<2, T> &t2) const; |
| 122 |
|
| 123 |
/** |
| 124 |
* Compare two scalar numbers. |
| 125 |
*/ |
| 126 |
ComparisonResult |
| 127 |
compare(const ScalarNumber s1, const ScalarNumber s2) const; |
| 128 |
|
| 129 |
/** |
| 130 |
* Compare two VectorizedArray instances. |
| 131 |
*/ |
| 132 |
ComparisonResult |
| 133 |
compare(const VectorizedArray<ScalarNumber, width> &v1, |
| 134 |
const VectorizedArray<ScalarNumber, width> &v2) const; |
| 135 |
|
| 136 |
private: |
| 137 |
const ScalarNumber tolerance; |
| 138 |
const bool use_absolute_tolerance; |
| 139 |
const std::bitset<width> mask; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
/* ------------------------------------------------------------------ */ |
| 144 |
|
| 145 |
|
| 146 |
template <typename Number> |
| 147 |
FloatingPointComparator<Number>::FloatingPointComparator( |
| 148 |
const ScalarNumber tolerance, |
| 149 |
const bool use_absolute_tolerance, |
| 150 |
const std::bitset<width> &mask) |
| 151 |
: tolerance(tolerance) |
| 152 |
, use_absolute_tolerance(use_absolute_tolerance) |
| 153 |
, mask(mask) |
| 154 |
{ |
| 155 |
Assert(mask.count() > 0, ExcMessage("No component selected")); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
template <typename Number> |
| 161 |
template <typename T> |
| 162 |
bool |
| 163 |
FloatingPointComparator<Number>::operator()(const T &object1, |
| 164 |
const T &object2) const |
| 165 |
{ |
| 166 |
return compare(object1, object2) == ComparisonResult::less; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
template <typename Number> |
| 172 |
template <typename T> |
| 173 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 174 |
FloatingPointComparator<Number>::compare(const std::vector<T> &v1, |
| 175 |
const std::vector<T> &v2) const |
| 176 |
{ |
| 177 |
const unsigned int s1 = v1.size(), s2 = v2.size(); |
| 178 |
if (s1 < s2) |
| 179 |
return ComparisonResult::less; |
| 180 |
else if (s1 > s2) |
| 181 |
return ComparisonResult::greater; |
| 182 |
else |
| 183 |
for (unsigned int i = 0; i < s1; ++i) |
| 184 |
{ |
| 185 |
const ComparisonResult result = compare(v1[i], v2[i]); |
| 186 |
if (result != ComparisonResult::equal) |
| 187 |
return result; |
| 188 |
} |
| 189 |
return ComparisonResult::equal; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
template <typename Number> |
| 195 |
template <std::size_t dim, typename T> |
| 196 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 197 |
FloatingPointComparator<Number>::compare(const std::array<T, dim> &t1, |
| 198 |
const std::array<T, dim> &t2) const |
| 199 |
{ |
| 200 |
for (unsigned int i = 0; i < t1.size(); ++i) |
| 201 |
{ |
| 202 |
const ComparisonResult result = compare(t1[i], t2[i]); |
| 203 |
if (result != ComparisonResult::equal) |
| 204 |
return result; |
| 205 |
} |
| 206 |
return ComparisonResult::equal; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
template <typename Number> |
| 212 |
template <int rank, int dim, typename T> |
| 213 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 214 |
FloatingPointComparator<Number>::compare(const Tensor<rank, dim, T> &t1, |
| 215 |
const Tensor<rank, dim, T> &t2) const |
| 216 |
{ |
| 217 |
for (unsigned int i = 0; i < dim; ++i) |
| 218 |
{ |
| 219 |
const ComparisonResult result = compare(t1[i], t2[i]); |
| 220 |
if (result != ComparisonResult::equal) |
| 221 |
return result; |
| 222 |
} |
| 223 |
return ComparisonResult::equal; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
template <typename Number> |
| 229 |
template <int rank, int dim, int spacedim, typename T> |
| 230 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 231 |
FloatingPointComparator<Number>::compare( |
| 232 |
const DerivativeForm<rank, dim, spacedim, T> &t1, |
| 233 |
const DerivativeForm<rank, dim, spacedim, T> &t2) const |
| 234 |
{ |
| 235 |
for (unsigned int i = 0; i < spacedim; ++i) |
| 236 |
{ |
| 237 |
const ComparisonResult result = compare(t1[i], t2[i]); |
| 238 |
if (result != ComparisonResult::equal) |
| 239 |
return result; |
| 240 |
} |
| 241 |
return ComparisonResult::equal; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
template <typename Number> |
| 247 |
template <typename T> |
| 248 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 249 |
FloatingPointComparator<Number>::compare(const Table<2, T> &t1, |
| 250 |
const Table<2, T> &t2) const |
| 251 |
{ |
| 252 |
AssertDimension(t1.size(0), t2.size(0)); |
| 253 |
AssertDimension(t1.size(1), t2.size(1)); |
| 254 |
|
| 255 |
for (unsigned int i = 0; i < t1.size(0); ++i) |
| 256 |
for (unsigned int j = 0; j < t1.size(1); ++j) |
| 257 |
{ |
| 258 |
const ComparisonResult result = compare(t1[i][j], t2[i][j]); |
| 259 |
if (result != ComparisonResult::equal) |
| 260 |
return result; |
| 261 |
} |
| 262 |
return ComparisonResult::equal; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
template <typename Number> |
| 268 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 269 |
FloatingPointComparator<Number>::compare(const ScalarNumber s1, |
| 270 |
const ScalarNumber s2) const |
| 271 |
{ |
| 272 |
if (width == 1 || mask[0]) |
| 273 |
{ |
| 274 |
const ScalarNumber tolerance = |
| 275 |
use_absolute_tolerance ? |
| 276 |
this->tolerance : |
| 277 |
((std::abs(s1) + std::abs(s2)) * this->tolerance); |
| 278 |
|
| 279 |
if (s1 < s2 - tolerance) |
| 280 |
return ComparisonResult::less; |
| 281 |
else if (s1 > s2 + tolerance) |
| 282 |
return ComparisonResult::greater; |
| 283 |
} |
| 284 |
|
| 285 |
return ComparisonResult::equal; |
| 286 |
} |
| 287 |
|
| 288 |
template <typename Number> |
| 289 |
typename FloatingPointComparator<Number>::ComparisonResult |
| 290 |
FloatingPointComparator<Number>::compare( |
| 291 |
const VectorizedArray<ScalarNumber, width> &v1, |
| 292 |
const VectorizedArray<ScalarNumber, width> &v2) const |
| 293 |
{ |
| 294 |
for (unsigned int i = 0; i < width; ++i) |
| 295 |
if (mask[i]) |
| 296 |
{ |
| 297 |
const ComparisonResult result = compare(v1[i], v2[i]); |
| 298 |
if (result != ComparisonResult::equal) |
| 299 |
return result; |
| 300 |
} |
| 301 |
|
| 302 |
return ComparisonResult::equal; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
DEAL_II_NAMESPACE_CLOSE |
| 307 |
|
| 308 |
#endif |