Flow123d  release_3.0.0-968-gc87a28e79
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 "mesh/long_idx.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) = 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) = 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  /// Get type of stored data
150  inline VTKValueType vtk_type() const {
151  return this->vtk_type_;
152  }
153 
154  /**
155  * Get dof_handler_hash_ value.
156  */
157  inline std::size_t dof_handler_hash() const {
158  return this->dof_handler_hash_;
159  }
160 
161  /**
162  * Set dof_handler_hash_ value.
163  */
164  inline void set_dof_handler_hash(std::size_t hash) {
165  this->dof_handler_hash_ = hash;
166  }
167 
168  /**
169  * Method for gathering parallel data to serial cache.
170  *
171  * Gather data of individual processes to serial cache that is created only on zero process.
172  * Other processes return uninitialized shared pointer.
173  *
174  * @param distr Collective distribution
175  * @param local_to_global Maps local indices to global
176  */
177  virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0;
178 
179  /**
180  * Create node data cache of constant data size for each elements.
181  *
182  * Method must be call on node data cache.
183  *
184  * Every element is represented of 4 nodes maximal. Method returns cache with size = 4*n_elements*n_components.
185  * If dimension of element is less than 3, part of data is not used. This construction of node data cache allow
186  * call gather of node data for continuous and discontinuous output meshes.
187  *
188  * @param offset_vec vector of appropriate offsets (number of nodes) of each elements
189  */
190  virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &offset_vec)=0;
191 
192  /**
193  * Inverse method to previous.
194  *
195  * Must be call on node cache with constant data size for each elements. Return data cache, that corresponds with
196  * offset vector.
197  *
198  * @param offset_vec vector of appropriate offsets (number of nodes) of each elements
199  */
200  virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &offset_vec)=0;
201 
202  /**
203  * Create final node data cache.
204  *
205  * Compute average value of each nodes.
206  *
207  * @param conn_vec Vector of connectivities, holds node indices to values of this cache
208  * @param data_size number of nodes
209  */
210  virtual std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &conn_vec, unsigned int data_size)=0;
211 
212 protected:
213  template <class T>
214  void set_vtk_type() {
219  } else if ( std::is_same<T, int>::value ) {
221  } else {
222  ASSERT(false).error("Unsupported VTK type");
223  }
224  }
225 
226  /// time step stored in cache
227  double time_;
228 
229  /// name of field stored in cache
230  std::string field_input_name_;
231  std::string field_name_;
232 
233  /**
234  * Data copied from Field.
235  */
236  std::string field_units_;
237 
238  /**
239  * Number of data values.
240  */
241  unsigned int n_values_;
242 
243  /**
244  * Number of data elements per data value.
245  */
246  unsigned int n_comp_;
247 
248  /// Type of stored data
250 
251  /// Hash of DOF handler (attribute of native VTK data)
252  std::size_t dof_handler_hash_;
253 
254  /// Is true for DummyElementDataCache
255  bool is_dummy_;
256 };
257 
258 
259 
260 
261 
262 /**
263  * Auxiliary implementation of ElementDataCacheBase that performs output of single zero data for the fields that are
264  * off for current time frame.
265  */
267 public:
268 
269  DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
270  {
271  this->field_input_name_ = field_name_in;
272  this->n_comp_ = n_comp_in;
273  this->n_values_ = 1;
274  this->is_dummy_ = true;
275  }
276 
277  virtual ~DummyElementDataCache() override
278  {}
279 
280  void print_ascii(ostream &out_stream, unsigned int) override
281  {
282  for(unsigned int i=0; i< n_comp_;i++) out_stream << 0 << " ";
283  }
284 
285  void print_ascii_all(ostream &out_stream) override
286  {
287  for(unsigned int i=0; i< n_comp_;i++) out_stream << 0 << " ";
288  }
289 
290  void print_binary_all(ostream &, bool) override
291  {
292  ASSERT(false).error("Not implemented.");
293  }
294 
295  void print_yaml_subarray(ostream &, unsigned int, unsigned int , unsigned int) override
296  {}
297 
298  void get_min_max_range(double &, double &) override
299  {}
300 
301  void read_ascii_data(Tokenizer &, unsigned int, unsigned int ) override
302  {}
303 
304  void read_binary_data(std::istream &, unsigned int, unsigned int) override
305  {}
306 
307  std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
308  {
309  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
310  }
311 
312  std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &) override
313  {
314  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
315  }
316 
317  std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &) override
318  {
319  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
320  }
321 
322  std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &, unsigned int ) override
323  {
324  return std::make_shared<DummyElementDataCache>(this->field_input_name_, this->n_comp_);
325  }
326 
327 };
328 
329 #endif /* ELEMENT_DATA_CACHE_BASE_HH_ */
ElementDataCacheBase::print_ascii_all
virtual void print_ascii_all(ostream &out_stream)=0
DummyElementDataCache::gather
std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
Definition: element_data_cache_base.hh:307
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:277
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:150
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::time_
double time_
time step stored in cache
Definition: element_data_cache_base.hh:227
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:346
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
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:246
ElementDataCacheBase::print_binary_all
virtual void print_binary_all(ostream &out_stream, bool print_data_size=true)=0
DummyElementDataCache::get_min_max_range
void get_min_max_range(double &, double &) override
Definition: element_data_cache_base.hh:298
DummyElementDataCache::compute_node_data
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &, unsigned int) override
Definition: element_data_cache_base.hh:322
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:280
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:241
ElementDataCacheBase::set_dof_handler_hash
void set_dof_handler_hash(std::size_t hash)
Definition: element_data_cache_base.hh:164
DummyElementDataCache::read_binary_data
void read_binary_data(std::istream &, unsigned int, unsigned int) override
Definition: element_data_cache_base.hh:304
ElementDataCacheBase::element_node_cache_optimize_size
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec)=0
Distribution
Definition: distribution.hh:50
ElementDataCacheBase::get_time
double get_time()
Getter for time of cache.
Definition: element_data_cache_base.hh:59
DummyElementDataCache::read_ascii_data
void read_ascii_data(Tokenizer &, unsigned int, unsigned int) override
Definition: element_data_cache_base.hh:301
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:249
ElementDataCacheBase::field_units_
std::string field_units_
Definition: element_data_cache_base.hh:236
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:312
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:266
ElementDataCacheBase::is_dummy_
bool is_dummy_
Is true for DummyElementDataCache.
Definition: element_data_cache_base.hh:255
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:317
ElementDataCacheBase::VTK_UINT8
@ VTK_UINT8
Definition: element_data_cache_base.hh:46
ElementDataCacheBase::N_VECTOR
@ N_VECTOR
Definition: element_data_cache_base.hh:41
DummyElementDataCache::print_ascii_all
void print_ascii_all(ostream &out_stream) override
Definition: element_data_cache_base.hh:285
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:295
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:252
ElementDataCacheBase::field_input_name_
std::string field_input_name_
name of field stored in cache
Definition: element_data_cache_base.hh:230
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
DummyElementDataCache::print_binary_all
void print_binary_all(ostream &, bool) override
Definition: element_data_cache_base.hh:290
long_idx.hh
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:214
DummyElementDataCache::DummyElementDataCache
DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
Definition: element_data_cache_base.hh:269
ElementDataCacheBase::dof_handler_hash
std::size_t dof_handler_hash() const
Definition: element_data_cache_base.hh:157
ElementDataCacheBase::field_name_
std::string field_name_
Definition: element_data_cache_base.hh:231
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::VTK_FLOAT64
@ VTK_FLOAT64
Definition: element_data_cache_base.hh:47
ElementDataCacheBase::VTK_INT8
@ VTK_INT8
Definition: element_data_cache_base.hh:46