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

Skip to content

Commit 388f289

Browse files
committed
Change for global data structures
1 parent 97f633c commit 388f289

File tree

9 files changed

+77
-189
lines changed

9 files changed

+77
-189
lines changed

include/numerics/numeric_vector.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ class NumericVector : public ReferenceCountedObject<NumericVector<T>>,
314314
*/
315315
Real l2_norm_diff (const NumericVector<T> & other_vec) const;
316316

317+
/**
318+
* \returns The \f$ \ell_1 \f$-norm of \f$ \vec{u} - \vec{v} \f$, where
319+
* \f$ \vec{u} \f$ is \p this.
320+
*/
321+
Real l1_norm_diff (const NumericVector<T> & other_vec) const;
322+
317323
/**
318324
* \returns The size of the vector.
319325
*/
@@ -786,15 +792,6 @@ class NumericVector : public ReferenceCountedObject<NumericVector<T>>,
786792
*/
787793
bool compatible(const NumericVector<T> & v) const;
788794

789-
/**
790-
* checks whether the vector \p v is fuzzy equal to this vector by doing element-wise comparisons.
791-
* A given element will be deemed fuzzy equal if either a relative or absolute tolerance fuzzy
792-
* equal comparison returns true
793-
*/
794-
bool fuzzy_equals(const NumericVector<T> & v,
795-
const Real rel_tol = TOLERANCE,
796-
const Real abs_tol = TOLERANCE) const;
797-
798795
protected:
799796

800797
/**
@@ -1077,8 +1074,24 @@ void NumericVector<T>::swap (NumericVector<T> & v)
10771074
}
10781075

10791076

1080-
} // namespace libMesh
10811077

1078+
template <typename T>
1079+
auto
1080+
l1_norm(const NumericVector<T> & vec) -> decltype(vec.l1_norm())
1081+
{
1082+
return vec.l1_norm();
1083+
}
1084+
1085+
1086+
1087+
template <typename T>
1088+
auto
1089+
l1_norm_diff(const NumericVector<T> & vec1,
1090+
const NumericVector<T> & vec2) -> decltype(vec1.l1_norm_diff(vec2))
1091+
{
1092+
return vec1.l1_norm_diff(vec2);
1093+
}
1094+
} // namespace libMesh
10821095

10831096
// Workaround for weird boost/NumericVector interaction bug
10841097
#ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION

include/numerics/petsc_matrix.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,6 @@ class PetscMatrix final : public SparseMatrix<T>
349349
const std::vector<numeric_index_type> & rows,
350350
const std::vector<numeric_index_type> & cols) const override;
351351

352-
virtual bool fuzzy_equals(const SparseMatrix<T> & other,
353-
const Real rel_tol = TOLERANCE,
354-
const Real abs_tol = TOLERANCE) const override;
355-
356352
virtual void scale(const T scale) override;
357353

358354
protected:

include/numerics/sparse_matrix.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
385385
*/
386386
virtual Real linfty_norm () const = 0;
387387

388+
/**
389+
* \returns The l1_norm() of the difference of \p this and \p other_mat
390+
*/
391+
Real l1_norm_diff (const SparseMatrix<T> & other_mat) const;
392+
388393
/**
389394
* \returns \p true if the matrix has been assembled.
390395
*/
@@ -573,15 +578,6 @@ class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
573578
std::vector<numeric_index_type> & indices,
574579
std::vector<T> & values) const = 0;
575580

576-
/**
577-
* checks whether the matrix \p other is fuzzy equal to this matrix by doing element-wise
578-
* comparisons. A given element will be deemed fuzzy equal if either a relative or absolute
579-
* tolerance fuzzy equal comparison returns true
580-
*/
581-
virtual bool fuzzy_equals(const SparseMatrix<T> & other,
582-
const Real rel_tol = TOLERANCE,
583-
const Real abs_tol = TOLERANCE) const;
584-
585581
/**
586582
* Scales all elements of this matrix by \p scale
587583
*/
@@ -639,6 +635,20 @@ std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
639635
return os;
640636
}
641637

638+
template <typename T>
639+
auto
640+
l1_norm(const SparseMatrix<T> & mat) -> decltype(mat.l1_norm())
641+
{
642+
return mat.l1_norm();
643+
}
644+
645+
template <typename T>
646+
auto
647+
l1_norm_diff(const SparseMatrix<T> & mat1,
648+
const SparseMatrix<T> & mat2) -> decltype(mat1.l1_norm_diff(mat2))
649+
{
650+
return mat1.l1_norm_diff(mat2);
651+
}
642652

643653
} // namespace libMesh
644654

include/numerics/type_vector.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,11 +1194,12 @@ l1_norm(const TypeVector<T> & var) -> decltype(var.l1_norm())
11941194
return var.l1_norm();
11951195
}
11961196

1197-
template <typename T>
1197+
template <typename T, typename T2>
11981198
auto
1199-
l1_norm_diff(const TypeVector<T> & var) -> decltype(var.l1_norm_diff())
1199+
l1_norm_diff(const TypeVector<T> & var1,
1200+
const TypeVector<T2> & var2) -> decltype(var1.l1_norm_diff(var2))
12001201
{
1201-
return var.l1_norm_diff();
1202+
return var1.l1_norm_diff(var2);
12021203
}
12031204

12041205
} // namespace libMesh

src/numerics/numeric_vector.C

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,22 @@ Real NumericVector<T>::l2_norm_diff (const NumericVector<T> & v) const
376376

377377

378378

379+
template <class T>
380+
Real NumericVector<T>::l1_norm_diff (const NumericVector<T> & v) const
381+
{
382+
libmesh_assert(this->compatible(v));
383+
384+
Real norm = 0;
385+
for (const auto i : make_range(this->first_local_index(), this->last_local_index()))
386+
norm += libMesh::l1_norm_diff((*this)(i), v(i));
387+
388+
this->comm().sum(norm);
389+
390+
return norm;
391+
}
392+
393+
394+
379395
template <typename T>
380396
void NumericVector<T>::add_vector (const T * v,
381397
const std::vector<numeric_index_type> & dof_indices)
@@ -430,34 +446,6 @@ bool NumericVector<T>::compatible (const NumericVector<T> & v) const
430446
this->last_local_index() == v.last_local_index();
431447
}
432448

433-
template <typename T>
434-
bool
435-
NumericVector<T>::fuzzy_equals(const NumericVector<T> & v,
436-
const Real rel_tol,
437-
const Real abs_tol) const
438-
{
439-
bool equiv = true;
440-
if (this->local_size() != v.local_size())
441-
equiv = false;
442-
443-
if (equiv)
444-
for (const auto i : make_range(this->first_local_index(), this->last_local_index()))
445-
{
446-
if (relative_fuzzy_equals((*this)(i), v(i), rel_tol) ||
447-
absolute_fuzzy_equals((*this)(i), v(i), abs_tol))
448-
continue;
449-
else
450-
{
451-
equiv = false;
452-
break;
453-
}
454-
}
455-
456-
this->comm().min(equiv);
457-
458-
return equiv;
459-
}
460-
461449
//------------------------------------------------------------------
462450
// Explicit instantiations
463451
template class LIBMESH_EXPORT NumericVector<Number>;

src/numerics/petsc_matrix.C

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "libmesh/parallel.h"
3333
#include "libmesh/utility.h"
3434
#include "libmesh/wrapped_petsc.h"
35-
#include "libmesh/fuzzy_equals.h"
3635

3736
// C++ includes
3837
#ifdef LIBMESH_HAVE_UNISTD_H
@@ -1576,104 +1575,6 @@ SparseMatrix<T> & PetscMatrix<T>::operator= (const SparseMatrix<T> & v)
15761575
return *this;
15771576
}
15781577

1579-
template <typename T>
1580-
bool
1581-
PetscMatrix<T>::fuzzy_equals(const SparseMatrix<T> & other,
1582-
const Real rel_tol,
1583-
const Real abs_tol) const
1584-
{
1585-
const auto * const petsc_other = dynamic_cast<const PetscMatrix<Number> *>(&other);
1586-
if (!petsc_other)
1587-
// This result should be the same on all procs
1588-
return false;
1589-
1590-
Mat petsc_other_mat = petsc_other->_mat;
1591-
const PetscScalar *petsc_row, *petsc_row_other;
1592-
const PetscInt *petsc_cols, *petsc_cols_other;
1593-
1594-
PetscErrorCode ierr = static_cast<PetscErrorCode>(0);
1595-
PetscInt ncols = 0, ncols_other = 0;
1596-
1597-
bool equiv = true;
1598-
if ((this->local_m() != other.local_m()) || (this->local_n() != other.local_n()))
1599-
{
1600-
equiv = false;
1601-
goto globalComm;
1602-
}
1603-
1604-
for (const auto i : make_range(this->row_start(), this->row_stop()))
1605-
{
1606-
const auto i_val = static_cast<PetscInt>(i);
1607-
1608-
// the matrix needs to be closed for this to work
1609-
// this->close();
1610-
// but closing it is a semiparallel operation; we want operator()
1611-
// to run on one processor.
1612-
libmesh_assert(this->closed());
1613-
libmesh_assert(petsc_other->closed());
1614-
1615-
ierr = MatGetRow(_mat, i_val, &ncols, &petsc_cols, &petsc_row);
1616-
LIBMESH_CHKERR(ierr);
1617-
ierr = MatGetRow(petsc_other_mat, i_val, &ncols_other, &petsc_cols_other, &petsc_row_other);
1618-
LIBMESH_CHKERR(ierr);
1619-
1620-
auto restore_rows = [this,
1621-
i_val,
1622-
petsc_other_mat,
1623-
&ierr,
1624-
&ncols,
1625-
&petsc_cols,
1626-
&petsc_row,
1627-
&ncols_other,
1628-
&petsc_cols_other,
1629-
&petsc_row_other]()
1630-
{
1631-
ierr = MatRestoreRow(_mat, i_val, &ncols, &petsc_cols, &petsc_row);
1632-
LIBMESH_CHKERR(ierr);
1633-
ierr =
1634-
MatRestoreRow(petsc_other_mat, i_val, &ncols_other, &petsc_cols_other, &petsc_row_other);
1635-
LIBMESH_CHKERR(ierr);
1636-
};
1637-
1638-
auto compared_false = [&equiv, restore_rows]()
1639-
{
1640-
restore_rows();
1641-
equiv = false;
1642-
};
1643-
1644-
if (ncols != ncols_other)
1645-
{
1646-
compared_false();
1647-
goto globalComm;
1648-
}
1649-
1650-
// No need for fuzzy comparison here
1651-
for (const auto j_val : make_range(ncols))
1652-
if (petsc_cols[j_val] != petsc_cols_other[j_val])
1653-
{
1654-
compared_false();
1655-
goto globalComm;
1656-
}
1657-
1658-
for (const auto j_val : make_range(ncols))
1659-
if (relative_fuzzy_equals(petsc_row[j_val], petsc_row_other[j_val], rel_tol) ||
1660-
absolute_fuzzy_equals(petsc_row[j_val], petsc_row_other[j_val], abs_tol))
1661-
continue;
1662-
else
1663-
{
1664-
compared_false();
1665-
goto globalComm;
1666-
}
1667-
1668-
restore_rows();
1669-
}
1670-
1671-
globalComm:
1672-
this->comm().min(equiv);
1673-
1674-
return equiv;
1675-
}
1676-
16771578
template <typename T>
16781579
void PetscMatrix<T>::scale(const T scale)
16791580
{

src/numerics/sparse_matrix.C

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "libmesh/trilinos_epetra_matrix.h"
3333
#include "libmesh/numeric_vector.h"
3434
#include "libmesh/enum_solver_package.h"
35-
#include "libmesh/fuzzy_equals.h"
3635

3736

3837
// gzstream for reading compressed files as a stream
@@ -858,45 +857,23 @@ void SparseMatrix<T>::read_petsc_hdf5(const std::string &)
858857

859858

860859
template <typename T>
861-
bool
862-
SparseMatrix<T>::fuzzy_equals(const SparseMatrix<T> & other,
863-
const Real rel_tol,
864-
const Real abs_tol) const
860+
void SparseMatrix<T>::scale(const T scale)
865861
{
866-
bool equiv = true;
867-
if ((this->local_m() != other.local_m()) || (this->local_n() != other.local_n()))
868-
equiv = false;
869-
870-
if (equiv)
871-
for (const auto i : make_range(this->row_start(), this->row_stop()))
872-
for (const auto j : make_range(this->col_start(), this->col_stop()))
873-
{
874-
if (relative_fuzzy_equals((*this)(i, j), other(i, j), rel_tol) ||
875-
absolute_fuzzy_equals((*this)(i, j), other(i, j), abs_tol))
876-
continue;
877-
else
878-
{
879-
equiv = false;
880-
goto globalComm;
881-
}
882-
}
883-
884-
globalComm:
885-
this->comm().min(equiv);
862+
libmesh_assert(this->closed());
886863

887-
return equiv;
864+
for (const auto i : make_range(this->row_start(), this->row_stop()))
865+
for (const auto j : make_range(this->col_start(), this->col_stop()))
866+
this->set(i, j, (*this)(i, j) * scale);
888867
}
889868

890869

891870

892871
template <typename T>
893-
void SparseMatrix<T>::scale(const T scale)
872+
Real SparseMatrix<T>::l1_norm_diff(const SparseMatrix<T> & other_mat) const
894873
{
895-
libmesh_assert(this->closed());
896-
897-
for (const auto i : make_range(this->row_start(), this->row_stop()))
898-
for (const auto j : make_range(this->col_start(), this->col_stop()))
899-
this->set(i, j, (*this)(i, j) * scale);
874+
auto diff_mat = this->clone();
875+
diff_mat->add(-1.0, other_mat);
876+
return diff_mat->l1_norm();
900877
}
901878

902879
//------------------------------------------------------------------

tests/numerics/numeric_vector_test.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
// libMesh includes
88
#include <libmesh/parallel.h>
9+
#include <libmesh/fuzzy_equals.h>
910

1011
#include "libmesh_cppunit.h"
1112

@@ -74,11 +75,11 @@ class NumericVectorTest : public CppUnit::TestCase {
7475
auto v_clone = v.clone();
7576
auto & vorig = *v_clone;
7677

77-
CPPUNIT_ASSERT(v.fuzzy_equals(vorig));
78+
CPPUNIT_ASSERT(fuzzy_equals(v, vorig));
7879

7980
v += v;
8081

81-
CPPUNIT_ASSERT(!v.fuzzy_equals(vorig));
82+
CPPUNIT_ASSERT(!fuzzy_equals(v, vorig));
8283

8384
for (libMesh::dof_id_type n=first; n != last; n++)
8485
LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v(n)),

0 commit comments

Comments
 (0)