]> https://gitweb.dealii.org/ - dealii.git/commitdiff
BlockSparsityPatternBase: Refactor some row number checking.
authorDavid Wells <drwells@email.unc.edu>
Sun, 30 Oct 2022 00:18:39 +0000 (20:18 -0400)
committerDavid Wells <drwells@email.unc.edu>
Sun, 6 Nov 2022 12:49:36 +0000 (07:49 -0500)
include/deal.II/lac/block_sparsity_pattern.h
source/lac/block_sparsity_pattern.cc

index 48e89f0b4426fd4e4ae6ff5be36131e38bd1e56e..d78bfa596f3f01074ee46b77f84763a0e70a3b1d 100644 (file)
@@ -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<SparsityPatternType>::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())
     {
index e927b177dd80d4b0745c529037d2f6b673df88a7..9b712c13f0b497760bf314051e9177cca0ad530a 100644 (file)
@@ -92,6 +92,34 @@ BlockSparsityPatternBase<SparsityPatternType>::operator=(
 
 
 
+template <class SparsityPatternType>
+typename BlockSparsityPatternBase<SparsityPatternType>::size_type
+BlockSparsityPatternBase<SparsityPatternType>::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 <class SparsityPatternType>
+typename BlockSparsityPatternBase<SparsityPatternType>::size_type
+BlockSparsityPatternBase<SparsityPatternType>::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 <class SparsityPatternType>
 void
 BlockSparsityPatternBase<SparsityPatternType>::collect_sizes()
@@ -178,12 +206,9 @@ template <class SparsityPatternType>
 typename BlockSparsityPatternBase<SparsityPatternType>::size_type
 BlockSparsityPatternBase<SparsityPatternType>::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 <class SparsityPatternType>
 typename BlockSparsityPatternBase<SparsityPatternType>::size_type
 BlockSparsityPatternBase<SparsityPatternType>::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();
 }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.