Reference documentation for deal.II version 8.5.1
petsc_block_vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 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__petsc_block_vector_h
17 #define dealii__petsc_block_vector_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_PETSC
23 
24 # include <deal.II/lac/petsc_vector.h>
25 # include <deal.II/lac/petsc_parallel_block_vector.h>
26 # include <deal.II/lac/block_indices.h>
27 # include <deal.II/lac/block_vector_base.h>
28 # include <deal.II/lac/exceptions.h>
29 # include <deal.II/lac/vector_type_traits.h>
30 
31 DEAL_II_NAMESPACE_OPEN
32 
33 
34 
35 namespace PETScWrappers
36 {
55  class BlockVector : public BlockVectorBase<Vector>
56  {
57  public:
62 
67 
71  typedef BaseClass::value_type value_type;
72  typedef BaseClass::pointer pointer;
73  typedef BaseClass::const_pointer const_pointer;
74  typedef BaseClass::reference reference;
75  typedef BaseClass::const_reference const_reference;
76  typedef BaseClass::size_type size_type;
79 
90  explicit BlockVector (const unsigned int num_blocks = 0,
91  const size_type block_size = 0);
92 
97  BlockVector (const BlockVector &V);
98 
109  explicit BlockVector (const MPI::BlockVector &v);
110 
115  BlockVector (const std::vector<size_type> &n);
116 
125  template <typename InputIterator>
126  BlockVector (const std::vector<size_type> &n,
127  const InputIterator first,
128  const InputIterator end);
129 
133  ~BlockVector ();
134 
140 
144  BlockVector &
145  operator= (const BlockVector &V);
146 
155  BlockVector &
156  operator = (const MPI::BlockVector &v);
157 
165  void reinit (const unsigned int num_blocks,
166  const size_type block_size,
167  const bool omit_zeroing_entries = false);
168 
187  void reinit (const std::vector<size_type> &N,
188  const bool omit_zeroing_entries=false);
189 
204  void reinit (const BlockVector &V,
205  const bool omit_zeroing_entries=false);
206 
213  void reinit (const unsigned int num_blocks);
214 
231  void swap (BlockVector &v);
232 
236  void print (std::ostream &out,
237  const unsigned int precision = 3,
238  const bool scientific = true,
239  const bool across = true) const;
240 
251  } DEAL_II_DEPRECATED;
252 
255  /*----------------------- Inline functions ----------------------------------*/
256 
257 
258 
259  inline
260  BlockVector::BlockVector (const unsigned int n_blocks,
261  const size_type block_size)
262  {
263  reinit (n_blocks, block_size);
264  }
265 
266 
267 
268  inline
269  BlockVector::BlockVector (const std::vector<size_type> &n)
270  {
271  reinit (n, false);
272  }
273 
274 
275  inline
277  :
279  {
280  this->components.resize (v.n_blocks());
282 
283  for (unsigned int i=0; i<this->n_blocks(); ++i)
284  this->components[i] = v.components[i];
285  }
286 
287 
288 
289  inline
291  :
293  {
294  this->components.resize (v.get_block_indices().size());
296 
297  for (unsigned int i=0; i<this->n_blocks(); ++i)
298  this->components[i] = v.block(i);
299  }
300 
301 
302 
303  template <typename InputIterator>
304  BlockVector::BlockVector (const std::vector<size_type> &n,
305  const InputIterator first,
306  const InputIterator end)
307  {
308  // first set sizes of blocks, but
309  // don't initialize them as we will
310  // copy elements soon
311  (void)end;
312  reinit (n, true);
313  InputIterator start = first;
314  for (unsigned int b=0; b<n.size(); ++b)
315  {
316  InputIterator end = start;
317  std::advance (end, static_cast<signed int>(n[b]));
318 
319  for (size_type i=0; i<n[b]; ++i, ++start)
320  this->block(b)(i) = *start;
321  }
323  }
324 
325 
326 
327  inline
328  BlockVector &
330  {
332  return *this;
333  }
334 
335 
336 
337  inline
338  BlockVector &
340  {
342  return *this;
343  }
344 
345 
346 
347  inline
348  BlockVector &
350  {
352  return *this;
353  }
354 
355 
356 
357  inline
359  {}
360 
361 
362  inline
363  void
364  BlockVector::reinit (const unsigned int n_bl,
365  const size_type bl_sz,
366  const bool omit_zeroing_entries)
367  {
368  std::vector<size_type> n(n_bl, bl_sz);
369  reinit(n, omit_zeroing_entries);
370  }
371 
372 
373 
374  inline
375  void
376  BlockVector::reinit (const std::vector<size_type> &n,
377  const bool omit_zeroing_entries)
378  {
379  block_indices.reinit (n);
380  if (this->components.size() != this->n_blocks())
381  this->components.resize(this->n_blocks());
382 
383  for (unsigned int i=0; i<this->n_blocks(); ++i)
384  this->components[i].reinit(n[i], omit_zeroing_entries);
385  }
386 
387 
388  inline
389  void
391  const bool omit_zeroing_entries)
392  {
394  if (this->components.size() != this->n_blocks())
395  this->components.resize(this->n_blocks());
396 
397  for (unsigned int i=0; i<this->n_blocks(); ++i)
398  block(i).reinit(v.block(i), omit_zeroing_entries);
399  }
400 
401 
402 
403  inline
404  void
405  BlockVector::reinit (const unsigned int num_blocks)
406  {
407  reinit (num_blocks, 0, true);
408  }
409 
410 
411 
412  inline
413  void
415  {
416  Assert (this->n_blocks() == v.n_blocks(),
417  ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
418 
419  for (unsigned int i=0; i<this->n_blocks(); ++i)
420  this->components[i].swap (v.components[i]);
422  }
423 
424 
425 
426  inline
427  void
428  BlockVector::print (std::ostream &out,
429  const unsigned int precision,
430  const bool scientific,
431  const bool across) const
432  {
433  for (unsigned int i=0; i<this->n_blocks(); ++i)
434  {
435  if (across)
436  out << 'C' << i << ':';
437  else
438  out << "Component " << i << std::endl;
439  this->components[i].print(out, precision, scientific, across);
440  }
441  }
442 
443 
444 
445 
454  inline
455  void swap (BlockVector &u,
456  BlockVector &v)
457  {
458  u.swap (v);
459  }
460 
461 }
462 
463 
464 namespace internal
465 {
466  namespace LinearOperator
467  {
468  template <typename> class ReinitHelper;
469 
474  template<>
476  {
477  public:
478  template <typename Matrix>
479  static
480  void reinit_range_vector (const Matrix &matrix,
482  bool omit_zeroing_entries)
483  {
484  v.reinit(matrix.locally_owned_range_sizes(), omit_zeroing_entries);
485  }
486 
487  template <typename Matrix>
488  static
489  void reinit_domain_vector(const Matrix &matrix,
491  bool omit_zeroing_entries)
492  {
493  v.reinit(matrix.locally_owned_domain_sizes(), omit_zeroing_entries);
494  }
495  };
496 
497  } /* namespace LinearOperator */
498 } /* namespace internal */
499 
500 
506 template <>
507 struct is_serial_vector< PETScWrappers::BlockVector > : std_cxx11::true_type
508 {
509 };
510 
511 
512 DEAL_II_NAMESPACE_CLOSE
513 
514 #endif // DEAL_II_WITH_PETSC
515 
516 #endif
static void reinit_domain_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
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)
const BlockIndices & get_block_indices() const
#define Assert(cond, exc)
Definition: exceptions.h:313
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
#define DeclException0(Exception0)
Definition: exceptions.h:541
std::vector< Vector > components
static void reinit_range_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
BaseClass::BlockType BlockType
void swap(BlockVector &u, BlockVector &v)
void reinit(const unsigned int num_blocks, const size_type block_size, const bool omit_zeroing_entries=false)
unsigned int n_blocks() const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
static ::ExceptionBase & ExcIteratorRangeDoesNotMatchVectorSize()
unsigned int size() const
BlockType & block(const unsigned int i)
BlockVectorBase & operator=(const value_type s)