Flow123d  release_2.2.0-914-gf1a3a4f
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 <vector>
22 #include <string>
23 #include <memory>
24 #include <ostream>
25 #include <typeinfo>
26 #include "system/system.hh"
27 #include "system/tokenizer.hh"
28 #include "system/global_defs.h"
30 
31 
32 template <typename T>
34 public:
35  typedef std::shared_ptr< std::vector<T> > ComponentDataPtr;
37 
38  /// Default constructor
40 
41  /**
42  * \brief Constructor of input ElementDataCache (allow read data from GMSH or VTK file)
43  *
44  * Allows set variable size of cache.
45  *
46  * @param field_name Field name thas is read
47  * @param time Actual time of data
48  * @param size_of_cache Count of columns of data cache
49  * @param row_vec_size Count of rows of data cache
50  */
51  ElementDataCache(std::string field_name, double time, unsigned int size_of_cache, unsigned int row_vec_size);
52 
53  /**
54  * \brief Constructor of output ElementDataCache (allow write data)
55  *
56  * Has fix size of cache.
57  *
58  * @param field_name Field name is written as parameter to output stream
59  * @param n_rows Given from shape of field
60  * @param n_cols Given from shape of field
61  * @param size Count of rows of data cache
62  */
63  ElementDataCache(std::string field_name, unsigned int n_rows, unsigned int n_cols, unsigned int size);
64 
65  /**
66  * \brief Destructor of ElementDataCache
67  */
68  virtual ~ElementDataCache() override;
69 
70  /// Return vector of element data for get component.
71  ComponentDataPtr get_component_data(unsigned int component_idx);
72 
73  /**
74  * Create data cache with given count of columns (\p size_of_cache) and rows (\p row_vec_size).
75  */
76  static CacheData create_data_cache(unsigned int size_of_cache, unsigned int row_vec_size);
77 
78  /// Implements @p ElementDataCacheBase::read_ascii_data.
79  void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override;
80 
81  /// Implements @p ElementDataCacheBase::read_binary_data.
82  void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override;
83 
84  /**
85  * Output data element on given index @p idx. Method for writing data
86  * to output stream.
87  *
88  * \note This method is used only by MSH file format.
89  */
90  void print_ascii(ostream &out_stream, unsigned int idx) override;
91 
92  /**
93  * \brief Print all data stored in output data ro ascii format
94  *
95  * TODO: indicate if the tensor data are output in column-first or raw-first order
96  * and possibly implement transposition. Set such property for individual file formats.
97  * Class OutputData stores always in raw-first order.
98  */
99  void print_ascii_all(ostream &out_stream) override;
100 
101  /**
102  * \brief Print all data stored in output data to appended binary format
103  */
104  void print_binary_all(ostream &out_stream, bool print_data_size = true) override;
105 
106  void print_all_yaml(ostream &out_stream, unsigned int precision) override;
107 
108  /**
109  * Store data element of given data value under given index.
110  */
111  void store_value(unsigned int idx, const T * value);
112 
113  /**
114  * Add value to given index
115  */
116  void add(unsigned int idx, const T * value);
117 
118  /**
119  * Reset values at given index
120  */
121  void zero(unsigned int idx);
122 
123  /**
124  * Normalize values at given index
125  */
126  void normalize(unsigned int idx, unsigned int divisor);
127 
128  /**
129  * Find minimal and maximal range of stored data
130  */
131  void get_min_max_range(double &min, double &max) override;
132 
133  /// Access i-th element in the data vector of 0th component.
134  T& operator[](unsigned int i);
135 
136  /**
137  * Declaration of new exception info used in following exception
138  */
139  TYPEDEF_ERR_INFO(EI_FieldName, std::string);
140 
141  /**
142  * Declaration of exception
143  */
144  DECLARE_EXCEPTION(ExcOutputVariableVector, << "Can not output field " << EI_FieldName::qval
145  << " returning variable size vectors. Try convert to MultiField.\n");
146 
147 protected:
148  /**
149  * Table of element data.
150  *
151  * For every components contains vector of element data.
152  */
153  CacheData data_;
154 
155 };
156 
157 #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.
std::shared_ptr< std::vector< T > > ComponentDataPtr
void print_ascii(ostream &out_stream, unsigned int idx) override
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.
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)
Global macros to enhance readability and debugging, general constants.
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)
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
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.