36unsigned long long identity(moab::Interface &
mb, EntityHandle entity) {
37 const auto type =
mb.type_from_handle(entity);
39 if (
type == MBVERTEX) {
44 const EntityHandle *conn;
49 "Fixture corner IDs");
50 std::sort(ids.begin(), ids.end());
53 for (
int i = 0;
i != 4; ++
i)
54 key =
key * 2048 + (
i < ids.size() ? ids[
i] : 0);
63 const std::vector<std::array<EntityHandle, 4>> &
cells,
67 ReadUtilIface *reader;
69 EntityHandle start, *conn;
70 CHKERR reader->get_element_connect(
cells.size(), 4, MBTET, 0, start, conn);
71 for (
size_t i = 0;
i !=
cells.size(); ++
i)
73 CHKERR reader->update_adjacencies(start,
cells.size(), 4, conn);
74 tets.insert(start, start +
cells.size() - 1);
95 const int rank = pc.rank(),
size = pc.size();
96 std::vector<unsigned long long> local;
97 for (
auto entity :
source) {
98 local.push_back(identity(
mb, entity));
99 local.push_back(entity);
101 int count = local.size();
103 CHKERR MPI_Allgather(&count, 1, MPI_INT, counts.data(), 1, MPI_INT,
106 for (
int p = 0; p !=
size; ++p) {
110 std::vector<unsigned long long> all(total);
111 CHKERR MPI_Allgatherv(local.data(), count, MPI_UNSIGNED_LONG_LONG, all.data(),
112 counts.data(),
offsets.data(), MPI_UNSIGNED_LONG_LONG,
114 std::map<unsigned long long, std::map<int, EntityHandle>> copies;
115 for (
int p = 0; p !=
size; ++p)
117 copies[all[
i]][p] = all[
i + 1];
118 for (
auto entity :
source) {
119 const auto &residents = copies.at(identity(
mb, entity));
121 auto key = identity(
mb, entity);
124 for (
int i = 0;
i != 4; ++
i) {
125 const int id =
key % 2048;
128 owner = (
id - 3) / 2;
130 if (residents.size() == 1)
132 std::vector<int> procs;
133 std::vector<EntityHandle> handles;
134 for (
const auto &[p,
handle] : residents)
137 handles.push_back(
handle);
139 CHKERR pc.update_remote_data(entity, procs, handles);
140 unsigned char status = PSTATUS_SHARED;
143 (
mb.type_from_handle(entity) == MBVERTEX
144 ? (identity(
mb, entity) / (2048ULL * 2048 * 2048)) <= 2
145 : identity(
mb, entity) ==
146 (((1ULL * 2048 + 1) * 2048 + 2) * 2048) * 2048);
148 status |= PSTATUS_INTERFACE;
149 else if (
owner != rank)
152 status |= PSTATUS_NOT_OWNED;
153 if (residents.size() > 2) {
155 status |= PSTATUS_MULTISHARED;
156 std::array<int, MAX_SHARING_PROCS> ps;
157 std::array<EntityHandle, MAX_SHARING_PROCS> hs{};
160 hs[0] = residents.at(
owner);
162 for (
const auto &[p,
handle] : residents)
167 CHKERR mb.tag_set_data(pc.sharedps_tag(), &entity, 1, ps.data());
168 CHKERR mb.tag_set_data(pc.sharedhs_tag(), &entity, 1, hs.data());
172 std::map<std::vector<int>, std::vector<EntityHandle>> interfaces;
175 for (
auto entity :
source) {
178 if (!(
status & PSTATUS_INTERFACE))
180 const auto &residents = copies.at(identity(
mb, entity));
181 std::vector<int> procs;
182 for (
const auto &[p,
handle] : residents)
183 if (residents.
size() > 2 || p != rank)
185 interfaces[procs].push_back(entity);
187 CHKERR pc.create_interface_sets(interfaces);
188 std::set<unsigned int> peers;
189 CHKERR pc.get_interface_procs(peers,
true);
190 CHKERR pc.check_all_shared_handles();
212 double scale,
bool different_coordinates =
false) {
220 const int rank = pc->rank(),
size = pc->size();
221 const int cell_count = layout ==
"all_empty" ? 0
222 : layout ==
"empty" &&
size > 2 ?
size - 1
224 std::vector<int>
cells;
225 for (
int i = 0;
i != cell_count; ++
i)
226 if (layout ==
"replicated" ||
i == rank ||
227 (layout ==
"root_complete" && rank == 0))
229 std::set<int> vertex_ids;
231 for (
int id : {1, 2, 3 + 2 *
i, 4 + 2 *
i})
232 vertex_ids.insert(
id);
233 std::vector<int>
order(vertex_ids.begin(), vertex_ids.end());
237 std::vector<double> padding(3 * (rank + 1), -1);
239 CHKERR mb.create_vertices(padding.data(), rank + 1, unused);
240 CHKERR bits->setEntitiesBitRefLevel(unused, level(7));
242 std::map<int, EntityHandle> node;
243 if (!
order.empty()) {
244 ReadUtilIface *reader;
249 for (
size_t n = 0;
n !=
order.size(); ++
n) {
251 node[id] = start +
n;
252 const auto angle =
id < 3 ? 0 : .7 * (
id - 3);
256 CHKERR mb.tag_set_data(
mb.globalId_tag(), &node[
id], 1, &
id);
262 {node.at(1), node.at(2), node.at(3 + 2 *
i), node.at(4 + 2 *
i)});
265 CHKERR bits->setBitRefLevel(source_tets, level(0),
false);
270 std::map<EntityHandle, std::pair<int, unsigned char>> source_layout;
271 for (
auto entity :
source) {
272 auto &expected = source_layout[entity];
273 CHKERR pc->get_owner(entity, expected.first);
274 CHKERR pc->get_pstatus(entity, expected.second);
276 std::vector<EntityHandle> boundary_sets;
280 const EntityHandle endpoints[] = {node.at(1), node.at(2)};
281 CHKERR mb.get_adjacencies(endpoints, 2, 1,
false, edge,
282 moab::Interface::INTERSECT);
284 for (
int id : {101, 102}) {
285 CHKERR meshsets_manager->addMeshset(
BLOCKSET,
id,
"sync_boundary");
287 meshsets_manager->getCubitMeshsetPtr(
id,
BLOCKSET)->getMeshset();
289 boundary_sets.push_back(set);
292 std::vector<EntityHandle> partitions;
295 for (
size_t n = 0;
n !=
cells.size(); ++
n) {
297 CHKERR mb.create_meshset(MESHSET_SET, part);
298 const auto tag = pc->part_tag();
300 const auto tet = source_tets[
n];
301 CHKERR mb.add_entities(part, &tet, 1);
302 pc->partition_sets().insert(part);
303 partitions.push_back(part);
305 const bool generate = producer < 0 || producer == rank;
306 if (generate && !
cells.empty()) {
307 EntityHandle midpoint;
308 const double x[] = {.5 *
scale, 0, 0};
309 CHKERR mb.create_vertex(x, midpoint);
311 if (rank ==
size - 1) {
314 else if (invalid == 4)
317 CHKERR mb.tag_set_data(
mb.globalId_tag(), &midpoint, 1, &id);
318 if (different_coordinates && rank ==
size - 1) {
323 const EntityHandle endpoints[] = {node.at(1), node.at(2)};
324 CHKERR mb.get_adjacencies(endpoints, 2, 1,
false, edge,
325 moab::Interface::INTERSECT);
329 {node.at(1), midpoint, node.at(3 + 2 *
i), node.at(4 + 2 *
i)});
331 {midpoint, node.at(2), node.at(3 + 2 *
i), node.at(4 + 2 *
i)});
335 Range child_tets, children;
337 CHKERR bits->setBitRefLevel(child_tets, level(1),
false);
342 source, children, {{midpoint, edge.front()}}, level(1));
343 if (different_coordinates && rank ==
size - 1) {
344 const auto vertex = node.at(1);
350 if (rank ==
size - 1 && (invalid == 3 || invalid == 6)) {
351 EntityHandle wrong_parent = unused.front();
354 const EntityHandle endpoints[] = {node.at(1), node.at(3)};
355 CHKERR mb.get_adjacencies(endpoints, 2, 1,
false, wrong_edge,
356 moab::Interface::INTERSECT);
357 wrong_parent = wrong_edge.front();
360 CHKERR require(refs->modify(refs->find(midpoint),
362 "Could not prepare invalid ancestry");
364 if (rank ==
size - 1 && invalid == 5) {
365 const auto entity = source_tets.front();
366 const unsigned char status = 0;
370 const auto old_source_count =
source.size();
375 CHKERR mb.get_entities_by_handle(0, before);
376 CHKERR PetscPushErrorHandler(PetscReturnErrorHandler,
nullptr);
378 comm->synchroniseByParentChild(level(0), level(1));
379 CHKERR PetscPopErrorHandler();
381 "Invalid concurrent input was not rejected");
382 CHKERR mb.get_entities_by_handle(0, after);
383 CHKERR require(before == after,
"Rejected input changed the mesh");
389 for (
int repeat = 0; repeat != 2; ++repeat) {
390 CHKERR comm->synchroniseByParentChild(level(0), level(1));
391 Range children, unchanged;
396 "Source level changed during synchronisation");
397 for (
const auto &[entity, expected] : source_layout) {
403 "Source ownership or parallel status changed");
405 CHKERR require(children.num_of_type(MBTET) == 2 *
cells.size(),
406 "Missing or duplicate received child tetrahedra");
408 CHKERR require(children ==
previous,
"Repeated sync created entities");
411 CHKERR mb.get_entities_by_handle(0, database);
413 CHKERR require(database == previous_database,
414 "Repeated sync created extra meshsets or topology");
415 previous_database = database;
416 std::set<unsigned long long> identities;
417 for (
auto child : children) {
418 CHKERR require(identities.insert(identity(
mb, child)).second,
419 "Synchronisation duplicated an entity");
423 CHKERR mb.tag_get_data(bits->get_th_RefParentHandle(), &child, 1,
426 "Invalid local parent");
427 int child_owner, parent_owner;
428 CHKERR pc->get_owner(child, child_owner);
430 CHKERR require(child_owner == parent_owner,
"Inherited owner changed");
431 unsigned char child_status, parent_status;
432 CHKERR pc->get_pstatus(child, child_status);
434 CHKERR require((child_status & PSTATUS_GHOST) ==
435 (parent_status & PSTATUS_GHOST),
436 "Inherited ghost role changed");
438 const auto entry = refs->find(child);
439 CHKERR require(entry != refs->end() && (*entry)->getParentEnt() ==
parent,
440 "Refinement parent was not registered");
443 std::any_of(bucket.first, bucket.second,
444 [&](
const auto &e) { return e->getEnt() == child; }),
445 "Parent index does not contain received child");
447 for (
auto partition : partitions) {
449 CHKERR mb.get_entities_by_handle(partition, members);
450 CHKERR require(intersect(members, children).num_of_type(MBTET) == 2,
451 "Partition membership was not inherited");
453 for (
auto boundary : boundary_sets) {
456 CHKERR require(members.num_of_type(MBEDGE) == 3 &&
457 members.num_of_type(MBVERTEX) == 1,
458 "Overlapping BC sets missed common-edge descendants");
462 CHKERR mb.tag_get_handle(
"SYNC_TEST_VALUE", 1, MB_TYPE_INTEGER,
values,
463 MB_TAG_CREAT | MB_TAG_DENSE, &zero);
467 for (
auto child : children) {
470 const int value =
owner == rank ?
owner + 10 : -1;
474 for (
auto child : children) {
478 CHKERR require(value ==
owner + 10,
"MOAB tag exchange missed child");
480 CHKERR pc->check_all_shared_handles();
483 <<
"Parent-child sync passed: " << layout <<
", producer " << producer;
494int main(
int argc,
char **argv) {
496 "Parent-child communication checks\n");
508 CHKERR require(
scale > 0,
"JSON must supply synchronisation_scale");
510 CHKERR MPI_Comm_size(PETSC_COMM_WORLD, &
size);
511 for (
const auto &layout :
512 {
"distributed",
"replicated",
"root_complete",
"empty",
"all_empty"})
519 for (
int invalid = 3; invalid != 7; ++invalid)
CGALInterfaceImpl & owner
std::vector< int > source
bool angle
Radians between candidate face and target plane.
std::vector< int > vertices
std::vector< int > parent
std::vector< Cell > cells
std::array< int, 4 > connectivity
std::map< EntityHandle, size_t > offsets
std::map< EntityHandle, std::pair< bool, std::vector< double > > > previous
std::vector< std::array< double, 3 > > coordinates
SmartPetscObj< Vec > values
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MYPCOMM_INDEX
default communicator number PCOMM
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
@ 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
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
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
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
MoFEMErrorCode run(MoFEM::Interface &m_field)
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.
Mesh refinement interface.
Interface for managing meshsets containing materials and boundary conditions.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.