feat(linear-static-mitc4-shell): step 7 - shell-sparse-assembly

This commit is contained in:
KOKO\Mimi
2026-08-12 20:21:31 +09:00
parent 5e86db461a
commit 047cdb9a4d
2 changed files with 370 additions and 12 deletions
@@ -1,12 +1,14 @@
#include "fesa/analysis/analysis_model.hpp"
#include "fesa/assembly/parallel_for.hpp"
#include "fesa/assembly/sparse_assembler.hpp"
#include "fesa/elements/mitc4_shell.hpp"
#include "fesa/fem/dof_manager.hpp"
#include "fesa/model/domain.hpp"
#include <gtest/gtest.h>
#include <algorithm>
#include <array>
#include <cstring>
#include <filesystem>
#include <utility>
@@ -43,6 +45,103 @@ fesa::ModelDefinition makeDefinition() {
return definition;
}
fesa::ModelDefinition makeShellDefinition(
const fesa::ShellSourceElementType sourceType,
const bool twoElements = false) {
const std::filesystem::path source{"models/shell-sparse-assembly.inp"};
fesa::ModelDefinition definition{};
definition.sourcePath = source;
definition.sourceContentIdentity = "fnv1a64:fedcba9876543210";
if (twoElements) {
definition.nodes = {
{{"Shell-1", 1, "1"}, {0.0, 0.0, 0.0}, {source, 10U}},
{{"Shell-1", 2, "2"}, {1.0, 0.0, 0.0}, {source, 11U}},
{{"Shell-1", 3, "3"}, {2.0, 0.0, 0.0}, {source, 12U}},
{{"Shell-1", 4, "4"}, {0.0, 1.0, 0.0}, {source, 13U}},
{{"Shell-1", 5, "5"}, {1.0, 1.0, 0.0}, {source, 14U}},
{{"Shell-1", 6, "6"}, {2.0, 1.0, 0.0}, {source, 15U}}};
for (std::size_t node = 0U; node < definition.nodes.size(); ++node) {
definition.shellNodeInitialFrames.push_back({
static_cast<fesa::EntityIndex>(node),
{0.0, 0.0, 1.0},
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0}});
}
definition.shellElements = {
{{"Shell-1", 10, "10"}, sourceType, {0U, 1U, 4U, 3U},
0U, 0U, {source, 40U}},
{{"Shell-1", 20, "20"}, sourceType, {1U, 2U, 5U, 4U},
0U, 0U, {source, 41U}}};
} else {
// A YZ-plane fixture catches any accidental global-Z director assumption.
definition.nodes = {
{{"Shell-1", 1, "1"}, {0.0, 0.0, 0.0}, {source, 10U}},
{{"Shell-1", 2, "2"}, {0.0, 1.0, 0.0}, {source, 11U}},
{{"Shell-1", 3, "3"}, {0.0, 1.0, 1.0}, {source, 12U}},
{{"Shell-1", 4, "4"}, {0.0, 0.0, 1.0}, {source, 13U}}};
for (std::size_t node = 0U; node < definition.nodes.size(); ++node) {
definition.shellNodeInitialFrames.push_back({
static_cast<fesa::EntityIndex>(node),
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}});
}
definition.shellElements = {{
{"Shell-1", 10, "10"}, sourceType, {0U, 1U, 2U, 3U},
0U, 0U, {source, 40U}}};
}
definition.materials = {
{"Material", 120.0, 0.25, {source, 20U}}};
definition.shellSections = {
{"ShellSection", 0.2, 0U, {source, 30U}}};
definition.steps = {{
"Step-1", {}, {}, 0.1, 1.0, 0.01, 1.0, {source, 50U}}};
return definition;
}
fesa::Result<fesa::Mitc4Stiffness> directShellStiffness(
const fesa::Domain& domain,
const fesa::EntityIndex elementIndex) {
const auto& definition = domain.shellElements().at(elementIndex);
std::array<const fesa::Node*, 4> nodes{};
std::array<std::array<double, 3>, 4> directors{};
for (std::size_t node = 0U; node < definition.nodeIndices.size(); ++node) {
const fesa::EntityIndex nodeIndex = definition.nodeIndices[node];
nodes[node] = &domain.nodes().at(nodeIndex);
directors[node] = domain.shellNodeInitialFrames().at(nodeIndex).director;
}
auto shell = fesa::Mitc4Shell::create(
nodes,
directors,
domain.shellSections().at(definition.sectionIndex),
domain.materials().at(definition.materialIndex));
if (!shell.hasValue()) {
return fesa::Result<fesa::Mitc4Stiffness>::failure(shell.status());
}
return shell.value().stiffness();
}
fesa::Result<fesa::SparseMatrix> assembleShell(
const fesa::ShellSourceElementType sourceType,
const fesa::ParallelFor& parallelFor,
const bool twoElements = false) {
auto domain = fesa::Domain::create(
makeShellDefinition(sourceType, twoElements));
if (!domain.hasValue()) {
return fesa::Result<fesa::SparseMatrix>::failure(domain.status());
}
auto model = fesa::AnalysisModel::create(domain.value());
if (!model.hasValue()) {
return fesa::Result<fesa::SparseMatrix>::failure(model.status());
}
auto dofs = fesa::DofManager::create(model.value());
if (!dofs.hasValue()) {
return fesa::Result<fesa::SparseMatrix>::failure(dofs.status());
}
return fesa::SparseAssembler::assembleStiffness(
model.value(), dofs.value(), parallelFor);
}
template<class T>
bool byteIdentical(const std::vector<T>& left, const std::vector<T>& right) {
return left.size() == right.size() &&
@@ -155,4 +254,86 @@ TEST(SparseAssembly, SerialTbbAndRepeatedRunsAreByteIdentical) {
EXPECT_NEAR(entry(serial.value(), 12U, 12U), 80.0, 1.0e-12);
}
TEST(
SparseAssembly,
AssemblesFourNodeTwentyFourDofKernelAndPreservesDiagonalSlots) {
auto domain = fesa::Domain::create(
makeShellDefinition(fesa::ShellSourceElementType::s4));
ASSERT_TRUE(domain.hasValue());
auto model = fesa::AnalysisModel::create(domain.value());
ASSERT_TRUE(model.hasValue());
auto dofs = fesa::DofManager::create(model.value());
ASSERT_TRUE(dofs.hasValue());
fesa::SerialParallelFor serialExecutor;
auto assembled = fesa::SparseAssembler::assembleStiffness(
model.value(), dofs.value(), serialExecutor);
auto expected = directShellStiffness(domain.value(), 0U);
ASSERT_TRUE(assembled.hasValue());
ASSERT_TRUE(expected.hasValue());
EXPECT_EQ(assembled.value().rows(), 24U);
EXPECT_EQ(assembled.value().columns(), 24U);
EXPECT_EQ(assembled.value().values().size(), 24U * 24U);
EXPECT_EQ(
assembled.value().rowOffsets(),
dofs.value().sparsePattern().rowOffsets);
EXPECT_EQ(
assembled.value().columnIndices(),
dofs.value().sparsePattern().columnIndices);
for (std::size_t row = 0U; row < 24U; ++row) {
const auto begin = assembled.value().columnIndices().begin() +
assembled.value().rowOffsets()[row];
const auto end = assembled.value().columnIndices().begin() +
assembled.value().rowOffsets()[row + 1U];
EXPECT_NE(std::lower_bound(begin, end, row), end);
for (std::size_t column = 0U; column < 24U; ++column) {
EXPECT_DOUBLE_EQ(
entry(assembled.value(), row, column),
expected.value().stabilizedGlobal24(row, column));
}
}
}
TEST(SparseAssembly, ShellSerialTbbReverseAndRepeatedRunsAreByteIdentical) {
fesa::SerialParallelFor serialExecutor;
fesa::TbbParallelFor tbbExecutor;
ReverseParallelFor reverseExecutor;
auto serial = assembleShell(
fesa::ShellSourceElementType::s4, serialExecutor, true);
auto tbb = assembleShell(
fesa::ShellSourceElementType::s4, tbbExecutor, true);
auto reversed = assembleShell(
fesa::ShellSourceElementType::s4, reverseExecutor, true);
ASSERT_TRUE(serial.hasValue());
ASSERT_TRUE(tbb.hasValue());
ASSERT_TRUE(reversed.hasValue());
EXPECT_EQ(reverseExecutor.calls(), 1U);
EXPECT_EQ(reverseExecutor.observedCount(), 2U);
expectByteIdentical(tbb.value(), serial.value());
expectByteIdentical(reversed.value(), serial.value());
for (std::size_t repetition = 0U; repetition < 8U; ++repetition) {
auto repeated = assembleShell(
fesa::ShellSourceElementType::s4, tbbExecutor, true);
ASSERT_TRUE(repeated.hasValue());
expectByteIdentical(repeated.value(), serial.value());
}
}
TEST(SparseAssembly, S4AndS4rSemanticFixturesAssembleIdenticalStiffness) {
fesa::SerialParallelFor serialExecutor;
auto s4 = assembleShell(
fesa::ShellSourceElementType::s4, serialExecutor);
auto s4r = assembleShell(
fesa::ShellSourceElementType::s4r, serialExecutor);
ASSERT_TRUE(s4.hasValue());
ASSERT_TRUE(s4r.hasValue());
EXPECT_TRUE(std::any_of(
s4.value().values().begin(),
s4.value().values().end(),
[](const double value) { return value != 0.0; }));
expectByteIdentical(s4r.value(), s4.value());
}
} // namespace