132 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
135 if (!
mesh->preparation().has_cached_elem_data)
136 mesh->cache_elem_data();
139 auto max_node_id =
mesh->max_node_id();
142 if (!
mesh->is_replicated() && !
mesh->is_prepared())
143 mesh->comm().max(max_node_id);
144#if LIBMESH_ENABLE_UNIQUE_ID
145 const auto max_unique_id =
mesh->parallel_max_unique_id();
148 BoundaryInfo & boundary_info =
mesh->get_boundary_info();
153 for (
const auto & block_name_pair :
154 getParam<std::vector<std::vector<SubdomainName>>>(
"block_pairs"))
156 if (block_name_pair.size() != 2)
158 "Each row of 'block_pairs' must have a size of two (block names).");
161 for (
const auto &
name : block_name_pair)
163 paramError(
"block_pairs",
"The block '",
name,
"' was not found in the mesh");
166 std::pair<SubdomainID, SubdomainID> pair = std::make_pair(
167 std::min(block_pair[0], block_pair[1]), std::max(block_pair[0], block_pair[1]));
178 for (
const auto &
name : getParam<std::vector<SubdomainName>>(
"surrounding_blocks"))
180 paramError(
"surrounding_blocks",
"The block '",
name,
"' was not found in the mesh");
183 *
mesh,
getParam<std::vector<SubdomainName>>(
"surrounding_blocks"));
184 std::copy(surrounding_block_ids.begin(),
185 surrounding_block_ids.end(),
190 std::unordered_map<dof_id_type, std::vector<dof_id_type>> node_to_elem_map;
191 for (
const auto & elem :
mesh->active_element_ptr_range())
192 for (
const auto n : make_range(elem->n_nodes()))
193 node_to_elem_map[elem->node_id(n)].push_back(elem->id());
200 for (
const auto & [current_node_id, connected_elems] : node_to_elem_map)
202 const auto * current_node =
mesh->node_ptr(current_node_id);
207 auto it = nodeid_to_connected_blocks.find(current_node_id);
208 if (it == nodeid_to_connected_blocks.end())
215 auto connected_blocks = it->second;
219 connected_blocks.erase(Elem::invalid_subdomain_id);
221 const unsigned int node_multiplicity = connected_blocks.size();
224 if (node_multiplicity > 1)
228 auto subdomain_it = connected_blocks.begin();
229 subdomain_id_type reference_subdomain_id =
231 connected_blocks.find(Elem::invalid_subdomain_id) != connected_blocks.end()
232 ? Elem::invalid_subdomain_id
237 bool should_create_new_node =
true;
241 should_create_new_node =
false;
245 const auto & sets_blocks_for_this_node = it->second;
251 if (sets_blocks_for_this_node.size() == 2)
254 auto set_blocks_for_this_node_it = sets_blocks_for_this_node.begin();
255 subdomain_id_type block1 = *set_blocks_for_this_node_it;
256 subdomain_id_type block2 = *std::next(set_blocks_for_this_node_it);
260 should_create_new_node =
true;
265 unsigned int multiplicity_counter = node_multiplicity;
266 for (
auto elem_id : connected_elems)
269 if (multiplicity_counter == 0)
272 Elem * current_elem =
mesh->elem_ptr(elem_id);
276 if ((block_id != reference_subdomain_id) ||
280 Node * new_node =
nullptr;
282 std::vector<boundary_id_type> node_boundary_ids;
284 for (
const auto local_node_id : make_range(current_elem->n_nodes()))
285 if (current_elem->node_id(local_node_id) ==
288 if (should_create_new_node)
295 new_node = Node::build(*current_node,
296 mesh->is_replicated()
298 : (current_elem->subdomain_id() + 1) * max_node_id +
301#if LIBMESH_ENABLE_UNIQUE_ID
302 new_node->set_unique_id((current_elem->subdomain_id() + 1) * max_unique_id +
303 current_node->unique_id());
308 new_node->processor_id() = current_elem->processor_id();
309 mesh->add_node(new_node);
310 current_elem->set_node(local_node_id, new_node);
312 boundary_info.boundary_ids(current_node, node_boundary_ids);
313 boundary_info.add_node(new_node, node_boundary_ids);
315 multiplicity_counter--;
320 if (should_create_new_node)
322 for (
auto connected_elem_id : connected_elems)
324 Elem * connected_elem =
mesh->elem_ptr(connected_elem_id);
328 if (connected_elem->subdomain_id() == current_elem->subdomain_id() &&
329 connected_elem != current_elem)
331 for (
const auto local_node_id : make_range(connected_elem->n_nodes()))
332 if (connected_elem->node_id(local_node_id) ==
335 connected_elem->set_node(local_node_id, new_node);
345 for (
auto elem_id : connected_elems)
347 for (
auto connected_elem_id : connected_elems)
349 Elem * current_elem =
mesh->elem_ptr(elem_id);
350 Elem * connected_elem =
mesh->elem_ptr(connected_elem_id);
353 if (current_elem == connected_elem)
357 subdomain_id_type connected_elem_subid =
360 if (curr_elem_subid < connected_elem_subid)
362 if (current_elem->has_neighbor(connected_elem))
364 unsigned int side = current_elem->which_neighbor_am_i(connected_elem);
365 unsigned int connected_elem_side = connected_elem->which_neighbor_am_i(current_elem);
369 bool need_to_switch =
false;
372 connected_elem_subid = connected_elem->subdomain_id();
373 if (curr_elem_subid > connected_elem_subid)
375 connected_elem_subid = current_elem->subdomain_id();
376 curr_elem_subid = connected_elem->subdomain_id();
378 side = connected_elem->which_neighbor_am_i(current_elem);
380 connected_elem_side = current_elem->which_neighbor_am_i(connected_elem);
381 need_to_switch =
true;
385 std::pair<subdomain_id_type, subdomain_id_type> blocks_pair =
386 std::make_pair(curr_elem_subid, connected_elem_subid);
388 std::pair<subdomain_id_type, subdomain_id_type> blocks_pair2 =
389 std::make_pair(connected_elem_subid, curr_elem_subid);
391 auto add_boundary_sides =
392 [&](
const std::pair<subdomain_id_type, subdomain_id_type> & blocks_pair,
393 const std::pair<subdomain_id_type, subdomain_id_type> & blocks_pair2,
395 Elem * connected_elem,
397 unsigned int connected_elem_side,
402 !need_to_switch ? current_elem : connected_elem, side);
405 !need_to_switch ? connected_elem : current_elem, connected_elem_side);
412 add_boundary_sides(blocks_pair,
421 add_boundary_sides(blocks_pair,
446 if (!
mesh->is_serial())
447 mesh->remove_orphaned_nodes();
448 Partitioner::set_node_processor_ids(*
mesh);
450 mesh->unset_is_prepared();
452 return dynamic_pointer_cast<MeshBase>(
mesh);
458 BoundaryInfo & boundary_info =
mesh.get_boundary_info();
460 boundary_id_type boundary_id;
462 BoundaryName boundary_name;
464 const std::set<boundary_id_type> & ids = boundary_info.get_boundary_ids();
465 boundary_id_type new_boundaryID = ids.empty() ? 0 : *ids.rbegin() + 1;
469 mesh.comm().max(new_boundaryID);
470 mooseAssert(new_boundaryID >= 0,
"Invalid new boundary ID computed.");
474 std::vector<std::pair<subdomain_id_type, subdomain_id_type>> sorted_pairs(
477 for (
auto & boundary_side : sorted_pairs)
480 boundary_id = new_boundaryID;
482 mesh, boundary_side.first, boundary_side.second, boundary_name, boundary_id, boundary_info);
490 for (
const auto & [elem, side] : boundary_side_map->second)
491 boundary_info.add_side(elem, side, boundary_id);
496 boundary_info.parallel_sync_side_ids();
497 boundary_info.parallel_sync_node_ids();
502 const auto rev_pair = std::make_pair(sub_pair.second, sub_pair.first);
503 const bool has_reverse =
508 mooseAssert(boundary_id != reverse_boundary_id,
509 "Disjoint neighbor boundary pairs must use different boundary IDs.");
511 mesh.add_disjoint_neighbor_boundary_pairs(
512 boundary_id, reverse_boundary_id, RealVectorValue(0.0, 0.0, 0.0));
540 const std::unordered_map<dof_id_type, std::vector<dof_id_type>> & node_to_elem_map,
541 const MeshBase & mesh)
543 std::unordered_map<dof_id_type, std::set<subdomain_id_type>> nodeid_to_connected_blocks;
546 for (
const auto & [node_id, elem_ids] : node_to_elem_map)
548 std::set<subdomain_id_type> connected_blocks;
549 for (
const dof_id_type elem_id : elem_ids)
551 const Elem * current_elem =
mesh.elem_ptr(elem_id);
553 nodeid_to_connected_blocks[node_id].insert(block_id);
558 if (
mesh.is_replicated())
559 return nodeid_to_connected_blocks;
562 const auto mesh_pid =
mesh.processor_id();
567 using NodeConnectedBlocksTuple =
568 std::tuple<dof_id_type, std::vector<subdomain_id_type>, processor_id_type>;
569 std::map<processor_id_type, std::vector<NodeConnectedBlocksTuple>> to_owner;
570 for (
const auto & [node_id,
blocks] : nodeid_to_connected_blocks)
572 const Node * node =
mesh.node_ptr(node_id);
573 if (node && node->processor_id() != mesh_pid)
574 to_owner[node->processor_id()].emplace_back(
575 node_id, std::vector<subdomain_id_type>(
blocks.begin(),
blocks.end()), mesh_pid);
579 std::unordered_map<dof_id_type, std::set<processor_id_type>> subscribers;
580 Parallel::push_parallel_vector_data(
583 [&](processor_id_type,
const std::vector<NodeConnectedBlocksTuple> & recv_data)
585 for (
const auto & [node_id, blocks_vec, ghost_pid] : recv_data)
587 subscribers[node_id].insert(ghost_pid);
588 nodeid_to_connected_blocks[node_id].insert(blocks_vec.begin(), blocks_vec.end());
593 std::map<processor_id_type, std::vector<NodeConnectedBlocksPair>> from_owner;
594 for (
const auto & [node_id, sub_pids] : subscribers)
596 const Node * node =
mesh.node_ptr(node_id);
597 if (node && node->processor_id() == mesh_pid)
599 const auto & blocks_set = libmesh_map_find(nodeid_to_connected_blocks, node_id);
600 for (
const auto pid : sub_pids)
601 from_owner[pid].emplace_back(
602 node_id, std::vector<subdomain_id_type>(blocks_set.begin(), blocks_set.end()));
606 Parallel::push_parallel_vector_data(
609 [&](processor_id_type,
const std::vector<NodeConnectedBlocksPair> & recv_data)
611 for (
const auto & [node_id, blocks_vec] : recv_data)
612 nodeid_to_connected_blocks[node_id].insert(blocks_vec.begin(), blocks_vec.end());
614 return nodeid_to_connected_blocks;