Reference documentation for deal.II version 8.4.2
petsc_block_vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2016 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 
30 DEAL_II_NAMESPACE_OPEN
31 
32 
33 
34 namespace PETScWrappers
35 {
54  class BlockVector : public BlockVectorBase<Vector>
55  {
56  public:
61 
66 
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 
89  explicit BlockVector (const unsigned int num_blocks = 0,
90  const size_type block_size = 0);
91 
96  BlockVector (const BlockVector &V);
97 
108  explicit BlockVector (const MPI::BlockVector &v);
109 
114  BlockVector (const std::vector<size_type> &n);
115 
124  template <typename InputIterator>
125  BlockVector (const std::vector<size_type> &n,
126  const InputIterator first,
127  const InputIterator end);
128 
132  ~BlockVector ();
133 
138  BlockVector &operator = (const value_type s);
139 
143  BlockVector &
144  operator= (const BlockVector &V);
145 
154  BlockVector &
155  operator = (const MPI::BlockVector &v);
156 
164  void reinit (const unsigned int num_blocks,
165  const size_type block_size,
166  const bool omit_zeroing_entries = false);
167 
186  void reinit (const std::vector<size_type> &N,
187  const bool omit_zeroing_entries=false);
188 
203  void reinit (const BlockVector &V,
204  const bool omit_zeroing_entries=false);
205 
212  void reinit (const unsigned int num_blocks);
213 
229  void swap (BlockVector &v);
230 
234  void print (std::ostream &out,
235  const unsigned int precision = 3,
236  const bool scientific = true,
237  const bool across = true) const;
238 
247  DeclException0 (ExcIteratorRangeDoesNotMatchVectorSize);
250 
253  /*----------------------- Inline functions ----------------------------------*/
254 
255 
256 
257  inline
258  BlockVector::BlockVector (const unsigned int n_blocks,
259  const size_type block_size)
260  {
261  reinit (n_blocks, block_size);
262  }
263 
264 
265 
266  inline
267  BlockVector::BlockVector (const std::vector<size_type> &n)
268  {
269  reinit (n, false);
270  }
271 
272 
273  inline
275  :
277  {
278  this->components.resize (v.n_blocks());
280 
281  for (unsigned int i=0; i<this->n_blocks(); ++i)
282  this->components[i] = v.components[i];
283  }
284 
285 
286 
287  inline
289  :
291  {
292  this->components.resize (v.get_block_indices().size());
294 
295  for (unsigned int i=0; i<this->n_blocks(); ++i)
296  this->components[i] = v.block(i);
297  }
298 
299 
300 
301  template <typename InputIterator>
302  BlockVector::BlockVector (const std::vector<size_type> &n,
303  const InputIterator first,
304  const InputIterator end)
305  {
306  // first set sizes of blocks, but
307  // don't initialize them as we will
308  // copy elements soon
309  (void)end;
310  reinit (n, true);
311  InputIterator start = first;
312  for (unsigned int b=0; b<n.size(); ++b)
313  {
314  InputIterator end = start;
315  std::advance (end, static_cast<signed int>(n[b]));
316 
317  for (size_type i=0; i<n[b]; ++i, ++start)
318  this->block(b)(i) = *start;
319  }
320  Assert (start == end, ExcIteratorRangeDoesNotMatchVectorSize());
321  }
322 
323 
324 
325  inline
326  BlockVector &
328  {
330  return *this;
331  }
332 
333 
334 
335  inline
336  BlockVector &
338  {
340  return *this;
341  }
342 
343 
344 
345  inline
346  BlockVector &
348  {
350  return *this;
351  }
352 
353 
354 
355  inline
357  {}
358 
359 
360  inline
361  void
362  BlockVector::reinit (const unsigned int n_bl,
363  const size_type bl_sz,
364  const bool omit_zeroing_entries)
365  {
366  std::vector<size_type> n(n_bl, bl_sz);
367  reinit(n, omit_zeroing_entries);
368  }
369 
370 
371 
372  inline
373  void
374  BlockVector::reinit (const std::vector<size_type> &n,
375  const bool omit_zeroing_entries)
376  {
377  block_indices.reinit (n);
378  if (this->components.size() != this->n_blocks())
379  this->components.resize(this->n_blocks());
380 
381  for (unsigned int i=0; i<this->n_blocks(); ++i)
382  this->components[i].reinit(n[i], omit_zeroing_entries);
383  }
384 
385 
386  inline
387  void
389  const bool omit_zeroing_entries)
390  {
392  if (this->components.size() != this->n_blocks())
393  this->components.resize(this->n_blocks());
394 
395  for (unsigned int i=0; i<this->n_blocks(); ++i)
396  block(i).reinit(v.block(i), omit_zeroing_entries);
397  }
398 
399 
400 
401  inline
402  void
403  BlockVector::reinit (const unsigned int num_blocks)
404  {
405  reinit (num_blocks, 0, true);
406  }
407 
408 
409 
410  inline
411  void
413  {
414  Assert (this->n_blocks() == v.n_blocks(),
415  ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
416 
417  for (unsigned int i=0; i<this->n_blocks(); ++i)
418  this->components[i].swap (v.components[i]);
420  }
421 
422 
423 
424  inline
425  void
426  BlockVector::print (std::ostream &out,
427  const unsigned int precision,
428  const bool scientific,
429  const bool across) const
430  {
431  for (unsigned int i=0; i<this->n_blocks(); ++i)
432  {
433  if (across)
434  out << 'C' << i << ':';
435  else
436  out << "Component " << i << std::endl;
437  this->components[i].print(out, precision, scientific, across);
438  }
439  }
440 
441 
442 
443 
452  inline
453  void swap (BlockVector &u,
454  BlockVector &v)
455  {
456  u.swap (v);
457  }
458 
459 }
460 
461 
462 DEAL_II_NAMESPACE_CLOSE
463 
464 #endif // DEAL_II_WITH_PETSC
465 
466 #endif
BlockVector & operator=(const value_type s)
BaseClass::value_type value_type
BlockDynamicSparsityPattern BlockCompressedSparsityPattern DEAL_II_DEPRECATED
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:294
DeclException0(ExcIteratorRangeDoesNotMatchVectorSize)
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
std::vector< Vector > components
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)
unsigned int size() const
BlockType & block(const unsigned int i)
BlockVectorBase & operator=(const value_type s)