Reference documentation for deal.II version 9.0.0
block_vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2018 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_block_vector_h
17 #define dealii_block_vector_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/lac/block_indices.h>
23 #include <deal.II/lac/block_vector_base.h>
24 #include <deal.II/lac/vector_operation.h>
25 #include <deal.II/lac/vector_type_traits.h>
26 
27 #include <cstdio>
28 #include <vector>
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 
33 #ifdef DEAL_II_WITH_TRILINOS
34 namespace TrilinosWrappers
35 {
36  namespace MPI
37  {
38  class BlockVector;
39  }
40 }
41 #endif
42 
43 
65 template <typename Number>
66 class BlockVector : public BlockVectorBase<Vector<Number> >
67 {
68 public:
73 
77  typedef typename BaseClass::BlockType BlockType;
78 
82  typedef typename BaseClass::value_type value_type;
83  typedef typename BaseClass::real_type real_type;
84  typedef typename BaseClass::pointer pointer;
85  typedef typename BaseClass::const_pointer const_pointer;
86  typedef typename BaseClass::reference reference;
87  typedef typename BaseClass::const_reference const_reference;
88  typedef typename BaseClass::size_type size_type;
89  typedef typename BaseClass::iterator iterator;
91 
102  explicit BlockVector (const unsigned int n_blocks = 0,
103  const size_type block_size = 0);
104 
109  BlockVector (const BlockVector<Number> &V);
110 
111 
116  BlockVector (BlockVector<Number> &&/*v*/) noexcept = default;
117 
130  template <typename OtherNumber>
131  explicit
132  BlockVector (const BlockVector<OtherNumber> &v);
133 
134 #ifdef DEAL_II_WITH_TRILINOS
135 
140 
141 #endif
142 
146  BlockVector (const std::vector<size_type> &block_sizes);
147 
153 
163  template <typename InputIterator>
164  BlockVector (const std::vector<size_type> &block_sizes,
165  const InputIterator first,
166  const InputIterator end);
167 
171  ~BlockVector () = default;
172 
183  void compress (::VectorOperation::values operation
185 
190  BlockVector &operator= (const value_type s);
191 
198 
203  BlockVector<Number> &operator= (BlockVector<Number> &&/*v*/) = default; // NOLINT
204 
209  template <class Number2>
212 
217  operator= (const Vector<Number> &V);
218 
219 #ifdef DEAL_II_WITH_TRILINOS
220 
226 #endif
227 
240  void reinit (const unsigned int n_blocks,
241  const size_type block_size = 0,
242  const bool omit_zeroing_entries = false);
243 
260  void reinit (const std::vector<size_type> &block_sizes,
261  const bool omit_zeroing_entries=false);
262 
272  void reinit (const BlockIndices &block_indices,
273  const bool omit_zeroing_entries=false);
274 
288  template <typename Number2>
289  void reinit (const BlockVector<Number2> &V,
290  const bool omit_zeroing_entries=false);
291 
296  template <class BlockVector2>
297  void scale (const BlockVector2 &v);
298 
310  void swap (BlockVector<Number> &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 
325  void block_write (std::ostream &out) const;
326 
338  void block_read (std::istream &in);
339 
350 };
351 
354 #ifndef DOXYGEN
355 /*----------------------- Inline functions ----------------------------------*/
356 
357 
358 
359 template <typename Number>
360 template <typename InputIterator>
361 BlockVector<Number>::BlockVector (const std::vector<size_type> &block_sizes,
362  const InputIterator first,
363  const InputIterator end)
364 {
365  // first set sizes of blocks, but
366  // don't initialize them as we will
367  // copy elements soon
368  (void)end;
369  reinit (block_sizes, true);
370  InputIterator start = first;
371  for (size_type b=0; b<block_sizes.size(); ++b)
372  {
373  InputIterator end = start;
374  std::advance (end, static_cast<signed int>(block_sizes[b]));
375  std::copy (start, end, this->block(b).begin());
376  start = end;
377  };
378  Assert (start == end, ExcIteratorRangeDoesNotMatchVectorSize());
379 }
380 
381 
382 
383 template <typename Number>
384 inline
386 BlockVector<Number>::operator= (const value_type s)
387 {
388 
389  AssertIsFinite(s);
390 
391  BaseClass::operator= (s);
392  return *this;
393 }
394 
395 
396 
397 template <typename Number>
398 inline
401 {
402  reinit (v, true);
403  BaseClass::operator= (v);
404  return *this;
405 }
406 
407 
408 
409 template <typename Number>
410 inline
412 BlockVector<Number>::operator= (const Vector<Number> &v)
413 {
414  BaseClass::operator= (v);
415  return *this;
416 }
417 
418 
419 
420 template <typename Number>
421 template <typename Number2>
422 inline
425 {
426  reinit (v, true);
427  BaseClass::operator= (v);
428  return *this;
429 }
430 
431 template <typename Number>
432 inline
434 {
435  for (size_type i=0; i<this->n_blocks(); ++i)
436  this->components[i].compress(operation);
437 }
438 
439 
440 
441 template <typename Number>
442 template <class BlockVector2>
443 void BlockVector<Number>::scale (const BlockVector2 &v)
444 {
445  BaseClass::scale (v);
446 }
447 
448 #endif // DOXYGEN
449 
450 
459 template <typename Number>
460 inline
463 {
464  u.swap (v);
465 }
466 
467 
468 namespace internal
469 {
470  namespace LinearOperatorImplementation
471  {
472  template <typename> class ReinitHelper;
473 
478  template <typename number>
479  class ReinitHelper<BlockVector<number> >
480  {
481  public:
482  template <typename Matrix>
483  static
484  void reinit_range_vector (const Matrix &matrix,
486  bool omit_zeroing_entries)
487  {
488  v.reinit(matrix.get_row_indices(), omit_zeroing_entries);
489  }
490 
491  template <typename Matrix>
492  static
493  void reinit_domain_vector(const Matrix &matrix,
495  bool omit_zeroing_entries)
496  {
497  v.reinit(matrix.get_column_indices(), omit_zeroing_entries);
498  }
499  };
500 
501  } /* namespace LinearOperator */
502 } /* namespace internal */
503 
504 
510 template <typename Number>
511 struct is_serial_vector< BlockVector< Number > > : std::true_type
512 {
513 };
514 
515 DEAL_II_NAMESPACE_CLOSE
516 
517 #endif
BaseClass::value_type value_type
Definition: block_vector.h:82
BlockVector & operator=(const value_type s)
void block_write(std::ostream &out) const
void scale(const BlockVector2 &v)
static void reinit_range_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
void swap(BlockVector< Number > &u, BlockVector< Number > &v)
Definition: block_vector.h:461
BlockVectorBase< Vector< Number > > BaseClass
Definition: block_vector.h:72
~BlockVector()=default
static void reinit_domain_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
static ::ExceptionBase & ExcIteratorRangeDoesNotMatchVectorSize()
#define Assert(cond, exc)
Definition: exceptions.h:1142
BlockType::real_type real_type
void compress(::VectorOperation::values operation=::VectorOperation::unknown)
#define DeclException0(Exception0)
Definition: exceptions.h:323
void reinit(const unsigned int n_blocks, const size_type block_size=0, const bool omit_zeroing_entries=false)
void swap(BlockVector< Number > &v)
BlockVector(const unsigned int n_blocks=0, const size_type block_size=0)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
BaseClass::BlockType BlockType
Definition: block_vector.h:77
#define AssertIsFinite(number)
Definition: exceptions.h:1299
void block_read(std::istream &in)