17 #ifndef __deal2__block_vector_base_h
18 #define __deal2__block_vector_base_h
21 #include <deal.II/base/config.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>
33 DEAL_II_NAMESPACE_OPEN
59 template <
typename VectorType>
103 template <
typename VectorType>
117 namespace BlockVectorIterators
126 template <
class BlockVectorType,
bool constness>
143 template <
class BlockVectorType>
153 typedef typename BlockVectorType::BlockType
Vector;
191 template <
class BlockVectorType>
202 typedef const typename BlockVectorType::BlockType
Vector;
292 template <
class BlockVectorType,
bool constness>
294 public std::iterator<std::random_access_iterator_tag,
295 typename Types<BlockVectorType,constness>::value_type>
334 typedef std::ptrdiff_t difference_type;
335 typedef typename BlockVectorType::reference reference;
425 dereference_type
operator [] (
const difference_type d)
const;
673 template <
typename N,
bool C>
720 template <
class VectorType>
740 typedef typename BlockType::value_type value_type;
741 typedef value_type *pointer;
742 typedef const value_type *const_pointer;
743 typedef ::internal::BlockVectorIterators::Iterator<BlockVectorBase,false>
iterator;
744 typedef ::internal::BlockVectorIterators::Iterator<BlockVectorBase,true>
const_iterator;
745 typedef typename BlockType::reference reference;
746 typedef typename BlockType::const_reference const_reference;
814 void compress (::VectorOperation::values operation);
825 block (const
unsigned int i);
831 block (const
unsigned int i) const;
856 std::
size_t size () const;
907 value_type operator() (const size_type i) const;
913 reference operator() (const size_type i);
920 value_type operator[] (const size_type i) const;
928 reference operator[] (const size_type i);
940 template <typename OtherNumber>
942 std::vector<OtherNumber> &values) const;
948 template <typename ForwardIterator, typename OutputIterator>
950 const ForwardIterator indices_end,
951 OutputIterator values_begin) const;
971 template <class VectorType2>
988 template <class VectorType2>
1078 template <typename Number>
1079 void add (const std::vector<size_type> &indices,
1080 const std::vector<Number> &values);
1089 template <typename Number>
1090 void add (const std::vector<size_type> &indices,
1091 const
Vector<Number> &values);
1102 template <typename Number>
1103 void add (const size_type n_elements,
1104 const size_type *indices,
1105 const Number *values);
1113 void add (const value_type s);
1151 void sadd (const value_type s, const value_type a,
1159 void sadd (const value_type s, const value_type a,
1183 template <class BlockVector2>
1184 void scale (const BlockVector2 &v);
1189 template <class BlockVector2>
1190 void equ (const value_type a, const BlockVector2 &V);
1241 template <typename N,
bool C>
1242 friend class ::internal::BlockVectorIterators::Iterator;
1256 namespace BlockVectorIterators
1259 template <
class BlockVectorType,
bool constness>
1261 Iterator<BlockVectorType,constness>::
1262 Iterator (
const Iterator<BlockVectorType,constness> &c)
1265 global_index (c.global_index),
1266 current_block (c.current_block),
1267 index_within_block (c.index_within_block),
1268 next_break_forward (c.next_break_forward),
1269 next_break_backward (c.next_break_backward)
1274 template <
class BlockVectorType,
bool constness>
1276 Iterator<BlockVectorType,constness>::
1277 Iterator (
const InverseConstnessIterator &c)
1279 parent (const_cast<BlockVectorType *>(c.parent)),
1280 global_index (c.global_index),
1281 current_block (c.current_block),
1282 index_within_block (c.index_within_block),
1283 next_break_forward (c.next_break_forward),
1284 next_break_backward (c.next_break_backward)
1291 Assert (constness==
true, ExcCastingAwayConstness());
1296 template <
class BlockVectorType,
bool constness>
1298 Iterator<BlockVectorType,constness>::
1300 const size_type global_index,
1301 const size_type current_block,
1302 const size_type index_within_block,
1303 const size_type next_break_forward,
1304 const size_type next_break_backward)
1307 global_index (global_index),
1308 current_block (current_block),
1309 index_within_block (index_within_block),
1310 next_break_forward (next_break_forward),
1311 next_break_backward (next_break_backward)
1317 template <
class BlockVectorType,
bool constness>
1319 Iterator<BlockVectorType,constness> &
1320 Iterator<BlockVectorType,constness>::
1321 operator = (
const Iterator &c)
1324 global_index = c.global_index;
1325 index_within_block = c.index_within_block;
1326 current_block = c.current_block;
1327 next_break_forward = c.next_break_forward;
1328 next_break_backward = c.next_break_backward;
1335 template <
class BlockVectorType,
bool constness>
1337 typename Iterator<BlockVectorType,constness>::dereference_type
1338 Iterator<BlockVectorType,constness>::operator * ()
const
1340 return parent->
block(current_block)(index_within_block);
1345 template <
class BlockVectorType,
bool constness>
1347 typename Iterator<BlockVectorType,constness>::dereference_type
1348 Iterator<BlockVectorType,constness>::operator [] (
const difference_type d)
const
1355 if ((global_index+d >= next_break_backward) &&
1356 (global_index+d <= next_break_forward))
1357 return parent->
block(current_block)(index_within_block + d);
1366 return (*parent)(global_index+d);
1371 template <
class BlockVectorType,
bool constness>
1373 Iterator<BlockVectorType,constness> &
1374 Iterator<BlockVectorType,constness>::operator ++ ()
1382 template <
class BlockVectorType,
bool constness>
1384 Iterator<BlockVectorType,constness>
1385 Iterator<BlockVectorType,constness>::operator ++ (
int)
1387 const Iterator old_value = *
this;
1394 template <
class BlockVectorType,
bool constness>
1396 Iterator<BlockVectorType,constness> &
1397 Iterator<BlockVectorType,constness>::operator -- ()
1405 template <
class BlockVectorType,
bool constness>
1407 Iterator<BlockVectorType,constness>
1408 Iterator<BlockVectorType,constness>::operator -- (
int)
1410 const Iterator old_value = *
this;
1417 template <
class BlockVectorType,
bool constness>
1420 Iterator<BlockVectorType,constness>::
1421 operator == (
const Iterator &i)
const
1423 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1425 return (global_index == i.global_index);
1430 template <
class BlockVectorType,
bool constness>
1433 Iterator<BlockVectorType,constness>::
1434 operator == (
const InverseConstnessIterator &i)
const
1436 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1438 return (global_index == i.global_index);
1443 template <
class BlockVectorType,
bool constness>
1446 Iterator<BlockVectorType,constness>::
1447 operator != (
const Iterator &i)
const
1449 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1451 return (global_index != i.global_index);
1456 template <
class BlockVectorType,
bool constness>
1459 Iterator<BlockVectorType,constness>::
1460 operator != (
const InverseConstnessIterator &i)
const
1462 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1464 return (global_index != i.global_index);
1469 template <
class BlockVectorType,
bool constness>
1472 Iterator<BlockVectorType,constness>::
1473 operator < (
const Iterator &i)
const
1475 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1477 return (global_index < i.global_index);
1482 template <
class BlockVectorType,
bool constness>
1485 Iterator<BlockVectorType,constness>::
1486 operator < (
const InverseConstnessIterator &i)
const
1488 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1490 return (global_index < i.global_index);
1495 template <
class BlockVectorType,
bool constness>
1498 Iterator<BlockVectorType,constness>::
1499 operator <= (
const Iterator &i)
const
1501 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1503 return (global_index <= i.global_index);
1508 template <
class BlockVectorType,
bool constness>
1511 Iterator<BlockVectorType,constness>::
1512 operator <= (
const InverseConstnessIterator &i)
const
1514 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1516 return (global_index <= i.global_index);
1521 template <
class BlockVectorType,
bool constness>
1524 Iterator<BlockVectorType,constness>::
1525 operator > (
const Iterator &i)
const
1527 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1529 return (global_index > i.global_index);
1534 template <
class BlockVectorType,
bool constness>
1537 Iterator<BlockVectorType,constness>::
1538 operator > (
const InverseConstnessIterator &i)
const
1540 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1542 return (global_index > i.global_index);
1547 template <
class BlockVectorType,
bool constness>
1550 Iterator<BlockVectorType,constness>::
1551 operator >= (
const Iterator &i)
const
1553 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1555 return (global_index >= i.global_index);
1560 template <
class BlockVectorType,
bool constness>
1563 Iterator<BlockVectorType,constness>::
1564 operator >= (
const InverseConstnessIterator &i)
const
1566 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1568 return (global_index >= i.global_index);
1573 template <
class BlockVectorType,
bool constness>
1575 typename Iterator<BlockVectorType,constness>::difference_type
1576 Iterator<BlockVectorType,constness>::
1577 operator - (
const Iterator &i)
const
1579 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1581 return (static_cast<signed int>(global_index) -
1582 static_cast<signed int>(i.global_index));
1587 template <
class BlockVectorType,
bool constness>
1589 typename Iterator<BlockVectorType,constness>::difference_type
1590 Iterator<BlockVectorType,constness>::
1591 operator - (
const InverseConstnessIterator &i)
const
1593 Assert (parent == i.parent, ExcPointerToDifferentVectors());
1595 return (static_cast<signed int>(global_index) -
1596 static_cast<signed int>(i.global_index));
1601 template <
class BlockVectorType,
bool constness>
1603 Iterator<BlockVectorType,constness>
1604 Iterator<BlockVectorType,constness>::
1605 operator + (
const difference_type &d)
const
1612 if ((global_index+d >= next_break_backward) &&
1613 (global_index+d <= next_break_forward))
1614 return Iterator (*parent, global_index+d, current_block,
1615 index_within_block+d,
1616 next_break_forward, next_break_backward);
1621 return Iterator (*parent, global_index+d);
1626 template <
class BlockVectorType,
bool constness>
1628 Iterator<BlockVectorType,constness>
1629 Iterator<BlockVectorType,constness>::
1630 operator - (
const difference_type &d)
const
1637 if ((global_index-d >= next_break_backward) &&
1638 (global_index-d <= next_break_forward))
1639 return Iterator (*parent, global_index-d, current_block,
1640 index_within_block-d,
1641 next_break_forward, next_break_backward);
1646 return Iterator (*parent, global_index-d);
1651 template <
class BlockVectorType,
bool constness>
1653 Iterator<BlockVectorType,constness> &
1654 Iterator<BlockVectorType,constness>::
1655 operator += (
const difference_type &d)
1662 if ((global_index+d >= next_break_backward) &&
1663 (global_index+d <= next_break_forward))
1666 index_within_block += d;
1672 *
this = Iterator (*parent, global_index+d);
1679 template <
class BlockVectorType,
bool constness>
1681 Iterator<BlockVectorType,constness> &
1682 Iterator<BlockVectorType,constness>::
1683 operator -= (
const difference_type &d)
1690 if ((global_index-d >= next_break_backward) &&
1691 (global_index-d <= next_break_forward))
1694 index_within_block -= d;
1700 *
this = Iterator (*parent, global_index-d);
1706 template <
class BlockVectorType,
bool constness>
1707 Iterator<BlockVectorType,constness>::
1709 const size_type global_index)
1712 global_index (global_index)
1720 if (global_index < parent.
size())
1722 const std::pair<size_type, size_type>
1724 current_block = indices.first;
1725 index_within_block = indices.second;
1737 this->global_index = parent.
size ();
1739 index_within_block = 0;
1740 next_break_backward = global_index;
1747 template <
class BlockVectorType,
bool constness>
1749 Iterator<BlockVectorType,constness>::move_forward ()
1751 if (global_index != next_break_forward)
1752 ++index_within_block;
1757 index_within_block = 0;
1762 next_break_backward = next_break_forward+1;
1765 if (current_block < parent->block_indices.
size())
1781 template <
class BlockVectorType,
bool constness>
1783 Iterator<BlockVectorType,constness>::move_backward ()
1785 if (global_index != next_break_backward)
1786 --index_within_block;
1787 else if (current_block != 0)
1796 next_break_forward = next_break_backward-1;
1808 next_break_forward = 0;
1809 next_break_backward = 0;
1821 template <
class VectorType>
1828 template <
class VectorType>
1838 template <
class VectorType>
1847 for (
unsigned int b=0; b<
n_blocks(); ++b)
1860 template <
class VectorType>
1865 return block_indices.
size();
1869 template <
class VectorType>
1876 return components[i];
1881 template <
class VectorType>
1888 return components[i];
1893 template <
class VectorType>
1902 template <
class VectorType>
1907 std::vector<size_type> sizes (
n_blocks());
1910 sizes[i] =
block(i).size();
1912 block_indices.
reinit(sizes);
1917 template <
class VectorType>
1922 for (
unsigned int i=0; i<
n_blocks(); ++i)
1923 block(i).compress (operation);
1928 template <
class VectorType>
1933 compress(VectorOperation::unknown);
1938 template <
class VectorType>
1943 return iterator(*
this, 0U);
1948 template <
class VectorType>
1953 return const_iterator(*
this, 0U);
1957 template <
class VectorType>
1962 return iterator(*
this,
size());
1967 template <
class VectorType>
1972 return const_iterator(*
this,
size());
1976 template <
class VectorType>
1982 const std::pair<size_type,size_type> local_index
1985 return components[local_index.first].in_local_range (global_index);
1989 template <
class VectorType>
1994 if (components[i].
all_zero() ==
false)
2002 template <
class VectorType>
2015 template <
class VectorType>
2016 typename BlockVectorBase<VectorType>::value_type
2023 value_type
sum = 0.;
2031 template <
class VectorType>
2044 template <
class VectorType>
2045 typename BlockVectorBase<VectorType>::value_type
2048 value_type sum = 0.;
2050 sum += components[i].
mean_value() * components[i].size();
2057 template <
class VectorType>
2063 sum += components[i].
l1_norm();
2070 template <
class VectorType>
2079 template <
class VectorType>
2086 value_type newval = components[i].linfty_norm();
2095 template <
class VectorType>
2105 template <
class VectorType>
2121 template <
class VectorType>
2122 template <
typename Number>
2126 const std::vector<Number> &values)
2128 Assert (indices.size() == values.size(),
2130 add (indices.size(), &indices[0], &values[0]);
2135 template <
class VectorType>
2136 template <
typename Number>
2144 const size_type n_indices = indices.size();
2146 (*
this)(indices[i]) += values(i);
2151 template <
class VectorType>
2152 template <
typename Number>
2157 const Number *values)
2160 (*
this)(indices[i]) += values[i];
2165 template <
class VectorType>
2172 components[i].add(a);
2178 template <
class VectorType>
2192 template <
class VectorType>
2210 template <
class VectorType>
2234 template <
class VectorType>
2252 template <
class VectorType>
2271 template <
class VectorType>
2295 template <
class VectorType>
2325 template <
class VectorType>
2326 template <
class BlockVector2>
2332 components[i].
scale(v.block(i));
2337 template <
class VectorType>
2360 template <
class VectorType>
2364 std::size_t mem =
sizeof(this->
n_blocks());
2365 for (
size_type i=0; i<this->components.size(); ++i)
2373 template <
class VectorType>
2374 template <
class BlockVector2>
2376 const BlockVector2 &v)
2385 components[i].
equ( a, v.components[i]);
2390 template <
class VectorType>
2394 block(i).update_ghost_values ();
2399 template <
class VectorType>
2413 template <
class VectorType>
2426 template <
class VectorType>
2427 template <
class VectorType2>
2441 template <
class VectorType>
2451 block(b)(i) = v(index_v);
2458 template <
class VectorType>
2459 template <
class VectorType2>
2476 template <
class VectorType>
2485 components[i] *= factor;
2492 template <
class VectorType>
2502 components[i] /= factor;
2508 template <
class VectorType>
2510 typename BlockVectorBase<VectorType>::value_type
2513 const std::pair<size_type,size_type> local_index
2515 return components[local_index.first](local_index.second);
2520 template <
class VectorType>
2522 typename BlockVectorBase<VectorType>::reference
2525 const std::pair<size_type,size_type> local_index
2527 return components[local_index.first](local_index.second);
2532 template <
class VectorType>
2534 typename BlockVectorBase<VectorType>::value_type
2542 template <
class VectorType>
2544 typename BlockVectorBase<VectorType>::reference
2552 template <
typename VectorType>
2553 template <
typename OtherNumber>
2556 std::vector<OtherNumber> &values)
const
2558 for (
size_type i = 0; i < indices.size(); ++i)
2559 values[i] =
operator()(indices[i]);
2564 template <
typename VectorType>
2565 template <
typename ForwardIterator,
typename OutputIterator>
2568 const ForwardIterator indices_end,
2569 OutputIterator values_begin)
const
2571 while (indices_begin != indices_end)
2581 DEAL_II_NAMESPACE_CLOSE
BlockIndices block_indices
const types::global_dof_index invalid_size_type
static yes_type check_for_block_vector(const BlockVectorBase< T > *)
BaseClass::value_type value_type
value_type dereference_type
void equ(const value_type a, const BlockVector2 &V)
#define AssertDimension(dim1, dim2)
BlockVector::value_type value_type
real_type linfty_norm() const
bool operator>(const Iterator &i) const
real_type l1_norm() const
Auxiliary class aiding in the handling of block structures like in BlockVector or FESystem...
types::global_dof_index size_type
bool in_local_range(const size_type global_index) const
const BlockIndices & get_block_indices() const
IndexSet locally_owned_elements() const
bool operator>=(const Iterator &i) const
size_type next_break_forward
bool operator==(const BlockVectorBase< VectorType2 > &v) const
unsigned int current_block
bool operator==(const Iterator &i) const
bool is_finite(const double x)
BlockVectorBase & operator*=(const value_type factor)
dereference_type operator*() const
Vector::reference dereference_type
Iterator & operator+=(const difference_type &d)
size_type block_start(const unsigned int i) const
Iterator & operator=(const Iterator &c)
real_type norm_sqr() const
dereference_type operator[](const difference_type d) const
BlockVectorBase & operator+=(const BlockVectorBase &V)
void compress() DEAL_II_DEPRECATED
difference_type operator-(const Iterator &i) const
void sadd(const value_type s, const BlockVectorBase &V)
T sum(const T &t, const MPI_Comm &mpi_communicator)
unsigned int global_dof_index
bool operator<(const Iterator &i) const
#define Assert(cond, exc)
std::size_t memory_consumption(const T &t)
BlockType & block(const unsigned int i)
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
BlockVectorBase & operator-=(const BlockVectorBase &V)
size_type total_size() const
void update_ghost_values() const
Types< BlockVectorType, constness >::value_type value_type
bool operator!=(const Iterator &i) const
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
real_type l2_norm() const
unsigned int n_blocks() const
value_type operator()(const size_type i) const
BlockCompressedSparsityPattern CompressedBlockSparsityPattern DEAL_II_DEPRECATED
DeclException0(ExcPointerToDifferentVectors)
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
const BlockVectorType BlockVector
BlockType::real_type real_type
size_type block_size(const unsigned int i) const
void add(const std::vector< size_type > &indices, const std::vector< Number > &values)
const BlockVector::value_type value_type
value_type operator*(const BlockVectorBase &V) const
BlockVectorType BlockVector
std::size_t memory_consumption() const
BlockVectorType::BlockType Vector
::ExceptionBase & ExcNumberNotFinite()
bool is_non_negative() const
BlockVectorBase & operator/=(const value_type factor)
Iterator< BlockVectorType,!constness > InverseConstnessIterator
Iterator operator+(const difference_type &d) const
bool operator<=(const Iterator &i) const
Iterator & operator-=(const difference_type &d)
static const bool supports_distributed_data
value_type operator[](const size_type i) const
const BlockVectorType::BlockType Vector
BlockVectorBase & operator=(const value_type s)
unsigned int size() const
::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
std::random_access_iterator_tag iterator_type
value_type mean_value() const
::ExceptionBase & ExcDivideByZero()
std::vector< VectorType > components
size_type local_to_global(const unsigned int block, const size_type index) const
Types< BlockVectorType, constness >::BlockVector BlockVector
void scale(const BlockVector2 &v)