13#ifndef dealii_block_vector_base_h
14#define dealii_block_vector_base_h
46 using has_block_t =
decltype(std::declval<const T>().block(0));
49 constexpr bool has_block = internal::is_supported_operation<has_block_t, T>;
56 internal::is_supported_operation<has_n_blocks_t, T>;
76template <
typename VectorType>
85 static const bool value = internal::is_block_vector<VectorType>;
90template <
typename VectorType>
100 namespace BlockVectorIterators
117 template <
typename BlockVectorType,
bool Constness>
132 std::conditional_t<Constness,
133 const typename BlockVectorType::value_type,
134 typename BlockVectorType::value_type>;
155 std::conditional_t<Constness,
157 typename BlockVectorType::BlockType::reference>;
164 std::conditional_t<Constness, const BlockVectorType, BlockVectorType>;
259 template <
bool OtherConstness>
267 template <
bool OtherConstness>
276 template <
bool OtherConstness>
283 template <
bool OtherConstness>
290 template <
bool OtherConstness>
297 template <
bool OtherConstness>
304 template <
bool OtherConstness>
346 "Your program tried to compare iterators pointing to "
347 "different block vectors. There is no reasonable way "
393 template <
typename,
bool>
437template <
typename VectorType>
646 template <typename OtherNumber>
649 std::vector<OtherNumber> &values) const;
682 template <typename ForwardIterator, typename OutputIterator>
685 const ForwardIterator indices_end,
686 OutputIterator values_begin) const;
712 template <typename VectorType2>
720 operator=(const VectorType &v);
726 template <typename VectorType2>
835 template <typename Number>
843 template <typename Number>
852 template <typename Number>
856 const Number *values);
930 template <class BlockVector2>
937 template <class BlockVector2>
976 template <typename N,
bool C>
992 namespace BlockVectorIterators
994 template <
typename BlockVectorType,
bool Constness>
995 inline Iterator<BlockVectorType, Constness>::Iterator(
996 const Iterator<BlockVectorType, Constness> &c)
998 , global_index(c.global_index)
999 , current_block(c.current_block)
1000 , index_within_block(c.index_within_block)
1001 , next_break_forward(c.next_break_forward)
1002 , next_break_backward(c.next_break_backward)
1007 template <
typename BlockVectorType,
bool Constness>
1008 inline Iterator<BlockVectorType, Constness>::Iterator(
1009 const Iterator<BlockVectorType, !Constness> &c)
1011 , global_index(c.global_index)
1012 , current_block(c.current_block)
1013 , index_within_block(c.index_within_block)
1014 , next_break_forward(c.next_break_forward)
1015 , next_break_backward(c.next_break_backward)
1020 static_assert(Constness ==
true,
1021 "Constructing a non-const iterator from a const iterator "
1022 "does not make sense.");
1027 template <
typename BlockVectorType,
bool Constness>
1028 inline Iterator<BlockVectorType, Constness>::Iterator(
1036 , global_index(global_index)
1037 , current_block(current_block)
1038 , index_within_block(index_within_block)
1039 , next_break_forward(next_break_forward)
1040 , next_break_backward(next_break_backward)
1045 template <
typename BlockVectorType,
bool Constness>
1046 inline Iterator<BlockVectorType, Constness> &
1047 Iterator<BlockVectorType, Constness>::operator=(
const Iterator &c) =
1052 template <
typename BlockVectorType,
bool Constness>
1053 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1054 Iterator<BlockVectorType, Constness>::operator*()
const
1056 return parent->
block(current_block)(index_within_block);
1061 template <
typename BlockVectorType,
bool Constness>
1062 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1063 Iterator<BlockVectorType, Constness>::operator[](
1071 if ((global_index + d >= next_break_backward) &&
1072 (global_index + d <= next_break_forward))
1073 return parent->
block(current_block)(index_within_block + d);
1082 return (*parent)(global_index + d);
1087 template <
typename BlockVectorType,
bool Constness>
1088 inline Iterator<BlockVectorType, Constness> &
1089 Iterator<BlockVectorType, Constness>::operator++()
1097 template <
typename BlockVectorType,
bool Constness>
1098 inline Iterator<BlockVectorType, Constness>
1099 Iterator<BlockVectorType, Constness>::operator++(
int)
1101 const Iterator old_value = *
this;
1108 template <
typename BlockVectorType,
bool Constness>
1109 inline Iterator<BlockVectorType, Constness> &
1110 Iterator<BlockVectorType, Constness>::operator--()
1118 template <
typename BlockVectorType,
bool Constness>
1119 inline Iterator<BlockVectorType, Constness>
1120 Iterator<BlockVectorType, Constness>::operator--(
int)
1122 const Iterator old_value = *
this;
1129 template <
typename BlockVectorType,
bool Constness>
1130 template <
bool OtherConstness>
1132 Iterator<BlockVectorType, Constness>::operator==(
1133 const Iterator<BlockVectorType, OtherConstness> &i)
const
1135 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1137 return (global_index == i.global_index);
1142 template <
typename BlockVectorType,
bool Constness>
1143 template <
bool OtherConstness>
1145 Iterator<BlockVectorType, Constness>::operator!=(
1146 const Iterator<BlockVectorType, OtherConstness> &i)
const
1148 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1150 return (global_index != i.global_index);
1155 template <
typename BlockVectorType,
bool Constness>
1156 template <
bool OtherConstness>
1158 Iterator<BlockVectorType, Constness>::operator<(
1159 const Iterator<BlockVectorType, OtherConstness> &i)
const
1161 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1163 return (global_index < i.global_index);
1168 template <
typename BlockVectorType,
bool Constness>
1169 template <
bool OtherConstness>
1171 Iterator<BlockVectorType, Constness>::operator<=(
1172 const Iterator<BlockVectorType, OtherConstness> &i)
const
1174 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1176 return (global_index <= i.global_index);
1181 template <
typename BlockVectorType,
bool Constness>
1182 template <
bool OtherConstness>
1184 Iterator<BlockVectorType, Constness>::operator>(
1185 const Iterator<BlockVectorType, OtherConstness> &i)
const
1187 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1189 return (global_index > i.global_index);
1194 template <
typename BlockVectorType,
bool Constness>
1195 template <
bool OtherConstness>
1197 Iterator<BlockVectorType, Constness>::operator>=(
1198 const Iterator<BlockVectorType, OtherConstness> &i)
const
1200 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1202 return (global_index >= i.global_index);
1207 template <
typename BlockVectorType,
bool Constness>
1208 template <
bool OtherConstness>
1209 inline typename Iterator<BlockVectorType, Constness>::difference_type
1210 Iterator<BlockVectorType, Constness>::operator-(
1211 const Iterator<BlockVectorType, OtherConstness> &i)
const
1213 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1215 return (
static_cast<signed int>(global_index) -
1216 static_cast<signed int>(i.global_index));
1221 template <
typename BlockVectorType,
bool Constness>
1222 inline Iterator<BlockVectorType, Constness>
1223 Iterator<BlockVectorType, Constness>::operator+(
1231 if ((global_index + d >= next_break_backward) &&
1232 (global_index + d <= next_break_forward))
1233 return Iterator(*parent,
1236 index_within_block + d,
1238 next_break_backward);
1243 return Iterator(*parent, global_index + d);
1248 template <
typename BlockVectorType,
bool Constness>
1249 inline Iterator<BlockVectorType, Constness>
1250 Iterator<BlockVectorType, Constness>::operator-(
1258 if ((global_index - d >= next_break_backward) &&
1259 (global_index - d <= next_break_forward))
1260 return Iterator(*parent,
1263 index_within_block - d,
1265 next_break_backward);
1270 return Iterator(*parent, global_index - d);
1275 template <
typename BlockVectorType,
bool Constness>
1276 inline Iterator<BlockVectorType, Constness> &
1277 Iterator<BlockVectorType, Constness>::operator+=(
const difference_type &d)
1284 if ((global_index + d >= next_break_backward) &&
1285 (global_index + d <= next_break_forward))
1288 index_within_block += d;
1294 *
this = Iterator(*parent, global_index + d);
1301 template <
typename BlockVectorType,
bool Constness>
1302 inline Iterator<BlockVectorType, Constness> &
1303 Iterator<BlockVectorType, Constness>::operator-=(
const difference_type &d)
1310 if ((global_index - d >= next_break_backward) &&
1311 (global_index - d <= next_break_forward))
1314 index_within_block -= d;
1320 *
this = Iterator(*parent, global_index - d);
1326 template <
typename BlockVectorType,
bool Constness>
1327 Iterator<BlockVectorType, Constness>::Iterator(
BlockVector &parent,
1330 , global_index(global_index)
1338 if (global_index < parent.
size())
1340 const std::pair<size_type, size_type> indices =
1342 current_block = indices.first;
1343 index_within_block = indices.second;
1345 next_break_backward =
1347 next_break_forward =
1355 this->global_index = parent.
size();
1357 index_within_block = 0;
1358 next_break_backward = global_index;
1365 template <
typename BlockVectorType,
bool Constness>
1367 Iterator<BlockVectorType, Constness>::move_forward()
1369 if (global_index != next_break_forward)
1370 ++index_within_block;
1375 index_within_block = 0;
1380 next_break_backward = next_break_forward + 1;
1383 if (current_block < parent->block_indices.
size())
1384 next_break_forward +=
1399 template <
typename BlockVectorType,
bool Constness>
1401 Iterator<BlockVectorType, Constness>::move_backward()
1403 if (global_index != next_break_backward)
1404 --index_within_block;
1405 else if (current_block != 0)
1410 index_within_block =
1415 next_break_forward = next_break_backward - 1;
1418 next_break_backward -=
1427 next_break_forward = 0;
1428 next_break_backward = 0;
1441template <
typename VectorType>
1450template <
typename VectorType>
1454 std::size_t local_size = 0;
1455 for (
unsigned int b = 0;
b <
n_blocks(); ++
b)
1456 local_size +=
block(b).locally_owned_size();
1462template <
typename VectorType>
1470 for (
unsigned int b = 0;
b <
n_blocks(); ++
b)
1483template <
typename VectorType>
1491template <
typename VectorType>
1502template <
typename VectorType>
1513template <
typename VectorType>
1521template <
typename VectorType>
1525 std::vector<size_type> sizes(
n_blocks());
1528 sizes[i] =
block(i).size();
1535template <
typename VectorType>
1539 for (
unsigned int i = 0; i <
n_blocks(); ++i)
1540 block(i).compress(operation);
1545template <
typename VectorType>
1554template <
typename VectorType>
1562template <
typename VectorType>
1571template <
typename VectorType>
1579template <
typename VectorType>
1583 const std::pair<size_type, size_type> local_index =
1586 return components[local_index.first].in_local_range(global_index);
1590template <
typename VectorType>
1595 return v.all_zero();
1601template <
typename VectorType>
1614template <
typename VectorType>
1630template <
typename VectorType>
1643template <
typename VectorType>
1660template <
typename VectorType>
1673template <
typename VectorType>
1682template <
typename VectorType>
1698template <
typename VectorType>
1717template <
typename VectorType>
1734template <
typename VectorType>
1750template <
typename VectorType>
1751template <
typename Number>
1754 const std::vector<Number> &values)
1758 add(indices.size(), indices.data(),
values.data());
1763template <
typename VectorType>
1764template <
typename Number>
1771 const size_type n_indices = indices.size();
1772 for (
size_type i = 0; i < n_indices; ++i)
1773 (*
this)(indices[i]) +=
values(i);
1778template <
typename VectorType>
1779template <
typename Number>
1783 const Number *values)
1785 for (
size_type i = 0; i < n_indices; ++i)
1786 (*
this)(indices[i]) += values[i];
1791template <
typename VectorType>
1805template <
typename VectorType>
1823template <
typename VectorType>
1847template <
typename VectorType>
1865template <
typename VectorType>
1885template <
typename VectorType>
1910template <
typename VectorType>
1941template <
typename VectorType>
1942template <
class BlockVector2>
1954template <
typename VectorType>
1964template <
typename VectorType>
1965template <
class BlockVector2>
1980template <
typename VectorType>
1985 block(i).update_ghost_values();
1990template <
typename VectorType>
1995 return block(0).get_mpi_communicator();
1997 return MPI_COMM_SELF;
2002template <
typename VectorType>
2015template <
typename VectorType>
2028template <
typename VectorType>
2029template <
typename VectorType2>
2043template <
typename VectorType>
2052 block(b)(i) = v(index_v);
2059template <
typename VectorType>
2060template <
typename VectorType2>
2076template <
typename VectorType>
2090template <
typename VectorType>
2106template <
typename VectorType>
2110 const std::pair<unsigned int, size_type> local_index =
2112 return components[local_index.first](local_index.second);
2117template <
typename VectorType>
2121 const std::pair<unsigned int, size_type> local_index =
2123 return components[local_index.first](local_index.second);
2128template <
typename VectorType>
2137template <
typename VectorType>
2146template <
typename VectorType>
2147template <
typename OtherNumber>
2150 const std::vector<size_type> &indices,
2151 std::vector<OtherNumber> &values)
const
2153 for (
size_type i = 0; i < indices.size(); ++i)
2154 values[i] =
operator()(indices[i]);
2159template <
typename VectorType>
2166 for (
unsigned int i = 0; i < indices.
size(); ++i)
2169 entries[i] = (*this)[indices[i]];
2175template <
typename VectorType>
2176template <
typename ForwardIterator,
typename OutputIterator>
2179 ForwardIterator indices_begin,
2180 const ForwardIterator indices_end,
2181 OutputIterator values_begin)
const
2183 while (indices_begin != indices_end)
std::ptrdiff_t difference_type
size_type block_size(const unsigned int i) const
size_type total_size() const
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
unsigned int size() const
size_type local_to_global(const unsigned int block, const size_type index) const
size_type block_start(const unsigned int i) const
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
BlockVectorBase & operator+=(const BlockVectorBase &V)
BlockVectorBase()=default
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
value_type mean_value() const
virtual size_type size() const override
void add(const std::vector< size_type > &indices, const std::vector< Number > &values)
::internal::BlockVectorIterators::Iterator< BlockVectorBase, false > iterator
BlockVectorBase & operator/=(const value_type factor)
unsigned int n_blocks() const
real_type norm_sqr() const
const value_type * const_pointer
typename BlockType::const_reference const_reference
void update_ghost_values() const
std::size_t memory_consumption() const
real_type l1_norm() const
types::global_dof_index size_type
real_type linfty_norm() const
value_type add_and_dot(const value_type a, const BlockVectorBase &V, const BlockVectorBase &W)
typename BlockType::value_type value_type
value_type operator*(const BlockVectorBase &V) const
void compress(VectorOperation::values operation)
BlockVectorBase & operator=(const value_type s)
BlockVectorBase & operator*=(const value_type factor)
void sadd(const value_type s, const BlockVectorBase &V)
std::vector< VectorType > components
bool in_local_range(const size_type global_index) const
::internal::BlockVectorIterators::Iterator< BlockVectorBase, true > const_iterator
MPI_Comm get_mpi_communicator() const
BlockVectorBase(BlockVectorBase &&) noexcept=default
BlockVectorBase(const BlockVectorBase &)=default
typename BlockType::reference reference
value_type operator[](const size_type i) const
BlockIndices block_indices
typename BlockType::real_type real_type
void scale(const BlockVector2 &v)
BlockType & block(const unsigned int i)
real_type l2_norm() const
value_type operator()(const size_type i) const
IndexSet locally_owned_elements() const
bool is_non_negative() const
BlockVectorBase & operator-=(const BlockVectorBase &V)
friend class ::internal::BlockVectorIterators::Iterator
bool operator==(const BlockVectorBase< VectorType2 > &v) const
std::size_t locally_owned_size() const
const BlockIndices & get_block_indices() const
void equ(const value_type a, const BlockVector2 &V)
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
bool operator==(const Iterator< BlockVectorType, OtherConstness > &i) const
dereference_type operator[](const difference_type d) const
Iterator & operator=(const Iterator &c)
bool operator<(const Iterator< BlockVectorType, OtherConstness > &i) const
Iterator(BlockVector &parent, const size_type global_index, const size_type current_block, const size_type index_within_block, const size_type next_break_forward, const size_type next_break_backward)
std::conditional_t< Constness, const typename BlockVectorType::value_type, typename BlockVectorType::value_type > value_type
Iterator(const Iterator< BlockVectorType, !Constness > &c)
Iterator(const Iterator &c)
std::conditional_t< Constness, const BlockVectorType, BlockVectorType > BlockVector
dereference_type operator*() const
Iterator operator-(const difference_type &d) const
difference_type operator-(const Iterator< BlockVectorType, OtherConstness > &i) const
std::random_access_iterator_tag iterator_category
Iterator(BlockVector &parent, const size_type global_index)
size_type next_break_forward
bool operator>=(const Iterator< BlockVectorType, OtherConstness > &i) const
unsigned int current_block
Iterator & operator-=(const difference_type &d)
std::conditional_t< Constness, value_type, typename BlockVectorType::BlockType::reference > dereference_type
size_type next_break_backward
std::ptrdiff_t difference_type
size_type index_within_block
bool operator<=(const Iterator< BlockVectorType, OtherConstness > &i) const
typename BlockVectorType::reference reference
Iterator operator+(const difference_type &d) const
Iterator & operator+=(const difference_type &d)
bool operator!=(const Iterator< BlockVectorType, OtherConstness > &i) const
bool operator>(const Iterator< BlockVectorType, OtherConstness > &i) const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
static ::ExceptionBase & ExcPointerToDifferentVectors()
#define AssertIsFinite(number)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcDifferentBlockIndices()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Tensor< 2, dim, Number > w(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
T sum(const T &t, const MPI_Comm mpi_communicator)
constexpr bool has_n_blocks
decltype(std::declval< const T >().n_blocks()) has_n_blocks_t
decltype(std::declval< const T >().block(0)) has_block_t
constexpr bool is_block_vector
constexpr types::global_dof_index invalid_size_type
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index