Reference documentation for deal.II version 8.1.0
block_sparse_matrix_ez.h
1 // ---------------------------------------------------------------------
2 // @f$Id: block_sparse_matrix_ez.h 30040 2013-07-18 17:06:48Z maier @f$
3 //
4 // Copyright (C) 2002 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__block_sparse_matrix_ez_h
18 #define __deal2__block_sparse_matrix_ez_h
19 
20 
21 //TODO: Derive BlockSparseMatrixEZ from BlockMatrixBase, like all the other block matrices as well; this would allow to instantiate a few functions with this template argument as well (in particular ConstraintMatrix::distribute_local_to_global)
22 
23 #include <deal.II/base/config.h>
25 #include <deal.II/base/subscriptor.h>
26 #include <deal.II/base/table.h>
27 #include <deal.II/base/smartpointer.h>
28 #include <deal.II/lac/block_indices.h>
29 #include <deal.II/lac/sparse_matrix_ez.h>
30 
32 
33 template <typename Number> class BlockVector;
34 
51 template<typename Number>
52 class BlockSparseMatrixEZ : public Subscriptor
53 {
54 public:
59 
66 
74  BlockSparseMatrixEZ (const unsigned int block_rows,
75  const unsigned int block_cols);
76 
88 
96 
113  BlockSparseMatrixEZ &operator = (const double d);
114 
115 
120  void clear ();
121 
136  void reinit (const unsigned int n_block_rows,
137  const unsigned int n_block_cols);
150  void collect_sizes ();
151 
157  block (const unsigned int row,
158  const unsigned int column);
159 
160 
166  const SparseMatrixEZ<Number> &
167  block (const unsigned int row,
168  const unsigned int column) const;
169 
174  unsigned int n_block_rows () const;
175 
180  unsigned int n_block_cols () const;
181 
193  bool empty () const;
194 
202  size_type n_rows () const;
203 
212  size_type n_cols () const;
213 
220  size_type m () const;
221 
228  size_type n () const;
229 
239  void set (const size_type i,
240  const size_type j,
241  const Number value);
242 
252  void add (const size_type i, const size_type j,
253  const Number value);
254 
255 
261  template <typename somenumber>
262  void vmult (BlockVector<somenumber> &dst,
263  const BlockVector<somenumber> &src) const;
264 
273  template <typename somenumber>
274  void Tvmult (BlockVector<somenumber> &dst,
275  const BlockVector<somenumber> &src) const;
276 
283  template <typename somenumber>
285  const BlockVector<somenumber> &src) const;
286 
295  template <typename somenumber>
297  const BlockVector<somenumber> &src) const;
298 
299 
309  template <class STREAM>
310  void print_statistics (STREAM &s, bool full = false);
311 
312 private:
320 
328 
333 };
334 
336 /*----------------------------------------------------------------------*/
337 
338 
339 template <typename Number>
340 inline
341 unsigned int
343 {
344  return row_indices.size();
345 }
346 
347 
348 
349 template <typename Number>
350 inline
353 {
354  return row_indices.total_size();
355 }
356 
357 
358 
359 template <typename Number>
360 inline
361 unsigned int
363 {
364  return column_indices.size();
365 }
366 
367 
368 
369 template <typename Number>
370 inline
373 {
374  return column_indices.total_size();
375 }
376 
377 
378 
379 template <typename Number>
380 inline
382 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
383  const unsigned int column)
384 {
385  Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
386  Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
387 
388  return blocks[row][column];
389 }
390 
391 
392 
393 template <typename Number>
394 inline
396 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
397  const unsigned int column) const
398 {
399  Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
400  Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
401 
402  return blocks[row][column];
403 }
404 
405 
406 
407 template <typename Number>
408 inline
411 {
412  return n_rows();
413 }
414 
415 
416 
417 template <typename Number>
418 inline
421 {
422  return n_cols();
423 }
424 
425 
426 
427 template <typename Number>
428 inline
429 void
431  const size_type j,
432  const Number value)
433 {
434 
436 
437  const std::pair<size_type,size_type>
438  row_index = row_indices.global_to_local (i),
439  col_index = column_indices.global_to_local (j);
440  block(row_index.first,col_index.first).set (row_index.second,
441  col_index.second,
442  value);
443 }
444 
445 
446 
447 template <typename Number>
448 inline
449 void
451  const size_type j,
452  const Number value)
453 {
454 
456 
457  const std::pair<unsigned int,size_type>
458  row_index = row_indices.global_to_local (i),
459  col_index = column_indices.global_to_local (j);
460  block(row_index.first,col_index.first).add (row_index.second,
461  col_index.second,
462  value);
463 }
464 
465 
466 template <typename Number>
467 template <typename somenumber>
468 void
470  const BlockVector<somenumber> &src) const
471 {
472  Assert (dst.n_blocks() == n_block_rows(),
473  ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
474  Assert (src.n_blocks() == n_block_cols(),
475  ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
476 
477  dst = 0.;
478 
479  for (unsigned int row=0; row<n_block_rows(); ++row)
480  for (unsigned int col=0; col<n_block_cols(); ++col)
481  block(row,col).vmult_add (dst.block(row),
482  src.block(col));
483 }
484 
485 
486 
487 template <typename Number>
488 template <typename somenumber>
489 void
492  const BlockVector<somenumber> &src) const
493 {
494  Assert (dst.n_blocks() == n_block_rows(),
495  ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
496  Assert (src.n_blocks() == n_block_cols(),
497  ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
498 
499  for (unsigned int row=0; row<n_block_rows(); ++row)
500  for (unsigned int col=0; col<n_block_cols(); ++col)
501  block(row,col).vmult_add (dst.block(row),
502  src.block(col));
503 }
504 
505 
506 
507 
508 template <typename Number>
509 template <typename somenumber>
510 void
513  const BlockVector<somenumber> &src) const
514 {
515  Assert (dst.n_blocks() == n_block_cols(),
516  ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
517  Assert (src.n_blocks() == n_block_rows(),
518  ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
519 
520  dst = 0.;
521 
522  for (unsigned int row=0; row<n_block_rows(); ++row)
523  for (unsigned int col=0; col<n_block_cols(); ++col)
524  block(row,col).Tvmult_add (dst.block(col),
525  src.block(row));
526 }
527 
528 
529 
530 template <typename Number>
531 template <typename somenumber>
532 void
535  const BlockVector<somenumber> &src) const
536 {
537  Assert (dst.n_blocks() == n_block_cols(),
538  ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
539  Assert (src.n_blocks() == n_block_rows(),
540  ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
541 
542  for (unsigned int row=0; row<n_block_rows(); ++row)
543  for (unsigned int col=0; col<n_block_cols(); ++col)
544  block(row,col).Tvmult_add (dst.block(col),
545  src.block(row));
546 }
547 
548 
549 template <typename number>
550 template <class STREAM>
551 inline
552 void
554 {
555  size_type used_total = 0;
556  size_type allocated_total = 0;
557  size_type reserved_total = 0;
558  std::vector<size_type> used_by_line_total;
559 
560  size_type used;
561  size_type allocated;
562  size_type reserved;
563  std::vector<size_type> used_by_line;
564 
565  for (size_type i=0; i<n_block_rows(); ++i)
566  for (size_type j=0; j<n_block_cols(); ++j)
567  {
568  used_by_line.clear();
569  out << "block:\t" << i << '\t' << j << std::endl;
570  block(i,j).compute_statistics (used, allocated, reserved,
571  used_by_line, full);
572 
573  out << "used:" << used << std::endl
574  << "allocated:" << allocated << std::endl
575  << "reserved:" << reserved << std::endl;
576 
577  used_total += used;
578  allocated_total += allocated;
579  reserved_total += reserved;
580 
581  if (full)
582  {
583  used_by_line_total.resize(used_by_line.size());
584  for (size_type i=0; i< used_by_line.size(); ++i)
585  if (used_by_line[i] != 0)
586  {
587  out << "row-entries\t" << i
588  << "\trows\t" << used_by_line[i]
589  << std::endl;
590  used_by_line_total[i] += used_by_line[i];
591  }
592  }
593  }
594  out << "Total" << std::endl
595  << "used:" << used_total << std::endl
596  << "allocated:" << allocated_total << std::endl
597  << "reserved:" << reserved_total << std::endl;
598  for (size_type i=0; i< used_by_line_total.size(); ++i)
599  if (used_by_line_total[i] != 0)
600  {
601  out << "row-entries\t" << i
602  << "\trows\t" << used_by_line_total[i]
603  << std::endl;
604  }
605 }
606 
607 
608 DEAL_II_NAMESPACE_CLOSE
609 
610 #endif //__deal2__block_sparse_matrix_ez_h
size_type n_rows() const
types::global_dof_index size_type
Auxiliary class aiding in the handling of block structures like in BlockVector or FESystem...
Definition: block_indices.h:55
Table< 2, SparseMatrixEZ< Number > > blocks
void vmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
SparseMatrixEZ< Number > & block(const unsigned int row, const unsigned int column)
void Tvmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
bool is_finite(const double x)
unsigned int n_block_rows() const
void add(const size_type i, const size_type j, const Number value)
unsigned int n_block_cols() const
unsigned int global_dof_index
Definition: types.h:100
void Tvmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
#define Assert(cond, exc)
Definition: exceptions.h:299
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void vmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
void set(const size_type i, const size_type j, const Number value)
::ExceptionBase & ExcNumberNotFinite()
size_type n_cols() const
BlockSparseMatrixEZ & operator=(const BlockSparseMatrixEZ< Number > &)
void print_statistics(STREAM &s, bool full=false)
::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void reinit(const unsigned int n_block_rows, const unsigned int n_block_cols)
BlockType & block(const unsigned int i)