libMesh
system_subset_by_subdomain.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 
38 SubdomainSelectionByList (const std::set<subdomain_id_type> & list):
39  _list(list)
40 {
41 }
42 
43 bool
45 operator()(const subdomain_id_type & subdomain_id)const
46 {
47  return _list.count(subdomain_id); // _list is actually a std::set
48 }
49 
50 // ------------------------------------------------------------
51 // SystemSubsetBySubdomain implementation
52 
55  const SubdomainSelection & subdomain_selection,
56  const std::set<unsigned int> * const var_nums):
57  SystemSubset(system),
58  ParallelObject(system),
59  _var_nums(),
60  _dof_ids()
61 {
62  this->set_var_nums(var_nums);
63  this->init(subdomain_selection);
64 }
65 
68  const std::set<subdomain_id_type> & subdomain_ids,
69  const std::set<unsigned int> * const var_nums):
70  SystemSubset(system),
71  ParallelObject(system),
72  _var_nums(),
73  _dof_ids()
74 {
75  this->set_var_nums(var_nums);
76  this->init(subdomain_ids);
77 }
78 
80 
81 const std::vector<unsigned int> &
83 {
84  return _dof_ids;
85 }
86 
87 void
89 set_var_nums (const std::set<unsigned int> * const var_nums)
90 {
91  _var_nums.clear();
92 
93  if (var_nums != nullptr)
94  _var_nums = *var_nums;
95 
96  else
97  for (auto i : make_range(_system.n_vars()))
98  _var_nums.insert(i);
99 }
100 
101 void
103 init (const SubdomainSelection & subdomain_selection)
104 {
105  _dof_ids.clear();
106 
107  std::vector<std::vector<dof_id_type>> dof_ids_per_processor(this->n_processors());
108 
109  const DofMap & dof_map = _system.get_dof_map();
110  std::vector<dof_id_type> dof_indices;
111 
112  const MeshBase & mesh = _system.get_mesh();
113  for (const auto & elem : mesh.active_local_element_ptr_range())
114  if (subdomain_selection(elem->subdomain_id()))
115  {
116  for (const auto & var_num : _var_nums)
117  {
118  dof_map.dof_indices (elem, dof_indices, var_num);
119  for (const auto & dof : dof_indices)
120  for (auto proc : make_range(this->n_processors()))
121  if ((dof>=dof_map.first_dof(proc)) && (dof<dof_map.end_dof(proc)))
122  dof_ids_per_processor[proc].push_back(dof);
123  }
124  }
125 
126  /* Distribute information among processors. */
127  std::vector<Parallel::Request> request_per_processor(this->n_processors());
128  for (auto proc : make_range(this->n_processors()))
129  if (proc!=this->processor_id())
130  this->comm().send(proc,dof_ids_per_processor[proc],request_per_processor[proc]);
131  for (auto proc : make_range(this->n_processors()))
132  {
133  std::vector<dof_id_type> received_dofs;
134  if (proc==this->processor_id())
135  received_dofs = dof_ids_per_processor[proc];
136  else
137  this->comm().receive(proc,received_dofs);
138  _dof_ids.insert(_dof_ids.end(), received_dofs.begin(), received_dofs.end());
139  }
140 
141  /* Sort and unique the vector (using the same mechanism as in \p
142  DofMap::prepare_send_list()). */
143  std::sort(_dof_ids.begin(), _dof_ids.end());
144  std::vector<unsigned int>::iterator new_end = std::unique (_dof_ids.begin(), _dof_ids.end());
145  std::vector<unsigned int> (_dof_ids.begin(), new_end).swap (_dof_ids);
146 
147  /* Wait for sends to be complete. */
148  for (auto proc : make_range(this->n_processors()))
149  {
150  if (proc!=this->processor_id())
151  {
152  request_per_processor[proc].wait();
153  }
154  }
155 }
156 
157 void
159 init (const std::set<subdomain_id_type> & subdomain_ids)
160 {
161  SubdomainSelectionByList selection(subdomain_ids);
162  this->init(selection);
163 }
164 
165 } // namespace libMesh
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map_base.h:191
void init(const SubdomainSelection &subdomain_selection)
Initializes the class.
virtual ~SystemSubsetBySubdomain()
Destructor.
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...
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition: dof_map.C:2164
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.
MeshBase & mesh
const Parallel::Communicator & comm() const
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.
const MeshBase & get_mesh() const
Definition: system.h:2358
This is the MeshBase class.
Definition: mesh_base.h:75
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
processor_id_type n_processors() const
Status receive(const unsigned int dest_processor_id, T &buf, const MessageTag &tag=any_tag) const
This is a base class for classes which represent subsets of the dofs of a System. ...
Definition: system_subset.h:42
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
virtual const std::vector< unsigned int > & dof_ids() const override
Subclass for user-specified selection of subdomain ids to be included in a SystemSubset.
An object whose state is distributed along a set of processors.
std::set< unsigned int > _var_nums
The set of all variable numbers that are contained in the subset.
void send(const unsigned int dest_processor_id, const T &buf, const MessageTag &tag=no_tag) const
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition: int_range.h:140
SystemSubsetBySubdomain(const System &system, const SubdomainSelection &subdomain_selection, const std::set< unsigned int > *const var_nums=nullptr)
Constructor.
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map_base.h:185
SubdomainSelectionByList(const std::set< subdomain_id_type > &list)
Constructor.
unsigned int n_vars() const
Definition: system.h:2430
processor_id_type processor_id() const
const DofMap & get_dof_map() const
Definition: system.h:2374
std::vector< unsigned int > _dof_ids
The actual set of the dof ids.