1#ifndef COMMA_PROJECT_ARCOMPUTER_H
2#define COMMA_PROJECT_ARCOMPUTER_H
20#include <unordered_map>
21#include <unordered_set>
35template<
typename IndexT,
typename RealT,
typename IntT>
83 const RealT measure = std::numeric_limits<RealT>::min(),
84 const RealT external_weights = 0.0,
85 const RealT internal_weights = 0.0,
86 const IntT n_internal_faces = 0,
87 const RealT min_edge = std::numeric_limits<RealT>::max(),
88 const RealT diam = std::numeric_limits<RealT>::min(),
89 const std::vector<RealT> sum_centers = std::vector<RealT>{},
90 const std::unordered_map<IndexT, IntT> external_facets =
91 std::unordered_map<IndexT, IntT>{}
112 _sq_diam(std::numeric_limits<RealT>::min()),
116 xyz *= graph->_volumes[index];
134 )
noexcept =
default;
145 template<
unsigned int dim>
147 if constexpr (dim < 2) {
149 }
else if constexpr (dim == 2) {
151 }
else if constexpr (dim == 3) {
167template<
typename IndexT,
typename RealT,
typename IntT>
195 const std::unordered_set<IndexT> &fc_of_cc,
206 std::shared_ptr<Dual_Graph<IndexT, RealT, IntT>>
_graph;
220 const std::unordered_set<IndexT> &fc_of_cc,
221 RealT &shared_weights,
222 std::unordered_set<IndexT> &shared_faces
225 auto n_it = this->_graph->neighbours_cbegin(i_fc);
226 auto w_it = this->_graph->weights_cbegin(i_fc);
227 for (; n_it != this->_graph->neighbours_cend(i_fc); ++n_it, ++w_it) {
228 if (*n_it != i_fc && (fc_of_cc.count(*n_it) != 0)) {
229 shared_faces.insert(*n_it);
230 shared_weights += *w_it;
245 const std::unordered_set<IndexT> &fc_of_cc,
248 const std::vector<RealT> &cen_fc = this->_graph->_centers[i_fc];
250 for (
const auto i_fc_cc : fc_of_cc) {
251 const auto dist = squared_euclidean_distance<RealT>(
252 cen_fc, this->_graph->_centers[i_fc_cc]
254 if (dist > max_diam) max_diam = dist;
255 if (dist < min_edge) min_edge = dist;
272 const std::unordered_set<IndexT> &fc_of_cc,
276 const auto &cur_c = this->_graph->_centers[i_fc];
277 std::vector<RealT> bary(cur_c.size());
278 const RealT ov_N = 1. / (fc_of_cc.size() + 1);
279 for (
auto i =
decltype(cur_c.size()){0}; i < cur_c.size(); ++i) {
280 new_feats.
_sum_centers[i] += this->_graph->_volumes[i_fc] * cur_c[i];
298 template<
bool compute_weights = false,
bool update_facets = false>
302 const std::unordered_set<IndexT> &fc_of_cc,
303 IntT &n_shared_faces,
307 RealT shared_weights{};
308 std::unordered_set<IndexT> shared_faces{};
310 n_shared_faces =
static_cast<IntT
>(shared_faces.size());
312 if constexpr (compute_weights) {
314 + this->_graph->estimated_total_weight(i_fc) - 2 * shared_weights;
317 if constexpr (update_facets) {
319 for (
const auto &face : shared_faces) {
323 this->_graph->get_total_n_faces(i_fc) - n_shared_faces;
336 template<
unsigned int dim>
340 if constexpr (dim < 2) {
341 return sqrt(sqx) / feats.template get_radius<dim>();
342 }
else if constexpr (dim == 2) {
344 }
else if constexpr (dim == 3) {
360template<
typename IndexT,
typename RealT,
typename IntT,
unsigned int dim>
390 const std::unordered_set<IndexT> &fc_of_cc,
397 this->
template update_basic_features<>(
398 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
401 i_fc, cc_feats, fc_of_cc, new_feats
405 this->
template x_over_radius<dim>(new_feats.
_sq_diam, new_feats);
415template<
typename IndexT,
typename RealT,
typename IntT>
444 const std::unordered_set<IndexT> &fc_of_cc,
451 this->
template update_basic_features<>(
452 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
455 aspect_ratio = 1. / new_feats.
_measure;
465template<
typename IndexT,
typename RealT,
typename IntT>
495 const std::unordered_set<IndexT> &fc_of_cc,
502 this->
template update_basic_features<>(
503 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
505 this->update_approximated_geometric_features(
506 i_fc, cc_feats, fc_of_cc, new_feats
511 aspect_ratio = fc_of_cc.size() == 1
528template<
typename IndexT,
typename RealT,
typename IntT,
unsigned int dim>
558 const std::unordered_set<IndexT> &fc_of_cc,
565 this->
template update_basic_features<true>(
566 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
569 aspect_ratio = this->compute_AR(new_feats);
582 if constexpr (dim < 2) {
584 }
else if constexpr (dim == 2) {
587 }
else if constexpr (dim == 3) {
605template<
typename IndexT,
typename RealT,
typename IntT>
633 const std::unordered_set<IndexT> &fc_of_cc,
640 this->
template update_basic_features<>(
641 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
643 this->update_approximated_geometric_features(
644 i_fc, cc_feats, fc_of_cc, new_feats
658template<
typename IndexT,
typename RealT,
typename IntT>
688 const std::unordered_set<IndexT> &fc_of_cc,
695 this->
template update_basic_features<true>(
696 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
709template<
typename IndexT,
typename RealT,
typename IntT>
739 const std::unordered_set<IndexT> &fc_of_cc,
746 this->
template update_basic_features<true>(
747 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
763template<
typename IndexT,
typename RealT,
typename IntT,
unsigned int dim>
797 const std::unordered_set<IndexT> &fc_of_cc,
804 this->
template update_basic_features<>(
805 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
808 this->compute_and_update_barycenter(i_fc, cc_feats, fc_of_cc, new_feats);
811 squared_euclidean_distance<RealT>(bary, this->_graph->_centers[i_fc]);
812 RealT max_dist = dist_fc;
813 for (
const auto i_fc_cc : fc_of_cc) {
814 const auto dist = squared_euclidean_distance<RealT>(
815 bary, this->_graph->_centers[i_fc_cc]
817 if (dist > max_dist) max_dist = dist;
819 aspect_ratio = this->
template x_over_radius<dim>(max_dist, new_feats);
831template<
typename IndexT,
typename RealT,
typename IntT>
841 const RealT tolerance
843 ARComputer<IndexT, RealT, IntT>(graph), _tolerance(tolerance){};
864 const std::unordered_set<IndexT> &fc_of_cc,
871 this->
template update_basic_features<false, true>(
872 i_fc, cc_feats, fc_of_cc, shared_faces, new_feats
875 this->compute_and_update_barycenter(i_fc, cc_feats, fc_of_cc, new_feats);
877 RealT min_dist = std::numeric_limits<RealT>::max(),
878 max_dist = std::numeric_limits<RealT>::min();
882 const auto dist = sqrt(
883 squared_euclidean_distance<RealT>(bary, this->_graph->_centers[i_fc])
886 if (dist > this->_tolerance) {
887 min_dist = max_dist = dist;
890 for (
const auto i_fc_cc : fc_of_cc) {
892 const auto dist = sqrt(squared_euclidean_distance<RealT>(
893 bary, this->_graph->_centers[i_fc_cc]
895 if (dist > this->_tolerance) {
896 if (dist > max_dist) max_dist = dist;
897 if (dist < min_dist) min_dist = dist;
901 aspect_ratio = max_dist / min_dist;
Similar to a functor, the key point is the method that computes the AR and update the features of a c...
Definition: ARComputer.h:168
RealT constexpr x_over_radius(const RealT sqx, const CellFeatures< IndexT, RealT, IntT > &feats) const
Given a squared quantity and cell features, compute the ratio between the quantity and the radius of ...
Definition: ARComputer.h:337
void update_basic_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &n_shared_faces, CellFeatures< IndexT, RealT, IntT > &new_feats) const
Compute approximated geometric features (e.g., diameter)
Definition: ARComputer.h:299
virtual void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const =0
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
void compute_shared_faces(const IndexT i_fc, const std::unordered_set< IndexT > &fc_of_cc, RealT &shared_weights, std::unordered_set< IndexT > &shared_faces) const
Computes number of shared faces and features of the CC obtained by adding a given fine cell....
Definition: ARComputer.h:218
std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > _graph
Dual_Graph object determining Fine cells graph and hence the connectivity.
Definition: ARComputer.h:206
virtual ~ARComputer()=default
The destructor of the class.
void update_approximated_geometric_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, CellFeatures< IndexT, RealT, IntT > &new_feats) const
Compute approximated geometric features (e.g., diameter)
Definition: ARComputer.h:242
std::vector< RealT > compute_and_update_barycenter(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, CellFeatures< IndexT, RealT, IntT > &new_feats) const
Compute the barycenter and update the features accordingly.
Definition: ARComputer.h:269
ARComputer(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:173
ARComputer. Here, AR is the ratio of the diameter over the smallest edge.
Definition: ARComputer.h:466
ARDiamOverMinEdge(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:471
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:492
~ARDiamOverMinEdge() override=default
Destructor.
ARComputer. Here, AR is the ratio of the diameter over the estimated one (typically,...
Definition: ARComputer.h:361
ARDiamOverRadius(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:366
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:387
~ARDiamOverRadius() override=default
Destructor.
ARComputer. Here, AR is the approximated diameter.
Definition: ARComputer.h:606
~ARDiameter() override=default
Destructor.
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:630
ARDiameter(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:611
ARComputer. Here, AR is the ratio of the external weights over the measure. With dim equal to 2,...
Definition: ARComputer.h:529
RealT constexpr compute_AR(const CellFeatures< IndexT, RealT, IntT > &feats) const
Compute the aspect-ratio.
Definition: ARComputer.h:579
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:555
~ARExternalWeightOverRadius() override=default
Destructor.
ARExternalWeightOverRadius(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:534
ARComputer. Here, AR is the total external weights (that is, from a geometric point of view,...
Definition: ARComputer.h:659
ARExternalWeights(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:664
~ARExternalWeights() override=default
Destructor.
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:685
ARComputer. Here, AR is the ratio of the maximum over minimum distance of the cell centers from the b...
Definition: ARComputer.h:764
ARMaxBaryDistanceOverRadius(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:770
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:794
~ARMaxBaryDistanceOverRadius() override=default
Destructor.
ARComputer. Here, AR is the ratio of the maximum over minimum distance of the cell centers from the b...
Definition: ARComputer.h:832
ARMaxOverMinBaryDistance(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph, const RealT tolerance)
Constructor.
Definition: ARComputer.h:839
RealT _tolerance
Definition: ARComputer.h:906
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:861
~ARMaxOverMinBaryDistance() override=default
Destructor.
ARComputer. Here, AR is one over the internal weights (looking for the minimum leads to the maximizat...
Definition: ARComputer.h:710
~AROverInternalWeights() override=default
Destructor.
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:736
AROverInternalWeights(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:715
ARComputer. Here, AR is the reciprocal of the measure, hence the optimal solution should be the one w...
Definition: ARComputer.h:416
AROverMeasure(std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:421
~AROverMeasure() override=default
Destructor.
void compute_and_update_features(const IndexT i_fc, const CellFeatures< IndexT, RealT, IntT > &cc_feats, const std::unordered_set< IndexT > &fc_of_cc, IntT &shared_faces, RealT &aspect_ratio, CellFeatures< IndexT, RealT, IntT > &new_feats) const override
Computes features of the CC obtained by adding a given fine cell. The features are Aspect-Ratio,...
Definition: ARComputer.h:441
Convenient class containing salient features of a cell. According to to the chosen AR computation (se...
Definition: ARComputer.h:36
CellFeatures(const IndexT index, std::shared_ptr< Dual_Graph< IndexT, RealT, IntT > > graph)
Constructor.
Definition: ARComputer.h:104
RealT _internal_weights
Definition: ARComputer.h:45
IntT _n_internal_faces
Definition: ARComputer.h:48
std::unordered_map< IndexT, IntT > _external_facets
Definition: ARComputer.h:65
CellFeatures & operator=(CellFeatures< IndexT, RealT, IntT > &&other) noexcept=default
Move assignment.
RealT _external_weights
Definition: ARComputer.h:42
CellFeatures(const CellFeatures< IndexT, RealT, IntT > &other)=default
Copy constructor.
RealT _measure
Definition: ARComputer.h:39
CellFeatures & operator=(const CellFeatures< IndexT, RealT, IntT > &other)=default
Copy assignment.
CellFeatures(CellFeatures< IndexT, RealT, IntT > &&other) noexcept=default
Move constructor.
RealT _sq_diam
Definition: ARComputer.h:58
std::vector< RealT > _sum_centers
Definition: ARComputer.h:62
RealT constexpr get_radius() const
Compute the radius of a cell.
Definition: ARComputer.h:146
CellFeatures(const RealT measure=std::numeric_limits< RealT >::min(), const RealT external_weights=0.0, const RealT internal_weights=0.0, const IntT n_internal_faces=0, const RealT min_edge=std::numeric_limits< RealT >::max(), const RealT diam=std::numeric_limits< RealT >::min(), const std::vector< RealT > sum_centers=std::vector< RealT >{}, const std::unordered_map< IndexT, IntT > external_facets=std::unordered_map< IndexT, IntT >{})
Constructor.
Definition: ARComputer.h:82
RealT _sq_min_edge
Definition: ARComputer.h:53
A class implementing the CRS global graph representation of the global mesh.
Definition: Dual_Graph.h:548
Definition: Agglomerator.h:37
T constexpr _cb(const T x)
Cube.
Definition: Util.h:61
T constexpr _sq(const T x)
Square.
Definition: Util.h:51
T constexpr int_power(const T x)
Raise a quantity to an integer power.
Definition: Util.h:72