Skip to content

Commit

Permalink
Improve unit-testing for sparse-matrix values. (Exawind#544)
Browse files Browse the repository at this point in the history
* Improve unit-testing for sparse-matrix values.

It used to just compare each matrix column and coefficient to
the expected gold values, expecting position to match as well.
Now it searchs the matrix row for each gold column and checks
if the coefficient matches, and doesn't fail if there are extra
columns in the matrix.

* Add a check to make sure non-gold columns have coeffs of zero.
  • Loading branch information
alanw0 authored and jhux2 committed Mar 23, 2020
1 parent 569c2a7 commit 635de17
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions unit_tests/UnitTestTpetraHelperObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@

namespace unit_test_utils {

inline
bool find_col(int col,
const std::vector<int>& cols,
int begin, int end)
{
for(int i=begin; i<end; ++i) {
if (cols[i] == col) {
return true;
}
}
return false;
}

struct TpetraHelperObjectsBase {
TpetraHelperObjectsBase(stk::mesh::BulkData& bulk, int numDof)
: yamlNode(unit_test_utils::get_default_inputs()),
Expand Down Expand Up @@ -120,10 +133,21 @@ struct TpetraHelperObjectsBase {

for(int i=0; i<localMatrix.numRows(); ++i) {
KokkosSparse::SparseRowViewConst<MatrixType> constRowView = localMatrix.rowConst(i);

for(int j=0; j<constRowView.length; ++j) {
EXPECT_EQ(cols[rowOffsets[i]+j], constRowView.colidx(j));
EXPECT_NEAR(vals[rowOffsets[i]+j], constRowView.value(j), 1.e-14)<<"i: "<<i<<", j: "<<j;
for(int offset=rowOffsets[i]; offset<rowOffsets[i+1]; ++offset) {
int goldCol = cols[offset];
bool foundGoldCol = false;
for(int j=0; j<constRowView.length; ++j) {
if (constRowView.colidx(j) == goldCol) {
foundGoldCol = true;
EXPECT_NEAR(vals[offset], constRowView.value(j), 1.e-14)<<"i: "<<i<<", j: "<<j;
}
else if (!find_col(constRowView.colidx(j),
cols, rowOffsets[i], rowOffsets[i+1]))
{
EXPECT_NEAR(0.0, constRowView.value(j), 1.e-14);
}
}
EXPECT_TRUE(foundGoldCol);
}

EXPECT_NEAR(rhs[i], localRhs(i,0), 1.e-14)<<"i: "<<i;
Expand Down

0 comments on commit 635de17

Please sign in to comment.