24 #include <unordered_map> 27 #include <sys/types.h> 62 const bool skip_find_neighbors,
66 #ifdef LIBMESH_ENABLE_UNIQUE_ID
71 LOG_SCOPE(
"copy_nodes_and_elements()",
"UnstructuredMesh");
87 MeshTools::libmesh_assert_valid_procids<Node>(other_mesh);
98 (wrap_proc_ids ? oldn->processor_id() %
_n_parts : oldn->processor_id());
101 #ifdef LIBMESH_ENABLE_UNIQUE_ID 105 oldn->id() + node_id_offset,
108 #ifdef LIBMESH_ENABLE_UNIQUE_ID 110 oldn->unique_id() + unique_id_offset;
121 typedef std::unordered_map<const Elem *, Elem *> map_type;
122 map_type old_elems_to_new_elems;
129 this->
elem_ptr(old->parent()->id() + element_id_offset) :
131 std::unique_ptr<Elem> ap =
Elem::build(old->type(), newparent);
132 Elem * el = ap.release();
136 for (
auto s : old->side_index_range())
138 el->set_neighbor(s, const_cast<RemoteElem *>(
remote_elem));
140 #ifdef LIBMESH_ENABLE_AMR 141 if (old->has_children())
142 for (
unsigned int c = 0, nc = old->n_children(); c != nc; ++c)
144 el->add_child(const_cast<RemoteElem *>(
remote_elem), c);
149 unsigned int oldc = old->parent()->which_child_am_i(old);
154 el->set_refinement_flag(old->refinement_flag());
158 el->hack_p_level(old->p_level());
160 el->set_p_refinement_flag(old->p_refinement_flag());
161 #endif // #ifdef LIBMESH_ENABLE_AMR 164 for (
auto i : el->node_index_range())
166 this->
node_ptr(old->node_id(i) + node_id_offset);
170 (wrap_proc_ids ? old->processor_id() %
_n_parts : old->processor_id());
173 el->set_id(old->id() + element_id_offset);
175 #ifdef LIBMESH_ENABLE_UNIQUE_ID 176 el->set_unique_id() =
177 old->unique_id() + unique_id_offset;
181 if (!skip_find_neighbors)
188 old_elems_to_new_elems[old] = new_el;
192 if (skip_find_neighbors)
193 old_elems_to_new_elems[old] = el;
197 if (skip_find_neighbors)
201 Elem * new_elem = old_elems_to_new_elems[old_elem];
202 for (
auto s : old_elem->side_index_range())
205 Elem * new_neighbor = old_elems_to_new_elems[old_neighbor];
241 const bool reset_current_list)
249 parallel_object_only();
251 LOG_SCOPE(
"find_neighbors()",
"Mesh");
254 if (reset_current_list)
256 for (
auto s : e->side_index_range())
257 if (e->neighbor_ptr(s) !=
remote_elem || reset_remote_elements)
258 e->set_neighbor(s,
nullptr);
265 typedef unsigned int key_type;
266 typedef std::pair<Elem *, unsigned char> val_type;
267 typedef std::pair<key_type, val_type> key_val_pair;
269 typedef std::unordered_multimap<key_type, val_type> map_type;
272 map_type side_to_elem_map;
275 std::unique_ptr<Elem> my_side, their_side;
279 for (
auto ms : element->side_index_range())
285 if (element->neighbor_ptr(ms) ==
nullptr ||
289 const unsigned int key = element->key(ms);
292 auto bounds = side_to_elem_map.equal_range(key);
296 if (bounds.first != bounds.second)
299 element->side_ptr(my_side, ms);
302 while (bounds.first != bounds.second)
305 Elem * neighbor = bounds.first->second.first;
308 const unsigned int ns = bounds.first->second.second;
321 if ((*my_side == *their_side) &&
322 (element->level() == neighbor->
level()) &&
323 ((element->dim() != 1) || (ns != ms)))
331 if (element->subactive() ==
336 element->set_neighbor (ms,neighbor);
339 else if (element->subactive())
341 element->set_neighbor(ms,neighbor);
347 side_to_elem_map.erase (bounds.first);
362 kvp.second.first = element;
363 kvp.second.second = cast_int<unsigned char>(ms);
364 side_to_elem_map.insert (kvp);
370 #ifdef LIBMESH_ENABLE_AMR 400 for (
unsigned int level = 1; level <
n_levels; ++level)
405 libmesh_assert(current_elem);
407 libmesh_assert(parent);
410 for (
auto s : current_elem->side_index_range())
412 if (current_elem->neighbor_ptr(s) ==
nullptr ||
429 (neigh->
level()+1) == current_elem->level())))
435 bool neigh_has_remote_children =
false;
438 neigh_has_remote_children =
true;
439 libmesh_assert(neigh_has_remote_children);
443 if (current_elem->active())
444 libmesh_assert_not_equal_to (current_elem->processor_id(),
452 else if (neigh && (current_elem->subactive() &&
465 bool found_neigh =
false;
466 for (
unsigned int c = 0, nc = neigh->
n_children();
467 !found_neigh && c != nc; ++c)
492 if ((!neigh->
active()) && (!current_elem->subactive()))
496 libMesh::err <<
"Bad element ID = " << current_elem->id()
497 <<
", Side " << s <<
", Bad neighbor ID = " << neigh->
id() << std::endl;
498 libMesh::err <<
"Bad element proc_ID = " << current_elem->processor_id()
499 <<
", Bad neighbor proc_ID = " << neigh->
processor_id() << std::endl;
500 libMesh::err <<
"Bad element size = " << current_elem->hmin()
501 <<
", Bad neighbor size = " << neigh->
hmin() << std::endl;
502 libMesh::err <<
"Bad element center = " << current_elem->centroid()
503 <<
", Bad neighbor center = " << neigh->
centroid() << std::endl;
505 << (current_elem->active()?
"Active":
"Ancestor")
506 <<
" Element at level " 507 << current_elem->level() << std::endl;
509 << (parent->
active()?
"active":
510 (parent->
subactive()?
"subactive":
"ancestor"))
512 << (neigh->
subactive()?
"subactive":
"ancestor")
513 <<
" neighbor at level " << neigh->
level()
516 libmesh_error_msg(
"Problematic mesh written to bad_mesh.gmv.");
524 if (current_elem->dim() >= LIBMESH_DIM)
528 current_elem->set_interior_parent(
nullptr);
556 current_elem->set_interior_parent
561 bool child_contains_our_nodes =
true;
562 for (
auto & n : current_elem->node_ref_range())
564 bool child_contains_this_node =
false;
565 for (
auto & cn : child.node_ref_range())
566 if (cn.absolute_fuzzy_equals
569 child_contains_this_node =
true;
572 if (!child_contains_this_node)
574 child_contains_our_nodes =
false;
578 if (child_contains_our_nodes)
580 current_elem->set_interior_parent(&child);
587 libmesh_assert(current_elem->interior_parent());
596 !reset_remote_elements);
605 bool skip_renumber_nodes_and_elements,
606 bool skip_find_neighbors)
614 if (
name.rfind(
".gmv") + 4 ==
name.size())
619 if (skip_renumber_nodes_and_elements)
622 libmesh_deprecated();
628 skip_find_neighbors);
635 LOG_SCOPE(
"write()",
"Mesh");
643 const std::vector<Number> & v,
644 const std::vector<std::string> & vn)
646 LOG_SCOPE(
"write()",
"Mesh");
668 <<
"mesh for a processor id (=" 671 <<
"the number of processors available for " 672 <<
"the calculation. (=" 708 libmesh_assert_not_equal_to (this->
n_nodes(), 0);
709 libmesh_assert_not_equal_to (this->
n_elem(), 0);
712 std::vector<boundary_id_type> bc_ids;
714 for (
const auto & old_elem :
as_range(it, it_end))
719 new_elem->
set_id() = old_elem->id();
720 #ifdef LIBMESH_ENABLE_UNIQUE_ID 728 libmesh_assert(new_elem);
731 for (
auto n : old_elem->node_index_range())
733 const dof_id_type this_node_id = old_elem->node_id(n);
738 #ifdef LIBMESH_ENABLE_UNIQUE_ID 743 old_elem->node_ptr(n)->processor_id());
745 #ifdef LIBMESH_ENABLE_UNIQUE_ID 755 for (
auto s : old_elem->side_index_range())
768 #ifdef LIBMESH_ENABLE_AMR 771 LOG_SCOPE (
"contract()",
"Mesh");
774 bool mesh_changed =
false;
828 #endif // #ifdef LIBMESH_ENABLE_AMR 843 START_LOG(
"all_first_order()",
"Mesh");
848 std::vector<bool> node_touched_by_me(this->
max_node_id(),
false);
855 libmesh_assert(so_elem);
863 (so_elem->type()), so_elem->parent()).release();
865 const unsigned short n_sides = so_elem->n_sides();
867 for (
unsigned short s=0; s != n_sides; ++s)
871 #ifdef LIBMESH_ENABLE_AMR 875 if (so_elem->has_children())
876 for (
unsigned int c = 0, nc = so_elem->n_children(); c != nc; ++c)
886 if (so_elem->parent())
889 so_elem->parent()->which_child_am_i(so_elem);
901 libmesh_assert_equal_to (lo_elem->
n_vertices(), so_elem->n_vertices());
908 for (
unsigned int v=0; v < so_elem->n_vertices(); v++)
910 lo_elem->
set_node(v) = so_elem->node_ptr(v);
911 node_touched_by_me[lo_elem->
node_id(v)] =
true;
918 for (
unsigned short s=0; s != n_sides; s++)
938 lo_elem->
set_id(so_elem->id());
939 #ifdef LIBMESH_ENABLE_UNIQUE_ID 949 if (!node_touched_by_me[
node->
id()])
957 STOP_LOG(
"all_first_order()",
"Mesh");
972 parallel_object_only();
997 bool already_second_order =
false;
1000 already_second_order =
true;
1001 this->
comm().
max(already_second_order);
1002 if (already_second_order)
1005 START_LOG(
"all_second_order()",
"Mesh");
1019 std::map<std::vector<dof_id_type>,
Node *> adj_vertices_to_so_nodes;
1035 (1.5*static_cast<double>(this->
n_nodes())));
1044 (2*static_cast<double>(this->
n_nodes())));
1056 (2.5*static_cast<double>(this->
n_nodes())));
1061 libmesh_error_msg(
"Unknown mesh dimension " << this->
mesh_dimension());
1073 std::vector<dof_id_type> adjacent_vertices_ids;
1085 for (; it != endit; ++it)
1088 Elem * lo_elem = *it;
1090 libmesh_assert(lo_elem);
1094 libmesh_error_msg(
"ERROR: This is not a linear element: type=" << lo_elem->
type());
1097 libmesh_assert_equal_to (lo_elem->
level (), 0);
1109 full_ordered) ).release();
1111 libmesh_assert_equal_to (lo_elem->
n_vertices(), so_elem->n_vertices());
1119 for (
unsigned int v=0; v < lo_elem->
n_vertices(); v++)
1120 so_elem->set_node(v) = lo_elem->
node_ptr(v);
1130 const unsigned int son_begin = so_elem->n_vertices();
1131 const unsigned int son_end = so_elem->n_nodes();
1134 for (
unsigned int son=son_begin; son<son_end; son++)
1136 const unsigned int n_adjacent_vertices =
1137 so_elem->n_second_order_adjacent_vertices(son);
1139 adjacent_vertices_ids.resize(n_adjacent_vertices);
1141 for (
unsigned int v=0; v<n_adjacent_vertices; v++)
1142 adjacent_vertices_ids[v] =
1143 so_elem->node_id( so_elem->second_order_adjacent_vertex(son,v) );
1151 std::sort(adjacent_vertices_ids.begin(),
1152 adjacent_vertices_ids.end());
1156 auto pos = adj_vertices_to_so_nodes.equal_range (adjacent_vertices_ids);
1159 if (pos.first == pos.second)
1168 Point new_location = this->
point(adjacent_vertices_ids[0]);
1169 for (
unsigned int v=1; v<n_adjacent_vertices; v++)
1170 new_location += this->
point(adjacent_vertices_ids[v]);
1172 new_location /=
static_cast<Real>(n_adjacent_vertices);
1195 adj_vertices_to_so_nodes.insert(pos.first,
1196 std::make_pair(adjacent_vertices_ids,
1199 so_elem->set_node(son) = so_node;
1204 Node * so_node = pos.first->second;
1205 libmesh_assert(so_node);
1207 so_elem->set_node(son) = so_node;
1237 so_elem->set_neighbor(s, const_cast<RemoteElem*>(
remote_elem));
1259 so_elem->set_id(lo_elem->
id());
1260 #ifdef LIBMESH_ENABLE_UNIQUE_ID 1261 so_elem->set_unique_id() = lo_elem->
unique_id();
1269 adj_vertices_to_so_nodes.clear();
1272 STOP_LOG(
"all_second_order()",
"Mesh");
std::string name(const ElemQuality q)
void set_p_level(const unsigned int p)
virtual void copy_nodes_and_elements(const UnstructuredMesh &other_mesh, const bool skip_find_neighbors=false, dof_id_type element_id_offset=0, dof_id_type node_id_offset=0, unique_id_type unique_id_offset=0)
unique_id_type & set_unique_id()
virtual void reserve_nodes(const dof_id_type nn)=0
const Elem * parent() const
virtual const Elem * elem(const dof_id_type i) const
Used by ParallelMesh to represent an Elem owned by another processor.
virtual Node *& set_node(const unsigned int i)
A geometric point in (x,y,z) space associated with a DOF.
bool is_ancestor_of(const Elem *descendant) const
virtual element_iterator level_elements_begin(unsigned int level)=0
const Elem * interior_parent() const
virtual void read(const std::string &mesh_file) override
void allow_renumbering(bool allow)
IntRange< unsigned short > side_index_range() const
void skip_partitioning(bool skip)
static void set_node_processor_ids(MeshBase &mesh)
virtual bool is_child_on_side(const unsigned int c, const unsigned int s) const =0
The base class for all geometric element types.
uint8_t processor_id_type
void add_child(Elem *elem)
void set_refinement_flag(const RefinementState rflag)
unique_id_type unique_id() const
const Parallel::Communicator & comm() const
virtual unsigned int n_children() const =0
static const Real TOLERANCE
virtual void own_node(Node &)
virtual void all_first_order() override
virtual void find_neighbors(const bool reset_remote_elements=false, const bool reset_current_list=true) override
void create_pid_mesh(UnstructuredMesh &pid_mesh, const processor_id_type pid) const
const BoundaryInfo & get_boundary_info() const
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
void set_interior_parent(Elem *p)
SimpleRange< ChildRefIter > child_ref_range()
std::vector< boundary_id_type > boundary_ids(const Node *node) const
virtual bool contract() override
virtual element_iterator elements_begin()=0
virtual element_iterator level_elements_end(unsigned int level)=0
void replace_child(Elem *elem, unsigned int c)
void allow_remote_element_removal(bool allow)
processor_id_type n_processors() const
virtual bool is_serial() const
virtual void delete_elem(Elem *e)=0
virtual SimpleRange< element_iterator > element_ptr_range()=0
virtual Real hmin() const
virtual element_iterator active_pid_elements_begin(processor_id_type proc_id)=0
Base class for Replicated and Distributed meshes.
virtual Elem * add_elem(Elem *e)=0
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
virtual element_iterator elements_end()=0
virtual const Node * query_node_ptr(const dof_id_type i) const =0
virtual SimpleRange< node_iterator > node_ptr_range()=0
void clear_point_locator()
virtual void write(const std::string &mesh_file) override
SimpleRange< I > as_range(const std::pair< I, I > &p)
static const dof_id_type invalid_id
virtual void delete_node(Node *n)=0
void prepare_for_use(const bool skip_renumber_nodes_and_elements=false, const bool skip_find_neighbors=false)
OStreamProxy err(std::cerr)
bool allow_remote_element_removal() const
void set_neighbor(const unsigned int i, Elem *n)
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
void regenerate_id_sets()
static ElemType second_order_equivalent_type(const ElemType et, const bool full_ordered=true)
unsigned int which_child_am_i(const Elem *e) const
bool skip_partitioning() const
virtual const Node & node(const dof_id_type i) const
virtual const Elem * elem_ptr(const dof_id_type i) const =0
UnstructuredMesh(const Parallel::Communicator &comm_in, unsigned char dim=1)
virtual Elem * insert_elem(Elem *e)=0
const Elem * neighbor_ptr(unsigned int i) const
std::set< GhostingFunctor * >::const_iterator ghosting_functors_begin() const
unsigned int level() const
virtual unsigned int n_vertices() const =0
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual element_iterator active_pid_elements_end(processor_id_type proc_id)=0
virtual std::unique_ptr< Elem > side_ptr(unsigned int i)=0
virtual ~UnstructuredMesh()
subdomain_id_type subdomain_id() const
void copy_boundary_ids(const BoundaryInfo &old_boundary_info, const Elem *const old_elem, const Elem *const new_elem)
const Node * node_ptr(const unsigned int i) const
void make_nodes_parallel_consistent(MeshBase &)
virtual bool is_replicated() const
void add_side(const dof_id_type elem, const unsigned short int side, const boundary_id_type id)
unsigned int mesh_dimension() const
virtual const Point & point(const dof_id_type i) const =0
SimpleRange< NeighborPtrIter > neighbor_ptr_range()
void set_p_refinement_flag(const RefinementState pflag)
bool allow_renumbering() const
virtual void delete_remote_elements()
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false) override
virtual dof_id_type max_node_id() const =0
virtual dof_id_type n_elem() const =0
virtual const Node * node_ptr(const dof_id_type i) const =0
processor_id_type processor_id() const
virtual Order default_order() const =0
OStreamProxy out(std::cout)
virtual void all_second_order(const bool full_ordered=true) override
static ElemType first_order_equivalent_type(const ElemType et)
processor_id_type processor_id() const
virtual ElemType type() const =0
long double min(long double a, double b)
A geometric point in (x,y,z) space.
dof_id_type node_id(const unsigned int i) const
virtual void reserve_elem(const dof_id_type ne)=0
virtual Point centroid() const
bool has_children() const
virtual void renumber_nodes_and_elements()=0
virtual dof_id_type n_nodes() const =0
void create_submesh(UnstructuredMesh &new_mesh, const const_element_iterator &it, const const_element_iterator &it_end) const
virtual void write(const std::string &name) override
const Elem * child_ptr(unsigned int i) const
std::set< GhostingFunctor * >::const_iterator ghosting_functors_end() const
const RemoteElem * remote_elem