25#ifndef VULPES_POSTPROCESSING_PROJECT_SOLUTION_HPP_
26#define VULPES_POSTPROCESSING_PROJECT_SOLUTION_HPP_
55template <
typename MeshType,
typename FeSpaceType>
61 !MeshType::parallel(),
62 "projectSolution() on full mesh is not supported for parallel meshes. "
63 "Use projectSolutionSubmesh() instead.");
65 using index_t = MeshType::index_t;
66 using Point = MeshType::SelfPoint;
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();
74 constexpr bool need_scaling = FeSpaceType::needBasisScaling();
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 =
84 ? std::sqrt(cell_manager.
getMapping(cell_id)->jacobianDet({}))
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;
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();
107 return solution_on_vtxs;
134template <
typename MeshType,
typename FeSpaceType>
141 using index_t = MeshType::index_t;
142 using Point = MeshType::SelfPoint;
144 if constexpr (MeshType::parallel()) {
147 PETSC_ERR_ARG_WRONGSTATE,
148 "projectSolutionSubmesh in parallel needs renumberMpiSubdomainWise() "
149 "(dof_distrib_type 2)");
152 const auto& mesh = cell_manager.
getMesh();
153 const index_t n_cells = [&]() -> index_t {
154 if constexpr (MeshType::parallel()) {
155 return mesh.nOwnedCells();
157 return mesh.numCells();
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();
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]);
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 =
190 ? std::sqrt(cell_manager.
getMapping(cell_id)->jacobianDet({}))
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) {
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;
212 solution_on_vtxs.
pushBack(cell_vtx_evals);
214 return solution_on_vtxs;
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