v0.16.3
Loading...
Searching...
No Matches
mesh_parentage.cpp
Go to the documentation of this file.
1/** \file mesh_parentage.cpp
2 * \brief Topological ancestry from vertex classification, independent of CGAL.
3 */
4#include <MoFEM.hpp>
5
6using namespace MoFEM;
7
8namespace {
9
10BitRefLevel level(int n) { return BitRefLevel().set(n); }
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> MoFEMErrorCode rejects(F operation) {
21 CHKERR PetscPushErrorHandler(PetscReturnErrorHandler, nullptr);
22 const auto error = operation();
23 CHKERR PetscPopErrorHandler();
24 CHKERR require(error == MOFEM_DATA_INCONSISTENCY,
25 "Invalid ancestry was not rejected");
27}
28
29MoFEMErrorCode makeEntity(moab::Interface &mb, EntityType type,
30 std::initializer_list<EntityHandle> vertices,
31 EntityHandle &entity) {
33 ReadUtilIface *reader;
34 CHKERR mb.query_interface(reader);
35 EntityHandle *conn;
36 CHKERR reader->get_element_connect(1, static_cast<int>(vertices.size()), type,
37 0, entity, conn);
38 std::copy(vertices.begin(), vertices.end(), conn);
39 CHKERR reader->update_adjacencies(entity, 1,
40 static_cast<int>(vertices.size()), conn);
42}
43
44MoFEMErrorCode getEntity(moab::Interface &mb, int dim,
45 std::initializer_list<EntityHandle> vertices,
46 EntityHandle &entity) {
48 Range matches;
49 CHKERR mb.get_adjacencies(vertices.begin(), vertices.size(), dim, false,
50 matches, moab::Interface::INTERSECT);
51 CHKERR require(matches.size() == 1, "Fixture has ambiguous topology");
52 entity = matches.front();
54}
55
56MoFEMErrorCode checkParent(MoFEM::Interface &m_field, EntityHandle child,
57 EntityHandle expected) {
59 EntityHandle parent;
60 CHKERR m_field.get_moab().tag_get_data(
62 1, &parent);
63 CHKERR require(parent == expected, "Incorrect persistent parent tag");
64 const auto *entities = m_field.get_ref_ents();
65 const auto it = entities->find(child);
66 CHKERR require(it != entities->end() && (*it)->getParentEnt() == expected,
67 "Refinement entity is missing or has an incorrect parent");
68 const auto &index = entities->get<Ent_Ent_mi_tag>();
69 const auto range = index.equal_range(expected);
70 const auto found =
71 std::find_if(range.first, range.second,
72 [&](const auto &e) { return e->getEnt() == child; });
73 CHKERR require(found != range.second, "Parent multi-index was not updated");
74 const auto *elements = m_field.get_ref_finite_elements();
75 const auto element = elements->find(child);
76 CHKERR require(element != elements->end() &&
77 (*element)->getParentEnt() == expected,
78 "Refinement element has an incorrect parent");
80}
81
84 auto &mb = m_field.get_moab();
85 auto bits = m_field.getInterface<BitRefManager>();
86 auto refinement = m_field.getInterface<MeshRefinement>();
87 double scale = 0;
88 PetscBool found = PETSC_FALSE;
89 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-parentage_scale", &scale,
90 &found);
91 CHKERR require(found && std::isfinite(scale) && scale > 0,
92 "JSON must supply a positive parentage_scale");
93
94 const std::vector<std::array<double, 3>> x{
95 {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {.25, 0, 0},
96 {.75, 0, 0}, {0, .25, 0}, {.25, .25, 0}, {.125, .125, .125}};
97 ReadUtilIface *reader;
98 CHKERR mb.query_interface(reader);
99 EntityHandle start;
100 std::vector<double *> arrays;
101 CHKERR reader->get_node_coords(3, x.size(), 0, start, arrays);
102 for (size_t n = 0; n != x.size(); ++n)
103 for (int d = 0; d != 3; ++d)
104 arrays[d][n] = scale * x[n][d];
105 const EntityHandle a = start + 4, b = start + 5, c = start + 6;
106 const EntityHandle f = start + 7, v = start + 8;
107 EntityHandle tet;
108 CHKERR makeEntity(mb, MBTET, {start, start + 1, start + 2, start + 3}, tet);
109 CHKERR bits->setBitRefLevel(Range(tet, tet), level(0));
111 CHKERR bits->getEntitiesByRefLevel(level(0), BitRefLevel().set(), source);
112 EntityHandle ab, ac, abc;
113 CHKERR getEntity(mb, 1, {start, start + 1}, ab);
114 CHKERR getEntity(mb, 1, {start, start + 2}, ac);
115 CHKERR getEntity(mb, 2, {start, start + 1, start + 2}, abc);
116 std::map<EntityHandle, EntityHandle> vertex_parents{
117 {a, ab}, {b, ab}, {c, ac}, {f, abc}, {v, tet}};
118 std::map<EntityHandle, EntityHandle> expected = vertex_parents;
119 EntityHandle split_edge, face_edge, volume_edge, boundary_face, interior_face;
120 EntityHandle retained_edge, child_tet;
121 CHKERR makeEntity(mb, MBEDGE, {a, b}, split_edge);
122 CHKERR makeEntity(mb, MBEDGE, {a, c}, face_edge);
123 CHKERR makeEntity(mb, MBEDGE, {f, v}, volume_edge);
124 CHKERR makeEntity(mb, MBEDGE, {start, a}, retained_edge);
125 CHKERR makeEntity(mb, MBTRI, {a, f, c}, boundary_face);
126 CHKERR makeEntity(mb, MBTRI, {a, c, v}, interior_face);
127 CHKERR makeEntity(mb, MBTET, {a, f, c, v}, child_tet);
128 expected.insert({{split_edge, ab},
129 {face_edge, abc},
130 {volume_edge, tet},
131 {retained_edge, ab},
132 {boundary_face, abc},
133 {interior_face, tet},
134 {child_tet, tet}});
135 Range children(start + 4, start + 8);
136 children.insert(start);
137 for (const auto &[child, parent] : expected)
138 children.insert(child);
139 for (int dim = 2; dim >= 1; --dim) {
140 Range adjacent;
141 for (int upper = dim + 1; upper <= 3; ++upper) {
142 const auto entities = children.subset_by_dimension(upper);
143 CHKERR mb.get_adjacencies(entities, dim, true, adjacent,
144 moab::Interface::UNION);
145 }
146 children.merge(adjacent);
147 }
148 // Register first to exercise the indexed-parent update, including a repeat.
149 CHKERR bits->setBitRefLevel(children, BitRefLevel(), false);
150 EntityHandle retained_parent;
151 CHKERR mb.tag_get_data(
153 1, &retained_parent);
154 for (int repeat = 0; repeat != 2; ++repeat) {
155 CHKERR refinement->setParents(source, children, vertex_parents, level(1));
156 for (const auto &[child, parent] : expected)
157 CHKERR checkParent(m_field, child, parent);
158 CHKERR checkParent(m_field, start, retained_parent);
159 }
160
161 // Existing BC transfer must find the children through the parent index.
162 EntityHandle face_set, edge_set;
163 CHKERR mb.create_meshset(MESHSET_SET, face_set);
164 CHKERR mb.create_meshset(MESHSET_SET, edge_set);
165 CHKERR mb.add_entities(face_set, &abc, 1);
166 CHKERR mb.add_entities(edge_set, &ab, 1);
167 for (auto meshset : {face_set, edge_set})
168 CHKERR bits->updateMeshsetByEntitiesChildren(meshset, level(1), meshset,
169 MBMAXTYPE, false);
170 Range face_members, edge_members;
171 CHKERR mb.get_entities_by_handle(face_set, face_members);
172 CHKERR mb.get_entities_by_handle(edge_set, edge_members);
173 CHKERR require(face_members.find(boundary_face) != face_members.end() &&
174 face_members.find(face_edge) != face_members.end() &&
175 edge_members.find(split_edge) != edge_members.end() &&
176 edge_members.find(a) != edge_members.end() &&
177 edge_members.find(b) != edge_members.end(),
178 "Meshset transfer missed descendants");
179
180 // A moved output vertex changes its classification; source coordinates stay
181 // intact and entries must leave the old parent-index bucket.
182 const double moved[] = {.125 * scale, .125 * scale, 0};
183 CHKERR mb.set_coords(&a, 1, moved);
184 vertex_parents[a] = abc;
185 CHKERR refinement->setParents(source, children, vertex_parents, level(1));
186 CHKERR checkParent(m_field, a, abc);
187 CHKERR checkParent(m_field, split_edge, abc);
188 const auto &parent_index = m_field.get_ref_ents()->get<Ent_Ent_mi_tag>();
189 const auto old_bucket = parent_index.equal_range(ab);
190 for (auto it = old_bucket.first; it != old_bucket.second; ++it)
191 CHKERR require((*it)->getEnt() != a && (*it)->getEnt() != split_edge,
192 "Changed child remains in its previous parent bucket");
193
194 auto missing = vertex_parents;
195 missing.erase(v);
196 CHKERR rejects([&] {
197 return refinement->setParents(source, children, missing, level(2));
198 });
199 CHKERR checkParent(m_field, a, abc);
200 Range unpublished;
201 CHKERR bits->getEntitiesByRefLevel(level(2), BitRefLevel().set(),
202 unpublished);
203 CHKERR require(unpublished.empty(),
204 "Failed ancestry published an output level");
205
206 // A duplicate face on another level must not enter this source selection.
207 EntityHandle duplicate, face_child;
208 CHKERR makeEntity(mb, MBTRI, {start, start + 1, start + 2}, duplicate);
209 CHKERR bits->setEntitiesBitRefLevel(Range(duplicate, duplicate), level(3));
210 CHKERR makeEntity(mb, MBTRI, {start, start + 1, start + 2}, face_child);
211 Range face_children(start, start + 2);
212 face_children.insert(face_child);
213 CHKERR refinement->setParents(source, face_children, {}, level(4));
214 CHKERR checkParent(m_field, face_child, abc);
215 Range ambiguous_source = source;
216 ambiguous_source.insert(duplicate);
217 CHKERR rejects([&] {
218 return refinement->setParents(ambiguous_source, face_children, {},
219 level(2));
220 });
221 CHKERR checkParent(m_field, face_child, abc);
222
223 // Without source edges/faces/volumes there is no parent for a new face.
224 CHKERR rejects([&] {
225 return refinement->setParents(source.subset_by_type(MBVERTEX),
226 face_children, {}, level(2));
227 });
228 CHKERR refinement->setParents({}, {}, {}, level(5));
230}
231
232} // namespace
233
234int main(int argc, char **argv) {
235 MoFEM::Core::Initialize(&argc, &argv, nullptr,
236 "Topological parentage test\n");
237 try {
238 moab::Core mb;
239 MoFEM::Core core(mb, PETSC_COMM_WORLD);
240 CHKERR run(core);
241 MOFEM_LOG("WORLD", Sev::inform) << "Topological parentage checks passed";
242 }
245 return 0;
246}
std::map< EntityHandle, int > index
std::vector< int > source
std::vector< int > vertices
std::vector< int > parent
moab::Interface & mb
std::string type
It it
Definition Schur.cpp:2568
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
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
@ F
virtual const RefEntity_multiIndex * get_ref_ents() const =0
Get the ref ents object.
virtual const RefElement_multiIndex * get_ref_finite_elements() const =0
Get the ref finite elements object.
#define MOFEM_LOG(channel, severity)
Log.
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
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
double scale
Definition plastic.cpp:124
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
Managing BitRefLevels.
Tag get_th_RefParentHandle() 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.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.