16 #ifndef dealii_chunk_sparse_matrix_h 17 #define dealii_chunk_sparse_matrix_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/base/subscriptor.h> 22 #include <deal.II/base/smartpointer.h> 23 #include <deal.II/lac/chunk_sparsity_pattern.h> 24 #include <deal.II/lac/identity_matrix.h> 25 #include <deal.II/lac/exceptions.h> 30 DEAL_II_NAMESPACE_OPEN
32 template <
typename number>
class Vector;
46 template <
typename number,
bool Constness>
59 template <
typename number,
bool Constness>
88 template <
typename number>
102 const unsigned int row);
117 number
value()
const;
139 template <
typename,
bool>
150 template <
typename number>
183 Reference (
const Accessor *accessor,
189 operator number ()
const;
194 const Reference &operator = (
const number n)
const;
199 const Reference &operator += (
const number n)
const;
204 const Reference &operator -= (
const number n)
const;
209 const Reference &operator *= (
const number n)
const;
214 const Reference &operator /= (
const number n)
const;
235 const unsigned int row);
245 Reference
value()
const;
267 template <
typename,
bool>
283 template <
typename number,
bool Constness>
306 const unsigned int row);
406 template <
typename number>
572 virtual void clear ();
660 template <
typename number2>
664 const number2 *values,
665 const bool elide_zero_values =
true,
666 const bool col_indices_are_sorted =
false);
708 template <
typename somenumber>
729 template <
typename ForwardIterator>
731 const ForwardIterator
end);
738 template <
typename somenumber>
752 template <
typename somenumber>
753 void add (
const number factor,
817 number *values)
const;
838 template <
class OutVector,
class InVector>
839 void vmult (OutVector &dst,
840 const InVector &src)
const;
857 template <
class OutVector,
class InVector>
858 void Tvmult (OutVector &dst,
859 const InVector &src)
const;
875 template <
class OutVector,
class InVector>
877 const InVector &src)
const;
894 template <
class OutVector,
class InVector>
896 const InVector &src)
const;
913 template <
typename somenumber>
919 template <
typename somenumber>
921 const Vector<somenumber> &v)
const;
929 template <
typename somenumber>
930 somenumber
residual (Vector<somenumber> &dst,
931 const Vector<somenumber> &x,
932 const Vector<somenumber> &b)
const;
974 template <
typename somenumber>
976 const Vector<somenumber> &src,
977 const number omega = 1.)
const;
982 template <
typename somenumber>
984 const Vector<somenumber> &src,
985 const number om = 1.)
const;
990 template <
typename somenumber>
992 const Vector<somenumber> &src,
993 const number om = 1.)
const;
998 template <
typename somenumber>
1000 const Vector<somenumber> &src,
1001 const number om = 1.)
const;
1008 template <
typename somenumber>
1009 void SSOR (Vector<somenumber> &v,
1010 const number omega = 1.)
const;
1016 template <
typename somenumber>
1017 void SOR (Vector<somenumber> &v,
1018 const number om = 1.)
const;
1024 template <
typename somenumber>
1025 void TSOR (Vector<somenumber> &v,
1026 const number om = 1.)
const;
1038 template <
typename somenumber>
1039 void PSOR (Vector<somenumber> &v,
1040 const std::vector<size_type> &permutation,
1041 const std::vector<size_type> &inverse_permutation,
1042 const number om = 1.)
const;
1054 template <
typename somenumber>
1055 void TPSOR (Vector<somenumber> &v,
1056 const std::vector<size_type> &permutation,
1057 const std::vector<size_type> &inverse_permutation,
1058 const number om = 1.)
const;
1064 template <
typename somenumber>
1065 void SOR_step (Vector<somenumber> &v,
1066 const Vector<somenumber> &b,
1067 const number om = 1.)
const;
1073 template <
typename somenumber>
1075 const Vector<somenumber> &b,
1076 const number om = 1.)
const;
1082 template <
typename somenumber>
1084 const Vector<somenumber> &b,
1085 const number om = 1.)
const;
1207 void print (std::ostream &out)
const;
1230 const unsigned int precision = 3,
1231 const bool scientific =
true,
1232 const unsigned int width = 0,
1233 const char *zero_string =
" ",
1234 const double denominator = 1.)
const;
1242 const double threshold = 0.)
const;
1284 <<
"You are trying to access the matrix entry with index <" 1285 << arg1 <<
',' << arg2
1286 <<
">, but this entry does not exist in the sparsity pattern" 1289 "The most common cause for this problem is that you used " 1290 "a method to build the sparsity pattern that did not " 1291 "(completely) take into account all of the entries you " 1292 "will later try to write into. An example would be " 1293 "building a sparsity pattern that does not include " 1294 "the entries you will write into due to constraints " 1295 "on degrees of freedom such as hanging nodes or periodic " 1296 "boundary conditions. In such cases, building the " 1297 "sparsity pattern will succeed, but you will get errors " 1298 "such as the current one at one point or other when " 1299 "trying to write into the entries of the matrix.");
1309 <<
"The iterators denote a range of " << arg1
1310 <<
" elements, but the given number of rows was " << arg2);
1315 "You are attempting an operation on two matrices that " 1316 "are the same object, but the operation requires that the " 1317 "two objects are in fact different.");
1334 std::unique_ptr<number[]>
val;
1367 template <
typename number>
1377 template <
typename number>
1388 template <
typename number>
1399 template <
typename number>
1403 const size_type j)
const 1407 = cols->sparsity_pattern(i/chunk_size, j/chunk_size);
1413 return (chunk_index * chunk_size * chunk_size
1415 (i % chunk_size) * chunk_size
1422 template <
typename number>
1434 const size_type index = compute_location(i,j);
1437 ExcInvalidIndex(i,j));
1445 template <
typename number>
1458 const size_type index = compute_location(i,j);
1460 ExcInvalidIndex(i,j));
1462 val[index] +=
value;
1468 template <
typename number>
1469 template <
typename number2>
1472 const size_type n_cols,
1473 const size_type *col_indices,
1474 const number2 *values,
1479 for (size_type col=0; col<n_cols; ++col)
1480 add(
row, col_indices[col], static_cast<number>(values[col]));
1485 template <
typename number>
1499 number *val_ptr = val.get();
1500 const number *
const end_ptr = val.get() +
1501 cols->sparsity_pattern.n_nonzero_elements()
1504 while (val_ptr != end_ptr)
1505 *val_ptr++ *= factor;
1512 template <
typename number>
1521 const number factor_inv = 1. / factor;
1529 number *val_ptr = val.get();
1530 const number *
const end_ptr = val.get() +
1531 cols->sparsity_pattern.n_nonzero_elements()
1535 while (val_ptr != end_ptr)
1536 *val_ptr++ *= factor_inv;
1543 template <
typename number>
1546 const size_type j)
const 1550 ExcInvalidIndex(i,j));
1551 return val[compute_location(i,j)];
1556 template <
typename number>
1559 const size_type j)
const 1562 const size_type index = compute_location(i,j);
1572 template <
typename number>
1583 return val[cols->sparsity_pattern.rowstart[i/
chunk_size]
1594 template <
typename number>
1595 template <
typename ForwardIterator>
1599 const ForwardIterator end)
1601 Assert (static_cast<size_type >(std::distance (begin, end)) == m(),
1602 ExcIteratorRange (std::distance (begin, end), m()));
1606 typedef typename std::iterator_traits<ForwardIterator>::value_type::const_iterator inner_iterator;
1608 for (ForwardIterator i=begin; i!=end; ++i, ++
row)
1610 const inner_iterator end_of_row = i->end();
1611 for (inner_iterator j=i->begin(); j!=end_of_row; ++j)
1613 set (
row, j->first, j->second);
1624 template <
typename number>
1627 Accessor (
const MatrixType *matrix,
1628 const unsigned int row)
1637 template <
typename number>
1639 Accessor<number,true>::
1640 Accessor (
const MatrixType *matrix)
1648 template <
typename number>
1650 Accessor<number,true>::
1659 template <
typename number>
1662 Accessor<number, true>::value ()
const 1664 const unsigned int chunk_size =
matrix->get_sparsity_pattern().get_chunk_size();
1674 template <
typename number>
1676 const typename Accessor<number, true>::MatrixType &
1677 Accessor<number, true>::get_matrix ()
const 1684 template <
typename number>
1686 Accessor<number, false>::Reference::Reference (
1687 const Accessor *accessor,
1694 template <
typename number>
1696 Accessor<number, false>::Reference::operator number()
const 1698 const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1703 accessor->chunk_col];
1708 template <
typename number>
1710 const typename Accessor<number, false>::Reference &
1711 Accessor<number, false>::Reference::operator = (
const number n)
const 1713 const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1718 accessor->chunk_col] = n;
1724 template <
typename number>
1726 const typename Accessor<number, false>::Reference &
1727 Accessor<number, false>::Reference::operator += (
const number n)
const 1729 const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1734 accessor->chunk_col] += n;
1740 template <
typename number>
1742 const typename Accessor<number, false>::Reference &
1743 Accessor<number, false>::Reference::operator -= (
const number n)
const 1745 const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1750 accessor->chunk_col] -= n;
1756 template <
typename number>
1758 const typename Accessor<number, false>::Reference &
1759 Accessor<number, false>::Reference::operator *= (
const number n)
const 1761 const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1766 accessor->chunk_col] *= n;
1772 template <
typename number>
1774 const typename Accessor<number, false>::Reference &
1775 Accessor<number, false>::Reference::operator /= (
const number n)
const 1777 const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1782 accessor->chunk_col] /= n;
1788 template <
typename number>
1790 Accessor<number,false>::
1791 Accessor (MatrixType *matrix,
1792 const unsigned int row)
1801 template <
typename number>
1803 Accessor<number,false>::
1804 Accessor (MatrixType *matrix)
1812 template <
typename number>
1814 typename Accessor<number, false>::Reference
1815 Accessor<number, false>::value()
const 1817 return Reference(
this,
true);
1823 template <
typename number>
1825 typename Accessor<number, false>::MatrixType &
1826 Accessor<number, false>::get_matrix ()
const 1833 template <
typename number,
bool Constness>
1835 Iterator<number, Constness>::
1836 Iterator (MatrixType *matrix,
1837 const unsigned int row)
1844 template <
typename number,
bool Constness>
1846 Iterator<number, Constness>::
1847 Iterator (MatrixType *matrix)
1854 template <
typename number,
bool Constness>
1856 Iterator<number, Constness>::
1864 template <
typename number,
bool Constness>
1866 Iterator<number, Constness> &
1867 Iterator<number,Constness>::operator++ ()
1869 accessor.advance ();
1874 template <
typename number,
bool Constness>
1876 Iterator<number,Constness>
1877 Iterator<number,Constness>::operator++ (
int)
1879 const Iterator iter = *
this;
1880 accessor.advance ();
1885 template <
typename number,
bool Constness>
1887 const Accessor<number,Constness> &
1894 template <
typename number,
bool Constness>
1896 const Accessor<number,Constness> *
1897 Iterator<number,Constness>::operator-> ()
const 1903 template <
typename number,
bool Constness>
1906 Iterator<number,Constness>::
1907 operator == (
const Iterator &other)
const 1909 return (accessor == other.accessor);
1913 template <
typename number,
bool Constness>
1916 Iterator<number,Constness>::
1917 operator != (
const Iterator &other)
const 1919 return ! (*
this == other);
1923 template <
typename number,
bool Constness>
1926 Iterator<number,Constness>::
1927 operator < (
const Iterator &other)
const 1929 Assert (&accessor.get_matrix() == &other.accessor.get_matrix(),
1932 return (accessor < other.accessor);
1936 template <
typename number,
bool Constness>
1939 Iterator<number,Constness>::
1940 operator > (
const Iterator &other)
const 1942 return (other < *
this);
1946 template <
typename number,
bool Constness>
1952 Assert (&accessor.get_matrix() == &other.accessor.get_matrix(),
1959 Iterator copy = *
this;
1960 while (copy != other)
1968 Iterator copy = other;
1969 while (copy != *
this)
1980 template <
typename number,
bool Constness>
1982 Iterator<number,Constness>
1987 for (
unsigned int i=0; i<n; ++i)
1997 template <
typename number>
2002 return const_iterator(
this, 0);
2006 template <
typename number>
2011 return const_iterator(
this);
2015 template <
typename number>
2020 return iterator(
this, 0);
2024 template <
typename number>
2029 return iterator(
this);
2033 template <
typename number>
2039 return const_iterator(
this, r);
2044 template <
typename number>
2050 return const_iterator(
this, r+1);
2055 template <
typename number>
2061 return iterator(
this, r);
2066 template <
typename number>
2072 return iterator(
this, r+1);
2083 DEAL_II_NAMESPACE_CLOSE
types::global_dof_index size_type
void block_write(std::ostream &out) const
ChunkSparseMatrixIterators::Iterator< number, false > iterator
void precondition_SOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
void vmult(OutVector &dst, const InVector &src) const
#define DeclException2(Exception2, type1, type2, outsequence)
somenumber residual(Vector< somenumber > &dst, const Vector< somenumber > &x, const Vector< somenumber > &b) const
Contents is actually a matrix.
static const size_type invalid_entry
ChunkSparseMatrix< number > & copy_from(const ChunkSparseMatrix< somenumber > &source)
bool operator==(const Iterator &) const
void TPSOR(Vector< somenumber > &v, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const number om=1.) const
void print(std::ostream &out) const
Iterator operator+(const unsigned int n) const
void print_pattern(std::ostream &out, const double threshold=0.) const
void extract_row_copy(const size_type row, const size_type array_length, size_type &row_length, size_type *column_indices, number *values) const
#define AssertIndexRange(index, range)
ChunkSparseMatrixIterators::Iterator< number, true > const_iterator
size_type n_nonzero_elements() const
types::global_dof_index size_type
static ::ExceptionBase & ExcNotInitialized()
Iterator(MatrixType *matrix, const unsigned int row)
#define AssertThrow(cond, exc)
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)
std::unique_ptr< number[]> val
static ::ExceptionBase & ExcDivideByZero()
void TSOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number om=1.) const
const ChunkSparseMatrix< number > & get_matrix() const
void SOR(Vector< somenumber > &v, const number om=1.) const
static ::ExceptionBase & ExcDifferentChunkSparsityPatterns()
void TSOR(Vector< somenumber > &v, const number om=1.) const
somenumber matrix_scalar_product(const Vector< somenumber > &u, const Vector< somenumber > &v) const
void precondition_SSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
void print_formatted(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const unsigned int width=0, const char *zero_string=" ", const double denominator=1.) const
somenumber matrix_norm_square(const Vector< somenumber > &v) const
numbers::NumberTraits< number >::real_type real_type
const Accessor * accessor
std::size_t memory_consumption() const
const_iterator begin() const
unsigned int global_dof_index
number operator()(const size_type i, const size_type j) const
void SSOR(Vector< somenumber > &v, const number omega=1.) const
#define Assert(cond, exc)
void SOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number om=1.) const
void SSOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number om=1.) const
virtual ~ChunkSparseMatrix()
ChunkSparseMatrix & operator*=(const number factor)
size_type compute_location(const size_type i, const size_type j) const
#define DeclExceptionMsg(Exception, defaulttext)
#define DeclException0(Exception0)
void PSOR(Vector< somenumber > &v, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const number om=1.) const
const Accessor< number, Constness > * operator->() const
virtual void reinit(const ChunkSparsityPattern &sparsity)
static ::ExceptionBase & ExcIteratorRange(int arg1, int arg2)
void precondition_Jacobi(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1.) const
bool operator<(const Iterator &) const
number el(const size_type i, const size_type j) const
real_type frobenius_norm() const
void Tvmult(OutVector &dst, const InVector &src) const
const Accessor< number, Constness > & value_type
Accessor(const ChunkSparsityPattern *matrix, const unsigned int row)
static ::ExceptionBase & ExcNotQuadratic()
number diag_element(const size_type i) const
ChunkSparseMatrix< number > MatrixType
const Accessor< number, Constness > & operator*() const
const_iterator end() const
void Tvmult_add(OutVector &dst, const InVector &src) const
size_type n_actually_nonzero_elements() const
Accessor< number, Constness > accessor
void vmult_add(OutVector &dst, const InVector &src) const
static const bool zero_addition_can_be_elided
Accessor< number, Constness >::MatrixType MatrixType
const ChunkSparsityPattern & get_sparsity_pattern() const
void block_read(std::istream &in)
int operator-(const Iterator &p) const
ChunkSparseMatrix & operator/=(const number factor)
real_type l1_norm() const
bool operator>(const Iterator &) const
void set(const size_type i, const size_type j, const number value)
static const size_type invalid_entry
void add(const size_type i, const size_type j, const number value)
bool operator!=(const Iterator &) const
void precondition_TSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
static ::ExceptionBase & ExcInvalidIndex(int arg1, int arg2)
SmartPointer< const ChunkSparsityPattern, ChunkSparseMatrix< number > > cols
const ChunkSparseMatrix< number > MatrixType
static ::ExceptionBase & ExcSourceEqualsDestination()
ChunkSparseMatrix< number > & operator=(const ChunkSparseMatrix< number > &)
SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator-(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
real_type linfty_norm() const
#define AssertIsFinite(number)
static ::ExceptionBase & ExcInternalError()