Flow123d  release_3.0.0-1106-ga3b2e4c
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 <unordered_map> // for unordered_map
24 #include "mesh/side_impl.hh"
25 #include "mesh/mesh.h"
26 #include "mesh/accessors.hh"
27 #include "mesh/long_idx.hh" // for LongIdx
28 #include "mesh/range_wrapper.hh"
30 #include "fem/discrete_space.hh" // for DiscreteSpace
31 #include "la/vector_mpi.hh" // for VectorMPI
32 #include "petscvec.h" // for Vec
33 
34 template<unsigned int dim> class FiniteElement;
35 class DHCellAccessor;
36 class Mesh;
37 class Distribution;
38 class Dof;
39 
40 
41 /**
42  * Class DOFHandlerBase provides an abstract interface for various dof handlers:
43  * - basic handler for a given spatial dimension
44  * - multi-dimensional handler for all spatial dimensions (1D-3D)
45  * - handler for a specific region / part of mesh
46  */
48 public:
49 
50  /**
51  * @brief Constructor.
52  * @param _mesh The mesh.
53  */
55  : n_global_dofs_(0), lsize_(0), loffset_(0), max_elem_dofs_(0), mesh_(&_mesh), dof_ds_(0) {}
56 
57  /**
58  * @brief Getter for the number of all mesh dofs required by the given
59  * finite element.
60  */
61  const unsigned int n_global_dofs() const { return n_global_dofs_; }
62 
63  /**
64  * @brief Returns the number of dofs on the current process.
65  */
66  const unsigned int lsize() const { return lsize_; }
67 
68  /**
69  * @brief Returns max. number of dofs on one element.
70  */
71  const unsigned int max_elem_dofs() const { return max_elem_dofs_; }
72 
73  std::shared_ptr<Distribution> distr() const { return dof_ds_; }
74 
75  /**
76  * @brief Returns the mesh.
77  */
78  Mesh *mesh() const { return mesh_; }
79 
80  /**
81  * @brief Compute hash value of DOF handler.
82  */
83  virtual std::size_t hash() const =0;
84 
85  /// Destructor.
86  virtual ~DOFHandlerBase();
87 
88 protected:
89 
90  /**
91  * @brief Fill vector of the global indices of dofs associated to the @p cell.
92  *
93  * @param cell The cell.
94  * @param indices Vector of dof indices on the cell.
95  */
96  virtual unsigned int get_dof_indices(const DHCellAccessor &cell, std::vector<LongIdx> &indices) const = 0;
97 
98  /**
99  * @brief Fill vector of the indices of dofs associated to the @p cell on the local process.
100  *
101  * @param cell The cell.
102  * @param indices Vector of dof indices on the cell.
103  */
104  virtual unsigned int get_loc_dof_indices(const DHCellAccessor &cell, std::vector<LongIdx> &indices) const =0;
105 
106  /**
107  * @brief Number of global dofs assigned by the handler.
108  */
109  unsigned int n_global_dofs_;
110 
111  /**
112  * @brief Number of dofs associated to local process.
113  */
114  unsigned int lsize_;
115 
116  /**
117  * @brief Index of the first dof on the local process.
118  */
119  unsigned int loffset_;
120 
121  /// Max. number of dofs per element.
122  unsigned int max_elem_dofs_;
123 
124  /**
125  * @brief Pointer to the mesh to which the dof handler is associated.
126  */
128 
129  /**
130  * @brief Distribution of dofs associated to local process.
131  */
132  std::shared_ptr<Distribution> dof_ds_;
133 
134 };
135 
136 
137 
138 
139 /**
140  * @brief Provides the numbering of the finite element degrees of freedom
141  * on the computational mesh.
142  *
143  * Class DOFHandlerMultiDim distributes the degrees of freedom (dof) for
144  * a particular triplet of 1d, 2d and 3d finite elements on the computational mesh
145  * and provides mappings between local and global dofs.
146  * The template parameter @p dim denotes the spatial dimension of
147  * the reference finite element.
148  *
149  * Currently the functionality is restricted to finite elements with internal and nodal dofs,
150  * i.e. the neighboring elements can share only dofs on nodes.
151  */
153 public:
154 
155  /**
156  * @brief Constructor.
157  * @param _mesh The mesh.
158  * @param make_elem_part Allow switch off make_element_partitioning, necessary for boundary DOF handler.
159  */
160  DOFHandlerMultiDim(Mesh &_mesh, bool make_elem_part = true);
161 
162 
163  /**
164  * @brief Distributes degrees of freedom on the mesh needed
165  * for the given discrete space.
166  *
167  * By default, the dof handler is parallel, meaning that each
168  * processor has access to dofs on the local elements and on one
169  * layer of ghost elements (owned by neighbouring elements).
170  * This can be changed by setting @p sequential to true.
171  *
172  * @param ds The discrete space consisting of finite elements for each mesh element.
173  * @param sequential If true then each processor will have information about all dofs.
174  */
175  void distribute_dofs(std::shared_ptr<DiscreteSpace> ds);
176 
177  /** @brief Returns sequential version of the current dof handler.
178  *
179  * Collective on all processors.
180  */
181  std::shared_ptr<DOFHandlerMultiDim> sequential();
182 
183  /**
184  * @brief Returns scatter context from parallel to sequential vectors.
185  *
186  * For sequential dof handler it returns null pointer.
187  * Collective on all processors.
188  */
189  std::shared_ptr<VecScatter> sequential_scatter();
190 
191  /**
192  * @brief Allocates PETSc vector according to the dof distribution.
193  */
194  virtual VectorMPI create_vector();
195 
196  /**
197  * @brief Returns the global index of local edge.
198  *
199  * @param loc_edg Local index of edge.
200  */
201  inline LongIdx edge_index(int loc_edg) const { return edg_4_loc[loc_edg]; }
202 
203  /**
204  * @brief Returns the global index of local neighbour.
205  *
206  * @param loc_nb Local index of neighbour.
207  */
208  inline LongIdx nb_index(int loc_nb) const { return nb_4_loc[loc_nb]; }
209 
210  /**
211  * @brief Returns number of local edges.
212  */
213  inline unsigned int n_loc_edges() const { return edg_4_loc.size(); }
214 
215  /**
216  * @brief Returns number of local neighbours.
217  */
218  inline unsigned int n_loc_nb() const { return nb_4_loc.size(); }
219 
220  /// Output structure of dof handler.
221  void print() const;
222 
223  /**
224  * Implements @p DOFHandlerBase::hash.
225  */
226  std::size_t hash() const override;
227 
228  /// Returns range of DOF handler cells (only range of own without ghost cells)
229  Range<DHCellAccessor> own_range() const;
230 
231  /// Returns range over own and ghost cells of DOF handler
232  Range<DHCellAccessor> local_range() const;
233 
234  /// Returns range over ghosts DOF handler cells
235  Range<DHCellAccessor> ghost_range() const;
236 
237  /// Return DHCellAccessor appropriate to ElementAccessor of given idx
238  const DHCellAccessor cell_accessor_from_element(unsigned int elm_idx) const;
239 
240  /// Return pointer to discrete space for which the handler distributes dofs.
241  std::shared_ptr<DiscreteSpace> ds() const { return ds_; }
242 
243 
244  /// Destructor.
245  ~DOFHandlerMultiDim() override;
246 
247 
248 
249  friend class DHCellAccessor;
250  friend class DHCellSide;
251  friend class DHNeighbSide;
252  friend class SubDOFHandlerMultiDim;
253 
254 protected:
255 
256  /**
257  * Returns true if element is on local process.
258  * @param index Global element index.
259  */
260  bool el_is_local(int index) const;
261 
262  /**
263  * @brief Prepare parallel distribution of elements, edges and neighbours.
264  */
265  void make_elem_partitioning();
266 
267  /**
268  * @brief Initialize vector of starting indices for elements.
269  */
270  void init_cell_starts();
271 
272  /**
273  * @brief Initialize auxiliary vector of starting indices of nodal/edge dofs.
274  *
275  * @param node_dof_starts Vector of nodal starting indices (output).
276  * @param edge_dof_starts Vector of edge starting indices (output).
277  */
278  void init_dof_starts(std::vector<LongIdx> &node_dof_starts,
279  std::vector<LongIdx> &edge_dof_starts);
280 
281  /**
282  * @brief Initialize node_status and edge_status.
283  *
284  * Set VALID_NFACE for nodes/edges owned by local elements and
285  * INVALID_NFACE for nodes/edges owned by ghost elements.
286  *
287  * @param node_status Vector of nodal status (output).
288  * @param edge_status Vector of edge status (output).
289  */
290  void init_status(std::vector<short int> &node_status,
291  std::vector<short int> &edge_status);
292 
293  /**
294  * @brief Obtain dof numbers on ghost elements from other processor.
295  * @param proc Neighbouring processor.
296  * @param dofs Array where dofs are stored (output).
297  */
298  void receive_ghost_dofs(unsigned int proc,
299  std::vector<LongIdx> &dofs);
300 
301  /**
302  * @brief Send dof numbers to other processor.
303  * @param proc Neighbouring processor.
304  */
305  void send_ghost_dofs(unsigned int proc);
306 
307  /**
308  * @brief Update dofs on local elements from ghost element dofs.
309  *
310  * @param proc Neighbouring processor.
311  * @param update_cells Vector of global indices of elements which need to be updated
312  * from ghost elements.
313  * @param dofs Vector of dof indices on ghost elements from processor @p proc.
314  * @param node_dof_starts Vector of starting indices of nodal dofs.
315  * @param node_dofs Vector of nodal dof indices (output).
316  * @param edge_dof_starts Vector of starting indices of edge dofs.
317  * @param edge_dofs Vector of edge dof indices (output).
318  */
319  void update_local_dofs(unsigned int proc,
320  const std::vector<bool> &update_cells,
321  const std::vector<LongIdx> &dofs,
322  const std::vector<LongIdx> &node_dof_starts,
323  std::vector<LongIdx> &node_dofs,
324  const std::vector<LongIdx> &edge_dof_starts,
325  std::vector<LongIdx> &edge_dofs);
326 
327  /**
328  * @brief Communicate local dof indices to all processors and create new sequential dof handler.
329  *
330  * Collective on all processors.
331  */
332  void create_sequential();
333 
334  /**
335  * @brief Returns the global indices of dofs associated to the @p cell.
336  *
337  * @param cell The cell.
338  * @param indices Array of dof indices on the cell.
339  */
340  unsigned int get_dof_indices(const DHCellAccessor &cell,
341  std::vector<LongIdx> &indices) const override;
342 
343  /**
344  * @brief Returns the indices of dofs associated to the @p cell on the local process.
345  *
346  * @param cell The cell.
347  * @param indices Array of dof indices on the cell.
348  */
349  unsigned int get_loc_dof_indices(const DHCellAccessor &cell,
350  std::vector<LongIdx> &indices) const override;
351 
352 
353 
354  /**
355  * Flags used during distribution of dofs to mark n-face and dof status.
356  *
357  * INVALID_NFACE means that on this node/edge/side/cell the current processor
358  * will not distribute dofs.
359  * VALID_NFACE means that on this node/edge/side/cell the current processor
360  * will distribute dofs.
361  * ASSIGNED_NFACE means that dofs on this n-face are already distributed.
362  *
363  * INVALID_DOF marks dofs whose global number has to be set by neighbouring processor.
364  */
365  static const int INVALID_NFACE;
366  static const int VALID_NFACE;
367  static const int ASSIGNED_NFACE;
368  static const int INVALID_DOF;
369 
370 
371  /// Pointer to the discrete space for which the handler distributes dofs.
372  std::shared_ptr<DiscreteSpace> ds_;
373 
374  /// Indicator for parallel/sequential dof handler.
376 
377  /// Sequential dof handler associated to the current (parallel) one.
378  std::shared_ptr<DOFHandlerMultiDim> dh_seq_;
379 
380  /// Scatter context for parallel to sequential vectors.
381  std::shared_ptr<VecScatter> scatter_to_seq_;
382 
383  /**
384  * @brief Starting indices for local (owned+ghost) element dofs.
385  *
386  * E.g. dof_indices[cell_starts[idx]] = dof number for first dof on the
387  * cell with local index idx within the parallel structure. To use with
388  * DHCellAccessor use the following:
389  *
390  * DHCellAccessor<3> cell;
391  * ...
392  * // i-th dof number on the cell
393  * dof_indices[cell_starts[cell.local_idx()]+i] = ...
394  *
395  * For sequential dof handler, dofs from all elements are stored and
396  * elements are ordered as in mesh_->get_row_4_el().
397  */
399 
400  /**
401  * @brief Dof numbers on local and ghost elements.
402  *
403  * Dofs are ordered accordingly with cell_starts and local dof order
404  * given by the finite element. See cell_starts for more description.
405  */
407 
408  /**
409  * @brief Maps local and ghost dof indices to global ones.
410  *
411  * First lsize_ entries correspond to dofs owned by local processor,
412  * the remaining entries are ghost dofs sorted by neighbouring processor id.
413  */
415 
416  /**
417  * @brief Maps global element index into local/ghost index (obsolete).
418  */
419  std::unordered_map<LongIdx,LongIdx> global_to_local_el_idx_;
420 
421  /// Distribution of elements
423 
424  /// Local edge index -> global edge index
426 
427  /// Local neighbour index -> global neighbour index
429 
430  /// Indices of ghost cells (neighbouring with local elements).
432 
433  /// Processors of ghost elements.
434  set<unsigned int> ghost_proc;
435 
436  /// Arrays of ghost cells for each neighbouring processor.
438 
439 };
440 
441 
443 {
444 public:
445 
446  /** @brief Creates a new dof handler for a component of FESystem.
447  *
448  * The @p component_idx indicates the index of finite-element
449  * from @p dh for which the new sub- handler is made.
450  * The numbering of dofs in sub-handler is compatible
451  * with the original handler.
452  */
453  SubDOFHandlerMultiDim(std::shared_ptr<DOFHandlerMultiDim> dh, unsigned int component_idx);
454 
455  /** @brief Update values in subvector from parent vector.
456  *
457  * @p vec Vector aligned with the parent dof handler.
458  * @p subvec Vctor aligned with the current sub-handler.
459  */
460  VectorMPI update_subvector(const VectorMPI &vec, VectorMPI &subvec);
461 
462  /** @brief Update values in parent vector from values of subvector.
463  *
464  * @p vec Vector aligned with parent dof handler.
465  * @p subvec Vector aligned with the current sub-handler.
466  */
467  void update_parent_vector(VectorMPI &vec, const VectorMPI &subvec);
468 
469  /// Local indices in the parent handler.
470  const std::vector<LongIdx> &parent_indices() { return parent_dof_idx_; }
471 
472 
473 private:
474 
475  /// Get global dof indices of ghost dofs for sub-handler.
476  void receive_sub_ghost_dofs(unsigned int proc, vector<LongIdx> &dofs);
477 
478  /// Send global indices of dofs that are ghost on other processors.
479  void send_sub_ghost_dofs(unsigned int proc, const map<LongIdx,LongIdx> &global_to_local_dof_idx);
480 
481  /// Parent dof handler.
482  std::shared_ptr<DOFHandlerMultiDim> parent_;
483 
484  /// Index of FE in parent FESystem.
485  unsigned int fe_idx_;
486 
487  /// Local indices in the parent handler.
489 };
490 
491 
492 
493 
494 #endif /* DOFHANDLER_HH_ */
void receive_ghost_dofs(unsigned int proc, std::vector< LongIdx > &dofs)
Obtain dof numbers on ghost elements from other processor.
Definition: dofhandler.cc:161
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
virtual unsigned int get_loc_dof_indices(const DHCellAccessor &cell, std::vector< LongIdx > &indices) const =0
Fill vector of the indices of dofs associated to the cell on the local process.
virtual ~DOFHandlerBase()
Destructor.
Definition: dofhandler.cc:41
void create_sequential()
Communicate local dof indices to all processors and create new sequential dof handler.
Definition: dofhandler.cc:441
void init_cell_starts()
Initialize vector of starting indices for elements.
Definition: dofhandler.cc:75
const std::vector< LongIdx > & parent_indices()
Local indices in the parent handler.
Definition: dofhandler.hh:470
const unsigned int lsize() const
Returns the number of dofs on the current process.
Definition: dofhandler.hh:66
Class allows to iterate over sides of neighbour.
void init_dof_starts(std::vector< LongIdx > &node_dof_starts, std::vector< LongIdx > &edge_dof_starts)
Initialize auxiliary vector of starting indices of nodal/edge dofs.
Definition: dofhandler.cc:89
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:489
Distribution * el_ds_
Distribution of elements.
Definition: dofhandler.hh:422
static const int INVALID_NFACE
Definition: dofhandler.hh:365
std::shared_ptr< DOFHandlerMultiDim > parent_
Parent dof handler.
Definition: dofhandler.hh:482
Definition: mesh.h:76
Template Iter serves as general template for internal iterators.
Cell accessor allow iterate over DOF handler cells.
std::vector< LongIdx > local_to_global_dof_idx_
Maps local and ghost dof indices to global ones.
Definition: dofhandler.hh:414
unsigned int n_loc_nb() const
Returns number of local neighbours.
Definition: dofhandler.hh:218
void init_status(std::vector< short int > &node_status, std::vector< short int > &edge_status)
Initialize node_status and edge_status.
Definition: dofhandler.cc:115
std::shared_ptr< Distribution > dof_ds_
Distribution of dofs associated to local process.
Definition: dofhandler.hh:132
Mesh * mesh() const
Returns the mesh.
Definition: dofhandler.hh:78
std::shared_ptr< DOFHandlerMultiDim > dh_seq_
Sequential dof handler associated to the current (parallel) one.
Definition: dofhandler.hh:378
unsigned int lsize_
Number of dofs associated to local process.
Definition: dofhandler.hh:114
map< unsigned int, vector< LongIdx > > ghost_proc_el
Arrays of ghost cells for each neighbouring processor.
Definition: dofhandler.hh:437
static const int ASSIGNED_NFACE
Definition: dofhandler.hh:367
void send_ghost_dofs(unsigned int proc)
Send dof numbers to other processor.
Definition: dofhandler.cc:182
std::vector< LongIdx > cell_starts
Starting indices for local (owned+ghost) element dofs.
Definition: dofhandler.hh:398
const unsigned int max_elem_dofs() const
Returns max. number of dofs on one element.
Definition: dofhandler.hh:71
Mesh * mesh_
Pointer to the mesh to which the dof handler is associated.
Definition: dofhandler.hh:127
Range helper class.
std::vector< LongIdx > dof_indices
Dof numbers on local and ghost elements.
Definition: dofhandler.hh:406
Provides the numbering of the finite element degrees of freedom on the computational mesh...
Definition: dofhandler.hh:152
static const int VALID_NFACE
Definition: dofhandler.hh:366
vector< LongIdx > nb_4_loc
Local neighbour index -> global neighbour index.
Definition: dofhandler.hh:428
virtual unsigned int get_dof_indices(const DHCellAccessor &cell, std::vector< LongIdx > &indices) const =0
Fill vector of the global indices of dofs associated to the cell.
unsigned int max_elem_dofs_
Max. number of dofs per element.
Definition: dofhandler.hh:122
vector< LongIdx > ghost_4_loc
Indices of ghost cells (neighbouring with local elements).
Definition: dofhandler.hh:431
std::unordered_map< LongIdx, LongIdx > global_to_local_el_idx_
Maps global element index into local/ghost index (obsolete).
Definition: dofhandler.hh:419
Declaration of class which provides the finite element for every mesh cell.
void make_elem_partitioning()
Prepare parallel distribution of elements, edges and neighbours.
Definition: dofhandler.cc:572
unsigned int n_loc_edges() const
Returns number of local edges.
Definition: dofhandler.hh:213
virtual std::size_t hash() const =0
Compute hash value of DOF handler.
std::shared_ptr< Distribution > distr() const
Definition: dofhandler.hh:73
std::vector< LongIdx > parent_dof_idx_
Local indices in the parent handler.
Definition: dofhandler.hh:488
LongIdx edge_index(int loc_edg) const
Returns the global index of local edge.
Definition: dofhandler.hh:201
vector< LongIdx > edg_4_loc
Local edge index -> global edge index.
Definition: dofhandler.hh:425
unsigned int loffset_
Index of the first dof on the local process.
Definition: dofhandler.hh:119
static const int INVALID_DOF
Definition: dofhandler.hh:368
set< unsigned int > ghost_proc
Processors of ghost elements.
Definition: dofhandler.hh:434
std::shared_ptr< DiscreteSpace > ds_
Pointer to the discrete space for which the handler distributes dofs.
Definition: dofhandler.hh:372
unsigned int fe_idx_
Index of FE in parent FESystem.
Definition: dofhandler.hh:485
unsigned int n_global_dofs_
Number of global dofs assigned by the handler.
Definition: dofhandler.hh:109
Abstract class for the description of a general finite element on a reference simplex in dim dimensio...
bool el_is_local(int index) const
Definition: dofhandler.cc:658
void update_local_dofs(unsigned int proc, const std::vector< bool > &update_cells, const std::vector< LongIdx > &dofs, const std::vector< LongIdx > &node_dof_starts, std::vector< LongIdx > &node_dofs, const std::vector< LongIdx > &edge_dof_starts, std::vector< LongIdx > &edge_dofs)
Update dofs on local elements from ghost element dofs.
Definition: dofhandler.cc:213
bool is_parallel_
Indicator for parallel/sequential dof handler.
Definition: dofhandler.hh:375
DOFHandlerBase(Mesh &_mesh)
Constructor.
Definition: dofhandler.hh:54
std::shared_ptr< VecScatter > scatter_to_seq_
Scatter context for parallel to sequential vectors.
Definition: dofhandler.hh:381
LongIdx nb_index(int loc_nb) const
Returns the global index of local neighbour.
Definition: dofhandler.hh:208
Implementation of range helper class.
Side accessor allows to iterate over sides of DOF handler cell.
const unsigned int n_global_dofs() const
Getter for the number of all mesh dofs required by the given finite element.
Definition: dofhandler.hh:61
std::shared_ptr< DiscreteSpace > ds() const
Return pointer to discrete space for which the handler distributes dofs.
Definition: dofhandler.hh:241