Flow123d  release_2.2.0-914-gf1a3a4f
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 <map>
23 #include <petscmat.h>
24 #include "mesh/mesh.h"
25 #include "mesh/mesh_types.hh"
26 #include "mesh/elements.h"
27 #include "la/distribution.hh"
28 
29 
30 template<unsigned int dim, unsigned int spacedim> class FiniteElement;
31 class Mesh;
32 class Distribution;
33 
34 /**
35  * Class DOFHandlerBase provides an abstract interface for various dof handlers:
36  * - basic handler for a given spatial dimension
37  * - multi-dimensional handler for all spatial dimensions (1D-3D)
38  * - handler for a specific region / part of mesh
39  */
41 public:
42 
43  /**
44  * @brief Constructor.
45  * @param _mesh The mesh.
46  */
47  DOFHandlerBase(Mesh &_mesh) : global_dof_offset(0), n_dofs(0), lsize_(0), mesh_(&_mesh) {};
48 
49  /**
50  * @brief Alias for iterator over cells.
51  *
52  * TODO: Notation to be fixed: element or cell
53  * TODO: Iterator goes through cells of all dimensions, but
54  * should go only through dim-dimensional ones.
55  */
57 
58  /**
59  * @brief Getter for the number of all mesh dofs required by the given
60  * finite element.
61  */
62  const unsigned int n_global_dofs() const { return n_dofs; }
63 
64  /**
65  * @brief Returns the number of the first global dof handled by this
66  * DOFHandler.
67  */
68  const unsigned int offset() const { return global_dof_offset; }
69 
70  /**
71  * @brief Returns the number of dofs on the current process.
72  */
73  const unsigned int lsize() const { return lsize_; }
74 
75  /**
76  * @brief Returns the offset of the local part of dofs.
77  */
78  const unsigned int loffset() const { return loffset_; }
79 
80  /**
81  * @brief Returns max. number of dofs on one element.
82  */
83  const unsigned int max_elem_dofs() const { return max_elem_dofs_; }
84 
85  /**
86  * @brief Returns the parallel distribution of dofs.
87  */
88  Distribution *distr() const { return ds_; }
89 
90  /**
91  * @brief Returns the mesh.
92  */
93  Mesh *mesh() const { return mesh_; }
94 
95  /**
96  * @brief Fill vector of the global indices of dofs associated to the @p cell.
97  *
98  * @param cell The cell.
99  * @param indices Vector of dof indices on the cell.
100  */
101  virtual unsigned int get_dof_indices(const CellIterator &cell, std::vector<IdxInt> &indices) const = 0;
102 
103  /**
104  * @brief Fill vector of the indices of dofs associated to the @p cell on the local process.
105  *
106  * @param cell The cell.
107  * @param indices Vector of dof indices on the cell.
108  */
109  virtual unsigned int get_loc_dof_indices(const CellIterator &cell, std::vector<IdxInt> &indices) const =0;
110 
111  /**
112  * @brief Returns the dof values associated to the @p cell.
113  *
114  * @param cell The cell.
115  * @param values The global vector of values.
116  * @param local_values Array of values at local dofs.
117  */
118  virtual void get_dof_values(const CellIterator &cell, const Vec &values,
119  double local_values[]) const = 0;
120 
121  /**
122  * @brief Compute hash value of DOF handler.
123  */
124  virtual std::size_t hash() const =0;
125 
126  /// Destructor.
127  virtual ~DOFHandlerBase() {};
128 
129 protected:
130 
131  /**
132  * @brief Index of first global dof.
133  *
134  * Positive value indicates that the first @p global_dof_offset
135  * entries in the global dof vector are reserved for a different
136  * DOFHandler.
137  */
138  unsigned int global_dof_offset;
139 
140  /**
141  * @brief Number of global dofs assigned by the handler.
142  */
143  unsigned int n_dofs;
144 
145  /**
146  * @brief Number of dofs associated to local process.
147  */
148  unsigned int lsize_;
149 
150  /**
151  * @brief Index of the first dof on the local process.
152  */
153  unsigned int loffset_;
154 
155  /// Max. number of dofs per element.
156  unsigned int max_elem_dofs_;
157 
158  /**
159  * @brief Pointer to the mesh to which the dof handler is associated.
160  */
162 
163  /**
164  * @brief Distribution of dofs associated to local process.
165  */
167 
168 };
169 
170 
171 
172 
173 ///**
174 // * @brief Provides the numbering of the finite element degrees of freedom
175 // * on the computational mesh.
176 // *
177 // * Class DOFHandler distributes the degrees of freedom (dof) for
178 // * a particular finite element on the computational mesh
179 // * and provides mappings between local and global dofs.
180 // * The template parameter @p dim denotes the spatial dimension of
181 // * the reference finite element.
182 // *
183 // * Currently the functionality is restricted to discontinuous
184 // * finite elements, i.e. when the neighboring elements do not
185 // * share any common dof.
186 // */
187 //template<unsigned int dim, unsigned int spacedim>
188 //class DOFHandler : public DOFHandlerBase {
189 //public:
190 //
191 // /**
192 // * @brief Constructor.
193 // * @param _mesh The mesh.
194 // */
195 // DOFHandler(Mesh &_mesh);
196 //
197 // /**
198 // * @brief Alias for iterator over cells.
199 // *
200 // * TODO: Notation to be fixed: element or cell
201 // * TODO: Iterator goes through cells of all dimensions, but
202 // * should go only through dim-dimensional ones.
203 // */
204 // typedef ElementFullIter CellIterator;
205 //
206 // /**
207 // * @brief Distributes degrees of freedom on the mesh needed
208 // * for the given finite element.
209 // *
210 // * The additional parameter @p offset allows to reserve space
211 // * for another finite element dofs in the beginning of the
212 // * global dof vector.
213 // *
214 // * @param fe The finite element.
215 // * @param offset The offset.
216 // */
217 // void distribute_dofs(FiniteElement<dim,spacedim> &fe, const unsigned int offset = 0);
218 //
219 // /**
220 // * @brief Getter for the number of dofs at a single cell.
221 // *
222 // * This value depends on the given finite element.
223 // */
224 // const unsigned int n_local_dofs();
225 //
226 // /**
227 // * @brief Returns the global indices of dofs associated to the @p cell.
228 // *
229 // * @param cell The cell.
230 // * @param indices Array of dof indices on the cell.
231 // */
232 // void get_dof_indices(const CellIterator &cell, unsigned int indices[]);
233 //
234 // /**
235 // * @brief Returns the dof values associated to the @p cell.
236 // *
237 // * @param cell The cell.
238 // * @param values The global vector of values.
239 // * @param local_values Array of values at local dofs.
240 // */
241 // void get_dof_values(const CellIterator &cell, const Vec &values,
242 // double local_values[]);
243 //
244 // /// Destructor.
245 // ~DOFHandler();
246 //
247 //private:
248 //
249 // /**
250 // * @brief Pointer to the finite element class for which the handler
251 // * distributes dofs.
252 // */
253 // FiniteElement<dim,spacedim> *finite_element;
254 //
255 // /**
256 // * @brief Number of dofs associated to geometrical entities.
257 // *
258 // * Global numbers of dofs associated to nodes (object_dofs[0]),
259 // * 1D edges (object_dofs[1]), 2D faces (object_difs[2]) and
260 // * volumes (object_dofs[3]).
261 // */
262 // int ***object_dofs;
263 //
264 //};
265 
266 
267 
269 public:
270 
271  /**
272  * @brief Constructor.
273  * @param _mesh The mesh.
274  */
275  DOFHandlerMultiDim(Mesh &_mesh);
276 
277  /**
278  * @brief Alias for iterator over cells.
279  *
280  * TODO: Notation to be fixed: element or cell
281  */
283 
284  /**
285  * @brief Distributes degrees of freedom on the mesh needed
286  * for the given finite elements.
287  *
288  * The additional parameter @p offset allows to reserve space
289  * for another finite element dofs in the beginning of the
290  * global dof vector.
291  *
292  * @param fe1d The 1D finite element.
293  * @param fe2d The 2D finite element.
294  * @param fe3d The 3D finite element.
295  * @param offset The offset.
296  */
297  void distribute_dofs(FiniteElement<1,3> &fe1d,
298  FiniteElement<2,3> &fe2d,
299  FiniteElement<3,3> &fe3d,
300  const unsigned int offset = 0);
301 
302  /**
303  * @brief Returns the global indices of dofs associated to the @p cell.
304  *
305  * @param cell The cell.
306  * @param indices Array of dof indices on the cell.
307  */
308  unsigned int get_dof_indices(const CellIterator &cell, std::vector<IdxInt> &indices) const override;
309 
310  /**
311  * @brief Returns the indices of dofs associated to the @p cell on the local process.
312  *
313  * @param cell The cell.
314  * @param indices Array of dof indices on the cell.
315  */
316  unsigned int get_loc_dof_indices(const CellIterator &cell, std::vector<IdxInt> &indices) const override;
317 
318  /**
319  * @brief Returns the dof values associated to the @p cell.
320  *
321  * @param cell The cell.
322  * @param values The global vector of values.
323  * @param local_values Array of values at local dofs.
324  */
325  void get_dof_values(const CellIterator &cell, const Vec &values,
326  double local_values[]) const override;
327 
328  /**
329  * @brief Returns the global index of local element.
330  *
331  * @param loc_el Local index of element.
332  */
333  inline int el_index(int loc_el) const { return el_4_loc[loc_el]; }
334 
335  /**
336  * @brief Returns the global index of local edge.
337  *
338  * @param loc_edg Local index of edge.
339  */
340  inline IdxInt edge_index(int loc_edg) const { return edg_4_loc[loc_edg]; }
341 
342  /**
343  * @brief Returns the global index of local neighbour.
344  *
345  * @param loc_nb Local index of neighbour.
346  */
347  inline IdxInt nb_index(int loc_nb) const { return nb_4_loc[loc_nb]; }
348 
349  /**
350  * @brief Returns number of local edges.
351  */
352  inline unsigned int n_loc_edges() const { return edg_4_loc.size(); }
353 
354  /**
355  * @brief Returns number of local neighbours.
356  */
357  inline unsigned int n_loc_nb() const { return nb_4_loc.size(); }
358 
359  /**
360  * Returns true if element is on local process.
361  * @param index Global element index.
362  */
363  bool el_is_local(int index) const;
364 
365  /// Returns finite element object for given space dimension.
366  template<unsigned int dim>
367  FiniteElement<dim,3> *fe() const;
368 
369  /**
370  * Implements @p DOFHandlerBase::hash.
371  */
372  std::size_t hash() const override;
373 
374  /// Destructor.
375  ~DOFHandlerMultiDim() override;
376 
377 private:
378 
379  /**
380  * @brief Prepare parallel distribution of elements, edges and neighbours.
381  */
382  void make_elem_partitioning();
383 
384  /**
385  * @brief Pointer to the finite element class for which the handler
386  * distributes dofs.
387  */
391 
392  /**
393  * @brief Number of dofs associated to geometrical entities.
394  *
395  * Global numbers of dofs associated to nodes (object_dofs[0]),
396  * 1D edges (object_dofs[1]), 2D faces (object_difs[2]) and
397  * volumes (object_dofs[3]).
398  */
400 
401 
402  /// Global element index -> index according to partitioning
404  /// Local element index -> global element index
406  /// Distribution of elements
408 
409  /// Local edge index -> global edge index
411 
412  /// Local neighbour index -> global neighbour index
414 
415 };
416 
417 
418 
419 
420 #endif /* DOFHANDLER_HH_ */
IdxInt edge_index(int loc_edg) const
Returns the global index of local edge.
Definition: dofhandler.hh:340
int IdxInt
Define integers that are indices into large arrays (elements, nodes, dofs etc.)
Definition: mesh.h:85
virtual unsigned int get_loc_dof_indices(const CellIterator &cell, std::vector< IdxInt > &indices) const =0
Fill vector of the indices of dofs associated to the cell on the local process.
ElementFullIter CellIterator
Alias for iterator over cells.
Definition: dofhandler.hh:282
const unsigned int lsize() const
Returns the number of dofs on the current process.
Definition: dofhandler.hh:73
virtual unsigned int get_dof_indices(const CellIterator &cell, std::vector< IdxInt > &indices) const =0
Fill vector of the global indices of dofs associated to the cell.
vector< IdxInt > nb_4_loc
Local neighbour index -> global neighbour index.
Definition: dofhandler.hh:413
IdxInt *** object_dofs
Number of dofs associated to geometrical entities.
Definition: dofhandler.hh:399
FiniteElement< 3, 3 > * fe3d_
Definition: dofhandler.hh:390
virtual ~DOFHandlerBase()
Destructor.
Definition: dofhandler.hh:127
FiniteElement< 1, 3 > * fe1d_
Pointer to the finite element class for which the handler distributes dofs.
Definition: dofhandler.hh:388
Distribution * el_ds_
Distribution of elements.
Definition: dofhandler.hh:407
Definition: mesh.h:99
vector< IdxInt > edg_4_loc
Local edge index -> global edge index.
Definition: dofhandler.hh:410
unsigned int global_dof_offset
Index of first global dof.
Definition: dofhandler.hh:127
unsigned int n_loc_nb() const
Returns number of local neighbours.
Definition: dofhandler.hh:357
int el_index(int loc_el) const
Returns the global index of local element.
Definition: dofhandler.hh:333
Mesh * mesh() const
Returns the mesh.
Definition: dofhandler.hh:93
unsigned int lsize_
Number of dofs associated to local process.
Definition: dofhandler.hh:148
IdxInt nb_index(int loc_nb) const
Returns the global index of local neighbour.
Definition: dofhandler.hh:347
const unsigned int loffset() const
Returns the offset of the local part of dofs.
Definition: dofhandler.hh:78
const unsigned int offset() const
Returns the number of the first global dof handled by this DOFHandler.
Definition: dofhandler.hh:68
const unsigned int max_elem_dofs() const
Returns max. number of dofs on one element.
Definition: dofhandler.hh:83
Mesh * mesh_
Pointer to the mesh to which the dof handler is associated.
Definition: dofhandler.hh:161
Provides the numbering of the finite element degrees of freedom on the computational mesh...
Definition: dofhandler.hh:268
unsigned int max_elem_dofs_
Max. number of dofs per element.
Definition: dofhandler.hh:156
ElementFullIter CellIterator
Alias for iterator over cells.
Definition: dofhandler.hh:47
Distribution * ds_
Distribution of dofs associated to local process.
Definition: dofhandler.hh:166
unsigned int n_loc_edges() const
Returns number of local edges.
Definition: dofhandler.hh:352
virtual std::size_t hash() const =0
Compute hash value of DOF handler.
virtual void get_dof_values(const CellIterator &cell, const Vec &values, double local_values[]) const =0
Returns the dof values associated to the cell.
Support classes for parallel programing.
FiniteElement< 2, 3 > * fe2d_
Definition: dofhandler.hh:389
unsigned int loffset_
Index of the first dof on the local process.
Definition: dofhandler.hh:153
IdxInt * row_4_el
Global element index -> index according to partitioning.
Definition: dofhandler.hh:403
unsigned int n_dofs
Number of global dofs assigned by the handler.
Definition: dofhandler.hh:143
Abstract class for the description of a general finite element on a reference simplex in dim dimensio...
Definition: dofhandler.hh:30
Distribution * distr() const
Returns the parallel distribution of dofs.
Definition: dofhandler.hh:88
DOFHandlerBase(Mesh &_mesh)
Constructor.
Definition: dofhandler.hh:47
IdxInt * el_4_loc
Local element index -> global element index.
Definition: dofhandler.hh:405
const unsigned int n_global_dofs() const
Getter for the number of all mesh dofs required by the given finite element.
Definition: dofhandler.hh:62