Flow123d  release_3.0.0-688-g6b683cf
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 #include <boost/exception/info.hpp> // for error_info::~error_info...
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 class Tokenizer;
30 struct MeshDataHeader; // lines 32-32
31 
32 
33 /// Return type of method that checked data stored in ElementDataCache (NaN values, limits)
34 typedef enum {
35  ok, ///< All values are not NaN and are in limits.
36  out_of_limits, ///< Some value(s) is out of limits
37  not_a_number ///< Some value(s) is set to NaN
38 } CheckResult;
39 
40 
41 template <typename T>
43 public:
44  typedef std::shared_ptr< std::vector<T> > ComponentDataPtr;
46 
47  /// Default constructor
49 
50  /**
51  * \brief Constructor of input ElementDataCache (allow read data from GMSH or VTK file)
52  *
53  * Allows set variable size of cache.
54  *
55  * @param field_name Field name thas is read
56  * @param time Actual time of data
57  * @param size_of_cache Count of columns of data cache
58  * @param row_vec_size Count of rows of data cache
59  */
60  ElementDataCache(std::string field_name, double time, unsigned int size_of_cache, unsigned int row_vec_size);
61 
62  /**
63  * \brief Constructor of output ElementDataCache (allow write data)
64  *
65  * Has fix size of cache.
66  *
67  * @param field_name Field name is written as parameter to output stream
68  * @param n_rows Given from shape of field
69  * @param n_cols Given from shape of field
70  * @param size Count of rows of data cache
71  */
72  ElementDataCache(std::string field_name, unsigned int n_rows, unsigned int n_cols, unsigned int size);
73 
74  /**
75  * \brief Destructor of ElementDataCache
76  */
77  virtual ~ElementDataCache() override;
78 
79  /// Return vector of element data for get component.
80  ComponentDataPtr get_component_data(unsigned int component_idx);
81 
82  /**
83  * Create data cache with given count of columns (\p size_of_cache) and rows (\p row_vec_size).
84  */
85  static CacheData create_data_cache(unsigned int size_of_cache, unsigned int row_vec_size);
86 
87  /// Implements @p ElementDataCacheBase::read_ascii_data.
88  void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override;
89 
90  /// Implements @p ElementDataCacheBase::read_binary_data.
91  void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override;
92 
93  /**
94  * Output data element on given index @p idx. Method for writing data
95  * to output stream.
96  *
97  * \note This method is used only by MSH file format.
98  */
99  void print_ascii(ostream &out_stream, unsigned int idx) override;
100 
101  /**
102  * \brief Print all data stored in output data ro ascii format
103  *
104  * TODO: indicate if the tensor data are output in column-first or raw-first order
105  * and possibly implement transposition. Set such property for individual file formats.
106  * Class OutputData stores always in raw-first order.
107  */
108  void print_ascii_all(ostream &out_stream) override;
109 
110  /**
111  * \brief Print all data stored in output data to appended binary format
112  */
113  void print_binary_all(ostream &out_stream, bool print_data_size = true) override;
114 
115  void print_all_yaml(ostream &out_stream, unsigned int precision) override;
116 
117  /**
118  * Store data element of given data value under given index.
119  */
120  void store_value(unsigned int idx, const T * value);
121 
122  /**
123  * Add value to given index
124  */
125  void add(unsigned int idx, const T * value);
126 
127  /**
128  * Reset values at given index
129  */
130  void zero(unsigned int idx);
131 
132  /**
133  * Normalize values at given index
134  */
135  void normalize(unsigned int idx, unsigned int divisor);
136 
137  /**
138  * Find minimal and maximal range of stored data
139  */
140  void get_min_max_range(double &min, double &max) override;
141 
142  /**
143  * Make full check of data stored in cache.
144  *
145  * Method iterates through data and
146  * - checks NaN data values, default_val replaces NaN
147  * - if default_val==NaN and some value(s) is not replaced with valid value return CheckResult::nan
148  * - if some value(s) is out of limits )lower_bound, upper_bound) return CheckResult::out_of_limits
149  * - in other cases return CheckResult::ok
150  *
151  * Method is executed only once.
152  */
153  CheckResult check_values(double default_val, double lower_bound, double upper_bound);
154 
155  /**
156  * Scale data vector of given 'component_idx' with scale 'coef'.
157  *
158  * Method is executed only once and should be called after check_values method.
159  * Method can be used e. g. for convert between units.
160  */
161  void scale_data(double coef);
162 
163  /// Access i-th element in the data vector of 0th component.
164  T& operator[](unsigned int i);
165 
166  /**
167  * Declaration of new exception info used in following exception
168  */
169  TYPEDEF_ERR_INFO(EI_FieldName, std::string);
170 
171  /**
172  * Declaration of exception
173  */
174  DECLARE_EXCEPTION(ExcOutputVariableVector, << "Can not output field " << EI_FieldName::qval
175  << " returning variable size vectors. Try convert to MultiField.\n");
176 
177 protected:
178  /// Allow to hold sign, if data in cache is checked and scale (both can be executed only once)
180  none, ///< Data is neither checked nor scaled.
181  check, ///< Data is only checked.
182  scale ///< Data is scaled.
183  };
184 
185  /// Sign, if data in cache is checked and scale.
187 
188  /**
189  * Table of element data.
190  *
191  * For every components contains vector of element data.
192  */
193  CacheData data_;
194 
195 };
196 
197 #endif /* ELEMENT_DATA_CACHE_HH_ */
void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override
Implements ElementDataCacheBase::read_binary_data.
Data is only checked.
std::shared_ptr< std::vector< T > > ComponentDataPtr
CheckScaleData
Allow to hold sign, if data in cache is checked and scale (both can be executed only once) ...
void print_ascii(ostream &out_stream, unsigned int idx) override
CheckScaleData check_scale_data_
Sign, if data in cache is checked and scale.
Some value(s) is set to NaN.
TYPEDEF_ERR_INFO(EI_FieldName, std::string)
std::vector< ComponentDataPtr > CacheData
void store_value(unsigned int idx, const T *value)
T & operator[](unsigned int i)
Access i-th element in the data vector of 0th component.
void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override
Implements ElementDataCacheBase::read_ascii_data.
Some value(s) is out of limits.
static constexpr bool value
Definition: json.hpp:87
ElementDataCache()
Default constructor.
void get_min_max_range(double &min, double &max) override
void add(unsigned int idx, const T *value)
virtual ~ElementDataCache() override
Destructor of ElementDataCache.
static CacheData create_data_cache(unsigned int size_of_cache, unsigned int row_vec_size)
void zero(unsigned int idx)
void scale_data(double coef)
DECLARE_EXCEPTION(ExcOutputVariableVector,<< "Can not output field "<< EI_FieldName::qval<< " returning variable size vectors. Try convert to MultiField.\n")
void print_all_yaml(ostream &out_stream, unsigned int precision) override
Data is neither checked nor scaled.
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits) ...
CheckResult check_values(double default_val, double lower_bound, double upper_bound)
void normalize(unsigned int idx, unsigned int divisor)
void print_binary_all(ostream &out_stream, bool print_data_size=true) override
Print all data stored in output data to appended binary format.
ComponentDataPtr get_component_data(unsigned int component_idx)
Return vector of element data for get component.
void print_ascii_all(ostream &out_stream) override
Print all data stored in output data ro ascii format.
All values are not NaN and are in limits.