Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
petsc_block_vector.h
1 // ---------------------------------------------------------------------
2 // @f$Id: petsc_block_vector.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2004 - 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__petsc_block_vector_h
18 #define __deal2__petsc_block_vector_h
19 
20 
21 #include <deal.II/base/config.h>
22 
23 #ifdef DEAL_II_WITH_PETSC
24 
25 # include <deal.II/lac/petsc_vector.h>
26 # include <deal.II/lac/petsc_parallel_block_vector.h>
27 # include <deal.II/lac/block_indices.h>
28 # include <deal.II/lac/block_vector_base.h>
29 # include <deal.II/lac/exceptions.h>
30 
32 
33 
34 
35 namespace PETScWrappers
36 {
51  class BlockVector : public BlockVectorBase<Vector>
52  {
53  public:
59 
65 
70  typedef BaseClass::value_type value_type;
71  typedef BaseClass::pointer pointer;
72  typedef BaseClass::const_pointer const_pointer;
73  typedef BaseClass::reference reference;
74  typedef BaseClass::const_reference const_reference;
75  typedef BaseClass::size_type size_type;
78 
98  explicit BlockVector (const unsigned int num_blocks = 0,
99  const size_type block_size = 0);
100 
106  BlockVector (const BlockVector &V);
107 
123  explicit BlockVector (const MPI::BlockVector &v);
124 
131  BlockVector (const std::vector<size_type> &n);
132 
151  template <typename InputIterator>
152  BlockVector (const std::vector<size_type> &n,
153  const InputIterator first,
154  const InputIterator end);
155 
159  ~BlockVector ();
160 
166  BlockVector &operator = (const value_type s);
167 
172  BlockVector &
173  operator= (const BlockVector &V);
174 
188  BlockVector &
189  operator = (const MPI::BlockVector &v);
190 
199  void reinit (const unsigned int num_blocks,
200  const size_type block_size,
201  const bool fast = false);
202 
235  void reinit (const std::vector<size_type> &N,
236  const bool fast=false);
237 
264  void reinit (const BlockVector &V,
265  const bool fast=false);
266 
278  void reinit (const unsigned int num_blocks);
279 
310  void swap (BlockVector &v);
311 
315  void print (std::ostream &out,
316  const unsigned int precision = 3,
317  const bool scientific = true,
318  const bool across = true) const;
319 
326  DeclException0 (ExcIteratorRangeDoesNotMatchVectorSize);
328  };
329 
332  /*----------------------- Inline functions ----------------------------------*/
333 
334 
335 
336  inline
337  BlockVector::BlockVector (const unsigned int n_blocks,
338  const size_type block_size)
339  {
340  reinit (n_blocks, block_size);
341  }
342 
343 
344 
345  inline
346  BlockVector::BlockVector (const std::vector<size_type> &n)
347  {
348  reinit (n, false);
349  }
350 
351 
352  inline
354  :
356  {
357  this->components.resize (v.n_blocks());
359 
360  for (unsigned int i=0; i<this->n_blocks(); ++i)
361  this->components[i] = v.components[i];
362  }
363 
364 
365 
366  inline
368  :
370  {
371  this->components.resize (v.get_block_indices().size());
373 
374  for (unsigned int i=0; i<this->n_blocks(); ++i)
375  this->components[i] = v.block(i);
376  }
377 
378 
379 
380  template <typename InputIterator>
381  BlockVector::BlockVector (const std::vector<size_type> &n,
382  const InputIterator first,
383  const InputIterator end)
384  {
385  // first set sizes of blocks, but
386  // don't initialize them as we will
387  // copy elements soon
388  reinit (n, true);
389  InputIterator start = first;
390  for (unsigned int b=0; b<n.size(); ++b)
391  {
392  InputIterator end = start;
393  std::advance (end, static_cast<signed int>(n[b]));
394 
395  for (size_type i=0; i<n[b]; ++i, ++start)
396  this->block(b)(i) = *start;
397  }
398  Assert (start == end, ExcIteratorRangeDoesNotMatchVectorSize());
399  }
400 
401 
402 
403  inline
404  BlockVector &
406  {
408  return *this;
409  }
410 
411 
412 
413  inline
414  BlockVector &
416  {
418  return *this;
419  }
420 
421 
422 
423  inline
424  BlockVector &
426  {
428  return *this;
429  }
430 
431 
432 
433  inline
435  {}
436 
437 
438  inline
439  void
440  BlockVector::reinit (const unsigned int n_bl,
441  const size_type bl_sz,
442  const bool fast)
443  {
444  std::vector<size_type> n(n_bl, bl_sz);
445  reinit(n, fast);
446  }
447 
448 
449 
450  inline
451  void
452  BlockVector::reinit (const std::vector<size_type> &n,
453  const bool fast)
454  {
455  block_indices.reinit (n);
456  if (this->components.size() != this->n_blocks())
457  this->components.resize(this->n_blocks());
458 
459  for (unsigned int i=0; i<this->n_blocks(); ++i)
460  this->components[i].reinit(n[i], fast);
461  }
462 
463 
464  inline
465  void
467  const bool fast)
468  {
470  if (this->components.size() != this->n_blocks())
471  this->components.resize(this->n_blocks());
472 
473  for (unsigned int i=0; i<this->n_blocks(); ++i)
474  block(i).reinit(v.block(i), fast);
475  }
476 
477 
478 
479  inline
480  void
481  BlockVector::reinit (const unsigned int num_blocks)
482  {
483  reinit (num_blocks, 0, true);
484  }
485 
486 
487 
488  inline
489  void
491  {
492  Assert (this->n_blocks() == v.n_blocks(),
493  ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
494 
495  for (unsigned int i=0; i<this->n_blocks(); ++i)
496  this->components[i].swap (v.components[i]);
498  }
499 
500 
501 
502  inline
503  void
504  BlockVector::print (std::ostream &out,
505  const unsigned int precision,
506  const bool scientific,
507  const bool across) const
508  {
509  for (unsigned int i=0; i<this->n_blocks(); ++i)
510  {
511  if (across)
512  out << 'C' << i << ':';
513  else
514  out << "Component " << i << std::endl;
515  this->components[i].print(out, precision, scientific, across);
516  }
517  }
518 
519 
520 
521 
530  inline
531  void swap (BlockVector &u,
532  BlockVector &v)
533  {
534  u.swap (v);
535  }
536 
537 }
538 
539 
540 DEAL_II_NAMESPACE_CLOSE
541 
542 #endif // DEAL_II_WITH_PETSC
543 
544 #endif
void reinit(const unsigned int num_blocks, const size_type block_size, const bool fast=false)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
virtual void reinit(const size_type N, const bool fast=false)
const BlockIndices & get_block_indices() const
BlockVector & operator=(const value_type s)
BaseClass::value_type value_type
BlockVectorBase< Vector > BaseClass
BlockVector(const unsigned int num_blocks=0, const size_type block_size=0)
#define Assert(cond, exc)
Definition: exceptions.h:299
DeclException0(ExcIteratorRangeDoesNotMatchVectorSize)
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
BlockType & block(const unsigned int i)
BaseClass::BlockType BlockType
void swap(BlockVector &u, BlockVector &v)
unsigned int n_blocks() const
BlockVectorBase & operator=(const value_type s)
unsigned int size() const
::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
std::vector< Vector > components