Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
mesh_base.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Francesco Rosnati <francesco.rosnati@polimi.it>
22// Author: Matteo Caldana <matteo.caldana@polimi.it>
23// Author: Pedro Peixoto <ppeixoto@usp.br>
24
25#ifndef VULPES_GEOMETRY_MESH_MESH_BASE_HPP_
26#define VULPES_GEOMETRY_MESH_MESH_BASE_HPP_
27
28#include <algorithm>
29#include <cstddef>
30#include <cstdint>
31#include <fstream>
32#include <iostream>
33#include <map>
34#include <stdexcept>
35#include <string>
36#include <string_view>
37#include <unordered_map>
38#include <utility>
39#include <vector>
40
41#include <Eigen/Dense>
42
43#include <boost/unordered/unordered_flat_set.hpp>
44
45#include <vtkAbstractArray.h>
46#include <vtkAppendFilter.h>
47#include <vtkCell.h>
48#include <vtkCellArray.h>
49#include <vtkCellData.h>
50#include <vtkDataArray.h>
51#include <vtkFieldData.h>
52#include <vtkFloatArray.h>
53#include <vtkIdList.h>
54#include <vtkNew.h>
55#include <vtkPointData.h>
56#include <vtkPoints.h>
57#include <vtkPolyData.h>
58#include <vtkPolyDataReader.h>
59#include <vtkPolygon.h>
60#include <vtkPolyhedron.h>
61#include <vtkSmartPointer.h>
62#include <vtkType.h>
63#include <vtkUnstructuredGrid.h>
64#include <vtkXMLUnstructuredGridReader.h>
65#include <vtkXMLUnstructuredGridWriter.h>
66
74
75namespace vulpes {
76namespace geometry {
77
78namespace internal {
79
101template <typename Dst, typename Src>
102inline constexpr auto cast(const datastructure::CrsMap<Src, Src>& map) {
103 if constexpr (std::is_same_v<Dst, Src>) {
104 return map;
105 } else {
106 using Vec = std::vector<Dst>;
108 Vec{map.data().begin(), map.data().end()},
109 Vec{map.indices().begin(), map.indices().end()});
110 }
111}
112
131template <typename VecType>
132void checkEmptySets(const VecType& labels) {
133 std::vector<bool> class_found;
134 for (size_t i = 0; i < labels.size(); i++) {
135 if (class_found.size() <= labels[i]) {
136 class_found.resize(labels[i] + 1, false);
137 }
138 class_found[labels[i]] = true;
139 }
140 for (size_t i = 0; i < class_found.size(); i++) {
141 if (!class_found[i]) {
142 std::cerr << "WARNING: The class " << i << " of " << class_found.size()
143 << " is empty!" << std::endl;
144 }
145 }
146}
147
163template <typename index_t>
166 const std::vector<index_t>& subset_idxs) {
168 using IndVec = std::vector<index_t>;
169
170 // sort subset_idxs
171 IndVec tmp;
172 const IndVec* subset_idxs_sorted = &subset_idxs;
173 if (!std::is_sorted(subset_idxs.begin(), subset_idxs.end())) {
174 tmp = subset_idxs;
175 std::sort(tmp.begin(), tmp.end());
176 subset_idxs_sorted = &tmp;
177 }
178
179 // compute the old to new cell id map
180 index_t n_nodes = dual_map.size();
181 IndVec old_to_new(n_nodes, -1);
182 const index_t n_subset_idxs = subset_idxs.size();
183 for (index_t new_id = 0; new_id < n_subset_idxs; ++new_id) {
184 old_to_new[(*subset_idxs_sorted)[new_id]] = new_id;
185 }
186
187 // extract the dual map of the subset directly in compressed form!
188 IndMap sub_dual_map;
189 sub_dual_map.data().resize(0);
190 sub_dual_map.indices().resize(0);
191 sub_dual_map.indices().push_back(0);
192 for (index_t ncid = 0; ncid < n_subset_idxs; ++ncid) {
193 const auto ocid = (*subset_idxs_sorted)[ncid];
194 for (const auto neigh_ocid : dual_map[ocid]) {
195 const auto it = std::lower_bound(
196 (*subset_idxs_sorted).begin(), (*subset_idxs_sorted).end(),
197 neigh_ocid);
198 if (it != (*subset_idxs_sorted).end() && (*it) == neigh_ocid) {
199 sub_dual_map.data().push_back(old_to_new[neigh_ocid]);
200 }
201 }
202 sub_dual_map.indices().push_back(sub_dual_map.data().size());
203 }
204 return sub_dual_map;
205};
206
222template <typename index_t>
223index_t removeEmptySets(std::vector<index_t>& labels) {
224 constexpr auto npos = static_cast<index_t>(-1);
225 if (labels.empty()) {
226 return 0;
227 }
228 // NOTE: we need much less memory than labels.size()
229 // we assume that few labels are missing
230 const index_t max_label_id = *std::max_element(labels.begin(), labels.end());
231 std::vector<index_t> old_to_new(max_label_id + 1, npos);
232 index_t new_id = 0;
233 for (std::size_t i = 0; i < labels.size(); i++) {
234 if (old_to_new[labels[i]] == npos) {
235 old_to_new[labels[i]] = new_id++;
236 }
237 labels[i] = old_to_new[labels[i]];
238 }
239 return new_id;
240}
241
254template <typename IndexType>
256 vtkFieldData* field_data,
257 std::string_view association,
258 std::unordered_map<std::string, std::vector<IndexType>>& tags) {
259 const vtkIdType n_arrays = field_data->GetNumberOfArrays();
260 for (vtkIdType i = 0; i < n_arrays; ++i) {
261 vtkAbstractArray* abstract_array = field_data->GetAbstractArray(i);
262 const char* raw_name =
263 abstract_array == nullptr ? nullptr : abstract_array->GetName();
264 const std::string array_name =
265 raw_name != nullptr && *raw_name != '\0'
266 ? raw_name
267 : "<unnamed array at index " + std::to_string(i) + ">";
268
269 vtkDataArray* data_array = vtkDataArray::SafeDownCast(abstract_array);
270 if (data_array == nullptr) {
271 throw std::runtime_error(
272 "Cannot import " + std::string(association) + " mesh-tag array \"" +
273 array_name + "\": the array is nonnumeric");
274 }
275
276 const int n_components = data_array->GetNumberOfComponents();
277 if (n_components != 1) {
278 // TODO: Support multicomponent mesh tags once the mesh-tag
279 // representation can retain all components.
280 std::cerr << "WARNING: Skipping " << association << " mesh-tag array \""
281 << array_name << "\" because it has " << n_components
282 << " components; only scalar arrays are supported.\n";
283 continue;
284 }
285
286 const int data_type = data_array->GetDataType();
287 if (data_type == VTK_FLOAT || data_type == VTK_DOUBLE) {
288 std::cerr << "WARNING: Converting floating-point " << association
289 << " mesh-tag array \"" << array_name
290 << "\" to an integer type; fractional values may be "
291 "truncated.\n";
292 }
293
294 auto& tag = tags[array_name];
295 const vtkIdType n_tuples = data_array->GetNumberOfTuples();
296 tag.reserve(static_cast<std::size_t>(n_tuples));
297 for (vtkIdType j = 0; j < n_tuples; ++j) {
298 tag.push_back(static_cast<IndexType>(data_array->GetComponent(j, 0)));
299 }
300 }
301}
302} // namespace internal
303
313template <typename FloatType, typename IndexType>
314struct Metadata {
316 using MetadataLabel = std::string;
318 std::unordered_map<MetadataLabel, std::vector<FloatType>> doubles;
320 std::unordered_map<MetadataLabel, std::vector<IndexType>> ints;
321};
322
332template <std::size_t Dim, typename IndexType>
333struct MeshTag {
335 using TagLabel = std::string;
337 using TagType = IndexType;
339 using TagTable = std::unordered_map<TagLabel, std::vector<TagType>>;
342 std::array<TagTable, Dim + 1> tags;
343};
344
364template <
365 typename FloatType,
366 typename IndexType,
367 IndexType OuterDim,
368 IndexType SelfDim,
369 typename Derived>
370class MeshBase {
371 static_assert(SelfDim > 0 && SelfDim <= 3);
372 static_assert(OuterDim > 0 && OuterDim <= 3);
373
374 public:
376 static constexpr IndexType outer_dim = OuterDim;
378 static constexpr IndexType self_dim = SelfDim;
381 static constexpr IndexType vtx_per_face = self_dim <= 2 ? self_dim : -1;
384 static constexpr bool is_manifold = !(outer_dim == self_dim);
386 static inline constexpr IndexType UNDEF = static_cast<IndexType>(-1);
387
388 //
389 // Type Aliases for clarity
390 //
391 using index_t = IndexType;
392 using float_t = FloatType;
406 using FacetToCellMap = std::conditional_t<
409 std::vector<std::array<IndexType, 2>>>;
416 using EdgeVtxs = std::array<IndexType, 2>;
418 using EdgeToVtxMap = std::vector<EdgeVtxs>;
423
428 enum Entity {
432 FacetEnty = SelfDim - 1,
434 CellEnty = SelfDim,
435 };
436
437 //
438 // Getters for accessing mesh data
439 //
440
442 std::vector<SelfPoint>& getVertexes() { return m_vertexes; }
444 const std::vector<SelfPoint>& getVertexes() const { return m_vertexes; }
445
451 const DualMap& getDualMap() const { return m_dual_graph; }
452
457
461 const CellToVtxMap& getCellToVtxMap() const { return m_cell_to_vtx; }
462
467
472
476 const VtxToCellMap& getVtxToCellMap() const { return m_vtx_to_cell; }
477
478 // These are aliases for the regular getters, used in parallel (distributed
479 // memory) context
480 const auto& getDualGlobalMap() const { return m_dual_graph; }
481 const auto& getCellToFacetGlobalMap() const { return m_cell_to_facet; }
482 const auto& getCellToVtxGlobalMap() const { return m_cell_to_vtx; }
483 const auto& getFacetToCellGlobalMap() const { return m_facet_to_cell; }
484 const auto& getFacetToVtxGlobalMap() const { return m_facet_to_vtx; }
485 const auto& getVtxToCellGlobalMap() const { return m_vtx_to_cell; }
486
488 auto& getMeshTags() { return m_mesh_tags; }
490 const auto& getMeshTags() const { return m_mesh_tags; }
491
493 auto& getMetadata() { return m_metadata; }
495 const auto& getMetadata() const { return m_metadata; }
496
502 IndexType numCells() const {
503 assert(m_cell_to_facet.size() == m_dual_graph.size());
504 assert(m_cell_to_facet.size() == m_cell_to_vtx.size());
505 return m_cell_to_facet.size();
506 }
507
519
527 constexpr IndexType nEntity(std::size_t dim) const {
528 return self().nEntity(dim);
529 }
530
538 constexpr IndexType nLocEntity(std::size_t dim) const {
539 return self().nEntity(dim);
540 }
541
549 template <std::size_t dim>
550 constexpr auto& getEntityToCellMap() const {
551 return self().template getEntityToCellMap<dim>();
552 }
553
561 template <std::size_t dim>
562 constexpr auto& getEntityToCellGlobalMap() const {
563 return self().template getEntityToCellMap<dim>();
564 }
565
570 constexpr IndexType nOwnedCells() const {
571 return self().nLocEntity(self_dim);
572 }
573
578 constexpr static inline bool parallel() { return false; }
579
586 template <class Archive>
587 void serialize(Archive& ar, const unsigned int /*version*/) {
588 ar & m_vertexes;
589 ar & m_dual_graph;
590 ar & m_cell_to_facet;
591 ar & m_cell_to_vtx;
592 ar & m_facet_to_cell;
593 ar & m_facet_to_vtx;
594 ar & m_vtx_to_cell;
595 // TODO serialize tags and metadata
596 // ar & m_mesh_tags;
597 // ar & m_metadata;
598 }
599
607 friend bool operator==(const MeshBase& lhs, const MeshBase& rhs) {
608 return (lhs.m_vertexes == rhs.m_vertexes) &&
609 (lhs.m_dual_graph == rhs.m_dual_graph) &&
610 (lhs.m_cell_to_facet == rhs.m_cell_to_facet) &&
611 (lhs.m_cell_to_vtx == rhs.m_cell_to_vtx) &&
612 (lhs.m_facet_to_cell == rhs.m_facet_to_cell) &&
613 (lhs.m_facet_to_vtx == rhs.m_facet_to_vtx) &&
614 (lhs.m_vtx_to_cell == rhs.m_vtx_to_cell);
615 // TODO: add comparison with respect to tags and metadata
616 // (lhs.m_mesh_tags == rhs.m_mesh_tags) &&
617 // (lhs.m_metadata.doubles == rhs.m_metadata.doubles) &&
618 // (lhs.m_metadata.ints == rhs.m_metadata.ints);
619 }
620
621 protected:
624 const auto& self() const { return static_cast<const Derived&>(*this); }
627 auto& self() { return static_cast<Derived&>(*this); }
628
638 const std::vector<index_t>& old_to_new_cell) const;
639
651 const auto& new_to_old_cell, Derived& agg_mesh) const;
652
662 const std::vector<index_t>& new_to_old_facet,
663 const std::vector<index_t>& old_to_new_cell,
664 Derived& agg_mesh) const;
665
674 const auto& new_to_old_cell,
675 const auto& old_to_new_vtx,
676 Derived& agg_mesh) const;
677
689 const std::vector<index_t>& old_to_new_cell,
690 bool check_connected = true) const;
691
702 const datastructure::CrsMap<IndexType, IndexType>& new_to_old_cell) const;
703
709
715
723
724 //
725 // Core Data Members
726 //
728 std::vector<SelfPoint> m_vertexes;
752};
753
754template <
755 typename FloatType,
756 typename IndexType,
757 IndexType OuterDim,
758 IndexType SelfDim,
759 typename Derived>
763 const std::vector<IndexType>& old_to_new_cell) const {
765 for (std::size_t cell_id = 0; cell_id < old_to_new_cell.size(); ++cell_id) {
766 const auto cell_id_new = old_to_new_cell[cell_id];
767 if (cell_id_new == UNDEF) {
768 continue;
769 }
770 new_to_old_cell_uncompr.getRow(cell_id_new).push_back(cell_id);
771 }
772 return new_to_old_cell_uncompr.template compress<IndexType>();
773}
774
775template <
776 typename FloatType,
777 typename IndexType,
778 IndexType OuterDim,
779 IndexType SelfDim,
780 typename Derived>
781std::vector<IndexType>
784 const auto& new_to_old_cell, Derived& agg_mesh) const {
785 std::vector<IndexType> new_to_old_facet;
786 std::vector<IndexType> old_to_new_facet(m_facet_to_cell.size(), -1);
788 new_cell_to_facets_uncpr.resize(new_to_old_cell.size());
789 // NOTE: a flat vector of booleans here performed much worse when tested
790 boost::unordered_flat_map<IndexType, IndexType> facet_table;
791 for (IndexType new_cid = 0; new_cid < new_to_old_cell.size(); ++new_cid) {
792 for (const auto old_cid : new_to_old_cell[new_cid]) {
793 for (const auto old_fid : m_cell_to_facet[old_cid]) {
794 // we only care if the count is only exactly 1
795 // it can be only 0 [face does not belong to cell]
796 // 1 [face is on boundary]
797 // 2 [face is internal] -> not(not(0)) == 0
798 facet_table[old_fid] = !facet_table[old_fid];
799 }
800 }
801 for (auto it = facet_table.begin(); it != facet_table.end(); ++it) {
802 const auto count = it->second;
803 const auto old_fid = it->first;
804 if (count) {
805 IndexType new_fid = old_to_new_facet[old_fid];
806 if (new_fid == UNDEF) {
807 new_fid = new_to_old_facet.size();
808 old_to_new_facet[old_fid] = new_fid;
809 new_to_old_facet.push_back(old_fid);
810 }
811 new_cell_to_facets_uncpr[new_cid].push_back(new_fid);
812 }
813 }
814 facet_table.clear();
815 }
816 agg_mesh.m_cell_to_facet =
817 new_cell_to_facets_uncpr.template compress<IndexType>();
818 return new_to_old_facet;
819}
820
821template <
822 typename FloatType,
823 typename IndexType,
824 IndexType OuterDim,
825 IndexType SelfDim,
826 typename Derived>
829 const std::vector<IndexType>& new_to_old_facet,
830 const std::vector<IndexType>& old_to_new_cell,
831 Derived& agg_mesh) const {
832 std::vector<IndexType> new_to_old_vtx;
833 std::vector<IndexType> old_to_new_vtx(m_vertexes.size(), UNDEF);
834
835 agg_mesh.m_facet_to_cell.resize(new_to_old_facet.size(), {UNDEF, UNDEF});
836 agg_mesh.m_facet_to_vtx.indices().push_back(0);
837
838 for (std::size_t new_fid = 0; new_fid < new_to_old_facet.size(); ++new_fid) {
839 const auto old_fid = new_to_old_facet[new_fid];
840 // face to cell
841 for (std::size_t d = 0; d < 2; ++d) {
842 const auto old_cid = m_facet_to_cell[old_fid][d];
843 if (old_cid != UNDEF) {
844 const auto new_cid = old_to_new_cell[old_cid];
845 assert(new_cid != UNDEF);
846 agg_mesh.m_facet_to_cell[new_fid][d] = new_cid;
847 }
848 }
849
850 // facet to vertex and vertex to vertex (old/new)
851 for (const auto old_vid : m_facet_to_vtx[old_fid]) {
852 IndexType new_vid = old_to_new_vtx[old_vid];
853 if (new_vid == UNDEF) {
854 new_vid = new_to_old_vtx.size();
855 new_to_old_vtx.push_back(old_vid);
856 old_to_new_vtx[old_vid] = new_vid;
857 }
858 agg_mesh.m_facet_to_vtx.data().push_back(new_vid);
859 }
860 if constexpr (vtx_per_face == IndexType(-1)) {
861 agg_mesh.m_facet_to_vtx.indices().push_back(
862 agg_mesh.m_facet_to_vtx.data().size());
863 }
864 }
865
866 // add vtxs
867 for (std::size_t ovid = 0; ovid < old_to_new_vtx.size(); ++ovid) {
868 const auto nvid = old_to_new_vtx[ovid];
869 if (nvid != UNDEF) {
870 if (agg_mesh.m_vertexes.size() < static_cast<std::size_t>(nvid + 1)) {
871 agg_mesh.m_vertexes.resize(nvid + 1);
872 }
873 agg_mesh.m_vertexes[nvid] = m_vertexes[ovid];
874 }
875 }
876
877 return old_to_new_vtx;
878}
879
880template <
881 typename FloatType,
882 typename IndexType,
883 IndexType OuterDim,
884 IndexType SelfDim,
885 typename Derived>
888 const auto& new_to_old_cell,
889 const auto& old_to_new_vtx,
890 Derived& agg_mesh) const {
891 agg_mesh.m_cell_to_vtx.indices().push_back(0);
892 std::vector<bool> is_vtx_present(old_to_new_vtx.size(), false);
893
894 for (IndexType new_cid = 0; new_cid < new_to_old_cell.size(); ++new_cid) {
895 for (const auto old_cid : new_to_old_cell[new_cid]) {
896 for (const auto old_vid : m_cell_to_vtx[old_cid]) {
897 const auto new_vid = old_to_new_vtx[old_vid];
898 if (new_vid != UNDEF && (!is_vtx_present[old_vid])) {
899 agg_mesh.m_cell_to_vtx.data().push_back(new_vid);
900 is_vtx_present[old_vid] = true;
901 }
902 }
903 }
904 std::fill(is_vtx_present.begin(), is_vtx_present.end(), false);
905 agg_mesh.m_cell_to_vtx.indices().push_back(
906 agg_mesh.m_cell_to_vtx.data().size());
907 }
908}
909
910template <
911 typename FloatType,
912 typename IndexType,
913 IndexType OuterDim,
914 IndexType SelfDim,
915 typename Derived>
918 new_to_old_cell) const {
919 // safety check: check each new cell is made by connected components
920 std::vector<IndexType> glob_to_loc(numCells(), UNDEF);
921 IndexType prev_new_cid = UNDEF;
922 for (IndexType new_cid = 0; new_cid < new_to_old_cell.size(); ++new_cid) {
923 if (new_to_old_cell[new_cid].size() <= 1) {
924 continue;
925 }
926 std::vector<IndexType> old_cids(
927 new_to_old_cell[new_cid].begin(), new_to_old_cell[new_cid].end());
928 // overwrite old map entries
929 if (VULPES_UNLIKELY(prev_new_cid != UNDEF)) {
930 for (auto id : new_to_old_cell[prev_new_cid]) {
931 glob_to_loc[id] = UNDEF;
932 }
933 }
934 prev_new_cid = new_cid;
935 // update map
936 for (std::size_t i = 0; i < old_cids.size(); ++i) {
937 glob_to_loc[old_cids[i]] = i;
938 }
939 // dfs to check it's connected
940 std::vector<bool> visited(old_cids.size(), false);
941 std::vector<IndexType> to_visit = {old_cids[0]};
942 while (!to_visit.empty()) {
943 const auto curr = to_visit.back();
944 visited[glob_to_loc[curr]] = true;
945 to_visit.pop_back();
946 for (const auto neigh : m_dual_graph[curr]) {
947 if (glob_to_loc[neigh] == UNDEF) {
948 continue;
949 }
950 if (!visited[glob_to_loc[neigh]]) {
951 to_visit.push_back(neigh);
952 }
953 }
954 }
955 // check all are visited
956 for (std::size_t i = 0; i < visited.size(); ++i) {
957 if (!visited[i]) {
958 std::cout << "ERROR: in new cell " + std::to_string(new_cid) +
959 ", starting from old cell " +
960 std::to_string(old_cids[0]) +
961 " is not possible to reach the old cell " +
962 std::to_string(old_cids[i])
963 << std::endl;
964
965 for (auto cid : {old_cids[0], old_cids[i]}) {
966 SelfPoint avg = SelfPoint::Zero();
967 for (const auto id : m_cell_to_vtx[cid]) {
968 avg += m_vertexes[id];
969 }
970 avg /= m_cell_to_vtx[cid].size();
971 std::cout << "Old cell " << cid << " avg: " << avg.transpose()
972 << std::endl;
973 }
974 throw std::runtime_error("agglomerated cell is not connected");
975 }
976 }
977 }
978}
979
980template <
981 typename FloatType,
982 typename IndexType,
983 IndexType OuterDim,
984 IndexType SelfDim,
985 typename Derived>
986Derived
988 const std::vector<IndexType>& old_to_new_cell_input,
989 bool check_connected) const {
990 auto old_to_new_cell = old_to_new_cell_input;
991 internal::removeEmptySets(old_to_new_cell);
992 assert(old_to_new_cell.size() == static_cast<std::size_t>(numCells()));
993 Derived agg_mesh;
994
995 // invert old to new cell
996 Profiler::start("invertCellAgglomeration");
997 const auto new_to_old_cell = invertCellAgglomeration(old_to_new_cell);
998 Profiler::stop("invertCellAgglomeration");
999
1000 if (check_connected) {
1001 Profiler::start("agglomerateCheckConnected");
1002 agglomerateCheckConnected(new_to_old_cell);
1003 Profiler::stop("agglomerateCheckConnected");
1004 }
1005 // cell to faces and facet to facet (old/new)
1006 Profiler::start("buildNewToOldFacetAgglomerationAndCellToFacet");
1007 const auto new_to_old_facet =
1008 buildNewToOldFacetAgglomerationAndCellToFacet(new_to_old_cell, agg_mesh);
1009 Profiler::stop("buildNewToOldFacetAgglomerationAndCellToFacet");
1010
1011 // face to cell && facet to vertex and vertex to vertex (old/new)
1012 Profiler::start("buildFacetToCellAndFacetToVtxAgglomeration");
1013 const auto old_to_new_vtx = buildFacetToCellAndFacetToVtxAgglomeration(
1014 new_to_old_facet, old_to_new_cell, agg_mesh);
1015 Profiler::stop("buildFacetToCellAndFacetToVtxAgglomeration");
1016
1017 // cell to vtx
1018 Profiler::start("buildCellToVtxAgglomeration");
1019 buildCellToVtxAgglomeration(new_to_old_cell, old_to_new_vtx, agg_mesh);
1020 Profiler::stop("buildCellToVtxAgglomeration");
1021
1022 Profiler::start("agglomeration::buildDualGraph");
1023 agg_mesh.buildDualGraph();
1024 Profiler::stop("agglomeration::buildDualGraph");
1025
1026 Profiler::start("agglomeration::buildVtxToCell");
1027 agg_mesh.buildVtxToCell();
1028 Profiler::stop("agglomeration::buildVtxToCell");
1029
1030 // transport tag
1031 if (!m_mesh_tags.tags[CellEnty].empty()) {
1032 for (const auto& [tag_name, tag] : m_mesh_tags.tags[CellEnty]) {
1033 agg_mesh.m_mesh_tags.tags[CellEnty][tag_name].resize(
1034 new_to_old_cell.size());
1035 for (index_t ncid = 0; ncid < new_to_old_cell.size(); ++ncid) {
1036 const auto row = new_to_old_cell[ncid];
1037 agg_mesh.m_mesh_tags.tags[CellEnty][tag_name][ncid] = tag[row[0]];
1038#ifndef NDEBUG
1039 for (const auto old_cid : row) {
1040 std::cout << "old_cid" << old_cid << " tag " << tag[old_cid]
1041 << " new_cid " << ncid << " tag " << tag[row[0]]
1042 << std::endl;
1043 assert(tag[old_cid] == tag[row[0]]);
1044 }
1045#endif
1046 }
1047 }
1048 }
1049 return agg_mesh;
1050}
1051
1052template <
1053 typename FloatType,
1054 typename IndexType,
1055 IndexType OuterDim,
1056 IndexType SelfDim,
1057 typename Derived>
1061 for (IndexType cell_id = 0; cell_id < m_cell_to_vtx.size(); ++cell_id) {
1062 for (const auto vid : m_cell_to_vtx[cell_id]) {
1063 vtx_to_cell.getRow(vid).push_back(cell_id);
1064 }
1065 }
1066 m_vtx_to_cell = vtx_to_cell.template compress<IndexType>();
1067};
1068
1069template <
1070 typename FloatType,
1071 typename IndexType,
1072 IndexType OuterDim,
1073 IndexType SelfDim,
1074 typename Derived>
1078 const IndexType n_facets = m_facet_to_cell.size();
1079 for (IndexType facet_id = 0; facet_id < n_facets; ++facet_id) {
1080 for (const auto cell_id : m_facet_to_cell[facet_id]) {
1081 // if not on the boundary
1082 if (cell_id != IndexType(-1)) {
1083 cell_to_facet.getRow(cell_id).push_back(facet_id);
1084 }
1085 }
1086 }
1087 m_cell_to_facet = cell_to_facet.template compress<IndexType>();
1088};
1089
1090template <
1091 typename FloatType,
1092 typename IndexType,
1093 IndexType OuterDim,
1094 IndexType SelfDim,
1095 typename Derived>
1099 dual_graph.resize(m_cell_to_facet.size());
1100 for (IndexType cell_id = 0; cell_id < m_cell_to_facet.size(); ++cell_id) {
1101 auto& cell_neighs = dual_graph[cell_id];
1102 for (const auto facet_id : m_cell_to_facet.getRow(cell_id)) {
1103 for (const auto cell_neigh : m_facet_to_cell[facet_id]) {
1104 if ((cell_neigh != UNDEF) && (cell_neigh != cell_id)) {
1105 const auto begin = cell_neighs.begin();
1106 const auto end = cell_neighs.end();
1107 if (std::find(begin, end, cell_neigh) == end) {
1108 cell_neighs.push_back(cell_neigh);
1109 }
1110 }
1111 }
1112 }
1113 assert(
1114 ((m_cell_to_facet.size() == 1) ||
1115 (dual_graph.getRow(cell_id).size() > 0)) &&
1116 "Cell is unconnected");
1117 }
1118 m_dual_graph = dual_graph.template compress<IndexType>();
1119
1120 if (m_cell_to_facet.size() == 1) {
1121 if (m_dual_graph.size() == 0) {
1122 m_dual_graph.indices().resize(2, 0);
1123 }
1124 }
1125}
1126
1127} // namespace geometry
1128} // namespace vulpes
1129
1130#endif // VULPES_GEOMETRY_MESH_MESH_BASE_HPP_
static void stop(const char *label)
stop(label) when recording is on.
Definition profiler.hpp:90
static void start(const char *label)
start(label) when recording is on.
Definition profiler.hpp:84
A class that behaves like a vector of vectors, providing automatic row range checking.
Definition crs_map.hpp:758
constexpr void resize(std::size_t new_size)
Resizes the map to contain the specified number of SmallVector objects.
Definition crs_map.hpp:807
constexpr void push_back(const SmallVector< DataType, N > &value)
Appends a copy of a SmallVector to the map.
Definition crs_map.hpp:830
SmallVector< DataType, N > & getRow(std::size_t row_idx)
Access a row of the matrix as a mutable reference.
Definition crs_map.hpp:857
Definition crs_map.hpp:72
IndexType size() const
Get the number of rows in the CRS map.
Definition crs_map.hpp:331
const auto & indices() const
Constant accessor to the indices buffer.
Definition crs_map.hpp:442
const auto & data() const
Constant accessor to the data buffer.
Definition crs_map.hpp:438
A base class for creating and managing mesh data structures.
Definition mesh_base.hpp:370
void buildCellToVtxAgglomeration(const auto &new_to_old_cell, const auto &old_to_new_vtx, Derived &agg_mesh) const
Builds the cell-to-vertex map for an agglomerated mesh.
Definition mesh_base.hpp:887
const VtxToCellMap & getVtxToCellMap() const
Gets a constant reference to the vertex-to-cell map.
Definition mesh_base.hpp:476
constexpr auto & getEntityToCellMap() const
Gets the map from a given entity dimension to cells.
Definition mesh_base.hpp:550
FacetToCellMap & getFacetToCellMap()
Gets a modifiable reference to the facet-to-cell map.
Definition mesh_base.hpp:464
Derived agglomerateBase(const std::vector< index_t > &old_to_new_cell, bool check_connected=true) const
Creates a new, coarser mesh by grouping cells together.
Definition mesh_base.hpp:987
void buildDualGraph()
Builds the dual graph (cell-to-cell adjacency).
Definition mesh_base.hpp:1097
datastructure::CrsMap< IndexType, IndexType > CellToVtxMap
Definition mesh_base.hpp:399
datastructure::CrsMap< IndexType, IndexType > DualMap
Definition mesh_base.hpp:395
Metadata< FloatType, IndexType > m_metadata
Definition mesh_base.hpp:751
const auto & getCellToFacetGlobalMap() const
Definition mesh_base.hpp:481
const FacetToCellMap & getFacetToCellMap() const
Gets a constant reference to the facet-to-cell map.
Definition mesh_base.hpp:466
const CellToFacetMap & getCellToFacetMap() const
Gets a constant reference to the cell-to-facet map.
Definition mesh_base.hpp:456
VtxToCellMap & getVtxToCellMap()
Gets a modifiable reference to the vertex-to-cell map.
Definition mesh_base.hpp:474
datastructure::CrsMap< IndexType, IndexType > CellToFacetMap
Definition mesh_base.hpp:397
const auto & getMetadata() const
Gets a constant reference to the mesh metadata.
Definition mesh_base.hpp:495
const auto & getMeshTags() const
Gets a constant reference to the mesh tags.
Definition mesh_base.hpp:490
Entity
Enumerates the different types of mesh entities based on their dimension.
Definition mesh_base.hpp:428
MeshTag< SelfDim, IndexType > m_mesh_tags
Definition mesh_base.hpp:749
const auto & self() const
Helper to get a constant reference to the Derived class instance.
Definition mesh_base.hpp:624
const auto & getCellToVtxGlobalMap() const
Definition mesh_base.hpp:482
CellToFacetMap & getCellToFacetMap()
Gets a modifiable reference to the cell-to-facet map.
Definition mesh_base.hpp:454
datastructure::CrsMap< IndexType, IndexType > VtxToCellMap
Definition mesh_base.hpp:414
const CellToVtxMap & getCellToVtxMap() const
Gets a constant reference to the cell-to-vertex map.
Definition mesh_base.hpp:461
datastructure::CrsMap< IndexType, IndexType > EdgeToCellMap
Definition mesh_base.hpp:422
DualMap & getDualMap()
Gets a modifiable reference to the dual graph (cell-to-cell connections).
Definition mesh_base.hpp:448
const FacetToVtxMap & getFacetToVtxMap() const
Gets a constant reference to the facet-to-vertex map.
Definition mesh_base.hpp:471
auto & self()
Helper to get a reference to the Derived class instance.
Definition mesh_base.hpp:627
void serialize(Archive &ar, const unsigned int)
Serializes the mesh data.
Definition mesh_base.hpp:587
datastructure::CrsMap< IndexType, IndexType > CellToEdgeMap
Definition mesh_base.hpp:420
std::vector< index_t > buildNewToOldFacetAgglomerationAndCellToFacet(const auto &new_to_old_cell, Derived &agg_mesh) const
Builds the facet mapping and cell-to-facet map for an agglomerated mesh.
Definition mesh_base.hpp:783
constexpr IndexType nLocEntity(std::size_t dim) const
Returns the number of locally owned entities of a given dimension.
Definition mesh_base.hpp:538
friend bool operator==(const MeshBase &lhs, const MeshBase &rhs)
Checks if two meshes are identical.
Definition mesh_base.hpp:607
std::vector< SelfPoint > & getVertexes()
Gets a modifiable reference to the vector of vertices.
Definition mesh_base.hpp:442
const auto & getFacetToVtxGlobalMap() const
Definition mesh_base.hpp:484
constexpr auto & getEntityToCellGlobalMap() const
Gets the global map from a given entity dimension to cells.
Definition mesh_base.hpp:562
void agglomerateCheckConnected(const datastructure::CrsMap< IndexType, IndexType > &new_to_old_cell) const
Checks if each new agglomerated cell consists of connected old cells.
Definition mesh_base.hpp:917
const auto & getDualGlobalMap() const
Definition mesh_base.hpp:480
void buildCellToFacet()
Builds the cell-to-facet connectivity map.
Definition mesh_base.hpp:1076
constexpr IndexType nEntity(std::size_t dim) const
Returns the total number of entities of a given dimension.
Definition mesh_base.hpp:527
FacetToVtxMap & getFacetToVtxMap()
Gets a modifiable reference to the facet-to-vertex map.
Definition mesh_base.hpp:469
auto & getMeshTags()
Gets a modifiable reference to the mesh tags.
Definition mesh_base.hpp:488
const std::vector< SelfPoint > & getVertexes() const
Gets a constant reference to the vector of vertices.
Definition mesh_base.hpp:444
CellToVtxMap & getCellToVtxMap()
Gets a modifiable reference to the cell-to-vertex map.
Definition mesh_base.hpp:459
auto buildFacetToCellAndFacetToVtxAgglomeration(const std::vector< index_t > &new_to_old_facet, const std::vector< index_t > &old_to_new_cell, Derived &agg_mesh) const
Builds the facet-to-cell and facet-to-vertex maps for an agglomerated mesh.
Definition mesh_base.hpp:828
datastructure::CrsMap< IndexType, IndexType, vtx_per_face > FacetToVtxMap
Definition mesh_base.hpp:411
const auto & getFacetToCellGlobalMap() const
Definition mesh_base.hpp:483
std::conditional_t< is_manifold, datastructure::CrsMap< IndexType, IndexType >, std::vector< std::array< IndexType, 2 > > > FacetToCellMap
Definition mesh_base.hpp:406
auto & getMetadata()
Gets a modifiable reference to the mesh metadata.
Definition mesh_base.hpp:493
IndexType numCells() const
Returns the total number of cells in the mesh.
Definition mesh_base.hpp:502
const DualMap & getDualMap() const
Gets a constant reference to the dual graph (cell-to-cell connections).
Definition mesh_base.hpp:451
const auto & getVtxToCellGlobalMap() const
Definition mesh_base.hpp:485
constexpr IndexType nOwnedCells() const
Returns the number of locally owned cells.
Definition mesh_base.hpp:570
void buildInternalMaps()
Builds all the secondary connectivity maps from the primary ones.
Definition mesh_base.hpp:514
datastructure::CrsMap< index_t, index_t > invertCellAgglomeration(const std::vector< index_t > &old_to_new_cell) const
Creates a map from new (agglomerated) cell IDs to old (fine) cell IDs.
Definition mesh_base.hpp:762
datastructure::CrsMapUncompressed< IndexType > CellToVtxMapUncompressed
Definition mesh_base.hpp:401
static constexpr bool parallel()
Indicates if the mesh is configured for parallel computation.
Definition mesh_base.hpp:578
void buildVtxToCell()
Builds the vertex-to-cell connectivity map.
Definition mesh_base.hpp:1059
#define VULPES_UNLIKELY(x)
Branch hint: x is expected to evaluate to false.
Definition macros.hpp:41
Helpers implementing VulPoly's Voronoi construction and cleanup.
Definition abs_agg_model.cpp:42
datastructure::CrsMap< index_t, index_t > buildSubDual(const datastructure::CrsMap< index_t, index_t > &dual_map, const std::vector< index_t > &subset_idxs)
Extracts the dual subgraph corresponding to a subset of vertices.
Definition mesh_base.hpp:164
void importVtkMeshTags(vtkFieldData *field_data, std::string_view association, std::unordered_map< std::string, std::vector< IndexType > > &tags)
Imports scalar numeric VTK arrays into an integer mesh-tag table.
Definition mesh_base.hpp:255
index_t removeEmptySets(std::vector< index_t > &labels)
Compresses labels to a contiguous range [0, N-1].
Definition mesh_base.hpp:223
constexpr auto cast(const datastructure::CrsMap< Src, Src > &map)
Re-types a CRS map to use a different integral type for indices and values.
Definition mesh_base.hpp:102
void checkEmptySets(const VecType &labels)
Checks for and warns about empty classes in a label vector.
Definition mesh_base.hpp:132
Definition abs_agg_model.cpp:40
Eigen::Matrix< DataType, Dim, 1, Eigen::ColMajor > Point
Definition traits.hpp:34
Definition cell_manager.hpp:38
A structure to store tags for different parts of a mesh.
Definition mesh_base.hpp:333
std::string TagLabel
< The name of the tag (e.g., "inlet_boundary").
Definition mesh_base.hpp:335
std::unordered_map< TagLabel, std::vector< TagType > > TagTable
An array of tag tables, one for each dimension from 0 (vertices) to Dim (cells).
Definition mesh_base.hpp:339
IndexType TagType
A table to store, for each tag name, a list of tag values.
Definition mesh_base.hpp:337
std::array< TagTable, Dim+1 > tags
Definition mesh_base.hpp:342
A structure to store global metadata.
Definition mesh_base.hpp:314
std::unordered_map< MetadataLabel, std::vector< IndexType > > ints
Definition mesh_base.hpp:320
std::unordered_map< MetadataLabel, std::vector< FloatType > > doubles
Maps metadata name to vector of integer values.
Definition mesh_base.hpp:318
std::string MetadataLabel
< The name of the metadata field (e.g., "XPeriod", "NumCells").
Definition mesh_base.hpp:316