15#ifndef dealii_block_vector_base_h
16#define dealii_block_vector_base_h
48 using has_block_t =
decltype(std::declval<const T>().block(0));
51 constexpr bool has_block = internal::is_supported_operation<has_block_t, T>;
58 internal::is_supported_operation<has_n_blocks_t, T>;
78template <
typename VectorType>
87 static const bool value = internal::is_block_vector<VectorType>;
92template <
typename VectorType>
102 namespace BlockVectorIterators
119 template <
typename BlockVectorType,
bool Constness>
134 std::conditional_t<Constness,
135 const typename BlockVectorType::value_type,
136 typename BlockVectorType::value_type>;
157 std::conditional_t<Constness,
159 typename BlockVectorType::BlockType::reference>;
166 std::conditional_t<Constness, const BlockVectorType, BlockVectorType>;
261 template <
bool OtherConstness>
269 template <
bool OtherConstness>
278 template <
bool OtherConstness>
285 template <
bool OtherConstness>
292 template <
bool OtherConstness>
299 template <
bool OtherConstness>
306 template <
bool OtherConstness>
348 "Your program tried to compare iterators pointing to "
349 "different block vectors. There is no reasonable way "
395 template <
typename,
bool>
102 namespace BlockVectorIterators {
…}
439template <
typename VectorType>
648 template <typename OtherNumber>
651 std::vector<OtherNumber> &values) const;
684 template <typename ForwardIterator, typename OutputIterator>
687 const ForwardIterator indices_end,
688 OutputIterator values_begin) const;
714 template <typename VectorType2>
722 operator=(const VectorType &v);
728 template <typename VectorType2>
837 template <typename Number>
845 template <typename Number>
854 template <typename Number>
858 const Number *values);
932 template <class BlockVector2>
939 template <class BlockVector2>
978 template <typename N,
bool C>
994 namespace BlockVectorIterators
996 template <
typename BlockVectorType,
bool Constness>
997 inline Iterator<BlockVectorType, Constness>::Iterator(
998 const Iterator<BlockVectorType, Constness> &c)
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)
1009 template <
typename BlockVectorType,
bool Constness>
1010 inline Iterator<BlockVectorType, Constness>::Iterator(
1011 const Iterator<BlockVectorType, !Constness> &c)
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)
1022 static_assert(Constness ==
true,
1023 "Constructing a non-const iterator from a const iterator "
1024 "does not make sense.");
1029 template <
typename BlockVectorType,
bool Constness>
1030 inline Iterator<BlockVectorType, Constness>::Iterator(
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)
1047 template <
typename BlockVectorType,
bool Constness>
1048 inline Iterator<BlockVectorType, Constness> &
1049 Iterator<BlockVectorType, Constness>::operator=(
const Iterator &c) =
1054 template <
typename BlockVectorType,
bool Constness>
1055 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1056 Iterator<BlockVectorType, Constness>::operator*()
const
1058 return parent->
block(current_block)(index_within_block);
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
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);
1084 return (*parent)(global_index + d);
1089 template <
typename BlockVectorType,
bool Constness>
1090 inline Iterator<BlockVectorType, Constness> &
1091 Iterator<BlockVectorType, Constness>::operator++()
1099 template <
typename BlockVectorType,
bool Constness>
1100 inline Iterator<BlockVectorType, Constness>
1101 Iterator<BlockVectorType, Constness>::operator++(
int)
1103 const Iterator old_value = *
this;
1110 template <
typename BlockVectorType,
bool Constness>
1111 inline Iterator<BlockVectorType, Constness> &
1112 Iterator<BlockVectorType, Constness>::operator--()
1120 template <
typename BlockVectorType,
bool Constness>
1121 inline Iterator<BlockVectorType, Constness>
1122 Iterator<BlockVectorType, Constness>::operator--(
int)
1124 const Iterator old_value = *
this;
1131 template <
typename BlockVectorType,
bool Constness>
1132 template <
bool OtherConstness>
1134 Iterator<BlockVectorType, Constness>::operator==(
1135 const Iterator<BlockVectorType, OtherConstness> &i)
const
1137 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1139 return (global_index == i.global_index);
1144 template <
typename BlockVectorType,
bool Constness>
1145 template <
bool OtherConstness>
1147 Iterator<BlockVectorType, Constness>::operator!=(
1148 const Iterator<BlockVectorType, OtherConstness> &i)
const
1150 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1152 return (global_index != i.global_index);
1157 template <
typename BlockVectorType,
bool Constness>
1158 template <
bool OtherConstness>
1160 Iterator<BlockVectorType, Constness>::operator<(
1161 const Iterator<BlockVectorType, OtherConstness> &i)
const
1163 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1165 return (global_index < i.global_index);
1170 template <
typename BlockVectorType,
bool Constness>
1171 template <
bool OtherConstness>
1173 Iterator<BlockVectorType, Constness>::operator<=(
1174 const Iterator<BlockVectorType, OtherConstness> &i)
const
1176 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1178 return (global_index <= i.global_index);
1183 template <
typename BlockVectorType,
bool Constness>
1184 template <
bool OtherConstness>
1186 Iterator<BlockVectorType, Constness>::operator>(
1187 const Iterator<BlockVectorType, OtherConstness> &i)
const
1189 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1191 return (global_index > i.global_index);
1196 template <
typename BlockVectorType,
bool Constness>
1197 template <
bool OtherConstness>
1199 Iterator<BlockVectorType, Constness>::operator>=(
1200 const Iterator<BlockVectorType, OtherConstness> &i)
const
1202 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1204 return (global_index >= i.global_index);
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
1215 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1217 return (
static_cast<signed int>(global_index) -
1218 static_cast<signed int>(i.global_index));
1223 template <
typename BlockVectorType,
bool Constness>
1224 inline Iterator<BlockVectorType, Constness>
1225 Iterator<BlockVectorType, Constness>::operator+(
1226 const difference_type &d)
const
1233 if ((global_index + d >= next_break_backward) &&
1234 (global_index + d <= next_break_forward))
1235 return Iterator(*parent,
1238 index_within_block + d,
1240 next_break_backward);
1245 return Iterator(*parent, global_index + d);
1250 template <
typename BlockVectorType,
bool Constness>
1251 inline Iterator<BlockVectorType, Constness>
1252 Iterator<BlockVectorType, Constness>::operator-(
1253 const difference_type &d)
const
1260 if ((global_index - d >= next_break_backward) &&
1261 (global_index - d <= next_break_forward))
1262 return Iterator(*parent,
1265 index_within_block - d,
1267 next_break_backward);
1272 return Iterator(*parent, global_index - d);
1277 template <
typename BlockVectorType,
bool Constness>
1278 inline Iterator<BlockVectorType, Constness> &
1279 Iterator<BlockVectorType, Constness>::operator+=(
const difference_type &d)
1286 if ((global_index + d >= next_break_backward) &&
1287 (global_index + d <= next_break_forward))
1290 index_within_block += d;
1296 *
this = Iterator(*parent, global_index + d);
1303 template <
typename BlockVectorType,
bool Constness>
1304 inline Iterator<BlockVectorType, Constness> &
1305 Iterator<BlockVectorType, Constness>::operator-=(
const difference_type &d)
1312 if ((global_index - d >= next_break_backward) &&
1313 (global_index - d <= next_break_forward))
1316 index_within_block -= d;
1322 *
this = Iterator(*parent, global_index - d);
1328 template <
typename BlockVectorType,
bool Constness>
1329 Iterator<BlockVectorType, Constness>::Iterator(
BlockVector &parent,
1332 , global_index(global_index)
1340 if (global_index < parent.
size())
1342 const std::pair<size_type, size_type> indices =
1344 current_block = indices.first;
1345 index_within_block = indices.second;
1347 next_break_backward =
1349 next_break_forward =
1357 this->global_index = parent.
size();
1359 index_within_block = 0;
1360 next_break_backward = global_index;
1367 template <
typename BlockVectorType,
bool Constness>
1369 Iterator<BlockVectorType, Constness>::move_forward()
1371 if (global_index != next_break_forward)
1372 ++index_within_block;
1377 index_within_block = 0;
1382 next_break_backward = next_break_forward + 1;
1385 if (current_block < parent->block_indices.
size())
1386 next_break_forward +=
1401 template <
typename BlockVectorType,
bool Constness>
1403 Iterator<BlockVectorType, Constness>::move_backward()
1405 if (global_index != next_break_backward)
1406 --index_within_block;
1407 else if (current_block != 0)
1412 index_within_block =
1417 next_break_forward = next_break_backward - 1;
1420 next_break_backward -=
1429 next_break_forward = 0;
1430 next_break_backward = 0;
1443template <
typename VectorType>
1452template <
typename VectorType>
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();
1464template <
typename VectorType>
1472 for (
unsigned int b = 0;
b <
n_blocks(); ++
b)
1485template <
typename VectorType>
1493template <
typename VectorType>
1504template <
typename VectorType>
1515template <
typename VectorType>
1523template <
typename VectorType>
1527 std::vector<size_type> sizes(
n_blocks());
1530 sizes[i] =
block(i).size();
1537template <
typename VectorType>
1541 for (
unsigned int i = 0; i <
n_blocks(); ++i)
1542 block(i).compress(operation);
1547template <
typename VectorType>
1556template <
typename VectorType>
1564template <
typename VectorType>
1573template <
typename VectorType>
1581template <
typename VectorType>
1585 const std::pair<size_type, size_type> local_index =
1588 return components[local_index.first].in_local_range(global_index);
1592template <
typename VectorType>
1605template <
typename VectorType>
1618template <
typename VectorType>
1634template <
typename VectorType>
1647template <
typename VectorType>
1664template <
typename VectorType>
1677template <
typename VectorType>
1686template <
typename VectorType>
1702template <
typename VectorType>
1721template <
typename VectorType>
1738template <
typename VectorType>
1754template <
typename VectorType>
1755template <
typename Number>
1758 const std::vector<Number> &values)
1762 add(indices.size(), indices.data(),
values.data());
1767template <
typename VectorType>
1768template <
typename Number>
1775 const size_type n_indices = indices.size();
1776 for (
size_type i = 0; i < n_indices; ++i)
1777 (*
this)(indices[i]) +=
values(i);
1782template <
typename VectorType>
1783template <
typename Number>
1787 const Number *values)
1789 for (
size_type i = 0; i < n_indices; ++i)
1790 (*
this)(indices[i]) += values[i];
1795template <
typename VectorType>
1809template <
typename VectorType>
1827template <
typename VectorType>
1851template <
typename VectorType>
1869template <
typename VectorType>
1889template <
typename VectorType>
1914template <
typename VectorType>
1945template <
typename VectorType>
1946template <
class BlockVector2>
1958template <
typename VectorType>
1968template <
typename VectorType>
1969template <
class BlockVector2>
1984template <
typename VectorType>
1989 block(i).update_ghost_values();
1994template <
typename VectorType>
1999 return block(0).get_mpi_communicator();
2001 return MPI_COMM_SELF;
2006template <
typename VectorType>
2019template <
typename VectorType>
2032template <
typename VectorType>
2033template <
typename VectorType2>
2047template <
typename VectorType>
2056 block(b)(i) = v(index_v);
2063template <
typename VectorType>
2064template <
typename VectorType2>
2080template <
typename VectorType>
2094template <
typename VectorType>
2110template <
typename VectorType>
2114 const std::pair<unsigned int, size_type> local_index =
2116 return components[local_index.first](local_index.second);
2121template <
typename VectorType>
2125 const std::pair<unsigned int, size_type> local_index =
2127 return components[local_index.first](local_index.second);
2132template <
typename VectorType>
2141template <
typename VectorType>
2150template <
typename VectorType>
2151template <
typename OtherNumber>
2154 const std::vector<size_type> &indices,
2155 std::vector<OtherNumber> &values)
const
2157 for (
size_type i = 0; i < indices.size(); ++i)
2158 values[i] =
operator()(indices[i]);
2163template <
typename VectorType>
2170 for (
unsigned int i = 0; i < indices.
size(); ++i)
2173 entries[i] = (*this)[indices[i]];
2179template <
typename VectorType>
2180template <
typename ForwardIterator,
typename OutputIterator>
2183 ForwardIterator indices_begin,
2184 const ForwardIterator indices_end,
2185 OutputIterator values_begin)
const
2187 while (indices_begin != indices_end)
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