v0.16.3
Loading...
Searching...
No Matches
cgal_interface.cpp
Go to the documentation of this file.
1/** \file cgal_interface.cpp
2 * \brief Delaunay transfer, identity and mesh-level isolation checks.
3 */
4#include <MoFEM.hpp>
6#include <CGAL/version.h>
7
8using namespace MoFEM;
9
10namespace {
11
12MoFEMErrorCode require(bool valid, const char *message) {
14 if (!valid)
15 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID, "%s", message);
17}
18
19template <typename F>
20MoFEMErrorCode rejects(F operation, MoFEMErrorCode expected) {
22 std::string diagnostic;
23 const auto capture = [](MPI_Comm, int, const char *, const char *,
24 PetscErrorCode error, PetscErrorType,
25 const char *message, void *context) {
26 auto &text = *static_cast<std::string *>(context);
27 if (message && text.empty())
28 text = message;
29 return error;
30 };
31 CHKERR PetscPushErrorHandler(capture, &diagnostic);
32 const auto error = operation();
33 CHKERR PetscPopErrorHandler();
34 if (error != expected)
35 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
36 "Unexpected CGAL transfer error: got %d, expected %d: %s", error,
37 expected, diagnostic.c_str());
39}
40
41BitRefLevel level(int n) { return BitRefLevel().set(n); }
42
43MoFEMErrorCode checkSkin(moab::Interface &mb, const Range &cells) {
46 CHKERR mb.get_adjacencies(cells, 2, false, faces, moab::Interface::UNION);
47 for (auto face : faces) {
48 Range adjacent;
49 CHKERR mb.get_adjacencies(&face, 1, 3, false, adjacent);
50 adjacent = intersect(adjacent, cells);
51 if (adjacent.size() != 1)
52 continue;
53 int side, sense, offset;
54 CHKERR mb.side_number(adjacent.front(), face, side, sense, offset);
55 CHKERR require(sense == 1, "CGAL output skin is not outward oriented");
56 }
58}
59
60MoFEMErrorCode skinOrientation() {
62 moab::Core mb;
63 MoFEM::CoreTmp<-1> core(mb);
64 auto bridge = core.getInterface<CGALInterface>();
66 mesh.points = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, -1}};
67 mesh.vertexIds = {1, 2, 3, 4, 5};
68 mesh.cells = {{0, 1, 2, 3}, {0, 1, 2, 4}};
69 // Both windings occur; the first triangle is an internal interface.
70 mesh.boundary = {{0, 1, 2}, {0, 1, 3}, {1, 2, 3}, {0, 2, 3},
71 {0, 1, 4}, {1, 2, 4}, {0, 2, 4}};
72 Range original, output;
73 EntityHandle volume, surface;
74 CHKERR bridge->importMesh(mesh, level(0), original, volume, surface);
75 CHKERR checkSkin(mb, original.subset_by_type(MBTET));
76 const auto vertices = original.subset_by_type(MBVERTEX);
77 std::array<EntityHandle, 3> internal{vertices[0], vertices[1], vertices[2]};
78 Range face;
79 CHKERR mb.get_adjacencies(internal.data(), 3, 2, false, face,
80 moab::Interface::INTERSECT);
81 const EntityHandle *conn;
82 int count;
83 CHKERR mb.get_connectivity(face.front(), conn, count, false);
84 CHKERR require(std::equal(internal.begin(), internal.end(), conn),
85 "Import reversed an internal interface");
86
87 // The old lower tetrahedron is still in MOAB, but not on output level 1.
88 mesh.cells.resize(1);
89 mesh.boundary.resize(4);
90 CHKERR bridge->importMesh(mesh, level(1), output, volume, surface);
91 CHKERR checkSkin(mb, output.subset_by_type(MBTET));
92
93 // Reverse a quadratic skin triangle and verify its edge-node permutation.
94 const auto triangle = face.front();
95 EntityHandle quadratic_set;
96 CHKERR mb.create_meshset(MESHSET_SET, quadratic_set);
97 CHKERR mb.add_entities(quadratic_set, &triangle, 1);
98 CHKERR mb.convert_entities(quadratic_set, true, false, false);
99 CHKERR mb.get_connectivity(triangle, conn, count, false);
100 CHKERR require(count == 6, "Expected a quadratic triangle fixture");
101 std::vector<EntityHandle> quadratic(conn, conn + count);
102 auto reversed = quadratic;
103 std::swap(reversed[1], reversed[2]);
104 std::swap(reversed[3], reversed[5]);
105 CHKERR mb.set_connectivity(triangle, reversed.data(), reversed.size());
106 CHKERR bridge->importMesh(mesh, level(2), output, volume, surface);
107 CHKERR checkSkin(mb, output.subset_by_type(MBTET));
108 CHKERR mb.get_connectivity(triangle, conn, count, false);
109 CHKERR require(count == 6 && std::equal(quadratic.begin(), quadratic.end(), conn),
110 "Skin orientation misplaced quadratic edge nodes");
112}
113
114// Inject one MOAB failure during ancestry publication, after import succeeds.
115struct PublicationFailureMoab : moab::Core {
116 using moab::Core::tag_set_data;
117 Tag parentTag = nullptr;
118 bool rejectParent = false, injected = false;
119 moab::ErrorCode tag_set_data(Tag tag, const EntityHandle *entities, int count,
120 const void *data) override {
121 if (rejectParent && tag == parentTag && count == 1 &&
122 *static_cast<const EntityHandle *>(data)) {
123 rejectParent = false;
124 injected = true;
125 return moab::MB_FAILURE;
126 }
127 return moab::Core::tag_set_data(tag, entities, count, data);
128 }
129};
130
131MoFEMErrorCode indexedIdentity() {
133 PublicationFailureMoab mb;
134 MoFEM::CoreTmp<-1> core(mb);
135 auto bridge = core.getInterface<CGALInterface>();
136 auto bits = core.getInterface<BitRefManager>();
138 mesh.points = {
139 {-1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {.1, .25, .4}, {.1, .25, -.4}};
140 mesh.cells = {{0, 1, 2, 3}, {0, 1, 2, 4}};
141 mesh.vertexIds = {11, 12, 13, 14, 15};
142 Range first, second, reused;
143 EntityHandle volume, surface;
144 CHKERR bridge->importMesh(mesh, level(0), first, volume, surface);
145 CHKERR checkSkin(mb, first.subset_by_type(MBTET));
146 mesh.vertexIds.clear();
147 CHKERR bridge->importMesh(mesh, level(1), second, volume, surface);
148 CHKERR require(intersect(first, second).empty(),
149 "Generated mesh was stitched to a coincident existing mesh");
150 TetMeshData indexed;
151 CHKERR bridge->exportMesh(second.subset_by_type(MBTET), indexed);
152 for (int id : indexed.vertexIds)
153 CHKERR require(id > 15, "Generated mesh reused an existing vertex ID");
154 CHKERR bridge->importMesh(indexed, level(2), reused, volume, surface);
155 CHKERR require(reused == second,
156 "Persistent-ID import did not reuse identity");
157
158 mb.parentTag = bits->get_th_RefParentHandle();
159 mb.rejectParent = true;
160 Range before, after, output = second, published;
161 CHKERR mb.get_entities_by_handle(0, before);
162 CHKERR rejects(
163 [&] {
164 return bridge->reconstructDelaunay(first.subset_by_type(MBTET),
165 level(3), output);
166 },
168 CHKERR mb.get_entities_by_handle(0, after);
169 CHKERR bits->getEntitiesByRefLevel(level(3), BitRefLevel().set(), published);
170 CHKERR require(
171 mb.injected && before == after && output == second && published.empty(),
172 "Failed reconstruction left entities, output or occupied bits");
173 CHKERR bridge->reconstructDelaunay(first.subset_by_type(MBTET), level(3),
174 output);
175 CHKERR checkSkin(mb, output.subset_by_type(MBTET));
176 CHKERR require(!output.subset_by_type(MBTET).empty(),
177 "Reconstruction could not reuse a rolled-back output bit");
178
179 // The last available IDs are valid; the following allocation must fail
180 // without wrapping or changing the database.
181 EntityHandle reserved;
182 const double far[] = {10, 10, 10};
183 CHKERR mb.create_vertex(far, reserved);
184 const int maximum = std::numeric_limits<int>::max() - mesh.points.size();
185 CHKERR mb.tag_set_data(mb.globalId_tag(), &reserved, 1, &maximum);
186 CHKERR bridge->importMesh(mesh, level(4), output, volume, surface);
187 CHKERR bridge->exportMesh(output.subset_by_type(MBTET), indexed);
188 CHKERR require(
189 *std::max_element(indexed.vertexIds.begin(), indexed.vertexIds.end()) ==
190 std::numeric_limits<int>::max(),
191 "Fresh IDs did not use the final available range");
192 CHKERR mb.get_entities_by_handle(0, before);
193 CHKERR rejects(
194 [&] {
195 return bridge->importMesh(mesh, level(5), output, volume, surface);
196 },
198 CHKERR mb.get_entities_by_handle(0, after);
199 CHKERR require(before == after, "Exhausted-ID import changed the database");
201}
202
203MoFEMErrorCode checkParent(MoFEM::Interface &m_field, EntityHandle child,
204 EntityHandle expected) {
206 EntityHandle parent;
207 CHKERR m_field.get_moab().tag_get_data(
209 1, &parent);
210 CHKERR require(parent == expected,
211 "CGAL export assigned an incorrect parent");
212 const auto &index = m_field.get_ref_ents()->get<Ent_Ent_mi_tag>();
213 const auto range = index.equal_range(expected);
214 CHKERR require(std::find_if(range.first, range.second,
215 [&](const auto &e) {
216 return e->getEnt() == child;
217 }) != range.second,
218 "CGAL export did not update the parent index");
220}
221
222MoFEMErrorCode runAncestry(MoFEM::Interface &m_field) {
224 auto &mb = m_field.get_moab();
225 auto bits = m_field.getInterface<BitRefManager>();
226 auto bridge = m_field.getInterface<CGALInterface>();
227 double scale;
228 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-cgal_test_scale", &scale,
229 nullptr);
230 ReadUtilIface *reader;
231 CHKERR mb.query_interface(reader);
232 EntityHandle start, tet, *conn;
233 std::vector<double *> arrays;
234 CHKERR reader->get_node_coords(3, 4, 0, start, arrays);
235 for (int n = 0; n != 4; ++n)
236 for (int d = 0; d != 3; ++d)
237 arrays[d][n] = n == d + 1 ? scale : 0;
238 CHKERR reader->get_element_connect(1, 4, MBTET, 0, tet, conn);
239 for (int n = 0; n != 4; ++n)
240 conn[n] = start + n;
241 CHKERR reader->update_adjacencies(tet, 1, 4, conn);
242 CHKERR bits->setBitRefLevel(Range(tet, tet), level(10));
244 CHKERR bits->getEntitiesByRefLevel(level(10), BitRefLevel().set(), source);
245 const EntityHandle ab_conn[] = {start, start + 1};
246 const EntityHandle ac_conn[] = {start, start + 2};
247 const EntityHandle abc_conn[] = {start, start + 1, start + 2};
248 Range ab, ac, abc;
249 CHKERR mb.get_adjacencies(ab_conn, 2, 1, false, ab,
250 moab::Interface::INTERSECT);
251 CHKERR mb.get_adjacencies(ac_conn, 2, 1, false, ac,
252 moab::Interface::INTERSECT);
253 CHKERR mb.get_adjacencies(abc_conn, 3, 2, false, abc,
254 moab::Interface::INTERSECT);
255 CHKERR require(ab.size() == 1 && ac.size() == 1 && abc.size() == 1,
256 "Invalid source tetrahedron topology");
257 CHKERR bridge->moabToCgal(level(10));
258 CGALMesh *mesh;
259 CHKERR bridge->getMesh(level(10), mesh);
260 const std::array<std::array<double, 3>, 5> points{{{.25, 0, 0},
261 {.75, 0, 0},
262 {0, .25, 0},
263 {.25, .25, 0},
264 {.125, .125, .125}}};
265 std::vector<CGALMesh::Delaunay::Vertex_handle> inserted;
266 for (const auto &p : points)
267 inserted.push_back(mesh->tr.insert(
268 CGALMesh::Kernel::Point_3(p[0] * scale, p[1] * scale, p[2] * scale)));
269 const std::array<EntityHandle, 5> expected{ab.front(), ab.front(), ac.front(),
270 abc.front(), tet};
271 Range output;
272 for (int repeat = 0; repeat != 2; ++repeat) {
273 CHKERR bridge->cgalToMoab(level(10), level(11));
274 for (size_t n = 0; n != inserted.size(); ++n)
275 CHKERR checkParent(m_field, inserted[n]->moabHandle, expected[n]);
276 Range current;
277 CHKERR bits->getEntitiesByRefLevel(level(11), BitRefLevel().set(), current);
278 if (repeat)
279 CHKERR require(current == output,
280 "Repeated ancestry export duplicated entities");
281 output = current;
282 for (auto child : subtract(current, source)) {
283 EntityHandle parent;
284 CHKERR mb.tag_get_data(
286 &child, 1, &parent);
287 CHKERR require(source.find(parent) != source.end(),
288 "Exported child has no selected source parent");
289 if (mb.type_from_handle(child) == MBTET)
290 CHKERR checkParent(m_field, child, tet);
291 }
292 }
293 EntityHandle meshset;
294 CHKERR mb.create_meshset(MESHSET_SET, meshset);
295 const EntityHandle face = abc.front();
296 CHKERR mb.add_entities(meshset, &face, 1);
297 CHKERR bits->setEntitiesBitRefLevel(Range(meshset, meshset), level(10));
298 CHKERR m_field.getInterface<MeshRefinement>()->refineMeshset(meshset,
299 level(11));
300 Range members;
301 CHKERR mb.get_entities_by_handle(meshset, members);
302 CHKERR require(members.find(inserted[3]->moabHandle) != members.end(),
303 "Boundary meshset did not inherit the inserted face vertex");
304
305 // Classification fails before allocating an outside vertex or publishing bits.
306 Range before;
307 CHKERR mb.get_entities_by_handle(0, before);
308 mesh->tr.move(inserted.back(), CGALMesh::Kernel::Point_3(-scale, 0, 0));
309 CHKERR rejects([&] { return bridge->cgalToMoab(level(10), level(12)); },
311 Range after, rejected_level;
312 CHKERR mb.get_entities_by_handle(0, after);
313 CHKERR bits->getEntitiesByRefLevel(level(12), BitRefLevel().set(),
314 rejected_level);
315 CHKERR require(before == after && rejected_level.empty(),
316 "Failed vertex classification changed the MOAB mesh");
318}
319
322 auto bridge = m_field.getInterface<CGALInterface>();
323 auto bits = m_field.getInterface<BitRefManager>();
324 auto testing = m_field.getInterface<TestingInterface>();
325 auto &mb = m_field.get_moab();
326
327 double scale = 0;
328 PetscBool found = PETSC_FALSE;
329 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-cgal_test_scale", &scale,
330 &found);
331 CHKERR require(found && std::isfinite(scale) && scale > 0,
332 "JSON must set a positive cgal_test_scale");
333 const std::vector<std::array<double, 3>> points{
334 {0, 0, 0}, {scale, 0, 0}, {0, scale, 0}, {0, 0, scale}};
335 Range input_vertices;
336 EntityHandle origin = 0;
337 for (const auto &p : points) {
338 EntityHandle v;
339 CHKERR mb.create_vertex(p.data(), v);
340 if (!origin)
341 origin = v;
342 input_vertices.insert(v);
343 }
344 CHKERR bits->setEntitiesBitRefLevel(input_vertices, level(0));
345 CHKERR bridge->moabToCgal(level(0));
346 CGALMesh *mesh;
347 CHKERR bridge->getMesh(level(0), mesh);
348 CHKERR require(mesh->tr.is_valid() && mesh->tr.number_of_vertices() == 4 &&
349 mesh->tr.number_of_finite_cells() == 1,
350 "Imported tetrahedron is not a valid Delaunay mesh");
351 Range linked;
352 CHKERR bridge->getLinkedVertices(level(0), linked);
353 CHKERR require(linked == input_vertices, "Import lost MOAB identities");
354
355 CHKERR bridge->cgalToMoab(level(0), level(1));
356 CHKERR bridge->cgalToMoab(level(0), level(1));
357 Range all_vertices, old_tets;
358 CHKERR mb.get_entities_by_type(0, MBVERTEX, all_vertices);
359 CHKERR mb.get_entities_by_type(0, MBTET, old_tets);
360 CHKERR checkSkin(mb, old_tets);
361 CHKERR require(all_vertices == input_vertices && old_tets.size() == 1,
362 "Repeated export duplicated existing entities");
363 CHKERR bridge->moabToCgal(level(1));
364 CHKERR bridge->cgalToMoab(level(1));
365 Range round_trip_tets;
366 CHKERR mb.get_entities_by_type(0, MBTET, round_trip_tets);
367 CHKERR require(round_trip_tets == old_tets,
368 "MOAB/CGAL round trip duplicated tetrahedra");
369
370 // An interior point subdivides the tetrahedron without changing its hull.
371 auto inserted = mesh->tr.insert(
372 CGALMesh::Kernel::Point_3(.2 * scale, .2 * scale, .2 * scale));
373 CHKERR require(inserted->moabHandle == 0,
374 "New CGAL vertex inherited a stale MOAB handle");
375 CHKERR bridge->cgalToMoab(level(0), level(2));
376 Range new_tets, new_vertices;
377 CHKERR bits->getEntitiesByTypeAndRefLevel(level(2), BitRefLevel().set(),
378 MBTET, new_tets);
379 CHKERR checkSkin(mb, new_tets);
380 CHKERR bits->getEntitiesByTypeAndRefLevel(level(2), BitRefLevel().set(),
381 MBVERTEX, new_vertices);
382 CHKERR require(new_tets.size() == 4 && new_vertices.size() == 5 &&
383 inserted->moabHandle,
384 "CGAL insertion was not transferred to MOAB");
385 double volume = 0;
386 for (auto tet : new_tets) {
387 const EntityHandle *conn;
388 int count;
389 CHKERR mb.get_connectivity(tet, conn, count, true);
390 std::array<double, 12> x;
391 CHKERR mb.get_coords(conn, count, x.data());
392 const double ax = x[3] - x[0], ay = x[4] - x[1], az = x[5] - x[2];
393 const double bx = x[6] - x[0], by = x[7] - x[1], bz = x[8] - x[2];
394 const double cx = x[9] - x[0], cy = x[10] - x[1], cz = x[11] - x[2];
395 const double signed_volume =
396 (ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) +
397 az * (bx * cy - by * cx)) /
398 6.;
399 CHKERR require(signed_volume > 0, "Exported tetrahedron is inverted");
400 volume += signed_volume;
401 }
402 CHKERR testing->checkValue<TestType::SCALAR>("CGAL exported volume", volume,
403 scale * scale * scale / 6.,
404 {1e-12, 1e-12});
405 Range preserved;
406 CHKERR bits->getEntitiesByTypeAndRefLevel(level(1), BitRefLevel().set(),
407 MBTET, preserved);
408 CHKERR require(preserved == old_tets, "Export modified the old mesh level");
409
410 CGALMesh *other;
411 CHKERR bridge->getMesh(level(1), other);
412 for (auto v = other->tr.finite_vertices_begin();
413 v != other->tr.finite_vertices_end(); ++v) {
414 if (v->moabHandle == origin) {
415 other->tr.move(v, CGALMesh::Kernel::Point_3(.1 * scale, 0, 0));
416 break;
417 }
418 }
419 CHKERR bridge->cgalToMoab(level(1), level(3));
420 std::array<double, 3> old_coords;
421 CHKERR mb.get_coords(&origin, 1, old_coords.data());
422 CHKERR require(old_coords == points.front(),
423 "Moving a CGAL vertex changed the original MOAB vertex");
424 CHKERR bridge->getLinkedVertices(level(1), linked);
425 CHKERR require(linked.size() == 4 && linked.find(origin) == linked.end(),
426 "Moved vertex was not relinked to a new MOAB vertex");
427
428 // Failed algorithm construction leaves the previous triangulation intact.
429 auto duplicates = points;
430 duplicates.push_back(points.front());
431 bool rejected = false;
432 try {
433 bridge->getTools().createDelaunay(*other, duplicates);
434 } catch (const MoFEMException &) {
435 rejected = true;
436 }
437 CHKERR require(rejected && other->tr.number_of_vertices() == 4,
438 "Coincident points were merged or replaced the valid mesh");
439 EntityHandle duplicate;
440 CHKERR mb.create_vertex(points.front().data(), duplicate);
441 Range coincident;
442 coincident.insert(origin);
443 coincident.insert(duplicate);
444 CHKERR bits->setEntitiesBitRefLevel(coincident, level(5));
445 CHKERR rejects([&] { return bridge->moabToCgal(level(5)); },
447 CHKERR rejects([&] { return bridge->moabToCgal(BitRefLevel()); },
449
450 // The mask excludes the origin, which also carries level 5.
451 CHKERR bridge->moabToCgal(level(0), BitRefLevel().set().reset(5));
452 CHKERR bridge->getMesh(level(0), mesh);
453 CHKERR require(mesh->tr.number_of_vertices() == 3,
454 "Import did not respect the level mask");
455 CHKERR bridge->cgalToMoab(level(0), level(4));
456 Range planar_tets;
457 CHKERR bits->getEntitiesByTypeAndRefLevel(level(4), BitRefLevel().set(),
458 MBTET, planar_tets);
459 CHKERR require(planar_tets.empty(), "Planar points created volume cells");
460 CHKERR bridge->moabToCgal(level(6));
461 CHKERR bridge->cgalToMoab(level(6), level(7));
462 CHKERR bridge->getMesh(level(6), other);
463 bridge->getTools().createDelaunay(*other, points);
464 CHKERR bridge->getLinkedVertices(level(6), linked);
465 CHKERR require(linked.empty(), "CGAL-only mesh unexpectedly has MOAB twins");
466 CHKERR bridge->cgalToMoab(level(6), level(8));
467 Range generated_vertices, generated_tets;
468 CHKERR bits->getEntitiesByTypeAndRefLevel(level(8), BitRefLevel().set(),
469 MBVERTEX, generated_vertices);
470 CHKERR bits->getEntitiesByTypeAndRefLevel(level(8), BitRefLevel().set(),
471 MBTET, generated_tets);
472 CHKERR require(generated_vertices.size() == 4 && generated_tets.size() == 1 &&
473 intersect(generated_vertices, input_vertices).empty(),
474 "CGAL-only mesh was not transferred to fresh MOAB entities");
475 TetMeshData generated, reconstructed;
476 CHKERR bridge->exportMesh(generated_tets, generated);
477 std::set<int> generated_ids;
478 for (int id : generated.vertexIds)
479 CHKERR require(id > 0 && generated_ids.insert(id).second,
480 "CGAL export omitted persistent vertex IDs");
481 bridge->getTools().reconstructDelaunay(reconstructed, generated);
482 CHKERR bridge->clear(level(0));
483 CHKERR rejects([&] { return bridge->getMesh(level(0), mesh); },
485 CHKERR bridge->getMesh(level(1), other);
486 CHKERR require(other->tr.number_of_vertices() == 4,
487 "Clearing one CGAL level destroyed another");
489}
490
491#if CGAL_VERSION_NR >= 1060300000
492MoFEMErrorCode constrainedReconstruction() {
494 using Method = CGALTools::ReconstructionMethod;
496 TetMeshData source, point, output;
497 source.points = {
498 {-1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {.1, .25, .4}, {.1, .25, -.4}};
499 source.vertexIds = {11, 12, 13, 14, 15};
500 source.cells = {{0, 1, 2, 3}, {0, 1, 2, 4}};
501 auto exterior = [](TetMeshData &mesh) {
502 std::map<std::array<int, 3>, int> counts;
503 for (const auto &cell : mesh.cells)
504 for (int d = 0; d != 4; ++d) {
505 std::array<int, 3> face;
506 int n = 0;
507 for (int j = 0; j != 4; ++j)
508 if (j != d)
509 face[n++] = cell[j];
510 std::sort(face.begin(), face.end());
511 ++counts[face];
512 }
513 mesh.boundary.clear();
514 mesh.surfaceLabels.clear();
515 for (const auto &[face, count] : counts)
516 if (count == 1) {
517 mesh.boundary.push_back(face);
518 mesh.surfaceLabels.push_back(1);
519 }
520 };
521 exterior(source);
522 source.boundary.push_back({0, 1, 2});
523 source.surfaceLabels.push_back(2);
525 const auto parents =
526 tools.reconstructDelaunay(output, source, Method::ConstrainedStrict);
527 CHKERR require(point.cells.size() == 3 && output.cells.size() == 2 &&
528 output.points == source.points &&
529 output.vertexIds == source.vertexIds &&
530 parents.size() == output.boundary.size() &&
531 std::count(output.surfaceLabels.begin(),
532 output.surfaceLabels.end(), 2) == 1,
533 "Constrained recovery did not recover the missing internal "
534 "face with unchanged vertices");
535
536 // A crack ends inside a flat exterior patch. The exterior triangles remain
537 // connected around its endpoint, but its seam must not disappear on grouping.
538 TetMeshData junction;
539 junction.points = {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
540 {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
541 {.5, .5, 0}, {.5, .5, .5}};
542 junction.vertexIds = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
543 const std::vector<std::array<int, 3>> skin{
544 {0, 1, 8}, {1, 2, 8}, {2, 3, 8}, {3, 0, 8}, {4, 5, 6},
545 {4, 6, 7}, {0, 1, 5}, {0, 5, 4}, {1, 2, 6}, {1, 6, 5},
546 {2, 3, 7}, {2, 7, 6}, {3, 0, 4}, {3, 4, 7}};
547 for (const auto &face : skin)
548 junction.cells.push_back({face[0], face[1], face[2], 9});
549 exterior(junction);
550 // An ordinary patch-interior source vertex must participate in the recovered
551 // surface too, even when no crack or curve explicitly protects it.
552 tools.reconstructDelaunay(output, junction, Method::ConstrainedStrict);
553 const auto base_triangles = [&] {
554 return std::count_if(
555 output.boundary.begin(), output.boundary.end(), [&](const auto &face) {
556 return std::find(face.begin(), face.end(), 8) != face.end() &&
557 std::all_of(face.begin(), face.end(),
558 [&](int v) { return output.points[v][2] == 0; });
559 });
560 };
561 CHKERR require(output.points == junction.points &&
562 output.vertexIds == junction.vertexIds &&
563 base_triangles() == 4,
564 "Constrained reconstruction lost a patch-interior vertex");
565 junction.boundary.push_back({0, 8, 9});
566 junction.surfaceLabels.push_back(2);
567 tools.reconstructDelaunay(output, junction, Method::ConstrainedStrict);
568 bool crack_found = false;
569 for (size_t i = 0; i != output.boundary.size(); ++i)
570 if (output.surfaceLabels[i] == 2) {
571 auto face = output.boundary[i];
572 std::sort(face.begin(), face.end());
573 crack_found |= face == std::array<int, 3>{0, 8, 9};
574 }
575 CHKERR require(
576 output.points == junction.points &&
577 output.vertexIds == junction.vertexIds && base_triangles() == 4 &&
578 crack_found &&
579 std::count(output.surfaceLabels.begin(), output.surfaceLabels.end(),
580 2) == 1,
581 "Constrained reconstruction erased the crack/exterior junction");
582
583 // Three unit cubes form an L prism. Its missing fourth cube lies inside the
584 // point-set convex hull; independent volume and centroid checks detect fill.
585 TetMeshData concave;
586 std::map<std::array<int, 3>, int> vertices;
587 auto vertex = [&](int x, int y, int z) {
588 const std::array<int, 3> key{x, y, z};
589 const auto [it, added] = vertices.emplace(key, concave.points.size());
590 if (added) {
591 concave.points.push_back({double(x), double(y), double(z)});
592 concave.vertexIds.push_back(100 + it->second);
593 }
594 return it->second;
595 };
596 for (auto origin : {std::array<int, 2>{0, 0}, {1, 0}, {0, 1}}) {
597 std::array<int, 3> order{0, 1, 2};
598 do {
599 std::array<int, 3> x{origin[0], origin[1], 0};
600 std::array<int, 4> cell;
601 cell[0] = vertex(x[0], x[1], x[2]);
602 for (int d = 0; d != 3; ++d) {
603 ++x[order[d]];
604 cell[d + 1] = vertex(x[0], x[1], x[2]);
605 }
606 concave.cells.push_back(cell);
607 } while (std::next_permutation(order.begin(), order.end()));
608 }
609 // Retain a volume-interior source point too. Splitting one old tetrahedron
610 // leaves its exterior unchanged and adds no new PLC feature.
611 const auto split = concave.cells.front();
612 const int middle = concave.points.size();
613 std::array<double, 3> centre{};
614 for (int v : split)
615 for (int d = 0; d != 3; ++d)
616 centre[d] += concave.points[v][d] / 4.;
617 concave.points.push_back(centre);
618 concave.vertexIds.push_back(100 + middle);
619 for (int d = 0; d != 4; ++d) {
620 auto child = split;
621 child[d] = middle;
622 if (d == 0)
623 concave.cells.front() = child;
624 else
625 concave.cells.push_back(child);
626 }
627 exterior(concave);
628 tools.reconstructDelaunay(output, concave, Method::ConstrainedStrict);
629 double volume = 0;
630 for (const auto &cell : output.cells) {
631 std::array<std::array<double, 3>, 4> x;
632 std::array<double, 3> centre{};
633 for (int i = 0; i != 4; ++i) {
634 x[i] = output.points[cell[i]];
635 for (int d = 0; d != 3; ++d)
636 centre[d] += x[i][d] / 4;
637 }
638 CHKERR require(
639 !(centre[0] > 1 && centre[1] > 1),
640 "Constrained reconstruction filled the excluded L-prism region");
641 for (int i = 1; i != 4; ++i)
642 for (int d = 0; d != 3; ++d)
643 x[i][d] -= x[0][d];
644 const double determinant =
645 x[1][0] * (x[2][1] * x[3][2] - x[2][2] * x[3][1]) -
646 x[1][1] * (x[2][0] * x[3][2] - x[2][2] * x[3][0]) +
647 x[1][2] * (x[2][0] * x[3][1] - x[2][1] * x[3][0]);
648 CHKERR require(determinant > 0,
649 "Constrained output has nonpositive volume");
650 volume += determinant / 6.;
651 }
652 CHKERR require(std::abs(volume - 3) < 1e-12 &&
653 output.points == concave.points &&
654 output.vertexIds == concave.vertexIds,
655 "Non-convex reconstruction changed the physical domain or "
656 "source identity");
657
658 // A missing edge requires Steiner points in CGAL's recovery. Rejection must
659 // retain both the algorithm result and the bridge's bit/output state.
660 source.points[3][2] = 1.;
661 source.points[4][2] = -1.;
662 source.cells = {{0, 1, 3, 4}, {1, 2, 3, 4}, {2, 0, 3, 4}};
663 exterior(source);
664 source.featureEdges = {{3, 4}};
665 const auto previous = output;
666 bool rejected = false;
667 try {
668 tools.reconstructDelaunay(output, source, Method::ConstrainedStrict);
669 } catch (const MoFEMException &error) {
670 rejected = std::string(error.what()).find("required additional points") !=
671 std::string::npos;
672 }
673 CHKERR require(rejected && output.cells == previous.cells &&
674 output.points == previous.points &&
675 output.vertexIds == previous.vertexIds,
676 "Strict Steiner rejection changed the result or diagnostic");
677 moab::Core mb;
678 MoFEM::CoreTmp<-1> core(mb);
679 auto bridge = core.getInterface<CGALInterface>();
680 auto bits = core.getInterface<BitRefManager>();
681 Range original, before, after, result, occupied;
682 EntityHandle volume_set, surface_set;
683 CHKERR bridge->importMesh(source, level(0), original, volume_set,
684 surface_set);
685 // Protect every source edge, including the missing axis edge.
686 result = original;
687 CHKERR mb.get_entities_by_handle(0, before);
688 CHKERR rejects(
689 [&] {
690 return bridge->reconstructDelaunay(original, level(1), result,
691 Method::ConstrainedStrict);
692 },
694 CHKERR mb.get_entities_by_handle(0, after);
695 CHKERR bits->getEntitiesByRefLevel(level(1), BitRefLevel().set(), occupied);
696 CHKERR require(
697 before == after && result == original && occupied.empty(),
698 "Strict rejection published partial entities or occupied its output bit");
699 CHKERR bridge->reconstructDelaunay(original.subset_by_type(MBTET), level(1),
700 result);
702}
703#endif
704
705} // namespace
706
707int main(int argc, char **argv) {
708 MoFEM::Core::Initialize(&argc, &argv, nullptr, "CGAL interface atom test\n");
709 try {
710 moab::Core mb;
711 MoFEM::Core core(mb, PETSC_COMM_WORLD);
712 CHKERR run(core);
713 CHKERR runAncestry(core);
714 CHKERR indexedIdentity();
715 CHKERR skinOrientation();
716#if CGAL_VERSION_NR >= 1060300000
717 CHKERR constrainedReconstruction();
718#endif
719 MOFEM_LOG("WORLD", Sev::inform) << "CGAL transfer checks passed";
720 }
723 return 0;
724}
Opt-in CGAL types; not included by MoFEM.hpp.
std::map< EntityHandle, int > index
CGALTools & tools
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
TetMeshData result
FaceSet skin
std::vector< int > source
const std::vector< Vec > * points
WorkMesh mesh
std::vector< int > vertices
std::vector< int > parent
std::vector< Cell > cells
ParentChildKey key
moab::Interface & mb
Tag tag
std::map< EntityHandle, std::pair< bool, std::vector< double > > > previous
std::vector< double > empty
One value per component.
It it
Definition Schur.cpp:2568
std::string text
int main()
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_MOAB_ERROR
Definition definitions.h:41
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
@ MOFEM_INVALID_DATA
Definition definitions.h:36
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int order
@ F
virtual const RefEntity_multiIndex * get_ref_ents() const =0
Get the ref ents object.
#define MOFEM_LOG(channel, severity)
Log.
FTensor::Index< 'i', SPACE_DIM > i
PetscBool testing
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'j', 3 > j
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
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
float offset
Definition sdf_python.py:3
double scale
Definition plastic.cpp:124
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
Managing BitRefLevels.
Tag get_th_RefParentHandle() const
std::vector< int > reconstructDelaunay(TetMeshData &result, const TetMeshData &vertices, ReconstructionMethod method=ReconstructionMethod::PointDelaunay) const
virtual moab::Interface & get_moab()=0
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.
Mesh refinement interface.
std::vector< std::array< int, 3 > > boundary
Definition CGALTools.hpp:20
std::vector< std::array< int, 4 > > cells
Zero-based point indices.
Definition CGALTools.hpp:19
std::vector< std::array< double, 3 > > points
Definition CGALTools.hpp:17
std::vector< int > vertexIds
Definition CGALTools.hpp:18
std::vector< int > surfaceLabels
Definition CGALTools.hpp:21
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.