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