// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: /** \file \brief Unit tests for the different dynamic matrices provided by ISTL */ #include #include #include #include #include #include #include #include #include using namespace Dune; // forward decls template void testSuperMatrix(MatrixType& matrix); template void testMatrix(MatrixType& matrix, X& x, Y& y); template void testSolve(const MatrixType& matrix); template void testSuperMatrix(MatrixType& matrix) { // //////////////////////////////////////////////////////// // Check the types which are exported by the matrix // //////////////////////////////////////////////////////// typedef typename MatrixType::field_type field_type; typedef typename MatrixType::block_type block_type; typedef typename MatrixType::size_type size_type; using allocator_type [[maybe_unused]] = typename MatrixType::allocator_type; size_type n = matrix.N(); size_type m = matrix.M(); BlockVector > x(m); BlockVector > y(n); testMatrix(matrix, x, y); } template void testMatrix(MatrixType& matrix, X& x, Y& y) { // //////////////////////////////////////////////////////// // Check the types which are exported by the matrix // //////////////////////////////////////////////////////// typedef typename MatrixType::field_type field_type; typedef typename FieldTraits::real_type real_type; using block_type [[maybe_unused]] = typename MatrixType::block_type; using row_type [[maybe_unused]] = typename MatrixType::row_type; typedef typename MatrixType::size_type size_type; using RowIterator [[maybe_unused]] = typename MatrixType::RowIterator; using ConstRowIterator [[maybe_unused]] = typename MatrixType::ConstRowIterator; using ColIterator [[maybe_unused]] = typename MatrixType::ColIterator; using ConstColIterator [[maybe_unused]] = typename MatrixType::ConstColIterator; static_assert(maxBlockLevel() >= 0, "Block level has to be at least 1 for a matrix!"); // //////////////////////////////////////////////////////// // Count number of rows, columns, and nonzero entries // //////////////////////////////////////////////////////// typename MatrixType::RowIterator rowIt = matrix.begin(); typename MatrixType::RowIterator rowEndIt = matrix.end(); typename MatrixType::size_type numRows = 0, numEntries = 0; for (; rowIt!=rowEndIt; ++rowIt) { typename MatrixType::ColIterator colIt = rowIt->begin(); typename MatrixType::ColIterator colEndIt = rowIt->end(); for (; colIt!=colEndIt; ++colIt) { assert(matrix.exists(rowIt.index(), colIt.index())); numEntries++; } numRows++; } assert (numRows == matrix.N()); // /////////////////////////////////////////////////////////////// // Count number of rows, columns, and nonzero entries again. // This time use the const iterators // /////////////////////////////////////////////////////////////// typename MatrixType::ConstRowIterator constRowIt = matrix.begin(); typename MatrixType::ConstRowIterator constRowEndIt = matrix.end(); numRows = 0; numEntries = 0; for (; constRowIt!=constRowEndIt; ++constRowIt) { typename MatrixType::ConstColIterator constColIt = constRowIt->begin(); typename MatrixType::ConstColIterator constColEndIt = constRowIt->end(); for (; constColIt!=constColEndIt; ++constColIt) numEntries++; numRows++; } assert (numRows == matrix.N()); // //////////////////////////////////////////////////////// // Count number of rows, columns, and nonzero entries // This time we're counting backwards // //////////////////////////////////////////////////////// rowIt = matrix.beforeEnd(); rowEndIt = matrix.beforeBegin(); numRows = 0; numEntries = 0; for (; rowIt!=rowEndIt; --rowIt) { typename MatrixType::ColIterator colIt = rowIt->beforeEnd(); typename MatrixType::ColIterator colEndIt = rowIt->beforeBegin(); for (; colIt!=colEndIt; --colIt) { assert(matrix.exists(rowIt.index(), colIt.index())); numEntries++; } numRows++; } assert (numRows == matrix.N()); // /////////////////////////////////////////////////////////////// // Count number of rows, columns, and nonzero entries again. // This time use the const iterators and count backwards. // /////////////////////////////////////////////////////////////// constRowIt = matrix.beforeEnd(); constRowEndIt = matrix.beforeBegin(); numRows = 0; numEntries = 0; for (; constRowIt!=constRowEndIt; --constRowIt) { typename MatrixType::ConstColIterator constColIt = constRowIt->beforeEnd(); typename MatrixType::ConstColIterator constColEndIt = constRowIt->beforeBegin(); for (; constColIt!=constColEndIt; --constColIt) numEntries++; numRows++; } assert (numRows == matrix.N()); // /////////////////////////////////////////////////////// // More dimension stuff // /////////////////////////////////////////////////////// size_type n = matrix.N(); ++n; size_type m = matrix.M(); ++m; // /////////////////////////////////////////////////////// // Test assignment operators and the copy constructor // /////////////////////////////////////////////////////// // assignment from other matrix MatrixType secondMatrix; secondMatrix = matrix; // assignment from scalar matrix = 0; // The copy constructor [[maybe_unused]] MatrixType thirdMatrix(matrix); // /////////////////////////////////////////////////////// // Test component-wise operations // /////////////////////////////////////////////////////// matrix *= real_type(1.0); matrix /= real_type(1.0); matrix += secondMatrix; matrix -= secondMatrix; // /////////////////////////////////////////////////////////// // Test the various matrix-vector multiplications // /////////////////////////////////////////////////////////// Y yy=y; matrix.mv(x,yy); matrix.mtv(x,yy); matrix.umv(x,y); matrix.umtv(x,y); matrix.umhv(x,y); matrix.mmv(x,y); matrix.mmtv(x,y); matrix.mmhv(x,y); matrix.usmv(field_type(1.0),x,y); matrix.usmtv(field_type(1.0),x,y); matrix.usmhv(field_type(1.0),x,y); // ////////////////////////////////////////////////////////////// // Test the matrix norms // ////////////////////////////////////////////////////////////// real_type frobenius_norm = matrix.frobenius_norm(); frobenius_norm += matrix.frobenius_norm2(); frobenius_norm += matrix.infinity_norm(); frobenius_norm += matrix.infinity_norm_real(); } // /////////////////////////////////////////////////////////////////// // Test the solve()-method for those matrix classes that have it // /////////////////////////////////////////////////////////////////// template void testSolve(const MatrixType& matrix) { typedef typename VectorType::size_type size_type; // create some right hand side VectorType b(matrix.N()); for (size_type i=0; i 1e-10) DUNE_THROW(ISTLError, "Solve() method doesn't appear to produce the solution!"); } // ////////////////////////////////////////////////////////////// // Test transposing the matrix // ////////////////////////////////////////////////////////////// template void testTranspose(const MatrixType& matrix) { MatrixType transposedMatrix = matrix.transpose(); for(size_t i = 0; i < matrix.N(); i++) for(size_t j = 0; j < matrix.M(); j++) if(fabs(transposedMatrix[j][i] - matrix[i][j]) > 1e-10) DUNE_THROW(ISTLError, "transpose() method produces wrong result!"); } int main() { #if defined( __APPLE__ ) or defined( __MINGW32__ ) or defined(_MSC_VER) feraiseexcept(FE_INVALID); #else feenableexcept(FE_INVALID); #endif // //////////////////////////////////////////////////////////// // Test the Matrix class -- a scalar dense dynamic matrix // //////////////////////////////////////////////////////////// { Matrix matrixScalar(10,10); for (int i=0; i<10; i++) for (int j=0; j<10; j++) matrixScalar[i][j] = (i+j)/((double)(i*j+1)); // just anything BlockVector x(10), y(10); testMatrix(matrixScalar, x, y); } // //////////////////////////////////////////////////////////// // Test the Matrix class -- a block-valued dense dynamic matrix // //////////////////////////////////////////////////////////// Matrix > matrix(10,10); for (int i=0; i<10; i++) for (int j=0; j<10; j++) for (int k=0; k<3; k++) for (int l=0; l<3; l++) matrix[i][j][k][l] = (i+j)/((double)(k*l+1)); // just anything testSuperMatrix(matrix); Matrix > nonquadraticMatrix(1,2); { size_t n = 1; for (size_t i=0; i<1; i++) for (size_t j=0; j<2; j++) nonquadraticMatrix[i][j] = n++; } testTranspose(nonquadraticMatrix); // //////////////////////////////////////////////////////////// // Test the BCRSMatrix class -- a sparse dynamic matrix // //////////////////////////////////////////////////////////// { BCRSMatrix bcrsMatrix(4,4, BCRSMatrix::random); bcrsMatrix.setrowsize(0,2); bcrsMatrix.setrowsize(1,3); bcrsMatrix.setrowsize(2,3); bcrsMatrix.setrowsize(3,2); bcrsMatrix.endrowsizes(); bcrsMatrix.addindex(0, 0); bcrsMatrix.addindex(0, 1); bcrsMatrix.addindex(1, 0); bcrsMatrix.addindex(1, 1); bcrsMatrix.addindex(1, 2); bcrsMatrix.addindex(2, 1); bcrsMatrix.addindex(2, 2); bcrsMatrix.addindex(2, 3); bcrsMatrix.addindex(3, 2); bcrsMatrix.addindex(3, 3); bcrsMatrix.endindices(); typedef BCRSMatrix::RowIterator RowIterator; typedef BCRSMatrix::ColIterator ColIterator; for(RowIterator row = bcrsMatrix.begin(); row != bcrsMatrix.end(); ++row) for(ColIterator col = row->begin(); col != row->end(); ++col) *col = 1.0 + (double) row.index() * (double) col.index(); BlockVector x(4), y(4); testMatrix(bcrsMatrix, x, y); // Test whether matrix resizing works int size = 3; bcrsMatrix.setSize(size,size,size); for (int i=0; i > bcrsMatrix(4,4, BCRSMatrix >::random); bcrsMatrix.setrowsize(0,2); bcrsMatrix.setrowsize(1,3); bcrsMatrix.setrowsize(2,3); bcrsMatrix.setrowsize(3,2); bcrsMatrix.endrowsizes(); bcrsMatrix.addindex(0, 0); bcrsMatrix.addindex(0, 1); bcrsMatrix.addindex(1, 0); bcrsMatrix.addindex(1, 1); bcrsMatrix.addindex(1, 2); bcrsMatrix.addindex(2, 1); bcrsMatrix.addindex(2, 2); bcrsMatrix.addindex(2, 3); bcrsMatrix.addindex(3, 2); bcrsMatrix.addindex(3, 3); bcrsMatrix.endindices(); typedef BCRSMatrix >::RowIterator RowIterator; typedef BCRSMatrix >::ColIterator ColIterator; for(RowIterator row = bcrsMatrix.begin(); row != bcrsMatrix.end(); ++row) for(ColIterator col = row->begin(); col != row->end(); ++col) *col = 1.0 + (double) row.index() * (double) col.index(); testSuperMatrix(bcrsMatrix); // Test whether matrix resizing works int size = 3; bcrsMatrix.setSize(size,size,size); for (int i=0; i bdMatrix(3); bdMatrix = 4.0; BlockVector x(3), y(3); testMatrix(bdMatrix, x, y); // Test construction from initializer list BDMatrix bdMatrix2 = {1.0, 2.0, 3.0}; testMatrix(bdMatrix2, x, y); // Test equation-solving BDMatrix bdMatrix3 = {1.0, 2.0, 3.0}; testSolve, BlockVector >(bdMatrix3); // Test whether inversion works bdMatrix3.invert(); // test whether resizing works bdMatrix2.setSize(5); bdMatrix2 = 4.0; x.resize(5); y.resize(5); testMatrix(bdMatrix2, x, y); } // //////////////////////////////////////////////////////////////////////// // Test the BDMatrix class with FieldMatrix entries // //////////////////////////////////////////////////////////////////////// BDMatrix > bdMatrix(2); bdMatrix = 4.0; testSuperMatrix(bdMatrix); // Test construction from initializer list BDMatrix > bdMatrix2 = { {{1,0},{0,1}}, {{0,1},{-1,0}}}; // Test equation-solving testSolve >, BlockVector > >(bdMatrix2); // Test whether inversion works bdMatrix2.invert(); // Run matrix tests on this matrix testSuperMatrix(bdMatrix2); // test whether resizing works bdMatrix2.setSize(5); bdMatrix2 = 4.0; testSuperMatrix(bdMatrix2); // //////////////////////////////////////////////////////////////////////// // Test the BTDMatrix class -- a dynamic block-tridiagonal matrix // a) the scalar case // //////////////////////////////////////////////////////////////////////// { BTDMatrix btdMatrixScalar(4); using size_type = BTDMatrix::size_type; btdMatrixScalar = 4.0; BlockVector x(4), y(4); testMatrix(btdMatrixScalar, x, y); btdMatrixScalar = 0.0; for (size_type i=0; i, BlockVector >(btdMatrixScalar); // test a 1x1 BTDMatrix, because that is a special case BTDMatrix btdMatrixScalar_1x1(1); btdMatrixScalar_1x1 = 1.0; x.resize(1); y.resize(1); testMatrix(btdMatrixScalar_1x1, x, y); // test whether resizing works btdMatrixScalar_1x1.setSize(5); btdMatrixScalar_1x1 = 4.0; x.resize(5); y.resize(5); testMatrix(btdMatrixScalar_1x1, x, y); } /////////////////////////////////////////////////////////////////////////// // Test the BTDMatrix class -- a dynamic block-tridiagonal matrix // b) the scalar case with FieldMatrix entries /////////////////////////////////////////////////////////////////////////// BTDMatrix > btdMatrixScalar(4); typedef BTDMatrix >::size_type size_type; btdMatrixScalar = 4.0; testSuperMatrix(btdMatrixScalar); btdMatrixScalar = 0.0; for (size_type i=0; i >, BlockVector > >(btdMatrixScalar); // test a 1x1 BTDMatrix, because that is a special case BTDMatrix > btdMatrixScalar_1x1(1); 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 // c) the block-valued case // //////////////////////////////////////////////////////////////////////// BTDMatrix > btdMatrix(4); typedef BTDMatrix >::size_type size_type; btdMatrix = 0.0; for (size_type i=0; i(1+i); for (size_type i=0; i(2+i); // upper off-diagonal for (size_type i=1; i(2+i); // lower off-diagonal // add some off diagonal stuff to the blocks in the matrix // diagonals btdMatrix[0][0][0][1] = 2; btdMatrix[0][0][1][0] = -1; btdMatrix[1][1][0][1] = 2; btdMatrix[1][1][1][0] = 3; btdMatrix[2][2][0][1] = 2; btdMatrix[2][2][0][0] += sqrt(2.); btdMatrix[2][2][1][0] = 3; btdMatrix[3][3][0][1] = -1; btdMatrix[3][3][0][0] -= 0.5; btdMatrix[3][3][1][0] = 2; // off diagonals btdMatrix[0][1][0][1] = std::sqrt(2); btdMatrix[1][0][0][1] = std::sqrt(2); btdMatrix[1][0][1][0] = -13./17.; btdMatrix[1][2][0][1] = -1./std::sqrt(2); btdMatrix[1][2][1][0] = -13./17.; btdMatrix[2][1][0][1] = -13./17.; btdMatrix[2][1][1][0] = -13./17.; btdMatrix[2][3][0][1] = -1./std::sqrt(2); btdMatrix[2][3][1][0] = -17.; btdMatrix[3][2][0][1] = 1.; btdMatrix[3][2][1][0] = 1.; BTDMatrix > btdMatrixThrowAway = btdMatrix; // the test method overwrites the matrix testSuperMatrix(btdMatrixThrowAway); testSolve >, BlockVector > >(btdMatrix); // test a 1x1 BTDMatrix, because that is a special case BTDMatrix > btdMatrix_1x1(1); 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 // //////////////////////////////////////////////////////////////////////// typedef FieldMatrix::size_type size_type; FieldMatrix fMatrix; for (size_type i=0; i fvX; FieldVector fvY; testMatrix(fMatrix, fvX, fvY); // //////////////////////////////////////////////////////////////////////// // Test the 1x1 specialization of the FieldMatrix class // //////////////////////////////////////////////////////////////////////// FieldMatrix fMatrix1x1; fMatrix1x1[0][0] = 2.3; // just anything FieldVector fvX1; FieldVector fvY1; testMatrix(fMatrix1x1, fvX1, fvY1); // //////////////////////////////////////////////////////////////////////// // Test the DiagonalMatrix class // //////////////////////////////////////////////////////////////////////// FieldVector dMatrixConstructFrom; dMatrixConstructFrom = 3.1459; DiagonalMatrix dMatrix1; dMatrix1 = 3.1459; testMatrix(dMatrix1, fvX, fvY); DiagonalMatrix dMatrix2(3.1459); testMatrix(dMatrix2, fvX, fvY); DiagonalMatrix dMatrix3(dMatrixConstructFrom); testMatrix(dMatrix3, fvX, fvY); // //////////////////////////////////////////////////////////////////////// // Test the ScaledIdentityMatrix class // //////////////////////////////////////////////////////////////////////// ScaledIdentityMatrix sIdMatrix; sIdMatrix = 3.1459; testMatrix(sIdMatrix, fvX, fvY); }