Debian Codegraph @

include/deal.II/lac/block_vector_base.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2004 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_block_vector_base_h
16 #define dealii_block_vector_base_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/memory_consumption.h>
23 #include <deal.II/base/numbers.h>
24
25 #include <deal.II/lac/block_indices.h>
26 #include <deal.II/lac/exceptions.h>
27 #include <deal.II/lac/read_vector.h>
28 #include <deal.II/lac/vector.h>
29 #include <deal.II/lac/vector_operation.h>
30
31 #include <cmath>
32 #include <cstddef>
33 #include <iterator>
34 #include <type_traits>
35 #include <vector>
36
37 DEAL_II_NAMESPACE_OPEN
38
39
40 /**
41 * @addtogroup Vectors
42 * @{
43 */
44
45 namespace internal
46 {
47 template <typename T>
48 using has_block_t = decltype(std::declval<const T>().block(0));
49
50 template <typename T>
51 constexpr bool has_block = internal::is_supported_operation<has_block_t, T>;
52
53 template <typename T>
54 using has_n_blocks_t = decltype(std::declval<const T>().n_blocks());
55
56 template <typename T>
57 constexpr bool has_n_blocks =
58 internal::is_supported_operation<has_n_blocks_t, T>;
59
60 template <typename T>
61 constexpr bool is_block_vector = has_block<T> && has_n_blocks<T>;
62 } // namespace internal
63
64 /**
65 * A class that can be used to determine whether a given type is a block
66 * vector type or not. For example,
67 * @code
68 * IsBlockVector<Vector<double> >::value
69 * @endcode
70 * has the value false, whereas
71 * @code
72 * IsBlockVector<BlockVector<double> >::value
73 * @endcode
74 * is true. This is sometimes useful in template contexts where we may want to
75 * do things differently depending on whether a template type denotes a
76 * regular or a block vector type.
77 */
78 template <typename VectorType>
79 struct IsBlockVector
80 {
81 public:
82 /**
83 * A statically computable value that indicates whether the template
84 * argument to this class is a block vector (in fact whether the type has
85 * the functions `block()` and `n_blocks()`).
86 */
87 static const bool value = internal::is_block_vector<VectorType>;
88 };
89
90
91 // instantiation of the static member
92 template <typename VectorType>
93 const bool IsBlockVector<VectorType>::value;
94
95
96
97 namespace internal
98 {
99 /**
100 * Namespace in which iterators in block vectors are implemented.
101 */
102 namespace BlockVectorIterators
103 {
104 /**
105 * General random-access iterator class for block vectors. Since we do not
106 * want to have two classes for non-const iterator and const_iterator, we
107 * take a second template argument which denotes whether the vector we
108 * point into is a constant object or not. The first template argument is
109 * always the number type of the block vector in use.
110 *
111 * This class satisfies all requirements of random access iterators
112 * defined in the C++ standard. Operations on these iterators are constant
113 * in the number of elements in the block vector. However, they are
114 * sometimes linear in the number of blocks in the vector, but since that
115 * does rarely change dynamically within an application, this is a
116 * constant and we again have that the iterator satisfies the requirements
117 * of a random access iterator.
118 */
119 template <typename BlockVectorType, bool Constness>
120 class Iterator
121 {
122 public:
123 /**
124 * Declare the type for container size.
125 */
126 using size_type = types::global_dof_index;
127
128 /**
129 * Type of the number this iterator points to. Depending on the value of
130 * the second template parameter, this is either a constant or non-const
131 * number.
132 */
133 using value_type =
134 std::conditional_t<Constness,
135 const typename BlockVectorType::value_type,
136 typename BlockVectorType::value_type>;
137
138 /**
139 * Declare some aliases that are standard for iterators and are used
140 * by algorithms to enquire about the specifics of the iterators they
141 * work on. (Example: `std::next()`, which needs to know about a local
142 * type named `difference_type`.)
143 *
144 * As for the iterator_category, C++20 has a more specialized
145 * contiguous_iterator_tag, but the elements of block vectors are
146 * not stored in a contiguous manner. They can be accessed in a
147 * random access order, though, with O(1) effort as long as we
148 * assume that the number of blocks of a vector is a constant
149 * (even though the *size* of the vector is not).
150 */
151 using iterator_category = std::random_access_iterator_tag;
152 using difference_type = std::ptrdiff_t;
153 using reference = typename BlockVectorType::reference;
154 using pointer = value_type *;
155
156 using dereference_type =
157 std::conditional_t<Constness,
158 value_type,
159 typename BlockVectorType::BlockType::reference>;
160
161 /**
162 * Typedef the type of the block vector (which differs in constness,
163 * depending on the second template parameter).
164 */
165 using BlockVector =
166 std::conditional_t<Constness, const BlockVectorType, BlockVectorType>;
167
168 /**
169 * Construct an iterator from a vector to which we point and the global
170 * index of the element pointed to.
171 *
172 * Depending on the value of the <tt>Constness</tt> template argument of
173 * this class, the first argument of this constructor is either is a
174 * const or non-const reference.
175 */
176 Iterator(BlockVector &parent, const size_type global_index);
177
178 /**
179 * Copy constructor from an iterator of different constness.
180 *
181 * @note Constructing a non-const iterator from a const iterator does
182 * not make sense. Attempting this will result in a compile-time error
183 * (via <code>static_assert</code>).
184 */
185 Iterator(const Iterator<BlockVectorType, !Constness> &c);
186
187
188 /**
189 * Copy constructor from an iterator with the same constness.
190 */
191 Iterator(const Iterator &c);
192
193 private:
194 /**
195 * Constructor used internally in this class. The arguments match
196 * exactly the values of the respective member variables.
197 */
198 Iterator(BlockVector &parent,
199 const size_type global_index,
200 const size_type current_block,
201 const size_type index_within_block,
202 const size_type next_break_forward,
203 const size_type next_break_backward);
204
205 public:
206 /**
207 * Copy operator.
208 */
209 Iterator &
210 operator=(const Iterator &c);
211
212 /**
213 * Dereferencing operator. If the template argument <tt>Constness</tt>
214 * is <tt>true</tt>, then no writing to the result is possible, making
215 * this a const_iterator.
216 */
217 dereference_type
218 operator*() const;
219
220 /**
221 * Random access operator, grant access to arbitrary elements relative
222 * to the one presently pointed to.
223 */
224 dereference_type
225 operator[](const difference_type d) const;
226
227 /**
228 * Prefix increment operator. This operator advances the iterator to the
229 * next element and returns a reference to <tt>*this</tt>.
230 */
231 Iterator &
232 operator++();
233
234 /**
235 * Postfix increment operator. This operator advances the iterator to
236 * the next element and returns a copy of the old value of this
237 * iterator.
238 */
239 Iterator
240 operator++(int);
241
242 /**
243 * Prefix decrement operator. This operator retracts the iterator to the
244 * previous element and returns a reference to <tt>*this</tt>.
245 */
246 Iterator &
247 operator--();
248
249 /**
250 * Postfix decrement operator. This operator retracts the iterator to
251 * the previous element and returns a copy of the old value of this
252 * iterator.
253 */
254 Iterator
255 operator--(int);
256
257 /**
258 * Compare for equality of iterators. This operator checks whether the
259 * vectors pointed to are the same, and if not it throws an exception.
260 */
261 template <bool OtherConstness>
262 bool
263 operator==(const Iterator<BlockVectorType, OtherConstness> &i) const;
264
265 /**
266 * Compare for inequality of iterators. This operator checks whether the
267 * vectors pointed to are the same, and if not it throws an exception.
268 */
269 template <bool OtherConstness>
270 bool
271 operator!=(const Iterator<BlockVectorType, OtherConstness> &i) const;
272
273 /**
274 * Check whether this iterators points to an element previous to the one
275 * pointed to by the given argument. This operator checks whether the
276 * vectors pointed to are the same, and if not it throws an exception.
277 */
278 template <bool OtherConstness>
279 bool
280 operator<(const Iterator<BlockVectorType, OtherConstness> &i) const;
281
282 /**
283 * Comparison operator alike to the one above.
284 */
285 template <bool OtherConstness>
286 bool
287 operator<=(const Iterator<BlockVectorType, OtherConstness> &i) const;
288
289 /**
290 * Comparison operator alike to the one above.
291 */
292 template <bool OtherConstness>
293 bool
294 operator>(const Iterator<BlockVectorType, OtherConstness> &i) const;
295
296 /**
297 * Comparison operator alike to the one above.
298 */
299 template <bool OtherConstness>
300 bool
301 operator>=(const Iterator<BlockVectorType, OtherConstness> &i) const;
302
303 /**
304 * Return the distance between the two iterators, in elements.
305 */
306 template <bool OtherConstness>
307 difference_type
308 operator-(const Iterator<BlockVectorType, OtherConstness> &i) const;
309
310 /**
311 * Return an iterator which is the given number of elements in front of
312 * the present one.
313 */
314 Iterator
315 operator+(const difference_type &d) const;
316
317 /**
318 * Return an iterator which is the given number of elements behind the
319 * present one.
320 */
321 Iterator
322 operator-(const difference_type &d) const;
323
324 /**
325 * Move the iterator <tt>d</tt> elements forward at once, and return the
326 * result.
327 */
328 Iterator &
329 operator+=(const difference_type &d);
330
331 /**
332 * Move the iterator <tt>d</tt> elements backward at once, and return
333 * the result.
334 */
335 Iterator &
336 operator-=(const difference_type &d);
337
338 /**
339 * @addtogroup Exceptions
340 * @{
341 */
342
343 /**
344 * Exception thrown when one performs arithmetical comparisons on
345 * iterators belonging to two different block vectors.
346 */
347 DeclExceptionMsg(ExcPointerToDifferentVectors,
348 "Your program tried to compare iterators pointing to "
349 "different block vectors. There is no reasonable way "
350 "to do this.");
351
352 /** @} */
353 private:
354 /**
355 * Pointer to the block vector object to which this iterator points.
356 * Depending on the value of the <tt>Constness</tt> template argument of
357 * this class, this is a <tt>const</tt> or non-<tt>const</tt> pointer.
358 */
359 BlockVector *parent;
360
361 /**
362 * Global index of the element to which we presently point.
363 */
364 size_type global_index;
365
366 /**
367 * Current block and index within this block of the element presently
368 * pointed to.
369 */
370 unsigned int current_block;
371 size_type index_within_block;
372
373 /**
374 * Indices of the global element address at which we have to move on to
375 * another block when moving forward and backward. These indices are
376 * kept as a cache since this is much more efficient than always asking
377 * the parent object.
378 */
379 size_type next_break_forward;
380 size_type next_break_backward;
381
382 /**
383 * Move forward one element.
384 */
385 void
386 move_forward();
387
388 /**
389 * Move backward one element.
390 */
391 void
392 move_backward();
393
394 // Mark all other instances of this template as friends.
395 template <typename, bool>
396 friend class Iterator;
397 };
398 } // namespace BlockVectorIterators
399 } // namespace internal
400
401
402 /**
403 * A vector composed of several blocks each representing a vector of its own.
404 *
405 * The BlockVector is a collection of vectors of a given type (e.g., deal.II
406 * Vector objects, PETScWrappers::MPI::Vector objects, etc.). Each of the
407 * vectors inside can have a different size.
408 *
409 * The functionality of BlockVector includes everything a Vector can do, plus
410 * the access to a single Vector inside the BlockVector by <tt>block(i)</tt>.
411 * It also has a complete random access iterator, just as the other Vector
412 * classes or the standard C++ library template <tt>std::vector</tt>.
413 * Therefore, all algorithms working on iterators also work with objects of
414 * this class.
415 *
416 * While this base class implements most of the functionality by dispatching
417 * calls to its member functions to the respective functions on each of the
418 * individual blocks, this class does not actually allocate some memory or
419 * change the size of vectors. For this, the constructors, assignment
420 * operators and reinit() functions of derived classes are responsible. This
421 * class only handles the common part that is independent of the actual vector
422 * type the block vector is built on.
423 *
424 *
425 * <h3>Accessing individual blocks, and resizing vectors</h3>
426 *
427 * Apart from using this object as a whole, you can use each block separately
428 * as a vector, using the block() function. There is a single caveat: if you
429 * have changed the size of one or several blocks, you must call the function
430 * collect_sizes() of the block vector to update its internal structures.
431 *
432 * @attention Warning: If you change the sizes of single blocks without
433 * calling collect_sizes(), results may be unpredictable. The debug version
434 * does not check consistency here for performance reasons!
435 *
436 * @see
437 * @ref GlossBlockLA "Block (linear algebra)"
438 */
439 template <typename VectorType>
440 class BlockVectorBase : public ReadVector<typename VectorType::value_type>
441 {
442 public:
443 /**
444 * Typedef the type of the underlying vector.
445 */
446 using BlockType = VectorType;
447
448 /*
449 * Declare standard types used in
450 * all containers. These types
451 * parallel those in the
452 * <tt>C++</tt> standard
453 * libraries
454 * <tt>std::vector<...></tt>
455 * class. This includes iterator
456 * types.
457 */
458 using value_type = typename BlockType::value_type;
459 using pointer = value_type *;
460 using const_pointer = const value_type *;
461 using iterator =
462 dealii::internal::BlockVectorIterators::Iterator<BlockVectorBase, false>;
463 using const_iterator =
464 dealii::internal::BlockVectorIterators::Iterator<BlockVectorBase, true>;
465 using reference = typename BlockType::reference;
466 using const_reference = typename BlockType::const_reference;
467 using size_type = types::global_dof_index;
468
469 /**
470 * Declare a type that has holds real-valued numbers with the same precision
471 * as the template argument to this class. If the template argument of this
472 * class is a real data type, then real_type equals the template argument.
473 * If the template argument is a std::complex type then real_type equals the
474 * type underlying the complex numbers.
475 *
476 * This alias is used to represent the return type of norms.
477 */
478 using real_type = typename BlockType::real_type;
479
480 /**
481 * Default constructor.
482 */
483 BlockVectorBase() = default;
484
485 /**
486 * Copy constructor.
487 */
488 BlockVectorBase(const BlockVectorBase & /*V*/) = default;
489
490 /**
491 * Move constructor. Each block of the argument vector is moved into the
492 * current object if the underlying <code>VectorType</code> is
493 * move-constructible, otherwise they are copied.
494 */
495 BlockVectorBase(BlockVectorBase && /*V*/) noexcept = default;
496
497 /**
498 * Update internal structures after resizing vectors. Whenever you reinited
499 * a block of a block vector, the internal data structures are corrupted.
500 * Therefore, you should call this function after all blocks got their new
501 * size.
502 */
503 void
504 collect_sizes();
505
506 /**
507 * Call the compress() function on all the subblocks of the vector.
508 *
509 * This functionality only needs to be called if using MPI based vectors and
510 * exists in other objects for compatibility.
511 *
512 * See
513 * @ref GlossCompress "Compressing distributed objects"
514 * for more information.
515 */
516 void
517 compress(VectorOperation::values operation);
518
519 /**
520 * Access to a single block.
521 */
522 BlockType &
523 block(const unsigned int i);
524
525 /**
526 * Read-only access to a single block.
527 */
528 const BlockType &
529 block(const unsigned int i) const;
530
531 /**
532 * Return a reference on the object that describes the mapping between block
533 * and global indices. The use of this function is highly deprecated and it
534 * should vanish in one of the next versions
535 */
536 const BlockIndices &
537 get_block_indices() const;
538
539 /**
540 * Number of blocks.
541 */
542 unsigned int
543 n_blocks() const;
544
545 /**
546 * Return dimension of the vector. This is the sum of the dimensions of all
547 * components.
548 */
549 virtual size_type
550 size() const override;
551
552 /**
553 * Return local dimension of the vector. This is the sum of the local
554 * dimensions (i.e., values stored on the current processor) of all
555 * components.
556 */
557 std::size_t
558 locally_owned_size() const;
559
560 /**
561 * Return an index set that describes which elements of this vector are
562 * owned by the current processor. Note that this index set does not include
563 * elements this vector may store locally as ghost elements but that are in
564 * fact owned by another processor. As a consequence, the index sets
565 * returned on different processors if this is a distributed vector will
566 * form disjoint sets that add up to the complete index set. Obviously, if a
567 * vector is created on only one processor, then the result would satisfy
568 * @code
569 * vec.locally_owned_elements() == complete_index_set (vec.size())
570 * @endcode
571 *
572 * For block vectors, this function returns the union of the locally owned
573 * elements of the individual blocks, shifted by their respective index
574 * offsets.
575 */
576 IndexSet
577 locally_owned_elements() const;
578
579 /**
580 * Return an iterator pointing to the first element.
581 */
582 iterator
583 begin();
584
585 /**
586 * Return an iterator pointing to the first element of a constant block
587 * vector.
588 */
589 const_iterator
590 begin() const;
591
592 /**
593 * Return an iterator pointing to the element past the end.
594 */
595 iterator
596 end();
597
598 /**
599 * Return an iterator pointing to the element past the end of a constant
600 * block vector.
601 */
602 const_iterator
603 end() const;
604
605 /**
606 * Access components, returns U(i).
607 */
608 value_type
609 operator()(const size_type i) const;
610
611 /**
612 * Access components, returns U(i) as a writeable reference.
613 */
614 reference
615 operator()(const size_type i);
616
617 /**
618 * Access components, returns U(i).
619 *
620 * Exactly the same as operator().
621 */
622 value_type
623 operator[](const size_type i) const;
624
625 /**
626 * Access components, returns U(i) as a writeable reference.
627 *
628 * Exactly the same as operator().
629 */
630 reference
631 operator[](const size_type i);
632
633 /**
634 * Instead of getting individual elements of a vector via operator(),
635 * this function allows getting a whole set of elements at once. The
636 * indices of the elements to be read are stated in the first argument, the
637 * corresponding values are returned in the second.
638 *
639 * If the current vector is called @p v, then this function is the equivalent
640 * to the code
641 * @code
642 * for (unsigned int i=0; i<indices.size(); ++i)
643 * values[i] = v[indices[i]];
644 * @endcode
645 *
646 * @pre The sizes of the @p indices and @p values arrays must be identical.
647 */
648 template <typename OtherNumber>
649 void
650 extract_subvector_to(const std::vector<size_type> &indices,
651 std::vector<OtherNumber> &values) const;
652
653 virtual void
654 extract_subvector_to(const ArrayView<const types::global_dof_index> &indices,
655 const ArrayView<value_type> &entries) const override;
656
657 /**
658 * Instead of getting individual elements of a vector via operator(),
659 * this function allows getting a whole set of elements at once. In
660 * contrast to the previous function, this function obtains the
661 * indices of the elements by dereferencing all elements of the iterator
662 * range provided by the first two arguments, and puts the vector
663 * values into memory locations obtained by dereferencing a range
664 * of iterators starting at the location pointed to by the third
665 * argument.
666 *
667 * If the current vector is called @p v, then this function is the equivalent
668 * to the code
669 * @code
670 * ForwardIterator indices_p = indices_begin;
671 * OutputIterator values_p = values_begin;
672 * while (indices_p != indices_end)
673 * {
674 * *values_p = v[*indices_p];
675 * ++indices_p;
676 * ++values_p;
677 * }
678 * @endcode
679 *
680 * @pre It must be possible to write into as many memory locations
681 * starting at @p values_begin as there are iterators between
682 * @p indices_begin and @p indices_end.
683 */
684 template <typename ForwardIterator, typename OutputIterator>
685 void
686 extract_subvector_to(ForwardIterator indices_begin,
687 const ForwardIterator indices_end,
688 OutputIterator values_begin) const;
689
690 /**
691 * Copy operator: fill all components of the vector with the given scalar
692 * value.
693 */
694 BlockVectorBase &
695 operator=(const value_type s);
696
697 /**
698 * Copy operator for arguments of the same type.
699 */
700 BlockVectorBase &
701 operator=(const BlockVectorBase &V);
702
703 /**
704 * Move assignment operator. Move each block of the given argument
705 * vector into the current object if `VectorType` is
706 * move-constructible, otherwise copy them.
707 */
708 BlockVectorBase &
709 operator=(BlockVectorBase && /*V*/) = default; // NOLINT
710
711 /**
712 * Copy operator for template arguments of different types.
713 */
714 template <typename VectorType2>
715 BlockVectorBase &
716 operator=(const BlockVectorBase<VectorType2> &V);
717
718 /**
719 * Copy operator from non-block vectors to block vectors.
720 */
721 BlockVectorBase &
722 operator=(const VectorType &v);
723
724 /**
725 * Check for equality of two block vector types. This operation is only
726 * allowed if the two vectors already have the same block structure.
727 */
728 template <typename VectorType2>
729 bool
730 operator==(const BlockVectorBase<VectorType2> &v) const;
731
732 /**
733 * $U = U * V$: scalar product.
734 */
735 value_type
736 operator*(const BlockVectorBase &V) const;
737
738 /**
739 * Return the square of the $l_2$-norm.
740 */
741 real_type
742 norm_sqr() const;
743
744 /**
745 * Return the mean value of the elements of this vector.
746 */
747 value_type
748 mean_value() const;
749
750 /**
751 * Return the $l_1$-norm of the vector, i.e. the sum of the absolute values.
752 */
753 real_type
754 l1_norm() const;
755
756 /**
757 * Return the $l_2$-norm of the vector, i.e. the square root of the sum of
758 * the squares of the elements.
759 */
760 real_type
761 l2_norm() const;
762
763 /**
764 * Return the maximum absolute value of the elements of this vector, which
765 * is the $l_\infty$-norm of a vector.
766 */
767 real_type
768 linfty_norm() const;
769
770 /**
771 * Performs a combined operation of a vector addition and a subsequent inner
772 * product, returning the value of the inner product. In other words, the
773 * result of this function is the same as if the user called
774 * @code
775 * this->add(a, V);
776 * return_value = *this * W;
777 * @endcode
778 *
779 * The reason this function exists is that this operation involves less
780 * memory transfer than calling the two functions separately on deal.II's
781 * vector classes (Vector<Number> and
782 * LinearAlgebra::distributed::Vector<double>). This method only needs to load
783 * three vectors, @p this, @p V, @p W, whereas calling separate methods
784 * means to load the calling vector @p this twice. Since most vector
785 * operations are memory transfer limited, this reduces the time by 25\% (or
786 * 50\% if @p W equals @p this).
787 *
788 * For complex-valued vectors, the scalar product in the second step is
789 * implemented as
790 * $\left<v,w\right>=\sum_i v_i \bar{w_i}$.
791 */
792 value_type
793 add_and_dot(const value_type a,
794 const BlockVectorBase &V,
795 const BlockVectorBase &W);
796
797 /**
798 * Return true if the given global index is in the local range of this
799 * processor. Asks the corresponding block.
800 */
801 bool
802 in_local_range(const size_type global_index) const;
803
804 /**
805 * Return whether the vector contains only elements with value zero. This
806 * function is mainly for internal consistency check and should seldom be
807 * used when not in debug mode since it uses quite some time.
808 */
809 bool
810 all_zero() const;
811
812 /**
813 * Return @p true if the vector has no negative entries, i.e. all entries
814 * are zero or positive. This function is used, for example, to check
815 * whether refinement indicators are really all positive (or zero).
816 */
817 bool
818 is_non_negative() const;
819
820 /**
821 * Addition operator. Fast equivalent to <tt>U.add(1, V)</tt>.
822 */
823 BlockVectorBase &
824 operator+=(const BlockVectorBase &V);
825
826 /**
827 * Subtraction operator. Fast equivalent to <tt>U.add(-1, V)</tt>.
828 */
829 BlockVectorBase &
830 operator-=(const BlockVectorBase &V);
831
832
833 /**
834 * A collective add operation: This function adds a whole set of values
835 * stored in @p values to the vector components specified by @p indices.
836 */
837 template <typename Number>
838 void
839 add(const std::vector<size_type> &indices, const std::vector<Number> &values);
840
841 /**
842 * This is a second collective add operation. As a difference, this function
843 * takes a deal.II vector of values.
844 */
845 template <typename Number>
846 void
847 add(const std::vector<size_type> &indices, const Vector<Number> &values);
848
849 /**
850 * Take an address where <tt>n_elements</tt> are stored contiguously and add
851 * them into the vector. Handles all cases which are not covered by the
852 * other two <tt>add()</tt> functions above.
853 */
854 template <typename Number>
855 void
856 add(const size_type n_elements,
857 const size_type *indices,
858 const Number *values);
859
860 /**
861 * $U(0-DIM)+=s$. Addition of <tt>s</tt> to all components. Note that
862 * <tt>s</tt> is a scalar and not a vector.
863 */
864 void
865 add(const value_type s);
866
867 /**
868 * U+=a*V. Simple addition of a scaled vector.
869 */
870 void
871 add(const value_type a, const BlockVectorBase &V);
872
873 /**
874 * U+=a*V+b*W. Multiple addition of scaled vectors.
875 */
876 void
877 add(const value_type a,
878 const BlockVectorBase &V,
879 const value_type b,
880 const BlockVectorBase &W);
881
882 /**
883 * U=s*U+V. Scaling and simple vector addition.
884 */
885 void
886 sadd(const value_type s, const BlockVectorBase &V);
887
888 /**
889 * U=s*U+a*V. Scaling and simple addition.
890 */
891 void
892 sadd(const value_type s, const value_type a, const BlockVectorBase &V);
893
894 /**
895 * U=s*U+a*V+b*W. Scaling and multiple addition.
896 */
897 void
898 sadd(const value_type s,
899 const value_type a,
900 const BlockVectorBase &V,
901 const value_type b,
902 const BlockVectorBase &W);
903
904 /**
905 * U=s*U+a*V+b*W+c*X. Scaling and multiple addition.
906 */
907 void
908 sadd(const value_type s,
909 const value_type a,
910 const BlockVectorBase &V,
911 const value_type b,
912 const BlockVectorBase &W,
913 const value_type c,
914 const BlockVectorBase &X);
915
916 /**
917 * Scale each element of the vector by a constant value.
918 */
919 BlockVectorBase &
920 operator*=(const value_type factor);
921
922 /**
923 * Scale each element of the vector by the inverse of the given value.
924 */
925 BlockVectorBase &
926 operator/=(const value_type factor);
927
928 /**
929 * Multiply each element of this vector by the corresponding element of
930 * <tt>v</tt>.
931 */
932 template <class BlockVector2>
933 void
934 scale(const BlockVector2 &v);
935
936 /**
937 * U=a*V. Assignment.
938 */
939 template <class BlockVector2>
940 void
941 equ(const value_type a, const BlockVector2 &V);
942
943 /**
944 * Update the ghost values by calling <code>update_ghost_values</code> for
945 * each block.
946 */
947 void
948 update_ghost_values() const;
949
950 /**
951 * This function returns the MPI communicator of the vector in the
952 * underlying blocks or, if the vector has not been initialized, the empty
953 * MPI_COMM_SELF.
954 */
955 MPI_Comm
956 get_mpi_communicator() const;
957
958 /**
959 * Determine an estimate for the memory consumption (in bytes) of this
960 * object.
961 */
962 std::size_t
963 memory_consumption() const;
964
965 protected:
966 /**
967 * Pointer to the array of components.
968 */
969 std::vector<VectorType> components;
970
971 /**
972 * Object managing the transformation between global indices and indices
973 * within the different blocks.
974 */
975 BlockIndices block_indices;
976
977 // Make the iterator class a friend.
978 template <typename N, bool C>
979 friend class dealii::internal::BlockVectorIterators::Iterator;
980
981 template <typename>
982 friend class BlockVectorBase;
983 };
984
985
986 /** @} */
987
988 /*----------------------- Inline functions ----------------------------------*/
989
990
991 #ifndef DOXYGEN
992 namespace internal
993 {
994 namespace BlockVectorIterators
995 {
996 template <typename BlockVectorType, bool Constness>
997 inline Iterator<BlockVectorType, Constness>::Iterator(
998 const Iterator<BlockVectorType, Constness> &c)
999 : parent(c.parent)
1000 , global_index(c.global_index)
1001 , current_block(c.current_block)
1002 , index_within_block(c.index_within_block)
1003 , next_break_forward(c.next_break_forward)
1004 , next_break_backward(c.next_break_backward)
1005 {}
1006
1007
1008
1009 template <typename BlockVectorType, bool Constness>
1010 inline Iterator<BlockVectorType, Constness>::Iterator(
1011 const Iterator<BlockVectorType, !Constness> &c)
1012 : parent(c.parent)
1013 , global_index(c.global_index)
1014 , current_block(c.current_block)
1015 , index_within_block(c.index_within_block)
1016 , next_break_forward(c.next_break_forward)
1017 , next_break_backward(c.next_break_backward)
1018 {
1019 // Only permit copy-constructing const iterators from non-const
1020 // iterators, and not vice versa (i.e., Constness must always be
1021 // true).
1022 static_assert(Constness == true,
1023 "Constructing a non-const iterator from a const iterator "
1024 "does not make sense.");
1025 }
1026
1027
1028
1029 template <typename BlockVectorType, bool Constness>
1030 inline Iterator<BlockVectorType, Constness>::Iterator(
1031 BlockVector &parent,
1032 const size_type global_index,
1033 const size_type current_block,
1034 const size_type index_within_block,
1035 const size_type next_break_forward,
1036 const size_type next_break_backward)
1037 : parent(&parent)
1038 , global_index(global_index)
1039 , current_block(current_block)
1040 , index_within_block(index_within_block)
1041 , next_break_forward(next_break_forward)
1042 , next_break_backward(next_break_backward)
1043 {}
1044
1045
1046
1047 template <typename BlockVectorType, bool Constness>
1048 inline Iterator<BlockVectorType, Constness> &
1049 Iterator<BlockVectorType, Constness>::operator=(const Iterator &c) =
1050 default;
1051
1052
1053
1054 template <typename BlockVectorType, bool Constness>
1055 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1056 Iterator<BlockVectorType, Constness>::operator*() const
1057 {
1058 return parent->block(current_block)(index_within_block);
1059 }
1060
1061
1062
1063 template <typename BlockVectorType, bool Constness>
1064 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1065 Iterator<BlockVectorType, Constness>::operator[](
1066 const difference_type d) const
1067 {
1068 // if the index pointed to is
1069 // still within the block we
1070 // currently point into, then we
1071 // can save the computation of
1072 // the block
1073 if ((global_index + d >= next_break_backward) &&
1074 (global_index + d <= next_break_forward))
1075 return parent->block(current_block)(index_within_block + d);
1076
1077 // if the index is not within the
1078 // block of the block vector into
1079 // which we presently point, then
1080 // there is no way: we have to
1081 // search for the block. this can
1082 // be done through the parent
1083 // class as well.
1084 return (*parent)(global_index + d);
1085 }
1086
1087
1088
1089 template <typename BlockVectorType, bool Constness>
1090 inline Iterator<BlockVectorType, Constness> &
1091 Iterator<BlockVectorType, Constness>::operator++()
1092 {
1093 move_forward();
1094 return *this;
1095 }
1096
1097
1098
1099 template <typename BlockVectorType, bool Constness>
1100 inline Iterator<BlockVectorType, Constness>
1101 Iterator<BlockVectorType, Constness>::operator++(int)
1102 {
1103 const Iterator old_value = *this;
1104 move_forward();
1105 return old_value;
1106 }
1107
1108
1109
1110 template <typename BlockVectorType, bool Constness>
1111 inline Iterator<BlockVectorType, Constness> &
1112 Iterator<BlockVectorType, Constness>::operator--()
1113 {
1114 move_backward();
1115 return *this;
1116 }
1117
1118
1119
1120 template <typename BlockVectorType, bool Constness>
1121 inline Iterator<BlockVectorType, Constness>
1122 Iterator<BlockVectorType, Constness>::operator--(int)
1123 {
1124 const Iterator old_value = *this;
1125 move_backward();
1126 return old_value;
1127 }
1128
1129
1130
1131 template <typename BlockVectorType, bool Constness>
1132 template <bool OtherConstness>
1133 inline bool
1134 Iterator<BlockVectorType, Constness>::operator==(
1135 const Iterator<BlockVectorType, OtherConstness> &i) const
1136 {
1137 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1138
1139 return (global_index == i.global_index);
1140 }
1141
1142
1143
1144 template <typename BlockVectorType, bool Constness>
1145 template <bool OtherConstness>
1146 inline bool
1147 Iterator<BlockVectorType, Constness>::operator!=(
1148 const Iterator<BlockVectorType, OtherConstness> &i) const
1149 {
1150 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1151
1152 return (global_index != i.global_index);
1153 }
1154
1155
1156
1157 template <typename BlockVectorType, bool Constness>
1158 template <bool OtherConstness>
1159 inline bool
1160 Iterator<BlockVectorType, Constness>::operator<(
1161 const Iterator<BlockVectorType, OtherConstness> &i) const
1162 {
1163 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1164
1165 return (global_index < i.global_index);
1166 }
1167
1168
1169
1170 template <typename BlockVectorType, bool Constness>
1171 template <bool OtherConstness>
1172 inline bool
1173 Iterator<BlockVectorType, Constness>::operator<=(
1174 const Iterator<BlockVectorType, OtherConstness> &i) const
1175 {
1176 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1177
1178 return (global_index <= i.global_index);
1179 }
1180
1181
1182
1183 template <typename BlockVectorType, bool Constness>
1184 template <bool OtherConstness>
1185 inline bool
1186 Iterator<BlockVectorType, Constness>::operator>(
1187 const Iterator<BlockVectorType, OtherConstness> &i) const
1188 {
1189 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1190
1191 return (global_index > i.global_index);
1192 }
1193
1194
1195
1196 template <typename BlockVectorType, bool Constness>
1197 template <bool OtherConstness>
1198 inline bool
1199 Iterator<BlockVectorType, Constness>::operator>=(
1200 const Iterator<BlockVectorType, OtherConstness> &i) const
1201 {
1202 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1203
1204 return (global_index >= i.global_index);
1205 }
1206
1207
1208
1209 template <typename BlockVectorType, bool Constness>
1210 template <bool OtherConstness>
1211 inline typename Iterator<BlockVectorType, Constness>::difference_type
1212 Iterator<BlockVectorType, Constness>::operator-(
1213 const Iterator<BlockVectorType, OtherConstness> &i) const
1214 {
1215 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1216
1217 return (static_cast<signed int>(global_index) -
1218 static_cast<signed int>(i.global_index));
1219 }
1220
1221
1222
1223 template <typename BlockVectorType, bool Constness>
1224 inline Iterator<BlockVectorType, Constness>
1225 Iterator<BlockVectorType, Constness>::operator+(
1226 const difference_type &d) const
1227 {
1228 // if the index pointed to is
1229 // still within the block we
1230 // currently point into, then we
1231 // can save the computation of
1232 // the block
1233 if ((global_index + d >= next_break_backward) &&
1234 (global_index + d <= next_break_forward))
1235 return Iterator(*parent,
1236 global_index + d,
1237 current_block,
1238 index_within_block + d,
1239 next_break_forward,
1240 next_break_backward);
1241 else
1242 // outside present block, so
1243 // have to seek new block
1244 // anyway
1245 return Iterator(*parent, global_index + d);
1246 }
1247
1248
1249
1250 template <typename BlockVectorType, bool Constness>
1251 inline Iterator<BlockVectorType, Constness>
1252 Iterator<BlockVectorType, Constness>::operator-(
1253 const difference_type &d) const
1254 {
1255 // if the index pointed to is
1256 // still within the block we
1257 // currently point into, then we
1258 // can save the computation of
1259 // the block
1260 if ((global_index - d >= next_break_backward) &&
1261 (global_index - d <= next_break_forward))
1262 return Iterator(*parent,
1263 global_index - d,
1264 current_block,
1265 index_within_block - d,
1266 next_break_forward,
1267 next_break_backward);
1268 else
1269 // outside present block, so
1270 // have to seek new block
1271 // anyway
1272 return Iterator(*parent, global_index - d);
1273 }
1274
1275
1276
1277 template <typename BlockVectorType, bool Constness>
1278 inline Iterator<BlockVectorType, Constness> &
1279 Iterator<BlockVectorType, Constness>::operator+=(const difference_type &d)
1280 {
1281 // if the index pointed to is
1282 // still within the block we
1283 // currently point into, then we
1284 // can save the computation of
1285 // the block
1286 if ((global_index + d >= next_break_backward) &&
1287 (global_index + d <= next_break_forward))
1288 {
1289 global_index += d;
1290 index_within_block += d;
1291 }
1292 else
1293 // outside present block, so
1294 // have to seek new block
1295 // anyway
1296 *this = Iterator(*parent, global_index + d);
1297
1298 return *this;
1299 }
1300
1301
1302
1303 template <typename BlockVectorType, bool Constness>
1304 inline Iterator<BlockVectorType, Constness> &
1305 Iterator<BlockVectorType, Constness>::operator-=(const difference_type &d)
1306 {
1307 // if the index pointed to is
1308 // still within the block we
1309 // currently point into, then we
1310 // can save the computation of
1311 // the block
1312 if ((global_index - d >= next_break_backward) &&
1313 (global_index - d <= next_break_forward))
1314 {
1315 global_index -= d;
1316 index_within_block -= d;
1317 }
1318 else
1319 // outside present block, so
1320 // have to seek new block
1321 // anyway
1322 *this = Iterator(*parent, global_index - d);
1323
1324 return *this;
1325 }
1326
1327
1328 template <typename BlockVectorType, bool Constness>
1329 Iterator<BlockVectorType, Constness>::Iterator(BlockVector &parent,
1330 const size_type global_index)
1331 : parent(&parent)
1332 , global_index(global_index)
1333 {
1334 // find which block we are
1335 // in. for this, take into
1336 // account that it happens at
1337 // times that people want to
1338 // initialize iterators
1339 // past-the-end
1340 if (global_index < parent.size())
1341 {
1342 const std::pair<size_type, size_type> indices =
1343 parent.block_indices.global_to_local(global_index);
1344 current_block = indices.first;
1345 index_within_block = indices.second;
1346
1347 next_break_backward =
1348 parent.block_indices.local_to_global(current_block, 0);
1349 next_break_forward =
1350 (parent.block_indices.local_to_global(current_block, 0) +
1351 parent.block_indices.block_size(current_block) - 1);
1352 }
1353 else
1354 // past the end. only have one
1355 // value for this
1356 {
1357 this->global_index = parent.size();
1358 current_block = parent.n_blocks();
1359 index_within_block = 0;
1360 next_break_backward = global_index;
1361 next_break_forward = numbers::invalid_size_type;
1362 };
1363 }
1364
1365
1366
1367 template <typename BlockVectorType, bool Constness>
1368 void
1369 Iterator<BlockVectorType, Constness>::move_forward()
1370 {
1371 if (global_index != next_break_forward)
1372 ++index_within_block;
1373 else
1374 {
1375 // ok, we traverse a boundary
1376 // between blocks:
1377 index_within_block = 0;
1378 ++current_block;
1379
1380 // break backwards is now old
1381 // break forward
1382 next_break_backward = next_break_forward + 1;
1383
1384 // compute new break forward
1385 if (current_block < parent->block_indices.size())
1386 next_break_forward +=
1387 parent->block_indices.block_size(current_block);
1388 else
1389 // if we are beyond the end,
1390 // then move the next
1391 // boundary arbitrarily far
1392 // away
1393 next_break_forward = numbers::invalid_size_type;
1394 };
1395
1396 ++global_index;
1397 }
1398
1399
1400
1401 template <typename BlockVectorType, bool Constness>
1402 void
1403 Iterator<BlockVectorType, Constness>::move_backward()
1404 {
1405 if (global_index != next_break_backward)
1406 --index_within_block;
1407 else if (current_block != 0)
1408 {
1409 // ok, we traverse a boundary
1410 // between blocks:
1411 --current_block;
1412 index_within_block =
1413 parent->block_indices.block_size(current_block) - 1;
1414
1415 // break forwards is now old
1416 // break backward
1417 next_break_forward = next_break_backward - 1;
1418
1419 // compute new break forward
1420 next_break_backward -=
1421 parent->block_indices.block_size(current_block);
1422 }
1423 else
1424 // current block was 0, we now
1425 // get into unspecified terrain
1426 {
1427 --current_block;
1428 index_within_block = numbers::invalid_size_type;
1429 next_break_forward = 0;
1430 next_break_backward = 0;
1431 };
1432
1433 --global_index;
1434 }
1435
1436
1437 } // namespace BlockVectorIterators
1438
1439 } // namespace internal
1440
1441
1442
1443 template <typename VectorType>
1444 inline typename BlockVectorBase<VectorType>::size_type
1445 BlockVectorBase<VectorType>::size() const
1446 {
1447 return block_indices.total_size();
1448 }
1449
1450
1451
1452 template <typename VectorType>
1453 inline std::size_t
1454 BlockVectorBase<VectorType>::locally_owned_size() const
1455 {
1456 std::size_t local_size = 0;
1457 for (unsigned int b = 0; b < n_blocks(); ++b)
1458 local_size += block(b).locally_owned_size();
1459 return local_size;
1460 }
1461
1462
1463
1464 template <typename VectorType>
1465 inline IndexSet
1466 BlockVectorBase<VectorType>::locally_owned_elements() const
1467 {
1468 IndexSet is(size());
1469
1470 // copy index sets from blocks into the global one, shifted
1471 // by the appropriate amount for each block
1472 for (unsigned int b = 0; b < n_blocks(); ++b)
1473 {
1474 IndexSet x = block(b).locally_owned_elements();
1475 is.add_indices(x, block_indices.block_start(b));
1476 }
1477
1478 is.compress();
1479
1480 return is;
1481 }
1482
1483
1484
1485 template <typename VectorType>
1486 inline unsigned int
1487 BlockVectorBase<VectorType>::n_blocks() const
1488 {
1489 return block_indices.size();
1490 }
1491
1492
1493 template <typename VectorType>
1494 inline typename BlockVectorBase<VectorType>::BlockType &
1495 BlockVectorBase<VectorType>::block(const unsigned int i)
1496 {
1497 AssertIndexRange(i, n_blocks());
1498
1499 return components[i];
1500 }
1501
1502
1503
1504 template <typename VectorType>
1505 inline const typename BlockVectorBase<VectorType>::BlockType &
1506 BlockVectorBase<VectorType>::block(const unsigned int i) const
1507 {
1508 AssertIndexRange(i, n_blocks());
1509
1510 return components[i];
1511 }
1512
1513
1514
1515 template <typename VectorType>
1516 inline const BlockIndices &
1517 BlockVectorBase<VectorType>::get_block_indices() const
1518 {
1519 return block_indices;
1520 }
1521
1522
1523 template <typename VectorType>
1524 inline void
1525 BlockVectorBase<VectorType>::collect_sizes()
1526 {
1527 std::vector<size_type> sizes(n_blocks());
1528
1529 for (size_type i = 0; i < n_blocks(); ++i)
1530 sizes[i] = block(i).size();
1531
1532 block_indices.reinit(sizes);
1533 }
1534
1535
1536
1537 template <typename VectorType>
1538 inline void
1539 BlockVectorBase<VectorType>::compress(VectorOperation::values operation)
1540 {
1541 for (unsigned int i = 0; i < n_blocks(); ++i)
1542 block(i).compress(operation);
1543 }
1544
1545
1546
1547 template <typename VectorType>
1548 inline typename BlockVectorBase<VectorType>::iterator
1549 BlockVectorBase<VectorType>::begin()
1550 {
1551 return iterator(*this, 0U);
1552 }
1553
1554
1555
1556 template <typename VectorType>
1557 inline typename BlockVectorBase<VectorType>::const_iterator
1558 BlockVectorBase<VectorType>::begin() const
1559 {
1560 return const_iterator(*this, 0U);
1561 }
1562
1563
1564 template <typename VectorType>
1565 inline typename BlockVectorBase<VectorType>::iterator
1566 BlockVectorBase<VectorType>::end()
1567 {
1568 return iterator(*this, size());
1569 }
1570
1571
1572
1573 template <typename VectorType>
1574 inline typename BlockVectorBase<VectorType>::const_iterator
1575 BlockVectorBase<VectorType>::end() const
1576 {
1577 return const_iterator(*this, size());
1578 }
1579
1580
1581 template <typename VectorType>
1582 inline bool
1583 BlockVectorBase<VectorType>::in_local_range(const size_type global_index) const
1584 {
1585 const std::pair<size_type, size_type> local_index =
1586 block_indices.global_to_local(global_index);
1587
1588 return components[local_index.first].in_local_range(global_index);
1589 }
1590
1591
1592 template <typename VectorType>
1593 bool
1594 BlockVectorBase<VectorType>::all_zero() const
1595 {
1596 for (size_type i = 0; i < n_blocks(); ++i)
1597 if (components[i].all_zero() == false)
1598 return false;
1599
1600 return true;
1601 }
1602
1603
1604
1605 template <typename VectorType>
1606 bool
1607 BlockVectorBase<VectorType>::is_non_negative() const
1608 {
1609 for (size_type i = 0; i < n_blocks(); ++i)
1610 if (components[i].is_non_negative() == false)
1611 return false;
1612
1613 return true;
1614 }
1615
1616
1617
1618 template <typename VectorType>
1619 typename BlockVectorBase<VectorType>::value_type
1620 BlockVectorBase<VectorType>::operator*(
1621 const BlockVectorBase<VectorType> &v) const
1622 {
1623 Assert(n_blocks() == v.n_blocks(),
1624 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1625
1626 value_type sum = 0.;
1627 for (size_type i = 0; i < n_blocks(); ++i)
1628 sum += components[i] * v.components[i];
1629
1630 return sum;
1631 }
1632
1633
1634 template <typename VectorType>
1635 typename BlockVectorBase<VectorType>::real_type
1636 BlockVectorBase<VectorType>::norm_sqr() const
1637 {
1638 real_type sum = 0.;
1639 for (size_type i = 0; i < n_blocks(); ++i)
1640 sum += components[i].norm_sqr();
1641
1642 return sum;
1643 }
1644
1645
1646
1647 template <typename VectorType>
1648 typename BlockVectorBase<VectorType>::value_type
1649 BlockVectorBase<VectorType>::mean_value() const
1650 {
1651 value_type sum = 0.;
1652 // need to do static_cast as otherwise it won't work with
1653 // value_type=complex<T>
1654 for (size_type i = 0; i < n_blocks(); ++i)
1655 sum += components[i].mean_value() *
1656 (typename numbers::NumberTraits<value_type>::real_type(
1657 components[i].size()));
1658
1659 return sum / (typename numbers::NumberTraits<value_type>::real_type(size()));
1660 }
1661
1662
1663
1664 template <typename VectorType>
1665 typename BlockVectorBase<VectorType>::real_type
1666 BlockVectorBase<VectorType>::l1_norm() const
1667 {
1668 real_type sum = 0.;
1669 for (size_type i = 0; i < n_blocks(); ++i)
1670 sum += components[i].l1_norm();
1671
1672 return sum;
1673 }
1674
1675
1676
1677 template <typename VectorType>
1678 typename BlockVectorBase<VectorType>::real_type
1679 BlockVectorBase<VectorType>::l2_norm() const
1680 {
1681 return std::sqrt(norm_sqr());
1682 }
1683
1684
1685
1686 template <typename VectorType>
1687 typename BlockVectorBase<VectorType>::real_type
1688 BlockVectorBase<VectorType>::linfty_norm() const
1689 {
1690 real_type sum = 0.;
1691 for (size_type i = 0; i < n_blocks(); ++i)
1692 {
1693 value_type newval = components[i].linfty_norm();
1694 if (sum < newval)
1695 sum = newval;
1696 }
1697 return sum;
1698 }
1699
1700
1701
1702 template <typename VectorType>
1703 typename BlockVectorBase<VectorType>::value_type
1704 BlockVectorBase<VectorType>::add_and_dot(
1705 const typename BlockVectorBase<VectorType>::value_type a,
1706 const BlockVectorBase<VectorType> &V,
1707 const BlockVectorBase<VectorType> &W)
1708 {
1709 AssertDimension(n_blocks(), V.n_blocks());
1710 AssertDimension(n_blocks(), W.n_blocks());
1711
1712 value_type sum = 0.;
1713 for (size_type i = 0; i < n_blocks(); ++i)
1714 sum += components[i].add_and_dot(a, V.components[i], W.components[i]);
1715
1716 return sum;
1717 }
1718
1719
1720
1721 template <typename VectorType>
1722 BlockVectorBase<VectorType> &
1723 BlockVectorBase<VectorType>::operator+=(const BlockVectorBase<VectorType> &v)
1724 {
1725 Assert(n_blocks() == v.n_blocks(),
1726 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1727
1728 for (size_type i = 0; i < n_blocks(); ++i)
1729 {
1730 components[i] += v.components[i];
1731 }
1732
1733 return *this;
1734 }
1735
1736
1737
1738 template <typename VectorType>
1739 BlockVectorBase<VectorType> &
1740 BlockVectorBase<VectorType>::operator-=(const BlockVectorBase<VectorType> &v)
1741 {
1742 Assert(n_blocks() == v.n_blocks(),
1743 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1744
1745 for (size_type i = 0; i < n_blocks(); ++i)
1746 {
1747 components[i] -= v.components[i];
1748 }
1749 return *this;
1750 }
1751
1752
1753
1754 template <typename VectorType>
1755 template <typename Number>
1756 inline void
1757 BlockVectorBase<VectorType>::add(const std::vector<size_type> &indices,
1758 const std::vector<Number> &values)
1759 {
1760 Assert(indices.size() == values.size(),
1761 ExcDimensionMismatch(indices.size(), values.size()));
1762 add(indices.size(), indices.data(), values.data());
1763 }
1764
1765
1766
1767 template <typename VectorType>
1768 template <typename Number>
1769 inline void
1770 BlockVectorBase<VectorType>::add(const std::vector<size_type> &indices,
1771 const Vector<Number> &values)
1772 {
1773 Assert(indices.size() == values.size(),
1774 ExcDimensionMismatch(indices.size(), values.size()));
1775 const size_type n_indices = indices.size();
1776 for (size_type i = 0; i < n_indices; ++i)
1777 (*this)(indices[i]) += values(i);
1778 }
1779
1780
1781
1782 template <typename VectorType>
1783 template <typename Number>
1784 inline void
1785 BlockVectorBase<VectorType>::add(const size_type n_indices,
1786 const size_type *indices,
1787 const Number *values)
1788 {
1789 for (size_type i = 0; i < n_indices; ++i)
1790 (*this)(indices[i]) += values[i];
1791 }
1792
1793
1794
1795 template <typename VectorType>
1796 void
1797 BlockVectorBase<VectorType>::add(const value_type a)
1798 {
1799 AssertIsFinite(a);
1800
1801 for (size_type i = 0; i < n_blocks(); ++i)
1802 {
1803 components[i].add(a);
1804 }
1805 }
1806
1807
1808
1809 template <typename VectorType>
1810 void
1811 BlockVectorBase<VectorType>::add(const value_type a,
1812 const BlockVectorBase<VectorType> &v)
1813 {
1814 AssertIsFinite(a);
1815
1816 Assert(n_blocks() == v.n_blocks(),
1817 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1818
1819 for (size_type i = 0; i < n_blocks(); ++i)
1820 {
1821 components[i].add(a, v.components[i]);
1822 }
1823 }
1824
1825
1826
1827 template <typename VectorType>
1828 void
1829 BlockVectorBase<VectorType>::add(const value_type a,
1830 const BlockVectorBase<VectorType> &v,
1831 const value_type b,
1832 const BlockVectorBase<VectorType> &w)
1833 {
1834 AssertIsFinite(a);
1835 AssertIsFinite(b);
1836
1837 Assert(n_blocks() == v.n_blocks(),
1838 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1839 Assert(n_blocks() == w.n_blocks(),
1840 ExcDimensionMismatch(n_blocks(), w.n_blocks()));
1841
1842
1843 for (size_type i = 0; i < n_blocks(); ++i)
1844 {
1845 components[i].add(a, v.components[i], b, w.components[i]);
1846 }
1847 }
1848
1849
1850
1851 template <typename VectorType>
1852 void
1853 BlockVectorBase<VectorType>::sadd(const value_type x,
1854 const BlockVectorBase<VectorType> &v)
1855 {
1856 AssertIsFinite(x);
1857
1858 Assert(n_blocks() == v.n_blocks(),
1859 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1860
1861 for (size_type i = 0; i < n_blocks(); ++i)
1862 {
1863 components[i].sadd(x, v.components[i]);
1864 }
1865 }
1866
1867
1868
1869 template <typename VectorType>
1870 void
1871 BlockVectorBase<VectorType>::sadd(const value_type x,
1872 const value_type a,
1873 const BlockVectorBase<VectorType> &v)
1874 {
1875 AssertIsFinite(x);
1876 AssertIsFinite(a);
1877
1878 Assert(n_blocks() == v.n_blocks(),
1879 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1880
1881 for (size_type i = 0; i < n_blocks(); ++i)
1882 {
1883 components[i].sadd(x, a, v.components[i]);
1884 }
1885 }
1886
1887
1888
1889 template <typename VectorType>
1890 void
1891 BlockVectorBase<VectorType>::sadd(const value_type x,
1892 const value_type a,
1893 const BlockVectorBase<VectorType> &v,
1894 const value_type b,
1895 const BlockVectorBase<VectorType> &w)
1896 {
1897 AssertIsFinite(x);
1898 AssertIsFinite(a);
1899 AssertIsFinite(b);
1900
1901 Assert(n_blocks() == v.n_blocks(),
1902 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1903 Assert(n_blocks() == w.n_blocks(),
1904 ExcDimensionMismatch(n_blocks(), w.n_blocks()));
1905
1906 for (size_type i = 0; i < n_blocks(); ++i)
1907 {
1908 components[i].sadd(x, a, v.components[i], b, w.components[i]);
1909 }
1910 }
1911
1912
1913
1914 template <typename VectorType>
1915 void
1916 BlockVectorBase<VectorType>::sadd(const value_type x,
1917 const value_type a,
1918 const BlockVectorBase<VectorType> &v,
1919 const value_type b,
1920 const BlockVectorBase<VectorType> &w,
1921 const value_type c,
1922 const BlockVectorBase<VectorType> &y)
1923 {
1924 AssertIsFinite(x);
1925 AssertIsFinite(a);
1926 AssertIsFinite(b);
1927 AssertIsFinite(c);
1928
1929 Assert(n_blocks() == v.n_blocks(),
1930 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1931 Assert(n_blocks() == w.n_blocks(),
1932 ExcDimensionMismatch(n_blocks(), w.n_blocks()));
1933 Assert(n_blocks() == y.n_blocks(),
1934 ExcDimensionMismatch(n_blocks(), y.n_blocks()));
1935
1936 for (size_type i = 0; i < n_blocks(); ++i)
1937 {
1938 components[i].sadd(
1939 x, a, v.components[i], b, w.components[i], c, y.components[i]);
1940 }
1941 }
1942
1943
1944
1945 template <typename VectorType>
1946 template <class BlockVector2>
1947 void
1948 BlockVectorBase<VectorType>::scale(const BlockVector2 &v)
1949 {
1950 Assert(n_blocks() == v.n_blocks(),
1951 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1952 for (size_type i = 0; i < n_blocks(); ++i)
1953 components[i].scale(v.block(i));
1954 }
1955
1956
1957
1958 template <typename VectorType>
1959 std::size_t
1960 BlockVectorBase<VectorType>::memory_consumption() const
1961 {
1962 return (MemoryConsumption::memory_consumption(this->block_indices) +
1963 MemoryConsumption::memory_consumption(this->components));
1964 }
1965
1966
1967
1968 template <typename VectorType>
1969 template <class BlockVector2>
1970 void
1971 BlockVectorBase<VectorType>::equ(const value_type a, const BlockVector2 &v)
1972 {
1973 AssertIsFinite(a);
1974
1975 Assert(n_blocks() == v.n_blocks(),
1976 ExcDimensionMismatch(n_blocks(), v.n_blocks()));
1977
1978 for (size_type i = 0; i < n_blocks(); ++i)
1979 components[i].equ(a, v.components[i]);
1980 }
1981
1982
1983
1984 template <typename VectorType>
1985 void
1986 BlockVectorBase<VectorType>::update_ghost_values() const
1987 {
1988 for (size_type i = 0; i < n_blocks(); ++i)
1989 block(i).update_ghost_values();
1990 }
1991
1992
1993
1994 template <typename VectorType>
1995 MPI_Comm
1996 BlockVectorBase<VectorType>::get_mpi_communicator() const
1997 {
1998 if (n_blocks() > 0)
1999 return block(0).get_mpi_communicator();
2000 else
2001 return MPI_COMM_SELF;
2002 }
2003
2004
2005
2006 template <typename VectorType>
2007 BlockVectorBase<VectorType> &
2008 BlockVectorBase<VectorType>::operator=(const value_type s)
2009 {
2010 AssertIsFinite(s);
2011
2012 for (size_type i = 0; i < n_blocks(); ++i)
2013 components[i] = s;
2014
2015 return *this;
2016 }
2017
2018
2019 template <typename VectorType>
2020 BlockVectorBase<VectorType> &
2021 BlockVectorBase<VectorType>::operator=(const BlockVectorBase<VectorType> &v)
2022 {
2023 AssertDimension(n_blocks(), v.n_blocks());
2024
2025 for (size_type i = 0; i < n_blocks(); ++i)
2026 components[i] = v.components[i];
2027
2028 return *this;
2029 }
2030
2031
2032 template <typename VectorType>
2033 template <typename VectorType2>
2034 BlockVectorBase<VectorType> &
2035 BlockVectorBase<VectorType>::operator=(const BlockVectorBase<VectorType2> &v)
2036 {
2037 AssertDimension(n_blocks(), v.n_blocks());
2038
2039 for (size_type i = 0; i < n_blocks(); ++i)
2040 components[i] = v.components[i];
2041
2042 return *this;
2043 }
2044
2045
2046
2047 template <typename VectorType>
2048 BlockVectorBase<VectorType> &
2049 BlockVectorBase<VectorType>::operator=(const VectorType &v)
2050 {
2051 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
2052
2053 size_type index_v = 0;
2054 for (size_type b = 0; b < n_blocks(); ++b)
2055 for (size_type i = 0; i < block(b).size(); ++i, ++index_v)
2056 block(b)(i) = v(index_v);
2057
2058 return *this;
2059 }
2060
2061
2062
2063 template <typename VectorType>
2064 template <typename VectorType2>
2065 inline bool
2066 BlockVectorBase<VectorType>::operator==(
2067 const BlockVectorBase<VectorType2> &v) const
2068 {
2069 Assert(block_indices == v.block_indices, ExcDifferentBlockIndices());
2070
2071 for (size_type i = 0; i < n_blocks(); ++i)
2072 if (!(components[i] == v.components[i]))
2073 return false;
2074
2075 return true;
2076 }
2077
2078
2079
2080 template <typename VectorType>
2081 inline BlockVectorBase<VectorType> &
2082 BlockVectorBase<VectorType>::operator*=(const value_type factor)
2083 {
2084 AssertIsFinite(factor);
2085
2086 for (size_type i = 0; i < n_blocks(); ++i)
2087 components[i] *= factor;
2088
2089 return *this;
2090 }
2091
2092
2093
2094 template <typename VectorType>
2095 inline BlockVectorBase<VectorType> &
2096 BlockVectorBase<VectorType>::operator/=(const value_type factor)
2097 {
2098 AssertIsFinite(factor);
2099 Assert(factor != 0., ExcDivideByZero());
2100
2101 const value_type inverse_factor = value_type(1.) / factor;
2102
2103 for (size_type i = 0; i < n_blocks(); ++i)
2104 components[i] *= inverse_factor;
2105
2106 return *this;
2107 }
2108
2109
2110 template <typename VectorType>
2111 inline typename BlockVectorBase<VectorType>::value_type
2112 BlockVectorBase<VectorType>::operator()(const size_type i) const
2113 {
2114 const std::pair<unsigned int, size_type> local_index =
2115 block_indices.global_to_local(i);
2116 return components[local_index.first](local_index.second);
2117 }
2118
2119
2120
2121 template <typename VectorType>
2122 inline typename BlockVectorBase<VectorType>::reference
2123 BlockVectorBase<VectorType>::operator()(const size_type i)
2124 {
2125 const std::pair<unsigned int, size_type> local_index =
2126 block_indices.global_to_local(i);
2127 return components[local_index.first](local_index.second);
2128 }
2129
2130
2131
2132 template <typename VectorType>
2133 inline typename BlockVectorBase<VectorType>::value_type
2134 BlockVectorBase<VectorType>::operator[](const size_type i) const
2135 {
2136 return operator()(i);
2137 }
2138
2139
2140
2141 template <typename VectorType>
2142 inline typename BlockVectorBase<VectorType>::reference
2143 BlockVectorBase<VectorType>::operator[](const size_type i)
2144 {
2145 return operator()(i);
2146 }
2147
2148
2149
2150 template <typename VectorType>
2151 template <typename OtherNumber>
2152 inline void
2153 BlockVectorBase<VectorType>::extract_subvector_to(
2154 const std::vector<size_type> &indices,
2155 std::vector<OtherNumber> &values) const
2156 {
2157 for (size_type i = 0; i < indices.size(); ++i)
2158 values[i] = operator()(indices[i]);
2159 }
2160
2161
2162
2163 template <typename VectorType>
2164 inline void
2165 BlockVectorBase<VectorType>::extract_subvector_to(
2166 const ArrayView<const types::global_dof_index> &indices,
2167 const ArrayView<value_type> &entries) const
2168 {
2169 AssertDimension(indices.size(), entries.size());
2170 for (unsigned int i = 0; i < indices.size(); ++i)
2171 {
2172 AssertIndexRange(indices[i], size());
2173 entries[i] = (*this)[indices[i]];
2174 }
2175 }
2176
2177
2178
2179 template <typename VectorType>
2180 template <typename ForwardIterator, typename OutputIterator>
2181 inline void
2182 BlockVectorBase<VectorType>::extract_subvector_to(
2183 ForwardIterator indices_begin,
2184 const ForwardIterator indices_end,
2185 OutputIterator values_begin) const
2186 {
2187 while (indices_begin != indices_end)
2188 {
2189 *values_begin = operator()(*indices_begin);
2190 ++indices_begin;
2191 ++values_begin;
2192 }
2193 }
2194
2195 #endif // DOXYGEN
2196
2197 DEAL_II_NAMESPACE_CLOSE
2198
2199 #endif