system_subset_by_subdomain.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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
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 != 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  for (const auto & var_num : _var_nums)
129  {
130  dof_map.dof_indices (elem, dof_indices, var_num);
131  for (const auto & dof : dof_indices)
132  for (processor_id_type proc=0; proc<this->n_processors(); proc++)
133  if ((dof>=dof_map.first_dof(proc)) && (dof<dof_map.end_dof(proc)))
134  dof_ids_per_processor[proc].push_back(dof);
135  }
136  }
137 
138  /* Distribute information among processors. */
139  std::vector<Parallel::Request> request_per_processor(this->n_processors());
140  for (unsigned int proc=0; proc<this->n_processors(); proc++)
141  {
142  if (proc!=this->processor_id())
143  {
144  this->comm().send(proc,dof_ids_per_processor[proc],request_per_processor[proc]);
145  }
146  }
147  for (unsigned int proc=0; proc<this->n_processors(); proc++)
148  {
149  std::vector<dof_id_type> received_dofs;
150  if (proc==this->processor_id())
151  {
152  received_dofs = dof_ids_per_processor[proc];
153  }
154  else
155  {
156  this->comm().receive(proc,received_dofs);
157  }
158  for (std::size_t i=0; i<received_dofs.size(); i++)
159  _dof_ids.push_back(received_dofs[i]);
160  }
161 
162  /* Sort and unique the vector (using the same mechanism as in \p
163  DofMap::prepare_send_list()). */
164  std::sort(_dof_ids.begin(), _dof_ids.end());
165  std::vector<unsigned int>::iterator new_end = std::unique (_dof_ids.begin(), _dof_ids.end());
166  std::vector<unsigned int> (_dof_ids.begin(), new_end).swap (_dof_ids);
167 
168  /* Wait for sends to be complete. */
169  for (unsigned int proc=0; proc<this->n_processors(); proc++)
170  {
171  if (proc!=this->processor_id())
172  {
173  request_per_processor[proc].wait();
174  }
175  }
176 }
177 
178 void
180 init (const std::set<subdomain_id_type> & subdomain_ids)
181 {
182  SubdomainSelectionByList selection(subdomain_ids);
183  this->init(selection);
184 }
185 
186 } // namespace libMesh
void send(const unsigned int dest_processor_id, const T &buf, const MessageTag &tag=no_tag) const
void init(const SubdomainSelection &subdomain_selection)
void set_var_nums(const std::set< unsigned int > *const var_nums)
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition: dof_map.C:1930
virtual bool operator()(const subdomain_id_type &subdomain_id) const override
MeshBase & mesh
uint8_t processor_id_type
Definition: id_types.h:99
const Parallel::Communicator & comm() const
const System & _system
Definition: system_subset.h:75
const MeshBase & get_mesh() const
Definition: system.h:2033
Base class for Mesh.
Definition: mesh_base.h:77
Manages the degrees of freedom (DOFs) in a simulation.
Definition: dof_map.h:176
processor_id_type n_processors() const
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:92
virtual const std::vector< unsigned int > & dof_ids() const override
An object whose state is distributed along a set of processors.
void swap(Iterator &lhs, Iterator &rhs)
Status receive(const unsigned int dest_processor_id, T &buf, const MessageTag &tag=any_tag) const
SystemSubsetBySubdomain(const System &system, const SubdomainSelection &subdomain_selection, const std::set< unsigned int > *const var_nums=nullptr)
SubdomainSelectionByList(const std::set< subdomain_id_type > &list)
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map.h:599
unsigned int n_vars() const
Definition: system.h:2105
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map.h:641
processor_id_type processor_id() const
const DofMap & get_dof_map() const
Definition: system.h:2049