feat(cpp-object-oriented-modular-refactoring): step 19 - boundary-condition-policy

This commit is contained in:
KOKO\Mimi
2026-08-16 11:23:20 +09:00
parent f26e0a61a8
commit 64a43948ef
27 changed files with 919 additions and 365 deletions
@@ -1,5 +1,3 @@
#include "fesa/constraints/essential_constraints.h"
#include <gtest/gtest.h>
#include <cstddef>
@@ -10,12 +8,14 @@
#include <vector>
#include "fesa/analysis/analysis_model.h"
#include "fesa/constraints/essential_constraint_policy.h"
#include "fesa/fem/dof_manager.h"
#include "fesa/model/domain.h"
namespace {
fesa::DofManager MakeDofs(std::vector<fesa::BoundaryCondition> boundaries) {
fesa::DofManager MakeDofs(
std::vector<fesa::PrescribedDisplacementDefinition> boundaries) {
const std::filesystem::path source{"models/essential-constraints.inp"};
fesa::ModelDefinition definition{};
definition.source_path = source;
@@ -40,7 +40,7 @@ fesa::DofManager MakeDofs(std::vector<fesa::BoundaryCondition> boundaries) {
}
fesa::DofManager MakeShellSizedDofs(
std::vector<fesa::BoundaryCondition> boundaries) {
std::vector<fesa::PrescribedDisplacementDefinition> boundaries) {
const std::filesystem::path source{"models/shell-essential-constraints.inp"};
fesa::ModelDefinition definition{};
definition.source_path = source;
@@ -124,93 +124,98 @@ TEST(EssentialConstraints, ExtractsHandComputedBlocksInStableOrder) {
full_values[2U * 6U + 4U] = 0.0;
const auto full = MakeMatrix(6U, 6U, full_values);
auto result = fesa::EssentialConstraints::Partition(full, dofs);
const fesa::EssentialConstraintPolicy policy;
auto result = policy.Partition(full, dofs);
ASSERT_TRUE(result.HasValue());
const auto& blocks = result.Value();
EXPECT_EQ(blocks.kff.RowOffsets(),
EXPECT_EQ(blocks.k_ff.RowOffsets(),
(std::vector<std::size_t>{0U, 4U, 8U, 12U, 16U}));
EXPECT_EQ(blocks.kff.ColumnIndices(),
EXPECT_EQ(blocks.k_ff.ColumnIndices(),
(std::vector<std::size_t>{0U, 1U, 2U, 3U, 0U, 1U, 2U, 3U, 0U, 1U,
2U, 3U, 0U, 1U, 2U, 3U}));
EXPECT_EQ(
blocks.kff.Values(),
blocks.k_ff.Values(),
(std::vector<double>{1.0, 3.0, 4.0, 6.0, 21.0, 23.0, 24.0, 26.0, 31.0,
33.0, 34.0, 36.0, 51.0, 53.0, 54.0, 56.0}));
EXPECT_EQ(blocks.kfc.RowOffsets(),
EXPECT_EQ(blocks.k_fc.RowOffsets(),
(std::vector<std::size_t>{0U, 2U, 4U, 6U, 8U}));
EXPECT_EQ(blocks.kfc.ColumnIndices(),
EXPECT_EQ(blocks.k_fc.ColumnIndices(),
(std::vector<std::size_t>{0U, 1U, 0U, 1U, 0U, 1U, 0U, 1U}));
EXPECT_EQ(blocks.kfc.Values(),
EXPECT_EQ(blocks.k_fc.Values(),
(std::vector<double>{2.0, 5.0, 22.0, 0.0, 32.0, 35.0, 52.0, 55.0}));
EXPECT_EQ(blocks.kcf.RowOffsets(), (std::vector<std::size_t>{0U, 4U, 8U}));
EXPECT_EQ(blocks.kcf.ColumnIndices(),
EXPECT_EQ(blocks.k_cf.RowOffsets(), (std::vector<std::size_t>{0U, 4U, 8U}));
EXPECT_EQ(blocks.k_cf.ColumnIndices(),
(std::vector<std::size_t>{0U, 1U, 2U, 3U, 0U, 1U, 2U, 3U}));
EXPECT_EQ(blocks.kcf.Values(), (std::vector<double>{11.0, 13.0, 14.0, 16.0,
41.0, 43.0, 44.0, 46.0}));
EXPECT_EQ(blocks.kcc.RowOffsets(), (std::vector<std::size_t>{0U, 2U, 4U}));
EXPECT_EQ(blocks.kcc.ColumnIndices(),
EXPECT_EQ(
blocks.k_cf.Values(),
(std::vector<double>{11.0, 13.0, 14.0, 16.0, 41.0, 43.0, 44.0, 46.0}));
EXPECT_EQ(blocks.k_cc.RowOffsets(), (std::vector<std::size_t>{0U, 2U, 4U}));
EXPECT_EQ(blocks.k_cc.ColumnIndices(),
(std::vector<std::size_t>{0U, 1U, 0U, 1U}));
EXPECT_EQ(blocks.kcc.Values(), (std::vector<double>{12.0, 15.0, 42.0, 45.0}));
EXPECT_EQ(blocks.k_cc.Values(),
(std::vector<double>{12.0, 15.0, 42.0, 45.0}));
EXPECT_EQ(blocks.kfc.Values()[3U], 0.0);
EXPECT_TRUE(blocks.kff.Validate().IsOk());
EXPECT_TRUE(blocks.kfc.Validate().IsOk());
EXPECT_TRUE(blocks.kcf.Validate().IsOk());
EXPECT_TRUE(blocks.kcc.Validate().IsOk());
EXPECT_EQ(blocks.k_fc.Values()[3U], 0.0);
EXPECT_TRUE(blocks.k_ff.Validate().IsOk());
EXPECT_TRUE(blocks.k_fc.Validate().IsOk());
EXPECT_TRUE(blocks.k_cf.Validate().IsOk());
EXPECT_TRUE(blocks.k_cc.Validate().IsOk());
}
TEST(EssentialConstraints, HandlesNoAllAndMixedConstraints) {
const auto full = MakeMatrix(6U, 6U, SequentialDense(6U));
const fesa::EssentialConstraintPolicy policy;
const auto no_constraints = MakeDofs({});
auto none = fesa::EssentialConstraints::Partition(full, no_constraints);
auto none = policy.Partition(full, no_constraints);
ASSERT_TRUE(none.HasValue());
ExpectShape(none.Value().kff, 6U, 6U);
ExpectShape(none.Value().kfc, 6U, 0U);
ExpectShape(none.Value().kcf, 0U, 6U);
ExpectShape(none.Value().kcc, 0U, 0U);
EXPECT_EQ(none.Value().kff.Values(), full.Values());
ExpectShape(none.Value().k_ff, 6U, 6U);
ExpectShape(none.Value().k_fc, 6U, 0U);
ExpectShape(none.Value().k_cf, 0U, 6U);
ExpectShape(none.Value().k_cc, 0U, 0U);
EXPECT_EQ(none.Value().k_ff.Values(), full.Values());
const auto all_constraints = MakeDofs({{"1", 1, 6, 1.0, {{}, 12U}}});
auto all = fesa::EssentialConstraints::Partition(full, all_constraints);
auto all = policy.Partition(full, all_constraints);
ASSERT_TRUE(all.HasValue());
ExpectShape(all.Value().kff, 0U, 0U);
ExpectShape(all.Value().kfc, 0U, 6U);
ExpectShape(all.Value().kcf, 6U, 0U);
ExpectShape(all.Value().kcc, 6U, 6U);
EXPECT_EQ(all.Value().kcc.Values(), full.Values());
ExpectShape(all.Value().k_ff, 0U, 0U);
ExpectShape(all.Value().k_fc, 0U, 6U);
ExpectShape(all.Value().k_cf, 6U, 0U);
ExpectShape(all.Value().k_cc, 6U, 6U);
EXPECT_EQ(all.Value().k_cc.Values(), full.Values());
const auto mixed_constraints = MakeDofs({{"1", 3, 4, 0.0, {{}, 12U}}});
auto mixed = fesa::EssentialConstraints::Partition(full, mixed_constraints);
auto mixed = policy.Partition(full, mixed_constraints);
ASSERT_TRUE(mixed.HasValue());
ExpectShape(mixed.Value().kff, 4U, 4U);
ExpectShape(mixed.Value().kfc, 4U, 2U);
ExpectShape(mixed.Value().kcf, 2U, 4U);
ExpectShape(mixed.Value().kcc, 2U, 2U);
ExpectShape(mixed.Value().k_ff, 4U, 4U);
ExpectShape(mixed.Value().k_fc, 4U, 2U);
ExpectShape(mixed.Value().k_cf, 2U, 4U);
ExpectShape(mixed.Value().k_cc, 2U, 2U);
}
// MITC4-DOF-003
TEST(EssentialConstraints,
PreservesShellSizedNoMixedAndAllConstraintRoundTrips) {
const auto full = MakeMatrix(24U, 24U, SequentialDense(24U));
const fesa::EssentialConstraintPolicy policy;
const auto no_constraints = MakeShellSizedDofs({});
auto none = fesa::EssentialConstraints::Partition(full, no_constraints);
auto none = policy.Partition(full, no_constraints);
ASSERT_TRUE(none.HasValue());
ExpectShape(none.Value().kff, 24U, 24U);
ExpectShape(none.Value().kfc, 24U, 0U);
ExpectShape(none.Value().kcf, 0U, 24U);
ExpectShape(none.Value().kcc, 0U, 0U);
ExpectShape(none.Value().k_ff, 24U, 24U);
ExpectShape(none.Value().k_fc, 24U, 0U);
ExpectShape(none.Value().k_cf, 0U, 24U);
ExpectShape(none.Value().k_cc, 0U, 0U);
const auto mixed_constraints = MakeShellSizedDofs(
{{"1", 1, 6, 0.0, {{}, 12U}}, {"4", 2, 2, 2.5, {{}, 13U}}});
auto mixed = fesa::EssentialConstraints::Partition(full, mixed_constraints);
auto mixed = policy.Partition(full, mixed_constraints);
ASSERT_TRUE(mixed.HasValue());
ExpectShape(mixed.Value().kff, 17U, 17U);
ExpectShape(mixed.Value().kfc, 17U, 7U);
ExpectShape(mixed.Value().kcf, 7U, 17U);
ExpectShape(mixed.Value().kcc, 7U, 7U);
ExpectShape(mixed.Value().k_ff, 17U, 17U);
ExpectShape(mixed.Value().k_fc, 17U, 7U);
ExpectShape(mixed.Value().k_cf, 7U, 17U);
ExpectShape(mixed.Value().k_cc, 7U, 7U);
fesa::Vector mixed_full{24U};
for (std::size_t index = 0U; index < mixed_full.Size(); ++index) {
@@ -221,9 +226,8 @@ TEST(EssentialConstraints,
mixed_full[mixed_constraints.ConstrainedDofs()[index]] =
mixed_constraints.PrescribedValues()[index];
}
const auto mixed_free =
fesa::EssentialConstraints::GatherFree(mixed_full, mixed_constraints);
const auto mixed_reconstructed = fesa::EssentialConstraints::ReconstructFull(
const auto mixed_free = policy.GatherFree(mixed_full, mixed_constraints);
const auto mixed_reconstructed = policy.ReconstructFull(
mixed_free, mixed_constraints.PrescribedValues(), mixed_constraints);
ASSERT_EQ(mixed_reconstructed.Size(), mixed_full.Size());
for (std::size_t index = 0U; index < mixed_full.Size(); ++index) {
@@ -235,14 +239,14 @@ TEST(EssentialConstraints,
{"2", 1, 6, 2.0, {{}, 15U}},
{"3", 1, 6, 3.0, {{}, 16U}},
{"4", 1, 6, 4.0, {{}, 17U}}});
auto all = fesa::EssentialConstraints::Partition(full, all_constraints);
auto all = policy.Partition(full, all_constraints);
ASSERT_TRUE(all.HasValue());
ExpectShape(all.Value().kff, 0U, 0U);
ExpectShape(all.Value().kfc, 0U, 24U);
ExpectShape(all.Value().kcf, 24U, 0U);
ExpectShape(all.Value().kcc, 24U, 24U);
ExpectShape(all.Value().k_ff, 0U, 0U);
ExpectShape(all.Value().k_fc, 0U, 24U);
ExpectShape(all.Value().k_cf, 24U, 0U);
ExpectShape(all.Value().k_cc, 24U, 24U);
const auto all_reconstructed = fesa::EssentialConstraints::ReconstructFull(
const auto all_reconstructed = policy.ReconstructFull(
fesa::Vector{0U}, all_constraints.PrescribedValues(), all_constraints);
ASSERT_EQ(all_reconstructed.Size(), 24U);
for (std::size_t node = 0U; node < 4U; ++node) {
@@ -253,6 +257,7 @@ TEST(EssentialConstraints,
}
TEST(EssentialConstraints, ReconstructsNonzeroPrescribedValues) {
const fesa::EssentialConstraintPolicy policy;
const auto dofs =
MakeDofs({{"1", 2, 2, 2.5, {{}, 12U}}, {"1", 5, 5, -3.25, {{}, 13U}}});
fesa::Vector full{6U};
@@ -263,9 +268,8 @@ TEST(EssentialConstraints, ReconstructsNonzeroPrescribedValues) {
full[4U] = -3.25;
full[5U] = 40.0;
const auto free = fesa::EssentialConstraints::GatherFree(full, dofs);
const auto constrained =
fesa::EssentialConstraints::GatherConstrained(full, dofs);
const auto free = policy.GatherFree(full, dofs);
const auto constrained = policy.GatherConstrained(full, dofs);
EXPECT_EQ(free.Size(), 4U);
EXPECT_DOUBLE_EQ(free[0U], 10.0);
EXPECT_DOUBLE_EQ(free[1U], 20.0);
@@ -277,8 +281,8 @@ TEST(EssentialConstraints, ReconstructsNonzeroPrescribedValues) {
EXPECT_EQ(constrained[0U], dofs.PrescribedValues()[0U]);
EXPECT_EQ(constrained[1U], dofs.PrescribedValues()[1U]);
const auto reconstructed = fesa::EssentialConstraints::ReconstructFull(
free, dofs.PrescribedValues(), dofs);
const auto reconstructed =
policy.ReconstructFull(free, dofs.PrescribedValues(), dofs);
ASSERT_EQ(reconstructed.Size(), full.Size());
for (std::size_t index = 0U; index < full.Size(); ++index) {
EXPECT_DOUBLE_EQ(reconstructed[index], full[index]);
@@ -286,10 +290,10 @@ TEST(EssentialConstraints, ReconstructsNonzeroPrescribedValues) {
}
TEST(EssentialConstraints, RejectsDimensionOrOrderMismatch) {
const fesa::EssentialConstraintPolicy policy;
const auto dofs = MakeDofs({{"1", 2, 2, 1.0, {{}, 12U}}});
const auto wrong_square = MakeMatrix(5U, 5U, SequentialDense(5U));
auto wrong_dimension =
fesa::EssentialConstraints::Partition(wrong_square, dofs);
auto wrong_dimension = policy.Partition(wrong_square, dofs);
ASSERT_FALSE(wrong_dimension.HasValue());
EXPECT_EQ(wrong_dimension.GetStatus().Category(),
fesa::FailureCategory::kModel);
@@ -298,18 +302,17 @@ TEST(EssentialConstraints, RejectsDimensionOrOrderMismatch) {
"invalid-constraint-dimensions");
const auto rectangular = MakeMatrix(6U, 5U, std::vector<double>(30U, 0.0));
auto wrong_order = fesa::EssentialConstraints::Partition(rectangular, dofs);
auto wrong_order = policy.Partition(rectangular, dofs);
ASSERT_FALSE(wrong_order.HasValue());
EXPECT_EQ(wrong_order.GetStatus().Diagnostics()[0U].code,
"invalid-constraint-dimensions");
EXPECT_THROW(static_cast<void>(fesa::EssentialConstraints::GatherFree(
fesa::Vector{5U}, dofs)),
EXPECT_THROW(static_cast<void>(policy.GatherFree(fesa::Vector{5U}, dofs)),
std::invalid_argument);
EXPECT_THROW(static_cast<void>(fesa::EssentialConstraints::GatherConstrained(
fesa::Vector{7U}, dofs)),
std::invalid_argument);
EXPECT_THROW(static_cast<void>(fesa::EssentialConstraints::ReconstructFull(
EXPECT_THROW(
static_cast<void>(policy.GatherConstrained(fesa::Vector{7U}, dofs)),
std::invalid_argument);
EXPECT_THROW(static_cast<void>(policy.ReconstructFull(
fesa::Vector{4U}, fesa::Vector{2U}, dofs)),
std::invalid_argument);
}