Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add fuzzy equals comparisons #3895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ include_HEADERS = \
utils/compare_types.h \
utils/enum_to_string.h \
utils/error_vector.h \
utils/fuzzy_equals.h \
utils/hashing.h \
utils/hashword.h \
utils/ignore_warnings.h \
Expand Down
2 changes: 2 additions & 0 deletions include/base/libmesh_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,8 @@ inline Tnew libmesh_cast_int (Told oldvar)
template <class T>
constexpr std::false_type always_false{};

static constexpr std::size_t libmesh_dim = LIBMESH_DIM;

// build a integer representation of version
#define LIBMESH_VERSION_ID(major,minor,patch) (((major) << 16) | ((minor) << 8) | ((patch) & 0xFF))

Expand Down
1 change: 1 addition & 0 deletions include/include_HEADERS
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ include_HEADERS = \
utils/compare_types.h \
utils/enum_to_string.h \
utils/error_vector.h \
utils/fuzzy_equals.h \
utils/hashing.h \
utils/hashword.h \
utils/ignore_warnings.h \
Expand Down
4 changes: 4 additions & 0 deletions include/libmesh/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ BUILT_SOURCES = \
compare_types.h \
enum_to_string.h \
error_vector.h \
fuzzy_equals.h \
hashing.h \
hashword.h \
ignore_warnings.h \
Expand Down Expand Up @@ -1937,6 +1938,9 @@ enum_to_string.h: $(top_srcdir)/include/utils/enum_to_string.h
error_vector.h: $(top_srcdir)/include/utils/error_vector.h
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@

fuzzy_equals.h: $(top_srcdir)/include/utils/fuzzy_equals.h
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@

hashing.h: $(top_srcdir)/include/utils/hashing.h
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@

Expand Down
24 changes: 14 additions & 10 deletions include/libmesh/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -670,16 +670,17 @@ BUILT_SOURCES = auto_ptr.h dirichlet_boundaries.h dof_map.h \
post_wait_free_buffer.h post_wait_unpack_buffer.h \
post_wait_work.h request.h standard_type.h status.h \
chunked_mapvector.h compare_types.h enum_to_string.h \
error_vector.h hashing.h hashword.h ignore_warnings.h \
int_range.h jacobi_polynomials.h libmesh_nullptr.h \
location_maps.h mapvector.h null_output_iterator.h \
number_lookups.h ostream_proxy.h parameters.h perf_log.h \
perfmon.h plt_loader.h point_locator_base.h \
point_locator_nanoflann.h point_locator_tree.h \
pointer_to_pointer_iter.h pool_allocator.h restore_warnings.h \
simple_range.h statistics.h string_to_enum.h timestamp.h \
topology_map.h tree.h tree_base.h tree_node.h utility.h \
vectormap.h win_gettimeofday.h xdr_cxx.h \
error_vector.h fuzzy_equals.h hashing.h hashword.h \
ignore_warnings.h int_range.h jacobi_polynomials.h \
libmesh_nullptr.h location_maps.h mapvector.h \
null_output_iterator.h number_lookups.h ostream_proxy.h \
parameters.h perf_log.h perfmon.h plt_loader.h \
point_locator_base.h point_locator_nanoflann.h \
point_locator_tree.h pointer_to_pointer_iter.h \
pool_allocator.h restore_warnings.h simple_range.h \
statistics.h string_to_enum.h timestamp.h topology_map.h \
tree.h tree_base.h tree_node.h utility.h vectormap.h \
win_gettimeofday.h xdr_cxx.h \
parallel_communicator_specializations $(am__append_1) \
$(am__append_3) $(am__append_5) $(am__append_7) \
$(am__append_9) $(am__append_11) $(am__append_13) \
Expand Down Expand Up @@ -2273,6 +2274,9 @@ enum_to_string.h: $(top_srcdir)/include/utils/enum_to_string.h
error_vector.h: $(top_srcdir)/include/utils/error_vector.h
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@

fuzzy_equals.h: $(top_srcdir)/include/utils/fuzzy_equals.h
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@

hashing.h: $(top_srcdir)/include/utils/hashing.h
$(AM_V_GEN)rm -f $@ && $(LN_S) -f $< $@

Expand Down
19 changes: 19 additions & 0 deletions include/numerics/numeric_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ class NumericVector : public ReferenceCountedObject<NumericVector<T>>,
*/
Real l2_norm_diff (const NumericVector<T> & other_vec) const;

/**
* \returns The \f$ \ell_1 \f$-norm of \f$ \vec{u} - \vec{v} \f$, where
* \f$ \vec{u} \f$ is \p this.
*/
Real l1_norm_diff (const NumericVector<T> & other_vec) const;

/**
* \returns The size of the vector.
*/
Expand Down Expand Up @@ -1067,6 +1073,19 @@ void NumericVector<T>::swap (NumericVector<T> & v)
std::swap(_type, v._type);
}

template <typename T>
auto
l1_norm(const NumericVector<T> & vec)
{
return vec.l1_norm();
}

template <typename T>
auto
l1_norm_diff(const NumericVector<T> & vec1, const NumericVector<T> & vec2)
{
return vec1.l1_norm_diff(vec2);
}

} // namespace libMesh

Expand Down
3 changes: 3 additions & 0 deletions include/numerics/petsc_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ class PetscMatrix final : public SparseMatrix<T>
virtual void create_submatrix_nosort(SparseMatrix<T> & submatrix,
const std::vector<numeric_index_type> & rows,
const std::vector<numeric_index_type> & cols) const override;

virtual void scale(const T scale) override;

protected:

/**
Expand Down
24 changes: 23 additions & 1 deletion include/numerics/sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
*/
virtual Real linfty_norm () const = 0;

/**
* \returns The l1_norm() of the difference of \p this and \p other_mat
*/
Real l1_norm_diff (const SparseMatrix<T> & other_mat) const;

/**
* \returns \p true if the matrix has been assembled.
*/
Expand Down Expand Up @@ -573,8 +578,12 @@ class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
std::vector<numeric_index_type> & indices,
std::vector<T> & values) const = 0;

protected:
/**
* Scales all elements of this matrix by \p scale
*/
virtual void scale(const T scale);

protected:
/**
* Protected implementation of the create_submatrix and reinit_submatrix
* routines.
Expand Down Expand Up @@ -626,6 +635,19 @@ std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
return os;
}

template <typename T>
auto
l1_norm(const SparseMatrix<T> & mat)
{
return mat.l1_norm();
}

template <typename T>
auto
l1_norm_diff(const SparseMatrix<T> & mat1, const SparseMatrix<T> & mat2)
{
return mat1.l1_norm_diff(mat2);
}

} // namespace libMesh

Expand Down
76 changes: 41 additions & 35 deletions include/numerics/type_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "libmesh/libmesh_common.h"
#include "libmesh/compare_types.h"
#include "libmesh/tensor_tools.h"
#include "libmesh/int_range.h"
#include "libmesh/fuzzy_equals.h"

// C++ includes
#include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
Expand Down Expand Up @@ -333,6 +335,11 @@ class TypeVector
*/
auto norm_sq() const -> decltype(std::norm(T()));

/**
* \returns The L1 norm of the vector
*/
auto l1_norm() const;

/**
* \returns True if all values in the vector are zero
*/
Expand Down Expand Up @@ -974,27 +981,26 @@ bool TypeVector<T>::is_zero() const
return true;
}

template <>
auto
TypeVector<bool>::l1_norm() const;

template <typename T>
inline
bool TypeVector<T>::absolute_fuzzy_equals(const TypeVector<T> & rhs, Real tol) const
auto
TypeVector<T>::l1_norm() const
{
#if LIBMESH_DIM == 1
return (std::abs(_coords[0] - rhs._coords[0])
<= tol);
#endif
decltype(std::abs(T())) ret{};
for (const auto i : make_range(libmesh_dim))
ret += std::abs(_coords[i]);

#if LIBMESH_DIM == 2
return (std::abs(_coords[0] - rhs._coords[0]) +
std::abs(_coords[1] - rhs._coords[1])
<= tol);
#endif
return ret;
}

#if LIBMESH_DIM == 3
return (std::abs(_coords[0] - rhs._coords[0]) +
std::abs(_coords[1] - rhs._coords[1]) +
std::abs(_coords[2] - rhs._coords[2])
<= tol);
#endif
template <typename T>
inline
bool TypeVector<T>::absolute_fuzzy_equals(const TypeVector<T> & rhs, Real tol) const
{
return libMesh::absolute_fuzzy_equals(*this, rhs, tol);
}


Expand All @@ -1003,23 +1009,7 @@ template <typename T>
inline
bool TypeVector<T>::relative_fuzzy_equals(const TypeVector<T> & rhs, Real tol) const
{
#if LIBMESH_DIM == 1
return this->absolute_fuzzy_equals(rhs, tol *
(std::abs(_coords[0]) + std::abs(rhs._coords[0])));
#endif

#if LIBMESH_DIM == 2
return this->absolute_fuzzy_equals(rhs, tol *
(std::abs(_coords[0]) + std::abs(rhs._coords[0]) +
std::abs(_coords[1]) + std::abs(rhs._coords[1])));
#endif

#if LIBMESH_DIM == 3
return this->absolute_fuzzy_equals(rhs, tol *
(std::abs(_coords[0]) + std::abs(rhs._coords[0]) +
std::abs(_coords[1]) + std::abs(rhs._coords[1]) +
std::abs(_coords[2]) + std::abs(rhs._coords[2])));
#endif
return libMesh::relative_fuzzy_equals(*this, rhs, tol);
}


Expand Down Expand Up @@ -1175,14 +1165,30 @@ outer_product(const TypeVector<T> & a, const T2 & b)

return ret;
}

template <typename T>
auto
l1_norm(const TypeVector<T> & var)
{
return var.l1_norm();
}

template <typename T, typename T2>
auto
l1_norm_diff(const TypeVector<T> & vec1, const TypeVector<T2> & vec2)
{
return l1_norm(vec1 - vec2);
}

} // namespace libMesh

namespace std
{
template <typename T>
auto norm(const libMesh::TypeVector<T> & vector) -> decltype(std::norm(T()))
{
return vector.norm();
// Yea I agree it's dumb that the standard returns the square of the Euclidean norm
return vector.norm_sq();
}
} // namespace std

Expand Down
6 changes: 6 additions & 0 deletions include/utils/compare_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ struct ScalarTraits<MetaPhysicL::DualNumber<T, D, asd>>
{
static const bool value = ScalarTraits<T>::value;
};
template <typename T, typename D, bool asd>
struct RealTraits<MetaPhysicL::DualNumber<T, D, asd>>
{
static const bool value = RealTraits<T>::value;
};

} // namespace libMesh

#endif // LIBMESH_HAVE_METAPHYSICL
Expand Down
Loading