v0.16.3
Loading...
Searching...
No Matches
cgal_generation.cpp
Go to the documentation of this file.
1/** \file cgal_generation.cpp
2 * \brief Implicit sphere/shell geometry and persistent-ID reconstruction.
3 */
4#include <MoFEM.hpp>
5#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
6#include <CGAL/squared_distance_3.h>
7#include <cmath>
8using namespace MoFEM;
9
10namespace {
11double determinant(const std::array<double, 3> &a,
12 const std::array<double, 3> &b,
13 const std::array<double, 3> &c) {
14 return a[0] * (b[1] * c[2] - b[2] * c[1]) -
15 a[1] * (b[0] * c[2] - b[2] * c[0]) +
16 a[2] * (b[0] * c[1] - b[1] * c[0]);
17}
18
22 auto bridge = m_field.getInterface<CGALInterface>();
23 auto &tools = bridge->getTools();
25 settings.cellSize = settings.facetSize = .35;
26 auto radius = [](const auto &x) {
27 return std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
28 };
29 for (bool shell : {false, true}) {
30 MOFEM_LOG("WORLD", Sev::inform)
31 << "Generating " << (shell ? "shell" : "sphere");
34 mesh,
35 [&](const auto &x) {
36 return shell ? std::max(radius(x) - 1., .45 - radius(x))
37 : radius(x) - 1.;
38 },
39 settings);
40 double volume = 0;
41 for (const auto &cell : mesh.cells) {
42 std::array<double, 3> a, b, c;
43 for (int d = 0; d != 3; ++d) {
44 a[d] = mesh.points[cell[1]][d] - mesh.points[cell[0]][d];
45 b[d] = mesh.points[cell[2]][d] - mesh.points[cell[0]][d];
46 c[d] = mesh.points[cell[3]][d] - mesh.points[cell[0]][d];
47 }
48 const double v = determinant(a, b, c) / 6.;
49 if (v <= 0)
50 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
51 "Nonpositive generated cell");
52 volume += v;
53 if (shell) {
54 std::array<double, 3> p;
55 for (int d = 0; d != 3; ++d)
56 p[d] = -mesh.points[cell[0]][d];
57 const double den = 6 * v;
58 const double u = determinant(p, b, c) / den;
59 const double w = determinant(a, p, c) / den;
60 const double z = determinant(a, b, p) / den;
61 if (u >= 0 && w >= 0 && z >= 0 && u + w + z <= 1)
62 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
63 "Mesher filled the cavity");
64 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
65 const K::Point_3 origin(0, 0, 0);
66 for (int omit = 0; omit != 4; ++omit) {
67 std::vector<K::Point_3> face;
68 for (int d = 0; d != 4; ++d)
69 if (d != omit) {
70 const auto &x = mesh.points[cell[d]];
71 face.emplace_back(x[0], x[1], x[2]);
72 }
73 const auto distance = CGAL::squared_distance(
74 origin, K::Triangle_3(face[0], face[1], face[2]));
75 if (distance < std::pow(.45 - settings.facetDistance, 2))
76 SETERRQ(
77 PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
78 "Generated cell penetrates the cavity beyond facet tolerance");
79 }
80 }
81 }
82 const double expected =
83 4. * std::acos(-1.) / 3. * (shell ? 1. - std::pow(.45, 3) : 1.);
85 ->checkValue<TestType::SCALAR>(shell ? "SDF shell volume"
86 : "SDF sphere volume",
87 volume, expected, {0., .08});
88 if (mesh.boundary.empty())
89 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
90 "Incorrect implicit-domain volume");
91 for (const auto &face : mesh.boundary)
92 for (int i : face) {
93 const double r = radius(mesh.points[i]);
94 const double error = shell
95 ? std::min(std::abs(r - 1), std::abs(r - .45))
96 : std::abs(r - 1);
97 if (error > .002)
98 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
99 "Boundary is off the SDF");
100 }
101 Range entities;
102 EntityHandle domain, surface;
103 // Generated point ordinals are not persistent IDs in this MOAB database.
104 mesh.vertexIds.clear();
105 CHKERR bridge->importMesh(mesh, BitRefLevel().set(shell ? 2 : 0), entities,
106 domain, surface);
107 if (entities.subset_by_type(MBVERTEX).size() != mesh.points.size())
108 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
109 "Mesh import lost vertices");
110 if (!shell) {
111 CHKERR bridge->exportMesh(entities.subset_by_type(MBTET), mesh);
112 for (auto &point : mesh.points)
113 point[0] *= 2.;
114 TetMeshData reconstructed;
115 tools.reconstructDelaunay(reconstructed, mesh);
116 if (reconstructed.points != mesh.points ||
117 reconstructed.vertexIds != mesh.vertexIds)
118 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
119 "Reconstruction changed vertices");
120 }
121 }
122 TetMeshData invalid;
123 bool rejected = false;
124 try {
126 invalid,
127 [](const auto &) { return std::numeric_limits<double>::quiet_NaN(); },
128 settings);
129 } catch (const std::exception &) {
130 rejected = true;
131 }
132 if (!rejected || !invalid.cells.empty())
133 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
134 "Non-finite SDF was accepted");
136}
137} // namespace
138
139int main(int argc, char *argv[]) {
140 MoFEM::Core::Initialize(&argc, &argv, nullptr, "Implicit mesh checks\n");
141 try {
142 moab::Core mb;
143 MoFEM::Core core(mb);
144 CHKERR run(core);
145 }
148 return 0;
149}
CGALTools & tools
double distance
Definition CGALTools.cpp:64
const Settings & settings
int boundary
const std::vector< Vec > * points
WorkMesh mesh
int w
Free node.
std::vector< Cell > cells
moab::Interface & mb
int main()
constexpr double a
#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
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MOFEM_LOG(channel, severity)
Log.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
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
MoFEMErrorCode TestingInterface::checkValue< TestType::SCALAR >(const std::string &check_name, PetscReal actual, PetscReal expected, Tolerance tolerance, Sev error_severity, Sev success_severity) const
int r
Definition sdf.py:205
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
void createImplicitMesh(TetMeshData &result, const std::function< double(const std::array< double, 3 > &)> &sdf, const ImplicitMeshSettings &settings) const
std::vector< int > reconstructDelaunay(TetMeshData &result, const TetMeshData &vertices, ReconstructionMethod method=ReconstructionMethod::PointDelaunay) 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, 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
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.