17 #include <deal.II/base/std_cxx14/memory.h> 18 #include <deal.II/base/utilities.h> 20 #include <deal.II/lac/dynamic_sparsity_pattern.h> 21 #include <deal.II/lac/full_matrix.h> 22 #include <deal.II/lac/sparsity_pattern.h> 23 #include <deal.II/lac/sparsity_tools.h> 32 DEAL_II_NAMESPACE_OPEN
52 , store_diagonal_first_in_row(false)
61 , store_diagonal_first_in_row(false)
66 "This constructor can only be called if the provided argument " 67 "is the sparsity pattern for an empty matrix. This constructor can " 68 "not be used to copy-construct a non-empty sparsity pattern."));
77 const unsigned int max_per_row)
79 , store_diagonal_first_in_row(m == n)
88 const std::vector<unsigned int> &row_lengths)
90 , store_diagonal_first_in_row(m == n)
98 const unsigned int max_per_row)
101 reinit(m, m, max_per_row);
107 const std::vector<unsigned int> &row_lengths)
110 reinit(m, m, row_lengths);
116 const unsigned int max_per_row,
138 const size_type *
const original_row_start =
142 const size_type *
const original_row_end =
148 const size_type *
const original_last_before_side_diagonals =
149 (row > extra_off_diagonals ?
152 row - extra_off_diagonals) :
155 const size_type *
const original_first_after_side_diagonals =
156 (row <
rows - extra_off_diagonals - 1 ?
157 std::upper_bound(original_row_start,
159 row + extra_off_diagonals) :
167 next_free_slot = std::copy(original_row_start,
168 original_last_before_side_diagonals,
172 for (
size_type i = 1; i <= std::min(row, extra_off_diagonals);
173 ++i, ++next_free_slot)
174 *next_free_slot = row - i;
175 for (
size_type i = 1; i <= std::min(extra_off_diagonals,
rows - row - 1);
176 ++i, ++next_free_slot)
177 *next_free_slot = row + i;
180 next_free_slot = std::copy(original_first_after_side_diagonals,
200 "This operator can only be called if the provided argument " 201 "is the sparsity pattern for an empty matrix. This operator can " 202 "not be used to copy a non-empty sparsity pattern."));
205 ExcMessage(
"This operator can only be called if the current object is " 216 const unsigned int max_per_row)
219 const std::vector<unsigned int> row_lengths(m, max_per_row);
220 reinit(m, n, row_lengths);
236 if ((m == 0) || (n == 0))
261 std::size_t vec_len = 0;
264 std::max(row_lengths[i], 1U) :
279 (row_lengths.
size() == 0 ?
281 std::min(static_cast<size_type>(
282 *std::max_element(row_lengths.
begin(), row_lengths.
end())),
313 std::max(std::min(static_cast<size_type>(row_lengths[i - 1]), n),
314 static_cast<size_type>(1U)) :
315 std::min(static_cast<size_type>(row_lengths[i - 1]), n));
352 const std::size_t nonzero_elements =
355 std::bind(std::not_equal_to<size_type>(),
356 std::placeholders::_1,
359 std::unique_ptr<size_type[]> new_colnums(
new size_type[nonzero_elements]);
389 new_colnums[next_free_entry++] = tmp_entries[j];
393 next_row_start = next_free_entry;
410 (std::find(&new_colnums[
rowstart[line] + 1],
411 &new_colnums[next_row_start],
413 &new_colnums[next_row_start]),
416 (std::adjacent_find(&new_colnums[
rowstart[line] + 1],
417 &new_colnums[next_row_start]) ==
418 &new_colnums[next_row_start]),
430 colnums = std::move(new_colnums);
447 const bool do_diag_optimize = (sp.
n_rows() == sp.
n_cols());
448 std::vector<unsigned int> row_lengths(sp.
n_rows());
452 if (do_diag_optimize && !sp.
exists(i, i))
465 end_row = sp.
end(row);
467 for (; col_num != end_row; ++col_num)
470 if ((col != row) || !do_diag_optimize)
491 const bool do_diag_optimize = (dsp.
n_rows() == dsp.
n_cols());
494 std::vector<unsigned int> row_lengths(dsp.
n_rows());
496 if (row_index_set.size() == 0)
501 if (do_diag_optimize && !dsp.
exists(i, i))
509 if (row_index_set.is_element(i))
512 if (do_diag_optimize && !dsp.
exists(i, i))
521 row_lengths[i] = do_diag_optimize ? 1 : 0;
532 for (
unsigned int index = 0; index <
row_length; ++index)
535 if ((col != row) || !do_diag_optimize)
548 template <
typename number>
555 const bool matrix_is_square = (matrix.m() == matrix.n());
557 std::vector<unsigned int> entries_per_row(matrix.m(), 0);
558 for (
size_type row = 0; row < matrix.m(); ++row)
560 for (
size_type col = 0; col < matrix.n(); ++col)
561 if (matrix(row, col) != 0)
562 ++entries_per_row[row];
563 if (matrix_is_square && (matrix(row, row) == 0))
564 ++entries_per_row[row];
567 reinit(matrix.m(), matrix.n(), entries_per_row);
574 std::vector<size_type> column_indices;
575 column_indices.reserve(
576 entries_per_row.size() > 0 ?
577 *std::max_element(entries_per_row.begin(), entries_per_row.end()) :
579 for (
size_type row = 0; row < matrix.m(); ++row)
581 column_indices.resize(entries_per_row[row]);
584 for (
size_type col = 0; col < matrix.n(); ++col)
585 if (matrix(row, col) != 0)
587 column_indices[current_index] = col;
594 if (matrix_is_square && (col == row))
596 column_indices[current_index] = row;
604 add_entries(row, column_indices.begin(), column_indices.end(),
true);
615 const std::vector<unsigned int> &row_lengths)
617 reinit(m, n, make_array_view(row_lengths));
691 Utilities::lower_bound<const size_type *>(sorted_region_start,
730 template <
typename ForwardIterator>
733 ForwardIterator begin,
735 const bool indices_are_sorted)
737 if (indices_are_sorted ==
true)
741 ForwardIterator it =
begin;
742 bool has_larger_entries =
false;
750 has_larger_entries =
true;
753 if (has_larger_entries ==
false)
754 for (; it !=
end; ++it)
766 for (ForwardIterator p =
begin; p !=
end; ++p)
773 for (ForwardIterator it =
begin; it !=
end; ++it)
815 std::pair<SparsityPatternBase::size_type, SparsityPatternBase::size_type>
835 return std::make_pair(row, col);
887 out <<
']' << std::endl;
908 out <<
colnums[j] <<
" " << -
static_cast<signed int>(i) << std::endl;
916 unsigned int m = this->
n_rows();
917 unsigned int n = this->
n_cols();
919 <<
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 " 920 << n + 2 <<
" " << m + 2
922 "<style type=\"text/css\" >\n" 930 << n + 2 <<
"\" height=\"" << m + 2
931 <<
"\" fill=\"rgb(128, 128, 128)\"/>\n" 932 " <rect x=\"1\" y=\"1\" width=\"" 933 << n + 0.1 <<
"\" height=\"" << m + 0.1
934 <<
"\" fill=\"rgb(255, 255, 255)\"/>\n\n";
937 for (; it !=
end; ++it)
939 out <<
" <rect class=\"pixel\" x=\"" << it->column() + 1 <<
"\" y=\"" 940 << it->row() + 1 <<
"\" width=\".9\" height=\".9\"/>\n";
942 out <<
"</svg>" << std::endl;
956 if (static_cast<size_type>(
957 std::abs(static_cast<int>(i -
colnums[j]))) > b)
958 b = std::abs(static_cast<signed int>(i -
colnums[j]));
977 out.write(reinterpret_cast<const char *>(
rowstart.get()),
979 reinterpret_cast<const char *>(
rowstart.get()));
981 out.write(reinterpret_cast<const char *>(
colnums.get()),
983 reinterpret_cast<const char *>(
colnums.get()));
1014 in.read(reinterpret_cast<char *>(
rowstart.get()),
1016 reinterpret_cast<char *>(
rowstart.get()));
1021 in.read(reinterpret_cast<char *>(
colnums.get()),
1023 reinterpret_cast<char *>(
colnums.get()));
1053 SparsityPattern::add_entries<const SparsityPattern::size_type *>(
1058 #ifndef DEAL_II_VECTOR_ITERATOR_IS_POINTER 1061 std::vector<SparsityPattern::size_type>::const_iterator>(
1063 std::vector<size_type>::const_iterator,
1064 std::vector<size_type>::const_iterator,
1068 SparsityPattern::add_entries<std::vector<SparsityPattern::size_type>::iterator>(
1070 std::vector<size_type>::iterator,
1071 std::vector<size_type>::iterator,
1074 DEAL_II_NAMESPACE_CLOSE
Iterator lower_bound(Iterator first, Iterator last, const T &val)
void block_write(std::ostream &out) const
const types::global_dof_index invalid_size_type
void add_entries(const size_type row, ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted=false)
#define AssertDimension(dim1, dim2)
unsigned int max_row_length
static ::ExceptionBase & ExcMatrixIsCompressed()
size_type operator()(const size_type i, const size_type j) const
static ::ExceptionBase & ExcIO()
size_type column_number(const size_type row, const size_type index) const
std::size_t memory_consumption() const
size_type row_position(const size_type i, const size_type j) const
std::pair< size_type, size_type > matrix_position(const std::size_t global_index) const
size_type bandwidth() const
#define AssertThrow(cond, exc)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void reinit(const size_type m, const size_type n, const unsigned int max_per_row)
std::unique_ptr< std::size_t[]> rowstart
void print_gnuplot(std::ostream &out) const
static ::ExceptionBase & ExcNotCompressed()
size_type max_entries_per_row() const
virtual void reinit(const size_type m, const size_type n, const ArrayView< const unsigned int > &row_lengths) override
static ::ExceptionBase & ExcMessage(std::string arg1)
SparsityPatternBase::size_type size_type
#define Assert(cond, exc)
const IndexSet & row_index_set() const
std::size_t memory_consumption() const
void add(const size_type i, const size_type j)
SparsityPattern & operator=(const SparsityPattern &)
void copy_from(const size_type n_rows, const size_type n_cols, const ForwardIterator begin, const ForwardIterator end)
std::size_t n_nonzero_elements() const
bool exists(const size_type i, const size_type j) const
std::unique_ptr< size_type[]> colnums
static ::ExceptionBase & ExcNotQuadratic()
static ::ExceptionBase & ExcNotEnoughSpace(int arg1, int arg2)
bool is_compressed() const
size_type row_length(const size_type row) const
bool store_diagonal_first_in_row
static ::ExceptionBase & ExcEmptyObject()
void block_read(std::istream &in)
static const size_type invalid_entry
types::global_dof_index size_type
bool exists(const size_type i, const size_type j) const
unsigned int row_length(const size_type row) const
void print(std::ostream &out) const
void print_svg(std::ostream &out) const
static ::ExceptionBase & ExcInternalError()