6#include <CGAL/version.h>
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())
31 CHKERR PetscPushErrorHandler(capture, &diagnostic);
32 const auto error = operation();
33 CHKERR PetscPopErrorHandler();
34 if (error != expected)
36 "Unexpected CGAL transfer error: got %d, expected %d: %s", error,
37 expected, diagnostic.c_str());
47 for (
auto face :
faces) {
49 CHKERR mb.get_adjacencies(&face, 1, 3,
false, adjacent);
50 adjacent = intersect(adjacent,
cells);
51 if (adjacent.size() != 1)
54 CHKERR mb.side_number(adjacent.front(), face, side, sense, offset);
55 CHKERR require(sense == 1,
"CGAL output skin is not outward oriented");
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}};
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;
75 CHKERR checkSkin(
mb, original.subset_by_type(MBTET));
76 const auto vertices = original.subset_by_type(MBVERTEX);
79 CHKERR mb.get_adjacencies(internal.data(), 3, 2,
false, face,
80 moab::Interface::INTERSECT);
81 const EntityHandle *conn;
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");
89 mesh.boundary.resize(4);
91 CHKERR checkSkin(
mb, output.subset_by_type(MBTET));
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());
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");
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;
125 return moab::MB_FAILURE;
127 return moab::Core::tag_set_data(
tag, entities, count, data);
133 PublicationFailureMoab
mb;
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;
145 CHKERR checkSkin(
mb, first.subset_by_type(MBTET));
146 mesh.vertexIds.clear();
149 "Generated mesh was stitched to a coincident existing mesh");
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");
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);
164 return bridge->reconstructDelaunay(first.subset_by_type(MBTET),
168 CHKERR mb.get_entities_by_handle(0, after);
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),
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");
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);
187 CHKERR bridge->exportMesh(output.subset_by_type(MBTET), indexed);
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);
195 return bridge->importMesh(
mesh, level(5), output, volume,
surface);
198 CHKERR mb.get_entities_by_handle(0, after);
199 CHKERR require(before == after,
"Exhausted-ID import changed the database");
204 EntityHandle expected) {
211 "CGAL export assigned an incorrect parent");
213 const auto range =
index.equal_range(expected);
214 CHKERR require(std::find_if(range.first, range.second,
216 return e->getEnt() == child;
218 "CGAL export did not update the parent index");
230 ReadUtilIface *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)
238 CHKERR reader->get_element_connect(1, 4, MBTET, 0, tet, conn);
239 for (
int n = 0;
n != 4; ++
n)
241 CHKERR reader->update_adjacencies(tet, 1, 4, conn);
242 CHKERR bits->setBitRefLevel(
Range(tet, tet), level(10));
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};
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));
260 const std::array<std::array<double, 3>, 5>
points{{{.25, 0, 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(
269 const std::array<EntityHandle, 5> expected{ab.front(), ab.front(), ac.front(),
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]);
279 CHKERR require(current == output,
280 "Repeated ancestry export duplicated entities");
282 for (
auto child : subtract(current,
source)) {
288 "Exported child has no selected source parent");
289 if (
mb.type_from_handle(child) == MBTET)
290 CHKERR checkParent(m_field, child, tet);
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));
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");
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);
315 CHKERR require(before == after && rejected_level.empty(),
316 "Failed vertex classification changed the MOAB mesh");
328 PetscBool found = PETSC_FALSE;
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) {
342 input_vertices.insert(
v);
344 CHKERR bits->setEntitiesBitRefLevel(input_vertices, level(0));
345 CHKERR bridge->moabToCgal(level(0));
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");
352 CHKERR bridge->getLinkedVertices(level(0), linked);
353 CHKERR require(linked == input_vertices,
"Import lost MOAB identities");
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);
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");
371 auto inserted =
mesh->tr.insert(
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;
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");
386 for (
auto tet : new_tets) {
387 const EntityHandle *conn;
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)) /
399 CHKERR require(signed_volume > 0,
"Exported tetrahedron is inverted");
400 volume += signed_volume;
402 CHKERR testing->checkValue<TestType::SCALAR>(
"CGAL exported volume", volume,
408 CHKERR require(preserved == old_tets,
"Export modified the old mesh level");
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));
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());
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");
430 duplicates.push_back(
points.front());
431 bool rejected =
false;
433 bridge->getTools().createDelaunay(*other, duplicates);
437 CHKERR require(rejected && other->
tr.number_of_vertices() == 4,
438 "Coincident points were merged or replaced the valid mesh");
439 EntityHandle duplicate;
442 coincident.insert(origin);
443 coincident.insert(duplicate);
444 CHKERR bits->setEntitiesBitRefLevel(coincident, level(5));
445 CHKERR rejects([&] {
return bridge->moabToCgal(level(5)); },
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));
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;
469 MBVERTEX, generated_vertices);
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");
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");
491#if CGAL_VERSION_NR >= 1060300000
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}};
502 std::map<std::array<int, 3>,
int> counts;
504 for (
int d = 0;
d != 4; ++
d) {
505 std::array<int, 3> face;
507 for (
int j = 0;
j != 4; ++
j)
510 std::sort(face.begin(), face.end());
513 mesh.boundary.clear();
514 mesh.surfaceLabels.clear();
515 for (
const auto &[face, count] : counts)
517 mesh.boundary.push_back(face);
518 mesh.surfaceLabels.push_back(1);
522 source.boundary.push_back({0, 1, 2});
523 source.surfaceLabels.push_back(2);
530 parents.size() == output.
boundary.size() &&
533 "Constrained recovery did not recover the missing internal "
534 "face with unchanged vertices");
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});
553 const auto base_triangles = [&] {
554 return std::count_if(
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; });
563 base_triangles() == 4,
564 "Constrained reconstruction lost a patch-interior vertex");
565 junction.
boundary.push_back({0, 8, 9});
568 bool crack_found =
false;
569 for (
size_t i = 0;
i != output.
boundary.size(); ++
i)
572 std::sort(face.begin(), face.end());
573 crack_found |= face == std::array<int, 3>{0, 8, 9};
581 "Constrained reconstruction erased the crack/exterior junction");
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};
596 for (
auto origin : {std::array<int, 2>{0, 0}, {1, 0}, {0, 1}}) {
597 std::array<int, 3>
order{0, 1, 2};
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) {
604 cell[
d + 1] = vertex(x[0], x[1], x[2]);
606 concave.
cells.push_back(cell);
607 }
while (std::next_permutation(
order.begin(),
order.end()));
611 const auto split = concave.
cells.front();
612 const int middle = concave.
points.size();
613 std::array<double, 3> centre{};
615 for (
int d = 0;
d != 3; ++
d)
617 concave.
points.push_back(centre);
618 concave.
vertexIds.push_back(100 + middle);
619 for (
int d = 0;
d != 4; ++
d) {
623 concave.
cells.front() = child;
625 concave.
cells.push_back(child);
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) {
635 for (
int d = 0;
d != 3; ++
d)
636 centre[
d] += x[
i][
d] / 4;
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)
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.;
652 CHKERR require(std::abs(volume - 3) < 1e-12 &&
655 "Non-convex reconstruction changed the physical domain or "
661 source.points[4][2] = -1.;
662 source.cells = {{0, 1, 3, 4}, {1, 2, 3, 4}, {2, 0, 3, 4}};
664 source.featureEdges = {{3, 4}};
666 bool rejected =
false;
670 rejected = std::string(error.
what()).find(
"required additional points") !=
676 "Strict Steiner rejection changed the result or diagnostic");
682 EntityHandle volume_set, surface_set;
683 CHKERR bridge->importMesh(
source, level(0), original, volume_set,
687 CHKERR mb.get_entities_by_handle(0, before);
690 return bridge->reconstructDelaunay(original, level(1),
result,
691 Method::ConstrainedStrict);
694 CHKERR mb.get_entities_by_handle(0, after);
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),
707int main(
int argc,
char **argv) {
716#if CGAL_VERSION_NR >= 1060300000
717 CHKERR constrainedReconstruction();
719 MOFEM_LOG(
"WORLD", Sev::inform) <<
"CGAL transfer checks passed";
Opt-in CGAL types; not included by MoFEM.hpp.
std::map< EntityHandle, int > index
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
std::vector< int > source
const std::vector< Vec > * points
std::vector< int > vertices
std::vector< int > parent
std::vector< Cell > cells
std::map< EntityHandle, std::pair< bool, std::vector< double > > > previous
std::vector< double > empty
One value per component.
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_ATOM_TEST_INVALID
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
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
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.
implementation of Data Operators for Forces and Sources
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
MoFEMErrorCode run(MoFEM::Interface &m_field)
Tag get_th_RefParentHandle() const
virtual moab::Interface & get_moab()=0
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Deprecated interface functions.
const char * what() const
Mesh refinement interface.
std::vector< std::array< int, 3 > > boundary
std::vector< std::array< int, 4 > > cells
Zero-based point indices.
std::vector< std::array< double, 3 > > points
std::vector< int > vertexIds
std::vector< int > surfaceLabels
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.