feat(linear-static-3d-euler-beam): step 21 - load-assembly
This commit is contained in:
@@ -6,6 +6,7 @@ add_executable(
|
||||
unit/analysis/analysis_model_test.cpp
|
||||
unit/analysis/analysis_state_test.cpp
|
||||
unit/assembly/parallel_for_test.cpp
|
||||
unit/assembly/load_assembler_test.cpp
|
||||
unit/assembly/sparse_assembler_test.cpp
|
||||
unit/constraints/essential_constraints_test.cpp
|
||||
unit/core/diagnostic_test.cpp
|
||||
|
||||
@@ -0,0 +1,349 @@
|
||||
#include "fesa/assembly/load_assembler.hpp"
|
||||
|
||||
#include "fesa/analysis/analysis_model.hpp"
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
#include "fesa/model/domain.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
struct LoadFixture {
|
||||
std::unique_ptr<fesa::Domain> domain;
|
||||
std::unique_ptr<fesa::AnalysisModel> model;
|
||||
std::unique_ptr<fesa::DofManager> dofs;
|
||||
};
|
||||
|
||||
LoadFixture makeFixture(
|
||||
const std::size_t nodeCount,
|
||||
std::vector<fesa::NodeSet> nodeSets,
|
||||
std::vector<fesa::BoundaryCondition> boundaries,
|
||||
std::vector<fesa::NodalLoad> loads) {
|
||||
const std::filesystem::path source{"models/load-assembly.inp"};
|
||||
fesa::ModelDefinition definition{};
|
||||
definition.sourcePath = source;
|
||||
definition.sourceContentIdentity = "fnv1a64:abcdef0123456789";
|
||||
for (std::size_t index = 0U; index < nodeCount; ++index) {
|
||||
const auto label = static_cast<std::int64_t>((index + 1U) * 10U);
|
||||
definition.nodes.push_back({
|
||||
{"Beam-1", label, std::to_string(label)},
|
||||
{static_cast<double>(index), 0.0, 0.0},
|
||||
{source, index + 2U}});
|
||||
}
|
||||
definition.nodeSets = std::move(nodeSets);
|
||||
definition.steps = {{
|
||||
"Step-1",
|
||||
std::move(boundaries),
|
||||
std::move(loads),
|
||||
0.1,
|
||||
1.0,
|
||||
0.01,
|
||||
1.0,
|
||||
{source, 20U}}};
|
||||
|
||||
auto domainResult = fesa::Domain::create(std::move(definition));
|
||||
if (!domainResult.hasValue()) {
|
||||
throw std::runtime_error{"Load fixture Domain construction failed."};
|
||||
}
|
||||
auto domain = std::make_unique<fesa::Domain>(
|
||||
std::move(domainResult.value()));
|
||||
|
||||
auto modelResult = fesa::AnalysisModel::create(*domain);
|
||||
if (!modelResult.hasValue()) {
|
||||
throw std::runtime_error{"Load fixture AnalysisModel construction failed."};
|
||||
}
|
||||
auto model = std::make_unique<fesa::AnalysisModel>(
|
||||
std::move(modelResult.value()));
|
||||
|
||||
auto dofResult = fesa::DofManager::create(*model);
|
||||
if (!dofResult.hasValue()) {
|
||||
throw std::runtime_error{"Load fixture DofManager construction failed."};
|
||||
}
|
||||
auto dofs = std::make_unique<fesa::DofManager>(
|
||||
std::move(dofResult.value()));
|
||||
return {std::move(domain), std::move(model), std::move(dofs)};
|
||||
}
|
||||
|
||||
fesa::SparseMatrix makeDenseSparse(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
const std::vector<double>& values) {
|
||||
if (values.size() != rows * columns) {
|
||||
throw std::invalid_argument{"Dense sparse fixture has the wrong value count."};
|
||||
}
|
||||
|
||||
fesa::SparsePattern pattern;
|
||||
std::vector<fesa::CooContribution> contributions;
|
||||
pattern.rowOffsets.reserve(rows + 1U);
|
||||
pattern.rowOffsets.push_back(0U);
|
||||
for (std::size_t row = 0U; row < rows; ++row) {
|
||||
for (std::size_t column = 0U; column < columns; ++column) {
|
||||
pattern.columnIndices.push_back(column);
|
||||
contributions.push_back({
|
||||
row,
|
||||
column,
|
||||
values[row * columns + column],
|
||||
row,
|
||||
column});
|
||||
}
|
||||
pattern.rowOffsets.push_back(pattern.columnIndices.size());
|
||||
}
|
||||
|
||||
auto result = fesa::SparseMatrix::fromCoo(
|
||||
rows, columns, std::move(contributions), pattern);
|
||||
if (!result.hasValue()) {
|
||||
throw std::runtime_error{"Sparse fixture construction failed."};
|
||||
}
|
||||
return std::move(result.value());
|
||||
}
|
||||
|
||||
void expectFailureCode(
|
||||
const fesa::Result<fesa::Vector>& result,
|
||||
const std::string& code) {
|
||||
ASSERT_FALSE(result.hasValue());
|
||||
EXPECT_EQ(result.status().failureCategory(), fesa::FailureCategory::model);
|
||||
ASSERT_EQ(result.status().diagnostics().size(), 1U);
|
||||
EXPECT_EQ(result.status().diagnostics()[0U].code, code);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(LoadAssembly, AssemblesNodeSetAndSixComponentLoads) {
|
||||
const std::filesystem::path source{"models/load-assembly.inp"};
|
||||
auto fixture = makeFixture(
|
||||
2U,
|
||||
{{"Pair", std::nullopt, {0U, 1U}, {source, 10U}}},
|
||||
{{"10", 1, 1, 0.0, {source, 21U}}},
|
||||
{{"pair", 1, 1.0, {source, 30U}},
|
||||
{"10", 2, 2.0, {source, 31U}},
|
||||
{"20", 3, 3.0, {source, 32U}},
|
||||
{"10", 4, -4.0, {source, 33U}},
|
||||
{"PAIR", 5, 5.0, {source, 34U}},
|
||||
{"20", 6, 6.0, {source, 35U}}});
|
||||
|
||||
auto result = fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*fixture.model, *fixture.dofs);
|
||||
ASSERT_TRUE(result.hasValue());
|
||||
ASSERT_EQ(result.value().size(), 12U);
|
||||
EXPECT_EQ(
|
||||
std::vector<double>(result.value().data(), result.value().data() + 12U),
|
||||
(std::vector<double>{
|
||||
1.0, 2.0, 0.0, -4.0, 5.0, 0.0,
|
||||
1.0, 0.0, 3.0, 0.0, 5.0, 6.0}));
|
||||
EXPECT_EQ(fixture.dofs->constrainedDofs(),
|
||||
(std::vector<std::size_t>{0U}));
|
||||
EXPECT_DOUBLE_EQ(result.value()[0U], 1.0);
|
||||
}
|
||||
|
||||
TEST(LoadAssembly, AccumulatesSignedLoadsInSourceOrder) {
|
||||
const std::filesystem::path source{"models/load-assembly.inp"};
|
||||
auto firstOrder = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{},
|
||||
{{"10", 1, 1.0e16, {source, 30U}},
|
||||
{"10", 1, -1.0e16, {source, 31U}},
|
||||
{"10", 1, 1.0, {source, 32U}}});
|
||||
auto secondOrder = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{},
|
||||
{{"10", 1, 1.0e16, {source, 30U}},
|
||||
{"10", 1, 1.0, {source, 31U}},
|
||||
{"10", 1, -1.0e16, {source, 32U}}});
|
||||
|
||||
auto first = fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*firstOrder.model, *firstOrder.dofs);
|
||||
auto second = fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*secondOrder.model, *secondOrder.dofs);
|
||||
ASSERT_TRUE(first.hasValue());
|
||||
ASSERT_TRUE(second.hasValue());
|
||||
EXPECT_DOUBLE_EQ(first.value()[0U], 1.0);
|
||||
EXPECT_DOUBLE_EQ(second.value()[0U], 0.0);
|
||||
}
|
||||
|
||||
TEST(LoadAssembly, FormsNonzeroPrescribedEffectiveRhs) {
|
||||
const std::filesystem::path source{"models/load-assembly.inp"};
|
||||
auto fixture = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{{"10", 2, 2, 2.0, {source, 21U}},
|
||||
{"10", 5, 5, -1.0, {source, 22U}}},
|
||||
{{"10", 1, 10.0, {source, 30U}},
|
||||
{"10", 2, 900.0, {source, 31U}},
|
||||
{"10", 3, 20.0, {source, 32U}},
|
||||
{"10", 4, 30.0, {source, 33U}},
|
||||
{"10", 5, 800.0, {source, 34U}},
|
||||
{"10", 6, 40.0, {source, 35U}}});
|
||||
auto full = fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*fixture.model, *fixture.dofs);
|
||||
ASSERT_TRUE(full.hasValue());
|
||||
const auto kfc = makeDenseSparse(
|
||||
4U,
|
||||
2U,
|
||||
{1.0, 2.0,
|
||||
3.0, 4.0,
|
||||
-2.0, 5.0,
|
||||
0.5, -1.0});
|
||||
|
||||
auto rhs = fesa::LoadAssembler::effectiveFreeRhs(
|
||||
full.value(), kfc, fixture.dofs->prescribedValues(), *fixture.dofs);
|
||||
ASSERT_TRUE(rhs.hasValue());
|
||||
ASSERT_EQ(rhs.value().size(), 4U);
|
||||
EXPECT_EQ(
|
||||
std::vector<double>(rhs.value().data(), rhs.value().data() + 4U),
|
||||
(std::vector<double>{10.0, 18.0, 39.0, 38.0}));
|
||||
}
|
||||
|
||||
TEST(LoadAssembly, RejectsNonfiniteOrDimensionMismatch) {
|
||||
const std::filesystem::path source{"models/load-assembly.inp"};
|
||||
const double maximum = (std::numeric_limits<double>::max)();
|
||||
auto nonfinite = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{},
|
||||
{{"10", 1, std::numeric_limits<double>::quiet_NaN(), {source, 30U}}});
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*nonfinite.model, *nonfinite.dofs),
|
||||
"nonfinite-load-value");
|
||||
|
||||
auto overflow = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{},
|
||||
{{"10", 1, maximum, {source, 30U}},
|
||||
{"10", 1, maximum, {source, 31U}}});
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*overflow.model, *overflow.dofs),
|
||||
"nonfinite-load-accumulation");
|
||||
|
||||
auto oneNode = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{{"10", 2, 2, 2.0, {source, 21U}},
|
||||
{"10", 5, 5, -1.0, {source, 22U}}},
|
||||
{});
|
||||
auto twoNodes = makeFixture(2U, {}, {}, {});
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*twoNodes.model, *oneNode.dofs),
|
||||
"invalid-load-dimensions");
|
||||
|
||||
const auto validKfc = makeDenseSparse(4U, 2U, std::vector<double>(8U, 0.0));
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
fesa::Vector{5U},
|
||||
validKfc,
|
||||
oneNode.dofs->prescribedValues(),
|
||||
*oneNode.dofs),
|
||||
"invalid-load-dimensions");
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
fesa::Vector{6U},
|
||||
makeDenseSparse(3U, 2U, std::vector<double>(6U, 0.0)),
|
||||
oneNode.dofs->prescribedValues(),
|
||||
*oneNode.dofs),
|
||||
"invalid-load-dimensions");
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
fesa::Vector{6U},
|
||||
makeDenseSparse(4U, 1U, std::vector<double>(4U, 0.0)),
|
||||
oneNode.dofs->prescribedValues(),
|
||||
*oneNode.dofs),
|
||||
"invalid-load-dimensions");
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
fesa::Vector{6U}, validKfc, fesa::Vector{1U}, *oneNode.dofs),
|
||||
"invalid-load-dimensions");
|
||||
|
||||
fesa::Vector nonfiniteFull{6U};
|
||||
nonfiniteFull[0U] = std::numeric_limits<double>::infinity();
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
nonfiniteFull,
|
||||
validKfc,
|
||||
oneNode.dofs->prescribedValues(),
|
||||
*oneNode.dofs),
|
||||
"nonfinite-load-value");
|
||||
|
||||
fesa::Vector nonfinitePrescribed{2U};
|
||||
nonfinitePrescribed[0U] = std::numeric_limits<double>::quiet_NaN();
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
fesa::Vector{6U}, validKfc, nonfinitePrescribed, *oneNode.dofs),
|
||||
"nonfinite-load-value");
|
||||
|
||||
const auto overflowingKfc = makeDenseSparse(
|
||||
4U,
|
||||
2U,
|
||||
{maximum, 0.0,
|
||||
0.0, 0.0,
|
||||
0.0, 0.0,
|
||||
0.0, 0.0});
|
||||
expectFailureCode(
|
||||
fesa::LoadAssembler::effectiveFreeRhs(
|
||||
fesa::Vector{6U},
|
||||
overflowingKfc,
|
||||
oneNode.dofs->prescribedValues(),
|
||||
*oneNode.dofs),
|
||||
"nonfinite-load-accumulation");
|
||||
}
|
||||
|
||||
TEST(LoadAssembly, ZeroLoadsRemainZero) {
|
||||
const std::filesystem::path source{"models/load-assembly.inp"};
|
||||
auto freeFixture = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{},
|
||||
{{"10", 3, 0.0, {source, 30U}}});
|
||||
auto full = fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*freeFixture.model, *freeFixture.dofs);
|
||||
ASSERT_TRUE(full.hasValue());
|
||||
EXPECT_TRUE(std::all_of(
|
||||
full.value().data(),
|
||||
full.value().data() + full.value().size(),
|
||||
[](const double value) { return value == 0.0; }));
|
||||
const auto noConstrainedColumns = makeDenseSparse(6U, 0U, {});
|
||||
auto freeRhs = fesa::LoadAssembler::effectiveFreeRhs(
|
||||
full.value(),
|
||||
noConstrainedColumns,
|
||||
freeFixture.dofs->prescribedValues(),
|
||||
*freeFixture.dofs);
|
||||
ASSERT_TRUE(freeRhs.hasValue());
|
||||
EXPECT_EQ(freeRhs.value().size(), 6U);
|
||||
EXPECT_TRUE(std::all_of(
|
||||
freeRhs.value().data(),
|
||||
freeRhs.value().data() + freeRhs.value().size(),
|
||||
[](const double value) { return value == 0.0; }));
|
||||
|
||||
auto constrainedFixture = makeFixture(
|
||||
1U,
|
||||
{},
|
||||
{{"10", 1, 6, 0.0, {source, 21U}}},
|
||||
{});
|
||||
auto constrainedFull = fesa::LoadAssembler::assembleFullNodalLoad(
|
||||
*constrainedFixture.model, *constrainedFixture.dofs);
|
||||
ASSERT_TRUE(constrainedFull.hasValue());
|
||||
const auto noFreeRows = makeDenseSparse(0U, 6U, {});
|
||||
auto constrainedRhs = fesa::LoadAssembler::effectiveFreeRhs(
|
||||
constrainedFull.value(),
|
||||
noFreeRows,
|
||||
constrainedFixture.dofs->prescribedValues(),
|
||||
*constrainedFixture.dofs);
|
||||
ASSERT_TRUE(constrainedRhs.hasValue());
|
||||
EXPECT_EQ(constrainedRhs.value().size(), 0U);
|
||||
}
|
||||
Reference in New Issue
Block a user