feat: add eigen2 frequency pipeline and fail-closed validation
Implement MITC4 mass, constrained whitening, MKL FEAST, modal HDF5, and deterministic Value comparison. MSVC Debug builds; 394 of 395 tests pass. Original-model all-c residual and the documented S3 static contract conflict remain unresolved; this is an implementation checkpoint, not feature acceptance.
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
#include "analysis/eigen_reduction.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/fem/dof_manager.h"
|
||||
#include "fesa/solvers/eigen/mkl_feast_eigen_solver.h"
|
||||
|
||||
namespace {
|
||||
|
||||
fesa::SparseMatrix Sparse(const fesa::Matrix& dense) {
|
||||
fesa::SparsePattern pattern;
|
||||
std::vector<fesa::CooContribution> entries;
|
||||
pattern.row_offsets.push_back(0U);
|
||||
for (std::size_t i = 0; i < dense.Rows(); ++i) {
|
||||
for (std::size_t j = 0; j < dense.Columns(); ++j) {
|
||||
pattern.column_indices.push_back(j);
|
||||
entries.push_back({i, j, dense(i, j), 0U, i * dense.Columns() + j});
|
||||
}
|
||||
pattern.row_offsets.push_back(pattern.column_indices.size());
|
||||
}
|
||||
auto result = fesa::SparseMatrix::FromCoo(dense.Rows(), dense.Columns(),
|
||||
std::move(entries), pattern);
|
||||
EXPECT_TRUE(result.HasValue());
|
||||
return std::move(result.Value());
|
||||
}
|
||||
|
||||
fesa::Matrix NodalMass(const std::array<double, 3>& d) {
|
||||
fesa::Matrix mass(6U, 6U);
|
||||
for (std::size_t i = 0; i < 3U; ++i) {
|
||||
mass(i, i) = 2.0;
|
||||
for (std::size_t j = 0; j < 3U; ++j) {
|
||||
mass(i + 3U, j + 3U) = (static_cast<double>(i == j) - d[i] * d[j]) / 6.0;
|
||||
}
|
||||
}
|
||||
return mass;
|
||||
}
|
||||
|
||||
fesa::ShellNodeInitialFrame TiltedFrame() {
|
||||
return {0U, {0.6, 0.0, 0.8}, {0.0, 1.0, 0.0}, {-0.8, 0.0, 0.6}};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(EigenReduction, KeepsPositivePartialGlobalRotationAndOriginalPencil) {
|
||||
const auto frame = TiltedFrame();
|
||||
fesa::Matrix stiffness(6U, 6U);
|
||||
stiffness(5U, 5U) = 10369.0 / 7500.0;
|
||||
std::vector<std::uint8_t> mask(6U, 1U);
|
||||
mask[5U] = 0U;
|
||||
const auto result = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {NodalMass(frame.director)}, {frame}, mask,
|
||||
{19.0 / 4800.0}, 4.0);
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
EXPECT_EQ(result.Value().null_count, 0U);
|
||||
EXPECT_EQ(result.Value().whitened_operator.Rows(), 1U);
|
||||
EXPECT_NEAR(result.Value().whitened_operator.Values()[0], 10369.0 / 450.0,
|
||||
1e-12);
|
||||
fesa::Matrix x(1U, 1U, 1.0);
|
||||
const auto y = fesa::eigen_internal::RestoreScaledMode(result.Value(), x, 0U);
|
||||
for (std::size_t i = 0; i < 5U; ++i) EXPECT_DOUBLE_EQ(y[i], 0.0);
|
||||
EXPECT_NEAR(y[5U] * y[5U] * (3.0 / 50.0), 1.0, 1e-12);
|
||||
}
|
||||
|
||||
TEST(EigenReduction, RemovesOnlyActualFreeDirectorNull) {
|
||||
const fesa::ShellNodeInitialFrame frame{0U, {0, 0, 1}, {1, 0, 0}, {0, 1, 0}};
|
||||
auto mass = NodalMass(frame.director);
|
||||
fesa::Matrix stiffness(6U, 6U);
|
||||
for (std::size_t i = 0; i < 5U; ++i) stiffness(i, i) = mass(i, i) * 7.0;
|
||||
stiffness(5U, 5U) = 0.25;
|
||||
const auto result = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {mass}, {frame}, std::vector<std::uint8_t>(6U, 0U),
|
||||
{0.25}, 2.0);
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
EXPECT_EQ(result.Value().free_count, 6U);
|
||||
EXPECT_EQ(result.Value().null_count, 1U);
|
||||
EXPECT_EQ(result.Value().whitened_operator.Rows(), 5U);
|
||||
fesa::Vector ones(5U, 1.0);
|
||||
const auto action = result.Value().whitened_operator.Multiply(ones);
|
||||
for (std::size_t i = 0; i < 5U; ++i) EXPECT_NEAR(action[i], 7.0, 1e-12);
|
||||
EXPECT_LE(result.Value().eta_kz, 1e-12);
|
||||
stiffness(0U, 5U) = stiffness(5U, 0U) = 0.1;
|
||||
EXPECT_FALSE(fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {mass}, {frame},
|
||||
std::vector<std::uint8_t>(6U, 0U), {0.25}, 2.0)
|
||||
.HasValue());
|
||||
}
|
||||
|
||||
TEST(EigenReduction, RejectsPositiveButUnresolvableMassWithoutDeletingDofs) {
|
||||
const double p = 1e-8;
|
||||
const std::array<double, 3> d{2 * p / (1 + p * p), 0,
|
||||
(1 - p * p) / (1 + p * p)};
|
||||
const fesa::ShellNodeInitialFrame frame{0U, d, {0, 1, 0}, {-d[2], 0, d[0]}};
|
||||
fesa::Matrix stiffness(6U, 6U);
|
||||
for (std::size_t i = 0; i < 6U; ++i) stiffness(i, i) = 1.0;
|
||||
std::vector<std::uint8_t> mask(6U, 0U);
|
||||
mask[3U] = 1U;
|
||||
const auto result = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {NodalMass(d)}, {frame}, mask, {0.25}, 1.0);
|
||||
ASSERT_FALSE(result.HasValue());
|
||||
EXPECT_EQ(result.GetStatus().Diagnostics()[0].code,
|
||||
"eigen-mass-conditioning");
|
||||
}
|
||||
|
||||
TEST(EigenReduction, WhitensOffDiagonalMassAndRestoresOriginalPencil) {
|
||||
const fesa::ShellNodeInitialFrame frame{0U, {0, 0, 1}, {1, 0, 0}, {0, 1, 0}};
|
||||
auto mass = NodalMass(frame.director);
|
||||
mass(0, 1) = mass(1, 0) = 1.0;
|
||||
fesa::Matrix stiffness(6U, 6U);
|
||||
stiffness(0, 0) = stiffness(1, 1) = 6.0;
|
||||
stiffness(0, 1) = stiffness(1, 0) = 4.0;
|
||||
std::vector<std::uint8_t> mask{0U, 0U, 1U, 1U, 1U, 1U};
|
||||
const auto reduction = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {mass}, {frame}, mask, {0.25}, 3.0);
|
||||
ASSERT_TRUE(reduction.HasValue());
|
||||
const auto eigen = fesa::MklFeastEigenSolver{}.Solve(
|
||||
reduction.Value().whitened_operator, 2U, 1.0);
|
||||
ASSERT_TRUE(eigen.HasValue());
|
||||
ASSERT_EQ(eigen.Value().eigenvalues.size(), 2U);
|
||||
EXPECT_NEAR(eigen.Value().eigenvalues[0], 2.0, 1e-12);
|
||||
EXPECT_NEAR(eigen.Value().eigenvalues[1], 10.0 / 3.0, 1e-12);
|
||||
for (std::size_t mode = 0; mode < 2U; ++mode) {
|
||||
auto y = fesa::eigen_internal::RestoreScaledMode(
|
||||
reduction.Value(), eigen.Value().eigenvectors, mode);
|
||||
fesa::Vector q = y;
|
||||
for (std::size_t i = 0; i < 3U; ++i) q[i] *= 3.0;
|
||||
const auto mq = mass.Multiply(q);
|
||||
EXPECT_NEAR(q.Dot(mq), 1.0, 1e-12);
|
||||
auto residual = stiffness.Multiply(q);
|
||||
residual.Axpy(-eigen.Value().eigenvalues[mode], mq);
|
||||
EXPECT_LE(residual.Norm(), 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EigenReduction, PreservesSmallResolvablePositivePartialRotationMass) {
|
||||
const double p = 1.0 / 2000.0;
|
||||
const std::array<double, 3> d{2 * p / (1 + p * p), 0,
|
||||
(1 - p * p) / (1 + p * p)};
|
||||
const fesa::ShellNodeInitialFrame frame{0U, d, {0, 1, 0}, {-d[2], 0, d[0]}};
|
||||
fesa::Matrix stiffness(6U, 6U);
|
||||
for (std::size_t i = 0; i < 6U; ++i) stiffness(i, i) = 1.0;
|
||||
std::vector<std::uint8_t> mask(6U, 0U);
|
||||
mask[3U] = 1U;
|
||||
const auto result = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {NodalMass(d)}, {frame}, mask, {0.25}, 1.0);
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
EXPECT_EQ(result.Value().null_count, 0U);
|
||||
EXPECT_EQ(result.Value().whitened_operator.Rows(), 5U);
|
||||
EXPECT_GE(result.Value().nodes[0].record.rcond_inf, 0x1p-32);
|
||||
}
|
||||
|
||||
TEST(EigenReduction, HandlesTwoFreeRotationsAndRejectsEmptyFiniteSpace) {
|
||||
const fesa::ShellNodeInitialFrame frame{0U, {0, 0, 1}, {1, 0, 0}, {0, 1, 0}};
|
||||
auto mass = NodalMass(frame.director);
|
||||
fesa::Matrix stiffness(6U, 6U);
|
||||
stiffness(3U, 3U) = 1.0;
|
||||
stiffness(5U, 5U) = 0.25;
|
||||
std::vector<std::uint8_t> mask{1U, 1U, 1U, 0U, 1U, 0U};
|
||||
auto result = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {mass}, {frame}, mask, {0.25}, 1.0);
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
EXPECT_EQ(result.Value().null_count, 1U);
|
||||
EXPECT_EQ(result.Value().whitened_operator.Rows(), 1U);
|
||||
mass(5U, 5U) = 0.1;
|
||||
EXPECT_FALSE(fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {mass}, {frame}, mask, {0.25}, 1.0)
|
||||
.HasValue());
|
||||
mass(5U, 5U) = 0.0;
|
||||
for (const bool drill_free : {false, true}) {
|
||||
mask.assign(6U, 1U);
|
||||
mask[5U] = drill_free ? 0U : 1U;
|
||||
const auto empty = fesa::eigen_internal::BuildEigenReduction(
|
||||
Sparse(stiffness), {mass}, {frame}, mask, {0.25}, 1.0);
|
||||
ASSERT_FALSE(empty.HasValue());
|
||||
EXPECT_EQ(empty.GetStatus().Category(), fesa::FailureCategory::kModel);
|
||||
EXPECT_EQ(empty.GetStatus().Diagnostics()[0].code, "empty-eigenproblem");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "fesa/elements/mitc4_shell.h"
|
||||
#include "fesa/model/model_types.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using Coordinates = std::array<double, 3>;
|
||||
|
||||
fesa::Node Node(std::int64_t label, Coordinates coordinates) {
|
||||
return {{"I", label, std::to_string(label)}, coordinates, {"mass.inp", 1U}};
|
||||
}
|
||||
|
||||
std::array<const fesa::Node*, 4> Pointers(
|
||||
const std::array<fesa::Node, 4>& nodes) {
|
||||
return {&nodes[0], &nodes[1], &nodes[2], &nodes[3]};
|
||||
}
|
||||
|
||||
fesa::Mitc4Shell WarpedShell() {
|
||||
const std::array<fesa::Node, 4> nodes{
|
||||
Node(1, {-1.0, -1.0, 0.1}), Node(2, {1.0, -1.0, -0.1}),
|
||||
Node(3, {1.0, 1.0, 0.2}), Node(4, {-1.0, 1.0, -0.1})};
|
||||
const std::array<Coordinates, 4> directors{
|
||||
{{{0.0, 0.0, 1.0}},
|
||||
{{0.6, 0.0, 0.8}},
|
||||
{{2.0 / 3.0, 1.0 / 3.0, 2.0 / 3.0}},
|
||||
{{0.0, 0.6, 0.8}}}};
|
||||
const fesa::ShellSection section{"S", 0.5, 0U, {"mass.inp", 2U}};
|
||||
const fesa::LinearElasticMaterial material{"M", 12.0, 0.0, {"mass.inp", 3U}};
|
||||
const auto result =
|
||||
fesa::Mitc4Shell::Create(Pointers(nodes), directors, section, material);
|
||||
EXPECT_TRUE(result.HasValue());
|
||||
return result.Value();
|
||||
}
|
||||
|
||||
double Quadratic(const fesa::Matrix& matrix, const fesa::Vector& vector) {
|
||||
return vector.Dot(matrix.Multiply(vector));
|
||||
}
|
||||
|
||||
// E2-C02: values derive independently in numerical-review-evidence.md.
|
||||
TEST(Mitc4ShellMassContract, PreservesWarpedMomentsRowsAndFirstSpatialMoment) {
|
||||
const auto mass = WarpedShell().Mass(3.0);
|
||||
ASSERT_TRUE(mass.HasValue());
|
||||
const auto& value = mass.Value();
|
||||
EXPECT_NEAR(value.consistent_moments[0U](0U, 0U), 17609.0 / 28800.0, 1e-12);
|
||||
EXPECT_NEAR(value.consistent_moments[1U](0U, 0U), 11003.0 / 1440000.0, 1e-12);
|
||||
EXPECT_NEAR(value.consistent_moments[2U](0U, 0U), 88177.0 / 6912000.0, 1e-12);
|
||||
double total_m = 0.0, total_h = 0.0, total_j = 0.0;
|
||||
for (const auto& row : value.nodal_moments) {
|
||||
total_m += row[0U];
|
||||
total_h += row[1U];
|
||||
total_j += row[2U];
|
||||
}
|
||||
EXPECT_NEAR(total_m, 7743.0 / 1600.0, 1e-12);
|
||||
EXPECT_NEAR(total_h, 739.0 / 12000.0, 1e-12);
|
||||
EXPECT_NEAR(total_j, 517.0 / 5120.0, 1e-12);
|
||||
}
|
||||
|
||||
// E2-C03: scalar row-sum lumping retains uniform translation energy and is
|
||||
// never below the consistent kinetic energy.
|
||||
TEST(Mitc4ShellMassContract, SatisfiesUniformAndJensenEnergyContracts) {
|
||||
const auto mass = WarpedShell().Mass(3.0);
|
||||
ASSERT_TRUE(mass.HasValue());
|
||||
fesa::Vector translation{24U};
|
||||
fesa::Vector field{24U};
|
||||
for (std::size_t node = 0U; node < 4U; ++node) {
|
||||
translation[6U * node] = 1.0;
|
||||
field[6U * node] = static_cast<double>(node + 1U);
|
||||
field[6U * node + 1U] = -0.5 * static_cast<double>(node);
|
||||
field[6U * node + 3U] = 0.25;
|
||||
}
|
||||
EXPECT_NEAR(Quadratic(mass.Value().consistent_global24, translation),
|
||||
Quadratic(mass.Value().lumped_global24, translation), 1e-12);
|
||||
EXPECT_GE(Quadratic(mass.Value().lumped_global24, field) + 1e-12,
|
||||
Quadratic(mass.Value().consistent_global24, field));
|
||||
}
|
||||
|
||||
// E2-C04: assemble the physical velocity work over successively finer meshes.
|
||||
TEST(Mitc4ShellMassContract, ReproducesAffineSquareVelocityWork) {
|
||||
const std::array<Coordinates, 4> directors{{{{0.0, 0.0, 1.0}},
|
||||
{{0.0, 0.0, 1.0}},
|
||||
{{0.0, 0.0, 1.0}},
|
||||
{{0.0, 0.0, 1.0}}}};
|
||||
const fesa::ShellSection section{"S", 1.0, 0U, {"mass.inp", 2U}};
|
||||
const fesa::LinearElasticMaterial material{"M", 12.0, 0.0, {"mass.inp", 3U}};
|
||||
for (const int mesh : {1, 2, 4}) {
|
||||
double consistent_work = 0.0, lumped_work = 0.0;
|
||||
const double edge = 2.0 / mesh;
|
||||
for (int row = 0; row < mesh; ++row) {
|
||||
for (int column = 0; column < mesh; ++column) {
|
||||
const double x = -1.0 + edge * column, y = -1.0 + edge * row;
|
||||
const std::array<fesa::Node, 4> nodes{
|
||||
Node(1, {x, y, 0.0}), Node(2, {x + edge, y, 0.0}),
|
||||
Node(3, {x + edge, y + edge, 0.0}), Node(4, {x, y + edge, 0.0})};
|
||||
const auto shell = fesa::Mitc4Shell::Create(Pointers(nodes), directors,
|
||||
section, material);
|
||||
ASSERT_TRUE(shell.HasValue());
|
||||
const auto mass = shell.Value().Mass(1.0);
|
||||
ASSERT_TRUE(mass.HasValue());
|
||||
fesa::Vector velocity{24U};
|
||||
for (std::size_t node = 0U; node < 4U; ++node)
|
||||
velocity[6U * node] = nodes[node].coordinates[0U];
|
||||
consistent_work +=
|
||||
Quadratic(mass.Value().consistent_global24, velocity);
|
||||
lumped_work += Quadratic(mass.Value().lumped_global24, velocity);
|
||||
}
|
||||
}
|
||||
EXPECT_NEAR(consistent_work, 4.0 / 3.0, 1e-12) << mesh;
|
||||
EXPECT_NEAR(lumped_work - consistent_work, 8.0 / (3.0 * mesh * mesh), 1e-12)
|
||||
<< mesh;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -53,6 +53,12 @@ Vector3 Cross(const Vector3& left, const Vector3& right) {
|
||||
left[0] * right[1] - left[1] * right[0]};
|
||||
}
|
||||
|
||||
Vector3 RotateAboutGlobalZ(const Vector3& value) {
|
||||
constexpr double kInverseSqrtTwo = 0.70710678118654752440;
|
||||
return {kInverseSqrtTwo * (value[0U] - value[1U]),
|
||||
kInverseSqrtTwo * (value[0U] + value[1U]), value[2U]};
|
||||
}
|
||||
|
||||
double Norm(const Vector3& value) { return std::sqrt(Dot(value, value)); }
|
||||
|
||||
void ExpectVectorNear(const Vector3& actual, const Vector3& expected,
|
||||
@@ -636,7 +642,6 @@ TEST(Mitc4ShellKernel,
|
||||
EXPECT_DOUBLE_EQ(stiffness.physical_local20(0U, 0U), 0x1.d555555555554p+6);
|
||||
EXPECT_DOUBLE_EQ(stiffness.physical_local20(0U, 4U), 0.0);
|
||||
EXPECT_DOUBLE_EQ(stiffness.physical_global24(2U, 2U), 0x1.aaaaaaaaaaaadp+5);
|
||||
EXPECT_DOUBLE_EQ(stiffness.drilling_stiffness, 0x1.0d6cffc5beeb5p-4);
|
||||
EXPECT_DOUBLE_EQ(recovery.strain_energy, 0x1.20a3d70a3d70bp+6);
|
||||
constexpr std::array<double, 8> kExpectedStrain{
|
||||
0x1.999999999999ap-4, -0x1.999999999999bp-5, 0x1.9999999999998p-3,
|
||||
@@ -791,8 +796,7 @@ TEST(Mitc4ShellPatch, ReproducesIndependentMembraneBendingShearAndTwistFields) {
|
||||
}
|
||||
|
||||
// MITC4-KERNEL-005
|
||||
TEST(Mitc4ShellDrilling,
|
||||
UsesOnlyEightPositivePhysicalRotationDiagonalsAndFixedFactor) {
|
||||
TEST(Mitc4ShellDrilling, UsesAllPhysicalRotationDiagonalsAndFixedFactor) {
|
||||
const std::array<fesa::Node, 4> nodes{
|
||||
Node(1, {-50.0, -50.0, 0.0}), Node(2, {50.0, -50.0, 0.0}),
|
||||
Node(3, {50.0, 50.0, 0.0}), Node(4, {-50.0, 50.0, 0.0})};
|
||||
@@ -803,27 +807,222 @@ TEST(Mitc4ShellDrilling,
|
||||
ASSERT_TRUE(stiffness_candidate.HasValue());
|
||||
const auto& stiffness = stiffness_candidate.Value();
|
||||
|
||||
double expected_reference = (std::numeric_limits<double>::max)();
|
||||
double all_diagonal_minimum = (std::numeric_limits<double>::max)();
|
||||
double expected_sum = 0.0;
|
||||
for (std::size_t node_index = 0U; node_index < 4U; ++node_index) {
|
||||
for (std::size_t rotation = 3U; rotation < 5U; ++rotation) {
|
||||
const std::size_t index = 5U * node_index + rotation;
|
||||
const double diagonal = stiffness.physical_local20(index, index);
|
||||
ASSERT_TRUE(std::isfinite(diagonal));
|
||||
ASSERT_GT(diagonal, 0.0);
|
||||
expected_reference = (std::min)(expected_reference, diagonal);
|
||||
}
|
||||
for (std::size_t component = 0U; component < 5U; ++component) {
|
||||
const std::size_t index = 5U * node_index + component;
|
||||
const double diagonal = stiffness.physical_local20(index, index);
|
||||
if (std::isfinite(diagonal) && diagonal > 0.0) {
|
||||
all_diagonal_minimum = (std::min)(all_diagonal_minimum, diagonal);
|
||||
}
|
||||
ASSERT_GE(diagonal, 0.0);
|
||||
expected_sum += diagonal;
|
||||
}
|
||||
}
|
||||
EXPECT_DOUBLE_EQ(stiffness.drilling_stiffness, 1.0e-3 * expected_reference);
|
||||
EXPECT_LT(all_diagonal_minimum, expected_reference);
|
||||
EXPECT_NE(stiffness.drilling_stiffness, 1.0e-3 * all_diagonal_minimum);
|
||||
EXPECT_DOUBLE_EQ(stiffness.drilling_stiffness, 1.0e-3 * expected_sum / 8.0);
|
||||
}
|
||||
|
||||
TEST(Mitc4ShellMass, IntegratesFlatConsistentAndScalarRowSumLumpedMass) {
|
||||
const auto nodes = PlanarNodes();
|
||||
const auto shell_candidate = fesa::Mitc4Shell::Create(
|
||||
NodePointers(nodes), Directors(), Section(), Material());
|
||||
ASSERT_TRUE(shell_candidate.HasValue());
|
||||
|
||||
const auto mass_candidate = shell_candidate.Value().Mass(3.0);
|
||||
ASSERT_TRUE(mass_candidate.HasValue());
|
||||
const auto& mass = mass_candidate.Value();
|
||||
const std::array<std::array<double, 4>, 4> pattern{{{{4.0, 2.0, 1.0, 2.0}},
|
||||
{{2.0, 4.0, 2.0, 1.0}},
|
||||
{{1.0, 2.0, 4.0, 2.0}},
|
||||
{{2.0, 1.0, 2.0, 4.0}}}};
|
||||
for (std::size_t row = 0U; row < 4U; ++row) {
|
||||
EXPECT_NEAR(mass.nodal_moments[row][0U], 6.0, 1.0e-12);
|
||||
EXPECT_NEAR(mass.nodal_moments[row][1U], 0.0, 1.0e-12);
|
||||
EXPECT_NEAR(mass.nodal_moments[row][2U], 2.0, 1.0e-12);
|
||||
for (std::size_t column = 0U; column < 4U; ++column) {
|
||||
EXPECT_NEAR(mass.consistent_moments[0U](row, column),
|
||||
pattern[row][column] * (2.0 / 3.0), 1.0e-12);
|
||||
EXPECT_NEAR(mass.consistent_moments[1U](row, column), 0.0, 1.0e-12);
|
||||
EXPECT_NEAR(mass.consistent_moments[2U](row, column),
|
||||
pattern[row][column] * (2.0 / 9.0), 1.0e-12);
|
||||
}
|
||||
for (std::size_t component = 0U; component < 3U; ++component) {
|
||||
EXPECT_NEAR(
|
||||
mass.lumped_global24(6U * row + component, 6U * row + component), 6.0,
|
||||
1.0e-12);
|
||||
EXPECT_NEAR(mass.lumped_global24(6U * row + 3U + component,
|
||||
6U * row + 3U + component),
|
||||
component == 2U ? 0.0 : 2.0, 1.0e-12);
|
||||
}
|
||||
}
|
||||
ExpectSymmetric(mass.consistent_global24);
|
||||
ExpectSymmetric(mass.lumped_global24);
|
||||
}
|
||||
|
||||
TEST(Mitc4ShellMass, RejectsNonpositiveAndNonfiniteDensity) {
|
||||
const auto nodes = PlanarNodes();
|
||||
const auto shell_candidate = fesa::Mitc4Shell::Create(
|
||||
NodePointers(nodes), Directors(), Section(), Material());
|
||||
ASSERT_TRUE(shell_candidate.HasValue());
|
||||
for (const double density :
|
||||
{0.0, -1.0, std::numeric_limits<double>::infinity()}) {
|
||||
const auto mass = shell_candidate.Value().Mass(density);
|
||||
ASSERT_FALSE(mass.HasValue());
|
||||
ASSERT_EQ(mass.GetStatus().Diagnostics().size(), 1U);
|
||||
EXPECT_EQ(mass.GetStatus().Diagnostics()[0U].code, "invalid-shell-mass");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Mitc4ShellMass, PreservesFiniteNodalVarianceAcrossDensityScale) {
|
||||
const auto nodes = PlanarNodes();
|
||||
const auto shell_candidate = fesa::Mitc4Shell::Create(
|
||||
NodePointers(nodes), Directors(), Section(), Material());
|
||||
ASSERT_TRUE(shell_candidate.HasValue());
|
||||
for (const double density : {1.0e-180, 1.0e180}) {
|
||||
const auto mass_candidate = shell_candidate.Value().Mass(density);
|
||||
ASSERT_TRUE(mass_candidate.HasValue());
|
||||
for (const auto& moments : mass_candidate.Value().nodal_moments) {
|
||||
EXPECT_TRUE(std::isfinite(moments[0U]));
|
||||
EXPECT_TRUE(std::isfinite(moments[1U]));
|
||||
EXPECT_TRUE(std::isfinite(moments[2U]));
|
||||
EXPECT_GT(moments[2U] - moments[1U] * (moments[1U] / moments[0U]), 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExpectGlobalZCovariance(const fesa::Matrix& original,
|
||||
const fesa::Matrix& rotated) {
|
||||
ASSERT_EQ(original.Rows(), 24U);
|
||||
ASSERT_EQ(original.Columns(), 24U);
|
||||
ASSERT_EQ(rotated.Rows(), 24U);
|
||||
ASSERT_EQ(rotated.Columns(), 24U);
|
||||
constexpr double kInverseSqrtTwo = 0.70710678118654752440;
|
||||
const std::array<std::array<double, 3>, 3> transform{
|
||||
{{{kInverseSqrtTwo, -kInverseSqrtTwo, 0.0}},
|
||||
{{kInverseSqrtTwo, kInverseSqrtTwo, 0.0}},
|
||||
{{0.0, 0.0, 1.0}}}};
|
||||
for (std::size_t row = 0U; row < 24U; ++row) {
|
||||
for (std::size_t column = 0U; column < 24U; ++column) {
|
||||
double expected = 0.0;
|
||||
const std::size_t row_offset =
|
||||
6U * (row / 6U) + (row % 6U < 3U ? 0U : 3U);
|
||||
const std::size_t column_offset =
|
||||
6U * (column / 6U) + (column % 6U < 3U ? 0U : 3U);
|
||||
const std::size_t row_component = row % 3U;
|
||||
const std::size_t column_component = column % 3U;
|
||||
for (std::size_t first = 0U; first < 3U; ++first) {
|
||||
for (std::size_t second = 0U; second < 3U; ++second) {
|
||||
expected += transform[row_component][first] *
|
||||
original(row_offset + first, column_offset + second) *
|
||||
transform[column_component][second];
|
||||
}
|
||||
}
|
||||
EXPECT_NEAR(rotated(row, column), expected, 1.0e-12)
|
||||
<< "at (" << row << ", " << column << ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Mitc4ShellMass, PreservesIndependentWarpedMomentOracle) {
|
||||
const std::array<fesa::Node, 4> nodes{
|
||||
Node(1, {-1.0, -1.0, 0.1}), Node(2, {1.0, -1.0, -0.1}),
|
||||
Node(3, {1.0, 1.0, 0.2}), Node(4, {-1.0, 1.0, -0.1})};
|
||||
const std::array<Vector3, 4> directors{{{{0.0, 0.0, 1.0}},
|
||||
{{0.6, 0.0, 0.8}},
|
||||
{{2.0 / 3.0, 1.0 / 3.0, 2.0 / 3.0}},
|
||||
{{0.0, 0.6, 0.8}}}};
|
||||
const auto shell_candidate = fesa::Mitc4Shell::Create(
|
||||
NodePointers(nodes), directors, Section(0.5), Material());
|
||||
ASSERT_TRUE(shell_candidate.HasValue());
|
||||
const auto mass_candidate = shell_candidate.Value().Mass(3.0);
|
||||
ASSERT_TRUE(mass_candidate.HasValue());
|
||||
const auto& mass = mass_candidate.Value();
|
||||
|
||||
EXPECT_NEAR(mass.consistent_moments[0U](0U, 0U), 17609.0 / 28800.0, 1.0e-12);
|
||||
EXPECT_NEAR(mass.consistent_moments[1U](0U, 0U), 11003.0 / 1440000.0,
|
||||
1.0e-12);
|
||||
EXPECT_NEAR(mass.consistent_moments[2U](0U, 0U), 88177.0 / 6912000.0,
|
||||
1.0e-12);
|
||||
double total_mass = 0.0;
|
||||
double total_first_moment = 0.0;
|
||||
double total_second_moment = 0.0;
|
||||
for (const auto& moments : mass.nodal_moments) {
|
||||
total_mass += moments[0U];
|
||||
total_first_moment += moments[1U];
|
||||
total_second_moment += moments[2U];
|
||||
}
|
||||
EXPECT_NEAR(total_mass, 7743.0 / 1600.0, 1.0e-12);
|
||||
EXPECT_NEAR(total_first_moment, 739.0 / 12000.0, 1.0e-12);
|
||||
EXPECT_NEAR(total_second_moment, 517.0 / 5120.0, 1.0e-12);
|
||||
}
|
||||
|
||||
TEST(Mitc4ShellMass, MeetsPartialBoundaryCompleteMatrixTargets) {
|
||||
const Vector3 tangent_a{0.0, 1.0, 0.0};
|
||||
const Vector3 tangent_b{-0.8, 0.0, 0.6};
|
||||
const std::array<fesa::Node, 4> nodes{
|
||||
Node(1, {-2.0 * tangent_a[0U] - tangent_b[0U],
|
||||
-2.0 * tangent_a[1U] - tangent_b[1U],
|
||||
-2.0 * tangent_a[2U] - tangent_b[2U]}),
|
||||
Node(2, {2.0 * tangent_a[0U] - tangent_b[0U],
|
||||
2.0 * tangent_a[1U] - tangent_b[1U],
|
||||
2.0 * tangent_a[2U] - tangent_b[2U]}),
|
||||
Node(3, {2.0 * tangent_a[0U] + tangent_b[0U],
|
||||
2.0 * tangent_a[1U] + tangent_b[1U],
|
||||
2.0 * tangent_a[2U] + tangent_b[2U]}),
|
||||
Node(4, {-2.0 * tangent_a[0U] + tangent_b[0U],
|
||||
-2.0 * tangent_a[1U] + tangent_b[1U],
|
||||
-2.0 * tangent_a[2U] + tangent_b[2U]})};
|
||||
const auto shell_candidate =
|
||||
fesa::Mitc4Shell::Create(NodePointers(nodes), Directors({0.6, 0.0, 0.8}),
|
||||
Section(1.0), Material(12.0, 0.0));
|
||||
ASSERT_TRUE(shell_candidate.HasValue());
|
||||
const auto stiffness_candidate = shell_candidate.Value().Stiffness();
|
||||
const auto mass_candidate = shell_candidate.Value().Mass(1.0);
|
||||
ASSERT_TRUE(stiffness_candidate.HasValue());
|
||||
ASSERT_TRUE(mass_candidate.HasValue());
|
||||
const double complete_stiffness =
|
||||
stiffness_candidate.Value().stabilized_global24(5U, 5U);
|
||||
const double lumped_mass = mass_candidate.Value().lumped_global24(5U, 5U);
|
||||
EXPECT_NEAR(stiffness_candidate.Value().drilling_stiffness, 19.0 / 4800.0,
|
||||
1.0e-12);
|
||||
EXPECT_NEAR(complete_stiffness, 10369.0 / 7500.0, 1.0e-12);
|
||||
EXPECT_NEAR(lumped_mass, 3.0 / 50.0, 1.0e-12);
|
||||
EXPECT_NEAR(complete_stiffness / lumped_mass, 10369.0 / 450.0, 1.0e-12);
|
||||
}
|
||||
|
||||
TEST(Mitc4ShellMass, RecomputesCompleteMassAndStiffnessCovariantly) {
|
||||
const std::array<fesa::Node, 4> nodes{
|
||||
Node(1, {-1.0, -1.0, 0.1}), Node(2, {1.0, -1.0, -0.1}),
|
||||
Node(3, {1.0, 1.0, 0.2}), Node(4, {-1.0, 1.0, -0.1})};
|
||||
const std::array<Vector3, 4> directors{{{{0.0, 0.0, 1.0}},
|
||||
{{0.6, 0.0, 0.8}},
|
||||
{{2.0 / 3.0, 1.0 / 3.0, 2.0 / 3.0}},
|
||||
{{0.0, 0.6, 0.8}}}};
|
||||
std::array<fesa::Node, 4> rotated_nodes = nodes;
|
||||
std::array<Vector3, 4> rotated_directors{};
|
||||
for (std::size_t node = 0U; node < nodes.size(); ++node) {
|
||||
rotated_nodes[node].coordinates =
|
||||
RotateAboutGlobalZ(nodes[node].coordinates);
|
||||
rotated_directors[node] = RotateAboutGlobalZ(directors[node]);
|
||||
}
|
||||
const auto original = fesa::Mitc4Shell::Create(NodePointers(nodes), directors,
|
||||
Section(0.5), Material());
|
||||
const auto rotated = fesa::Mitc4Shell::Create(
|
||||
NodePointers(rotated_nodes), rotated_directors, Section(0.5), Material());
|
||||
ASSERT_TRUE(original.HasValue());
|
||||
ASSERT_TRUE(rotated.HasValue());
|
||||
const auto original_stiffness = original.Value().Stiffness();
|
||||
const auto rotated_stiffness = rotated.Value().Stiffness();
|
||||
const auto original_mass = original.Value().Mass(3.0);
|
||||
const auto rotated_mass = rotated.Value().Mass(3.0);
|
||||
ASSERT_TRUE(original_stiffness.HasValue());
|
||||
ASSERT_TRUE(rotated_stiffness.HasValue());
|
||||
ASSERT_TRUE(original_mass.HasValue());
|
||||
ASSERT_TRUE(rotated_mass.HasValue());
|
||||
ExpectGlobalZCovariance(original_stiffness.Value().stabilized_global24,
|
||||
rotated_stiffness.Value().stabilized_global24);
|
||||
ExpectGlobalZCovariance(original_mass.Value().consistent_global24,
|
||||
rotated_mass.Value().consistent_global24);
|
||||
ExpectGlobalZCovariance(original_mass.Value().lumped_global24,
|
||||
rotated_mass.Value().lumped_global24);
|
||||
}
|
||||
|
||||
// MITC4-KERNEL-006
|
||||
|
||||
@@ -157,6 +157,9 @@ TEST(DomainMappingComponents,
|
||||
EXPECT_EQ(domain.Steps()[0].Loads().size(), 1U);
|
||||
ASSERT_EQ(domain.Warnings().size(), 3U);
|
||||
EXPECT_EQ(domain.Warnings()[0].keyword, "PREPRINT");
|
||||
EXPECT_EQ(
|
||||
domain.Warnings()[0].message,
|
||||
"The allowlisted Abaqus keyword is ignored without semantic effect.");
|
||||
EXPECT_EQ(domain.Warnings()[1].keyword, "OUTPUT");
|
||||
EXPECT_EQ(domain.Warnings()[2].keyword, "NODE OUTPUT");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "fesa/io/abaqus/domain_mapper.h"
|
||||
#include "fesa/io/abaqus/input_reader.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class TemporaryInputFile {
|
||||
public:
|
||||
TemporaryInputFile(const std::string& stem, const std::string& content)
|
||||
: path_{std::filesystem::temp_directory_path() /
|
||||
("fesa-eigen-domain-" + stem + ".inp")} {
|
||||
std::ofstream stream{path_, std::ios::binary | std::ios::trunc};
|
||||
stream.write(content.data(), static_cast<std::streamsize>(content.size()));
|
||||
if (!stream) {
|
||||
throw std::runtime_error{"Unable to create eigen domain fixture."};
|
||||
}
|
||||
}
|
||||
|
||||
~TemporaryInputFile() {
|
||||
std::error_code error;
|
||||
std::filesystem::remove(path_, error);
|
||||
}
|
||||
|
||||
const std::filesystem::path& Path() const noexcept { return path_; }
|
||||
|
||||
private:
|
||||
std::filesystem::path path_;
|
||||
};
|
||||
|
||||
fesa::Result<fesa::Domain> MapText(const std::string& stem,
|
||||
const std::string& content) {
|
||||
const TemporaryInputFile input{stem, content};
|
||||
auto parsed = fesa::AbaqusInputReader{}.Read(input.Path());
|
||||
if (!parsed.HasValue()) {
|
||||
return fesa::Result<fesa::Domain>::Failure(parsed.GetStatus());
|
||||
}
|
||||
return fesa::AbaqusDomainMapper{}.Map(parsed.Value());
|
||||
}
|
||||
|
||||
std::string EigenDeck(const std::string& density = "2700.,",
|
||||
const std::string& frequency = "10, , , , ,") {
|
||||
return R"inp(*Part, name=ShellPart
|
||||
*Node
|
||||
1, 0., 0., 0.
|
||||
2, 1., 0., 0.
|
||||
3, 1., 1., 0.
|
||||
4, 0., 1., 0.
|
||||
*Element, type=S4R
|
||||
1, 1, 2, 3, 4
|
||||
*Elset, elset=ShellSet
|
||||
1
|
||||
*Shell Section, elset=ShellSet, material=Steel
|
||||
0.5, 5
|
||||
*End Part
|
||||
*Assembly, name=Assembly
|
||||
*Instance, name=Shell-1, part=ShellPart
|
||||
*End Instance
|
||||
*Nset, nset=Root, instance=Shell-1
|
||||
1
|
||||
*End Assembly
|
||||
*Material, name=Steel
|
||||
*Density
|
||||
)inp" + density +
|
||||
R"inp(
|
||||
*Elastic
|
||||
2.1e11, 0.3
|
||||
*Boundary
|
||||
Root, 1, 6
|
||||
*Step, name=Step-2, nlgeom=NO, perturbation
|
||||
*Frequency, eigensolver=Lanczos, sim, acoustic coupling=on, normalization=mass
|
||||
)inp" + frequency +
|
||||
R"inp(
|
||||
*Restart, write, frequency=0
|
||||
*Output, field
|
||||
*Node Output, variable=PRESELECT
|
||||
*Output, history
|
||||
*Energy Output, variable=ALL
|
||||
*End Step
|
||||
)inp";
|
||||
}
|
||||
|
||||
const fesa::Diagnostic* FindDiagnostic(const fesa::Status& status,
|
||||
const std::string& code) {
|
||||
for (const auto& diagnostic : status.Diagnostics()) {
|
||||
if (diagnostic.code == code) {
|
||||
return &diagnostic;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
TEST(EigenDomainMapping, PreservesFrequencyDensityAndSourceStepIdentity) {
|
||||
auto result = MapText("accepted", EigenDeck());
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
const fesa::Domain& domain = result.Value();
|
||||
|
||||
ASSERT_EQ(domain.Densities().size(), 1U);
|
||||
EXPECT_EQ(domain.Densities()[0].material_index, fesa::EntityIndex{0U});
|
||||
EXPECT_DOUBLE_EQ(domain.Densities()[0].rho, 2700.0);
|
||||
ASSERT_NE(domain.Frequency(), nullptr);
|
||||
EXPECT_EQ(domain.Frequency()->source_step_name_raw, "Step-2");
|
||||
EXPECT_EQ(domain.Frequency()->source_step_name_canonical, "STEP-2");
|
||||
EXPECT_EQ(domain.Frequency()->requested_mode_count, 10);
|
||||
EXPECT_TRUE(domain.Frequency()->perturbation_explicit);
|
||||
EXPECT_TRUE(domain.Frequency()->sim_explicit);
|
||||
ASSERT_EQ(domain.Steps().Size(), 1U);
|
||||
EXPECT_TRUE(domain.Steps()[0U].IsFrequency());
|
||||
EXPECT_EQ(domain.Steps()[0U].Frequency(), domain.Frequency());
|
||||
EXPECT_EQ(domain.Steps()[0U].Name(), "Step-2");
|
||||
ASSERT_EQ(domain.Steps()[0U].BoundaryConditions().size(), 6U);
|
||||
EXPECT_TRUE(domain.Steps()[0U].Loads().empty());
|
||||
}
|
||||
|
||||
TEST(EigenDomainMapping, RejectsInvalidDensityAndFrequencySlots) {
|
||||
auto density = MapText("bad-density", EigenDeck("2700., 1."));
|
||||
ASSERT_FALSE(density.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(density.GetStatus(), "invalid-density-data"),
|
||||
nullptr);
|
||||
|
||||
auto slots = MapText("bad-slots", EigenDeck("2700.,", "10, 0"));
|
||||
ASSERT_FALSE(slots.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(slots.GetStatus(), "invalid-frequency-data"),
|
||||
nullptr);
|
||||
}
|
||||
|
||||
TEST(EigenDomainMapping, RejectsMissingActiveDensityAndNonzeroBoundary) {
|
||||
std::string missing_deck = EigenDeck();
|
||||
const std::string density_block = "*Density\n2700.,\n";
|
||||
const std::size_t density_location = missing_deck.find(density_block);
|
||||
ASSERT_NE(density_location, std::string::npos);
|
||||
missing_deck.erase(density_location, density_block.size());
|
||||
auto missing = MapText("missing-density", missing_deck);
|
||||
ASSERT_FALSE(missing.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(missing.GetStatus(), "missing-material-density"),
|
||||
nullptr);
|
||||
|
||||
std::string nonzero = EigenDeck();
|
||||
const std::string source = "Root, 1, 6";
|
||||
const std::size_t location = nonzero.rfind(source);
|
||||
ASSERT_NE(location, std::string::npos);
|
||||
nonzero.replace(location, source.size(), "Root, 1, 6, 1.");
|
||||
auto boundary = MapText("nonzero-boundary", nonzero);
|
||||
ASSERT_FALSE(boundary.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(boundary.GetStatus(), "unsupported-eigen-boundary"),
|
||||
nullptr);
|
||||
}
|
||||
|
||||
TEST(EigenDomainMapping, RejectsMixedProceduresInvalidOutputAndSurface) {
|
||||
std::string mixed = EigenDeck();
|
||||
mixed.insert(mixed.find("*Restart"), "*Static\n0.1, 1., 0.01, 1.\n");
|
||||
auto mixed_result = MapText("mixed-procedures", mixed);
|
||||
ASSERT_FALSE(mixed_result.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(mixed_result.GetStatus(),
|
||||
"unsupported-analysis-procedure"),
|
||||
nullptr);
|
||||
|
||||
auto wrong_output = MapText(
|
||||
"wrong-output",
|
||||
EigenDeck().replace(EigenDeck().find("*Output, field\n*Node Output"),
|
||||
std::string{"*Output, field\n*Node Output"}.size(),
|
||||
"*Output, history\n*Node Output"));
|
||||
ASSERT_FALSE(wrong_output.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(wrong_output.GetStatus(), "unsupported-keyword"),
|
||||
nullptr);
|
||||
|
||||
std::string surface = EigenDeck();
|
||||
surface.insert(surface.find("*Material"),
|
||||
"*Surface, name=Face, type=ELEMENT\n");
|
||||
auto surface_result = MapText("surface", surface);
|
||||
ASSERT_FALSE(surface_result.HasValue());
|
||||
}
|
||||
|
||||
TEST(EigenDomainMapping, RejectsInvalidPreprintAndAcceptsStrictNoOp) {
|
||||
const std::string preprint =
|
||||
"*Preprint, echo=NO, model=NO, history=NO, contact=NO\n";
|
||||
auto accepted = MapText("preprint", preprint + EigenDeck());
|
||||
ASSERT_TRUE(accepted.HasValue());
|
||||
const auto warning = std::find_if(accepted.Value().Warnings().begin(),
|
||||
accepted.Value().Warnings().end(),
|
||||
[](const fesa::Diagnostic& diagnostic) {
|
||||
return diagnostic.keyword == "PREPRINT";
|
||||
});
|
||||
ASSERT_NE(warning, accepted.Value().Warnings().end());
|
||||
EXPECT_EQ(warning->message, "mandatory-eigen-output-unchanged");
|
||||
|
||||
auto rejected =
|
||||
MapText("bad-preprint", "*Preprint, echo=YES\n" + EigenDeck());
|
||||
ASSERT_FALSE(rejected.HasValue());
|
||||
EXPECT_NE(FindDiagnostic(rejected.GetStatus(), "unsupported-keyword"),
|
||||
nullptr);
|
||||
|
||||
auto missing_parameters =
|
||||
MapText("bare-preprint", "*Preprint\n" + EigenDeck());
|
||||
ASSERT_FALSE(missing_parameters.HasValue());
|
||||
EXPECT_NE(
|
||||
FindDiagnostic(missing_parameters.GetStatus(), "unsupported-keyword"),
|
||||
nullptr);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,125 @@
|
||||
#include "fesa/io/hdf5/eigen_hdf5_results_writer.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <hdf5.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "fesa/analysis/linear_frequency_procedure.h"
|
||||
#include "fesa/model/domain.h"
|
||||
#include "io/hdf5/eigen_hdf5_self_check.h"
|
||||
|
||||
namespace {
|
||||
|
||||
fesa::ModelDefinition Definition(const std::filesystem::path& source) {
|
||||
fesa::ModelDefinition definition{};
|
||||
definition.source_path = source;
|
||||
definition.source_content_identity = "fnv1a64:0123456789abcdef";
|
||||
definition.nodes = {{{"I", 1, "1"}, {-1.0, -1.0, 0.0}, {source, 1U}},
|
||||
{{"I", 2, "2"}, {1.0, -1.0, 0.0}, {source, 2U}},
|
||||
{{"I", 3, "3"}, {1.0, 1.0, 0.0}, {source, 3U}},
|
||||
{{"I", 4, "4"}, {-1.0, 1.0, 0.0}, {source, 4U}}};
|
||||
definition.materials = {{"Steel", 12.0, 0.0, {source, 5U}}};
|
||||
definition.shell_sections = {{"S", 1.0, 0U, {source, 6U}}};
|
||||
definition.shell_elements = {{{"I", 1, "1"},
|
||||
fesa::ShellSourceElementType::kS4,
|
||||
{0U, 1U, 2U, 3U},
|
||||
0U,
|
||||
0U,
|
||||
{source, 7U}}};
|
||||
for (std::size_t node = 0U; node < 4U; ++node) {
|
||||
definition.shell_node_initial_frames.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.densities = {{0U, 1.0, {source, 8U}}};
|
||||
definition.frequency_step =
|
||||
fesa::FrequencyStepDefinition{"Step-2",
|
||||
"STEP-2",
|
||||
{source, 9U},
|
||||
{source, 10U},
|
||||
{source, 11U},
|
||||
1,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
fesa::FrequencyEigensolver::kLanczos,
|
||||
fesa::FrequencyNormalization::kMass,
|
||||
{}};
|
||||
return definition;
|
||||
}
|
||||
|
||||
TEST(EigenHdf5ResultsWriter, WritesModalSchemaAndRejectsTamperedState) {
|
||||
const auto path =
|
||||
std::filesystem::temp_directory_path() / "fesa-eigen-writer-test.h5";
|
||||
std::error_code ignored;
|
||||
std::filesystem::remove(path, ignored);
|
||||
const auto source = path.parent_path() / "fesa-eigen-writer-test.inp";
|
||||
auto domain_result = fesa::Domain::Create(Definition(source));
|
||||
ASSERT_TRUE(domain_result.HasValue());
|
||||
fesa::Domain domain{std::move(domain_result.Value())};
|
||||
fesa::EigenHdf5ResultsWriter writer;
|
||||
auto execution = fesa::LinearFrequencyProcedureExecutor{}.Execute(domain);
|
||||
ASSERT_TRUE(execution.HasValue());
|
||||
fesa::EigenAnalysisState state = std::move(execution.Value());
|
||||
ASSERT_TRUE(writer.Write(path, domain, state, {}).IsOk());
|
||||
const hid_t file =
|
||||
H5Fopen(path.string().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
||||
ASSERT_GE(file, 0);
|
||||
EXPECT_GT(H5Lexists(file, "/steps/Step-1/eigen/modes", H5P_DEFAULT), 0);
|
||||
EXPECT_GE(H5Fclose(file), 0);
|
||||
std::ifstream original_file{path, std::ios::binary};
|
||||
const std::string original{std::istreambuf_iterator<char>{original_file}, {}};
|
||||
original_file.close();
|
||||
const std::vector<std::function<void(fesa::EigenAnalysisState&)>> mutations{
|
||||
[](auto& candidate) {
|
||||
candidate.vectors(0U, 0U) = std::numeric_limits<double>::quiet_NaN();
|
||||
},
|
||||
[](auto& candidate) { candidate.eigenpair_checks[0].rho_b = 1e-5; },
|
||||
[](auto& candidate) { candidate.eigenpair_checks[0].eigenvalue += 1.0; },
|
||||
[](auto& candidate) { candidate.modes[0].cluster_id = 7U; },
|
||||
[](auto& candidate) { candidate.constraint_mask[0] = 1U; },
|
||||
[](auto& candidate) { candidate.nodal_reduction[0].rcond_inf = 1e-15; },
|
||||
[](auto& candidate) { candidate.eta_gram = 1e-5; }};
|
||||
for (const auto& mutate : mutations) {
|
||||
auto candidate = state;
|
||||
mutate(candidate);
|
||||
EXPECT_FALSE(writer.Write(path, domain, candidate, {}).IsOk());
|
||||
}
|
||||
std::ifstream retained_file{path, std::ios::binary};
|
||||
const std::string retained{std::istreambuf_iterator<char>{retained_file}, {}};
|
||||
EXPECT_TRUE(retained == original);
|
||||
retained_file.close();
|
||||
ASSERT_TRUE(
|
||||
fesa::hdf5_internal::SelfCheckEigenFile(path, domain, state, {}).IsOk());
|
||||
const hid_t corrupt_file =
|
||||
H5Fopen(path.string().c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
|
||||
ASSERT_GE(corrupt_file, 0);
|
||||
const hid_t nodes = H5Dopen2(corrupt_file, "/model/nodes", H5P_DEFAULT);
|
||||
ASSERT_GE(nodes, 0);
|
||||
const hid_t id_type = H5Tcreate(H5T_COMPOUND, sizeof(std::uint64_t));
|
||||
ASSERT_GE(H5Tinsert(id_type, "internal_node_id", 0U, H5T_NATIVE_UINT64), 0);
|
||||
const std::array<std::uint64_t, 4> duplicate_ids{0U, 0U, 2U, 3U};
|
||||
ASSERT_GE(H5Dwrite(nodes, id_type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
||||
duplicate_ids.data()),
|
||||
0);
|
||||
EXPECT_GE(H5Tclose(id_type), 0);
|
||||
EXPECT_GE(H5Dclose(nodes), 0);
|
||||
EXPECT_GE(H5Fclose(corrupt_file), 0);
|
||||
EXPECT_FALSE(
|
||||
fesa::hdf5_internal::SelfCheckEigenFile(path, domain, state, {}).IsOk());
|
||||
std::filesystem::remove(path, ignored);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,251 @@
|
||||
#include "fesa/solvers/eigen/mkl_feast_eigen_solver.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <mkl.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/fem/dof_manager.h"
|
||||
#include "fesa/math/sparse_matrix.h"
|
||||
|
||||
namespace {
|
||||
|
||||
fesa::SparseMatrix MakeDenseCsr(const std::size_t size,
|
||||
const std::vector<double>& values) {
|
||||
EXPECT_EQ(values.size(), size * size);
|
||||
fesa::SparsePattern pattern;
|
||||
std::vector<fesa::CooContribution> contributions;
|
||||
pattern.row_offsets.push_back(0U);
|
||||
for (std::size_t row = 0U; row < size; ++row) {
|
||||
for (std::size_t column = 0U; column < size; ++column) {
|
||||
pattern.column_indices.push_back(column);
|
||||
contributions.push_back(
|
||||
{row, column, values[row * size + column], row, column});
|
||||
}
|
||||
pattern.row_offsets.push_back(pattern.column_indices.size());
|
||||
}
|
||||
auto matrix = fesa::SparseMatrix::FromCoo(size, size,
|
||||
std::move(contributions), pattern);
|
||||
EXPECT_TRUE(matrix.HasValue());
|
||||
return std::move(matrix.Value());
|
||||
}
|
||||
|
||||
void ExpectUnitGram(const fesa::Matrix& vectors) {
|
||||
for (std::size_t left = 0U; left < vectors.Columns(); ++left) {
|
||||
for (std::size_t right = 0U; right < vectors.Columns(); ++right) {
|
||||
double dot = 0.0;
|
||||
for (std::size_t row = 0U; row < vectors.Rows(); ++row) {
|
||||
dot += vectors(row, left) * vectors(row, right);
|
||||
}
|
||||
EXPECT_NEAR(dot, left == right ? 1.0 : 0.0, 1.0e-10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RequireSuccess(const fesa::Result<fesa::FeastEigenResult>& result) {
|
||||
if (result.HasValue()) return;
|
||||
const auto& diagnostics = result.GetStatus().Diagnostics();
|
||||
ASSERT_FALSE(diagnostics.empty());
|
||||
ADD_FAILURE() << diagnostics.front().code << " "
|
||||
<< diagnostics.front().entity_identity << " "
|
||||
<< diagnostics.front().message;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(MklFeastEigenSolver, SolvesN1AndRetainsBackendEvidence) {
|
||||
const auto matrix = MakeDenseCsr(1U, {4.0});
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto result = solver.Solve(matrix, 1U, 1.0);
|
||||
|
||||
RequireSuccess(result);
|
||||
if (!result.HasValue()) return;
|
||||
EXPECT_EQ(result.Value().requested_modes, 1U);
|
||||
ASSERT_EQ(result.Value().eigenvalues.size(), 1U);
|
||||
EXPECT_NEAR(result.Value().eigenvalues[0U], 4.0, 1.0e-10);
|
||||
EXPECT_DOUBLE_EQ(result.Value().operator_scale, 4.0);
|
||||
EXPECT_EQ(result.Value().eigenvectors.Rows(), 1U);
|
||||
EXPECT_EQ(result.Value().eigenvectors.Columns(), 1U);
|
||||
EXPECT_EQ(result.Value().thread_count, 1U);
|
||||
ASSERT_FALSE(result.Value().backend_calls.empty());
|
||||
EXPECT_EQ(result.Value().backend_calls.front().purpose, "total-enclosure");
|
||||
EXPECT_EQ(result.Value().backend_calls.back().kind, "solve");
|
||||
EXPECT_TRUE(result.Value().backend_calls.back().count_available);
|
||||
EXPECT_EQ(result.Value().backend_calls.back().count,
|
||||
result.Value().eigenvalues.size());
|
||||
EXPECT_TRUE(result.Value().backend_calls.back().loop_available);
|
||||
EXPECT_TRUE(result.Value().backend_calls.back().epsout_available);
|
||||
EXPECT_EQ(result.Value().backend_calls.back().outcome, "accepted");
|
||||
EXPECT_EQ(result.Value().backend_calls.back().decision_reason, "accepted");
|
||||
EXPECT_FALSE(result.Value().upper_search_termination.empty());
|
||||
ExpectUnitGram(result.Value().eigenvectors);
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, SolvesAllZeroWithExplicitPhysicalScale) {
|
||||
const auto matrix = MakeDenseCsr(2U, {0.0, 0.0, 0.0, 0.0});
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto result = solver.Solve(matrix, 2U, 7.0);
|
||||
|
||||
RequireSuccess(result);
|
||||
if (!result.HasValue()) return;
|
||||
ASSERT_EQ(result.Value().eigenvalues.size(), 2U);
|
||||
EXPECT_DOUBLE_EQ(result.Value().operator_scale, 7.0);
|
||||
EXPECT_DOUBLE_EQ(result.Value().eigenvalues[0U], 0.0);
|
||||
EXPECT_DOUBLE_EQ(result.Value().eigenvalues[1U], 0.0);
|
||||
ExpectUnitGram(result.Value().eigenvectors);
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, PreservesRepeatedMultiplicityAndStableOrder) {
|
||||
const auto matrix =
|
||||
MakeDenseCsr(3U, {2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 5.0});
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto first = solver.Solve(matrix, 3U, 1.0);
|
||||
const auto second = solver.Solve(matrix, 3U, 1.0);
|
||||
|
||||
RequireSuccess(first);
|
||||
RequireSuccess(second);
|
||||
if (!first.HasValue() || !second.HasValue()) return;
|
||||
ASSERT_EQ(first.Value().eigenvalues.size(), 3U);
|
||||
EXPECT_NEAR(first.Value().eigenvalues[0U], 2.0, 1.0e-10);
|
||||
EXPECT_NEAR(first.Value().eigenvalues[1U], 2.0, 1.0e-10);
|
||||
EXPECT_NEAR(first.Value().eigenvalues[2U], 5.0, 1.0e-10);
|
||||
EXPECT_EQ(first.Value().column_ids, second.Value().column_ids);
|
||||
ExpectUnitGram(first.Value().eigenvectors);
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, UsesGuardedPrefixWithoutDroppingRepeatedCluster) {
|
||||
constexpr std::size_t kSize = 64U;
|
||||
std::vector<double> values(kSize * kSize, 0.0);
|
||||
for (std::size_t index = 0U; index < kSize; ++index) {
|
||||
values[index * kSize + index] = index < 40U ? 1.0 : index - 38U;
|
||||
}
|
||||
const auto matrix = MakeDenseCsr(kSize, values);
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto one_mode = solver.Solve(matrix, 1U, 1.0);
|
||||
const auto forty_first = solver.Solve(matrix, 41U, 1.0);
|
||||
|
||||
RequireSuccess(one_mode);
|
||||
RequireSuccess(forty_first);
|
||||
if (!one_mode.HasValue() || !forty_first.HasValue()) return;
|
||||
EXPECT_GE(one_mode.Value().eigenvalues.size(), 40U);
|
||||
EXPECT_GE(forty_first.Value().eigenvalues.size(), 41U);
|
||||
for (std::size_t index = 0U; index < 40U; ++index) {
|
||||
EXPECT_NEAR(one_mode.Value().eigenvalues[index], 1.0, 1.0e-10);
|
||||
}
|
||||
EXPECT_NEAR(forty_first.Value().eigenvalues[40U], 2.0, 1.0e-10);
|
||||
EXPECT_EQ(one_mode.Value().backend_calls.back().kind, "solve");
|
||||
EXPECT_EQ(forty_first.Value().backend_calls.back().kind, "solve");
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, RejectsSignificantNegativeSpectrum) {
|
||||
const auto matrix = MakeDenseCsr(1U, {-2.0});
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto result = solver.Solve(matrix, 1U, 1.0);
|
||||
|
||||
ASSERT_FALSE(result.HasValue());
|
||||
EXPECT_EQ(result.GetStatus().Category(), fesa::FailureCategory::kSolver);
|
||||
ASSERT_EQ(result.GetStatus().Diagnostics().size(), 1U);
|
||||
EXPECT_EQ(result.GetStatus().Diagnostics()[0U].code,
|
||||
"negative-eigenvalue-spectrum");
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, KeepsCloseDistinctModesSeparated) {
|
||||
const auto matrix = MakeDenseCsr(2U, {1.0, 0.0, 0.0, 1.001});
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto result = solver.Solve(matrix, 2U, 1.0);
|
||||
|
||||
RequireSuccess(result);
|
||||
if (!result.HasValue()) return;
|
||||
ASSERT_EQ(result.Value().eigenvalues.size(), 2U);
|
||||
EXPECT_NEAR(result.Value().eigenvalues[0U], 1.0, 1.0e-10);
|
||||
EXPECT_NEAR(result.Value().eigenvalues[1U], 1.001, 1.0e-10);
|
||||
EXPECT_NEAR(std::abs(result.Value().eigenvectors(0U, 0U)), 1.0, 1.0e-10);
|
||||
EXPECT_NEAR(std::abs(result.Value().eigenvectors(1U, 1U)), 1.0, 1.0e-10);
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, RestoresCallingThreadLocalSetting) {
|
||||
const auto matrix = MakeDenseCsr(1U, {4.0});
|
||||
const int prior = mkl_set_num_threads_local(2);
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto result = solver.Solve(matrix, 1U, 1.0);
|
||||
const int restored = mkl_set_num_threads_local(2);
|
||||
static_cast<void>(mkl_set_num_threads_local(prior));
|
||||
|
||||
RequireSuccess(result);
|
||||
if (!result.HasValue()) return;
|
||||
EXPECT_EQ(restored, 2);
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, RejectsInvalidRequestAndZeroScale) {
|
||||
const auto matrix = MakeDenseCsr(1U, {0.0});
|
||||
fesa::MklFeastEigenSolver solver;
|
||||
|
||||
const auto zero_request = solver.Solve(matrix, 0U, 1.0);
|
||||
ASSERT_FALSE(zero_request.HasValue());
|
||||
EXPECT_EQ(zero_request.GetStatus().Diagnostics()[0U].code,
|
||||
"feast-invalid-dimension");
|
||||
const auto invalid_scale = solver.Solve(matrix, 1U, 0.0);
|
||||
ASSERT_FALSE(invalid_scale.HasValue());
|
||||
EXPECT_EQ(invalid_scale.GetStatus().Diagnostics()[0U].code,
|
||||
"feast-invalid-zero-scale");
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, PreservesLowestPrefixForZeroAndPositiveSpectra) {
|
||||
const std::vector<std::vector<double>> spectra{{2.0},
|
||||
{0.0, 0.0, 0.0, 2.0, 5.0, 9.0},
|
||||
{1.0, 2.0, 4.0, 8.0},
|
||||
{0.0, 0.0, 0.0, 0.0}};
|
||||
const std::vector<std::vector<std::size_t>> requests{
|
||||
{1U}, {1U, 4U, 6U}, {1U, 2U, 4U}, {1U, 4U}};
|
||||
const fesa::MklFeastEigenSolver solver;
|
||||
for (std::size_t case_index = 0U; case_index < spectra.size(); ++case_index) {
|
||||
const auto& spectrum = spectra[case_index];
|
||||
const auto n = spectrum.size();
|
||||
std::vector<double> values(n * n, 0.0);
|
||||
for (std::size_t i = 0U; i < n; ++i) values[i * n + i] = spectrum[i];
|
||||
const auto matrix = MakeDenseCsr(n, values);
|
||||
for (const auto requested : requests[case_index]) {
|
||||
const auto result = solver.Solve(matrix, requested, 1.0);
|
||||
RequireSuccess(result);
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
ASSERT_GE(result.Value().eigenvalues.size(), requested);
|
||||
for (std::size_t i = 0U; i < requested; ++i)
|
||||
EXPECT_NEAR(result.Value().eigenvalues[i], spectrum[i], 1e-10);
|
||||
ExpectUnitGram(result.Value().eigenvectors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MklFeastEigenSolver, ResolvesCloseDistinctSpectrumAfterGivensCongruence) {
|
||||
const std::array<double, 4> spectrum{1.0, 1.0 + std::ldexp(1.0, -28),
|
||||
1.0 + std::ldexp(1.0, -27), 8.0};
|
||||
const std::array<std::array<double, 4>, 4> q{{{{0.6, 0.0, 0.0, -0.8}},
|
||||
{{0.0, 0.6, -0.8, 0.0}},
|
||||
{{0.0, 0.8, 0.6, 0.0}},
|
||||
{{0.8, 0.0, 0.0, 0.6}}}};
|
||||
std::vector<double> values(16U, 0.0);
|
||||
for (std::size_t i = 0U; i < 4U; ++i)
|
||||
for (std::size_t j = 0U; j < 4U; ++j)
|
||||
for (std::size_t k = 0U; k < 4U; ++k)
|
||||
values[4U * i + j] += q[i][k] * spectrum[k] * q[j][k];
|
||||
const auto result =
|
||||
fesa::MklFeastEigenSolver{}.Solve(MakeDenseCsr(4U, values), 2U, 1.0);
|
||||
RequireSuccess(result);
|
||||
ASSERT_TRUE(result.HasValue());
|
||||
ASSERT_EQ(result.Value().eigenvalues.size(), 4U);
|
||||
for (std::size_t i = 0U; i < 4U; ++i)
|
||||
EXPECT_NEAR(result.Value().eigenvalues[i], spectrum[i], 1e-12);
|
||||
EXPECT_GT(result.Value().eigenvalues[1U] - result.Value().eigenvalues[0U],
|
||||
1e-9);
|
||||
ExpectUnitGram(result.Value().eigenvectors);
|
||||
}
|
||||
Reference in New Issue
Block a user