Flow123d  JS_before_hm-1804-gf2ad740aa
element_data_cache.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.hh
15  * @brief
16  */
17 
18 #ifndef ELEMENT_DATA_CACHE_HH_
19 #define ELEMENT_DATA_CACHE_HH_
20 
21 
22 #include <memory> // for shared_ptr
23 #include <sstream> // for basic_ostream::write
24 #include <string> // for string, operator<<
25 #include <vector> // for vector
26 #include <armadillo>
27 #include "io/element_data_cache_base.hh" // for ElementDataCacheBase
28 #include "system/exceptions.hh" // for ExcStream, operator<<
29 #include "system/index_types.hh" // for LongIdx
30 class Tokenizer;
31 class Distribution;
32 struct MeshDataHeader;
33 
34 
35 /// Return type of method that checked data stored in ElementDataCache (NaN values, limits)
36 typedef enum {
37  ok, ///< All values are not NaN and are in limits.
38  out_of_limits, ///< Some value(s) is out of limits
39  not_a_number ///< Some value(s) is set to NaN
40 } CheckResult;
41 
42 
43 template <typename T>
45 /**
46  * Container of the field data on elements used as a common data storage for
47  * output of various fields using various output formats and to cache data of several fields when reading the input file.
48  * This container also perform serialization for the serial output.
49  * Read of values from tokenizer and output of values to stream is implemented as it depends on the value type T.
50  */
51 public:
52  typedef std::shared_ptr< std::vector<T> > ComponentDataPtr;
54 
55  /// Default constructor
57 
58  /**
59  * \brief Constructor of input ElementDataCache (allow read data from GMSH or VTK file)
60  *
61  * Allows set variable size of cache.
62  *
63  * @param field_name Field name thas is read
64  * @param time Actual time of data
65  * @param size_of_cache Count of columns of data cache
66  * @param row_vec_size Count of rows of data cache
67  */
68  ElementDataCache(std::string field_name, double time, unsigned int size_of_cache, unsigned int row_vec_size);
69 
70  /**
71  * \brief Constructor of output ElementDataCache (allow write data)
72  *
73  * Has fix size of cache.
74  *
75  * @param field_name Field name is written as parameter to output stream
76  * @param n_comp Given from shape of field
77  * @param size Count of rows of data cache
78  * @param fe_type FiniteElement type (used only for native data)
79  * @param n_dofs_per_element Number of DOFs per element (used only for native data)
80  */
81  ElementDataCache(std::string field_name, unsigned int n_comp, unsigned int size, std::string fe_type = "", unsigned int n_dofs_per_element = 1);
82 
83  /**
84  * \brief Destructor of ElementDataCache
85  */
86  virtual ~ElementDataCache() override;
87 
88  /// Return vector of element data for get component.
89  ComponentDataPtr get_component_data(unsigned int component_idx);
90 
91  /**
92  * Create data cache with given count of columns (\p size_of_cache) and rows (\p row_vec_size).
93  */
94  static CacheData create_data_cache(unsigned int size_of_cache, unsigned int row_vec_size);
95 
96  /// Implements @p ElementDataCacheBase::read_ascii_data.
97  void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override;
98 
99  /// Implements @p ElementDataCacheBase::read_binary_data.
100  void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override;
101 
102  /**
103  * Output data element on given index @p idx. Method for writing data
104  * to output stream.
105  *
106  * \note This method is used only by MSH file format.
107  */
108  void print_ascii(ostream &out_stream, unsigned int idx) override;
109 
110  /**
111  * \brief Print all data stored in output data ro ascii format
112  *
113  * TODO: indicate if the tensor data are output in column-first or raw-first order
114  * and possibly implement transposition. Set such property for individual file formats.
115  * Class OutputData stores always in raw-first order.
116  */
117  void print_ascii_all(ostream &out_stream, unsigned int start=0) override;
118 
119  /**
120  * \brief Print all data stored in output data to appended binary format
121  */
122  void print_binary_all(ostream &out_stream, bool print_data_size = true, unsigned int start = 0) override;
123 
124  void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) override;
125 
126  /**
127  * Store data element of given data value under given index.
128  */
129  void store_value(unsigned int idx, const T * value);
130 
131  /**
132  * Add value to given index
133  */
134  void add(unsigned int idx, const T * value);
135 
136  /**
137  * Reset values at given index
138  */
139  void zero(unsigned int idx);
140 
141  /**
142  * Normalize values at given index
143  */
144  void normalize(unsigned int idx, unsigned int divisor);
145 
146  /**
147  * Find minimal and maximal range of stored data
148  */
149  void get_min_max_range(double &min, double &max) override;
150 
151  /**
152  * Make full check of data stored in cache.
153  *
154  * Method iterates through data and
155  * - checks NaN data values, default_val replaces NaN
156  * - if default_val==NaN and some value(s) is not replaced with valid value return CheckResult::nan
157  * - if some value(s) is out of limits )lower_bound, upper_bound) return CheckResult::out_of_limits
158  * - in other cases return CheckResult::ok
159  *
160  * Method is executed only once.
161  */
162  CheckResult check_values(double default_val, double lower_bound, double upper_bound);
163 
164  /**
165  * Scale data vector of given 'component_idx' with scale 'coef'.
166  *
167  * Method is executed only once and should be called after check_values method.
168  * Method can be used e. g. for convert between units.
169  */
170  void scale_data(double coef);
171 
172  /// Implements ElementDataCacheBase::gather.
173  std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global) override;
174 
175  /// Implements ElementDataCacheBase::element_node_cache_fixed_size.
176  std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &offset_vec) override;
177 
178  /// Implements ElementDataCacheBase::element_node_cache_optimize_size.
179  std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &offset_vec) override;
180 
181  /// Implements ElementDataCacheBase::compute_node_data.
182  std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &conn_vec, unsigned int data_size) override;
183 
184  /// Access i-th element in the data vector of 0th component.
185  T& operator[](unsigned int i);
186 
187 protected:
188  /// Allow to hold sign, if data in cache is checked and scale (both can be executed only once)
190  none, ///< Data is neither checked nor scaled.
191  check, ///< Data is only checked.
192  scale ///< Data is scaled.
193  };
194 
195 
196  /// Return MPI data type corresponding with template parameter of cache. Needs template specialization.
198 
199  /// Sign, if data in cache is checked and scale.
201 
202  /**
203  * Table of element data.
204  *
205  * For every components contains vector of element data.
206  */
208 
209 };
210 
211 
212 #endif /* ELEMENT_DATA_CACHE_HH_ */
ElementDataCache::ElementDataCache
ElementDataCache()
Default constructor.
Definition: element_data_cache.cc:32
ElementDataCache::read_ascii_data
void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override
Implements ElementDataCacheBase::read_ascii_data.
Definition: element_data_cache.cc:91
ElementDataCache
Definition: element_data_cache.hh:44
ElementDataCache::get_component_data
ComponentDataPtr get_component_data(unsigned int component_idx)
Return vector of element data for get component.
Definition: element_data_cache.cc:71
element_data_cache_base.hh
ElementDataCache::print_yaml_subarray
void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) override
Definition: element_data_cache.cc:170
ok
@ ok
All values are not NaN and are in limits.
Definition: element_data_cache.hh:37
ElementDataCache::gather
std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global) override
Implements ElementDataCacheBase::gather.
Definition: element_data_cache.cc:309
value
static constexpr bool value
Definition: json.hpp:87
ElementDataCacheBase::n_comp
unsigned int n_comp() const
Definition: element_data_cache_base.hh:145
std::vector< ComponentDataPtr >
ElementDataCache::scale
@ scale
Data is scaled.
Definition: element_data_cache.hh:192
ElementDataCache::scale_data
void scale_data(double coef)
Definition: element_data_cache.cc:293
ElementDataCache::check
@ check
Data is only checked.
Definition: element_data_cache.hh:191
ElementDataCacheBase::n_dofs_per_element
unsigned int n_dofs_per_element() const
Definition: element_data_cache_base.hh:152
index_types.hh
exceptions.hh
ElementDataCache::CheckScaleData
CheckScaleData
Allow to hold sign, if data in cache is checked and scale (both can be executed only once)
Definition: element_data_cache.hh:189
Distribution
Definition: distribution.hh:50
ElementDataCache::print_ascii
void print_ascii(ostream &out_stream, unsigned int idx) override
Definition: element_data_cache.cc:125
ElementDataCache::compute_node_data
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &conn_vec, unsigned int data_size) override
Implements ElementDataCacheBase::compute_node_data.
Definition: element_data_cache.cc:410
ElementDataCache::check_scale_data_
CheckScaleData check_scale_data_
Sign, if data in cache is checked and scale.
Definition: element_data_cache.hh:200
ElementDataCache::element_node_cache_fixed_size
std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &offset_vec) override
Implements ElementDataCacheBase::element_node_cache_fixed_size.
Definition: element_data_cache.cc:362
CheckResult
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits)
Definition: element_data_cache.hh:36
ElementDataCache::element_node_cache_optimize_size
std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec) override
Implements ElementDataCacheBase::element_node_cache_optimize_size.
Definition: element_data_cache.cc:387
ElementDataCache::mpi_data_type
MPI_Datatype mpi_data_type()
Return MPI data type corresponding with template parameter of cache. Needs template specialization.
ElementDataCacheBase
Definition: element_data_cache_base.hh:33
ElementDataCache::print_binary_all
void print_binary_all(ostream &out_stream, bool print_data_size=true, unsigned int start=0) override
Print all data stored in output data to appended binary format.
Definition: element_data_cache.cc:153
not_a_number
@ not_a_number
Some value(s) is set to NaN.
Definition: element_data_cache.hh:39
ElementDataCache::read_binary_data
void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override
Implements ElementDataCacheBase::read_binary_data.
Definition: element_data_cache.cc:106
MPI_Datatype
#define MPI_Datatype
Definition: mpi.h:154
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
ElementDataCache::add
void add(unsigned int idx, const T *value)
Definition: element_data_cache.cc:235
ElementDataCache::ComponentDataPtr
std::shared_ptr< std::vector< T > > ComponentDataPtr
Definition: element_data_cache.hh:52
ElementDataCache::data_
CacheData data_
Definition: element_data_cache.hh:207
ElementDataCache::~ElementDataCache
virtual ~ElementDataCache() override
Destructor of ElementDataCache.
Definition: element_data_cache.cc:67
ElementDataCache::none
@ none
Data is neither checked nor scaled.
Definition: element_data_cache.hh:190
ElementDataCache::get_min_max_range
void get_min_max_range(double &min, double &max) override
Definition: element_data_cache.cc:204
out_of_limits
@ out_of_limits
Some value(s) is out of limits.
Definition: element_data_cache.hh:38
ElementDataCache::CacheData
std::vector< ComponentDataPtr > CacheData
Definition: element_data_cache.hh:53
ElementDataCache::create_data_cache
static CacheData create_data_cache(unsigned int size_of_cache, unsigned int row_vec_size)
Definition: element_data_cache.cc:78
ElementDataCache::zero
void zero(unsigned int idx)
Definition: element_data_cache.cc:248
ElementDataCache::check_values
CheckResult check_values(double default_val, double lower_bound, double upper_bound)
Definition: element_data_cache.cc:271
ElementDataCache::normalize
void normalize(unsigned int idx, unsigned int divisor)
Definition: element_data_cache.cc:261
ElementDataCache::print_ascii_all
void print_ascii_all(ostream &out_stream, unsigned int start=0) override
Print all data stored in output data ro ascii format.
Definition: element_data_cache.cc:141
ElementDataCache::store_value
void store_value(unsigned int idx, const T *value)
Definition: element_data_cache.cc:222
ElementDataCacheBase::fe_type
std::string fe_type() const
Definition: element_data_cache_base.hh:178
ElementDataCache::operator[]
T & operator[](unsigned int i)
Access i-th element in the data vector of 0th component.
Definition: element_data_cache.cc:454