Flow123d  JS_before_hm-1710-g489f4168f
mesh_optimizer.hh
Go to the documentation of this file.
1 
2 #ifndef MESH_OPTIMIZER_HH_
3 #define MESH_OPTIMIZER_HH_
4 
5 #include <vector>
6 
7 #include <armadillo>
8 
9 #include "mesh/mesh.h"
10 #include "mesh/accessors.hh"
11 #include "mesh/range_wrapper.hh"
12 #include "bounding_box.hh"
13 
14 struct Permutee {
15  inline Permutee(uint original_index, double curve_value) : original_index_(original_index), curve_value_(curve_value) {}
17  double curve_value_;
18 };
19 
20 struct Normalizer {
21  inline Normalizer() : shift_({0, 0, 0}), scalar_(1) {}
22  inline Normalizer(arma::vec3 shift, double scalar) : shift_(shift), scalar_(scalar) {}
24  return (vec - shift_) / scalar_;
25  }
27  double scalar_;
28 };
29 
30 inline bool operator<(const Permutee& first, const Permutee& second) {
31  return first.curve_value_ < second.curve_value_;
32 }
33 
34 template <uint DIM>
36  static_assert(DIM == 2 || DIM == 3, "DIM must be either 2 or 3.");
37 public:
38  inline MeshOptimizer(Mesh* mesh) : mesh_(mesh) {}
39 
40  inline void calculate_sizes() {
41  BoundingBox bounding_box( *mesh_->node(0) );
42  for (auto nod : mesh_->node_range()) {
43  bounding_box.expand(*nod);
44  }
45  double mesh_longest_size = bounding_box.longest_size();
46  normalizer_ = Normalizer(bounding_box.min(), mesh_longest_size);
47 
48  node_sizes_.resize(mesh_->n_nodes(), INFINITY);
49  element_sizes_.reserve(mesh_->n_elements());
50  for (const ElementAccessor<3>& elm : mesh_->elements_range()) {
51  double elm_norm_size = elm.bounding_box().longest_size() / mesh_longest_size;
52  element_sizes_.push_back(elm_norm_size);
53  for (uint i = 0; i < elm.dim() + 1; ++i) {
54  node_sizes_[elm->node_idx(i)] = std::min({ node_sizes_[elm->node_idx(i)], elm_norm_size });
55  }
56  }
57  }
58 
60  node_refs_.reserve(mesh_->n_nodes());
61  uint i = 0;
62  for (auto nod : mesh_->node_range()) {
63  node_refs_.emplace_back(i, hilbert_value(normalizer_.normalize( *nod ), node_sizes_[i]));
64  i++;
65  }
66  }
67 
69  node_refs_.reserve(mesh_->n_nodes());
70  uint i = 0;
71  for (auto nod : mesh_->node_range()) {
72  node_refs_.emplace_back(i, zcurve_value(normalizer_.normalize( *nod ), node_sizes_[i]));
73  }
74  }
75 
77  node_refs_.reserve(mesh_->n_nodes());
78  for (uint i = 0; i < mesh_->n_elements(); ++i) {
79  auto elm = mesh_->element_accessor(element_refs_[i].original_index_);
80  for (uint j = 0; j < elm->dim() + 1; ++j) {
81  node_refs_.emplace_back(elm->node_idx(j), element_refs_[i].curve_value_);
82  }
83  }
84  }
85 
87  element_refs_.reserve(mesh_->n_elements());
88  uint i = 0;
89  for (auto elm : mesh_->elements_range()) {
90  element_refs_.emplace_back(i, hilbert_value(normalizer_.normalize(elm.centre()), element_sizes_[i]));
91  i++;
92  }
93  }
94 
96  element_refs_.reserve(mesh_->n_elements());
97  uint i = 0;
98  for (auto elm : mesh_->elements_range()) {
99  element_refs_.emplace_back(i, zcurve_value(normalizer_.normalize(elm.centre()), element_sizes_[i]));
100  i++;
101  }
102  }
103 
105  return this->sort(node_refs_, node_permutation);
106  }
107 
109  return this->sort(element_refs_, elem_permutation);
110  }
111 
112 private:
119 
120  inline double hilbert_value(double x, double y, double eps) const {
121  if (eps > 1) {
122  return 0;
123  } else {
124  if (x < 0.5) {
125  if (y < 0.5) {
126  return hilbert_value(2 * y, 2 * x, 4 * eps) / 4;
127  } else {
128  return (1 + hilbert_value(2 * x, 2 * y - 1, 4 * eps)) / 4;
129  }
130  } else {
131  if (y >= 0.5) {
132  return (2 + hilbert_value(2 * x - 1, 2 * y - 1, 4 * eps)) / 4;
133  } else {
134  return (3 + hilbert_value(1 - 2 * y, 2 - 2 * x, 4 * eps)) / 4;
135  }
136  }
137  }
138  }
139 
140  inline double hilbert_value(double x, double y, double z, double eps) const {
141  if (eps > 1) {
142  return 0;
143  } else {
144  if (z < 0.5) {
145  if (x < 0.5) {
146  if (y < 0.5) {
147  return hilbert_value(2 * z, 2 * x, 2 * y, 8 * eps) / 8;
148  } else {
149  return (1 + hilbert_value(2 * y - 1, 2 * z, 2 * x, 8 * eps)) / 8;
150  }
151  } else {
152  if (y >= 0.5) {
153  return (2 + hilbert_value(2 * y - 1, 2 * z, 2 * x - 1, 8 * eps)) / 8;
154  } else {
155  return (3 + hilbert_value(2 - 2 * x, 1 - 2 * y, 2 * z, 8 * eps)) / 8;
156  }
157  }
158  } else {
159  if (x >= 0.5) {
160  if (y < 0.5) {
161  return (4 + hilbert_value(2 - 2 * x, 1 - 2 * y, 2 * z - 1, 8 * eps)) / 8;
162  } else {
163  return (5 + hilbert_value(2 * y - 1, 2 - 2 * z, 2 - 2 * x, 8 * eps)) / 8;
164  }
165  } else {
166  if (y >= 0.5) {
167  return (6 + hilbert_value(2 * y - 1, 2 - 2 * z, 1 - 2 * x, 8 * eps)) / 8;
168  } else {
169  return (7 + hilbert_value(2 - 2 * z, 2 * x, 1 - 2 * y, 8 * eps)) / 8;
170  }
171  }
172  }
173  }
174  }
175 
176  inline double zcurve_value(double x, double y, double eps) const {
177  if (eps > 1) {
178  return 0;
179  } else {
180  if (y < 0.5) {
181  if (x < 0.5) {
182  return zcurve_value(2 * x, 2 * y, 4 * eps) / 4;
183  } else {
184  return (1 + zcurve_value(2 * x - 1, 2 * y, 4 * eps)) / 4;
185  }
186  } else {
187  if (x < 0.5) {
188  return (2 + zcurve_value(2 * x, 2 * y - 1, 4 * eps)) / 4;
189  } else {
190  return (3 + zcurve_value(2 * x - 1, 2 * y - 1, 4 * eps)) / 4;
191  }
192  }
193  }
194  }
195 
196  inline double zcurve_value(double x, double y, double z, double eps) const {
197  if (eps > 1) {
198  return 0;
199  } else {
200  if (z < 0.5) {
201  if (y < 0.5) {
202  if (x < 0.5) {
203  return zcurve_value(2 * x, 2 * y, 2 * z, 8 * eps) / 8;
204  } else {
205  return (1 + zcurve_value(2 * x - 1, 2 * y, 2 * z, 8 * eps)) / 8;
206  }
207  } else {
208  if (x < 0.5) {
209  return (2 + zcurve_value(2 * x, 2 * y - 1, 2 * z, 8 * eps)) / 8;
210  } else {
211  return (3 + zcurve_value(2 * x - 1, 2 * y - 1, 2 * z, 8 * eps)) / 8;
212  }
213  }
214  } else {
215  if (y < 0.5) {
216  if (x < 0.5) {
217  return (4 + zcurve_value(2 * x, 2 * y, 2 * z - 1, 8 * eps)) / 8;
218  } else {
219  return (5 + zcurve_value(2 * x - 1, 2 * y, 2 * z - 1, 8 * eps)) / 8;
220  }
221  } else {
222  if (x < 0.5) {
223  return (6 + zcurve_value(2 * x, 2 * y - 1, 2 * z - 1, 8 * eps)) / 8;
224  } else {
225  return (7 + zcurve_value(2 * x - 1, 2 * y - 1, 2 * z - 1, 8 * eps)) / 8;
226  }
227  }
228  }
229  }
230  }
231 
232  inline double hilbert_value(arma::vec3 vec, double size) const {
233  return hilbert_value(vec[0], vec[1], vec[2], size * size * size);
234  }
235 
236  inline double zcurve_value(arma::vec3 vec, double size) const {
237  return zcurve_value(vec[0], vec[1], vec[2], size * size * size);
238  }
239 
241  ASSERT_DBG(refs.size() <= mesh_perm.size());
242  std::sort(refs.begin(), refs.end());
243  std::vector<int> mesh_ids;
244  mesh_ids.reserve(refs.size());
245  for (uint i = 0; i < refs.size(); ++i) {
246  mesh_perm[ refs[i].original_index_ ] = i;
247  mesh_ids.push_back( refs[i].original_index_ );
248  }
249  return mesh_ids;
250  }
251 
252 };
253 
254 template<>
255 inline double MeshOptimizer<2>::hilbert_value(arma::vec3 vec, double size) const {
256  return hilbert_value(vec[0], vec[1], size * size);
257 }
258 
259 template <>
260 inline double MeshOptimizer<2>::zcurve_value(arma::vec3 vec, double size) const {
261  return zcurve_value(vec[0], vec[1], size * size);
262 }
263 
264 #endif /* MESH_OPTIMIZER_HH_ */
MeshOptimizer::calculate_node_curve_values_as_obtained_from_elements
void calculate_node_curve_values_as_obtained_from_elements()
Definition: mesh_optimizer.hh:76
MeshOptimizer::calculate_node_curve_values_as_hilbert
void calculate_node_curve_values_as_hilbert()
Definition: mesh_optimizer.hh:59
MeshOptimizer::zcurve_value
double zcurve_value(double x, double y, double z, double eps) const
Definition: mesh_optimizer.hh:196
MeshOptimizer::sort
std::vector< int > sort(std::vector< Permutee > &refs, std::vector< unsigned int > &mesh_perm)
Definition: mesh_optimizer.hh:240
MeshOptimizer::sort_elements
std::vector< int > sort_elements(std::vector< unsigned int > &elem_permutation)
Definition: mesh_optimizer.hh:108
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
MeshOptimizer::zcurve_value
double zcurve_value(double x, double y, double eps) const
Definition: mesh_optimizer.hh:176
MeshOptimizer::hilbert_value
double hilbert_value(arma::vec3 vec, double size) const
Definition: mesh_optimizer.hh:232
MeshOptimizer::element_sizes_
std::vector< double > element_sizes_
Definition: mesh_optimizer.hh:117
BoundingBox::expand
void expand(const Point &point)
Definition: bounding_box.hh:253
MeshOptimizer::calculate_element_curve_values_as_hilbert_of_centers
void calculate_element_curve_values_as_hilbert_of_centers()
Definition: mesh_optimizer.hh:86
MeshOptimizer::hilbert_value
double hilbert_value(double x, double y, double z, double eps) const
Definition: mesh_optimizer.hh:140
Mesh::elements_range
virtual Range< ElementAccessor< 3 > > elements_range() const
Returns range of bulk elements.
Definition: mesh.cc:1315
Normalizer::normalize
arma::vec3 normalize(const arma::vec3 vec)
Definition: mesh_optimizer.hh:23
MeshOptimizer::node_sizes_
std::vector< double > node_sizes_
Definition: mesh_optimizer.hh:116
MeshOptimizer::mesh_
Mesh * mesh_
Definition: mesh_optimizer.hh:113
ASSERT_DBG
#define ASSERT_DBG(expr)
Definition: include_fadbad.hh:28
MeshOptimizer::calculate_sizes
void calculate_sizes()
Definition: mesh_optimizer.hh:40
std::vector< int >
ElementAccessor< 3 >
arma::vec3
Definition: doxy_dummy_defs.hh:17
Mesh::node_range
Range< NodeAccessor< 3 > > node_range() const
Returns range of nodes.
Definition: mesh.cc:1321
Permutee::original_index_
uint original_index_
Definition: mesh_optimizer.hh:16
uint
unsigned int uint
Definition: mh_dofhandler.hh:101
Mesh::n_elements
virtual unsigned int n_elements() const
Returns count of boundary or bulk elements.
Definition: mesh.h:373
BoundingBox
Bounding box in 3d ambient space.
Definition: bounding_box.hh:54
MeshOptimizer::sort_nodes
std::vector< int > sort_nodes(std::vector< unsigned int > &node_permutation)
Definition: mesh_optimizer.hh:104
Permutee::Permutee
Permutee(uint original_index, double curve_value)
Definition: mesh_optimizer.hh:15
MeshOptimizer::zcurve_value
double zcurve_value(arma::vec3 vec, double size) const
Definition: mesh_optimizer.hh:236
accessors.hh
Normalizer
Definition: mesh_optimizer.hh:20
mesh.h
bounding_box.hh
operator<
bool operator<(const Permutee &first, const Permutee &second)
Definition: mesh_optimizer.hh:30
Normalizer::Normalizer
Normalizer(arma::vec3 shift, double scalar)
Definition: mesh_optimizer.hh:22
MeshOptimizer::normalizer_
Normalizer normalizer_
Definition: mesh_optimizer.hh:118
MeshOptimizer::node_refs_
std::vector< Permutee > node_refs_
Definition: mesh_optimizer.hh:114
Normalizer::shift_
arma::vec3 shift_
Definition: mesh_optimizer.hh:26
Mesh
Definition: mesh.h:77
BoundingBox::longest_size
double longest_size() const
Definition: bounding_box.hh:301
MeshOptimizer::calculate_element_curve_values_as_zcurve_of_center
void calculate_element_curve_values_as_zcurve_of_center()
Definition: mesh_optimizer.hh:95
MeshOptimizer::calculate_node_curve_values_as_zcurve
void calculate_node_curve_values_as_zcurve()
Definition: mesh_optimizer.hh:68
Normalizer::scalar_
double scalar_
Definition: mesh_optimizer.hh:27
MeshOptimizer::hilbert_value
double hilbert_value(double x, double y, double eps) const
Definition: mesh_optimizer.hh:120
MeshOptimizer::element_refs_
std::vector< Permutee > element_refs_
Definition: mesh_optimizer.hh:115
Normalizer::Normalizer
Normalizer()
Definition: mesh_optimizer.hh:21
MeshOptimizer
Definition: mesh_optimizer.hh:35
Permutee::curve_value_
double curve_value_
Definition: mesh_optimizer.hh:17
Mesh::node
NodeAccessor< 3 > node(unsigned int idx) const
Create and return NodeAccessor to node of given idx.
Definition: mesh.cc:914
Mesh::element_accessor
virtual ElementAccessor< 3 > element_accessor(unsigned int idx) const
Create and return ElementAccessor to element of given idx.
Definition: mesh.cc:908
MeshOptimizer::MeshOptimizer
MeshOptimizer(Mesh *mesh)
Definition: mesh_optimizer.hh:38
Mesh::n_nodes
virtual unsigned int n_nodes() const
Definition: mesh.h:143
BoundingBox::min
const Point & min() const
Definition: bounding_box.hh:129
Permutee
Definition: mesh_optimizer.hh:14
range_wrapper.hh
Implementation of range helper class.