Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testSimplexFactories.cpp
Go to the documentation of this file.
1#include <SimplicialMesh.h>
2#include <iostream>
3#include <stdexcept>
4
5namespace
6{
7 void require(bool condition, const char *message)
8 {
9 if (!condition)
10 {
11 throw std::runtime_error(message);
12 }
13 }
14
15 template <short order> void check(model::SimplicialMesh<3> &mesh)
16 {
17 for (const auto &entry : mesh.simplexFactory<order>().liveSimplices())
18 {
19 const auto &simplex = entry.second;
20 require(mesh.simplexFactory<order>().get(simplex->xID) == simplex, "Factory identity mismatch");
21 if constexpr (order > 0)
22 {
23 for (const auto &child : simplex->children())
24 {
25 require(mesh.simplexFactory<order - 1>().get(child->xID) == child, "Child is not shared");
26 require(child->parents().at(simplex->xID) == simplex, "Missing reciprocal parent");
27 }
28 }
29 }
30 if constexpr (order > 0)
31 {
32 check<order - 1>(mesh);
33 }
34 }
35
36 void exercise(const std::string &path)
37 {
38 std::weak_ptr<model::Simplex<3, 0>> weakVertex;
39 std::shared_ptr<model::Simplex<3, 2>> retainedFace;
40 {
41 model::SimplicialMesh<3> mesh(path, Eigen::Matrix3d::Identity(), Eigen::Vector3d::Zero(), {-1});
43 auto root = mesh.simplices().begin()->second;
44 const auto key = root->xID;
45 auto reversed = key;
46 std::reverse(reversed.begin(), reversed.end());
47 require(mesh.simplexFactory<3>().getOrCreate(reversed, root->region->regionID) == root,
48 "Canonical factory lookup duplicated a simplex");
49 const auto before = mesh.simplexFactory<3>().liveSimplices().size();
50 bool rejected = false;
51 try
52 {
53 mesh.simplexFactory<3>().getOrCreate(key, root->region->regionID + 100000);
54 }
55 catch (const std::runtime_error &)
56 {
57 rejected = true;
58 }
59 require(rejected, "Conflicting region was accepted");
60 require(mesh.simplexFactory<3>().liveSimplices().size() == before, "Failed creation changed the hierarchy");
62 // Keep children alive while replacing their parent under the same canonical key.
63 const auto regionID = root->region->regionID;
64 {
65 const auto children = root->children();
66 std::weak_ptr<model::Simplex<3, 3>> previous = root;
67 mesh.simplices().erase(key);
68 root.reset();
69 require(previous.expired(), "Children retain their deleted parent");
70 root = mesh.simplexFactory<3>().getOrCreate(key, regionID);
71 mesh.simplices().emplace(key, root);
72 for (const auto &child : children)
73 {
74 require(child->parents().at(key) == root, "Recreation did not replace expired parent link");
75 }
76 }
78 retainedFace = root->children().front();
79 weakVertex = retainedFace->children().front()->children().front();
80 std::weak_ptr<model::Simplex<3, 3>> weakRoot = root;
81 root.reset();
82 mesh.simplices().clear();
83 require(weakRoot.expired(), "Parent/child ownership cycle retains volume simplex");
84 require(retainedFace->parents().empty(), "Expired parents remain in topology queries");
85 require(mesh.simplexFactory<3>().liveSimplices().empty(), "Factory owns volume simplices");
86 mesh.simplexFactory<3>().pruneExpired();
87 require(mesh.simplexFactory<3>().empty(), "Expired factory entries were not pruned");
88 require(!weakVertex.expired(), "Retained face lost its children");
89 }
90 require(retainedFace->parents().empty(), "Retained face has dangling parent links");
91 for (const auto* vertex : retainedFace->vertices())
92 {
93 require(vertex != nullptr && vertex->P0.allFinite(), "Retained simplex lost its vertex geometry");
94 }
95 retainedFace.reset();
96 require(weakVertex.expired(), "Simplex hierarchy leaked after final owner release");
97 }
98} // namespace
99
100int main(int argc, char **argv)
101{
102 try
103 {
104 require(argc == 2, "Expected repository root");
105 for (const auto *name : {"bicrystal_48", "polycrystalCube_10_periodicX", "polycrystalCube_10_periodicXY"})
106 {
107 exercise(std::string(argv[1]) + "/Library/Meshes/" + name + ".msh");
108 }
109 std::cout << "PASS: shared simplex identity, weak parent links, failed creation, and lifetime cleanup\n";
110 return 0;
111 }
112 catch (const std::exception &error)
113 {
114 std::cerr << error.what() << '\n';
115 return 1;
116 }
117}
void exercise(const std::filesystem::path &root, int rank, bool clamp)
void require(bool value, const char *message)
void check(bool ok, const char *message)
void argv
void b int argc
b order
newbad key
REAL * vertex
Definition triangle.cpp:542