From 92ca9bfa21957c95d0a761f4fac821c86065efdb Mon Sep 17 00:00:00 2001 From: David Wells Date: Sat, 29 Oct 2022 20:18:39 -0400 Subject: [PATCH] BlockSparsityPatternBase: Refactor some row number checking. --- include/deal.II/lac/block_sparsity_pattern.h | 26 +++++++++++ source/lac/block_sparsity_pattern.cc | 46 +++++++++++++++----- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/include/deal.II/lac/block_sparsity_pattern.h b/include/deal.II/lac/block_sparsity_pattern.h index 48e89f0b44..d78bfa596f 100644 --- a/include/deal.II/lac/block_sparsity_pattern.h +++ b/include/deal.II/lac/block_sparsity_pattern.h @@ -315,6 +315,15 @@ public: * @{ */ + /** + * Exception + */ + DeclExceptionMsg( + ExcNeedsCollectSizes, + "The number of rows and columns (returned by n_rows() and n_cols()) does " + "not match their directly computed values. This typically means that a " + "call to collect_sizes() is missing."); + /** * Exception */ @@ -366,6 +375,18 @@ protected: BlockIndices column_indices; private: + /** + * Internal utility function for computing the number of rows. + */ + size_type + compute_n_rows() const; + + /** + * Internal utility function for computing the number of columns. + */ + size_type + compute_n_cols() const; + /** * Temporary vector for counting the elements written into the individual * blocks when doing a collective add or set. @@ -814,6 +835,11 @@ BlockSparsityPatternBase::add_entries( ForwardIterator end, const bool indices_are_sorted) { + // In debug mode, verify that collect_sizes() was called by redoing the + // calculation + Assert(n_rows() == compute_n_rows(), ExcNeedsCollectSizes()); + Assert(n_cols() == compute_n_cols(), ExcNeedsCollectSizes()); + // Resize scratch arrays if (block_column_indices.size() < this->n_block_cols()) { diff --git a/source/lac/block_sparsity_pattern.cc b/source/lac/block_sparsity_pattern.cc index e927b177dd..9b712c13f0 100644 --- a/source/lac/block_sparsity_pattern.cc +++ b/source/lac/block_sparsity_pattern.cc @@ -92,6 +92,34 @@ BlockSparsityPatternBase::operator=( +template +typename BlockSparsityPatternBase::size_type +BlockSparsityPatternBase::compute_n_rows() const +{ + // only count in first column, since + // all rows should be equivalent + size_type count = 0; + for (size_type r = 0; r < rows; ++r) + count += sub_objects[r][0]->n_rows(); + return count; +} + + + +template +typename BlockSparsityPatternBase::size_type +BlockSparsityPatternBase::compute_n_cols() const +{ + // only count in first row, since + // all rows should be equivalent + size_type count = 0; + for (size_type c = 0; c < columns; ++c) + count += sub_objects[0][c]->n_cols(); + return count; +} + + + template void BlockSparsityPatternBase::collect_sizes() @@ -178,12 +206,9 @@ template typename BlockSparsityPatternBase::size_type BlockSparsityPatternBase::n_rows() const { - // only count in first column, since - // all rows should be equivalent - size_type count = 0; - for (size_type r = 0; r < rows; ++r) - count += sub_objects[r][0]->n_rows(); - return count; + // While trivial for the moment, this will be replaced by a base class + // function in a future patch + return compute_n_rows(); } @@ -192,12 +217,9 @@ template typename BlockSparsityPatternBase::size_type BlockSparsityPatternBase::n_cols() const { - // only count in first row, since - // all rows should be equivalent - size_type count = 0; - for (size_type c = 0; c < columns; ++c) - count += sub_objects[0][c]->n_cols(); - return count; + // While trivial for the moment, this will be replaced by a base class + // function in a future patch + return compute_n_cols(); } -- 2.39.5