deal.II version GIT relicensing-2167-g9622207b8f 2024-11-21 12:40:00+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
floating_point_comparator.h
Go to the documentation of this file.
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
21#include <deal.II/base/table.h>
22#include <deal.II/base/tensor.h>
24
25#include <bitset>
26#include <vector>
27
29
43template <typename Number>
45{
47 typename ::internal::VectorizedArrayTrait<Number>::value_type;
48 static constexpr std::size_t width =
50
57 {
58 less,
59 equal,
61 };
62
68 const bool use_absolute_tolerance = true,
69 const std::bitset<width> &mask = std::bitset<width>().flip());
70
72
74
81 template <typename T>
82 bool
83 operator()(const T &object1, const T &object2) const;
84
90 template <typename T>
92 compare(const std::vector<T> &v1, const std::vector<T> &v2) const;
93
97 template <std::size_t dim, typename T>
99 compare(const std::array<T, dim> &t1, const std::array<T, dim> &t2) const;
100
104 template <int rank, int dim, typename T>
106 compare(const Tensor<rank, dim, T> &t1, const Tensor<rank, dim, T> &t2) const;
107
111 template <int rank, int dim, int spacedim, typename T>
115
119 template <typename T>
121 compare(const Table<2, T> &t1, const Table<2, T> &t2) const;
122
127 compare(const ScalarNumber s1, const ScalarNumber s2) const;
128
135
136private:
139 const std::bitset<width> mask;
140};
141
142
143/* ------------------------------------------------------------------ */
144
145
146template <typename Number>
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
160template <typename Number>
161template <typename T>
162bool
164 const T &object2) const
165{
166 return compare(object1, object2) == ComparisonResult::less;
167}
168
169
170
171template <typename Number>
172template <typename T>
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
194template <typename Number>
195template <std::size_t dim, typename T>
197FloatingPointComparator<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
211template <typename Number>
212template <int rank, int dim, typename T>
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
228template <typename Number>
229template <int rank, int dim, int spacedim, typename T>
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
246template <typename Number>
247template <typename T>
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
267template <typename Number>
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
288template <typename Number>
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
307
308#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
const unsigned int v1
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcMessage(std::string arg1)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
bool operator()(const T &object1, const T &object2) const
FloatingPointComparator(const FloatingPointComparator &rhs)=default
const std::bitset< width > mask
FloatingPointComparator(FloatingPointComparator &&rhs) noexcept=default
static constexpr std::size_t width
typename ::internal::VectorizedArrayTrait< Number >::value_type ScalarNumber
ComparisonResult compare(const std::vector< T > &v1, const std::vector< T > &v2) const
FloatingPointComparator(const ScalarNumber tolerance, const bool use_absolute_tolerance=true, const std::bitset< width > &mask=std::bitset< width >().flip())
static constexpr std::size_t width()