Flow123d  release_3.0.0-955-g4db4b48
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
249  VTKValueType vtk_type_;
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_ */
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
double time_
time step stored in cache
virtual std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &conn_vec, unsigned int data_size)=0
void set_dof_handler_hash(std::size_t hash)
virtual void print_ascii(ostream &out_stream, unsigned int idx)=0
std::string field_input_name()
Getter for quantity name of cache.
void print_ascii_all(ostream &out_stream) override
unsigned int n_values() const
virtual void print_binary_all(ostream &out_stream, bool print_data_size=true)=0
virtual ~ElementDataCacheBase()
Destructor.
DummyElementDataCache(std::string field_name_in, unsigned int n_comp_in)
void set_n_values(unsigned int n_values)
double get_time()
Getter for time of cache.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
std::size_t dof_handler_hash() const
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &, unsigned int) override
bool is_dummy_
Is true for DummyElementDataCache.
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec)=0
void print_yaml_subarray(ostream &, unsigned int, unsigned int, unsigned int) override
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &offset_vec)=0
static constexpr bool value
Definition: json.hpp:87
std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &) override
void set_field_units(std::string unit_string)
bool is_actual(double time, std::string field_name)
Check if cache stored actual data.
VTKValueType
Types of VTK value.
virtual void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row)=0
std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &) override
void get_min_max_range(double &, double &) override
void print_ascii(ostream &out_stream, unsigned int) override
std::string field_input_name_
name of field stored in cache
virtual void get_min_max_range(double &min, double &max)=0
virtual void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end)=0
VTKValueType vtk_type_
Type of stored data.
virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0
std::shared_ptr< ElementDataCacheBase > gather(Distribution *, LongIdx *) override
void read_binary_data(std::istream &, unsigned int, unsigned int) override
virtual ~DummyElementDataCache() override
void print_binary_all(ostream &, bool) override
std::size_t dof_handler_hash_
Hash of DOF handler (attribute of native VTK data)
unsigned int n_comp() const
virtual void print_ascii_all(ostream &out_stream)=0
VTKValueType vtk_type() const
Get type of stored data.
virtual void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row)=0
void read_ascii_data(Tokenizer &, unsigned int, unsigned int) override
std::string field_units() const