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