Reference documentation for deal.II version 9.1.0-pre
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.md at
12 // the top level directory of deal.II.
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 
22 #include <deal.II/base/exceptions.h>
23 
24 #include <deal.II/lac/block_indices.h>
25 #include <deal.II/lac/block_vector_base.h>
26 #include <deal.II/lac/vector_operation.h>
27 #include <deal.II/lac/vector_type_traits.h>
28 
29 #include <cstdio>
30 #include <vector>
31 
32 DEAL_II_NAMESPACE_OPEN
33 
34 
35 #ifdef DEAL_II_WITH_TRILINOS
36 namespace TrilinosWrappers
37 {
38  namespace MPI
39  {
40  class BlockVector;
41  }
42 } // namespace TrilinosWrappers
43 #endif
44 
45 
67 template <typename Number>
68 class BlockVector : public BlockVectorBase<Vector<Number>>
69 {
70 public:
75 
79  using BlockType = typename BaseClass::BlockType;
80 
84  using value_type = typename BaseClass::value_type;
85  using real_type = typename BaseClass::real_type;
86  using pointer = typename BaseClass::pointer;
87  using const_pointer = typename BaseClass::const_pointer;
88  using reference = typename BaseClass::reference;
89  using const_reference = typename BaseClass::const_reference;
90  using size_type = typename BaseClass::size_type;
91  using iterator = typename BaseClass::iterator;
93 
104  explicit BlockVector(const unsigned int n_blocks = 0,
105  const size_type block_size = 0);
106 
112 
113 
118  BlockVector(BlockVector<Number> && /*v*/) noexcept = default;
119 
132  template <typename OtherNumber>
133  explicit BlockVector(const BlockVector<OtherNumber> &v);
134 
135 #ifdef DEAL_II_WITH_TRILINOS
136 
141 
142 #endif
143 
147  BlockVector(const std::vector<size_type> &block_sizes);
148 
154 
164  template <typename InputIterator>
165  BlockVector(const std::vector<size_type> &block_sizes,
166  const InputIterator first,
167  const InputIterator end);
168 
172  ~BlockVector() override = default;
173 
184  void
185  compress(::VectorOperation::values operation =
187 
194  bool
195  has_ghost_elements() const;
196 
201  BlockVector &
202  operator=(const value_type s);
203 
209  operator=(const BlockVector<Number> &v);
210 
216  operator=(BlockVector<Number> && /*v*/) = default; // NOLINT
217 
222  template <class Number2>
225 
230  operator=(const Vector<Number> &V);
231 
232 #ifdef DEAL_II_WITH_TRILINOS
233 
239 #endif
240 
253  void
254  reinit(const unsigned int n_blocks,
255  const size_type block_size = 0,
256  const bool omit_zeroing_entries = false);
257 
274  void
275  reinit(const std::vector<size_type> &block_sizes,
276  const bool omit_zeroing_entries = false);
277 
287  void
289  const bool omit_zeroing_entries = false);
290 
304  template <typename Number2>
305  void
306  reinit(const BlockVector<Number2> &V,
307  const bool omit_zeroing_entries = false);
308 
313  template <class BlockVector2>
314  void
315  scale(const BlockVector2 &v);
316 
328  void
330 
334  void
335  print(std::ostream & out,
336  const unsigned int precision = 3,
337  const bool scientific = true,
338  const bool across = true) const;
339 
345  void
346  block_write(std::ostream &out) const;
347 
359  void
360  block_read(std::istream &in);
361 
372 };
373 
376 #ifndef DOXYGEN
377 /*----------------------- Inline functions ----------------------------------*/
378 
379 
380 
381 template <typename Number>
382 template <typename InputIterator>
383 BlockVector<Number>::BlockVector(const std::vector<size_type> &block_sizes,
384  const InputIterator first,
385  const InputIterator end)
386 {
387  // first set sizes of blocks, but
388  // don't initialize them as we will
389  // copy elements soon
390  (void)end;
391  reinit(block_sizes, true);
392  InputIterator start = first;
393  for (size_type b = 0; b < block_sizes.size(); ++b)
394  {
395  InputIterator end = start;
396  std::advance(end, static_cast<signed int>(block_sizes[b]));
397  std::copy(start, end, this->block(b).begin());
398  start = end;
399  };
401 }
402 
403 
404 
405 template <typename Number>
406 inline BlockVector<Number> &
407 BlockVector<Number>::operator=(const value_type s)
408 {
409  AssertIsFinite(s);
410 
412  return *this;
413 }
414 
415 
416 
417 template <typename Number>
418 inline BlockVector<Number> &
420 {
421  reinit(v, true);
423  return *this;
424 }
425 
426 
427 
428 template <typename Number>
429 inline BlockVector<Number> &
430 BlockVector<Number>::operator=(const Vector<Number> &v)
431 {
433  return *this;
434 }
435 
436 
437 
438 template <typename Number>
439 template <typename Number2>
440 inline BlockVector<Number> &
442 {
443  reinit(v, true);
445  return *this;
446 }
447 
448 template <typename Number>
449 inline void
451 {
452  for (size_type i = 0; i < this->n_blocks(); ++i)
453  this->components[i].compress(operation);
454 }
455 
456 
457 
458 template <typename Number>
459 inline bool
461 {
462  return false;
463 }
464 
465 
466 
467 template <typename Number>
468 template <class BlockVector2>
469 void
470 BlockVector<Number>::scale(const BlockVector2 &v)
471 {
472  BaseClass::scale(v);
473 }
474 
475 #endif // DOXYGEN
476 
477 
486 template <typename Number>
487 inline void
489 {
490  u.swap(v);
491 }
492 
493 
494 namespace internal
495 {
496  namespace LinearOperatorImplementation
497  {
498  template <typename>
500 
505  template <typename number>
506  class ReinitHelper<BlockVector<number>>
507  {
508  public:
509  template <typename Matrix>
510  static void
511  reinit_range_vector(const Matrix & matrix,
513  bool omit_zeroing_entries)
514  {
515  v.reinit(matrix.get_row_indices(), omit_zeroing_entries);
516  }
517 
518  template <typename Matrix>
519  static void
520  reinit_domain_vector(const Matrix & matrix,
522  bool omit_zeroing_entries)
523  {
524  v.reinit(matrix.get_column_indices(), omit_zeroing_entries);
525  }
526  };
527 
528  } // namespace LinearOperatorImplementation
529 } /* namespace internal */
530 
531 
537 template <typename Number>
538 struct is_serial_vector<BlockVector<Number>> : std::true_type
539 {};
540 
541 DEAL_II_NAMESPACE_CLOSE
542 
543 #endif
void compress(::VectorOperation::values operation)
static::ExceptionBase & ExcIteratorRangeDoesNotMatchVectorSize()
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
unsigned int n_blocks() const
void reinit(const std::vector< IndexSet > &parallel_partitioning, const MPI_Comm &communicator=MPI_COMM_WORLD, const bool omit_zeroing_entries=false)
void swap(BlockVector< Number > &u, BlockVector< Number > &v)
Definition: block_vector.h:488
void scale(const BlockVector2 &v)
#define Assert(cond, exc)
Definition: exceptions.h:1227
#define DeclException0(Exception0)
Definition: exceptions.h:385
void reinit(const unsigned int n_blocks, const size_type block_size=0, const bool omit_zeroing_entries=false)
std::vector< MPI::Vector > components
void swap(BlockVector< Number > &v)
BlockVector & operator=(const value_type s)
typename BlockType::real_type real_type
BlockType & block(const unsigned int i)
BlockVectorBase & operator=(const value_type s)
#define AssertIsFinite(number)
Definition: exceptions.h:1428