system_subset_by_subdomain.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 
29 namespace libMesh
30 {
31 // ------------------------------------------------------------
32 // SubdomainSelection implementation
35  {
36  }
37 
40  {
41  }
42 
44  SubdomainSelectionByList (const std::set<subdomain_id_type>& list):
45  _list(list)
46  {
47  }
48 
49  bool
51  operator()(const subdomain_id_type& subdomain_id)const
52  {
53  return _list.find(subdomain_id)!=_list.end();
54  }
55 
56 // ------------------------------------------------------------
57 // SystemSubsetBySubdomain implementation
58 
61  const SubdomainSelection& subdomain_selection,
62  const std::set<unsigned int>* const var_nums):
63  SystemSubset(system),
64  ParallelObject(system),
65  _var_nums(),
66  _dof_ids()
67  {
68  this->set_var_nums(var_nums);
69  this->init(subdomain_selection);
70  }
71 
74  const std::set<subdomain_id_type>& subdomain_ids,
75  const std::set<unsigned int>* const var_nums):
76  SystemSubset(system),
77  ParallelObject(system),
78  _var_nums(),
79  _dof_ids()
80  {
81  this->set_var_nums(var_nums);
82  this->init(subdomain_ids);
83  }
84 
87  {
88  }
89 
90  const std::vector<unsigned int>&
92  dof_ids(void)const
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  if(var_nums!=NULL)
103  {
104  _var_nums = *var_nums;
105  }
106  else
107  {
108  for(unsigned int i=0; i<_system.n_vars(); i++)
109  {
110  _var_nums.insert(i);
111  }
112  }
113  }
114 
115  void
117  init (const SubdomainSelection& subdomain_selection)
118  {
119  _dof_ids.clear();
120 
121  std::vector<std::vector<dof_id_type> > dof_ids_per_processor(this->n_processors());
122 
123  const DofMap & dof_map = _system.get_dof_map();
124  std::vector<dof_id_type> dof_indices;
125 
126  const MeshBase& mesh = _system.get_mesh();
129  for ( ; el != end_el; ++el)
130  {
131  const Elem* elem = *el;
132  if(subdomain_selection(elem->subdomain_id()))
133  {
134  std::set<unsigned int>::const_iterator it = _var_nums.begin();
135  const std::set<unsigned int>::const_iterator itEnd = _var_nums.end();
136  for (; it!=itEnd; ++it)
137  {
138  dof_map.dof_indices (elem, dof_indices, *it);
139  for(size_t i=0; i<dof_indices.size(); i++)
140  {
141  const dof_id_type dof = dof_indices[i];
142  for(unsigned int proc=0; proc<this->n_processors(); proc++)
143  {
144  if((dof>=dof_map.first_dof(proc)) && (dof<dof_map.end_dof(proc)))
145  {
146  dof_ids_per_processor[proc].push_back(dof);
147  }
148  }
149  }
150  }
151  }
152  }
153 
154  /* Distribute information among processors. */
155  std::vector<Parallel::Request> request_per_processor(this->n_processors());
156  for(unsigned int proc=0; proc<this->n_processors(); proc++)
157  {
158  if(proc!=this->processor_id())
159  {
160  this->comm().send(proc,dof_ids_per_processor[proc],request_per_processor[proc]);
161  }
162  }
163  for(unsigned int proc=0; proc<this->n_processors(); proc++)
164  {
165  std::vector<dof_id_type> received_dofs;
166  if(proc==this->processor_id())
167  {
168  received_dofs = dof_ids_per_processor[proc];
169  }
170  else
171  {
172  this->comm().receive(proc,received_dofs);
173  }
174  for(unsigned int i=0; i<received_dofs.size(); i++)
175  {
176  _dof_ids.push_back(received_dofs[i]);
177  }
178  }
179 
180  /* Sort and unique the vector (using the same mechanism as in \p
181  DofMap::prepare_send_list()). */
182  std::sort(_dof_ids.begin(), _dof_ids.end());
183  std::vector<unsigned int>::iterator new_end = std::unique (_dof_ids.begin(), _dof_ids.end());
184  std::vector<unsigned int> (_dof_ids.begin(), new_end).swap (_dof_ids);
185 
186  /* Wait for sends to be complete. */
187  for(unsigned int proc=0; proc<this->n_processors(); proc++)
188  {
189  if(proc!=this->processor_id())
190  {
191  request_per_processor[proc].wait();
192  }
193  }
194  }
195 
196  void
198  init (const std::set<subdomain_id_type>& subdomain_ids)
199  {
200  SubdomainSelectionByList selection(subdomain_ids);
201  this->init(selection);
202  }
203 
204 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo