v0.16.3
Loading...
Searching...
No Matches
MeshRefinement.cpp
Go to the documentation of this file.
1/** \file MeshRefinement.cpp
2 * \brief Mesh refinement interface
3 */
4
5
6
7#include <EntityRefine.hpp>
8
9namespace MoFEM {
10
11typedef multi_index_container<
12 boost::shared_ptr<RefElement>,
13 // ptrWrapperRefElement,
14 indexed_by<
15 ordered_unique<tag<Ent_mi_tag>,
17 EntityHandle, &RefElement::getEnt>>,
18 ordered_non_unique<
19 tag<Ent_Ent_mi_tag>,
20 const_mem_fun<RefElement::interface_type_RefEntity, EntityHandle,
22 ordered_non_unique<
23 tag<Composite_ParentEnt_And_BitsOfRefinedEdges_mi_tag>,
24 composite_key<
25 RefElement,
27 EntityHandle, &RefElement::getParentEnt>,
28 const_mem_fun<RefElement, int,
31
33MeshRefinement::query_interface(boost::typeindex::type_index type_index,
34 UnknownInterface **iface) const {
35 *iface = const_cast<MeshRefinement *>(this);
36 return 0;
37}
38
40 : cOre(const_cast<Core &>(core)) {}
41
43 const EntityHandle meshset, const BitRefLevel &bit, const bool recursive,
44 int verb, EntityHandle start_v) {
45 Interface &m_field = cOre;
46 moab::Interface &moab = m_field.get_moab();
48 Range edges;
49 CHKERR moab.get_entities_by_type(meshset, MBEDGE, edges, recursive);
50 if (edges.empty()) {
51 Range tets;
52 CHKERR moab.get_entities_by_type(meshset, MBTET, tets, recursive);
53 CHKERR moab.get_adjacencies(tets, 1, true, edges, moab::Interface::UNION);
54 if (tets.empty()) {
55 Range prisms;
56 CHKERR moab.get_entities_by_type(meshset, MBPRISM, prisms, recursive);
57 for (Range::iterator pit = prisms.begin(); pit != prisms.end(); pit++) {
58 const EntityHandle *conn;
59 int num_nodes;
60 CHKERR moab.get_connectivity(*pit, conn, num_nodes, true);
61 assert(num_nodes == 6);
62 //
63 Range edge;
64 CHKERR moab.get_adjacencies(&conn[0], 2, 1, true, edge);
65 assert(edge.size() == 1);
66 edges.insert(edge[0]);
67 edge.clear();
68 CHKERR moab.get_adjacencies(&conn[1], 2, 1, true, edge);
69 assert(edge.size() == 1);
70 edges.insert(edge[0]);
71 EntityHandle conn_edge2[] = {conn[2], conn[0]};
72 edge.clear();
73 CHKERR moab.get_adjacencies(conn_edge2, 2, 1, true, edge);
74 assert(edge.size() == 1);
75 edges.insert(edge[0]);
76 //
77 edge.clear();
78 CHKERR moab.get_adjacencies(&conn[3], 2, 1, true, edge);
79 assert(edge.size() == 1);
80 edges.insert(edge[0]);
81 edge.clear();
82 CHKERR moab.get_adjacencies(&conn[4], 2, 1, true, edge);
83 assert(edge.size() == 1);
84 edges.insert(edge[0]);
85 EntityHandle conn_edge8[] = {conn[5], conn[3]};
86 edge.clear();
87 CHKERR moab.get_adjacencies(conn_edge8, 2, 1, true, edge);
88 assert(edge.size() == 1);
89 edges.insert(edge[0]);
90 }
91 }
92 }
93 CHKERR addVerticesInTheMiddleOfEdges(edges, bit, verb, start_v);
95}
97 const Range &ents, const BitRefLevel &bit, int verb, EntityHandle start_v) {
98 Interface &m_field = cOre;
99 moab::Interface &moab = m_field.get_moab();
100 auto refined_ents_ptr = m_field.get_ref_ents();
102 auto miit =
103 refined_ents_ptr->get<Composite_EntType_and_ParentEntType_mi_tag>()
104 .lower_bound(boost::make_tuple(MBVERTEX, MBEDGE));
105 auto hi_miit =
106 refined_ents_ptr->get<Composite_EntType_and_ParentEntType_mi_tag>()
107 .upper_bound(boost::make_tuple(MBVERTEX, MBEDGE));
109 ref_parent_ents_view.insert(miit, hi_miit);
110
111 Range edges = ents.subset_by_type(MBEDGE);
112 if (verb >= VERBOSE) {
113 MOFEM_TAG_AND_LOG("WORLD", Sev::verbose, "MeshRefinemnt")
114 << "ref level " << bit << " nb. edges to refine " << edges.size();
115 }
116
117 std::array<std::vector<double>, 3> vert_coords;
118 for (auto &vc : vert_coords)
119 vc.reserve(edges.size());
120
121 std::vector<EntityHandle> parent_edge;
122 parent_edge.reserve(edges.size());
123
124 std::array<double, 9> coords;
126 &coords[0], &coords[1], &coords[2]};
128 &coords[3], &coords[4], &coords[5]};
130 &coords[6], &coords[7], &coords[8]};
131
132 FTensor::Index<'i', 3> i;
133
134 Range add_bit;
135 for (auto p_eit = edges.pair_begin(); p_eit != edges.pair_end(); ++p_eit) {
136 auto miit_view = ref_parent_ents_view.lower_bound(p_eit->first);
137
138 Range edge_having_parent_vertex;
139 if (miit_view != ref_parent_ents_view.end()) {
140 for (auto hi_miit_view = ref_parent_ents_view.upper_bound(p_eit->second);
141 miit_view != hi_miit_view; ++miit_view) {
142 edge_having_parent_vertex.insert(edge_having_parent_vertex.end(),
143 miit_view->get()->getParentEnt());
144 add_bit.insert(add_bit.end(), miit_view->get()->getEnt());
145 }
146 }
147
148 Range add_vertex_edges =
149 subtract(Range(p_eit->first, p_eit->second), edge_having_parent_vertex);
150
151 for (auto e : add_vertex_edges)
152 parent_edge.emplace_back(e);
153
154 for (auto e : add_vertex_edges) {
155
156 const EntityHandle *conn;
157 int num_nodes;
158 CHKERR moab.get_connectivity(e, conn, num_nodes, false);
159 if(num_nodes == 2) {
160 CHKERR moab.get_coords(conn, num_nodes, coords.data());
161 t_0(i) += t_1(i);
162 t_0(i) *= 0.5;
163 for (auto j : {0, 1, 2})
164 vert_coords[j].emplace_back(t_0(j));
165
166 } else if (num_nodes == 3) {
167 for (auto j : {0, 1, 2})
168 vert_coords[j].emplace_back(t_2(j));
169 } else {
170 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
171 "edge should have 2 or 3 nodes but have %d", num_nodes);
172 }
173 }
174 }
175
176 CHKERR m_field.getInterface<BitRefManager>()->addBitRefLevel(add_bit, bit);
177
178 if (!vert_coords[0].empty()) {
179 ReadUtilIface *read_util;
180 CHKERR moab.query_interface(read_util);
181 int num_nodes = vert_coords[0].size();
182 vector<double *> arrays_coord;
183 CHKERR read_util->get_node_coords(3, num_nodes, 0, start_v, arrays_coord);
184 Range verts(start_v, start_v + num_nodes - 1);
185 for (auto dd : {0, 1, 2}) {
186 std::copy(vert_coords[dd].begin(), vert_coords[dd].end(),
187 arrays_coord[dd]);
188 }
189 CHKERR moab.tag_set_data(cOre.get_th_RefParentHandle(), verts,
190 &*parent_edge.begin());
191 CHKERR m_field.getInterface<BitRefManager>()->setEntitiesBitRefLevel(
192 verts, bit, verb);
193 }
195}
196
198 const BitRefLevel &bit, int verb,
199 const bool debug) {
200 Interface &m_field = cOre;
201 moab::Interface &moab = m_field.get_moab();
203 Range tets;
204 CHKERR moab.get_entities_by_type(meshset, MBTET, tets, false);
205 CHKERR refineTets(tets, bit, verb, debug);
207}
208
210 const EntityHandle ent, const EntityHandle parent,
211 const RefEntity_multiIndex *ref_ents_ptr, MoFEM::Core &cOre) {
212 MoFEM::Interface &m_field = cOre;
214 auto it = ref_ents_ptr->find(ent);
215 if (it != ref_ents_ptr->end()) {
216 if (it->get()->getParentEnt() != parent && ent != parent) {
217 parentsToChange[ent] = parent;
218 }
219 } else {
220 if (ent != parent) {
221 CHKERR m_field.get_moab().tag_set_data(cOre.get_th_RefParentHandle(),
222 &ent, 1, &parent);
223 }
224 }
226}
227
229 const RefEntity_multiIndex *ref_ents_ptr) {
231 for (auto mit = parentsToChange.begin(); mit != parentsToChange.end();
232 ++mit) {
233 auto it = ref_ents_ptr->find(mit->first);
234 bool success = const_cast<RefEntity_multiIndex *>(ref_ents_ptr)
235 ->modify(it, RefEntity_change_parent(mit->second));
236 if (!success) {
237 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
238 "impossible to set parent");
239 }
240 }
242}
243
246 const Range &source_entities, const Range &child_entities,
247 const BitRefLevel &output_bit, int verb,
248 const std::map<EntityHandle, EntityHandle> &moved_vertices) {
249 MoFEM::Interface &m_field = cOre;
251 auto &mb = m_field.get_moab();
252 if (output_bit.none())
253 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
254 "Select a nonzero output level");
255 std::map<EntityHandle, Range> incidence;
256 std::map<EntityHandle, std::vector<int>> keys;
257 std::set<std::pair<EntityType, std::vector<int>>> unique_keys;
258 std::set<int> vertex_ids;
259 for (auto v : source_entities.subset_by_type(MBVERTEX)) {
260 int id;
261 CHKERR mb.tag_get_data(mb.globalId_tag(), &v, 1, &id);
262 if (id <= 0 || !vertex_ids.insert(id).second)
263 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
264 "Topological lineage needs unique positive source vertex IDs");
265 }
266 for (auto s : source_entities) {
267 if (mb.type_from_handle(s) == MBVERTEX)
268 continue;
269 const EntityHandle *conn;
270 int count;
271 CHKERR mb.get_connectivity(s, conn, count, true);
272 if ((mb.type_from_handle(s) != MBEDGE && mb.type_from_handle(s) != MBTRI &&
273 mb.type_from_handle(s) != MBTET) ||
274 count != moab::CN::Dimension(mb.type_from_handle(s)) + 1)
275 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "Expected source simplices");
276 auto &key = keys[s];
277 key.resize(count);
278 CHKERR mb.tag_get_data(mb.globalId_tag(), conn, count, key.data());
279 std::sort(key.begin(), key.end());
280 if (!unique_keys.emplace(mb.type_from_handle(s), key).second)
281 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
282 "Duplicate source simplex identity");
283 for (int n = 0; n != count; ++n) {
284 if (source_entities.find(conn[n]) == source_entities.end())
285 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
286 "Incomplete source closure");
287 incidence[conn[n]].insert(s);
288 }
289 }
290 std::map<EntityHandle, EntityHandle> parents;
291 for (const auto &[vertex, source] : moved_vertices)
292 if (child_entities.find(vertex) == child_entities.end() ||
293 source_entities.find(vertex) != source_entities.end() ||
294 mb.type_from_handle(vertex) != MBVERTEX ||
295 source_entities.find(source) == source_entities.end() ||
296 mb.type_from_handle(source) != MBVERTEX)
297 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
298 "A moved vertex must map a new child vertex to a source vertex");
299 for (auto child : subtract(child_entities, source_entities)) {
300 const auto type = mb.type_from_handle(child);
301 if (type == MBVERTEX) {
302 const auto moved = moved_vertices.find(child);
303 if (moved == moved_vertices.end())
304 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
305 "Reconstruction must retain the source vertices");
306 parents[child] = moved->second;
307 continue;
308 }
309 if (type != MBEDGE && type != MBTRI && type != MBTET)
310 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
311 "Reconstruction must retain the source vertices");
312 const EntityHandle *conn;
313 int count;
314 CHKERR mb.get_connectivity(child, conn, count, true);
315 Range common, candidates;
316 for (int n = 0; n != count; ++n) {
317 const auto moved = moved_vertices.find(conn[n]);
318 const auto it = incidence.find(moved == moved_vertices.end()
319 ? conn[n]
320 : moved->second);
321 if (it == incidence.end())
322 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
323 "Missing source incidence");
324 common = n ? intersect(common, it->second) : it->second;
325 candidates.merge(it->second.subset_by_type(MBTET));
326 }
327 for (int dim = moab::CN::Dimension(type); dim <= 3; ++dim) {
328 auto matches = common.subset_by_dimension(dim);
329 if (!matches.empty()) {
330 candidates = matches;
331 break;
332 }
333 }
334 if (candidates.empty())
335 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
336 "No topological parent candidate");
337 parents[child] =
338 *std::min_element(candidates.begin(), candidates.end(),
339 [&](auto a, auto b) { return keys[a] < keys[b]; });
340 }
341 SetParent set_parent;
342 for (const auto &[child, parent] : parents)
343 CHKERR set_parent(child, parent, m_field.get_ref_ents(), cOre);
344 CHKERR set_parent(m_field.get_ref_ents());
345 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevel(
346 child_entities, output_bit, false, verb);
348}
349
351 const Range &source_entities, const Range &child_entities,
352 const std::map<EntityHandle, EntityHandle> &vertex_parents,
353 const BitRefLevel &output_bit, int verb) {
354 MoFEM::Interface &m_field = cOre;
356 if (output_bit.none())
357 SETERRQ(m_field.get_comm(), MOFEM_INVALID_DATA,
358 "Parent assignment requires a nonzero output level");
359 auto &mb = m_field.get_moab();
360 const auto source_vertices = source_entities.subset_by_type(MBVERTEX);
361 const auto child_vertices = child_entities.subset_by_type(MBVERTEX);
362 std::map<EntityHandle, Range> vertex_sources;
363
364 auto get_corners = [&](EntityHandle entity, Range &corners) {
366 const auto type = mb.type_from_handle(entity);
367 if (type == MBVERTEX) {
368 corners.insert(entity);
369 } else {
370 if (type != MBEDGE && type != MBTRI && type != MBTET)
371 SETERRQ(m_field.get_comm(), MOFEM_NOT_IMPLEMENTED,
372 "Topological parent assignment supports linear simplices");
373 const EntityHandle *conn;
374 int count;
375 CHKERR mb.get_connectivity(entity, conn, count, false);
376 if (count != moab::CN::Dimension(type) + 1)
377 SETERRQ(m_field.get_comm(), MOFEM_NOT_IMPLEMENTED,
378 "Topological parent assignment requires linear connectivity");
379 corners.insert_list(conn, conn + count);
380 }
382 };
383
384 // Invert source connectivity; new vertices inherit the upward adjacency
385 // of their containing source entity, restricted to this source selection.
386 for (auto source : source_entities) {
387 Range corners;
388 CHKERR get_corners(source, corners);
389 if (!subtract(corners, source_vertices).empty())
390 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
391 "Source selection is missing bounding vertices");
392 for (auto vertex : corners)
393 vertex_sources[vertex].insert(source);
394 }
395 for (const auto &[vertex, parent] : vertex_parents) {
396 if (child_vertices.find(vertex) == child_vertices.end() ||
397 source_entities.find(vertex) != source_entities.end() ||
398 source_entities.find(parent) == source_entities.end() ||
399 mb.type_from_handle(parent) == MBVERTEX)
400 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
401 "Vertex ancestry must link a new child vertex to a selected "
402 "source edge, face or volume");
403 auto &candidates = vertex_sources[vertex];
404 candidates.insert(parent);
405 for (int dim = moab::CN::Dimension(mb.type_from_handle(parent)) + 1;
406 dim <= 3; ++dim) {
407 Range adjacent;
408 CHKERR mb.get_adjacencies(&parent, 1, dim, false, adjacent,
409 moab::Interface::UNION);
410 candidates.merge(intersect(adjacent, source_entities));
411 }
412 }
413 if (vertex_parents.size() != subtract(child_vertices, source_vertices).size())
414 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
415 "Every new child vertex requires a source parent");
416
417 std::map<EntityHandle, EntityHandle> parents = vertex_parents;
418 for (auto child : child_entities) {
419 Range corners;
420 CHKERR get_corners(child, corners);
421 if (!subtract(corners, child_vertices).empty())
422 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
423 "Child selection is missing bounding vertices");
424 if (source_entities.find(child) != source_entities.end() ||
425 mb.type_from_handle(child) == MBVERTEX)
426 continue;
427 Range candidates;
428 bool first = true;
429 for (auto vertex : corners) {
430 const auto it = vertex_sources.find(vertex);
431 if (it == vertex_sources.end())
432 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
433 "Child vertex has no source membership");
434 candidates = first ? it->second : intersect(candidates, it->second);
435 first = false;
436 }
437 EntityHandle parent = 0;
438 for (int dim = moab::CN::Dimension(mb.type_from_handle(child)); dim <= 3;
439 ++dim) {
440 const auto matches = candidates.subset_by_dimension(dim);
441 if (matches.size() > 1)
442 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
443 "Child has ambiguous source parents of dimension %d", dim);
444 if (!matches.empty()) {
445 parent = matches.front();
446 break;
447 }
448 }
449 if (!parent)
450 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
451 "Child is not contained in a single selected source entity");
452 parents.emplace(child, parent);
453 }
454
455 // Publish only after every child has an unambiguous parent. SetParent also
456 // reorders parent indices when the child was registered in an earlier pass.
457 const auto *ref_entities = m_field.get_ref_ents();
458 SetParent set_parent;
459 for (const auto &[child, parent] : parents)
460 CHKERR set_parent(child, parent, ref_entities, cOre);
461 CHKERR set_parent(ref_entities);
462 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevel(
463 child_entities, output_bit, false, verb);
465}
466
468 const BitRefLevel &bit, int verb,
469 const bool debug) {
470
472
473 auto set_edge_bits = [](moab::Interface &moab,
475 &ref_parent_ents_view,
476 EntityHandle tet, BitRefEdges &parent_edges_bit,
477 EntityHandle *edge_new_nodes, int *split_edges) {
479
480 for (int ee = 0; ee != 6; ++ee) {
481 EntityHandle edge;
482 CHKERR moab.side_element(tet, 1, ee, edge);
483 auto eit = ref_parent_ents_view.find(edge);
484 if (eit != ref_parent_ents_view.end()) {
485 edge_new_nodes[ee] = (*eit)->getEnt();
486 split_edges[parent_edges_bit.count()] = ee;
487 parent_edges_bit.set(ee, 1);
488 }
489 }
490
492 };
493
494 CHKERR refineTets(_tets, bit, set_edge_bits, verb, debug);
495
497}
498
500 const BitRefLevel &bit,
501 int verb,
502 const bool debug) {
503
505
506 auto set_edge_bits = [](moab::Interface &moab,
508 &ref_parent_ents_view,
509 EntityHandle tet, BitRefEdges &parent_edges_bit,
510 EntityHandle *edge_new_nodes, int *split_edges) {
512
513 for (int ee = 0; ee != 6; ++ee) {
514 EntityHandle edge;
515 CHKERR moab.side_element(tet, 1, ee, edge);
516 auto eit = ref_parent_ents_view.find(edge);
517 if (eit != ref_parent_ents_view.end()) {
518 edge_new_nodes[ee] = (*eit)->getEnt();
519 split_edges[parent_edges_bit.count()] = ee;
520 parent_edges_bit.set(ee, 1);
521 }
522 }
523
524 if(parent_edges_bit.count() < 6)
525 parent_edges_bit.reset();
526
528 };
529
530 CHKERR refineTets(_tets, bit, set_edge_bits, verb, debug);
531
533}
534
536MeshRefinement::refineTetsHangingNodes(const EntityHandle meshset,
537 const BitRefLevel &bit, int verb,
538 const bool debug) {
539 Interface &m_field = cOre;
540 moab::Interface &moab = m_field.get_moab();
542 Range tets;
543 CHKERR moab.get_entities_by_type(meshset, MBTET, tets, false);
546}
547
549 const BitRefLevel &bit,
550 SetEdgeBitsFun set_edge_bits,
551 int verb, const bool debug) {
552
553 Interface &m_field = cOre;
554 moab::Interface &moab = m_field.get_moab();
555 auto refined_ents_ptr = m_field.get_ref_ents();
556 auto refined_finite_elements_ptr = m_field.get_ref_finite_elements();
557 ReadUtilIface *read_util;
559
560 CHKERR m_field.get_moab().query_interface(read_util);
561
562 Range ents_to_set_bit;
563
564 // FIXME: refinement is based on entity handlers, should work on global ids of
565 // nodes, this will allow parallelise algorithm in the future
566
567 auto get_parent_ents_view = [&](const auto type_child,
568 const auto type_parent) {
569 auto &ref_ents =
570 refined_ents_ptr->get<Composite_EntType_and_ParentEntType_mi_tag>();
571 auto range =
572 ref_ents.equal_range(boost::make_tuple(type_child, type_parent));
573
575
576 using I = decltype(range.first);
577
578 boost::function<bool(I it)> tester = [&](I it) {
579 return ((*it)->getBitRefLevel() & bit).any();
580 };
581
582 boost::function<MoFEMErrorCode(I f, I s)> inserter = [&](I f, I s) {
583 ref_parent_ents_view.insert(f, s);
584 return 0;
585 };
586
587 rangeInserter(range.first, range.second, tester, inserter);
588
589 return ref_parent_ents_view;
590 };
591
592 auto ref_parent_ents_view = get_parent_ents_view(MBVERTEX, MBEDGE);
593
594 auto tets = _tets.subset_by_type(MBTET);
595
596 auto get_ele_parent_view = [&]() {
597 auto &ref_ents = refined_finite_elements_ptr->get<Ent_mi_tag>();
598 RefElement_multiIndex_parents_view ref_ele_parent_view;
599 for (auto p = tets.pair_begin(); p != tets.pair_end(); ++p) {
600 auto tmi = ref_ents.lower_bound(p->first);
601 auto tme = ref_ents.upper_bound(p->second);
602 ref_ele_parent_view.insert(tmi, tme);
603 }
604 return ref_ele_parent_view;
605 };
606
607 auto ref_ele_parent_view = get_ele_parent_view();
608
609 std::vector<EntityHandle> parent_tets_refinded;
610 std::vector<EntityHandle> vertices_of_created_tets;
611 std::vector<BitRefEdges> parent_edges_bit_vec;
612 std::vector<int> nb_new_tets_vec;
613 std::vector<int> sub_type_vec;
614
615 parent_tets_refinded.reserve(tets.size());
616 vertices_of_created_tets.reserve(4 * tets.size());
617 parent_edges_bit_vec.reserve(tets.size());
618 nb_new_tets_vec.reserve(tets.size());
619 sub_type_vec.reserve(tets.size());
620
621 // make loop over all tets which going to be refined
622 for (auto tit : tets) {
623
624 // get tet connectivity
625 const EntityHandle *conn;
626 int num_nodes;
627 CHKERR moab.get_connectivity(tit, conn, num_nodes, true);
628
629 // get edges
630 BitRefEdges parent_edges_bit(0);
631 EntityHandle edge_new_nodes[] = {0, 0, 0, 0, 0, 0};
632 int split_edges[] = {-1, -1, -1, -1, -1, -1};
633
634 CHKERR set_edge_bits(moab, ref_parent_ents_view, tit, parent_edges_bit,
635 edge_new_nodes, split_edges);
636
637 if (parent_edges_bit.count()) {
638
639 // swap nodes forward
640 EntityHandle _conn_[4];
641 std::copy(&conn[0], &conn[4], &_conn_[0]);
642
643 // build connectivity for ref tets
644 EntityHandle new_tets_conns[8 * 4];
645 std::fill(&new_tets_conns[0], &new_tets_conns[8 * 4], no_handle);
646
647 int sub_type = -1, nb_new_tets = 0;
648 switch (parent_edges_bit.count()) {
649 case 1:
650 sub_type = 0;
651 tet_type_1(_conn_, split_edges[0], edge_new_nodes[split_edges[0]],
652 new_tets_conns);
653 nb_new_tets = 2;
654 break;
655 case 2:
656 sub_type =
657 tet_type_2(_conn_, split_edges, edge_new_nodes, new_tets_conns);
658 if (sub_type & (4 | 8 | 16)) {
659 nb_new_tets = 3;
660 break;
661 } else if (sub_type == 1) {
662 nb_new_tets = 4;
663 break;
664 };
665 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE, "Impossible case");
666 break;
667 case 3:
668 sub_type =
669 tet_type_3(_conn_, split_edges, edge_new_nodes, new_tets_conns);
670 if (sub_type <= 4) {
671 nb_new_tets = 4;
672 break;
673 } else if (sub_type <= 7) {
674 nb_new_tets = 5;
675 break;
676 }
677 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE, "Impossible case");
678 case 4:
679 sub_type =
680 tet_type_4(_conn_, split_edges, edge_new_nodes, new_tets_conns);
681 if (sub_type == 0) {
682 nb_new_tets = 5;
683 break;
684 } else if (sub_type <= 7) {
685 nb_new_tets = 6;
686 break;
687 }
688 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE, "Impossible case");
689 case 5:
690 sub_type = tet_type_5(moab, _conn_, edge_new_nodes, new_tets_conns);
691 nb_new_tets = 7;
692 break;
693 case 6:
694 sub_type = 0;
695 tet_type_6(moab, _conn_, edge_new_nodes, new_tets_conns);
696 nb_new_tets = 8;
697 break;
698 default:
699 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE, "Impossible case");
700 }
701
702 // find that tets
703 auto &ref_ele_by_parent_and_ref_edges =
704 ref_ele_parent_view
706 auto it_by_ref_edges = ref_ele_by_parent_and_ref_edges.equal_range(
707 boost::make_tuple(tit, parent_edges_bit.to_ulong()));
708 // check if tet with this refinement scheme already exits
709 std::vector<EntityHandle> ents_to_set_bit_vec;
710 if (std::distance(it_by_ref_edges.first, it_by_ref_edges.second) ==
711 static_cast<size_t>(nb_new_tets)) {
712 ents_to_set_bit_vec.reserve(nb_new_tets);
713 for (int tt = 0; it_by_ref_edges.first != it_by_ref_edges.second;
714 it_by_ref_edges.first++, tt++) {
715 auto tet = (*it_by_ref_edges.first)->getEnt();
716 ents_to_set_bit_vec.emplace_back(tet);
717 // set ref tets entities
718 if (debug) {
719 // add this tet if exist to this ref level
720 auto ref_tet_it = refined_ents_ptr->find(tet);
721 if (ref_tet_it == refined_ents_ptr->end()) {
722 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
723 "Tetrahedron should be in database");
724 }
725 }
726 }
727 ents_to_set_bit.insert_list(ents_to_set_bit_vec.begin(),
728 ents_to_set_bit_vec.end());
729
730 } else {
731 // if this element was not refined or was refined with different
732 // patterns of split edges create new elements
733
734 parent_tets_refinded.emplace_back(tit);
735 for (int tt = 0; tt != nb_new_tets; ++tt) {
736 for (auto nn : {0, 1, 2, 3})
737 vertices_of_created_tets.emplace_back(new_tets_conns[4 * tt + nn]);
738 }
739 parent_edges_bit_vec.emplace_back(parent_edges_bit);
740 nb_new_tets_vec.emplace_back(nb_new_tets);
741 sub_type_vec.emplace_back(sub_type);
742 }
743 } else {
744
745 if (debug) {
746 RefEntity_multiIndex::iterator tit_miit;
747 tit_miit = refined_ents_ptr->find(tit);
748 if (tit_miit == refined_ents_ptr->end())
749 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
750 "can not find this tet");
751 }
752
753 ents_to_set_bit.insert(tit);
754 }
755 }
756
757 // Create tets
758 EntityHandle start_e = 0;
759 EntityHandle *conn_moab;
760 const int nb_new_tets = vertices_of_created_tets.size() / 4;
761 read_util->get_element_connect(nb_new_tets, 4, MBTET, 0, start_e, conn_moab);
762 std::copy(vertices_of_created_tets.begin(), vertices_of_created_tets.end(),
763 conn_moab);
764 CHKERR read_util->update_adjacencies(start_e, nb_new_tets, 4, conn_moab);
765 ents_to_set_bit.insert(start_e, start_e + nb_new_tets - 1);
766
767 // Create adj entities
768 Range ents;
769 for (auto d : {1, 2}) {
770 CHKERR moab.get_adjacencies(ents_to_set_bit, d, true, ents,
771 moab::Interface::UNION);
772 }
773
774 SetParent set_parent;
775
776 // Set parents and adjacencies
777 for (int idx = 0; idx != parent_tets_refinded.size(); ++idx) {
778
779 const EntityHandle tit = parent_tets_refinded[idx];
780 const BitRefEdges &parent_edges_bit = parent_edges_bit_vec[idx];
781 const int nb_new_tets = nb_new_tets_vec[idx];
782
783 std::array<EntityHandle, 8> ref_tets;
784 for (int tt = 0; tt != nb_new_tets; ++tt, ++start_e)
785 ref_tets[tt] = start_e;
786
787 if (nb_new_tets) {
788
789 CHKERR moab.tag_clear_data(cOre.get_th_RefBitEdge(), &ref_tets[0],
790 nb_new_tets, &parent_edges_bit);
791 CHKERR moab.tag_clear_data(cOre.get_th_RefParentHandle(), &ref_tets[0],
792 nb_new_tets, &tit);
793
794 // hash map of nodes (RefEntity) by edges (EntityHandle)
795 std::map<EntityHandle /*edge*/, EntityHandle /*node*/>
796 map_ref_nodes_by_edges;
797
798 Range tit_edges;
799 CHKERR moab.get_adjacencies(&tit, 1, 1, false, tit_edges);
800 for (auto edge : tit_edges) {
801 auto eit = ref_parent_ents_view.find(edge);
802 if (eit != ref_parent_ents_view.end()) {
803 map_ref_nodes_by_edges[(*eit)->getParentEnt()] = eit->get()->getEnt();
804 }
805 }
806
807 // find parents for new edges and faces
808 // get tet edges and faces
809 Range tit_faces;
810 CHKERR moab.get_adjacencies(&tit, 1, 2, false, tit_faces);
811 Range edges_nodes[6], faces_nodes[4];
812 // for edges - add ref nodes
813 // edges_nodes[ee] - contains all nodes on edge ee including mid nodes if
814 // exist
815 Range::iterator eit = tit_edges.begin();
816 for (int ee = 0; eit != tit_edges.end(); eit++, ee++) {
817 CHKERR moab.get_connectivity(&*eit, 1, edges_nodes[ee], true);
818 auto map_miit = map_ref_nodes_by_edges.find(*eit);
819 if (map_miit != map_ref_nodes_by_edges.end()) {
820 edges_nodes[ee].insert(map_miit->second);
821 }
822 }
823 // for faces - add ref nodes
824 // faces_nodes[ff] - contains all nodes on face ff including mid nodes if
825 // exist
826 Range::iterator fit = tit_faces.begin();
827 for (int ff = 0; fit != tit_faces.end(); fit++, ff++) {
828 CHKERR moab.get_connectivity(&*fit, 1, faces_nodes[ff], true);
829 // Get edges on face and loop over those edges to add mid-nodes to range
830 Range fit_edges;
831 CHKERR moab.get_adjacencies(&*fit, 1, 1, false, fit_edges);
832 for (Range::iterator eit2 = fit_edges.begin(); eit2 != fit_edges.end();
833 eit2++) {
834 auto map_miit = map_ref_nodes_by_edges.find(*eit2);
835 if (map_miit != map_ref_nodes_by_edges.end()) {
836 faces_nodes[ff].insert(map_miit->second);
837 }
838 }
839 }
840 // add ref nodes to tet
841 // tet_nodes contains all nodes on tet including mid edge nodes
842 Range tet_nodes;
843 CHKERR moab.get_connectivity(&tit, 1, tet_nodes, true);
844 for (std::map<EntityHandle, EntityHandle>::iterator map_miit =
845 map_ref_nodes_by_edges.begin();
846 map_miit != map_ref_nodes_by_edges.end(); map_miit++) {
847 tet_nodes.insert(map_miit->second);
848 }
849 Range ref_edges;
850 // Get all all edges of refined tets
851 CHKERR moab.get_adjacencies(ref_tets.data(), nb_new_tets, 1, false,
852 ref_edges, moab::Interface::UNION);
853 // Check for all ref edge and set parents
854 for (Range::iterator reit = ref_edges.begin(); reit != ref_edges.end();
855 reit++) {
856 Range ref_edges_nodes;
857 CHKERR moab.get_connectivity(&*reit, 1, ref_edges_nodes, true);
858
859 // Check if ref edge is an coarse edge (loop over coarse tet edges)
860 int ee = 0;
861 for (; ee < 6; ee++) {
862 // Two nodes are common (node[0],node[1],ref_node (if exist))
863 // this tests if given edge is contained by edge of refined
864 // tetrahedral
865 if (intersect(edges_nodes[ee], ref_edges_nodes).size() == 2) {
866 EntityHandle edge = tit_edges[ee];
867 CHKERR set_parent(*reit, edge, refined_ents_ptr, cOre);
868 break;
869 }
870 }
871 if (ee < 6)
872 continue; // this refined edge is contained by edge of tetrahedral
873 // check if ref edge is in coarse face
874 int ff = 0;
875 for (; ff < 4; ff++) {
876 // two nodes are common (node[0],node[1],ref_node (if exist))
877 // this tests if given face is contained by face of tetrahedral
878 if (intersect(faces_nodes[ff], ref_edges_nodes).size() == 2) {
879 EntityHandle face = tit_faces[ff];
880 CHKERR set_parent(*reit, face, refined_ents_ptr, cOre);
881 break;
882 }
883 }
884 if (ff < 4)
885 continue; // this refined edge is contained by face of tetrahedral
886
887 // check if ref edge is in coarse tetrahedral (i.e. that is internal
888 // edge of refined tetrahedral)
889 if (intersect(tet_nodes, ref_edges_nodes).size() == 2) {
890 CHKERR set_parent(*reit, tit, refined_ents_ptr, cOre);
891 continue;
892 }
893
894 // Refined edge is not child of any edge, face or tetrahedral, this is
895 // Impossible edge
896 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
897 "impossible refined edge");
898 }
899
900 Range ref_faces;
901 CHKERR moab.get_adjacencies(ref_tets.data(), nb_new_tets, 2, false,
902 ref_faces, moab::Interface::UNION);
903 Tag th_interface_side;
904 const int def_side[] = {0};
905 CHKERR moab.tag_get_handle("INTERFACE_SIDE", 1, MB_TYPE_INTEGER,
906 th_interface_side,
907 MB_TAG_CREAT | MB_TAG_SPARSE, def_side);
908 // Check for all ref faces
909 for (auto rfit : ref_faces) {
910 Range ref_faces_nodes;
911 CHKERR moab.get_connectivity(&rfit, 1, ref_faces_nodes, true);
912 // Check if ref face is in coarse face
913 int ff = 0;
914 for (; ff < 4; ff++) {
915 // Check if refined triangle is contained by face of tetrahedral
916 if (intersect(faces_nodes[ff], ref_faces_nodes).size() == 3) {
917 EntityHandle face = tit_faces[ff];
918 CHKERR set_parent(rfit, face, refined_ents_ptr, cOre);
919 int side = 0;
920 // Set face side if it is on interface
921 CHKERR moab.tag_get_data(th_interface_side, &face, 1, &side);
922 CHKERR moab.tag_set_data(th_interface_side, &rfit, 1, &side);
923 break;
924 }
925 }
926 if (ff < 4)
927 continue; // this face is contained by one of tetrahedrons
928 // check if ref face is in coarse tetrahedral
929 // this is ref face which is contained by tetrahedral volume
930 if (intersect(tet_nodes, ref_faces_nodes).size() == 3) {
931 CHKERR set_parent(rfit, tit, refined_ents_ptr, cOre);
932 continue;
933 }
934 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
935 "impossible refined face");
936 }
937 }
938 }
939
940 CHKERR set_parent(refined_ents_ptr);
941 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevel(
942 ents_to_set_bit, bit, true, verb, &ents);
944}
946 const BitRefLevel &bit, int verb) {
947
948 Interface &m_field = cOre;
949 moab::Interface &moab = m_field.get_moab();
950 auto refined_ents_ptr = m_field.get_ref_ents();
951 auto refined_finite_elements_ptr = m_field.get_ref_finite_elements();
952
953 // FIXME: refinement is based on entity handlers, should work on global ids of
954 // nodes, this will allow parallelise algorithm in the future
955
957
958 RefElement_multiIndex_parents_view ref_ele_parent_view;
959 ref_ele_parent_view.insert(
960 refined_finite_elements_ptr->get<Ent_mi_tag>().lower_bound(
961 get_id_for_min_type<MBPRISM>()),
962 refined_finite_elements_ptr->get<Ent_mi_tag>().upper_bound(
963 get_id_for_max_type<MBPRISM>()));
964 auto &ref_ele_by_parent_and_ref_edges =
965 ref_ele_parent_view
967 // find all vertices which parent is edge
968 auto &ref_ents_by_comp =
969 refined_ents_ptr->get<Composite_EntType_and_ParentEntType_mi_tag>();
971 ref_parent_ents_view.insert(
972 ref_ents_by_comp.lower_bound(boost::make_tuple(MBVERTEX, MBEDGE)),
973 ref_ents_by_comp.upper_bound(boost::make_tuple(MBVERTEX, MBEDGE)));
974 Range prisms;
975 CHKERR moab.get_entities_by_type(meshset, MBPRISM, prisms, false);
976 Range::iterator pit = prisms.begin();
977 for (; pit != prisms.end(); pit++) {
978 auto miit_prism = refined_ents_ptr->get<Ent_mi_tag>().find(*pit);
979 if (miit_prism == refined_ents_ptr->end()) {
980 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
981 "this prism is not in ref database");
982 }
983 if (verb >= NOISY) {
984 MOFEM_TAG_AND_LOG("WORLD", Sev::noisy, "MeshRefinment")
985 << "ref prism " << **miit_prism;
986 }
987 // prism connectivity
988 int num_nodes;
989 const EntityHandle *conn;
990 CHKERR moab.get_connectivity(*pit, conn, num_nodes, true);
991 assert(num_nodes == 6);
992 // edges connectivity
993 EntityHandle edges[6];
994 for (int ee = 0; ee < 3; ee++) {
995 CHKERR moab.side_element(*pit, 1, ee, edges[ee]);
996 }
997 for (int ee = 6; ee < 9; ee++) {
998 CHKERR moab.side_element(*pit, 1, ee, edges[ee - 3]);
999 }
1000 // detect split edges
1001 BitRefEdges split_edges(0);
1002 EntityHandle edge_nodes[6];
1003 std::fill(&edge_nodes[0], &edge_nodes[6], no_handle);
1004 for (int ee = 0; ee < 6; ee++) {
1005 RefEntity_multiIndex_view_by_hashed_parent_entity::iterator miit_view =
1006 ref_parent_ents_view.find(edges[ee]);
1007 if (miit_view != ref_parent_ents_view.end()) {
1008 if (((*miit_view)->getBitRefLevel() & bit).any()) {
1009 edge_nodes[ee] = (*miit_view)->getEnt();
1010 split_edges.set(ee);
1011 }
1012 }
1013 }
1014 if (split_edges.count() == 0) {
1015 *(const_cast<RefEntity *>(miit_prism->get())->getBitRefLevelPtr()) |= bit;
1016 if (verb >= VERY_NOISY)
1017 MOFEM_TAG_AND_LOG("WORLD", Sev::noisy, "MeshRefinment")
1018 << "no refinement";
1019 continue;
1020 }
1021 // check consistency
1022 if (verb >= NOISY) {
1023 std::ostringstream ss;
1024 ss << "prism split edges " << split_edges << " count "
1025 << split_edges.count() << std::endl;
1026 PetscPrintf(m_field.get_comm(), "%s", ss.str().c_str());
1027 }
1028 // prism ref
1029 EntityHandle new_prism_conn[4 * 6];
1030 std::fill(&new_prism_conn[0], &new_prism_conn[4 * 6], no_handle);
1031 int nb_new_prisms = 0;
1032 switch (split_edges.count()) {
1033 case 0:
1034 break;
1035 case 2:
1036 CHKERR prism_type_1(conn, split_edges, edge_nodes, new_prism_conn);
1037 nb_new_prisms = 2;
1038 break;
1039 case 4:
1040 CHKERR prism_type_2(conn, split_edges, edge_nodes, new_prism_conn);
1041 nb_new_prisms = 3;
1042 break;
1043 case 6:
1044 CHKERR prism_type_3(conn, split_edges, edge_nodes, new_prism_conn);
1045 nb_new_prisms = 4;
1046 break;
1047 default:
1048 std::ostringstream ss;
1049 ss << split_edges << " : [ " << conn[0] << " " << conn[1] << " "
1050 << conn[2] << " " << conn[3] << " " << conn[4] << " " << conn[5]
1051 << " ]";
1052 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY, "%s",
1053 ss.str().c_str());
1054 }
1055 // find that prism
1056 std::bitset<4> ref_prism_bit(0);
1057 auto it_by_ref_edges = ref_ele_by_parent_and_ref_edges.lower_bound(
1058 boost::make_tuple(*pit, split_edges.to_ulong()));
1059 auto hi_it_by_ref_edges = ref_ele_by_parent_and_ref_edges.upper_bound(
1060 boost::make_tuple(*pit, split_edges.to_ulong()));
1061 auto it_by_ref_edges2 = it_by_ref_edges;
1062 for (int pp = 0; it_by_ref_edges2 != hi_it_by_ref_edges;
1063 it_by_ref_edges2++, pp++) {
1064 // Add this tet to this ref
1065 *(const_cast<RefElement *>(it_by_ref_edges2->get())
1066 ->getBitRefLevelPtr()) |= bit;
1067 ref_prism_bit.set(pp, 1);
1068 if (verb > 2) {
1069 std::ostringstream ss;
1070 ss << "is refined " << *it_by_ref_edges2 << std::endl;
1071 PetscPrintf(m_field.get_comm(), "%s", ss.str().c_str());
1072 }
1073 }
1074 if (it_by_ref_edges != hi_it_by_ref_edges) {
1075 if (ref_prism_bit.count() != (unsigned int)nb_new_prisms)
1076 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1077 "data inconsistency");
1078 } else {
1079 EntityHandle ref_prisms[4];
1080 // create prism
1081 for (int pp = 0; pp < nb_new_prisms; pp++) {
1082 if (verb > 3) {
1083 std::ostringstream ss;
1084 ss << "ref prism " << ref_prism_bit << std::endl;
1085 PetscPrintf(m_field.get_comm(), "%s", ss.str().c_str());
1086 }
1087 if (!ref_prism_bit.test(pp)) {
1088 CHKERR moab.create_element(MBPRISM, &new_prism_conn[6 * pp], 6,
1089 ref_prisms[pp]);
1090 CHKERR moab.tag_set_data(cOre.get_th_RefParentHandle(),
1091 &ref_prisms[pp], 1, &*pit);
1092 CHKERR moab.tag_set_data(cOre.get_th_RefBitLevel(), &ref_prisms[pp],
1093 1, &bit);
1094 CHKERR moab.tag_set_data(cOre.get_th_RefBitEdge(), &ref_prisms[pp], 1,
1095 &split_edges);
1096 auto p_ent =
1097 const_cast<RefEntity_multiIndex *>(refined_ents_ptr)
1098 ->insert(boost::shared_ptr<RefEntity>(new RefEntity(
1099 m_field.get_basic_entity_data_ptr(), ref_prisms[pp])));
1100 std::pair<RefElement_multiIndex::iterator, bool> p_fe;
1101 try {
1102 p_fe =
1103 const_cast<RefElement_multiIndex *>(refined_finite_elements_ptr)
1104 ->insert(boost::shared_ptr<RefElement>(
1105 new RefElement_PRISM(*p_ent.first)));
1106 } catch (MoFEMException const &e) {
1107 SETERRQ(PETSC_COMM_SELF, e.errorCode, "%s", e.errorMessage);
1108 }
1109 ref_prism_bit.set(pp);
1110 CHKERR cOre.addPrismToDatabase(ref_prisms[pp]);
1111 if (verb > 2) {
1112 std::ostringstream ss;
1113 ss << "add prism: " << **p_fe.first << std::endl;
1114 if (verb > 7) {
1115 for (int nn = 0; nn < 6; nn++) {
1116 ss << new_prism_conn[nn] << " ";
1117 }
1118 ss << std::endl;
1119 }
1120 PetscPrintf(m_field.get_comm(), "%s", ss.str().c_str());
1121 }
1122 }
1123 }
1124 }
1125 }
1127}
1129 const BitRefLevel &bit,
1130 const bool recursive, int verb) {
1131 Interface &m_field = cOre;
1132 auto refined_ents_ptr = m_field.get_ref_ents();
1134 auto miit = refined_ents_ptr->find(meshset);
1135 if (miit == refined_ents_ptr->end()) {
1136 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1137 "this meshset is not in ref database");
1138 }
1139 CHKERR m_field.getInterface<BitRefManager>()->updateMeshsetByEntitiesChildren(
1140 meshset, bit, meshset, MBMAXTYPE, recursive, verb);
1141 *(const_cast<RefEntity *>(miit->get())->getBitRefLevelPtr()) |= bit;
1143}
1144
1146 const BitRefLevel &bit, int verb,
1147 const bool debug) {
1148 Interface &m_field = cOre;
1149 moab::Interface &moab = m_field.get_moab();
1151 Range tris;
1152 CHKERR moab.get_entities_by_type(meshset, MBTRI, tris, false);
1153 CHKERR refineTris(tris, bit, verb, debug);
1155}
1156
1158 const BitRefLevel &bit, int verb,
1159 const bool debug) {
1161
1162 auto set_edge_bits = [](moab::Interface &moab,
1164 &ref_parent_ents_view,
1165 EntityHandle tet, BitRefEdges &parent_edges_bit,
1166 EntityHandle *edge_new_nodes, int *split_edges) {
1168
1169 for (int ee = 0; ee != 3; ++ee) {
1170 EntityHandle edge;
1171 CHKERR moab.side_element(tet, 1, ee, edge);
1172 auto eit = ref_parent_ents_view.find(edge);
1173 if (eit != ref_parent_ents_view.end()) {
1174 edge_new_nodes[ee] = (*eit)->getEnt();
1175 split_edges[parent_edges_bit.count()] = ee;
1176 parent_edges_bit.set(ee, 1);
1177 }
1178 }
1179
1181 };
1182
1183 CHKERR refineTris(_tris, bit, set_edge_bits, verb, debug);
1184
1186}
1187
1189 const BitRefLevel &bit,
1190 int verb,
1191 const bool debug) {
1192
1194
1195 auto set_edge_bits = [](moab::Interface &moab,
1197 &ref_parent_ents_view,
1198 EntityHandle tet, BitRefEdges &parent_edges_bit,
1199 EntityHandle *edge_new_nodes, int *split_edges) {
1201
1202 for (int ee = 0; ee != 3; ++ee) {
1203 EntityHandle edge;
1204 CHKERR moab.side_element(tet, 1, ee, edge);
1205 auto eit = ref_parent_ents_view.find(edge);
1206 if (eit != ref_parent_ents_view.end()) {
1207 edge_new_nodes[ee] = (*eit)->getEnt();
1208 split_edges[parent_edges_bit.count()] = ee;
1209 parent_edges_bit.set(ee, 1);
1210 }
1211 }
1212
1213 if (parent_edges_bit.count() < 3) {
1214 parent_edges_bit.reset();
1215 }
1216
1218 };
1219
1220 CHKERR refineTris(_tris, bit, set_edge_bits, verb, debug);
1221
1223};
1224
1227 const BitRefLevel &bit, int verb,
1228 const bool debug) {
1229 Interface &m_field = cOre;
1230 moab::Interface &moab = m_field.get_moab();
1232 Range tris;
1233 CHKERR moab.get_entities_by_type(meshset, MBTRI, tris, false);
1234 CHKERR refineTrisHangingNodes(tris, bit, verb, debug);
1236}
1237
1239 const BitRefLevel &bit,
1240 SetEdgeBitsFun set_edge_bits,
1241 int verb, const bool debug) {
1242 Interface &m_field = cOre;
1243 moab::Interface &moab = m_field.get_moab();
1244 auto refined_ents_ptr = m_field.get_ref_ents();
1245 auto refined_finite_elements_ptr = m_field.get_ref_finite_elements();
1246 ReadUtilIface *read_util;
1247
1249
1250 auto get_parent_ents_view = [&](const auto type_child,
1251 const auto type_parent) {
1252 auto &ref_ents =
1253 refined_ents_ptr->get<Composite_EntType_and_ParentEntType_mi_tag>();
1254 auto range =
1255 ref_ents.equal_range(boost::make_tuple(type_child, type_parent));
1256
1258
1259 using I = decltype(range.first);
1260
1261 boost::function<bool(I it)> tester = [&](I it) {
1262 return ((*it)->getBitRefLevel() & bit).any();
1263 };
1264
1265 boost::function<MoFEMErrorCode(I f, I s)> inserter = [&](I f, I s) {
1266 ref_parent_ents_view.insert(f, s);
1267 return 0;
1268 };
1269
1270 rangeInserter(range.first, range.second, tester, inserter);
1271
1272 return ref_parent_ents_view;
1273 };
1274
1275 auto ref_parent_ents_view = get_parent_ents_view(MBVERTEX, MBEDGE);
1276
1277 auto tris = _tris.subset_by_type(MBTRI);
1278
1279 auto get_ele_parent_view = [&]() {
1280 auto &ref_ents = refined_finite_elements_ptr->get<Ent_mi_tag>();
1281 RefElement_multiIndex_parents_view ref_ele_parent_view;
1282 for (auto p = tris.pair_begin(); p != tris.pair_end(); ++p) {
1283 auto tmi = ref_ents.lower_bound(p->first);
1284 auto tme = ref_ents.upper_bound(p->second);
1285 ref_ele_parent_view.insert(tmi, tme);
1286 }
1287 return ref_ele_parent_view;
1288 };
1289
1290 auto ref_ele_parent_view = get_ele_parent_view();
1291
1292 Range ents_to_set_bit;
1293
1294 std::vector<EntityHandle> parent_tris_refined; // entities refined
1295 std::vector<EntityHandle> vertices_of_created_tris;
1296 std::vector<BitRefEdges> parent_edges_bit_vec;
1297 std::vector<int> nb_new_tris_vec;
1298
1299 parent_tris_refined.reserve(tris.size());
1300 vertices_of_created_tris.reserve(3 * tris.size());
1301 parent_edges_bit_vec.reserve(tris.size());
1302 nb_new_tris_vec.reserve(tris.size());
1303
1304 // make loop over all tets which going to be refined
1305 for (auto tit : tris) {
1306
1307 // get tet connectivity
1308 const EntityHandle *conn;
1309 int num_nodes;
1310 CHKERR moab.get_connectivity(tit, conn, num_nodes, true);
1311
1312 // get edges
1313 BitRefEdges parent_edges_bit(0);
1314 EntityHandle edge_new_nodes[] = {0, 0, 0};
1315 int split_edges[] = {-1, -1, -1};
1316
1317 CHKERR set_edge_bits(moab, ref_parent_ents_view, tit, parent_edges_bit,
1318 edge_new_nodes, split_edges);
1319
1320 if (parent_edges_bit.count()) {
1321
1322 // swap nodes forward
1323 EntityHandle _conn_[3];
1324 std::copy(&conn[0], &conn[3], &_conn_[0]);
1325
1326 // build connectivity for ref tets
1327 EntityHandle new_tets_conns[4 * 3];
1328 std::fill(&new_tets_conns[0], &new_tets_conns[4 * 3], no_handle);
1329
1330 int nb_new_tris = 0;
1331 switch (parent_edges_bit.count()) {
1332 case 1:
1333 CHKERR tri_type_1(conn, split_edges[0], edge_new_nodes[split_edges[0]],
1334 new_tets_conns);
1335 nb_new_tris += 2;
1336 break;
1337 case 2:
1338 CHKERR tri_type_2(conn, split_edges, edge_new_nodes, new_tets_conns);
1339 nb_new_tris += 3;
1340 break;
1341 case 3:
1342 CHKERR tri_type_3(conn, edge_new_nodes, new_tets_conns);
1343 nb_new_tris += 4;
1344 break;
1345 default:
1346 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE, "Impossible case");
1347 }
1348
1349 // find that tets
1350 auto &ref_ele_by_parent_and_ref_edges =
1351 ref_ele_parent_view
1353 auto it_by_ref_edges = ref_ele_by_parent_and_ref_edges.equal_range(
1354 boost::make_tuple(tit, parent_edges_bit.to_ulong()));
1355 // check if tet with this refinement scheme already exits
1356 std::vector<EntityHandle> ents_to_set_bit_vec;
1357 if (std::distance(it_by_ref_edges.first, it_by_ref_edges.second) ==
1358 static_cast<size_t>(nb_new_tris)) {
1359 ents_to_set_bit_vec.reserve(nb_new_tris);
1360 for (int tt = 0; it_by_ref_edges.first != it_by_ref_edges.second;
1361 it_by_ref_edges.first++, tt++) {
1362 auto tet = (*it_by_ref_edges.first)->getEnt();
1363 ents_to_set_bit_vec.emplace_back(tet);
1364 // set ref tets entities
1365 if (debug) {
1366 // add this tet if exist to this ref level
1367 auto ref_tet_it = refined_ents_ptr->find(tet);
1368 if (ref_tet_it == refined_ents_ptr->end()) {
1369 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1370 "Tetrahedron should be in database");
1371 }
1372 }
1373 }
1374 ents_to_set_bit.insert_list(ents_to_set_bit_vec.begin(),
1375 ents_to_set_bit_vec.end());
1376
1377 } else {
1378 // if this element was not refined or was refined with different
1379 // patterns of split edges create new elements
1380
1381 parent_tris_refined.emplace_back(tit);
1382 for (int tt = 0; tt != nb_new_tris; ++tt) {
1383 for (auto nn : {0, 1, 2})
1384 vertices_of_created_tris.emplace_back(new_tets_conns[3 * tt + nn]);
1385 }
1386 parent_edges_bit_vec.emplace_back(parent_edges_bit);
1387 nb_new_tris_vec.emplace_back(nb_new_tris);
1388 }
1389 } else {
1390
1391 if (debug) {
1392 RefEntity_multiIndex::iterator tit_miit;
1393 tit_miit = refined_ents_ptr->find(tit);
1394 if (tit_miit == refined_ents_ptr->end())
1395 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1396 "can not find this tet");
1397 }
1398
1399 ents_to_set_bit.insert(tit);
1400 }
1401 }
1402
1403 const int nb_new_tris = vertices_of_created_tris.size() / 3;
1404 EntityHandle start_e = 0;
1405
1406 if (nb_new_tris) {
1407
1408 // Create tets
1409 EntityHandle *conn_moab;
1410 CHKERR m_field.get_moab().query_interface(read_util);
1411 CHKERR read_util->get_element_connect(nb_new_tris, 3, MBTRI, 0, start_e,
1412 conn_moab);
1413 std::copy(vertices_of_created_tris.begin(), vertices_of_created_tris.end(),
1414 conn_moab);
1415 CHKERR read_util->update_adjacencies(start_e, nb_new_tris, 3, conn_moab);
1416 ents_to_set_bit.insert(start_e, start_e + nb_new_tris - 1);
1417
1418 if (debug) {
1419 auto meshset_ptr = get_temp_meshset_ptr(moab);
1420 CHKERR moab.add_entities(*meshset_ptr, ents_to_set_bit);
1421 CHKERR moab.write_file("out_refinded_debug.vtk", "VTK", "",
1422 meshset_ptr->get_ptr(), 1);
1423 }
1424 }
1425
1426 // Create adj entities
1427 Range ents;
1428 for (auto d : {1}) {
1429 CHKERR moab.get_adjacencies(ents_to_set_bit, d, true, ents,
1430 moab::Interface::UNION);
1431 }
1432
1433 SetParent set_parent;
1434
1435 // Set parrents and adjacencies. Iterate over all parent triangles.
1436 for (int idx = 0; idx != parent_tris_refined.size(); ++idx) {
1437
1438 const auto tit = parent_tris_refined[idx]; // parent triangle
1439 const auto &parent_edges_bit =
1440 parent_edges_bit_vec[idx]; // marks what edges are refined
1441 const auto nb_new_tris =
1442 nb_new_tris_vec[idx]; // number of triangles created by refinement
1443
1444 // Iterate over refined triangles
1445 std::array<EntityHandle, 4> ref_tris;
1446 for (int tt = 0; tt != nb_new_tris; ++tt, ++start_e)
1447 ref_tris[tt] = start_e;
1448
1449 if (nb_new_tris) {
1450
1451 CHKERR moab.tag_clear_data(cOre.get_th_RefBitEdge(), &ref_tris[0],
1452 nb_new_tris, &parent_edges_bit);
1453 CHKERR moab.tag_clear_data(cOre.get_th_RefParentHandle(), &ref_tris[0],
1454 nb_new_tris, &tit);
1455
1456 // Hash map of nodes (RefEntity) by edges (EntityHandle)
1457 std::map<EntityHandle /*edge*/, EntityHandle /*node*/>
1458 map_ref_nodes_by_edges;
1459
1460 // find parents for new edges and faces
1461 // get tet edges and faces
1462 Range tit_edges; // parent edges
1463 CHKERR moab.get_adjacencies(&tit, 1, 1, false, tit_edges);
1464 for (auto edge : tit_edges) {
1465 auto eit = ref_parent_ents_view.find(edge);
1466 if (eit != ref_parent_ents_view.end()) {
1467 map_ref_nodes_by_edges[(*eit)->getParentEnt()] = eit->get()->getEnt();
1468 }
1469 }
1470
1471 Range edges_nodes[3];
1472 // for edges - add ref nodes
1473 // edges_nodes[ee] - contains all nodes on edge ee including mid nodes if
1474 // exist
1475 Range::iterator eit = tit_edges.begin();
1476 for (int ee = 0; eit != tit_edges.end(); eit++, ee++) {
1477 CHKERR moab.get_connectivity(&*eit, 1, edges_nodes[ee], true);
1478 auto map_miit = map_ref_nodes_by_edges.find(*eit);
1479 if (map_miit != map_ref_nodes_by_edges.end()) {
1480 edges_nodes[ee].insert(map_miit->second);
1481 }
1482 }
1483
1484 // add ref nodes to tri
1485 // tet_nodes contains all nodes on tet including mid edge nodes
1486 Range tri_nodes;
1487 CHKERR moab.get_connectivity(&tit, 1, tri_nodes, true);
1488 for (auto map_miit = map_ref_nodes_by_edges.begin();
1489 map_miit != map_ref_nodes_by_edges.end(); map_miit++) {
1490 tri_nodes.insert(map_miit->second);
1491 }
1492
1493 Range ref_edges; // edges on all new tets
1494 // Get all all edges of refined tets
1495 CHKERR moab.get_adjacencies(ref_tris.data(), nb_new_tris, 1, false,
1496 ref_edges, moab::Interface::UNION);
1497
1498 // Check for all ref edge and set parents
1499 for (auto reit = ref_edges.begin(); reit != ref_edges.end(); ++reit) {
1500 Range ref_edges_nodes;
1501 CHKERR moab.get_connectivity(&*reit, 1, ref_edges_nodes, true);
1502 // Check if ref edge is an coarse edge (loop over coarse tet edges)
1503 int ee = 0;
1504 for (; ee < 3; ee++) {
1505 // Two nodes are common (node[0],node[1],ref_node (if exist))
1506 // this tests if given edge is contained by edge of refined
1507 // triangle
1508 if (intersect(edges_nodes[ee], ref_edges_nodes).size() == 2) {
1509 EntityHandle edge = tit_edges[ee];
1510 CHKERR set_parent(*reit, edge, refined_ents_ptr, cOre);
1511 break;
1512 }
1513 }
1514 if (ee < 3)
1515 continue; // this refined edge is contained by edge of triangle
1516
1517 // check if ref edge is in coarse tetrahedral (i.e. that is internal
1518 // edge of refined tetrahedral)
1519 if (intersect(tri_nodes, ref_edges_nodes).size() == 2) {
1520 CHKERR set_parent(*reit, tit, refined_ents_ptr, cOre);
1521 continue;
1522 }
1523
1524 // Refined edge is not child of any edge, face or tetrahedral, this is
1525 // Impossible edge
1526 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1527 "impossible refined edge");
1528 }
1529 }
1530 }
1531
1532 CHKERR set_parent(refined_ents_ptr);
1533 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevel(
1534 ents_to_set_bit, bit, true, verb, &ents);
1535
1537}
1538
1540 const BitRefLevel &bit,
1541 int verb, const bool debug) {
1542 MoFEM::Interface &m_field = cOre;
1543 moab::Interface &moab = m_field.get_moab();
1544 auto refined_ents_ptr = m_field.get_ref_ents();
1545 ReadUtilIface *read_util;
1547
1548 CHKERR m_field.get_moab().query_interface(read_util);
1549
1550 auto hexes = _hexes.subset_by_type(MBHEX);
1551 if (hexes.empty()) {
1552 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1553 "no hexahedrons to refine");
1555 }
1556
1557 auto get_hex_size = [&]() {
1558 std::array<double, 3> dx;
1559 const EntityHandle *conn;
1560 int n;
1561 moab.get_connectivity(hexes[0], conn, n, true);
1562 double p0[3], p1[3], p3[3], p4[3];
1563 moab.get_coords(&conn[0], 1, p0); // v0
1564 moab.get_coords(&conn[1], 1, p1); // v1
1565 moab.get_coords(&conn[3], 1, p3); // v3
1566 moab.get_coords(&conn[4], 1, p4); // v4
1567
1568 dx[0] = std::fabs(p4[0] - p0[0]);
1569 dx[1] = std::fabs(p3[1] - p0[1]);
1570 dx[2] = std::fabs(p1[2] - p0[2]);
1571 return dx;
1572 };
1573
1574 auto get_origin = [&]() {
1575 MatrixDouble coords(hexes.size(), 3);
1576 CHKERR moab.get_coords(hexes, &*coords.data().begin());
1577
1578 constexpr double eps_max = std::numeric_limits<double>::max();
1579 std::array<double, 3> min_coords{eps_max, eps_max, eps_max};
1580
1581 for (auto v = 0; v != hexes.size(); ++v) {
1582 for (int c = 0; c < 3; ++c)
1583 min_coords[c] = std::min(min_coords[c], coords(v, c));
1584
1585 }
1586 return min_coords;
1587 };
1588
1589 auto x0 = get_origin();
1590 auto dx = get_hex_size();
1591
1592 Range ents_to_set_bit;
1593
1594 std::vector<EntityHandle> parent_hexes_refined;
1595 std::vector<EntityHandle> vertices_of_created_tets;
1596 std::vector<BitRefEdges> parent_edges_bit_vec;
1597 std::vector<int> nb_new_tets_vec;
1598
1599 parent_hexes_refined.reserve(hexes.size());
1600 vertices_of_created_tets.reserve(24 * hexes.size()); // Maximum 6 tets per hex
1601 parent_edges_bit_vec.reserve(hexes.size());
1602 nb_new_tets_vec.reserve(hexes.size());
1603
1604
1605 auto use_type1_from_coords = [&](const EntityHandle ent) {
1606 double p[3];
1607 moab.get_coords(&ent, 1, p); // hex position
1608 int i = std::llround((p[0] - x0[0]) / dx[0]);
1609 // int j = std::llround((p[1] - x0[1]) / dx[1]);
1610 int k = std::llround((p[2] - x0[2]) / dx[2]);
1611 // checkerboard pattern for xz planes
1612 return ((i + k) % 2) == 0;
1613 };
1614
1615 // Loop over all hexes which are going to be refined
1616 for (auto hit : hexes) {
1617 // Get hexahedron connectivity
1618 const EntityHandle *conn;
1619 int num_nodes;
1620 CHKERR moab.get_connectivity(hit, conn, num_nodes, true);
1621 BitRefEdges parent_edges_bit(0);
1622
1623 // Swap nodes forward
1624 EntityHandle _conn_[8];
1625 std::copy(&conn[0], &conn[8], &_conn_[0]);
1626
1627 // Build connectivity for refined tetrahedrons
1628 EntityHandle new_tets_conns[6 * 4];
1629 std::fill(&new_tets_conns[0], &new_tets_conns[6 * 4], no_handle);
1630
1631 if (use_type1_from_coords(hit)) {
1632 hex_to_tet_type_2(_conn_, new_tets_conns);
1633 } else {
1634 hex_to_tet_type_1(_conn_, new_tets_conns);
1635 }
1636
1637 int nb_new_tets = 6; //TODO: in future allow for structured refinement to 24 tets
1638
1639 parent_hexes_refined.emplace_back(hit);
1640 for (int ht = 0; ht != nb_new_tets; ++ht) {
1641 for (auto nn : {0, 1, 2, 3}) // 4 vertices per tet
1642 vertices_of_created_tets.emplace_back(new_tets_conns[4 * ht + nn]);
1643 }
1644 parent_edges_bit_vec.emplace_back(parent_edges_bit);
1645 nb_new_tets_vec.emplace_back(nb_new_tets);
1646 }
1647
1648 // Create tetrahedrons
1649 EntityHandle start_e = 0;
1650 EntityHandle *conn_moab;
1651 const int nb_new_tets = vertices_of_created_tets.size() / 4;
1652 read_util->get_element_connect(nb_new_tets, 4, MBTET, 0, start_e, conn_moab);
1653 std::copy(vertices_of_created_tets.begin(), vertices_of_created_tets.end(),
1654 conn_moab);
1655 CHKERR read_util->update_adjacencies(start_e, nb_new_tets, 4, conn_moab);
1656 ents_to_set_bit.insert(start_e, start_e + nb_new_tets - 1);
1657
1658 // Create adj entities
1659 Range ents;
1660 for (auto d : {1, 2}) {
1661 CHKERR moab.get_adjacencies(ents_to_set_bit, d, true, ents,
1662 moab::Interface::UNION);
1663 }
1664 SetParent set_parent;
1665
1666 // Set parents and adjacencies for the new tets
1667 for (int idx = 0; idx != parent_hexes_refined.size(); ++idx) {
1668 const EntityHandle hex = parent_hexes_refined[idx];
1669
1670 const BitRefEdges &parent_edges_bit = parent_edges_bit_vec[idx];
1671 const int nb_new_tets = nb_new_tets_vec[idx];
1672
1673 std::array<EntityHandle, 6> ref_tets;
1674 for (int tt = 0; tt != nb_new_tets; ++tt, ++start_e)
1675 ref_tets[tt] = start_e;
1676
1677 if (nb_new_tets) {
1678
1679 CHKERR moab.tag_clear_data(cOre.get_th_RefBitEdge(), &ref_tets[0],
1680 nb_new_tets, &parent_edges_bit);
1681 CHKERR moab.tag_clear_data(cOre.get_th_RefParentHandle(), &ref_tets[0],
1682 nb_new_tets, &hex);
1683
1684
1685 Range hex_edges;
1686 CHKERR moab.get_adjacencies(&hex, 1, 1, false, hex_edges);
1687
1688 // find parents for new edges and faces
1689 // get tet edges and faces
1690 Range hex_faces;
1691 CHKERR moab.get_adjacencies(&hex, 1, 2, false, hex_faces);
1692 Range edges_nodes[12], faces_nodes[6];
1693 // for edges - add ref nodes
1694 // edges_nodes[ee] - contains all nodes on edge ee
1695 Range::iterator eit = hex_edges.begin();
1696 for (int ee = 0; eit != hex_edges.end(); eit++, ee++) {
1697 CHKERR moab.get_connectivity(&*eit, 1, edges_nodes[ee], true);
1698 }
1699 // for faces - add ref nodes
1700 // faces_nodes[ff] - contains all nodes on face ff including mid nodes
1701 // if exist
1702 Range::iterator fit = hex_faces.begin();
1703 for (int ff = 0; fit != hex_faces.end(); fit++, ff++) {
1704 CHKERR moab.get_connectivity(&*fit, 1, faces_nodes[ff], true);
1705 }
1706
1707 Range hex_nodes;
1708 CHKERR moab.get_connectivity(&hex, 1, hex_nodes, true);
1709
1710 Range ref_edges;
1711 // Get all all edges of refined tets
1712 CHKERR moab.get_adjacencies(ref_tets.data(), nb_new_tets, 1, false,
1713 ref_edges, moab::Interface::UNION);
1714 // Check for all ref edge and set parents
1715 for (Range::iterator reit = ref_edges.begin(); reit != ref_edges.end();
1716 reit++) {
1717 Range ref_edges_nodes;
1718 CHKERR moab.get_connectivity(&*reit, 1, ref_edges_nodes, true);
1719
1720 // Check if ref edge is an coarse edge (loop over coarse tet edges)
1721 int ee = 0;
1722 for (; ee < 12; ee++) {
1723 // Two nodes are common (node[0],node[1],ref_node (if exist))
1724 // this tests if given edge is contained by edge of refined
1725 // tetrahedral
1726 if (intersect(edges_nodes[ee], ref_edges_nodes).size() == 2) {
1727 EntityHandle edge = hex_edges[ee];
1728 CHKERR set_parent(*reit, edge, refined_ents_ptr, cOre);
1729 break;
1730 }
1731 }
1732 if (ee < 12)
1733 continue; // this refined edge is contained by edge of tetrahedral
1734
1735 // check if ref edge is in coarse face
1736 int ff = 0;
1737 for (; ff < 6; ff++) {
1738 // two nodes are common (node[0],node[1],ref_node (if exist))
1739 // this tests if given face is contained by face of tetrahedral
1740 if (intersect(faces_nodes[ff], ref_edges_nodes).size() == 2) {
1741 EntityHandle face = hex_faces[ff];
1742 CHKERR set_parent(*reit, face, refined_ents_ptr, cOre);
1743 // break;
1744 }
1745 }
1746 if (ff < 6)
1747 continue; // this refined edge is contained by face of tetrahedral
1748
1749 // otherwise interior (the hex diagonal)
1750 if (intersect(hex_nodes, ref_edges_nodes).size() == 2) {
1751 CHKERR set_parent(*reit, hex, refined_ents_ptr, cOre);
1752 continue;
1753 }
1754
1755 // Refined edge is not child of any edge, face or tetrahedral, this is
1756 // Impossible edge
1757 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1758 "impossible refined edge");
1759 }
1760
1761 Range ref_faces;
1762 CHKERR moab.get_adjacencies(ref_tets.data(), nb_new_tets, 2, false,
1763 ref_faces, moab::Interface::UNION);
1764 Tag th_interface_side;
1765 const int def_side[] = {0};
1766 CHKERR moab.tag_get_handle("INTERFACE_SIDE", 1, MB_TYPE_INTEGER,
1767 th_interface_side,
1768 MB_TAG_CREAT | MB_TAG_SPARSE, def_side);
1769 // Check for all ref faces
1770 for (auto rfit : ref_faces) {
1771 Range ref_faces_nodes;
1772 CHKERR moab.get_connectivity(&rfit, 1, ref_faces_nodes, true);
1773 // Check if ref face is in coarse face
1774 int ff = 0;
1775 for (; ff < 6; ff++) {
1776 // Check if refined triangle is contained by face of hex
1777 if (intersect(faces_nodes[ff], ref_faces_nodes).size() == 3) {
1778 EntityHandle face = hex_faces[ff];
1779 CHKERR set_parent(rfit, face, refined_ents_ptr, cOre);
1780 int side = 0;
1781 // Set face side if it is on interface
1782 CHKERR moab.tag_get_data(th_interface_side, &face, 1, &side);
1783 CHKERR moab.tag_set_data(th_interface_side, &rfit, 1, &side);
1784 break;
1785 }
1786 }
1787 if (ff < 6)
1788 continue; // this face is contained by one of tetrahedrons
1789
1790 // check if ref face is in coarse hex
1791 // this is ref face which is contained by hex volume (diagonal)
1792 if (intersect(hex_nodes, ref_faces_nodes).size() == 3) {
1793 CHKERR set_parent(rfit, hex, refined_ents_ptr, cOre);
1794 continue;
1795 }
1796 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1797 "impossible refined face");
1798 }
1799 }
1800 }
1801 CHKERR set_parent(refined_ents_ptr);
1802 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevel(
1803 ents_to_set_bit, bit, true, verb, &ents);
1805}
1806
1807} // namespace MoFEM
std::vector< int > source
std::vector< int > size
std::vector< int > parent
ParentChildKey key
moab::Interface & mb
Core & cOre
std::vector< double > empty
One value per component.
std::string type
#define MOFEM_TAG_AND_LOG(channel, severity, tag)
Tag and log in channel.
It it
Definition Schur.cpp:2568
It end
Definition Schur.cpp:2568
constexpr double a
@ NOISY
@ VERBOSE
@ VERY_NOISY
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_IMPOSSIBLE_CASE
Definition definitions.h:35
@ 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.
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
static const bool debug
multi_index_container< boost::shared_ptr< RefEntity >, indexed_by< hashed_non_unique< const_mem_fun< RefEntity, EntityHandle, &RefEntity::getParentEnt > >, hashed_unique< tag< Composite_EntType_and_ParentEntType_mi_tag >, composite_key< boost::shared_ptr< RefEntity >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getEnt >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getParentEnt > > > > > RefEntity_multiIndex_view_by_hashed_parent_entity
multi-index view of RefEntity by parent entity
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.
auto bit
set bit
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)
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITREFEDGES_SIZE > BitRefEdges
Definition Types.hpp:34
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 tet_type_3(const EntityHandle *conn, const int *split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_tets_conn)
multi_index_container< boost::shared_ptr< RefEntity >, indexed_by< ordered_unique< tag< Ent_mi_tag >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getEnt > >, ordered_non_unique< tag< Ent_Ent_mi_tag >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getParentEnt > >, ordered_non_unique< tag< Composite_EntType_and_ParentEntType_mi_tag >, composite_key< RefEntity, const_mem_fun< RefEntity, EntityType, &RefEntity::getEntType >, const_mem_fun< RefEntity, EntityType, &RefEntity::getParentEntType > > >, ordered_non_unique< tag< Composite_ParentEnt_And_EntType_mi_tag >, composite_key< RefEntity, const_mem_fun< RefEntity, EntityType, &RefEntity::getEntType >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getParentEnt > > > > > RefEntity_multiIndex
void tet_type_6(moab::Interface &moab, const EntityHandle *conn, const EntityHandle *edge_new_nodes, EntityHandle *new_tets_conn)
MoFEMErrorCode tri_type_2(const EntityHandle *conn, const int *split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_tris_conn)
void hex_to_tet_type_2(const EntityHandle *conn, EntityHandle *new_tets_conn)
MoFEMErrorCode prism_type_2(const EntityHandle *conn, const BitRefEdges split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_prism_conn)
MoFEMErrorCode tri_type_1(const EntityHandle *conn, const int split_edge, const EntityHandle edge_new_node, EntityHandle *new_tris_conn)
RefEntityTmp< 0 > RefEntity
MoFEMErrorCode prism_type_3(const EntityHandle *conn, const BitRefEdges split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_prism_conn)
void hex_to_tet_type_1(const EntityHandle *conn, EntityHandle *new_tets_conn)
MoFEMErrorCode tri_type_3(const EntityHandle *conn, const EntityHandle *edge_new_nodes, EntityHandle *new_tris_conn)
MoFEMErrorCode prism_type_1(const EntityHandle *conn, const BitRefEdges split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_prism_conn)
multi_index_container< boost::shared_ptr< RefElement >, indexed_by< ordered_unique< tag< Ent_mi_tag >, const_mem_fun< RefElement::interface_type_RefEntity, EntityHandle, &RefElement::getEnt > > > > RefElement_multiIndex
int tet_type_2(const EntityHandle *conn, const int *split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_tets_conn)
multi_index_container< boost::shared_ptr< RefElement >, indexed_by< ordered_unique< tag< Ent_mi_tag >, const_mem_fun< RefElement::interface_type_RefEntity, EntityHandle, &RefElement::getEnt > >, ordered_non_unique< tag< Ent_Ent_mi_tag >, const_mem_fun< RefElement::interface_type_RefEntity, EntityHandle, &RefElement::getParentEnt > >, ordered_non_unique< tag< Composite_ParentEnt_And_BitsOfRefinedEdges_mi_tag >, composite_key< RefElement, const_mem_fun< RefElement::interface_type_RefEntity, EntityHandle, &RefElement::getParentEnt >, const_mem_fun< RefElement, int, &RefElement::getBitRefEdgesUlong > > > > > RefElement_multiIndex_parents_view
int tet_type_5(moab::Interface &moab, const EntityHandle *conn, const EntityHandle *edge_new_nodes, EntityHandle *new_tets_conn)
const EntityHandle no_handle
No entity handle is indicated by zero handle, i.e. root meshset.
Definition Common.hpp:12
int tet_type_4(const EntityHandle *conn, const int *split_edges, const EntityHandle *edge_new_nodes, EntityHandle *new_tets_conn)
auto get_temp_meshset_ptr(moab::Interface &moab)
Create smart pointer to temporary meshset.
void tet_type_1(const EntityHandle *conn, const int split_edge, const EntityHandle edge_new_node, EntityHandle *new_tets_conn)
auto rangeInserter(const I f, const I s, boost::function< bool(I it)> tester, boost::function< MoFEMErrorCode(I f, I s)> inserter)
Insert ranges.
multi_index_container< boost::shared_ptr< RefEntity >, indexed_by< ordered_non_unique< const_mem_fun< RefEntity, EntityHandle, &RefEntity::getParentEnt > >, hashed_unique< tag< Composite_EntType_and_ParentEntType_mi_tag >, composite_key< boost::shared_ptr< RefEntity >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getEnt >, const_mem_fun< RefEntity, EntityHandle, &RefEntity::getParentEnt > > > > > RefEntity_multiIndex_view_by_ordered_parent_entity
constexpr IntegrationType I
Managing BitRefLevels.
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
virtual boost::shared_ptr< BasicEntityData > & get_basic_entity_data_ptr()=0
Get pointer to basic entity data.
Core (interface) class.
Definition Core.hpp:83
MoFEMErrorCode addPrismToDatabase(const EntityHandle prism, int verb=DEFAULT_VERBOSITY)
add prim element
Definition Core.cpp:565
Tag get_th_RefBitLevel() const
Definition Core.hpp:203
Tag get_th_RefParentHandle() const
Definition Core.hpp:202
Tag get_th_RefBitEdge() const
Definition Core.hpp:204
Deprecated interface functions.
MoFEMErrorCode operator()(const EntityHandle ent, const EntityHandle parent, const RefEntity_multiIndex *ref_ents_ptr, MoFEM::Core &cOre)
map< EntityHandle, EntityHandle > parentsToChange
Mesh refinement interface.
MoFEMErrorCode refineHexToTets(const Range &hexes, const BitRefLevel &bit, int verb=QUIET, const bool debug=false)
MoFEMErrorCode refineTetsHangingNodes(const Range &tets, const BitRefLevel &bit, int verb=QUIET, const bool debug=false)
refine TET in the meshset
MoFEMErrorCode setTopologicalParents(const Range &source_entities, const Range &child_entities, const BitRefLevel &output_bit, int verb=QUIET, const std::map< EntityHandle, EntityHandle > &moved_vertices={})
MoFEMErrorCode refineTets(const EntityHandle meshset, const BitRefLevel &bit, int verb=QUIET, const bool debug=false)
refine TET in the meshset
MoFEMErrorCode setParents(const Range &source_entities, const Range &child_entities, const std::map< EntityHandle, EntityHandle > &vertex_parents, const BitRefLevel &output_bit, int verb=QUIET)
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
MoFEMErrorCode addVerticesInTheMiddleOfEdges(const EntityHandle meshset, const BitRefLevel &bit, const bool recursive=false, int verb=QUIET, EntityHandle start_v=0)
make vertices in the middle of edges in meshset and add them to refinement levels defined by bit
MoFEMErrorCode refinePrisms(const EntityHandle meshset, const BitRefLevel &bit, int verb=QUIET)
refine PRISM in the meshset
MoFEMErrorCode refineTrisHangingNodes(const EntityHandle meshset, const BitRefLevel &bit, int verb=QUIET, const bool debug=false)
refine TRI in the meshset
MeshRefinement(const MoFEM::Core &core)
boost::function< MoFEMErrorCode(moab::Interface &moab, RefEntity_multiIndex_view_by_ordered_parent_entity &ref_parent_ents_view, EntityHandle tet, BitRefEdges &parent_edges_bit, EntityHandle *edge_new_nodes, int *split_edges)> SetEdgeBitsFun
Functions setting edges for refinemnt on enetity level.
MoFEMErrorCode refineTris(const EntityHandle meshset, const BitRefLevel &bit, int verb=QUIET, const bool debug=false)
refine triangles in the meshset
MoFEMErrorCode refineMeshset(const EntityHandle meshset, const BitRefLevel &bit, const bool recursive=false, int verb=QUIET)
refine meshset, i.e. add child of refined entities to meshset
keeps data about abstract PRISM finite element
keeps data about abstract refined finite element
interface_RefEntity< RefEntity > interface_type_RefEntity
virtual int getBitRefEdgesUlong() const
Struct keeps handle to refined handle.
base class for all interface classes
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.