Reference documentation for deal.II version 8.1.0
block_indices.h
1 // ---------------------------------------------------------------------
2 // @f$Id: block_indices.h 30040 2013-07-18 17:06:48Z maier @f$
3 //
4 // Copyright (C) 2000 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__block_indices_h
18 #define __deal2__block_indices_h
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/subscriptor.h>
24 #include <deal.II/base/logstream.h>
25 #include <cstddef>
26 #include <vector>
27 
29 
30 
55 class BlockIndices : public Subscriptor
56 {
57 public:
62 
68  BlockIndices ();
69 
77  BlockIndices (const std::vector<size_type> &n);
78 
83  explicit BlockIndices(const unsigned int n_blocks, const size_type block_size = 0);
84 
90  void reinit (const unsigned int n_blocks,
91  const size_type n_elements_per_block);
92 
102  void reinit (const std::vector<size_type> &n);
103 
109  void push_back(const size_type size);
110 
115 
119  unsigned int size () const;
120 
128  size_type total_size () const;
129 
133  size_type block_size (const unsigned int i) const;
134 
136 
151 
160  std::pair<unsigned int,size_type>
161  global_to_local (const size_type i) const;
162 
167  size_type local_to_global (const unsigned int block,
168  const size_type index) const;
169 
173  size_type block_start (const unsigned int i) const;
175 
180 
187  bool operator == (const BlockIndices &b) const;
188 
193  void swap (BlockIndices &b);
194 
200  std::size_t memory_consumption () const;
201 
202 private:
211  unsigned int n_blocks;
212 
219  std::vector<size_type> start_indices;
220 };
221 
222 
231 inline
232 LogStream &
233 operator << (LogStream &s, const BlockIndices &bi)
234 {
235  const unsigned int n = bi.size();
236  s << n << ":[";
237  // Write first size without leading space
238  if (n>0)
239  s << bi.block_size(0);
240  // Write all other sizes
241  for (unsigned int i=1; i<n; ++i)
242  s << ' ' << bi.block_size(i);
243  s << "]->" << bi.total_size();
244  return s;
245 }
246 
247 
248 template <typename MatrixType> class BlockMatrixBase;
249 template <typename SparsityType> class BlockSparsityPatternBase;
250 template <typename number> class BlockSparseMatrixEZ;
251 
269 template <typename MatrixType>
271 {
272 private:
273  struct yes_type
274  {
275  char c[1];
276  };
277  struct no_type
278  {
279  char c[2];
280  };
281 
289  template <typename T>
290  static yes_type check_for_block_matrix (const BlockMatrixBase<T> *);
291 
298  template <typename T>
299  static yes_type check_for_block_matrix (const BlockSparsityPatternBase<T> *);
300 
307  template <typename T>
308  static yes_type check_for_block_matrix (const BlockSparseMatrixEZ<T> *);
309 
315  static no_type check_for_block_matrix (...);
316 
317 public:
325  static const bool value = (sizeof(check_for_block_matrix
326  ((MatrixType *)0))
327  ==
328  sizeof(yes_type));
329 };
330 
331 
332 // instantiation of the static member
333 template <typename MatrixType>
335 
336 
337 /* ---------------------- template and inline functions ------------------- */
338 
339 inline
340 void
341 BlockIndices::reinit (const unsigned int nb,
342  const size_type block_size)
343 {
344  n_blocks = nb;
345  start_indices.resize(n_blocks+1);
346  for (size_type i=0; i<=n_blocks; ++i)
347  start_indices[i] = i * block_size;
348 }
349 
350 
351 
352 inline
353 void
354 BlockIndices::reinit (const std::vector<size_type> &n)
355 {
356  if (start_indices.size() != n.size()+1)
357  {
358  n_blocks = static_cast<unsigned int>(n.size());
359  start_indices.resize(n_blocks+1);
360  }
361  start_indices[0] = 0;
362  for (size_type i=1; i<=n_blocks; ++i)
363  start_indices[i] = start_indices[i-1] + n[i-1];
364 }
365 
366 
367 inline
369  :
370  n_blocks(0),
371  start_indices(1, 0)
372 {}
373 
374 
375 
376 inline
378  const unsigned int n_blocks,
379  const size_type block_size)
380  :
381  n_blocks(n_blocks),
382  start_indices(n_blocks+1)
383 {
384  for (size_type i=0; i<=n_blocks; ++i)
385  start_indices[i] = i * block_size;
386 }
387 
388 
389 
390 inline
391 BlockIndices::BlockIndices (const std::vector<size_type> &n)
392  :
393  n_blocks(static_cast<unsigned int>(n.size())),
394  start_indices(n.size()+1)
395 {
396  reinit (n);
397 }
398 
399 
400 inline
401 void
403 {
404  start_indices.push_back(start_indices[n_blocks]+sz);
405  ++n_blocks;
407 }
408 
409 
410 inline
411 std::pair<unsigned int,BlockIndices::size_type>
413 {
414  Assert (i<total_size(), ExcIndexRangeType<size_type>(i, 0, total_size()));
415  Assert (n_blocks > 0, ExcLowerRangeType<size_type>(i, size_type(1)));
416 
417  unsigned int block = n_blocks-1;
418  while (i < start_indices[block])
419  --block;
420 
421  return std::pair<size_type,size_type>(block,
422  i-start_indices[block]);
423 }
424 
425 
426 inline
428 BlockIndices::local_to_global (const unsigned int block,
429  const size_type index) const
430 {
431  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
432  Assert (index < start_indices[block+1]-start_indices[block],
433  ExcIndexRangeType<size_type> (index, 0, start_indices[block+1]-start_indices[block]));
434 
435  return start_indices[block]+index;
436 }
437 
438 
439 inline
440 unsigned int
442 {
443  return n_blocks;
444 }
445 
446 
447 
448 inline
451 {
452  if (n_blocks == 0) return 0;
453  return start_indices[n_blocks];
454 }
455 
456 
457 
458 inline
460 BlockIndices::block_size (const unsigned int block) const
461 {
462  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
463  return start_indices[block+1]-start_indices[block];
464 }
465 
466 
467 
468 inline
470 BlockIndices::block_start (const unsigned int block) const
471 {
472  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
473  return start_indices[block];
474 }
475 
476 
477 
478 inline
479 BlockIndices &
481 {
483  n_blocks = b.n_blocks;
484  return *this;
485 }
486 
487 
488 
489 inline
490 bool
492 {
493  if (n_blocks != b.n_blocks)
494  return false;
495 
496  for (size_type i=0; i<=n_blocks; ++i)
497  if (start_indices[i] != b.start_indices[i])
498  return false;
499 
500  return true;
501 }
502 
503 
504 
505 inline
506 void
508 {
509  Assert (n_blocks == b.n_blocks,
511 
512  for (size_type i=0; i<=n_blocks; ++i)
513  std::swap (start_indices[i], b.start_indices[i]);
514 }
515 
516 
517 
518 inline
519 std::size_t
521 {
522  return (sizeof(*this) +
523  start_indices.size() * sizeof(start_indices[0]));
524 }
525 
526 
527 
528 /* ----------------- global functions ---------------------------- */
529 
530 
539 inline
541 {
542  u.swap (v);
543 }
544 
545 
546 
547 
548 DEAL_II_NAMESPACE_CLOSE
549 
550 #endif
bool operator==(const BlockIndices &b) const
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:858
Auxiliary class aiding in the handling of block structures like in BlockVector or FESystem...
Definition: block_indices.h:55
std::size_t memory_consumption() const
unsigned int n_blocks
size_type block_start(const unsigned int i) const
void swap(BlockIndices &u, BlockIndices &v)
unsigned int global_dof_index
Definition: types.h:100
#define Assert(cond, exc)
Definition: exceptions.h:299
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
types::global_dof_index size_type
Definition: block_indices.h:61
size_type total_size() const
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
size_type block_size(const unsigned int i) const
std::vector< size_type > start_indices
std::ostream & operator<<(std::ostream &os, const Vector< number > &v)
Definition: vector.h:1546
BlockIndices & operator=(const BlockIndices &b)
void push_back(const size_type size)
void swap(BlockIndices &b)
unsigned int size() const
::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
size_type local_to_global(const unsigned int block, const size_type index) const