Flow123d  JS_before_hm-1804-gf2ad740aa
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,
43  };
44 
45  /// Types of VTK value
48  } VTKValueType;
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 protected:
227  template <class T>
228  void set_vtk_type() {
233  } else if ( std::is_same<T, int>::value ) {
235  } else {
236  ASSERT(false).error("Unsupported VTK type");
237  }
238  }
239 
240  /// time step stored in cache
241  double time_;
242 
243  /// name of field stored in cache
244  std::string field_input_name_;
245  std::string field_name_;
246 
247  /**
248  * Data copied from Field.
249  */
250  std::string field_units_;
251 
252  /**
253  * Number of data values.
254  */
255  unsigned int n_values_;
256 
257  /**
258  * Number of data elements per data value.
259  */
260  unsigned int n_comp_;
261 
262  /// Type of stored data
264 
265  /// Hash of DOF handler (attribute of native VTK data)
266  std::size_t dof_handler_hash_;
267 
268  /**
269  * FiniteElement type (attribute of native VTK data)
270  */
271  std::string fe_type_;
272 
273  /**
274  * Number of DOFs per element (attribute of native VTK data)
275  */
276  unsigned int n_dofs_per_element_;
277 
278  /// Is true for DummyElementDataCache
279  bool is_dummy_;
280 };
281 
282 
283 
284 
285 
286 /**
287  * Auxiliary implementation of ElementDataCacheBase that performs output of single zero data for the fields that are
288  * off for current time frame.
289  */
291 public:
292 
293  DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
294  {
295  this->field_input_name_ = field_name_in;
296  this->n_comp_ = n_comp_in;
297  this->n_values_ = 1;
298  this->is_dummy_ = true;
299  }
300 
301  virtual ~DummyElementDataCache() override
302  {}
303 
304  void print_ascii(ostream &out_stream, unsigned int) override
305  {
306  for(unsigned int i=0; i< n_comp_;i++) out_stream << 0 << " ";
307  }
308 
309  void print_ascii_all(ostream &out_stream, unsigned int start=0) override
310  {
311  for(unsigned int i=start; i< n_comp_;i++) out_stream << 0 << " ";
312  }
313 
314  void print_binary_all(ostream &, bool, unsigned int) override
315  {
316  ASSERT(false).error("Not implemented.");
317  }
318 
319  void print_yaml_subarray(ostream &, unsigned int, unsigned int , unsigned int) override
320  {}
321 
322  void get_min_max_range(double &, double &) override
323  {}
324 
325  void read_ascii_data(Tokenizer &, unsigned int, unsigned int ) override
326  {}
327 
328  void read_binary_data(std::istream &, unsigned int, unsigned int) override
329  {}
330 
331  std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
332  {
333  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
334  }
335 
336  std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &) override
337  {
338  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
339  }
340 
341  std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &) override
342  {
343  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
344  }
345 
346  std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &, unsigned int ) override
347  {
348  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
349  }
350 
351 };
352 
353 #endif /* ELEMENT_DATA_CACHE_BASE_HH_ */
DummyElementDataCache::gather
std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
Definition: element_data_cache_base.hh:331
ElementDataCacheBase::compute_node_data
virtual std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &conn_vec, unsigned int data_size)=0
DummyElementDataCache::~DummyElementDataCache
virtual ~DummyElementDataCache() override
Definition: element_data_cache_base.hh:301
ElementDataCacheBase::field_units
std::string field_units() const
Definition: element_data_cache_base.hh:124
ElementDataCacheBase::vtk_type
VTKValueType vtk_type() const
Get type of stored data.
Definition: element_data_cache_base.hh:157
ElementDataCacheBase::N_SCALAR
@ N_SCALAR
Definition: element_data_cache_base.hh:40
ElementDataCacheBase::VTK_FLOAT32
@ VTK_FLOAT32
Definition: element_data_cache_base.hh:47
ElementDataCacheBase::print_ascii_all
virtual void print_ascii_all(ostream &out_stream, unsigned int start=0)=0
ElementDataCacheBase::print_binary_all
virtual void print_binary_all(ostream &out_stream, bool print_data_size=true, unsigned int start=0)=0
ElementDataCacheBase::time_
double time_
time step stored in cache
Definition: element_data_cache_base.hh:241
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
ElementDataCacheBase::n_dofs_per_element_
unsigned int n_dofs_per_element_
Definition: element_data_cache_base.hh:276
ElementDataCacheBase::VTK_INT32
@ VTK_INT32
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::n_comp_
unsigned int n_comp_
Definition: element_data_cache_base.hh:260
DummyElementDataCache::get_min_max_range
void get_min_max_range(double &, double &) override
Definition: element_data_cache_base.hh:322
DummyElementDataCache::compute_node_data
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &, unsigned int) override
Definition: element_data_cache_base.hh:346
ElementDataCacheBase::VTK_INT16
@ VTK_INT16
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::is_dummy
bool is_dummy() const
Definition: element_data_cache_base.hh:138
ElementDataCacheBase::VTK_UINT16
@ VTK_UINT16
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::field_input_name
std::string field_input_name()
Getter for quantity name of cache.
Definition: element_data_cache_base.hh:63
value
static constexpr bool value
Definition: json.hpp:87
ElementDataCacheBase::n_comp
unsigned int n_comp() const
Definition: element_data_cache_base.hh:145
ElementDataCacheBase::set_field_units
void set_field_units(std::string unit_string)
Definition: element_data_cache_base.hh:110
ElementDataCacheBase::element_node_cache_fixed_size
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &offset_vec)=0
std::vector< unsigned int >
DummyElementDataCache::print_ascii
void print_ascii(ostream &out_stream, unsigned int) override
Definition: element_data_cache_base.hh:304
system.hh
ElementDataCacheBase::NumCompValueType
NumCompValueType
Definition: element_data_cache_base.hh:39
ElementDataCacheBase::set_n_values
void set_n_values(unsigned int n_values)
Definition: element_data_cache_base.hh:117
ElementDataCacheBase::VTK_UINT32
@ VTK_UINT32
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::n_values_
unsigned int n_values_
Definition: element_data_cache_base.hh:255
ElementDataCacheBase::set_dof_handler_hash
void set_dof_handler_hash(std::size_t hash)
Definition: element_data_cache_base.hh:171
ElementDataCacheBase::n_dofs_per_element
unsigned int n_dofs_per_element() const
Definition: element_data_cache_base.hh:152
index_types.hh
DummyElementDataCache::read_binary_data
void read_binary_data(std::istream &, unsigned int, unsigned int) override
Definition: element_data_cache_base.hh:328
ElementDataCacheBase::element_node_cache_optimize_size
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec)=0
ElementDataCacheBase::fe_type_
std::string fe_type_
Definition: element_data_cache_base.hh:271
Distribution
Definition: distribution.hh:50
ElementDataCacheBase::get_time
double get_time()
Getter for time of cache.
Definition: element_data_cache_base.hh:59
DummyElementDataCache::print_ascii_all
void print_ascii_all(ostream &out_stream, unsigned int start=0) override
Definition: element_data_cache_base.hh:309
DummyElementDataCache::read_ascii_data
void read_ascii_data(Tokenizer &, unsigned int, unsigned int) override
Definition: element_data_cache_base.hh:325
ElementDataCacheBase::print_yaml_subarray
virtual void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end)=0
ElementDataCacheBase::vtk_type_
VTKValueType vtk_type_
Type of stored data.
Definition: element_data_cache_base.hh:263
ElementDataCacheBase::field_units_
std::string field_units_
Definition: element_data_cache_base.hh:250
DummyElementDataCache::element_node_cache_fixed_size
std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &) override
Definition: element_data_cache_base.hh:336
ElementDataCacheBase::gather
virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0
ElementDataCacheBase
Definition: element_data_cache_base.hh:33
DummyElementDataCache
Definition: element_data_cache_base.hh:290
ElementDataCacheBase::is_dummy_
bool is_dummy_
Is true for DummyElementDataCache.
Definition: element_data_cache_base.hh:279
DummyElementDataCache::element_node_cache_optimize_size
std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &) override
Definition: element_data_cache_base.hh:341
ElementDataCacheBase::VTK_UINT8
@ VTK_UINT8
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::N_VECTOR
@ N_VECTOR
Definition: element_data_cache_base.hh:41
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
ElementDataCacheBase::read_ascii_data
virtual void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row)=0
DummyElementDataCache::print_yaml_subarray
void print_yaml_subarray(ostream &, unsigned int, unsigned int, unsigned int) override
Definition: element_data_cache_base.hh:319
ElementDataCacheBase::dof_handler_hash_
std::size_t dof_handler_hash_
Hash of DOF handler (attribute of native VTK data)
Definition: element_data_cache_base.hh:266
ElementDataCacheBase::field_input_name_
std::string field_input_name_
name of field stored in cache
Definition: element_data_cache_base.hh:244
DummyElementDataCache::print_binary_all
void print_binary_all(ostream &, bool, unsigned int) override
Definition: element_data_cache_base.hh:314
std
Definition: doxy_dummy_defs.hh:5
ElementDataCacheBase::read_binary_data
virtual void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row)=0
ElementDataCacheBase::is_actual
bool is_actual(double time, std::string field_name)
Check if cache stored actual data.
Definition: element_data_cache_base.hh:67
ElementDataCacheBase::VTKValueType
VTKValueType
Types of VTK value.
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::print_ascii
virtual void print_ascii(ostream &out_stream, unsigned int idx)=0
ElementDataCacheBase::set_vtk_type
void set_vtk_type()
Definition: element_data_cache_base.hh:228
DummyElementDataCache::DummyElementDataCache
DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
Definition: element_data_cache_base.hh:293
ElementDataCacheBase::dof_handler_hash
std::size_t dof_handler_hash() const
Definition: element_data_cache_base.hh:164
ElementDataCacheBase::field_name_
std::string field_name_
Definition: element_data_cache_base.hh:245
ElementDataCacheBase::ElementDataCacheBase
ElementDataCacheBase()
Constructor.
Definition: element_data_cache_base.hh:51
ElementDataCacheBase::n_values
unsigned int n_values() const
Definition: element_data_cache_base.hh:131
ElementDataCacheBase::get_min_max_range
virtual void get_min_max_range(double &min, double &max)=0
ElementDataCacheBase::N_TENSOR
@ N_TENSOR
Definition: element_data_cache_base.hh:42
ElementDataCacheBase::~ElementDataCacheBase
virtual ~ElementDataCacheBase()
Destructor.
Definition: element_data_cache_base.hh:56
ElementDataCacheBase::fe_type
std::string fe_type() const
Definition: element_data_cache_base.hh:178
ElementDataCacheBase::VTK_FLOAT64
@ VTK_FLOAT64
Definition: element_data_cache_base.hh:47
ElementDataCacheBase::VTK_INT8
@ VTK_INT8
Definition: element_data_cache_base.hh:46