From 9d60ed310fbbbe5d4950c2b0c5e44a2884cc35b6 Mon Sep 17 00:00:00 2001 From: wolf Date: Tue, 16 Nov 1999 14:25:32 +0000 Subject: [PATCH] Import branch with new sparsematrixstruct constructors/reinit functions. git-svn-id: https://svn.dealii.org/trunk@1856 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/sparse_matrix.h | 103 +++++++++++++++--------- deal.II/lac/source/sparse_matrix.cc | 102 +++++++++++++++++------ 2 files changed, 141 insertions(+), 64 deletions(-) diff --git a/deal.II/lac/include/lac/sparse_matrix.h b/deal.II/lac/include/lac/sparse_matrix.h index 06f47d0336..3789eb35fd 100644 --- a/deal.II/lac/include/lac/sparse_matrix.h +++ b/deal.II/lac/include/lac/sparse_matrix.h @@ -15,7 +15,7 @@ #include #include -#include +#include //forward declarations @@ -23,7 +23,7 @@ template class Vector; template class SparseMatrix; class ostream; -//TODO: Documentation on full rows + /** * Structure representing the sparsity pattern of a sparse matrix. @@ -74,23 +74,43 @@ class SparseMatrixStruct : public Subscriptor * Initialize a rectangular matrix with * #m# rows and #n# columns. * The matrix may contain at most #max_per_row# - * nonzero entries per row, with the exception - * of the first #n_long_rows# rows. These may even - * be full. + * nonzero entries per row. */ SparseMatrixStruct (const unsigned int m, const unsigned int n, - const unsigned int max_per_row, - const unsigned int n_long_rows = 0); + const unsigned int max_per_row); + + /** + * Initialize a rectangular + * matrix with #m# rows and #n# + * columns. The maximal number + * of nonzero entries for each + * row is given by the + * #row_lengths# array. + */ + SparseMatrixStruct (const unsigned int m, + const unsigned int n, + const vector &row_lengths); /** * Initialize a square matrix of dimension - * #n# with at most #max_per_row#. There are - * no full rows with this constructor + * #n# with at most #max_per_row# + * nonzero entries per row. */ SparseMatrixStruct (const unsigned int n, const unsigned int max_per_row); + /** + * Initialize a square + * matrix with #m# rows and #m# + * columns. The maximal number + * of nonzero entries for each + * row is given by the + * #row_lengths# array. + */ + SparseMatrixStruct (const unsigned int m, + const vector &row_lengths); + /** * Copy operator. For this the same holds * as for the copy constructor: it is @@ -149,13 +169,22 @@ class SparseMatrixStruct : public Subscriptor * with at most #max_per_row# * nonzero entries per row. * - * This matrix also allows for - * the first rows to have up to - * #n# entries, if #n_long_rows# - * is different from zero. This - * is useful in the case of - * globally coupling degrees of - * freedom. + * This function simply maps its + * operations to the other + * #reinit# function. + */ + void reinit (const unsigned int m, + const unsigned int n, + const unsigned int max_per_row); + + /** + * Reallocate memory for a matrix + * of size #m \times n#. The + * number of entries for each row + * is taken from the array + * #row_lengths# which has to + * give this number of each row + * #i=1...m#. * * If #m*n==0# all memory is freed, * resulting in a total reinitialization @@ -163,12 +192,12 @@ class SparseMatrixStruct : public Subscriptor * memory is only allocated if the new * size extends the old one. This is done * to save time and to avoid fragmentation - * of the heap. */ - void reinit (const unsigned int m, - const unsigned int n, - const unsigned int max_per_row, - const unsigned int n_long_rows = 0); - + * of the heap. + */ + void reinit (const unsigned int m, + const unsigned int n, + const vector &row_lengths); + /** * This function compresses the sparsity * structure that this object represents. @@ -487,23 +516,20 @@ class SparseMatrixStruct : public Subscriptor /** * Maximum number of elements per - * row. This is set to the value given - * to the #reinit# function (or to the - * constructor). Its value is more - * or less meaningsless after #compress()# - * has been called. + * row. This is set to the value + * given to the #reinit# function + * (or to the constructor), or to + * the maximum row length + * computed from the vectors in + * case the more flexible + * constructors or reinit + * versions are called. Its value + * is more or less meaningsless + * after #compress()# has been + * called. */ - unsigned int max_row_len; + unsigned int max_row_length; - /** - * Number of full rows. This - * value only has a meaning - * before compression and allows - * the first rows to have more - * elements than #max_per_row# - */ - unsigned int n_long_rows; - /** * Array which hold for each row which * is the first element in #colnums# @@ -524,7 +550,8 @@ class SparseMatrixStruct : public Subscriptor * allocated memory may be larger than * the region that is used. The actual * number of elements that was allocated - * is stored in #max_dim#. */ + * is stored in #max_dim#. + */ unsigned int *rowstart; /** diff --git a/deal.II/lac/source/sparse_matrix.cc b/deal.II/lac/source/sparse_matrix.cc index 3669359bbc..134a7b77e9 100644 --- a/deal.II/lac/source/sparse_matrix.cc +++ b/deal.II/lac/source/sparse_matrix.cc @@ -45,14 +45,26 @@ SparseMatrixStruct::SparseMatrixStruct (const SparseMatrixStruct &s) : SparseMatrixStruct::SparseMatrixStruct (const unsigned int m, const unsigned int n, - const unsigned int max_per_row, - const unsigned int long_rows) + const unsigned int max_per_row) : max_dim(0), max_vec_len(0), rowstart(0), colnums(0) { - reinit (m,n,max_per_row,long_rows); + reinit (m,n,max_per_row); +}; + + + +SparseMatrixStruct::SparseMatrixStruct (const unsigned int m, + const unsigned int n, + const vector &row_lengths) + : max_dim(0), + max_vec_len(0), + rowstart(0), + colnums(0) +{ + reinit (m, n, row_lengths); }; @@ -64,7 +76,19 @@ SparseMatrixStruct::SparseMatrixStruct (const unsigned int n, rowstart(0), colnums(0) { - reinit (n,n,max_per_row,0); + reinit (n,n,max_per_row); +}; + + + +SparseMatrixStruct::SparseMatrixStruct (const unsigned int m, + const vector &row_lengths) + : max_dim(0), + max_vec_len(0), + rowstart(0), + colnums(0) +{ + reinit (m, m, row_lengths); }; @@ -186,14 +210,31 @@ SparseMatrixStruct::operator = (const SparseMatrixStruct &s) void SparseMatrixStruct::reinit (const unsigned int m, const unsigned int n, - const unsigned int max_per_row, - const unsigned int long_rows) + const unsigned int max_per_row) { + // simply map this function to the + // other #reinit# function + vector row_lengths (m, max_per_row); + reinit (m, n, row_lengths); +}; + + + +void +SparseMatrixStruct::reinit (const unsigned int m, + const unsigned int n, + const vector &row_lengths) +{ + Assert (((m==0) && (n==0)) || (*max_element(row_lengths.begin(), row_lengths.end()) > 0), + ExcInvalidNumber(*max_element(row_lengths.begin(), row_lengths.end()))); + Assert (row_lengths.size() == m, ExcInvalidNumber (m)); + rows = m; cols = n; - vec_len = m * max_per_row + (n-max_per_row) * long_rows; - max_row_len = max_per_row; - n_long_rows = long_rows; + vec_len = accumulate (row_lengths.begin(), row_lengths.end(), 0); + max_row_length = (row_lengths.size() == 0 ? + 0 : + *max_element(row_lengths.begin(), row_lengths.end())); // delete empty matrices if ((m==0) || (n==0)) @@ -202,34 +243,46 @@ SparseMatrixStruct::reinit (const unsigned int m, if (colnums) delete[] colnums; rowstart = 0; colnums = 0; - max_vec_len = vec_len = max_dim = rows = cols = max_row_len = n_long_rows = 0; + max_vec_len = vec_len = max_dim = rows = cols = 0; + // if dimension is zero: ignore + // max_per_row + max_row_length = 0; compressed = false; return; }; - + + // allocate memory for the rowstart + // values, if necessary if (rows > max_dim) { if (rowstart) delete[] rowstart; max_dim = rows; rowstart = new unsigned int[max_dim+1]; }; - + + // allocate memory for the column + // numbers if necessary if (vec_len > max_vec_len) { if (colnums) delete[] colnums; max_vec_len = vec_len; colnums = new int[max_vec_len]; }; - - unsigned int start = 0; - for (unsigned int i=0; i<=rows; i++) - { - rowstart[i] = start; - start += (i tmp_entries (max_row_length); + // Traverse all rows for (unsigned int line=0; line