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