libMesh
system_subset_by_subdomain.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 
19 
20 // C++ includes
21 
22 
23 // Local includes
24 #include "libmesh/system_subset_by_subdomain.h"
25 #include "libmesh/system.h"
26 #include "libmesh/dof_map.h"
27 #include "libmesh/parallel.h"
28 #include "libmesh/elem.h"
29 
30 namespace libMesh
31 {
32 // ------------------------------------------------------------
33 // SubdomainSelection implementation
36 {
37 }
38 
41 {
42 }
43 
45 SubdomainSelectionByList (const std::set<subdomain_id_type> & list):
46  _list(list)
47 {
48 }
49 
50 bool
52 operator()(const subdomain_id_type & subdomain_id)const
53 {
54  return _list.find(subdomain_id)!=_list.end();
55 }
56 
57 // ------------------------------------------------------------
58 // SystemSubsetBySubdomain implementation
59 
62  const SubdomainSelection & subdomain_selection,
63  const std::set<unsigned int> * const var_nums):
64  SystemSubset(system),
65  ParallelObject(system),
66  _var_nums(),
67  _dof_ids()
68 {
69  this->set_var_nums(var_nums);
70  this->init(subdomain_selection);
71 }
72 
75  const std::set<subdomain_id_type> & subdomain_ids,
76  const std::set<unsigned int> * const var_nums):
77  SystemSubset(system),
78  ParallelObject(system),
79  _var_nums(),
80  _dof_ids()
81 {
82  this->set_var_nums(var_nums);
83  this->init(subdomain_ids);
84 }
85 
88 {
89 }
90 
91 const std::vector<unsigned int> &
93 {
94  return _dof_ids;
95 }
96 
97 void
99 set_var_nums (const std::set<unsigned int> * const var_nums)
100 {
101  _var_nums.clear();
102 
103  if (var_nums != libmesh_nullptr)
104  _var_nums = *var_nums;
105 
106  else
107  {
108  for (unsigned int i=0; i<_system.n_vars(); i++)
109  _var_nums.insert(i);
110  }
111 }
112 
113 void
115 init (const SubdomainSelection & subdomain_selection)
116 {
117  _dof_ids.clear();
118 
119  std::vector<std::vector<dof_id_type>> dof_ids_per_processor(this->n_processors());
120 
121  const DofMap & dof_map = _system.get_dof_map();
122  std::vector<dof_id_type> dof_indices;
123 
124  const MeshBase & mesh = _system.get_mesh();
125  for (const auto & elem : mesh.active_local_element_ptr_range())
126  if (subdomain_selection(elem->subdomain_id()))
127  {
128  std::set<unsigned int>::const_iterator it = _var_nums.begin();
129  const std::set<unsigned int>::const_iterator itEnd = _var_nums.end();
130  for (; it!=itEnd; ++it)
131  {
132  dof_map.dof_indices (elem, dof_indices, *it);
133  for (std::size_t i=0; i<dof_indices.size(); i++)
134  {
135  const dof_id_type dof = dof_indices[i];
136  for (processor_id_type proc=0; proc<this->n_processors(); proc++)
137  if ((dof>=dof_map.first_dof(proc)) && (dof<dof_map.end_dof(proc)))
138  dof_ids_per_processor[proc].push_back(dof);
139  }
140  }
141  }
142 
143  /* Distribute information among processors. */
144  std::vector<Parallel::Request> request_per_processor(this->n_processors());
145  for (unsigned int proc=0; proc<this->n_processors(); proc++)
146  {
147  if (proc!=this->processor_id())
148  {
149  this->comm().send(proc,dof_ids_per_processor[proc],request_per_processor[proc]);
150  }
151  }
152  for (unsigned int proc=0; proc<this->n_processors(); proc++)
153  {
154  std::vector<dof_id_type> received_dofs;
155  if (proc==this->processor_id())
156  {
157  received_dofs = dof_ids_per_processor[proc];
158  }
159  else
160  {
161  this->comm().receive(proc,received_dofs);
162  }
163  for (std::size_t i=0; i<received_dofs.size(); i++)
164  _dof_ids.push_back(received_dofs[i]);
165  }
166 
167  /* Sort and unique the vector (using the same mechanism as in \p
168  DofMap::prepare_send_list()). */
169  std::sort(_dof_ids.begin(), _dof_ids.end());
170  std::vector<unsigned int>::iterator new_end = std::unique (_dof_ids.begin(), _dof_ids.end());
171  std::vector<unsigned int> (_dof_ids.begin(), new_end).swap (_dof_ids);
172 
173  /* Wait for sends to be complete. */
174  for (unsigned int proc=0; proc<this->n_processors(); proc++)
175  {
176  if (proc!=this->processor_id())
177  {
178  request_per_processor[proc].wait();
179  }
180  }
181 }
182 
183 void
185 init (const std::set<subdomain_id_type> & subdomain_ids)
186 {
187  SubdomainSelectionByList selection(subdomain_ids);
188  this->init(selection);
189 }
190 
191 } // namespace libMesh
void init(const SubdomainSelection &subdomain_selection)
Initializes the class.
void set_var_nums(const std::set< unsigned int > *const var_nums)
Sets _var_nums to either a copy of var_nums or, if that is NULL, a set of all variable numbers that o...
processor_id_type n_processors() const
MeshBase & mesh
uint8_t processor_id_type
Definition: id_types.h:99
const class libmesh_nullptr_t libmesh_nullptr
const System & _system
A reference to the System we belong to.
Definition: system_subset.h:75
The libMesh namespace provides an interface to certain functionality in the library.
virtual bool operator()(const subdomain_id_type &subdomain_id) const libmesh_override
Method that decides whether a given subdomain id is included in the subset or nor.
This is the MeshBase class.
Definition: mesh_base.h:68
virtual SimpleRange< element_iterator > active_local_element_ptr_range()=0
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:167
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map.h:535
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map.h:577
const MeshBase & get_mesh() const
Definition: system.h:2014
virtual const std::vector< unsigned int > & dof_ids() const libmesh_override
const DofMap & get_dof_map() const
Definition: system.h:2030
This is a base class for classes which represent subsets of the dofs of a System. ...
Definition: system_subset.h:42
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
void send(const unsigned int dest_processor_id, const T &buf, const MessageTag &tag=no_tag) const
Blocking-send to one processor with data-defined type.
Subclass for user-specified selection of subdomain ids to be included in a SystemSubset.
This class forms the base class for all other classes that are expected to be implemented in parallel...
std::set< unsigned int > _var_nums
The set of all variable numbers that are contained in the subset.
const std::set< subdomain_id_type > & _list
The actual list.
X_input swap(X_system)
const Parallel::Communicator & comm() const
Status receive(const unsigned int dest_processor_id, T &buf, const MessageTag &tag=any_tag) const
Blocking-receive from one processor with data-defined type.
unsigned int n_vars() const
Definition: system.h:2086
SystemSubsetBySubdomain(const System &system, const SubdomainSelection &subdomain_selection, const std::set< unsigned int > *const var_nums=libmesh_nullptr)
Constructor.
SubdomainSelectionByList(const std::set< subdomain_id_type > &list)
Constructor.
std::vector< unsigned int > _dof_ids
The actual set of the dof ids.
processor_id_type processor_id() const
uint8_t dof_id_type
Definition: id_types.h:64
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Fills the vector di with the global degree of freedom indices for the element.
Definition: dof_map.C:1917