libMesh
default_coupling.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/default_coupling.h"
22 #include "libmesh/coupling_matrix.h"
23 #include "libmesh/elem.h"
24 #include "libmesh/periodic_boundaries.h"
25 #include "libmesh/remote_elem.h"
26 #include "libmesh/int_range.h"
27 #include "libmesh/libmesh_logging.h"
28 
29 // C++ Includes
30 #include <unordered_set>
31 
32 namespace libMesh
33 {
34 
36 {
37  // We used to treat an empty 0x0 _dof_coupling matrix as if it
38  // were an NxN all-ones matrix. We'd like to stop supporting this
39  // behavior, but for now we'll just warn about it, while supporting
40  // it via the preferred mechanism: a nullptr _dof_coupling
41  // matrix pointer is interpreted as a full coupling matrix.
42  if (dof_coupling && dof_coupling->empty())
43  {
44  libmesh_deprecated();
45  _dof_coupling = nullptr;
46  }
47  else
48  _dof_coupling = dof_coupling;
49 }
50 
51 
52 
54 {
55  // Unless we have periodic boundary conditions, we don't need
56  // anything precomputed.
57 #ifdef LIBMESH_ENABLE_PERIODIC
58  if (!_periodic_bcs || _periodic_bcs->empty())
59  return;
60 #endif
61 
62  // If we do have periodic boundary conditions, we'll need a master
63  // point locator, so we'd better have a mesh to build it on.
65 
66  // Make sure an up-to-date master point locator has been
67  // constructed; we'll need to grab sub-locators soon.
69 }
70 
71 
72 
73 void DefaultCoupling::operator()
74  (const MeshBase::const_element_iterator & range_begin,
75  const MeshBase::const_element_iterator & range_end,
77  map_type & coupled_elements)
78 {
79  LOG_SCOPE("operator()", "DefaultCoupling");
80 
81 #ifdef LIBMESH_ENABLE_PERIODIC
82  bool check_periodic_bcs =
83  (_periodic_bcs && !_periodic_bcs->empty());
84 
85  std::unique_ptr<PointLocatorBase> point_locator;
86  if (check_periodic_bcs)
87  {
88  libmesh_assert(_mesh);
89  point_locator = _mesh->sub_point_locator();
90  }
91 #endif
92 
93  if (!this->_n_levels)
94  {
95  for (const auto & elem : as_range(range_begin, range_end))
96  if (elem->processor_id() != p)
97  coupled_elements.insert (std::make_pair(elem,_dof_coupling));
98  return;
99  }
100 
101  typedef std::unordered_set<const Elem*> set_type;
102  set_type next_elements_to_check(range_begin, range_end);
103  set_type elements_to_check;
104  set_type elements_checked;
105 
106  for (unsigned int i=0; i != this->_n_levels; ++i)
107  {
108  elements_to_check.swap(next_elements_to_check);
109  next_elements_to_check.clear();
110  elements_checked.insert(elements_to_check.begin(), elements_to_check.end());
111 
112  for (const auto & elem : elements_to_check)
113  {
114  std::vector<const Elem *> active_neighbors;
115 
116  if (elem->processor_id() != p)
117  coupled_elements.insert (std::make_pair(elem,_dof_coupling));
118 
119  for (auto s : elem->side_index_range())
120  {
121  const Elem * neigh = elem->neighbor_ptr(s);
122 
123  // If we have a neighbor here
124  if (neigh)
125  {
126  // Mesh ghosting might ask us about what we want to
127  // distribute along with non-local elements, and those
128  // non-local elements might have remote neighbors, and
129  // if they do then we can't say anything about them.
130  if (neigh == remote_elem)
131  continue;
132  }
133 #ifdef LIBMESH_ENABLE_PERIODIC
134  // We might still have a periodic neighbor here
135  else if (check_periodic_bcs)
136  {
137  libmesh_assert(_mesh);
138 
139  neigh = elem->topological_neighbor
140  (s, *_mesh, *point_locator, _periodic_bcs);
141  }
142 #endif
143 
144  // With no regular *or* periodic neighbors we have nothing
145  // to do.
146  if (!neigh)
147  continue;
148 
149  // With any kind of neighbor, we need to couple to all the
150  // active descendants on our side.
151 #ifdef LIBMESH_ENABLE_AMR
152  if (neigh == elem->neighbor_ptr(s))
153  neigh->active_family_tree_by_neighbor(active_neighbors,elem);
154 # ifdef LIBMESH_ENABLE_PERIODIC
155  else
157  (active_neighbors,elem,*_mesh,*point_locator,_periodic_bcs);
158 # endif
159 #else
160  active_neighbors.clear();
161  active_neighbors.push_back(neigh);
162 #endif
163 
164  for (const auto & neighbor : active_neighbors)
165  {
166  if (!elements_checked.count(neighbor))
167  next_elements_to_check.insert(neighbor);
168 
169  if (neighbor->processor_id() != p)
170  coupled_elements.insert
171  (std::make_pair(neighbor, _dof_coupling));
172  }
173  }
174  }
175  }
176 }
177 
178 
179 } // namespace libMesh
libMesh::DefaultCoupling::_mesh
const MeshBase * _mesh
Definition: default_coupling.h:111
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::GhostingFunctor::map_type
std::unordered_map< const Elem *, const CouplingMatrix * > map_type
What elements do we care about and what variables do we care about on each element?
Definition: ghosting_functor.h:171
libMesh::DefaultCoupling::_dof_coupling
const CouplingMatrix * _dof_coupling
Definition: default_coupling.h:107
libMesh::DefaultCoupling::set_dof_coupling
void set_dof_coupling(const CouplingMatrix *dof_coupling)
Definition: default_coupling.C:35
libMesh::CouplingMatrix::empty
bool empty() const
Definition: coupling_matrix.h:631
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::Elem::active_family_tree_by_topological_neighbor
void active_family_tree_by_topological_neighbor(std::vector< const Elem * > &family, const Elem *neighbor, const MeshBase &mesh, const PointLocatorBase &point_locator, const PeriodicBoundaries *pb, bool reset=true) const
Same as the active_family_tree_by_neighbor() member, but the neighbor here may be a topological (e....
Definition: elem.C:1619
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
libMesh::as_range
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
Helper function that allows us to treat a homogenous pair as a range.
Definition: simple_range.h:57
libMesh::MeshBase::const_element_iterator
The definition of the const_element_iterator struct.
Definition: mesh_base.h:1891
libMesh::MeshBase::sub_point_locator
std::unique_ptr< PointLocatorBase > sub_point_locator() const
Definition: mesh_base.C:672
libMesh::DefaultCoupling::mesh_reinit
virtual void mesh_reinit() override
If we have periodic boundaries, then we'll need the mesh to have an updated point locator whenever we...
Definition: default_coupling.C:53
libMesh::Elem::topological_neighbor
const Elem * topological_neighbor(const unsigned int i, const MeshBase &mesh, const PointLocatorBase &point_locator, const PeriodicBoundaries *pb) const
Definition: elem.C:865
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
libMesh::CouplingMatrix
This class defines a coupling matrix.
Definition: coupling_matrix.h:54
libMesh::Elem::neighbor_ptr
const Elem * neighbor_ptr(unsigned int i) const
Definition: elem.h:2085
libMesh::DefaultCoupling::_periodic_bcs
const PeriodicBoundaries * _periodic_bcs
Definition: default_coupling.h:109
libMesh::Elem::active_family_tree_by_neighbor
void active_family_tree_by_neighbor(std::vector< const Elem * > &family, const Elem *neighbor, bool reset=true) const
Same as the active_family_tree() member, but only adds elements which are next to neighbor.
Definition: elem.C:1601
libMesh::remote_elem
const RemoteElem * remote_elem
Definition: remote_elem.C:57