Files
FESADev/tests/unit/math/sparse_matrix_test.cpp
T

237 lines
9.3 KiB
C++

#include "fesa/math/sparse_matrix.h"
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <future>
#include <limits>
#include <type_traits>
#include <utility>
#include <vector>
#include "fesa/fem/dof_manager.h"
#include "fesa/math/matrix.h"
namespace {
using fesa::CooContribution;
using fesa::SparseMatrix;
using fesa::SparsePattern;
using fesa::Vector;
static_assert(
!std::is_base_of_v<fesa::Matrix, SparseMatrix>,
"SparseMatrix must own CSR storage independently of dense Matrix.");
TEST(SparseAssembly, ValidatesKnownCsrAndMultiply) {
const SparsePattern pattern{{0U, 2U, 2U, 4U, 5U}, {0U, 2U, 1U, 3U, 3U}};
std::vector<CooContribution> contributions{{2U, 3U, 4.0, 2U, 0U},
{0U, 2U, 2.0, 0U, 1U},
{3U, 3U, 5.0, 3U, 0U},
{0U, 0U, 1.0, 0U, 0U},
{2U, 1U, 3.0, 1U, 0U}};
auto result =
SparseMatrix::FromCoo(4U, 4U, std::move(contributions), pattern);
ASSERT_TRUE(result.HasValue());
const SparseMatrix& matrix = result.Value();
EXPECT_EQ(matrix.Rows(), 4U);
EXPECT_EQ(matrix.Columns(), 4U);
EXPECT_EQ(matrix.RowOffsets(), pattern.row_offsets);
EXPECT_EQ(matrix.ColumnIndices(), pattern.column_indices);
EXPECT_EQ(matrix.Values(), (std::vector<double>{1.0, 2.0, 3.0, 4.0, 5.0}));
EXPECT_TRUE(matrix.Validate().IsOk());
Vector rhs{4U};
rhs[0U] = 1.0;
rhs[1U] = 2.0;
rhs[2U] = 3.0;
rhs[3U] = 4.0;
const Vector product = matrix.Multiply(rhs);
ASSERT_EQ(product.Size(), 4U);
EXPECT_DOUBLE_EQ(product[0U], 7.0);
EXPECT_DOUBLE_EQ(product[1U], 0.0);
EXPECT_DOUBLE_EQ(product[2U], 22.0);
EXPECT_DOUBLE_EQ(product[3U], 20.0);
EXPECT_THROW(static_cast<void>(matrix.Multiply(Vector{3U})),
std::invalid_argument);
}
TEST(SparseAssembly, ReducesDuplicatesInFixedTupleOrder) {
const SparsePattern pattern{{0U, 1U}, {0U}};
const std::vector<CooContribution> contributions{{0U, 0U, 1.0, 2U, 0U},
{0U, 0U, -1.0e16, 1U, 0U},
{0U, 0U, 1.0e16, 0U, 0U}};
auto first = SparseMatrix::FromCoo(1U, 1U, contributions, pattern);
ASSERT_TRUE(first.HasValue());
ASSERT_EQ(first.Value().Values().size(), 1U);
EXPECT_DOUBLE_EQ(first.Value().Values()[0U], 1.0);
auto reversed_contributions = contributions;
std::reverse(reversed_contributions.begin(), reversed_contributions.end());
auto second =
SparseMatrix::FromCoo(1U, 1U, std::move(reversed_contributions), pattern);
ASSERT_TRUE(second.HasValue());
EXPECT_EQ(second.Value().RowOffsets(), first.Value().RowOffsets());
EXPECT_EQ(second.Value().ColumnIndices(), first.Value().ColumnIndices());
EXPECT_EQ(second.Value().Values(), first.Value().Values());
}
TEST(SparseAssembly, RejectsInvalidIndexPatternAndShape) {
const SparsePattern one_entry{{0U, 1U}, {0U}};
const auto expect_failure = [](std::size_t rows, std::size_t columns,
std::vector<CooContribution> contributions,
const SparsePattern& pattern) {
auto result =
SparseMatrix::FromCoo(rows, columns, std::move(contributions), pattern);
EXPECT_FALSE(result.HasValue());
if (!result.HasValue()) {
EXPECT_FALSE(result.GetStatus().IsOk());
EXPECT_EQ(result.GetStatus().Category(), fesa::FailureCategory::kModel);
EXPECT_FALSE(result.GetStatus().Diagnostics().empty());
}
};
expect_failure(2U, 2U, {}, {{0U, 0U}, {}});
expect_failure(1U, 1U, {}, {{1U, 1U}, {0U}});
expect_failure(2U, 2U, {}, {{0U, 1U, 0U}, {0U}});
expect_failure(1U, 2U, {}, {{0U, 2U}, {1U, 0U}});
expect_failure(1U, 1U, {}, {{0U, 2U}, {0U, 0U}});
expect_failure(1U, 1U, {}, {{0U, 1U}, {1U}});
expect_failure(1U, 1U, {{1U, 0U, 1.0, 0U, 0U}}, one_entry);
expect_failure(1U, 1U, {{0U, 1U, 1.0, 0U, 0U}}, one_entry);
expect_failure(1U, 2U, {{0U, 1U, 1.0, 0U, 0U}}, {{0U, 1U}, {0U}});
expect_failure(1U, 1U,
{{0U, 0U, (std::numeric_limits<double>::infinity)(), 0U, 0U}},
one_entry);
expect_failure(1U, 1U,
{{0U, 0U, (std::numeric_limits<double>::quiet_NaN)(), 0U, 0U}},
one_entry);
expect_failure(1U, 1U,
{{0U, 0U, (std::numeric_limits<double>::max)(), 0U, 0U},
{0U, 0U, (std::numeric_limits<double>::max)(), 1U, 0U}},
one_entry);
}
TEST(SparseAssembly, PreservesExpectedStructuralZeros) {
const SparsePattern pattern{{0U, 2U, 4U, 5U}, {0U, 2U, 1U, 2U, 0U}};
std::vector<CooContribution> contributions{
{0U, 0U, 2.0, 0U, 0U}, {1U, 1U, 4.0, 0U, 1U}, {1U, 1U, -4.0, 1U, 0U}};
auto result =
SparseMatrix::FromCoo(3U, 3U, std::move(contributions), pattern);
ASSERT_TRUE(result.HasValue());
EXPECT_EQ(result.Value().RowOffsets(), pattern.row_offsets);
EXPECT_EQ(result.Value().ColumnIndices(), pattern.column_indices);
EXPECT_EQ(result.Value().Values(),
(std::vector<double>{2.0, 0.0, 0.0, 0.0, 0.0}));
EXPECT_EQ(std::count(result.Value().Values().begin(),
result.Value().Values().end(), 0.0),
4);
}
TEST(SparseAssembly, MultipliesRectangularCopyMoveAndConcurrentConstCalls) {
auto assembled = SparseMatrix::FromCoo(
3U, 5U, {{0U, 4U, 3.0, 0U, 0U}, {2U, 1U, -2.0, 1U, 0U}},
{{0U, 1U, 1U, 2U}, {4U, 1U}});
ASSERT_TRUE(assembled.HasValue());
const auto original = assembled.Value();
auto copied = original;
auto moved = std::move(copied);
Vector rhs{5U, 2.0};
const auto evaluate = [&rhs](const SparseMatrix& matrix) {
for (int repeat = 0; repeat < 20; ++repeat) {
const auto product = matrix.Multiply(rhs);
if (product.Size() != 3U || product[0U] != 6.0 ||
product[1U] != 0.0 || product[2U] != -4.0) return false;
}
return true;
};
auto first = std::async(std::launch::async, [&] { return evaluate(moved); });
auto second = std::async(std::launch::async, [&] { return evaluate(moved); });
EXPECT_TRUE(first.get());
EXPECT_TRUE(second.get());
EXPECT_TRUE(evaluate(original));
auto replacement = SparseMatrix::FromCoo(
1U, 1U, {{0U, 0U, 7.0, 0U, 0U}}, {{0U, 1U}, {0U}});
ASSERT_TRUE(replacement.HasValue());
moved = replacement.Value();
EXPECT_DOUBLE_EQ(moved.Multiply(Vector{1U, 3.0})[0U], 21.0);
EXPECT_TRUE(evaluate(original));
}
TEST(SparseAssembly, PreservesZeroShapesAndEmptyRows) {
auto no_rows = SparseMatrix::FromCoo(0U, 3U, {}, {{0U}, {}});
auto no_columns = SparseMatrix::FromCoo(2U, 0U, {}, {{0U, 0U, 0U}, {}});
auto no_entries = SparseMatrix::FromCoo(2U, 3U, {}, {{0U, 0U, 0U}, {}});
ASSERT_TRUE(no_rows.HasValue());
ASSERT_TRUE(no_columns.HasValue());
ASSERT_TRUE(no_entries.HasValue());
EXPECT_EQ(no_rows.Value().Multiply(Vector{3U}).Size(), 0U);
const auto empty_columns = no_columns.Value().Multiply(Vector{0U});
const auto empty_entries = no_entries.Value().Multiply(Vector{3U});
ASSERT_EQ(empty_columns.Size(), 2U);
ASSERT_EQ(empty_entries.Size(), 2U);
for (std::size_t row = 0; row < 2U; ++row) {
EXPECT_DOUBLE_EQ(empty_columns[row], 0.0);
EXPECT_DOUBLE_EQ(empty_entries[row], 0.0);
}
}
TEST(SparseAssembly, RejectsNonfiniteRhsAndRecoversAfterFiniteOverflow) {
auto matrix = SparseMatrix::FromCoo(
2U, 2U, {{0U, 0U, 2.0, 0U, 0U}}, {{0U, 1U, 2U}, {0U, 1U}});
ASSERT_TRUE(matrix.HasValue());
Vector rhs{2U, (std::numeric_limits<double>::infinity)()};
EXPECT_THROW(static_cast<void>(matrix.Value().Multiply(rhs)),
std::invalid_argument);
rhs[1U] = 4.0;
rhs[0U] = (std::numeric_limits<double>::max)();
EXPECT_TRUE(std::isinf(matrix.Value().Multiply(rhs)[0U]));
rhs[0U] = (std::numeric_limits<double>::quiet_NaN)();
EXPECT_THROW(static_cast<void>(matrix.Value().Multiply(rhs)),
std::invalid_argument);
EXPECT_THROW(static_cast<void>(matrix.Value().Multiply(Vector{1U})),
std::invalid_argument);
rhs[0U] = 3.0;
rhs[1U] = 4.0;
const auto finite = matrix.Value().Multiply(rhs);
EXPECT_DOUBLE_EQ(finite[0U], 6.0);
EXPECT_DOUBLE_EQ(finite[1U], 0.0);
}
TEST(SparseAssembly, MatchesIndependentLongDoubleResidualOnUnevenRows) {
const std::vector<double> entries{1.25, -0.375, 4.5, -3.0, 0.125, 2.0};
const SparsePattern pattern{{0U, 3U, 3U, 5U, 6U}, {0U, 2U, 4U, 1U, 3U, 2U}};
std::vector<CooContribution> contributions;
for (std::size_t row = 0; row < 4U; ++row) {
for (auto position = pattern.row_offsets[row];
position < pattern.row_offsets[row + 1U]; ++position) {
contributions.push_back(
{row, pattern.column_indices[position], entries[position], row,
position});
}
}
auto matrix = SparseMatrix::FromCoo(4U, 5U, contributions, pattern);
ASSERT_TRUE(matrix.HasValue());
Vector rhs{5U};
for (std::size_t column = 0; column < 5U; ++column) {
rhs[column] = 0.3 + static_cast<double>(column) * 0.7;
}
const auto product = matrix.Value().Multiply(rhs);
for (std::size_t row = 0; row < 4U; ++row) {
long double expected = 0;
for (auto position = pattern.row_offsets[row];
position < pattern.row_offsets[row + 1U]; ++position) {
expected += static_cast<long double>(entries[position]) *
rhs[pattern.column_indices[position]];
}
EXPECT_NEAR(product[row], static_cast<double>(expected), 2.0e-14);
}
}
} // namespace