Flow123d  DF_mechanic_bench-71cc9cb
element_data_cache_base.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 element_data_cache_base.hh
15  * @brief
16  */
17 
18 #ifndef ELEMENT_DATA_CACHE_BASE_HH_
19 #define ELEMENT_DATA_CACHE_BASE_HH_
20 
21 
22 
23 #include <ostream>
24 #include <string>
25 #include <istream>
26 #include "system/system.hh"
27 #include "system/index_types.hh"
28 
29 class Tokenizer;
30 class Distribution;
31 
32 
34 public:
35 
36  /**
37  * Number of components of element data stored in the database.
38  */
40  N_SCALAR = 1,
41  N_VECTOR = 3,
42  N_TENSOR = 9
43  };
44 
45  /// Types of VTK value
49 
50  /// Constructor.
52  : time_(-std::numeric_limits<double>::infinity()),
53  field_name_(""), is_dummy_(false) {}
54 
55  /// Destructor
56  virtual ~ElementDataCacheBase() {}
57 
58  /// Getter for time of cache
59  double get_time()
60  { return time_; }
61 
62  /// Getter for quantity name of cache
63  std::string field_input_name()
64  { return field_input_name_; }
65 
66  /// Check if cache stored actual data
67  bool is_actual(double time, std::string field_name) {
68  return (time_ == time) && (field_input_name_ == field_name);
69  }
70 
71  /**
72  * Read ascii data of given \p i_row from tokenizer
73  */
74  virtual void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row)=0;
75 
76  /**
77  * Read binary data of given \p i_row from data stream
78  */
79  virtual void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row)=0;
80 
81  /**
82  * Print one value at given index in ascii format
83  */
84  virtual void print_ascii(ostream &out_stream, unsigned int idx) = 0;
85 
86  /**
87  * Print all data in ascii format at once stored in database
88  */
89  virtual void print_ascii_all(ostream &out_stream, unsigned int start=0) = 0;
90 
91  /**
92  * Print all data in binary format at once stored in database
93  */
94  virtual void print_binary_all(ostream &out_stream, bool print_data_size = true, unsigned int start = 0) = 0;
95 
96  /**
97  * Print stored values in the YAML format (using JSON like arrays).
98  * Used for output of observe values.
99  */
100  virtual void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) = 0;
101 
102  /**
103  * Find minimal and maximal range of stored data
104  */
105  virtual void get_min_max_range(double &min, double &max) = 0;
106 
107  /**
108  * Set string representation of SI units.
109  */
110  void set_field_units(std::string unit_string) {
111  this->field_units_ = unit_string;
112  }
113 
114  /**
115  * Set string representation of SI units.
116  */
117  void set_n_values(unsigned int n_values) {
118  this->n_values_ = n_values;
119  }
120 
121  /**
122  * Get string representation of SI units.
123  */
124  inline std::string field_units() const {
125  return this->field_units_;
126  }
127 
128  /**
129  * Get number of data values.
130  */
131  inline unsigned int n_values() const {
132  return this->n_values_;
133  }
134 
135  /**
136  * Is only true when the object is DummyElementDataCache.
137  */
138  inline bool is_dummy() const {
139  return this->is_dummy_;
140  }
141 
142  /**
143  * Get number of data elements per data value.
144  */
145  inline unsigned int n_comp() const {
146  return this->n_comp_;
147  }
148 
149  /**
150  * Get number of DOFs per element.
151  */
152  inline unsigned int n_dofs_per_element() const {
153  return this->n_dofs_per_element_;
154  }
155 
156  /// Get type of stored data
157  inline VTKValueType vtk_type() const {
158  return this->vtk_type_;
159  }
160 
161  /**
162  * Get dof_handler_hash_ value.
163  */
164  inline std::size_t dof_handler_hash() const {
165  return this->dof_handler_hash_;
166  }
167 
168  /**
169  * Set dof_handler_hash_ value.
170  */
171  inline void set_dof_handler_hash(std::size_t hash) {
172  this->dof_handler_hash_ = hash;
173  }
174 
175  /**
176  * Get fe_type_ value.
177  */
178  inline std::string fe_type() const {
179  return this->fe_type_;
180  }
181 
182  /**
183  * Method for gathering parallel data to serial cache.
184  *
185  * Gather data of individual processes to serial cache that is created only on zero process.
186  * Other processes return uninitialized shared pointer.
187  *
188  * @param distr Collective distribution
189  * @param local_to_global Maps local indices to global
190  */
191  virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0;
192 
193  /**
194  * Create node data cache of constant data size for each elements.
195  *
196  * Method must be call on node data cache.
197  *
198  * Every element is represented of 4 nodes maximal. Method returns cache with size = 4*n_elements*n_components.
199  * If dimension of element is less than 3, part of data is not used. This construction of node data cache allow
200  * call gather of node data for continuous and discontinuous output meshes.
201  *
202  * @param offset_vec vector of appropriate offsets (number of nodes) of each elements
203  */
204  virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &offset_vec)=0;
205 
206  /**
207  * Inverse method to previous.
208  *
209  * Must be call on node cache with constant data size for each elements. Return data cache, that corresponds with
210  * offset vector.
211  *
212  * @param offset_vec vector of appropriate offsets (number of nodes) of each elements
213  */
214  virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &offset_vec)=0;
215 
216  /**
217  * Create final node data cache.
218  *
219  * Compute average value of each nodes.
220  *
221  * @param conn_vec Vector of connectivities, holds node indices to values of this cache
222  * @param data_size number of nodes
223  */
224  virtual std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &conn_vec, unsigned int data_size)=0;
225 
226  /// Returns start position of boundary data in cache.
227  unsigned int get_boundary_begin() const {
228  return boundary_begin_;
229  }
230 
231 protected:
232  template <class T>
233  void set_vtk_type() {
238  } else if ( std::is_same<T, int>::value ) {
240  } else {
241  ASSERT_PERMANENT(false).error("Unsupported VTK type");
242  }
243  }
244 
245  /// time step stored in cache
246  double time_;
247 
248  /// name of field stored in cache
249  std::string field_input_name_;
250  std::string field_name_;
251 
252  /**
253  * Data copied from Field.
254  */
255  std::string field_units_;
256 
257  /**
258  * Number of data values.
259  */
260  unsigned int n_values_;
261 
262  /**
263  * Number of data elements per data value.
264  */
265  unsigned int n_comp_;
266 
267  /// Type of stored data
269 
270  /// Hash of DOF handler (attribute of native VTK data)
271  std::size_t dof_handler_hash_;
272 
273  /**
274  * FiniteElement type (attribute of native VTK data)
275  */
276  std::string fe_type_;
277 
278  /**
279  * Number of DOFs per element (attribute of native VTK data)
280  */
281  unsigned int n_dofs_per_element_;
282 
283  /// Is true for DummyElementDataCache
284  bool is_dummy_;
285 
286  /// Start position of boundary data in cache.
287  unsigned int boundary_begin_;
288 };
289 
290 
291 
292 
293 
294 /**
295  * Auxiliary implementation of ElementDataCacheBase that performs output of single zero data for the fields that are
296  * off for current time frame.
297  */
299 public:
300 
301  DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
302  {
303  this->field_input_name_ = field_name_in;
304  this->n_comp_ = n_comp_in;
305  this->n_values_ = 1;
306  this->is_dummy_ = true;
307  }
308 
309  virtual ~DummyElementDataCache() override
310  {}
311 
312  void print_ascii(ostream &out_stream, unsigned int) override
313  {
314  for(unsigned int i=0; i< n_comp_;i++) out_stream << 0 << " ";
315  }
316 
317  void print_ascii_all(ostream &out_stream, unsigned int start=0) override
318  {
319  for(unsigned int i=start; i< n_comp_;i++) out_stream << 0 << " ";
320  }
321 
322  void print_binary_all(ostream &, bool, unsigned int) override
323  {
324  ASSERT_PERMANENT(false).error("Not implemented.");
325  }
326 
327  void print_yaml_subarray(ostream &, unsigned int, unsigned int , unsigned int) override
328  {}
329 
330  void get_min_max_range(double &, double &) override
331  {}
332 
333  void read_ascii_data(Tokenizer &, unsigned int, unsigned int ) override
334  {}
335 
336  void read_binary_data(std::istream &, unsigned int, unsigned int) override
337  {}
338 
339  std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
340  {
341  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
342  }
343 
344  std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &) override
345  {
346  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
347  }
348 
349  std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &) override
350  {
351  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
352  }
353 
354  std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &, unsigned int ) override
355  {
356  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
357  }
358 
359 };
360 
361 #endif /* ELEMENT_DATA_CACHE_BASE_HH_ */
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &) override
std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
void read_ascii_data(Tokenizer &, unsigned int, unsigned int) override
void get_min_max_range(double &, double &) override
virtual ~DummyElementDataCache() override
void print_ascii_all(ostream &out_stream, unsigned int start=0) override
void print_ascii(ostream &out_stream, unsigned int) override
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &, unsigned int) override
void print_binary_all(ostream &, bool, unsigned int) override
void read_binary_data(std::istream &, unsigned int, unsigned int) override
DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &) override
void print_yaml_subarray(ostream &, unsigned int, unsigned int, unsigned int) override
unsigned int get_boundary_begin() const
Returns start position of boundary data in cache.
virtual void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row)=0
virtual void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end)=0
virtual void print_ascii(ostream &out_stream, unsigned int idx)=0
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &offset_vec)=0
VTKValueType vtk_type_
Type of stored data.
virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0
virtual std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &conn_vec, unsigned int data_size)=0
unsigned int n_comp() const
virtual void print_ascii_all(ostream &out_stream, unsigned int start=0)=0
void set_field_units(std::string unit_string)
virtual void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row)=0
void set_dof_handler_hash(std::size_t hash)
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec)=0
unsigned int n_dofs_per_element() const
double time_
time step stored in cache
void set_n_values(unsigned int n_values)
virtual ~ElementDataCacheBase()
Destructor.
unsigned int boundary_begin_
Start position of boundary data in cache.
virtual void print_binary_all(ostream &out_stream, bool print_data_size=true, unsigned int start=0)=0
std::size_t dof_handler_hash() const
bool is_dummy_
Is true for DummyElementDataCache.
VTKValueType
Types of VTK value.
std::string field_input_name_
name of field stored in cache
bool is_actual(double time, std::string field_name)
Check if cache stored actual data.
std::string field_input_name()
Getter for quantity name of cache.
virtual void get_min_max_range(double &min, double &max)=0
unsigned int n_values() const
std::string field_units() const
double get_time()
Getter for time of cache.
std::size_t dof_handler_hash_
Hash of DOF handler (attribute of native VTK data)
VTKValueType vtk_type() const
Get type of stored data.
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
static constexpr bool value
Definition: json.hpp:87