libMesh
system_subset_by_subdomain.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 // 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 != nullptr)
104  _var_nums = *var_nums;
105 
106  else
107  for (auto i : IntRange<unsigned int>(0, _system.n_vars()))
108  _var_nums.insert(i);
109 }
110 
111 void
113 init (const SubdomainSelection & subdomain_selection)
114 {
115  _dof_ids.clear();
116 
117  std::vector<std::vector<dof_id_type>> dof_ids_per_processor(this->n_processors());
118 
119  const DofMap & dof_map = _system.get_dof_map();
120  std::vector<dof_id_type> dof_indices;
121 
122  const MeshBase & mesh = _system.get_mesh();
123  for (const auto & elem : mesh.active_local_element_ptr_range())
124  if (subdomain_selection(elem->subdomain_id()))
125  {
126  for (const auto & var_num : _var_nums)
127  {
128  dof_map.dof_indices (elem, dof_indices, var_num);
129  for (const auto & dof : dof_indices)
130  for (auto proc : IntRange<processor_id_type>(0, this->n_processors()))
131  if ((dof>=dof_map.first_dof(proc)) && (dof<dof_map.end_dof(proc)))
132  dof_ids_per_processor[proc].push_back(dof);
133  }
134  }
135 
136  /* Distribute information among processors. */
137  std::vector<Parallel::Request> request_per_processor(this->n_processors());
138  for (auto proc : IntRange<processor_id_type>(0, this->n_processors()))
139  if (proc!=this->processor_id())
140  this->comm().send(proc,dof_ids_per_processor[proc],request_per_processor[proc]);
141  for (auto proc : IntRange<processor_id_type>(0, this->n_processors()))
142  {
143  std::vector<dof_id_type> received_dofs;
144  if (proc==this->processor_id())
145  received_dofs = dof_ids_per_processor[proc];
146  else
147  this->comm().receive(proc,received_dofs);
148  _dof_ids.insert(_dof_ids.end(), received_dofs.begin(), received_dofs.end());
149  }
150 
151  /* Sort and unique the vector (using the same mechanism as in \p
152  DofMap::prepare_send_list()). */
153  std::sort(_dof_ids.begin(), _dof_ids.end());
154  std::vector<unsigned int>::iterator new_end = std::unique (_dof_ids.begin(), _dof_ids.end());
155  std::vector<unsigned int> (_dof_ids.begin(), new_end).swap (_dof_ids);
156 
157  /* Wait for sends to be complete. */
158  for (auto proc : IntRange<processor_id_type>(0, this->n_processors()))
159  {
160  if (proc!=this->processor_id())
161  {
162  request_per_processor[proc].wait();
163  }
164  }
165 }
166 
167 void
169 init (const std::set<subdomain_id_type> & subdomain_ids)
170 {
171  SubdomainSelectionByList selection(subdomain_ids);
172  this->init(selection);
173 }
174 
175 } // namespace libMesh
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::System::n_vars
unsigned int n_vars() const
Definition: system.h:2155
libMesh::SystemSubsetBySubdomain::_var_nums
std::set< unsigned int > _var_nums
The set of all variable numbers that are contained in the subset.
Definition: system_subset_by_subdomain.h:189
libMesh::DofMap::dof_indices
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:1967
libMesh::SystemSubsetBySubdomain::SubdomainSelection::SubdomainSelection
SubdomainSelection()
Constructor.
Definition: system_subset_by_subdomain.C:35
libMesh::SystemSubsetBySubdomain::SubdomainSelection::~SubdomainSelection
virtual ~SubdomainSelection()
Destructor.
Definition: system_subset_by_subdomain.C:40
libMesh::SystemSubsetBySubdomain::dof_ids
virtual const std::vector< unsigned int > & dof_ids() const override
Definition: system_subset_by_subdomain.C:92
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::SystemSubsetBySubdomain::SystemSubsetBySubdomain
SystemSubsetBySubdomain(const System &system, const SubdomainSelection &subdomain_selection, const std::set< unsigned int > *const var_nums=nullptr)
Constructor.
Definition: system_subset_by_subdomain.C:61
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::DofMap::first_dof
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map.h:650
libMesh::SystemSubsetBySubdomain::SubdomainSelectionByList::operator()
virtual bool operator()(const subdomain_id_type &subdomain_id) const override
Method that decides whether a given subdomain id is included in the subset or nor.
Definition: system_subset_by_subdomain.C:52
libMesh::SystemSubsetBySubdomain::_dof_ids
std::vector< unsigned int > _dof_ids
The actual set of the dof ids.
Definition: system_subset_by_subdomain.h:194
libMesh::IntRange
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::SystemSubset::_system
const System & _system
A reference to the System we belong to.
Definition: system_subset.h:75
libMesh::ParallelObject::n_processors
processor_id_type n_processors() const
Definition: parallel_object.h:100
libMesh::SystemSubsetBySubdomain::init
void init(const SubdomainSelection &subdomain_selection)
Initializes the class.
Definition: system_subset_by_subdomain.C:113
libMesh::System::get_mesh
const MeshBase & get_mesh() const
Definition: system.h:2083
libMesh::ParallelObject::processor_id
processor_id_type processor_id() const
Definition: parallel_object.h:106
libMesh::SystemSubset
This is a base class for classes which represent subsets of the dofs of a System.
Definition: system_subset.h:42
libMesh::SystemSubsetBySubdomain::SubdomainSelectionByList
Selection of subdomain ids by a list.
Definition: system_subset_by_subdomain.h:97
libMesh::SystemSubsetBySubdomain::set_var_nums
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 nullptr, a set of all variable numbers tha...
Definition: system_subset_by_subdomain.C:99
libMesh::SystemSubsetBySubdomain::~SystemSubsetBySubdomain
virtual ~SystemSubsetBySubdomain()
Destructor.
Definition: system_subset_by_subdomain.C:87
swap
void swap(Iterator &lhs, Iterator &rhs)
swap, used to implement op=
Definition: variant_filter_iterator.h:478
libMesh::DofMap
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:176
libMesh::SystemSubsetBySubdomain::SubdomainSelectionByList::SubdomainSelectionByList
SubdomainSelectionByList(const std::set< subdomain_id_type > &list)
Constructor.
Definition: system_subset_by_subdomain.C:45
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
libMesh::SystemSubsetBySubdomain::SubdomainSelection
Subclass for user-specified selection of subdomain ids to be included in a SystemSubset.
Definition: system_subset_by_subdomain.h:54
libMesh::TestClass
Definition: id_types.h:33
libMesh::ParallelObject
An object whose state is distributed along a set of processors.
Definition: parallel_object.h:55
libMesh::DofMap::end_dof
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map.h:692