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
+189 -12
View File
@@ -3,6 +3,7 @@
#include "fesa/analysis/analysis_model.hpp"
#include "fesa/assembly/parallel_for.hpp"
#include "fesa/elements/euler_beam_3d.hpp"
#include "fesa/elements/mitc4_shell.hpp"
#include "fesa/fem/dof_manager.hpp"
#include <array>
@@ -17,11 +18,17 @@ namespace fesa {
namespace {
constexpr std::size_t kDofsPerNode = 6U;
constexpr std::size_t kElementDofCount = 12U;
constexpr std::size_t kContributionCount =
kElementDofCount * kElementDofCount;
constexpr std::size_t kBeamElementDofCount = 12U;
constexpr std::size_t kBeamContributionCount =
kBeamElementDofCount * kBeamElementDofCount;
constexpr std::size_t kShellElementDofCount = 24U;
constexpr std::size_t kShellContributionCount =
kShellElementDofCount * kShellElementDofCount;
using ElementBuffer = std::array<CooContribution, kContributionCount>;
using BeamElementBuffer =
std::array<CooContribution, kBeamContributionCount>;
using ShellElementBuffer =
std::array<CooContribution, kShellContributionCount>;
Result<SparseMatrix> assemblyFailure(
const std::string& code,
@@ -54,8 +61,178 @@ Result<SparseMatrix> SparseAssembler::assembleStiffness(
std::to_string(dofs.fullDofCount()),
"DofManager dimensions do not match the active model nodes.");
}
if (!model.activeElements().empty() && !domain.shellElements().empty()) {
return assemblyFailure(
"unsupported-mixed-element-model",
{domain.sourcePath(), 0U},
"B33:FESA-MITC4",
"Sparse assembly does not support mixed beam and shell models.");
}
if (!domain.shellElements().empty()) {
if (domain.shellElements().size() >
(std::numeric_limits<std::size_t>::max)() /
kShellContributionCount) {
return assemblyFailure(
"invalid-assembly-dimensions",
{domain.sourcePath(), 0U},
std::to_string(domain.shellElements().size()),
"Shell contribution storage exceeds the addressable range.");
}
std::vector<std::optional<std::array<double, 3>>> directorsByNode(
domain.nodes().size());
for (const auto& frame : domain.shellNodeInitialFrames()) {
if (frame.nodeIndex >= directorsByNode.size() ||
directorsByNode[frame.nodeIndex]) {
return assemblyFailure(
"invalid-assembly-element",
{domain.sourcePath(), 0U},
std::to_string(frame.nodeIndex),
"Shell initial frames must map uniquely to model nodes.");
}
directorsByNode[frame.nodeIndex] = frame.director;
}
struct ShellInput {
std::array<const Node*, 4> nodes;
std::array<std::array<double, 3>, 4> directors;
const ShellSection* section;
const LinearElasticMaterial* material;
std::array<std::size_t, kShellElementDofCount> scatter;
};
std::vector<ShellInput> inputs;
inputs.reserve(domain.shellElements().size());
for (std::size_t elementOrder = 0U;
elementOrder < domain.shellElements().size();
++elementOrder) {
const auto& element = domain.shellElements()[elementOrder];
if (element.materialIndex >= domain.materials().size() ||
element.sectionIndex >= domain.shellSections().size()) {
return assemblyFailure(
"invalid-assembly-element",
element.location,
element.sourceId.sourceLabelText,
"Shell element references an entity outside the Domain.");
}
ShellInput input{};
input.section = &domain.shellSections()[element.sectionIndex];
input.material = &domain.materials()[element.materialIndex];
try {
input.scatter = dofs.shellElementScatter(
static_cast<EntityIndex>(elementOrder));
} catch (const std::out_of_range&) {
return assemblyFailure(
"invalid-assembly-scatter",
element.location,
element.sourceId.sourceLabelText,
"DofManager does not contain the active shell scatter.");
}
for (std::size_t nodePosition = 0U;
nodePosition < element.nodeIndices.size();
++nodePosition) {
const EntityIndex nodeIndex = element.nodeIndices[nodePosition];
if (nodeIndex >= domain.nodes().size() ||
!directorsByNode[nodeIndex]) {
return assemblyFailure(
"invalid-assembly-element",
element.location,
element.sourceId.sourceLabelText,
"Shell element requires a valid node and initial director.");
}
input.nodes[nodePosition] = &domain.nodes()[nodeIndex];
input.directors[nodePosition] = *directorsByNode[nodeIndex];
for (std::size_t component = 0U;
component < kDofsPerNode;
++component) {
const std::size_t local =
nodePosition * kDofsPerNode + component;
const std::size_t expected =
static_cast<std::size_t>(nodeIndex) * kDofsPerNode +
component;
if (input.scatter[local] != expected ||
input.scatter[local] >= dofs.fullDofCount()) {
return assemblyFailure(
"invalid-assembly-scatter",
element.location,
element.sourceId.sourceLabelText,
"Shell scatter does not match the active model topology.");
}
}
}
inputs.push_back(input);
}
std::vector<ShellElementBuffer> localBuffers(inputs.size());
std::vector<std::optional<Status>> localFailures(inputs.size());
parallelFor.execute(
inputs.size(),
[&](const std::size_t elementOrder) {
const auto& input = inputs[elementOrder];
const auto shell = Mitc4Shell::create(
input.nodes,
input.directors,
*input.section,
*input.material);
if (!shell.hasValue()) {
localFailures[elementOrder] = shell.status();
return;
}
const auto stiffness = shell.value().stiffness();
if (!stiffness.hasValue()) {
localFailures[elementOrder] = stiffness.status();
return;
}
auto& buffer = localBuffers[elementOrder];
for (std::size_t localRow = 0U;
localRow < kShellElementDofCount;
++localRow) {
for (std::size_t localColumn = 0U;
localColumn < kShellElementDofCount;
++localColumn) {
const std::size_t localOrder =
localRow * kShellElementDofCount + localColumn;
buffer[localOrder] = {
input.scatter[localRow],
input.scatter[localColumn],
stiffness.value().stabilizedGlobal24(
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() * kShellContributionCount);
// Flatten in source-element order after workers complete. The canonical
// COO reduction remains the sole writer of global CSR values.
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());
}
if (model.activeElements().size() >
(std::numeric_limits<std::size_t>::max)() / kContributionCount) {
(std::numeric_limits<std::size_t>::max)() /
kBeamContributionCount) {
return assemblyFailure(
"invalid-assembly-dimensions",
{domain.sourcePath(), 0U},
@@ -63,7 +240,7 @@ Result<SparseMatrix> SparseAssembler::assembleStiffness(
"Element contribution storage exceeds the addressable range.");
}
std::vector<std::array<std::size_t, kElementDofCount>> scatters;
std::vector<std::array<std::size_t, kBeamElementDofCount>> scatters;
scatters.reserve(model.activeElements().size());
for (const EntityIndex elementIndex : model.activeElements()) {
if (elementIndex >= domain.elements().size()) {
@@ -85,7 +262,7 @@ Result<SparseMatrix> SparseAssembler::assembleStiffness(
"Element references an entity outside the Domain.");
}
std::array<std::size_t, kElementDofCount> scatter{};
std::array<std::size_t, kBeamElementDofCount> scatter{};
try {
scatter = dofs.elementScatter(elementIndex);
} catch (const std::out_of_range&) {
@@ -117,7 +294,7 @@ Result<SparseMatrix> SparseAssembler::assembleStiffness(
scatters.push_back(scatter);
}
std::vector<ElementBuffer> localBuffers(model.activeElements().size());
std::vector<BeamElementBuffer> localBuffers(model.activeElements().size());
std::vector<std::optional<Status>> localFailures(
model.activeElements().size());
parallelFor.execute(
@@ -139,13 +316,13 @@ Result<SparseMatrix> SparseAssembler::assembleStiffness(
auto& buffer = localBuffers[elementOrder];
const auto& scatter = scatters[elementOrder];
for (std::size_t localRow = 0U;
localRow < kElementDofCount;
localRow < kBeamElementDofCount;
++localRow) {
for (std::size_t localColumn = 0U;
localColumn < kElementDofCount;
localColumn < kBeamElementDofCount;
++localColumn) {
const std::size_t localOrder =
localRow * kElementDofCount + localColumn;
localRow * kBeamElementDofCount + localColumn;
buffer[localOrder] = {
scatter[localRow],
scatter[localColumn],
@@ -167,7 +344,7 @@ Result<SparseMatrix> SparseAssembler::assembleStiffness(
std::vector<CooContribution> contributions;
contributions.reserve(
localBuffers.size() * kContributionCount);
localBuffers.size() * kBeamContributionCount);
// Flatten only after all workers complete; workers never share CSR state.
for (const auto& buffer : localBuffers) {
contributions.insert(