Flow123d  release_3.0.0-506-g34af125
dofhandler.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file dofhandler.hh
15  * @brief Declaration of class which handles the ordering of degrees of freedom (dof) and mappings between local and global dofs.
16  * @author Jan Stebel
17  */
18 
19 #ifndef DOFHANDLER_HH_
20 #define DOFHANDLER_HH_
21 
22 #include <vector> // for vector
23 #include "mesh/side_impl.hh"
24 #include "mesh/mesh.h"
25 #include "mesh/accessors.hh"
26 // #include "mesh/mesh_types.hh" // for ElementFullIter
27 #include "mesh/long_idx.hh" // for LongIdx
28 #include "fem/discrete_space.hh" // for DiscreteSpace
29 #include "petscvec.h" // for Vec
30 
31 template<unsigned int dim> class FiniteElement;
32 class Mesh;
33 class Distribution;
34 class Dof;
35 
36 
37 /**
38  * Class DOFHandlerBase provides an abstract interface for various dof handlers:
39  * - basic handler for a given spatial dimension
40  * - multi-dimensional handler for all spatial dimensions (1D-3D)
41  * - handler for a specific region / part of mesh
42  */
44 public:
45 
46  /**
47  * @brief Constructor.
48  * @param _mesh The mesh.
49  */
51  : n_global_dofs_(0), lsize_(0), loffset_(0), max_elem_dofs_(0), mesh_(&_mesh), dof_ds_(0) {};
52 
53  /**
54  * @brief Getter for the number of all mesh dofs required by the given
55  * finite element.
56  */
57  const unsigned int n_global_dofs() const { return n_global_dofs_; }
58 
59  /**
60  * @brief Returns the number of dofs on the current process.
61  */
62  const unsigned int lsize() const { return lsize_; }
63 
64  /**
65  * @brief Returns the offset of the local part of dofs.
66  */
67  const unsigned int loffset() const { return loffset_; }
68 
69  /**
70  * @brief Returns max. number of dofs on one element.
71  */
72  const unsigned int max_elem_dofs() const { return max_elem_dofs_; }
73 
74  Distribution *distr() const { return dof_ds_; }
75 
76  /**
77  * @brief Returns the mesh.
78  */
79  Mesh *mesh() const { return mesh_; }
80 
81  /**
82  * @brief Fill vector of the global indices of dofs associated to the @p cell.
83  *
84  * @param cell The cell.
85  * @param indices Vector of dof indices on the cell.
86  */
87  virtual unsigned int get_dof_indices(const ElementAccessor<3> &cell, std::vector<LongIdx> &indices) const = 0;
88 
89  /**
90  * @brief Fill vector of the indices of dofs associated to the @p cell on the local process.
91  *
92  * @param cell The cell.
93  * @param indices Vector of dof indices on the cell.
94  */
95  virtual unsigned int get_loc_dof_indices(const ElementAccessor<3> &cell, std::vector<LongIdx> &indices) const =0;
96 
97  /**
98  * @brief Compute hash value of DOF handler.
99  */
100  virtual std::size_t hash() const =0;
101 
102  /// Destructor.
103  virtual ~DOFHandlerBase();
104 
105 protected:
106 
107  /**
108  * @brief Number of global dofs assigned by the handler.
109  */
110  unsigned int n_global_dofs_;
111 
112  /**
113  * @brief Number of dofs associated to local process.
114  */
115  unsigned int lsize_;
116 
117  /**
118  * @brief Index of the first dof on the local process.
119  */
120  unsigned int loffset_;
121 
122  /// Max. number of dofs per element.
123  unsigned int max_elem_dofs_;
124 
125  /**
126  * @brief Pointer to the mesh to which the dof handler is associated.
127  */
129 
130  /**
131  * @brief Distribution of dofs associated to local process.
132  */
134 
135 };
136 
137 
138 
139 
140 /**
141  * @brief Provides the numbering of the finite element degrees of freedom
142  * on the computational mesh.
143  *
144  * Class DOFHandlerMultiDim distributes the degrees of freedom (dof) for
145  * a particular triplet of 1d, 2d and 3d finite elements on the computational mesh
146  * and provides mappings between local and global dofs.
147  * The template parameter @p dim denotes the spatial dimension of
148  * the reference finite element.
149  *
150  * Currently the functionality is restricted to finite elements with internal and nodal dofs,
151  * i.e. the neighboring elements can share only dofs on nodes.
152  */
154 public:
155 
156  /**
157  * @brief Constructor.
158  * @param _mesh The mesh.
159  * @param make_elem_part Allow switch off make_element_partitioning, necessary for boundary DOF handler.
160  */
161  DOFHandlerMultiDim(Mesh &_mesh, bool make_elem_part = true);
162 
163 
164  /**
165  * @brief Distributes degrees of freedom on the mesh needed
166  * for the given discrete space.
167  *
168  * By default, the dof handler is parallel, meaning that each
169  * processor has access to dofs on the local elements and on one
170  * layer of ghost elements (owned by neighbouring elements).
171  * This can be changed by setting @p sequential to true.
172  *
173  * @param ds The discrete space consisting of finite elements for each mesh element.
174  * @param sequential If true then each processor will have information about all dofs.
175  */
176  void distribute_dofs(std::shared_ptr<DiscreteSpace> ds,
177  bool sequential = false);
178 
179  /**
180  * @brief Returns the global indices of dofs associated to the @p cell.
181  *
182  * @param cell The cell.
183  * @param indices Array of dof indices on the cell.
184  */
185  unsigned int get_dof_indices(const ElementAccessor<3> &cell,
186  std::vector<LongIdx> &indices) const override;
187 
188  /**
189  * @brief Returns the indices of dofs associated to the @p cell on the local process.
190  *
191  * @param cell The cell.
192  * @param indices Array of dof indices on the cell.
193  */
194  unsigned int get_loc_dof_indices(const ElementAccessor<3> &cell,
195  std::vector<LongIdx> &indices) const override;
196 
197  /**
198  * @brief Returns the global index of local element.
199  *
200  * @param loc_el Local index of element.
201  */
202  inline int el_index(int loc_el) const { return el_4_loc[loc_el]; }
203 
204  /**
205  * @brief Returns the global index of local edge.
206  *
207  * @param loc_edg Local index of edge.
208  */
209  inline LongIdx edge_index(int loc_edg) const { return edg_4_loc[loc_edg]; }
210 
211  /**
212  * @brief Returns the global index of local neighbour.
213  *
214  * @param loc_nb Local index of neighbour.
215  */
216  inline LongIdx nb_index(int loc_nb) const { return nb_4_loc[loc_nb]; }
217 
218  /**
219  * @brief Return number of dofs on given cell.
220  *
221  * @param cell Cell accessor.
222  */
223  unsigned int n_dofs(ElementAccessor<3> cell) const;
224 
225  /**
226  * @brief Returns number of local edges.
227  */
228  inline unsigned int n_loc_edges() const { return edg_4_loc.size(); }
229 
230  /**
231  * @brief Returns number of local neighbours.
232  */
233  inline unsigned int n_loc_nb() const { return nb_4_loc.size(); }
234 
235  /**
236  * Returns true if element is on local process.
237  * @param index Global element index.
238  */
239  bool el_is_local(int index) const;
240 
241  /**
242  * @brief Returns finite element object for given space dimension.
243  *
244  * @param cell Cell accessor.
245  */
246  template<unsigned int dim>
247  FiniteElement<dim> *fe(const ElementAccessor<3> &cell) const { return ds_->fe<dim>(cell); }
248 
249  /**
250  * @brief Return dof on a given cell.
251  * @param cell Mesh cell.
252  * @param idof Number of dof on the cell.
253  */
254  const Dof &cell_dof(ElementAccessor<3> cell,
255  unsigned int idof) const;
256 
257  /**
258  * Implements @p DOFHandlerBase::hash.
259  */
260  std::size_t hash() const override;
261 
262  /// Destructor.
263  ~DOFHandlerMultiDim() override;
264 
265 
266 
267 
268 private:
269 
270  /**
271  * @brief Prepare parallel distribution of elements, edges and neighbours.
272  */
273  void make_elem_partitioning();
274 
275  /**
276  * @brief Initialize vector of starting indices for elements.
277  */
278  void init_cell_starts();
279 
280  /**
281  * @brief Initialize auxiliary vector of starting indices of nodal dofs.
282  *
283  * @param node_dof_starts Vector of starting indices (output).
284  */
285  void init_node_dof_starts(std::vector<LongIdx> &node_dof_starts);
286 
287  /**
288  * @brief Initialize node_status.
289  *
290  * Set VALID_NODE for nodes owned by local elements and
291  * INVALID_NODE for nodes owned by ghost elements.
292  *
293  * @param node_status Vector of nodal status (output).
294  */
295  void init_node_status(std::vector<short int> &node_status);
296 
297  /**
298  * @brief Obtain dof numbers on ghost elements from other processor.
299  * @param proc Neighbouring processor.
300  * @param dofs Array where dofs are stored (output).
301  */
302  void receive_ghost_dofs(unsigned int proc,
303  std::vector<LongIdx> &dofs);
304 
305  /**
306  * @brief Send dof numbers to other processor.
307  * @param proc Neighbouring processor.
308  */
309  void send_ghost_dofs(unsigned int proc);
310 
311  /**
312  * @brief Update dofs on local elements from ghost element dofs.
313  *
314  * @param proc Neighbouring processor.
315  * @param update_cells Vector of global indices of elements which need to be updated
316  * from ghost elements.
317  * @param dofs Vector of dof indices on ghost elements from processor @p proc.
318  * @param node_dof_starts Vector of starting indices of nodal dofs.
319  * @param node_dofs Vector of nodal dof indices (output).
320  */
321  void update_local_dofs(unsigned int proc,
322  const std::vector<bool> &update_cells,
323  const std::vector<LongIdx> &dofs,
324  const std::vector<LongIdx> &node_dof_starts,
325  std::vector<LongIdx> &node_dofs
326  );
327 
328  /**
329  * @brief Communicate local dof indices to all processors.
330  */
331  void create_sequential();
332 
333 
334  /**
335  * Flags used during distribution of dofs to mark node and dof status.
336  */
337  static const int INVALID_NODE = 1;
338  static const int VALID_NODE = 2;
339  static const int ASSIGNED_NODE = 3;
340  static const int INVALID_DOF = -1;
341 
342 
343  /// Pointer to the discrete space for which the handler distributes dofs.
344  std::shared_ptr<DiscreteSpace> ds_;
345 
346  /**
347  * @brief Starting indices for element dofs (parallel version).
348  *
349  * E.g. dof_indices[cell_starts[idx]] = dof number for first dof on the
350  * cell with index idx within the paralle structure. To use with element
351  * accessor use the following:
352  *
353  * ElementAccessor<3> cell;
354  * ...
355  * // i-th dof number on the cell
356  * dof_indices[cell_starts[row_4_el[cell.idx()]]+i] = ...
357  *
358  * Only local and ghost elements are stored, but the vector has size mesh_->n_elements()+1.
359  */
361 
362  /**
363  * @brief Dof numbers on local and ghost elements (parallel version).
364  *
365  * Dofs are ordered accordingly with cell_starts and local dof order
366  * given by the finite element. See cell_starts for more description.
367  */
369 
370  /**
371  * @brief Starting indices for element dofs (sequential version).
372  *
373  * This vector stores information about all mesh elements.
374  * See cell_starts for paralle version.
375  */
377 
378  /**
379  * @brief Dof numbers on mesh elements (sequential version).
380  *
381  * This vector stores information about all mesh elements.
382  * See dof_indices for paralle version.
383  */
385 
386 
387  /// Global element index -> index according to partitioning
389 
390  /// Local element index -> global element index
392 
393  /// Distribution of elements
395 
396  /// Local edge index -> global edge index
398 
399  /// Local neighbour index -> global neighbour index
401 
402  /// Indices of local nodes in mesh tree.
404 
405  /// Indices of ghost cells (neighbouring with local elements).
407 
408  /// Processors of ghost elements.
409  set<unsigned int> ghost_proc;
410 
411  /// Arrays of ghost cells for each neighbouring processor.
413 
414 };
415 
416 
417 
418 
419 #endif /* DOFHANDLER_HH_ */
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
virtual ~DOFHandlerBase()
Destructor.
Definition: dofhandler.cc:33
LongIdx * el_4_loc
Local element index -> global element index.
Definition: dofhandler.hh:391
const unsigned int lsize() const
Returns the number of dofs on the current process.
Definition: dofhandler.hh:62
std::vector< LongIdx > cell_starts_seq
Starting indices for element dofs (sequential version).
Definition: dofhandler.hh:376
Distribution * el_ds_
Distribution of elements.
Definition: dofhandler.hh:394
Definition: mesh.h:80
unsigned int n_loc_nb() const
Returns number of local neighbours.
Definition: dofhandler.hh:233
int el_index(int loc_el) const
Returns the global index of local element.
Definition: dofhandler.hh:202
Mesh * mesh() const
Returns the mesh.
Definition: dofhandler.hh:79
Distribution * dof_ds_
Distribution of dofs associated to local process.
Definition: dofhandler.hh:133
unsigned int lsize_
Number of dofs associated to local process.
Definition: dofhandler.hh:115
map< unsigned int, vector< LongIdx > > ghost_proc_el
Arrays of ghost cells for each neighbouring processor.
Definition: dofhandler.hh:412
std::vector< LongIdx > dof_indices_seq
Dof numbers on mesh elements (sequential version).
Definition: dofhandler.hh:384
virtual unsigned int get_dof_indices(const ElementAccessor< 3 > &cell, std::vector< LongIdx > &indices) const =0
Fill vector of the global indices of dofs associated to the cell.
std::vector< LongIdx > cell_starts
Starting indices for element dofs (parallel version).
Definition: dofhandler.hh:360
const unsigned int loffset() const
Returns the offset of the local part of dofs.
Definition: dofhandler.hh:67
const unsigned int max_elem_dofs() const
Returns max. number of dofs on one element.
Definition: dofhandler.hh:72
Mesh * mesh_
Pointer to the mesh to which the dof handler is associated.
Definition: dofhandler.hh:128
std::vector< LongIdx > dof_indices
Dof numbers on local and ghost elements (parallel version).
Definition: dofhandler.hh:368
Provides the numbering of the finite element degrees of freedom on the computational mesh...
Definition: dofhandler.hh:153
vector< LongIdx > nb_4_loc
Local neighbour index -> global neighbour index.
Definition: dofhandler.hh:400
virtual unsigned int get_loc_dof_indices(const ElementAccessor< 3 > &cell, std::vector< LongIdx > &indices) const =0
Fill vector of the indices of dofs associated to the cell on the local process.
unsigned int max_elem_dofs_
Max. number of dofs per element.
Definition: dofhandler.hh:123
vector< LongIdx > ghost_4_loc
Indices of ghost cells (neighbouring with local elements).
Definition: dofhandler.hh:406
Declaration of class which provides the finite element for every mesh cell.
unsigned int n_loc_edges() const
Returns number of local edges.
Definition: dofhandler.hh:228
virtual std::size_t hash() const =0
Compute hash value of DOF handler.
LongIdx edge_index(int loc_edg) const
Returns the global index of local edge.
Definition: dofhandler.hh:209
vector< LongIdx > edg_4_loc
Local edge index -> global edge index.
Definition: dofhandler.hh:397
unsigned int loffset_
Index of the first dof on the local process.
Definition: dofhandler.hh:120
set< unsigned int > ghost_proc
Processors of ghost elements.
Definition: dofhandler.hh:409
vector< LongIdx > node_4_loc
Indices of local nodes in mesh tree.
Definition: dofhandler.hh:403
std::shared_ptr< DiscreteSpace > ds_
Pointer to the discrete space for which the handler distributes dofs.
Definition: dofhandler.hh:344
unsigned int n_global_dofs_
Number of global dofs assigned by the handler.
Definition: dofhandler.hh:110
Abstract class for the description of a general finite element on a reference simplex in dim dimensio...
Distribution * distr() const
Definition: dofhandler.hh:74
FiniteElement< dim > * fe(const ElementAccessor< 3 > &cell) const
Returns finite element object for given space dimension.
Definition: dofhandler.hh:247
DOFHandlerBase(Mesh &_mesh)
Constructor.
Definition: dofhandler.hh:50
LongIdx * row_4_el
Global element index -> index according to partitioning.
Definition: dofhandler.hh:388
LongIdx nb_index(int loc_nb) const
Returns the global index of local neighbour.
Definition: dofhandler.hh:216
const unsigned int n_global_dofs() const
Getter for the number of all mesh dofs required by the given finite element.
Definition: dofhandler.hh:57