From: Wolfgang Bangerth Date: Thu, 7 Mar 2024 21:56:15 +0000 (-0700) Subject: Add tests. X-Git-Tag: v9.6.0-rc1~494^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=28198e2eb8729d179a73dcd2c95a073210d53141;p=dealii.git Add tests. --- diff --git a/tests/trilinos_tpetra/sparse_matrix_set_01.cc b/tests/trilinos_tpetra/sparse_matrix_set_01.cc new file mode 100644 index 0000000000..7b6aaab940 --- /dev/null +++ b/tests/trilinos_tpetra/sparse_matrix_set_01.cc @@ -0,0 +1,133 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// compare collective setting of elements in a trilinos matrix using +// LinearAlgebra::TpetraWrappers::SparseMatrix::set() with element-wise +// setting + +#include + +#include + +#include + +#include "../tests.h" + + +void +test(LinearAlgebra::TpetraWrappers::SparseMatrix &m) +{ + // first set a few entries one-by-one + for (unsigned int i = 0; i < m.m(); ++i) + for (unsigned int j = 0; j < m.n(); ++j) + if ((i + 2 * j + 1) % 3 == 0) + m.set(i, j, i * j * .5 + .5); + + m.compress(VectorOperation::insert); + + LinearAlgebra::TpetraWrappers::SparseMatrix m2(m.m(), + m.n(), + m.n() / 3 + 1); + + // now set the same elements row-wise + { + std::vector col_indices(m.n() / 3 + 1); + std::vector col_values(m.n() / 3 + 1); + for (unsigned int i = 0; i < m.m(); ++i) + { + unsigned int col_index = 0; + // count the number of elements in this + // row + for (unsigned int j = 0; j < m.n(); ++j) + if ((i + 2 * j + 1) % 3 == 0) + ++col_index; + + col_indices.resize(col_index); + col_values.resize(col_index); + col_index = 0; + + // extract column values + for (unsigned int j = 0; j < m.n(); ++j) + if ((i + 2 * j + 1) % 3 == 0) + { + col_indices[col_index] = j; + col_values[col_index] = i * j * .5 + .5; + col_index++; + } + + m2.set(i, col_indices, col_values); + } + } + + m2.compress(VectorOperation::insert); + + // subtract the matrix m from this one, + // we should get a zero matrix. + m2.add(-1.0, m); + // Calculate the Frobenius norm of the + // matrix in order to check whether all + // elements really are zero. + double norm = m2.frobenius_norm(); + AssertThrow(norm == 0, ExcInternalError()); + + deallog << "OK" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + Utilities::MPI::MPI_InitFinalize mpi_initialization( + argc, argv, testing_max_num_threads()); + + try + { + { + LinearAlgebra::TpetraWrappers::SparseMatrix m(5U, 5U, 3U); + test(m); + } + } + catch (const std::exception &exc) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; +} diff --git a/tests/trilinos_tpetra/sparse_matrix_set_01.output b/tests/trilinos_tpetra/sparse_matrix_set_01.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/trilinos_tpetra/sparse_matrix_set_01.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/trilinos_tpetra/sparse_matrix_set_02.cc b/tests/trilinos_tpetra/sparse_matrix_set_02.cc new file mode 100644 index 0000000000..e09c40835f --- /dev/null +++ b/tests/trilinos_tpetra/sparse_matrix_set_02.cc @@ -0,0 +1,131 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// compare collective setting of elements in a trilinos matrix using +// LinearAlgebra::TpetraWrappers::SparseMatrix::set() and a +// FullMatrix with setting the same elements on an element-by-element +// level. Use the entries as they would result from a Laplace operator in 1D. + +#include + +#include +#include + +#include + +#include "../tests.h" + + +void +test(LinearAlgebra::TpetraWrappers::SparseMatrix &m) +{ + LinearAlgebra::TpetraWrappers::SparseMatrix m2(m.m(), m.n(), 3U); + + // first set a few entries one-by-one and + // initialize the sparsity pattern for m2 + for (unsigned int i = 0; i < m.m(); ++i) + for (unsigned int j = 0; j < m.n(); ++j) + if (std::fabs((double)i - j) < 2) + { + double value; + if (i == j) + if (i > 0 && i < m.m() - 1) + value = 2.; + else + value = 1.; + else + value = -1.; + + m.set(i, j, value); + } + m.compress(VectorOperation::insert); + // now add the same elements from a full + // matrix (if the same element is set + // more than once, Trilinos adds them) + { + FullMatrix full_matrix(2, 2); + full_matrix(0, 0) = full_matrix(1, 1) = 1.; + full_matrix(0, 1) = full_matrix(1, 0) = -1.; + std::vector local_indices(2); + + for (unsigned int i = 0; i < m.m() - 1; ++i) + { + local_indices[0] = i; + local_indices[1] = i + 1; + + m2.set(local_indices, local_indices, full_matrix); + } + } + + m2.compress(VectorOperation::insert); + + // subtract the matrix m from this one, + // we should get a zero matrix + m2.add(-1.0, m); + // calculate the Frobenius norm of the + // matrix in order to check whether all + // elements really are zero + double norm = m2.frobenius_norm(); + AssertThrow(norm == 0, ExcInternalError()); + + deallog << "OK" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + Utilities::MPI::MPI_InitFinalize mpi_initialization( + argc, argv, testing_max_num_threads()); + + try + { + { + LinearAlgebra::TpetraWrappers::SparseMatrix m(16U, 16U, 3U); + test(m); + } + } + catch (const std::exception &exc) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; +} diff --git a/tests/trilinos_tpetra/sparse_matrix_set_02.output b/tests/trilinos_tpetra/sparse_matrix_set_02.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/trilinos_tpetra/sparse_matrix_set_02.output @@ -0,0 +1,2 @@ + +DEAL::OK