feat(linear-static-3d-euler-beam): step 18 - sparse-assembly
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
#include "fesa/analysis/analysis_model.hpp"
|
||||
#include "fesa/assembly/parallel_for.hpp"
|
||||
#include "fesa/assembly/sparse_assembler.hpp"
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
#include "fesa/model/domain.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
fesa::ModelDefinition makeDefinition() {
|
||||
const std::filesystem::path source{"models/sparse-assembly.inp"};
|
||||
fesa::ModelDefinition definition{};
|
||||
definition.sourcePath = source;
|
||||
definition.sourceContentIdentity = "fnv1a64:0123456789abcdef";
|
||||
definition.nodes = {
|
||||
{{"Beam-1", 1, "1"}, {0.0, 0.0, 0.0}, {source, 10U}},
|
||||
{{"Beam-1", 2, "2"}, {2.0, 0.0, 0.0}, {source, 11U}},
|
||||
{{"Beam-1", 3, "3"}, {5.0, 0.0, 0.0}, {source, 12U}}};
|
||||
definition.materials = {
|
||||
{"Material", 120.0, 0.25, {source, 20U}}};
|
||||
definition.sections = {{
|
||||
"Section",
|
||||
2.0,
|
||||
1.5,
|
||||
0.0,
|
||||
0.75,
|
||||
0.5,
|
||||
{0.0, 1.0, 0.0},
|
||||
{},
|
||||
{source, 30U}}};
|
||||
definition.elements = {
|
||||
{{"Beam-1", 10, "10"}, {0U, 1U}, 0U, 0U, {source, 40U}},
|
||||
{{"Beam-1", 20, "20"}, {1U, 2U}, 0U, 0U, {source, 41U}}};
|
||||
definition.steps = {{
|
||||
"Step-1", {}, {}, 0.1, 1.0, 0.01, 1.0, {source, 50U}}};
|
||||
return definition;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool byteIdentical(const std::vector<T>& left, const std::vector<T>& right) {
|
||||
return left.size() == right.size() &&
|
||||
(left.empty() ||
|
||||
std::memcmp(
|
||||
left.data(), right.data(), left.size() * sizeof(T)) == 0);
|
||||
}
|
||||
|
||||
double entry(
|
||||
const fesa::SparseMatrix& matrix,
|
||||
const std::size_t row,
|
||||
const std::size_t column) {
|
||||
const auto begin = matrix.columnIndices().begin() + matrix.rowOffsets()[row];
|
||||
const auto end = matrix.columnIndices().begin() + matrix.rowOffsets()[row + 1U];
|
||||
const auto found = std::lower_bound(begin, end, column);
|
||||
if (found == end || *found != column) {
|
||||
return 0.0;
|
||||
}
|
||||
return matrix.values()[static_cast<std::size_t>(
|
||||
std::distance(matrix.columnIndices().begin(), found))];
|
||||
}
|
||||
|
||||
class ReverseParallelFor final : public fesa::ParallelFor {
|
||||
public:
|
||||
void execute(
|
||||
const std::size_t count,
|
||||
const std::function<void(std::size_t)>& body) const override {
|
||||
++calls_;
|
||||
observedCount_ = count;
|
||||
for (std::size_t index = count; index > 0U; --index) {
|
||||
body(index - 1U);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t calls() const noexcept {
|
||||
return calls_;
|
||||
}
|
||||
|
||||
std::size_t observedCount() const noexcept {
|
||||
return observedCount_;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::size_t calls_{0U};
|
||||
mutable std::size_t observedCount_{0U};
|
||||
};
|
||||
|
||||
void expectByteIdentical(
|
||||
const fesa::SparseMatrix& actual,
|
||||
const fesa::SparseMatrix& expected) {
|
||||
EXPECT_TRUE(byteIdentical(actual.rowOffsets(), expected.rowOffsets()));
|
||||
EXPECT_TRUE(byteIdentical(actual.columnIndices(), expected.columnIndices()));
|
||||
EXPECT_TRUE(byteIdentical(actual.values(), expected.values()));
|
||||
}
|
||||
|
||||
TEST(SparseAssembly, SerialTbbAndRepeatedRunsAreByteIdentical) {
|
||||
auto domainResult = fesa::Domain::create(makeDefinition());
|
||||
ASSERT_TRUE(domainResult.hasValue());
|
||||
auto modelResult = fesa::AnalysisModel::create(domainResult.value());
|
||||
ASSERT_TRUE(modelResult.hasValue());
|
||||
auto dofsResult = fesa::DofManager::create(modelResult.value());
|
||||
ASSERT_TRUE(dofsResult.hasValue());
|
||||
|
||||
fesa::SerialParallelFor serialExecutor;
|
||||
fesa::TbbParallelFor tbbExecutor;
|
||||
ReverseParallelFor reverseExecutor;
|
||||
auto serial = fesa::SparseAssembler::assembleStiffness(
|
||||
modelResult.value(), dofsResult.value(), serialExecutor);
|
||||
auto tbb = fesa::SparseAssembler::assembleStiffness(
|
||||
modelResult.value(), dofsResult.value(), tbbExecutor);
|
||||
auto reversed = fesa::SparseAssembler::assembleStiffness(
|
||||
modelResult.value(), dofsResult.value(), reverseExecutor);
|
||||
ASSERT_TRUE(serial.hasValue());
|
||||
ASSERT_TRUE(tbb.hasValue());
|
||||
ASSERT_TRUE(reversed.hasValue());
|
||||
|
||||
EXPECT_EQ(reverseExecutor.calls(), 1U);
|
||||
EXPECT_EQ(reverseExecutor.observedCount(), 2U);
|
||||
EXPECT_EQ(serial.value().rows(), 18U);
|
||||
EXPECT_EQ(serial.value().columns(), 18U);
|
||||
EXPECT_EQ(serial.value().rowOffsets(), dofsResult.value().sparsePattern().rowOffsets);
|
||||
EXPECT_EQ(
|
||||
serial.value().columnIndices(),
|
||||
dofsResult.value().sparsePattern().columnIndices);
|
||||
EXPECT_TRUE(serial.value().validate().isOk());
|
||||
expectByteIdentical(tbb.value(), serial.value());
|
||||
expectByteIdentical(reversed.value(), serial.value());
|
||||
|
||||
for (std::size_t repetition = 0U; repetition < 8U; ++repetition) {
|
||||
auto repeated = fesa::SparseAssembler::assembleStiffness(
|
||||
modelResult.value(), dofsResult.value(), tbbExecutor);
|
||||
ASSERT_TRUE(repeated.hasValue());
|
||||
expectByteIdentical(repeated.value(), serial.value());
|
||||
}
|
||||
|
||||
for (std::size_t row = 0U; row < serial.value().rows(); ++row) {
|
||||
for (std::size_t column = 0U;
|
||||
column < serial.value().columns();
|
||||
++column) {
|
||||
EXPECT_DOUBLE_EQ(
|
||||
entry(serial.value(), row, column),
|
||||
entry(serial.value(), column, row));
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_NEAR(entry(serial.value(), 0U, 0U), 120.0, 1.0e-12);
|
||||
EXPECT_NEAR(entry(serial.value(), 0U, 6U), -120.0, 1.0e-12);
|
||||
EXPECT_NEAR(entry(serial.value(), 6U, 6U), 200.0, 1.0e-12);
|
||||
EXPECT_NEAR(entry(serial.value(), 6U, 12U), -80.0, 1.0e-12);
|
||||
EXPECT_NEAR(entry(serial.value(), 12U, 12U), 80.0, 1.0e-12);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,149 @@
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
#include "fesa/math/matrix.hpp"
|
||||
#include "fesa/math/sparse_matrix.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
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.rowOffsets);
|
||||
EXPECT_EQ(matrix.columnIndices(), pattern.columnIndices);
|
||||
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 reversedContributions = contributions;
|
||||
std::reverse(reversedContributions.begin(), reversedContributions.end());
|
||||
auto second = SparseMatrix::fromCoo(
|
||||
1U, 1U, std::move(reversedContributions), 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 oneEntry{{0U, 1U}, {0U}};
|
||||
const auto expectFailure = [](
|
||||
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.status().isOk());
|
||||
EXPECT_EQ(result.status().failureCategory(), fesa::FailureCategory::model);
|
||||
EXPECT_FALSE(result.status().diagnostics().empty());
|
||||
}
|
||||
};
|
||||
|
||||
expectFailure(2U, 2U, {}, {{0U, 0U}, {}});
|
||||
expectFailure(1U, 1U, {}, {{1U, 1U}, {0U}});
|
||||
expectFailure(2U, 2U, {}, {{0U, 1U, 0U}, {0U}});
|
||||
expectFailure(1U, 2U, {}, {{0U, 2U}, {1U, 0U}});
|
||||
expectFailure(1U, 1U, {}, {{0U, 2U}, {0U, 0U}});
|
||||
expectFailure(1U, 1U, {}, {{0U, 1U}, {1U}});
|
||||
expectFailure(1U, 1U, {{1U, 0U, 1.0, 0U, 0U}}, oneEntry);
|
||||
expectFailure(1U, 1U, {{0U, 1U, 1.0, 0U, 0U}}, oneEntry);
|
||||
expectFailure(1U, 2U, {{0U, 1U, 1.0, 0U, 0U}}, {{0U, 1U}, {0U}});
|
||||
expectFailure(
|
||||
1U,
|
||||
1U,
|
||||
{{0U, 0U, (std::numeric_limits<double>::infinity)(), 0U, 0U}},
|
||||
oneEntry);
|
||||
expectFailure(
|
||||
1U,
|
||||
1U,
|
||||
{{0U, 0U, (std::numeric_limits<double>::quiet_NaN)(), 0U, 0U}},
|
||||
oneEntry);
|
||||
expectFailure(
|
||||
1U,
|
||||
1U,
|
||||
{{0U, 0U, (std::numeric_limits<double>::max)(), 0U, 0U},
|
||||
{0U, 0U, (std::numeric_limits<double>::max)(), 1U, 0U}},
|
||||
oneEntry);
|
||||
}
|
||||
|
||||
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.rowOffsets);
|
||||
EXPECT_EQ(result.value().columnIndices(), pattern.columnIndices);
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user