/usr/include/deal.II/lac/matrix_block.h is in libdeal.ii-dev 6.3.1-1.1.
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 | //---------------------------------------------------------------------------
// $Id: matrix_block.h 21131 2010-05-15 14:05:24Z kronbichler $
// Version: $Name$
//
// Copyright (C) 2007, 2008, 2009, 2010 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__matrix_block_h
#define __deal2__matrix_block_h
#include <base/config.h>
#include <base/named_data.h>
#include <base/smartpointer.h>
#include <base/std_cxx1x/shared_ptr.h>
#include <lac/block_indices.h>
#include <lac/full_matrix.h>
DEAL_II_NAMESPACE_OPEN
/**
* A wrapper around a matrix object, storing the coordinates in a
* block matrix as well.
*
* This class is an alternative to BlockMatrixBase, if you only want
* to generate a single block of the system, not the whole
* system. Using the add() functions of this class, it is possible to
* use the standard assembling functions used for block matrices, but
* only enter in one of the blocks and still avoiding the index
* computations innvolved.
* The reason for this class is, that we may need a different number
* of matrices for different blocks in a block system. For example, a
* preconditioner for the Oseen system can be built as a block system,
* where the pressure block is of the form
* <b>M</b><sup>-1</sup><b>FA</b><sup>-1</sup> with <b>M</b> the
* pressure mass matrix, <b>A</b> the pressure Laplacian and <b>F</b>
* the advection diffusion operator applied to the pressure
* space. Since only a single matrix is needed for the other blocks,
* using BlockSparseMatrix or similar would be a waste of memory.
*
* While the add() functions make a MatrixBlock appear like a block
* matrix for assembling, the functions vmult(), Tvmult(),
* vmult_add(), and Tvmult_add() make it behave like a MATRIX, when it
* comes to applying it to a vector. This behavior allows us to store
* MatrixBlock objects in vectors, for instance in MGLevelObject
* without extracting the #matrix first.
*
* MatrixBlock comes handy when using BlockMatrixArray. Once the
* MatrixBlock has been properly initalized and filled, it can be used
* in the simplest case as:
* @code
* MatrixBlockVector<SparseMatrix<double> > > blocks;
*
* ...
*
* BlockMatrixArray matrix (n_blocks, n_blocks);
*
* for (unsigned int i=0;i<blocks.size;++i)
* matrix.enter(blocks.block(i).row, blocks.block(i).column, blocks.matrix(i));
* @endcode
*
* Here, we have not gained very much, except that we do not need to
* set up empty blocks in the block system.
*
* @todo Example for the product preconditioner of the pressure Schur
* complement.
*
* @ingroup Matrix2
* @ingroup vector_valued
* @see @ref GlossBlockLA "Block (linear algebra)"
* @author Guido Kanschat, 2006
*/
template <class MATRIX>
class MatrixBlock
{
public:
/**
* Constructor rendering an
* uninitialized object.
*/
MatrixBlock();
/**
* Copy constructor.
*/
MatrixBlock(const MatrixBlock<MATRIX>& M);
/**
* Constructor setting block
* coordinates, but not
* initializing the matrix.
*/
MatrixBlock(unsigned int i, unsigned int j,
const BlockIndices* block_indices = 0);
/**
* Add <tt>value</tt> to the
* element (<i>i,j</i>). Throws
* an error if the entry does not
* exist or if it is in a
* different block.
*/
void add (const unsigned int i,
const unsigned int j,
const typename MATRIX::value_type value);
/**
* Add all elements in a
* FullMatrix into sparse
* matrix locations given by
* <tt>indices</tt>. This function
* assumes a quadratic sparse
* matrix and a quadratic
* full_matrix. The global
* locations are translated
* into locations in this block
* and ExcBlockIndexMismatch is
* thrown, if the global index
* does not point into the
* block refered to by #row and
* #column.
*
* @todo
* <tt>elide_zero_values</tt> is
* currently ignored.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
template <typename number>
void add (const std::vector<unsigned int>& indices,
const FullMatrix<number>& full_matrix,
const bool elide_zero_values = true);
/**
* Add all elements in a
* FullMatrix into global
* locations given by
* <tt>row_indices</tt> and
* <tt>col_indices</tt>,
* respectively. The global
* locations are translated
* into locations in this block
* and ExcBlockIndexMismatch is
* thrown, if the global index
* does not point into the
* block refered to by #row and
* #column.
*
* @todo
* <tt>elide_zero_values</tt> is
* currently ignored.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
template <typename number>
void add (const std::vector<unsigned int>& row_indices,
const std::vector<unsigned int>& col_indices,
const FullMatrix<number>& full_matrix,
const bool elide_zero_values = true);
/**
* Set several elements in the
* specified row of the matrix
* with column indices as given
* by <tt>col_indices</tt> to
* the respective value. This
* is the function doing thye
* actual work for the ones
* adding full matrices. The
* global locations
* <tt>row_index</tt> and
* <tt>col_indices</tt> are
* translated into locations in
* this block and
* ExcBlockIndexMismatch is
* thrown, if the global index
* does not point into the
* block refered to by #row and
* #column.
*
* @todo
* <tt>elide_zero_values</tt> is
* currently ignored.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
template <typename number>
void add (const unsigned int row_index,
const std::vector<unsigned int>& col_indices,
const std::vector<number>& values,
const bool elide_zero_values = true);
/**
* Add an array of values given by
* <tt>values</tt> in the given
* global matrix row at columns
* specified by col_indices in the
* sparse matrix.
*
* The optional parameter
* <tt>elide_zero_values</tt> can be
* used to specify whether zero
* values should be added anyway or
* these should be filtered away and
* only non-zero data is added. The
* default value is <tt>true</tt>,
* i.e., zero values won't be added
* into the matrix.
*/
template <typename number>
void add (const unsigned int row,
const unsigned int n_cols,
const unsigned int *col_indices,
const number *values,
const bool elide_zero_values = true,
const bool col_indices_are_sorted = false);
/**
* Matrix-vector-multiplication,
* forwarding to the same
* function in MATRIX. No index
* computations are done, thus,
* the vectors need to have sizes
* matching #matrix.
*/
template<class VECTOR>
void vmult (VECTOR& w, const VECTOR& v) const;
/**
* Matrix-vector-multiplication,
* forwarding to the same
* function in MATRIX. No index
* computations are done, thus,
* the vectors need to have sizes
* matching #matrix.
*/
template<class VECTOR>
void vmult_add (VECTOR& w, const VECTOR& v) const;
/**
* Matrix-vector-multiplication,
* forwarding to the same
* function in MATRIX. No index
* computations are done, thus,
* the vectors need to have sizes
* matching #matrix.
*/
template<class VECTOR>
void Tvmult (VECTOR& w, const VECTOR& v) const;
/**
* Matrix-vector-multiplication,
* forwarding to the same
* function in MATRIX. No index
* computations are done, thus,
* the vectors need to have sizes
* matching #matrix.
*/
template<class VECTOR>
void Tvmult_add (VECTOR& w, const VECTOR& v) const;
/**
* The block number computed from
* an index by using
* #block_indices does not match
* the block coordinates stored
* in this object.
*/
DeclException2(ExcBlockIndexMismatch, unsigned int, unsigned int,
<< "Block index " << arg1 << " does not match " << arg2);
/**
* Row coordinate. This is the
* position of the data member
* matrix on the global matrix.
*/
unsigned int row;
/**
* Column coordinate. This is
* the position of the data
* member matrix on the global
* matrix.
*/
unsigned int column;
/**
* The matrix itself
*/
MATRIX matrix;
/**
* The BlockIndices of the whole
* system. Using row() and
* column(), this allows us to
* find the index of the first
* row and column degrees of
* freedom for this block.
*/
SmartPointer<const BlockIndices, MatrixBlock<MATRIX> > block_indices;
};
/**
* A vector of MatrixBlock, which is implemented using shared
* pointers, in order to allow for copying and rearranging. Each
* matrix block can be identified by name.
*
* @relates MatrixBlock
* @ingroup vector_valued
* @author Baerbel Janssen, Guido Kanschat, 2010
*/
template <class MATRIX>
class MatrixBlockVector : public NamedData<std_cxx1x::shared_ptr<MatrixBlock<MATRIX> > >
{
public:
/**
* Add a new matrix block at the
* position <tt>(row,column)</tt>
* in the block system.
*/
void add(unsigned int row, unsigned int column,
const std::string& name,
const BlockIndices* block_indices);
/**
* Access a constant reference to
* the block at position <i>i</i>.
*/
const MatrixBlock<MATRIX>& block(unsigned int i) const;
/**
* Access a reference to
* the block at position <i>i</i>.
*/
MatrixBlock<MATRIX>& block(unsigned int i);
/**
* Access the matrix at position
* <i>i</i> for read and write
* access.
*/
MATRIX& matrix(unsigned int i);
};
/**
* A vector of MGLevelObject<MatrixBlock>, which is implemented using shared
* pointers, in order to allow for copying and rearranging. Each
* matrix block can be identified by name.
*
* @relates MatrixBlock
* @ingroup vector_valued
* @author Baerbel Janssen, Guido Kanschat, 2010
*/
template <class MATRIX>
class MGMatrixBlockVector : public NamedData<std_cxx1x::shared_ptr<MatrixBlock<MATRIX> > >
{
public:
/**
* Add a new matrix block at the
* position <tt>(row,column)</tt>
* in the block system.
*/
void add(unsigned int row, unsigned int column,
const std::string& name,
const BlockIndices* block_indices);
/**
* Access a constant reference to
* the block at position <i>i</i>.
*/
const MatrixBlock<MATRIX>& block(unsigned int i) const;
/**
* Access a reference to
* the block at position <i>i</i>.
*/
MatrixBlock<MATRIX>& block(unsigned int i);
/**
* Access the matrix at position
* <i>i</i> for read and write
* access.
*/
MATRIX& matrix(unsigned int i);
};
//----------------------------------------------------------------------//
template <class MATRIX>
inline
MatrixBlock<MATRIX>::MatrixBlock()
:
row(deal_II_numbers::invalid_unsigned_int),
column(deal_II_numbers::invalid_unsigned_int)
{}
template <class MATRIX>
inline
MatrixBlock<MATRIX>::MatrixBlock(const MatrixBlock<MATRIX>& M)
:
row(M.row),
column(M.column),
matrix(M.matrix),
block_indices(M.block_indices)
{}
template <class MATRIX>
inline
MatrixBlock<MATRIX>::MatrixBlock(unsigned int i, unsigned int j,
const BlockIndices* block_indices)
:
row(i), column(j), block_indices(block_indices)
{}
template <class MATRIX>
inline void
MatrixBlock<MATRIX>::add (
const unsigned int gi,
const unsigned int gj,
const typename MATRIX::value_type value)
{
Assert(block_indices != 0, ExcNotInitialized());
const std::pair<unsigned int, unsigned int> bi
= block_indices->global_to_local(gi);
const std::pair<unsigned int, unsigned int> bj
= block_indices->global_to_local(gj);
Assert (bi.first == row, ExcBlockIndexMismatch(bi.first, row));
Assert (bj.first == column, ExcBlockIndexMismatch(bj.first, column));
matrix.add(bi.second, bj.second, value);
}
template <class MATRIX>
template <typename number>
inline
void
MatrixBlock<MATRIX>::add (const std::vector<unsigned int>& row_indices,
const std::vector<unsigned int>& col_indices,
const FullMatrix<number>& values,
const bool elide_zero_values)
{
Assert(block_indices != 0, ExcNotInitialized());
AssertDimension (row_indices.size(), values.m());
AssertDimension (col_indices.size(), values.n());
for (unsigned int i=0; i<row_indices.size(); ++i)
add (row_indices[i], col_indices.size(), &col_indices[0], &values(i,0),
elide_zero_values);
}
template <class MATRIX>
template <typename number>
inline
void
MatrixBlock<MATRIX>::add (const unsigned int b_row,
const unsigned int n_cols,
const unsigned int *col_indices,
const number *values,
const bool,
const bool)
{
Assert(block_indices != 0, ExcNotInitialized());
const std::pair<unsigned int, unsigned int> bi
= block_indices->global_to_local(b_row);
// In debug mode, we check whether
// all indices are in the correct
// block.
// Actually, for the time being, we
// leave it at this. While it may
// not be the most efficient way,
// it is at least thread safe.
//#ifdef DEBUG
Assert(bi.first == row, ExcBlockIndexMismatch(bi.first, row));
for (unsigned int j=0;j<n_cols;++j)
{
const std::pair<unsigned int, unsigned int> bj
= block_indices->global_to_local(col_indices[j]);
Assert(bj.first == column, ExcBlockIndexMismatch(bj.first, column));
matrix.add(bi.second, bj.second, values[j]);
}
//#endif
}
template <class MATRIX>
template <typename number>
inline
void
MatrixBlock<MATRIX>::add (const std::vector<unsigned int> &indices,
const FullMatrix<number> &values,
const bool elide_zero_values)
{
Assert(block_indices != 0, ExcNotInitialized());
AssertDimension (indices.size(), values.m());
Assert (values.n() == values.m(), ExcNotQuadratic());
for (unsigned int i=0; i<indices.size(); ++i)
add (indices[i], indices.size(), &indices[0], &values(i,0),
elide_zero_values);
}
template <class MATRIX>
template <typename number>
inline
void
MatrixBlock<MATRIX>::add (const unsigned int row,
const std::vector<unsigned int> &col_indices,
const std::vector<number> &values,
const bool elide_zero_values)
{
Assert(block_indices != 0, ExcNotInitialized());
AssertDimension (col_indices.size(), values.size());
add (row, col_indices.size(), &col_indices[0], &values[0],
elide_zero_values);
}
template <class MATRIX>
template <class VECTOR>
inline
void
MatrixBlock<MATRIX>::vmult (VECTOR& w, const VECTOR& v) const
{
matrix.vmult(w,v);
}
template <class MATRIX>
template <class VECTOR>
inline
void
MatrixBlock<MATRIX>::vmult_add (VECTOR& w, const VECTOR& v) const
{
matrix.vmult_add(w,v);
}
template <class MATRIX>
template <class VECTOR>
inline
void
MatrixBlock<MATRIX>::Tvmult (VECTOR& w, const VECTOR& v) const
{
matrix.Tvmult(w,v);
}
template <class MATRIX>
template <class VECTOR>
inline
void
MatrixBlock<MATRIX>::Tvmult_add (VECTOR& w, const VECTOR& v) const
{
matrix.Tvmult_add(w,v);
}
//----------------------------------------------------------------------//
template <class MATRIX>
inline void
MatrixBlockVector<MATRIX>::add(
unsigned int row, unsigned int column,
const std::string& name,
const BlockIndices* block_indices)
{
std_cxx1x::shared_ptr<MatrixBlock<MATRIX> > p(new MatrixBlock<MATRIX>(row, column, block_indices));
NamedData<std_cxx1x::shared_ptr<MatrixBlock<MATRIX> > >::add(p, name);
}
template <class MATRIX>
inline const MatrixBlock<MATRIX>&
MatrixBlockVector<MATRIX>::block(unsigned int i) const
{
return *this->read(i);
}
template <class MATRIX>
inline MatrixBlock<MATRIX>&
MatrixBlockVector<MATRIX>::block(unsigned int i)
{
return *(*this)(i);
}
template <class MATRIX>
inline MATRIX&
MatrixBlockVector<MATRIX>::matrix(unsigned int i)
{
return (*this)(i)->matrix;
}
DEAL_II_NAMESPACE_CLOSE
#endif
|