640 lines
28 KiB
C++
640 lines
28 KiB
C++
// VDE-1241 回归测试:FEA 命名选择集(B-Rep face/edge → 网格节点/单元 + NSET/ELSET 导出)
|
||
// owner=cae-geometry
|
||
#include <gtest/gtest.h>
|
||
|
||
#include "vde/brep/modeling.h"
|
||
#include "vde/cae/fea_mesh.h"
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <cstdio>
|
||
#include <fstream>
|
||
#include <iterator>
|
||
#include <set>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
using namespace vde::mesh;
|
||
using namespace vde::brep;
|
||
using namespace vde::core;
|
||
|
||
namespace {
|
||
|
||
std::string read_text_file(const std::string& path) {
|
||
std::ifstream f(path);
|
||
if (!f.good()) return {};
|
||
return std::string((std::istreambuf_iterator<char>(f)),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
int count_substring(const std::string& hay, const std::string& needle) {
|
||
int count = 0;
|
||
size_t pos = 0;
|
||
while ((pos = hay.find(needle, pos)) != std::string::npos) {
|
||
++count;
|
||
pos += needle.size();
|
||
}
|
||
return count;
|
||
}
|
||
|
||
double point_segment_distance(const Point3D& p, const Point3D& a, const Point3D& b) {
|
||
const Vector3D ab = b - a;
|
||
const double len2 = ab.dot(ab);
|
||
if (!(len2 > 0.0)) return (p - a).norm();
|
||
double t = (p - a).dot(ab) / len2;
|
||
t = std::min(1.0, std::max(0.0, t));
|
||
return (p - (a + ab * t)).norm();
|
||
}
|
||
|
||
/** 粗四面体 box 网格(测试用:快、面标签稳定) */
|
||
FEAMesh make_box_mesh(const BrepModel& box) {
|
||
TetMeshParams params;
|
||
params.max_size = 0.2;
|
||
params.min_size = 0.05;
|
||
params.quality_iterations = 0;
|
||
return tetrahedral_mesh(box, params);
|
||
}
|
||
|
||
/** 轴对齐 box 表面距离(box(w,h,d) 以原点为中心) */
|
||
double distance_to_box_surface(const Point3D& p, double hx, double hy, double hz) {
|
||
double d = std::abs(std::abs(p.x()) - hx);
|
||
d = std::min(d, std::abs(std::abs(p.y()) - hy));
|
||
d = std::min(d, std::abs(std::abs(p.z()) - hz));
|
||
return d;
|
||
}
|
||
|
||
/** 网格体积(Tet4:|det|/6 求和) */
|
||
double mesh_volume(const FEAMesh& mesh) {
|
||
double volume = 0.0;
|
||
for (const auto& e : mesh.elements) {
|
||
if (e.size() < 4) continue;
|
||
const Point3D& a = mesh.vertices[static_cast<size_t>(e[0])];
|
||
const Point3D& b = mesh.vertices[static_cast<size_t>(e[1])];
|
||
const Point3D& c = mesh.vertices[static_cast<size_t>(e[2])];
|
||
const Point3D& d = mesh.vertices[static_cast<size_t>(e[3])];
|
||
volume += std::abs((b - a).cross(c - a).dot(d - a)) / 6.0;
|
||
}
|
||
return volume;
|
||
}
|
||
|
||
std::set<int> boundary_node_set(const FEAMesh& mesh) {
|
||
std::set<int> nodes;
|
||
for (const auto& bf : mesh.boundary_faces)
|
||
for (int vi : bf) nodes.insert(vi);
|
||
return nodes;
|
||
}
|
||
|
||
std::set<int> nodes_with_tag(const FEAMesh& mesh, int tag) {
|
||
std::set<int> nodes;
|
||
for (size_t fi = 0; fi < mesh.boundary_faces.size(); ++fi) {
|
||
if (fi >= mesh.boundary_face_tags.size()) break;
|
||
if (mesh.boundary_face_tags[fi] != tag) continue;
|
||
for (int vi : mesh.boundary_faces[fi]) nodes.insert(vi);
|
||
}
|
||
return nodes;
|
||
}
|
||
|
||
std::set<int> to_set(const std::vector<int>& v) { return std::set<int>(v.begin(), v.end()); }
|
||
|
||
/** 手工构造的单位四面体网格(用于导出/UNASSIGNED 单元测试) */
|
||
FEAMesh make_unit_tet_mesh() {
|
||
FEAMesh mesh;
|
||
mesh.element_type = FEAElementType::Tet4;
|
||
mesh.vertices = {Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0, 1, 0),
|
||
Point3D(0, 0, 1)};
|
||
mesh.elements = {{0, 1, 2, 3}};
|
||
mesh.material_ids = {0};
|
||
mesh.boundary_faces = {{0, 2, 1}, {0, 1, 3}, {0, 3, 2}, {1, 2, 3}};
|
||
mesh.boundary_face_tags = {-1, 0, 0, -1};
|
||
mesh.boundary_edges.clear();
|
||
mesh.boundary_edge_tags.clear();
|
||
return mesh;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1. tag 语义 / 长度一致性 / 确定性
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(VDE1241, TetrahedralMeshTagsLengthsAndRange) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
ASSERT_GT(mesh.num_boundary_faces(), 0u);
|
||
ASSERT_EQ(mesh.boundary_face_tags.size(), mesh.boundary_faces.size());
|
||
ASSERT_EQ(mesh.boundary_edges.size(), mesh.boundary_edge_tags.size());
|
||
|
||
// tag 必须是 B-Rep face 索引或 -1
|
||
for (int tag : mesh.boundary_face_tags) {
|
||
EXPECT_GE(tag, -1);
|
||
EXPECT_LT(tag, static_cast<int>(box.num_faces()));
|
||
}
|
||
for (int tag : mesh.boundary_edge_tags) {
|
||
EXPECT_GE(tag, -1);
|
||
EXPECT_LT(tag, static_cast<int>(box.num_edges()));
|
||
}
|
||
// 边界边 = 边界面多边形的边(三角形 → 3N)
|
||
size_t expected_edges = 0;
|
||
for (const auto& bf : mesh.boundary_faces) {
|
||
expected_edges += bf.size();
|
||
EXPECT_GE(bf.size(), 3u);
|
||
}
|
||
EXPECT_EQ(mesh.boundary_edges.size(), expected_edges);
|
||
}
|
||
|
||
TEST(VDE1241, TetrahedralMeshTagsDeterministic) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh a = make_box_mesh(box);
|
||
const FEAMesh b = make_box_mesh(box);
|
||
ASSERT_EQ(a.boundary_face_tags.size(), b.boundary_face_tags.size());
|
||
EXPECT_EQ(a.boundary_face_tags, b.boundary_face_tags);
|
||
EXPECT_EQ(a.boundary_edge_tags, b.boundary_edge_tags);
|
||
}
|
||
|
||
TEST(VDE1241, BoxAllSixFacesTagged) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
std::set<int> distinct;
|
||
for (int tag : mesh.boundary_face_tags) {
|
||
if (tag >= 0) distinct.insert(tag);
|
||
}
|
||
EXPECT_EQ(distinct.size(), 6u) << "box 的 6 个 B-Rep 面都应得到边界面标签";
|
||
for (int f = 0; f < 6; ++f) EXPECT_TRUE(distinct.count(f)) << "face " << f;
|
||
// 未归属应尽量少(box 为凸体,理论上不需要 -1)
|
||
const int unassigned = static_cast<int>(std::count(
|
||
mesh.boundary_face_tags.begin(), mesh.boundary_face_tags.end(), -1));
|
||
EXPECT_LE(unassigned, static_cast<int>(mesh.num_boundary_faces()) / 20);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1b. verifier 复算回归:薄板 box 的伪边界面(内部采样点混入)
|
||
// box(60,60,20) max_size=15/5 曾出现 boundary_faces 引用内部采样点
|
||
// (9/29、39/59),导致选择集并集 ≠ 全部边界节点。
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(VDE1241, ThinBoxBoundaryFacesAllOnBrepSurface) {
|
||
// 第二轮(R2):轴对齐 box 走结构化四面体快速路径后,边界面严格落在
|
||
// B-Rep 6 个平面上、6 面 partition 非空且并集 = 全部边界节点、体积守恒。
|
||
const BrepModel box = make_box(60.0, 60.0, 20.0);
|
||
const double hx = 30.0, hy = 30.0, hz = 10.0;
|
||
const double diag = std::sqrt(4.0 * (hx * hx + hy * hy + hz * hz));
|
||
const double tol = std::max(diag * 1e-6, 1e-12);
|
||
const double expected_volume = 60.0 * 60.0 * 20.0;
|
||
|
||
auto cells = [](double extent, double max_size) {
|
||
return std::max(1, static_cast<int>(
|
||
std::ceil(extent / max_size - 1e-9)));
|
||
};
|
||
|
||
for (double max_size : {15.0, 5.0}) {
|
||
TetMeshParams params;
|
||
params.max_size = max_size;
|
||
params.quality_iterations = 0;
|
||
const FEAMesh mesh = tetrahedral_mesh(box, params);
|
||
const int nx = cells(60.0, max_size);
|
||
const int ny = cells(60.0, max_size);
|
||
const int nz = cells(20.0, max_size);
|
||
|
||
// 结构化网格规模严格符合 nx×ny×nz(每六面体 6 个 Tet4)
|
||
EXPECT_EQ(mesh.num_vertices(),
|
||
static_cast<size_t>(nx + 1) * (ny + 1) * (nz + 1))
|
||
<< "max_size=" << max_size;
|
||
EXPECT_EQ(mesh.num_elements(),
|
||
static_cast<size_t>(nx) * ny * nz * 6)
|
||
<< "max_size=" << max_size;
|
||
EXPECT_EQ(mesh.num_boundary_faces(),
|
||
static_cast<size_t>(4 * (nx * ny + ny * nz + nz * nx)))
|
||
<< "max_size=" << max_size;
|
||
ASSERT_EQ(mesh.boundary_face_tags.size(), mesh.boundary_faces.size())
|
||
<< "max_size=" << max_size;
|
||
ASSERT_EQ(mesh.boundary_edges.size(), mesh.boundary_edge_tags.size())
|
||
<< "max_size=" << max_size;
|
||
EXPECT_TRUE(mesh.diagnostic.empty())
|
||
<< "结构化路径不应产生剔除诊断: " << mesh.diagnostic;
|
||
|
||
// 6 个 B-Rep 面都有边界面标签,且没有未归属(-1)
|
||
std::set<int> distinct;
|
||
for (int tag : mesh.boundary_face_tags) {
|
||
EXPECT_GE(tag, 0) << "max_size=" << max_size;
|
||
distinct.insert(tag);
|
||
}
|
||
EXPECT_EQ(distinct.size(), 6u) << "max_size=" << max_size;
|
||
|
||
// 所有边界面顶点都落在 B-Rep 表面容差内(无内部节点混入)
|
||
int off_surface = 0;
|
||
for (const auto& bf : mesh.boundary_faces) {
|
||
for (int vi : bf) {
|
||
const double d = distance_to_box_surface(
|
||
mesh.vertices[static_cast<size_t>(vi)], hx, hy, hz);
|
||
if (d > tol) ++off_surface;
|
||
}
|
||
}
|
||
EXPECT_EQ(off_surface, 0)
|
||
<< "max_size=" << max_size << ":boundary_faces 含不在 B-Rep 表面上的顶点";
|
||
|
||
// 边界面三角片本身也必须贴在表面上(质心在面上)
|
||
int off_plane = 0;
|
||
for (const auto& bf : mesh.boundary_faces) {
|
||
if (bf.size() != 3) continue;
|
||
const Point3D c = (mesh.vertices[static_cast<size_t>(bf[0])] +
|
||
mesh.vertices[static_cast<size_t>(bf[1])] +
|
||
mesh.vertices[static_cast<size_t>(bf[2])]) / 3.0;
|
||
if (distance_to_box_surface(c, hx, hy, hz) > tol * 10) ++off_plane;
|
||
}
|
||
EXPECT_EQ(off_plane, 0)
|
||
<< "max_size=" << max_size << ":边界面三角片穿过实体内部";
|
||
|
||
// 体积守恒:Σ|det|/6 ≈ box 体积(rel ≤ 1e-6)
|
||
const double volume = mesh_volume(mesh);
|
||
EXPECT_LE(std::abs(volume - expected_volume) / expected_volume, 1e-6)
|
||
<< "max_size=" << max_size << " volume=" << volume;
|
||
|
||
// 边界边 / 边标签:box 棱上的边必须有 B-Rep edge 标签,面内剖分边为 -1
|
||
int tagged_edges = 0;
|
||
int untagged_edges = 0;
|
||
for (int tag : mesh.boundary_edge_tags) {
|
||
if (tag >= 0) ++tagged_edges;
|
||
else ++untagged_edges;
|
||
}
|
||
EXPECT_GT(tagged_edges, 0) << "max_size=" << max_size;
|
||
EXPECT_GT(untagged_edges, 0) << "max_size=" << max_size;
|
||
|
||
// partition:6 面均非空、互斥、并集 = 全部边界节点
|
||
std::vector<std::string> names;
|
||
for (int f = 0; f < 6; ++f) names.push_back("FACE_" + std::to_string(f));
|
||
std::string diag;
|
||
const auto parts = make_face_partition(mesh, box, {0, 1, 2, 3, 4, 5}, names,
|
||
0.0, &diag);
|
||
ASSERT_EQ(parts.size(), 6u);
|
||
std::set<int> uni;
|
||
for (const auto& s : parts) {
|
||
EXPECT_FALSE(s.node_ids.empty())
|
||
<< "max_size=" << max_size << ":partition 有空集合;diag=" << diag;
|
||
uni.insert(s.node_ids.begin(), s.node_ids.end());
|
||
}
|
||
EXPECT_EQ(uni, boundary_node_set(mesh))
|
||
<< "max_size=" << max_size << ":partition 并集 != 全部边界节点;diag="
|
||
<< diag;
|
||
for (size_t i = 0; i < parts.size(); ++i)
|
||
for (size_t j = i + 1; j < parts.size(); ++j) {
|
||
std::vector<int> inter;
|
||
std::set_intersection(parts[i].node_ids.begin(),
|
||
parts[i].node_ids.end(),
|
||
parts[j].node_ids.begin(),
|
||
parts[j].node_ids.end(),
|
||
std::back_inserter(inter));
|
||
EXPECT_TRUE(inter.empty())
|
||
<< "max_size=" << max_size << ":partition " << i << "/" << j
|
||
<< " 相交";
|
||
}
|
||
|
||
// make_face_selection:6 面均非空且并集覆盖全部边界节点
|
||
std::set<int> sel_uni;
|
||
for (int f = 0; f < 6; ++f) {
|
||
const NamedSet s = make_face_selection(mesh, box, std::vector<int>{f}, "F");
|
||
EXPECT_FALSE(s.node_ids.empty())
|
||
<< "max_size=" << max_size << ":face " << f << " 选择为空";
|
||
sel_uni.insert(s.node_ids.begin(), s.node_ids.end());
|
||
}
|
||
EXPECT_EQ(sel_uni, boundary_node_set(mesh))
|
||
<< "max_size=" << max_size << ":face selection 并集 != 全部边界节点";
|
||
}
|
||
}
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 2. make_face_selection 语义
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(VDE1241, FaceSelectionPerFaceNonEmptyAndUnionCoversAll) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
std::vector<NamedSet> sets;
|
||
for (int f = 0; f < 6; ++f) {
|
||
NamedSet set = make_face_selection(mesh, box, std::vector<int>{f}, "FACE_" + std::to_string(f));
|
||
EXPECT_FALSE(set.node_ids.empty()) << "face " << f;
|
||
ASSERT_EQ(set.face_ids.size(), 1u);
|
||
EXPECT_EQ(set.face_ids[0], f);
|
||
// 节点升序去重
|
||
EXPECT_TRUE(std::is_sorted(set.node_ids.begin(), set.node_ids.end()));
|
||
EXPECT_EQ(std::adjacent_find(set.node_ids.begin(), set.node_ids.end()),
|
||
set.node_ids.end());
|
||
sets.push_back(std::move(set));
|
||
}
|
||
|
||
std::set<int> uni;
|
||
for (const auto& s : sets) uni.insert(s.node_ids.begin(), s.node_ids.end());
|
||
EXPECT_EQ(uni, boundary_node_set(mesh)) << "并集应等于全部边界节点";
|
||
}
|
||
|
||
// 跨面共享节点(B-Rep 边/角)在有限元语义下必须同时属于相邻两个面集合,
|
||
// 否则面边界条件会漏节点;因此强"两两不相交"不可实现。
|
||
// 这里验证严格口径:不相邻面(box 对面)交集必须为空,相邻面交集只允许出现在
|
||
// 共享 B-Rep 边上。互斥分区口径由 make_face_partition 提供(见下个用例)。
|
||
TEST(VDE1241, FaceSelectionOverlapOnlyOnSharedBrepEdges) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
std::vector<std::set<int>> nodes(6);
|
||
std::vector<Point3D> centers(6);
|
||
std::vector<Vector3D> normals(6);
|
||
for (int f = 0; f < 6; ++f) {
|
||
nodes[static_cast<size_t>(f)] =
|
||
to_set(make_face_selection(mesh, box, std::vector<int>{f}, "F").node_ids);
|
||
centers[static_cast<size_t>(f)] = box.face_center(f);
|
||
normals[static_cast<size_t>(f)] = box.face_normal(f);
|
||
}
|
||
const std::set<std::pair<int, int>> opposite = {{0, 1}, {2, 3}, {4, 5}};
|
||
bool any_shared = false;
|
||
for (int i = 0; i < 6; ++i) {
|
||
for (int j = i + 1; j < 6; ++j) {
|
||
std::vector<int> inter;
|
||
std::set_intersection(nodes[static_cast<size_t>(i)].begin(),
|
||
nodes[static_cast<size_t>(i)].end(),
|
||
nodes[static_cast<size_t>(j)].begin(),
|
||
nodes[static_cast<size_t>(j)].end(),
|
||
std::back_inserter(inter));
|
||
if (opposite.count({i, j})) {
|
||
EXPECT_TRUE(inter.empty())
|
||
<< "box 对面 " << i << "/" << j << " 不应共享节点";
|
||
continue;
|
||
}
|
||
// 相邻面:交集节点必须落在两平面交线(即共享 B-Rep 边)上
|
||
Vector3D n1 = normals[static_cast<size_t>(i)];
|
||
Vector3D n2 = normals[static_cast<size_t>(j)];
|
||
ASSERT_GT(n1.norm(), 1e-9);
|
||
ASSERT_GT(n2.norm(), 1e-9);
|
||
n1 = n1 / n1.norm();
|
||
n2 = n2 / n2.norm();
|
||
const Vector3D dir = n1.cross(n2);
|
||
ASSERT_GT(dir.norm(), 1e-6);
|
||
// 两平面交线上一点:x0 = (d1·(n2×dir) + d2·(dir×n1)) / |dir|²
|
||
const double d1 = n1.dot(centers[static_cast<size_t>(i)]);
|
||
const double d2 = n2.dot(centers[static_cast<size_t>(j)]);
|
||
const double dir2 = dir.dot(dir);
|
||
const Point3D p0 = (n2.cross(dir) * d1 + dir.cross(n1) * d2) / dir2;
|
||
for (int vi : inter) {
|
||
any_shared = true;
|
||
const Point3D p = mesh.vertices[static_cast<size_t>(vi)];
|
||
// 点到交线距离
|
||
const Vector3D w = p - p0;
|
||
const double d_line = w.cross(dir).norm() / dir.norm();
|
||
EXPECT_LT(d_line, 1e-6)
|
||
<< "相邻面共享节点应位于共享 B-Rep 边上 i=" << i << " j=" << j
|
||
<< " p=(" << p.x() << "," << p.y() << "," << p.z() << ")";
|
||
}
|
||
}
|
||
}
|
||
EXPECT_TRUE(any_shared) << "相邻面之间应存在共享边界节点(有限元语义)";
|
||
}
|
||
|
||
TEST(VDE1241, FacePartitionIsDisjointAndCoversAllBoundaryNodes) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
std::vector<std::string> names;
|
||
for (int f = 0; f < 6; ++f) names.push_back("FACE_" + std::to_string(f));
|
||
std::string diag;
|
||
const auto sets = make_face_partition(mesh, box, {0, 1, 2, 3, 4, 5}, names, 0.0, &diag);
|
||
ASSERT_EQ(sets.size(), 6u);
|
||
|
||
std::set<int> uni;
|
||
for (const auto& s : sets) {
|
||
EXPECT_FALSE(s.node_ids.empty());
|
||
EXPECT_TRUE(std::is_sorted(s.node_ids.begin(), s.node_ids.end()));
|
||
uni.insert(s.node_ids.begin(), s.node_ids.end());
|
||
}
|
||
for (size_t i = 0; i < sets.size(); ++i) {
|
||
for (size_t j = i + 1; j < sets.size(); ++j) {
|
||
std::vector<int> inter;
|
||
std::set_intersection(sets[i].node_ids.begin(), sets[i].node_ids.end(),
|
||
sets[j].node_ids.begin(), sets[j].node_ids.end(),
|
||
std::back_inserter(inter));
|
||
EXPECT_TRUE(inter.empty()) << "partition 集合 " << i << "/" << j << " 相交";
|
||
}
|
||
}
|
||
EXPECT_EQ(uni, boundary_node_set(mesh))
|
||
<< "6 面分区并集应等于全部边界节点;diag=" << diag;
|
||
}
|
||
|
||
TEST(VDE1241, FaceSelectionEmptyAndInvalidInputs) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
std::string diag;
|
||
// 空选择 → 空集合,不报错
|
||
const NamedSet empty = make_face_selection(mesh, box, std::vector<int>{}, "EMPTY", 0.0, &diag);
|
||
EXPECT_TRUE(empty.node_ids.empty());
|
||
EXPECT_TRUE(empty.element_ids.empty());
|
||
EXPECT_TRUE(empty.face_ids.empty());
|
||
|
||
// 非法 id 被忽略并给出诊断
|
||
const NamedSet invalid = make_face_selection(mesh, box, std::vector<int>{77}, "BAD", 0.0, &diag);
|
||
EXPECT_TRUE(invalid.node_ids.empty());
|
||
EXPECT_FALSE(diag.empty());
|
||
|
||
// 部分合法:非法项忽略,合法项正常
|
||
const NamedSet partial = make_face_selection(mesh, box, {0, 77}, "PART", 0.0, &diag);
|
||
EXPECT_FALSE(partial.node_ids.empty());
|
||
EXPECT_EQ(partial.face_ids, std::vector<int>({0}));
|
||
|
||
// 空 name 合法
|
||
const NamedSet unnamed = make_face_selection(mesh, box, std::vector<int>{0}, "");
|
||
EXPECT_FALSE(unnamed.node_ids.empty());
|
||
}
|
||
|
||
TEST(VDE1241, FaceSelectionDuplicateFaceIdsDedup) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
const NamedSet one = make_face_selection(mesh, box, std::vector<int>{3}, "A");
|
||
const NamedSet dup = make_face_selection(mesh, box, {3, 3, 3}, "A");
|
||
EXPECT_EQ(one.face_ids, dup.face_ids);
|
||
EXPECT_EQ(one.node_ids, dup.node_ids);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 3. 圆柱:侧面选择不得混入顶/底盖节点
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(VDE1241, CylinderSideSelectionExcludesCapNodes) {
|
||
const BrepModel cyl = make_cylinder(0.5, 1.0, 32);
|
||
ASSERT_GE(static_cast<int>(cyl.num_faces()), 34);
|
||
TetMeshParams params;
|
||
params.max_size = 0.2;
|
||
params.min_size = 0.05;
|
||
params.quality_iterations = 0;
|
||
const FEAMesh mesh = tetrahedral_mesh(cyl, params);
|
||
ASSERT_GT(mesh.num_boundary_faces(), 0u);
|
||
|
||
// make_cylinder 面序:0=底盖, 1=顶盖, 2..33=侧面
|
||
std::vector<int> side_faces;
|
||
for (int f = 2; f < static_cast<int>(cyl.num_faces()); ++f) side_faces.push_back(f);
|
||
const NamedSet side =
|
||
make_face_selection(mesh, cyl, side_faces, "CYL_SIDE");
|
||
EXPECT_FALSE(side.node_ids.empty());
|
||
|
||
// 底面/顶盖节点(按 tag 归属)
|
||
const std::set<int> caps = nodes_with_tag(mesh, 0);
|
||
std::set<int> cap_nodes = caps;
|
||
for (int vi : nodes_with_tag(mesh, 1)) cap_nodes.insert(vi);
|
||
|
||
// 允许出现在侧面集合中的 cap 节点只能是圆周棱边节点(r≈R 且位于端面)
|
||
int misclassified = 0;
|
||
for (int vi : side.node_ids) {
|
||
const Point3D p = mesh.vertices[static_cast<size_t>(vi)];
|
||
const double r = std::sqrt(p.x() * p.x() + p.y() * p.y());
|
||
const bool on_rim = std::abs(std::abs(p.z()) - 0.5) < 1e-6 &&
|
||
std::abs(r - 0.5) < 1e-6;
|
||
if (on_rim) continue;
|
||
// 不在圆周棱边上的节点若来自端面(|z|≈0.5 且 r 明显小于 R)→ 误入
|
||
if (std::abs(std::abs(p.z()) - 0.5) < 1e-6 && r < 0.499) ++misclassified;
|
||
(void)cap_nodes;
|
||
}
|
||
EXPECT_EQ(misclassified, 0) << "侧面选择不得混入顶/底盖内部节点";
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 4. 边标签 + 边选择
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(VDE1241, EdgeTagsAndEdgeSelection) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
// 面内部剖分边应大量 -1;真实 B-Rep 边应有命中
|
||
int tagged = 0;
|
||
int unassigned = 0;
|
||
int sample_eid = -1;
|
||
for (size_t i = 0; i < mesh.boundary_edge_tags.size(); ++i) {
|
||
if (mesh.boundary_edge_tags[i] >= 0) {
|
||
++tagged;
|
||
if (sample_eid < 0) sample_eid = mesh.boundary_edge_tags[i];
|
||
} else {
|
||
++unassigned;
|
||
}
|
||
}
|
||
EXPECT_GT(tagged, 0);
|
||
EXPECT_GT(unassigned, 0);
|
||
ASSERT_GE(sample_eid, 0);
|
||
|
||
const NamedSet sel = make_edge_selection(mesh, box, {sample_eid}, "EDGE");
|
||
ASSERT_FALSE(sel.node_ids.empty());
|
||
// 选中节点必须落在该 B-Rep 边(box 直边)上
|
||
const auto& edge = box.edge(sample_eid);
|
||
const Point3D a = box.vertex(edge.v_start).point;
|
||
const Point3D b = box.vertex(edge.v_end).point;
|
||
for (int vi : sel.node_ids) {
|
||
const double d = point_segment_distance(
|
||
mesh.vertices[static_cast<size_t>(vi)], a, b);
|
||
EXPECT_LT(d, 1e-6);
|
||
}
|
||
// 空选择
|
||
EXPECT_TRUE(make_edge_selection(mesh, box, {}, "E").node_ids.empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 5. 导出:NSET/ELSET/SURFACE + 材料参数
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(VDE1241Export, AbaqusWritesSixNsetAndSixSurface) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
|
||
std::vector<NamedSet> sets;
|
||
for (int f = 0; f < 6; ++f)
|
||
sets.push_back(make_face_selection(mesh, box, std::vector<int>{f}, "FACE_" + std::to_string(f)));
|
||
|
||
const std::string path = VDE_TEST_TEMP_DIR "vde1241_abaqus.inp";
|
||
FEAMaterialParams material;
|
||
material.name = "STEEL";
|
||
material.youngs_modulus = 70000.0;
|
||
material.poisson_ratio = 0.33;
|
||
std::string error;
|
||
ASSERT_TRUE(export_abaqus(mesh, path, sets, material, &error));
|
||
const std::string content = read_text_file(path);
|
||
ASSERT_FALSE(content.empty());
|
||
std::remove(path.c_str());
|
||
|
||
EXPECT_EQ(count_substring(content, "*NSET, NSET=FACE_"), 6);
|
||
EXPECT_EQ(count_substring(content, "*SURFACE, NAME=FACE_"), 6);
|
||
EXPECT_NE(content.find("*MATERIAL, NAME=STEEL"), std::string::npos);
|
||
EXPECT_NE(content.find("70000"), std::string::npos);
|
||
EXPECT_NE(content.find("0.33"), std::string::npos);
|
||
EXPECT_EQ(content.find("210000"), std::string::npos)
|
||
<< "材料参数应由应用层传入,不得硬编码默认值";
|
||
}
|
||
|
||
TEST(VDE1241Export, AbaqusUnassignedTagBecomesUnassignedGroup) {
|
||
const FEAMesh mesh = make_unit_tet_mesh();
|
||
const std::string path = VDE_TEST_TEMP_DIR "vde1241_unassigned.inp";
|
||
ASSERT_TRUE(export_abaqus(mesh, path));
|
||
const std::string content = read_text_file(path);
|
||
std::remove(path.c_str());
|
||
|
||
EXPECT_NE(content.find("*NSET, NSET=UNASSIGNED"), std::string::npos);
|
||
EXPECT_NE(content.find("*SURFACE, NAME=UNASSIGNED"), std::string::npos);
|
||
EXPECT_NE(content.find("*SURFACE, NAME=FACE_0"), std::string::npos);
|
||
}
|
||
|
||
TEST(VDE1241Export, IllegalSetNameEscaped) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
std::vector<NamedSet> sets;
|
||
NamedSet bad;
|
||
bad.name = "bad name,1";
|
||
bad.node_ids = {0, 1};
|
||
sets.push_back(bad);
|
||
|
||
const std::string path = VDE_TEST_TEMP_DIR "vde1241_badname.inp";
|
||
std::string error;
|
||
EXPECT_TRUE(export_abaqus(mesh, path, sets, FEAMaterialParams{}, &error));
|
||
const std::string content = read_text_file(path);
|
||
std::remove(path.c_str());
|
||
|
||
EXPECT_NE(content.find("*NSET, NSET=bad_name_1"), std::string::npos);
|
||
EXPECT_FALSE(error.empty()) << "非法名称应返回明确说明";
|
||
}
|
||
|
||
TEST(VDE1241Export, AnsysAndNastranWriteNamedSets) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
std::vector<NamedSet> sets;
|
||
for (int f = 0; f < 6; ++f)
|
||
sets.push_back(make_face_selection(mesh, box, std::vector<int>{f}, "FACE_" + std::to_string(f)));
|
||
FEAMaterialParams material;
|
||
material.youngs_modulus = 12345.0;
|
||
material.poisson_ratio = 0.25;
|
||
|
||
{
|
||
const std::string path = VDE_TEST_TEMP_DIR "vde1241_ansys.cdb";
|
||
ASSERT_TRUE(export_ansys(mesh, path, sets, material));
|
||
const std::string content = read_text_file(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_NE(content.find("CMBLOCK,FACE_0,NODE"), std::string::npos);
|
||
EXPECT_NE(content.find("MP,EX,1,12345"), std::string::npos);
|
||
}
|
||
{
|
||
const std::string path = VDE_TEST_TEMP_DIR "vde1241_nastran.bdf";
|
||
ASSERT_TRUE(export_nastran(mesh, path, sets, material));
|
||
const std::string content = read_text_file(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_NE(content.find("$ NSET FACE_0"), std::string::npos);
|
||
EXPECT_NE(content.find("$ SURFACE FACE_0"), std::string::npos);
|
||
EXPECT_NE(content.find("12345"), std::string::npos);
|
||
}
|
||
}
|
||
|
||
TEST(VDE1241Export, LegacyTwoArgumentSignatureCompatible) {
|
||
const BrepModel box = make_box(1.0, 1.0, 1.0);
|
||
const FEAMesh mesh = make_box_mesh(box);
|
||
const std::string path = VDE_TEST_TEMP_DIR "vde1241_legacy.inp";
|
||
EXPECT_TRUE(export_abaqus(mesh, path));
|
||
const std::string content = read_text_file(path);
|
||
std::remove(path.c_str());
|
||
EXPECT_NE(content.find("*NODE"), std::string::npos);
|
||
EXPECT_NE(content.find("*ELEMENT"), std::string::npos);
|
||
EXPECT_NE(content.find("*SURFACE, NAME=FACE_0"), std::string::npos);
|
||
}
|