Flow123d  DF_mechanic_bench-4968b1b
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
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> > CacheData;
53 
54  /// Default constructor
56 
57  /**
58  * \brief Constructor of input ElementDataCache (allow read data from GMSH or VTK file)
59  *
60  * Allows set variable size of cache.
61  *
62  * @param field_name Field name thas is read
63  * @param time Actual time of data
64  * @param row_vec_size Count of rows of data cache
65  */
66  ElementDataCache(std::string field_name, double time, unsigned int row_vec_size, unsigned int boundary_begin);
67 
68  /**
69  * \brief Constructor of output ElementDataCache (allow write data)
70  *
71  * Has fix size of cache.
72  *
73  * @param field_name Field name is written as parameter to output stream
74  * @param n_comp Given from shape of field
75  * @param size Count of rows of data cache
76  * @param fe_type FiniteElement type (used only for native data)
77  * @param n_dofs_per_element Number of DOFs per element (used only for native data)
78  */
79  ElementDataCache(std::string field_name, unsigned int n_comp, unsigned int size, std::string fe_type = "", unsigned int n_dofs_per_element = 1);
80 
81  /**
82  * \brief Destructor of ElementDataCache
83  */
84  virtual ~ElementDataCache() override;
85 
86  /// Return underlying vector of element data.
88 
89  /**
90  * Create data cache with given count rows (\p row_vec_size).
91  */
92  static CacheData create_data_cache(unsigned int row_vec_size);
93 
94  /// Implements @p ElementDataCacheBase::read_ascii_data.
95  void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override;
96 
97  /// Implements @p ElementDataCacheBase::read_binary_data.
98  void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override;
99 
100  /**
101  * Output data element on given index @p idx. Method for writing data
102  * to output stream.
103  *
104  * \note This method is used only by MSH file format.
105  */
106  void print_ascii(ostream &out_stream, unsigned int idx) override;
107 
108  /**
109  * \brief Print all data stored in output data ro ascii format
110  *
111  * TODO: indicate if the tensor data are output in column-first or raw-first order
112  * and possibly implement transposition. Set such property for individual file formats.
113  * Class OutputData stores always in raw-first order.
114  */
115  void print_ascii_all(ostream &out_stream, unsigned int start=0) override;
116 
117  /**
118  * \brief Print all data stored in output data to appended binary format
119  */
120  void print_binary_all(ostream &out_stream, bool print_data_size = true, unsigned int start = 0) override;
121 
122  void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) override;
123 
124  /**
125  * Store data element of given data value under given index.
126  */
127  void store_value(unsigned int idx, const T * value);
128 
129  /**
130  * Add value to given index
131  */
132  void add(unsigned int idx, const T * value);
133 
134  /**
135  * Reset values at given index
136  */
137  void zero(unsigned int idx);
138 
139  /**
140  * Normalize values at given index
141  */
142  void normalize(unsigned int idx, unsigned int divisor);
143 
144  /**
145  * Find minimal and maximal range of stored data
146  */
147  void get_min_max_range(double &min, double &max) override;
148 
149 
150  /// Implements ElementDataCacheBase::gather.
151  std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global) override;
152 
153  /// Implements ElementDataCacheBase::element_node_cache_fixed_size.
154  std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &offset_vec) override;
155 
156  /// Implements ElementDataCacheBase::element_node_cache_optimize_size.
157  std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &offset_vec) override;
158 
159  /// Implements ElementDataCacheBase::compute_node_data.
160  std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &conn_vec, unsigned int data_size) override;
161 
162  /// Access i-th element in the data vector of 0th component.
163  T& operator[](unsigned int i);
164 
165 protected:
166  /// Allow to hold sign, if data in cache is checked and scale (both can be executed only once)
168  none, ///< Data is neither checked nor scaled.
169  check, ///< Data is only checked.
170  scale ///< Data is scaled.
171  };
172 
173 
174  /// Return MPI data type corresponding with template parameter of cache. Needs template specialization.
176 
177  /// Sign, if data in cache is checked and scale.
179 
180  /**
181  * Table of element data.
182  *
183  * For every components contains vector of element data.
184  */
186 
187 };
188 
189 
190 #endif /* ELEMENT_DATA_CACHE_HH_ */
unsigned int n_comp() const
unsigned int n_dofs_per_element() const
void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override
Implements ElementDataCacheBase::read_binary_data.
std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global) override
Implements ElementDataCacheBase::gather.
void print_ascii_all(ostream &out_stream, unsigned int start=0) override
Print all data stored in output data ro ascii format.
void get_min_max_range(double &min, double &max) override
MPI_Datatype mpi_data_type()
Return MPI data type corresponding with template parameter of cache. Needs template specialization.
ElementDataCache()
Default constructor.
void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) override
void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override
Implements ElementDataCacheBase::read_ascii_data.
CheckScaleData
Allow to hold sign, if data in cache is checked and scale (both can be executed only once)
@ scale
Data is scaled.
@ check
Data is only checked.
@ none
Data is neither checked nor scaled.
void store_value(unsigned int idx, const T *value)
static CacheData create_data_cache(unsigned int row_vec_size)
std::shared_ptr< std::vector< T > > CacheData
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.
CheckScaleData check_scale_data_
Sign, if data in cache is checked and scale.
void zero(unsigned int idx)
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &conn_vec, unsigned int data_size) override
Implements ElementDataCacheBase::compute_node_data.
std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &offset_vec) override
Implements ElementDataCacheBase::element_node_cache_fixed_size.
CacheData get_data()
Return underlying vector of element data.
T & operator[](unsigned int i)
Access i-th element in the data vector of 0th component.
void print_ascii(ostream &out_stream, unsigned int idx) override
virtual ~ElementDataCache() override
Destructor of ElementDataCache.
void add(unsigned int idx, const T *value)
void normalize(unsigned int idx, unsigned int divisor)
std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec) override
Implements ElementDataCacheBase::element_node_cache_optimize_size.
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits)
@ out_of_limits
Some value(s) is out of limits.
@ not_a_number
Some value(s) is set to NaN.
@ ok
All values are not NaN and are in limits.
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
static constexpr bool value
Definition: json.hpp:87
#define MPI_Datatype
Definition: mpi.h:154