Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
cell_manager.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_ASSEMBLE_CELL_MANAGER_HPP_
25#define VULPES_ASSEMBLE_CELL_MANAGER_HPP_
26
27#include <algorithm>
28#include <cassert>
29#include <memory>
30#include <stdexcept>
31#include <vector>
32
37
38namespace vulpes {
39namespace assemble {
40
61template <typename MeshType, typename FeSpaceType>
63 public:
64 using index_t = typename MeshType::index_t;
65 using FeType = FeSpaceType;
66 static inline constexpr std::size_t dim = MeshType::self_dim;
68 using MappingType = std::decay_t<decltype(FeType::getMapping(CellType{}))>;
69
89
95 CellManager(const MeshType& mesh, const FeSpaceType& fe_space)
96 : m_mesh(mesh), m_fe_space(fe_space) {
97 m_cells.resize(mesh.numCells());
98 m_cell_is_normalized.resize(mesh.numCells(), false);
99 m_mappings.resize(mesh.numCells());
100 // copy the structure, empty the content
101 m_cell_to_facet_neigh.indices() = m_mesh.getCellToFacetMap().indices();
102 m_cell_to_facet_neigh.data().resize(
103 m_mesh.getCellToFacetMap().data().size());
104 }
105
107 const auto& getFeSpace() const { return m_fe_space; }
108
110 const auto& getMesh() const { return m_mesh; }
111
118 const CellType& getCell(index_t cell_id) {
119 Profiler::start("getCell");
120 constexpr auto cell_options = FeType::cellInitOptions();
121
122 // create cell
123 if (!m_cells[cell_id]) {
124 m_cells[cell_id] = std::make_unique<geometry::Cell<dim, double, index_t>>(
125 m_mesh, cell_id, cell_options);
126 }
127
128 // map cell to reference frame
129 auto& cell = *m_cells[cell_id];
130 std::unique_ptr<MappingType> mapping;
131 if (!m_cell_is_normalized[cell_id]) {
132 mapping = std::make_unique<MappingType>(
133 m_fe_space.getMapping(*m_cells[cell_id]));
134 mapping->transformInv_(cell.vtxs());
135 m_cell_is_normalized[cell_id] = true;
136 }
137
138 // build mapping
139 if (!m_mappings[cell_id]) {
140 m_mappings[cell_id] = std::make_unique<MappingType>(
141 m_fe_space.getMapping(*m_cells[cell_id]));
142 }
143 Profiler::stop("getCell");
144 return cell;
145 }
146
152 const auto& getMapping(index_t cell_id) const {
153 assert(m_mappings[cell_id]);
154 return m_mappings[cell_id];
155 }
156
158 const auto& getCells() const { return m_cells; }
159
170 NeighInfo findCellNeigh(index_t cell_id, index_t loc_face_id) {
171 auto& neigh_info = m_cell_to_facet_neigh[cell_id][loc_face_id];
172 if (neigh_info.built) {
173 return neigh_info;
174 }
175 // global means not of the cell, it is not global in MPI sense
176 const auto glob_face_id = m_mesh.getCellToFacetMap()[cell_id][loc_face_id];
177 // find the two cells to which this face belongs to
178 const auto cell_ids = m_mesh.getFacetToCellMap()[glob_face_id];
179 // find the cell that is not cell_id
180 neigh_info.neigh_cell_id = cell_ids[(cell_ids[0] == cell_id)];
181 neigh_info.have_neigh =
182 (neigh_info.neigh_cell_id != static_cast<index_t>(-1));
183 if (neigh_info.have_neigh) {
184 // find the local_cell_id in dual map from global id
185 const auto& dual_map = m_mesh.getDualMap();
186 const auto neighs_begin = dual_map[cell_id].begin();
187 const auto neighs_end = dual_map[cell_id].end();
188 neigh_info.loc_neigh_cell_id = std::distance(
189 neighs_begin,
190 std::find(neighs_begin, neighs_end, neigh_info.neigh_cell_id));
191 assert(neigh_info.loc_neigh_cell_id < dual_map[cell_id].size());
192 const auto neigh_cell_to_facet =
193 m_mesh.getCellToFacetMap()[neigh_info.neigh_cell_id];
194 const auto loc_neigh_face_it = std::find(
195 neigh_cell_to_facet.begin(), neigh_cell_to_facet.end(), glob_face_id);
196 neigh_info.loc_neigh_face_id =
197 std::distance(neigh_cell_to_facet.begin(), loc_neigh_face_it);
198 assert(neigh_info.loc_neigh_face_id < neigh_cell_to_facet.size());
199 }
200 neigh_info.built = true;
201 return neigh_info;
202 }
203
216 Eigen::Array<double, MeshType::self_dim, 1> getPeriodicShift(
217 index_t neigh_cell_id, index_t current_cell_id) const {
218 const auto& curr_cell = m_cells[current_cell_id];
219 const auto& neigh_cell = m_cells[neigh_cell_id];
220
221 if (!curr_cell || !neigh_cell) {
222 return Eigen::Array<double, dim, 1>::Zero();
223 }
224
225 // Extract period values from metadata
226 const auto& md = m_mesh.getMetadata();
227 Eigen::Array<double, dim, 1> period = Eigen::Array<double, dim, 1>::Zero();
228
229 if (md.doubles.find("XPeriod") != md.doubles.end()) {
230 period[0] = md.doubles.at("XPeriod")[0];
231 }
232 if constexpr (dim > 1) {
233 if (md.doubles.find("YPeriod") != md.doubles.end()) {
234 period[1] = md.doubles.at("YPeriod")[0];
235 }
236 }
237 if constexpr (dim > 2) {
238 if (md.doubles.find("ZPeriod") != md.doubles.end()) {
239 throw std::logic_error("3D periodic conditions not yet implemented.");
240 // period[2] = md.doubles.at("ZPeriod")[0];
241 }
242 }
243
245 curr_cell->bbox(), neigh_cell->bbox(), period);
246 }
247
248 protected:
249 const MeshType& m_mesh;
251
252 // the cells
253 std::vector<std::unique_ptr<CellType>> m_cells;
254 // table to check if the cell mapped in to the reference frame
255 std::vector<bool> m_cell_is_normalized;
256 // mappings for each cell
257 std::vector<std::unique_ptr<MappingType>> m_mappings;
262};
263
264} // namespace assemble
265} // namespace vulpes
266
267#endif // VULPES_ASSEMBLE_CELL_MANAGER_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
std::decay_t< decltype(FeType::getMapping(CellType{}))> MappingType
Definition cell_manager.hpp:68
const auto & getCells() const
Access the underlying memory buffer for cells (some might be empty).
Definition cell_manager.hpp:158
std::vector< std::unique_ptr< MappingType > > m_mappings
Definition cell_manager.hpp:257
const auto & getFeSpace() const
Get the finite element space associated to this cell manager.
Definition cell_manager.hpp:107
geometry::Cell< dim, double, index_t > CellType
Definition cell_manager.hpp:67
const CellType & getCell(index_t cell_id)
Get and build lazily the cells of the mesh.
Definition cell_manager.hpp:118
datastructure::CrsMap< NeighInfo, index_t > m_cell_to_facet_neigh
Definition cell_manager.hpp:261
const FeType & m_fe_space
Definition cell_manager.hpp:250
std::vector< std::unique_ptr< CellType > > m_cells
Definition cell_manager.hpp:253
static constexpr std::size_t dim
Definition cell_manager.hpp:66
typename MeshType::index_t index_t
Definition cell_manager.hpp:64
Eigen::Array< double, MeshType::self_dim, 1 > getPeriodicShift(index_t neigh_cell_id, index_t current_cell_id) const
Get the periodic shift needed for neighbor cell coordinate transformation.
Definition cell_manager.hpp:216
const MeshType & m_mesh
Definition cell_manager.hpp:249
FeSpaceType FeType
Definition cell_manager.hpp:65
CellManager(const MeshType &mesh, const FeSpaceType &fe_space)
Construct a new Cell Manager object.
Definition cell_manager.hpp:95
std::vector< bool > m_cell_is_normalized
Definition cell_manager.hpp:255
const auto & getMapping(index_t cell_id) const
Get the geometrical mapping of the reference element to physical element.
Definition cell_manager.hpp:152
NeighInfo findCellNeigh(index_t cell_id, index_t loc_face_id)
Find the cell id of the neighbour of a cell given the face id in local cell enumeration.
Definition cell_manager.hpp:170
const auto & getMesh() const
Get the underlying mesh.
Definition cell_manager.hpp:110
Definition crs_map.hpp:72
Definition cell_manager.hpp:39
Eigen::Array< double, Dim, 1 > computePeriodicShift(const Eigen::Matrix< double, Dim, 2 > &curr_bbox, const Eigen::Matrix< double, Dim, 2 > &neigh_bbox, const Eigen::Array< double, Dim, 1 > &period)
Compute periodic shift between two cells based on bboxes.
Definition mapping.hpp:228
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
Definition cell_manager.hpp:38
Information about the neighbouring cell for a certain face.
Definition cell_manager.hpp:73
index_t neigh_cell_id
cell id of the neighbor in global cell enumeration, local MPI enumeration
Definition cell_manager.hpp:76
bool have_neigh
the currently considered face of the cell has a neigh (i.e. not boundary)
Definition cell_manager.hpp:85
bool built
this class instance has been built
Definition cell_manager.hpp:87
index_t loc_neigh_cell_id
cell id of the neighbor in local cell enumeration, ranges [0, num of cell neighs)
Definition cell_manager.hpp:79
index_t loc_neigh_face_id
face id of the cell currently considered in local cell enumeration
Definition cell_manager.hpp:82