15#ifndef dealii_block_vector_base_h
16#define dealii_block_vector_base_h
49 using has_block_t =
decltype(std::declval<const T>().block(0));
52 constexpr bool has_block = internal::is_supported_operation<has_block_t, T>;
59 internal::is_supported_operation<has_n_blocks_t, T>;
79template <
typename VectorType>
88 static const bool value = internal::is_block_vector<VectorType>;
93template <
typename VectorType>
103 namespace BlockVectorIterators
120 template <
typename BlockVectorType,
bool Constness>
135 std::conditional_t<Constness,
136 const typename BlockVectorType::value_type,
137 typename BlockVectorType::value_type>;
158 std::conditional_t<Constness,
160 typename BlockVectorType::BlockType::reference>;
167 std::conditional_t<Constness, const BlockVectorType, BlockVectorType>;
262 template <
bool OtherConstness>
270 template <
bool OtherConstness>
279 template <
bool OtherConstness>
286 template <
bool OtherConstness>
293 template <
bool OtherConstness>
300 template <
bool OtherConstness>
307 template <
bool OtherConstness>
349 "Your program tried to compare iterators pointing to "
350 "different block vectors. There is no reasonable way "
396 template <
typename,
bool>
440template <
typename VectorType>
442 public ReadVector<typename VectorType::value_type>
650 template <typename OtherNumber>
653 std::vector<OtherNumber> &values) const;
686 template <typename ForwardIterator, typename OutputIterator>
689 const ForwardIterator indices_end,
690 OutputIterator values_begin) const;
716 template <typename VectorType2>
724 operator=(const VectorType &v);
730 template <typename VectorType2>
839 template <typename Number>
847 template <typename Number>
856 template <typename Number>
860 const Number *values);
934 template <class BlockVector2>
941 template <class BlockVector2>
980 template <typename N,
bool C>
996 namespace BlockVectorIterators
998 template <
typename BlockVectorType,
bool Constness>
999 inline Iterator<BlockVectorType, Constness>::Iterator(
1000 const Iterator<BlockVectorType, Constness> &c)
1002 , global_index(c.global_index)
1003 , current_block(c.current_block)
1004 , index_within_block(c.index_within_block)
1005 , next_break_forward(c.next_break_forward)
1006 , next_break_backward(c.next_break_backward)
1011 template <
typename BlockVectorType,
bool Constness>
1012 inline Iterator<BlockVectorType, Constness>::Iterator(
1013 const Iterator<BlockVectorType, !Constness> &c)
1015 , global_index(c.global_index)
1016 , current_block(c.current_block)
1017 , index_within_block(c.index_within_block)
1018 , next_break_forward(c.next_break_forward)
1019 , next_break_backward(c.next_break_backward)
1024 static_assert(Constness ==
true,
1025 "Constructing a non-const iterator from a const iterator "
1026 "does not make sense.");
1031 template <
typename BlockVectorType,
bool Constness>
1032 inline Iterator<BlockVectorType, Constness>::Iterator(
1040 , global_index(global_index)
1041 , current_block(current_block)
1042 , index_within_block(index_within_block)
1043 , next_break_forward(next_break_forward)
1044 , next_break_backward(next_break_backward)
1049 template <
typename BlockVectorType,
bool Constness>
1050 inline Iterator<BlockVectorType, Constness> &
1051 Iterator<BlockVectorType, Constness>::operator=(
const Iterator &c)
1054 global_index = c.global_index;
1055 index_within_block = c.index_within_block;
1056 current_block = c.current_block;
1057 next_break_forward = c.next_break_forward;
1058 next_break_backward = c.next_break_backward;
1065 template <
typename BlockVectorType,
bool Constness>
1066 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1067 Iterator<BlockVectorType, Constness>::operator*()
const
1069 return parent->
block(current_block)(index_within_block);
1074 template <
typename BlockVectorType,
bool Constness>
1075 inline typename Iterator<BlockVectorType, Constness>::dereference_type
1076 Iterator<BlockVectorType, Constness>::operator[](
1077 const difference_type d)
const
1084 if ((global_index + d >= next_break_backward) &&
1085 (global_index + d <= next_break_forward))
1086 return parent->
block(current_block)(index_within_block + d);
1095 return (*parent)(global_index + d);
1100 template <
typename BlockVectorType,
bool Constness>
1101 inline Iterator<BlockVectorType, Constness> &
1102 Iterator<BlockVectorType, Constness>::operator++()
1110 template <
typename BlockVectorType,
bool Constness>
1111 inline Iterator<BlockVectorType, Constness>
1112 Iterator<BlockVectorType, Constness>::operator++(
int)
1114 const Iterator old_value = *
this;
1121 template <
typename BlockVectorType,
bool Constness>
1122 inline Iterator<BlockVectorType, Constness> &
1123 Iterator<BlockVectorType, Constness>::operator--()
1131 template <
typename BlockVectorType,
bool Constness>
1132 inline Iterator<BlockVectorType, Constness>
1133 Iterator<BlockVectorType, Constness>::operator--(
int)
1135 const Iterator old_value = *
this;
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>
1210 Iterator<BlockVectorType, Constness>::operator>=(
1211 const Iterator<BlockVectorType, OtherConstness> &i)
const
1213 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1215 return (global_index >= i.global_index);
1220 template <
typename BlockVectorType,
bool Constness>
1221 template <
bool OtherConstness>
1222 inline typename Iterator<BlockVectorType, Constness>::difference_type
1223 Iterator<BlockVectorType, Constness>::operator-(
1224 const Iterator<BlockVectorType, OtherConstness> &i)
const
1226 Assert(parent == i.parent, ExcPointerToDifferentVectors());
1228 return (
static_cast<signed int>(global_index) -
1229 static_cast<signed int>(i.global_index));
1234 template <
typename BlockVectorType,
bool Constness>
1235 inline Iterator<BlockVectorType, Constness>
1236 Iterator<BlockVectorType, Constness>::operator+(
1237 const difference_type &d)
const
1244 if ((global_index + d >= next_break_backward) &&
1245 (global_index + d <= next_break_forward))
1246 return Iterator(*parent,
1249 index_within_block + d,
1251 next_break_backward);
1256 return Iterator(*parent, global_index + d);
1261 template <
typename BlockVectorType,
bool Constness>
1262 inline Iterator<BlockVectorType, Constness>
1263 Iterator<BlockVectorType, Constness>::operator-(
1264 const difference_type &d)
const
1271 if ((global_index - d >= next_break_backward) &&
1272 (global_index - d <= next_break_forward))
1273 return Iterator(*parent,
1276 index_within_block - d,
1278 next_break_backward);
1283 return Iterator(*parent, global_index - d);
1288 template <
typename BlockVectorType,
bool Constness>
1289 inline Iterator<BlockVectorType, Constness> &
1290 Iterator<BlockVectorType, Constness>::operator+=(
const difference_type &d)
1297 if ((global_index + d >= next_break_backward) &&
1298 (global_index + d <= next_break_forward))
1301 index_within_block += d;
1307 *
this = Iterator(*parent, global_index + d);
1314 template <
typename BlockVectorType,
bool Constness>
1315 inline Iterator<BlockVectorType, Constness> &
1316 Iterator<BlockVectorType, Constness>::operator-=(
const difference_type &d)
1323 if ((global_index - d >= next_break_backward) &&
1324 (global_index - d <= next_break_forward))
1327 index_within_block -= d;
1333 *
this = Iterator(*parent, global_index - d);
1339 template <
typename BlockVectorType,
bool Constness>
1340 Iterator<BlockVectorType, Constness>::Iterator(
BlockVector &parent,
1343 , global_index(global_index)
1351 if (global_index < parent.
size())
1353 const std::pair<size_type, size_type> indices =
1355 current_block = indices.first;
1356 index_within_block = indices.second;
1358 next_break_backward =
1360 next_break_forward =
1368 this->global_index = parent.
size();
1370 index_within_block = 0;
1371 next_break_backward = global_index;
1378 template <
typename BlockVectorType,
bool Constness>
1380 Iterator<BlockVectorType, Constness>::move_forward()
1382 if (global_index != next_break_forward)
1383 ++index_within_block;
1388 index_within_block = 0;
1393 next_break_backward = next_break_forward + 1;
1396 if (current_block < parent->block_indices.
size())
1397 next_break_forward +=
1412 template <
typename BlockVectorType,
bool Constness>
1414 Iterator<BlockVectorType, Constness>::move_backward()
1416 if (global_index != next_break_backward)
1417 --index_within_block;
1418 else if (current_block != 0)
1423 index_within_block =
1428 next_break_forward = next_break_backward - 1;
1431 next_break_backward -=
1440 next_break_forward = 0;
1441 next_break_backward = 0;
1454template <
typename VectorType>
1463template <
typename VectorType>
1467 std::size_t local_size = 0;
1468 for (
unsigned int b = 0;
b <
n_blocks(); ++
b)
1469 local_size +=
block(b).locally_owned_size();
1475template <
typename VectorType>
1483 for (
unsigned int b = 0;
b <
n_blocks(); ++
b)
1496template <
typename VectorType>
1504template <
typename VectorType>
1515template <
typename VectorType>
1526template <
typename VectorType>
1534template <
typename VectorType>
1538 std::vector<size_type> sizes(
n_blocks());
1541 sizes[i] =
block(i).size();
1548template <
typename VectorType>
1552 for (
unsigned int i = 0; i <
n_blocks(); ++i)
1553 block(i).compress(operation);
1558template <
typename VectorType>
1567template <
typename VectorType>
1575template <
typename VectorType>
1584template <
typename VectorType>
1592template <
typename VectorType>
1596 const std::pair<size_type, size_type> local_index =
1599 return components[local_index.first].in_local_range(global_index);
1603template <
typename VectorType>
1616template <
typename VectorType>
1629template <
typename VectorType>
1645template <
typename VectorType>
1658template <
typename VectorType>
1675template <
typename VectorType>
1688template <
typename VectorType>
1697template <
typename VectorType>
1713template <
typename VectorType>
1732template <
typename VectorType>
1749template <
typename VectorType>
1765template <
typename VectorType>
1766template <
typename Number>
1769 const std::vector<Number> &values)
1773 add(indices.size(), indices.data(),
values.data());
1778template <
typename VectorType>
1779template <
typename Number>
1786 const size_type n_indices = indices.size();
1787 for (
size_type i = 0; i < n_indices; ++i)
1788 (*
this)(indices[i]) +=
values(i);
1793template <
typename VectorType>
1794template <
typename Number>
1798 const Number *values)
1800 for (
size_type i = 0; i < n_indices; ++i)
1801 (*
this)(indices[i]) += values[i];
1806template <
typename VectorType>
1820template <
typename VectorType>
1838template <
typename VectorType>
1862template <
typename VectorType>
1880template <
typename VectorType>
1900template <
typename VectorType>
1925template <
typename VectorType>
1956template <
typename VectorType>
1957template <
class BlockVector2>
1969template <
typename VectorType>
1979template <
typename VectorType>
1980template <
class BlockVector2>
1995template <
typename VectorType>
2000 block(i).update_ghost_values();
2005template <
typename VectorType>
2010 return block(0).get_mpi_communicator();
2012 return MPI_COMM_SELF;
2017template <
typename VectorType>
2030template <
typename VectorType>
2043template <
typename VectorType>
2044template <
typename VectorType2>
2058template <
typename VectorType>
2067 block(b)(i) = v(index_v);
2074template <
typename VectorType>
2075template <
typename VectorType2>
2091template <
typename VectorType>
2105template <
typename VectorType>
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>
2136 const std::pair<unsigned int, size_type> local_index =
2138 return components[local_index.first](local_index.second);
2143template <
typename VectorType>
2152template <
typename VectorType>
2161template <
typename VectorType>
2162template <
typename OtherNumber>
2165 const std::vector<size_type> &indices,
2166 std::vector<OtherNumber> &values)
const
2168 for (
size_type i = 0; i < indices.size(); ++i)
2169 values[i] =
operator()(indices[i]);
2174template <
typename VectorType>
2181 for (
unsigned int i = 0; i < indices.
size(); ++i)
2184 entries[i] = (*this)[indices[i]];
2190template <
typename VectorType>
2191template <
typename ForwardIterator,
typename OutputIterator>
2194 ForwardIterator indices_begin,
2195 const ForwardIterator indices_end,
2196 OutputIterator values_begin)
const
2198 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
const types::global_dof_index invalid_size_type
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index