Reference documentation for deal.II version 9.0.0
block_sparse_matrix_ez.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2017 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 
63  BlockSparseMatrixEZ () = default;
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 
158  size_type m () const;
159 
166  size_type n () const;
167 
173  void set (const size_type i,
174  const size_type j,
175  const Number value);
176 
182  void add (const size_type i, const size_type j,
183  const Number value);
184 
185 
190  template <typename somenumber>
191  void vmult (BlockVector<somenumber> &dst,
192  const BlockVector<somenumber> &src) const;
193 
199  template <typename somenumber>
200  void Tvmult (BlockVector<somenumber> &dst,
201  const BlockVector<somenumber> &src) const;
202 
207  template <typename somenumber>
209  const BlockVector<somenumber> &src) const;
210 
216  template <typename somenumber>
218  const BlockVector<somenumber> &src) const;
219 
220 
226  template <class StreamType>
227  void print_statistics (StreamType &s, bool full = false);
228 
229 private:
235 
241 
246 };
247 
249 /*----------------------------------------------------------------------*/
250 
251 
252 template <typename Number>
253 inline
254 unsigned int
256 {
257  return row_indices.size();
258 }
259 
260 
261 
262 template <typename Number>
263 inline
264 unsigned int
266 {
267  return column_indices.size();
268 }
269 
270 
271 
272 template <typename Number>
273 inline
275 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
276  const unsigned int column)
277 {
278  Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
279  Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
280 
281  return blocks[row][column];
282 }
283 
284 
285 
286 template <typename Number>
287 inline
289 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
290  const unsigned int column) const
291 {
292  Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
293  Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
294 
295  return blocks[row][column];
296 }
297 
298 
299 
300 template <typename Number>
301 inline
304 {
305  return row_indices.total_size();
306 }
307 
308 
309 
310 template <typename Number>
311 inline
314 {
315  return column_indices.total_size();
316 }
317 
318 
319 
320 template <typename Number>
321 inline
322 void
324  const size_type j,
325  const Number value)
326 {
327 
328  AssertIsFinite(value);
329 
330  const std::pair<size_type,size_type>
331  row_index = row_indices.global_to_local (i),
332  col_index = column_indices.global_to_local (j);
333  block(row_index.first,col_index.first).set (row_index.second,
334  col_index.second,
335  value);
336 }
337 
338 
339 
340 template <typename Number>
341 inline
342 void
344  const size_type j,
345  const Number value)
346 {
347 
348  AssertIsFinite(value);
349 
350  const std::pair<unsigned int,size_type>
351  row_index = row_indices.global_to_local (i),
352  col_index = column_indices.global_to_local (j);
353  block(row_index.first,col_index.first).add (row_index.second,
354  col_index.second,
355  value);
356 }
357 
358 
359 template <typename Number>
360 template <typename somenumber>
361 void
363  const BlockVector<somenumber> &src) const
364 {
365  Assert (dst.n_blocks() == n_block_rows(),
366  ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
367  Assert (src.n_blocks() == n_block_cols(),
368  ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
369 
370  dst = 0.;
371 
372  for (unsigned int row=0; row<n_block_rows(); ++row)
373  for (unsigned int col=0; col<n_block_cols(); ++col)
374  block(row,col).vmult_add (dst.block(row),
375  src.block(col));
376 }
377 
378 
379 
380 template <typename Number>
381 template <typename somenumber>
382 void
385  const BlockVector<somenumber> &src) const
386 {
387  Assert (dst.n_blocks() == n_block_rows(),
388  ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
389  Assert (src.n_blocks() == n_block_cols(),
390  ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
391 
392  for (unsigned int row=0; row<n_block_rows(); ++row)
393  for (unsigned int col=0; col<n_block_cols(); ++col)
394  block(row,col).vmult_add (dst.block(row),
395  src.block(col));
396 }
397 
398 
399 
400 
401 template <typename Number>
402 template <typename somenumber>
403 void
406  const BlockVector<somenumber> &src) const
407 {
408  Assert (dst.n_blocks() == n_block_cols(),
409  ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
410  Assert (src.n_blocks() == n_block_rows(),
411  ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
412 
413  dst = 0.;
414 
415  for (unsigned int row=0; row<n_block_rows(); ++row)
416  for (unsigned int col=0; col<n_block_cols(); ++col)
417  block(row,col).Tvmult_add (dst.block(col),
418  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),
438  src.block(row));
439 }
440 
441 
442 template <typename number>
443 template <class StreamType>
444 inline
445 void
447 {
448  size_type used_total = 0;
449  size_type allocated_total = 0;
450  size_type reserved_total = 0;
451  std::vector<size_type> used_by_line_total;
452 
453  size_type used;
454  size_type allocated;
455  size_type reserved;
456  std::vector<size_type> used_by_line;
457 
458  for (size_type i=0; i<n_block_rows(); ++i)
459  for (size_type j=0; j<n_block_cols(); ++j)
460  {
461  used_by_line.clear();
462  out << "block:\t" << i << '\t' << j << std::endl;
463  block(i,j).compute_statistics (used, allocated, reserved,
464  used_by_line, full);
465 
466  out << "used:" << used << std::endl
467  << "allocated:" << allocated << std::endl
468  << "reserved:" << reserved << std::endl;
469 
470  used_total += used;
471  allocated_total += allocated;
472  reserved_total += reserved;
473 
474  if (full)
475  {
476  used_by_line_total.resize(used_by_line.size());
477  for (size_type i=0; i< used_by_line.size(); ++i)
478  if (used_by_line[i] != 0)
479  {
480  out << "row-entries\t" << i
481  << "\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
495  << "\trows\t" << used_by_line_total[i]
496  << std::endl;
497  }
498 }
499 
500 
501 DEAL_II_NAMESPACE_CLOSE
502 
503 #endif //dealii_block_sparse_matrix_ez_h
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)
void add(const size_type i, const size_type j, const Number value)
bool empty() const
unsigned int n_block_cols() const
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:1142
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)
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:34
void vmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
BlockType & block(const unsigned int i)
#define AssertIsFinite(number)
Definition: exceptions.h:1299
unsigned int n_block_rows() const