/usr/include/deal.II/lac/block_matrix_array.h is in libdeal.ii-dev 8.1.0-6ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 | // ---------------------------------------------------------------------
// $Id: block_matrix_array.h 30036 2013-07-18 16:55:32Z maier $
//
// Copyright (C) 2001 - 2013 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef __deal2__block_matrix_array_h
#define __deal2__block_matrix_array_h
#include <deal.II/base/config.h>
#include <deal.II/base/subscriptor.h>
#include <deal.II/base/table.h>
#include <deal.II/lac/pointer_matrix.h>
#include <deal.II/lac/vector_memory.h>
#include <vector>
#include <map>
#include <string>
#include <memory>
#include <sstream>
DEAL_II_NAMESPACE_OPEN
template <typename> class BlockVector;
template <typename> class Vector;
/*! @addtogroup Matrix2
*@{
*/
/**
* Block matrix composed of different single matrices; these matrices
* may even be of different types.
*
* Given a set of arbitrary matrices <i>A<sub>i</sub></i>, this class
* implements a block matrix with block entries of the form
* <i>M<sub>jk</sub> = s<sub>jk</sub>A<sub>i</sub></i>. Each
* <i>A<sub>i</sub></i> may be used several times with different
* prefix. The matrices are not copied into the BlockMatrixArray
* object, but rather a PointerMatrix referencing each of them will be
* stored along with factors and transposition flags.
*
* Non-zero entries are registered by the function enter(), zero
* entries are not stored at all. Using enter() with the same location
* <tt>(i,j)</tt> several times will add the corresponding matrices in
* matrix-vector multiplications. These matrices will not be actually
* added, but the multiplications with them will be summed up.
*
* @note This mechanism makes it impossible to access single entries
* of BlockMatrixArray. In particular, (block) relaxation
* preconditioners based on PreconditionRelaxation or
* PreconditionBlock <b>cannot</b> be used with this class. If you
* need a preconditioner for a BlockMatrixArray object, use
* BlockTrianglePrecondition.
*
* <h3>Requirements on MATRIX</h3>
*
* The template argument <tt>MATRIX</tt> is a class providing the
* matrix-vector multiplication functions vmult(), Tvmult(),
* vmult_add() and Tvmult_add() used in this class, but with arguments
* of type Vector<number> instead of
* BlockVector<number>. Every matrix which can be used by
* PointerMatrix is allowed, in particular SparseMatrix is a possible
* entry type.
*
* <h3>Example program</h3>
* We document the relevant parts of <tt>examples/doxygen/block_matrix_array.cc</tt>.
*
* @dontinclude block_matrix_array.cc
*
* Obviously, we have to include the header file containing the definition
* of BlockMatrixArray:
* @skipline block_matrix_array.h
*
* First, we set up some matrices to be entered into the blocks.
* @skip main
* @until C.fill
*
* The BlockMatrixArray needs a VectorMemory<Vector<number>
* > object to allocate auxiliary vectors. <tt>number</tt> must
* equal the second template argument of BlockMatrixArray and also the
* number type of the BlockVector used later. We use the
* GrowingVectorMemory type, since it remembers the vector and avoids
* reallocating.
*
* @line Growing
*
* Now, we are ready to build a <i>2x2</i> BlockMatrixArray.
* @line Block
* First, we enter the matrix <tt>A</tt> multiplied by 2 in the upper left block
* @line enter
* Now -1 times <tt>B1</tt> in the upper right block.
* @line enter
* We add the transpose of <tt>B2</tt> to the upper right block and
* continue in a similar fashion. In the end, the block matrix
* structure is printed into an LaTeX table.
* @until latex
*
* Now, we set up vectors to be multiplied with this matrix and do a
* multiplication.
* @until vmult
*
* Finally, we solve a linear system with BlockMatrixArray, using no
* preconditioning and the conjugate gradient method.
* @until Error
*
* The remaining code of this sample program concerns preconditioning
* and is described in the documentation of
* BlockTrianglePrecondition.
*
* @see @ref GlossBlockLA "Block (linear algebra)"
* @author Guido Kanschat
* @date 2000-2005, 2010
*/
template <typename number = double>
class BlockMatrixArray : public Subscriptor
{
public:
/**
* Declare the type for container size.
*/
typedef types::global_dof_index size_type;
/**
* Default constructor creating a
* useless object. initialize()
* must be called before using
* it.
*/
BlockMatrixArray ();
/**
* Constructor fixing the
* dimensions.
*/
BlockMatrixArray (const unsigned int n_block_rows,
const unsigned int n_block_cols);
/**
* Initialize object
* completely. This is the
* function to call for an object
* created by the default
* constructor.
*/
void initialize (const unsigned int n_block_rows,
const unsigned int n_block_cols);
/**
* Constructor fixing the
* dimensions.
*
* @deprecated The last argument is ignored. Use the constructor with only
* the first two arguments.
*/
BlockMatrixArray (const unsigned int n_block_rows,
const unsigned int n_block_cols,
VectorMemory<Vector<number> > &mem) DEAL_II_DEPRECATED;
/**
* Initialize object
* completely. This is the
* function to call for an object
* created by the default
* constructor.
*
* @deprecated The last argument is ignored. Use the function with same name
* but only the first two arguments.
*/
void initialize (const unsigned int n_block_rows,
const unsigned int n_block_cols,
VectorMemory<Vector<number> > &mem) DEAL_II_DEPRECATED;
/**
* Adjust the matrix to a new
* size and delete all blocks.
*/
void reinit (const unsigned int n_block_rows,
const unsigned int n_block_cols);
/**
* Add a block matrix entry. The
* <tt>matrix</tt> is entered
* into a list of blocks for
* multiplication, together with
* its coordinates <tt>row</tt>
* and <tt>col</tt> as well as
* optional multiplication factor
* <tt>prefix</tt> and transpose
* flag <tt>transpose</tt>.
*
* @note No check for consistency
* of block sizes is
* made. Therefore, entering a
* block of wrong dimension here
* will only lead to a
* ExcDimensionMismatch in one of
* the multiplication functions.
*/
template <class MATRIX>
void enter (const MATRIX &matrix,
const unsigned int row,
const unsigned int col,
const double prefix = 1.,
const bool transpose = false);
/**
* Add an entry like with enter,
* but use PointerMatrixAux for
* matrices not having functions
* vmult_add() and TVmult_add().
*
* @deprecated The first argument
* is ignored. Use the function with same name
* but without the first argument.
*/
template <class MATRIX>
void enter_aux (VectorMemory<Vector<number> > &mem,
const MATRIX &matrix,
const unsigned int row,
const unsigned int col,
const double prefix = 1.,
const bool transpose = false) DEAL_II_DEPRECATED;
/**
* Delete all entries, i.e. reset
* the matrix to an empty state.
*/
void clear();
/**
* Number of block-entries per
* column.
*/
unsigned int n_block_rows () const;
/**
* Number of block-entries per
* row.
*/
unsigned int n_block_cols () const;
/**
* Matrix-vector multiplication.
*/
void vmult (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Matrix-vector multiplication
* adding to <tt>dst</tt>.
*/
void vmult_add (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Transposed matrix-vector
* multiplication.
*/
void Tvmult (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Transposed matrix-vector
* multiplication adding to
* <tt>dst</tt>.
*/
void Tvmult_add (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Matrix scalar product between
* two vectors (at least for a
* symmetric matrix).
*/
number matrix_scalar_product (const BlockVector<number> &u,
const BlockVector<number> &v) const;
/**
* Compute $u^T M u$. This is the square
* of the norm induced by the matrix
* assuming the matrix is symmetric
* positive definitive.
*/
number matrix_norm_square (const BlockVector<number> &u) const;
/**
* Print the block structure as a
* LaTeX-array. This output will
* not be very intuitive, since
* the matrix object lacks
* important information. What
* you see is an entry for each
* block showing all the matrices
* with their multiplicaton
* factors and possibly transpose
* mark. The matrices itself are
* numbered successively upon
* being entred. If the same
* matrix is entered several
* times, it will be listed with
* the same number everytime.
*/
template <class STREAM>
void print_latex (STREAM &out) const;
protected:
/**
* Internal data structure.
*
* For each entry of a
* BlockMatrixArray, its
* position, matrix, prefix and
* optional transposition must be
* stored. This structure
* encapsulates all of them.
*
* @author Guido Kanschat, 2000, 2001
*/
class Entry
{
public:
/**
* Constructor initializing
* all data fields. A
* PointerMatrix object is
* generated for
* <tt>matrix</tt>.
*/
template<class MATRIX>
Entry (const MATRIX &matrix,
size_type row, size_type col,
double prefix, bool transpose);
/**
* Copy constructor
* invalidating the old
* object. Since it is only
* used for entering
* temporary objects into a
* vector, this is ok.
*
* For a deep copy, we would
* need a reproduction
* operator in
* PointerMatixBase.
*/
Entry(const Entry &);
/**
* Destructor, where we
* delete the PointerMatrix
* created by the
* constructor.
*/
~Entry();
/**
* Row number in the block
* matrix.
*/
size_type row;
/**
* Column number in the block
* matrix.
*/
size_type col;
/**
* Factor in front of the matrix
* block.
*/
double prefix;
/**
* Indicates that matrix block
* must be transposed for
* multiplication.
*/
bool transpose;
/**
* The matrix block itself.
*/
PointerMatrixBase<Vector<number> > *matrix;
};
/**
* Array of block entries in the
* matrix.
*/
std::vector<Entry> entries;
private:
/**
* Number of blocks per column.
*/
unsigned int block_rows;
/**
* number of blocks per row.
*/
unsigned int block_cols;
};
/*@}*/
/**
* Inversion of a block-triangular matrix.
*
* In this block matrix, the inverses of the diagonal blocks are
* stored together with the off-diagonal blocks of a block
* matrix. Then, forward or backward insertion is performed
* block-wise. The diagonal blocks are NOT inverted for this purpose!
*
* Like for all preconditioners, the preconditioning operation is
* performed by the vmult() member function.
*
* @note While block indices may be duplicated (see BlockMatrixArray)
* to add blocks, this has to be used with caution, since
* summing up the inverse of two blocks does not yield the inverse of
* the sum. While the latter would be desirable, we can only perform
* the first.
*
* The implementation may be a little clumsy, but it should be
* sufficient as long as the block sizes are much larger than the
* number of blocks.
*
* <h3>Example</h3>
* Here, we document the second part of
* <tt>examples/doxygen/block_matrix_array.cc</tt>. For the beginning
* of this file, see BlockMatrixArray.
*
* In order to set up the preconditioner, we have to compute the
* inverses of the diagonal blocks ourselves. Since we used FullMatrix
* objects, this is fairly easy.
* @dontinclude block_matrix_array.cc
* @skip Error
* @until Cinv.invert
*
* After creating a <i>2x2</i> BlockTrianglePrecondition object, we
* only fill its diagonals. The scaling factor <i>1/2</i> used for
* <tt>A</tt> is the reciprocal of the scaling factor used for the
* <tt>matrix</tt> itself. Remember, this preconditioner actually
* <b>multiplies</b> with the diagonal blocks.
* @until Cinv
*
* Now, we have a block Jacobi preconditioner, which is still
* symmetric, since the blocks are symmetric. Therefore, we can still
* use the preconditioned conjugate gradient method.
* @until Error
*
* Now, we enter the subdiagonal block. This is the same as in
* <tt>matrix</tt>.
* @until B2
*
* Since the preconditioner is not symmetric anymore, we use the GMRES
* method for solving.
* @until Error
*
*
* @ingroup Preconditioners
* @author Guido Kanschat, 2001, 2005
*/
template <typename number = double>
class BlockTrianglePrecondition
: private BlockMatrixArray<number>
{
public:
/**
* Declare type for container size.
*/
typedef types::global_dof_index size_type;
/**
* Default constructor creating a
* useless object. initialize()
* must be called before using
* it.
*/
BlockTrianglePrecondition ();
/**
* Constructor. This matrix must be
* block-quadratic, and
* <tt>n_blocks</tt> is the
* number of blocks in each direction.
*/
BlockTrianglePrecondition (const unsigned int n_blocks);
/**
* Constructor. This matrix must be
* block-quadratic. The additional
* parameter allows for backward
* insertion instead of forward.
*
* @deprecated The second argument is ignored. Use the constructor with only
* the first and third argument.
*/
BlockTrianglePrecondition (const unsigned int n_block_rows,
VectorMemory<Vector<number> > &mem,
const bool backward = false) DEAL_II_DEPRECATED;
/**
* Initialize object
* completely. This is the
* function to call for an object
* created by the default
* constructor.
*
* @deprecated The second argument
* is ignored. Use the function without that argument.
*/
void initialize (const unsigned int n_block_rows,
VectorMemory<Vector<number> > &mem,
const bool backward = false) DEAL_II_DEPRECATED;
/**
* Resize preconditioner to a new
* size and clear all blocks.
*/
void reinit (const unsigned int n_block_rows);
/**
* Enter a block. This calls
* BlockMatrixArray::enter(). Remember
* that the diagonal blocks
* should actually be inverse
* matrices or preconditioners.
*/
template <class MATRIX>
void enter (const MATRIX &matrix,
const size_type row,
const size_type col,
const double prefix = 1.,
const bool transpose = false);
/**
* Enter a block. This calls
* BlockMatrixArray::enter_aux(). Remember
* that the diagonal blocks
* should actually be inverse
* matrices or preconditioners.
*
* @deprecated The first
* argument is ignored. Use
* enter() instead.
*/
template <class MATRIX>
void enter_aux (VectorMemory<Vector<double> > &mem,
const MATRIX &matrix,
const size_type row,
const size_type col,
const double prefix = 1.,
const bool transpose = false) DEAL_II_DEPRECATED;
/**
* Preconditioning.
*/
void vmult (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Preconditioning
* adding to <tt>dst</tt>.
*/
void vmult_add (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Transposed preconditioning
*/
void Tvmult (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Transposed preconditioning
* adding to <tt>dst</tt>.
*/
void Tvmult_add (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Make function of base class available.
*/
using BlockMatrixArray<number>::print_latex;
/**
* Make function of base class available.
*/
using BlockMatrixArray<number>::n_block_rows;
/**
* Make function of base class available.
*/
using BlockMatrixArray<number>::n_block_cols;
using BlockMatrixArray<number>::clear;
using BlockMatrixArray<number>::Subscriptor::subscribe;
using BlockMatrixArray<number>::Subscriptor::unsubscribe;
/** @addtogroup Exceptions
* @{ */
/**
* Each diagonal block must
* contain one and only one
* matrix. If this exception is
* thrown, you did not enter a
* matrix here.
*/
DeclException1(ExcNoDiagonal,
size_type,
<< "No diagonal entry was added for block " << arg1);
/**
* Each diagonal block must
* contain one and only one
* matrix. If this exception is
* thrown, you entered a second
* matrix here.
*/
DeclException1(ExcMultipleDiagonal,
size_type,
<< "Inverse diagonal entries may not be added in block "
<< arg1);
//@}
private:
/**
* Add all off-diagonal
* contributions and return the
* entry of the diagonal element
* for one row.
*/
void do_row (BlockVector<number> &dst,
size_type row_num) const;
/**
* Flag for backward insertion.
*/
bool backward;
};
#ifndef DOXYGEN
//---------------------------------------------------------------------------
template <typename number>
template <class MATRIX>
inline
BlockMatrixArray<number>::Entry::Entry (
const MATRIX &m,
size_type row,
size_type col,
double prefix,
bool transpose)
:
row (row),
col (col),
prefix (prefix),
transpose (transpose),
matrix (new_pointer_matrix_base(m, Vector<number>(), typeid(*this).name()))
{}
template <typename number>
template <class MATRIX>
inline
void
BlockMatrixArray<number>::enter (
const MATRIX &matrix,
unsigned int row,
unsigned int col,
double prefix,
bool transpose)
{
Assert(row<n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
Assert(col<n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
entries.push_back(Entry(matrix, row, col, prefix, transpose));
}
template <typename number>
template <class MATRIX>
inline
void
BlockMatrixArray<number>::enter_aux (
VectorMemory<Vector<number> > &mem,
const MATRIX &matrix,
unsigned int row,
unsigned int col,
double prefix,
bool transpose)
{
Assert(row<n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
Assert(col<n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
entries.push_back(Entry(matrix, row, col, prefix, transpose, mem));
}
template<typename number>
struct BlockMatrixArrayPointerMatrixLess
{
bool operator () (const PointerMatrixBase<Vector<number> > *a,
const PointerMatrixBase<Vector<number> > *b) const
{
return *a < *b;
}
};
template <typename number>
template <class STREAM>
inline
void
BlockMatrixArray<number>::print_latex (STREAM &out) const
{
out << "\\begin{array}{"
<< std::string(n_block_cols(), 'c')
<< "}" << std::endl;
Table<2,std::string> array(n_block_rows(), n_block_cols());
typedef std::map<const PointerMatrixBase<Vector<number> > *,
std::string, BlockMatrixArrayPointerMatrixLess<number> > NameMap;
NameMap matrix_names;
typename std::vector<Entry>::const_iterator m = entries.begin();
typename std::vector<Entry>::const_iterator end = entries.end();
size_type matrix_number = 0;
for (; m != end ; ++m)
{
if (matrix_names.find(m->matrix) == matrix_names.end())
{
std::pair<typename NameMap::iterator, bool> x =
matrix_names.insert(
std::pair<const PointerMatrixBase<Vector<number> >*, std::string> (m->matrix,
std::string("M")));
std::ostringstream stream;
stream << matrix_number++;
x.first->second += stream.str();
}
std::ostringstream stream;
if (array(m->row, m->col) != "" && m->prefix >= 0)
stream << "+";
if (m->prefix != 1.)
stream << m->prefix << 'x';
stream << matrix_names.find(m->matrix)->second;
// stream << '(' << m->matrix << ')';
if (m->transpose)
stream << "^T";
array(m->row, m->col) += stream.str();
}
for (unsigned int i=0; i<n_block_rows(); ++i)
for (unsigned int j=0; j<n_block_cols(); ++j)
{
out << '\t' << array(i,j);
if (j==n_block_cols()-1)
out << "\\\\" << std::endl;
else
out << " &";
}
out << "\\end{array}" << std::endl;
}
template <typename number>
template <class MATRIX>
inline
void
BlockTrianglePrecondition<number>::enter (const MATRIX &matrix,
size_type row, size_type col,
double prefix, bool transpose)
{
BlockMatrixArray<number>::enter(matrix, row, col, prefix, transpose);
}
template <typename number>
template <class MATRIX>
inline
void
BlockTrianglePrecondition<number>::enter_aux (
VectorMemory<Vector<double> > &mem,
const MATRIX &matrix,
size_type row,
size_type col,
double prefix,
bool transpose)
{
BlockMatrixArray<number>::enter_aux(mem, matrix, row, col, prefix, transpose);
}
#endif // DOXYGEN
DEAL_II_NAMESPACE_CLOSE
#endif
|