Reference documentation for deal.II version 9.5.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
block_sparse_matrix_ez.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2002 - 2022 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
30#include <deal.II/base/table.h>
31
34
36
37// Forward declaration
38#ifndef DOXYGEN
39template <typename Number>
40class BlockVector;
41#endif
42
60template <typename Number>
62{
63public:
68
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
112
120 void
121 reinit(const unsigned int n_block_rows, const unsigned int n_block_cols);
128 void
130
135 block(const unsigned int row, const unsigned int column);
136
137
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
173 m() const;
174
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
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
248private:
254
260
265};
266
268/*----------------------------------------------------------------------*/
269
270
271template <typename Number>
272inline unsigned int
274{
275 return row_indices.size();
276}
277
278
279
280template <typename Number>
281inline unsigned int
283{
284 return column_indices.size();
285}
286
287
288
289template <typename Number>
292 const unsigned int column)
293{
294 AssertIndexRange(row, n_block_rows());
295 AssertIndexRange(column, n_block_cols());
296
297 return blocks[row][column];
298}
299
300
301
302template <typename Number>
303inline const SparseMatrixEZ<Number> &
305 const unsigned int column) const
306{
307 AssertIndexRange(row, n_block_rows());
308 AssertIndexRange(column, n_block_cols());
309
310 return blocks[row][column];
311}
312
313
314
315template <typename Number>
318{
319 return row_indices.total_size();
320}
321
322
323
324template <typename Number>
327{
328 return column_indices.total_size();
329}
330
331
332
333template <typename Number>
334inline 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 =
342 row_indices.global_to_local(i),
343 col_index =
344 column_indices.global_to_local(j);
345 block(row_index.first, col_index.first)
346 .set(row_index.second, col_index.second, value);
347}
348
349
350
351template <typename Number>
352inline 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 =
360 row_indices.global_to_local(i),
361 col_index =
362 column_indices.global_to_local(j);
363 block(row_index.first, col_index.first)
364 .add(row_index.second, col_index.second, value);
365}
366
367
368template <typename Number>
369template <typename somenumber>
370void
372 const BlockVector<somenumber> &src) const
373{
374 Assert(dst.n_blocks() == n_block_rows(),
375 ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
376 Assert(src.n_blocks() == n_block_cols(),
377 ExcDimensionMismatch(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
388template <typename Number>
389template <typename somenumber>
390void
392 const BlockVector<somenumber> &src) const
393{
394 Assert(dst.n_blocks() == n_block_rows(),
395 ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
396 Assert(src.n_blocks() == n_block_cols(),
397 ExcDimensionMismatch(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
406template <typename Number>
407template <typename somenumber>
408void
410 const BlockVector<somenumber> &src) const
411{
412 Assert(dst.n_blocks() == n_block_cols(),
413 ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
414 Assert(src.n_blocks() == n_block_rows(),
415 ExcDimensionMismatch(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
426template <typename Number>
427template <typename somenumber>
428void
431 const BlockVector<somenumber> &src) const
432{
433 Assert(dst.n_blocks() == n_block_cols(),
434 ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
435 Assert(src.n_blocks() == n_block_rows(),
436 ExcDimensionMismatch(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
444template <typename number>
445template <class StreamType>
446inline 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;
464 block(i, j).compute_statistics(
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
501
502#endif // dealii_block_sparse_matrix_ez_h
void print_statistics(StreamType &s, bool full=false)
unsigned int n_block_cols() const
unsigned int n_block_rows() const
BlockSparseMatrixEZ()=default
void reinit(const unsigned int n_block_rows, const unsigned int n_block_cols)
void Tvmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
BlockSparseMatrixEZ & operator=(const BlockSparseMatrixEZ< Number > &)
BlockSparseMatrixEZ & operator=(const double d)
void vmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
Table< 2, SparseMatrixEZ< Number > > blocks
void Tvmult(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
SparseMatrixEZ< Number > & block(const unsigned int row, const unsigned int column)
BlockSparseMatrixEZ(const unsigned int block_rows, const unsigned int block_cols)
void set(const size_type i, const size_type j, const Number value)
void add(const size_type i, const size_type j, const Number value)
bool empty() const
void vmult_add(BlockVector< somenumber > &dst, const BlockVector< somenumber > &src) const
BlockSparseMatrixEZ(const BlockSparseMatrixEZ< Number > &)
unsigned int n_blocks() const
BlockType & block(const unsigned int i)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
unsigned int global_dof_index
Definition types.h:82