00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __deal2__block_sparse_matrix_ez_h
00013 #define __deal2__block_sparse_matrix_ez_h
00014
00015
00016
00017
00018 #include <deal.II/base/config.h>
00019 #include <deal.II/base/exceptions.h>
00020 #include <deal.II/base/subscriptor.h>
00021 #include <deal.II/base/table.h>
00022 #include <deal.II/base/smartpointer.h>
00023 #include <deal.II/lac/block_indices.h>
00024 #include <deal.II/lac/sparse_matrix_ez.h>
00025
00026 DEAL_II_NAMESPACE_OPEN
00027
00028 template <typename Number> class BlockVector;
00029
00046 template<typename Number>
00047 class BlockSparseMatrixEZ : public Subscriptor
00048 {
00049 public:
00055 BlockSparseMatrixEZ ();
00056
00064 BlockSparseMatrixEZ (const unsigned int block_rows,
00065 const unsigned int block_cols);
00066
00077 BlockSparseMatrixEZ (const BlockSparseMatrixEZ<Number>&);
00078
00085 BlockSparseMatrixEZ & operator = (const BlockSparseMatrixEZ<Number>&);
00086
00103 BlockSparseMatrixEZ & operator = (const double d);
00104
00105
00110 void clear ();
00111
00126 void reinit (const unsigned int n_block_rows,
00127 const unsigned int n_block_cols);
00140 void collect_sizes ();
00141
00146 SparseMatrixEZ<Number>&
00147 block (const unsigned int row,
00148 const unsigned int column);
00149
00150
00156 const SparseMatrixEZ<Number>&
00157 block (const unsigned int row,
00158 const unsigned int column) const;
00159
00164 unsigned int n_block_rows () const;
00165
00170 unsigned int n_block_cols () const;
00171
00183 bool empty () const;
00184
00192 unsigned int n_rows () const;
00193
00202 unsigned int n_cols () const;
00203
00210 unsigned int m () const;
00211
00218 unsigned int n () const;
00219
00229 void set (const unsigned int i,
00230 const unsigned int j,
00231 const Number value);
00232
00242 void add (const unsigned int i, const unsigned int j,
00243 const Number value);
00244
00245
00251 template <typename somenumber>
00252 void vmult (BlockVector<somenumber> &dst,
00253 const BlockVector<somenumber> &src) const;
00254
00263 template <typename somenumber>
00264 void Tvmult (BlockVector<somenumber> &dst,
00265 const BlockVector<somenumber> &src) const;
00266
00273 template <typename somenumber>
00274 void vmult_add (BlockVector<somenumber> &dst,
00275 const BlockVector<somenumber> &src) const;
00276
00285 template <typename somenumber>
00286 void Tvmult_add (BlockVector<somenumber> &dst,
00287 const BlockVector<somenumber> &src) const;
00288
00289
00299 template <class STREAM>
00300 void print_statistics (STREAM& s, bool full = false);
00301
00302 private:
00309 BlockIndices row_indices;
00310
00317 BlockIndices column_indices;
00318
00322 Table<2, SparseMatrixEZ<Number> > blocks;
00323 };
00324
00326
00327
00328
00329 template <typename Number>
00330 inline
00331 unsigned int
00332 BlockSparseMatrixEZ<Number>::n_block_rows () const
00333 {
00334 return row_indices.size();
00335 }
00336
00337
00338
00339 template <typename Number>
00340 inline
00341 unsigned int
00342 BlockSparseMatrixEZ<Number>::n_rows () const
00343 {
00344 return row_indices.total_size();
00345 }
00346
00347
00348
00349 template <typename Number>
00350 inline
00351 unsigned int
00352 BlockSparseMatrixEZ<Number>::n_block_cols () const
00353 {
00354 return column_indices.size();
00355 }
00356
00357
00358
00359 template <typename Number>
00360 inline
00361 unsigned int
00362 BlockSparseMatrixEZ<Number>::n_cols () const
00363 {
00364 return column_indices.total_size();
00365 }
00366
00367
00368
00369 template <typename Number>
00370 inline
00371 SparseMatrixEZ<Number> &
00372 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
00373 const unsigned int column)
00374 {
00375 Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
00376 Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
00377
00378 return blocks[row][column];
00379 }
00380
00381
00382
00383 template <typename Number>
00384 inline
00385 const SparseMatrixEZ<Number> &
00386 BlockSparseMatrixEZ<Number>::block (const unsigned int row,
00387 const unsigned int column) const
00388 {
00389 Assert (row<n_block_rows(), ExcIndexRange (row, 0, n_block_rows()));
00390 Assert (column<n_block_cols(), ExcIndexRange (column, 0, n_block_cols()));
00391
00392 return blocks[row][column];
00393 }
00394
00395
00396
00397 template <typename Number>
00398 inline
00399 unsigned int
00400 BlockSparseMatrixEZ<Number>::m () const
00401 {
00402 return n_rows();
00403 }
00404
00405
00406
00407 template <typename Number>
00408 inline
00409 unsigned int
00410 BlockSparseMatrixEZ<Number>::n () const
00411 {
00412 return n_cols();
00413 }
00414
00415
00416
00417 template <typename Number>
00418 inline
00419 void
00420 BlockSparseMatrixEZ<Number>::set (const unsigned int i,
00421 const unsigned int j,
00422 const Number value)
00423 {
00424
00425 Assert (numbers::is_finite(value), ExcNumberNotFinite());
00426
00427 const std::pair<unsigned int,unsigned int>
00428 row_index = row_indices.global_to_local (i),
00429 col_index = column_indices.global_to_local (j);
00430 block(row_index.first,col_index.first).set (row_index.second,
00431 col_index.second,
00432 value);
00433 }
00434
00435
00436
00437 template <typename Number>
00438 inline
00439 void
00440 BlockSparseMatrixEZ<Number>::add (const unsigned int i,
00441 const unsigned int j,
00442 const Number value)
00443 {
00444
00445 Assert (numbers::is_finite(value), ExcNumberNotFinite());
00446
00447 const std::pair<unsigned int,unsigned int>
00448 row_index = row_indices.global_to_local (i),
00449 col_index = column_indices.global_to_local (j);
00450 block(row_index.first,col_index.first).add (row_index.second,
00451 col_index.second,
00452 value);
00453 }
00454
00455
00456 template <typename Number>
00457 template <typename somenumber>
00458 void
00459 BlockSparseMatrixEZ<Number>::vmult (BlockVector<somenumber> &dst,
00460 const BlockVector<somenumber> &src) const
00461 {
00462 Assert (dst.n_blocks() == n_block_rows(),
00463 ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
00464 Assert (src.n_blocks() == n_block_cols(),
00465 ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
00466
00467 dst = 0.;
00468
00469 for (unsigned int row=0; row<n_block_rows(); ++row)
00470 for (unsigned int col=0; col<n_block_cols(); ++col)
00471 block(row,col).vmult_add (dst.block(row),
00472 src.block(col));
00473 }
00474
00475
00476
00477 template <typename Number>
00478 template <typename somenumber>
00479 void
00480 BlockSparseMatrixEZ<Number>::
00481 vmult_add (BlockVector<somenumber> &dst,
00482 const BlockVector<somenumber> &src) const
00483 {
00484 Assert (dst.n_blocks() == n_block_rows(),
00485 ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
00486 Assert (src.n_blocks() == n_block_cols(),
00487 ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
00488
00489 for (unsigned int row=0; row<n_block_rows(); ++row)
00490 for (unsigned int col=0; col<n_block_cols(); ++col)
00491 block(row,col).vmult_add (dst.block(row),
00492 src.block(col));
00493 }
00494
00495
00496
00497
00498 template <typename Number>
00499 template <typename somenumber>
00500 void
00501 BlockSparseMatrixEZ<Number>::
00502 Tvmult (BlockVector<somenumber> &dst,
00503 const BlockVector<somenumber> &src) const
00504 {
00505 Assert (dst.n_blocks() == n_block_cols(),
00506 ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
00507 Assert (src.n_blocks() == n_block_rows(),
00508 ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
00509
00510 dst = 0.;
00511
00512 for (unsigned int row=0; row<n_block_rows(); ++row)
00513 for (unsigned int col=0; col<n_block_cols(); ++col)
00514 block(row,col).Tvmult_add (dst.block(col),
00515 src.block(row));
00516 }
00517
00518
00519
00520 template <typename Number>
00521 template <typename somenumber>
00522 void
00523 BlockSparseMatrixEZ<Number>::
00524 Tvmult_add (BlockVector<somenumber> &dst,
00525 const BlockVector<somenumber> &src) const
00526 {
00527 Assert (dst.n_blocks() == n_block_cols(),
00528 ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
00529 Assert (src.n_blocks() == n_block_rows(),
00530 ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
00531
00532 for (unsigned int row=0; row<n_block_rows(); ++row)
00533 {
00534 for (unsigned int col=0; col<n_block_cols(); ++col)
00535 block(row,col).Tvmult_add (dst.block(col),
00536 src.block(row));
00537 };
00538 }
00539
00540
00541 template <typename number>
00542 template <class STREAM>
00543 inline
00544 void
00545 BlockSparseMatrixEZ<number>::print_statistics (STREAM& out, bool full)
00546 {
00547 unsigned int used_total = 0;
00548 unsigned int allocated_total = 0;
00549 unsigned int reserved_total = 0;
00550 std::vector<unsigned int> used_by_line_total;
00551
00552 unsigned int used;
00553 unsigned int allocated;
00554 unsigned int reserved;
00555 std::vector<unsigned int> used_by_line;
00556
00557 for (unsigned int i=0;i<n_block_rows();++i)
00558 for (unsigned int j=0;j<n_block_cols();++j)
00559 {
00560 used_by_line.clear();
00561 out << "block:\t" << i << '\t' << j << std::endl;
00562 block(i,j).compute_statistics (used, allocated, reserved,
00563 used_by_line, full);
00564
00565 out << "used:" << used << std::endl
00566 << "allocated:" << allocated << std::endl
00567 << "reserved:" << reserved << std::endl;
00568
00569 used_total += used;
00570 allocated_total += allocated;
00571 reserved_total += reserved;
00572
00573 if (full)
00574 {
00575 used_by_line_total.resize(used_by_line.size());
00576 for (unsigned int i=0; i< used_by_line.size();++i)
00577 if (used_by_line[i] != 0)
00578 {
00579 out << "row-entries\t" << i
00580 << "\trows\t" << used_by_line[i]
00581 << std::endl;
00582 used_by_line_total[i] += used_by_line[i];
00583 }
00584 }
00585 }
00586 out << "Total" << std::endl
00587 << "used:" << used_total << std::endl
00588 << "allocated:" << allocated_total << std::endl
00589 << "reserved:" << reserved_total << std::endl;
00590 for (unsigned int i=0; i< used_by_line_total.size();++i)
00591 if (used_by_line_total[i] != 0)
00592 {
00593 out << "row-entries\t" << i
00594 << "\trows\t" << used_by_line_total[i]
00595 << std::endl;
00596 }
00597 }
00598
00599
00600 DEAL_II_NAMESPACE_CLOSE
00601
00602 #endif //__deal2__block_sparse_matrix_ez_h
00603