Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
project_solution.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_POSTPROCESSING_PROJECT_SOLUTION_HPP_
26#define VULPES_POSTPROCESSING_PROJECT_SOLUTION_HPP_
27
28#include <vector>
29
33#include "vulpes/la/vector.hpp"
34
35namespace vulpes {
36namespace postprocessing {
37
55template <typename MeshType, typename FeSpaceType>
56Eigen::MatrixXd projectSolution(
57 const dofs::DofHandler<MeshType>& dof_handler,
59 const la::Vector& solution) {
60 static_assert(
61 !MeshType::parallel(),
62 "projectSolution() on full mesh is not supported for parallel meshes. "
63 "Use projectSolutionSubmesh() instead.");
64
65 using index_t = MeshType::index_t;
66 using Point = MeshType::SelfPoint;
67
68 const auto& mesh = cell_manager.getMesh();
69 const auto n_cells = mesh.numCells();
70 const auto& vtxs = mesh.getVertexes();
71 const auto& fe_space = cell_manager.getFeSpace();
72 const auto& basis = fe_space.getBasisFast();
73
74 constexpr bool need_scaling = FeSpaceType::needBasisScaling();
75
76 const auto bs = solution.getBlockSize();
77 const auto sol_data = solution.toEigenMap();
78
79 Eigen::MatrixXd solution_on_vtxs = Eigen::MatrixXd::Zero(bs, vtxs.size());
80 for (index_t cell_id = 0; cell_id < n_cells; ++cell_id) {
81 const auto& cell = cell_manager.getCells()[cell_id];
82 const double scaling =
83 need_scaling
84 ? std::sqrt(cell_manager.getMapping(cell_id)->jacobianDet({}))
85 : 1.0;
86 const auto dof_ids = dof_handler.getCellToDof()[cell_id];
87 const auto cell_vtxs = mesh.getCellToVtxMap()[cell_id];
88 for (index_t loc_vtx_id = 0; loc_vtx_id < cell_vtxs.size(); ++loc_vtx_id) {
89 const Point ref_pt = cell->vtxs().col(loc_vtx_id);
90 for (std::size_t fn_id = 0; fn_id < basis.size(); ++fn_id) {
91 const double fn_eval = basis[fn_id].evaluate(ref_pt);
92 for (PetscInt b = 0; b < bs; ++b) {
93 const double coef = sol_data[dof_ids[fn_id] * bs + b] / scaling;
94 solution_on_vtxs(b, cell_vtxs[loc_vtx_id]) += coef * fn_eval;
95 }
96 }
97 }
98 }
99 // if the space is discontinuous we average on the vertices
100 if (fe_space.getNumDofsForSubdimEntity(0) == 0) {
101 for (std::size_t vtx_id = 0; vtx_id < vtxs.size(); ++vtx_id) {
102 for (PetscInt b = 0; b < bs; ++b) {
103 solution_on_vtxs(b, vtx_id) /= mesh.getVtxToCellMap()[vtx_id].size();
104 }
105 }
106 }
107 return solution_on_vtxs;
108}
109
134template <typename MeshType, typename FeSpaceType>
137 const geometry::MeshExporterSubmesh<MeshType>& mesh_exporter,
138 const dofs::DofHandler<MeshType>& dof_handler,
140 const la::Vector& solution) {
141 using index_t = MeshType::index_t;
142 using Point = MeshType::SelfPoint;
143
144 if constexpr (MeshType::parallel()) {
145 PetscCheckAbort(
146 dof_handler.hasRenumbering(), PETSC_COMM_WORLD,
147 PETSC_ERR_ARG_WRONGSTATE,
148 "projectSolutionSubmesh in parallel needs renumberMpiSubdomainWise() "
149 "(dof_distrib_type 2)");
150 }
151
152 const auto& mesh = cell_manager.getMesh();
153 const index_t n_cells = [&]() -> index_t {
154 if constexpr (MeshType::parallel()) {
155 return mesh.nOwnedCells();
156 } else {
157 return mesh.numCells();
158 }
159 }();
160 const auto& fe_space = cell_manager.getFeSpace();
161 const auto& basis = fe_space.getBasisFast();
162 const auto& vtk_grids = mesh_exporter.getVtkGrids();
163 constexpr bool need_scaling = FeSpaceType::needBasisScaling();
164
165 const auto bs = solution.getBlockSize();
166 const auto sol_data = solution.toLocalEigenMap();
167 // range of global DoF IDs that the rank owns, in serial execution all DoFs
168 const auto ownership_range = solution.getOwnershipRange();
169
170 // helper to read locally-owned coefficient for global
171 // DoF given by dof_id and component b
172 const auto get_coef = [&](index_t dof_id, PetscInt b) -> double {
173 const PetscInt global_scalar_id = static_cast<PetscInt>(dof_id) * bs + b;
174 assert(global_scalar_id >= ownership_range[0]);
175 assert(global_scalar_id < ownership_range[1]);
176 const PetscInt local_scalar_id = global_scalar_id - ownership_range[0];
177 assert(local_scalar_id >= 0);
178 assert(local_scalar_id < sol_data.size());
179 return static_cast<double>(sol_data[local_scalar_id]);
180 };
181
183 solution_on_vtxs.indices().push_back(0);
184 std::vector<double> cell_vtx_evals;
185 assert(static_cast<index_t>(vtk_grids.size()) == n_cells);
186 for (index_t cell_id = 0; cell_id < n_cells; ++cell_id) {
187 const auto dof_ids = dof_handler.getCellToDof()[cell_id];
188 const double scaling =
189 need_scaling
190 ? std::sqrt(cell_manager.getMapping(cell_id)->jacobianDet({}))
191 : 1.0;
192 auto vtk_grid = vtk_grids[cell_id];
193 vtkPoints* points = vtk_grid->GetPoints();
194 const auto n_loc_vtxs = points->GetNumberOfPoints();
195 cell_vtx_evals.resize(n_loc_vtxs * bs);
196 std::fill(cell_vtx_evals.begin(), cell_vtx_evals.end(), 0.0);
197 for (index_t loc_vtx_id = 0; loc_vtx_id < n_loc_vtxs; ++loc_vtx_id) {
198 static_assert(
199 Point::RowsAtCompileTime > 0 && Point::RowsAtCompileTime <= 3);
200 double point_buffer[3];
201 points->GetPoint(loc_vtx_id, point_buffer);
202 Eigen::Map<Point> ref_pt(point_buffer);
203 cell_manager.getMapping(cell_id)->transformInv_(ref_pt);
204 for (std::size_t fn_id = 0; fn_id < basis.size(); ++fn_id) {
205 const double fn_eval = basis[fn_id].evaluate(ref_pt);
206 for (PetscInt b = 0; b < bs; ++b) {
207 const double coef = get_coef(dof_ids[fn_id], b) / scaling;
208 cell_vtx_evals[loc_vtx_id * bs + b] += coef * fn_eval;
209 }
210 }
211 }
212 solution_on_vtxs.pushBack(cell_vtx_evals);
213 }
214 return solution_on_vtxs;
215}
216
217} // namespace postprocessing
218} // namespace vulpes
219
220#endif // VULPES_POSTPROCESSING_PROJECT_SOLUTION_HPP_
Manages geometrical information about the cell and its mapping to the reference element.
Definition cell_manager.hpp:62
const auto & getCells() const
Access the underlying memory buffer for cells (some might be empty).
Definition cell_manager.hpp:158
const auto & getFeSpace() const
Get the finite element space associated to this cell manager.
Definition cell_manager.hpp:107
const auto & getMapping(index_t cell_id) const
Get the geometrical mapping of the reference element to physical element.
Definition cell_manager.hpp:152
const auto & getMesh() const
Get the underlying mesh.
Definition cell_manager.hpp:110
Definition crs_map.hpp:72
const auto & indices() const
Constant accessor to the indices buffer.
Definition crs_map.hpp:442
void pushBack(const std::vector< DataType > &row)
Definition crs_map.hpp:427
Compute and distribute the DoFs and sparsity pattern associated with a mesh and a certain finite elem...
Definition dof_handler.hpp:72
bool hasRenumbering() const
Definition dof_handler.hpp:134
const auto & getCellToDof() const
Get the cell to dof map.
Definition dof_handler.hpp:127
const auto & getVtkGrids() const
Gets a constant reference to the generated VTK grids.
Definition mesh_exporter.hpp:210
Exports a mesh as multiple VTK grids, one for each cell.
Definition mesh_exporter.hpp:857
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
PetscInt getBlockSize() const
Get the block size of the vector.
Definition vector.cpp:270
std::array< PetscInt, 2 > getOwnershipRange() const
Get the ownership range of the vector.
Definition vector.cpp:263
Eigen::Map< const Eigen::VectorXd > toLocalEigenMap() const
Convert the locally-owned vector entries to an Eigen map.
Definition vector.cpp:173
Eigen::Map< const Eigen::VectorXd > toEigenMap() const
Convert the locally-owned vector entries to an Eigen map (same as toLocalEigenMap()).
Definition vector.cpp:169
Definition err_order.hpp:42
Eigen::MatrixXd projectSolution(const dofs::DofHandler< MeshType > &dof_handler, const assemble::CellManager< MeshType, FeSpaceType > &cell_manager, const la::Vector &solution)
Evaluate a solution vector on the vertices of a mesh.
Definition project_solution.hpp:56
datastructure::CrsMap< double, typename MeshType::index_t > projectSolutionSubmesh(const geometry::MeshExporterSubmesh< MeshType > &mesh_exporter, const dofs::DofHandler< MeshType > &dof_handler, const assemble::CellManager< MeshType, FeSpaceType > &cell_manager, const la::Vector &solution)
Evaluate a finite element solution on the vertices of the mesh sub-triangulation used for visualizati...
Definition project_solution.hpp:136
Definition cell_manager.hpp:38