Reference documentation for deal.II version 8.5.1
block_sparse_matrix_ez.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2015 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 at
12 // the top level of the deal.II distribution.
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 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)
21 
22 #include <deal.II/base/config.h>
23 #include <deal.II/base/exceptions.h>
24 #include <deal.II/base/subscriptor.h>
25 #include <deal.II/base/table.h>
26 #include <deal.II/base/smartpointer.h>
27 #include <deal.II/lac/block_indices.h>
28 #include <deal.II/lac/sparse_matrix_ez.h>
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 template <typename Number> class BlockVector;
33 
51 template<typename Number>
53 {
54 public:
59 
64 
69  BlockSparseMatrixEZ (const unsigned int block_rows,
70  const unsigned int block_cols);
71 
78 
84 
93  BlockSparseMatrixEZ &operator = (const double d);
94 
95 
99  void clear ();
100 
108  void reinit (const unsigned int n_block_rows,
109  const unsigned int n_block_cols);
116  void collect_sizes ();
117 
122  block (const unsigned int row,
123  const unsigned int column);
124 
125 
130  const SparseMatrixEZ<Number> &
131  block (const unsigned int row,
132  const unsigned int column) const;
133 
137  unsigned int n_block_rows () const;
138 
142  unsigned int n_block_cols () const;
143 
150  bool empty () const;
151 
159  size_type n_rows () const DEAL_II_DEPRECATED;
160 
168  size_type n_cols () const DEAL_II_DEPRECATED;
169 
176  size_type m () const;
177 
184  size_type n () const;
185 
191  void set (const size_type i,
192  const size_type j,
193  const Number value);
194 
200  void add (const size_type i, const size_type j,
201  const Number value);
202 
203 
208  template <typename somenumber>
209  void vmult (BlockVector<somenumber> &dst,
210  const BlockVector<somenumber> &src) const;
211 
217  template <typename somenumber>
218  void Tvmult (BlockVector<somenumber> &dst,
219  const BlockVector<somenumber> &src) const;
220 
225  template <typename somenumber>
226  void vmult_add (BlockVector<somenumber> &dst,
227  const BlockVector<somenumber> &src) const;
228 
234  template <typename somenumber>
235  void Tvmult_add (BlockVector<somenumber> &dst,
236  const BlockVector<somenumber> &src) const;
237 
238 
244  template <class StreamType>
245  void print_statistics (StreamType &s, bool full = false);
246 
247 private:
253 
259 
264 };
265 
267 /*----------------------------------------------------------------------*/
268 
269 
270 template <typename Number>
271 inline
272 unsigned int
274 {
275  return row_indices.size();
276 }
277 
278 
279 
280 template <typename Number>
281 inline
284 {
285  return row_indices.total_size();
286 }
287 
288 
289 
290 template <typename Number>
291 inline
292 unsigned int
294 {
295  return column_indices.size();
296 }
297 
298 
299 
300 template <typename Number>
301 inline
304 {
305  return column_indices.total_size();
306 }
307 
308 
309 
310 template <typename Number>
311 inline
313 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
314  const unsigned int column)
315 {
316  Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
317  Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
318 
319  return blocks[row][column];
320 }
321 
322 
323 
324 template <typename Number>
325 inline
327 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
328  const unsigned int column) const
329 {
330  Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
331  Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
332 
333  return blocks[row][column];
334 }
335 
336 
337 
338 template <typename Number>
339 inline
342 {
343  return row_indices.total_size();
344 }
345 
346 
347 
348 template <typename Number>
349 inline
352 {
353  return column_indices.total_size();
354 }
355 
356 
357 
358 template <typename Number>
359 inline
360 void
362  const size_type j,
363  const Number value)
364 {
365 
366  AssertIsFinite(value);
367 
368  const std::pair<size_type,size_type>
369  row_index = row_indices.global_to_local (i),
370  col_index = column_indices.global_to_local (j);
371  block(row_index.first,col_index.first).set (row_index.second,
372  col_index.second,
373  value);
374 }
375 
376 
377 
378 template <typename Number>
379 inline
380 void
382  const size_type j,
383  const Number value)
384 {
385 
386  AssertIsFinite(value);
387 
388  const std::pair<unsigned int,size_type>
389  row_index = row_indices.global_to_local (i),
390  col_index = column_indices.global_to_local (j);
391  block(row_index.first,col_index.first).add (row_index.second,
392  col_index.second,
393  value);
394 }
395 
396 
397 template <typename Number>
398 template <typename somenumber>
399 void
401  const BlockVector<somenumber> &src) const
402 {
403  Assert (dst.n_blocks() == n_block_rows(),
405  Assert (src.n_blocks() == n_block_cols(),
407 
408  dst = 0.;
409 
410  for (unsigned int row=0; row<n_block_rows(); ++row)
411  for (unsigned int col=0; col<n_block_cols(); ++col)
412  block(row,col).vmult_add (dst.block(row),
413  src.block(col));
414 }
415 
416 
417 
418 template <typename Number>
419 template <typename somenumber>
420 void
423  const BlockVector<somenumber> &src) const
424 {
425  Assert (dst.n_blocks() == n_block_rows(),
427  Assert (src.n_blocks() == n_block_cols(),
429 
430  for (unsigned int row=0; row<n_block_rows(); ++row)
431  for (unsigned int col=0; col<n_block_cols(); ++col)
432  block(row,col).vmult_add (dst.block(row),
433  src.block(col));
434 }
435 
436 
437 
438 
439 template <typename Number>
440 template <typename somenumber>
441 void
444  const BlockVector<somenumber> &src) const
445 {
446  Assert (dst.n_blocks() == n_block_cols(),
448  Assert (src.n_blocks() == n_block_rows(),
450 
451  dst = 0.;
452 
453  for (unsigned int row=0; row<n_block_rows(); ++row)
454  for (unsigned int col=0; col<n_block_cols(); ++col)
455  block(row,col).Tvmult_add (dst.block(col),
456  src.block(row));
457 }
458 
459 
460 
461 template <typename Number>
462 template <typename somenumber>
463 void
466  const BlockVector<somenumber> &src) const
467 {
468  Assert (dst.n_blocks() == n_block_cols(),
470  Assert (src.n_blocks() == n_block_rows(),
472 
473  for (unsigned int row=0; row<n_block_rows(); ++row)
474  for (unsigned int col=0; col<n_block_cols(); ++col)
475  block(row,col).Tvmult_add (dst.block(col),
476  src.block(row));
477 }
478 
479 
480 template <typename number>
481 template <class StreamType>
482 inline
483 void
485 {
486  size_type used_total = 0;
487  size_type allocated_total = 0;
488  size_type reserved_total = 0;
489  std::vector<size_type> used_by_line_total;
490 
491  size_type used;
492  size_type allocated;
493  size_type reserved;
494  std::vector<size_type> used_by_line;
495 
496  for (size_type i=0; i<n_block_rows(); ++i)
497  for (size_type j=0; j<n_block_cols(); ++j)
498  {
499  used_by_line.clear();
500  out << "block:\t" << i << '\t' << j << std::endl;
501  block(i,j).compute_statistics (used, allocated, reserved,
502  used_by_line, full);
503 
504  out << "used:" << used << std::endl
505  << "allocated:" << allocated << std::endl
506  << "reserved:" << reserved << std::endl;
507 
508  used_total += used;
509  allocated_total += allocated;
510  reserved_total += reserved;
511 
512  if (full)
513  {
514  used_by_line_total.resize(used_by_line.size());
515  for (size_type i=0; i< used_by_line.size(); ++i)
516  if (used_by_line[i] != 0)
517  {
518  out << "row-entries\t" << i
519  << "\trows\t" << used_by_line[i]
520  << std::endl;
521  used_by_line_total[i] += used_by_line[i];
522  }
523  }
524  }
525  out << "Total" << std::endl
526  << "used:" << used_total << std::endl
527  << "allocated:" << allocated_total << std::endl
528  << "reserved:" << reserved_total << std::endl;
529  for (size_type i=0; i< used_by_line_total.size(); ++i)
530  if (used_by_line_total[i] != 0)
531  {
532  out << "row-entries\t" << i
533  << "\trows\t" << used_by_line_total[i]
534  << std::endl;
535  }
536 }
537 
538 
539 DEAL_II_NAMESPACE_CLOSE
540 
541 #endif //dealii__block_sparse_matrix_ez_h
size_type n_rows() const 1
void Tvmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
types::global_dof_index size_type
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)
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
size_type n_cols() const 1
unsigned int global_dof_index
Definition: types.h:88
void vmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
#define Assert(cond, exc)
Definition: exceptions.h:313
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)
void set(const size_type i, const size_type j, const Number value)
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:33
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:1197
unsigned int n_block_rows() const