feat(linear-static-3d-euler-beam): step 18 - sparse-assembly
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -716,3 +716,66 @@
|
||||
| REVIEW1-VERIFY-discovery | `ctest --test-dir .harness/build -C Debug --show-only=json-v1` | 0 | 42 tests discovered, including the unchanged three exact ParallelFor names |
|
||||
| REVIEW1-VERIFY-full | `ctest --test-dir .harness/build -C Debug --output-on-failure` | 0 | full accumulated suite passed 42/42 |
|
||||
| REVIEW1-VERIFY-scans | public TBB leak, production scope, atomic reliability, reference, and diff/whitespace scans | 0 | public leaks 0; forbidden production scope 0; atomic vectors 5; relaxed duplicate counters 4; legacy non-atomic visit/output paths 0; reference unchanged; diff clean |
|
||||
|
||||
## Step 18 — sparse-assembly
|
||||
|
||||
- task_id: `TASK-18`
|
||||
- status: `completed`
|
||||
- changed_files: `include/fesa/math/sparse_matrix.hpp`,
|
||||
`src/fesa/math/sparse_matrix.cpp`,
|
||||
`include/fesa/assembly/sparse_assembler.hpp`,
|
||||
`src/fesa/assembly/sparse_assembler.cpp`,
|
||||
`tests/unit/math/sparse_matrix_test.cpp`,
|
||||
`tests/unit/assembly/sparse_assembler_test.cpp`, `src/fesa/CMakeLists.txt`,
|
||||
`tests/CMakeLists.txt`,
|
||||
`docs/implementation-plans/linear-static-3d-euler-beam-implementation-report.md`,
|
||||
`phases/linear-static-3d-euler-beam/index.json`,
|
||||
`.superpowers/sdd/linear-static-3d-euler-beam/task-18-report.md`
|
||||
- requirement_ids: `FESA-REQ-LS3DEB-024`, `FESA-REQ-LS3DEB-025`,
|
||||
`FESA-REQ-LS3DEB-034`, `FESA-REQ-LS3DEB-035`
|
||||
- test_ids: `T18-SPARSE-001`, `T18-SPARSE-002`, `T18-SPARSE-003`,
|
||||
`T18-SPARSE-004`, `T18-SPARSE-005`
|
||||
|
||||
| stage | exact command | exit_code | expected_or_observed_result | evidence_tail |
|
||||
| --- | --- | ---: | --- | --- |
|
||||
| RED-build | `cmake --build .harness/build --config Debug --target fesa_tests` | 1 | Exactly five planned tests were registered before production and both sparse public APIs were absent | CMake regenerated, then MSVC C1083 reported missing `fesa/assembly/sparse_assembler.hpp` and `fesa/math/sparse_matrix.hpp` from the two new test files |
|
||||
| GREEN-build | `cmake --build .harness/build --config Debug --target fesa_tests` | 0 | Minimum CSR owner, deterministic assembler, five tests, solver library, and unit executable compile and link | `sparse_assembler.cpp`, `sparse_matrix.cpp`, both tests, `fesa_solver.lib`, and `fesa_unit_tests.exe` built without a FESA warning under `/W4 /WX` |
|
||||
| GREEN-test | `ctest --test-dir .harness/build -C Debug -R SparseAssembly --output-on-failure` | 0 | CSR validation/reduction/zero policy and serial/TBB deterministic beam assembly pass | 5/5 exact `SparseAssembly` tests passed |
|
||||
| VERIFY-configure | `cmake -S . -B .harness/build -A x64 -DFESA_GTEST_SOURCE_DIR=C:/git/googletest "-DMKL_DIR=C:/Program Files (x86)/Intel/oneAPI/mkl/2026.1/lib/cmake/mkl" "-DTBB_DIR=C:/Program Files (x86)/Intel/oneAPI/tbb/2023.1/lib/cmake/tbb" "-DHDF5_DIR=C:/Program Files/HDF_Group/HDF5/2.1.1/cmake"` | 0 | Approved explicit-dependency MSVC x64 build tree generates | Windows SDK, oneMKL 2026.1, oneTBB, and HDF5 resolved; configure and generate completed |
|
||||
| VERIFY-build | `cmake --build .harness/build --config Debug` | 0 | Full Debug build passes without a new FESA warning | `fesa_solver.lib` and `fesa_unit_tests.exe` built under `/W4 /WX` |
|
||||
| VERIFY-targeted | `ctest --test-dir .harness/build -C Debug -R SparseAssembly --output-on-failure` | 0 | Focused Step 18 suite remains green | 5/5 exact `SparseAssembly` tests passed |
|
||||
| VERIFY-discovery | `ctest --test-dir .harness/build -C Debug --show-only=json-v1` | 0 | CTest discovers the accumulated suite and all five exact SparseAssembly names | 47 tests discovered, including 5 `SparseAssembly` tests, with feature/unit labels |
|
||||
| VERIFY-full | `ctest --test-dir .harness/build -C Debug --output-on-failure` | 0 | Full accumulated C++ suite has zero failures | 47/47 tests passed |
|
||||
| VERIFY-contract-scans | Public backend/upward-dependency/PARDISO, assembler shared-CSR-member, dense inheritance, exact-test-count, and CMake registration scans using fail-on-match/count `rg` wrappers | 0 | Sparse storage and reduction remain behind the exact approved boundaries | public backend leaks 0; math-header upward dependencies 0; PARDISO matches 0; assembler CSR-member writes 0; dense inheritance 0; tests 5; registrations 2/2 |
|
||||
| VERIFY-determinism | Exact bytes, reversed completion, serial/TBB, repeated TBB, tuple-cancellation, structural-zero, empty-row, multiply, validation, and symmetry assertions in the targeted suite | 0 | NR-O02 fixed-order reduction and NR-T11 scheduler independence are observable | one serial, one TBB, one reverse-order, and eight repeated TBB assemblies produced byte-identical row/column/value vectors; duplicate cancellation produced the tuple-ordered literal `1.0` |
|
||||
| VERIFY-diff | `git diff --check` plus trailing-whitespace scan over the six new files | 0 | Tracked and untracked Step 18 files have no whitespace errors | Diff check exit 0; new-file trailing whitespace matches 0 |
|
||||
| VERIFY-reference | `git diff --exit-code -- reference/`; `git status --short -- reference/` | 0 | Approved legacy reference artifacts remain read-only and unchanged | Reference diff exit 0 and reference status empty |
|
||||
|
||||
- contract_checks: `SparseMatrix` is a separate owning CSR type with 0-based
|
||||
row offsets and sorted-unique columns. It validates row-offset shape/range,
|
||||
column range/order/uniqueness, COO dimensions, expected-pattern membership,
|
||||
finite inputs, finite ordered sums, and multiply dimensions. Empty rows and
|
||||
rectangular/zero structural layouts remain valid; every expected slot is
|
||||
emitted even when its exact stored value is zero.
|
||||
- contract_checks: COO contributions are stable-sorted by exactly
|
||||
`(row,column,elementOrder,localOrder)` and duplicates are accumulated in
|
||||
that sequence. The cancellation fixture distinguishes tuple order from
|
||||
incoming vector order, and input permutation retains the same result.
|
||||
- contract_checks: `SparseAssembler` validates model/DOF dimensions,
|
||||
element references, and scatter ownership before work. It invokes the
|
||||
injected `ParallelFor` once over stable active-element order; each worker
|
||||
writes only its caller-indexed 144-entry row-major local buffer and failure
|
||||
slot. CSR allocation and reduction occur only after the parallel call.
|
||||
- contract_checks: the public headers expose no oneTBB, MKL, HDF5, or PARDISO
|
||||
type. `SparseMatrix` neither includes nor inherits dense `Matrix`; the math
|
||||
header forward-declares DofManager-owned `SparsePattern` to avoid a public
|
||||
upward include.
|
||||
- generated_evidence: `.harness/build/src/fesa/Debug/fesa_solver.lib`,
|
||||
`.harness/build/tests/Debug/fesa_unit_tests.exe`
|
||||
- reference_diff: unchanged; `git diff --exit-code -- reference/` exit 0
|
||||
- handoff: Step 19 can consume canonical full CSR arrays and deterministic
|
||||
matrix-vector multiplication without depending on dense storage, oneTBB,
|
||||
or a solver backend. Step 20 remains solely responsible for PARDISO
|
||||
descriptors, indexing conversion, factorization, and substitution.
|
||||
- concerns: none; no implementation, environment, backend, or upstream
|
||||
contract conflict was found.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "fesa/core/status.hpp"
|
||||
#include "fesa/math/sparse_matrix.hpp"
|
||||
|
||||
namespace fesa {
|
||||
|
||||
class AnalysisModel;
|
||||
class DofManager;
|
||||
class ParallelFor;
|
||||
|
||||
class SparseAssembler {
|
||||
public:
|
||||
static Result<SparseMatrix> assembleStiffness(
|
||||
const AnalysisModel& model,
|
||||
const DofManager& dofs,
|
||||
const ParallelFor& parallelFor);
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include "fesa/core/status.hpp"
|
||||
#include "fesa/math/vector.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
struct SparsePattern;
|
||||
|
||||
struct CooContribution {
|
||||
std::size_t row;
|
||||
std::size_t column;
|
||||
double value;
|
||||
std::size_t elementOrder;
|
||||
std::size_t localOrder;
|
||||
};
|
||||
|
||||
// Owns canonical 0-based CSR data independently of the dense Matrix adapter.
|
||||
class SparseMatrix {
|
||||
public:
|
||||
static Result<SparseMatrix> fromCoo(
|
||||
std::size_t rows,
|
||||
std::size_t columns,
|
||||
std::vector<CooContribution> contributions,
|
||||
const SparsePattern& expectedPattern);
|
||||
|
||||
std::size_t rows() const noexcept;
|
||||
std::size_t columns() const noexcept;
|
||||
const std::vector<std::size_t>& rowOffsets() const noexcept;
|
||||
const std::vector<std::size_t>& columnIndices() const noexcept;
|
||||
const std::vector<double>& values() const noexcept;
|
||||
Vector multiply(const Vector& rhs) const;
|
||||
Status validate() const;
|
||||
|
||||
private:
|
||||
SparseMatrix(
|
||||
std::size_t rows,
|
||||
std::size_t columns,
|
||||
std::vector<std::size_t> rowOffsets,
|
||||
std::vector<std::size_t> columnIndices,
|
||||
std::vector<double> values);
|
||||
|
||||
std::size_t rows_;
|
||||
std::size_t columns_;
|
||||
std::vector<std::size_t> rowOffsets_;
|
||||
std::vector<std::size_t> columnIndices_;
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
@@ -4,6 +4,7 @@ add_library(
|
||||
analysis/analysis_model.cpp
|
||||
analysis/analysis_state.cpp
|
||||
assembly/parallel_for.cpp
|
||||
assembly/sparse_assembler.cpp
|
||||
build_info.cpp
|
||||
core/diagnostic.cpp
|
||||
core/status.cpp
|
||||
@@ -12,6 +13,7 @@ add_library(
|
||||
io/abaqus/domain_mapper.cpp
|
||||
io/abaqus/input_reader.cpp
|
||||
math/matrix.cpp
|
||||
math/sparse_matrix.cpp
|
||||
math/vector.cpp
|
||||
model/domain.cpp
|
||||
)
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
#include "fesa/assembly/sparse_assembler.hpp"
|
||||
|
||||
#include "fesa/analysis/analysis_model.hpp"
|
||||
#include "fesa/assembly/parallel_for.hpp"
|
||||
#include "fesa/elements/euler_beam_3d.hpp"
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
constexpr std::size_t kDofsPerNode = 6U;
|
||||
constexpr std::size_t kElementDofCount = 12U;
|
||||
constexpr std::size_t kContributionCount =
|
||||
kElementDofCount * kElementDofCount;
|
||||
|
||||
using ElementBuffer = std::array<CooContribution, kContributionCount>;
|
||||
|
||||
Result<SparseMatrix> assemblyFailure(
|
||||
const std::string& code,
|
||||
const SourceLocation& location,
|
||||
const std::string& identity,
|
||||
const std::string& message) {
|
||||
return Result<SparseMatrix>::failure(Status::failure(
|
||||
FailureCategory::model,
|
||||
{{Severity::error,
|
||||
code,
|
||||
location,
|
||||
"*ELEMENT",
|
||||
identity,
|
||||
message}}));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Result<SparseMatrix> SparseAssembler::assembleStiffness(
|
||||
const AnalysisModel& model,
|
||||
const DofManager& dofs,
|
||||
const ParallelFor& parallelFor) {
|
||||
const Domain& domain = model.domain();
|
||||
if (domain.nodes().size() >
|
||||
(std::numeric_limits<std::size_t>::max)() / kDofsPerNode ||
|
||||
dofs.fullDofCount() != domain.nodes().size() * kDofsPerNode) {
|
||||
return assemblyFailure(
|
||||
"invalid-assembly-dimensions",
|
||||
{domain.sourcePath(), 0U},
|
||||
std::to_string(dofs.fullDofCount()),
|
||||
"DofManager dimensions do not match the active model nodes.");
|
||||
}
|
||||
if (model.activeElements().size() >
|
||||
(std::numeric_limits<std::size_t>::max)() / kContributionCount) {
|
||||
return assemblyFailure(
|
||||
"invalid-assembly-dimensions",
|
||||
{domain.sourcePath(), 0U},
|
||||
std::to_string(model.activeElements().size()),
|
||||
"Element contribution storage exceeds the addressable range.");
|
||||
}
|
||||
|
||||
std::vector<std::array<std::size_t, kElementDofCount>> scatters;
|
||||
scatters.reserve(model.activeElements().size());
|
||||
for (const EntityIndex elementIndex : model.activeElements()) {
|
||||
if (elementIndex >= domain.elements().size()) {
|
||||
return assemblyFailure(
|
||||
"invalid-assembly-element",
|
||||
{domain.sourcePath(), 0U},
|
||||
std::to_string(elementIndex),
|
||||
"Active element index is outside the Domain.");
|
||||
}
|
||||
const auto& element = domain.elements()[elementIndex];
|
||||
if (element.nodeIndices[0U] >= domain.nodes().size() ||
|
||||
element.nodeIndices[1U] >= domain.nodes().size() ||
|
||||
element.materialIndex >= domain.materials().size() ||
|
||||
element.sectionIndex >= domain.sections().size()) {
|
||||
return assemblyFailure(
|
||||
"invalid-assembly-element",
|
||||
element.location,
|
||||
element.sourceId.sourceLabelText,
|
||||
"Element references an entity outside the Domain.");
|
||||
}
|
||||
|
||||
std::array<std::size_t, kElementDofCount> scatter{};
|
||||
try {
|
||||
scatter = dofs.elementScatter(elementIndex);
|
||||
} catch (const std::out_of_range&) {
|
||||
return assemblyFailure(
|
||||
"invalid-assembly-scatter",
|
||||
element.location,
|
||||
element.sourceId.sourceLabelText,
|
||||
"DofManager does not contain the active element scatter.");
|
||||
}
|
||||
for (std::size_t endpoint = 0U; endpoint < 2U; ++endpoint) {
|
||||
for (std::size_t component = 0U;
|
||||
component < kDofsPerNode;
|
||||
++component) {
|
||||
const std::size_t local = endpoint * kDofsPerNode + component;
|
||||
const std::size_t expected =
|
||||
static_cast<std::size_t>(element.nodeIndices[endpoint]) *
|
||||
kDofsPerNode +
|
||||
component;
|
||||
if (scatter[local] != expected ||
|
||||
scatter[local] >= dofs.fullDofCount()) {
|
||||
return assemblyFailure(
|
||||
"invalid-assembly-scatter",
|
||||
element.location,
|
||||
element.sourceId.sourceLabelText,
|
||||
"Element scatter does not match the active model topology.");
|
||||
}
|
||||
}
|
||||
}
|
||||
scatters.push_back(scatter);
|
||||
}
|
||||
|
||||
std::vector<ElementBuffer> localBuffers(model.activeElements().size());
|
||||
std::vector<std::optional<Status>> localFailures(
|
||||
model.activeElements().size());
|
||||
parallelFor.execute(
|
||||
model.activeElements().size(),
|
||||
[&](const std::size_t elementOrder) {
|
||||
const EntityIndex elementIndex = model.activeElements()[elementOrder];
|
||||
const auto& definition = domain.elements()[elementIndex];
|
||||
const auto beam = EulerBeam3D::create(
|
||||
domain.nodes()[definition.nodeIndices[0U]],
|
||||
domain.nodes()[definition.nodeIndices[1U]],
|
||||
domain.sections()[definition.sectionIndex],
|
||||
domain.materials()[definition.materialIndex]);
|
||||
if (!beam.hasValue()) {
|
||||
localFailures[elementOrder] = beam.status();
|
||||
return;
|
||||
}
|
||||
|
||||
const Matrix stiffness = beam.value().globalStiffness();
|
||||
auto& buffer = localBuffers[elementOrder];
|
||||
const auto& scatter = scatters[elementOrder];
|
||||
for (std::size_t localRow = 0U;
|
||||
localRow < kElementDofCount;
|
||||
++localRow) {
|
||||
for (std::size_t localColumn = 0U;
|
||||
localColumn < kElementDofCount;
|
||||
++localColumn) {
|
||||
const std::size_t localOrder =
|
||||
localRow * kElementDofCount + localColumn;
|
||||
buffer[localOrder] = {
|
||||
scatter[localRow],
|
||||
scatter[localColumn],
|
||||
stiffness(localRow, localColumn),
|
||||
elementOrder,
|
||||
localOrder};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (std::size_t elementOrder = 0U;
|
||||
elementOrder < localFailures.size();
|
||||
++elementOrder) {
|
||||
if (localFailures[elementOrder]) {
|
||||
return Result<SparseMatrix>::failure(
|
||||
*localFailures[elementOrder]);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<CooContribution> contributions;
|
||||
contributions.reserve(
|
||||
localBuffers.size() * kContributionCount);
|
||||
// Flatten only after all workers complete; workers never share CSR state.
|
||||
for (const auto& buffer : localBuffers) {
|
||||
contributions.insert(
|
||||
contributions.end(), buffer.begin(), buffer.end());
|
||||
}
|
||||
return SparseMatrix::fromCoo(
|
||||
dofs.fullDofCount(),
|
||||
dofs.fullDofCount(),
|
||||
std::move(contributions),
|
||||
dofs.sparsePattern());
|
||||
}
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,239 @@
|
||||
#include "fesa/math/sparse_matrix.hpp"
|
||||
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
Status sparseFailure(
|
||||
const std::string& code,
|
||||
const std::string& identity,
|
||||
const std::string& message) {
|
||||
return Status::failure(
|
||||
FailureCategory::model,
|
||||
{{Severity::error,
|
||||
code,
|
||||
{{}, 0U},
|
||||
"SPARSE_MATRIX",
|
||||
identity,
|
||||
message}});
|
||||
}
|
||||
|
||||
Status validateCsr(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
const std::vector<std::size_t>& rowOffsets,
|
||||
const std::vector<std::size_t>& columnIndices,
|
||||
const std::vector<double>* const values) {
|
||||
if (rows == (std::numeric_limits<std::size_t>::max)() ||
|
||||
rowOffsets.size() != rows + 1U) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-shape",
|
||||
"row-offset-count",
|
||||
"CSR row offsets must contain exactly rows plus one entries.");
|
||||
}
|
||||
if (rowOffsets.empty() || rowOffsets.front() != 0U ||
|
||||
rowOffsets.back() != columnIndices.size()) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-pattern",
|
||||
"row-offset-range",
|
||||
"CSR row offsets must start at zero and end at the column count.");
|
||||
}
|
||||
if (values != nullptr && values->size() != columnIndices.size()) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-shape",
|
||||
"value-count",
|
||||
"CSR column and value arrays must have equal sizes.");
|
||||
}
|
||||
|
||||
for (std::size_t row = 0U; row < rows; ++row) {
|
||||
const std::size_t begin = rowOffsets[row];
|
||||
const std::size_t end = rowOffsets[row + 1U];
|
||||
if (begin > end || end > columnIndices.size()) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-pattern",
|
||||
std::to_string(row),
|
||||
"CSR row offsets must be nondecreasing and remain in range.");
|
||||
}
|
||||
for (std::size_t position = begin; position < end; ++position) {
|
||||
if (columnIndices[position] >= columns) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-index",
|
||||
std::to_string(position),
|
||||
"CSR column index is outside the matrix dimensions.");
|
||||
}
|
||||
if (position > begin &&
|
||||
columnIndices[position - 1U] >= columnIndices[position]) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-pattern",
|
||||
std::to_string(row),
|
||||
"CSR columns must be sorted and unique within each row.");
|
||||
}
|
||||
if (values != nullptr && !std::isfinite((*values)[position])) {
|
||||
return sparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(position),
|
||||
"CSR values must be finite.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Status::ok();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Result<SparseMatrix> SparseMatrix::fromCoo(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
std::vector<CooContribution> contributions,
|
||||
const SparsePattern& expectedPattern) {
|
||||
const Status patternStatus = validateCsr(
|
||||
rows,
|
||||
columns,
|
||||
expectedPattern.rowOffsets,
|
||||
expectedPattern.columnIndices,
|
||||
nullptr);
|
||||
if (!patternStatus.isOk()) {
|
||||
return Result<SparseMatrix>::failure(patternStatus);
|
||||
}
|
||||
|
||||
for (const auto& contribution : contributions) {
|
||||
if (contribution.row >= rows || contribution.column >= columns) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"invalid-sparse-index",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"COO contribution index is outside the matrix dimensions."));
|
||||
}
|
||||
if (!std::isfinite(contribution.value)) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(contribution.elementOrder) + ":" +
|
||||
std::to_string(contribution.localOrder),
|
||||
"COO contribution values must be finite."));
|
||||
}
|
||||
}
|
||||
|
||||
// The complete tuple fixes duplicate summation order independently of
|
||||
// worker completion order. stable_sort also preserves exact tuple ties.
|
||||
std::stable_sort(
|
||||
contributions.begin(),
|
||||
contributions.end(),
|
||||
[](const CooContribution& left, const CooContribution& right) {
|
||||
return std::tie(
|
||||
left.row,
|
||||
left.column,
|
||||
left.elementOrder,
|
||||
left.localOrder) <
|
||||
std::tie(
|
||||
right.row,
|
||||
right.column,
|
||||
right.elementOrder,
|
||||
right.localOrder);
|
||||
});
|
||||
|
||||
std::vector<double> values(expectedPattern.columnIndices.size(), 0.0);
|
||||
for (const auto& contribution : contributions) {
|
||||
const std::size_t begin = expectedPattern.rowOffsets[contribution.row];
|
||||
const std::size_t end = expectedPattern.rowOffsets[contribution.row + 1U];
|
||||
const auto first = expectedPattern.columnIndices.begin() + begin;
|
||||
const auto last = expectedPattern.columnIndices.begin() + end;
|
||||
const auto found = std::lower_bound(first, last, contribution.column);
|
||||
if (found == last || *found != contribution.column) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"sparse-pattern-mismatch",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"COO contribution is absent from the expected sparse pattern."));
|
||||
}
|
||||
|
||||
const std::size_t position = static_cast<std::size_t>(
|
||||
std::distance(expectedPattern.columnIndices.begin(), found));
|
||||
values[position] += contribution.value;
|
||||
if (!std::isfinite(values[position])) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"Ordered COO duplicate summation produced a nonfinite value."));
|
||||
}
|
||||
}
|
||||
|
||||
SparseMatrix matrix{
|
||||
rows,
|
||||
columns,
|
||||
expectedPattern.rowOffsets,
|
||||
expectedPattern.columnIndices,
|
||||
std::move(values)};
|
||||
const Status status = matrix.validate();
|
||||
if (!status.isOk()) {
|
||||
return Result<SparseMatrix>::failure(status);
|
||||
}
|
||||
return Result<SparseMatrix>::success(std::move(matrix));
|
||||
}
|
||||
|
||||
std::size_t SparseMatrix::rows() const noexcept {
|
||||
return rows_;
|
||||
}
|
||||
|
||||
std::size_t SparseMatrix::columns() const noexcept {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const std::vector<std::size_t>& SparseMatrix::rowOffsets() const noexcept {
|
||||
return rowOffsets_;
|
||||
}
|
||||
|
||||
const std::vector<std::size_t>& SparseMatrix::columnIndices() const noexcept {
|
||||
return columnIndices_;
|
||||
}
|
||||
|
||||
const std::vector<double>& SparseMatrix::values() const noexcept {
|
||||
return values_;
|
||||
}
|
||||
|
||||
Vector SparseMatrix::multiply(const Vector& rhs) const {
|
||||
if (columns_ != rhs.size()) {
|
||||
throw std::invalid_argument{
|
||||
"Sparse matrix-vector multiplication has incompatible dimensions."};
|
||||
}
|
||||
|
||||
Vector result{rows_};
|
||||
for (std::size_t row = 0U; row < rows_; ++row) {
|
||||
double value = 0.0;
|
||||
for (std::size_t position = rowOffsets_[row];
|
||||
position < rowOffsets_[row + 1U];
|
||||
++position) {
|
||||
value += values_[position] * rhs[columnIndices_[position]];
|
||||
}
|
||||
result[row] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Status SparseMatrix::validate() const {
|
||||
return validateCsr(
|
||||
rows_, columns_, rowOffsets_, columnIndices_, &values_);
|
||||
}
|
||||
|
||||
SparseMatrix::SparseMatrix(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
std::vector<std::size_t> rowOffsets,
|
||||
std::vector<std::size_t> columnIndices,
|
||||
std::vector<double> values)
|
||||
: rows_{rows},
|
||||
columns_{columns},
|
||||
rowOffsets_{std::move(rowOffsets)},
|
||||
columnIndices_{std::move(columnIndices)},
|
||||
values_{std::move(values)} {}
|
||||
|
||||
} // namespace fesa
|
||||
@@ -6,12 +6,14 @@ add_executable(
|
||||
unit/analysis/analysis_model_test.cpp
|
||||
unit/analysis/analysis_state_test.cpp
|
||||
unit/assembly/parallel_for_test.cpp
|
||||
unit/assembly/sparse_assembler_test.cpp
|
||||
unit/core/diagnostic_test.cpp
|
||||
unit/core/source_identity_test.cpp
|
||||
unit/core/status_test.cpp
|
||||
unit/elements/euler_beam_3d_test.cpp
|
||||
unit/fem/dof_manager_test.cpp
|
||||
unit/math/matrix_test.cpp
|
||||
unit/math/sparse_matrix_test.cpp
|
||||
unit/math/vector_test.cpp
|
||||
unit/io/abaqus/domain_mapper_test.cpp
|
||||
unit/io/abaqus/input_reader_test.cpp
|
||||
|
||||
@@ -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