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