Flow123d  3.9.1-8f37ba8
field_value_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 field_value_cache.hh
15  * @brief
16  * @author David Flanderka
17  */
18 
19 #ifndef FIELD_VALUE_CACHE_HH_
20 #define FIELD_VALUE_CACHE_HH_
21 
22 #include <set>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 #include "system/armor.hh"
27 #include "fields/eval_points.hh"
28 #include "mesh/accessors.hh"
29 #include "tools/mixed.hh"
30 #include "tools/revertable_list.hh"
31 #include "fem/dofhandler.hh"
32 
33 class EvalPoints;
34 class ElementCacheMap;
35 class DHCellAccessor;
36 class DHCellSide;
37 template < template<IntDim...> class DimAssembly> class GenericAssembly;
38 
39 
40 /**
41  * @brief Class holds precomputed field values of selected element set.
42  *
43  * Every field in equation use own instance used for elements of all dimensions.
44  */
45 template<class elm_type> using FieldValueCache = Armor::Array<elm_type>;
46 
47 
48 /**
49  * Specifies eval points by idx of region, element and eval point.
50  *
51  * TODO Add better description after finish implementation
52  */
53 struct EvalPointData {
54  EvalPointData() {} ///< Default constructor
55  /// Constructor sets all data members
56  EvalPointData(unsigned int i_reg, unsigned int i_ele, unsigned int i_ep, unsigned int dh_loc_idx)
57  : i_reg_(i_reg), i_element_(i_ele), i_eval_point_(i_ep), dh_loc_idx_(dh_loc_idx) {}
58  /// Copy constructor
61 
62 
63  bool operator < (const EvalPointData &other) {
64  if (i_reg_ == other.i_reg_) {
65  if (i_element_ == other.i_element_)
66  return (i_eval_point_ < other.i_eval_point_);
67  else
68  return (i_element_ < other.i_element_);
69  } else
70  return (i_reg_ < other.i_reg_);
71  }
72 
73  unsigned int i_reg_; ///< region_idx of element
74  unsigned int i_element_; ///< mesh_idx of ElementAccessor appropriate to element
75  unsigned int i_eval_point_; ///< index of point in EvalPoint object
76  unsigned int dh_loc_idx_; ///< local index of cell in DOF handler
77 };
78 
79 
80 
81 
82 /**
83  * @brief Auxiliary data class holds number of elements in cache and allow to set this value
84  * explicitly (e.g. as input parameter).
85  *
86  * Implementation is done as singletone with two access through static methods 'get' and 'set'.
87  *
88  * TODO: This is actually a constant so make it a constant int in element cache map.
89  */
91 public:
92  /// Return number of stored elements
93  static unsigned int get() {
94  return get_instance().n_elem_;
95  }
96 
97  /// Set number of stored elements
98  static void set(unsigned int n_elem) {
99  get_instance().n_elem_ = n_elem;
100  }
101 
102  CacheMapElementNumber(CacheMapElementNumber const&) = delete; ///< We don't need copy constructor.
103  void operator=(CacheMapElementNumber const&) = delete; ///< We don't need assignment operator.
104 
105 private:
106  /// Forbiden default constructor
108 
109 
111  {
112  static CacheMapElementNumber instance;
113  return instance;
114  }
115 
116  /// Maximal number of elements stored in cache.
117  unsigned int n_elem_;
118 };
119 
120 
121 /**
122  * @brief Directing class of FieldValueCache.
123  *
124  * Manage storing and updating element data (elements of same dimension) to cache. We need only
125  * one shared instance of this class for all fields in equation (but typically for dim = 1,2,3).
126  *
127  * IMPORTANT: Because there are combined bulk and boundary elements, we must use mesh_idx value
128  * to correct identification of elements.
129  *
130  * TODO:
131  * 1. Generic assembly pass through the patch collecting needed quadrature points. (PASS ORDER)
132  * 2. Then we sort these points for efficient chae_upadate of the fields (CACHE ORDER)
133  * 3. We pass through the patch again evaluating actual integrals. This second pass is currently inefficient since
134  * we can not map efficiently from the PASS ORDER to the CACHE ORDER), this leads to many complications in
135  * quad point classes.
136  * We should:
137  * 1. have templated patch iteration mechanism, so that we can iterate through the evaluated integrals
138  * twice in consistent way performing:
139  * FIRST: collection of evaluation points
140  * SECOND: evaluation of integrals using the fields and fe_values
141  * Having a consistent implementation allows us to assign unique indices to the integral points on the patch.
142  * 2. Sort collected points, remove duplicities, mark new indices to the original list of points.
143  * 3. SECOND pass, use unique index to find the point in the cache.
144  *
145  * Resulting simplifications:
146  * - no need for associating point operations for Edge, Coupling and Boundary points,
147  * we add assiciated eval point pairs as separate eval points with unique ids. Consistent integral iteration
148  * allows us to simply take two succesive points at the second pass.
149  * - no need for the matrix mapping (element, eval_point) to the cache index
150  */
152 public:
153  /// Index of invalid element in cache.
154  static const unsigned int undef_elem_idx;
155 
156  /// Size of block (evaluation of FieldFormula) must be multiple of this value.
157  /// TODO We should take this value from BParser and it should be dependent on processor configuration.
158  static const unsigned int simd_size_double;
159 
160  /// Constructor
161  ElementCacheMap();
162 
163  /// Destructor
165 
166  /// Init cache
167  void init(std::shared_ptr<EvalPoints> eval_points);
168 
169  /// Create patch of cached elements before reading data to cache.
170  void create_patch();
171 
172  /// Reset all items of elements_eval_points_map
175  unsigned int last_element_idx = -1, i_elem_row = -1;
176  for (unsigned int i=0; i<eval_point_data_.permanent_size(); ++i) {
177  if (eval_point_data_[i].i_element_ != last_element_idx) { // new element
178  i_elem_row++;
179  last_element_idx =eval_point_data_[i].i_element_;
180  }
182  }
184  }
185 
186  /// Start update of cache.
187  void start_elements_update();
188 
189  /// Finish update after reading data to cache.
190  void finish_elements_update();
191 
192  /// Getter of eval_points object.
193  inline std::shared_ptr<EvalPoints> eval_points() const {
194  return eval_points_;
195  }
196 
197  /** Adds EvalPointData using emplace_back.
198  * Arguments correspond to constructor of EvalPointData.
199  */
200  inline void add_eval_point(unsigned int i_reg, unsigned int i_ele, unsigned int i_eval_point, unsigned int dh_loc_idx)
201  {
202  eval_point_data_.emplace_back(i_reg, i_ele, i_eval_point, dh_loc_idx);
203  set_of_regions_.insert(i_reg);
204  }
205 
206  /// Returns number of eval. points with addition of max simd duplicates due to regions.
207  inline unsigned int get_simd_rounded_size()
208  {
210  }
211 
212  /*
213  * Access to item of \p element_eval_points_map_ like to two-dimensional array.
214  *
215  * @param i_elem_in_cache idx of ElementAccessor in ElementCacheMap
216  * @param i_eval_point index of local point in EvalPoints
217  * @return index of point in FieldValueCache.
218  */
219  inline int element_eval_point(unsigned int i_elem_in_cache, unsigned int i_eval_point) const {
221  return element_eval_points_map_[i_elem_in_cache*eval_points_->max_size()+i_eval_point];
222  }
223 
224  /// Return mesh_idx of element stored at given position of ElementCacheMap
225  inline unsigned int elm_idx_on_position(unsigned pos) const {
226  return elm_idx_[pos];
227  }
228 
229  /// Return position of element stored in ElementCacheMap
230  inline unsigned int position_in_cache(unsigned mesh_elm_idx, bool bdr=false) const {
231  std::unordered_map<unsigned int, unsigned int>::const_iterator it;
232  if (bdr) {
233  it = element_to_map_bdr_.find(mesh_elm_idx);
234  if ( it != element_to_map_bdr_.end() ) return it->second;
236  } else {
237  it = element_to_map_.find(mesh_elm_idx);
238  if ( it != element_to_map_.end() ) return it->second;
240  }
241  }
242 
243  /// Return number of stored regions.
244  inline unsigned int n_regions() const {
245  return regions_starts_.permanent_size() - 1;
246  }
247 
248  /// Return number of stored elements.
249  inline unsigned int n_elements() const {
250  return element_starts_.permanent_size() - 1;
251  }
252 
253  /// Return begin position of element chunk in FieldValueCache
254  inline unsigned int element_chunk_begin(unsigned int elm_patch_idx) const {
255  ASSERT_LT(elm_patch_idx, n_elements());
256  return element_starts_[elm_patch_idx];
257  }
258 
259  /// Return end position of element chunk in FieldValueCache
260  inline unsigned int element_chunk_end(unsigned int elm_patch_idx) const {
261  ASSERT_LT(elm_patch_idx, n_elements());
262  return element_starts_[elm_patch_idx+1];
263  }
264 
265  /// Return begin position of region chunk in FieldValueCache
266  inline unsigned int region_chunk_begin(unsigned int region_patch_idx) const {
267  ASSERT_LT(region_patch_idx, n_regions());
268  return element_starts_[ regions_starts_[region_patch_idx] ];
269  }
270 
271  /// Return end position of region chunk in FieldValueCache
272  inline unsigned int region_chunk_end(unsigned int region_patch_idx) const {
273  ASSERT_LT(region_patch_idx, n_regions());
274  return element_starts_[ regions_starts_[region_patch_idx+1] ];
275  }
276 
277  /// Return begin position of region chunk specified by position in map
278  inline unsigned int region_chunk_by_map_index(unsigned int r_idx) const {
279  if (r_idx <= n_regions()) return element_starts_[ regions_starts_[r_idx] ];
281  }
282 
283  /// Return begin position of region chunk specified by position in map
284  inline unsigned int region_idx_from_chunk_position(unsigned int chunk_pos) const {
285  return eval_point_data_[ this->region_chunk_by_map_index(chunk_pos) ].i_reg_;
286  }
287 
288  /// Return item of eval_point_data_ specified by its position
289  inline const EvalPointData &eval_point_data(unsigned int point_idx) const {
290  return eval_point_data_[point_idx];
291  }
292 
293  /// Return value of evaluation point given by idx of element in patch and local point idx in EvalPoints from cache.
294  template<class Value>
295  inline typename Value::return_type get_value(const FieldValueCache<typename Value::element_type> &field_cache,
296  unsigned int elem_patch_idx, unsigned int eval_points_idx) const {
297  ASSERT_EQ(Value::NRows_, field_cache.n_rows());
298  ASSERT_EQ(Value::NCols_, field_cache.n_cols());
299  unsigned int value_cache_idx = this->element_eval_point(elem_patch_idx, eval_points_idx);
300  ASSERT(value_cache_idx != ElementCacheMap::undef_elem_idx);
301  return Value::get_from_array(field_cache, value_cache_idx);
302  }
303 protected:
304 
305  /// Special constant (@see element_eval_points_map_).
306  static const int unused_point = -1;
307 
308  /// Base number of stored regions in patch
309  static const unsigned int regions_in_chunk = 3;
310 
311  /// Base number of stored elements in patch
312  static const unsigned int elements_in_chunk = 10;
313 
314  /// Set item of \p element_eval_points_map_.
315  inline void set_element_eval_point(unsigned int i_elem_in_cache, unsigned int i_eval_point, int val) const {
317  element_eval_points_map_[i_elem_in_cache*eval_points_->max_size()+i_eval_point] = val;
318  }
319 
320  /// Vector of element indexes stored in cache.
322 
323  /// Pointer to EvalPoints
324  std::shared_ptr<EvalPoints> eval_points_;
325 
326  /// Flag is set down during update of cache when this can't be read
328 
329  /**
330  * This array provides indexes to FieldValueCache.
331  *
332  * This one dimensional array behaves like two dimensional factually.
333  * Size is set to 'n_cached_elements * n_eval_points' and items are
334  * accessible through two indices:
335  *
336  * 1: Over elements holds in ElementCacheMap
337  * 2: Over EvalPoints for each element
338  *
339  * Use always and only methods \p element_eval_point for read and
340  * \p set_element_eval_point (for write) to access to items!
341  *
342  * Array is filled in those three steps:
343  * a. Reset - all items are set to ElementCacheMap::unused_point
344  * b. Used eval points are set to ElementCacheMap::point_in_proggress
345  * c. Eval points marked in previous step are sequentially numbered
346  *
347  * TODO improve description
348  */
350 
351  ///< Holds data of evaluating points in patch.
353 
354  /// @name Holds start positions and orders of region chunks and element chunks
355  // @{
356 
357  RevertableList<unsigned int> regions_starts_; ///< Start positions of elements in regions (size = n_regions+1, last value is end of last region)
358  RevertableList<unsigned int> element_starts_; ///< Start positions of elements in eval_point_data_ (size = n_elements+1)
359  std::unordered_map<unsigned int, unsigned int> element_to_map_; ///< Maps bulk element_idx to element index in patch - TODO remove
360  std::unordered_map<unsigned int, unsigned int> element_to_map_bdr_; ///< Maps boundary element_idx to element index in patch - TODO remove
361 
362  // @}
363 
364  /// Keeps set of unique region indices of added eval. points.
365  std::unordered_set<unsigned int> set_of_regions_;
366 
367  // TODO: remove friend class
368  template < template<IntDim...> class DimAssembly>
369  friend class GenericAssembly;
370 };
371 
372 
373 
374 #endif /* FIELD_VALUE_CACHE_HH_ */
EvalPointData::i_element_
unsigned int i_element_
mesh_idx of ElementAccessor appropriate to element
Definition: field_value_cache.hh:74
ElementCacheMap::element_chunk_begin
unsigned int element_chunk_begin(unsigned int elm_patch_idx) const
Return begin position of element chunk in FieldValueCache.
Definition: field_value_cache.hh:254
ElementCacheMap::elm_idx_on_position
unsigned int elm_idx_on_position(unsigned pos) const
Return mesh_idx of element stored at given position of ElementCacheMap.
Definition: field_value_cache.hh:225
CacheMapElementNumber::get
static unsigned int get()
Return number of stored elements.
Definition: field_value_cache.hh:93
CacheMapElementNumber::CacheMapElementNumber
CacheMapElementNumber()
Forbiden default constructor.
Definition: field_value_cache.hh:107
EvalPointData::i_eval_point_
unsigned int i_eval_point_
index of point in EvalPoint object
Definition: field_value_cache.hh:75
armor.hh
ElementCacheMap::element_to_map_bdr_
std::unordered_map< unsigned int, unsigned int > element_to_map_bdr_
Maps boundary element_idx to element index in patch - TODO remove.
Definition: field_value_cache.hh:360
ElementCacheMap::create_patch
void create_patch()
Create patch of cached elements before reading data to cache.
Definition: field_value_cache.cc:61
EvalPointData::operator<
bool operator<(const EvalPointData &other)
Definition: field_value_cache.hh:63
ElementCacheMap::region_chunk_by_map_index
unsigned int region_chunk_by_map_index(unsigned int r_idx) const
Return begin position of region chunk specified by position in map.
Definition: field_value_cache.hh:278
EvalPointData::EvalPointData
EvalPointData(const EvalPointData &other)
Copy constructor.
Definition: field_value_cache.hh:59
RevertableList::emplace_back
std::size_t emplace_back(Args &&... args)
Definition: revertable_list.hh:114
ElementCacheMap::position_in_cache
unsigned int position_in_cache(unsigned mesh_elm_idx, bool bdr=false) const
Return position of element stored in ElementCacheMap.
Definition: field_value_cache.hh:230
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
ElementCacheMap::simd_size_double
static const unsigned int simd_size_double
Definition: field_value_cache.hh:158
CacheMapElementNumber
Auxiliary data class holds number of elements in cache and allow to set this value explicitly (e....
Definition: field_value_cache.hh:90
ElementCacheMap
Directing class of FieldValueCache.
Definition: field_value_cache.hh:151
ElementCacheMap::undef_elem_idx
static const unsigned int undef_elem_idx
Index of invalid element in cache.
Definition: field_value_cache.hh:154
eval_points.hh
RevertableList::permanent_size
std::size_t permanent_size() const
Return permanent size of list.
Definition: revertable_list.hh:71
ElementCacheMap::n_elements
unsigned int n_elements() const
Return number of stored elements.
Definition: field_value_cache.hh:249
ElementCacheMap::set_element_eval_point
void set_element_eval_point(unsigned int i_elem_in_cache, unsigned int i_eval_point, int val) const
Set item of element_eval_points_map_.
Definition: field_value_cache.hh:315
std::vector< unsigned int >
ElementCacheMap::element_chunk_end
unsigned int element_chunk_end(unsigned int elm_patch_idx) const
Return end position of element chunk in FieldValueCache.
Definition: field_value_cache.hh:260
CacheMapElementNumber::operator=
void operator=(CacheMapElementNumber const &)=delete
We don't need assignment operator.
RevertableList::reset
void reset()
Clear the list.
Definition: revertable_list.hh:142
ElementCacheMap::element_eval_points_map_
int * element_eval_points_map_
Holds data of evaluating points in patch.
Definition: field_value_cache.hh:349
dofhandler.hh
Declaration of class which handles the ordering of degrees of freedom (dof) and mappings between loca...
ElementCacheMap::set_of_regions_
std::unordered_set< unsigned int > set_of_regions_
Keeps set of unique region indices of added eval. points.
Definition: field_value_cache.hh:365
ElementCacheMap::regions_starts_
RevertableList< unsigned int > regions_starts_
Start positions of elements in regions (size = n_regions+1, last value is end of last region)
Definition: field_value_cache.hh:357
ElementCacheMap::eval_point_data_
RevertableList< EvalPointData > eval_point_data_
Definition: field_value_cache.hh:352
EvalPointData::dh_loc_idx_
unsigned int dh_loc_idx_
local index of cell in DOF handler
Definition: field_value_cache.hh:76
ASSERT_LT
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
ElementCacheMap::init
void init(std::shared_ptr< EvalPoints > eval_points)
Init cache.
Definition: field_value_cache.cc:50
ElementCacheMap::eval_point_data
const EvalPointData & eval_point_data(unsigned int point_idx) const
Return item of eval_point_data_ specified by its position.
Definition: field_value_cache.hh:289
RevertableList::temporary_size
std::size_t temporary_size() const
Return temporary size of list (full size of stored data).
Definition: revertable_list.hh:77
Armor::Array::n_rows
uint n_rows() const
Definition: armor.hh:715
ElementCacheMap::region_chunk_end
unsigned int region_chunk_end(unsigned int region_patch_idx) const
Return end position of region chunk in FieldValueCache.
Definition: field_value_cache.hh:272
DHCellSide
Side accessor allows to iterate over sides of DOF handler cell.
Definition: dh_cell_accessor.hh:176
ElementCacheMap::elements_in_chunk
static const unsigned int elements_in_chunk
Base number of stored elements in patch.
Definition: field_value_cache.hh:312
EvalPointData::i_reg_
unsigned int i_reg_
region_idx of element
Definition: field_value_cache.hh:73
accessors.hh
CacheMapElementNumber::get_instance
static CacheMapElementNumber & get_instance()
Definition: field_value_cache.hh:110
ElementCacheMap::clear_element_eval_points_map
void clear_element_eval_points_map()
Reset all items of elements_eval_points_map.
Definition: field_value_cache.hh:173
ElementCacheMap::eval_points_
std::shared_ptr< EvalPoints > eval_points_
Pointer to EvalPoints.
Definition: field_value_cache.hh:324
EvalPointData::EvalPointData
EvalPointData(unsigned int i_reg, unsigned int i_ele, unsigned int i_ep, unsigned int dh_loc_idx)
Constructor sets all data members.
Definition: field_value_cache.hh:56
ElementCacheMap::eval_points
std::shared_ptr< EvalPoints > eval_points() const
Getter of eval_points object.
Definition: field_value_cache.hh:193
ElementCacheMap::element_eval_point
int element_eval_point(unsigned int i_elem_in_cache, unsigned int i_eval_point) const
Definition: field_value_cache.hh:219
ElementCacheMap::unused_point
static const int unused_point
Special constant (.
Definition: field_value_cache.hh:306
ElementCacheMap::regions_in_chunk
static const unsigned int regions_in_chunk
Base number of stored regions in patch.
Definition: field_value_cache.hh:309
ASSERT_EQ
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:333
ElementCacheMap::element_to_map_
std::unordered_map< unsigned int, unsigned int > element_to_map_
Maps bulk element_idx to element index in patch - TODO remove.
Definition: field_value_cache.hh:359
ElementCacheMap::elm_idx_
std::vector< unsigned int > elm_idx_
Vector of element indexes stored in cache.
Definition: field_value_cache.hh:321
Armor::Array::n_cols
uint n_cols() const
Definition: armor.hh:720
RevertableList< EvalPointData >
ElementCacheMap::add_eval_point
void add_eval_point(unsigned int i_reg, unsigned int i_ele, unsigned int i_eval_point, unsigned int dh_loc_idx)
Definition: field_value_cache.hh:200
Input::Type
Definition: balance.hh:41
ElementCacheMap::finish_elements_update
void finish_elements_update()
Finish update after reading data to cache.
Definition: field_value_cache.cc:123
ElementCacheMap::get_simd_rounded_size
unsigned int get_simd_rounded_size()
Returns number of eval. points with addition of max simd duplicates due to regions.
Definition: field_value_cache.hh:207
ElementCacheMap::region_chunk_begin
unsigned int region_chunk_begin(unsigned int region_patch_idx) const
Return begin position of region chunk in FieldValueCache.
Definition: field_value_cache.hh:266
DHCellAccessor
Cell accessor allow iterate over DOF handler cells.
Definition: dh_cell_accessor.hh:43
ElementCacheMap::region_idx_from_chunk_position
unsigned int region_idx_from_chunk_position(unsigned int chunk_pos) const
Return begin position of region chunk specified by position in map.
Definition: field_value_cache.hh:284
revertable_list.hh
ElementCacheMap::~ElementCacheMap
~ElementCacheMap()
Destructor.
Definition: field_value_cache.cc:43
Armor::Array
Definition: armor.hh:597
EvalPointData
Definition: field_value_cache.hh:53
ElementCacheMap::ElementCacheMap
ElementCacheMap()
Constructor.
Definition: field_value_cache.cc:36
CacheMapElementNumber::n_elem_
unsigned int n_elem_
Maximal number of elements stored in cache.
Definition: field_value_cache.hh:117
ElementCacheMap::start_elements_update
void start_elements_update()
Start update of cache.
Definition: field_value_cache.cc:119
ASSERT_PTR
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
CacheMapElementNumber::set
static void set(unsigned int n_elem)
Set number of stored elements.
Definition: field_value_cache.hh:98
mixed.hh
GenericAssembly
Generic class of assemblation.
Definition: generic_assembly.hh:160
ElementCacheMap::get_value
Value::return_type get_value(const FieldValueCache< typename Value::element_type > &field_cache, unsigned int elem_patch_idx, unsigned int eval_points_idx) const
Return value of evaluation point given by idx of element in patch and local point idx in EvalPoints f...
Definition: field_value_cache.hh:295
ElementCacheMap::n_regions
unsigned int n_regions() const
Return number of stored regions.
Definition: field_value_cache.hh:244
ElementCacheMap::ready_to_reading_
bool ready_to_reading_
Flag is set down during update of cache when this can't be read.
Definition: field_value_cache.hh:327
ElementCacheMap::element_starts_
RevertableList< unsigned int > element_starts_
Start positions of elements in eval_point_data_ (size = n_elements+1)
Definition: field_value_cache.hh:358
EvalPoints
Class holds local coordinations of evaluating points (bulk and sides) specified by element dimension.
Definition: eval_points.hh:43
IntDim
unsigned int IntDim
A dimension index type.
Definition: mixed.hh:19