libMesh
centroid_partitioner.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 // C++ includes
19 #include <algorithm> // for std::sort
20 
21 // Local includes
22 #include "libmesh/centroid_partitioner.h"
23 #include "libmesh/elem.h"
24 
25 namespace libMesh
26 {
27 
28 
32  unsigned int n)
33 {
34  // Check for an easy return
35  if (n == 1)
36  {
37  this->single_partition_range (it, end);
38  return;
39  }
40 
41  // Compute the element centroids. Note: we used to skip this step
42  // if the number of elements was unchanged from the last call, but
43  // that doesn't account for elements that have moved a lot since the
44  // last time the Partitioner was called...
45  this->compute_centroids (it, end);
46 
47  switch (this->sort_method())
48  {
49  case X:
50  {
51  std::sort(_elem_centroids.begin(),
52  _elem_centroids.end(),
54 
55  break;
56  }
57 
58 
59  case Y:
60  {
61  std::sort(_elem_centroids.begin(),
62  _elem_centroids.end(),
64 
65  break;
66 
67  }
68 
69 
70  case Z:
71  {
72  std::sort(_elem_centroids.begin(),
73  _elem_centroids.end(),
75 
76  break;
77  }
78 
79 
80  case RADIAL:
81  {
82  std::sort(_elem_centroids.begin(),
83  _elem_centroids.end(),
85 
86  break;
87  }
88  default:
89  libmesh_error_msg("Unknown sort method: " << this->sort_method());
90  }
91 
92  // Make sure the user has not handed us an
93  // invalid number of partitions.
94  libmesh_assert_greater (n, 0);
95 
96  // Compute target_size, the approximate number of elements on each processor.
97  const dof_id_type target_size = _elem_centroids.size() / n;
98 
99  for (dof_id_type i=0; i<_elem_centroids.size(); i++)
100  {
101  Elem * elem = _elem_centroids[i].second;
102 
103  // FIXME: All "extra" elements go on the last processor... this
104  // could probably be improved.
105  elem->processor_id() =
106  std::min (cast_int<processor_id_type>(i / target_size),
107  cast_int<processor_id_type>(n-1));
108  }
109 }
110 
111 
112 
114  const unsigned int n)
115 {
116  this->partition_range(mesh,
117  mesh.elements_begin(),
118  mesh.elements_end(),
119  n);
120 }
121 
122 
123 
126 {
127  _elem_centroids.clear();
128 
129  for (; it != end; ++it)
130  {
131  Elem * elem = *it;
132 
133  _elem_centroids.push_back(std::make_pair(elem->centroid(), elem));
134  }
135 }
136 
137 
138 
139 
140 bool CentroidPartitioner::sort_x (const std::pair<Point, Elem *> & lhs,
141  const std::pair<Point, Elem *> & rhs)
142 {
143  return (lhs.first(0) < rhs.first(0));
144 }
145 
146 
147 
148 
149 bool CentroidPartitioner::sort_y (const std::pair<Point, Elem *> & lhs,
150  const std::pair<Point, Elem *> & rhs)
151 {
152  return (lhs.first(1) < rhs.first(1));
153 }
154 
155 
156 
157 
158 
159 bool CentroidPartitioner::sort_z (const std::pair<Point, Elem *> & lhs,
160  const std::pair<Point, Elem *> & rhs)
161 {
162  return (lhs.first(2) < rhs.first(2));
163 }
164 
165 
166 
167 bool CentroidPartitioner::sort_radial (const std::pair<Point, Elem *> & lhs,
168  const std::pair<Point, Elem *> & rhs)
169 {
170  return (lhs.first.norm() < rhs.first.norm());
171 }
172 
173 } // namespace libMesh
The definition of the element_iterator struct.
Definition: mesh_base.h:1476
std::vector< std::pair< Point, Elem * > > _elem_centroids
Vector which holds pairs of centroids and their respective element pointers.
This is the base class from which all geometric element types are derived.
Definition: elem.h:89
MeshBase & mesh
virtual void _do_partition(MeshBase &mesh, const unsigned int n) libmesh_override
Partitions the mesh into n subdomains.
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end...
The libMesh namespace provides an interface to certain functionality in the library.
static bool sort_y(const std::pair< Point, Elem * > &lhs, const std::pair< Point, Elem * > &rhs)
Helper function which sorts by the centroid&#39;s y-coordinate in the internal std::sort call...
This is the MeshBase class.
Definition: mesh_base.h:68
virtual element_iterator elements_begin()=0
Iterate over all the elements in the Mesh.
void single_partition_range(MeshBase::element_iterator it, MeshBase::element_iterator end)
Slightly generalized version of single_partition which acts on a range of elements defined by the pai...
Definition: partitioner.C:159
virtual element_iterator elements_end()=0
static bool sort_radial(const std::pair< Point, Elem * > &lhs, const std::pair< Point, Elem * > &rhs)
Helper function which sorts by the centroid&#39;s distance from the origin in the internal std::sort call...
static bool sort_z(const std::pair< Point, Elem * > &lhs, const std::pair< Point, Elem * > &rhs)
Helper function which sorts by the centroid&#39;s z-coordinate in the internal std::sort call...
CentroidSortMethod sort_method() const
Getter for the current sorting method.
static bool sort_x(const std::pair< Point, Elem * > &lhs, const std::pair< Point, Elem * > &rhs)
Helper function which sorts by the centroid&#39;s x-coordinate in the internal std::sort call...
virtual Point centroid() const
Definition: elem.C:446
long double min(long double a, double b)
virtual void partition_range(MeshBase &mesh, MeshBase::element_iterator it, MeshBase::element_iterator end, const unsigned int n) libmesh_override
Called by the SubdomainPartitioner to partition elements in the range (it, end).
uint8_t dof_id_type
Definition: id_types.h:64
processor_id_type processor_id() const
Definition: dof_object.h:694
void compute_centroids(MeshBase::element_iterator it, MeshBase::element_iterator end)
Computes a list of element centroids for the mesh.