| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2009 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_matrix_block_h |
| 16 |
#define dealii_matrix_block_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/algorithms/any_data.h> |
| 21 |
|
| 22 |
#include <deal.II/base/memory_consumption.h> |
| 23 |
#include <deal.II/base/mg_level_object.h> |
| 24 |
#include <deal.II/base/observer_pointer.h> |
| 25 |
|
| 26 |
#include <deal.II/lac/block_indices.h> |
| 27 |
#include <deal.II/lac/block_sparsity_pattern.h> |
| 28 |
#include <deal.II/lac/full_matrix.h> |
| 29 |
#include <deal.II/lac/sparse_matrix.h> |
| 30 |
|
| 31 |
#include <memory> |
| 32 |
|
| 33 |
|
| 34 |
DEAL_II_NAMESPACE_OPEN |
| 35 |
|
| 36 |
// Forward declarations |
| 37 |
#ifndef DOXYGEN |
| 38 |
template <typename MatrixType> |
| 39 |
class MatrixBlock; |
| 40 |
#endif |
| 41 |
|
| 42 |
namespace internal |
| 43 |
{ |
| 44 |
template <typename MatrixType> |
| 45 |
void |
| 46 |
reinit(MatrixBlock<MatrixType> &v, const BlockSparsityPattern &p); |
| 47 |
|
| 48 |
template <typename number> |
| 49 |
void |
| 50 |
reinit(MatrixBlock<dealii::SparseMatrix<number>> &v, |
| 51 |
const BlockSparsityPattern &p); |
| 52 |
} // namespace internal |
| 53 |
|
| 54 |
/** |
| 55 |
* A wrapper around a matrix object, storing the coordinates in a block matrix |
| 56 |
* as well. |
| 57 |
* |
| 58 |
* This class is an alternative to BlockMatrixBase, if you only want to |
| 59 |
* generate a single block of the system, not the whole system. Using the |
| 60 |
* add() functions of this class, it is possible to use the standard |
| 61 |
* assembling functions used for block matrices, but only enter in one of the |
| 62 |
* blocks and still avoiding the index computations involved. The reason for |
| 63 |
* this class is, that we may need a different number of matrices for |
| 64 |
* different blocks in a block system. For example, a preconditioner for the |
| 65 |
* Oseen system can be built as a block system, where the pressure block is of |
| 66 |
* the form <b>M</b><sup>-1</sup><b>FA</b><sup>-1</sup> with <b>M</b> the |
| 67 |
* pressure @ref GlossMassMatrix "mass matrix", <b>A</b> the pressure Laplacian and <b>F</b> the |
| 68 |
* advection diffusion operator applied to the pressure space. Since only a |
| 69 |
* single matrix is needed for the other blocks, using BlockSparseMatrix or |
| 70 |
* similar would be a waste of memory. |
| 71 |
* |
| 72 |
* While the add() functions make a MatrixBlock appear like a block matrix for |
| 73 |
* assembling, the functions vmult(), Tvmult(), vmult_add(), and Tvmult_add() |
| 74 |
* make it behave like a MatrixType, when it comes to applying it to a vector. |
| 75 |
* This behavior allows us to store MatrixBlock objects in vectors, for |
| 76 |
* instance in MGLevelObject without extracting the #matrix first. |
| 77 |
* |
| 78 |
* @note This class expects, that the row and column BlockIndices objects for |
| 79 |
* the system are equal. If they are not, some functions will throw |
| 80 |
* ExcNotImplemented. |
| 81 |
* |
| 82 |
* @todo Example for the product preconditioner of the pressure Schur |
| 83 |
* complement. |
| 84 |
* |
| 85 |
* @ingroup Matrix2 |
| 86 |
* @ingroup vector_valued |
| 87 |
* |
| 88 |
* @see |
| 89 |
* @ref GlossBlockLA "Block (linear algebra)" |
| 90 |
*/ |
| 91 |
template <typename MatrixType> |
| 92 |
class MatrixBlock : public EnableObserverPointer |
| 93 |
{ |
| 94 |
public: |
| 95 |
/** |
| 96 |
* Declare type for container size. |
| 97 |
*/ |
| 98 |
using size_type = types::global_dof_index; |
| 99 |
|
| 100 |
/** |
| 101 |
* Declare a type for matrix entries. |
| 102 |
*/ |
| 103 |
using value_type = typename MatrixType::value_type; |
| 104 |
|
| 105 |
/** |
| 106 |
* Constructor rendering an uninitialized object. |
| 107 |
*/ |
| 108 |
MatrixBlock(); |
| 109 |
|
| 110 |
/** |
| 111 |
* Copy constructor. |
| 112 |
*/ |
| 113 |
MatrixBlock(const MatrixBlock<MatrixType> &M) = default; |
| 114 |
|
| 115 |
/** |
| 116 |
* Assignment operator. |
| 117 |
*/ |
| 118 |
MatrixBlock<MatrixType> & |
| 119 |
operator=(const MatrixBlock<MatrixType> &) = default; |
| 120 |
|
| 121 |
/** |
| 122 |
* Constructor setting block coordinates, but not initializing the matrix. |
| 123 |
*/ |
| 124 |
|
| 125 |
MatrixBlock(size_type i, size_type j); |
| 126 |
|
| 127 |
/** |
| 128 |
* Reinitialize the matrix for a new BlockSparsityPattern. This adjusts the |
| 129 |
* #matrix as well as the #row_indices and #column_indices. |
| 130 |
* |
| 131 |
* @note The row and column block structure of the sparsity pattern must be |
| 132 |
* equal. |
| 133 |
*/ |
| 134 |
void |
| 135 |
reinit(const BlockSparsityPattern &sparsity); |
| 136 |
|
| 137 |
operator MatrixType &(); |
| 138 |
operator const MatrixType &() const; |
| 139 |
|
| 140 |
/** |
| 141 |
* Add <tt>value</tt> to the element (<i>i,j</i>). Throws an error if the |
| 142 |
* entry does not exist or if it is in a different block. |
| 143 |
*/ |
| 144 |
void |
| 145 |
add(const size_type i, |
| 146 |
const size_type j, |
| 147 |
const typename MatrixType::value_type value); |
| 148 |
|
| 149 |
/** |
| 150 |
* Add all elements in a FullMatrix into sparse matrix locations given by |
| 151 |
* <tt>indices</tt>. This function assumes a quadratic sparse matrix and a |
| 152 |
* quadratic full_matrix. The global locations are translated into |
| 153 |
* locations in this block and ExcBlockIndexMismatch is thrown, if the |
| 154 |
* global index does not point into the block referred to by #row and |
| 155 |
* #column. |
| 156 |
* |
| 157 |
* @todo <tt>elide_zero_values</tt> is currently ignored. |
| 158 |
* |
| 159 |
* The optional parameter <tt>elide_zero_values</tt> can be used to specify |
| 160 |
* whether zero values should be added anyway or these should be filtered |
| 161 |
* away and only non-zero data is added. The default value is <tt>true</tt>, |
| 162 |
* i.e., zero values won't be added into the matrix. |
| 163 |
*/ |
| 164 |
template <typename number> |
| 165 |
void |
| 166 |
add(const std::vector<size_type> &indices, |
| 167 |
const FullMatrix<number> &full_matrix, |
| 168 |
const bool elide_zero_values = true); |
| 169 |
|
| 170 |
/** |
| 171 |
* Add all elements in a FullMatrix into global locations given by |
| 172 |
* <tt>row_indices</tt> and <tt>col_indices</tt>, respectively. The global |
| 173 |
* locations are translated into locations in this block and |
| 174 |
* ExcBlockIndexMismatch is thrown, if the global index does not point into |
| 175 |
* the block referred to by #row and #column. |
| 176 |
* |
| 177 |
* @todo <tt>elide_zero_values</tt> is currently ignored. |
| 178 |
* |
| 179 |
* The optional parameter <tt>elide_zero_values</tt> can be used to specify |
| 180 |
* whether zero values should be added anyway or these should be filtered |
| 181 |
* away and only non-zero data is added. The default value is <tt>true</tt>, |
| 182 |
* i.e., zero values won't be added into the matrix. |
| 183 |
*/ |
| 184 |
template <typename number> |
| 185 |
void |
| 186 |
add(const std::vector<size_type> &row_indices, |
| 187 |
const std::vector<size_type> &col_indices, |
| 188 |
const FullMatrix<number> &full_matrix, |
| 189 |
const bool elide_zero_values = true); |
| 190 |
|
| 191 |
/** |
| 192 |
* Set several elements in the specified row of the matrix with column |
| 193 |
* indices as given by <tt>col_indices</tt> to the respective value. This is |
| 194 |
* the function doing the actual work for the ones adding full matrices. The |
| 195 |
* global locations <tt>row_index</tt> and <tt>col_indices</tt> are |
| 196 |
* translated into locations in this block and ExcBlockIndexMismatch is |
| 197 |
* thrown, if the global index does not point into the block referred to by |
| 198 |
* #row and #column. |
| 199 |
* |
| 200 |
* @todo <tt>elide_zero_values</tt> is currently ignored. |
| 201 |
* |
| 202 |
* The optional parameter <tt>elide_zero_values</tt> can be used to specify |
| 203 |
* whether zero values should be added anyway or these should be filtered |
| 204 |
* away and only non-zero data is added. The default value is <tt>true</tt>, |
| 205 |
* i.e., zero values won't be added into the matrix. |
| 206 |
*/ |
| 207 |
template <typename number> |
| 208 |
void |
| 209 |
add(const size_type row_index, |
| 210 |
const std::vector<size_type> &col_indices, |
| 211 |
const std::vector<number> &values, |
| 212 |
const bool elide_zero_values = true); |
| 213 |
|
| 214 |
/** |
| 215 |
* Add an array of values given by <tt>values</tt> in the given global |
| 216 |
* matrix row at columns specified by col_indices in the sparse matrix. |
| 217 |
* |
| 218 |
* The optional parameter <tt>elide_zero_values</tt> can be used to specify |
| 219 |
* whether zero values should be added anyway or these should be filtered |
| 220 |
* away and only non-zero data is added. The default value is <tt>true</tt>, |
| 221 |
* i.e., zero values won't be added into the matrix. |
| 222 |
*/ |
| 223 |
template <typename number> |
| 224 |
void |
| 225 |
add(const size_type row, |
| 226 |
const size_type n_cols, |
| 227 |
const size_type *col_indices, |
| 228 |
const number *values, |
| 229 |
const bool elide_zero_values = true, |
| 230 |
const bool col_indices_are_sorted = false); |
| 231 |
|
| 232 |
/** |
| 233 |
* Matrix-vector-multiplication, forwarding to the same function in |
| 234 |
* MatrixType. No index computations are done, thus, the vectors need to |
| 235 |
* have sizes matching #matrix. |
| 236 |
*/ |
| 237 |
template <typename VectorType> |
| 238 |
void |
| 239 |
vmult(VectorType &w, const VectorType &v) const; |
| 240 |
|
| 241 |
/** |
| 242 |
* Matrix-vector-multiplication, forwarding to the same function in |
| 243 |
* MatrixType. No index computations are done, thus, the vectors need to |
| 244 |
* have sizes matching #matrix. |
| 245 |
*/ |
| 246 |
template <typename VectorType> |
| 247 |
void |
| 248 |
vmult_add(VectorType &w, const VectorType &v) const; |
| 249 |
|
| 250 |
/** |
| 251 |
* Matrix-vector-multiplication, forwarding to the same function in |
| 252 |
* MatrixType. No index computations are done, thus, the vectors need to |
| 253 |
* have sizes matching #matrix. |
| 254 |
*/ |
| 255 |
template <typename VectorType> |
| 256 |
void |
| 257 |
Tvmult(VectorType &w, const VectorType &v) const; |
| 258 |
|
| 259 |
/** |
| 260 |
* Matrix-vector-multiplication, forwarding to the same function in |
| 261 |
* MatrixType. No index computations are done, thus, the vectors need to |
| 262 |
* have sizes matching #matrix. |
| 263 |
*/ |
| 264 |
template <typename VectorType> |
| 265 |
void |
| 266 |
Tvmult_add(VectorType &w, const VectorType &v) const; |
| 267 |
|
| 268 |
/** |
| 269 |
* The memory used by this object. |
| 270 |
*/ |
| 271 |
std::size_t |
| 272 |
memory_consumption() const; |
| 273 |
|
| 274 |
/** |
| 275 |
* The block number computed from an index by using BlockIndices does not |
| 276 |
* match the block coordinates stored in this object. |
| 277 |
*/ |
| 278 |
DeclException2(ExcBlockIndexMismatch, |
| 279 |
size_type, |
| 280 |
size_type, |
| 281 |
<< "Block index " << arg1 << " does not match " << arg2); |
| 282 |
|
| 283 |
/** |
| 284 |
* Row coordinate. This is the position of the data member matrix on the |
| 285 |
* global matrix. |
| 286 |
*/ |
| 287 |
size_type row; |
| 288 |
/** |
| 289 |
* Column coordinate. This is the position of the data member matrix on the |
| 290 |
* global matrix. |
| 291 |
*/ |
| 292 |
size_type column; |
| 293 |
|
| 294 |
/** |
| 295 |
* The matrix itself |
| 296 |
*/ |
| 297 |
MatrixType matrix; |
| 298 |
|
| 299 |
private: |
| 300 |
/** |
| 301 |
* The row BlockIndices of the whole system. Using row(), this allows us to |
| 302 |
* find the index of the first row degree of freedom for this block. |
| 303 |
*/ |
| 304 |
BlockIndices row_indices; |
| 305 |
/** |
| 306 |
* The column BlockIndices of the whole system. Using column(), this allows |
| 307 |
* us to find the index of the first column degree of freedom for this |
| 308 |
* block. |
| 309 |
*/ |
| 310 |
BlockIndices column_indices; |
| 311 |
|
| 312 |
template <class OTHER_MatrixType> |
| 313 |
friend void |
| 314 |
dealii::internal::reinit(MatrixBlock<OTHER_MatrixType> &, |
| 315 |
const BlockSparsityPattern &); |
| 316 |
|
| 317 |
template <typename number> |
| 318 |
friend void |
| 319 |
internal::reinit(MatrixBlock<dealii::SparseMatrix<number>> &v, |
| 320 |
const BlockSparsityPattern &p); |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
/** |
| 325 |
* A vector of MatrixBlock, which is implemented using shared pointers, in |
| 326 |
* order to allow for copying and rearranging. Each matrix block can be |
| 327 |
* identified by name. |
| 328 |
* |
| 329 |
* @relatesalso MatrixBlock |
| 330 |
* @ingroup vector_valued |
| 331 |
*/ |
| 332 |
template <typename MatrixType> |
| 333 |
class MatrixBlockVector : public AnyData |
| 334 |
{ |
| 335 |
public: |
| 336 |
/** |
| 337 |
* Declare type for container size. |
| 338 |
*/ |
| 339 |
using size_type = types::global_dof_index; |
| 340 |
|
| 341 |
/** |
| 342 |
* The type of object stored. |
| 343 |
*/ |
| 344 |
using value_type = MatrixBlock<MatrixType>; |
| 345 |
|
| 346 |
/** |
| 347 |
* The pointer type used for storing the objects. We use a shard pointer, |
| 348 |
* such that they get deleted automatically when not used anymore. |
| 349 |
*/ |
| 350 |
using ptr_type = std::shared_ptr<value_type>; |
| 351 |
|
| 352 |
/** |
| 353 |
* Add a new matrix block at the position <tt>(row,column)</tt> in the block |
| 354 |
* system. |
| 355 |
*/ |
| 356 |
void |
| 357 |
add(size_type row, size_type column, const std::string &name); |
| 358 |
|
| 359 |
/** |
| 360 |
* For matrices using a SparsityPattern, this function reinitializes each |
| 361 |
* matrix in the vector with the correct pattern from the block system. |
| 362 |
*/ |
| 363 |
void |
| 364 |
reinit(const BlockSparsityPattern &sparsity); |
| 365 |
|
| 366 |
/** |
| 367 |
* Clear the object. |
| 368 |
* |
| 369 |
* Since often only clearing of the individual matrices is desired, but not |
| 370 |
* removing the blocks themselves, there is an optional argument. If the |
| 371 |
* argument is missing or @p false, all matrices will be empty, but the size |
| 372 |
* of this object and the block positions will not change. If @p |
| 373 |
* really_clean is @p true, then the object will contain no blocks at the |
| 374 |
* end. |
| 375 |
*/ |
| 376 |
void |
| 377 |
clear(bool really_clean = false); |
| 378 |
|
| 379 |
/** |
| 380 |
* The memory used by this object. |
| 381 |
*/ |
| 382 |
std::size_t |
| 383 |
memory_consumption() const; |
| 384 |
|
| 385 |
/** |
| 386 |
* Access a constant reference to the block at position <i>i</i>. |
| 387 |
*/ |
| 388 |
const value_type & |
| 389 |
block(size_type i) const; |
| 390 |
|
| 391 |
/** |
| 392 |
* Access a reference to the block at position <i>i</i>. |
| 393 |
*/ |
| 394 |
value_type & |
| 395 |
block(size_type i); |
| 396 |
|
| 397 |
/** |
| 398 |
* Access the matrix at position <i>i</i> for read and write access. |
| 399 |
*/ |
| 400 |
MatrixType & |
| 401 |
matrix(size_type i); |
| 402 |
|
| 403 |
/** |
| 404 |
* import functions from private base class |
| 405 |
*/ |
| 406 |
using AnyData::name; |
| 407 |
using AnyData::size; |
| 408 |
}; |
| 409 |
|
| 410 |
|
| 411 |
/** |
| 412 |
* A vector of MGLevelObject<MatrixBlock>, which is implemented using shared |
| 413 |
* pointers, in order to allow for copying and rearranging. Each matrix block |
| 414 |
* can be identified by name. |
| 415 |
* |
| 416 |
* @relatesalso MatrixBlock |
| 417 |
* @ingroup vector_valued |
| 418 |
*/ |
| 419 |
template <typename MatrixType> |
| 420 |
class MGMatrixBlockVector : public EnableObserverPointer |
| 421 |
{ |
| 422 |
public: |
| 423 |
/** |
| 424 |
* Declare type for container size. |
| 425 |
*/ |
| 426 |
using size_type = types::global_dof_index; |
| 427 |
|
| 428 |
/** |
| 429 |
* The type of object stored. |
| 430 |
*/ |
| 431 |
using value_type = MGLevelObject<MatrixBlock<MatrixType>>; |
| 432 |
/** |
| 433 |
* Constructor, determining which matrices should be stored. |
| 434 |
* |
| 435 |
* If <tt>edge_matrices</tt> is true, then objects for edge matrices for |
| 436 |
* discretizations with degrees of freedom on faces are allocated. |
| 437 |
* |
| 438 |
* If <tt>edge_flux_matrices</tt> is true, then objects for DG fluxes on the |
| 439 |
* refinement edge are allocated. |
| 440 |
*/ |
| 441 |
MGMatrixBlockVector(const bool edge_matrices = false, |
| 442 |
const bool edge_flux_matrices = false); |
| 443 |
|
| 444 |
/** |
| 445 |
* The number of blocks. |
| 446 |
*/ |
| 447 |
unsigned int |
| 448 |
size() const; |
| 449 |
|
| 450 |
/** |
| 451 |
* Add a new matrix block at the position <tt>(row,column)</tt> in the block |
| 452 |
* system. The third argument allows to give the matrix a name for later |
| 453 |
* identification. |
| 454 |
*/ |
| 455 |
void |
| 456 |
add(size_type row, size_type column, const std::string &name); |
| 457 |
|
| 458 |
/** |
| 459 |
* For matrices using a SparsityPattern, this function reinitializes each |
| 460 |
* matrix in the vector with the correct pattern from the block system. |
| 461 |
* |
| 462 |
* This function reinitializes the level matrices. |
| 463 |
*/ |
| 464 |
void |
| 465 |
reinit_matrix(const MGLevelObject<BlockSparsityPattern> &sparsity); |
| 466 |
/** |
| 467 |
* For matrices using a SparsityPattern, this function reinitializes each |
| 468 |
* matrix in the vector with the correct pattern from the block system. |
| 469 |
* |
| 470 |
* This function reinitializes the matrices for degrees of freedom on the |
| 471 |
* refinement edge. |
| 472 |
*/ |
| 473 |
void |
| 474 |
reinit_edge(const MGLevelObject<BlockSparsityPattern> &sparsity); |
| 475 |
/** |
| 476 |
* For matrices using a SparsityPattern, this function reinitializes each |
| 477 |
* matrix in the vector with the correct pattern from the block system. |
| 478 |
* |
| 479 |
* This function reinitializes the flux matrices over the refinement edge. |
| 480 |
*/ |
| 481 |
void |
| 482 |
reinit_edge_flux(const MGLevelObject<BlockSparsityPattern> &sparsity); |
| 483 |
|
| 484 |
/** |
| 485 |
* Clear the object. |
| 486 |
* |
| 487 |
* Since often only clearing of the individual matrices is desired, but not |
| 488 |
* removing the blocks themselves, there is an optional argument. If the |
| 489 |
* argument is missing or @p false, all matrices will be empty, but the size |
| 490 |
* of this object and the block positions will not change. If @p |
| 491 |
* really_clean is @p true, then the object will contain no blocks at the |
| 492 |
* end. |
| 493 |
*/ |
| 494 |
void |
| 495 |
clear(bool really_clean = false); |
| 496 |
|
| 497 |
/** |
| 498 |
* Access a constant reference to the matrix block at position <i>i</i>. |
| 499 |
*/ |
| 500 |
const value_type & |
| 501 |
block(size_type i) const; |
| 502 |
|
| 503 |
/** |
| 504 |
* Access a reference to the matrix block at position <i>i</i>. |
| 505 |
*/ |
| 506 |
value_type & |
| 507 |
block(size_type i); |
| 508 |
|
| 509 |
/** |
| 510 |
* Access a constant reference to the edge matrix block at position |
| 511 |
* <i>i</i>. |
| 512 |
*/ |
| 513 |
const value_type & |
| 514 |
block_in(size_type i) const; |
| 515 |
|
| 516 |
/** |
| 517 |
* Access a reference to the edge matrix block at position <i>i</i>. |
| 518 |
*/ |
| 519 |
value_type & |
| 520 |
block_in(size_type i); |
| 521 |
|
| 522 |
/** |
| 523 |
* Access a constant reference to the edge matrix block at position |
| 524 |
* <i>i</i>. |
| 525 |
*/ |
| 526 |
const value_type & |
| 527 |
block_out(size_type i) const; |
| 528 |
|
| 529 |
/** |
| 530 |
* Access a reference to the edge matrix block at position <i>i</i>. |
| 531 |
*/ |
| 532 |
value_type & |
| 533 |
block_out(size_type i); |
| 534 |
|
| 535 |
/** |
| 536 |
* Access a constant reference to the edge flux matrix block at position |
| 537 |
* <i>i</i>. |
| 538 |
*/ |
| 539 |
const value_type & |
| 540 |
block_up(size_type i) const; |
| 541 |
|
| 542 |
/** |
| 543 |
* Access a reference to the edge flux matrix block at position <i>i</i>. |
| 544 |
*/ |
| 545 |
value_type & |
| 546 |
block_up(size_type i); |
| 547 |
|
| 548 |
/** |
| 549 |
* Access a constant reference to the edge flux matrix block at position |
| 550 |
* <i>i</i>. |
| 551 |
*/ |
| 552 |
const value_type & |
| 553 |
block_down(size_type i) const; |
| 554 |
|
| 555 |
/** |
| 556 |
* Access a reference to the edge flux matrix block at position <i>i</i>. |
| 557 |
*/ |
| 558 |
value_type & |
| 559 |
block_down(size_type i); |
| 560 |
|
| 561 |
/** |
| 562 |
* The memory used by this object. |
| 563 |
*/ |
| 564 |
std::size_t |
| 565 |
memory_consumption() const; |
| 566 |
|
| 567 |
private: |
| 568 |
/// Clear one of the matrix objects |
| 569 |
void |
| 570 |
clear_object(AnyData &); |
| 571 |
|
| 572 |
/// Flag for storing matrices_in and matrices_out |
| 573 |
const bool edge_matrices; |
| 574 |
|
| 575 |
/// Flag for storing flux_matrices_up and flux_matrices_down |
| 576 |
const bool edge_flux_matrices; |
| 577 |
|
| 578 |
/// The level matrices |
| 579 |
AnyData matrices; |
| 580 |
/// The matrix from the interior of a level to the refinement edge |
| 581 |
AnyData matrices_in; |
| 582 |
/// The matrix from the refinement edge to the interior of a level |
| 583 |
AnyData matrices_out; |
| 584 |
/// The DG flux from a level to the lower level |
| 585 |
AnyData flux_matrices_down; |
| 586 |
/// The DG flux from the lower level to a level |
| 587 |
AnyData flux_matrices_up; |
| 588 |
}; |
| 589 |
|
| 590 |
|
| 591 |
//----------------------------------------------------------------------// |
| 592 |
|
| 593 |
namespace internal |
| 594 |
{ |
| 595 |
template <typename MatrixType> |
| 596 |
void |
| 597 |
reinit(MatrixBlock<MatrixType> &v, const BlockSparsityPattern &p) |
| 598 |
{ |
| 599 |
v.row_indices = p.get_row_indices(); |
| 600 |
v.column_indices = p.get_column_indices(); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
template <typename number> |
| 605 |
void |
| 606 |
reinit(MatrixBlock<dealii::SparseMatrix<number>> &v, |
| 607 |
const BlockSparsityPattern &p) |
| 608 |
{ |
| 609 |
v.row_indices = p.get_row_indices(); |
| 610 |
v.column_indices = p.get_column_indices(); |
| 611 |
v.matrix.reinit(p.block(v.row, v.column)); |
| 612 |
} |
| 613 |
} // namespace internal |
| 614 |
|
| 615 |
|
| 616 |
template <typename MatrixType> |
| 617 |
inline MatrixBlock<MatrixType>::MatrixBlock() |
| 618 |
: row(numbers::invalid_size_type) |
| 619 |
, column(numbers::invalid_size_type) |
| 620 |
{} |
| 621 |
|
| 622 |
|
| 623 |
template <typename MatrixType> |
| 624 |
inline MatrixBlock<MatrixType>::MatrixBlock(size_type i, size_type j) |
| 625 |
: row(i) |
| 626 |
, column(j) |
| 627 |
{} |
| 628 |
|
| 629 |
|
| 630 |
template <typename MatrixType> |
| 631 |
inline void |
| 632 |
MatrixBlock<MatrixType>::reinit(const BlockSparsityPattern &sparsity) |
| 633 |
{ |
| 634 |
internal::reinit(*this, sparsity); |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
template <typename MatrixType> |
| 639 |
inline MatrixBlock<MatrixType>::operator MatrixType &() |
| 640 |
{ |
| 641 |
return matrix; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
template <typename MatrixType> |
| 646 |
inline MatrixBlock<MatrixType>::operator const MatrixType &() const |
| 647 |
{ |
| 648 |
return matrix; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
template <typename MatrixType> |
| 653 |
inline void |
| 654 |
MatrixBlock<MatrixType>::add(const size_type gi, |
| 655 |
const size_type gj, |
| 656 |
const typename MatrixType::value_type value) |
| 657 |
{ |
| 658 |
Assert(row_indices.size() != 0, ExcNotInitialized()); |
| 659 |
Assert(column_indices.size() != 0, ExcNotInitialized()); |
| 660 |
|
| 661 |
const std::pair<unsigned int, size_type> bi = row_indices.global_to_local(gi); |
| 662 |
const std::pair<unsigned int, size_type> bj = |
| 663 |
column_indices.global_to_local(gj); |
| 664 |
|
| 665 |
Assert(bi.first == row, ExcBlockIndexMismatch(bi.first, row)); |
| 666 |
Assert(bj.first == column, ExcBlockIndexMismatch(bj.first, column)); |
| 667 |
|
| 668 |
matrix.add(bi.second, bj.second, value); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
template <typename MatrixType> |
| 673 |
template <typename number> |
| 674 |
inline void |
| 675 |
MatrixBlock<MatrixType>::add(const std::vector<size_type> &r_indices, |
| 676 |
const std::vector<size_type> &c_indices, |
| 677 |
const FullMatrix<number> &values, |
| 678 |
const bool elide_zero_values) |
| 679 |
{ |
| 680 |
Assert(row_indices.size() != 0, ExcNotInitialized()); |
| 681 |
Assert(column_indices.size() != 0, ExcNotInitialized()); |
| 682 |
|
| 683 |
AssertDimension(r_indices.size(), values.m()); |
| 684 |
AssertDimension(c_indices.size(), values.n()); |
| 685 |
|
| 686 |
for (size_type i = 0; i < row_indices.size(); ++i) |
| 687 |
add(r_indices[i], |
| 688 |
c_indices.size(), |
| 689 |
c_indices.data(), |
| 690 |
&values(i, 0), |
| 691 |
elide_zero_values); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
template <typename MatrixType> |
| 696 |
template <typename number> |
| 697 |
inline void |
| 698 |
MatrixBlock<MatrixType>::add(const size_type b_row, |
| 699 |
const size_type n_cols, |
| 700 |
const size_type *col_indices, |
| 701 |
const number *values, |
| 702 |
const bool, |
| 703 |
const bool) |
| 704 |
{ |
| 705 |
Assert(row_indices.size() != 0, ExcNotInitialized()); |
| 706 |
Assert(column_indices.size() != 0, ExcNotInitialized()); |
| 707 |
|
| 708 |
const std::pair<unsigned int, size_type> bi = |
| 709 |
row_indices.global_to_local(b_row); |
| 710 |
|
| 711 |
// In debug mode, we check whether |
| 712 |
// all indices are in the correct |
| 713 |
// block. |
| 714 |
|
| 715 |
// Actually, for the time being, we |
| 716 |
// leave it at this. While it may |
| 717 |
// not be the most efficient way, |
| 718 |
// it is at least thread safe. |
| 719 |
// if constexpr (running_in_debug_mode()){ |
| 720 |
Assert(bi.first == row, ExcBlockIndexMismatch(bi.first, row)); |
| 721 |
|
| 722 |
for (size_type j = 0; j < n_cols; ++j) |
| 723 |
{ |
| 724 |
const std::pair<unsigned int, size_type> bj = |
| 725 |
column_indices.global_to_local(col_indices[j]); |
| 726 |
Assert(bj.first == column, ExcBlockIndexMismatch(bj.first, column)); |
| 727 |
|
| 728 |
matrix.add(bi.second, bj.second, values[j]); |
| 729 |
} |
| 730 |
// } |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
template <typename MatrixType> |
| 735 |
template <typename number> |
| 736 |
inline void |
| 737 |
MatrixBlock<MatrixType>::add(const std::vector<size_type> &indices, |
| 738 |
const FullMatrix<number> &values, |
| 739 |
const bool elide_zero_values) |
| 740 |
{ |
| 741 |
Assert(row_indices.size() != 0, ExcNotInitialized()); |
| 742 |
Assert(column_indices.size() != 0, ExcNotInitialized()); |
| 743 |
|
| 744 |
AssertDimension(indices.size(), values.m()); |
| 745 |
Assert(values.n() == values.m(), ExcNotQuadratic()); |
| 746 |
|
| 747 |
for (size_type i = 0; i < indices.size(); ++i) |
| 748 |
add(indices[i], |
| 749 |
indices.size(), |
| 750 |
indices.data(), |
| 751 |
&values(i, 0), |
| 752 |
elide_zero_values); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
template <typename MatrixType> |
| 758 |
template <typename number> |
| 759 |
inline void |
| 760 |
MatrixBlock<MatrixType>::add(const size_type row, |
| 761 |
const std::vector<size_type> &col_indices, |
| 762 |
const std::vector<number> &values, |
| 763 |
const bool elide_zero_values) |
| 764 |
{ |
| 765 |
Assert(row_indices.size() != 0, ExcNotInitialized()); |
| 766 |
Assert(column_indices.size() != 0, ExcNotInitialized()); |
| 767 |
|
| 768 |
AssertDimension(col_indices.size(), values.size()); |
| 769 |
add(row, |
| 770 |
col_indices.size(), |
| 771 |
col_indices.data(), |
| 772 |
values.data(), |
| 773 |
elide_zero_values); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
template <typename MatrixType> |
| 778 |
template <typename VectorType> |
| 779 |
inline void |
| 780 |
MatrixBlock<MatrixType>::vmult(VectorType &w, const VectorType &v) const |
| 781 |
{ |
| 782 |
matrix.vmult(w, v); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
template <typename MatrixType> |
| 787 |
template <typename VectorType> |
| 788 |
inline void |
| 789 |
MatrixBlock<MatrixType>::vmult_add(VectorType &w, const VectorType &v) const |
| 790 |
{ |
| 791 |
matrix.vmult_add(w, v); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
template <typename MatrixType> |
| 796 |
template <typename VectorType> |
| 797 |
inline void |
| 798 |
MatrixBlock<MatrixType>::Tvmult(VectorType &w, const VectorType &v) const |
| 799 |
{ |
| 800 |
matrix.Tvmult(w, v); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
template <typename MatrixType> |
| 805 |
template <typename VectorType> |
| 806 |
inline void |
| 807 |
MatrixBlock<MatrixType>::Tvmult_add(VectorType &w, const VectorType &v) const |
| 808 |
{ |
| 809 |
matrix.Tvmult_add(w, v); |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
template <typename MatrixType> |
| 814 |
inline std::size_t |
| 815 |
MatrixBlock<MatrixType>::memory_consumption() const |
| 816 |
{ |
| 817 |
return (sizeof(*this) + MemoryConsumption::memory_consumption(matrix) - |
| 818 |
sizeof(matrix)); |
| 819 |
} |
| 820 |
|
| 821 |
//----------------------------------------------------------------------// |
| 822 |
|
| 823 |
template <typename MatrixType> |
| 824 |
inline void |
| 825 |
MatrixBlockVector<MatrixType>::add(size_type row, |
| 826 |
size_type column, |
| 827 |
const std::string &name) |
| 828 |
{ |
| 829 |
ptr_type p(new value_type(row, column)); |
| 830 |
AnyData::add(p, name); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
template <typename MatrixType> |
| 835 |
inline void |
| 836 |
MatrixBlockVector<MatrixType>::reinit(const BlockSparsityPattern &sparsity) |
| 837 |
{ |
| 838 |
for (size_type i = 0; i < this->size(); ++i) |
| 839 |
{ |
| 840 |
block(i).reinit(sparsity); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
template <typename MatrixType> |
| 846 |
inline void |
| 847 |
MatrixBlockVector<MatrixType>::clear(bool really_clean) |
| 848 |
{ |
| 849 |
if (really_clean) |
| 850 |
{ |
| 851 |
DEAL_II_NOT_IMPLEMENTED(); |
| 852 |
} |
| 853 |
else |
| 854 |
{ |
| 855 |
for (size_type i = 0; i < this->size(); ++i) |
| 856 |
matrix(i).clear(); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
template <typename MatrixType> |
| 863 |
inline const MatrixBlock<MatrixType> & |
| 864 |
MatrixBlockVector<MatrixType>::block(size_type i) const |
| 865 |
{ |
| 866 |
return *this->read<ptr_type>(i); |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
template <typename MatrixType> |
| 871 |
inline MatrixBlock<MatrixType> & |
| 872 |
MatrixBlockVector<MatrixType>::block(size_type i) |
| 873 |
{ |
| 874 |
return *this->entry<ptr_type>(i); |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
template <typename MatrixType> |
| 879 |
inline MatrixType & |
| 880 |
MatrixBlockVector<MatrixType>::matrix(size_type i) |
| 881 |
{ |
| 882 |
return this->entry<ptr_type>(i)->matrix; |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
//----------------------------------------------------------------------// |
| 888 |
|
| 889 |
template <typename MatrixType> |
| 890 |
inline MGMatrixBlockVector<MatrixType>::MGMatrixBlockVector(const bool e, |
| 891 |
const bool f) |
| 892 |
: edge_matrices(e) |
| 893 |
, edge_flux_matrices(f) |
| 894 |
{} |
| 895 |
|
| 896 |
|
| 897 |
template <typename MatrixType> |
| 898 |
inline unsigned int |
| 899 |
MGMatrixBlockVector<MatrixType>::size() const |
| 900 |
{ |
| 901 |
return matrices.size(); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
template <typename MatrixType> |
| 906 |
inline void |
| 907 |
MGMatrixBlockVector<MatrixType>::add(size_type row, |
| 908 |
size_type column, |
| 909 |
const std::string &name) |
| 910 |
{ |
| 911 |
MGLevelObject<MatrixBlock<MatrixType>> p(0, 1); |
| 912 |
p[0].row = row; |
| 913 |
p[0].column = column; |
| 914 |
|
| 915 |
matrices.add(p, name); |
| 916 |
if (edge_matrices) |
| 917 |
{ |
| 918 |
matrices_in.add(p, name); |
| 919 |
matrices_out.add(p, name); |
| 920 |
} |
| 921 |
if (edge_flux_matrices) |
| 922 |
{ |
| 923 |
flux_matrices_up.add(p, name); |
| 924 |
flux_matrices_down.add(p, name); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
template <typename MatrixType> |
| 930 |
inline const MGLevelObject<MatrixBlock<MatrixType>> & |
| 931 |
MGMatrixBlockVector<MatrixType>::block(size_type i) const |
| 932 |
{ |
| 933 |
return *matrices.read<const MGLevelObject<MatrixType> *>(i); |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
template <typename MatrixType> |
| 938 |
inline MGLevelObject<MatrixBlock<MatrixType>> & |
| 939 |
MGMatrixBlockVector<MatrixType>::block(size_type i) |
| 940 |
{ |
| 941 |
return *matrices.entry<MGLevelObject<MatrixType> *>(i); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
template <typename MatrixType> |
| 946 |
inline const MGLevelObject<MatrixBlock<MatrixType>> & |
| 947 |
MGMatrixBlockVector<MatrixType>::block_in(size_type i) const |
| 948 |
{ |
| 949 |
return *matrices_in.read<const MGLevelObject<MatrixType> *>(i); |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
template <typename MatrixType> |
| 954 |
inline MGLevelObject<MatrixBlock<MatrixType>> & |
| 955 |
MGMatrixBlockVector<MatrixType>::block_in(size_type i) |
| 956 |
{ |
| 957 |
return *matrices_in.entry<MGLevelObject<MatrixType> *>(i); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
template <typename MatrixType> |
| 962 |
inline const MGLevelObject<MatrixBlock<MatrixType>> & |
| 963 |
MGMatrixBlockVector<MatrixType>::block_out(size_type i) const |
| 964 |
{ |
| 965 |
return *matrices_out.read<const MGLevelObject<MatrixType> *>(i); |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
template <typename MatrixType> |
| 970 |
inline MGLevelObject<MatrixBlock<MatrixType>> & |
| 971 |
MGMatrixBlockVector<MatrixType>::block_out(size_type i) |
| 972 |
{ |
| 973 |
return *matrices_out.entry<MGLevelObject<MatrixType> *>(i); |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
template <typename MatrixType> |
| 978 |
inline const MGLevelObject<MatrixBlock<MatrixType>> & |
| 979 |
MGMatrixBlockVector<MatrixType>::block_up(size_type i) const |
| 980 |
{ |
| 981 |
return *flux_matrices_up.read<const MGLevelObject<MatrixType> *>(i); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
template <typename MatrixType> |
| 986 |
inline MGLevelObject<MatrixBlock<MatrixType>> & |
| 987 |
MGMatrixBlockVector<MatrixType>::block_up(size_type i) |
| 988 |
{ |
| 989 |
return *flux_matrices_up.entry<MGLevelObject<MatrixType> *>(i); |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
template <typename MatrixType> |
| 994 |
inline const MGLevelObject<MatrixBlock<MatrixType>> & |
| 995 |
MGMatrixBlockVector<MatrixType>::block_down(size_type i) const |
| 996 |
{ |
| 997 |
return *flux_matrices_down.read<const MGLevelObject<MatrixType> *>(i); |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
template <typename MatrixType> |
| 1002 |
inline MGLevelObject<MatrixBlock<MatrixType>> & |
| 1003 |
MGMatrixBlockVector<MatrixType>::block_down(size_type i) |
| 1004 |
{ |
| 1005 |
return *flux_matrices_down.entry<MGLevelObject<MatrixType> *>(i); |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
template <typename MatrixType> |
| 1010 |
inline void |
| 1011 |
MGMatrixBlockVector<MatrixType>::reinit_matrix( |
| 1012 |
const MGLevelObject<BlockSparsityPattern> &sparsity) |
| 1013 |
{ |
| 1014 |
for (size_type i = 0; i < this->size(); ++i) |
| 1015 |
{ |
| 1016 |
MGLevelObject<MatrixBlock<MatrixType>> &o = block(i); |
| 1017 |
const size_type row = o[o.min_level()].row; |
| 1018 |
const size_type col = o[o.min_level()].column; |
| 1019 |
|
| 1020 |
o.resize(sparsity.min_level(), sparsity.max_level()); |
| 1021 |
for (size_type level = o.min_level(); level <= o.max_level(); ++level) |
| 1022 |
{ |
| 1023 |
o[level].row = row; |
| 1024 |
o[level].column = col; |
| 1025 |
internal::reinit(o[level], sparsity[level]); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
template <typename MatrixType> |
| 1032 |
inline void |
| 1033 |
MGMatrixBlockVector<MatrixType>::reinit_edge( |
| 1034 |
const MGLevelObject<BlockSparsityPattern> &sparsity) |
| 1035 |
{ |
| 1036 |
for (size_type i = 0; i < this->size(); ++i) |
| 1037 |
{ |
| 1038 |
MGLevelObject<MatrixBlock<MatrixType>> &o = block(i); |
| 1039 |
const size_type row = o[o.min_level()].row; |
| 1040 |
const size_type col = o[o.min_level()].column; |
| 1041 |
|
| 1042 |
block_in(i).resize(sparsity.min_level(), sparsity.max_level()); |
| 1043 |
block_out(i).resize(sparsity.min_level(), sparsity.max_level()); |
| 1044 |
for (size_type level = o.min_level(); level <= o.max_level(); ++level) |
| 1045 |
{ |
| 1046 |
block_in(i)[level].row = row; |
| 1047 |
block_in(i)[level].column = col; |
| 1048 |
internal::reinit(block_in(i)[level], sparsity[level]); |
| 1049 |
block_out(i)[level].row = row; |
| 1050 |
block_out(i)[level].column = col; |
| 1051 |
internal::reinit(block_out(i)[level], sparsity[level]); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
template <typename MatrixType> |
| 1058 |
inline void |
| 1059 |
MGMatrixBlockVector<MatrixType>::reinit_edge_flux( |
| 1060 |
const MGLevelObject<BlockSparsityPattern> &sparsity) |
| 1061 |
{ |
| 1062 |
for (size_type i = 0; i < this->size(); ++i) |
| 1063 |
{ |
| 1064 |
MGLevelObject<MatrixBlock<MatrixType>> &o = block(i); |
| 1065 |
const size_type row = o[o.min_level()].row; |
| 1066 |
const size_type col = o[o.min_level()].column; |
| 1067 |
|
| 1068 |
block_up(i).resize(sparsity.min_level(), sparsity.max_level()); |
| 1069 |
block_down(i).resize(sparsity.min_level(), sparsity.max_level()); |
| 1070 |
for (size_type level = o.min_level(); level <= o.max_level(); ++level) |
| 1071 |
{ |
| 1072 |
block_up(i)[level].row = row; |
| 1073 |
block_up(i)[level].column = col; |
| 1074 |
internal::reinit(block_up(i)[level], sparsity[level]); |
| 1075 |
block_down(i)[level].row = row; |
| 1076 |
block_down(i)[level].column = col; |
| 1077 |
internal::reinit(block_down(i)[level], sparsity[level]); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
template <typename MatrixType> |
| 1084 |
inline void |
| 1085 |
MGMatrixBlockVector<MatrixType>::clear_object(AnyData &mo) |
| 1086 |
{ |
| 1087 |
for (size_type i = 0; i < mo.size(); ++i) |
| 1088 |
{ |
| 1089 |
MGLevelObject<MatrixBlock<MatrixType>> &o = |
| 1090 |
mo.entry<MGLevelObject<MatrixType> *>(i); |
| 1091 |
for (size_type level = o.min_level(); level <= o.max_level(); ++level) |
| 1092 |
o[level].matrix.clear(); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
template <typename MatrixType> |
| 1098 |
inline void |
| 1099 |
MGMatrixBlockVector<MatrixType>::clear(bool really_clean) |
| 1100 |
{ |
| 1101 |
if (really_clean) |
| 1102 |
{ |
| 1103 |
DEAL_II_NOT_IMPLEMENTED(); |
| 1104 |
} |
| 1105 |
else |
| 1106 |
{ |
| 1107 |
clear_object(matrices); |
| 1108 |
clear_object(matrices_in); |
| 1109 |
clear_object(matrices_out); |
| 1110 |
clear_object(flux_matrices_up); |
| 1111 |
clear_object(flux_matrices_down); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
DEAL_II_NAMESPACE_CLOSE |
| 1118 |
|
| 1119 |
#endif |