Flow123d  release_3.0.0-1008-gca43bb7
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_("") {}
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_all_yaml(ostream &out_stream, unsigned int precision) = 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  * Get number of data elements per data value.
137  */
138  inline unsigned int n_comp() const {
139  return this->n_comp_;
140  }
141 
142  /// Get type of stored data
143  inline VTKValueType vtk_type() const {
144  return this->vtk_type_;
145  }
146 
147  /**
148  * Get dof_handler_hash_ value.
149  */
150  inline std::size_t dof_handler_hash() const {
151  return this->dof_handler_hash_;
152  }
153 
154  /**
155  * Set dof_handler_hash_ value.
156  */
157  inline void set_dof_handler_hash(std::size_t hash) {
158  this->dof_handler_hash_ = hash;
159  }
160 
161  /**
162  * Method for gathering parallel data to serial cache.
163  *
164  * Gather data of individual processes to serial cache that is created only on zero process.
165  * Other processes return uninitialized shared pointer.
166  *
167  * @param distr Collective distribution
168  * @param local_to_global Maps local indices to global
169  */
170  virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0;
171 
172  /**
173  * Create node data cache of constant data size for each elements.
174  *
175  * Method must be call on node data cache.
176  *
177  * Every element is represented of 4 nodes maximal. Method returns cache with size = 4*n_elements*n_components.
178  * If dimension of element is less than 3, part of data is not used. This construction of node data cache allow
179  * call gather of node data for continuous and discontinuous output meshes.
180  *
181  * @param offset_vec vector of appropriate offsets (number of nodes) of each elements
182  */
183  virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &offset_vec)=0;
184 
185  /**
186  * Inverse method to previous.
187  *
188  * Must be call on node cache with constant data size for each elements. Return data cache, that corresponds with
189  * offset vector.
190  *
191  * @param offset_vec vector of appropriate offsets (number of nodes) of each elements
192  */
193  virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &offset_vec)=0;
194 
195  /**
196  * Create final node data cache.
197  *
198  * Compute average value of each nodes.
199  *
200  * @param conn_vec Vector of connectivities, holds node indices to values of this cache
201  * @param data_size number of nodes
202  */
203  virtual std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &conn_vec, unsigned int data_size)=0;
204 
205 protected:
206  template <class T>
207  void set_vtk_type() {
212  } else if ( std::is_same<T, int>::value ) {
214  } else {
215  ASSERT(false).error("Unsupported VTK type");
216  }
217  }
218 
219  /// time step stored in cache
220  double time_;
221 
222  /// name of field stored in cache
223  std::string field_input_name_;
224  std::string field_name_;
225 
226  /**
227  * Data copied from Field.
228  */
229  std::string field_units_;
230 
231  /**
232  * Number of data values.
233  */
234  unsigned int n_values_;
235 
236  /**
237  * Number of data elements per data value.
238  */
239  unsigned int n_comp_;
240 
241  /// Type of stored data
242  VTKValueType vtk_type_;
243 
244  /// Hash of DOF handler (attribute of native VTK data)
245  std::size_t dof_handler_hash_;
246 };
247 
248 
249 #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.
unsigned int n_values() const
virtual void print_binary_all(ostream &out_stream, bool print_data_size=true)=0
virtual ~ElementDataCacheBase()
Destructor.
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
virtual std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec)=0
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
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::string field_input_name_
name of field stored in cache
virtual void get_min_max_range(double &min, double &max)=0
VTKValueType vtk_type_
Type of stored data.
virtual std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global)=0
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
virtual void print_all_yaml(ostream &out_stream, unsigned int precision)=0
std::string field_units() const