libMesh
topology_map.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // Local Includes
21 #include "libmesh/elem.h"
22 #include "libmesh/topology_map.h"
23 #include "libmesh/mesh_base.h"
24 #include "libmesh/node.h"
25 #include "libmesh/parallel_only.h"
26 #include "libmesh/remote_elem.h"
27 #include "libmesh/libmesh_logging.h"
28 
29 // C++ Includes
30 #include <limits>
31 #include <utility>
32 
33 namespace libMesh
34 {
35 
37 {
38  // This function must be run on all processors at once
39  // for non-serial meshes
40  if (!mesh.is_serial())
41  libmesh_parallel_only(mesh.comm());
42 
43  LOG_SCOPE("init()", "TopologyMap");
44 
45  // Clear the old map
46  _map.clear();
47 
48  this->fill(mesh);
49 }
50 
51 
52 
53 void TopologyMap::add_node(const Node & mid_node,
54  const std::vector<std::pair<dof_id_type, dof_id_type>> & bracketing_nodes)
55 {
56  const dof_id_type mid_node_id = mid_node.id();
57 
58  libmesh_assert_not_equal_to(mid_node_id, DofObject::invalid_id);
59 
60  for (auto pair : bracketing_nodes)
61  {
62  const dof_id_type id1 = pair.first;
63  const dof_id_type id2 = pair.second;
64  const dof_id_type lower_id = std::min(id1, id2);
65  const dof_id_type upper_id = std::max(id1, id2);
66 
67  // We should never be inserting inconsistent data
68 #ifndef NDEBUG
69  map_type::iterator it =
70  _map.find(std::make_pair(lower_id, upper_id));
71 
72  if (it != _map.end())
73  libmesh_assert_equal_to (it->second, mid_node_id);
74 #endif
75 
76  this->_map.insert(std::make_pair(std::make_pair(lower_id, upper_id),
77  mid_node_id));
78 
79  }
80 }
81 
82 
83 dof_id_type TopologyMap::find(const std::vector<std::pair<dof_id_type, dof_id_type>> & bracketing_nodes) const
84 {
85  dof_id_type new_node_id = DofObject::invalid_id;
86 
87  for (auto pair : bracketing_nodes)
88  {
89  const dof_id_type lower_id = std::min(pair.first, pair.second);
90  const dof_id_type upper_id = std::max(pair.first, pair.second);
91 
92  const dof_id_type possible_new_node_id =
93  this->find(lower_id, upper_id);
94 
95  if (possible_new_node_id != DofObject::invalid_id)
96  {
97  // If we found a node already, but we're still here, it's to
98  // debug map consistency: we'd better always find the same
99  // node
100  if (new_node_id != DofObject::invalid_id)
101  libmesh_assert_equal_to (new_node_id, possible_new_node_id);
102 
103  new_node_id = possible_new_node_id;
104  }
105 
106  // If we're not debugging map consistency then we can quit as
107  // soon as we find a node
108 #ifdef NDEBUG
109  if (new_node_id != DofObject::invalid_id)
110  break;
111 #endif
112  }
113  return new_node_id;
114 }
115 
116 
118  dof_id_type bracket_node2) const
119 {
120  const dof_id_type lower_id = std::min(bracket_node1, bracket_node2);
121  const dof_id_type upper_id = std::max(bracket_node1, bracket_node2);
122 
123  map_type::const_iterator it =
124  _map.find(std::make_pair(lower_id, upper_id));
125 
126  if (it == _map.end())
127  return DofObject::invalid_id;
128 
129  libmesh_assert_not_equal_to (it->second, DofObject::invalid_id);
130 
131  return it->second;
132 }
133 
134 
135 
136 #ifdef LIBMESH_ENABLE_AMR
137 
139 {
140  // Populate the nodes map
141  for (const auto & elem : mesh.element_ptr_range())
142  {
143  // We only need to add nodes which might be added during mesh
144  // refinement; this means they need to be child nodes.
145  if (!elem->has_children())
146  continue;
147 
148  for (unsigned int c = 0, nc = elem->n_children(); c != nc; ++c)
149  {
150  const Elem * child = elem->child_ptr(c);
151  if (child == remote_elem)
152  continue;
153 
154  for (unsigned int n = 0, nnic = elem->n_nodes_in_child(c); n != nnic; ++n)
155  {
156  const std::vector<std::pair<dof_id_type, dof_id_type>>
157  bracketing_nodes = elem->bracketing_nodes(c,n);
158 
159  this->add_node(child->node_ref(n), bracketing_nodes);
160  }
161  }
162  }
163 }
164 
165 #else
166 
167 // no-op without AMR
168 void TopologyMap::fill(const MeshBase &) {}
169 
170 #endif
171 
172 
173 
174 } // namespace libMesh
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::Elem::child_ptr
const Elem * child_ptr(unsigned int i) const
Definition: elem.h:2567
libMesh::Elem::bracketing_nodes
virtual const std::vector< std::pair< dof_id_type, dof_id_type > > bracketing_nodes(unsigned int c, unsigned int n) const
Definition: elem.C:1999
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::TopologyMap::_map
map_type _map
Definition: topology_map.h:100
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::DofObject::invalid_id
static const dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition: dof_object.h:421
libMesh::Node
A Node is like a Point, but with more information.
Definition: node.h:52
libMesh::TopologyMap::fill
void fill(const MeshBase &)
Definition: topology_map.C:138
libMesh::TopologyMap::add_node
void add_node(const Node &mid_node, const std::vector< std::pair< dof_id_type, dof_id_type >> &bracketing_nodes)
Add a node to the map, between each pair of specified bracketing nodes.
Definition: topology_map.C:53
libMesh::TopologyMap::init
void init(MeshBase &)
Definition: topology_map.C:36
libMesh::DofObject::id
dof_id_type id() const
Definition: dof_object.h:767
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
libMesh::TopologyMap::find
dof_id_type find(dof_id_type bracket_node1, dof_id_type bracket_node2) const
Definition: topology_map.C:117
libMesh::Elem::node_ref
const Node & node_ref(const unsigned int i) const
Definition: elem.h:2031
libMesh::remote_elem
const RemoteElem * remote_elem
Definition: remote_elem.C:57