Flow123d  release_3.0.0-885-g06276d1
output_mesh.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 output_mesh.hh
15  * @brief Classes for auxiliary output mesh.
16  */
17 
18 #ifndef OUTPUT_MESH_HH_
19 #define OUTPUT_MESH_HH_
20 
21 #include <memory> // for shared_ptr, enable_shared_from_...
22 #include <string> // for string
23 #include <vector> // for vector
24 #include "input/accessors.hh" // for Record
25 #include "mesh/point.hh"
26 #include "tools/general_iterator.hh" // for Iter
27 
28 class Mesh;
29 class OutputElement;
30 class OutputMesh;
32 namespace Input { namespace Type { class Record; } }
33 template<class T> class ElementDataCache;
34 template<int> class ElementAccessor;
35 
36 
38 
39 
40 /**
41  * @brief Base class for Output mesh.
42  *
43  * Defines common members for Output mesh classes:
44  * - OutputMesh represents output mesh with continuous elements
45  * - OutputMeshDiscontinuous represents output mesh with discontinuous elements
46  *
47  * Making of output meshes and calling of their initialization methods must be execute in correct order, see example:
48 @code
49  // Create or get Mesh object
50  Mesh * my_mesh = ...
51 
52  // Construct mesh with continuous elements
53  std::make_shared<OutputMesh> output_mesh = std::make_shared<OutputMesh>(*my_mesh);
54  // Creates the sub meshes on all processes identical to the computational one.
55  output_mesh->create_sub_mesh();
56  // Creates mesh on zero process identical to the computational one.
57  std::make_shared<OutputMesh> serial_output_mesh = output_mesh->make_serial_master_mesh();
58 
59  // Construct mesh with discontinuous elements
60  std::make_shared<OutputMeshDiscontinuous> output_mesh_discont = std::make_shared<OutputMeshDiscontinuous>(*my_mesh);
61  // Creates sub meshes on all processes mesh from the original my_mesh.
62  output_mesh_discont->create_sub_mesh();
63  // Creates mesh on zero process from the original my_mesh.
64  std::make_shared<OutputMeshDiscontinuous> serial_output_mesh = output_mesh->make_serial_master_mesh();
65 @endcode
66  */
67 class OutputMeshBase : public std::enable_shared_from_this<OutputMeshBase>
68 {
69 public:
70  /// Shortcut instead of spacedim template. We suppose only spacedim=3 at the moment.
71  static const unsigned int spacedim = 3;
72 
73  typedef std::function<void(const std::vector< Space<spacedim>::Point > &, const ElementAccessor<spacedim> &, std::vector<double> &)>
75 
76  /// Constructor. Takes computational mesh as a parameter.
77  OutputMeshBase(Mesh &mesh);
78  /// Constructor. Takes computational mesh and input record as a parameters.
79  OutputMeshBase(Mesh &mesh, const Input::Record &in_rec);
80  virtual ~OutputMeshBase();
81 
82  /**
83  * @brief The specification of output mesh.
84  * @return record for output mesh
85  */
86  static const Input::Type::Record & get_input_type();
87 
88  /// Gives iterator to the FIRST element of the output mesh.
89  OutputElementIterator begin();
90  /// Gives iterator to the LAST element of the output mesh.
92 
93  /**
94  * Creates sub mesh containing only local part of original (computation) mesh.
95  *
96  * TODO: should be replaced by local part of future parallel computational mesh.
97  */
98  void create_sub_mesh();
99 
100  /**
101  * Creates refined sub mesh containing only local part of original (computation) mesh.
102  */
103  virtual void create_refined_sub_mesh()=0;
104 
105  /// Selects the error control field computing function of output field set according to input record.
106  void set_error_control_field(ErrorControlFieldFunc error_control_field_func);
107 
108  /// Returns number of nodes.
109  unsigned int n_nodes();
110  /// Returns number of element.
111  unsigned int n_elements();
112 
113  /// Check if nodes_, connectivity_ and offsets_ data caches are created
114  bool is_created();
115 
116  /// Create nodes and elements data caches
117  void create_id_caches();
118 
119  /// Synchronize parallel data and create serial COLECTIVE output mesh on zero process.
120  void make_serial_master_mesh();
121 
122  /// Create output mesh of parallel output (implemented only for discontinuous mesh)
124  {};
125 
126  /// Return master output mesh if exists or shared_ptr of this object.
127  inline std::shared_ptr<OutputMeshBase> get_master_mesh() {
128  if (master_mesh_) return master_mesh_;
129  else return shared_from_this();
130  };
131 
132 protected:
133  /**
134  * Possible types of OutputMesh.
135  */
136  enum MeshType
137  {
138  orig, //!< same as original (computational) mesh
139  refined, //!< refined mesh
140  discont //!< discontinuous mesh
141  };
142 
143  /**
144  * Construct empty output mesh.
145  *
146  * Use in make_serial_master_mesh method and create mesh of same type as this object (continuous / discontinuos)
147  */
148  virtual std::shared_ptr<OutputMeshBase> construct_mesh()=0;
149 
150  /**
151  * Create serial (collective) nodes cache on zero process.
152  *
153  * Implements part of \p make_serial_master_mesh that are specific for continuous and discontinuous case.
154  */
155  virtual std::shared_ptr<ElementDataCache<double>> make_serial_nodes_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets)=0;
156 
157  /**
158  * Create serial (collective) connectivity cache on zero process.
159  *
160  * Implements part of \p make_serial_master_mesh that are specific for continuous and discontinuous case.
161  */
162  virtual std::shared_ptr<ElementDataCache<unsigned int>> make_serial_connectivity_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets)=0;
163 
164  /// Compute and return number of nodes for each elements (compute from offsets)
165  std::shared_ptr<ElementDataCache<unsigned int>> get_elems_n_nodes();
166 
167  /// Input record for output mesh.
169 
170  /// Pointer to the computational mesh.
172 
173  /// Maximal level of refinement.
174  const unsigned int max_level_;
175 
176  /// Refinement error control field function (hold value_list function of field).
178 
179  MeshType mesh_type_; ///< Type of OutputMesh
180  bool refine_by_error_; ///< True, if output mesh is to be refined by error criterion.
181  double refinement_error_tolerance_; ///< Tolerance for error criterion refinement.
182 
183  /// Vector of element indices in the computational mesh. (Important when refining.)
184  std::shared_ptr<std::vector<unsigned int>> orig_element_indices_;
185 
186  /// Vector of node coordinates. [spacedim x n_nodes]
187  std::shared_ptr<ElementDataCache<double>> nodes_;
188  /// Vector maps the nodes to their coordinates in vector @p nodes_.
189  std::shared_ptr<ElementDataCache<unsigned int>> connectivity_;
190  /// Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
191  std::shared_ptr<ElementDataCache<unsigned int>> offsets_;
192 
193  /// Vector gets ids of nodes. Data is used in GMSH output.
194  std::shared_ptr<ElementDataCache<unsigned int>> node_ids_;
195  /// Vector gets ids of elements. Data is used in GMSH output.
196  std::shared_ptr<ElementDataCache<unsigned int>> elem_ids_;
197  /// Vector gets ids of regions. Data is used in GMSH output.
198  std::shared_ptr<ElementDataCache<unsigned int>> region_ids_;
199  /// Vector gets partitions of elements. Data is used in GMSH output.
200  std::shared_ptr<ElementDataCache<int>> partitions_;
201 
202  /**
203  * Master OutputMesh.
204  *
205  * - serial output: is constructed on zero process (collective) and allow to produce serial output of parallel computation
206  * - parallel output: is constructed on each process only for discontinuous mesh
207  */
208  std::shared_ptr<OutputMeshBase> master_mesh_;
209 
210  /**
211  * Next variables hold distributions of elements and nodes. They differ for mesh types
212  * - continuous and discontinuous mesh shared objects with computational (orig) mesh
213  * - refined mesh creates own objects
214  */
215  LongIdx *el_4_loc_; ///< Index set assigning to local element index its global index.
216  Distribution *el_ds_; ///< Parallel distribution of elements.
217  LongIdx *node_4_loc_; ///< Index set assigning to local node index its global index.
218  Distribution *node_ds_; ///< Parallel distribution of nodes. Depends on elements distribution.
219  unsigned int n_local_nodes_; ///< Hold number of local nodes (own + ghost), value is equal with size of node_4_loc array.
220 
221  /// Friend provides access to vectors for element accessor class.
222  friend class OutputElement;
223  friend class OutputTime;
224  friend class OutputMSH;
225  friend class OutputVTK;
226  friend class OutputMesh;
228 };
229 
230 
231 /// @brief Class represents output mesh with continuous elements.
233 {
234 public:
235  OutputMesh(Mesh &mesh);
236  OutputMesh(Mesh &mesh, const Input::Record &in_rec);
237  ~OutputMesh();
238 
239  /// Implements OutputMeshBase::create_refined_sub_mesh
240  void create_refined_sub_mesh() override;
241 
242 protected:
243  bool refinement_criterion();
244 
245  /// Implements OutputMeshBase::construct_mesh
246  std::shared_ptr<OutputMeshBase> construct_mesh() override;
247 
248  /// Implements OutputMeshBase::make_serial_nodes_cache
249  std::shared_ptr<ElementDataCache<double>> make_serial_nodes_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
250 
251  /// Implements OutputMeshBase::make_serial_connectivity_cache
252  std::shared_ptr<ElementDataCache<unsigned int>> make_serial_connectivity_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
253 
254  /// Friend provides access to vectors for discontinous output mesh.
256 };
257 
258 
259 /// @brief Class represents output mesh with discontinuous elements.
261 {
262 public:
264  OutputMeshDiscontinuous(Mesh &mesh, const Input::Record& in_rec);
266 
267  /// Implements OutputMeshBase::create_refined_sub_mesh
268  void create_refined_sub_mesh() override;
269 
270  /// Overrides OutputMeshBase::make_parallel_master_mesh
271  void make_parallel_master_mesh() override;
272 
273 protected:
274  ///Auxiliary structure defining element of refined output mesh.
275  struct AuxElement{
277  unsigned int level;
278  };
279 
280  ///Performs the actual refinement of AuxElement. Recurrent.
281  template<int dim>
282  void refine_aux_element(const AuxElement& aux_element,
283  std::vector< AuxElement >& refinement,
284  const ElementAccessor<spacedim> &ele_acc
285  );
286 
287  /// Collects different refinement criteria results.
288  bool refinement_criterion(const AuxElement& ele,
289  const ElementAccessor<spacedim> &ele_acc);
290 
291  /// Refinement flag - checks only maximal level of refinement.
292  bool refinement_criterion_uniform(const AuxElement& ele);
293 
294  /// Refinement flag - measures discretisation error according to error control field.
295  bool refinement_criterion_error(const AuxElement& ele,
296  const Space<spacedim>::Point &centre,
297  const ElementAccessor<spacedim> &ele_acc
298  );
299 
300  /// Implements OutputMeshBase::construct_mesh
301  std::shared_ptr<OutputMeshBase> construct_mesh() override;
302 
303  /// Implements OutputMeshBase::make_serial_nodes_cache
304  std::shared_ptr<ElementDataCache<double>> make_serial_nodes_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
305 
306  /// Implements OutputMeshBase::make_serial_connectivity_cache
307  std::shared_ptr<ElementDataCache<unsigned int>> make_serial_connectivity_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
308 };
309 
310 #endif // OUTPUT_MESH_HH_
311 
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:232
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
Iter< OutputElement > OutputElementIterator
Definition: output_mesh.hh:34
Base class for Output mesh.
Definition: output_mesh.hh:67
Distribution * el_ds_
Parallel distribution of elements.
Definition: output_mesh.hh:216
Represents an element of the output mesh. Provides element access on the data of the output mesh (nod...
std::function< void(const std::vector< Space< spacedim >::Point > &, const ElementAccessor< spacedim > &, std::vector< double > &)> ErrorControlFieldFunc
Definition: output_mesh.hh:74
Mesh * orig_mesh_
Pointer to the computational mesh.
Definition: output_mesh.hh:171
Abstract linear system class.
Definition: balance.hh:35
const unsigned int max_level_
Maximal level of refinement.
Definition: output_mesh.hh:174
Definition: mesh.h:80
Template Iter serves as general template for internal iterators.
double refinement_error_tolerance_
Tolerance for error criterion refinement.
Definition: output_mesh.hh:181
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_mesh.hh:189
std::shared_ptr< ElementDataCache< unsigned int > > node_ids_
Vector gets ids of nodes. Data is used in GMSH output.
Definition: output_mesh.hh:194
Distribution * node_ds_
Parallel distribution of nodes. Depends on elements distribution.
Definition: output_mesh.hh:218
std::shared_ptr< ElementDataCache< unsigned int > > offsets_
Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
Definition: output_mesh.hh:191
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_mesh.hh:187
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:260
std::shared_ptr< OutputMeshBase > get_master_mesh()
Return master output mesh if exists or shared_ptr of this object.
Definition: output_mesh.hh:127
ErrorControlFieldFunc error_control_field_func_
Refinement error control field function (hold value_list function of field).
Definition: output_mesh.hh:177
arma::vec::fixed< spacedim > Point
Definition: point.hh:42
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
Auxiliary structure defining element of refined output mesh.
Definition: output_mesh.hh:275
This class is used for output data to VTK file format.
Definition: output_vtk.hh:42
std::shared_ptr< ElementDataCache< unsigned int > > region_ids_
Vector gets ids of regions. Data is used in GMSH output.
Definition: output_mesh.hh:198
The class for outputting data during time.
Definition: output_time.hh:50
LongIdx * node_4_loc_
Index set assigning to local node index its global index.
Definition: output_mesh.hh:217
Input::Record input_record_
Input record for output mesh.
Definition: output_mesh.hh:168
General iterator template. Provides iterator over objects in some container.
std::shared_ptr< ElementDataCache< int > > partitions_
Vector gets partitions of elements. Data is used in GMSH output.
Definition: output_mesh.hh:200
std::shared_ptr< OutputMeshBase > master_mesh_
Definition: output_mesh.hh:208
bool refine_by_error_
True, if output mesh is to be refined by error criterion.
Definition: output_mesh.hh:180
virtual void make_parallel_master_mesh()
Create output mesh of parallel output (implemented only for discontinuous mesh)
Definition: output_mesh.hh:123
MeshType mesh_type_
Type of OutputMesh.
Definition: output_mesh.hh:179
Record type proxy class.
Definition: type_record.hh:182
LongIdx * el_4_loc_
Index set assigning to local element index its global index.
Definition: output_mesh.hh:215
std::vector< Space< spacedim >::Point > nodes
Definition: output_mesh.hh:276
std::shared_ptr< ElementDataCache< unsigned int > > elem_ids_
Vector gets ids of elements. Data is used in GMSH output.
Definition: output_mesh.hh:196
same as original (computational) mesh
Definition: output_mesh.hh:138
This class is used for output data to VTK file format.
Definition: output_msh.hh:30
unsigned int n_local_nodes_
Hold number of local nodes (own + ghost), value is equal with size of node_4_loc array.
Definition: output_mesh.hh:219
std::shared_ptr< std::vector< unsigned int > > orig_element_indices_
Vector of element indices in the computational mesh. (Important when refining.)
Definition: output_mesh.hh:184