Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
mesh_3d.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: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_GEOMETRY_MESH_MESH_3D_HPP_
25#define VULPES_GEOMETRY_MESH_MESH_3D_HPP_
26
27#include <algorithm>
28#include <cstddef>
29#include <cstdint>
30#include <fstream>
31#include <iostream>
32#include <map>
33#include <stdexcept>
34#include <string>
35#include <unordered_map>
36#include <utility>
37#include <vector>
38
39#include <Eigen/Dense>
40
41#include <vtkCell.h>
42#include <vtkCellArray.h>
43#include <vtkCellData.h>
44#include <vtkFloatArray.h>
45#include <vtkIdList.h>
46#include <vtkNew.h>
47#include <vtkPointData.h>
48#include <vtkPoints.h>
49#include <vtkPolyData.h>
50#include <vtkPolyDataReader.h>
51#include <vtkPolygon.h>
52#include <vtkPolyhedron.h>
53#include <vtkSmartPointer.h>
54#include <vtkType.h>
55#include <vtkUnstructuredGrid.h>
56#include <vtkXMLUnstructuredGridReader.h>
57#include <vtkXMLUnstructuredGridWriter.h>
58
64
65namespace vulpes {
66namespace geometry {
67
76template <typename FloatType, typename IndexType>
77class Mesh3D : public MeshBase<
78 FloatType,
79 IndexType,
80 3,
81 3,
82 Mesh3D<FloatType, IndexType>> {
83 public:
91 using BaseType =
93 using typename BaseType::CellToVtxMap;
95
104 using CellsFaceVtxsUncompressed = std::vector<std::vector<IndexType>>;
105 using typename BaseType::CellToEdgeMap;
106 using typename BaseType::EdgeToCellMap;
107 using typename BaseType::EdgeToVtxMap;
108 using typename BaseType::EdgeVtxs;
109
113 Mesh3D() = default;
114
124 static Mesh3D extrude(
125 const Mesh2D<FloatType, IndexType>& mesh_2d,
126 const Eigen::Vector<FloatType, 2> z_range,
127 IndexType n_elems);
128
139 explicit Mesh3D(
140 const std::vector<SelfPoint>& vertexes,
141 const CellToVtxMapUncompressed& cell_vertices,
142 const CellsFaceVtxsUncompressed& cells_face_vtxs) {
143 this->m_vertexes = vertexes;
144 this->m_cell_to_vtx = cell_vertices.template compress<IndexType>();
145 buildFacetToCellAndFacetToVertex(cells_face_vtxs);
146 this->buildInternalMaps();
148 }
149
161 explicit Mesh3D(
162 const std::vector<SelfPoint>& vertexes,
163 const CellToVtxMapUncompressed& cell_vertices,
164 const CellsFaceVtxsUncompressed& cells_face_vtxs,
165 const MeshTag<3, IndexType>& mesh_tags)
166 : Mesh3D(vertexes, cell_vertices, cells_face_vtxs) {
167 this->m_mesh_tags = mesh_tags;
168 }
169
176 template <class Archive>
177 void serialize(Archive& ar, const unsigned int) {
178 ar& static_cast<BaseType&>(*this);
179 ar & m_cell_to_edge;
180 ar & m_edge_to_cell;
181 ar & m_edge_to_vtx;
182 }
183
191 friend bool operator==(const Mesh3D& lhs, const Mesh3D& rhs) {
192 return (static_cast<const BaseType&>(lhs) ==
193 static_cast<const BaseType&>(rhs)) &&
194 (lhs.m_cell_to_edge == rhs.m_cell_to_edge) &&
195 (lhs.m_edge_to_cell == rhs.m_edge_to_cell) &&
196 (lhs.m_edge_to_vtx == rhs.m_edge_to_vtx);
197 }
198
208 const EdgeToVtxMap& getEdgeToVtxMap() const { return m_edge_to_vtx; }
209
220
231
236 const auto& getEdgeToVtxGlobalMap() const { return m_edge_to_vtx; }
241 const auto& getCellToEdgeGlobalMap() const { return m_cell_to_edge; }
246 const auto& getEdgeToCellGlobalMap() const { return m_edge_to_cell; }
247
255 constexpr IndexType nEntity(std::size_t dim) const {
256 if (dim == 0) {
257 return this->getVertexes().size();
258 } else if (dim == 1) {
259 return this->getEdgeToCellMap().size();
260 } else if (dim == 2) {
261 return this->getFacetToCellMap().size();
262 } else if (dim == 3) {
263 assert(this->m_cell_to_facet.size() == this->m_dual_graph.size());
264 return this->getDualMap().size();
265 } else {
266 throw std::invalid_argument("invalid entity dimension");
267 }
268 }
269
277 template <std::size_t dim>
278 constexpr auto& getEntityToCellMap() const {
279 if constexpr (dim == 0) {
280 return this->getVtxToCellMap();
281 } else if constexpr (dim == 1) {
282 return this->getEdgeToCellMap();
283 } else if constexpr (dim == 2) {
284 return this->getFacetToCellMap();
285 } else {
286 static_assert(dim * 0);
287 }
288 }
289
298 Mesh3D agglomerate(const std::vector<IndexType>& old_to_new_cell) const {
299 auto agg_mesh = this->agglomerateBase(old_to_new_cell);
300 PROFILE(agg_mesh.buildEdgeMaps());
301 return agg_mesh;
302 }
303
304 protected:
318 const CellsFaceVtxsUncompressed& cells_face_vtxs);
327 void buildEdgeMaps();
328
346};
347
348extern template class Mesh3D<double, int32_t>;
349extern template class Mesh3D<double, uint32_t>;
350extern template class Mesh3D<double, int64_t>;
351extern template class Mesh3D<double, uint64_t>;
352
353namespace mesh3d {
370template <typename FloatType, typename IndexType>
372 const std::string& filename,
373 bool check_vtxs_unique = false,
374 bool add_tags = true);
375
393template <typename FloatType, typename IndexType>
395 const Point<FloatType, 3>& corner_a,
396 const Point<FloatType, 3>& corner_b,
397 const Point<IndexType, 3>& n_divisions,
398 bool tetra_elements = false);
399
401
402extern template Mesh3D<double, int32_t> readVTU<double, int32_t>(
403 const std::string&, bool, bool);
404extern template Mesh3D<double, uint32_t> readVTU<double, uint32_t>(
405 const std::string&, bool, bool);
406extern template Mesh3D<double, int64_t> readVTU<double, int64_t>(
407 const std::string&, bool, bool);
408extern template Mesh3D<double, uint64_t> readVTU<double, uint64_t>(
409 const std::string&, bool, bool);
410
411extern template Mesh3D<double, int32_t> rect_cuboid<double, int32_t>(
412 const Point<double, 3>&,
413 const Point<double, 3>&,
414 const Point<int32_t, 3>&,
415 bool);
416extern template Mesh3D<double, uint32_t> rect_cuboid<double, uint32_t>(
417 const Point<double, 3>&,
418 const Point<double, 3>&,
419 const Point<uint32_t, 3>&,
420 bool);
421extern template Mesh3D<double, int64_t> rect_cuboid<double, int64_t>(
422 const Point<double, 3>&,
423 const Point<double, 3>&,
424 const Point<int64_t, 3>&,
425 bool);
426extern template Mesh3D<double, uint64_t> rect_cuboid<double, uint64_t>(
427 const Point<double, 3>&,
428 const Point<double, 3>&,
429 const Point<uint64_t, 3>&,
430 bool);
431} // namespace mesh3d
432
433} // namespace geometry
434} // namespace vulpes
435
436#endif // VULPES_GEOMETRY_MESH_MESH_3D_HPP_
IndexType size() const
Get the number of rows in the CRS map.
Definition crs_map.hpp:331
A class for a 2D mesh, inheriting from MeshBase.
Definition mesh_2d.hpp:94
Represents a 3D mesh.
Definition mesh_3d.hpp:82
std::vector< EdgeVtxs > EdgeToVtxMap
Map from cells to their edges.
Definition mesh_base.hpp:418
datastructure::CrsMap< IndexType, IndexType > CellToEdgeMap
Map from edges to adjacent cells.
Definition mesh_base.hpp:420
Point< FloatType, 3 > SelfPoint
Alias for a 3D point with coordinates of type FloatType.
Definition mesh_3d.hpp:87
void buildFacetToCellAndFacetToVertex(const CellsFaceVtxsUncompressed &cells_face_vtxs)
Builds the facet-to-cell and facet-to-vertex maps.
Definition mesh_3d.cpp:65
EdgeToVtxMap & getEdgeToVtxMap()
Gets a mutable reference to the edge-to-vertex map.
Definition mesh_3d.hpp:203
const EdgeToCellMap & getEdgeToCellMap() const
Gets a constant reference to the edge-to-cell map.
Definition mesh_3d.hpp:230
datastructure::CrsMapUncompressed< IndexType > CellToVtxMapUncompressed
Map from facets to the cells they separate.
Definition mesh_base.hpp:401
void serialize(Archive &ar, const unsigned int)
Serializes the mesh data for saving or transferring.
Definition mesh_3d.hpp:177
static Mesh3D extrude(const Mesh2D< FloatType, IndexType > &mesh_2d, const Eigen::Vector< FloatType, 2 > z_range, IndexType n_elems)
Creates a 3D mesh by extruding a 2D mesh.
Definition mesh_3d.cpp:181
const CellToEdgeMap & getCellToEdgeMap() const
Gets a constant reference to the cell-to-edge map.
Definition mesh_3d.hpp:219
EdgeToCellMap & getEdgeToCellMap()
Gets a mutable reference to the edge-to-cell map.
Definition mesh_3d.hpp:225
const auto & getCellToEdgeGlobalMap() const
Gets a constant reference to the global cell-to-edge map.
Definition mesh_3d.hpp:241
const auto & getEdgeToVtxGlobalMap() const
Gets a constant reference to the global edge-to-vertex map.
Definition mesh_3d.hpp:236
friend bool operator==(const Mesh3D &lhs, const Mesh3D &rhs)
Compares two Mesh3D objects for equality.
Definition mesh_3d.hpp:191
const auto & getEdgeToCellGlobalMap() const
Gets a constant reference to the global edge-to-cell map.
Definition mesh_3d.hpp:246
Mesh3D agglomerate(const std::vector< IndexType > &old_to_new_cell) const
Creates a new mesh by agglomerating (merging) cells of the current mesh.
Definition mesh_3d.hpp:298
datastructure::CrsMap< IndexType, IndexType > EdgeToCellMap
Definition mesh_base.hpp:422
CellToEdgeMap & getCellToEdgeMap()
Gets a mutable reference to the cell-to-edge map.
Definition mesh_3d.hpp:214
Mesh3D(const std::vector< SelfPoint > &vertexes, const CellToVtxMapUncompressed &cell_vertices, const CellsFaceVtxsUncompressed &cells_face_vtxs, const MeshTag< 3, IndexType > &mesh_tags)
Constructor for creating a 3D mesh with tags.
Definition mesh_3d.hpp:161
const EdgeToVtxMap & getEdgeToVtxMap() const
Gets a constant reference to the edge-to-vertex map.
Definition mesh_3d.hpp:208
constexpr IndexType nEntity(std::size_t dim) const
Returns the number of entities of a specific dimension in the mesh.
Definition mesh_3d.hpp:255
EdgeToCellMap m_edge_to_cell
A map from edges to the cells that contain them.
Definition mesh_3d.hpp:345
std::vector< std::vector< IndexType > > CellsFaceVtxsUncompressed
A vector of vectors, where each inner vector holds the vertex indices for the faces of a cell.
Definition mesh_3d.hpp:104
void buildEdgeMaps()
Builds the edge-related maps.
Definition mesh_3d.cpp:116
MeshBase< FloatType, IndexType, 3, 3, Mesh3D< FloatType, IndexType > > BaseType
Alias for the base class, MeshBase.
Definition mesh_3d.hpp:91
constexpr auto & getEntityToCellMap() const
Returns the entity-to-cell map for a given dimension.
Definition mesh_3d.hpp:278
Mesh3D()=default
Default constructor. Creates an empty 3D mesh.
Mesh3D(const std::vector< SelfPoint > &vertexes, const CellToVtxMapUncompressed &cell_vertices, const CellsFaceVtxsUncompressed &cells_face_vtxs)
Constructor for creating a 3D mesh from a list of vertices, cell-to-vertex connectivity,...
Definition mesh_3d.hpp:139
CellToEdgeMap m_cell_to_edge
A map from cells to their edges.
Definition mesh_3d.hpp:341
EdgeToVtxMap m_edge_to_vtx
A map from edges to their constituent vertices.
Definition mesh_3d.hpp:334
A base class for creating and managing mesh data structures.
Definition mesh_base.hpp:370
Mesh3D< FloatType, IndexType > agglomerateBase(const std::vector< index_t > &old_to_new_cell, bool check_connected=true) const
datastructure::CrsMap< IndexType, IndexType > CellToVtxMap
Definition mesh_base.hpp:399
MeshTag< SelfDim, IndexType > m_mesh_tags
Definition mesh_base.hpp:749
datastructure::CrsMap< IndexType, IndexType > EdgeToCellMap
Definition mesh_base.hpp:422
datastructure::CrsMap< IndexType, IndexType > CellToEdgeMap
Definition mesh_base.hpp:420
std::vector< SelfPoint > & getVertexes()
Definition mesh_base.hpp:442
datastructure::CrsMapUncompressed< IndexType > CellToVtxMapUncompressed
Definition mesh_base.hpp:401
Definition mesh_3d.cpp:281
Definition abs_agg_model.cpp:40
Eigen::Matrix< DataType, Dim, 1, Eigen::ColMajor > Point
Definition traits.hpp:34
Definition cell_manager.hpp:38
#define PROFILE(func)
Time a single execution of func under the label #func using Profiler::start()/stop().
Definition profiler.hpp:44
A structure to store tags for different parts of a mesh.
Definition mesh_base.hpp:333