v0.16.3
Loading...
Searching...
No Matches
cgal_crack_extension.cpp
Go to the documentation of this file.
1/** \file cgal_crack_extension.cpp
2 * \brief Crack extension by local flips: CGALTools candidate search checks.
3 *
4 * Meshes are generated in code: a planar triangulation extruded into prisms,
5 * each split into three tetrahedra with a global vertex-order rule, so planar
6 * layers are faces of the mesh and the crack is a set of layer triangles.
7 * See doc/development/cgal_crack_extension_algorithm.md, "Verification".
8 */
9#include <MoFEM.hpp>
10
11using namespace MoFEM;
12
13namespace {
14
15using Vec = std::array<double, 3>;
16using Edge = std::array<int, 2>;
17using Face = std::array<int, 3>;
18
19Vec sub(const Vec &a, const Vec &b) {
20 return {a[0] - b[0], a[1] - b[1], a[2] - b[2]};
21}
22double dot(const Vec &a, const Vec &b) {
23 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
24}
25Vec cross(const Vec &a, const Vec &b) {
26 return {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
27 a[0] * b[1] - a[1] * b[0]};
28}
29double norm(const Vec &a) { return std::sqrt(dot(a, a)); }
30Vec unit(const Vec &a) {
31 const double l = norm(a);
32 return {a[0] / l, a[1] / l, a[2] / l};
33}
34double det(const TetMeshData &mesh, const std::array<int, 4> &c) {
35 const auto &p = mesh.points;
36 return dot(sub(p[c[1]], p[c[0]]),
37 cross(sub(p[c[2]], p[c[0]]), sub(p[c[3]], p[c[0]])));
38}
39Edge edgeKey(int a, int b) { return a < b ? Edge{a, b} : Edge{b, a}; }
40Face faceKey(int a, int b, int c) {
41 Face f{a, b, c};
42 std::sort(f.begin(), f.end());
43 return f;
44}
45std::vector<Face> cellFaces(const std::array<int, 4> &c) {
46 return {faceKey(c[1], c[2], c[3]), faceKey(c[0], c[2], c[3]),
47 faceKey(c[0], c[1], c[3]), faceKey(c[0], c[1], c[2])};
48}
49std::vector<Edge> cellEdges(const std::array<int, 4> &c) {
50 return {edgeKey(c[0], c[1]), edgeKey(c[0], c[2]), edgeKey(c[0], c[3]),
51 edgeKey(c[1], c[2]), edgeKey(c[1], c[3]), edgeKey(c[2], c[3])};
52}
53
54struct Plane2D {
55 std::vector<std::array<double, 2>> points;
56 std::vector<std::array<int, 3>> triangles;
57 std::vector<char> crack;
58};
59
60struct Model {
62 std::vector<int> crack; ///< Indices into mesh.boundary.
63 int planar = 0; ///< Points per layer.
64 double volume = 0;
65 int node(int level, int i) const { return level * planar + i; }
66};
67
68/** Extrude a planar triangulation through levels; the crack is the flagged
69 * triangles at crack_level. Prisms split with the lowest-index rule, which
70 * gives conforming quadrilateral diagonals between neighbouring prisms. */
71Model extrude(const Plane2D &plane, const std::vector<double> &levels,
72 int crack_level) {
73 Model model;
74 model.planar = plane.points.size();
75 auto &mesh = model.mesh;
76 for (double z : levels)
77 for (const auto &p : plane.points)
78 mesh.points.push_back({p[0], p[1], z});
79 for (int l = 0; l + 1 < static_cast<int>(levels.size()); ++l)
80 for (auto tri : plane.triangles) {
81 std::sort(tri.begin(), tri.end());
82 const int b0 = model.node(l, tri[0]), b1 = model.node(l, tri[1]),
83 b2 = model.node(l, tri[2]);
84 const int t0 = model.node(l + 1, tri[0]), t1 = model.node(l + 1, tri[1]),
85 t2 = model.node(l + 1, tri[2]);
86 for (std::array<int, 4> cell : {std::array<int, 4>{b0, b1, b2, t2},
87 std::array<int, 4>{b0, b1, t1, t2},
88 std::array<int, 4>{b0, t0, t1, t2}}) {
89 if (det(mesh, cell) < 0)
90 std::swap(cell[2], cell[3]);
91 mesh.cells.push_back(cell);
92 }
93 }
94 std::map<Face, int> count;
95 for (const auto &cell : mesh.cells)
96 for (const auto &face : cellFaces(cell))
97 ++count[face];
98 for (const auto &[face, n] : count)
99 if (n == 1)
100 mesh.boundary.push_back(face);
101 for (std::size_t t = 0; t != plane.triangles.size(); ++t)
102 if (plane.crack[t]) {
103 const auto &tri = plane.triangles[t];
104 mesh.boundary.push_back({model.node(crack_level, tri[0]),
105 model.node(crack_level, tri[1]),
106 model.node(crack_level, tri[2])});
107 model.crack.push_back(mesh.boundary.size() - 1);
108 }
109 // Box edges are geometry curves: both end points on two common box planes.
110 Vec lo = mesh.points.front(), hi = lo;
111 for (const auto &p : mesh.points)
112 for (int i = 0; i != 3; ++i) {
113 lo[i] = std::min(lo[i], p[i]);
114 hi[i] = std::max(hi[i], p[i]);
115 }
116 const auto planes = [&](int v) {
117 int mask = 0;
118 for (int i = 0; i != 3; ++i) {
119 mask |= (mesh.points[v][i] == lo[i]) << (2 * i);
120 mask |= (mesh.points[v][i] == hi[i]) << (2 * i + 1);
121 }
122 return mask;
123 };
124 std::set<Edge> edges;
125 for (const auto &cell : mesh.cells)
126 for (const auto &edge : cellEdges(cell))
127 edges.insert(edge);
128 for (const auto &edge : edges)
129 if (__builtin_popcount(planes(edge[0]) & planes(edge[1])) >= 2)
130 mesh.featureEdges.push_back(edge);
131 for (const auto &cell : mesh.cells)
132 model.volume += det(mesh, cell) / 6;
133 return model;
134}
135
136/** Unit squares with diagonals (i, j)-(i + 1, j + 1); cells left of crack_x
137 * are cracked. */
138Plane2D grid(int nx, int ny, int crack_x) {
139 Plane2D plane;
140 for (int j = 0; j <= ny; ++j)
141 for (int i = 0; i <= nx; ++i)
142 plane.points.push_back({double(i), double(j)});
143 const auto id = [nx](int i, int j) { return j * (nx + 1) + i; };
144 for (int j = 0; j != ny; ++j)
145 for (int i = 0; i != nx; ++i) {
146 plane.triangles.push_back({id(i, j), id(i + 1, j), id(i + 1, j + 1)});
147 plane.triangles.push_back({id(i, j), id(i + 1, j + 1), id(i, j + 1)});
148 plane.crack.push_back(i < crack_x);
149 plane.crack.push_back(i < crack_x);
150 }
151 return plane;
152}
153
154/** A V notch in the front: bottom b = 0, shoulders s1 = 1 and s2 = 2, and
155 * apex x = 3 beyond them, joined to b by the edge b-x. The two faces (b, s1,
156 * x) and (b, x, s2) extend the notch sides; the flat face (b, s1, s2) would
157 * need the bridging edge s1-s2, which must never be created. */
158Plane2D notch() {
159 Plane2D plane;
160 plane.points = {{0, 0}, {1, -1}, {1, 1}, {1.6, 0}, {-1, -2}, {1, -2},
161 {2.6, -2}, {2.6, 0}, {2.6, 2}, {1, 2}, {-1, 2}, {-1, 0}};
162 plane.triangles = {{4, 5, 1}, {4, 1, 0}, {4, 0, 11}, {11, 0, 10}, {10, 0, 2},
163 {10, 2, 9}, {0, 1, 3}, {0, 3, 2}, {1, 5, 6}, {1, 6, 3},
164 {3, 6, 7}, {3, 7, 8}, {3, 8, 2}, {2, 8, 9}};
165 plane.crack = {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
166 return plane;
167}
168
169std::vector<char> crackNodes(const Model &model) {
170 std::vector<char> nodes(model.mesh.points.size(), 0);
171 for (int i : model.crack)
172 for (int v : model.mesh.boundary[i])
173 nodes[v] = 1;
174 return nodes;
175}
176
177/** Front edges sorted by midpoint (x, y, z). */
178std::vector<Edge> frontEdges(const Model &model) {
179 std::map<Edge, int> crack_count;
180 std::set<Edge> skin;
181 std::vector<char> is_crack(model.mesh.boundary.size(), 0);
182 for (int i : model.crack)
183 is_crack[i] = 1;
184 for (std::size_t i = 0; i != model.mesh.boundary.size(); ++i) {
185 const auto &f = model.mesh.boundary[i];
186 for (const auto &edge :
187 {edgeKey(f[0], f[1]), edgeKey(f[1], f[2]), edgeKey(f[0], f[2])}) {
188 if (is_crack[i])
189 ++crack_count[edge];
190 else
191 skin.insert(edge);
192 }
193 }
194 std::vector<Edge> front;
195 for (const auto &[edge, n] : crack_count)
196 if (n == 1 && !skin.count(edge))
197 front.push_back(edge);
198 const auto &p = model.mesh.points;
199 std::sort(front.begin(), front.end(), [&](const Edge &a, const Edge &b) {
200 const Vec ma{p[a[0]][0] + p[a[1]][0], p[a[0]][1] + p[a[1]][1],
201 p[a[0]][2] + p[a[1]][2]};
202 const Vec mb{p[b[0]][0] + p[b[1]][0], p[b[0]][1] + p[b[1]][1],
203 p[b[0]][2] + p[b[1]][2]};
204 return ma < mb;
205 });
206 return front;
207}
208
209/** In-plane normal of the front edge away from its crack face, rotated by
210 * angle (degrees) about the edge. */
211Vec direction(const Model &model, const Edge &edge, double angle) {
212 const auto &p = model.mesh.points;
213 int apex = -1;
214 for (int i : model.crack) {
215 const auto &f = model.mesh.boundary[i];
216 if (std::count(f.begin(), f.end(), edge[0]) &&
217 std::count(f.begin(), f.end(), edge[1]))
218 for (int v : f)
219 if (v != edge[0] && v != edge[1])
220 apex = v;
221 }
222 const auto t = unit(sub(p[edge[1]], p[edge[0]]));
223 auto d = unit(cross(t, Vec{0, 0, 1}));
224 const Vec mid{(p[edge[0]][0] + p[edge[1]][0]) / 2,
225 (p[edge[0]][1] + p[edge[1]][1]) / 2,
226 (p[edge[0]][2] + p[edge[1]][2]) / 2};
227 if (dot(sub(p[apex], mid), d) > 0)
228 d = {-d[0], -d[1], -d[2]};
229 const auto b = cross(t, d);
230 const double a = angle * M_PI / 180;
231 return {std::cos(a) * d[0] + std::sin(a) * b[0],
232 std::cos(a) * d[1] + std::sin(a) * b[1],
233 std::cos(a) * d[2] + std::sin(a) * b[2]};
234}
235
236/** Independent validity check of an applied mesh. */
237MoFEMErrorCode checkMesh(TestingInterface &testing, const Model &model,
238 const std::string &name) {
240 const auto &mesh = model.mesh;
241 int inverted = 0, overfull = 0;
242 double volume = 0;
243 std::map<Face, int> count;
244 for (const auto &cell : mesh.cells) {
245 const double v = det(mesh, cell) / 6;
246 inverted += !(v > 0);
247 volume += v;
248 for (const auto &face : cellFaces(cell))
249 ++count[face];
250 }
251 for (const auto &[face, n] : count)
252 overfull += n > 2;
253 int boundary = 0;
254 for (const auto &[face, n] : count)
255 boundary += n == 1;
256 CHKERR testing.checkValue<TestType::SCALAR>(name + " inverted cells",
257 inverted, 0, {0, 0});
258 CHKERR testing.checkValue<TestType::SCALAR>(name + " overfull faces",
259 overfull, 0, {0, 0});
260 CHKERR testing.checkValue<TestType::SCALAR>(name + " volume", volume,
261 model.volume, {0, 1e-12});
262 // Body skin faces are the first boundary entries and must be unchanged.
263 int skin = 0;
264 for (const auto &face : mesh.boundary)
265 skin += count.count(faceKey(face[0], face[1], face[2])) &&
266 count.at(faceKey(face[0], face[1], face[2])) == 1;
267 CHKERR testing.checkValue<TestType::SCALAR>(name + " boundary faces",
268 boundary, skin, {0, 0});
270}
271
272static char help[] = "Crack extension candidate search checks\n";
273
274void logReport(const std::string &name,
276 MOFEM_LOG("WORLD", Sev::inform)
277 << name << ": tried " << r.candidatesTried << ", accepted " << r.accepted
278 << ", rejected behind/angle/move " << r.rejectedBehind << '/'
279 << r.rejectedAngle << '/' << r.rejectedMove << ", inversion/quality "
280 << r.rejectedInversion << '/' << r.rejectedQuality
281 << ", bridging/pocket/fixed/topology " << r.rejectedBridging << '/'
282 << r.rejectedPocket << '/' << r.rejectedFixed << '/' << r.rejectedTopology
283 << ", audited " << r.auditedOperations;
284}
285
286/** Edges created by a candidate that join two crack nodes, other than the
287 * closing edges of its corner closures. */
288int bridgingEdges(const Model &model, const std::vector<char> &crack,
290 std::set<Edge> existing;
291 for (const auto &cell : model.mesh.cells)
292 for (const auto &edge : cellEdges(cell))
293 existing.insert(edge);
294 for (const auto &f : candidate.closureFaces)
295 for (const auto &edge :
296 {edgeKey(f[0], f[1]), edgeKey(f[1], f[2]), edgeKey(f[0], f[2])})
297 existing.insert(edge);
298 std::set<Edge> bridging;
299 for (const auto &cell : candidate.createdCells)
300 for (const auto &edge : cellEdges(cell))
301 if (!existing.count(edge) && crack[edge[0]] && crack[edge[1]])
302 bridging.insert(edge);
303 return bridging.size();
304}
305
306CGALTools::CrackExtensionSettings auditSettings() {
308 settings.audit = true;
309 settings.lookAheadCandidates = 0;
310 return settings;
311}
312
313/** 1: flat crack grows in its own plane with flips only, without moves.
314 * Each step extends the first front edge with a candidate. A front edge whose
315 * in-plane face has three crack nodes is a notch for the edge-based fill in
316 * the fracture module, not an extension, and correctly has no candidate. */
319 auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
320 const auto settings = auditSettings();
321 int steps = 0, wrong_class = 0, out_of_plane = 0, closures = 0, open = 0;
322 double max_move = 0, max_angle = 0;
323 for (int step = 0; step != 6; ++step) {
325 for (const auto &edge : frontEdges(model)) {
327 const auto candidates = tools.crackExtensionCandidates(
328 model.mesh, model.crack, edge, direction(model, edge, 0), settings,
329 &report);
330 if (!candidates.empty()) {
331 chosen = candidates.front();
332 break;
333 }
334 }
335 if (chosen.freeNode < 0) {
336 MOFEM_LOG("WORLD", Sev::error) << "No front edge has a candidate";
337 break;
338 }
339 wrong_class += chosen.candidateClass != 1;
340 closures += chosen.closureFaces.size();
341 open += chosen.openCorners;
342 max_move = std::max(max_move, norm(chosen.displacement));
343 max_angle = std::max(max_angle, chosen.angleDeviationDegrees);
344 tools.applyCrackExtension(model.mesh, model.crack, chosen);
345 CHKERR checkMesh(testing, model, "Flat step");
346 const auto nodes = crackNodes(model);
347 for (std::size_t v = 0; v != nodes.size(); ++v)
348 out_of_plane += nodes[v] && model.mesh.points[v][2] != 0;
349 ++steps;
350 }
351 MOFEM_LOG("WORLD", Sev::inform) << "Flat: " << closures << " closure faces, "
352 << open << " corners left open";
353 CHKERR testing.checkValue<TestType::SCALAR>("Flat extension steps", steps, 6,
354 {0, 0});
355 CHKERR testing.checkValue<TestType::SCALAR>("Flat non-class-1 steps",
356 wrong_class, 0, {0, 0});
357 CHKERR testing.checkValue<TestType::SCALAR>("Flat node move", max_move, 0,
358 {0, 0});
359 CHKERR testing.checkValue<TestType::SCALAR>("Flat angle deviation", max_angle,
360 0, {0, 0});
361 CHKERR testing.checkValue<TestType::SCALAR>("Flat crack nodes off plane",
362 out_of_plane, 0, {0, 0});
364}
365
366/** 2: kinked direction needs a move; the new face follows the target plane
367 * within the tolerance. */
370 auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
371 const auto settings = auditSettings();
372 const auto front = frontEdges(model);
373 const auto edge = front[1];
374 const auto d = direction(model, edge, 30);
376 const auto candidates = tools.crackExtensionCandidates(
377 model.mesh, model.crack, edge, d, settings, &report);
378 CHKERR testing.checkValue<TestType::SCALAR>("Kinked candidates found",
379 !candidates.empty(), 1, {0, 0});
380 const auto c = candidates.front();
381 CHKERR testing.checkValue<TestType::SCALAR>("Kinked class is a move",
382 c.candidateClass >= 2, 1, {0, 0});
383 CHKERR testing.checkValue<TestType::SCALAR>(
384 "Kinked audited operations", report.auditedOperations > 0, 1, {0, 0});
385 tools.applyCrackExtension(model.mesh, model.crack, c);
386 CHKERR checkMesh(testing, model, "Kinked");
387 const auto &p = model.mesh.points;
388 const auto t = unit(sub(p[edge[1]], p[edge[0]]));
389 const auto n = unit(cross(t, d));
390 const auto face_normal = unit(cross(sub(p[c.newFace[1]], p[c.newFace[0]]),
391 sub(p[c.newFace[2]], p[c.newFace[0]])));
392 const double angle =
393 std::asin(std::min(1., norm(cross(face_normal, n)))) * 180 / M_PI;
394 CHKERR testing.checkValue<TestType::SCALAR>(
395 "Kinked face within tolerance", angle <= settings.angleToleranceDegrees,
396 1, {0, 0});
397 CHKERR testing.checkValue<TestType::SCALAR>(
398 "Kinked reported angle", c.angleDeviationDegrees, angle, {1e-9, 0});
400}
401
402/** 3: a V notch. Its sides extend through the apex beyond the shoulders;
403 * no candidate creates the bridging edge across the notch. */
406 auto model = extrude(notch(), {-2, -1, 0, 1, 2}, 2);
407 auto settings = auditSettings();
408 const int b = model.node(2, 0), s1 = model.node(2, 1), s2 = model.node(2, 2),
409 x = model.node(2, 3);
410 const auto crack = crackNodes(model);
411 const auto front = frontEdges(model);
412 CHKERR testing.checkValue<TestType::SCALAR>("Notch front edges", front.size(),
413 4, {0, 0});
414
415 // The notch side b-s1 extends in plane through the apex x.
416 const auto side = edgeKey(b, s1);
417 {
418 const auto candidates = tools.crackExtensionCandidates(
419 model.mesh, model.crack, side, direction(model, side, 0), settings);
420 CHKERR testing.checkValue<TestType::SCALAR>("Notch side candidates found",
421 !candidates.empty(), 1, {0, 0});
422 const auto &c = candidates.front();
423 CHKERR testing.checkValue<TestType::SCALAR>("Notch side class",
424 c.candidateClass, 1, {0, 0});
425 CHKERR testing.checkValue<TestType::SCALAR>("Notch side free node",
426 c.freeNode, x, {0, 0});
427 // The new edge b-x and the other side b-s2 form a non-convex corner at
428 // b; the existing face (b, x, s2) closes it in the same step. The corner
429 // then left at s2 would need a flip that inverts cells in this mesh, so
430 // it stays open and is counted in openCorners.
431 bool closed = false;
432 for (const auto &f : c.closureFaces)
433 closed |= faceKey(f[0], f[1], f[2]) == faceKey(b, x, s2);
434 CHKERR testing.checkValue<TestType::SCALAR>("Notch closed by (b, x, s2)",
435 closed, 1, {0, 0});
436 // Applied, the notch bottom becomes an interior crack node.
437 auto applied = model;
438 tools.applyCrackExtension(applied.mesh, applied.crack, c);
439 CHKERR checkMesh(testing, applied, "Notch closed");
440 const auto front_after = frontEdges(applied);
441 int at_bottom = 0;
442 for (const auto &e : front_after)
443 at_bottom += e[0] == b || e[1] == b;
444 CHKERR testing.checkValue<TestType::SCALAR>(
445 "Notch bottom has no front edge", at_bottom, 0, {0, 0});
446 }
447
448 // Every candidate of every class, for all front edges and directions.
449 settings.stopAtFirstClass = false;
451 int candidates_seen = 0, bridging = 0, notch_face = 0, flipped = 0;
452 int closures = 0;
453 std::array<int, 4> classes{0, 0, 0, 0};
454 for (const auto &edge : front)
455 for (double angle : {-20., 0., 20.}) {
456 const auto candidates = tools.crackExtensionCandidates(
457 model.mesh, model.crack, edge, direction(model, edge, angle),
458 settings, &report);
459 for (const auto &c : candidates) {
460 ++candidates_seen;
461 closures += c.closureFaces.size();
462 flipped += c.operations > 0;
463 ++classes[c.candidateClass];
464 bridging += bridgingEdges(model, crack, c);
465 for (const auto &cell : c.createdCells)
466 for (const auto &face : cellFaces(cell))
467 notch_face += face == faceKey(b, s1, s2);
468 notch_face += faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) ==
469 faceKey(b, s1, s2);
470 }
471 }
472 CHKERR testing.checkValue<TestType::SCALAR>("Notch candidates found",
473 candidates_seen > 0, 1, {0, 0});
474 CHKERR testing.checkValue<TestType::SCALAR>("Notch bridging edges", bridging,
475 0, {0, 0});
476 CHKERR testing.checkValue<TestType::SCALAR>("Notch filling face", notch_face,
477 0, {0, 0});
478 CHKERR testing.checkValue<TestType::SCALAR>("Notch candidates with flips",
479 flipped > 0, 1, {0, 0});
480 CHKERR testing.checkValue<TestType::SCALAR>(
481 "Notch bridging rule exercised", report.rejectedBridging > 0, 1, {0, 0});
482 MOFEM_LOG("WORLD", Sev::inform)
483 << "Notch sweep: tried " << report.candidatesTried << ", accepted "
484 << report.accepted << ", bridging rejections " << report.rejectedBridging
485 << ", pocket rejections " << report.rejectedPocket << "; closure faces "
486 << closures << "; candidates with flips " << flipped << ", by class "
487 << classes[1] << '/' << classes[2] << '/' << classes[3];
488 logReport("Notch sweep", report);
490}
491
492/** 4: moved skin nodes stay on their plane, curve nodes on their line,
493 * within their two curve edges. A vertical geometry curve through planar
494 * node (4, 1) crosses kinked target planes ahead of the front. */
497 auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
498 constexpr int curve = 1 * 7 + 4;
499 std::set<int> curve_nodes;
500 for (int l = 0; l != 4; ++l) {
501 model.mesh.featureEdges.push_back(
502 {model.node(l, curve), model.node(l + 1, curve)});
503 curve_nodes.insert(model.node(l, curve));
504 }
505 auto settings = auditSettings();
506 settings.stopAtFirstClass = false;
507 const auto &p = model.mesh.points;
508 Vec lo = p.front(), hi = lo;
509 for (const auto &x : p)
510 for (int i = 0; i != 3; ++i) {
511 lo[i] = std::min(lo[i], x[i]);
512 hi[i] = std::max(hi[i], x[i]);
513 }
514 int moved_skin = 0, moved_curve = 0, left_plane = 0, moved = 0;
515 int moved_line = 0, left_line = 0;
516 for (const auto &edge : frontEdges(model))
517 for (double angle : {-30., -15., 15., 30.}) {
518 const auto candidates = tools.crackExtensionCandidates(
519 model.mesh, model.crack, edge, direction(model, edge, angle),
520 settings);
521 for (const auto &c : candidates) {
522 if (norm(c.displacement) == 0)
523 continue;
524 ++moved;
525 const auto &x = p[c.freeNode];
526 int planes = 0;
527 for (int i = 0; i != 3; ++i)
528 for (double bound : {lo[i], hi[i]})
529 if (x[i] == bound) {
530 ++planes;
531 left_plane += std::abs(x[i] + c.displacement[i] - bound) > 1e-12;
532 }
533 moved_skin += planes == 1;
534 moved_curve += planes >= 2;
535 if (curve_nodes.count(c.freeNode)) {
536 ++moved_line;
537 left_line += c.displacement[0] != 0 || c.displacement[1] != 0 ||
538 std::abs(c.displacement[2]) >= 1;
539 }
540 }
541 }
542 CHKERR testing.checkValue<TestType::SCALAR>("Boundary moved candidates",
543 moved > 0, 1, {0, 0});
544 CHKERR testing.checkValue<TestType::SCALAR>("Boundary moved skin nodes",
545 moved_skin > 0, 1, {0, 0});
546 CHKERR testing.checkValue<TestType::SCALAR>("Boundary nodes leaving skin",
547 left_plane, 0, {0, 0});
548 CHKERR testing.checkValue<TestType::SCALAR>("Boundary moved curve nodes",
549 moved_line > 0, 1, {0, 0});
550 CHKERR testing.checkValue<TestType::SCALAR>("Boundary nodes leaving curve",
551 left_line, 0, {0, 0});
552 MOFEM_LOG("WORLD", Sev::inform)
553 << "Boundary sweep: moved " << moved << ", on one skin plane "
554 << moved_skin << ", on a box edge " << moved_curve
555 << ", on the interior curve " << moved_line;
557}
558
559/** 5: no admissible extension returns nothing; invalid input throws. */
562 const auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
563 auto settings = auditSettings();
564 settings.maxMoveRelative = 0;
565 const auto edge = frontEdges(model)[1];
567 const auto candidates = tools.crackExtensionCandidates(
568 model.mesh, model.crack, edge, direction(model, edge, 30), settings,
569 &report);
570 CHKERR testing.checkValue<TestType::SCALAR>("Failure candidates",
571 candidates.size(), 0, {0, 0});
572 CHKERR testing.checkValue<TestType::SCALAR>(
573 "Failure move rejections", report.rejectedMove > 0, 1, {0, 0});
574 bool threw = false;
575 try {
576 const auto d = direction(model, edge, 180);
577 tools.crackExtensionCandidates(model.mesh, model.crack, edge, d,
578 auditSettings());
579 } catch (const MoFEMException &) {
580 threw = true;
581 }
582 CHKERR testing.checkValue<TestType::SCALAR>(
583 "Failure backward direction rejected", threw, 1, {0, 0});
584 threw = false;
585 try {
586 tools.crackExtensionCandidates(model.mesh, model.crack,
587 {model.node(2, 0), model.node(2, 1)},
588 {1, 0, 0}, auditSettings());
589 } catch (const MoFEMException &) {
590 threw = true;
591 }
592 CHKERR testing.checkValue<TestType::SCALAR>("Failure skin edge rejected",
593 threw, 1, {0, 0});
594
595 // A non-positive cell in the region is rejected, not flipped.
596 {
597 auto inverted = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
598 const auto front = frontEdges(inverted)[1];
599 for (auto &cell : inverted.mesh.cells)
600 if (std::count(cell.begin(), cell.end(), front[0])) {
601 std::swap(cell[2], cell[3]);
602 break;
603 }
604 std::string what;
605 try {
606 tools.crackExtensionCandidates(inverted.mesh, inverted.crack, front,
607 direction(inverted, front, 0),
608 auditSettings());
609 } catch (const MoFEMException &e) {
610 what = e.what();
611 }
612 CHKERR testing.checkValue<TestType::SCALAR>(
613 "Failure inverted cell rejected",
614 what.find("Inverted cell in the crack extension region") !=
615 std::string::npos,
616 1, {0, 0});
617 }
618
619 // Candidates are bound to the patch state they were generated from.
620 {
621 auto stale = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
622 auto all_classes = auditSettings();
623 all_classes.stopAtFirstClass = false;
624 const auto front = frontEdges(stale)[1];
625 const auto all = tools.crackExtensionCandidates(
626 stale.mesh, stale.crack, front, direction(stale, front, 30),
627 all_classes);
628 CHKERR testing.checkValue<TestType::SCALAR>("Failure several candidates",
629 all.size() >= 2, 1, {0, 0});
630 tools.applyCrackExtension(stale.mesh, stale.crack, all[0]);
631 const auto points = stale.mesh.points;
632 const auto cells = stale.mesh.cells;
633 threw = false;
634 try {
635 tools.applyCrackExtension(stale.mesh, stale.crack, all[1]);
636 } catch (const MoFEMException &) {
637 threw = true;
638 }
639 CHKERR testing.checkValue<TestType::SCALAR>(
640 "Failure stale candidate rejected", threw, 1, {0, 0});
641 CHKERR testing.checkValue<TestType::SCALAR>(
642 "Failure stale candidate left patch unchanged",
643 stale.mesh.points == points && stale.mesh.cells == cells, 1, {0, 0});
644 }
645
646 // A result that would invert cells is rejected before publication.
647 {
648 auto fresh = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
649 const auto front = frontEdges(fresh)[1];
650 auto candidates = tools.crackExtensionCandidates(
651 fresh.mesh, fresh.crack, front, direction(fresh, front, 30),
652 auditSettings());
653 auto c = candidates.front();
654 CHKERR testing.checkValue<TestType::SCALAR>(
655 "Failure tampered candidate moves", norm(c.displacement) > 0, 1,
656 {0, 0});
657 for (auto &x : c.displacement)
658 x *= 20;
659 const auto points = fresh.mesh.points;
660 const auto crack = fresh.crack;
661 threw = false;
662 try {
663 tools.applyCrackExtension(fresh.mesh, fresh.crack, c);
664 } catch (const MoFEMException &) {
665 threw = true;
666 }
667 CHKERR testing.checkValue<TestType::SCALAR>(
668 "Failure inverting candidate rejected", threw, 1, {0, 0});
669 CHKERR testing.checkValue<TestType::SCALAR>(
670 "Failure inverting candidate left patch unchanged",
671 fresh.mesh.points == points && fresh.crack == crack, 1, {0, 0});
672
673 // Labels changed after generation are rejected, not silently dropped.
674 // The digest catches this before the explicit size check.
675 fresh.mesh.cellLabels.assign(fresh.mesh.cells.size() - 1, 0);
676 threw = false;
677 try {
678 tools.applyCrackExtension(fresh.mesh, fresh.crack, candidates.front());
679 } catch (const MoFEMException &) {
680 threw = true;
681 }
682 CHKERR testing.checkValue<TestType::SCALAR>(
683 "Failure changed labels rejected", threw, 1, {0, 0});
684 }
686}
687
688/** 7: a hole. One uncracked cell is enclosed by crack faces: its corners
689 * span 270 degrees of crack, which a sum of edge directions misjudges. The
690 * fill closes it with the existing faces; a blocked front edge closes it by
691 * a closure candidate whose free node is a crack node, without flips. */
694 auto plane = grid(6, 4, 5);
695 // Cell (3, 1) holds triangles 2 * (1 * 6 + 3) and the next one.
696 plane.crack[2 * (1 * 6 + 3)] = plane.crack[2 * (1 * 6 + 3) + 1] = 0;
697 auto model = extrude(plane, {-2, -1, 0, 1, 2}, 2);
698 const auto id = [&](int i, int j) { return model.node(2, j * 7 + i); };
699 const auto hole_a = faceKey(id(3, 1), id(4, 1), id(4, 2));
700 const auto hole_b = faceKey(id(3, 1), id(4, 2), id(3, 2));
701
702 int filled = 0;
703 for (const auto &f : tools.crackFillFaces(model.mesh, model.crack)) {
704 const auto key = faceKey(f[0], f[1], f[2]);
705 filled += key == hole_a || key == hole_b;
706 }
707 CHKERR testing.checkValue<TestType::SCALAR>("Hole closed by the fill", filled,
708 2, {0, 0});
709
710 const auto edge = edgeKey(id(3, 1), id(3, 2));
711 const auto candidates = tools.crackExtensionCandidates(
712 model.mesh, model.crack, edge, direction(model, edge, 0),
713 auditSettings());
714 CHKERR testing.checkValue<TestType::SCALAR>("Hole edge has a candidate",
715 !candidates.empty(), 1, {0, 0});
716 const auto &c = candidates.front();
717 std::set<Face> faces{faceKey(c.newFace[0], c.newFace[1], c.newFace[2])};
718 for (const auto &f : c.closureFaces)
719 faces.insert(faceKey(f[0], f[1], f[2]));
720 CHKERR testing.checkValue<TestType::SCALAR>(
721 "Hole closed by one candidate",
722 faces.count(hole_a) && faces.count(hole_b), 1, {0, 0});
723 CHKERR testing.checkValue<TestType::SCALAR>("Hole closure without flips",
724 c.operations, 0, {0, 0});
725 CHKERR testing.checkValue<TestType::SCALAR>("Hole closure without a move",
726 norm(c.displacement), 0, {0, 0});
727 tools.applyCrackExtension(model.mesh, model.crack, c);
728 CHKERR checkMesh(testing, model, "Hole closed");
729 int at_hole = 0;
730 for (const auto &e : frontEdges(model))
731 for (int v : e)
732 at_hole +=
733 v == id(3, 1) || v == id(4, 1) || v == id(4, 2) || v == id(3, 2);
734 CHKERR testing.checkValue<TestType::SCALAR>("Hole leaves no front edge",
735 at_hole, 0, {0, 0});
737}
738
739/** 6: general position. Nodes off the crack plane and off the skin are
740 * jittered; several steps with kinked directions run under the audit. */
743 // Interior vertices off the crack plane move by up to 0.2 in each
744 // direction, which removes cospherical degeneracies.
745 auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
746 auto &p = model.mesh.points;
747 Vec lo = p.front(), hi = lo;
748 for (const auto &x : p)
749 for (int i = 0; i != 3; ++i) {
750 lo[i] = std::min(lo[i], x[i]);
751 hi[i] = std::max(hi[i], x[i]);
752 }
753 std::uint64_t state = 2463534242u;
754 const auto random = [&state]() {
755 state ^= state << 13;
756 state ^= state >> 7;
757 state ^= state << 17;
758 return double(state % 2001) / 1000. - 1.;
759 };
760 for (auto &x : p) {
761 bool fixed = x[2] == 0;
762 for (int i = 0; i != 3; ++i)
763 fixed |= x[i] == lo[i] || x[i] == hi[i];
764 if (!fixed)
765 for (int i = 0; i != 3; ++i)
766 x[i] += 0.2 * random();
767 }
768 model.volume = 0;
769 for (const auto &cell : model.mesh.cells)
770 model.volume += det(model.mesh, cell) / 6;
771 int inverted = 0;
772 for (const auto &cell : model.mesh.cells)
773 inverted += !(det(model.mesh, cell) > 0);
774 CHKERR testing.checkValue<TestType::SCALAR>("Jitter input inverted cells",
775 inverted, 0, {0, 0});
776
777 auto settings = auditSettings();
779 int steps = 0, bridging = 0;
780 const std::array<double, 6> angles{20, -25, 10, -15, 30, 0};
781 for (double angle : angles) {
783 for (const auto &edge : frontEdges(model)) {
784 const auto candidates = tools.crackExtensionCandidates(
785 model.mesh, model.crack, edge, direction(model, edge, angle),
786 settings, &report);
787 if (!candidates.empty()) {
788 chosen = candidates.front();
789 break;
790 }
791 }
792 if (chosen.freeNode < 0)
793 break;
794 bridging += bridgingEdges(model, crackNodes(model), chosen);
795 tools.applyCrackExtension(model.mesh, model.crack, chosen);
796 CHKERR checkMesh(testing, model, "Jitter step");
797 ++steps;
798 }
799 logReport("Jitter", report);
800 CHKERR testing.checkValue<TestType::SCALAR>("Jitter extension steps", steps,
801 angles.size(), {0, 0});
802 CHKERR testing.checkValue<TestType::SCALAR>("Jitter bridging edges", bridging,
803 0, {0, 0});
805}
806
807/** 9: a curved skin. Nodes inside the side y = 0, off the crack, are
808 * pushed out of its plane, so the skin is faceted, not planar. A moved skin
809 * node slides along one of its skin edges and stays on the faceted skin;
810 * the audit accounts for the volume its star gains. */
813 auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
814 auto &p = model.mesh.points;
815 std::set<int> bumped;
816 for (int v = 0; v != static_cast<int>(p.size()); ++v) {
817 auto &x = p[v];
818 const bool cracked = x[2] == 0 && x[0] <= 3;
819 if (x[1] == 0 && x[0] > 0 && x[0] < 6 && std::abs(x[2]) < 2 && !cracked) {
820 x[1] = -0.1 * (1 + (static_cast<int>(x[0] + x[2] + 4) % 2));
821 bumped.insert(v);
822 }
823 }
824 model.volume = 0;
825 for (const auto &cell : model.mesh.cells)
826 model.volume += det(model.mesh, cell) / 6;
827 std::map<Face, int> count;
828 for (const auto &cell : model.mesh.cells)
829 for (const auto &face : cellFaces(cell))
830 ++count[face];
831 std::map<int, std::set<int>> skin_neighbours;
832 for (const auto &[face, n] : count)
833 if (n == 1)
834 for (int i = 0; i != 3; ++i)
835 for (int j = 0; j != 3; ++j)
836 if (i != j)
837 skin_neighbours[face[i]].insert(face[j]);
838 // On a skin edge: parallel to it, between a tenth behind and its far end.
839 const auto on_skin_edge = [&](int v, const Vec &displacement) {
840 for (int u : skin_neighbours[v]) {
841 const auto e = sub(p[u], p[v]);
842 const double alpha = dot(displacement, e) / dot(e, e);
843 if (norm(cross(displacement, e)) <= 1e-12 * norm(e) * norm(e) &&
844 alpha >= -0.1 - 1e-12 && alpha < 1)
845 return true;
846 }
847 return false;
848 };
849 auto settings = auditSettings();
850 settings.stopAtFirstClass = false;
851 int moved = 0, off_skin = 0;
853 for (const auto &edge : frontEdges(model))
854 for (double angle : {-30., -15., 15., 30.})
855 for (const auto &c : tools.crackExtensionCandidates(
856 model.mesh, model.crack, edge, direction(model, edge, angle),
857 settings)) {
858 if (norm(c.displacement) == 0 || !bumped.count(c.freeNode))
859 continue;
860 ++moved;
861 off_skin += !on_skin_edge(c.freeNode, c.displacement);
862 if (chosen.freeNode < 0)
863 chosen = c;
864 }
865 MOFEM_LOG("WORLD", Sev::inform)
866 << "Curved skin: " << moved << " candidates move a node of the "
867 << "curved side";
868 CHKERR testing.checkValue<TestType::SCALAR>("Curved skin moved nodes",
869 moved > 0, 1, {0, 0});
870 CHKERR testing.checkValue<TestType::SCALAR>("Curved skin nodes off edges",
871 off_skin, 0, {0, 0});
872 if (chosen.freeNode >= 0) {
873 // The applied move changes the volume by what the node's star gains.
874 const int w = chosen.freeNode;
875 auto moved_mesh = model.mesh;
876 for (int i = 0; i != 3; ++i)
877 moved_mesh.points[w][i] += chosen.displacement[i];
878 for (const auto &cell : model.mesh.cells)
879 if (std::count(cell.begin(), cell.end(), w))
880 model.volume += (det(moved_mesh, cell) - det(model.mesh, cell)) / 6;
881 tools.applyCrackExtension(model.mesh, model.crack, chosen);
882 CHKERR checkMesh(testing, model, "Curved skin step");
883 }
885}
886
887/** Import an indexed crack fixture and its protected facets on level zero. */
888MoFEMErrorCode createCrackFixture(MoFEM::Interface &field, const Model &model,
889 std::vector<EntityHandle> &vertices,
890 Range &tets, Range &crack_faces,
891 Range &protected_faces) {
893 auto &mb = field.get_moab();
894 for (const auto &point : model.mesh.points) {
895 EntityHandle v;
896 CHKERR mb.create_vertex(point.data(), v);
897 vertices.push_back(v);
898 }
899 std::vector<int> ids(vertices.size());
900 std::iota(ids.begin(), ids.end(), 1);
901 CHKERR mb.tag_set_data(mb.globalId_tag(), vertices.data(), vertices.size(),
902 ids.data());
903 Range geometry;
904 for (const auto &cell : model.mesh.cells) {
905 EntityHandle conn[4];
906 for (int n = 0; n != 4; ++n)
907 conn[n] = vertices[cell[n]];
908 EntityHandle t;
909 CHKERR mb.create_element(MBTET, conn, 4, t);
910 tets.insert(t);
911 }
912 geometry = tets;
913 for (int dim : {0, 1, 2}) {
915 CHKERR mb.get_adjacencies(tets, dim, true, closure, moab::Interface::UNION);
916 geometry.merge(closure);
917 }
918 for (int i : model.crack) {
919 EntityHandle conn[3];
920 for (int n = 0; n != 3; ++n)
921 conn[n] = vertices[model.mesh.boundary[i][n]];
922 Range face;
923 CHKERR mb.get_adjacencies(conn, 3, 2, false, face,
924 moab::Interface::INTERSECT);
925 crack_faces.merge(face);
926 }
927 auto bits = field.getInterface<BitRefManager>();
928 CHKERR bits->setBitRefLevel(geometry, BitRefLevel().set(0), false);
929 const int owner = 0;
930 CHKERR mb.tag_clear_data(
931 ParallelComm::get_pcomm(&mb, MYPCOMM_INDEX)->part_tag(), geometry,
932 &owner);
933 for (std::size_t i = 0; i != model.mesh.boundary.size(); ++i) {
934 if (std::find(model.crack.begin(), model.crack.end(),
935 static_cast<int>(i)) != model.crack.end())
936 continue;
937 EntityHandle conn[3];
938 for (int n = 0; n != 3; ++n)
939 conn[n] = vertices[model.mesh.boundary[i][n]];
940 Range face;
941 CHKERR mb.get_adjacencies(conn, 3, 2, false, face,
942 moab::Interface::INTERSECT);
943 protected_faces.merge(face);
944 }
946}
947
948/** 8: joining. Two fingers of crack leave a slit one element wide; the
949 * faces through its front edges whose third node is on the crack join it.
950 * The slit closes, the crack stays manifold, and no node moves. */
953 auto plane = grid(6, 4, 3);
954 for (int j = 0; j != 4; ++j)
955 for (int i = 0; i != 6; ++i)
956 for (int k = 0; k != 2; ++k)
957 plane.crack[2 * (j * 6 + i) + k] = i < 3 || (i < 5 && j != 1);
958 auto model = extrude(plane, {-2, -1, 0, 1, 2}, 2);
959 const auto points = model.mesh.points;
960 const auto joins = tools.crackJoinFaces(model.mesh, model.crack);
961 int closing = 0;
962 for (const auto &j : joins) {
964 model.mesh.boundary.push_back(j.face);
965 model.crack.push_back(model.mesh.boundary.size() - 1);
966 }
967 MOFEM_LOG("WORLD", Sev::inform) << "Join: " << joins.size() << " faces, "
968 << closing << " closing onto the front";
969 std::map<Edge, int> count;
970 int off_plane = 0;
971 for (int i : model.crack) {
972 const auto &f = model.mesh.boundary[i];
973 for (int v : f)
974 off_plane += model.mesh.points[v][2] != 0 || model.mesh.points[v][0] > 5;
975 for (const auto &e :
976 {edgeKey(f[0], f[1]), edgeKey(f[1], f[2]), edgeKey(f[0], f[2])})
977 ++count[e];
978 }
979 int non_manifold = 0;
980 for (const auto &[e, n] : count)
981 non_manifold += n > 2;
982 CHKERR testing.checkValue<TestType::SCALAR>("Join closes the slit",
983 joins.size(), 4, {0, 0});
984 CHKERR testing.checkValue<TestType::SCALAR>("Join faces close onto the front",
985 closing, 4, {0, 0});
986 CHKERR testing.checkValue<TestType::SCALAR>("Join crack faces",
987 model.crack.size(), 40, {0, 0});
988 CHKERR testing.checkValue<TestType::SCALAR>("Join faces in the slit",
989 off_plane, 0, {0, 0});
990 CHKERR testing.checkValue<TestType::SCALAR>("Join crack manifold",
991 non_manifold, 0, {0, 0});
992 CHKERR testing.checkValue<TestType::SCALAR>(
993 "Join moves no node", model.mesh.points == points, 1, {0, 0});
994 CHKERR checkMesh(testing, model, "Join");
995 // Nothing more joins a straight front.
996 CHKERR testing.checkValue<TestType::SCALAR>(
997 "Join is idempotent",
998 tools.crackJoinFaces(model.mesh, model.crack).size(), 0, {0, 0});
1000}
1001
1002/** 10: a front corner closed along the skin. In the crack layer, the
1003 * uncracked triangles (2, 0)-(3, 0)-(3, 1) and (3, 0)-(4, 0)-(3, 1) form a
1004 * corner at the interior node (3, 1). Its front edges end on the skin y = 0
1005 * at (2, 0) and (4, 0), joined only through the skin node (3, 0). Joining
1006 * closes the corner with both triangles, closing along the skin and then
1007 * closing onto the front; no edge gets a third crack face and the crack ends
1008 * on the skin. */
1011 constexpr int nx = 6;
1012 auto plane = grid(nx, 4, 3);
1013 const auto id = [](int i, int j) { return j * (nx + 1) + i; };
1014 // Square (i, j) holds triangles 2 (j nx + i) and the next one. Square
1015 // (3, 0) takes the other diagonal, so (3, 1) sees (2, 0), (3, 0), (4, 0).
1016 const int square = 2 * 3;
1017 plane.triangles[square] = {id(3, 0), id(4, 0), id(3, 1)};
1018 plane.triangles[square + 1] = {id(4, 0), id(4, 1), id(3, 1)};
1019 plane.crack[square + 1] = 1;
1020 plane.crack[2 * 2] = 0; // (2, 0)-(3, 0)-(3, 1)
1021 auto model = extrude(plane, {-2, -1, 0, 1, 2}, 2);
1022 const auto corner =
1023 std::set<Face>{faceKey(model.node(2, id(2, 0)), model.node(2, id(3, 0)),
1024 model.node(2, id(3, 1))),
1025 faceKey(model.node(2, id(3, 0)), model.node(2, id(4, 0)),
1026 model.node(2, id(3, 1)))};
1027 const auto joins = tools.crackJoinFaces(model.mesh, model.crack);
1028 std::vector<int> categories;
1029 int in_corner = 0;
1030 for (const auto &j : joins) {
1031 categories.push_back(j.category);
1032 in_corner += corner.count(faceKey(j.face[0], j.face[1], j.face[2]));
1033 model.mesh.boundary.push_back(j.face);
1034 model.crack.push_back(model.mesh.boundary.size() - 1);
1035 }
1036 std::map<Edge, int> count;
1037 for (int i : model.crack) {
1038 const auto &f = model.mesh.boundary[i];
1039 for (const auto &e :
1040 {edgeKey(f[0], f[1]), edgeKey(f[1], f[2]), edgeKey(f[0], f[2])})
1041 ++count[e];
1042 }
1043 int non_manifold = 0;
1044 for (const auto &[e, n] : count)
1045 non_manifold += n > 2;
1046 MOFEM_LOG("WORLD", Sev::inform)
1047 << "Skin corner: " << joins.size() << " join faces, " << in_corner
1048 << " in the corner";
1049 CHKERR testing.checkValue<TestType::SCALAR>(
1050 "Skin corner opened along the skin",
1051 categories.size() >= 2 &&
1054 1, {0, 0});
1055 CHKERR testing.checkValue<TestType::SCALAR>("Skin corner faces", in_corner, 2,
1056 {0, 0});
1057 CHKERR testing.checkValue<TestType::SCALAR>("Skin corner crack manifold",
1058 non_manifold, 0, {0, 0});
1059 CHKERR checkMesh(testing, model, "Skin corner");
1061}
1062
1063/** A join across an open notch must not enclose an unfillable hole. The
1064 * same face may connect two separate crack components without making a loop. */
1067 const auto euler = [](const Model &model) {
1068 std::set<int> vertices;
1069 std::set<Edge> edges;
1070 for (int i : model.crack) {
1071 const auto &f = model.mesh.boundary[i];
1072 vertices.insert(f.begin(), f.end());
1073 for (auto edge :
1074 {edgeKey(f[0], f[1]), edgeKey(f[1], f[2]), edgeKey(f[0], f[2])})
1075 edges.insert(edge);
1076 }
1077 return static_cast<int>(vertices.size()) - static_cast<int>(edges.size()) +
1078 static_cast<int>(model.crack.size());
1079 };
1080 for (const auto &[separate, depth] :
1081 std::vector<std::pair<bool, int>>{{false, 2}, {false, 5}, {true, 2}}) {
1082 const int ny = depth + 2;
1083 auto plane = grid(4, ny, 4);
1084 for (int j = 0; j != ny; ++j)
1085 for (int i = 0; i != 4; ++i)
1086 for (int k = 0; k != 2; ++k)
1087 plane.crack[2 * (j * 4 + i) + k] =
1088 separate ? i != 2 : !(i == 2 && j < depth);
1089 auto model = extrude(plane, {-2, -1, 0, 1, 2}, 2);
1090 const auto node = [&](int i, int j) { return model.node(2, j * 5 + i); };
1091 const auto target = faceKey(node(2, 1), node(3, 1), node(3, 2));
1092 // Other missing faces are protected: closing this notch cannot rely on
1093 // filling them later. Only the target is available to join the crack.
1094 for (std::size_t t = 0; t != plane.triangles.size(); ++t)
1095 if (!plane.crack[t]) {
1096 const auto &f = plane.triangles[t];
1097 const auto face = faceKey(model.node(2, f[0]), model.node(2, f[1]),
1098 model.node(2, f[2]));
1099 if (face != target)
1100 model.mesh.boundary.push_back(face);
1101 }
1102 const auto tag =
1103 separate ? std::string("Separate components")
1104 : std::string(depth == 2 ? "Open notch" : "Long open notch");
1105 CHKERR testing.checkValue<TestType::SCALAR>(
1106 tag + " initial Euler characteristic", euler(model), separate ? 2 : 1,
1107 {0, 0});
1108 model.mesh.featureEdges.push_back(edgeKey(node(2, 1), node(3, 1)));
1109 CHKERR testing.checkValue<TestType::SCALAR>(
1110 tag + " accepted feature fills",
1111 tools.crackFillFaces(model.mesh, model.crack).size(), separate ? 1 : 0,
1112 {0, 0});
1113 {
1114 moab::Core mb;
1115 MoFEM::CoreTmp<-1> field(mb);
1116 std::vector<EntityHandle> vertices;
1117 Range tets, crack_faces, protected_faces;
1118 CHKERR createCrackFixture(field, model, vertices, tets, crack_faces,
1119 protected_faces);
1120 const Edge edge = edgeKey(node(3, 1), node(3, 2));
1121 EntityHandle conn[2] = {vertices[edge[0]], vertices[edge[1]]};
1122 Range handles;
1123 CHKERR mb.get_adjacencies(conn, 2, 1, false, handles,
1124 moab::Interface::INTERSECT);
1125 auto local_settings = auditSettings();
1126 local_settings.ringLimit = 1;
1127 Range result;
1128 EntityHandle face = 0;
1129 CHKERR field.getInterface<CGALInterface>()->getCrackExtension().joinCrack(
1130 BitRefLevel().set(0), BitRefLevel().set(1), crack_faces,
1131 protected_faces, {{handles.front(), direction(model, edge, 0)}},
1132 local_settings, result, face);
1133 CHKERR testing.checkValue<TestType::SCALAR>(
1134 tag + " local bridge keeps remote crack connectivity", face != 0,
1135 separate, {0, 0});
1136 }
1137 const auto joins = tools.crackJoinFaces(model.mesh, model.crack);
1138 auto recovery_settings = auditSettings();
1139 const auto recovery_edge = edgeKey(node(3, 1), node(3, 2));
1140 const auto recoverable = tools.crackJoiningCandidates(
1141 model.mesh, model.crack, recovery_edge,
1142 direction(model, recovery_edge, 0), recovery_settings);
1143 const bool accepts_target = std::any_of(
1144 recoverable.begin(), recoverable.end(),
1146 return faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) == target;
1147 });
1148 CHKERR testing.checkValue<TestType::SCALAR>(
1149 tag + " recovery preserves hole guard", accepts_target, separate,
1150 {0, 0});
1151 CHKERR testing.checkValue<TestType::SCALAR>(
1152 tag + " accepted joins", joins.size(), separate ? 1 : 0, {0, 0});
1153 for (const auto &join : joins) {
1154 CHKERR testing.checkValue<TestType::SCALAR>(
1155 tag + " target face",
1156 faceKey(join.face[0], join.face[1], join.face[2]) == target, 1,
1157 {0, 0});
1158 model.mesh.boundary.push_back(join.face);
1159 model.crack.push_back(model.mesh.boundary.size() - 1);
1160 }
1161 CHKERR testing.checkValue<TestType::SCALAR>(
1162 tag + " final Euler characteristic", euler(model), 1, {0, 0});
1163 CHKERR checkMesh(testing, model, tag);
1164 }
1166}
1167
1168/** Reconstruct the block with deterministic general-position connectivity. */
1169Model irregularModel(const CGALTools &tools, Model irregular) {
1170 std::uint64_t state = 2463534242u;
1171 for (auto &point : irregular.mesh.points)
1172 if (point[0] > 0 && point[0] < 6 && point[1] > 0 && point[1] < 4 &&
1173 point[2] != 0 && std::abs(point[2]) < 2)
1174 for (double &coordinate : point) {
1175 state ^= state << 13;
1176 state ^= state >> 7;
1177 state ^= state << 17;
1178 coordinate += 0.2 * (double(state % 2001) / 1000. - 1.);
1179 }
1180 TetMeshData reconstructed;
1181 irregular.mesh.vertexIds.resize(irregular.mesh.points.size());
1182 std::iota(irregular.mesh.vertexIds.begin(), irregular.mesh.vertexIds.end(),
1183 1);
1184 tools.reconstructDelaunay(reconstructed, irregular.mesh);
1185 irregular.mesh = std::move(reconstructed);
1186 irregular.crack.clear();
1187 irregular.mesh.boundary.clear();
1188 irregular.mesh.surfaceLabels.clear();
1189 std::map<Face, int> incidence;
1190 irregular.volume = 0;
1191 for (const auto &cell : irregular.mesh.cells) {
1192 irregular.volume += det(irregular.mesh, cell) / 6;
1193 for (const auto &face : cellFaces(cell))
1194 ++incidence[face];
1195 }
1196 for (const auto &[face, count] : incidence) {
1197 bool crack_face = count == 2;
1198 for (int v : face)
1199 crack_face &=
1200 irregular.mesh.points[v][2] == 0 && irregular.mesh.points[v][0] <= 3;
1201 if (count == 1 || crack_face) {
1202 if (crack_face)
1203 irregular.crack.push_back(irregular.mesh.boundary.size());
1204 irregular.mesh.boundary.push_back(face);
1205 }
1206 }
1207 return irregular;
1208}
1209
1210/** One-step previews must be bounded and leave the supplied mesh untouched. */
1213 auto model = extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2);
1214 auto settings = auditSettings();
1215 settings.lookAheadCandidates = 4;
1216 settings.lookAheadJoin = false; // Keep the ordinary-continuation regression.
1217 const auto points = model.mesh.points;
1218 const auto cells = model.mesh.cells;
1219 const auto boundary = model.mesh.boundary;
1220 const auto crack = model.crack;
1221 int feasible = 0, alternatives = 0, trials = 0;
1222 for (const auto &edge : frontEdges(model)) {
1223 for (double angle : {0., 8., 20.}) {
1225 const auto candidates = tools.crackExtensionCandidates(
1226 model.mesh, model.crack, edge, direction(model, edge, angle),
1227 settings, &report);
1228 feasible += report.lookAheadFeasible;
1229 alternatives += report.alternativeTriangulations;
1230 trials += report.lookAheadTrials;
1231 MOFEM_LOG("WORLD", Sev::inform)
1232 << "Look-ahead edge " << edge[0] << ' ' << edge[1] << " angle "
1233 << angle << ": candidates " << candidates.size() << ", trials "
1234 << report.lookAheadTrials << ", feasible " << report.lookAheadFeasible
1235 << ", alternatives " << report.alternativeTriangulations;
1236 for (const auto &c : candidates)
1237 MOFEM_LOG("WORLD", Sev::inform)
1238 << "Preview node " << c.freeNode << ", state " << c.continuation
1239 << ", score " << c.score << ", operations " << c.operations;
1240 CHKERR testing.checkValue<TestType::SCALAR>(
1241 "Look-ahead bounded trials", report.lookAheadTrials <= 4, 1, {0, 0});
1242 if (!candidates.empty()) {
1243 auto result = model;
1245 candidates.front());
1246 CHKERR checkMesh(testing, result, "Look-ahead chosen extension");
1247 }
1248 }
1249 }
1250 CHKERR testing.checkValue<TestType::SCALAR>("Look-ahead evaluated candidates",
1251 trials > 0, 1, {0, 0});
1252 CHKERR testing.checkValue<TestType::SCALAR>(
1253 "Look-ahead feasible continuation", feasible > 0, 1, {0, 0});
1254 CHKERR testing.checkValue<TestType::SCALAR>(
1255 "Look-ahead source unchanged",
1256 model.mesh.points == points && model.mesh.cells == cells &&
1257 model.mesh.boundary == boundary && model.crack == crack,
1258 1, {0, 0});
1259 auto irregular = irregularModel(tools, model);
1260 int promoted = 0, unknown = 0;
1261 for (const auto &edge : frontEdges(irregular))
1262 for (double angle : {0., 8., 20.}) {
1264 const auto candidates = tools.crackExtensionCandidates(
1265 irregular.mesh, irregular.crack, edge,
1266 direction(irregular, edge, angle), settings, &report);
1267 alternatives += report.alternativeTriangulations;
1268 MOFEM_LOG("WORLD", Sev::inform)
1269 << "Irregular edge " << edge[0] << ' ' << edge[1] << " angle "
1270 << angle << ": candidates " << candidates.size() << ", feasible "
1271 << report.lookAheadFeasible << ", alternatives "
1272 << report.alternativeTriangulations;
1273 for (const auto &c : candidates)
1274 MOFEM_LOG("WORLD", Sev::inform)
1275 << "Irregular node " << c.freeNode << ", state " << c.continuation
1276 << ", score " << c.score << ", operations " << c.operations
1277 << ", open corners " << c.openCorners;
1278 if (!candidates.empty()) {
1279 const auto &chosen = candidates.front();
1280 for (const auto &c : candidates) {
1281 unknown +=
1283 promoted += chosen.continuation ==
1285 c.continuation != chosen.continuation &&
1286 c.candidateClass == chosen.candidateClass &&
1287 c.openCorners == chosen.openCorners &&
1288 c.score > chosen.score;
1289 }
1290 auto result = irregular;
1292 candidates.front());
1293 CHKERR checkMesh(testing, result, "Look-ahead irregular extension");
1294 if (chosen.continuation ==
1296 const auto target_normal = cross(sub(irregular.mesh.points[edge[1]],
1297 irregular.mesh.points[edge[0]]),
1298 direction(irregular, edge, angle));
1299 std::set<Edge> affected;
1300 auto faces = chosen.closureFaces;
1301 faces.push_back(chosen.newFace);
1302 for (const auto &face : faces)
1303 for (const auto &e :
1304 {edgeKey(face[0], face[1]), edgeKey(face[1], face[2]),
1305 edgeKey(face[0], face[2])})
1306 affected.insert(e);
1307 bool continued = false;
1308 auto next_settings = settings;
1309 next_settings.lookAheadCandidates = 0;
1310 next_settings.stopAtFirstClass = false;
1311 for (const auto &next : frontEdges(result)) {
1312 if (!affected.count(next))
1313 continue;
1314 auto d = cross(target_normal, sub(result.mesh.points[next[1]],
1315 result.mesh.points[next[0]]));
1316 if (!(norm(d) > 1e-8))
1317 continue;
1318 d = unit(d);
1319 for (int f : result.crack) {
1320 const auto &face = result.mesh.boundary[f];
1321 if (!std::count(face.begin(), face.end(), next[0]) ||
1322 !std::count(face.begin(), face.end(), next[1]))
1323 continue;
1324 for (int v : face)
1325 if (v != next[0] && v != next[1] &&
1326 dot(sub(result.mesh.points[v], result.mesh.points[next[0]]),
1327 d) > 0)
1328 for (double &x : d)
1329 x = -x;
1330 }
1331 if (dot(d, direction(irregular, edge, angle)) <= 1e-8)
1332 continue;
1333 const auto following = tools.crackExtensionCandidates(
1334 result.mesh, result.crack, next, d, next_settings);
1335 for (const auto &c : following)
1336 if (c.angleDeviationDegrees <= settings.angleToleranceDegrees) {
1337 tools.applyCrackExtension(result.mesh, result.crack, c);
1338 CHKERR checkMesh(testing, result,
1339 "Look-ahead actual second step");
1340 continued = true;
1341 break;
1342 }
1343 if (continued)
1344 break;
1345 }
1346 CHKERR testing.checkValue<TestType::SCALAR>(
1347 "Look-ahead prediction can be applied", continued, 1, {0, 0});
1348 }
1349 }
1350 }
1351 CHKERR testing.checkValue<TestType::SCALAR>(
1352 "Look-ahead alternative triangulations", alternatives > 0, 1, {0, 0});
1353 CHKERR testing.checkValue<TestType::SCALAR>(
1354 "Look-ahead promotes a continuable extension", promoted > 0, 1, {0, 0});
1355 CHKERR testing.checkValue<TestType::SCALAR>("Look-ahead cut patch is unknown",
1356 unknown > 0, 1, {0, 0});
1358}
1359
1360/** Join and closure previews must be real operations on the next front. */
1361MoFEMErrorCode lookAheadClosure(TestingInterface &testing,
1362 const CGALTools &tools) {
1364 using Candidate = CGALTools::CrackExtensionCandidate;
1365 auto slit = grid(6, 4, 3);
1366 for (int j = 0; j != 4; ++j)
1367 for (int i = 0; i != 6; ++i)
1368 for (int k = 0; k != 2; ++k)
1369 slit.crack[2 * (j * 6 + i) + k] = i < 3 || (i < 5 && j != 1);
1370 std::vector<Model> models{extrude(grid(6, 4, 3), {-2, -1, 0, 1, 2}, 2),
1371 extrude(notch(), {-2, -1, 0, 1, 2}, 2),
1372 extrude(slit, {-2, -1, 0, 1, 2}, 2)};
1373 auto settings = auditSettings();
1374 settings.lookAheadCandidates = 4;
1375 models.push_back(irregularModel(tools, models.front()));
1376 int joins = 0, closures = 0, preferred = 0, standalone = 0;
1377 const auto available = tools.crackJoinFaces(models[2].mesh, models[2].crack);
1378 CHKERR testing.checkValue<TestType::SCALAR>(
1379 "Join filter fixture has eligible faces", !available.empty(), 1, {0, 0});
1380 const Edge join_edge{available.front().face[0], available.front().face[1]};
1381 const auto filtered =
1382 tools.crackJoinFaces(models[2].mesh, models[2].crack, {join_edge},
1383 [](const Face &) { return false; });
1384 CHKERR testing.checkValue<TestType::SCALAR>(
1385 "Join direction filter can exclude faces", filtered.empty(), 1, {0, 0});
1386 for (const auto &model : models)
1387 for (const auto &edge : frontEdges(model))
1388 for (double angle : {0., 8., 20.}) {
1389 const auto points = model.mesh.points;
1390 const auto cells = model.mesh.cells;
1391 const auto boundary = model.mesh.boundary;
1392 const auto crack = model.crack;
1394 const auto candidates = tools.crackExtensionCandidates(
1395 model.mesh, model.crack, edge, direction(model, edge, angle),
1396 settings, &report);
1397 CHKERR testing.checkValue<TestType::SCALAR>(
1398 "Closure preview leaves source untouched",
1399 points == model.mesh.points && cells == model.mesh.cells &&
1400 boundary == model.mesh.boundary && crack == model.crack,
1401 1, {0, 0});
1402 for (const auto &chosen : candidates) {
1403 if (chosen.continuationKind != Candidate::JOIN &&
1404 chosen.continuationKind != Candidate::CLOSURE)
1405 continue;
1406 CHKERR testing.checkValue<TestType::SCALAR>(
1407 "Join or closure follows target direction",
1408 chosen.continuationAngleDegrees <= settings.angleToleranceDegrees,
1409 1, {0, 0});
1410 joins += chosen.continuationKind == Candidate::JOIN;
1411 closures += chosen.continuationKind == Candidate::CLOSURE;
1412 auto result = model;
1413 tools.applyCrackExtension(result.mesh, result.crack, chosen);
1414 const auto &face = chosen.continuationFace;
1415 const auto next = edgeKey(face[0], face[1]);
1416 const auto target_normal =
1417 cross(sub(model.mesh.points[edge[1]], model.mesh.points[edge[0]]),
1418 direction(model, edge, angle));
1419 auto d = unit(cross(target_normal, sub(result.mesh.points[next[1]],
1420 result.mesh.points[next[0]])));
1421 if (dot(sub(result.mesh.points[face[2]], result.mesh.points[next[0]]),
1422 d) < 0)
1423 for (double &x : d)
1424 x = -x;
1425 bool applied = false;
1426 if (chosen.continuationKind == Candidate::JOIN) {
1427 const auto joining = tools.crackJoinFaces(
1428 result.mesh, result.crack, {next}, [&](const Face &f) {
1429 return faceKey(f[0], f[1], f[2]) ==
1430 faceKey(face[0], face[1], face[2]);
1431 });
1432 if (!joining.empty()) {
1433 result.mesh.boundary.push_back(joining.front().face);
1434 result.crack.push_back(result.mesh.boundary.size() - 1);
1435 applied = true;
1436 }
1437 if (!applied) {
1438 auto future_settings = settings;
1439 future_settings.lookAheadCandidates = 0;
1440 const auto recovered = tools.crackJoiningCandidates(
1441 result.mesh, result.crack, next, d, future_settings);
1442 for (const auto &c : recovered)
1443 if (faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) ==
1444 faceKey(face[0], face[1], face[2])) {
1445 tools.applyCrackExtension(result.mesh, result.crack, c);
1446 applied = true;
1447 break;
1448 }
1449 }
1450 } else {
1451 auto future_settings = settings;
1452 future_settings.lookAheadCandidates = 0;
1453 future_settings.stopAtFirstClass = false;
1454 auto following = tools.crackClosureCandidates(
1455 result.mesh, result.crack, next, d, future_settings);
1456 const auto gap = std::find_if(
1457 following.begin(), following.end(), [&](const Candidate &c) {
1458 return faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) ==
1459 faceKey(face[0], face[1], face[2]);
1460 });
1461 standalone += gap != following.end();
1462 if (gap == following.end())
1463 following = tools.crackExtensionCandidates(
1464 result.mesh, result.crack, next, d, future_settings);
1465 for (const auto &c : following)
1466 if (faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) ==
1467 faceKey(face[0], face[1], face[2])) {
1468 tools.applyCrackExtension(result.mesh, result.crack, c);
1469 applied = true;
1470 break;
1471 }
1472 if (!applied) {
1473 future_settings.joinGapsOnly = true;
1474 const auto recovered = tools.crackJoiningCandidates(
1475 result.mesh, result.crack, next, d, future_settings);
1476 for (const auto &c : recovered)
1477 if (faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) ==
1478 faceKey(face[0], face[1], face[2])) {
1479 tools.applyCrackExtension(result.mesh, result.crack, c);
1480 applied = true;
1481 break;
1482 }
1483 }
1484 }
1485 CHKERR testing.checkValue<TestType::SCALAR>(
1486 "Predicted join or closure can be applied", applied, 1, {0, 0});
1487 CHKERR checkMesh(testing, result, "Actual future join or closure");
1488 }
1489 if (!candidates.empty())
1490 for (const auto &other : candidates) {
1491 const auto &best = candidates.front();
1492 preferred += best.candidateClass == other.candidateClass &&
1493 best.openCorners == other.openCorners &&
1494 best.continuation == other.continuation &&
1495 best.continuationKind > other.continuationKind &&
1496 best.score < other.score;
1497 }
1498 }
1499 MOFEM_LOG("WORLD", Sev::inform)
1500 << "Join preview: joins " << joins << ", closures " << closures
1501 << ", standalone " << standalone << ", promoted " << preferred;
1502 CHKERR testing.checkValue<TestType::SCALAR>("Existing joins previewed",
1503 joins > 0, 1, {0, 0});
1504 CHKERR testing.checkValue<TestType::SCALAR>("Gap closures previewed",
1505 closures > 0, 1, {0, 0});
1506 CHKERR testing.checkValue<TestType::SCALAR>(
1507 "Join or closure preference changes selection", preferred > 0, 1, {0, 0});
1509}
1510
1511/** Reverse an interior face to make a genuinely missing join, then recover it.
1512 * Both a gap within one crack and a join between components need flips. */
1513MoFEMErrorCode joinRecovery(TestingInterface &testing, const CGALTools &tools) {
1515 using Candidate = CGALTools::CrackExtensionCandidate;
1516 for (bool gap : {true, false}) {
1517 auto plane = grid(6, 4, 3);
1518 for (int j = 0; j != 4; ++j)
1519 for (int i = 0; i != 6; ++i)
1520 for (int k = 0; k != 2; ++k)
1521 plane.crack[2 * (j * 6 + i) + k] =
1522 gap ? (i < 3 || (i < 5 && j != 1)) : i != 3;
1523 auto model = extrude(plane, {-2, -1, 0, 1, 2}, 2);
1524 const auto joins = tools.crackJoinFaces(model.mesh, model.crack);
1525 const auto initial_front = frontEdges(model);
1526 Face target{};
1527 std::vector<int> incident;
1528 std::array<int, 2> apex{};
1529 bool prepared = false;
1530 for (const auto &join : joins) {
1531 Edge base{};
1532 bool on_front = false;
1533 for (const auto &edge : {edgeKey(join.face[0], join.face[1]),
1534 edgeKey(join.face[1], join.face[2]),
1535 edgeKey(join.face[0], join.face[2])})
1536 if (std::find(initial_front.begin(), initial_front.end(), edge) !=
1537 initial_front.end()) {
1538 base = edge;
1539 on_front = true;
1540 break;
1541 }
1542 if (!on_front)
1543 continue;
1544 if (gap && join.category != CGALTools::CrackJoinFace::ClosingFront)
1545 continue;
1546 target = faceKey(join.face[0], join.face[1], join.face[2]);
1547 incident.clear();
1548 for (std::size_t c = 0; c != model.mesh.cells.size(); ++c) {
1549 const auto faces = cellFaces(model.mesh.cells[c]);
1550 if (std::find(faces.begin(), faces.end(), target) != faces.end())
1551 incident.push_back(c);
1552 }
1553 if (incident.size() != 2)
1554 continue;
1555 for (int n = 0; n != 2; ++n)
1556 for (int v : model.mesh.cells[incident[n]])
1557 if (!std::count(target.begin(), target.end(), v))
1558 apex[n] = v;
1559 bool interior = true;
1560 for (int v : apex) {
1561 const auto &point = model.mesh.points[v];
1562 interior &= point[0] > 0 && point[0] < 6 && point[1] > 0 &&
1563 point[1] < 4 && std::abs(point[2]) < 2;
1564 }
1565 if (!interior)
1566 continue;
1567 auto trial = model;
1568 Vec centre{};
1569 for (int v : target)
1570 for (int d = 0; d != 3; ++d)
1571 centre[d] += model.mesh.points[v][d] / 3.;
1572 for (int v : apex)
1573 for (int d = 0; d != 2; ++d)
1574 trial.mesh.points[v][d] =
1575 0.9 * trial.mesh.points[v][d] + 0.1 * centre[d];
1576 bool valid = true;
1577 for (const auto &cell : trial.mesh.cells)
1578 valid &= det(trial.mesh, cell) > 0;
1579 std::vector<std::array<int, 4>> cells;
1580 for (std::size_t c = 0; c != trial.mesh.cells.size(); ++c)
1581 if (c != static_cast<std::size_t>(incident[0]) &&
1582 c != static_cast<std::size_t>(incident[1]))
1583 cells.push_back(trial.mesh.cells[c]);
1584 for (int n = 0; n != 3; ++n) {
1585 std::array<int, 4> cell{apex[0], apex[1], target[n],
1586 target[(n + 1) % 3]};
1587 if (det(trial.mesh, cell) < 0)
1588 std::swap(cell[2], cell[3]);
1589 valid &= det(trial.mesh, cell) > 0;
1590 cells.push_back(cell);
1591 }
1592 if (!valid)
1593 continue;
1594 trial.mesh.cells = std::move(cells);
1595 model = std::move(trial);
1596 target = {base[0], base[1], -1};
1597 for (int v : join.face)
1598 if (v != base[0] && v != base[1])
1599 target[2] = v;
1600 prepared = true;
1601 break;
1602 }
1603 CHKERR testing.checkValue<TestType::SCALAR>("Missing joining face fixture",
1604 prepared, 1, {0, 0});
1605 const auto key = faceKey(target[0], target[1], target[2]);
1606 int target_cells = 0;
1607 for (const auto &cell : model.mesh.cells)
1608 for (const auto &face : cellFaces(cell))
1609 target_cells += face == key;
1610 CHKERR testing.checkValue<TestType::SCALAR>(
1611 "Joining face is initially absent", target_cells, 0, {0, 0});
1612 auto settings = auditSettings();
1613 settings.joinGapsOnly = gap;
1614 const Edge edge = edgeKey(target[0], target[1]);
1615 const auto candidates = tools.crackJoiningCandidates(
1616 model.mesh, model.crack, edge, direction(model, edge, 0), settings);
1617 const auto selected = std::find_if(
1618 candidates.begin(), candidates.end(), [&](const Candidate &c) {
1619 return faceKey(c.newFace[0], c.newFace[1], c.newFace[2]) == key;
1620 });
1621 CHKERR testing.checkValue<TestType::SCALAR>(
1622 "Missing join recovered", selected != candidates.end(), 1, {0, 0});
1623 CHKERR testing.checkValue<TestType::SCALAR>(
1624 "Join recovery uses flips", selected->operations > 0, 1, {0, 0});
1625 CHKERR testing.checkValue<TestType::SCALAR>("Join recovery moves no vertex",
1626 norm(selected->displacement), 0,
1627 {0, 0});
1628 auto applied = model;
1629 tools.applyCrackExtension(applied.mesh, applied.crack, *selected);
1630 CHKERR checkMesh(testing, applied, "Recovered gap or join");
1631
1632 // Exercise publication on MoFEM levels as well as the indexed algorithm.
1633 moab::Core mb;
1634 MoFEM::CoreTmp<-1> core(mb);
1635 std::vector<EntityHandle> vertices;
1636 Range tets, crack_faces, protected_faces;
1637 CHKERR createCrackFixture(core, model, vertices, tets, crack_faces,
1638 protected_faces);
1639 auto bits = core.getInterface<BitRefManager>();
1640 EntityHandle conn[2] = {vertices[edge[0]], vertices[edge[1]]};
1641 Range edges;
1642 CHKERR mb.get_adjacencies(conn, 2, 1, false, edges,
1643 moab::Interface::INTERSECT);
1644 Range result;
1645 EntityHandle face = 0;
1646 Candidate published;
1647 CHKERR core.getInterface<CGALInterface>()->getCrackExtension().joinCrack(
1648 BitRefLevel().set(0), BitRefLevel().set(1), crack_faces,
1649 protected_faces, {{edges.front(), direction(model, edge, 0)}}, settings,
1650 result, face, &published);
1651 CHKERR testing.checkValue<TestType::SCALAR>("Flipped join published",
1652 face != 0, 1, {0, 0});
1653 CHKERR testing.checkValue<TestType::SCALAR>(
1654 "Published join has no move", norm(published.displacement), 0, {0, 0});
1655 Range source;
1656 CHKERR bits->getEntitiesByTypeAndRefLevel(
1657 BitRefLevel().set(0), BitRefLevel().set(), MBTET, source);
1658 CHKERR testing.checkValue<TestType::SCALAR>("Join keeps source cells",
1659 source == tets, 1, {0, 0});
1660 }
1662}
1663
1664} // namespace
1665
1666int main(int argc, char *argv[]) {
1667 MoFEM::Core::Initialize(&argc, &argv, nullptr, help);
1668 try {
1669 moab::Core mb_instance;
1670 moab::Interface &moab = mb_instance;
1671 MoFEM::Core core(moab);
1673 auto testing = core.getInterface<TestingInterface>();
1675 CHKERR testing->addTestCommandLine("", 1, "flat",
1676 [&]() { return flat(*testing, tools); });
1677 CHKERR testing->addTestCommandLine(
1678 "", 2, "kinked", [&]() { return kinked(*testing, tools); });
1679 CHKERR testing->addTestCommandLine(
1680 "", 3, "notch", [&]() { return notchTest(*testing, tools); });
1681 CHKERR testing->addTestCommandLine(
1682 "", 4, "boundary", [&]() { return boundary(*testing, tools); });
1683 CHKERR testing->addTestCommandLine(
1684 "", 5, "failure", [&]() { return failure(*testing, tools); });
1685 CHKERR testing->addTestCommandLine(
1686 "", 6, "jitter", [&]() { return jitter(*testing, tools); });
1687 CHKERR testing->addTestCommandLine("", 7, "hole",
1688 [&]() { return hole(*testing, tools); });
1689 CHKERR testing->addTestCommandLine("", 8, "join",
1690 [&]() { return join(*testing, tools); });
1691 CHKERR testing->addTestCommandLine(
1692 "", 9, "curved_skin", [&]() { return curvedSkin(*testing, tools); });
1693 CHKERR testing->addTestCommandLine(
1694 "", 10, "skin_corner", [&]() { return skinCorner(*testing, tools); });
1695 CHKERR testing->addTestCommandLine(
1696 "", 11, "no_hole_join", [&]() { return noHoleJoin(*testing, tools); });
1697 CHKERR testing->addTestCommandLine(
1698 "", 12, "look_ahead", [&]() { return lookAhead(*testing, tools); });
1699 CHKERR testing->addTestCommandLine("", 13, "look_ahead_closure", [&]() {
1700 return lookAheadClosure(*testing, tools);
1701 });
1702 CHKERR testing->addTestCommandLine("", 14, "join_recovery", [&]() {
1703 return joinRecovery(*testing, tools);
1704 });
1705 CHKERR testing->runTests();
1706 }
1709 return 0;
1710}
CGALInterfaceImpl & owner
CGALTools & tools
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
TetMeshData result
CGALTools::CrackExtensionCandidate candidate
std::function< bool(size_t, size_t)> preferred
Definition CGALTools.cpp:62
const Triangles & triangles
Definition CGALTools.cpp:60
bool selected
Definition CGALTools.cpp:97
size_t best
Definition CGALTools.cpp:63
Report & report
FaceSet skin
std::vector< int > source
const Settings & settings
int boundary
bool joining
const std::vector< Vec > * points
FaceSet crack
std::vector< Face > closures
Faces closing non-convex front corners.
bool angle
Radians between candidate face and target plane.
WorkMesh base
WorkMesh mesh
std::vector< int > vertices
int w
Free node.
bool fixed
Face newFace
Vec t
Removal: the ring edge.
bool closure
The face (f0, f1, w) closes a corner at f0 or f1.
Edge closing
Its edge allowed to join two crack nodes.
std::vector< Cell > cells
bool bridging
int operations
ParentChildKey key
moab::Interface & mb
Tag tag
const Field * target
ProjectedField & field
PyGILState_STATE state
Definition SDFPython.cpp:13
It end
Definition Schur.cpp:2568
std::string name
static char help[]
int main()
constexpr double a
#define CATCH_ERRORS
Catch errors.
#define MYPCOMM_INDEX
default communicator number PCOMM
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
MoFEMErrorCode setBitRefLevel(const Range &ents, const BitRefLevel bit, const bool only_tets=true, int verb=0, Range *adj_ents_ptr=nullptr) const
Add entities to database and set bit ref level.
#define MOFEM_LOG(channel, severity)
Log.
SeverityLevel
Severity levels.
FTensor::Index< 'i', SPACE_DIM > i
PetscBool testing
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'l', 3 > l
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
const FTensor::Tensor2< T, Dim, Dim > Vec
auto cross(const Tensor1_Expr< A, T, 3, i > &a, const Tensor1_Expr< B, U, 3, j > &b, const Index< k, 3 > &)
Definition cross.hpp:10
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
int r
Definition sdf.py:205
Managing BitRefLevels.
Continuation continuation
UNKNOWN includes a preview cut by the patch boundary or not evaluated.
std::vector< std::array< int, 3 > > closureFaces
std::array< int, 3 > newFace
(f0, f1, freeNode).
int freeNode
Patch point index of the free node.
int openCorners
Non-convex corners at new edges left open.
bool audit
Validate the region after every operation.
@ ClosingFront
Closes onto another front edge; manifold, no pinch.
void applyCrackExtension(TetMeshData &patch, std::vector< int > &crack_faces, const CrackExtensionCandidate &candidate) const
std::vector< int > reconstructDelaunay(TetMeshData &result, const TetMeshData &vertices, ReconstructionMethod method=ReconstructionMethod::PointDelaunay) const
std::vector< std::array< int, 3 > > crackFillFaces(const TetMeshData &patch, const std::vector< int > &crack_faces) const
std::vector< CrackExtensionCandidate > crackClosureCandidates(const TetMeshData &patch, const std::vector< int > &crack_faces, std::array< int, 2 > front_edge, std::array< double, 3 > direction, const CrackExtensionSettings &settings, CrackExtensionReport *report=nullptr) const
std::vector< CrackExtensionCandidate > crackJoiningCandidates(const TetMeshData &patch, const std::vector< int > &crack_faces, std::array< int, 2 > front_edge, std::array< double, 3 > direction, const CrackExtensionSettings &settings, CrackExtensionReport *report=nullptr) const
std::vector< CrackExtensionCandidate > crackExtensionCandidates(const TetMeshData &patch, const std::vector< int > &crack_faces, std::array< int, 2 > front_edge, std::array< double, 3 > direction, const CrackExtensionSettings &settings, CrackExtensionReport *report=nullptr) const
std::vector< CrackJoinFace > crackJoinFaces(const TetMeshData &patch, const std::vector< int > &crack_faces, const std::vector< std::array< int, 2 > > &front_order={}, const std::function< bool(const std::array< int, 3 > &)> &accept_face={}) const
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
Deprecated interface functions.
std::vector< std::array< int, 3 > > boundary
Definition CGALTools.hpp:20
std::vector< std::array< double, 3 > > points
Definition CGALTools.hpp:17
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.