Flow123d  DF_patch_fe_data_tables-3227f73
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 #include "la/distribution.hh" // for Distribution
30 
31 class Mesh;
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.
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.
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  /**
136  * Return local element index to its global index.
137  *
138  * Temporary method. Used only in output assembly classes. DO NOT USE in other code!
139  */
140  inline LongIdx get_loc_elem_idx(LongIdx global_idx) {
141  ASSERT_LT(global_idx, (int)el_ds_->size()).error("Index of element is out of mesh.\n");
142  ASSERT(loc_4_el_[global_idx] != -1)(global_idx).error("Element is not local.\n");
143  return loc_4_el_[global_idx];
144  }
145 
146  /// Getter to offsets data
147  inline std::shared_ptr<ElementDataCache<unsigned int>> offsets() const {
148  return offsets_;
149  }
150 
151 protected:
152  /**
153  * Possible types of OutputMesh.
154  */
155  enum MeshType
156  {
157  orig, //!< same as original (computational) mesh
158  refined, //!< refined mesh
159  discont //!< discontinuous mesh
160  };
161 
162  /**
163  * Construct empty output mesh.
164  *
165  * Use in make_serial_master_mesh method and create mesh of same type as this object (continuous / discontinuos)
166  */
167  virtual std::shared_ptr<OutputMeshBase> construct_mesh()=0;
168 
169  /**
170  * Create serial (collective) nodes cache on zero process.
171  *
172  * Implements part of \p make_serial_master_mesh that are specific for continuous and discontinuous case.
173  */
174  virtual std::shared_ptr<ElementDataCache<double>> make_serial_nodes_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets)=0;
175 
176  /**
177  * Create serial (collective) connectivity cache on zero process.
178  *
179  * Implements part of \p make_serial_master_mesh that are specific for continuous and discontinuous case.
180  */
181  virtual std::shared_ptr<ElementDataCache<unsigned int>> make_serial_connectivity_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets)=0;
182 
183  /// Compute and return number of nodes for each elements (compute from offsets)
184  std::shared_ptr<ElementDataCache<unsigned int>> get_elems_n_nodes();
185 
186  /// Input record for output mesh.
188 
189  /// Pointer to the computational mesh.
191 
192  /// Maximal level of refinement.
193  const unsigned int max_level_;
194 
195  /// Refinement error control field function (hold value_list function of field).
197 
198  MeshType mesh_type_; ///< Type of OutputMesh
199  bool refine_by_error_; ///< True, if output mesh is to be refined by error criterion.
200  double refinement_error_tolerance_; ///< Tolerance for error criterion refinement.
201 
202  /// Vector of element indices in the computational mesh. (Important when refining.)
203  std::shared_ptr<std::vector<unsigned int>> orig_element_indices_;
204 
205  /// Vector of node coordinates. [spacedim x n_nodes]
206  std::shared_ptr<ElementDataCache<double>> nodes_;
207  /// Vector maps the nodes to their coordinates in vector @p nodes_.
208  std::shared_ptr<ElementDataCache<unsigned int>> connectivity_;
209  /// Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
210  std::shared_ptr<ElementDataCache<unsigned int>> offsets_;
211 
212  /// Vector gets ids of nodes. Data is used in GMSH output.
213  std::shared_ptr<ElementDataCache<unsigned int>> node_ids_;
214  /// Vector gets ids of elements. Data is used in GMSH output.
215  std::shared_ptr<ElementDataCache<unsigned int>> elem_ids_;
216  /// Vector gets ids of regions. Data is used in GMSH output.
217  std::shared_ptr<ElementDataCache<unsigned int>> region_ids_;
218  /// Vector gets partitions of elements. Data is used in GMSH output.
219  std::shared_ptr<ElementDataCache<int>> partitions_;
220 
221  /**
222  * Master OutputMesh.
223  *
224  * - serial output: is constructed on zero process (collective) and allow to produce serial output of parallel computation
225  * - parallel output: is constructed on each process only for discontinuous mesh
226  */
227  std::shared_ptr<OutputMeshBase> master_mesh_;
228 
229  /**
230  * Next variables hold distributions of elements and nodes. They differ for mesh types
231  * - continuous and discontinuous mesh shared objects with computational (orig) mesh
232  * - refined mesh creates own objects
233  */
234  LongIdx *el_4_loc_; ///< Index set assigning to local element index its global index.
235  LongIdx *loc_4_el_; ///< Index set assigning to global index its local element index.
236  Distribution *el_ds_; ///< Parallel distribution of elements.
237  LongIdx *node_4_loc_; ///< Index set assigning to local node index its global index.
238  Distribution *node_ds_; ///< Parallel distribution of nodes. Depends on elements distribution.
239  unsigned int n_local_nodes_; ///< Hold number of local nodes (own + ghost), value is equal with size of node_4_loc array.
240 
241  /// Friend provides access to vectors for element accessor class.
242  friend class OutputElement;
243  friend class OutputTime;
244  friend class OutputMSH;
245  friend class OutputVTK;
246  friend class OutputMesh;
248 };
249 
250 
251 /// @brief Class represents output mesh with continuous elements.
253 {
254 public:
255  OutputMesh(Mesh &mesh);
256  OutputMesh(Mesh &mesh, const Input::Record &in_rec);
257  ~OutputMesh();
258 
259  /// Implements OutputMeshBase::create_refined_sub_mesh
260  void create_refined_sub_mesh() override;
261 
262 protected:
263  bool refinement_criterion();
264 
265  /// Implements OutputMeshBase::construct_mesh
266  std::shared_ptr<OutputMeshBase> construct_mesh() override;
267 
268  /// Implements OutputMeshBase::make_serial_nodes_cache
269  std::shared_ptr<ElementDataCache<double>> make_serial_nodes_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
270 
271  /// Implements OutputMeshBase::make_serial_connectivity_cache
272  std::shared_ptr<ElementDataCache<unsigned int>> make_serial_connectivity_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
273 
274  /// Friend provides access to vectors for discontinous output mesh.
276 };
277 
278 
279 /// @brief Class represents output mesh with discontinuous elements.
281 {
282 public:
284  OutputMeshDiscontinuous(Mesh &mesh, const Input::Record& in_rec);
286 
287  /// Implements OutputMeshBase::create_refined_sub_mesh
288  void create_refined_sub_mesh() override;
289 
290  /// Overrides OutputMeshBase::make_parallel_master_mesh
291  void make_parallel_master_mesh() override;
292 
293 protected:
294  ///Auxiliary structure defining element of refined output mesh.
295  struct AuxElement{
297  unsigned int level;
298  };
299 
300  ///Performs the actual refinement of AuxElement. Recurrent.
301  template<int dim>
302  void refine_aux_element(const AuxElement& aux_element,
303  std::vector< AuxElement >& refinement,
304  const ElementAccessor<spacedim> &ele_acc
305  );
306 
307  /// Collects different refinement criteria results.
308  bool refinement_criterion(const AuxElement& ele,
309  const ElementAccessor<spacedim> &ele_acc);
310 
311  /// Refinement flag - checks only maximal level of refinement.
312  bool refinement_criterion_uniform(const AuxElement& ele);
313 
314  /// Refinement flag - measures discretisation error according to error control field.
315  bool refinement_criterion_error(const AuxElement& ele,
316  const Space<spacedim>::Point &centre,
317  const ElementAccessor<spacedim> &ele_acc
318  );
319 
320  /// Implements OutputMeshBase::construct_mesh
321  std::shared_ptr<OutputMeshBase> construct_mesh() override;
322 
323  /// Implements OutputMeshBase::make_serial_nodes_cache
324  std::shared_ptr<ElementDataCache<double>> make_serial_nodes_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
325 
326  /// Implements OutputMeshBase::make_serial_connectivity_cache
327  std::shared_ptr<ElementDataCache<unsigned int>> make_serial_connectivity_cache(std::shared_ptr<ElementDataCache<unsigned int>> global_offsets) override;
328 };
329 
330 #endif // OUTPUT_MESH_HH_
331 
#define ASSERT(expr)
Definition: asserts.hh:351
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
unsigned int size() const
get global size
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Record type proxy class.
Definition: type_record.hh:182
General iterator template. Provides iterator over objects of type Object in some container.
Definition: mesh.h:362
Represents an element of the output mesh. Provides element access on the data of the output mesh (nod...
This class is used for output data to VTK file format.
Definition: output_msh.hh:30
Base class for Output mesh.
Definition: output_mesh.hh:71
double refinement_error_tolerance_
Tolerance for error criterion refinement.
Definition: output_mesh.hh:200
virtual std::shared_ptr< ElementDataCache< double > > make_serial_nodes_cache(std::shared_ptr< ElementDataCache< unsigned int >> global_offsets)=0
OutputMeshBase(Mesh &mesh)
Constructor. Takes computational mesh as a parameter.
Definition: output_mesh.cc:46
LongIdx * node_4_loc_
Index set assigning to local node index its global index.
Definition: output_mesh.hh:237
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_mesh.hh:208
void make_serial_master_mesh()
Synchronize parallel data and create serial COLECTIVE output mesh on zero process.
Definition: output_mesh.cc:217
std::function< void(const Armor::array &, const ElementAccessor< spacedim > &, std::vector< double > &)> ErrorControlFieldFunc
Definition: output_mesh.hh:77
std::shared_ptr< OutputMeshBase > get_master_mesh()
Return master output mesh if exists or shared_ptr of this object.
Definition: output_mesh.hh:130
@ orig
same as original (computational) mesh
Definition: output_mesh.hh:157
@ discont
discontinuous mesh
Definition: output_mesh.hh:159
@ refined
refined mesh
Definition: output_mesh.hh:158
virtual std::shared_ptr< OutputMeshBase > construct_mesh()=0
static const Input::Type::Record & get_input_type()
The specification of output mesh.
Definition: output_mesh.cc:31
std::shared_ptr< ElementDataCache< unsigned int > > get_elems_n_nodes()
Compute and return number of nodes for each elements (compute from offsets)
Definition: output_mesh.cc:256
virtual void make_parallel_master_mesh()
Create output mesh of parallel output (implemented only for discontinuous mesh)
Definition: output_mesh.hh:126
unsigned int n_elements()
Returns number of element.
Definition: output_mesh.cc:101
friend class OutputMeshDiscontinuous
Definition: output_mesh.hh:247
std::shared_ptr< OutputMeshBase > master_mesh_
Definition: output_mesh.hh:227
bool is_created()
Check if nodes_, connectivity_ and offsets_ data caches are created.
Definition: output_mesh.cc:145
LongIdx * el_4_loc_
Index set assigning to local element index its global index.
Definition: output_mesh.hh:234
virtual std::shared_ptr< ElementDataCache< unsigned int > > make_serial_connectivity_cache(std::shared_ptr< ElementDataCache< unsigned int >> global_offsets)=0
Input::Record input_record_
Input record for output mesh.
Definition: output_mesh.hh:187
Distribution * el_ds_
Parallel distribution of elements.
Definition: output_mesh.hh:236
std::shared_ptr< ElementDataCache< unsigned int > > node_ids_
Vector gets ids of nodes. Data is used in GMSH output.
Definition: output_mesh.hh:213
void create_sub_mesh()
Definition: output_mesh.cc:151
bool refine_by_error_
True, if output mesh is to be refined by error criterion.
Definition: output_mesh.hh:199
LongIdx get_loc_elem_idx(LongIdx global_idx)
Definition: output_mesh.hh:140
friend class OutputMesh
Definition: output_mesh.hh:246
ErrorControlFieldFunc error_control_field_func_
Refinement error control field function (hold value_list function of field).
Definition: output_mesh.hh:196
OutputElementIterator begin()
Gives iterator to the FIRST element of the output mesh.
Definition: output_mesh.cc:81
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:203
const unsigned int max_level_
Maximal level of refinement.
Definition: output_mesh.hh:193
virtual void create_refined_sub_mesh()=0
OutputElementIterator end()
Gives iterator to the LAST element of the output mesh.
Definition: output_mesh.cc:88
std::shared_ptr< ElementDataCache< unsigned int > > elem_ids_
Vector gets ids of elements. Data is used in GMSH output.
Definition: output_mesh.hh:215
std::shared_ptr< ElementDataCache< int > > partitions_
Vector gets partitions of elements. Data is used in GMSH output.
Definition: output_mesh.hh:219
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_mesh.hh:206
static const unsigned int spacedim
Shortcut instead of spacedim template. We suppose only spacedim=3 at the moment.
Definition: output_mesh.hh:74
MeshType mesh_type_
Type of OutputMesh.
Definition: output_mesh.hh:198
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:210
std::shared_ptr< ElementDataCache< unsigned int > > offsets() const
Getter to offsets data.
Definition: output_mesh.hh:147
unsigned int n_nodes()
Returns number of nodes.
Definition: output_mesh.cc:107
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:239
Mesh * orig_mesh_
Pointer to the computational mesh.
Definition: output_mesh.hh:190
void set_error_control_field(ErrorControlFieldFunc error_control_field_func)
Selects the error control field computing function of output field set according to input record.
Definition: output_mesh.cc:96
LongIdx * loc_4_el_
Index set assigning to global index its local element index.
Definition: output_mesh.hh:235
virtual ~OutputMeshBase()
Definition: output_mesh.cc:70
Distribution * node_ds_
Parallel distribution of nodes. Depends on elements distribution.
Definition: output_mesh.hh:238
std::shared_ptr< ElementDataCache< unsigned int > > region_ids_
Vector gets ids of regions. Data is used in GMSH output.
Definition: output_mesh.hh:217
void create_id_caches()
Create nodes and elements data caches.
Definition: output_mesh.cc:113
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:281
bool refinement_criterion_error(const AuxElement &ele, const Space< spacedim >::Point &centre, const ElementAccessor< spacedim > &ele_acc)
Refinement flag - measures discretisation error according to error control field.
Definition: output_mesh.cc:542
void make_parallel_master_mesh() override
Overrides OutputMeshBase::make_parallel_master_mesh.
Definition: output_mesh.cc:729
void create_refined_sub_mesh() override
Implements OutputMeshBase::create_refined_sub_mesh.
Definition: output_mesh.cc:625
bool refinement_criterion_uniform(const AuxElement &ele)
Refinement flag - checks only maximal level of refinement.
Definition: output_mesh.cc:537
bool refinement_criterion(const AuxElement &ele, const ElementAccessor< spacedim > &ele_acc)
Collects different refinement criteria results.
Definition: output_mesh.cc:516
void refine_aux_element(const AuxElement &aux_element, std::vector< AuxElement > &refinement, const ElementAccessor< spacedim > &ele_acc)
Performs the actual refinement of AuxElement. Recurrent.
Definition: output_mesh.cc:370
std::shared_ptr< ElementDataCache< unsigned int > > make_serial_connectivity_cache(std::shared_ptr< ElementDataCache< unsigned int >> global_offsets) override
Implements OutputMeshBase::make_serial_connectivity_cache.
Definition: output_mesh.cc:610
std::shared_ptr< ElementDataCache< double > > make_serial_nodes_cache(std::shared_ptr< ElementDataCache< unsigned int >> global_offsets) override
Implements OutputMeshBase::make_serial_nodes_cache.
Definition: output_mesh.cc:579
std::shared_ptr< OutputMeshBase > construct_mesh() override
Implements OutputMeshBase::construct_mesh.
Definition: output_mesh.cc:573
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:253
std::shared_ptr< ElementDataCache< unsigned int > > make_serial_connectivity_cache(std::shared_ptr< ElementDataCache< unsigned int >> global_offsets) override
Implements OutputMeshBase::make_serial_connectivity_cache.
Definition: output_mesh.cc:324
std::shared_ptr< ElementDataCache< double > > make_serial_nodes_cache(std::shared_ptr< ElementDataCache< unsigned int >> global_offsets) override
Implements OutputMeshBase::make_serial_nodes_cache.
Definition: output_mesh.cc:312
std::shared_ptr< OutputMeshBase > construct_mesh() override
Implements OutputMeshBase::construct_mesh.
Definition: output_mesh.cc:306
bool refinement_criterion()
Definition: output_mesh.cc:299
void create_refined_sub_mesh() override
Implements OutputMeshBase::create_refined_sub_mesh.
Definition: output_mesh.cc:294
The class for outputting data during time.
Definition: output_time.hh:51
This class is used for output data to VTK file format.
Definition: output_vtk.hh:44
Armor::ArmaVec< double, spacedim > Point
Definition: point.hh:42
Support classes for parallel programing.
Template Iter serves as general template for internal iterators.
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
Abstract linear system class.
Definition: balance.hh:40
Iter< OutputElement > OutputElementIterator
Definition: output_mesh.hh:37
Auxiliary structure defining element of refined output mesh.
Definition: output_mesh.hh:295
std::vector< Space< spacedim >::Point > nodes
Definition: output_mesh.hh:296