16 #ifndef dealii_block_vector_base_h 17 #define dealii_block_vector_base_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/base/numbers.h> 22 #include <deal.II/base/exceptions.h> 23 #include <deal.II/base/subscriptor.h> 24 #include <deal.II/base/memory_consumption.h> 25 #include <deal.II/lac/exceptions.h> 26 #include <deal.II/lac/block_indices.h> 27 #include <deal.II/lac/vector.h> 28 #include <deal.II/lac/vector_operation.h> 35 DEAL_II_NAMESPACE_OPEN
61 template <
typename VectorType>
94 ((VectorType *)
nullptr))
101 template <
typename VectorType>
115 namespace BlockVectorIterators
121 template <
class BlockVectorType,
bool Constness>
134 template <
class BlockVectorType>
141 typedef typename BlockVectorType::BlockType
Vector;
169 template <
class BlockVectorType>
176 typedef const typename BlockVectorType::BlockType
Vector;
216 template <
class BlockVectorType,
bool Constness>
240 typedef std::ptrdiff_t difference_type;
241 typedef typename BlockVectorType::reference reference;
312 dereference_type
operator [] (
const difference_type d)
const;
344 template <
bool OtherConstness>
351 template <
bool OtherConstness>
359 template <
bool OtherConstness>
360 bool operator < (const Iterator<BlockVectorType, OtherConstness> &i)
const;
365 template <
bool OtherConstness>
366 bool operator <= (const Iterator<BlockVectorType, OtherConstness> &i)
const;
371 template <
bool OtherConstness>
377 template <
bool OtherConstness>
383 template <
bool OtherConstness>
420 "Your program tried to compare iterators pointing to " 421 "different block vectors. There is no reasonable way " 468 template <
typename,
bool>
513 template <
class VectorType>
532 typedef typename BlockType::value_type value_type;
533 typedef value_type *pointer;
534 typedef const value_type *const_pointer;
535 typedef ::internal::BlockVectorIterators::Iterator<BlockVectorBase,false>
iterator;
536 typedef ::internal::BlockVectorIterators::Iterator<BlockVectorBase,true>
const_iterator;
537 typedef typename BlockType::reference reference;
538 typedef typename BlockType::const_reference const_reference;
593 block (const
unsigned int i);
599 block (const
unsigned int i) const;
663 value_type operator() (const size_type i) const;
668 reference operator() (const size_type i);
675 value_type operator[] (const size_type i) const;
682 reference operator[] (const size_type i);
699 template <typename OtherNumber>
701 std::vector<OtherNumber> &values) const;
730 template <typename ForwardIterator, typename OutputIterator>
732 const ForwardIterator indices_end,
733 OutputIterator values_begin) const;
757 template <class VectorType2>
765 operator = (const VectorType &v);
771 template <class VectorType2>
869 template <typename Number>
870 void add (const
std::vector<size_type> &indices,
871 const
std::vector<Number> &values);
877 template <typename Number>
878 void add (const
std::vector<size_type> &indices,
879 const Vector<Number> &values);
886 template <typename Number>
887 void add (const size_type n_elements,
888 const size_type *indices,
889 const Number *values);
895 void add (const value_type s);
921 void sadd (const value_type s, const value_type a,
928 void sadd (const value_type s, const value_type a,
947 template <class BlockVector2>
948 void scale (const BlockVector2 &v);
953 template <class BlockVector2>
954 void equ (const value_type a, const BlockVector2 &V);
989 template <typename N,
bool C>
990 friend class ::
internal::BlockVectorIterators::Iterator;
1004 namespace BlockVectorIterators
1006 template <
class BlockVectorType,
bool Constness>
1008 Iterator<BlockVectorType,Constness>::
1009 Iterator (
const Iterator<BlockVectorType,Constness> &c)
1012 global_index (c.global_index),
1013 current_block (c.current_block),
1014 index_within_block (c.index_within_block),
1015 next_break_forward (c.next_break_forward),
1016 next_break_backward (c.next_break_backward)
1021 template <
class BlockVectorType,
bool Constness>
1023 Iterator<BlockVectorType,Constness>::
1024 Iterator (
const Iterator<BlockVectorType,!Constness> &c)
1027 global_index (c.global_index),
1028 current_block (c.current_block),
1029 index_within_block (c.index_within_block),
1030 next_break_forward (c.next_break_forward),
1031 next_break_backward (c.next_break_backward)
1036 static_assert(Constness ==
true,
1037 "Constructing a non-const iterator from a const iterator " 1038 "does not make sense.");
1043 template <
class BlockVectorType,
bool Constness>
1045 Iterator<BlockVectorType,Constness>::
1047 const size_type global_index,
1048 const size_type current_block,
1049 const size_type index_within_block,
1050 const size_type next_break_forward,
1051 const size_type next_break_backward)
1054 global_index (global_index),
1055 current_block (current_block),
1056 index_within_block (index_within_block),
1057 next_break_forward (next_break_forward),
1058 next_break_backward (next_break_backward)
1064 template <
class BlockVectorType,
bool Constness>
1066 Iterator<BlockVectorType,Constness> &
1067 Iterator<BlockVectorType,Constness>::
1068 operator = (
const Iterator &c)
1071 global_index = c.global_index;
1072 index_within_block = c.index_within_block;
1073 current_block = c.current_block;
1074 next_break_forward = c.next_break_forward;
1075 next_break_backward = c.next_break_backward;
1082 template <
class BlockVectorType,
bool Constness>
1084 typename Iterator<BlockVectorType,Constness>::dereference_type
1087 return parent->
block(current_block)(index_within_block);
1092 template <
class BlockVectorType,
bool Constness>
1094 typename Iterator<BlockVectorType,Constness>::dereference_type
1095 Iterator<BlockVectorType,Constness>::operator [] (
const difference_type d)
const 1102 if ((global_index+d >= next_break_backward) &&
1103 (global_index+d <= next_break_forward))
1104 return parent->
block(current_block)(index_within_block + d);
1113 return (*parent)(global_index+d);
1118 template <
class BlockVectorType,
bool Constness>
1120 Iterator<BlockVectorType,Constness> &
1121 Iterator<BlockVectorType,Constness>::operator ++ ()
1129 template <
class BlockVectorType,
bool Constness>
1131 Iterator<BlockVectorType,Constness>
1132 Iterator<BlockVectorType,Constness>::operator ++ (
int)
1134 const Iterator old_value = *
this;
1141 template <
class BlockVectorType,
bool Constness>
1143 Iterator<BlockVectorType,Constness> &
1144 Iterator<BlockVectorType,Constness>::operator -- ()
1152 template <
class BlockVectorType,
bool Constness>
1154 Iterator<BlockVectorType,Constness>
1155 Iterator<BlockVectorType,Constness>::operator -- (
int)
1157 const Iterator old_value = *
this;
1164 template <
class BlockVectorType,
bool Constness>
1165 template <
bool OtherConstness>
1168 Iterator<BlockVectorType,Constness>::
1169 operator == (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1171 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1173 return (global_index == i.global_index);
1178 template <
class BlockVectorType,
bool Constness>
1179 template <
bool OtherConstness>
1182 Iterator<BlockVectorType,Constness>::
1183 operator != (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1185 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1187 return (global_index != i.global_index);
1192 template <
class BlockVectorType,
bool Constness>
1193 template <
bool OtherConstness>
1196 Iterator<BlockVectorType,Constness>::
1197 operator < (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1199 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1201 return (global_index < i.global_index);
1206 template <
class BlockVectorType,
bool Constness>
1207 template <
bool OtherConstness>
1210 Iterator<BlockVectorType,Constness>::
1211 operator <= (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1213 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1215 return (global_index <= i.global_index);
1220 template <
class BlockVectorType,
bool Constness>
1221 template <
bool OtherConstness>
1224 Iterator<BlockVectorType,Constness>::
1225 operator > (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1227 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1229 return (global_index > i.global_index);
1234 template <
class BlockVectorType,
bool Constness>
1235 template <
bool OtherConstness>
1238 Iterator<BlockVectorType,Constness>::
1239 operator >= (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1241 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1243 return (global_index >= i.global_index);
1248 template <
class BlockVectorType,
bool Constness>
1249 template <
bool OtherConstness>
1251 typename Iterator<BlockVectorType,Constness>::difference_type
1253 operator - (
const Iterator<BlockVectorType, OtherConstness> &i)
const 1255 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1257 return (static_cast<signed int>(global_index) -
1258 static_cast<signed int>(i.global_index));
1263 template <
class BlockVectorType,
bool Constness>
1265 Iterator<BlockVectorType,Constness>
1274 if ((global_index+d >= next_break_backward) &&
1275 (global_index+d <= next_break_forward))
1276 return Iterator (*parent, global_index+d, current_block,
1277 index_within_block+d,
1278 next_break_forward, next_break_backward);
1283 return Iterator (*parent, global_index+d);
1288 template <
class BlockVectorType,
bool Constness>
1290 Iterator<BlockVectorType,Constness>
1299 if ((global_index-d >= next_break_backward) &&
1300 (global_index-d <= next_break_forward))
1301 return Iterator (*parent, global_index-d, current_block,
1302 index_within_block-d,
1303 next_break_forward, next_break_backward);
1308 return Iterator (*parent, global_index-d);
1313 template <
class BlockVectorType,
bool Constness>
1315 Iterator<BlockVectorType,Constness> &
1316 Iterator<BlockVectorType,Constness>::
1317 operator += (
const difference_type &d)
1324 if ((global_index+d >= next_break_backward) &&
1325 (global_index+d <= next_break_forward))
1328 index_within_block += d;
1334 *
this = Iterator (*parent, global_index+d);
1341 template <
class BlockVectorType,
bool Constness>
1343 Iterator<BlockVectorType,Constness> &
1344 Iterator<BlockVectorType,Constness>::
1345 operator -= (
const difference_type &d)
1352 if ((global_index-d >= next_break_backward) &&
1353 (global_index-d <= next_break_forward))
1356 index_within_block -= d;
1362 *
this = Iterator (*parent, global_index-d);
1368 template <
class BlockVectorType,
bool Constness>
1369 Iterator<BlockVectorType,Constness>::
1371 const size_type global_index)
1374 global_index (global_index)
1382 if (global_index < parent.
size())
1384 const std::pair<size_type, size_type>
1386 current_block = indices.first;
1387 index_within_block = indices.second;
1399 this->global_index = parent.
size ();
1401 index_within_block = 0;
1402 next_break_backward = global_index;
1409 template <
class BlockVectorType,
bool Constness>
1411 Iterator<BlockVectorType,Constness>::move_forward ()
1413 if (global_index != next_break_forward)
1414 ++index_within_block;
1419 index_within_block = 0;
1424 next_break_backward = next_break_forward+1;
1443 template <
class BlockVectorType,
bool Constness>
1445 Iterator<BlockVectorType,Constness>::move_backward ()
1447 if (global_index != next_break_backward)
1448 --index_within_block;
1449 else if (current_block != 0)
1458 next_break_forward = next_break_backward-1;
1470 next_break_forward = 0;
1471 next_break_backward = 0;
1484 template <
class VectorType>
1494 template <
class VectorType>
1516 template <
class VectorType>
1525 template <
class VectorType>
1537 template <
class VectorType>
1549 template <
class VectorType>
1558 template <
class VectorType>
1563 std::vector<size_type> sizes (
n_blocks());
1565 for (size_type i=0; i<
n_blocks(); ++i)
1566 sizes[i] =
block(i).size();
1573 template <
class VectorType>
1578 for (
unsigned int i=0; i<
n_blocks(); ++i)
1579 block(i).compress (operation);
1584 template <
class VectorType>
1589 return iterator(*
this, 0U);
1594 template <
class VectorType>
1599 return const_iterator(*
this, 0U);
1603 template <
class VectorType>
1608 return iterator(*
this,
size());
1613 template <
class VectorType>
1618 return const_iterator(*
this,
size());
1622 template <
class VectorType>
1626 (
const size_type global_index)
const 1628 const std::pair<size_type,size_type> local_index
1631 return components[local_index.first].in_local_range (global_index);
1635 template <
class VectorType>
1639 for (size_type i=0; i<
n_blocks(); ++i)
1648 template <
class VectorType>
1652 for (size_type i=0; i<
n_blocks(); ++i)
1661 template <
class VectorType>
1662 typename BlockVectorBase<VectorType>::value_type
1669 value_type
sum = 0.;
1670 for (size_type i=0; i<
n_blocks(); ++i)
1677 template <
class VectorType>
1682 for (size_type i=0; i<
n_blocks(); ++i)
1690 template <
class VectorType>
1691 typename BlockVectorBase<VectorType>::value_type
1694 value_type
sum = 0.;
1696 for (size_type i=0; i<
n_blocks(); ++i)
1704 template <
class VectorType>
1709 for (size_type i=0; i<
n_blocks(); ++i)
1717 template <
class VectorType>
1726 template <
class VectorType>
1731 for (size_type i=0; i<
n_blocks(); ++i)
1733 value_type newval =
components[i].linfty_norm();
1742 template <
class VectorType>
1743 typename BlockVectorBase<VectorType>::value_type
1745 add_and_dot (
const typename BlockVectorBase<VectorType>::value_type a,
1752 value_type
sum = 0.;
1753 for (size_type i=0; i<
n_blocks(); ++i)
1761 template <
class VectorType>
1768 for (size_type i=0; i<
n_blocks(); ++i)
1778 template <
class VectorType>
1785 for (size_type i=0; i<
n_blocks(); ++i)
1794 template <
class VectorType>
1795 template <
typename Number>
1799 const std::vector<Number> &values)
1801 Assert (indices.size() == values.size(),
1803 add (indices.size(), indices.data(), values.data());
1808 template <
class VectorType>
1809 template <
typename Number>
1813 const Vector<Number> &values)
1815 Assert (indices.size() == values.size(),
1817 const size_type n_indices = indices.size();
1818 for (size_type i=0; i<n_indices; ++i)
1819 (*
this)(indices[i]) += values(i);
1824 template <
class VectorType>
1825 template <
typename Number>
1829 const size_type *indices,
1830 const Number *values)
1832 for (size_type i=0; i<n_indices; ++i)
1833 (*
this)(indices[i]) += values[i];
1838 template <
class VectorType>
1843 for (size_type i=0; i<
n_blocks(); ++i)
1851 template <
class VectorType>
1861 for (size_type i=0; i<
n_blocks(); ++i)
1869 template <
class VectorType>
1885 for (size_type i=0; i<
n_blocks(); ++i)
1893 template <
class VectorType>
1903 for (size_type i=0; i<
n_blocks(); ++i)
1911 template <
class VectorType>
1922 for (size_type i=0; i<
n_blocks(); ++i)
1930 template <
class VectorType>
1946 for (size_type i=0; i<
n_blocks(); ++i)
1954 template <
class VectorType>
1975 for (size_type i=0; i<
n_blocks(); ++i)
1984 template <
class VectorType>
1985 template <
class BlockVector2>
1990 for (size_type i=0; i<
n_blocks(); ++i)
1996 template <
class VectorType>
2011 for (size_type i=0; i<
n_blocks(); ++i)
2019 template <
class VectorType>
2030 template <
class VectorType>
2031 template <
class BlockVector2>
2033 const BlockVector2 &v)
2041 for (size_type i=0; i<
n_blocks(); ++i)
2047 template <
class VectorType>
2050 for (size_type i=0; i<
n_blocks(); ++i)
2051 block(i).update_ghost_values ();
2056 template <
class VectorType>
2063 for (size_type i=0; i<
n_blocks(); ++i)
2070 template <
class VectorType>
2076 for (size_type i=0; i<
n_blocks(); ++i)
2083 template <
class VectorType>
2084 template <
class VectorType2>
2090 for (size_type i=0; i<
n_blocks(); ++i)
2098 template <
class VectorType>
2107 for (size_type i=0; i<
block(b).size(); ++i, ++index_v)
2108 block(b)(i) = v(index_v);
2115 template <
class VectorType>
2116 template <
class VectorType2>
2124 for (size_type i=0; i<
n_blocks(); ++i)
2133 template <
class VectorType>
2141 for (size_type i=0; i<
n_blocks(); ++i)
2149 template <
class VectorType>
2158 for (size_type i=0; i<
n_blocks(); ++i)
2165 template <
class VectorType>
2167 typename BlockVectorBase<VectorType>::value_type
2170 const std::pair<unsigned int,size_type> local_index
2172 return components[local_index.first](local_index.second);
2177 template <
class VectorType>
2179 typename BlockVectorBase<VectorType>::reference
2182 const std::pair<unsigned int,size_type> local_index
2184 return components[local_index.first](local_index.second);
2189 template <
class VectorType>
2191 typename BlockVectorBase<VectorType>::value_type
2199 template <
class VectorType>
2201 typename BlockVectorBase<VectorType>::reference
2209 template <
typename VectorType>
2210 template <
typename OtherNumber>
2213 std::vector<OtherNumber> &values)
const 2215 for (size_type i = 0; i < indices.size(); ++i)
2216 values[i] =
operator()(indices[i]);
2221 template <
typename VectorType>
2222 template <
typename ForwardIterator,
typename OutputIterator>
2225 const ForwardIterator indices_end,
2226 OutputIterator values_begin)
const 2228 while (indices_begin != indices_end)
2238 DEAL_II_NAMESPACE_CLOSE
types::global_dof_index size_type
const types::global_dof_index invalid_size_type
static yes_type check_for_block_vector(const BlockVectorBase< T > *)
BaseClass::value_type value_type
bool operator>(const Iterator< BlockVectorType, OtherConstness > &i) const
value_type dereference_type
#define AssertDimension(dim1, dim2)
value_type operator[](const size_type i) const
void add(const std::vector< size_type > &indices, const std::vector< Number > &values)
real_type l2_norm() const
value_type operator*(const BlockVectorBase &V) const
dereference_type operator[](const difference_type d) const
bool operator==(const BlockVectorBase< VectorType2 > &v) const
void compress(::VectorOperation::values operation)
BlockVector::value_type value_type
bool in_local_range(const size_type global_index) const
size_type block_size(const unsigned int i) const
SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator+(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
SymmetricTensor< rank_, dim, Number > operator*(const SymmetricTensor< rank_, dim, Number > &t, const Number &factor)
static ::ExceptionBase & ExcDivideByZero()
Iterator operator+(const difference_type &d) const
BlockVectorBase & operator/=(const value_type factor)
value_type add_and_dot(const value_type a, const BlockVectorBase &V, const BlockVectorBase &W)
size_type total_size() const
bool is_non_negative() const
void scale(const BlockVector2 &v)
Vector::reference dereference_type
BlockVectorBase()=default
BlockIndices block_indices
IndexSet locally_owned_elements() const
real_type norm_sqr() const
size_type next_break_forward
T sum(const T &t, const MPI_Comm &mpi_communicator)
unsigned int global_dof_index
const BlockIndices & get_block_indices() const
#define Assert(cond, exc)
BlockType::real_type real_type
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
#define DeclExceptionMsg(Exception, defaulttext)
real_type l1_norm() const
void update_ghost_values() const
size_type local_to_global(const unsigned int block, const size_type index) const
bool operator==(const Iterator< BlockVectorType, OtherConstness > &i) const
static ::ExceptionBase & ExcDifferentBlockIndices()
const BlockVectorType BlockVector
BlockVectorBase & operator+=(const BlockVectorBase &V)
std::vector< VectorType > components
BlockVectorBase & operator*=(const value_type factor)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
Types< BlockVectorType, Constness >::value_type value_type
Tensor< 2, dim, Number > w(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
Iterator & operator+=(const difference_type &d)
unsigned int current_block
const BlockVector::value_type value_type
BlockVectorType BlockVector
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
void sadd(const value_type s, const BlockVectorBase &V)
std::size_t memory_consumption() const
BlockVectorType::BlockType Vector
unsigned int n_blocks() const
types::global_dof_index size_type
BlockVectorBase & operator-=(const BlockVectorBase &V)
bool operator!=(const Iterator< BlockVectorType, OtherConstness > &i) const
size_type block_start(const unsigned int i) const
value_type operator()(const size_type i) const
const BlockVectorType::BlockType Vector
difference_type operator-(const Iterator< BlockVectorType, OtherConstness > &i) const
real_type linfty_norm() const
bool operator>=(const Iterator< BlockVectorType, OtherConstness > &i) const
unsigned int size() const
BlockType & block(const unsigned int i)
SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator-(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
BlockVectorBase & operator=(const value_type s)
static ::ExceptionBase & ExcPointerToDifferentVectors()
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
#define AssertIsFinite(number)
void equ(const value_type a, const BlockVector2 &V)
Iterator & operator=(const Iterator &c)
std::random_access_iterator_tag iterator_category
Iterator & operator-=(const difference_type &d)
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
dereference_type operator*() const
value_type mean_value() const
Types< BlockVectorType, Constness >::BlockVector BlockVector