Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
mesh_2d.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: Andrea Cassara <andrea.cassara@mail.polimi.it>
22// Author: Matteo Caldana <matteo.caldana@polimi.it>
23// Author: Pedro Peixoto <ppeixoto@usp.br>
24
25#ifndef VULPES_GEOMETRY_MESH_MESH_2D_HPP_
26#define VULPES_GEOMETRY_MESH_MESH_2D_HPP_
27
28#include <algorithm>
29#include <cstddef>
30#include <cstdint>
31#include <fstream>
32#include <iostream>
33#include <limits>
34#include <map>
35#include <stdexcept>
36#include <string>
37#include <unordered_map>
38#include <utility>
39#include <vector>
40
41#include <Eigen/Dense>
42
43#include <vtkCell.h>
44#include <vtkCellArray.h>
45#include <vtkIdList.h>
46#include <vtkNew.h>
47#include <vtkPoints.h>
48#include <vtkPolyData.h>
49#include <vtkPolyDataReader.h>
50#include <vtkPolygon.h>
51#include <vtkPolyhedron.h>
52#include <vtkSmartPointer.h>
53#include <vtkType.h>
54#include <vtkUnstructuredGrid.h>
55#include <vtkXMLUnstructuredGridReader.h>
56#include <vtkXMLUnstructuredGridWriter.h>
57
62
63namespace vulpes {
64namespace geometry {
65
88template <typename FloatType, typename IndexType>
89class Mesh2D : public MeshBase<
90 FloatType,
91 IndexType,
92 2,
93 2,
94 Mesh2D<FloatType, IndexType>> {
95 public:
98 using BaseType =
105 static inline constexpr IndexType UNDEF = static_cast<IndexType>(-1);
106
110 Mesh2D() = default;
111
119 explicit Mesh2D(
120 const std::vector<SelfPoint>& vertexes,
121 const CellToVtxMapUncompressed& cell_vertices) {
122 this->m_vertexes = vertexes;
123 this->m_cell_to_vtx = cell_vertices.template compress<IndexType>();
124 buildMesh();
125 }
126
133 explicit Mesh2D(
134 const std::vector<SelfPoint>& vertexes,
135 const CellToVtxMap& cell_vertices) {
136 this->m_vertexes = vertexes;
137 this->m_cell_to_vtx = cell_vertices;
138 buildMesh();
139 }
140
147 explicit Mesh2D(
148 const std::vector<SelfPoint>& vertexes,
149 const CellToVtxMap& cell_vertices,
150 const MeshTag<2, IndexType>& tags)
151 : Mesh2D(vertexes, cell_vertices) {
152 this->m_mesh_tags = tags;
153 };
154
162 explicit Mesh2D(
163 const std::vector<SelfPoint>& vertexes,
164 const CellToVtxMapUncompressed& cell_vertices,
165 const MeshTag<2, IndexType>& tags)
166 : Mesh2D(vertexes, cell_vertices) {
167 this->m_mesh_tags = tags;
168 };
169
174 template <class Archive>
175 void serialize(Archive& ar, const unsigned int /*version*/) {
176 ar& static_cast<BaseType&>(*this);
177 }
178
186 friend bool operator==(const Mesh2D& lhs, const Mesh2D& rhs) {
187 return static_cast<const BaseType&>(lhs) ==
188 static_cast<const BaseType&>(rhs);
189 }
190
197 constexpr IndexType nEntity(std::size_t dim) const {
198 if (dim == 0) {
199 return this->getVertexes().size();
200 } else if (dim == 1) {
201 return this->getFacetToCellMap().size();
202 } else if (dim == 2) {
203 assert(this->m_cell_to_facet.size() == this->m_dual_graph.size());
204 return this->getDualMap().size();
205 } else {
206 throw std::invalid_argument("invalid entity dimension");
207 }
208 }
209
215 template <std::size_t dim>
216 constexpr auto& getEntityToCellMap() const {
217 if constexpr (dim == 0) {
218 return this->getVtxToCellMap();
219 } else if constexpr (dim == 1) {
220 return this->getFacetToCellMap();
221 } else {
222 static_assert(
223 dim < 2, "Dimension for getEntityToCellMap must be 0 or 1.");
224 }
225 }
226
236 Mesh2D agglomerate(const std::vector<IndexType>& old_to_new_cell) const {
237 auto agg_mesh = this->agglomerateBase(old_to_new_cell);
238 agg_mesh.fixVertexAndFacetsOrders();
239 return agg_mesh;
240 }
241
247 Mesh2D subMesh(const std::vector<IndexType>& cell_ids) const;
248
249 protected:
260
273
281};
282
283extern template class Mesh2D<double, int32_t>;
284extern template class Mesh2D<double, uint32_t>;
285extern template class Mesh2D<double, int64_t>;
286extern template class Mesh2D<double, uint64_t>;
287
288namespace mesh2d {
289
290namespace internal {
291
303template <typename FloatType, typename IndexType>
304std::array<std::vector<FloatType>, 2> buildCartesianBbox(
306 IndexType n_cells_x,
307 FloatType stretch) {
308 constexpr auto MAX = std::numeric_limits<FloatType>::max();
311 for (const auto& point : mesh.getVertexes()) {
312 min_corner = min_corner.cwiseMin(point);
313 max_corner = max_corner.cwiseMax(point);
314 }
315 // avoid exact match of cartesian grid with mesh by adding little extra space
316 const Point<FloatType, 2> bbox_size = max_corner - min_corner;
317 constexpr auto DELTA = 1e-2;
318 min_corner -= bbox_size * DELTA;
319 max_corner(0) += bbox_size(0) * DELTA;
320 using Vectord = Eigen::Vector<FloatType, -1>;
321 const Vectord x_grid =
322 Vectord::LinSpaced(n_cells_x + 1, min_corner(0), max_corner(0));
323 std::array<FloatType, 2> h = {(max_corner(0) - min_corner(0)) / n_cells_x, 0};
324 h[1] = h[0] * stretch;
325 std::array<std::vector<FloatType>, 2> grids = {
326 std::vector<FloatType>{min_corner[0]},
327 std::vector<FloatType>{min_corner[1]}};
328 for (std::size_t d = 0; d < 2; ++d) {
329 std::size_t i = 1;
330 do {
331 grids[d].push_back(grids[d][0] + i * h[d]);
332 i++;
333 } while (grids[d].back() < max_corner(d));
334 }
335 return grids;
336}
337} // namespace internal
338
352template <typename FloatType, typename IndexType>
353Mesh2D<FloatType, IndexType> intersectCartesianGrid(
355 const std::array<std::vector<FloatType>, 2>& grids,
356 FloatType agglomeration_threshold = 0.25);
357
372template <typename FloatType, typename IndexType>
375 IndexType n_cells_x,
376 FloatType stretch = 1,
377 FloatType agglomeration_threshold = 0.25) {
378 const auto grids = internal::buildCartesianBbox(mesh, n_cells_x, stretch);
379 return intersectCartesianGrid(mesh, grids, agglomeration_threshold);
380};
381
398template <typename FloatType, typename IndexType>
400 const Mesh2D<FloatType, IndexType>& mesh_a,
401 const Mesh2D<FloatType, IndexType>& mesh_b,
402 FloatType atol = 0.0,
403 FloatType rtol = 1e-4);
404
413template <typename FloatType, typename IndexType>
415 const std::string& filename,
416 bool check_vtxs_unique = true,
417 bool add_tags = true);
418
428template <typename FloatType, typename IndexType>
430 const Point<FloatType, 2>& corner_a,
431 const Point<FloatType, 2>& corner_b,
432 const Point<IndexType, 2>& n_divisions,
433 bool tria_elements = false);
434
458template <typename FloatType, typename IndexType>
459Mesh2D<FloatType, IndexType> rectangle_periodic(
460 const Point<FloatType, 2>& corner_a,
461 const Point<FloatType, 2>& corner_b,
462 const Point<IndexType, 2>& n_divisions,
463 bool tria_elements = false,
464 bool x_periodic = true,
465 bool y_periodic = true);
466
473template <typename FloatType, typename IndexType>
474Mesh2D<FloatType, IndexType> collapseEdges(
475 const Mesh2D<FloatType, IndexType>& mesh, FloatType edge_collapse_tol);
476
478
479extern template Mesh2D<double, int32_t> intersectCartesianGrid<double, int32_t>(
481 const std::array<std::vector<double>, 2>&,
482 double);
483
484extern template Mesh2D<double, int32_t> mergeMeshes<double, int32_t>(
487 double,
488 double);
489
490extern template Mesh2D<double, int32_t> collapseEdges(
491 const Mesh2D<double, int32_t>&, double);
492
493extern template Mesh2D<double, int32_t> read<double, int32_t>(
494 const std::string&, bool, bool);
495extern template Mesh2D<double, uint32_t> read<double, uint32_t>(
496 const std::string&, bool, bool);
497extern template Mesh2D<double, int64_t> read<double, int64_t>(
498 const std::string&, bool, bool);
499extern template Mesh2D<double, uint64_t> read<double, uint64_t>(
500 const std::string&, bool, bool);
501
502extern template Mesh2D<double, int32_t> rectangle<double, int32_t>(
503 const Point<double, 2>&,
504 const Point<double, 2>&,
505 const Point<int32_t, 2>&,
506 bool);
507extern template Mesh2D<double, uint32_t> rectangle<double, uint32_t>(
508 const Point<double, 2>&,
509 const Point<double, 2>&,
510 const Point<uint32_t, 2>&,
511 bool);
512extern template Mesh2D<double, int64_t> rectangle<double, int64_t>(
513 const Point<double, 2>&,
514 const Point<double, 2>&,
515 const Point<int64_t, 2>&,
516 bool);
517extern template Mesh2D<double, uint64_t> rectangle<double, uint64_t>(
518 const Point<double, 2>&,
519 const Point<double, 2>&,
520 const Point<uint64_t, 2>&,
521 bool);
522
523extern template Mesh2D<double, int32_t> rectangle_periodic<double, int32_t>(
524 const Point<double, 2>&,
525 const Point<double, 2>&,
526 const Point<int32_t, 2>&,
527 bool,
528 bool,
529 bool);
530extern template Mesh2D<double, uint32_t> rectangle_periodic<double, uint32_t>(
531 const Point<double, 2>&,
532 const Point<double, 2>&,
533 const Point<uint32_t, 2>&,
534 bool,
535 bool,
536 bool);
537extern template Mesh2D<double, int64_t> rectangle_periodic<double, int64_t>(
538 const Point<double, 2>&,
539 const Point<double, 2>&,
540 const Point<int64_t, 2>&,
541 bool,
542 bool,
543 bool);
544extern template Mesh2D<double, uint64_t> rectangle_periodic<double, uint64_t>(
545 const Point<double, 2>&,
546 const Point<double, 2>&,
547 const Point<uint64_t, 2>&,
548 bool,
549 bool,
550 bool);
551
552// Bi-Periodic Grid Utilities
553// ----------
558template <typename FloatType, typename IndexType>
559bool isPeriodic(const Mesh2D<FloatType, IndexType>& mesh);
560
561extern template bool isPeriodic<double, int32_t>(
563
567template <typename FloatType, typename IndexType>
568Mesh2D<FloatType, IndexType> closePeriodicTopology(
569 const Mesh2D<FloatType, IndexType>& mesh);
570
571extern template Mesh2D<double, int32_t> closePeriodicTopology<double, int32_t>(
573
579template <typename FloatType, typename IndexType>
580Mesh2D<FloatType, IndexType> openPeriodicTopology(
581 const Mesh2D<FloatType, IndexType>& mesh);
582
583extern template Mesh2D<double, int32_t> openPeriodicTopology<double, int32_t>(
585
586// Get vertices for a cell, including ghost vertices if the mesh is periodic.
587// Handles both periodic meshes with ghost metadata and standard meshes.
588template <typename FloatType, typename IndexType>
589std::vector<Point<FloatType, 2>> getCellVertexesPeriodic(
590 const Mesh2D<FloatType, IndexType>& mesh, IndexType cell_id);
591
592extern template std::vector<Point<double, 2>>
593getCellVertexesPeriodic<double, int32_t>(
594 const Mesh2D<double, int32_t>&, int32_t);
595
596} // namespace mesh2d
597
598} // namespace geometry
599} // namespace vulpes
600
601#endif // VULPES_GEOMETRY_MESH_MESH_2D_HPP_
A class that behaves like a vector of vectors, providing automatic row range checking.
Definition crs_map.hpp:758
Definition crs_map.hpp:72
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
static constexpr IndexType UNDEF
Definition mesh_2d.hpp:105
Mesh2D agglomerate(const std::vector< IndexType > &old_to_new_cell) const
Creates a new, coarser mesh by grouping cells together.
Definition mesh_2d.hpp:236
Mesh2D(const std::vector< SelfPoint > &vertexes, const CellToVtxMapUncompressed &cell_vertices)
Constructs a mesh from vertices and an uncompressed cell-to-vertex map.
Definition mesh_2d.hpp:119
constexpr IndexType nEntity(std::size_t dim) const
Returns the total number of entities of a given dimension.
Definition mesh_2d.hpp:197
Mesh2D()=default
Default constructor. Creates an empty mesh.
void buildMesh()
Builds all the necessary connectivity maps for the mesh.
Definition mesh_2d.hpp:255
Mesh2D(const std::vector< SelfPoint > &vertexes, const CellToVtxMapUncompressed &cell_vertices, const MeshTag< 2, IndexType > &tags)
Constructs a mesh from vertices, an uncompressed cell-to-vertex map, and tags.
Definition mesh_2d.hpp:162
Point< FloatType, 2 > SelfPoint
Alias for a 2D point.
Definition mesh_2d.hpp:96
MeshBase< FloatType, IndexType, 2, 2, Mesh2D< FloatType, IndexType > > BaseType
Alias for the map from cells to vertices.
Definition mesh_2d.hpp:98
void serialize(Archive &ar, const unsigned int)
Serializes the mesh data by calling the base class's serialization.
Definition mesh_2d.hpp:175
Mesh2D subMesh(const std::vector< IndexType > &cell_ids) const
Creates a new mesh from a subset of cells of the current mesh.
Definition mesh_2d.cpp:181
void fixVertexAndFacetsOrders()
Enforces the CCW ordering conventions for the mesh.
Definition mesh_2d.cpp:73
void buildFacetToCellAndCellToFacetAndFacetToVertex()
Builds the core connectivity maps relating facets (edges) to cells and vertices.
Definition mesh_2d.cpp:126
constexpr auto & getEntityToCellMap() const
Gets the connectivity map from a given entity dimension to cells.
Definition mesh_2d.hpp:216
friend bool operator==(const Mesh2D &lhs, const Mesh2D &rhs)
Checks if two 2D meshes are identical by comparing their base class data.
Definition mesh_2d.hpp:186
datastructure::CrsMapUncompressed< IndexType > CellToVtxMapUncompressed
A special value used to represent an undefined or invalid index.
Definition mesh_2d.hpp:103
Mesh2D(const std::vector< SelfPoint > &vertexes, const CellToVtxMap &cell_vertices, const MeshTag< 2, IndexType > &tags)
Constructs a mesh from vertices, a cell-to-vertex map, and tags.
Definition mesh_2d.hpp:147
datastructure::CrsMap< IndexType, IndexType > CellToVtxMap
Alias for the uncompressed version of the cell-to-vertex map.
Definition mesh_2d.hpp:101
Mesh2D(const std::vector< SelfPoint > &vertexes, const CellToVtxMap &cell_vertices)
Constructs a mesh from vertices and a compressed cell-to-vertex map.
Definition mesh_2d.hpp:133
A base class for creating and managing mesh data structures.
Definition mesh_base.hpp:370
Mesh2D< FloatType, IndexType > agglomerateBase(const std::vector< index_t > &old_to_new_cell, bool check_connected=true) const
MeshTag< SelfDim, IndexType > m_mesh_tags
Definition mesh_base.hpp:749
std::vector< SelfPoint > & getVertexes()
Definition mesh_base.hpp:442
std::array< std::vector< FloatType >, 2 > buildCartesianBbox(const Mesh2D< FloatType, IndexType > &mesh, IndexType n_cells_x, FloatType stretch)
Builds a 2D Cartesian grid that encloses a given mesh.
Definition mesh_2d.hpp:304
Mesh2D< FloatType, IndexType > intersectCartesianGrid(const Mesh2D< FloatType, IndexType > &mesh, const std::array< std::vector< FloatType >, 2 > &grids, FloatType agglomeration_threshold)
Creates a new mesh by intersecting an existing mesh with a Cartesian grid.
Definition mesh_2d.cpp:900
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