Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vector_block_utils.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: Pedro Peixoto <ppeixoto@usp.br>
22
23#ifndef VULPES_LA_VECTOR_BLOCK_UTILS_HPP_
24#define VULPES_LA_VECTOR_BLOCK_UTILS_HPP_
25
26#include <petscsys.h>
27
28#include <array>
29#include <cassert>
30#include <cinttypes>
31#include <cmath>
32#include <cstddef>
33#include <cstdint>
34#include <functional>
35#include <vector>
36
38#include "vulpes/la/vector.hpp"
39
40namespace vulpes {
41namespace la {
42
57inline void extractComponent(
58 const Vector& blocked_vec, std::size_t f, Vector& comp) {
59 const PetscInt scalar_glob = comp.getLocAndGlobSize().second;
60 const PetscInt blocked_glob = blocked_vec.getLocAndGlobSize().second;
61 assert(
62 blocked_glob % scalar_glob == 0 &&
63 "blocked size not a multiple of scalar size");
64 const PetscInt nfields = blocked_glob / scalar_glob;
65 assert(static_cast<PetscInt>(f) < nfields && "Component index out of range");
66 // Setting the block size corrects under-specified metadata on the input;
67 // it is idempotent when the block size is already nfields.
68 Vec petsc_vec = blocked_vec.getPetscVector();
69 PetscCallAbortWorld(VecSetBlockSize(petsc_vec, nfields));
70 PetscCallAbortWorld(VecStrideGather(
71 blocked_vec.getPetscVector(), static_cast<PetscInt>(f),
72 comp.getPetscVector(), INSERT_VALUES));
73}
74
84template <typename DofHandlerType>
86 const Vector& blocked_vec,
87 const DofHandlerType& dof_handler,
88 std::size_t f) {
89 Vector comp(dof_handler);
90 extractComponent(blocked_vec, f, comp);
91 return comp;
92}
93
102template <std::size_t NFields, typename DofHandlerType>
103inline std::vector<Vector> extractAllComponents(
104 const Vector& blocked_vec, const DofHandlerType& dof_handler) {
105 assert(
106 blocked_vec.getBlockSize() == static_cast<PetscInt>(NFields) &&
107 "blocked_vec block size must equal NFields");
108
109 std::vector<Vector> components;
110 components.reserve(NFields);
111 for (std::size_t f = 0; f < NFields; ++f) {
112 components.push_back(extractComponent(blocked_vec, dof_handler, f));
113 }
114 return components;
115}
116
125template <std::size_t NFields, typename DofHandlerType>
127 const std::vector<Vector>& scalars, const DofHandlerType& dof_handler) {
128 assert(scalars.size() == NFields && "scalars.size() must equal NFields");
129
130 Vector blocked(dof_handler, static_cast<PetscInt>(NFields));
131 // Re-apply block size after VecSetFromOptions, which may not preserve it
132 // for VECMPI (mirrors the pattern in addComponent / extractComponent).
133 PetscCallAbortWorld(VecSetBlockSize(
134 blocked.getPetscVector(), static_cast<PetscInt>(NFields)));
135
136 for (std::size_t f = 0; f < NFields; ++f) {
137 assert(
138 scalars[f].getBlockSize() == 1 &&
139 "scalar components must be plain vectors");
140 PetscCallAbortWorld(VecStrideScatter(
141 scalars[f].getPetscVector(), static_cast<PetscInt>(f),
142 blocked.getPetscVector(), INSERT_VALUES));
143 }
144 return blocked;
145}
146
159inline void addComponent(
160 const Vector& scalar_vec, Vector& blocked_vec, std::size_t f) {
161 const PetscInt scalar_glob = scalar_vec.getLocAndGlobSize().second;
162 const PetscInt blocked_glob = blocked_vec.getLocAndGlobSize().second;
163 assert(
164 blocked_glob % scalar_glob == 0 &&
165 "blocked size not a multiple of scalar size");
166 const PetscInt nfields = blocked_glob / scalar_glob;
167 assert(static_cast<PetscInt>(f) < nfields && "Component index out of range");
168 PetscCallAbortWorld(VecSetBlockSize(blocked_vec.getPetscVector(), nfields));
169 PetscCallAbortWorld(VecStrideScatter(
170 scalar_vec.getPetscVector(), static_cast<PetscInt>(f),
171 blocked_vec.getPetscVector(), ADD_VALUES));
172}
173
179static constexpr PetscInt kMaxDenseExtractSize = 2000;
180
193 const SparseMatrix& blocked_mat,
194 std::size_t component,
195 SparseMatrix& scalar_mat,
196 double zero_tol = 0.0) {
197 const PetscInt bs = blocked_mat.getBlockSize();
198 assert(bs > 0);
199 assert(static_cast<PetscInt>(component) < bs);
200
201 const auto size = blocked_mat.getSize();
202 assert(size[0] == size[1]);
203 assert(size[0] % bs == 0);
204
205 const PetscInt n_scalar = size[0] / bs;
206 assert(
207 scalar_mat.getSize()[0] == n_scalar &&
208 scalar_mat.getSize()[1] == n_scalar);
209
210 PetscCheckAbort(
211 n_scalar <= kMaxDenseExtractSize, PETSC_COMM_WORLD, PETSC_ERR_SUP,
212 "extractDiagonalBlockComponent: %" PRId64
213 " scalar rows exceed the dense extraction limit %" PRId64
214 ". Project with the blocked matrix itself "
215 "(assemble::dg::l2ProjectExpressionBlockedSubset) or assemble the "
216 "scalar matrix directly from a scalar weak form.",
217 static_cast<int64_t>(n_scalar),
218 static_cast<int64_t>(kMaxDenseExtractSize));
219
220 const Eigen::MatrixXd dense = blocked_mat.toEigenDense();
221 for (PetscInt i = 0; i < n_scalar; ++i) {
222 for (PetscInt j = 0; j < n_scalar; ++j) {
223 const double v = dense(
224 i * bs + static_cast<PetscInt>(component),
225 j * bs + static_cast<PetscInt>(component));
226 if (std::abs(v) <= zero_tol) {
227 continue;
228 }
229 scalar_mat.insertValue(v, i, j, INSERT_VALUES);
230 }
231 }
232 scalar_mat.compress();
233}
234
245template <std::size_t NFields>
247 const std::array<std::reference_wrapper<const SparseMatrix>, NFields>&
248 scalar_components,
249 SparseMatrix& blocked_mat,
250 double zero_tol = 0.0) {
251 const PetscInt bs = blocked_mat.getBlockSize();
252 assert(bs == static_cast<PetscInt>(NFields));
253
254 const auto blocked_size = blocked_mat.getSize();
255 assert(blocked_size[0] == blocked_size[1]);
256 assert(blocked_size[0] % bs == 0);
257 const PetscInt n_scalar = blocked_size[0] / bs;
258
259 PetscCheckAbort(
260 n_scalar <= kMaxDenseExtractSize, PETSC_COMM_WORLD, PETSC_ERR_SUP,
261 "mergeDiagonalBlockedMatrix: %" PRId64
262 " scalar rows exceed the dense merge limit %" PRId64
263 ". Assemble the blocked matrix directly from an NFields-component "
264 "weak form.",
265 static_cast<int64_t>(n_scalar),
266 static_cast<int64_t>(kMaxDenseExtractSize));
267
268 for (std::size_t f = 0; f < NFields; ++f) {
269 const Eigen::MatrixXd dense = scalar_components[f].get().toEigenDense();
270 assert(dense.rows() == n_scalar && dense.cols() == n_scalar);
271 for (PetscInt i = 0; i < n_scalar; ++i) {
272 for (PetscInt j = 0; j < n_scalar; ++j) {
273 const double v = dense(i, j);
274 if (std::abs(v) <= zero_tol) {
275 continue;
276 }
277 blocked_mat.insertValue(
278 v, i * bs + static_cast<PetscInt>(f),
279 j * bs + static_cast<PetscInt>(f), INSERT_VALUES);
280 }
281 }
282 }
283 blocked_mat.compress();
284}
285
286} // namespace la
287} // namespace vulpes
288
289#endif // VULPES_LA_VECTOR_BLOCK_UTILS_HPP_
Definition sparse_matrix.hpp:52
Eigen::MatrixXd toEigenDense() const
Definition sparse_matrix.cpp:335
PetscInt getBlockSize() const
Get the block size of the matrix.
Definition sparse_matrix.cpp:189
void compress()
Finalize the assembly of the matrix by doing intracommunication.
Definition sparse_matrix.cpp:118
std::array< PetscInt, 2 > getSize() const
Get the size of the matrix as an array [rows, cols].
Definition sparse_matrix.cpp:183
void insertValue(PetscScalar value, PetscInt i, PetscInt j, InsertMode mode=ADD_VALUES)
Insert a single value into the matrix.
Definition sparse_matrix.cpp:203
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
std::pair< PetscInt, PetscInt > getLocAndGlobSize() const
Get local and global sizes of the vector.
Definition vector.cpp:256
PetscInt getBlockSize() const
Get the block size of the vector.
Definition vector.cpp:270
const Vec & getPetscVector() const
Get the underlying PETSc vector (const version).
Definition vector.hpp:268
Definition boomer_amg.hpp:27
static constexpr PetscInt kMaxDenseExtractSize
Largest scalar dimension for which the dense helpers below call toEigenDense(). Beyond it the O(N^2) ...
Definition vector_block_utils.hpp:179
void mergeDiagonalBlockedMatrix(const std::array< std::reference_wrapper< const SparseMatrix >, NFields > &scalar_components, SparseMatrix &blocked_mat, double zero_tol=0.0)
Merge scalar diagonal component matrices into a blocked matrix.
Definition vector_block_utils.hpp:246
void extractComponent(const Vector &blocked_vec, std::size_t f, Vector &comp)
Extract one scalar component from a blocked vector into an existing scalar vector (owned entries; gho...
Definition vector_block_utils.hpp:57
void addComponent(const Vector &scalar_vec, Vector &blocked_vec, std::size_t f)
Add a scalar vector into one stride of a blocked vector.
Definition vector_block_utils.hpp:159
Vector mergeBlockedVector(const std::vector< Vector > &scalars, const DofHandlerType &dof_handler)
Merge scalar vectors into a blocked vector.
Definition vector_block_utils.hpp:126
std::vector< Vector > extractAllComponents(const Vector &blocked_vec, const DofHandlerType &dof_handler)
Extract all scalar components from a blocked vector.
Definition vector_block_utils.hpp:103
void extractDiagonalBlockComponent(const SparseMatrix &blocked_mat, std::size_t component, SparseMatrix &scalar_mat, double zero_tol=0.0)
Extract one scalar diagonal component from a blocked matrix.
Definition vector_block_utils.hpp:192
Definition cell_manager.hpp:38
#define PetscCallAbortWorld(...)
Definition petsc_utils.hpp:32