Skip to content
Snippets Groups Projects
Commit 55645953 authored by Oliver Sander's avatar Oliver Sander
Browse files

Merge branch 'setsize-for-band-matrices' into 'master'

Implement the method 'setSize' for BDMatrix and BTDMatrix

See merge request !110
parents 93505aa0 9c50ab41
No related branches found
No related tags found
1 merge request!110Implement the method 'setSize' for BDMatrix and BTDMatrix
Pipeline #
......@@ -74,6 +74,24 @@ namespace Dune {
(*this)[i][i] = *it;
}
/** \brief Resize the matrix. Invalidates the content! */
void setSize(size_type size)
{
this->BCRSMatrix<B,A>::setSize(size, // rows
size, // columns
size); // nonzeros
for (int i=0; i<size; i++)
this->BCRSMatrix<B,A>::setrowsize(i, 1);
this->BCRSMatrix<B,A>::endrowsizes();
for (int i=0; i<size; i++)
this->BCRSMatrix<B,A>::addindex(i, i);
this->BCRSMatrix<B,A>::endindices();
}
//! assignment
BDMatrix& operator= (const BDMatrix& other) {
this->BCRSMatrix<B,A>::operator=(other);
......
......@@ -49,46 +49,53 @@ namespace Dune {
/** \brief Default constructor */
BTDMatrix() : BCRSMatrix<B,A>() {}
explicit BTDMatrix(int size)
explicit BTDMatrix(size_type size)
: BCRSMatrix<B,A>(size, size, BCRSMatrix<B,A>::random)
{
// special handling for 1x1 matrices
if (size==1) {
this->BCRSMatrix<B,A>::setrowsize(0, 1);
this->BCRSMatrix<B,A>::endrowsizes();
// Set number of entries for each row
// All rows get three entries, except for the first and the last one
for (size_t i=0; i<size; i++)
this->BCRSMatrix<B,A>::setrowsize(i, 3 - (i==0) - (i==(size-1)));
this->BCRSMatrix<B,A>::addindex(0, 0);
this->BCRSMatrix<B,A>::endindices();
this->BCRSMatrix<B,A>::endrowsizes();
return;
// The actual entries for each row
for (size_t i=0; i<size; i++) {
if (i>0)
this->BCRSMatrix<B,A>::addindex(i, i-1);
this->BCRSMatrix<B,A>::addindex(i, i );
if (i<size-1)
this->BCRSMatrix<B,A>::addindex(i, i+1);
}
// Set number of entries for each row
this->BCRSMatrix<B,A>::setrowsize(0, 2);
this->BCRSMatrix<B,A>::endindices();
}
for (int i=1; i<size-1; i++)
this->BCRSMatrix<B,A>::setrowsize(i, 3);
/** \brief Resize the matrix. Invalidates the content! */
void setSize(size_type size)
{
auto nonZeros = (size==0) ? 0 : size + 2*(size-1);
this->BCRSMatrix<B,A>::setSize(size, // rows
size, // columns
nonZeros);
this->BCRSMatrix<B,A>::setrowsize(size-1, 2);
// Set number of entries for each row
// All rows get three entries, except for the first and the last one
for (size_t i=0; i<size; i++)
this->BCRSMatrix<B,A>::setrowsize(i, 3 - (i==0) - (i==(size-1)));
this->BCRSMatrix<B,A>::endrowsizes();
// The actual entries for each row
this->BCRSMatrix<B,A>::addindex(0, 0);
this->BCRSMatrix<B,A>::addindex(0, 1);
for (int i=1; i<size-1; i++) {
this->BCRSMatrix<B,A>::addindex(i, i-1);
for (size_t i=0; i<size; i++) {
if (i>0)
this->BCRSMatrix<B,A>::addindex(i, i-1);
this->BCRSMatrix<B,A>::addindex(i, i );
this->BCRSMatrix<B,A>::addindex(i, i+1);
if (i<size-1)
this->BCRSMatrix<B,A>::addindex(i, i+1);
}
this->BCRSMatrix<B,A>::addindex(size-1, size-2);
this->BCRSMatrix<B,A>::addindex(size-1, size-1);
this->BCRSMatrix<B,A>::endindices();
}
//! assignment
......
......@@ -408,6 +408,11 @@ int main()
BDMatrix<FieldMatrix<double,2,2> > bdMatrix2 = { {{1,0},{0,1}}, {{0,1},{-1,0}}};
testSuperMatrix(bdMatrix2);
// test whether resizing works
bdMatrix2.setSize(5);
bdMatrix2 = 4.0;
testSuperMatrix(bdMatrix);
// ////////////////////////////////////////////////////////////////////////
// Test the BTDMatrix class -- a dynamic block-tridiagonal matrix
// a) the scalar case
......@@ -434,6 +439,11 @@ int main()
btdMatrixScalar_1x1 = 1.0;
testSuperMatrix(btdMatrixScalar_1x1);
// test whether resizing works
btdMatrixScalar_1x1.setSize(5);
btdMatrixScalar_1x1 = 4.0;
testSuperMatrix(btdMatrixScalar_1x1);
// ////////////////////////////////////////////////////////////////////////
// Test the BTDMatrix class -- a dynamic block-tridiagonal matrix
// b) the block-valued case
......@@ -494,6 +504,11 @@ int main()
btdMatrix_1x1 = 1.0;
testSuperMatrix(btdMatrix_1x1);
// test whether resizing works
btdMatrix_1x1.setSize(5);
btdMatrix_1x1 = 4.0;
testSuperMatrix(btdMatrix_1x1);
// ////////////////////////////////////////////////////////////////////////
// Test the FieldMatrix class
// ////////////////////////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment