23#ifndef VULPES_LA_VECTOR_BLOCK_UTILS_HPP_
24#define VULPES_LA_VECTOR_BLOCK_UTILS_HPP_
58 const Vector& blocked_vec, std::size_t f,
Vector& comp) {
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");
84template <
typename DofHandlerType>
87 const DofHandlerType& dof_handler,
102template <std::
size_t NFields,
typename DofHandlerType>
104 const Vector& blocked_vec,
const DofHandlerType& dof_handler) {
106 blocked_vec.
getBlockSize() ==
static_cast<PetscInt
>(NFields) &&
107 "blocked_vec block size must equal NFields");
109 std::vector<Vector> components;
110 components.reserve(NFields);
111 for (std::size_t f = 0; f < NFields; ++f) {
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");
130 Vector blocked(dof_handler,
static_cast<PetscInt
>(NFields));
136 for (std::size_t f = 0; f < NFields; ++f) {
138 scalars[f].getBlockSize() == 1 &&
139 "scalar components must be plain vectors");
141 scalars[f].getPetscVector(),
static_cast<PetscInt
>(f),
160 const Vector& scalar_vec,
Vector& blocked_vec, std::size_t f) {
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");
194 std::size_t component,
196 double zero_tol = 0.0) {
199 assert(
static_cast<PetscInt
>(component) < bs);
201 const auto size = blocked_mat.
getSize();
202 assert(size[0] == size[1]);
203 assert(size[0] % bs == 0);
205 const PetscInt n_scalar = size[0] / bs;
207 scalar_mat.
getSize()[0] == n_scalar &&
208 scalar_mat.
getSize()[1] == n_scalar);
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),
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) {
245template <std::
size_t NFields>
247 const std::array<std::reference_wrapper<const SparseMatrix>, NFields>&
250 double zero_tol = 0.0) {
252 assert(bs ==
static_cast<PetscInt
>(NFields));
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;
261 "mergeDiagonalBlockedMatrix: %" PRId64
262 " scalar rows exceed the dense merge limit %" PRId64
263 ". Assemble the blocked matrix directly from an NFields-component "
265 static_cast<int64_t
>(n_scalar),
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) {
278 v, i * bs +
static_cast<PetscInt
>(f),
279 j * bs +
static_cast<PetscInt
>(f), INSERT_VALUES);
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