Reference documentation for deal.II version Git cb0bd54b52 2019-09-21 16:31:22 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
block_sparse_matrix_ez.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_block_sparse_matrix_ez_h
17 #define dealii_block_sparse_matrix_ez_h
18 
19 
20 // TODO: Derive BlockSparseMatrixEZ from BlockMatrixBase, like all the
21 // other block matrices as well; this would allow to instantiate a few
22 // functions with this template argument as well (in particular
23 // AffineConstraints::distribute_local_to_global)
24 
25 #include <deal.II/base/config.h>
26 
27 #include <deal.II/base/exceptions.h>
28 #include <deal.II/base/smartpointer.h>
29 #include <deal.II/base/subscriptor.h>
30 #include <deal.II/base/table.h>
31 
32 #include <deal.II/lac/block_indices.h>
33 #include <deal.II/lac/sparse_matrix_ez.h>
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 // Forward declaration
38 #ifndef DOXYGEN
39 template <typename Number>
40 class BlockVector;
41 #endif
42 
60 template <typename Number>
61 class BlockSparseMatrixEZ : public Subscriptor
62 {
63 public:
68 
72  BlockSparseMatrixEZ() = default;
73 
78  BlockSparseMatrixEZ(const unsigned int block_rows,
79  const unsigned int block_cols);
80 
87 
94 
104  operator=(const double d);
105 
106 
110  void
111  clear();
112 
120  void
121  reinit(const unsigned int n_block_rows, const unsigned int n_block_cols);
128  void
129  collect_sizes();
130 
135  block(const unsigned int row, const unsigned int column);
136 
137 
142  const SparseMatrixEZ<Number> &
143  block(const unsigned int row, const unsigned int column) const;
144 
148  unsigned int
149  n_block_rows() const;
150 
154  unsigned int
155  n_block_cols() const;
156 
163  bool
164  empty() const;
165 
172  size_type
173  m() const;
174 
181  size_type
182  n() const;
183 
189  void
190  set(const size_type i, const size_type j, const Number value);
191 
197  void
198  add(const size_type i, const size_type j, const Number value);
199 
200 
205  template <typename somenumber>
206  void
207  vmult(BlockVector<somenumber> &dst, const BlockVector<somenumber> &src) const;
208 
214  template <typename somenumber>
215  void
217  const BlockVector<somenumber> &src) const;
218 
223  template <typename somenumber>
224  void
226  const BlockVector<somenumber> &src) const;
227 
233  template <typename somenumber>
234  void
236  const BlockVector<somenumber> &src) const;
237 
238 
244  template <class StreamType>
245  void
246  print_statistics(StreamType &s, bool full = false);
247 
248 private:
254 
260 
265 };
266 
268 /*----------------------------------------------------------------------*/
269 
270 
271 template <typename Number>
272 inline unsigned int
274 {
275  return row_indices.size();
276 }
277 
278 
279 
280 template <typename Number>
281 inline unsigned int
283 {
284  return column_indices.size();
285 }
286 
287 
288 
289 template <typename Number>
290 inline SparseMatrixEZ<Number> &
291 BlockSparseMatrixEZ<Number>::block(const unsigned int row,
292  const unsigned int column)
293 {
294  Assert(row < n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
295  Assert(column < n_block_cols(), ExcIndexRange(column, 0, n_block_cols()));
296 
297  return blocks[row][column];
298 }
299 
300 
301 
302 template <typename Number>
303 inline const SparseMatrixEZ<Number> &
304 BlockSparseMatrixEZ<Number>::block(const unsigned int row,
305  const unsigned int column) const
306 {
307  Assert(row < n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
308  Assert(column < n_block_cols(), ExcIndexRange(column, 0, n_block_cols()));
309 
310  return blocks[row][column];
311 }
312 
313 
314 
315 template <typename Number>
318 {
319  return row_indices.total_size();
320 }
321 
322 
323 
324 template <typename Number>
327 {
328  return column_indices.total_size();
329 }
330 
331 
332 
333 template <typename Number>
334 inline void
336  const size_type j,
337  const Number value)
338 {
339  AssertIsFinite(value);
340 
341  const std::pair<size_type, size_type> row_index =
343  col_index =
345  block(row_index.first, col_index.first)
346  .set(row_index.second, col_index.second, value);
347 }
348 
349 
350 
351 template <typename Number>
352 inline void
354  const size_type j,
355  const Number value)
356 {
357  AssertIsFinite(value);
358 
359  const std::pair<unsigned int, size_type> row_index =
361  col_index =
363  block(row_index.first, col_index.first)
364  .add(row_index.second, col_index.second, value);
365 }
366 
367 
368 template <typename Number>
369 template <typename somenumber>
370 void
372  const BlockVector<somenumber> &src) const
373 {
374  Assert(dst.n_blocks() == n_block_rows(),
376  Assert(src.n_blocks() == n_block_cols(),
378 
379  dst = 0.;
380 
381  for (unsigned int row = 0; row < n_block_rows(); ++row)
382  for (unsigned int col = 0; col < n_block_cols(); ++col)
383  block(row, col).vmult_add(dst.block(row), src.block(col));
384 }
385 
386 
387 
388 template <typename Number>
389 template <typename somenumber>
390 void
392  const BlockVector<somenumber> &src) const
393 {
394  Assert(dst.n_blocks() == n_block_rows(),
396  Assert(src.n_blocks() == n_block_cols(),
398 
399  for (unsigned int row = 0; row < n_block_rows(); ++row)
400  for (unsigned int col = 0; col < n_block_cols(); ++col)
401  block(row, col).vmult_add(dst.block(row), src.block(col));
402 }
403 
404 
405 
406 template <typename Number>
407 template <typename somenumber>
408 void
410  const BlockVector<somenumber> &src) const
411 {
412  Assert(dst.n_blocks() == n_block_cols(),
414  Assert(src.n_blocks() == n_block_rows(),
416 
417  dst = 0.;
418 
419  for (unsigned int row = 0; row < n_block_rows(); ++row)
420  for (unsigned int col = 0; col < n_block_cols(); ++col)
421  block(row, col).Tvmult_add(dst.block(col), src.block(row));
422 }
423 
424 
425 
426 template <typename Number>
427 template <typename somenumber>
428 void
431  const BlockVector<somenumber> &src) const
432 {
433  Assert(dst.n_blocks() == n_block_cols(),
435  Assert(src.n_blocks() == n_block_rows(),
437 
438  for (unsigned int row = 0; row < n_block_rows(); ++row)
439  for (unsigned int col = 0; col < n_block_cols(); ++col)
440  block(row, col).Tvmult_add(dst.block(col), src.block(row));
441 }
442 
443 
444 template <typename number>
445 template <class StreamType>
446 inline void
448 {
449  size_type used_total = 0;
450  size_type allocated_total = 0;
451  size_type reserved_total = 0;
452  std::vector<size_type> used_by_line_total;
453 
454  size_type used;
455  size_type allocated;
456  size_type reserved;
457  std::vector<size_type> used_by_line;
458 
459  for (size_type i = 0; i < n_block_rows(); ++i)
460  for (size_type j = 0; j < n_block_cols(); ++j)
461  {
462  used_by_line.clear();
463  out << "block:\t" << i << '\t' << j << std::endl;
465  used, allocated, reserved, used_by_line, full);
466 
467  out << "used:" << used << std::endl
468  << "allocated:" << allocated << std::endl
469  << "reserved:" << reserved << std::endl;
470 
471  used_total += used;
472  allocated_total += allocated;
473  reserved_total += reserved;
474 
475  if (full)
476  {
477  used_by_line_total.resize(used_by_line.size());
478  for (size_type i = 0; i < used_by_line.size(); ++i)
479  if (used_by_line[i] != 0)
480  {
481  out << "row-entries\t" << i << "\trows\t" << used_by_line[i]
482  << std::endl;
483  used_by_line_total[i] += used_by_line[i];
484  }
485  }
486  }
487  out << "Total" << std::endl
488  << "used:" << used_total << std::endl
489  << "allocated:" << allocated_total << std::endl
490  << "reserved:" << reserved_total << std::endl;
491  for (size_type i = 0; i < used_by_line_total.size(); ++i)
492  if (used_by_line_total[i] != 0)
493  {
494  out << "row-entries\t" << i << "\trows\t" << used_by_line_total[i]
495  << std::endl;
496  }
497 }
498 
499 
500 DEAL_II_NAMESPACE_CLOSE
501 
502 #endif // dealii_block_sparse_matrix_ez_h
void Tvmult_add(Vector< somenumber > &dst, const Vector< somenumber > &src) const
void add(const size_type i, const size_type j, const number value)
void Tvmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
void compute_statistics(size_type &used, size_type &allocated, size_type &reserved, std::vector< size_type > &used_by_line, const bool compute_by_line) const
Table< 2, SparseMatrixEZ< Number > > blocks
SparseMatrixEZ< Number > & block(const unsigned int row, const unsigned int column)
void set(const size_type i, const size_type j, const number value, const bool elide_zero_values=true)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void vmult_add(Vector< somenumber > &dst, const Vector< somenumber > &src) const
size_type total_size() const
void add(const size_type i, const size_type j, const Number value)
bool empty() const
unsigned int n_block_cols() const
types::global_dof_index size_type
void vmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
#define Assert(cond, exc)
Definition: exceptions.h:1407
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void reinit(const unsigned int n_block_rows, const unsigned int n_block_cols)
BlockSparseMatrixEZ()=default
void set(const size_type i, const size_type j, const Number value)
unsigned int global_dof_index
Definition: types.h:89
void Tvmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
BlockSparseMatrixEZ & operator=(const BlockSparseMatrixEZ< Number > &)
void print_statistics(StreamType &s, bool full=false)
Definition: table.h:699
void vmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
unsigned int size() const
BlockType & block(const unsigned int i)
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
#define AssertIsFinite(number)
Definition: exceptions.h:1669
unsigned int n_block_rows() const