Reference documentation for deal.II version 9.1.1
\(\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 template <typename Number>
38 class BlockVector;
39 
57 template <typename Number>
58 class BlockSparseMatrixEZ : public Subscriptor
59 {
60 public:
65 
69  BlockSparseMatrixEZ() = default;
70 
75  BlockSparseMatrixEZ(const unsigned int block_rows,
76  const unsigned int block_cols);
77 
84 
91 
101  operator=(const double d);
102 
103 
107  void
108  clear();
109 
117  void
118  reinit(const unsigned int n_block_rows, const unsigned int n_block_cols);
125  void
126  collect_sizes();
127 
132  block(const unsigned int row, const unsigned int column);
133 
134 
139  const SparseMatrixEZ<Number> &
140  block(const unsigned int row, const unsigned int column) const;
141 
145  unsigned int
146  n_block_rows() const;
147 
151  unsigned int
152  n_block_cols() const;
153 
160  bool
161  empty() const;
162 
169  size_type
170  m() const;
171 
178  size_type
179  n() const;
180 
186  void
187  set(const size_type i, const size_type j, const Number value);
188 
194  void
195  add(const size_type i, const size_type j, const Number value);
196 
197 
202  template <typename somenumber>
203  void
204  vmult(BlockVector<somenumber> &dst, const BlockVector<somenumber> &src) const;
205 
211  template <typename somenumber>
212  void
214  const BlockVector<somenumber> &src) const;
215 
220  template <typename somenumber>
221  void
223  const BlockVector<somenumber> &src) const;
224 
230  template <typename somenumber>
231  void
233  const BlockVector<somenumber> &src) const;
234 
235 
241  template <class StreamType>
242  void
243  print_statistics(StreamType &s, bool full = false);
244 
245 private:
251 
257 
262 };
263 
265 /*----------------------------------------------------------------------*/
266 
267 
268 template <typename Number>
269 inline unsigned int
271 {
272  return row_indices.size();
273 }
274 
275 
276 
277 template <typename Number>
278 inline unsigned int
280 {
281  return column_indices.size();
282 }
283 
284 
285 
286 template <typename Number>
287 inline SparseMatrixEZ<Number> &
288 BlockSparseMatrixEZ<Number>::block(const unsigned int row,
289  const unsigned int column)
290 {
291  Assert(row < n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
292  Assert(column < n_block_cols(), ExcIndexRange(column, 0, n_block_cols()));
293 
294  return blocks[row][column];
295 }
296 
297 
298 
299 template <typename Number>
300 inline const SparseMatrixEZ<Number> &
301 BlockSparseMatrixEZ<Number>::block(const unsigned int row,
302  const unsigned int column) const
303 {
304  Assert(row < n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
305  Assert(column < n_block_cols(), ExcIndexRange(column, 0, n_block_cols()));
306 
307  return blocks[row][column];
308 }
309 
310 
311 
312 template <typename Number>
315 {
316  return row_indices.total_size();
317 }
318 
319 
320 
321 template <typename Number>
324 {
325  return column_indices.total_size();
326 }
327 
328 
329 
330 template <typename Number>
331 inline void
333  const size_type j,
334  const Number value)
335 {
336  AssertIsFinite(value);
337 
338  const std::pair<size_type, size_type> row_index =
339  row_indices.global_to_local(i),
340  col_index =
341  column_indices.global_to_local(j);
342  block(row_index.first, col_index.first)
343  .set(row_index.second, col_index.second, value);
344 }
345 
346 
347 
348 template <typename Number>
349 inline void
351  const size_type j,
352  const Number value)
353 {
354  AssertIsFinite(value);
355 
356  const std::pair<unsigned int, size_type> row_index =
357  row_indices.global_to_local(i),
358  col_index =
359  column_indices.global_to_local(j);
360  block(row_index.first, col_index.first)
361  .add(row_index.second, col_index.second, value);
362 }
363 
364 
365 template <typename Number>
366 template <typename somenumber>
367 void
369  const BlockVector<somenumber> &src) const
370 {
371  Assert(dst.n_blocks() == n_block_rows(),
372  ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
373  Assert(src.n_blocks() == n_block_cols(),
374  ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
375 
376  dst = 0.;
377 
378  for (unsigned int row = 0; row < n_block_rows(); ++row)
379  for (unsigned int col = 0; col < n_block_cols(); ++col)
380  block(row, col).vmult_add(dst.block(row), src.block(col));
381 }
382 
383 
384 
385 template <typename Number>
386 template <typename somenumber>
387 void
389  const BlockVector<somenumber> &src) const
390 {
391  Assert(dst.n_blocks() == n_block_rows(),
392  ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
393  Assert(src.n_blocks() == n_block_cols(),
394  ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
395 
396  for (unsigned int row = 0; row < n_block_rows(); ++row)
397  for (unsigned int col = 0; col < n_block_cols(); ++col)
398  block(row, col).vmult_add(dst.block(row), src.block(col));
399 }
400 
401 
402 
403 template <typename Number>
404 template <typename somenumber>
405 void
407  const BlockVector<somenumber> &src) const
408 {
409  Assert(dst.n_blocks() == n_block_cols(),
410  ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
411  Assert(src.n_blocks() == n_block_rows(),
412  ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
413 
414  dst = 0.;
415 
416  for (unsigned int row = 0; row < n_block_rows(); ++row)
417  for (unsigned int col = 0; col < n_block_cols(); ++col)
418  block(row, col).Tvmult_add(dst.block(col), src.block(row));
419 }
420 
421 
422 
423 template <typename Number>
424 template <typename somenumber>
425 void
428  const BlockVector<somenumber> &src) const
429 {
430  Assert(dst.n_blocks() == n_block_cols(),
431  ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
432  Assert(src.n_blocks() == n_block_rows(),
433  ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
434 
435  for (unsigned int row = 0; row < n_block_rows(); ++row)
436  for (unsigned int col = 0; col < n_block_cols(); ++col)
437  block(row, col).Tvmult_add(dst.block(col), src.block(row));
438 }
439 
440 
441 template <typename number>
442 template <class StreamType>
443 inline void
445 {
446  size_type used_total = 0;
447  size_type allocated_total = 0;
448  size_type reserved_total = 0;
449  std::vector<size_type> used_by_line_total;
450 
451  size_type used;
452  size_type allocated;
453  size_type reserved;
454  std::vector<size_type> used_by_line;
455 
456  for (size_type i = 0; i < n_block_rows(); ++i)
457  for (size_type j = 0; j < n_block_cols(); ++j)
458  {
459  used_by_line.clear();
460  out << "block:\t" << i << '\t' << j << std::endl;
461  block(i, j).compute_statistics(
462  used, allocated, reserved, used_by_line, full);
463 
464  out << "used:" << used << std::endl
465  << "allocated:" << allocated << std::endl
466  << "reserved:" << reserved << std::endl;
467 
468  used_total += used;
469  allocated_total += allocated;
470  reserved_total += reserved;
471 
472  if (full)
473  {
474  used_by_line_total.resize(used_by_line.size());
475  for (size_type i = 0; i < used_by_line.size(); ++i)
476  if (used_by_line[i] != 0)
477  {
478  out << "row-entries\t" << i << "\trows\t" << used_by_line[i]
479  << std::endl;
480  used_by_line_total[i] += used_by_line[i];
481  }
482  }
483  }
484  out << "Total" << std::endl
485  << "used:" << used_total << std::endl
486  << "allocated:" << allocated_total << std::endl
487  << "reserved:" << reserved_total << std::endl;
488  for (size_type i = 0; i < used_by_line_total.size(); ++i)
489  if (used_by_line_total[i] != 0)
490  {
491  out << "row-entries\t" << i << "\trows\t" << used_by_line_total[i]
492  << std::endl;
493  }
494 }
495 
496 
497 DEAL_II_NAMESPACE_CLOSE
498 
499 #endif // dealii_block_sparse_matrix_ez_h
void Tvmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
Table< 2, SparseMatrixEZ< Number > > blocks
SparseMatrixEZ< Number > & block(const unsigned int row, const unsigned int column)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
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:37
void vmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
BlockType & block(const unsigned int i)
#define AssertIsFinite(number)
Definition: exceptions.h:1669
unsigned int n_block_rows() const